Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1 | //===-- AMDGPUStructurizeCFG.cpp - ------------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// The pass implemented in this file transforms the programs control flow |
| 12 | /// graph into a form that's suitable for code generation on hardware that |
| 13 | /// implements control flow by execution masking. This currently includes all |
| 14 | /// AMD GPUs but may as well be useful for other types of hardware. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "AMDGPU.h" |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SCCIterator.h" |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | 58a2cbe | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/RegionIterator.h" |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/RegionPass.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // Definition of the complex types used in this pass. |
| 31 | |
| 32 | typedef std::pair<BasicBlock *, Value *> BBValuePair; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 33 | |
| 34 | typedef SmallVector<RegionNode*, 8> RNVector; |
| 35 | typedef SmallVector<BasicBlock*, 8> BBVector; |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 36 | typedef SmallVector<BranchInst*, 8> BranchVector; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 37 | typedef SmallVector<BBValuePair, 2> BBValueVector; |
| 38 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 39 | typedef SmallPtrSet<BasicBlock *, 8> BBSet; |
| 40 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 41 | typedef DenseMap<PHINode *, BBValueVector> PhiMap; |
Christian Konig | f0e469b | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 42 | typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 43 | typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap; |
| 44 | typedef DenseMap<BasicBlock *, Value *> BBPredicates; |
| 45 | typedef DenseMap<BasicBlock *, BBPredicates> PredMap; |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 46 | typedef DenseMap<BasicBlock *, BBVector> BB2BBVecMap; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 47 | |
| 48 | // The name for newly created blocks. |
| 49 | |
| 50 | static const char *FlowBlockName = "Flow"; |
| 51 | |
Christian Konig | f0e469b | 2013-02-16 11:27:29 +0000 | [diff] [blame] | 52 | /// @brief Find the nearest common dominator for multiple BasicBlocks |
| 53 | /// |
| 54 | /// Helper class for AMDGPUStructurizeCFG |
| 55 | /// TODO: Maybe move into common code |
| 56 | class NearestCommonDominator { |
| 57 | |
| 58 | DominatorTree *DT; |
| 59 | |
| 60 | DTN2UnsignedMap IndexMap; |
| 61 | |
| 62 | BasicBlock *Result; |
| 63 | unsigned ResultIndex; |
| 64 | bool ExplicitMentioned; |
| 65 | |
| 66 | public: |
| 67 | /// \brief Start a new query |
| 68 | NearestCommonDominator(DominatorTree *DomTree) { |
| 69 | DT = DomTree; |
| 70 | Result = 0; |
| 71 | } |
| 72 | |
| 73 | /// \brief Add BB to the resulting dominator |
| 74 | void addBlock(BasicBlock *BB, bool Remember = true) { |
| 75 | |
| 76 | DomTreeNode *Node = DT->getNode(BB); |
| 77 | |
| 78 | if (Result == 0) { |
| 79 | unsigned Numbering = 0; |
| 80 | for (;Node;Node = Node->getIDom()) |
| 81 | IndexMap[Node] = ++Numbering; |
| 82 | Result = BB; |
| 83 | ResultIndex = 1; |
| 84 | ExplicitMentioned = Remember; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | for (;Node;Node = Node->getIDom()) |
| 89 | if (IndexMap.count(Node)) |
| 90 | break; |
| 91 | else |
| 92 | IndexMap[Node] = 0; |
| 93 | |
| 94 | assert(Node && "Dominator tree invalid!"); |
| 95 | |
| 96 | unsigned Numbering = IndexMap[Node]; |
| 97 | if (Numbering > ResultIndex) { |
| 98 | Result = Node->getBlock(); |
| 99 | ResultIndex = Numbering; |
| 100 | ExplicitMentioned = Remember && (Result == BB); |
| 101 | } else if (Numbering == ResultIndex) { |
| 102 | ExplicitMentioned |= Remember; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// \brief Is "Result" one of the BBs added with "Remember" = True? |
| 107 | bool wasResultExplicitMentioned() { |
| 108 | return ExplicitMentioned; |
| 109 | } |
| 110 | |
| 111 | /// \brief Get the query result |
| 112 | BasicBlock *getResult() { |
| 113 | return Result; |
| 114 | } |
| 115 | }; |
| 116 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 117 | /// @brief Transforms the control flow graph on one single entry/exit region |
| 118 | /// at a time. |
| 119 | /// |
| 120 | /// After the transform all "If"/"Then"/"Else" style control flow looks like |
| 121 | /// this: |
| 122 | /// |
| 123 | /// \verbatim |
| 124 | /// 1 |
| 125 | /// || |
| 126 | /// | | |
| 127 | /// 2 | |
| 128 | /// | / |
| 129 | /// |/ |
| 130 | /// 3 |
| 131 | /// || Where: |
| 132 | /// | | 1 = "If" block, calculates the condition |
| 133 | /// 4 | 2 = "Then" subregion, runs if the condition is true |
| 134 | /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow |
| 135 | /// |/ 4 = "Else" optional subregion, runs if the condition is false |
| 136 | /// 5 5 = "End" block, also rejoins the control flow |
| 137 | /// \endverbatim |
| 138 | /// |
| 139 | /// Control flow is expressed as a branch where the true exit goes into the |
| 140 | /// "Then"/"Else" region, while the false exit skips the region |
| 141 | /// The condition for the optional "Else" region is expressed as a PHI node. |
| 142 | /// The incomming values of the PHI node are true for the "If" edge and false |
| 143 | /// for the "Then" edge. |
| 144 | /// |
| 145 | /// Additionally to that even complicated loops look like this: |
| 146 | /// |
| 147 | /// \verbatim |
| 148 | /// 1 |
| 149 | /// || |
| 150 | /// | | |
| 151 | /// 2 ^ Where: |
| 152 | /// | / 1 = "Entry" block |
| 153 | /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block |
| 154 | /// 3 3 = "Flow" block, with back edge to entry block |
| 155 | /// | |
| 156 | /// \endverbatim |
| 157 | /// |
| 158 | /// The back edge of the "Flow" block is always on the false side of the branch |
| 159 | /// while the true side continues the general flow. So the loop condition |
| 160 | /// consist of a network of PHI nodes where the true incoming values expresses |
| 161 | /// breaks and the false values expresses continue states. |
| 162 | class AMDGPUStructurizeCFG : public RegionPass { |
| 163 | |
| 164 | static char ID; |
| 165 | |
| 166 | Type *Boolean; |
| 167 | ConstantInt *BoolTrue; |
| 168 | ConstantInt *BoolFalse; |
| 169 | UndefValue *BoolUndef; |
| 170 | |
| 171 | Function *Func; |
| 172 | Region *ParentRegion; |
| 173 | |
| 174 | DominatorTree *DT; |
| 175 | |
| 176 | RNVector Order; |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 177 | BBSet Visited; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 178 | PredMap Predicates; |
| 179 | BBPhiMap DeletedPhis; |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 180 | BB2BBVecMap AddedPhis; |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 181 | BranchVector Conditions; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 182 | |
| 183 | BasicBlock *LoopStart; |
| 184 | BasicBlock *LoopEnd; |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 185 | BBSet LoopTargets; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 186 | BBPredicates LoopPred; |
| 187 | |
| 188 | void orderNodes(); |
| 189 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 190 | Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 191 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 192 | bool analyzeLoopStart(BasicBlock *From, BasicBlock *To, Value *Condition); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 193 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 194 | void analyzeNode(RegionNode *N); |
| 195 | |
| 196 | void analyzeLoopEnd(RegionNode *N); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 197 | |
| 198 | void collectInfos(); |
| 199 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 200 | void insertConditions(); |
| 201 | |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 202 | void delPhiValues(BasicBlock *From, BasicBlock *To); |
| 203 | |
| 204 | void addPhiValues(BasicBlock *From, BasicBlock *To); |
| 205 | |
| 206 | void setPhiValues(); |
| 207 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 208 | void killTerminator(BasicBlock *BB); |
| 209 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 210 | void changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 211 | bool IncludeDominator); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 212 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 213 | BasicBlock *getNextFlow(BasicBlock *Dominator); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 214 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 215 | BasicBlock *needPrefix(RegionNode *&Prev, RegionNode *Node); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 216 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 217 | BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed); |
| 218 | |
| 219 | RegionNode *getNextPrev(BasicBlock *Next); |
| 220 | |
| 221 | bool dominatesPredicates(BasicBlock *BB, RegionNode *Node); |
| 222 | |
| 223 | bool isPredictableTrue(RegionNode *Who, RegionNode *Where); |
| 224 | |
| 225 | RegionNode *wireFlow(RegionNode *&Prev, bool ExitUseAllowed); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 226 | |
| 227 | void createFlow(); |
| 228 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 229 | void rebuildSSA(); |
| 230 | |
| 231 | public: |
| 232 | AMDGPUStructurizeCFG(): |
| 233 | RegionPass(ID) { |
| 234 | |
| 235 | initializeRegionInfoPass(*PassRegistry::getPassRegistry()); |
| 236 | } |
| 237 | |
| 238 | virtual bool doInitialization(Region *R, RGPassManager &RGM); |
| 239 | |
| 240 | virtual bool runOnRegion(Region *R, RGPassManager &RGM); |
| 241 | |
| 242 | virtual const char *getPassName() const { |
| 243 | return "AMDGPU simplify control flow"; |
| 244 | } |
| 245 | |
| 246 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 247 | |
| 248 | AU.addRequired<DominatorTree>(); |
| 249 | AU.addPreserved<DominatorTree>(); |
| 250 | RegionPass::getAnalysisUsage(AU); |
| 251 | } |
| 252 | |
| 253 | }; |
| 254 | |
| 255 | } // end anonymous namespace |
| 256 | |
| 257 | char AMDGPUStructurizeCFG::ID = 0; |
| 258 | |
| 259 | /// \brief Initialize the types and constants used in the pass |
| 260 | bool AMDGPUStructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 261 | LLVMContext &Context = R->getEntry()->getContext(); |
| 262 | |
| 263 | Boolean = Type::getInt1Ty(Context); |
| 264 | BoolTrue = ConstantInt::getTrue(Context); |
| 265 | BoolFalse = ConstantInt::getFalse(Context); |
| 266 | BoolUndef = UndefValue::get(Boolean); |
| 267 | |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | /// \brief Build up the general order of nodes |
| 272 | void AMDGPUStructurizeCFG::orderNodes() { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 273 | scc_iterator<Region *> I = scc_begin(ParentRegion), |
| 274 | E = scc_end(ParentRegion); |
| 275 | for (Order.clear(); I != E; ++I) { |
| 276 | std::vector<RegionNode *> &Nodes = *I; |
| 277 | Order.append(Nodes.begin(), Nodes.end()); |
| 278 | } |
| 279 | } |
| 280 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 281 | /// \brief Build the condition for one edge |
| 282 | Value *AMDGPUStructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx, |
| 283 | bool Invert) { |
| 284 | Value *Cond = Invert ? BoolFalse : BoolTrue; |
| 285 | if (Term->isConditional()) { |
| 286 | Cond = Term->getCondition(); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 287 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 288 | if (Idx != Invert) |
| 289 | Cond = BinaryOperator::CreateNot(Cond, "", Term); |
| 290 | } |
| 291 | return Cond; |
| 292 | } |
| 293 | |
| 294 | /// \brief Analyze the start of a loop and insert predicates as necessary |
| 295 | bool AMDGPUStructurizeCFG::analyzeLoopStart(BasicBlock *From, BasicBlock *To, |
| 296 | Value *Condition) { |
| 297 | LoopPred[From] = Condition; |
| 298 | LoopTargets.insert(To); |
| 299 | if (!LoopStart) { |
| 300 | LoopStart = To; |
| 301 | return true; |
| 302 | |
| 303 | } else if (LoopStart == To) |
| 304 | return true; |
| 305 | |
| 306 | // We need to handle the case of intersecting loops, e. g. |
| 307 | // |
| 308 | // /----<----- |
| 309 | // | | |
| 310 | // -> A -> B -> C -> D |
| 311 | // | | |
| 312 | // -----<----/ |
| 313 | |
| 314 | RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend(); |
| 315 | |
| 316 | for (;OI != OE; ++OI) |
| 317 | if ((*OI)->getEntry() == LoopStart) |
| 318 | break; |
| 319 | |
| 320 | for (;OI != OE && (*OI)->getEntry() != To; ++OI) { |
| 321 | BBPredicates &Pred = Predicates[(*OI)->getEntry()]; |
| 322 | if (!Pred.count(From)) |
| 323 | Pred[From] = Condition; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /// \brief Analyze the predecessors of each block and build up predicates |
| 329 | void AMDGPUStructurizeCFG::analyzeNode(RegionNode *N) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 330 | RegionInfo *RI = ParentRegion->getRegionInfo(); |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 331 | BasicBlock *BB = N->getEntry(); |
| 332 | BBPredicates &Pred = Predicates[BB]; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 333 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 334 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 335 | PI != PE; ++PI) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 336 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 337 | if (!ParentRegion->contains(*PI)) { |
| 338 | // It's a branch from outside into our region entry |
| 339 | Pred[*PI] = BoolTrue; |
| 340 | continue; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 343 | Region *R = RI->getRegionFor(*PI); |
| 344 | if (R == ParentRegion) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 345 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 346 | // It's a top level block in our region |
| 347 | BranchInst *Term = cast<BranchInst>((*PI)->getTerminator()); |
| 348 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 349 | BasicBlock *Succ = Term->getSuccessor(i); |
| 350 | if (Succ != BB) |
| 351 | continue; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 352 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 353 | if (Visited.count(*PI)) { |
| 354 | // Normal forward edge |
| 355 | if (Term->isConditional()) { |
| 356 | // Try to treat it like an ELSE block |
| 357 | BasicBlock *Other = Term->getSuccessor(!i); |
| 358 | if (Visited.count(Other) && !LoopTargets.count(Other) && |
| 359 | !Pred.count(Other) && !Pred.count(*PI)) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 360 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 361 | Pred[Other] = BoolFalse; |
| 362 | Pred[*PI] = BoolTrue; |
| 363 | continue; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | } else { |
| 368 | // Back edge |
| 369 | if (analyzeLoopStart(*PI, BB, buildCondition(Term, i, true))) |
| 370 | continue; |
| 371 | } |
| 372 | Pred[*PI] = buildCondition(Term, i, false); |
| 373 | } |
| 374 | |
| 375 | } else { |
| 376 | |
| 377 | // It's an exit from a sub region |
| 378 | while(R->getParent() != ParentRegion) |
| 379 | R = R->getParent(); |
| 380 | |
| 381 | // Edge from inside a subregion to its entry, ignore it |
| 382 | if (R == N) |
| 383 | continue; |
| 384 | |
| 385 | BasicBlock *Entry = R->getEntry(); |
| 386 | if (!Visited.count(Entry)) |
| 387 | if (analyzeLoopStart(Entry, BB, BoolFalse)) |
| 388 | continue; |
| 389 | |
| 390 | Pred[Entry] = BoolTrue; |
| 391 | } |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 395 | /// \brief Determine the end of the loop |
| 396 | void AMDGPUStructurizeCFG::analyzeLoopEnd(RegionNode *N) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 397 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 398 | if (N->isSubRegion()) { |
| 399 | // Test for exit as back edge |
| 400 | BasicBlock *Exit = N->getNodeAs<Region>()->getExit(); |
| 401 | if (Visited.count(Exit)) |
| 402 | LoopEnd = N->getEntry(); |
| 403 | |
| 404 | } else { |
| 405 | // Test for sucessors as back edge |
| 406 | BasicBlock *BB = N->getNodeAs<BasicBlock>(); |
| 407 | BranchInst *Term = cast<BranchInst>(BB->getTerminator()); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 408 | |
| 409 | for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { |
| 410 | BasicBlock *Succ = Term->getSuccessor(i); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 411 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 412 | if (Visited.count(Succ)) |
| 413 | LoopEnd = BB; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /// \brief Collect various loop and predicate infos |
| 419 | void AMDGPUStructurizeCFG::collectInfos() { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 420 | |
| 421 | // Reset predicate |
| 422 | Predicates.clear(); |
| 423 | |
| 424 | // and loop infos |
| 425 | LoopStart = LoopEnd = 0; |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 426 | LoopTargets.clear(); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 427 | LoopPred.clear(); |
| 428 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 429 | // Reset the visited nodes |
| 430 | Visited.clear(); |
| 431 | |
| 432 | for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend(); |
| 433 | OI != OE; ++OI) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 434 | |
| 435 | // Analyze all the conditions leading to a node |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 436 | analyzeNode(*OI); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 437 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 438 | // Remember that we've seen this node |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 439 | Visited.insert((*OI)->getEntry()); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 440 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 441 | // Find the last back edge |
| 442 | analyzeLoopEnd(*OI); |
| 443 | } |
| 444 | |
| 445 | // Both or neither must be set |
| 446 | assert(!LoopStart == !LoopEnd); |
| 447 | } |
| 448 | |
| 449 | /// \brief Insert the missing branch conditions |
| 450 | void AMDGPUStructurizeCFG::insertConditions() { |
| 451 | SSAUpdater PhiInserter; |
| 452 | |
| 453 | for (BranchVector::iterator I = Conditions.begin(), |
| 454 | E = Conditions.end(); I != E; ++I) { |
| 455 | |
| 456 | BranchInst *Term = *I; |
| 457 | BasicBlock *Parent = Term->getParent(); |
| 458 | |
| 459 | assert(Term->isConditional()); |
| 460 | |
| 461 | PhiInserter.Initialize(Boolean, ""); |
| 462 | if (Parent == LoopEnd) { |
| 463 | PhiInserter.AddAvailableValue(LoopStart, BoolTrue); |
| 464 | } else { |
| 465 | PhiInserter.AddAvailableValue(&Func->getEntryBlock(), BoolFalse); |
| 466 | PhiInserter.AddAvailableValue(Parent, BoolFalse); |
| 467 | } |
| 468 | |
| 469 | bool ParentHasValue = false; |
| 470 | BasicBlock *Succ = Term->getSuccessor(0); |
| 471 | BBPredicates &Preds = (Parent == LoopEnd) ? LoopPred : Predicates[Succ]; |
| 472 | for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); |
| 473 | PI != PE; ++PI) { |
| 474 | |
| 475 | PhiInserter.AddAvailableValue(PI->first, PI->second); |
| 476 | ParentHasValue |= PI->first == Parent; |
| 477 | } |
| 478 | |
| 479 | if (ParentHasValue) |
| 480 | Term->setCondition(PhiInserter.GetValueAtEndOfBlock(Parent)); |
| 481 | else |
| 482 | Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent)); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 486 | /// \brief Remove all PHI values coming from "From" into "To" and remember |
| 487 | /// them in DeletedPhis |
| 488 | void AMDGPUStructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) { |
| 489 | PhiMap &Map = DeletedPhis[To]; |
| 490 | for (BasicBlock::iterator I = To->begin(), E = To->end(); |
| 491 | I != E && isa<PHINode>(*I);) { |
| 492 | |
| 493 | PHINode &Phi = cast<PHINode>(*I++); |
| 494 | while (Phi.getBasicBlockIndex(From) != -1) { |
| 495 | Value *Deleted = Phi.removeIncomingValue(From, false); |
| 496 | Map[&Phi].push_back(std::make_pair(From, Deleted)); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /// \brief Add a dummy PHI value as soon as we knew the new predecessor |
| 502 | void AMDGPUStructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) { |
| 503 | for (BasicBlock::iterator I = To->begin(), E = To->end(); |
| 504 | I != E && isa<PHINode>(*I);) { |
| 505 | |
| 506 | PHINode &Phi = cast<PHINode>(*I++); |
| 507 | Value *Undef = UndefValue::get(Phi.getType()); |
| 508 | Phi.addIncoming(Undef, From); |
| 509 | } |
| 510 | AddedPhis[To].push_back(From); |
| 511 | } |
| 512 | |
| 513 | /// \brief Add the real PHI value as soon as everything is set up |
| 514 | void AMDGPUStructurizeCFG::setPhiValues() { |
| 515 | |
| 516 | SSAUpdater Updater; |
| 517 | for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end(); |
| 518 | AI != AE; ++AI) { |
| 519 | |
| 520 | BasicBlock *To = AI->first; |
| 521 | BBVector &From = AI->second; |
| 522 | |
| 523 | if (!DeletedPhis.count(To)) |
| 524 | continue; |
| 525 | |
| 526 | PhiMap &Map = DeletedPhis[To]; |
| 527 | for (PhiMap::iterator PI = Map.begin(), PE = Map.end(); |
| 528 | PI != PE; ++PI) { |
| 529 | |
| 530 | PHINode *Phi = PI->first; |
| 531 | Value *Undef = UndefValue::get(Phi->getType()); |
| 532 | Updater.Initialize(Phi->getType(), ""); |
| 533 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 534 | Updater.AddAvailableValue(To, Undef); |
| 535 | |
Christian Konig | 4c79c71 | 2013-02-16 11:27:35 +0000 | [diff] [blame^] | 536 | NearestCommonDominator Dominator(DT); |
| 537 | Dominator.addBlock(To, false); |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 538 | for (BBValueVector::iterator VI = PI->second.begin(), |
| 539 | VE = PI->second.end(); VI != VE; ++VI) { |
| 540 | |
| 541 | Updater.AddAvailableValue(VI->first, VI->second); |
Christian Konig | 4c79c71 | 2013-02-16 11:27:35 +0000 | [diff] [blame^] | 542 | Dominator.addBlock(VI->first); |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Christian Konig | 4c79c71 | 2013-02-16 11:27:35 +0000 | [diff] [blame^] | 545 | if (!Dominator.wasResultExplicitMentioned()) |
| 546 | Updater.AddAvailableValue(Dominator.getResult(), Undef); |
| 547 | |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 548 | for (BBVector::iterator FI = From.begin(), FE = From.end(); |
| 549 | FI != FE; ++FI) { |
| 550 | |
| 551 | int Idx = Phi->getBasicBlockIndex(*FI); |
| 552 | assert(Idx != -1); |
| 553 | Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI)); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | DeletedPhis.erase(To); |
| 558 | } |
| 559 | assert(DeletedPhis.empty()); |
| 560 | } |
| 561 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 562 | /// \brief Remove phi values from all successors and then remove the terminator. |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 563 | void AMDGPUStructurizeCFG::killTerminator(BasicBlock *BB) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 564 | TerminatorInst *Term = BB->getTerminator(); |
| 565 | if (!Term) |
| 566 | return; |
| 567 | |
| 568 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 569 | SI != SE; ++SI) { |
| 570 | |
| 571 | delPhiValues(BB, *SI); |
| 572 | } |
| 573 | |
| 574 | Term->eraseFromParent(); |
| 575 | } |
| 576 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 577 | /// \brief Let node exit(s) point to NewExit |
| 578 | void AMDGPUStructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit, |
| 579 | bool IncludeDominator) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 580 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 581 | if (Node->isSubRegion()) { |
| 582 | Region *SubRegion = Node->getNodeAs<Region>(); |
| 583 | BasicBlock *OldExit = SubRegion->getExit(); |
| 584 | BasicBlock *Dominator = 0; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 585 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 586 | // Find all the edges from the sub region to the exit |
| 587 | for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit); |
| 588 | I != E;) { |
| 589 | |
| 590 | BasicBlock *BB = *I++; |
| 591 | if (!SubRegion->contains(BB)) |
| 592 | continue; |
| 593 | |
| 594 | // Modify the edges to point to the new exit |
| 595 | delPhiValues(BB, OldExit); |
| 596 | BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit); |
| 597 | addPhiValues(BB, NewExit); |
| 598 | |
| 599 | // Find the new dominator (if requested) |
| 600 | if (IncludeDominator) { |
| 601 | if (!Dominator) |
| 602 | Dominator = BB; |
| 603 | else |
| 604 | Dominator = DT->findNearestCommonDominator(Dominator, BB); |
| 605 | } |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 608 | // Change the dominator (if requested) |
| 609 | if (Dominator) |
| 610 | DT->changeImmediateDominator(NewExit, Dominator); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 611 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 612 | // Update the region info |
| 613 | SubRegion->replaceExit(NewExit); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 614 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 615 | } else { |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 616 | BasicBlock *BB = Node->getNodeAs<BasicBlock>(); |
| 617 | killTerminator(BB); |
| 618 | BranchInst::Create(NewExit, BB); |
| 619 | addPhiValues(BB, NewExit); |
| 620 | if (IncludeDominator) |
| 621 | DT->changeImmediateDominator(NewExit, BB); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 622 | } |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 625 | /// \brief Create a new flow node and update dominator tree and region info |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 626 | BasicBlock *AMDGPUStructurizeCFG::getNextFlow(BasicBlock *Dominator) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 627 | LLVMContext &Context = Func->getContext(); |
| 628 | BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() : |
| 629 | Order.back()->getEntry(); |
| 630 | BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName, |
| 631 | Func, Insert); |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 632 | DT->addNewBlock(Flow, Dominator); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 633 | ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 634 | return Flow; |
| 635 | } |
| 636 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 637 | /// \brief Create a new or reuse the previous node as flow node |
| 638 | BasicBlock *AMDGPUStructurizeCFG::needPrefix(RegionNode *&Prev, |
| 639 | RegionNode *Node) { |
| 640 | |
| 641 | if (!Prev || Prev->isSubRegion() || |
| 642 | (Node && Node->getEntry() == LoopStart)) { |
| 643 | |
| 644 | // We need to insert a flow node, first figure out the dominator |
| 645 | DomTreeNode *Dominator = Prev ? DT->getNode(Prev->getEntry()) : 0; |
| 646 | if (!Dominator) |
| 647 | Dominator = DT->getNode(Node->getEntry())->getIDom(); |
| 648 | assert(Dominator && "Illegal loop to function entry"); |
| 649 | |
| 650 | // then create the flow node |
| 651 | BasicBlock *Flow = getNextFlow(Dominator->getBlock()); |
| 652 | |
| 653 | // wire up the new flow |
| 654 | if (Prev) { |
| 655 | changeExit(Prev, Flow, true); |
| 656 | } else { |
| 657 | // Parent regions entry needs predicates, create a new region entry |
| 658 | BasicBlock *Entry = Node->getEntry(); |
| 659 | for (pred_iterator I = pred_begin(Entry), E = pred_end(Entry); |
| 660 | I != E;) { |
| 661 | |
| 662 | BasicBlock *BB = *(I++); |
| 663 | if (ParentRegion->contains(BB)) |
| 664 | continue; |
| 665 | |
| 666 | // Remove PHY values from outside to our entry node |
| 667 | delPhiValues(BB, Entry); |
| 668 | |
| 669 | // Update the branch instructions |
| 670 | BB->getTerminator()->replaceUsesOfWith(Entry, Flow); |
| 671 | } |
| 672 | |
| 673 | // Populate the region tree with the new entry |
| 674 | for (Region *R = ParentRegion; R && R->getEntry() == Entry; |
| 675 | R = R->getParent()) { |
| 676 | R->replaceEntry(Flow); |
| 677 | } |
| 678 | } |
| 679 | Prev = ParentRegion->getBBNode(Flow); |
| 680 | |
| 681 | } else { |
| 682 | killTerminator(Prev->getEntry()); |
| 683 | } |
| 684 | |
| 685 | return Prev->getEntry(); |
| 686 | } |
| 687 | |
| 688 | /// \brief Returns the region exit if possible, otherwise just a new flow node |
| 689 | BasicBlock *AMDGPUStructurizeCFG::needPostfix(BasicBlock *Flow, |
| 690 | bool ExitUseAllowed) { |
| 691 | |
| 692 | if (Order.empty() && ExitUseAllowed) { |
| 693 | BasicBlock *Exit = ParentRegion->getExit(); |
| 694 | DT->changeImmediateDominator(Exit, Flow); |
| 695 | addPhiValues(Flow, Exit); |
| 696 | return Exit; |
| 697 | } |
| 698 | return getNextFlow(Flow); |
| 699 | } |
| 700 | |
| 701 | /// \brief Returns the region node for Netx, or null if Next is the exit |
| 702 | RegionNode *AMDGPUStructurizeCFG::getNextPrev(BasicBlock *Next) { |
| 703 | return ParentRegion->contains(Next) ? ParentRegion->getBBNode(Next) : 0; |
| 704 | } |
| 705 | |
| 706 | /// \brief Does BB dominate all the predicates of Node ? |
| 707 | bool AMDGPUStructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) { |
| 708 | BBPredicates &Preds = Predicates[Node->getEntry()]; |
| 709 | for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); |
| 710 | PI != PE; ++PI) { |
| 711 | |
| 712 | if (!DT->dominates(BB, PI->first)) |
| 713 | return false; |
| 714 | } |
| 715 | return true; |
| 716 | } |
| 717 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 718 | /// \brief Can we predict that this node will always be called? |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 719 | bool AMDGPUStructurizeCFG::isPredictableTrue(RegionNode *Who, |
| 720 | RegionNode *Where) { |
| 721 | |
| 722 | BBPredicates &Preds = Predicates[Who->getEntry()]; |
| 723 | bool Dominated = Where == 0; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 724 | |
| 725 | for (BBPredicates::iterator I = Preds.begin(), E = Preds.end(); |
| 726 | I != E; ++I) { |
| 727 | |
| 728 | if (I->second != BoolTrue) |
| 729 | return false; |
| 730 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 731 | if (!Dominated && DT->dominates(I->first, Where->getEntry())) |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 732 | Dominated = true; |
| 733 | } |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 734 | |
| 735 | // TODO: The dominator check is too strict |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 736 | return Dominated; |
| 737 | } |
| 738 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 739 | /// Take one node from the order vector and wire it up |
| 740 | RegionNode *AMDGPUStructurizeCFG::wireFlow(RegionNode *&Prev, |
| 741 | bool ExitUseAllowed) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 742 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 743 | RegionNode *Node = Order.pop_back_val(); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 744 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 745 | if (isPredictableTrue(Node, Prev)) { |
| 746 | // Just a linear flow |
| 747 | if (Prev) { |
| 748 | changeExit(Prev, Node->getEntry(), true); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 749 | } |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 750 | Prev = Node; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 751 | |
| 752 | } else { |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 753 | // Insert extra prefix node (or reuse last one) |
| 754 | BasicBlock *Flow = needPrefix(Prev, Node); |
| 755 | if (Node->getEntry() == LoopStart) |
| 756 | LoopStart = Flow; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 757 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 758 | // Insert extra postfix node (or use exit instead) |
| 759 | BasicBlock *Entry = Node->getEntry(); |
| 760 | BasicBlock *Next = needPostfix(Flow, ExitUseAllowed && Entry != LoopEnd); |
| 761 | |
| 762 | // let it point to entry and next block |
| 763 | Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow)); |
| 764 | addPhiValues(Flow, Entry); |
| 765 | DT->changeImmediateDominator(Entry, Flow); |
| 766 | |
| 767 | Prev = Node; |
| 768 | while (!Order.empty() && Node->getEntry() != LoopEnd && |
| 769 | !LoopTargets.count(Order.back()->getEntry()) && |
| 770 | dominatesPredicates(Entry, Order.back())) { |
| 771 | Node = wireFlow(Prev, false); |
| 772 | } |
| 773 | |
| 774 | changeExit(Prev, Next, false); |
| 775 | Prev = getNextPrev(Next); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 778 | return Node; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 781 | /// After this function control flow looks like it should be, but |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 782 | /// branches and PHI nodes only have undefined conditions. |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 783 | void AMDGPUStructurizeCFG::createFlow() { |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 784 | |
| 785 | BasicBlock *Exit = ParentRegion->getExit(); |
| 786 | bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit); |
| 787 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 788 | DeletedPhis.clear(); |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 789 | AddedPhis.clear(); |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 790 | Conditions.clear(); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 791 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 792 | RegionNode *Prev = 0; |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 793 | while (!Order.empty()) { |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 794 | |
| 795 | RegionNode *Node = wireFlow(Prev, EntryDominatesExit); |
| 796 | |
| 797 | // Create an extra loop end node |
| 798 | if (Node->getEntry() == LoopEnd) { |
| 799 | LoopEnd = needPrefix(Prev, 0); |
| 800 | BasicBlock *Next = needPostfix(LoopEnd, EntryDominatesExit); |
| 801 | |
| 802 | Conditions.push_back(BranchInst::Create(Next, LoopStart, |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 803 | BoolUndef, LoopEnd)); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 804 | addPhiValues(LoopEnd, LoopStart); |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 805 | Prev = getNextPrev(Next); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Tom Stellard | f4e471a | 2013-02-08 22:24:38 +0000 | [diff] [blame] | 809 | if (Prev) |
| 810 | changeExit(Prev, Exit, EntryDominatesExit); |
| 811 | else |
| 812 | assert(EntryDominatesExit); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 815 | /// Handle a rare case where the disintegrated nodes instructions |
| 816 | /// no longer dominate all their uses. Not sure if this is really nessasary |
| 817 | void AMDGPUStructurizeCFG::rebuildSSA() { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 818 | SSAUpdater Updater; |
| 819 | for (Region::block_iterator I = ParentRegion->block_begin(), |
| 820 | E = ParentRegion->block_end(); |
| 821 | I != E; ++I) { |
| 822 | |
| 823 | BasicBlock *BB = *I; |
| 824 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); |
| 825 | II != IE; ++II) { |
| 826 | |
| 827 | bool Initialized = false; |
| 828 | for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) { |
| 829 | |
| 830 | Next = I->getNext(); |
| 831 | |
| 832 | Instruction *User = cast<Instruction>(I->getUser()); |
| 833 | if (User->getParent() == BB) { |
| 834 | continue; |
| 835 | |
| 836 | } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) { |
| 837 | if (UserPN->getIncomingBlock(*I) == BB) |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | if (DT->dominates(II, User)) |
| 842 | continue; |
| 843 | |
| 844 | if (!Initialized) { |
| 845 | Value *Undef = UndefValue::get(II->getType()); |
| 846 | Updater.Initialize(II->getType(), ""); |
| 847 | Updater.AddAvailableValue(&Func->getEntryBlock(), Undef); |
| 848 | Updater.AddAvailableValue(BB, II); |
| 849 | Initialized = true; |
| 850 | } |
| 851 | Updater.RewriteUseAfterInsertions(*I); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /// \brief Run the transformation for each region found |
| 858 | bool AMDGPUStructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) { |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 859 | if (R->isTopLevelRegion()) |
| 860 | return false; |
| 861 | |
| 862 | Func = R->getEntry()->getParent(); |
| 863 | ParentRegion = R; |
| 864 | |
| 865 | DT = &getAnalysis<DominatorTree>(); |
| 866 | |
| 867 | orderNodes(); |
| 868 | collectInfos(); |
| 869 | createFlow(); |
| 870 | insertConditions(); |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 871 | setPhiValues(); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 872 | rebuildSSA(); |
| 873 | |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 874 | // Cleanup |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 875 | Order.clear(); |
| 876 | Visited.clear(); |
| 877 | Predicates.clear(); |
| 878 | DeletedPhis.clear(); |
Tom Stellard | 13cf6cb | 2013-02-08 22:24:35 +0000 | [diff] [blame] | 879 | AddedPhis.clear(); |
Tom Stellard | 27f5d06 | 2013-02-08 22:24:37 +0000 | [diff] [blame] | 880 | Conditions.clear(); |
| 881 | LoopTargets.clear(); |
| 882 | LoopPred.clear(); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 883 | |
| 884 | return true; |
| 885 | } |
| 886 | |
| 887 | /// \brief Create the pass |
| 888 | Pass *llvm::createAMDGPUStructurizeCFGPass() { |
| 889 | return new AMDGPUStructurizeCFG(); |
| 890 | } |