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