FuncletLayout.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. using namespace llvm;
  18. #define DEBUG_TYPE "funclet-layout"
  19. namespace {
  20. class FuncletLayout : public MachineFunctionPass {
  21. public:
  22. static char ID; // Pass identification, replacement for typeid
  23. FuncletLayout() : MachineFunctionPass(ID) {
  24. initializeFuncletLayoutPass(*PassRegistry::getPassRegistry());
  25. }
  26. bool runOnMachineFunction(MachineFunction &F) override;
  27. MachineFunctionProperties getRequiredProperties() const override {
  28. return MachineFunctionProperties().set(
  29. MachineFunctionProperties::Property::NoVRegs);
  30. }
  31. };
  32. }
  33. char FuncletLayout::ID = 0;
  34. char &llvm::FuncletLayoutID = FuncletLayout::ID;
  35. INITIALIZE_PASS(FuncletLayout, DEBUG_TYPE,
  36. "Contiguously Lay Out Funclets", false, false)
  37. bool FuncletLayout::runOnMachineFunction(MachineFunction &F) {
  38. // Even though this gets information from getEHScopeMembership(), this pass is
  39. // only necessary for funclet-based EH personalities, in which these EH scopes
  40. // are outlined at the end.
  41. DenseMap<const MachineBasicBlock *, int> FuncletMembership =
  42. getEHScopeMembership(F);
  43. if (FuncletMembership.empty())
  44. return false;
  45. F.sort([&](MachineBasicBlock &X, MachineBasicBlock &Y) {
  46. auto FuncletX = FuncletMembership.find(&X);
  47. auto FuncletY = FuncletMembership.find(&Y);
  48. assert(FuncletX != FuncletMembership.end());
  49. assert(FuncletY != FuncletMembership.end());
  50. return FuncletX->second < FuncletY->second;
  51. });
  52. // Conservatively assume we changed something.
  53. return true;
  54. }