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