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