David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 1 | //===-- FuncletLayout.cpp - Contiguously lay out funclets -----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements basic block placement transformations which result in |
| 10 | // funclets being contiguous. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/Analysis.h" |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunction.h" |
| 15 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | #define DEBUG_TYPE "funclet-layout" |
| 20 | |
| 21 | namespace { |
| 22 | class FuncletLayout : public MachineFunctionPass { |
| 23 | public: |
| 24 | static char ID; // Pass identification, replacement for typeid |
| 25 | FuncletLayout() : MachineFunctionPass(ID) { |
| 26 | initializeFuncletLayoutPass(*PassRegistry::getPassRegistry()); |
| 27 | } |
| 28 | |
| 29 | bool runOnMachineFunction(MachineFunction &F) override; |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 30 | MachineFunctionProperties getRequiredProperties() const override { |
| 31 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 32 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 33 | } |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 34 | }; |
| 35 | } |
| 36 | |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 37 | char FuncletLayout::ID = 0; |
| 38 | char &llvm::FuncletLayoutID = FuncletLayout::ID; |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 39 | INITIALIZE_PASS(FuncletLayout, DEBUG_TYPE, |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 40 | "Contiguously Lay Out Funclets", false, false) |
| 41 | |
| 42 | bool FuncletLayout::runOnMachineFunction(MachineFunction &F) { |
Heejin Ahn | d69acf3 | 2018-06-01 00:03:21 +0000 | [diff] [blame] | 43 | // Even though this gets information from getEHScopeMembership(), this pass is |
| 44 | // only necessary for funclet-based EH personalities, in which these EH scopes |
| 45 | // are outlined at the end. |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 46 | DenseMap<const MachineBasicBlock *, int> FuncletMembership = |
Heejin Ahn | 1e4d350 | 2018-05-23 00:32:46 +0000 | [diff] [blame] | 47 | getEHScopeMembership(F); |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 48 | if (FuncletMembership.empty()) |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 49 | return false; |
| 50 | |
David Majnemer | e4f9b09 | 2015-10-05 20:09:16 +0000 | [diff] [blame] | 51 | F.sort([&](MachineBasicBlock &X, MachineBasicBlock &Y) { |
| 52 | auto FuncletX = FuncletMembership.find(&X); |
| 53 | auto FuncletY = FuncletMembership.find(&Y); |
| 54 | assert(FuncletX != FuncletMembership.end()); |
| 55 | assert(FuncletY != FuncletMembership.end()); |
| 56 | return FuncletX->second < FuncletY->second; |
David Majnemer | 9966fe8 | 2015-09-18 08:18:07 +0000 | [diff] [blame] | 57 | }); |
David Majnemer | 9789023 | 2015-09-17 20:45:18 +0000 | [diff] [blame] | 58 | |
| 59 | // Conservatively assume we changed something. |
| 60 | return true; |
| 61 | } |