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