llvm.org GIT mirror llvm / c89f135
[PlaceSafepoints] Remove dependence on LoopSimplify As a step towards getting rid of internal pass manager hack entirely, remove the need for loop simplify to run in the inner pass manager. The new code does produce slightly different loop structures, so this isn't technically NFC. Differential Revision: http://reviews.llvm.org/D9585 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237172 91177308-0d34-0410-b5e6-96231b3b80d8 Philip Reames 4 years ago
2 changed file(s) with 44 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
5050 #include "llvm/Pass.h"
5151 #include "llvm/IR/LegacyPassManager.h"
5252 #include "llvm/ADT/SetOperations.h"
53 #include "llvm/ADT/SetVector.h"
5354 #include "llvm/ADT/Statistic.h"
5455 #include "llvm/Analysis/LoopPass.h"
5556 #include "llvm/Analysis/LoopInfo.h"
128129 bool runOnLoop(Loop *, LPPassManager &LPM) override;
129130
130131 void getAnalysisUsage(AnalysisUsage &AU) const override {
131 // needed for determining if the loop is finite
132132 AU.addRequired();
133 // to ensure each edge has a single backedge
134 // TODO: is this still required?
135 AU.addRequiredID(LoopSimplifyID);
136
137133 // We no longer modify the IR at all in this pass. Thus all
138134 // analysis are preserved.
139135 AU.setPreservesAll();
316312 bool PlaceBackedgeSafepointsImpl::runOnLoop(Loop *L, LPPassManager &LPM) {
317313 ScalarEvolution *SE = &getAnalysis();
318314
319 // Loop through all predecessors of the loop header and identify all
320 // backedges. We need to place a safepoint on every backedge (potentially).
321 // Note: Due to LoopSimplify there should only be one. Assert? Or can we
322 // relax this?
315 // Loop through all loop latches (branches controlling backedges). We need
316 // to place a safepoint on every backedge (potentially).
317 // Note: In common usage, there will be only one edge due to LoopSimplify
318 // having run sometime earlier in the pipeline, but this code must be correct
319 // w.r.t. loops with multiple backedges.
323320 BasicBlock *header = L->getHeader();
324321
325322 // TODO: Use the analysis pass infrastructure for this. There is no reason
327324 DominatorTree DT;
328325 DT.recalculate(*header->getParent());
329326
330 bool modified = false;
331 for (BasicBlock *pred : predecessors(header)) {
332 if (!L->contains(pred)) {
333 // This is not a backedge, it's coming from outside the loop
334 continue;
335 }
327 SmallVector LoopLatches;
328 L->getLoopLatches(LoopLatches);
329 for (BasicBlock *pred : LoopLatches) {
330 assert(L->contains(pred));
336331
337332 // Make a policy decision about whether this loop needs a safepoint or
338333 // not. Note that this is about unburdening the optimizer in loops, not
361356 // not help runtime performance that much, but it might help our ability to
362357 // optimize the inner loop.
363358
364 // We're unconditionally going to modify this loop.
365 modified = true;
366
367359 // Safepoint insertion would involve creating a new basic block (as the
368360 // target of the current backedge) which does the safepoint (of all live
369361 // variables) and branches to the true header
377369 PollLocations.push_back(term);
378370 }
379371
380 return modified;
372 return false;
381373 }
382374
383375 static Instruction *findLocationForEntrySafepoint(Function &F,
582574 PlaceBackedgeSafepointsImpl *PBS =
583575 new PlaceBackedgeSafepointsImpl(CanAssumeCallSafepoints);
584576 FPM.add(PBS);
585 // Note: While the analysis pass itself won't modify the IR, LoopSimplify
586 // (which it depends on) may. i.e. analysis must be recalculated after run
587577 FPM.run(F);
588578
589579 // We preserve dominance information when inserting the poll, otherwise
590580 // we'd have to recalculate this on every insert
591581 DT.recalculate(F);
592582
583 auto &PollLocations = PBS->PollLocations;
584
585 auto OrderByBBName = [](Instruction *a, Instruction *b) {
586 return a->getParent()->getName() < b->getParent()->getName();
587 };
588 // We need the order of list to be stable so that naming ends up stable
589 // when we split edges. This makes test cases much easier to write.
590 std::sort(PollLocations.begin(), PollLocations.end(), OrderByBBName);
591
592 // We can sometimes end up with duplicate poll locations. This happens if
593 // a single loop is visited more than once. The fact this happens seems
594 // wrong, but it does happen for the split-backedge.ll test case.
595 PollLocations.erase(std::unique(PollLocations.begin(),
596 PollLocations.end()),
597 PollLocations.end());
598
593599 // Insert a poll at each point the analysis pass identified
594 for (size_t i = 0; i < PBS->PollLocations.size(); i++) {
600 for (size_t i = 0; i < PollLocations.size(); i++) {
595601 // We are inserting a poll, the function is modified
596602 modified = true;
597603
598604 // The poll location must be the terminator of a loop latch block.
599 TerminatorInst *Term = PBS->PollLocations[i];
605 TerminatorInst *Term = PollLocations[i];
600606
601607 std::vector ParsePoints;
602608 if (SplitBackedge) {
609615 // Since this is a latch, at least one of the successors must dominate
610616 // it. Its possible that we have a) duplicate edges to the same header
611617 // and b) edges to distinct loop headers. We need to insert pools on
612 // each. (Note: This still relies on LoopSimplify.)
613 DenseSet Headers;
618 // each.
619 SetVector Headers;
614620 for (unsigned i = 0; i < Term->getNumSuccessors(); i++) {
615621 BasicBlock *Succ = Term->getSuccessor(i);
616622 if (DT.dominates(Succ, Term->getParent())) {
622628 // The split loop structure here is so that we only need to recalculate
623629 // the dominator tree once. Alternatively, we could just keep it up to
624630 // date and use a more natural merged loop.
625 DenseSet SplitBackedges;
631 SetVector SplitBackedges;
626632 for (BasicBlock *Header : Headers) {
627633 BasicBlock *NewBB = SplitEdge(Term->getParent(), Header, nullptr);
628634 SplitBackedges.insert(NewBB);
629635 }
630636 DT.recalculate(F);
631637 for (BasicBlock *NewBB : SplitBackedges) {
632 InsertSafepointPoll(DT, NewBB->getTerminator(), ParsePoints);
638 std::vector RuntimeCalls;
639 InsertSafepointPoll(DT, NewBB->getTerminator(), RuntimeCalls);
633640 NumBackedgeSafepoints++;
641 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
642 RuntimeCalls.end());
634643 }
635644
636645 } else {
637646 // Split the latch block itself, right before the terminator.
638 InsertSafepointPoll(DT, Term, ParsePoints);
647 std::vector RuntimeCalls;
648 InsertSafepointPoll(DT, Term, RuntimeCalls);
639649 NumBackedgeSafepoints++;
650 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
651 RuntimeCalls.end());
640652 }
641653
642654 // Record the parse points for later use
733745 "place-backedge-safepoints-impl",
734746 "Place Backedge Safepoints", false, false)
735747 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
736 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
737748 INITIALIZE_PASS_END(PlaceBackedgeSafepointsImpl,
738749 "place-backedge-safepoints-impl",
739750 "Place Backedge Safepoints", false, false)
2121 ; to be sure this keeps working.
2222 define void @test2(i32, i1 %cond) gc "statepoint-example" {
2323 ; CHECK-LABEL: @test2
24 ; CHECK-LABE: loop.loopexit.split
24 ; CHECK-LABEL: loop2.loop2_crit_edge:
25 ; CHECK: gc.statepoint
26 ; CHECK-NEXT: br label %loop2
27 ; CHECK-LABEL: loop2.loop_crit_edge:
2528 ; CHECK: gc.statepoint
2629 ; CHECK-NEXT: br label %loop
27 ; CHECK-LABEL: loop2.loop2_crit_edge
28 ; CHECK: gc.statepoint
29 ; CHECK-NEXT: br label %loop2
3030 entry:
3131 br label %loop
3232