1 //===-- FuncletLayout.cpp - Contiguously lay out funclets -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements basic block placement transformations which result in 11 // funclets being contiguous. 12 // 13 //===----------------------------------------------------------------------===// 14 #include "llvm/CodeGen/Passes.h" 15 #include "llvm/CodeGen/MachineBasicBlock.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/CodeGen/MachineFunctionPass.h" 18 #include "llvm/CodeGen/MachineModuleInfo.h" 19 #include "llvm/Target/TargetInstrInfo.h" 20 #include "llvm/Target/TargetSubtargetInfo.h" 21 using namespace llvm; 22 23 #define DEBUG_TYPE "funclet-layout" 24 25 namespace { 26 class FuncletLayout : public MachineFunctionPass { 27 public: 28 static char ID; // Pass identification, replacement for typeid 29 FuncletLayout() : MachineFunctionPass(ID) { 30 initializeFuncletLayoutPass(*PassRegistry::getPassRegistry()); 31 } 32 33 bool runOnMachineFunction(MachineFunction &F) override; 34 }; 35 } 36 37 static void 38 collectFuncletMembers(DenseMap<MachineBasicBlock *, int> &FuncletMembership, 39 int Funclet, MachineBasicBlock *MBB) { 40 // Don't revisit blocks. 41 if (FuncletMembership.count(MBB) > 0) { 42 if (FuncletMembership[MBB] != Funclet) { 43 assert(false && "MBB is part of two funclets!"); 44 report_fatal_error("MBB is part of two funclets!"); 45 } 46 return; 47 } 48 49 // Add this MBB to our funclet. 50 FuncletMembership[MBB] = Funclet; 51 52 bool IsReturn = false; 53 int NumTerminators = 0; 54 for (MachineInstr &MI : MBB->terminators()) { 55 IsReturn |= MI.isReturn(); 56 ++NumTerminators; 57 } 58 assert((!IsReturn || NumTerminators == 1) && 59 "Expected only one terminator when a return is present!"); 60 61 // Returns are boundaries where funclet transfer can occur, don't follow 62 // successors. 63 if (IsReturn) 64 return; 65 66 for (MachineBasicBlock *SMBB : MBB->successors()) 67 if (!SMBB->isEHPad()) 68 collectFuncletMembers(FuncletMembership, Funclet, SMBB); 69 } 70 71 char FuncletLayout::ID = 0; 72 char &llvm::FuncletLayoutID = FuncletLayout::ID; 73 INITIALIZE_PASS(FuncletLayout, "funclet-layout", 74 "Contiguously Lay Out Funclets", false, false) 75 76 bool FuncletLayout::runOnMachineFunction(MachineFunction &F) { 77 // We don't have anything to do if there aren't any EH pads. 78 if (!F.getMMI().hasEHFunclets()) 79 return false; 80 81 const TargetInstrInfo *TII = F.getSubtarget().getInstrInfo(); 82 SmallVector<MachineBasicBlock *, 16> FuncletBlocks; 83 SmallVector<std::pair<MachineBasicBlock *, int>, 16> CatchRetSuccessors; 84 for (MachineBasicBlock &MBB : F) { 85 if (MBB.isEHFuncletEntry()) 86 FuncletBlocks.push_back(&MBB); 87 88 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); 89 if (MBBI->getOpcode() != TII->getCatchReturnOpcode()) 90 continue; 91 92 MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB(); 93 MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB(); 94 CatchRetSuccessors.push_back({Successor, SuccessorColor->getNumber()}); 95 } 96 97 // We don't have anything to do if there aren't any EH pads. 98 if (FuncletBlocks.empty()) 99 return false; 100 101 DenseMap<MachineBasicBlock *, int> FuncletMembership; 102 // Identify all the basic blocks reachable from the function entry. 103 collectFuncletMembers(FuncletMembership, F.front().getNumber(), F.begin()); 104 // Next, identify all the blocks inside the funclets. 105 for (MachineBasicBlock *MBB : FuncletBlocks) 106 collectFuncletMembers(FuncletMembership, MBB->getNumber(), MBB); 107 // Finally, identify all the targets of a catchret. 108 for (std::pair<MachineBasicBlock *, int> CatchRetPair : CatchRetSuccessors) 109 collectFuncletMembers(FuncletMembership, CatchRetPair.second, 110 CatchRetPair.first); 111 112 F.sort([&](MachineBasicBlock &x, MachineBasicBlock &y) { 113 return FuncletMembership[&x] < FuncletMembership[&y]; 114 }); 115 116 // Conservatively assume we changed something. 117 return true; 118 } 119