Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 1 | //===-- StructurizeCFG.cpp ------------------------------------------------===// |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 9 | |
Christian Konig | 90b4512 | 2013-03-26 10:24:20 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/MapVector.h" |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/PostOrderIterator.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SCCIterator.h" |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/DivergenceAnalysis.h" |
Tom Stellard | 1f0dded | 2014-12-03 04:28:32 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/LoopInfo.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/RegionIterator.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/RegionPass.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 19 | #include "llvm/IR/PatternMatch.h" |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 22 | #include "llvm/Transforms/Scalar.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 26 | using namespace llvm::PatternMatch; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 27 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "structurizecfg" |
| 29 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // Definition of the complex types used in this pass. |
| 33 | |
| 34 | typedef std::pair<BasicBlock *, Value *> BBValuePair; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 35 | |
| 36 | typedef SmallVector<RegionNode*, 8> RNVector; |
| 37 | typedef SmallVector<BasicBlock*, 8> BBVector; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 38 | typedef SmallVector<BranchInst*, 8> BranchVector; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 39 | typedef SmallVector<BBValuePair, 2> BBValueVector; |
| 40 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 41 | typedef SmallPtrSet<BasicBlock *, 8> BBSet; |
| 42 | |
Christian Konig | 90b4512 | 2013-03-26 10:24:20 +0000 | [diff] [blame] | 43 | typedef MapVector<PHINode *, BBValueVector> PhiMap; |
| 44 | typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap; |
| 45 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 46 | typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap; |
| 47 | typedef DenseMap<BasicBlock *, Value *> BBPredicates; |
| 48 | typedef DenseMap<BasicBlock *, BBPredicates> PredMap; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 49 | typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 50 | |
| 51 | // The name for newly created blocks. |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 52 | static const char *const FlowBlockName = "Flow"; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 53 | |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 54 | /// Finds the nearest common dominator of a set of BasicBlocks. |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 55 | /// |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 56 | /// For every BB you add to the set, you can specify whether we "remember" the |
| 57 | /// block. When you get the common dominator, you can also ask whether it's one |
| 58 | /// of the blocks we remembered. |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 59 | class NearestCommonDominator { |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 60 | DominatorTree *DT; |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 61 | BasicBlock *Result = nullptr; |
| 62 | bool ResultIsRemembered = false; |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 63 | |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 64 | /// Add BB to the resulting dominator. |
| 65 | void addBlock(BasicBlock *BB, bool Remember) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 66 | if (!Result) { |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 67 | Result = BB; |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 68 | ResultIsRemembered = Remember; |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 72 | BasicBlock *NewResult = DT->findNearestCommonDominator(Result, BB); |
| 73 | if (NewResult != Result) |
| 74 | ResultIsRemembered = false; |
| 75 | if (NewResult == BB) |
| 76 | ResultIsRemembered |= Remember; |
| 77 | Result = NewResult; |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 80 | public: |
| 81 | explicit NearestCommonDominator(DominatorTree *DomTree) : DT(DomTree) {} |
| 82 | |
| 83 | void addBlock(BasicBlock *BB) { |
| 84 | addBlock(BB, /* Remember = */ false); |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 87 | void addAndRememberBlock(BasicBlock *BB) { |
| 88 | addBlock(BB, /* Remember = */ true); |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 89 | } |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 90 | |
| 91 | /// Get the nearest common dominator of all the BBs added via addBlock() and |
| 92 | /// addAndRememberBlock(). |
| 93 | BasicBlock *result() { return Result; } |
| 94 | |
| 95 | /// Is the BB returned by getResult() one of the blocks we added to the set |
| 96 | /// with addAndRememberBlock()? |
| 97 | bool resultIsRememberedBlock() { return ResultIsRemembered; } |
Christian Konig | d08e3d7 | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 100 | /// @brief Transforms the control flow graph on one single entry/exit region |
| 101 | /// at a time. |
| 102 | /// |
| 103 | /// After the transform all "If"/"Then"/"Else" style control flow looks like |
| 104 | /// this: |
| 105 | /// |
| 106 | /// \verbatim |
| 107 | /// 1 |
| 108 | /// || |
| 109 | /// | | |
| 110 | /// 2 | |
| 111 | /// | / |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 112 | /// |/ |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 113 | /// 3 |
| 114 | /// || Where: |
| 115 | /// | | 1 = "If" block, calculates the condition |
| 116 | /// 4 | 2 = "Then" subregion, runs if the condition is true |
| 117 | /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow |
| 118 | /// |/ 4 = "Else" optional subregion, runs if the condition is false |
| 119 | /// 5 5 = "End" block, also rejoins the control flow |
| 120 | /// \endverbatim |
| 121 | /// |
| 122 | /// Control flow is expressed as a branch where the true exit goes into the |
| 123 | /// "Then"/"Else" region, while the false exit skips the region |
| 124 | /// The condition for the optional "Else" region is expressed as a PHI node. |
Simon Pilgrim | 7d18a70 | 2016-11-20 13:19:49 +0000 | [diff] [blame] | 125 | /// The incoming values of the PHI node are true for the "If" edge and false |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 126 | /// for the "Then" edge. |
| 127 | /// |
| 128 | /// Additionally to that even complicated loops look like this: |
| 129 | /// |
| 130 | /// \verbatim |
| 131 | /// 1 |
| 132 | /// || |
| 133 | /// | | |
| 134 | /// 2 ^ Where: |
| 135 | /// | / 1 = "Entry" block |
| 136 | /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block |
| 137 | /// 3 3 = "Flow" block, with back edge to entry block |
| 138 | /// | |
| 139 | /// \endverbatim |
| 140 | /// |
| 141 | /// The back edge of the "Flow" block is always on the false side of the branch |
| 142 | /// while the true side continues the general flow. So the loop condition |
| 143 | /// consist of a network of PHI nodes where the true incoming values expresses |
| 144 | /// breaks and the false values expresses continue states. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 145 | class StructurizeCFG : public RegionPass { |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 146 | bool SkipUniformRegions; |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 147 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 148 | Type *Boolean; |
| 149 | ConstantInt *BoolTrue; |
| 150 | ConstantInt *BoolFalse; |
| 151 | UndefValue *BoolUndef; |
| 152 | |
| 153 | Function *Func; |
| 154 | Region *ParentRegion; |
| 155 | |
| 156 | DominatorTree *DT; |
Tom Stellard | 1f0dded | 2014-12-03 04:28:32 +0000 | [diff] [blame] | 157 | LoopInfo *LI; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 158 | |
Justin Lebar | 6c0f25a | 2016-11-22 23:14:11 +0000 | [diff] [blame] | 159 | SmallVector<RegionNode *, 8> Order; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 160 | BBSet Visited; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 161 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 162 | BBPhiMap DeletedPhis; |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 163 | BB2BBVecMap AddedPhis; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 164 | |
| 165 | PredMap Predicates; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 166 | BranchVector Conditions; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 167 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 168 | BB2BBMap Loops; |
| 169 | PredMap LoopPreds; |
| 170 | BranchVector LoopConds; |
| 171 | |
| 172 | RegionNode *PrevNode; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 173 | |
| 174 | void orderNodes(); |
| 175 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 176 | void analyzeLoops(RegionNode *N); |
| 177 | |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 178 | Value *invert(Value *Condition); |
| 179 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 180 | Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 181 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 182 | void gatherPredicates(RegionNode *N); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 183 | |
| 184 | void collectInfos(); |
| 185 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 186 | void insertConditions(bool Loops); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 187 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 188 | void delPhiValues(BasicBlock *From, BasicBlock *To); |
| 189 | |
| 190 | void addPhiValues(BasicBlock *From, BasicBlock *To); |
| 191 | |
| 192 | void setPhiValues(); |
| 193 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 194 | void killTerminator(BasicBlock *BB); |
| 195 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 196 | void changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 197 | bool IncludeDominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 198 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 199 | BasicBlock *getNextFlow(BasicBlock *Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 200 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 201 | BasicBlock *needPrefix(bool NeedEmpty); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 202 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 203 | BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed); |
| 204 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 205 | void setPrevNode(BasicBlock *BB); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 206 | |
| 207 | bool dominatesPredicates(BasicBlock *BB, RegionNode *Node); |
| 208 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 209 | bool isPredictableTrue(RegionNode *Node); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 210 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 211 | void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd); |
| 212 | |
| 213 | void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 214 | |
| 215 | void createFlow(); |
| 216 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 217 | void rebuildSSA(); |
| 218 | |
| 219 | public: |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 220 | static char ID; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 221 | |
Justin Lebar | 73c4baf | 2016-11-22 23:13:44 +0000 | [diff] [blame] | 222 | explicit StructurizeCFG(bool SkipUniformRegions = false) |
| 223 | : RegionPass(ID), SkipUniformRegions(SkipUniformRegions) { |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 224 | initializeStructurizeCFGPass(*PassRegistry::getPassRegistry()); |
| 225 | } |
| 226 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 227 | bool doInitialization(Region *R, RGPassManager &RGM) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 228 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 229 | bool runOnRegion(Region *R, RGPassManager &RGM) override; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 230 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 231 | StringRef getPassName() const override { return "Structurize control flow"; } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 232 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 233 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 234 | if (SkipUniformRegions) |
| 235 | AU.addRequired<DivergenceAnalysis>(); |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 236 | AU.addRequiredID(LowerSwitchID); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 237 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 238 | AU.addRequired<LoopInfoWrapperPass>(); |
Justin Lebar | 23aaf60 | 2016-11-22 23:14:07 +0000 | [diff] [blame] | 239 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 240 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 241 | RegionPass::getAnalysisUsage(AU); |
| 242 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | } // end anonymous namespace |
| 246 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 247 | char StructurizeCFG::ID = 0; |
| 248 | |
| 249 | INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG", |
| 250 | false, false) |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 251 | INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis) |
Tom Stellard | d3e916e | 2013-10-02 17:04:59 +0000 | [diff] [blame] | 252 | INITIALIZE_PASS_DEPENDENCY(LowerSwitch) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 253 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 254 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass) |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 255 | INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG", |
| 256 | false, false) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 257 | |
| 258 | /// \brief Initialize the types and constants used in the pass |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 259 | bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 260 | LLVMContext &Context = R->getEntry()->getContext(); |
| 261 | |
| 262 | Boolean = Type::getInt1Ty(Context); |
| 263 | BoolTrue = ConstantInt::getTrue(Context); |
| 264 | BoolFalse = ConstantInt::getFalse(Context); |
| 265 | BoolUndef = UndefValue::get(Boolean); |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | /// \brief Build up the general order of nodes |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 271 | void StructurizeCFG::orderNodes() { |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 272 | ReversePostOrderTraversal<Region*> RPOT(ParentRegion); |
Justin Lebar | 6c0f25a | 2016-11-22 23:14:11 +0000 | [diff] [blame] | 273 | SmallDenseMap<Loop*, unsigned, 8> LoopBlocks; |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 274 | |
| 275 | // The reverse post-order traversal of the list gives us an ordering close |
| 276 | // to what we want. The only problem with it is that sometimes backedges |
| 277 | // for outer loops will be visited before backedges for inner loops. |
Justin Lebar | 6c0f25a | 2016-11-22 23:14:11 +0000 | [diff] [blame] | 278 | for (RegionNode *RN : RPOT) { |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 279 | BasicBlock *BB = RN->getEntry(); |
| 280 | Loop *Loop = LI->getLoopFor(BB); |
Benjamin Kramer | 4dea8f5 | 2016-06-17 18:59:41 +0000 | [diff] [blame] | 281 | ++LoopBlocks[Loop]; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 282 | } |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 283 | |
| 284 | unsigned CurrentLoopDepth = 0; |
| 285 | Loop *CurrentLoop = nullptr; |
Justin Lebar | 6c0f25a | 2016-11-22 23:14:11 +0000 | [diff] [blame] | 286 | for (auto I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 287 | BasicBlock *BB = (*I)->getEntry(); |
| 288 | unsigned LoopDepth = LI->getLoopDepth(BB); |
| 289 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 290 | if (is_contained(Order, *I)) |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 291 | continue; |
| 292 | |
| 293 | if (LoopDepth < CurrentLoopDepth) { |
| 294 | // Make sure we have visited all blocks in this loop before moving back to |
| 295 | // the outer loop. |
| 296 | |
Justin Lebar | 6c0f25a | 2016-11-22 23:14:11 +0000 | [diff] [blame] | 297 | auto LoopI = I; |
Benjamin Kramer | 4dea8f5 | 2016-06-17 18:59:41 +0000 | [diff] [blame] | 298 | while (unsigned &BlockCount = LoopBlocks[CurrentLoop]) { |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 299 | LoopI++; |
| 300 | BasicBlock *LoopBB = (*LoopI)->getEntry(); |
| 301 | if (LI->getLoopFor(LoopBB) == CurrentLoop) { |
Benjamin Kramer | 4dea8f5 | 2016-06-17 18:59:41 +0000 | [diff] [blame] | 302 | --BlockCount; |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 303 | Order.push_back(*LoopI); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | CurrentLoop = LI->getLoopFor(BB); |
Justin Lebar | 6c0f25a | 2016-11-22 23:14:11 +0000 | [diff] [blame] | 309 | if (CurrentLoop) |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 310 | LoopBlocks[CurrentLoop]--; |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 311 | |
| 312 | CurrentLoopDepth = LoopDepth; |
| 313 | Order.push_back(*I); |
| 314 | } |
| 315 | |
| 316 | // This pass originally used a post-order traversal and then operated on |
| 317 | // the list in reverse. Now that we are using a reverse post-order traversal |
| 318 | // rather than re-working the whole pass to operate on the list in order, |
| 319 | // we just reverse the list and continue to operate on it in reverse. |
| 320 | std::reverse(Order.begin(), Order.end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 323 | /// \brief Determine the end of the loops |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 324 | void StructurizeCFG::analyzeLoops(RegionNode *N) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 325 | if (N->isSubRegion()) { |
| 326 | // Test for exit as back edge |
| 327 | BasicBlock *Exit = N->getNodeAs<Region>()->getExit(); |
| 328 | if (Visited.count(Exit)) |
| 329 | Loops[Exit] = N->getEntry(); |
| 330 | |
| 331 | } else { |
| 332 | // Test for sucessors as back edge |
| 333 | BasicBlock *BB = N->getNodeAs<BasicBlock>(); |
| 334 | BranchInst *Term = cast<BranchInst>(BB->getTerminator()); |
| 335 | |
Pete Cooper | ebcd748 | 2015-08-06 20:22:46 +0000 | [diff] [blame] | 336 | for (BasicBlock *Succ : Term->successors()) |
| 337 | if (Visited.count(Succ)) |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 338 | Loops[Succ] = BB; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 342 | /// \brief Invert the given condition |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 343 | Value *StructurizeCFG::invert(Value *Condition) { |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 344 | // First: Check if it's a constant |
Matt Arsenault | 93be6e8 | 2016-07-15 22:13:16 +0000 | [diff] [blame] | 345 | if (Constant *C = dyn_cast<Constant>(Condition)) |
| 346 | return ConstantExpr::getNot(C); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 347 | |
| 348 | // Second: If the condition is already inverted, return the original value |
| 349 | if (match(Condition, m_Not(m_Value(Condition)))) |
| 350 | return Condition; |
| 351 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 352 | if (Instruction *Inst = dyn_cast<Instruction>(Condition)) { |
| 353 | // Third: Check all the users for an invert |
| 354 | BasicBlock *Parent = Inst->getParent(); |
Matt Arsenault | 4474652 | 2017-04-24 20:25:01 +0000 | [diff] [blame] | 355 | for (User *U : Condition->users()) |
| 356 | if (Instruction *I = dyn_cast<Instruction>(U)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 357 | if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) |
| 358 | return I; |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 359 | |
| 360 | // Last option: Create a new instruction |
| 361 | return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator()); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Matt Arsenault | 9fb6e0b | 2013-11-22 19:24:37 +0000 | [diff] [blame] | 364 | if (Argument *Arg = dyn_cast<Argument>(Condition)) { |
| 365 | BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock(); |
| 366 | return BinaryOperator::CreateNot(Condition, |
| 367 | Arg->getName() + ".inv", |
| 368 | EntryBlock.getTerminator()); |
| 369 | } |
| 370 | |
| 371 | llvm_unreachable("Unhandled condition to invert"); |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 374 | /// \brief Build the condition for one edge |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 375 | Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx, |
| 376 | bool Invert) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 377 | Value *Cond = Invert ? BoolFalse : BoolTrue; |
| 378 | if (Term->isConditional()) { |
| 379 | Cond = Term->getCondition(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 380 | |
Aaron Ballman | 1997855 | 2013-06-04 01:03:03 +0000 | [diff] [blame] | 381 | if (Idx != (unsigned)Invert) |
Christian Konig | d886099 | 2013-02-16 11:27:50 +0000 | [diff] [blame] | 382 | Cond = invert(Cond); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 383 | } |
| 384 | return Cond; |
| 385 | } |
| 386 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 387 | /// \brief Analyze the predecessors of each block and build up predicates |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 388 | void StructurizeCFG::gatherPredicates(RegionNode *N) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 389 | RegionInfo *RI = ParentRegion->getRegionInfo(); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 390 | BasicBlock *BB = N->getEntry(); |
| 391 | BBPredicates &Pred = Predicates[BB]; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 392 | BBPredicates &LPred = LoopPreds[BB]; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 393 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 394 | for (BasicBlock *P : predecessors(BB)) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 395 | // Ignore it if it's a branch from outside into our region entry |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 396 | if (!ParentRegion->contains(P)) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 397 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 398 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 399 | Region *R = RI->getRegionFor(P); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 400 | if (R == ParentRegion) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 401 | // It's a top level block in our region |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 402 | BranchInst *Term = cast<BranchInst>(P->getTerminator()); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 403 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 404 | BasicBlock *Succ = Term->getSuccessor(i); |
| 405 | if (Succ != BB) |
| 406 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 407 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 408 | if (Visited.count(P)) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 409 | // Normal forward edge |
| 410 | if (Term->isConditional()) { |
| 411 | // Try to treat it like an ELSE block |
| 412 | BasicBlock *Other = Term->getSuccessor(!i); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 413 | if (Visited.count(Other) && !Loops.count(Other) && |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 414 | !Pred.count(Other) && !Pred.count(P)) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 415 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 416 | Pred[Other] = BoolFalse; |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 417 | Pred[P] = BoolTrue; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 418 | continue; |
| 419 | } |
| 420 | } |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 421 | Pred[P] = buildCondition(Term, i, false); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 422 | } else { |
| 423 | // Back edge |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 424 | LPred[P] = buildCondition(Term, i, true); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 425 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 426 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 427 | } else { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 428 | // It's an exit from a sub region |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 429 | while (R->getParent() != ParentRegion) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 430 | R = R->getParent(); |
| 431 | |
| 432 | // Edge from inside a subregion to its entry, ignore it |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 433 | if (*R == *N) |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 434 | continue; |
| 435 | |
| 436 | BasicBlock *Entry = R->getEntry(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 437 | if (Visited.count(Entry)) |
| 438 | Pred[Entry] = BoolTrue; |
| 439 | else |
| 440 | LPred[Entry] = BoolFalse; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /// \brief Collect various loop and predicate infos |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 446 | void StructurizeCFG::collectInfos() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 447 | // Reset predicate |
| 448 | Predicates.clear(); |
| 449 | |
| 450 | // and loop infos |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 451 | Loops.clear(); |
| 452 | LoopPreds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 453 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 454 | // Reset the visited nodes |
| 455 | Visited.clear(); |
| 456 | |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 457 | for (RegionNode *RN : reverse(Order)) { |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 458 | DEBUG(dbgs() << "Visiting: " |
| 459 | << (RN->isSubRegion() ? "SubRegion with entry: " : "") |
| 460 | << RN->getEntry()->getName() << " Loop Depth: " |
| 461 | << LI->getLoopDepth(RN->getEntry()) << "\n"); |
Tom Stellard | 071ec90 | 2015-02-04 20:49:44 +0000 | [diff] [blame] | 462 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 463 | // Analyze all the conditions leading to a node |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 464 | gatherPredicates(RN); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 465 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 466 | // Remember that we've seen this node |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 467 | Visited.insert(RN->getEntry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 468 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 469 | // Find the last back edges |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 470 | analyzeLoops(RN); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 471 | } |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /// \brief Insert the missing branch conditions |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 475 | void StructurizeCFG::insertConditions(bool Loops) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 476 | BranchVector &Conds = Loops ? LoopConds : Conditions; |
| 477 | Value *Default = Loops ? BoolTrue : BoolFalse; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 478 | SSAUpdater PhiInserter; |
| 479 | |
Matt Arsenault | 04b67ce | 2014-05-19 17:52:48 +0000 | [diff] [blame] | 480 | for (BranchInst *Term : Conds) { |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 481 | assert(Term->isConditional()); |
| 482 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 483 | BasicBlock *Parent = Term->getParent(); |
| 484 | BasicBlock *SuccTrue = Term->getSuccessor(0); |
| 485 | BasicBlock *SuccFalse = Term->getSuccessor(1); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 486 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 487 | PhiInserter.Initialize(Boolean, ""); |
| 488 | PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 489 | PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 490 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 491 | BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue]; |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 492 | |
| 493 | NearestCommonDominator Dominator(DT); |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 494 | Dominator.addBlock(Parent); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 495 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 496 | Value *ParentValue = nullptr; |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 497 | for (std::pair<BasicBlock *, Value *> BBAndPred : Preds) { |
| 498 | BasicBlock *BB = BBAndPred.first; |
| 499 | Value *Pred = BBAndPred.second; |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 500 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 501 | if (BB == Parent) { |
| 502 | ParentValue = Pred; |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 505 | PhiInserter.AddAvailableValue(BB, Pred); |
| 506 | Dominator.addAndRememberBlock(BB); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 509 | if (ParentValue) { |
| 510 | Term->setCondition(ParentValue); |
| 511 | } else { |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 512 | if (!Dominator.resultIsRememberedBlock()) |
| 513 | PhiInserter.AddAvailableValue(Dominator.result(), Default); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 514 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 515 | Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent)); |
Christian Konig | b5d8866 | 2013-02-16 11:27:40 +0000 | [diff] [blame] | 516 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 520 | /// \brief Remove all PHI values coming from "From" into "To" and remember |
| 521 | /// them in DeletedPhis |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 522 | void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 523 | PhiMap &Map = DeletedPhis[To]; |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 524 | for (Instruction &I : *To) { |
| 525 | if (!isa<PHINode>(I)) |
| 526 | break; |
| 527 | PHINode &Phi = cast<PHINode>(I); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 528 | while (Phi.getBasicBlockIndex(From) != -1) { |
| 529 | Value *Deleted = Phi.removeIncomingValue(From, false); |
| 530 | Map[&Phi].push_back(std::make_pair(From, Deleted)); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /// \brief Add a dummy PHI value as soon as we knew the new predecessor |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 536 | void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) { |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 537 | for (Instruction &I : *To) { |
| 538 | if (!isa<PHINode>(I)) |
| 539 | break; |
| 540 | PHINode &Phi = cast<PHINode>(I); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 541 | Value *Undef = UndefValue::get(Phi.getType()); |
| 542 | Phi.addIncoming(Undef, From); |
| 543 | } |
| 544 | AddedPhis[To].push_back(From); |
| 545 | } |
| 546 | |
| 547 | /// \brief Add the real PHI value as soon as everything is set up |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 548 | void StructurizeCFG::setPhiValues() { |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 549 | SSAUpdater Updater; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 550 | for (const auto &AddedPhi : AddedPhis) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 551 | BasicBlock *To = AddedPhi.first; |
| 552 | const BBVector &From = AddedPhi.second; |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 553 | |
| 554 | if (!DeletedPhis.count(To)) |
| 555 | continue; |
| 556 | |
| 557 | PhiMap &Map = DeletedPhis[To]; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 558 | for (const auto &PI : Map) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 559 | PHINode *Phi = PI.first; |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 560 | Value *Undef = UndefValue::get(Phi->getType()); |
| 561 | Updater.Initialize(Phi->getType(), ""); |
| 562 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 563 | Updater.AddAvailableValue(To, Undef); |
| 564 | |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 565 | NearestCommonDominator Dominator(DT); |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 566 | Dominator.addBlock(To); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 567 | for (const auto &VI : PI.second) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 568 | Updater.AddAvailableValue(VI.first, VI.second); |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 569 | Dominator.addAndRememberBlock(VI.first); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Justin Lebar | 62c20d8 | 2016-11-28 18:49:59 +0000 | [diff] [blame] | 572 | if (!Dominator.resultIsRememberedBlock()) |
| 573 | Updater.AddAvailableValue(Dominator.result(), Undef); |
Christian Konig | 0bccf9d | 2013-02-16 11:27:35 +0000 | [diff] [blame] | 574 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 575 | for (BasicBlock *FI : From) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 576 | int Idx = Phi->getBasicBlockIndex(FI); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 577 | assert(Idx != -1); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 578 | Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(FI)); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | DeletedPhis.erase(To); |
| 583 | } |
| 584 | assert(DeletedPhis.empty()); |
| 585 | } |
| 586 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 587 | /// \brief Remove phi values from all successors and then remove the terminator. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 588 | void StructurizeCFG::killTerminator(BasicBlock *BB) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 589 | TerminatorInst *Term = BB->getTerminator(); |
| 590 | if (!Term) |
| 591 | return; |
| 592 | |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 593 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 594 | SI != SE; ++SI) |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 595 | delPhiValues(BB, *SI); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 596 | |
| 597 | Term->eraseFromParent(); |
| 598 | } |
| 599 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 600 | /// \brief Let node exit(s) point to NewExit |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 601 | void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 602 | bool IncludeDominator) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 603 | if (Node->isSubRegion()) { |
| 604 | Region *SubRegion = Node->getNodeAs<Region>(); |
| 605 | BasicBlock *OldExit = SubRegion->getExit(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 606 | BasicBlock *Dominator = nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 607 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 608 | // Find all the edges from the sub region to the exit |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 609 | for (auto BBI = pred_begin(OldExit), E = pred_end(OldExit); BBI != E;) { |
| 610 | // Incrememt BBI before mucking with BB's terminator. |
| 611 | BasicBlock *BB = *BBI++; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 612 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 613 | if (!SubRegion->contains(BB)) |
| 614 | continue; |
| 615 | |
| 616 | // Modify the edges to point to the new exit |
| 617 | delPhiValues(BB, OldExit); |
| 618 | BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit); |
| 619 | addPhiValues(BB, NewExit); |
| 620 | |
| 621 | // Find the new dominator (if requested) |
| 622 | if (IncludeDominator) { |
| 623 | if (!Dominator) |
| 624 | Dominator = BB; |
| 625 | else |
| 626 | Dominator = DT->findNearestCommonDominator(Dominator, BB); |
| 627 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 630 | // Change the dominator (if requested) |
| 631 | if (Dominator) |
| 632 | DT->changeImmediateDominator(NewExit, Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 633 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 634 | // Update the region info |
| 635 | SubRegion->replaceExit(NewExit); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 636 | } else { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 637 | BasicBlock *BB = Node->getNodeAs<BasicBlock>(); |
| 638 | killTerminator(BB); |
| 639 | BranchInst::Create(NewExit, BB); |
| 640 | addPhiValues(BB, NewExit); |
| 641 | if (IncludeDominator) |
| 642 | DT->changeImmediateDominator(NewExit, BB); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 643 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 646 | /// \brief Create a new flow node and update dominator tree and region info |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 647 | BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 648 | LLVMContext &Context = Func->getContext(); |
| 649 | BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() : |
| 650 | Order.back()->getEntry(); |
| 651 | BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName, |
| 652 | Func, Insert); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 653 | DT->addNewBlock(Flow, Dominator); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 654 | ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 655 | return Flow; |
| 656 | } |
| 657 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 658 | /// \brief Create a new or reuse the previous node as flow node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 659 | BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 660 | BasicBlock *Entry = PrevNode->getEntry(); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 661 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 662 | if (!PrevNode->isSubRegion()) { |
| 663 | killTerminator(Entry); |
| 664 | if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end()) |
| 665 | return Entry; |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 666 | } |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 667 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 668 | // create a new flow node |
| 669 | BasicBlock *Flow = getNextFlow(Entry); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 670 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 671 | // and wire it up |
| 672 | changeExit(PrevNode, Flow, true); |
| 673 | PrevNode = ParentRegion->getBBNode(Flow); |
| 674 | return Flow; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /// \brief Returns the region exit if possible, otherwise just a new flow node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 678 | BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow, |
| 679 | bool ExitUseAllowed) { |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 680 | if (!Order.empty() || !ExitUseAllowed) |
| 681 | return getNextFlow(Flow); |
| 682 | |
| 683 | BasicBlock *Exit = ParentRegion->getExit(); |
| 684 | DT->changeImmediateDominator(Exit, Flow); |
| 685 | addPhiValues(Flow, Exit); |
| 686 | return Exit; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 689 | /// \brief Set the previous node |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 690 | void StructurizeCFG::setPrevNode(BasicBlock *BB) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 691 | PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) |
| 692 | : nullptr; |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 695 | /// \brief Does BB dominate all the predicates of Node? |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 696 | bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 697 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 698 | return llvm::all_of(Preds, [&](std::pair<BasicBlock *, Value *> Pred) { |
| 699 | return DT->dominates(BB, Pred.first); |
| 700 | }); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 703 | /// \brief Can we predict that this node will always be called? |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 704 | bool StructurizeCFG::isPredictableTrue(RegionNode *Node) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 705 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
| 706 | bool Dominated = false; |
| 707 | |
| 708 | // Regionentry is always true |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 709 | if (!PrevNode) |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 710 | return true; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 711 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 712 | for (std::pair<BasicBlock*, Value*> Pred : Preds) { |
| 713 | BasicBlock *BB = Pred.first; |
| 714 | Value *V = Pred.second; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 715 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 716 | if (V != BoolTrue) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 717 | return false; |
| 718 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 719 | if (!Dominated && DT->dominates(BB, PrevNode->getEntry())) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 720 | Dominated = true; |
| 721 | } |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 722 | |
| 723 | // TODO: The dominator check is too strict |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 724 | return Dominated; |
| 725 | } |
| 726 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 727 | /// Take one node from the order vector and wire it up |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 728 | void StructurizeCFG::wireFlow(bool ExitUseAllowed, |
| 729 | BasicBlock *LoopEnd) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 730 | RegionNode *Node = Order.pop_back_val(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 731 | Visited.insert(Node->getEntry()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 732 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 733 | if (isPredictableTrue(Node)) { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 734 | // Just a linear flow |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 735 | if (PrevNode) { |
| 736 | changeExit(PrevNode, Node->getEntry(), true); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 737 | } |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 738 | PrevNode = Node; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 739 | |
| 740 | } else { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 741 | // Insert extra prefix node (or reuse last one) |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 742 | BasicBlock *Flow = needPrefix(false); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 743 | |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 744 | // Insert extra postfix node (or use exit instead) |
| 745 | BasicBlock *Entry = Node->getEntry(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 746 | BasicBlock *Next = needPostfix(Flow, ExitUseAllowed); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 747 | |
| 748 | // let it point to entry and next block |
| 749 | Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow)); |
| 750 | addPhiValues(Flow, Entry); |
| 751 | DT->changeImmediateDominator(Entry, Flow); |
| 752 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 753 | PrevNode = Node; |
| 754 | while (!Order.empty() && !Visited.count(LoopEnd) && |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 755 | dominatesPredicates(Entry, Order.back())) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 756 | handleLoops(false, LoopEnd); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 759 | changeExit(PrevNode, Next, false); |
| 760 | setPrevNode(Next); |
| 761 | } |
| 762 | } |
| 763 | |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 764 | void StructurizeCFG::handleLoops(bool ExitUseAllowed, |
| 765 | BasicBlock *LoopEnd) { |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 766 | RegionNode *Node = Order.back(); |
| 767 | BasicBlock *LoopStart = Node->getEntry(); |
| 768 | |
| 769 | if (!Loops.count(LoopStart)) { |
| 770 | wireFlow(ExitUseAllowed, LoopEnd); |
| 771 | return; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 774 | if (!isPredictableTrue(Node)) |
| 775 | LoopStart = needPrefix(true); |
| 776 | |
| 777 | LoopEnd = Loops[Node->getEntry()]; |
| 778 | wireFlow(false, LoopEnd); |
| 779 | while (!Visited.count(LoopEnd)) { |
| 780 | handleLoops(false, LoopEnd); |
| 781 | } |
| 782 | |
Matt Arsenault | 6ea0aad | 2013-11-22 19:24:39 +0000 | [diff] [blame] | 783 | // If the start of the loop is the entry block, we can't branch to it so |
| 784 | // insert a new dummy entry block. |
| 785 | Function *LoopFunc = LoopStart->getParent(); |
| 786 | if (LoopStart == &LoopFunc->getEntryBlock()) { |
| 787 | LoopStart->setName("entry.orig"); |
| 788 | |
| 789 | BasicBlock *NewEntry = |
| 790 | BasicBlock::Create(LoopStart->getContext(), |
| 791 | "entry", |
| 792 | LoopFunc, |
| 793 | LoopStart); |
| 794 | BranchInst::Create(LoopStart, NewEntry); |
Serge Pavlov | 0668cd2 | 2017-01-10 02:50:47 +0000 | [diff] [blame] | 795 | DT->setNewRoot(NewEntry); |
Matt Arsenault | 6ea0aad | 2013-11-22 19:24:39 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 798 | // Create an extra loop end node |
| 799 | LoopEnd = needPrefix(false); |
| 800 | BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed); |
| 801 | LoopConds.push_back(BranchInst::Create(Next, LoopStart, |
| 802 | BoolUndef, LoopEnd)); |
| 803 | addPhiValues(LoopEnd, LoopStart); |
| 804 | setPrevNode(Next); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 807 | /// After this function control flow looks like it should be, but |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 808 | /// branches and PHI nodes only have undefined conditions. |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 809 | void StructurizeCFG::createFlow() { |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 810 | BasicBlock *Exit = ParentRegion->getExit(); |
| 811 | bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit); |
| 812 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 813 | DeletedPhis.clear(); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 814 | AddedPhis.clear(); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 815 | Conditions.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 816 | LoopConds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 817 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 818 | PrevNode = nullptr; |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 819 | Visited.clear(); |
| 820 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 821 | while (!Order.empty()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 822 | handleLoops(EntryDominatesExit, nullptr); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 825 | if (PrevNode) |
| 826 | changeExit(PrevNode, Exit, EntryDominatesExit); |
Tom Stellard | 7370ede | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 827 | else |
| 828 | assert(EntryDominatesExit); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 831 | /// Handle a rare case where the disintegrated nodes instructions |
| 832 | /// no longer dominate all their uses. Not sure if this is really nessasary |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 833 | void StructurizeCFG::rebuildSSA() { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 834 | SSAUpdater Updater; |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 835 | for (BasicBlock *BB : ParentRegion->blocks()) |
| 836 | for (Instruction &I : *BB) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 837 | bool Initialized = false; |
Justin Lebar | 96e2915 | 2016-11-29 21:49:02 +0000 | [diff] [blame] | 838 | // We may modify the use list as we iterate over it, so be careful to |
| 839 | // compute the next element in the use list at the top of the loop. |
| 840 | for (auto UI = I.use_begin(), E = I.use_end(); UI != E;) { |
| 841 | Use &U = *UI++; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 842 | Instruction *User = cast<Instruction>(U.getUser()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 843 | if (User->getParent() == BB) { |
| 844 | continue; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 845 | } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 846 | if (UserPN->getIncomingBlock(U) == BB) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 847 | continue; |
| 848 | } |
| 849 | |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 850 | if (DT->dominates(&I, User)) |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 851 | continue; |
| 852 | |
| 853 | if (!Initialized) { |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 854 | Value *Undef = UndefValue::get(I.getType()); |
| 855 | Updater.Initialize(I.getType(), ""); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 856 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
Justin Lebar | 3aec10c | 2016-11-28 18:50:03 +0000 | [diff] [blame] | 857 | Updater.AddAvailableValue(BB, &I); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 858 | Initialized = true; |
| 859 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 860 | Updater.RewriteUseAfterInsertions(U); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Justin Lebar | c7445d5 | 2016-11-22 23:13:33 +0000 | [diff] [blame] | 865 | static bool hasOnlyUniformBranches(const Region *R, |
| 866 | const DivergenceAnalysis &DA) { |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 867 | for (const BasicBlock *BB : R->blocks()) { |
| 868 | const BranchInst *Br = dyn_cast<BranchInst>(BB->getTerminator()); |
| 869 | if (!Br || !Br->isConditional()) |
| 870 | continue; |
| 871 | |
Justin Lebar | c7445d5 | 2016-11-22 23:13:33 +0000 | [diff] [blame] | 872 | if (!DA.isUniform(Br->getCondition())) |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 873 | return false; |
| 874 | DEBUG(dbgs() << "BB: " << BB->getName() << " has uniform terminator\n"); |
| 875 | } |
| 876 | return true; |
| 877 | } |
| 878 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 879 | /// \brief Run the transformation for each region found |
Matt Arsenault | d46fce1 | 2013-06-19 20:18:24 +0000 | [diff] [blame] | 880 | bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) { |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 881 | if (R->isTopLevelRegion()) |
| 882 | return false; |
| 883 | |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 884 | if (SkipUniformRegions) { |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 885 | // TODO: We could probably be smarter here with how we handle sub-regions. |
Justin Lebar | c7445d5 | 2016-11-22 23:13:33 +0000 | [diff] [blame] | 886 | auto &DA = getAnalysis<DivergenceAnalysis>(); |
| 887 | if (hasOnlyUniformBranches(R, DA)) { |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 888 | DEBUG(dbgs() << "Skipping region with uniform control flow: " << *R << '\n'); |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 889 | |
| 890 | // Mark all direct child block terminators as having been treated as |
| 891 | // uniform. To account for a possible future in which non-uniform |
| 892 | // sub-regions are treated more cleverly, indirect children are not |
| 893 | // marked as uniform. |
| 894 | MDNode *MD = MDNode::get(R->getEntry()->getParent()->getContext(), {}); |
Justin Lebar | 1b60d70 | 2016-11-22 23:13:37 +0000 | [diff] [blame] | 895 | for (RegionNode *E : R->elements()) { |
| 896 | if (E->isSubRegion()) |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 897 | continue; |
| 898 | |
Justin Lebar | 1b60d70 | 2016-11-22 23:13:37 +0000 | [diff] [blame] | 899 | if (Instruction *Term = E->getEntry()->getTerminator()) |
Nicolai Haehnle | 05b127d | 2016-04-14 17:42:35 +0000 | [diff] [blame] | 900 | Term->setMetadata("structurizecfg.uniform", MD); |
| 901 | } |
| 902 | |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 903 | return false; |
| 904 | } |
| 905 | } |
| 906 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 907 | Func = R->getEntry()->getParent(); |
| 908 | ParentRegion = R; |
| 909 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 910 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 911 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 912 | |
| 913 | orderNodes(); |
| 914 | collectInfos(); |
| 915 | createFlow(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 916 | insertConditions(false); |
| 917 | insertConditions(true); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 918 | setPhiValues(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 919 | rebuildSSA(); |
| 920 | |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 921 | // Cleanup |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 922 | Order.clear(); |
| 923 | Visited.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 924 | DeletedPhis.clear(); |
Tom Stellard | 7ec0e4f | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 925 | AddedPhis.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 926 | Predicates.clear(); |
Tom Stellard | 048f14f | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 927 | Conditions.clear(); |
Christian Konig | fc6a985 | 2013-02-16 11:27:45 +0000 | [diff] [blame] | 928 | Loops.clear(); |
| 929 | LoopPreds.clear(); |
| 930 | LoopConds.clear(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 931 | |
| 932 | return true; |
| 933 | } |
| 934 | |
Tom Stellard | 755a4e6 | 2016-02-10 00:39:37 +0000 | [diff] [blame] | 935 | Pass *llvm::createStructurizeCFGPass(bool SkipUniformRegions) { |
| 936 | return new StructurizeCFG(SkipUniformRegions); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 937 | } |