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 | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AssumptionCache.h" |
Hyojin Sung | 4673f10 | 2016-03-29 04:08:57 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CFG.h" |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/GlobalsModRef.h" |
| 30 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 32 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Constants.h" |
| 34 | #include "llvm/IR/DataLayout.h" |
| 35 | #include "llvm/IR/Instructions.h" |
| 36 | #include "llvm/IR/IntrinsicInst.h" |
| 37 | #include "llvm/IR/Module.h" |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Jingyue Wu | fc02967 | 2014-09-30 22:23:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Scalar.h" |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Scalar/SimplifyCFG.h" |
| 42 | #include "llvm/Transforms/Utils/Local.h" |
| 43 | #include <utility> |
Chris Lattner | 49525f8 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 44 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 45 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "simplifycfg" |
| 47 | |
Jingyue Wu | fc02967 | 2014-09-30 22:23:38 +0000 | [diff] [blame] | 48 | static cl::opt<unsigned> |
| 49 | UserBonusInstThreshold("bonus-inst-threshold", cl::Hidden, cl::init(1), |
| 50 | cl::desc("Control the number of bonus instructions (default = 1)")); |
| 51 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 52 | STATISTIC(NumSimpl, "Number of blocks simplified"); |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 53 | |
Sanjay Patel | 09159b8f | 2015-06-24 20:40:57 +0000 | [diff] [blame] | 54 | /// If we have more than one empty (other than phi node) return blocks, |
| 55 | /// merge them together to promote recursive block merging. |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 56 | static bool mergeEmptyReturnBlocks(Function &F) { |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 57 | bool Changed = false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 58 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 59 | BasicBlock *RetBlock = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 60 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 61 | // Scan all the blocks in the function, looking for empty return blocks. |
| 62 | for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) { |
| 63 | BasicBlock &BB = *BBI++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 64 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 65 | // Only look at return blocks. |
| 66 | ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 67 | if (!Ret) continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 68 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 69 | // Only look at the block if it is empty or the only other thing in it is a |
| 70 | // single PHI node that is the operand to the return. |
| 71 | if (Ret != &BB.front()) { |
| 72 | // Check for something else in the block. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 73 | BasicBlock::iterator I(Ret); |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 74 | --I; |
Bill Wendling | 55e69d1 | 2010-03-14 10:40:55 +0000 | [diff] [blame] | 75 | // Skip over debug info. |
| 76 | while (isa<DbgInfoIntrinsic>(I) && I != BB.begin()) |
| 77 | --I; |
| 78 | if (!isa<DbgInfoIntrinsic>(I) && |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 79 | (!isa<PHINode>(I) || I != BB.begin() || Ret->getNumOperands() == 0 || |
| 80 | Ret->getOperand(0) != &*I)) |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 81 | continue; |
| 82 | } |
Bill Wendling | 55e69d1 | 2010-03-14 10:40:55 +0000 | [diff] [blame] | 83 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 84 | // If this is the first returning block, remember it and keep going. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 85 | if (!RetBlock) { |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 86 | RetBlock = &BB; |
| 87 | continue; |
| 88 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 89 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 90 | // Otherwise, we found a duplicate return block. Merge the two. |
| 91 | Changed = true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 92 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 93 | // Case when there is no input to the return or when the returned values |
| 94 | // agree is trivial. Note that they can't agree if there are phis in the |
| 95 | // blocks. |
| 96 | if (Ret->getNumOperands() == 0 || |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 97 | Ret->getOperand(0) == |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 98 | cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) { |
| 99 | BB.replaceAllUsesWith(RetBlock); |
| 100 | BB.eraseFromParent(); |
| 101 | continue; |
| 102 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 103 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 104 | // If the canonical return block has no PHI node, create one now. |
| 105 | PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 106 | if (!RetBlockPHI) { |
Devang Patel | d3f41e8 | 2010-03-15 19:05:46 +0000 | [diff] [blame] | 107 | Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0); |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 108 | pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 109 | RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(), |
| 110 | std::distance(PB, PE), "merge", |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 111 | &RetBlock->front()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 112 | |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 113 | for (pred_iterator PI = PB; PI != PE; ++PI) |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 114 | RetBlockPHI->addIncoming(InVal, *PI); |
| 115 | RetBlock->getTerminator()->setOperand(0, RetBlockPHI); |
| 116 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 117 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 118 | // Turn BB into a block that just unconditionally branches to the return |
| 119 | // block. This handles the case when the two return blocks have a common |
| 120 | // predecessor but that return different things. |
| 121 | RetBlockPHI->addIncoming(Ret->getOperand(0), &BB); |
| 122 | BB.getTerminator()->eraseFromParent(); |
| 123 | BranchInst::Create(RetBlock, &BB); |
| 124 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 125 | |
Chris Lattner | f21a220 | 2009-12-22 06:07:30 +0000 | [diff] [blame] | 126 | return Changed; |
| 127 | } |
| 128 | |
Sanjay Patel | 09159b8f | 2015-06-24 20:40:57 +0000 | [diff] [blame] | 129 | /// Call SimplifyCFG on all the blocks in the function, |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 130 | /// iterating until no more changes are made. |
Chandler Carruth | 0b4ef9c | 2013-01-07 03:53:25 +0000 | [diff] [blame] | 131 | static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI, |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 132 | const SimplifyCFGOptions &Options) { |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 133 | bool Changed = false; |
Reid Kleckner | ba85781 | 2016-03-28 18:07:40 +0000 | [diff] [blame] | 134 | bool LocalChange = true; |
Hyojin Sung | 4673f10 | 2016-03-29 04:08:57 +0000 | [diff] [blame] | 135 | |
| 136 | SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 32> Edges; |
| 137 | FindFunctionBackedges(F, Edges); |
| 138 | SmallPtrSet<BasicBlock *, 16> LoopHeaders; |
| 139 | for (unsigned i = 0, e = Edges.size(); i != e; ++i) |
| 140 | LoopHeaders.insert(const_cast<BasicBlock *>(Edges[i].second)); |
| 141 | |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 142 | while (LocalChange) { |
| 143 | LocalChange = false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 144 | |
Sanjay Patel | 64ea207 | 2015-06-24 20:42:33 +0000 | [diff] [blame] | 145 | // Loop over all of the basic blocks and remove them if they are unneeded. |
Dan Gohman | 4a63fad | 2010-08-14 00:29:42 +0000 | [diff] [blame] | 146 | for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) { |
Sanjay Patel | 4c33d52 | 2017-10-04 20:26:25 +0000 | [diff] [blame^] | 147 | if (simplifyCFG(&*BBIt++, TTI, Options, &LoopHeaders)) { |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 148 | LocalChange = true; |
| 149 | ++NumSimpl; |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | Changed |= LocalChange; |
| 153 | } |
Chris Lattner | fa7dad8 | 2002-05-21 20:49:37 +0000 | [diff] [blame] | 154 | return Changed; |
| 155 | } |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 156 | |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 157 | static bool simplifyFunctionCFG(Function &F, const TargetTransformInfo &TTI, |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 158 | const SimplifyCFGOptions &Options) { |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 159 | bool EverChanged = removeUnreachableBlocks(F); |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 160 | EverChanged |= mergeEmptyReturnBlocks(F); |
Sanjay Patel | 4c33d52 | 2017-10-04 20:26:25 +0000 | [diff] [blame^] | 161 | EverChanged |= iterativelySimplifyCFG(F, TTI, Options); |
Jakob Stoklund Olesen | 916f48a | 2010-02-05 22:03:18 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 163 | // If neither pass changed anything, we're done. |
| 164 | if (!EverChanged) return false; |
| 165 | |
Jim Grosbach | 30c4282 | 2012-09-06 00:59:08 +0000 | [diff] [blame] | 166 | // iterativelySimplifyCFG can (rarely) make some loops dead. If this happens, |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 167 | // removeUnreachableBlocks is needed to nuke them, which means we should |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 168 | // iterate between the two optimizations. We structure the code like this to |
Sanjay Patel | 64ea207 | 2015-06-24 20:42:33 +0000 | [diff] [blame] | 169 | // avoid rerunning iterativelySimplifyCFG if the second pass of |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 170 | // removeUnreachableBlocks doesn't do anything. |
| 171 | if (!removeUnreachableBlocks(F)) |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 172 | return true; |
Jakob Stoklund Olesen | 916f48a | 2010-02-05 22:03:18 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 174 | do { |
Sanjay Patel | 4c33d52 | 2017-10-04 20:26:25 +0000 | [diff] [blame^] | 175 | EverChanged = iterativelySimplifyCFG(F, TTI, Options); |
Peter Collingbourne | 8d642de | 2013-08-12 22:38:43 +0000 | [diff] [blame] | 176 | EverChanged |= removeUnreachableBlocks(F); |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 177 | } while (EverChanged); |
Jakob Stoklund Olesen | 916f48a | 2010-02-05 22:03:18 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 61ce4df | 2007-11-13 07:32:38 +0000 | [diff] [blame] | 179 | return true; |
| 180 | } |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 181 | |
| 182 | SimplifyCFGPass::SimplifyCFGPass() |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 183 | : Options(UserBonusInstThreshold, true, false) {} |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 184 | |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 185 | SimplifyCFGPass::SimplifyCFGPass(const SimplifyCFGOptions &PassOptions) |
| 186 | : Options(PassOptions) {} |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 187 | |
| 188 | PreservedAnalyses SimplifyCFGPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 189 | FunctionAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 190 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
Sanjay Patel | 4c33d52 | 2017-10-04 20:26:25 +0000 | [diff] [blame^] | 191 | Options.AC = &AM.getResult<AssumptionAnalysis>(F); |
| 192 | if (!simplifyFunctionCFG(F, TTI, Options)) |
Davide Italiano | d8d83f4 | 2016-06-08 13:32:23 +0000 | [diff] [blame] | 193 | return PreservedAnalyses::all(); |
| 194 | PreservedAnalyses PA; |
| 195 | PA.preserve<GlobalsAA>(); |
| 196 | return PA; |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 199 | namespace { |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 200 | struct BaseCFGSimplifyPass : public FunctionPass { |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 201 | std::function<bool(const Function &)> PredicateFtor; |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 202 | int BonusInstThreshold; |
| 203 | bool ConvertSwitchToLookupTable; |
| 204 | bool KeepCanonicalLoops; |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 205 | |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 206 | BaseCFGSimplifyPass(int T, bool ConvertSwitch, bool KeepLoops, |
| 207 | std::function<bool(const Function &)> Ftor, char &ID) |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 208 | : FunctionPass(ID), PredicateFtor(std::move(Ftor)), |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 209 | ConvertSwitchToLookupTable(ConvertSwitch), |
| 210 | KeepCanonicalLoops(KeepLoops) { |
| 211 | BonusInstThreshold = (T == -1) ? UserBonusInstThreshold : T; |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 212 | } |
| 213 | bool runOnFunction(Function &F) override { |
| 214 | if (skipFunction(F) || (PredicateFtor && !PredicateFtor(F))) |
| 215 | return false; |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 216 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 217 | AssumptionCache *AC = |
| 218 | &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 219 | const TargetTransformInfo &TTI = |
| 220 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Sanjay Patel | 4c33d52 | 2017-10-04 20:26:25 +0000 | [diff] [blame^] | 221 | return simplifyFunctionCFG(F, TTI, |
| 222 | {BonusInstThreshold, ConvertSwitchToLookupTable, |
| 223 | KeepCanonicalLoops, AC}); |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 224 | } |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 225 | |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 226 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 227 | AU.addRequired<AssumptionCacheTracker>(); |
Dehao Chen | f50c67c | 2016-05-05 20:47:53 +0000 | [diff] [blame] | 228 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 229 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 230 | } |
| 231 | }; |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 232 | |
| 233 | struct CFGSimplifyPass : public BaseCFGSimplifyPass { |
| 234 | static char ID; // Pass identification, replacement for typeid |
| 235 | |
| 236 | CFGSimplifyPass(int T = -1, |
| 237 | std::function<bool(const Function &)> Ftor = nullptr) |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 238 | : BaseCFGSimplifyPass(T, false, true, Ftor, ID) { |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 239 | initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry()); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | struct LateCFGSimplifyPass : public BaseCFGSimplifyPass { |
| 244 | static char ID; // Pass identification, replacement for typeid |
| 245 | |
| 246 | LateCFGSimplifyPass(int T = -1, |
| 247 | std::function<bool(const Function &)> Ftor = nullptr) |
Sanjay Patel | 0f9b477 | 2017-09-27 14:54:16 +0000 | [diff] [blame] | 248 | : BaseCFGSimplifyPass(T, true, false, Ftor, ID) { |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 249 | initializeLateCFGSimplifyPassPass(*PassRegistry::getPassRegistry()); |
| 250 | } |
| 251 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 252 | } |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 253 | |
| 254 | char CFGSimplifyPass::ID = 0; |
| 255 | INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, |
| 256 | false) |
| 257 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 258 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 259 | INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false, |
| 260 | false) |
| 261 | |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 262 | char LateCFGSimplifyPass::ID = 0; |
| 263 | INITIALIZE_PASS_BEGIN(LateCFGSimplifyPass, "latesimplifycfg", |
| 264 | "Simplify the CFG more aggressively", false, false) |
| 265 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| 266 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 267 | INITIALIZE_PASS_END(LateCFGSimplifyPass, "latesimplifycfg", |
| 268 | "Simplify the CFG more aggressively", false, false) |
| 269 | |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 270 | // Public interface to the CFGSimplification pass |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 271 | FunctionPass * |
| 272 | llvm::createCFGSimplificationPass(int Threshold, |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 273 | std::function<bool(const Function &)> Ftor) { |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 274 | return new CFGSimplifyPass(Threshold, std::move(Ftor)); |
Chandler Carruth | fdffd87 | 2015-02-01 11:34:21 +0000 | [diff] [blame] | 275 | } |
Joerg Sonnenberger | fa73674 | 2017-03-26 06:44:08 +0000 | [diff] [blame] | 276 | |
| 277 | // Public interface to the LateCFGSimplification pass |
| 278 | FunctionPass * |
| 279 | llvm::createLateCFGSimplificationPass(int Threshold, |
| 280 | std::function<bool(const Function &)> Ftor) { |
| 281 | return new LateCFGSimplifyPass(Threshold, std::move(Ftor)); |
| 282 | } |