1 //===-- FuncletLayout.cpp - Contiguously lay out funclets -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements basic block placement transformations which result in
11 // funclets being contiguous.
13 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/CodeGen/Analysis.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #define DEBUG_TYPE "funclet-layout"
23 class FuncletLayout : public MachineFunctionPass {
25 static char ID; // Pass identification, replacement for typeid
26 FuncletLayout() : MachineFunctionPass(ID) {
27 initializeFuncletLayoutPass(*PassRegistry::getPassRegistry());
30 bool runOnMachineFunction(MachineFunction &F) override;
34 char FuncletLayout::ID = 0;
35 char &llvm::FuncletLayoutID = FuncletLayout::ID;
36 INITIALIZE_PASS(FuncletLayout, "funclet-layout",
37 "Contiguously Lay Out Funclets", false, false)
39 bool FuncletLayout::runOnMachineFunction(MachineFunction &F) {
40 DenseMap<const MachineBasicBlock *, int> FuncletMembership =
41 getFuncletMembership(F);
42 if (FuncletMembership.empty())
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;
53 // Conservatively assume we changed something.