llvm.org GIT mirror llvm / a4697da
[C++11] Add predecessors(BasicBlock *) / successors(BasicBlock *) iterator ranges. Summary: This patch introduces two new iterator ranges and updates existing code to use it. No functional change intended. Test Plan: All tests (make check-all) still pass. Reviewers: dblaikie Reviewed By: dblaikie Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D4481 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213474 91177308-0d34-0410-b5e6-96231b3b80d8 Manuel Jacob 5 years ago
41 changed file(s) with 166 addition(s) and 226 deletion(s). Raw diff Collapse all Expand all
18531853 #include "llvm/Support/CFG.h"
18541854 BasicBlock *BB = ...;
18551855
1856 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1857 BasicBlock *Pred = *PI;
1856 for (BasicBlock *Pred : predecessors(BB)) {
18581857 // ...
18591858 }
18601859
1861 Similarly, to iterate over successors use ``succ_iterator/succ_begin/succ_end``.
1860 Similarly, to iterate over successors use ``successors``.
18621861
18631862 .. _simplechanges:
18641863
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));
94100 }
95101
96102
256262 inline succ_const_iterator succ_end(const BasicBlock *BB) {
257263 return succ_const_iterator(BB->getTerminator(), true);
258264 }
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 }
259271
260272 template struct isPodLike > {
261273 static const bool value = isPodLike::value;
529529 assert(LastF && "Cannot print prior to running over a function");
530530 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
531531 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);
532 for (const BasicBlock *Succ : successors(BI)) {
533 printEdgeProbability(OS << " ", BI, Succ);
535534 }
536535 }
537536 }
562561 uint32_t MaxWeight = 0;
563562 BasicBlock *MaxSucc = nullptr;
564563
565 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
566 BasicBlock *Succ = *I;
564 for (BasicBlock *Succ : successors(BB)) {
567565 uint32_t Weight = getEdgeWeight(BB, Succ);
568566 uint32_t PrevSum = Sum;
569567
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 (::pred_iterator I = ::pred_begin(HeaderNode), E = ::pred_end(HeaderNode);
32 I != E; ++I)
33 if (contains(*I))
31 for (BasicBlock *Pred : predecessors(HeaderNode))
32 if (contains(Pred))
3433 return true;
3534 return false;
3635 }
624624 // Loop over all of our predecessors, merging what we know from them into
625625 // result.
626626 bool EdgesMissing = false;
627 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
627 for (BasicBlock *Pred : predecessors(BB)) {
628628 LVILatticeVal EdgeResult;
629 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
629 EdgesMissing |= !getEdgeValue(Val, Pred, 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 (pred_iterator PI = pred_begin(ExitBlocks[i]),
339 PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
340 if (!contains(*PI))
338 for (BasicBlock *Pred : predecessors(ExitBlocks[i]))
339 if (!contains(Pred))
341340 return false;
342341 // All the requirements are met.
343342 return true;
359358 BasicBlock *current = *BI;
360359 switchExitBlocks.clear();
361360
362 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
361 for (BasicBlock *Succ : successors(*BI)) {
363362 // If block is inside the loop then it is not a exit block.
364 if (contains(*I))
363 if (contains(Succ))
365364 continue;
366365
367 pred_iterator PI = pred_begin(*I);
366 pred_iterator PI = pred_begin(Succ);
368367 BasicBlock *firstPred = *PI;
369368
370369 // If current basic block is this exit block's first predecessor
378377 // then it is possible that there are multiple edges from current block
379378 // to one exit block.
380379 if (std::distance(succ_begin(current), succ_end(current)) <= 2) {
381 ExitBlocks.push_back(*I);
380 ExitBlocks.push_back(Succ);
382381 continue;
383382 }
384383
385384 // In case of multiple edges from current block to exit block, collect
386385 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
387386 // duplicate edges.
388 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
387 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), Succ)
389388 == switchExitBlocks.end()) {
390 switchExitBlocks.push_back(*I);
391 ExitBlocks.push_back(*I);
389 switchExitBlocks.push_back(Succ);
390 ExitBlocks.push_back(Succ);
392391 }
393392 }
394393 }
45114511 // lead to the loop header.
45124512 bool MustExecuteLoopHeader = true;
45134513 BasicBlock *Exit = nullptr;
4514 for (succ_iterator SI = succ_begin(ExitingBlock), SE = succ_end(ExitingBlock);
4515 SI != SE; ++SI)
4516 if (!L->contains(*SI)) {
4514 for (BasicBlock *Succ : successors(ExitingBlock))
4515 if (!L->contains(Succ)) {
45174516 if (Exit) // Multiple exit successors.
45184517 return getCouldNotCompute();
4519 Exit = *SI;
4520 } else if (*SI != L->getHeader()) {
4518 Exit = Succ;
4519 } else if (Succ != L->getHeader()) {
45214520 MustExecuteLoopHeader = false;
45224521 }
45234522
452452 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
453453 PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
454454 } else {
455 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
456 PN->addIncoming(InVal, *PI);
455 for (BasicBlock *Pred : predecessors(BB))
456 PN->addIncoming(InVal, Pred);
457457 }
458458 }
459459 }
976976 }
977977 } else {
978978 SmallPtrSet VisitedBBs;
979 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
980 if (!VisitedBBs.insert(*PI))
979 for (BasicBlock *Pred : predecessors(BB)) {
980 if (!VisitedBBs.insert(Pred))
981981 continue;
982982
983 BasicBlock::InstListType &InstList = (*PI)->getInstList();
983 BasicBlock::InstListType &InstList = Pred->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_pred_iterator PI = pred_begin(LLVMBB), PE = pred_end(LLVMBB);
1055 PI != PE; ++PI) {
1056 if (!FuncInfo->VisitedBBs.count(*PI)) {
1054 for (const BasicBlock *Pred : predecessors(LLVMBB)) {
1055 if (!FuncInfo->VisitedBBs.count(Pred)) {
10571056 AllPredsVisited = false;
10581057 break;
10591058 }
141141 if (!LiveBBs.insert(BB))
142142 return; // already been here.
143143
144 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
145 MarkBlocksLiveIn(*PI, LiveBBs);
144 for (BasicBlock *Pred : predecessors(BB))
145 MarkBlocksLiveIn(Pred, 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 (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
82 (*SI)->removePredecessor(BB);
81 for (BasicBlock *S : successors(BB))
82 S->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 (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
323 for (BasicBlock *Successor : successors(New)) {
324324 // Loop over any phi nodes in the basic block, updating the BB field of
325325 // incoming values...
326 BasicBlock *Successor = *I;
327326 PHINode *PN;
328327 for (BasicBlock::iterator II = Successor->begin();
329328 (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_pred_iterator PI = pred_begin(End), E = pred_end(End);
182 PI != E; ++PI) {
183 const BasicBlock *BB = *PI;
181 for (const BasicBlock *BB : predecessors(End)) {
184182 if (BB == Start)
185183 continue;
186184
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 (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
2110 const InvokeInst *II = dyn_cast((*I)->getTerminator());
2109 for (BasicBlock *Pred : predecessors(BB)) {
2110 const InvokeInst *II = dyn_cast(Pred->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 (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
476 BasicBlock *P = *PI;
475 for (BasicBlock *P : predecessors(BB)) {
477476 for (idf_ext_iterator >
478477 I = idf_ext_begin(P, TranspBlocks),
479478 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 (pred_iterator
232 PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) {
233 BasicBlock *BB = *PI;
231 for (BasicBlock *BB : predecessors(LPad)) {
234232 if (BB->isLandingPad() && BB != Parent &&
235233 isa(Parent->getTerminator())) {
236234 Split = true;
5757 BasicBlock* returnBlock = nullptr;
5858 BasicBlock* nonReturnBlock = nullptr;
5959 unsigned returnCount = 0;
60 for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
61 SI != SE; ++SI)
62 if (isa((*SI)->getTerminator())) {
63 returnBlock = *SI;
60 for (BasicBlock *Succ : successors(entryBlock))
61 if (isa(Succ->getTerminator())) {
62 returnBlock = Succ;
6463 returnCount++;
6564 } else
66 nonReturnBlock = *SI;
65 nonReturnBlock = Succ;
6766
6867 if (returnCount != 1)
6968 return nullptr;
27172717 if (UserParent != BB) {
27182718 bool UserIsSuccessor = false;
27192719 // See if the user is one of our successors.
2720 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
2721 if (*SI == UserParent) {
2720 for (BasicBlock *Succ : successors(BB))
2721 if (Succ == UserParent) {
27222722 UserIsSuccessor = true;
27232723 break;
27242724 }
641641 /// them to F.
642642 static void FindUnconditionalPreds(SmallVectorImpl &Blocks,
643643 BasicBlock *BB, DominatorTree *DT) {
644 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
645 BasicBlock *Pred = *I;
644 for (BasicBlock *Pred : predecessors(BB)) {
646645 if (Pred == BB) continue;
647646 TerminatorInst *PredTI = Pred->getTerminator();
648647 if (PredTI->getNumSuccessors() != 1)
15541554 FullyAvailableBlocks[UnavailableBlocks[i]] = false;
15551555
15561556 SmallVector CriticalEdgePred;
1557 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
1558 PI != E; ++PI) {
1559 BasicBlock *Pred = *PI;
1557 for (BasicBlock *Pred : predecessors(LoadBB)) {
15601558 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
15611559 continue;
15621560 }
24822480 BasicBlock *PREPred = nullptr;
24832481 predMap.clear();
24842482
2485 for (pred_iterator PI = pred_begin(CurrentBlock),
2486 PE = pred_end(CurrentBlock); PI != PE; ++PI) {
2487 BasicBlock *P = *PI;
2483 for (BasicBlock *P : predecessors(CurrentBlock)) {
24882484 // We're not interested in PRE where the block is its
24892485 // own predecessor, or in blocks with predecessors
24902486 // that are not reachable.
27122708 for (SmallVectorImpl::iterator I = Dom.begin(),
27132709 E = Dom.end(); I != E; I++) {
27142710 BasicBlock *B = *I;
2715 for (succ_iterator SI = succ_begin(B), SE = succ_end(B); SI != SE; SI++) {
2716 BasicBlock *S = *SI;
2711 for (BasicBlock *S : successors(B)) {
27172712 if (DeadBlocks.count(S))
27182713 continue;
27192714
27202715 bool AllPredDead = true;
2721 for (pred_iterator PI = pred_begin(S), PE = pred_end(S); PI != PE; PI++)
2722 if (!DeadBlocks.count(*PI)) {
2716 for (BasicBlock *Pred : predecessors(S))
2717 if (!DeadBlocks.count(Pred)) {
27232718 AllPredDead = false;
27242719 break;
27252720 }
352352
353353 // If V is a constant, then it is known in all predecessors.
354354 if (Constant *KC = getKnownConstant(V, Preference)) {
355 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
356 Result.push_back(std::make_pair(KC, *PI));
355 for (BasicBlock *Pred : predecessors(BB))
356 Result.push_back(std::make_pair(KC, Pred));
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 (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
380 BasicBlock *P = *PI;
379 for (BasicBlock *P : predecessors(BB)) {
381380 // If the value is known by LazyValueInfo to be a constant in a
382381 // predecessor, use that information to try to thread this block.
383382 Constant *PredCst = LVI->getConstantOnEdge(V, P, BB);
531530 cast(Cmp->getOperand(0))->getParent() != BB) {
532531 Constant *RHSCst = cast(Cmp->getOperand(1));
533532
534 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);PI != E; ++PI){
535 BasicBlock *P = *PI;
533 for (BasicBlock *P : predecessors(BB)) {
536534 // If the value is known by LazyValueInfo to be a constant in a
537535 // predecessor, use that information to try to thread this block.
538536 LazyValueInfo::Tristate Res =
605603 // If all else fails, see if LVI can figure out a constant value for us.
606604 Constant *CI = LVI->getConstant(V, BB);
607605 if (Constant *KC = getKnownConstant(CI, Preference)) {
608 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
609 Result.push_back(std::make_pair(KC, *PI));
606 for (BasicBlock *Pred : predecessors(BB))
607 Result.push_back(std::make_pair(KC, Pred));
610608 }
611609
612610 return !Result.empty();
898896
899897 // If we got here, the loaded value is transparent through to the start of the
900898 // block. Check to see if it is available in any of the predecessor blocks.
901 for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
902 PI != PE; ++PI) {
903 BasicBlock *PredBB = *PI;
904
899 for (BasicBlock *PredBB : predecessors(LoadBB)) {
905900 // If we already scanned this predecessor, skip it.
906901 if (!PredsScanned.insert(PredBB))
907902 continue;
951946 AvailablePredSet.insert(AvailablePreds[i].first);
952947
953948 // Add all the unavailable predecessors to the PredsToSplit list.
954 for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
955 PI != PE; ++PI) {
956 BasicBlock *P = *PI;
949 for (BasicBlock *P : predecessors(LoadBB)) {
957950 // If the predecessor is an indirect goto, we can't split the edge.
958951 if (isa(P->getTerminator()))
959952 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 (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
148 ++SI) {
149 BasicBlock *SuccBB = *SI;
147 for (BasicBlock *SuccBB : successors(BB)) {
150148 if (!Visited.insert(SuccBB))
151149 continue;
152150
514514 }
515515
516516 // Otherwise, this is an unvisited intra-loop node. Check all successors.
517 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
517 for (BasicBlock *Succ : successors(BB)) {
518518 // Check to see if the successor is a trivial loop exit.
519 if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
519 if (!isTrivialLoopExitBlockHelper(L, Succ, ExitBB, Visited))
520520 return false;
521521 }
522522
860860 PHINode *PN = PHINode::Create(LPad->getType(), 0, "",
861861 ExitSucc->getFirstInsertionPt());
862862
863 for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc);
864 I != E; ++I) {
865 BasicBlock *BB = *I;
863 for (BasicBlock *BB : predecessors(ExitSucc)) {
866864 LandingPadInst *LPI = BB->getLandingPadInst();
867865 LPI->replaceAllUsesWith(PN);
868866 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 (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) {
868 BasicBlock *B2 = *PI;
867 for (BasicBlock *B2 : predecessors(B1)) {
869868 if (Visited.insert(B2))
870869 Predecessors[B1].push_back(B2);
871870 }
874873 Visited.clear();
875874 if (!Successors[B1].empty())
876875 llvm_unreachable("Found a stale successors list in a basic block.");
877 for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) {
878 BasicBlock *B2 = *SI;
876 for (BasicBlock *B2 : successors(B1)) {
879877 if (Visited.insert(B2))
880878 Successors[B1].push_back(B2);
881879 }
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 (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;
260 for (BasicBlock *Succ : successors(Inst->getParent())) {
261 if (SuccToSinkTo)
262 break;
263 if (IsAcceptableTarget(Inst, Succ))
264 SuccToSinkTo = Succ;
264265 }
265266
266267 // 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 (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
368 PI != PE; ++PI) {
369
367 for (BasicBlock *Predecessor : predecessors(BB)) {
370368 // Ignore it if it's a branch from outside into our region entry
371 if (!ParentRegion->contains(*PI))
369 if (!ParentRegion->contains(Predecessor))
372370 continue;
373371
374 Region *R = RI->getRegionFor(*PI);
372 Region *R = RI->getRegionFor(Predecessor);
375373 if (R == ParentRegion) {
376374
377375 // It's a top level block in our region
378 BranchInst *Term = cast((*PI)->getTerminator());
376 BranchInst *Term = cast(Predecessor->getTerminator());
379377 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
380378 BasicBlock *Succ = Term->getSuccessor(i);
381379 if (Succ != BB)
382380 continue;
383381
384 if (Visited.count(*PI)) {
382 if (Visited.count(Predecessor)) {
385383 // Normal forward edge
386384 if (Term->isConditional()) {
387385 // Try to treat it like an ELSE block
388386 BasicBlock *Other = Term->getSuccessor(!i);
389387 if (Visited.count(Other) && !Loops.count(Other) &&
390 !Pred.count(Other) && !Pred.count(*PI)) {
388 !Pred.count(Other) && !Pred.count(Predecessor)) {
391389
392390 Pred[Other] = BoolFalse;
393 Pred[*PI] = BoolTrue;
391 Pred[Predecessor] = BoolTrue;
394392 continue;
395393 }
396394 }
397 Pred[*PI] = buildCondition(Term, i, false);
395 Pred[Predecessor] = buildCondition(Term, i, false);
398396
399397 } else {
400398 // Back edge
401 LPred[*PI] = buildCondition(Term, i, true);
399 LPred[Predecessor] = buildCondition(Term, i, true);
402400 }
403401 }
404402
573571 if (!Term)
574572 return;
575573
576 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
577 SI != SE; ++SI) {
578
579 delPhiValues(BB, *SI);
580 }
574 for (BasicBlock *Succ : successors(BB))
575 delPhiValues(BB, Succ);
581576
582577 Term->eraseFromParent();
583578 }
591586 BasicBlock *Dominator = nullptr;
592587
593588 // Find all the edges from the sub region to the exit
594 for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
595 I != E;) {
596
597 BasicBlock *BB = *I++;
589 for (BasicBlock *BB : predecessors(OldExit)) {
598590 if (!SubRegion->contains(BB))
599591 continue;
600592
334334 }
335335 }
336336
337 for (auto *SuccBB : make_range(succ_begin(BB), succ_end(BB))) {
337 for (auto *SuccBB : successors(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 (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
810 BasicBlock *Pred = *PI;
809 for (BasicBlock *Pred : predecessors(BB)) {
811810 TerminatorInst *PTI = Pred->getTerminator();
812811 if (BranchInst *BI = dyn_cast(PTI))
813812 if (BI->isUnconditional())
548548
549549 // Move the remaining edges from OrigBB to point to NewBB2.
550550 SmallVector NewBB2Preds;
551 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
552 i != e; ) {
553 BasicBlock *Pred = *i++;
551 for (BasicBlock *Pred : predecessors(OrigBB)) {
554552 if (Pred == NewBB1) continue;
555553 assert(!isa(Pred->getTerminator()) &&
556554 "Cannot split an edge from an IndirectBrInst");
557555 NewBB2Preds.push_back(Pred);
558 e = pred_end(OrigBB);
559556 }
560557
561558 BasicBlock *NewBB2 = nullptr;
232232 if (PN->getIncomingBlock(i) != NewBB)
233233 OtherPreds.push_back(PN->getIncomingBlock(i));
234234 } else {
235 for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
236 I != E; ++I) {
237 BasicBlock *P = *I;
235 for (BasicBlock *P : predecessors(DestBB)) {
238236 if (P != NewBB)
239237 OtherPreds.push_back(P);
240238 }
320318 // the predecessor must be directly in TIL, not in a subloop, or again
321319 // LoopSimplify doesn't hold.
322320 SmallVector LoopPreds;
323 for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
324 ++I) {
325 BasicBlock *P = *I;
321 for (BasicBlock *P : predecessors(DestBB)) {
326322 if (P == NewBB)
327323 continue; // The new block is known.
328324 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 (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
517 PI != E; ++PI)
518 --PredCount[*PI];
516 for (BasicBlock *Pred : predecessors(NewBB))
517 --PredCount[Pred];
519518
520519 // Figure out how many entries to remove from each PHI.
521520 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 (pred_iterator PI = pred_begin(*I), PE = pred_end(*I);
94 PI != PE; ++PI)
95 assert(Result.count(*PI) &&
93 for (BasicBlock *Pred : predecessors(*I))
94 assert(Result.count(Pred) &&
9695 "No blocks in this region may have entries from outside the region"
9796 " except for the first block!");
9897 #endif
720719 SmallPtrSet ExitBlocks;
721720 for (SetVector::iterator I = Blocks.begin(), E = Blocks.end();
722721 I != E; ++I)
723 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
724 if (!Blocks.count(*SI))
725 ExitBlocks.insert(*SI);
722 for (BasicBlock *Succ : successors(*I))
723 if (!Blocks.count(Succ))
724 ExitBlocks.insert(Succ);
726725 NumExitBlocks = ExitBlocks.size();
727726
728727 // 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 (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
1132 (*SI)->removePredecessor(BB);
1131 for (BasicBlock *Succ : successors(BB))
1132 Succ->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 (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
1239 if (Reachable.insert(*SI))
1240 Worklist.push_back(*SI);
1238 for (BasicBlock *Succ : successors(BB))
1239 if (Reachable.insert(Succ))
1240 Worklist.push_back(Succ);
12411241 } while (!Worklist.empty());
12421242 return Changed;
12431243 }
12621262 if (Reachable.count(BB))
12631263 continue;
12641264
1265 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
1266 if (Reachable.count(*SI))
1267 (*SI)->removePredecessor(BB);
1265 for (BasicBlock *Succ : successors(BB))
1266 if (Reachable.count(Succ))
1267 Succ->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 (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
117 PI != PE; ++PI) {
118 BasicBlock *P = *PI;
116 for (BasicBlock *P : predecessors(Header)) {
119117 if (!L->contains(P)) { // Coming in from outside the loop?
120118 // If the loop is branched to from an indirect branch, we won't
121119 // be able to fully transform the loop, because it prohibits
157155 /// the loop.
158156 static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit, Pass *PP) {
159157 SmallVector LoopBlocks;
160 for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
161 BasicBlock *P = *I;
158 for (BasicBlock *P : predecessors(Exit)) {
162159 if (L->contains(P)) {
163160 // Don't do this if the loop is exited via an indirect branch.
164161 if (isa(P->getTerminator())) return nullptr;
198195 if (Blocks.insert(BB).second && BB != StopBlock)
199196 // If BB is not already processed and it is not a stop block then
200197 // insert its predecessor in the work list
201 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
202 BasicBlock *WBB = *I;
198 for (BasicBlock *WBB : predecessors(BB))
203199 Worklist.push_back(WBB);
204 }
205200 } while (!Worklist.empty());
206201 }
207202
315310 // Determine which blocks should stay in L and which should be moved out to
316311 // the Outer loop now.
317312 std::set BlocksInL;
318 for (pred_iterator PI=pred_begin(Header), E = pred_end(Header); PI!=E; ++PI) {
319 BasicBlock *P = *PI;
313 for (BasicBlock *P : predecessors(Header)) {
320314 if (DT->dominates(Header, P))
321315 addBlockAndPredsToSet(P, Header, BlocksInL);
322316 }
370364
371365 // Figure out which basic blocks contain back-edges to the loop header.
372366 std::vector BackedgeBlocks;
373 for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I){
374 BasicBlock *P = *I;
375
367 for (BasicBlock *P : predecessors(Header)) {
376368 // Indirectbr edges cannot be split, so we must fail if we find one.
377369 if (isa(P->getTerminator()))
378370 return nullptr;
487479 if (*BB == L->getHeader()) continue;
488480
489481 SmallPtrSet BadPreds;
490 for (pred_iterator PI = pred_begin(*BB),
491 PE = pred_end(*BB); PI != PE; ++PI) {
492 BasicBlock *P = *PI;
482 for (BasicBlock *P : predecessors(*BB)) {
493483 if (!L->contains(P))
494484 BadPreds.insert(P);
495485 }
502492 << (*I)->getName() << "\n");
503493
504494 // Inform each successor of each dead pred.
505 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
506 (*SI)->removePredecessor(*I);
495 for (BasicBlock *Succ : successors(*I))
496 Succ->removePredecessor(*I);
507497 // Zap the dead pred's terminator and replace it with unreachable.
508498 TerminatorInst *TI = (*I)->getTerminator();
509499 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
560550 for (SmallSetVector::iterator I = ExitBlockSet.begin(),
561551 E = ExitBlockSet.end(); I != E; ++I) {
562552 BasicBlock *ExitBlock = *I;
563 for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
564 PI != PE; ++PI)
553 for (BasicBlock *Pred : predecessors(ExitBlock))
565554 // Must be exactly this loop: no subloops, parent loops, or non-loop preds
566555 // allowed.
567 if (!L->contains(*PI)) {
556 if (!L->contains(Pred)) {
568557 if (rewriteLoopExitBlock(L, ExitBlock, PP)) {
569558 ++NumInserted;
570559 Changed = true;
327327 L->addBasicBlockToLoop(New, LI->getBase());
328328
329329 // Add phi entries for newly created values to all exit blocks.
330 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB);
331 SI != SE; ++SI) {
332 if (L->contains(*SI))
330 for (BasicBlock *Succ : successors(*BB)) {
331 if (L->contains(Succ))
333332 continue;
334 for (BasicBlock::iterator BBI = (*SI)->begin();
333 for (BasicBlock::iterator BBI = Succ->begin();
335334 PHINode *phi = dyn_cast(BBI); ++BBI) {
336335 Value *Incoming = phi->getIncomingValueForBlock(*BB);
337336 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
413412 // Remove phi operands at this loop exit
414413 if (Dest != LoopExit) {
415414 BasicBlock *BB = Latches[i];
416 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
417 SI != SE; ++SI) {
418 if (*SI == Headers[i])
415 for (BasicBlock *Succ : successors(BB)) {
416 if (Succ == Headers[i])
419417 continue;
420 for (BasicBlock::iterator BBI = (*SI)->begin();
418 for (BasicBlock::iterator BBI = Succ->begin();
421419 PHINode *Phi = dyn_cast(BBI); ++BBI) {
422420 Phi->removeIncomingValue(BB, false);
423421 }
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 (succ_iterator SBI = succ_begin(Latch), SBE = succ_end(Latch);
69 SBI != SBE; ++SBI) {
70 for (BasicBlock::iterator BBI = (*SBI)->begin();
68 for (BasicBlock *Succ : successors(Latch)) {
69 for (BasicBlock::iterator BBI = Succ->begin();
7170 PHINode *PN = dyn_cast(BBI); ++BBI) {
7271
7372 // 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 (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
825 BasicBlock *P = *PI;
826
824 for (BasicBlock *P : predecessors(BB)) {
827825 // The value is not live into a predecessor if it defines the value.
828826 if (DefBlocks.count(P))
829827 continue;
884882 DomTreeNode *Node = Worklist.pop_back_val();
885883 BasicBlock *BB = Node->getBlock();
886884
887 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
888 ++SI) {
889 DomTreeNode *SuccNode = DT.getNode(*SI);
885 for (BasicBlock *Succ : successors(BB)) {
886 DomTreeNode *SuccNode = DT.getNode(Succ);
890887
891888 // Quickly skip all CFG edges that are also dominator tree edges instead
892889 // of catching them below.
109109 }
110110 } else {
111111 bool isFirstPred = true;
112 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
113 BasicBlock *PredBB = *PI;
112 for (BasicBlock *PredBB : predecessors(BB)) {
114113 Value *PredVal = GetValueAtEndOfBlock(PredBB);
115114 PredValues.push_back(std::make_pair(PredBB, PredVal));
116115
247246 for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI)
248247 Preds->push_back(SomePhi->getIncomingBlock(PI));
249248 } else {
250 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
251 Preds->push_back(*PI);
249 Preds->insert(Preds->end(), pred_begin(BB), pred_end(BB));
252250 }
253251 }
254252
129129 BasicBlock *SI2BB = SI2->getParent();
130130 SmallPtrSet SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
131131
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();
132 for (BasicBlock *Succ : successors(SI2BB))
133 if (SI1Succs.count(Succ))
134 for (BasicBlock::iterator BBI = Succ->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 (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();
173 for (BasicBlock *Succ : successors(SI2BB))
174 if (SI1Succs.count(Succ))
175 for (BasicBlock::iterator BBI = Succ->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 (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
686 if (*SI != CheckEdge)
687 (*SI)->removePredecessor(TIBB);
685 for (BasicBlock *Succ : successors(TIBB))
686 if (Succ != CheckEdge)
687 Succ->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 (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
983 for (BasicBlock *Succ : successors(BB1)) {
984984 PHINode *PN;
985 for (BasicBlock::iterator BBI = SI->begin();
985 for (BasicBlock::iterator BBI = Succ->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 (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
1065 for (BasicBlock *Succ : successors(BB1)) {
10661066 PHINode *PN;
1067 for (BasicBlock::iterator BBI = SI->begin();
1067 for (BasicBlock::iterator BBI = Succ->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 (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
1096 for (BasicBlock *Succ : successors(BB1)) {
10971097 PHINode *PN;
1098 for (BasicBlock::iterator BBI = SI->begin();
1098 for (BasicBlock::iterator BBI = Succ->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 (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
1121 AddPredecessorToBlock(*SI, BIParent, BB1);
1120 for (BasicBlock *Succ : successors(BB1))
1121 AddPredecessorToBlock(Succ, BIParent, BB1);
11221122
11231123 EraseTerminatorInstAndDCECond(BI);
11241124 return true;
20502050 if (TrueDest == BB || FalseDest == BB)
20512051 return false;
20522052
2053 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2054 BasicBlock *PredBlock = *PI;
2053 for (BasicBlock *PredBlock : predecessors(BB)) {
20552054 BranchInst *PBI = dyn_cast(PredBlock->getTerminator());
20562055
20572056 // Check that we have two conditional branches. If there is a PHI node in
28842883 // Turn all invokes that unwind here into calls and delete the basic block.
28852884 bool InvokeRequiresTableEntry = false;
28862885 bool Changed = false;
2887 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
2888 InvokeInst *II = cast((*PI++)->getTerminator());
2886 for (BasicBlock *Pred : predecessors(BB)) {
2887 InvokeInst *II = cast(Pred->getTerminator());
28892888
28902889 if (II->hasFnAttr(Attribute::UWTable)) {
28912890 // Don't remove an `invoke' instruction if the ABI requires an entry into
29322931 // Find predecessors that end with branches.
29332932 SmallVector UncondBranchPreds;
29342933 SmallVector CondBranchPreds;
2935 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2936 BasicBlock *P = *PI;
2934 for (BasicBlock *P : predecessors(BB)) {
29372935 TerminatorInst *PTI = P->getTerminator();
29382936 if (BranchInst *BI = dyn_cast(PTI)) {
29392937 if (BI->isUnconditional())
40994097 return SimplifyCFG(BB, TTI, DL) | true;
41004098
41014099 // Scan predecessor blocks for conditional branches.
4102 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
4103 if (BranchInst *PBI = dyn_cast((*PI)->getTerminator()))
4100 for (BasicBlock *Pred : predecessors(BB))
4101 if (BranchInst *PBI = dyn_cast(Pred->getTerminator()))
41044102 if (PBI != BI && PBI->isConditional())
41054103 if (SimplifyCondBranchToCondBranch(PBI, BI))
41064104 return SimplifyCFG(BB, TTI, DL) | true;
29332933 Value *Zero = ConstantInt::get(IntegerType::getInt1Ty(BB->getContext()), 0);
29342934 VectorParts BlockMask = getVectorValue(Zero);
29352935
2936 // For each pred:
2937 for (pred_iterator it = pred_begin(BB), e = pred_end(BB); it != e; ++it) {
2938 VectorParts EM = createEdgeMask(*it, BB);
2936 for (BasicBlock *Pred : predecessors(BB)) {
2937 VectorParts EM = createEdgeMask(Pred, BB);
29392938 for (unsigned part = 0; part < UF; ++part)
29402939 BlockMask[part] = Builder.CreateOr(BlockMask[part], EM[part]);
29412940 }
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 (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
295 (*SI)->removePredecessor(BB);
294 for (BasicBlock *Succ : successors(BB))
295 Succ->removePredecessor(BB);
296296
297297 TerminatorInst *BBTerm = BB->getTerminator();
298298
124124
125125 unsigned getUnprocPredCount(BasicBlock *Block) const {
126126 unsigned Count = 0;
127 for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I)
128 if (!Blocks.count(*I)) Count++;
127 for (BasicBlock *Pred : predecessors(Block))
128 if (!Blocks.count(Pred)) Count++;
129129 return Count;
130130 }
131131