1 //===-- FuncletLayout.cpp - Contiguously lay out funclets -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements basic block placement transformations which result in
10 // funclets being contiguous.
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"
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
;
31 MachineFunctionProperties
getRequiredProperties() const override
{
32 return MachineFunctionProperties().set(
33 MachineFunctionProperties::Property::NoVRegs
);
38 char FuncletLayout::ID
= 0;
39 char &llvm::FuncletLayoutID
= FuncletLayout::ID
;
40 INITIALIZE_PASS(FuncletLayout
, DEBUG_TYPE
,
41 "Contiguously Lay Out Funclets", false, false)
43 bool FuncletLayout::runOnMachineFunction(MachineFunction
&F
) {
44 // Even though this gets information from getEHScopeMembership(), this pass is
45 // only necessary for funclet-based EH personalities, in which these EH scopes
46 // are outlined at the end.
47 DenseMap
<const MachineBasicBlock
*, int> FuncletMembership
=
48 getEHScopeMembership(F
);
49 if (FuncletMembership
.empty())
52 F
.sort([&](MachineBasicBlock
&X
, MachineBasicBlock
&Y
) {
53 auto FuncletX
= FuncletMembership
.find(&X
);
54 auto FuncletY
= FuncletMembership
.find(&Y
);
55 assert(FuncletX
!= FuncletMembership
.end());
56 assert(FuncletY
!= FuncletMembership
.end());
57 return FuncletX
->second
< FuncletY
->second
;
60 // Conservatively assume we changed something.