llvm.org GIT mirror llvm / d5a74a7
Fix PR18449: SCEV needs more precise max BECount for multi-exit loop. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199299 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Trick 6 years ago
3 changed file(s) with 82 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
252252 /// Mark predicate values currently being processed by isImpliedCond.
253253 DenseSet PendingLoopPredicates;
254254
255 /// ExitLimit - Information about the number of loop iterations for
256 /// which a loop exit's branch condition evaluates to the not-taken path.
257 /// This is a temporary pair of exact and max expressions that are
258 /// eventually summarized in ExitNotTakenInfo and BackedgeTakenInfo.
255 /// ExitLimit - Information about the number of loop iterations for which a
256 /// loop exit's branch condition evaluates to the not-taken path. This is a
257 /// temporary pair of exact and max expressions that are eventually
258 /// summarized in ExitNotTakenInfo and BackedgeTakenInfo.
259 ///
260 /// If MustExit is true, then the exit must be taken when the BECount
261 /// reaches Exact (and before surpassing Max). If MustExit is false, then
262 /// BECount may exceed Exact or Max if the loop exits via another branch. In
263 /// either case, the loop may exit early via another branch.
264 ///
265 /// MustExit is true for most cases. However, an exit guarded by an
266 /// (in)equality on a nonunit stride may be skipped.
259267 struct ExitLimit {
260268 const SCEV *Exact;
261269 const SCEV *Max;
262
263 /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {}
264
265 ExitLimit(const SCEV *E, const SCEV *M) : Exact(E), Max(M) {}
270 bool MustExit;
271
272 /*implicit*/ ExitLimit(const SCEV *E)
273 : Exact(E), Max(E), MustExit(true) {}
274
275 ExitLimit(const SCEV *E, const SCEV *M, bool MustExit)
276 : Exact(E), Max(M), MustExit(MustExit) {}
266277
267278 /// hasAnyInfo - Test whether this ExitLimit contains any computed
268279 /// information, or whether it's all SCEVCouldNotCompute values.
43364336 // Examine all exits and pick the most conservative values.
43374337 const SCEV *MaxBECount = getCouldNotCompute();
43384338 bool CouldComputeBECount = true;
4339 BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
4340 const SCEV *LatchMaxCount = 0;
43394341 SmallVector, 4> ExitCounts;
43404342 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
43414343 ExitLimit EL = ComputeExitLimit(L, ExitingBlocks[i]);
43524354 // We cannot take the "min" MaxBECount, because non-unit stride loops may
43534355 // skip some loop tests. Taking the max over the exits is sufficiently
43544356 // conservative. TODO: We could do better taking into consideration
4355 // that (1) the loop has unit stride (2) the last loop test is
4356 // less-than/greater-than (3) any loop test is less-than/greater-than AND
4357 // falls-through some constant times less then the other tests.
4358 MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, EL.Max);
4359 }
4360 }
4361
4357 // non-latch exits that dominate the latch.
4358 if (EL.MustExit && ExitingBlocks[i] == Latch)
4359 LatchMaxCount = EL.Max;
4360 else
4361 MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, EL.Max);
4362 }
4363 }
4364 // Be more precise in the easy case of a loop latch that must exit.
4365 if (LatchMaxCount) {
4366 MaxBECount = getUMinFromMismatchedTypes(MaxBECount, LatchMaxCount);
4367 }
43624368 return BackedgeTakenInfo(ExitCounts, CouldComputeBECount, MaxBECount);
43634369 }
43644370
44544460 IsSubExpr || EitherMayExit);
44554461 const SCEV *BECount = getCouldNotCompute();
44564462 const SCEV *MaxBECount = getCouldNotCompute();
4463 bool MustExit = false;
44574464 if (EitherMayExit) {
44584465 // Both conditions must be true for the loop to continue executing.
44594466 // Choose the less conservative count.
44684475 MaxBECount = EL0.Max;
44694476 else
44704477 MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max);
4478 MustExit = EL0.MustExit || EL1.MustExit;
44714479 } else {
44724480 // Both conditions must be true at the same time for the loop to exit.
44734481 // For now, be conservative.
44764484 MaxBECount = EL0.Max;
44774485 if (EL0.Exact == EL1.Exact)
44784486 BECount = EL0.Exact;
4487 MustExit = EL0.MustExit && EL1.MustExit;
44794488 }
44804489
4481 return ExitLimit(BECount, MaxBECount);
4490 return ExitLimit(BECount, MaxBECount, MustExit);
44824491 }
44834492 if (BO->getOpcode() == Instruction::Or) {
44844493 // Recurse on the operands of the or.
44894498 IsSubExpr || EitherMayExit);
44904499 const SCEV *BECount = getCouldNotCompute();
44914500 const SCEV *MaxBECount = getCouldNotCompute();
4501 bool MustExit = false;
44924502 if (EitherMayExit) {
44934503 // Both conditions must be false for the loop to continue executing.
44944504 // Choose the less conservative count.
45034513 MaxBECount = EL0.Max;
45044514 else
45054515 MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max);
4516 MustExit = EL0.MustExit || EL1.MustExit;
45064517 } else {
45074518 // Both conditions must be false at the same time for the loop to exit.
45084519 // For now, be conservative.
45114522 MaxBECount = EL0.Max;
45124523 if (EL0.Exact == EL1.Exact)
45134524 BECount = EL0.Exact;
4525 MustExit = EL0.MustExit && EL1.MustExit;
45144526 }
45154527
4516 return ExitLimit(BECount, MaxBECount);
4528 return ExitLimit(BECount, MaxBECount, MustExit);
45174529 }
45184530 }
45194531
55935605 else
55945606 MaxBECount = getConstant(CountDown ? CR.getUnsignedMax()
55955607 : -CR.getUnsignedMin());
5596 return ExitLimit(Distance, MaxBECount);
5608 return ExitLimit(Distance, MaxBECount, /*MustExit=*/true);
55975609 }
55985610
55995611 // If the recurrence is known not to wraparound, unsigned divide computes the
56015613 // that the value will either become zero (and thus the loop terminates), that
56025614 // the loop will terminate through some other exit condition first, or that
56035615 // the loop has undefined behavior. This means we can't "miss" the exit
5604 // value, even with nonunit stride.
5616 // value, even with nonunit stride, and exit later via the same branch. Note
5617 // that we can skip this exit if loop later exits via a different
5618 // branch. Hence MustExit=false.
56055619 //
56065620 // This is only valid for expressions that directly compute the loop exit. It
56075621 // is invalid for subexpressions in which the loop may exit through this
56085622 // branch even if this subexpression is false. In that case, the trip count
56095623 // computed by this udiv could be smaller than the number of well-defined
56105624 // iterations.
5611 if (!IsSubExpr && AddRec->getNoWrapFlags(SCEV::FlagNW))
5612 return getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
5613
5625 if (!IsSubExpr && AddRec->getNoWrapFlags(SCEV::FlagNW)) {
5626 const SCEV *Exact =
5627 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
5628 return ExitLimit(Exact, Exact, /*MustExit=*/false);
5629 }
56145630 // Then, try to solve the above equation provided that Start is constant.
56155631 if (const SCEVConstant *StartC = dyn_cast(Start))
56165632 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
64746490 if (isa(MaxBECount))
64756491 MaxBECount = BECount;
64766492
6477 return ExitLimit(BECount, MaxBECount);
6493 return ExitLimit(BECount, MaxBECount, /*MustExit=*/true);
64786494 }
64796495
64806496 ScalarEvolution::ExitLimit
65466562 if (isa(MaxBECount))
65476563 MaxBECount = BECount;
65486564
6549 return ExitLimit(BECount, MaxBECount);
6565 return ExitLimit(BECount, MaxBECount, /*MustExit=*/true);
65506566 }
65516567
65526568 /// getNumIterationsInRange - Return the number of iterations of this loop that
159159 loopexit9: ; preds = %loop2
160160 ret i32 %l.next
161161 }
162
163 ; PR18449. Check that the early exit is reduced to never taken.
164 ;
165 ; CHECK-LABEL: @twoexit
166 ; CHECK-LABEL: loop:
167 ; CHECK: phi
168 ; CHECK: br i1 false
169 ; CHECK: br
170 ; CHECK: ret
171 define void @twoexit() {
172 "function top level":
173 br label %loop
174
175 loop: ; preds = %body, %"function top level"
176 %0 = phi i64 [ 0, %"function top level" ], [ %2, %body ]
177 %1 = icmp ugt i64 %0, 2
178 br i1 %1, label %fail, label %body
179
180 fail: ; preds = %loop
181 tail call void @bounds_fail()
182 unreachable
183
184 body: ; preds = %loop
185 %2 = add i64 %0, 1
186 %3 = icmp slt i64 %2, 3
187 br i1 %3, label %loop, label %out
188
189 out: ; preds = %body
190 ret void
191 }
192 declare void @bounds_fail()