llvm.org GIT mirror llvm / facdfc6
Revert "[C++11] Add predecessors(BasicBlock *) / successors(BasicBlock *) iterator ranges." This reverts commit r213474 (and r213475), which causes a miscompile on a stage2 LTO build. I'll reply on the list in a moment. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213562 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
41 changed file(s) with 230 addition(s) and 167 deletion(s). Raw diff Collapse all Expand all
18531853 #include "llvm/Support/CFG.h"
18541854 BasicBlock *BB = ...;
18551855
1856 for (BasicBlock *Pred : predecessors(BB)) {
1856 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1857 BasicBlock *Pred = *PI;
18571858 // ...
18581859 }
18591860
1860 Similarly, to iterate over successors use ``successors``.
1861 Similarly, to iterate over successors use ``succ_iterator/succ_begin/succ_end``.
18611862
18621863 .. _simplechanges:
18631864
9191 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
9292 inline const_pred_iterator pred_end(const BasicBlock *BB) {
9393 return const_pred_iterator(BB, true);
94 }
95 inline iterator_range predecessors(BasicBlock *BB) {
96 return make_range(pred_begin(BB), pred_end(BB));
97 }
98 inline iterator_range predecessors(const BasicBlock *BB) {
99 return make_range(pred_begin(BB), pred_end(BB));
10094 }
10195
10296
262256 inline succ_const_iterator succ_end(const BasicBlock *BB) {
263257 return succ_const_iterator(BB->getTerminator(), true);
264258 }
265 inline iterator_range successors(BasicBlock *BB) {
266 return make_range(succ_begin(BB), succ_end(BB));
267 }
268 inline iterator_range successors(const BasicBlock *BB) {
269 return make_range(succ_begin(BB), succ_end(BB));
270 }
271259
272260 template struct isPodLike > {
273261 static const bool value = isPodLike::value;
527527 // We print the probabilities from the last function the analysis ran over,
528528 // or the function it is currently running over.
529529 assert(LastF && "Cannot print prior to running over a function");
530 for (const BasicBlock &BB : *LastF)
531 for (const BasicBlock *Succ : successors(&BB))
532 printEdgeProbability(OS << " ", &BB, Succ);
530 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
531 BI != BE; ++BI) {
532 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
533 SI != SE; ++SI) {
534 printEdgeProbability(OS << " ", BI, *SI);
535 }
536 }
533537 }
534538
535539 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
558562 uint32_t MaxWeight = 0;
559563 BasicBlock *MaxSucc = nullptr;
560564
561 for (BasicBlock *Succ : successors(BB)) {
565 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
566 BasicBlock *Succ = *I;
562567 uint32_t Weight = getEdgeWeight(BB, Succ);
563568 uint32_t PrevSum = Sum;
564569
2828 bool Interval::isLoop() const {
2929 // There is a loop in this interval iff one of the predecessors of the header
3030 // node lives in the interval.
31 for (BasicBlock *Pred : predecessors(HeaderNode))
32 if (contains(Pred))
31 for (::pred_iterator I = ::pred_begin(HeaderNode), E = ::pred_end(HeaderNode);
32 I != E; ++I)
33 if (contains(*I))
3334 return true;
3435 return false;
3536 }
624624 // Loop over all of our predecessors, merging what we know from them into
625625 // result.
626626 bool EdgesMissing = false;
627 for (BasicBlock *Pred : predecessors(BB)) {
627 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
628628 LVILatticeVal EdgeResult;
629 EdgesMissing |= !getEdgeValue(Val, Pred, BB, EdgeResult);
629 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
630630 if (EdgesMissing)
631631 continue;
632632
335335 SmallVector ExitBlocks;
336336 getExitBlocks(ExitBlocks);
337337 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
338 for (BasicBlock *Pred : predecessors(ExitBlocks[i]))
339 if (!contains(Pred))
338 for (pred_iterator PI = pred_begin(ExitBlocks[i]),
339 PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
340 if (!contains(*PI))
340341 return false;
341342 // All the requirements are met.
342343 return true;
358359 BasicBlock *current = *BI;
359360 switchExitBlocks.clear();
360361
361 for (BasicBlock *Succ : successors(*BI)) {
362 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
362363 // If block is inside the loop then it is not a exit block.
363 if (contains(Succ))
364 if (contains(*I))
364365 continue;
365366
366 pred_iterator PI = pred_begin(Succ);
367 pred_iterator PI = pred_begin(*I);
367368 BasicBlock *firstPred = *PI;
368369
369370 // If current basic block is this exit block's first predecessor
377378 // then it is possible that there are multiple edges from current block
378379 // to one exit block.
379380 if (std::distance(succ_begin(current), succ_end(current)) <= 2) {
380 ExitBlocks.push_back(Succ);
381 ExitBlocks.push_back(*I);
381382 continue;
382383 }
383384
384385 // In case of multiple edges from current block to exit block, collect
385386 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
386387 // duplicate edges.
387 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), Succ)
388 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
388389 == switchExitBlocks.end()) {
389 switchExitBlocks.push_back(Succ);
390 ExitBlocks.push_back(Succ);
390 switchExitBlocks.push_back(*I);
391 ExitBlocks.push_back(*I);
391392 }
392393 }
393394 }
45114511 // lead to the loop header.
45124512 bool MustExecuteLoopHeader = true;
45134513 BasicBlock *Exit = nullptr;
4514 for (BasicBlock *Succ : successors(ExitingBlock))
4515 if (!L->contains(Succ)) {
4514 for (succ_iterator SI = succ_begin(ExitingBlock), SE = succ_end(ExitingBlock);
4515 SI != SE; ++SI)
4516 if (!L->contains(*SI)) {
45164517 if (Exit) // Multiple exit successors.
45174518 return getCouldNotCompute();
4518 Exit = Succ;
4519 } else if (Succ != L->getHeader()) {
4519 Exit = *SI;
4520 } else if (*SI != L->getHeader()) {
45204521 MustExecuteLoopHeader = false;
45214522 }
45224523
452452 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
453453 PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
454454 } else {
455 for (BasicBlock *Pred : predecessors(BB))
456 PN->addIncoming(InVal, Pred);
455 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
456 PN->addIncoming(InVal, *PI);
457457 }
458458 }
459459 }
976976 }
977977 } else {
978978 SmallPtrSet VisitedBBs;
979 for (BasicBlock *Pred : predecessors(BB)) {
980 if (!VisitedBBs.insert(Pred))
979 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
980 if (!VisitedBBs.insert(*PI))
981981 continue;
982982
983 BasicBlock::InstListType &InstList = Pred->getInstList();
983 BasicBlock::InstListType &InstList = (*PI)->getInstList();
984984 BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
985985 BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
986986 do { ++RI; } while (RI != RE && isa(&*RI));
10511051
10521052 if (OptLevel != CodeGenOpt::None) {
10531053 bool AllPredsVisited = true;
1054 for (const BasicBlock *Pred : predecessors(LLVMBB)) {
1055 if (!FuncInfo->VisitedBBs.count(Pred)) {
1054 for (const_pred_iterator PI = pred_begin(LLVMBB), PE = pred_end(LLVMBB);
1055 PI != PE; ++PI) {
1056 if (!FuncInfo->VisitedBBs.count(*PI)) {
10561057 AllPredsVisited = false;
10571058 break;
10581059 }
141141 if (!LiveBBs.insert(BB))
142142 return; // already been here.
143143
144 for (BasicBlock *Pred : predecessors(BB))
145 MarkBlocksLiveIn(Pred, LiveBBs);
144 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
145 MarkBlocksLiveIn(*PI, LiveBBs);
146146 }
147147
148148 /// substituteLPadValues - Substitute the values returned by the landingpad
7878 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
7979 BB->getInstList().pop_front();
8080 }
81 for (BasicBlock *S : successors(BB))
82 S->removePredecessor(BB);
81 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
82 (*SI)->removePredecessor(BB);
8383 BB->dropAllReferences();
8484 }
8585
320320 // successors. If there were PHI nodes in the successors, then they need to
321321 // know that incoming branches will be from New, not from Old.
322322 //
323 for (BasicBlock *Successor : successors(New)) {
323 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
324324 // Loop over any phi nodes in the basic block, updating the BB field of
325325 // incoming values...
326 BasicBlock *Successor = *I;
326327 PHINode *PN;
327328 for (BasicBlock::iterator II = Successor->begin();
328329 (PN = dyn_cast(II)); ++II) {
178178 // trivially dominates itself, so we only have to find if it dominates the
179179 // other predecessors. Since the only way out of X is via NormalDest, X can
180180 // only properly dominate a node if NormalDest dominates that node too.
181 for (const BasicBlock *BB : predecessors(End)) {
181 for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
182 PI != E; ++PI) {
183 const BasicBlock *BB = *PI;
182184 if (BB == Start)
183185 continue;
184186
21062106
21072107 // The landingpad instruction defines its parent as a landing pad block. The
21082108 // landing pad block may be branched to only by the unwind edge of an invoke.
2109 for (BasicBlock *Pred : predecessors(BB)) {
2110 const InvokeInst *II = dyn_cast(Pred->getTerminator());
2109 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
2110 const InvokeInst *II = dyn_cast((*I)->getTerminator());
21112111 Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
21122112 "Block containing LandingPadInst must be jumped to "
21132113 "only by the unwind edge of an invoke.", &LPI);
472472 // Now check every path from the entry block to the load for transparency.
473473 // To do this, we perform a depth first search on the inverse CFG from the
474474 // loading block.
475 for (BasicBlock *P : predecessors(BB)) {
475 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
476 BasicBlock *P = *PI;
476477 for (idf_ext_iterator >
477478 I = idf_ext_begin(P, TranspBlocks),
478479 E = idf_ext_end(P, TranspBlocks); I != E; ++I)
228228 // Look through the landing pad's predecessors. If one of them ends in an
229229 // 'invoke', then we want to split the landing pad.
230230 bool Split = false;
231 for (BasicBlock *BB : predecessors(LPad)) {
231 for (pred_iterator
232 PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) {
233 BasicBlock *BB = *PI;
232234 if (BB->isLandingPad() && BB != Parent &&
233235 isa(Parent->getTerminator())) {
234236 Split = true;
5757 BasicBlock* returnBlock = nullptr;
5858 BasicBlock* nonReturnBlock = nullptr;
5959 unsigned returnCount = 0;
60 for (BasicBlock *Succ : successors(entryBlock))
61 if (isa(Succ->getTerminator())) {
62 returnBlock = Succ;
60 for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
61 SI != SE; ++SI)
62 if (isa((*SI)->getTerminator())) {
63 returnBlock = *SI;
6364 returnCount++;
6465 } else
65 nonReturnBlock = Succ;
66 nonReturnBlock = *SI;
6667
6768 if (returnCount != 1)
6869 return nullptr;
27172717 if (UserParent != BB) {
27182718 bool UserIsSuccessor = false;
27192719 // See if the user is one of our successors.
2720 for (BasicBlock *Succ : successors(BB))
2721 if (Succ == UserParent) {
2720 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
2721 if (*SI == UserParent) {
27222722 UserIsSuccessor = true;
27232723 break;
27242724 }
641641 /// them to F.
642642 static void FindUnconditionalPreds(SmallVectorImpl &Blocks,
643643 BasicBlock *BB, DominatorTree *DT) {
644 for (BasicBlock *Pred : predecessors(BB)) {
644 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
645 BasicBlock *Pred = *I;
645646 if (Pred == BB) continue;
646647 TerminatorInst *PredTI = Pred->getTerminator();
647648 if (PredTI->getNumSuccessors() != 1)
15541554 FullyAvailableBlocks[UnavailableBlocks[i]] = false;
15551555
15561556 SmallVector CriticalEdgePred;
1557 for (BasicBlock *Pred : predecessors(LoadBB)) {
1557 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
1558 PI != E; ++PI) {
1559 BasicBlock *Pred = *PI;
15581560 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
15591561 continue;
15601562 }
24802482 BasicBlock *PREPred = nullptr;
24812483 predMap.clear();
24822484
2483 for (BasicBlock *P : predecessors(CurrentBlock)) {
2485 for (pred_iterator PI = pred_begin(CurrentBlock),
2486 PE = pred_end(CurrentBlock); PI != PE; ++PI) {
2487 BasicBlock *P = *PI;
24842488 // We're not interested in PRE where the block is its
24852489 // own predecessor, or in blocks with predecessors
24862490 // that are not reachable.
27082712 for (SmallVectorImpl::iterator I = Dom.begin(),
27092713 E = Dom.end(); I != E; I++) {
27102714 BasicBlock *B = *I;
2711 for (BasicBlock *S : successors(B)) {
2715 for (succ_iterator SI = succ_begin(B), SE = succ_end(B); SI != SE; SI++) {
2716 BasicBlock *S = *SI;
27122717 if (DeadBlocks.count(S))
27132718 continue;
27142719
27152720 bool AllPredDead = true;
2716 for (BasicBlock *Pred : predecessors(S))
2717 if (!DeadBlocks.count(Pred)) {
2721 for (pred_iterator PI = pred_begin(S), PE = pred_end(S); PI != PE; PI++)
2722 if (!DeadBlocks.count(*PI)) {
27182723 AllPredDead = false;
27192724 break;
27202725 }
352352
353353 // If V is a constant, then it is known in all predecessors.
354354 if (Constant *KC = getKnownConstant(V, Preference)) {
355 for (BasicBlock *Pred : predecessors(BB))
356 Result.push_back(std::make_pair(KC, Pred));
355 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
356 Result.push_back(std::make_pair(KC, *PI));
357357
358358 return true;
359359 }
376376 // "X < 4" and "X < 3" is known true but "X < 4" itself is not available.
377377 // Perhaps getConstantOnEdge should be smart enough to do this?
378378
379 for (BasicBlock *P : predecessors(BB)) {
379 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
380 BasicBlock *P = *PI;
380381 // If the value is known by LazyValueInfo to be a constant in a
381382 // predecessor, use that information to try to thread this block.
382383 Constant *PredCst = LVI->getConstantOnEdge(V, P, BB);
530531 cast(Cmp->getOperand(0))->getParent() != BB) {
531532 Constant *RHSCst = cast(Cmp->getOperand(1));
532533
533 for (BasicBlock *P : predecessors(BB)) {
534 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);PI != E; ++PI){
535 BasicBlock *P = *PI;
534536 // If the value is known by LazyValueInfo to be a constant in a
535537 // predecessor, use that information to try to thread this block.
536538 LazyValueInfo::Tristate Res =
603605 // If all else fails, see if LVI can figure out a constant value for us.
604606 Constant *CI = LVI->getConstant(V, BB);
605607 if (Constant *KC = getKnownConstant(CI, Preference)) {
606 for (BasicBlock *Pred : predecessors(BB))
607 Result.push_back(std::make_pair(KC, Pred));
608 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
609 Result.push_back(std::make_pair(KC, *PI));
608610 }
609611
610612 return !Result.empty();
896898
897899 // If we got here, the loaded value is transparent through to the start of the
898900 // block. Check to see if it is available in any of the predecessor blocks.
899 for (BasicBlock *PredBB : predecessors(LoadBB)) {
901 for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
902 PI != PE; ++PI) {
903 BasicBlock *PredBB = *PI;
904
900905 // If we already scanned this predecessor, skip it.
901906 if (!PredsScanned.insert(PredBB))
902907 continue;
946951 AvailablePredSet.insert(AvailablePreds[i].first);
947952
948953 // Add all the unavailable predecessors to the PredsToSplit list.
949 for (BasicBlock *P : predecessors(LoadBB)) {
954 for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
955 PI != PE; ++PI) {
956 BasicBlock *P = *PI;
950957 // If the predecessor is an indirect goto, we can't split the edge.
951958 if (isa(P->getTerminator()))
952959 return false;
144144 // bodies of subloops. We visit the headers of loops so that we can process
145145 // their phis, but we contract the rest of the subloop body and only follow
146146 // edges leading back to the original loop.
147 for (BasicBlock *SuccBB : successors(BB)) {
147 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
148 ++SI) {
149 BasicBlock *SuccBB = *SI;
148150 if (!Visited.insert(SuccBB))
149151 continue;
150152
514514 }
515515
516516 // Otherwise, this is an unvisited intra-loop node. Check all successors.
517 for (BasicBlock *Succ : successors(BB)) {
517 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
518518 // Check to see if the successor is a trivial loop exit.
519 if (!isTrivialLoopExitBlockHelper(L, Succ, ExitBB, Visited))
519 if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
520520 return false;
521521 }
522522
860860 PHINode *PN = PHINode::Create(LPad->getType(), 0, "",
861861 ExitSucc->getFirstInsertionPt());
862862
863 for (BasicBlock *BB : predecessors(ExitSucc)) {
863 for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc);
864 I != E; ++I) {
865 BasicBlock *BB = *I;
864866 LandingPadInst *LPI = BB->getLandingPadInst();
865867 LPI->replaceAllUsesWith(PN);
866868 PN->addIncoming(LPI, BB);
864864 SmallPtrSet Visited;
865865 if (!Predecessors[B1].empty())
866866 llvm_unreachable("Found a stale predecessors list in a basic block.");
867 for (BasicBlock *B2 : predecessors(B1)) {
867 for (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) {
868 BasicBlock *B2 = *PI;
868869 if (Visited.insert(B2))
869870 Predecessors[B1].push_back(B2);
870871 }
873874 Visited.clear();
874875 if (!Successors[B1].empty())
875876 llvm_unreachable("Found a stale successors list in a basic block.");
876 for (BasicBlock *B2 : successors(B1)) {
877 for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) {
878 BasicBlock *B2 = *SI;
877879 if (Visited.insert(B2))
878880 Successors[B1].push_back(B2);
879881 }
257257
258258 // If no suitable postdominator was found, look at all the successors and
259259 // decide which one we should sink to, if any.
260 for (BasicBlock *Succ : successors(Inst->getParent())) {
261 if (SuccToSinkTo)
262 break;
263 if (IsAcceptableTarget(Inst, Succ))
264 SuccToSinkTo = Succ;
260 for (succ_iterator I = succ_begin(Inst->getParent()),
261 E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) {
262 if (IsAcceptableTarget(Inst, *I))
263 SuccToSinkTo = *I;
265264 }
266265
267266 // If we couldn't find a block to sink to, ignore this instruction.
364364 BBPredicates &Pred = Predicates[BB];
365365 BBPredicates &LPred = LoopPreds[BB];
366366
367 for (BasicBlock *Predecessor : predecessors(BB)) {
367 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
368 PI != PE; ++PI) {
369
368370 // Ignore it if it's a branch from outside into our region entry
369 if (!ParentRegion->contains(Predecessor))
371 if (!ParentRegion->contains(*PI))
370372 continue;
371373
372 Region *R = RI->getRegionFor(Predecessor);
374 Region *R = RI->getRegionFor(*PI);
373375 if (R == ParentRegion) {
374376
375377 // It's a top level block in our region
376 BranchInst *Term = cast(Predecessor->getTerminator());
378 BranchInst *Term = cast((*PI)->getTerminator());
377379 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
378380 BasicBlock *Succ = Term->getSuccessor(i);
379381 if (Succ != BB)
380382 continue;
381383
382 if (Visited.count(Predecessor)) {
384 if (Visited.count(*PI)) {
383385 // Normal forward edge
384386 if (Term->isConditional()) {
385387 // Try to treat it like an ELSE block
386388 BasicBlock *Other = Term->getSuccessor(!i);
387389 if (Visited.count(Other) && !Loops.count(Other) &&
388 !Pred.count(Other) && !Pred.count(Predecessor)) {
390 !Pred.count(Other) && !Pred.count(*PI)) {
389391
390392 Pred[Other] = BoolFalse;
391 Pred[Predecessor] = BoolTrue;
393 Pred[*PI] = BoolTrue;
392394 continue;
393395 }
394396 }
395 Pred[Predecessor] = buildCondition(Term, i, false);
397 Pred[*PI] = buildCondition(Term, i, false);
396398
397399 } else {
398400 // Back edge
399 LPred[Predecessor] = buildCondition(Term, i, true);
401 LPred[*PI] = buildCondition(Term, i, true);
400402 }
401403 }
402404
571573 if (!Term)
572574 return;
573575
574 for (BasicBlock *Succ : successors(BB))
575 delPhiValues(BB, Succ);
576 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
577 SI != SE; ++SI) {
578
579 delPhiValues(BB, *SI);
580 }
576581
577582 Term->eraseFromParent();
578583 }
586591 BasicBlock *Dominator = nullptr;
587592
588593 // Find all the edges from the sub region to the exit
589 for (BasicBlock *BB : predecessors(OldExit)) {
594 for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
595 I != E;) {
596
597 BasicBlock *BB = *I++;
590598 if (!SubRegion->contains(BB))
591599 continue;
592600
334334 }
335335 }
336336
337 for (auto *SuccBB : successors(BB)) {
337 for (auto *SuccBB : make_range(succ_begin(BB), succ_end(BB))) {
338338 auto &State = Visited[SuccBB];
339339 if (State < Escaped) {
340340 State = Escaped;
806806 // predecessors and perform TRC there. Look for predecessors that end
807807 // in unconditional branch and recursive call(s).
808808 SmallVector UncondBranchPreds;
809 for (BasicBlock *Pred : predecessors(BB)) {
809 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
810 BasicBlock *Pred = *PI;
810811 TerminatorInst *PTI = Pred->getTerminator();
811812 if (BranchInst *BI = dyn_cast(PTI))
812813 if (BI->isUnconditional())
548548
549549 // Move the remaining edges from OrigBB to point to NewBB2.
550550 SmallVector NewBB2Preds;
551 for (BasicBlock *Pred : predecessors(OrigBB)) {
551 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
552 i != e; ) {
553 BasicBlock *Pred = *i++;
552554 if (Pred == NewBB1) continue;
553555 assert(!isa(Pred->getTerminator()) &&
554556 "Cannot split an edge from an IndirectBrInst");
555557 NewBB2Preds.push_back(Pred);
558 e = pred_end(OrigBB);
556559 }
557560
558561 BasicBlock *NewBB2 = nullptr;
232232 if (PN->getIncomingBlock(i) != NewBB)
233233 OtherPreds.push_back(PN->getIncomingBlock(i));
234234 } else {
235 for (BasicBlock *P : predecessors(DestBB)) {
235 for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
236 I != E; ++I) {
237 BasicBlock *P = *I;
236238 if (P != NewBB)
237239 OtherPreds.push_back(P);
238240 }
318320 // the predecessor must be directly in TIL, not in a subloop, or again
319321 // LoopSimplify doesn't hold.
320322 SmallVector LoopPreds;
321 for (BasicBlock *P : predecessors(DestBB)) {
323 for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
324 ++I) {
325 BasicBlock *P = *I;
322326 if (P == NewBB)
323327 continue; // The new block is known.
324328 if (LI->getLoopFor(P) != TIL) {
513513 assert(NumPreds < PN->getNumIncomingValues());
514514 // Count how many times each predecessor comes to this block.
515515 std::map PredCount;
516 for (BasicBlock *Pred : predecessors(NewBB))
517 --PredCount[Pred];
516 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
517 PI != E; ++PI)
518 --PredCount[*PI];
518519
519520 // Figure out how many entries to remove from each PHI.
520521 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
9090 for (SetVector::iterator I = std::next(Result.begin()),
9191 E = Result.end();
9292 I != E; ++I)
93 for (BasicBlock *Pred : predecessors(*I))
94 assert(Result.count(Pred) &&
93 for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I);
94 PI != PE; ++PI)
95 assert(Result.count(*PI) &&
9596 "No blocks in this region may have entries from outside the region"
9697 " except for the first block!");
9798 #endif
719720 SmallPtrSet ExitBlocks;
720721 for (SetVector::iterator I = Blocks.begin(), E = Blocks.end();
721722 I != E; ++I)
722 for (BasicBlock *Succ : successors(*I))
723 if (!Blocks.count(Succ))
724 ExitBlocks.insert(Succ);
723 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
724 if (!Blocks.count(*SI))
725 ExitBlocks.insert(*SI);
725726 NumExitBlocks = ExitBlocks.size();
726727
727728 // Construct new function based on inputs/outputs & add allocas for all defs.
11281128 BasicBlock *BB = I->getParent();
11291129 // Loop over all of the successors, removing BB's entry from any PHI
11301130 // nodes.
1131 for (BasicBlock *Succ : successors(BB))
1132 Succ->removePredecessor(BB);
1131 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
1132 (*SI)->removePredecessor(BB);
11331133
11341134 // Insert a call to llvm.trap right before this. This turns the undefined
11351135 // behavior into a hard fail instead of falling through into random code.
12351235 }
12361236
12371237 Changed |= ConstantFoldTerminator(BB, true);
1238 for (BasicBlock *Succ : successors(BB))
1239 if (Reachable.insert(Succ))
1240 Worklist.push_back(Succ);
1238 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
1239 if (Reachable.insert(*SI))
1240 Worklist.push_back(*SI);
12411241 } while (!Worklist.empty());
12421242 return Changed;
12431243 }
12621262 if (Reachable.count(BB))
12631263 continue;
12641264
1265 for (BasicBlock *Succ : successors(BB))
1266 if (Reachable.count(Succ))
1267 Succ->removePredecessor(BB);
1265 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
1266 if (Reachable.count(*SI))
1267 (*SI)->removePredecessor(BB);
12681268 BB->dropAllReferences();
12691269 }
12701270
113113
114114 // Compute the set of predecessors of the loop that are not in the loop.
115115 SmallVector OutsideBlocks;
116 for (BasicBlock *P : predecessors(Header)) {
116 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
117 PI != PE; ++PI) {
118 BasicBlock *P = *PI;
117119 if (!L->contains(P)) { // Coming in from outside the loop?
118120 // If the loop is branched to from an indirect branch, we won't
119121 // be able to fully transform the loop, because it prohibits
155157 /// the loop.
156158 static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit, Pass *PP) {
157159 SmallVector LoopBlocks;
158 for (BasicBlock *P : predecessors(Exit)) {
160 for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
161 BasicBlock *P = *I;
159162 if (L->contains(P)) {
160163 // Don't do this if the loop is exited via an indirect branch.
161164 if (isa(P->getTerminator())) return nullptr;
195198 if (Blocks.insert(BB).second && BB != StopBlock)
196199 // If BB is not already processed and it is not a stop block then
197200 // insert its predecessor in the work list
198 for (BasicBlock *WBB : predecessors(BB))
201 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
202 BasicBlock *WBB = *I;
199203 Worklist.push_back(WBB);
204 }
200205 } while (!Worklist.empty());
201206 }
202207
310315 // Determine which blocks should stay in L and which should be moved out to
311316 // the Outer loop now.
312317 std::set BlocksInL;
313 for (BasicBlock *P : predecessors(Header)) {
318 for (pred_iterator PI=pred_begin(Header), E = pred_end(Header); PI!=E; ++PI) {
319 BasicBlock *P = *PI;
314320 if (DT->dominates(Header, P))
315321 addBlockAndPredsToSet(P, Header, BlocksInL);
316322 }
364370
365371 // Figure out which basic blocks contain back-edges to the loop header.
366372 std::vector BackedgeBlocks;
367 for (BasicBlock *P : predecessors(Header)) {
373 for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I){
374 BasicBlock *P = *I;
375
368376 // Indirectbr edges cannot be split, so we must fail if we find one.
369377 if (isa(P->getTerminator()))
370378 return nullptr;
479487 if (*BB == L->getHeader()) continue;
480488
481489 SmallPtrSet BadPreds;
482 for (BasicBlock *P : predecessors(*BB)) {
490 for (pred_iterator PI = pred_begin(*BB),
491 PE = pred_end(*BB); PI != PE; ++PI) {
492 BasicBlock *P = *PI;
483493 if (!L->contains(P))
484494 BadPreds.insert(P);
485495 }
492502 << (*I)->getName() << "\n");
493503
494504 // Inform each successor of each dead pred.
495 for (BasicBlock *Succ : successors(*I))
496 Succ->removePredecessor(*I);
505 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
506 (*SI)->removePredecessor(*I);
497507 // Zap the dead pred's terminator and replace it with unreachable.
498508 TerminatorInst *TI = (*I)->getTerminator();
499509 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
550560 for (SmallSetVector::iterator I = ExitBlockSet.begin(),
551561 E = ExitBlockSet.end(); I != E; ++I) {
552562 BasicBlock *ExitBlock = *I;
553 for (BasicBlock *Pred : predecessors(ExitBlock))
563 for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
564 PI != PE; ++PI)
554565 // Must be exactly this loop: no subloops, parent loops, or non-loop preds
555566 // allowed.
556 if (!L->contains(Pred)) {
567 if (!L->contains(*PI)) {
557568 if (rewriteLoopExitBlock(L, ExitBlock, PP)) {
558569 ++NumInserted;
559570 Changed = true;
327327 L->addBasicBlockToLoop(New, LI->getBase());
328328
329329 // Add phi entries for newly created values to all exit blocks.
330 for (BasicBlock *Succ : successors(*BB)) {
331 if (L->contains(Succ))
330 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB);
331 SI != SE; ++SI) {
332 if (L->contains(*SI))
332333 continue;
333 for (BasicBlock::iterator BBI = Succ->begin();
334 for (BasicBlock::iterator BBI = (*SI)->begin();
334335 PHINode *phi = dyn_cast(BBI); ++BBI) {
335336 Value *Incoming = phi->getIncomingValueForBlock(*BB);
336337 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
412413 // Remove phi operands at this loop exit
413414 if (Dest != LoopExit) {
414415 BasicBlock *BB = Latches[i];
415 for (BasicBlock *Succ : successors(BB)) {
416 if (Succ == Headers[i])
416 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
417 SI != SE; ++SI) {
418 if (*SI == Headers[i])
417419 continue;
418 for (BasicBlock::iterator BBI = Succ->begin();
420 for (BasicBlock::iterator BBI = (*SI)->begin();
419421 PHINode *Phi = dyn_cast(BBI); ++BBI) {
420422 Phi->removeIncomingValue(BB, false);
421423 }
6565 // The new PHI node is inserted in the prolog end basic block.
6666 // The new PHI name is added as an operand of a PHI node in either
6767 // the loop header or the loop exit block.
68 for (BasicBlock *Succ : successors(Latch)) {
69 for (BasicBlock::iterator BBI = Succ->begin();
68 for (succ_iterator SBI = succ_begin(Latch), SBE = succ_end(Latch);
69 SBI != SBE; ++SBI) {
70 for (BasicBlock::iterator BBI = (*SBI)->begin();
7071 PHINode *PN = dyn_cast(BBI); ++BBI) {
7172
7273 // Add a new PHI node to the prolog end block and add the
821821 // Since the value is live into BB, it is either defined in a predecessor or
822822 // live into it to. Add the preds to the worklist unless they are a
823823 // defining block.
824 for (BasicBlock *P : predecessors(BB)) {
824 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
825 BasicBlock *P = *PI;
826
825827 // The value is not live into a predecessor if it defines the value.
826828 if (DefBlocks.count(P))
827829 continue;
882884 DomTreeNode *Node = Worklist.pop_back_val();
883885 BasicBlock *BB = Node->getBlock();
884886
885 for (BasicBlock *Succ : successors(BB)) {
886 DomTreeNode *SuccNode = DT.getNode(Succ);
887 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
888 ++SI) {
889 DomTreeNode *SuccNode = DT.getNode(*SI);
887890
888891 // Quickly skip all CFG edges that are also dominator tree edges instead
889892 // of catching them below.
109109 }
110110 } else {
111111 bool isFirstPred = true;
112 for (BasicBlock *PredBB : predecessors(BB)) {
112 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
113 BasicBlock *PredBB = *PI;
113114 Value *PredVal = GetValueAtEndOfBlock(PredBB);
114115 PredValues.push_back(std::make_pair(PredBB, PredVal));
115116
246247 for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI)
247248 Preds->push_back(SomePhi->getIncomingBlock(PI));
248249 } else {
249 Preds->insert(Preds->end(), pred_begin(BB), pred_end(BB));
250 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
251 Preds->push_back(*PI);
250252 }
251253 }
252254
129129 BasicBlock *SI2BB = SI2->getParent();
130130 SmallPtrSet SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
131131
132 for (BasicBlock *Succ : successors(SI2BB))
133 if (SI1Succs.count(Succ))
134 for (BasicBlock::iterator BBI = Succ->begin();
132 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
133 if (SI1Succs.count(*I))
134 for (BasicBlock::iterator BBI = (*I)->begin();
135135 isa(BBI); ++BBI) {
136136 PHINode *PN = cast(BBI);
137137 if (PN->getIncomingValueForBlock(SI1BB) !=
170170 BasicBlock *SI1BB = SI1->getParent();
171171 BasicBlock *SI2BB = SI2->getParent();
172172 SmallPtrSet SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
173 for (BasicBlock *Succ : successors(SI2BB))
174 if (SI1Succs.count(Succ))
175 for (BasicBlock::iterator BBI = Succ->begin();
173 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
174 if (SI1Succs.count(*I))
175 for (BasicBlock::iterator BBI = (*I)->begin();
176176 isa(BBI); ++BBI) {
177177 PHINode *PN = cast(BBI);
178178 if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
682682
683683 // Remove PHI node entries for dead edges.
684684 BasicBlock *CheckEdge = TheRealDest;
685 for (BasicBlock *Succ : successors(TIBB))
686 if (Succ != CheckEdge)
687 Succ->removePredecessor(TIBB);
685 for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
686 if (*SI != CheckEdge)
687 (*SI)->removePredecessor(TIBB);
688688 else
689689 CheckEdge = nullptr;
690690
980980 // to put the select in this case.
981981 static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
982982 Instruction *I1, Instruction *I2) {
983 for (BasicBlock *Succ : successors(BB1)) {
983 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
984984 PHINode *PN;
985 for (BasicBlock::iterator BBI = Succ->begin();
985 for (BasicBlock::iterator BBI = SI->begin();
986986 (PN = dyn_cast(BBI)); ++BBI) {
987987 Value *BB1V = PN->getIncomingValueForBlock(BB1);
988988 Value *BB2V = PN->getIncomingValueForBlock(BB2);
10621062 if (isa(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
10631063 return Changed;
10641064
1065 for (BasicBlock *Succ : successors(BB1)) {
1065 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
10661066 PHINode *PN;
1067 for (BasicBlock::iterator BBI = Succ->begin();
1067 for (BasicBlock::iterator BBI = SI->begin();
10681068 (PN = dyn_cast(BBI)); ++BBI) {
10691069 Value *BB1V = PN->getIncomingValueForBlock(BB1);
10701070 Value *BB2V = PN->getIncomingValueForBlock(BB2);
10931093 // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
10941094 // nodes, so we insert select instruction to compute the final result.
10951095 std::map, SelectInst*> InsertedSelects;
1096 for (BasicBlock *Succ : successors(BB1)) {
1096 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
10971097 PHINode *PN;
1098 for (BasicBlock::iterator BBI = Succ->begin();
1098 for (BasicBlock::iterator BBI = SI->begin();
10991099 (PN = dyn_cast(BBI)); ++BBI) {
11001100 Value *BB1V = PN->getIncomingValueForBlock(BB1);
11011101 Value *BB2V = PN->getIncomingValueForBlock(BB2);
11171117 }
11181118
11191119 // Update any PHI nodes in our new successors.
1120 for (BasicBlock *Succ : successors(BB1))
1121 AddPredecessorToBlock(Succ, BIParent, BB1);
1120 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
1121 AddPredecessorToBlock(*SI, BIParent, BB1);
11221122
11231123 EraseTerminatorInstAndDCECond(BI);
11241124 return true;
20502050 if (TrueDest == BB || FalseDest == BB)
20512051 return false;
20522052
2053 for (BasicBlock *PredBlock : predecessors(BB)) {
2053 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2054 BasicBlock *PredBlock = *PI;
20542055 BranchInst *PBI = dyn_cast(PredBlock->getTerminator());
20552056
20562057 // Check that we have two conditional branches. If there is a PHI node in
28832884 // Turn all invokes that unwind here into calls and delete the basic block.
28842885 bool InvokeRequiresTableEntry = false;
28852886 bool Changed = false;
2886 for (BasicBlock *Pred : predecessors(BB)) {
2887 InvokeInst *II = cast(Pred->getTerminator());
2887 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
2888 InvokeInst *II = cast((*PI++)->getTerminator());
28882889
28892890 if (II->hasFnAttr(Attribute::UWTable)) {
28902891 // Don't remove an `invoke' instruction if the ABI requires an entry into
29312932 // Find predecessors that end with branches.
29322933 SmallVector UncondBranchPreds;
29332934 SmallVector CondBranchPreds;
2934 for (BasicBlock *P : predecessors(BB)) {
2935 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2936 BasicBlock *P = *PI;
29352937 TerminatorInst *PTI = P->getTerminator();
29362938 if (BranchInst *BI = dyn_cast(PTI)) {
29372939 if (BI->isUnconditional())
40974099 return SimplifyCFG(BB, TTI, DL) | true;
40984100
40994101 // Scan predecessor blocks for conditional branches.
4100 for (BasicBlock *Pred : predecessors(BB))
4101 if (BranchInst *PBI = dyn_cast(Pred->getTerminator()))
4102 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
4103 if (BranchInst *PBI = dyn_cast((*PI)->getTerminator()))
41024104 if (PBI != BI && PBI->isConditional())
41034105 if (SimplifyCondBranchToCondBranch(PBI, BI))
41044106 return SimplifyCFG(BB, TTI, DL) | true;
29492949 Value *Zero = ConstantInt::get(IntegerType::getInt1Ty(BB->getContext()), 0);
29502950 VectorParts BlockMask = getVectorValue(Zero);
29512951
2952 for (BasicBlock *Pred : predecessors(BB)) {
2953 VectorParts EM = createEdgeMask(Pred, BB);
2952 // For each pred:
2953 for (pred_iterator it = pred_begin(BB), e = pred_end(BB); it != e; ++it) {
2954 VectorParts EM = createEdgeMask(*it, BB);
29542955 for (unsigned part = 0; part < UF; ++part)
29552956 BlockMask[part] = Builder.CreateOr(BlockMask[part], EM[part]);
29562957 }
291291 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
292292 // Loop over all of the successors of this block, deleting any PHI nodes
293293 // that might include it.
294 for (BasicBlock *Succ : successors(BB))
295 Succ->removePredecessor(BB);
294 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
295 (*SI)->removePredecessor(BB);
296296
297297 TerminatorInst *BBTerm = BB->getTerminator();
298298
124124
125125 unsigned getUnprocPredCount(BasicBlock *Block) const {
126126 unsigned Count = 0;
127 for (BasicBlock *Pred : predecessors(Block))
128 if (!Blocks.count(Pred)) Count++;
127 for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I)
128 if (!Blocks.count(*I)) Count++;
129129 return Count;
130130 }
131131