FuncletLayout.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //===-- FuncletLayout.cpp - Contiguously lay out funclets -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements basic block placement transformations which result in
  10. // funclets being contiguous.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/Analysis.h"
  14. #include "llvm/CodeGen/MachineFunction.h"
  15. #include "llvm/CodeGen/MachineFunctionPass.h"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/InitializePasses.h"
  18. using namespace llvm;
  19. #define DEBUG_TYPE "funclet-layout"
  20. namespace {
  21. class FuncletLayout : public MachineFunctionPass {
  22. public:
  23. static char ID; // Pass identification, replacement for typeid
  24. FuncletLayout() : MachineFunctionPass(ID) {
  25. initializeFuncletLayoutPass(*PassRegistry::getPassRegistry());
  26. }
  27. bool runOnMachineFunction(MachineFunction &F) override;
  28. MachineFunctionProperties getRequiredProperties() const override {
  29. return MachineFunctionProperties().set(
  30. MachineFunctionProperties::Property::NoVRegs);
  31. }
  32. };
  33. }
  34. char FuncletLayout::ID = 0;
  35. char &llvm::FuncletLayoutID = FuncletLayout::ID;
  36. INITIALIZE_PASS(FuncletLayout, DEBUG_TYPE,
  37. "Contiguously Lay Out Funclets", false, false)
  38. bool FuncletLayout::runOnMachineFunction(MachineFunction &F) {
  39. // Even though this gets information from getEHScopeMembership(), this pass is
  40. // only necessary for funclet-based EH personalities, in which these EH scopes
  41. // are outlined at the end.
  42. DenseMap<const MachineBasicBlock *, int> FuncletMembership =
  43. getEHScopeMembership(F);
  44. if (FuncletMembership.empty())
  45. return false;
  46. F.sort([&](MachineBasicBlock &X, MachineBasicBlock &Y) {
  47. auto FuncletX = FuncletMembership.find(&X);
  48. auto FuncletY = FuncletMembership.find(&Y);
  49. assert(FuncletX != FuncletMembership.end());
  50. assert(FuncletY != FuncletMembership.end());
  51. return FuncletX->second < FuncletY->second;
  52. });
  53. // Conservatively assume we changed something.
  54. return true;
  55. }