Chris Lattner | e15051d | 2008-05-14 20:38:44 +0000 | [diff] [blame] | 1 | //===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 19970e6 | 2007-12-03 19:43:18 +0000 | [diff] [blame] | 10 | // This file implements dead code elimination and basic block merging, along |
| 11 | // with a collection of other peephole control flow optimizations. For example: |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 12 | // |
Gordon Henriksen | d568767 | 2007-11-04 16:15:04 +0000 | [diff] [blame] | 13 | // * Removes basic blocks with no predecessors. |
| 14 | // * Merges a basic block into its predecessor if there is only one and the |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 15 | // predecessor only has one successor. |
Gordon Henriksen | d568767 | 2007-11-04 16:15:04 +0000 | [diff] [blame] | 16 | // * Eliminates PHI nodes for basic blocks with a single predecessor. |
| 17 | // * Eliminates a basic block that only contains an unconditional branch. |
Chris Lattner | 19970e6 | 2007-12-03 19:43:18 +0000 | [diff] [blame] | 18 | // * Changes invoke instructions to nounwind functions to be calls. |
| 19 | // * Change things like "if (x) if (y)" into "if (x&y)". |
| 20 | // * etc.. |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame^] | 24 | #include "llvm/Transforms/Scalar/SimplifyCFG.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Constants.h" |
| 33 | #include "llvm/IR/DataLayout.h" |
| 34 | #include "llvm/IR/Instructions.h" |
| 35 | #include "llvm/IR/IntrinsicInst.h" |
| 36 | #include "llvm/IR/Module.h" |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 37 | #include "llvm/Pass.h" |
Jingyue Wu | fc02967 | 2014-09-30 22:23:38 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame^] | 40 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "simplifycfg" |
| 44 | |
Jingyue Wu | fc02967 | 2014-09-30 22:23:38 +0000 | [diff] [blame] | 45 | static cl::opt<unsigned> |
| 46 | UserBonusInstThreshold("bonus-inst-threshold", cl::Hidden, cl::init(1), |
| 47 | cl::desc("Control the number of bonus instructions (default = 1)")); |
| 48 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 49 | STATISTIC(NumSimpl, "Number of blocks simplified"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 50 | |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 51 | /// mergeEmptyReturnBlocks - If we have more than one empty (other than phi |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 52 | /// node) return blocks, merge them together to promote recursive block merging. |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 53 | static bool mergeEmptyReturnBlocks(Function &F) { |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 54 | bool Changed = false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 55 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 56 | BasicBlock *RetBlock = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 57 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 58 | // Scan all the blocks in the function, looking for empty return blocks. |
| 59 | for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) { |
| 60 | BasicBlock &BB = *BBI++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 61 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 62 | // Only look at return blocks. |
| 63 | ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 64 | if (!Ret) continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 65 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 66 | // Only look at the block if it is empty or the only other thing in it is a |
| 67 | // single PHI node that is the operand to the return. |
| 68 | if (Ret != &BB.front()) { |
| 69 | // Check for something else in the block. |
| 70 | BasicBlock::iterator I = Ret; |
| 71 | --I; |
Bill Wendling | 55e69d1 | 2010-03-14 10:40:55 +0000 | [diff] [blame] | 72 | // Skip over debug info. |
| 73 | while (isa<DbgInfoIntrinsic>(I) && I != BB.begin()) |
| 74 | --I; |
| 75 | if (!isa<DbgInfoIntrinsic>(I) && |
| 76 | (!isa<PHINode>(I) || I != BB.begin() || |
| 77 | Ret->getNumOperands() == 0 || |
| 78 | Ret->getOperand(0) != I)) |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 79 | continue; |
| 80 | } |
Bill Wendling | 55e69d1 | 2010-03-14 10:40:55 +0000 | [diff] [blame] | 81 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 82 | // If this is the first returning block, remember it and keep going. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 83 | if (!RetBlock) { |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 84 | RetBlock = &BB; |
| 85 | continue; |
| 86 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 87 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 88 | // Otherwise, we found a duplicate return block. Merge the two. |
| 89 | Changed = true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 90 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 91 | // Case when there is no input to the return or when the returned values |
| 92 | // agree is trivial. Note that they can't agree if there are phis in the |
| 93 | // blocks. |
| 94 | if (Ret->getNumOperands() == 0 || |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 95 | Ret->getOperand(0) == |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 96 | cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) { |
| 97 | BB.replaceAllUsesWith(RetBlock); |
| 98 | BB.eraseFromParent(); |
| 99 | continue; |
| 100 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 101 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 102 | // If the canonical return block has no PHI node, create one now. |
| 103 | PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 104 | if (!RetBlockPHI) { |
Devang Patel | d3f41e8 | 2010-03-15 19:05:46 +0000 | [diff] [blame] | 105 | Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0); |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 106 | pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 107 | RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(), |
| 108 | std::distance(PB, PE), "merge", |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 109 | &RetBlock->front()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 110 | |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 111 | for (pred_iterator PI = PB; PI != PE; ++PI) |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 112 | RetBlockPHI->addIncoming(InVal, *PI); |
| 113 | RetBlock->getTerminator()->setOperand(0, RetBlockPHI); |
| 114 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 115 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 116 | // Turn BB into a block that just unconditionally branches to the return |
| 117 | // block. This handles the case when the two return blocks have a common |
| 118 | // predecessor but that return different things. |
| 119 | RetBlockPHI->addIncoming(Ret->getOperand(0), &BB); |
| 120 | BB.getTerminator()->eraseFromParent(); |
| 121 | BranchInst::Create(RetBlock, &BB); |
| 122 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 123 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 124 | return Changed; |
| 125 | } |
| 126 | |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 127 | /// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function, |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 128 | /// iterating until no more changes are made. |
Chandler Carruth | 0b4ef9c | 2013-01-07 03:53:25 +0000 | [diff] [blame] | 129 | static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 130 | const DataLayout *DL, AssumptionCache *AC, |
Jingyue Wu | fc02967 | 2014-09-30 22:23:38 +0000 | [diff] [blame] | 131 | unsigned BonusInstThreshold) { |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 132 | bool Changed = false; |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 133 | bool LocalChange = true; |
| 134 | while (LocalChange) { |
| 135 | LocalChange = false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 136 | |
Dan Gohman | 4a63fad | 2010-08-14 00:29:42 +0000 | [diff] [blame] | 137 | // Loop over all of the basic blocks and remove them if they are unneeded... |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 138 | // |
Dan Gohman | 4a63fad | 2010-08-14 00:29:42 +0000 | [diff] [blame] | 139 | for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 140 | if (SimplifyCFG(BBIt++, TTI, BonusInstThreshold, DL, AC)) { |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 141 | LocalChange = true; |
| 142 | ++NumSimpl; |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | Changed |= LocalChange; |
| 146 | } |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 147 | return Changed; |
| 148 | } |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame^] | 150 | static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI, |
| 151 | const DataLayout *DL, AssumptionCache *AC, |
| 152 | int BonusInstThreshold) { |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 153 | bool EverChanged = removeUnreachableBlocks(F); |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 154 | EverChanged |= mergeEmptyReturnBlocks(F); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 155 | EverChanged |= iterativelySimplifyCFG(F, TTI, DL, AC, BonusInstThreshold); |
Jakob Stoklund Olesen | 916f48a | 2010-02-05 22:03:18 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 157 | // If neither pass changed anything, we're done. |
| 158 | if (!EverChanged) return false; |
| 159 | |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 160 | // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens, |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 161 | // removeUnreachableBlocks is needed to nuke them, which means we should |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 162 | // iterate between the two optimizations. We structure the code like this to |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 163 | // avoid reruning iterativelySimplifyCFG if the second pass of |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 164 | // removeUnreachableBlocks doesn't do anything. |
| 165 | if (!removeUnreachableBlocks(F)) |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 166 | return true; |
Jakob Stoklund Olesen | 916f48a | 2010-02-05 22:03:18 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 168 | do { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 169 | EverChanged = iterativelySimplifyCFG(F, TTI, DL, AC, BonusInstThreshold); |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 170 | EverChanged |= removeUnreachableBlocks(F); |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 171 | } while (EverChanged); |
Jakob Stoklund Olesen | 916f48a | 2010-02-05 22:03:18 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame^] | 175 | |
| 176 | SimplifyCFGPass::SimplifyCFGPass() |
| 177 | : BonusInstThreshold(UserBonusInstThreshold) {} |
| 178 | |
| 179 | SimplifyCFGPass::SimplifyCFGPass(int BonusInstThreshold) |
| 180 | : BonusInstThreshold(BonusInstThreshold) {} |
| 181 | |
| 182 | PreservedAnalyses SimplifyCFGPass::run(Function &F, |
| 183 | AnalysisManager<Function> *AM) { |
| 184 | auto *DL = F.getParent()->getDataLayout(); |
| 185 | auto &TTI = AM->getResult<TargetIRAnalysis>(F); |
| 186 | auto &AC = AM->getResult<AssumptionAnalysis>(F); |
| 187 | |
| 188 | if (!simplifyFunctionCFG(F, TTI, DL, &AC, BonusInstThreshold)) |
| 189 | return PreservedAnalyses::none(); |
| 190 | |
| 191 | return PreservedAnalyses::all(); |
| 192 | } |
| 193 | |
| 194 | namespace { |
| 195 | struct CFGSimplifyPass : public FunctionPass { |
| 196 | static char ID; // Pass identification, replacement for typeid |
| 197 | unsigned BonusInstThreshold; |
| 198 | CFGSimplifyPass(int T = -1) : FunctionPass(ID) { |
| 199 | BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : unsigned(T); |
| 200 | initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry()); |
| 201 | } |
| 202 | bool runOnFunction(Function &F) override { |
| 203 | if (skipOptnoneFunction(F)) |
| 204 | return false; |
| 205 | |
| 206 | AssumptionCache *AC = |
| 207 | &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
| 208 | const TargetTransformInfo &TTI = |
| 209 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(); |
| 210 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 211 | const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr; |
| 212 | return simplifyFunctionCFG(F, TTI, DL, AC, BonusInstThreshold); |
| 213 | } |
| 214 | |
| 215 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 216 | AU.addRequired<AssumptionCacheTracker>(); |
| 217 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 218 | } |
| 219 | }; |
| 220 | } |
| 221 | |
| 222 | char CFGSimplifyPass::ID = 0; |
| 223 | INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, |
| 224 | false) |
| 225 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| 226 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 227 | INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, |
| 228 | false) |
| 229 | |
| 230 | // Public interface to the CFGSimplification pass |
| 231 | FunctionPass *llvm::createCFGSimplificationPass(int Threshold) { |
| 232 | return new CFGSimplifyPass(Threshold); |
| 233 | } |
| 234 | |