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