llvm.org GIT mirror llvm / 2aaf722
[TI removal] Make variables declared as `TerminatorInst` and initialized by `getTerminator()` calls instead be declared as `Instruction`. This is the biggest remaining chunk of the usage of `getTerminator()` that insists on the narrow type and so is an easy batch of updates. Several files saw more extensive updates where this would cascade to requiring API updates within the file to use `Instruction` instead of `TerminatorInst`. All of these were trivial in nature (pervasively using `Instruction` instead just worked). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@344502 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 1 year, 1 month ago
58 changed file(s) with 143 addition(s) and 144 deletion(s). Raw diff Collapse all Expand all
134134 /// Add \p BB to PostDominatedByUnreachable set if applicable.
135135 void
136136 BranchProbabilityInfo::updatePostDominatedByUnreachable(const BasicBlock *BB) {
137 const TerminatorInst *TI = BB->getTerminator();
137 const Instruction *TI = BB->getTerminator();
138138 if (TI->getNumSuccessors() == 0) {
139139 if (isa(TI) ||
140140 // If this block is terminated by a call to
166166 void
167167 BranchProbabilityInfo::updatePostDominatedByColdCall(const BasicBlock *BB) {
168168 assert(!PostDominatedByColdCall.count(BB));
169 const TerminatorInst *TI = BB->getTerminator();
169 const Instruction *TI = BB->getTerminator();
170170 if (TI->getNumSuccessors() == 0)
171171 return;
172172
201201 /// Predict that a successor which leads necessarily to an
202202 /// unreachable-terminated block as extremely unlikely.
203203 bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) {
204 const TerminatorInst *TI = BB->getTerminator();
204 const Instruction *TI = BB->getTerminator();
205205 (void) TI;
206206 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
207207 assert(!isa(TI) &&
245245 // heuristic. The probability of the edge coming to unreachable block is
246246 // set to min of metadata and unreachable heuristic.
247247 bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
248 const TerminatorInst *TI = BB->getTerminator();
248 const Instruction *TI = BB->getTerminator();
249249 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
250250 if (!(isa(TI) || isa(TI) || isa(TI)))
251251 return false;
347347 /// Return true if we could compute the weights for cold edges.
348348 /// Return false, otherwise.
349349 bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) {
350 const TerminatorInst *TI = BB->getTerminator();
350 const Instruction *TI = BB->getTerminator();
351351 (void) TI;
352352 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
353353 assert(!isa(TI) &&
7070 /// successor.
7171 unsigned llvm::GetSuccessorNumber(const BasicBlock *BB,
7272 const BasicBlock *Succ) {
73 const TerminatorInst *Term = BB->getTerminator();
73 const Instruction *Term = BB->getTerminator();
7474 #ifndef NDEBUG
7575 unsigned e = Term->getNumSuccessors();
7676 #endif
119119 << "\'.\n");
120120
121121 BasicBlock *SuccColor = Color;
122 TerminatorInst *Terminator = Visiting->getTerminator();
122 Instruction *Terminator = Visiting->getTerminator();
123123 if (auto *CatchRet = dyn_cast(Terminator)) {
124124 Value *ParentPad = CatchRet->getCatchSwitchParentPad();
125125 if (isa(ParentPad))
18301830 if (!IR)
18311831 return IR;
18321832
1833 TerminatorInst *TI = BB->getTerminator();
1833 Instruction *TI = BB->getTerminator();
18341834
18351835 // Add in the live successors by first checking whether we have terminator
18361836 // that may be simplified based on the values simplified by this call.
218218 SmallVector LatchesBlocks;
219219 getLoopLatches(LatchesBlocks);
220220 for (BasicBlock *BB : LatchesBlocks) {
221 TerminatorInst *TI = BB->getTerminator();
221 Instruction *TI = BB->getTerminator();
222222 MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
223223
224224 if (!MD)
249249 "The loop should have no single latch at this point");
250250 BasicBlock *H = getHeader();
251251 for (BasicBlock *BB : this->blocks()) {
252 TerminatorInst *TI = BB->getTerminator();
252 Instruction *TI = BB->getTerminator();
253253 for (BasicBlock *Successor : successors(TI)) {
254254 if (Successor == H)
255255 TI->setMetadata(LLVMContext::MD_loop, LoopID);
11031103 const SmallPtrSetImpl &DeadBlocks) {
11041104 // First delete all uses of BB in MemoryPhis.
11051105 for (BasicBlock *BB : DeadBlocks) {
1106 TerminatorInst *TI = BB->getTerminator();
1106 Instruction *TI = BB->getTerminator();
11071107 assert(TI && "Basic block expected to have a terminator instruction");
11081108 for (BasicBlock *Succ : successors(TI))
11091109 if (!DeadBlocks.count(Succ))
70777077 return getCouldNotCompute();
70787078
70797079 bool IsOnlyExit = (L->getExitingBlock() != nullptr);
7080 TerminatorInst *Term = ExitingBlock->getTerminator();
7080 Instruction *Term = ExitingBlock->getTerminator();
70817081 if (BranchInst *BI = dyn_cast(Term)) {
70827082 assert(BI->isConditional() && "If unconditional, it can't be in loop!");
70837083 bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
470470 bool llvm::isInTailCallPosition(ImmutableCallSite CS, const TargetMachine &TM) {
471471 const Instruction *I = CS.getInstruction();
472472 const BasicBlock *ExitBB = I->getParent();
473 const TerminatorInst *Term = ExitBB->getTerminator();
473 const Instruction *Term = ExitBB->getTerminator();
474474 const ReturnInst *Ret = dyn_cast(Term);
475475
476476 // The block must end in a return statement or unreachable.
22222222 /// might result in multiple MBB's for one BB. As such, the start of the
22232223 /// BB might correspond to a different MBB than the end.
22242224 bool FastISel::handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
2225 const TerminatorInst *TI = LLVMBB->getTerminator();
2225 const Instruction *TI = LLVMBB->getTerminator();
22262226
22272227 SmallPtrSet SuccsHandled;
22282228 FuncInfo.OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
92489248 /// the end.
92499249 void
92509250 SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
9251 const TerminatorInst *TI = LLVMBB->getTerminator();
9251 const Instruction *TI = LLVMBB->getTerminator();
92529252
92539253 SmallPtrSet SuccsHandled;
92549254
450450 if (!succ_empty(&BB))
451451 continue;
452452
453 const TerminatorInst *Term = BB.getTerminator();
453 const Instruction *Term = BB.getTerminator();
454454 if (isa(Term) || isa(Term))
455455 continue;
456456
217217 // to. If the unwind edge came from an invoke, return null.
218218 static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB,
219219 Value *ParentPad) {
220 const TerminatorInst *TI = BB->getTerminator();
220 const Instruction *TI = BB->getTerminator();
221221 if (isa(TI))
222222 return nullptr;
223223 if (auto *CatchSwitch = dyn_cast(TI)) {
976976 break;
977977 }
978978
979 TerminatorInst *TI = BB->getTerminator();
979 Instruction *TI = BB->getTerminator();
980980 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
981981 bool IsUnreachableRet = isa(TI) && FuncletPad;
982982 // The token consumed by a CatchReturnInst must match the funclet token.
436436 }
437437
438438 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
439 TerminatorInst *TI = getTerminator();
439 Instruction *TI = getTerminator();
440440 if (!TI)
441441 // Cope with being called on a BasicBlock that doesn't have a terminator
442442 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
467467 }
468468
469469 Optional BasicBlock::getIrrLoopHeaderWeight() const {
470 const TerminatorInst *TI = getTerminator();
470 const Instruction *TI = getTerminator();
471471 if (MDNode *MDIrrLoopHeader =
472472 TI->getMetadata(LLVMContext::MD_irr_loop)) {
473473 MDString *MDName = cast(MDIrrLoopHeader->getOperand(0));
4040 #endif
4141
4242 bool BasicBlockEdge::isSingleEdge() const {
43 const TerminatorInst *TI = Start->getTerminator();
43 const Instruction *TI = Start->getTerminator();
4444 unsigned NumEdgesToEnd = 0;
4545 for (unsigned int i = 0, n = TI->getNumSuccessors(); i < n; ++i) {
4646 if (TI->getSuccessor(i) == End)
133133 // Top-down walk of the dominator tree
134134 ReversePostOrderTraversal RPOT(&F);
135135 for (const BasicBlock *BB : RPOT) {
136 const TerminatorInst *TI = BB->getTerminator();
136 const Instruction *TI = BB->getTerminator();
137137 assert(TI && "blocks must be well formed");
138138
139139 // For conditional branches, we can perform simple conditional propagation on
34493449 Instruction *ToPad = &I;
34503450 Value *ToPadParent = getParentPad(ToPad);
34513451 for (BasicBlock *PredBB : predecessors(BB)) {
3452 TerminatorInst *TI = PredBB->getTerminator();
3452 Instruction *TI = PredBB->getTerminator();
34533453 Value *FromPad;
34543454 if (auto *II = dyn_cast(TI)) {
34553455 Assert(II->getUnwindDest() == BB && II->getNormalDest() != BB,
287287 }
288288 }
289289
290 TerminatorInst *Insert = From->getTerminator();
290 Instruction *Insert = From->getTerminator();
291291 Value *PhiArg = CallInst::Create(Break, Broken, "", Insert);
292292 NewPhi->setIncomingValue(i, PhiArg);
293293 }
4040 bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
4141 bool functionModified = false;
4242 Function::iterator I = function.begin();
43 TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
43 Instruction *firstTerminatorInst = (I++)->getTerminator();
4444
4545 for (Function::iterator E = function.end(); I != E; ++I) {
4646 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
489489 if (!FuncInfo->BPI) return PPC::BR_NO_HINT;
490490
491491 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
492 const TerminatorInst *BBTerm = BB->getTerminator();
492 const Instruction *BBTerm = BB->getTerminator();
493493
494494 if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT;
495495
10291029
10301030 // Free setjmpTable buffer before each return instruction
10311031 for (BasicBlock &BB : F) {
1032 TerminatorInst *TI = BB.getTerminator();
1032 Instruction *TI = BB.getTerminator();
10331033 if (isa(TI))
10341034 CallInst::CreateFree(SetjmpTable, TI);
10351035 }
368368
369369 // Insert an unlink before all returns.
370370 for (BasicBlock &BB : *F) {
371 TerminatorInst *T = BB.getTerminator();
371 Instruction *T = BB.getTerminator();
372372 if (!isa(T))
373373 continue;
374374 Builder.SetInsertPoint(T);
103103 bool blockEndsInUnreachable(const BasicBlock &BB) {
104104 if (BB.empty())
105105 return true;
106 const TerminatorInst *I = BB.getTerminator();
106 const Instruction *I = BB.getTerminator();
107107 if (isa(I) || isa(I))
108108 return true;
109109 // Unreachable blocks do not have any successor.
103103 bool ShouldExtractLoop = false;
104104
105105 // Extract the loop if the entry block doesn't branch to the loop header.
106 TerminatorInst *EntryTI =
107 L->getHeader()->getParent()->getEntryBlock().getTerminator();
106 Instruction *EntryTI =
107 L->getHeader()->getParent()->getEntryBlock().getTerminator();
108108 if (!isa(EntryTI) ||
109109 !cast(EntryTI)->isUnconditional() ||
110110 EntryTI->getSuccessor(0) != L->getHeader()) {
555555 };
556556
557557 auto IsReturnBlock = [](BasicBlock *BB) {
558 TerminatorInst *TI = BB->getTerminator();
558 Instruction *TI = BB->getTerminator();
559559 return isa(TI);
560560 };
561561
106106 continue;
107107
108108 for (const BasicBlock &BB : *F) {
109 const TerminatorInst *TI = BB.getTerminator();
109 const Instruction *TI = BB.getTerminator();
110110 if (CheckUnwind && TI->mayThrow()) {
111111 SCCMightUnwind = true;
112112 } else if (CheckReturn && isa(TI)) {
12961296 }
12971297 }
12981298 }
1299 TerminatorInst *TI = BB->getTerminator();
1299 Instruction *TI = BB->getTerminator();
13001300 if (TI->getNumSuccessors() == 1)
13011301 continue;
13021302 if (!isa(TI) && !isa(TI))
37313731 // Scan down this block to see if there is another stack restore in the
37323732 // same block without an intervening call/alloca.
37333733 BasicBlock::iterator BI(II);
3734 TerminatorInst *TI = II->getParent()->getTerminator();
3734 Instruction *TI = II->getParent()->getTerminator();
37353735 bool CannotRemove = false;
37363736 for (++BI; &*BI != TI; ++BI) {
37373737 if (isa(BI)) {
651651 Instruction *InstCombiner::FoldPHIArgZextsIntoPHI(PHINode &Phi) {
652652 // We cannot create a new instruction after the PHI if the terminator is an
653653 // EHPad because there is no valid insertion point.
654 if (TerminatorInst *TI = Phi.getParent()->getTerminator())
654 if (Instruction *TI = Phi.getParent()->getTerminator())
655655 if (TI->isEHPad())
656656 return nullptr;
657657
725725 Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
726726 // We cannot create a new instruction after the PHI if the terminator is an
727727 // EHPad because there is no valid insertion point.
728 if (TerminatorInst *TI = PN.getParent()->getTerminator())
728 if (Instruction *TI = PN.getParent()->getTerminator())
729729 if (TI->isEHPad())
730730 return nullptr;
731731
23462346 return nullptr;
23472347
23482348 // Validate the rest of constraint #1 by matching on the pred branch.
2349 TerminatorInst *TI = PredBB->getTerminator();
2349 Instruction *TI = PredBB->getTerminator();
23502350 BasicBlock *TrueBB, *FalseBB;
23512351 ICmpInst::Predicate Pred;
23522352 if (!match(TI, m_Br(m_ICmp(Pred, m_Specific(Op), m_Zero()), TrueBB, FalseBB)))
32843284
32853285 // Recursively visit successors. If this is a branch or switch on a
32863286 // constant, only visit the reachable successor.
3287 TerminatorInst *TI = BB->getTerminator();
3287 Instruction *TI = BB->getTerminator();
32883288 if (BranchInst *BI = dyn_cast(TI)) {
32893289 if (BI->isConditional() && isa(BI->getCondition())) {
32903290 bool CondVal = cast(BI->getCondition())->getZExtValue();
118118 static const uint32_t CriticalEdgeMultiplier = 1000;
119119
120120 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
121 TerminatorInst *TI = BB->getTerminator();
121 Instruction *TI = BB->getTerminator();
122122 uint64_t BBWeight =
123123 (BFI != nullptr ? BFI->getBlockFreq(&*BB).getFrequency() : 2);
124124 uint64_t Weight = 2;
577577
578578 for (auto &BB : F) {
579579 GCOVBlock &Block = Func.getBlock(&BB);
580 TerminatorInst *TI = BB.getTerminator();
580 Instruction *TI = BB.getTerminator();
581581 if (int successors = TI->getNumSuccessors()) {
582582 for (int i = 0; i != successors; ++i) {
583583 Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
645645 DenseMap, unsigned> EdgeToCounter;
646646 unsigned Edges = 0;
647647 for (auto &BB : F) {
648 TerminatorInst *TI = BB.getTerminator();
648 Instruction *TI = BB.getTerminator();
649649 if (isa(TI)) {
650650 EdgeToCounter[{&BB, nullptr}] = Edges++;
651651 } else {
689689 Count = Builder.CreateAdd(Count, Builder.getInt64(1));
690690 Builder.CreateStore(Count, Phi);
691691
692 TerminatorInst *TI = BB.getTerminator();
692 Instruction *TI = BB.getTerminator();
693693 if (isa(TI)) {
694694 auto It = EdgeToCounter.find({&BB, nullptr});
695695 assert(It != EdgeToCounter.end());
585585 std::vector Indexes;
586586 JamCRC JC;
587587 for (auto &BB : F) {
588 const TerminatorInst *TI = BB.getTerminator();
588 const Instruction *TI = BB.getTerminator();
589589 for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) {
590590 BasicBlock *Succ = TI->getSuccessor(I);
591591 auto BI = findBBInfo(Succ);
697697
698698 // Instrument the SrcBB if it has a single successor,
699699 // otherwise, the DestBB if this is not a critical edge.
700 TerminatorInst *TI = SrcBB->getTerminator();
700 Instruction *TI = SrcBB->getTerminator();
701701 if (TI->getNumSuccessors() <= 1)
702702 return SrcBB;
703703 if (!E->IsCritical)
11661166 // Generate MD_prof metadata for every branch instruction.
11671167 LLVM_DEBUG(dbgs() << "\nSetting branch weights.\n");
11681168 for (auto &BB : F) {
1169 TerminatorInst *TI = BB.getTerminator();
1169 Instruction *TI = BB.getTerminator();
11701170 if (TI->getNumSuccessors() < 2)
11711171 continue;
11721172 if (!(isa(TI) || isa(TI) ||
12121212 // to become an irreducible loop header after the indirectbr tail
12131213 // duplication.
12141214 if (BFI->isIrrLoopHeader(&BB) || isIndirectBrTarget(&BB)) {
1215 TerminatorInst *TI = BB.getTerminator();
1215 Instruction *TI = BB.getTerminator();
12161216 const UseBBInfo &BBCountInfo = getBBInfo(&BB);
12171217 setIrrLoopHeaderMetadata(M, TI, BBCountInfo.CountValue);
12181218 }
102102 BasicBlock *BB = nullptr;
103103
104104 /// Cache of BB->getTerminator().
105 TerminatorInst *Terminator = nullptr;
105 Instruction *Terminator = nullptr;
106106
107107 /// Post-order numbering of reverse control flow graph.
108108 unsigned PostOrder;
205205 return removeDeadInstructions();
206206 }
207207
208 static bool isUnconditionalBranch(TerminatorInst *Term) {
208 static bool isUnconditionalBranch(Instruction *Term) {
209209 auto *BR = dyn_cast(Term);
210210 return BR && BR->isUnconditional();
211211 }
276276 // treat all edges to a block already seen as loop back edges
277277 // and mark the branch live it if there is a back edge.
278278 for (auto *BB: depth_first_ext(&F.getEntryBlock(), State)) {
279 TerminatorInst *Term = BB->getTerminator();
279 Instruction *Term = BB->getTerminator();
280280 if (isLive(Term))
281281 continue;
282282
642642
643643 void AggressiveDeadCodeElimination::makeUnconditional(BasicBlock *BB,
644644 BasicBlock *Target) {
645 TerminatorInst *PredTerm = BB->getTerminator();
645 Instruction *PredTerm = BB->getTerminator();
646646 // Collect the live debug info scopes attached to this instruction.
647647 if (const DILocation *DL = PredTerm->getDebugLoc())
648648 collectLiveScopes(*DL);
247247 ReturnInst* RI = dyn_cast(&*II);
248248 assert(RI && "`musttail` call must be followed by `ret` instruction");
249249
250 TerminatorInst *TI = SplitBB->getTerminator();
250 Instruction *TI = SplitBB->getTerminator();
251251 Value *V = NewCI;
252252 if (BCI)
253253 V = cloneInstForMustTail(BCI, TI, V);
642642 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
643643 BasicBlock *Pred = *I;
644644 if (Pred == BB) continue;
645 TerminatorInst *PredTI = Pred->getTerminator();
645 Instruction *PredTI = Pred->getTerminator();
646646 if (PredTI->getNumSuccessors() != 1)
647647 continue;
648648
539539 }
540540 }
541541
542 TerminatorInst *TI = BB->getTerminator();
542 Instruction *TI = BB->getTerminator();
543543
544544 // Add in the live successors by first checking whether we have terminator
545545 // that may be simplified based on the values simplified by this call.
245245 bool TryTrivialLoopUnswitch(bool &Changed);
246246
247247 bool UnswitchIfProfitable(Value *LoopCond, Constant *Val,
248 TerminatorInst *TI = nullptr);
248 Instruction *TI = nullptr);
249249 void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
250 BasicBlock *ExitBlock, TerminatorInst *TI);
250 BasicBlock *ExitBlock, Instruction *TI);
251251 void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L,
252 TerminatorInst *TI);
252 Instruction *TI);
253253
254254 void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
255255 Constant *Val, bool isEqual);
257257 void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
258258 BasicBlock *TrueDest,
259259 BasicBlock *FalseDest,
260 BranchInst *OldBranch,
261 TerminatorInst *TI);
260 BranchInst *OldBranch, Instruction *TI);
262261
263262 void SimplifyCode(std::vector &Worklist, Loop *L);
264263
712711 // loop.
713712 for (Loop::block_iterator I = currentLoop->block_begin(),
714713 E = currentLoop->block_end(); I != E; ++I) {
715 TerminatorInst *TI = (*I)->getTerminator();
714 Instruction *TI = (*I)->getTerminator();
716715
717716 // Unswitching on a potentially uninitialized predicate is not
718717 // MSan-friendly. Limit this to the cases when the original predicate is
875874 /// simplify the loop. If we decide that this is profitable,
876875 /// unswitch the loop, reprocess the pieces, then return true.
877876 bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,
878 TerminatorInst *TI) {
877 Instruction *TI) {
879878 // Check to see if it would be profitable to unswitch current loop.
880879 if (!BranchesInfo.CostAllowsUnswitching()) {
881880 LLVM_DEBUG(dbgs() << "NOT unswitching loop %"
930929 BasicBlock *TrueDest,
931930 BasicBlock *FalseDest,
932931 BranchInst *OldBranch,
933 TerminatorInst *TI) {
932 Instruction *TI) {
934933 assert(OldBranch->isUnconditional() && "Preheader is not split correctly");
935934 assert(TrueDest != FalseDest && "Branch targets should be different");
936935 // Insert a conditional branch on LIC to the two preheaders. The original
995994 /// outside of the loop and updating loop info.
996995 void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
997996 BasicBlock *ExitBlock,
998 TerminatorInst *TI) {
997 Instruction *TI) {
999998 LLVM_DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %"
1000999 << loopHeader->getName() << " [" << L->getBlocks().size()
10011000 << " blocks] in Function "
10531052 /// condition.
10541053 bool LoopUnswitch::TryTrivialLoopUnswitch(bool &Changed) {
10551054 BasicBlock *CurrentBB = currentLoop->getHeader();
1056 TerminatorInst *CurrentTerm = CurrentBB->getTerminator();
1055 Instruction *CurrentTerm = CurrentBB->getTerminator();
10571056 LLVMContext &Context = CurrentBB->getContext();
10581057
10591058 // If loop header has only one reachable successor (currently via an
12261225 /// Split it into loop versions and test the condition outside of either loop.
12271226 /// Return the loops created as Out1/Out2.
12281227 void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
1229 Loop *L, TerminatorInst *TI) {
1228 Loop *L, Instruction *TI) {
12301229 Function *F = loopHeader->getParent();
12311230 LLVM_DEBUG(dbgs() << "loop-unswitch: Unswitching loop %"
12321231 << loopHeader->getName() << " [" << L->getBlocks().size()
104104
105105 /// The output of the pass - gives a list of each backedge (described by
106106 /// pointing at the branch) which need a poll inserted.
107 std::vector<TerminatorInst *> PollLocations;
107 std::vector<Instruction *> PollLocations;
108108
109109 /// True unless we're running spp-no-calls in which case we need to disable
110110 /// the call-dependent placement opts.
347347 // Safepoint insertion would involve creating a new basic block (as the
348348 // target of the current backedge) which does the safepoint (of all live
349349 // variables) and branches to the true header
350 TerminatorInst *Term = Pred->getTerminator();
350 Instruction *Term = Pred->getTerminator();
351351
352352 LLVM_DEBUG(dbgs() << "[LSP] terminator instruction: " << *Term);
353353
534534
535535 // Insert a poll at each point the analysis pass identified
536536 // The poll location must be the terminator of a loop latch block.
537 for (TerminatorInst *Term : PollLocations) {
537 for (Instruction *Term : PollLocations) {
538538 // We are inserting a poll, the function is modified
539539 Modified = true;
540540
18501850 StoreInst *Store = new StoreInst(Def, Alloca);
18511851 if (Instruction *Inst = dyn_cast(Def)) {
18521852 if (InvokeInst *Invoke = dyn_cast(Inst)) {
1853 // InvokeInst is a TerminatorInst so the store need to be inserted
1854 // into its normal destination block.
1853 // InvokeInst is a terminator so the store need to be inserted into its
1854 // normal destination block.
18551855 BasicBlock *NormalDest = Invoke->getNormalDest();
18561856 Store->insertBefore(NormalDest->getFirstNonPHI());
18571857 } else {
18581858 assert(!Inst->isTerminator() &&
1859 "The only TerminatorInst that can produce a value is "
1859 "The only terminator that can produce a value is "
18601860 "InvokeInst which is handled above.");
18611861 Store->insertAfter(Inst);
18621862 }
25832583 // increase the liveset of any statepoint we move over. This is profitable
25842584 // as long as all statepoints are in rare blocks. If we had in-register
25852585 // lowering for live values this would be a much safer transform.
2586 auto getConditionInst = [](TerminatorInst *TI) -> Instruction* {
2586 auto getConditionInst = [](Instruction *TI) -> Instruction * {
25872587 if (auto *BI = dyn_cast(TI))
25882588 if (BI->isConditional())
25892589 return dyn_cast(BI->getCondition());
25912591 return nullptr;
25922592 };
25932593 for (BasicBlock &BB : F) {
2594 TerminatorInst *TI = BB.getTerminator();
2594 Instruction *TI = BB.getTerminator();
25952595 if (auto *Cond = getConditionInst(TI))
25962596 // TODO: Handle more than just ICmps here. We should be able to move
25972597 // most instructions without side effects or memory access.
26742674 /// Check that the items in 'Live' dominate 'TI'. This is used as a basic
26752675 /// sanity check for the liveness computation.
26762676 static void checkBasicSSA(DominatorTree &DT, SetVector &Live,
2677 TerminatorInst *TI, bool TermOkay = false) {
2677 Instruction *TI, bool TermOkay = false) {
26782678 for (Value *V : Live) {
26792679 if (auto *I = dyn_cast(V)) {
26802680 // The terminator can be a member of the LiveOut set. LLVM's definition
16131613 // Check to see if we have a branch or switch on an undefined value. If so
16141614 // we force the branch to go one way or the other to make the successor
16151615 // values live. It doesn't really matter which way we force it.
1616 TerminatorInst *TI = BB.getTerminator();
1616 Instruction *TI = BB.getTerminator();
16171617 if (auto *BI = dyn_cast(TI)) {
16181618 if (!BI->isConditional()) continue;
16191619 if (!getValueState(BI->getCondition()).isUnknown())
12101210 // predecessor blocks. The only thing to watch out for is that we can't put
12111211 // a possibly trapping load in the predecessor if it is a critical edge.
12121212 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1213 TerminatorInst *TI = PN.getIncomingBlock(Idx)->getTerminator();
1213 Instruction *TI = PN.getIncomingBlock(Idx)->getTerminator();
12141214 Value *InVal = PN.getIncomingValue(Idx);
12151215
12161216 // If the value is produced by the terminator of the predecessor (an
12741274 continue;
12751275 }
12761276
1277 TerminatorInst *TI = Pred->getTerminator();
1277 Instruction *TI = Pred->getTerminator();
12781278 IRBuilderTy PredBuilder(TI);
12791279
12801280 LoadInst *Load = PredBuilder.CreateLoad(
782782 [](Instruction &I) { return I.mayHaveSideEffects(); }))
783783 return Changed;
784784
785 TerminatorInst *CurrentTerm = CurrentBB->getTerminator();
785 Instruction *CurrentTerm = CurrentBB->getTerminator();
786786
787787 if (auto *SI = dyn_cast(CurrentTerm)) {
788788 // Don't bother trying to unswitch past a switch with a constant
635635
636636 /// Remove phi values from all successors and then remove the terminator.
637637 void StructurizeCFG::killTerminator(BasicBlock *BB) {
638 TerminatorInst *Term = BB->getTerminator();
638 Instruction *Term = BB->getTerminator();
639639 if (!Term)
640640 return;
641641
701701 SmallVector UncondBranchPreds;
702702 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
703703 BasicBlock *Pred = *PI;
704 TerminatorInst *PTI = Pred->getTerminator();
704 Instruction *PTI = Pred->getTerminator();
705705 if (BranchInst *BI = dyn_cast(PTI))
706706 if (BI->isUnconditional())
707707 UncondBranchPreds.push_back(BI);
317317 BasicBlock *IBB = nullptr;
318318 for (unsigned Pred = 0, E = PN->getNumIncomingValues(); Pred != E; ++Pred) {
319319 BasicBlock *PredBB = PN->getIncomingBlock(Pred);
320 TerminatorInst *PredTerm = PredBB->getTerminator();
320 Instruction *PredTerm = PredBB->getTerminator();
321321 switch (PredTerm->getOpcode()) {
322322 case Instruction::IndirectBr:
323323 if (IBB)
364364 }
365365
366366 // Finally, clone over the terminator.
367 const TerminatorInst *OldTI = BB->getTerminator();
367 const Instruction *OldTI = BB->getTerminator();
368368 bool TerminatorDone = false;
369369 if (const BranchInst *BI = dyn_cast(OldTI)) {
370370 if (BI->isConditional()) {
413413 CodeInfo->OperandBundleCallSites.push_back(NewInst);
414414
415415 // Recursively clone any reachable successor blocks.
416 const TerminatorInst *TI = BB->getTerminator();
416 const Instruction *TI = BB->getTerminator();
417417 for (const BasicBlock *Succ : successors(TI))
418418 ToClone.push_back(Succ);
419419 }
565565 // changing them to branch to NewBB instead.
566566 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
567567 if (Blocks.count(PN->getIncomingBlock(i))) {
568 TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator();
568 Instruction *TI = PN->getIncomingBlock(i)->getTerminator();
569569 TI->replaceUsesOfWith(OldPred, NewBB);
570570 }
571571
777777 Value *Idx[2];
778778 Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
779779 Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
780 TerminatorInst *TI = newFunction->begin()->getTerminator();
780 Instruction *TI = newFunction->begin()->getTerminator();
781781 GetElementPtrInst *GEP = GetElementPtrInst::Create(
782782 StructTy, &*AI, Idx, "gep_" + inputs[i]->getName(), TI);
783783 RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI);
971971
972972 unsigned switchVal = 0;
973973 for (BasicBlock *Block : Blocks) {
974 TerminatorInst *TI = Block->getTerminator();
974 Instruction *TI = Block->getTerminator();
975975 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
976976 if (!Blocks.count(TI->getSuccessor(i))) {
977977 BasicBlock *OldTarget = TI->getSuccessor(i);
10771077 using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
10781078
10791079 // Update the branch weights for the exit block.
1080 TerminatorInst *TI = CodeReplacer->getTerminator();
1080 Instruction *TI = CodeReplacer->getTerminator();
10811081 SmallVector BranchWeights(TI->getNumSuccessors(), 0);
10821082
10831083 // Block Frequency distribution with dummy node.
3636
3737 // Branches and invokes do not escape, only unwind, resume, and return
3838 // do.
39 TerminatorInst *TI = CurBB->getTerminator();
39 Instruction *TI = CurBB->getTerminator();
4040 if (!isa(TI) && !isa(TI))
4141 continue;
4242
231231 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
232232 return false;
233233
234 TerminatorInst *TBB = LastCondBlock->getTerminator();
234 Instruction *TBB = LastCondBlock->getTerminator();
235235 BasicBlock *PS1 = TBB->getSuccessor(0);
236236 BasicBlock *PS2 = TBB->getSuccessor(1);
237237 BranchInst *PBI1 = dyn_cast(PS1->getTerminator());
324324 bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
325325 BasicBlock *Block1,
326326 BasicBlock *Block2) {
327 TerminatorInst *PTI2 = Head2->getTerminator();
327 Instruction *PTI2 = Head2->getTerminator();
328328 Instruction *PBI2 = &Head2->front();
329329
330330 bool eq1 = (Block1 == Head1);
420420 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))
421421 return false;
422422
423 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();
423 Instruction *PTI2 = SecondEntryBlock->getTerminator();
424424 Instruction *PBI2 = &SecondEntryBlock->front();
425425
426426 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1,
866866 if (int Res = cmpBasicBlocks(BBL, BBR))
867867 return Res;
868868
869 const TerminatorInst *TermL = BBL->getTerminator();
870 const TerminatorInst *TermR = BBR->getTerminator();
869 const Instruction *TermL = BBL->getTerminator();
870 const Instruction *TermR = BBR->getTerminator();
871871
872872 assert(TermL->getNumSuccessors() == TermR->getNumSuccessors());
873873 for (unsigned i = 0, e = TermL->getNumSuccessors(); i != e; ++i) {
937937 for (auto &Inst : *BB) {
938938 H.add(Inst.getOpcode());
939939 }
940 const TerminatorInst *Term = BB->getTerminator();
940 const Instruction *Term = BB->getTerminator();
941941 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
942942 if (!VisitedBBs.insert(Term->getSuccessor(i)).second)
943943 continue;
22462246 // Change the branch that used to go to AfterCallBB to branch to the first
22472247 // basic block of the inlined function.
22482248 //
2249 TerminatorInst *Br = OrigBB->getTerminator();
2249 Instruction *Br = OrigBB->getTerminator();
22502250 assert(Br && Br->getOpcode() == Instruction::Br &&
22512251 "splitBasicBlock broken!");
22522252 Br->setOperand(0, &*FirstNewBlock);
104104 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
105105 const TargetLibraryInfo *TLI,
106106 DomTreeUpdater *DTU) {
107 TerminatorInst *T = BB->getTerminator();
107 Instruction *T = BB->getTerminator();
108108 IRBuilder<> Builder(T);
109109
110110 // Branch - See if we are conditional jumping on constant
21002100 }
21012101 }
21022102
2103 TerminatorInst *Terminator = BB->getTerminator();
2103 Instruction *Terminator = BB->getTerminator();
21042104 if (auto *II = dyn_cast(Terminator)) {
21052105 // Turn invokes that call 'nounwind' functions into ordinary calls.
21062106 Value *Callee = II->getCalledValue();
21752175 }
21762176
21772177 void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) {
2178 TerminatorInst *TI = BB->getTerminator();
2178 Instruction *TI = BB->getTerminator();
21792179
21802180 if (auto *II = dyn_cast(TI)) {
21812181 changeToCall(II, DTU);
298298
299299 // For the rest of the instructions, either hoist to the OrigPreheader if
300300 // possible or create a clone in the OldPreHeader if not.
301 TerminatorInst *LoopEntryBranch = OrigPreheader->getTerminator();
301 Instruction *LoopEntryBranch = OrigPreheader->getTerminator();
302302
303303 // Record all debug intrinsics preceding LoopEntryBranch to avoid duplication.
304304 using DbgIntrinsicHash =
434434 unsigned LoopMDKind = BEBlock->getContext().getMDKindID("llvm.loop");
435435 MDNode *LoopMD = nullptr;
436436 for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
437 TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
437 Instruction *TI = BackedgeBlocks[i]->getTerminator();
438438 if (!LoopMD)
439439 LoopMD = TI->getMetadata(LoopMDKind);
440440 TI->setMetadata(LoopMDKind, nullptr);
487487 << P->getName() << "\n");
488488
489489 // Zap the dead pred's terminator and replace it with unreachable.
490 TerminatorInst *TI = P->getTerminator();
490 Instruction *TI = P->getTerminator();
491491 changeToUnreachable(TI, /*UseLLVMTrap=*/false, PreserveLCSSA);
492492 Changed = true;
493493 }
780780 // there is no such latch.
781781 NewIDom = Latches.back();
782782 for (BasicBlock *IterLatch : Latches) {
783 TerminatorInst *Term = IterLatch->getTerminator();
783 Instruction *Term = IterLatch->getTerminator();
784784 if (isa(Term) && cast(Term)->isConditional()) {
785785 NewIDom = IterLatch;
786786 break;
7171 for (BasicBlock *BB : ForeBlocks) {
7272 if (BB == SubLoopPreHeader)
7373 continue;
74 TerminatorInst *TI = BB->getTerminator();
74 Instruction *TI = BB->getTerminator();
7575 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
7676 if (!ForeBlocks.count(TI->getSuccessor(i)))
7777 return false;
174174 const SimplifyCFGOptions &Options;
175175 bool Resimplify;
176176
177 Value *isValueEqualityComparison(TerminatorInst *TI);
177 Value *isValueEqualityComparison(Instruction *TI);
178178 BasicBlock *GetValueEqualityComparisonCases(
179 TerminatorInst *TI, std::vector &Cases);
180 bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
179 Instruction *TI, std::vector &Cases);
180 bool SimplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
181181 BasicBlock *Pred,
182182 IRBuilder<> &Builder);
183 bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
183 bool FoldValueComparisonIntoPredecessors(Instruction *TI,
184184 IRBuilder<> &Builder);
185185
186186 bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
218218 /// Return true if it is safe to merge these two
219219 /// terminator instructions together.
220220 static bool
221 SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2,
221 SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
222222 SmallSetVector *FailBlocks = nullptr) {
223223 if (SI1 == SI2)
224224 return false; // Can't merge with self!
669669
670670 } // end anonymous namespace
671671
672 static void EraseTerminatorInstAndDCECond(TerminatorInst *TI) {
672 static void EraseTerminatorAndDCECond(Instruction *TI) {
673673 Instruction *Cond = nullptr;
674674 if (SwitchInst *SI = dyn_cast(TI)) {
675675 Cond = dyn_cast(SI->getCondition());
687687
688688 /// Return true if the specified terminator checks
689689 /// to see if a value is equal to constant integer value.
690 Value *SimplifyCFGOpt::isValueEqualityComparison(TerminatorInst *TI) {
690 Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
691691 Value *CV = nullptr;
692692 if (SwitchInst *SI = dyn_cast(TI)) {
693693 // Do not permit merging of large switch instructions into their
715715 /// Given a value comparison instruction,
716716 /// decode all of the 'cases' that it represents and return the 'default' block.
717717 BasicBlock *SimplifyCFGOpt::GetValueEqualityComparisonCases(
718 TerminatorInst *TI, std::vector &Cases) {
718 Instruction *TI, std::vector &Cases) {
719719 if (SwitchInst *SI = dyn_cast(TI)) {
720720 Cases.reserve(SI->getNumCases());
721721 for (auto Case : SI->cases())
805805 /// determines the outcome of this comparison. If so, simplify TI. This does a
806806 /// very limited form of jump threading.
807807 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
808 TerminatorInst *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
808 Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
809809 Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
810810 if (!PredVal)
811811 return false; // Not a value comparison in predecessor.
853853 << "Through successor TI: " << *TI << "Leaving: " << *NI
854854 << "\n");
855855
856 EraseTerminatorInstAndDCECond(TI);
856 EraseTerminatorAndDCECond(TI);
857857 return true;
858858 }
859859
935935 << "Through successor TI: " << *TI << "Leaving: " << *NI
936936 << "\n");
937937
938 EraseTerminatorInstAndDCECond(TI);
938 EraseTerminatorAndDCECond(TI);
939939 return true;
940940 }
941941
970970 return false;
971971 }
972972
973 /// Get Weights of a given TerminatorInst, the default weight is at the front
973 /// Get Weights of a given terminator, the default weight is at the front
974974 /// of the vector. If TI is a conditional eq, we need to swap the branch-weight
975975 /// metadata.
976 static void GetBranchWeights(TerminatorInst *TI,
976 static void GetBranchWeights(Instruction *TI,
977977 SmallVectorImpl &Weights) {
978978 MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
979979 assert(MD);
10071007 /// (either a switch or a branch on "X == c").
10081008 /// See if any of the predecessors of the terminator block are value comparisons
10091009 /// on the same value. If so, and if safe to do so, fold them together.
1010 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
1010 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
10111011 IRBuilder<> &Builder) {
10121012 BasicBlock *BB = TI->getParent();
10131013 Value *CV = isValueEqualityComparison(TI); // CondVal
10191019 BasicBlock *Pred = Preds.pop_back_val();
10201020
10211021 // See if the predecessor is a comparison with the same value.
1022 TerminatorInst *PTI = Pred->getTerminator();
1022 Instruction *PTI = Pred->getTerminator();
10231023 Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
10241024
10251025 if (PCV == CV && TI != PTI) {
11961196 setBranchWeights(NewSI, MDWeights);
11971197 }
11981198
1199 EraseTerminatorInstAndDCECond(PTI);
1199 EraseTerminatorAndDCECond(PTI);
12001200
12011201 // Okay, last check. If BB is still a successor of PSI, then we must
12021202 // have an infinite loop case. If so, add an infinitely looping block
14121412 for (BasicBlock *Succ : successors(BB1))
14131413 AddPredecessorToBlock(Succ, BIParent, BB1);
14141414
1415 EraseTerminatorInstAndDCECond(BI);
1415 EraseTerminatorAndDCECond(BI);
14161416 return true;
14171417 }
14181418
22462246
22472247 // Loop over all of the edges from PredBB to BB, changing them to branch
22482248 // to EdgeBB instead.
2249 TerminatorInst *PredBBTI = PredBB->getTerminator();
2249 Instruction *PredBBTI = PredBB->getTerminator();
22502250 for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
22512251 if (PredBBTI->getSuccessor(i) == BB) {
22522252 BB->removePredecessor(PredBB);
24072407 // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
24082408 // has been flattened. Change DomBlock to jump directly to our new block to
24092409 // avoid other simplifycfg's kicking in on the diamond.
2410 TerminatorInst *OldTI = DomBlock->getTerminator();
2410 Instruction *OldTI = DomBlock->getTerminator();
24112411 Builder.SetInsertPoint(OldTI);
24122412 Builder.CreateBr(BB);
24132413 OldTI->eraseFromParent();
24412441 TrueSucc->removePredecessor(BI->getParent());
24422442 FalseSucc->removePredecessor(BI->getParent());
24432443 Builder.CreateRetVoid();
2444 EraseTerminatorInstAndDCECond(BI);
2444 EraseTerminatorAndDCECond(BI);
24452445 return true;
24462446 }
24472447
24972497 << "\n " << *BI << "NewRet = " << *RI << "TRUEBLOCK: "
24982498 << *TrueSucc << "FALSEBLOCK: " << *FalseSucc);
24992499
2500 EraseTerminatorInstAndDCECond(BI);
2500 EraseTerminatorAndDCECond(BI);
25012501
25022502 return true;
25032503 }
28212821 }
28222822 // Change PBI from Conditional to Unconditional.
28232823 BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
2824 EraseTerminatorInstAndDCECond(PBI);
2824 EraseTerminatorAndDCECond(PBI);
28252825 PBI = New_PBI;
28262826 }
28272827
34163416 // Takes care of updating the successors and removing the old terminator.
34173417 // Also makes sure not to introduce new successors by assuming that edges to
34183418 // non-successor TrueBBs and FalseBBs aren't reachable.
3419 static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond,
3419 static bool SimplifyTerminatorOnSelect(Instruction *OldTerm, Value *Cond,
34203420 BasicBlock *TrueBB, BasicBlock *FalseBB,
34213421 uint32_t TrueWeight,
34223422 uint32_t FalseWeight) {
34713471 Builder.CreateBr(FalseBB);
34723472 }
34733473
3474 EraseTerminatorInstAndDCECond(OldTerm);
3474 EraseTerminatorAndDCECond(OldTerm);
34753475 return true;
34763476 }
34773477
37143714 BasicBlock *NewBB =
37153715 BB->splitBasicBlock(BI->getIterator(), "switch.early.test");
37163716 // Remove the uncond branch added to the old block.
3717 TerminatorInst *OldTI = BB->getTerminator();
3717 Instruction *OldTI = BB->getTerminator();
37183718 Builder.SetInsertPoint(OldTI);
37193719
37203720 if (TrueWhenEqual)
37583758 }
37593759
37603760 // Erase the old branch instruction.
3761 EraseTerminatorInstAndDCECond(BI);
3761 EraseTerminatorAndDCECond(BI);
37623762
37633763 LLVM_DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n');
37643764 return true;
40064006 if (UnwindDest == nullptr) {
40074007 removeUnwindEdge(PredBB);
40084008 } else {
4009 TerminatorInst *TI = PredBB->getTerminator();
4009 Instruction *TI = PredBB->getTerminator();
40104010 TI->replaceUsesOfWith(BB, UnwindDest);
40114011 }
40124012 }
40754075 SmallVector CondBranchPreds;
40764076 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
40774077 BasicBlock *P = *PI;
4078 TerminatorInst *PTI = P->getTerminator();
4078 Instruction *PTI = P->getTerminator();
40794079 if (BranchInst *BI = dyn_cast(PTI)) {
40804080 if (BI->isUnconditional())
40814081 UncondBranchPreds.push_back(P);
41804180
41814181 SmallVector Preds(pred_begin(BB), pred_end(BB));
41824182 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
4183 TerminatorInst *TI = Preds[i]->getTerminator();
4183 Instruction *TI = Preds[i]->getTerminator();
41844184 IRBuilder<> Builder(TI);
41854185 if (auto *BI = dyn_cast(TI)) {
41864186 if (BI->isUnconditional()) {
41924192 } else {
41934193 if (BI->getSuccessor(0) == BB) {
41944194 Builder.CreateBr(BI->getSuccessor(1));
4195 EraseTerminatorInstAndDCECond(BI);
4195 EraseTerminatorAndDCECond(BI);
41964196 } else if (BI->getSuccessor(1) == BB) {
41974197 Builder.CreateBr(BI->getSuccessor(0));
4198 EraseTerminatorInstAndDCECond(BI);
4198 EraseTerminatorAndDCECond(BI);
41994199 Changed = true;
42004200 }
42014201 }
44374437 SplitBlock(&*NewDefault, &NewDefault->front());
44384438 auto *OldTI = NewDefault->getTerminator();
44394439 new UnreachableInst(SI->getContext(), OldTI);
4440 EraseTerminatorInstAndDCECond(OldTI);
4440 EraseTerminatorAndDCECond(OldTI);
44414441 return true;
44424442 }
44434443
46484648 SmallDenseMap ConstantPool;
46494649 ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
46504650 for (Instruction &I :CaseDest->instructionsWithoutDebug()) {
4651 if (TerminatorInst *T = dyn_cast(&I)) {
4651 if (I.isTerminator()) {
46524652 // If the terminator is a simple branch, continue to the next block.
4653 if (T->getNumSuccessors() != 1 || T->isExceptionalTerminator())
4653 if (I.getNumSuccessors() != 1 || I.isExceptionalTerminator())
46544654 return false;
46554655 Pred = CaseDest;
4656 CaseDest = T->getSuccessor(0);
4656 CaseDest = I.getSuccessor(0);
46574657 } else if (Constant *C = ConstantFold(&I, DL, ConstantPool)) {
46584658 // Instruction is side-effect free and constant.
46594659
56625662 if (IBI->getNumDestinations() == 0) {
56635663 // If the indirectbr has no successors, change it to unreachable.
56645664 new UnreachableInst(IBI->getContext(), IBI);
5665 EraseTerminatorInstAndDCECond(IBI);
5665 EraseTerminatorAndDCECond(IBI);
56665666 return true;
56675667 }
56685668
56695669 if (IBI->getNumDestinations() == 1) {
56705670 // If the indirectbr has one successor, change it to a direct branch.
56715671 BranchInst::Create(IBI->getDestination(0), IBI);
5672 EraseTerminatorInstAndDCECond(IBI);
5672 EraseTerminatorAndDCECond(IBI);
56735673 return true;
56745674 }
56755675
58915891 } else {
58925892 // If Successor #1 has multiple preds, we may be able to conditionally
58935893 // execute Successor #0 if it branches to Successor #1.
5894 TerminatorInst *Succ0TI = BI->getSuccessor(0)->getTerminator();
5894 Instruction *Succ0TI = BI->getSuccessor(0)->getTerminator();
58955895 if (Succ0TI->getNumSuccessors() == 1 &&
58965896 Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
58975897 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), TTI))
59005900 } else if (BI->getSuccessor(1)->getSinglePredecessor()) {
59015901 // If Successor #0 has multiple preds, we may be able to conditionally
59025902 // execute Successor #1 if it branches to Successor #0.
5903 TerminatorInst *Succ1TI = BI->getSuccessor(1)->getTerminator();
5903 Instruction *Succ1TI = BI->getSuccessor(1)->getTerminator();
59045904 if (Succ1TI->getNumSuccessors() == 1 &&
59055905 Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
59065906 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), TTI))
59905990 for (PHINode &PHI : BB->phis())
59915991 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i)
59925992 if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) {
5993 TerminatorInst *T = PHI.getIncomingBlock(i)->getTerminator();
5993 Instruction *T = PHI.getIncomingBlock(i)->getTerminator();
59945994 IRBuilder<> Builder(T);
59955995 if (BranchInst *BI = dyn_cast(T)) {
59965996 BB->removePredecessor(PHI.getIncomingBlock(i));
267267 // Set VPBB successors. We create empty VPBBs for successors if they don't
268268 // exist already. Recipes will be created when the successor is visited
269269 // during the RPO traversal.
270 TerminatorInst *TI = BB->getTerminator();
270 Instruction *TI = BB->getTerminator();
271271 assert(TI && "Terminator expected.");
272272 unsigned NumSuccs = TI->getNumSuccessors();
273273