Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 1 | //===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements flattening of CFG. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 15 | #include "llvm/IR/CFG.h" |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 17 | #include "llvm/Transforms/Scalar.h" |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/Local.h" |
| 19 | using namespace llvm; |
| 20 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "flattencfg" |
| 22 | |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | struct FlattenCFGPass : public FunctionPass { |
| 25 | static char ID; // Pass identification, replacement for typeid |
| 26 | public: |
| 27 | FlattenCFGPass() : FunctionPass(ID) { |
| 28 | initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry()); |
| 29 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 30 | bool runOnFunction(Function &F) override; |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 31 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 32 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 33 | AU.addRequired<AAResultsWrapperPass>(); |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | private: |
| 37 | AliasAnalysis *AA; |
| 38 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 39 | } |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 40 | |
| 41 | char FlattenCFGPass::ID = 0; |
| 42 | INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false, |
| 43 | false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 44 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 45 | INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false, |
| 46 | false) |
| 47 | |
| 48 | // Public interface to the FlattenCFG pass |
| 49 | FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); } |
| 50 | |
| 51 | /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function, |
| 52 | /// iterating until no more changes are made. |
| 53 | static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) { |
| 54 | bool Changed = false; |
| 55 | bool LocalChange = true; |
| 56 | while (LocalChange) { |
| 57 | LocalChange = false; |
| 58 | |
| 59 | // Loop over all of the basic blocks and remove them if they are unneeded... |
| 60 | // |
| 61 | for (Function::iterator BBIt = F.begin(); BBIt != F.end();) { |
Duncan P. N. Exon Smith | 3a9c9e3 | 2015-10-13 18:26:00 +0000 | [diff] [blame] | 62 | if (FlattenCFG(&*BBIt++, AA)) { |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 63 | LocalChange = true; |
| 64 | } |
| 65 | } |
| 66 | Changed |= LocalChange; |
| 67 | } |
| 68 | return Changed; |
| 69 | } |
| 70 | |
| 71 | bool FlattenCFGPass::runOnFunction(Function &F) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 72 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 73 | bool EverChanged = false; |
| 74 | // iterativelyFlattenCFG can make some blocks dead. |
| 75 | while (iterativelyFlattenCFG(F, AA)) { |
| 76 | removeUnreachableBlocks(F); |
| 77 | EverChanged = true; |
| 78 | } |
| 79 | return EverChanged; |
| 80 | } |