llvm.org GIT mirror llvm / 8566963
Make LoopSimplify change conditional branches in loop exiting blocks which branch on undef to branch on a boolean constant for the edge exiting the loop. This helps ScalarEvolution compute trip counts for loops. Teach ScalarEvolution to recognize single-value PHIs, when safe, and ForgetSymbolicName to forget such single-value PHI nodes as apprpriate in ForgetSymbolicName. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97126 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
6 changed file(s) with 128 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
25482548 /// the Scalars map if they reference SymName. This is used during PHI
25492549 /// resolution.
25502550 void
2551 ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) {
2551 ScalarEvolution::ForgetSymbolicName(Instruction *PN, const SCEV *SymName) {
25522552 SmallVector Worklist;
2553 PushDefUseChildren(I, Worklist);
2553 PushDefUseChildren(PN, Worklist);
25542554
25552555 SmallPtrSet Visited;
2556 Visited.insert(I);
2556 Visited.insert(PN);
25572557 while (!Worklist.empty()) {
2558 I = Worklist.pop_back_val();
2558 Instruction *I = Worklist.pop_back_val();
25592559 if (!Visited.insert(I)) continue;
25602560
25612561 std::map::iterator It =
25672567 continue;
25682568
25692569 // SCEVUnknown for a PHI either means that it has an unrecognized
2570 // structure, or it's a PHI that's in the progress of being computed
2571 // by createNodeForPHI. In the former case, additional loop trip
2572 // count information isn't going to change anything. In the later
2573 // case, createNodeForPHI will perform the necessary updates on its
2574 // own when it gets to that point.
2575 if (!isa(I) || !isa(It->second)) {
2570 // structure, it's a PHI that's in the progress of being computed
2571 // by createNodeForPHI, or it's a single-value PHI. In the first case,
2572 // additional loop trip count information isn't going to change anything.
2573 // In the second case, createNodeForPHI will perform the necessary
2574 // updates on its own when it gets to that point. In the third, we do
2575 // want to forget the SCEVUnknown.
2576 if (!isa(I) ||
2577 !isa(It->second) ||
2578 (I != PN && It->second == SymName)) {
25762579 ValuesAtScopes.erase(It->second);
25772580 Scalars.erase(It);
25782581 }
26952698 return SymbolicName;
26962699 }
26972700
2698 // It's tempting to recognize PHIs with a unique incoming value, however
2699 // this leads passes like indvars to break LCSSA form. Fortunately, such
2700 // PHIs are rare, as instcombine zaps them.
2701 // If the PHI has a single incoming value, follow that value, unless the
2702 // PHI's incoming blocks are in a different loop, in which case doing so
2703 // risks breaking LCSSA form. Instcombine would normally zap these, but
2704 // it doesn't have DominatorTree information, so it may miss cases.
2705 if (Value *V = PN->hasConstantValue(DT)) {
2706 bool AllSameLoop = true;
2707 Loop *PNLoop = LI->getLoopFor(PN->getParent());
2708 for (size_t i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
2709 if (LI->getLoopFor(PN->getIncomingBlock(i)) != PNLoop) {
2710 AllSameLoop = false;
2711 break;
2712 }
2713 if (AllSameLoop)
2714 return getSCEV(V);
2715 }
27012716
27022717 // If it's not a loop phi, we can't handle it yet.
27032718 return getUnknown(PN);
383383 // in this loop, insert a canonical induction variable of the largest size.
384384 Value *IndVar = 0;
385385 if (NeedCannIV) {
386 // Check to see if the loop already has a canonical-looking induction
387 // variable. If one is present and it's wider than the planned canonical
388 // induction variable, temporarily remove it, so that the Rewriter
389 // doesn't attempt to reuse it.
390 PHINode *OldCannIV = L->getCanonicalInductionVariable();
391 if (OldCannIV) {
386 // Check to see if the loop already has any canonical-looking induction
387 // variables. If any are present and wider than the planned canonical
388 // induction variable, temporarily remove them, so that the Rewriter
389 // doesn't attempt to reuse them.
390 SmallVector OldCannIVs;
391 while (PHINode *OldCannIV = L->getCanonicalInductionVariable()) {
392392 if (SE->getTypeSizeInBits(OldCannIV->getType()) >
393393 SE->getTypeSizeInBits(LargestType))
394394 OldCannIV->removeFromParent();
395395 else
396 OldCannIV = 0;
396 break;
397 OldCannIVs.push_back(OldCannIV);
397398 }
398399
399400 IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType);
403404 DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n');
404405
405406 // Now that the official induction variable is established, reinsert
406 // the old canonical-looking variable after it so that the IR remains
407 // consistent. It will be deleted as part of the dead-PHI deletion at
407 // any old canonical-looking variables after it so that the IR remains
408 // consistent. They will be deleted as part of the dead-PHI deletion at
408409 // the end of the pass.
409 if (OldCannIV)
410 OldCannIV->insertAfter(cast(IndVar));
410 while (!OldCannIVs.empty()) {
411 PHINode *OldCannIV = OldCannIVs.pop_back_val();
412 OldCannIV->insertBefore(L->getHeader()->getFirstNonPHI());
413 }
411414 }
412415
413416 // If we have a trip count expression, rewrite the loop's exit condition
414417 // using it. We can currently only handle loops with a single exit.
415418 ICmpInst *NewICmp = 0;
416 if (!isa(BackedgeTakenCount) && ExitingBlock) {
419 if (!isa(BackedgeTakenCount) &&
420 !BackedgeTakenCount->isZero() &&
421 ExitingBlock) {
417422 assert(NeedCannIV &&
418423 "LinearFunctionTestReplace requires a canonical induction variable");
419424 // Can't rewrite non-branch yet.
157157 Changed = true;
158158 }
159159 }
160
161 // If there are exiting blocks with branches on undef, resolve the undef in
162 // the direction which will exit the loop. This will help simplify loop
163 // trip count computations.
164 SmallVector ExitingBlocks;
165 L->getExitingBlocks(ExitingBlocks);
166 for (SmallVectorImpl::iterator I = ExitingBlocks.begin(),
167 E = ExitingBlocks.end(); I != E; ++I)
168 if (BranchInst *BI = dyn_cast((*I)->getTerminator()))
169 if (BI->isConditional()) {
170 if (UndefValue *Cond = dyn_cast(BI->getCondition())) {
171 BI->setCondition(ConstantInt::get(Cond->getType(),
172 !L->contains(BI->getSuccessor(0))));
173 Changed = true;
174 }
175 }
160176
161177 // Does the loop already have a preheader? If so, don't insert one.
162178 BasicBlock *Preheader = L->getLoopPreheader();
249265 break;
250266 }
251267 if (UniqueExit) {
252 SmallVector ExitingBlocks;
253 L->getExitingBlocks(ExitingBlocks);
254268 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
255269 BasicBlock *ExitingBlock = ExitingBlocks[i];
256270 if (!ExitingBlock->getSinglePredecessor()) continue;
77 define i64 @hammer_time(i64 %modulep, i64 %physfree) nounwind ssp noredzone noimplicitfloat {
88 ; CHECK: hammer_time:
99 ; CHECK: movq $Xrsvd, %rax
10 ; CHECK: movq $Xrsvd, %rsi
11 ; CHECK: movq $Xrsvd, %rdi
10 ; CHECK: movq $Xrsvd, %rcx
1211 entry:
1312 br i1 undef, label %if.then, label %if.end
1413
66 LoopHead: ; preds = %LoopHead, %0, %0
77 %A = phi i32 [ 7, %0 ], [ 7, %0 ], [ %B, %LoopHead ] ; [#uses=1]
88 %B = add i32 %A, 1 ; [#uses=2]
9 br i1 undef, label %LoopHead, label %Out
9 br i1 true, label %LoopHead, label %Out
1010
1111 Out: ; preds = %LoopHead
1212 ret i32 %B
0 ; RUN: opt < %s -S -indvars -loop-deletion -simplifycfg | FileCheck %s
1 ; PR5794
2
3 ; Indvars and loop deletion should be able to eliminate all looping
4 ; in this testcase.
5
6 ; CHECK: define i32 @pmat(i32 %m, i32 %n, double* %y) nounwind {
7 ; CHECK-NEXT: entry:
8 ; CHECK-NEXT: ret i32 0
9 ; CHECK-NEXT: }
10
11 target datalayout = "e-p:64:64:64"
12
13 define i32 @pmat(i32 %m, i32 %n, double* %y) nounwind {
14 entry:
15 %cmp4 = icmp sgt i32 %m, 0
16 br i1 %cmp4, label %bb.n10, label %w.e12
17
18 w.c:
19 %cmp = icmp slt i32 %inc11, %m
20 br i1 %cmp, label %w.c2.p, label %w.c.w.e12c
21
22 w.c.w.e12c:
23 br label %w.c.w.e12c.s
24
25 w.c.w.e12c.s:
26 br label %w.e12
27
28 bb.n10:
29 %cmp51 = icmp sgt i32 %n, 0
30 br i1 %cmp51, label %bb.n10.w.c.w.e12c.sc, label %bb.n10.bb.n10.sc
31
32 bb.n10.bb.n10.sc:
33 br label %bb.n10.s
34
35 bb.n10.w.c.w.e12c.sc:
36 br label %w.c.w.e12c.s
37
38 bb.n10.s:
39 br label %w.c2.p
40
41 w.c2.p:
42 %i.05 = phi i32 [ 0, %bb.n10.s ], [ %inc11, %w.c ]
43 br i1 false, label %bb.n, label %w.e
44
45 w.c2:
46 br i1 undef, label %w.b6, label %w.c2.w.ec
47
48 w.c2.w.ec:
49 br label %w.e
50
51 bb.n:
52 br label %w.b6
53
54 w.b6:
55 br label %w.c2
56
57 w.e:
58 %i.08 = phi i32 [ undef, %w.c2.w.ec ], [ %i.05, %w.c2.p ]
59 %inc11 = add nsw i32 %i.08, 1
60 br label %w.c
61
62 w.e12:
63 ret i32 0
64 }