Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 1 | //===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===// |
| 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 |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements flattening of CFG. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 14 | #include "llvm/IR/CFG.h" |
Jakub Kuderski | e6b2164 | 2019-09-19 19:39:42 +0000 | [diff] [blame] | 15 | #include "llvm/IR/ValueHandle.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 16 | #include "llvm/InitializePasses.h" |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Scalar.h" |
Jakub Kuderski | e6b2164 | 2019-09-19 19:39:42 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
| 20 | |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "flattencfg" |
| 24 | |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | struct FlattenCFGPass : public FunctionPass { |
| 27 | static char ID; // Pass identification, replacement for typeid |
| 28 | public: |
| 29 | FlattenCFGPass() : FunctionPass(ID) { |
| 30 | initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry()); |
| 31 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 32 | bool runOnFunction(Function &F) override; |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 33 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 34 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 35 | AU.addRequired<AAResultsWrapperPass>(); |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | private: |
| 39 | AliasAnalysis *AA; |
| 40 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 41 | } |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 42 | |
| 43 | char FlattenCFGPass::ID = 0; |
| 44 | INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false, |
| 45 | false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 47 | INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false, |
| 48 | false) |
| 49 | |
| 50 | // Public interface to the FlattenCFG pass |
| 51 | FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); } |
| 52 | |
| 53 | /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function, |
| 54 | /// iterating until no more changes are made. |
| 55 | static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) { |
| 56 | bool Changed = false; |
| 57 | bool LocalChange = true; |
Jakub Kuderski | e6b2164 | 2019-09-19 19:39:42 +0000 | [diff] [blame] | 58 | |
| 59 | // Use block handles instead of iterating over function blocks directly |
| 60 | // to avoid using iterators invalidated by erasing blocks. |
| 61 | std::vector<WeakVH> Blocks; |
| 62 | Blocks.reserve(F.size()); |
| 63 | for (auto &BB : F) |
| 64 | Blocks.push_back(&BB); |
| 65 | |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 66 | while (LocalChange) { |
| 67 | LocalChange = false; |
| 68 | |
Jakub Kuderski | e6b2164 | 2019-09-19 19:39:42 +0000 | [diff] [blame] | 69 | // Loop over all of the basic blocks and try to flatten them. |
| 70 | for (WeakVH &BlockHandle : Blocks) { |
| 71 | // Skip blocks erased by FlattenCFG. |
| 72 | if (auto *BB = cast_or_null<BasicBlock>(BlockHandle)) |
| 73 | if (FlattenCFG(BB, AA)) |
| 74 | LocalChange = true; |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 75 | } |
| 76 | Changed |= LocalChange; |
| 77 | } |
| 78 | return Changed; |
| 79 | } |
| 80 | |
| 81 | bool FlattenCFGPass::runOnFunction(Function &F) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 82 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 83 | bool EverChanged = false; |
| 84 | // iterativelyFlattenCFG can make some blocks dead. |
| 85 | while (iterativelyFlattenCFG(F, AA)) { |
| 86 | removeUnreachableBlocks(F); |
| 87 | EverChanged = true; |
| 88 | } |
| 89 | return EverChanged; |
| 90 | } |