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