10b57cec5SDimitry Andric //===-- FuncletLayout.cpp - Contiguously lay out funclets -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements basic block placement transformations which result in
100b57cec5SDimitry Andric // funclets being contiguous.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
17*480093f4SDimitry Andric #include "llvm/InitializePasses.h"
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric #define DEBUG_TYPE "funclet-layout"
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric namespace {
230b57cec5SDimitry Andric class FuncletLayout : public MachineFunctionPass {
240b57cec5SDimitry Andric public:
250b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
FuncletLayout()260b57cec5SDimitry Andric FuncletLayout() : MachineFunctionPass(ID) {
270b57cec5SDimitry Andric initializeFuncletLayoutPass(*PassRegistry::getPassRegistry());
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &F) override;
getRequiredProperties() const310b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
320b57cec5SDimitry Andric return MachineFunctionProperties().set(
330b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric };
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric char FuncletLayout::ID = 0;
390b57cec5SDimitry Andric char &llvm::FuncletLayoutID = FuncletLayout::ID;
400b57cec5SDimitry Andric INITIALIZE_PASS(FuncletLayout, DEBUG_TYPE,
410b57cec5SDimitry Andric "Contiguously Lay Out Funclets", false, false)
420b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & F)430b57cec5SDimitry Andric bool FuncletLayout::runOnMachineFunction(MachineFunction &F) {
440b57cec5SDimitry Andric // Even though this gets information from getEHScopeMembership(), this pass is
450b57cec5SDimitry Andric // only necessary for funclet-based EH personalities, in which these EH scopes
460b57cec5SDimitry Andric // are outlined at the end.
470b57cec5SDimitry Andric DenseMap<const MachineBasicBlock *, int> FuncletMembership =
480b57cec5SDimitry Andric getEHScopeMembership(F);
490b57cec5SDimitry Andric if (FuncletMembership.empty())
500b57cec5SDimitry Andric return false;
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric F.sort([&](MachineBasicBlock &X, MachineBasicBlock &Y) {
530b57cec5SDimitry Andric auto FuncletX = FuncletMembership.find(&X);
540b57cec5SDimitry Andric auto FuncletY = FuncletMembership.find(&Y);
550b57cec5SDimitry Andric assert(FuncletX != FuncletMembership.end());
560b57cec5SDimitry Andric assert(FuncletY != FuncletMembership.end());
570b57cec5SDimitry Andric return FuncletX->second < FuncletY->second;
580b57cec5SDimitry Andric });
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric // Conservatively assume we changed something.
610b57cec5SDimitry Andric return true;
620b57cec5SDimitry Andric }
63