llvm.org GIT mirror llvm / 34fd05d
Revert two SCEV changes that caused test failures in clang. r248606: "[SCEV] Exploit A < B => (A+K) < (B+K) when possible" r248608: "[SCEV] Teach isLoopBackedgeGuardedByCond to exploit trip counts." git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@248614 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 5 years ago
4 changed file(s) with 1 addition(s) and 357 deletion(s). Raw diff Collapse all Expand all
535535 const SCEV *FoundLHS,
536536 const SCEV *FoundRHS);
537537
538 /// Test whether the condition described by Pred, LHS, and RHS is true
539 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
540 /// true.
541 ///
542 /// This routine tries to rule out certain kinds of integer overflow, and
543 /// then tries to reason about arithmetic properties of the predicates.
544 bool isImpliedCondOperandsViaNoOverflow(ICmpInst::Predicate Pred,
545 const SCEV *LHS, const SCEV *RHS,
546 const SCEV *FoundLHS,
547 const SCEV *FoundRHS);
548
549538 /// If we know that the specified Phi is in the header of its containing
550539 /// loop, we know the loop executes a constant number of times, and the PHI
551540 /// node is just a recurrence involving constants, fold it.
69936993
69946994 SaveAndRestore ClearOnExit(WalkingBEDominatingConds, true);
69956995
6996 // See if we can exploit a trip count to prove the predicate.
6997 const auto &BETakenInfo = getBackedgeTakenInfo(L);
6998 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
6999 if (LatchBECount != getCouldNotCompute()) {
7000 // We know that Latch branches back to the loop header exactly
7001 // LatchBECount times. This means the backdege condition at Latch is
7002 // equivalent to "{0,+,1} u< LatchBECount".
7003 Type *Ty = LatchBECount->getType();
7004 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
7005 const SCEV *LoopCounter =
7006 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
7007 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
7008 LatchBECount))
7009 return true;
7010 }
7011
70126996 // Check conditions due to any @llvm.assume intrinsics.
70136997 for (auto &AssumeVH : AC.assumptions()) {
70146998 if (!AssumeVH)
73037287 return false;
73047288 }
73057289
7306 // Return true if More == (Less + C), where C is a constant.
7307 static bool IsConstDiff(ScalarEvolution &SE, const SCEV *Less, const SCEV *More,
7308 APInt &C) {
7309 // We avoid subtracting expressions here because this function is usually
7310 // fairly deep in the call stack (i.e. is called many times).
7311
7312 auto SplitBinaryAdd = [](const SCEV *Expr, const SCEV *&L, const SCEV *&R) {
7313 const auto *AE = dyn_cast(Expr);
7314 if (!AE || AE->getNumOperands() != 2)
7315 return false;
7316
7317 L = AE->getOperand(0);
7318 R = AE->getOperand(1);
7319 return true;
7320 };
7321
7322 if (isa(Less) && isa(More)) {
7323 const auto *LAR = cast(Less);
7324 const auto *MAR = cast(More);
7325
7326 if (LAR->getLoop() != MAR->getLoop())
7327 return false;
7328
7329 // We look at affine expressions only; not for correctness but to keep
7330 // getStepRecurrence cheap.
7331 if (!LAR->isAffine() || !MAR->isAffine())
7332 return false;
7333
7334 if (LAR->getStepRecurrence(SE) != MAR->getStepRecurrence(SE))
7335 return false;
7336
7337 Less = LAR->getStart();
7338 More = MAR->getStart();
7339
7340 // fall through
7341 }
7342
7343 if (isa(Less) && isa(More)) {
7344 const auto &M = cast(More)->getValue()->getValue();
7345 const auto &L = cast(Less)->getValue()->getValue();
7346 C = M - L;
7347 return true;
7348 }
7349
7350 const SCEV *L, *R;
7351 if (SplitBinaryAdd(Less, L, R))
7352 if (const auto *LC = dyn_cast(L))
7353 if (R == More) {
7354 C = -(LC->getValue()->getValue());
7355 return true;
7356 }
7357
7358 if (SplitBinaryAdd(More, L, R))
7359 if (const auto *LC = dyn_cast(L))
7360 if (R == Less) {
7361 C = LC->getValue()->getValue();
7362 return true;
7363 }
7364
7365 return false;
7366 }
7367
7368 bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(
7369 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
7370 const SCEV *FoundLHS, const SCEV *FoundRHS) {
7371 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
7372 return false;
7373
7374 const auto *AddRecLHS = dyn_cast(LHS);
7375 if (!AddRecLHS)
7376 return false;
7377
7378 const auto *AddRecFoundLHS = dyn_cast(FoundLHS);
7379 if (!AddRecFoundLHS)
7380 return false;
7381
7382 // We'd like to let SCEV reason about control dependencies, so we constrain
7383 // both the inequalities to be about add recurrences on the same loop. This
7384 // way we can use isLoopEntryGuardedByCond later.
7385
7386 const Loop *L = AddRecFoundLHS->getLoop();
7387 if (L != AddRecLHS->getLoop())
7388 return false;
7389
7390 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1)
7391 //
7392 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
7393 // ... (2)
7394 //
7395 // Informal proof for (2), assuming (1) [*]:
7396 //
7397 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
7398 //
7399 // Then
7400 //
7401 // FoundLHS s< FoundRHS s< INT_MIN - C
7402 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ]
7403 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
7404 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s<
7405 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
7406 // <=> FoundLHS + C s< FoundRHS + C
7407 //
7408 // [*]: (1) can be proved by ruling out overflow.
7409 //
7410 // [**]: This can be proved by analyzing all the four possibilities:
7411 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
7412 // (A s>= 0, B s>= 0).
7413 //
7414 // Note:
7415 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
7416 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS
7417 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS
7418 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is
7419 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
7420 // C)".
7421
7422 APInt LDiff, RDiff;
7423 if (!IsConstDiff(*this, FoundLHS, LHS, LDiff) ||
7424 !IsConstDiff(*this, FoundRHS, RHS, RDiff) ||
7425 LDiff != RDiff)
7426 return false;
7427
7428 if (LDiff == 0)
7429 return true;
7430
7431 unsigned Width = cast(RHS->getType())->getBitWidth();
7432 APInt FoundRHSLimit;
7433
7434 if (Pred == CmpInst::ICMP_ULT) {
7435 FoundRHSLimit = -RDiff;
7436 } else {
7437 assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
7438 FoundRHSLimit = APInt::getSignedMinValue(Width) - RDiff;
7439 }
7440
7441 // Try to prove (1) or (2), as needed.
7442 return isLoopEntryGuardedByCond(L, Pred, FoundRHS,
7443 getConstant(FoundRHSLimit));
7444 }
7445
74467290 /// isImpliedCondOperands - Test whether the condition described by Pred,
74477291 /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS,
74487292 /// and FoundRHS is true.
74517295 const SCEV *FoundLHS,
74527296 const SCEV *FoundRHS) {
74537297 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS))
7454 return true;
7455
7456 if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS))
74577298 return true;
74587299
74597300 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
99
1010 ; This cast shouldn't be folded into the addrec.
1111 ; CHECK: %tmp = zext i8 %l_95.0.i1 to i16
12 ; CHECK: --> (zext i8 {0,+,-1}<nw><%bb.i> to i16){{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: 2
12 ; CHECK: --> (zext i8 {0,+,-1}<%bb.i> to i16){{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: 2
1313
1414 %tmp = zext i8 %l_95.0.i1 to i16
1515
208208 unrolledend: ; preds = %forcond38
209209 ret i32 0
210210 }
211
212 declare void @side_effect()
213
214 define void @func_13(i32* %len.ptr) {
215 ; CHECK-LABEL: @func_13(
216 entry:
217 %len = load i32, i32* %len.ptr, !range !0
218 %len.sub.1 = add i32 %len, -1
219 %len.is.zero = icmp eq i32 %len, 0
220 br i1 %len.is.zero, label %leave, label %loop
221
222 loop:
223 ; CHECK: loop:
224 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
225 call void @side_effect()
226 %iv.inc = add i32 %iv, 1
227 %iv.cmp = icmp ult i32 %iv, %len
228 br i1 %iv.cmp, label %be, label %leave
229 ; CHECK: br i1 true, label %be, label %leave
230
231 be:
232 call void @side_effect()
233 %be.cond = icmp ult i32 %iv, %len.sub.1
234 br i1 %be.cond, label %loop, label %leave
235
236 leave:
237 ret void
238 }
239
240 define void @func_14(i32* %len.ptr) {
241 ; CHECK-LABEL: @func_14(
242 entry:
243 %len = load i32, i32* %len.ptr, !range !0
244 %len.sub.1 = add i32 %len, -1
245 %len.is.zero = icmp eq i32 %len, 0
246 %len.is.int_min = icmp eq i32 %len, 2147483648
247 %no.entry = or i1 %len.is.zero, %len.is.int_min
248 br i1 %no.entry, label %leave, label %loop
249
250 loop:
251 ; CHECK: loop:
252 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
253 call void @side_effect()
254 %iv.inc = add i32 %iv, 1
255 %iv.cmp = icmp slt i32 %iv, %len
256 br i1 %iv.cmp, label %be, label %leave
257 ; CHECK: br i1 true, label %be, label %leave
258
259 be:
260 call void @side_effect()
261 %be.cond = icmp slt i32 %iv, %len.sub.1
262 br i1 %be.cond, label %loop, label %leave
263
264 leave:
265 ret void
266 }
267
268 define void @func_15(i32* %len.ptr) {
269 ; CHECK-LABEL: @func_15(
270 entry:
271 %len = load i32, i32* %len.ptr, !range !0
272 %len.add.1 = add i32 %len, 1
273 %len.add.1.is.zero = icmp eq i32 %len.add.1, 0
274 br i1 %len.add.1.is.zero, label %leave, label %loop
275
276 loop:
277 ; CHECK: loop:
278 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
279 call void @side_effect()
280 %iv.inc = add i32 %iv, 1
281 %iv.cmp = icmp ult i32 %iv, %len.add.1
282 br i1 %iv.cmp, label %be, label %leave
283 ; CHECK: br i1 true, label %be, label %leave
284
285 be:
286 call void @side_effect()
287 %be.cond = icmp ult i32 %iv, %len
288 br i1 %be.cond, label %loop, label %leave
289
290 leave:
291 ret void
292 }
293
294 define void @func_16(i32* %len.ptr) {
295 ; CHECK-LABEL: @func_16(
296 entry:
297 %len = load i32, i32* %len.ptr, !range !0
298 %len.add.5 = add i32 %len, 5
299 %entry.cond.0 = icmp slt i32 %len, 2147483643
300 %entry.cond.1 = icmp slt i32 4, %len.add.5
301 %entry.cond = and i1 %entry.cond.0, %entry.cond.1
302 br i1 %entry.cond, label %loop, label %leave
303
304 loop:
305 ; CHECK: loop:
306 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %be ]
307 call void @side_effect()
308 %iv.inc = add i32 %iv, 1
309 %iv.add.4 = add i32 %iv, 4
310 %iv.cmp = icmp slt i32 %iv.add.4, %len.add.5
311 br i1 %iv.cmp, label %be, label %leave
312 ; CHECK: br i1 true, label %be, label %leave
313
314 be:
315 call void @side_effect()
316 %be.cond = icmp slt i32 %iv, %len
317 br i1 %be.cond, label %loop, label %leave
318
319 leave:
320 ret void
321 }
322
323 define void @func_17(i32* %len.ptr) {
324 ; CHECK-LABEL: @func_17(
325 entry:
326 %len = load i32, i32* %len.ptr
327 %len.add.5 = add i32 %len, -5
328 %entry.cond.0 = icmp slt i32 %len, 2147483653 ;; 2147483653 == INT_MIN - (-5)
329 %entry.cond.1 = icmp slt i32 -6, %len.add.5
330 %entry.cond = and i1 %entry.cond.0, %entry.cond.1
331 br i1 %entry.cond, label %loop, label %leave
332
333 loop:
334 ; CHECK: loop:
335 %iv.2 = phi i32 [ 0, %entry ], [ %iv.2.inc, %be ]
336 %iv = phi i32 [ -6, %entry ], [ %iv.inc, %be ]
337 call void @side_effect()
338 %iv.inc = add i32 %iv, 1
339 %iv.2.inc = add i32 %iv.2, 1
340 %iv.cmp = icmp slt i32 %iv, %len.add.5
341
342 ; Deduces {-5,+,1} s< (-5 + %len) from {0,+,1} < %len
343 ; since %len s< INT_MIN - (-5) from the entry condition
344
345 ; CHECK: br i1 true, label %be, label %leave
346 br i1 %iv.cmp, label %be, label %leave
347
348 be:
349 ; CHECK: be:
350 call void @side_effect()
351 %be.cond = icmp slt i32 %iv.2, %len
352 br i1 %be.cond, label %loop, label %leave
353
354 leave:
355 ret void
356 }
357
358 define i1 @func_18(i16* %tmp20, i32* %len.addr) {
359 ; CHECK-LABEL: @func_18(
360 entry:
361 %len = load i32, i32* %len.addr, !range !0
362 %tmp18 = icmp eq i32 %len, 0
363 br i1 %tmp18, label %bb2, label %bb0.preheader
364
365 bb0.preheader:
366 br label %bb0
367
368 bb0:
369 ; CHECK: bb0:
370 %var_0.in = phi i32 [ %var_0, %bb1 ], [ %len, %bb0.preheader ]
371 %var_1 = phi i32 [ %tmp30, %bb1 ], [ 0, %bb0.preheader ]
372 %var_0 = add nsw i32 %var_0.in, -1
373 %tmp23 = icmp ult i32 %var_1, %len
374 ; CHECK: br i1 true, label %stay, label %bb2.loopexit
375 br i1 %tmp23, label %stay, label %bb2
376
377 stay:
378 ; CHECK: stay:
379 %tmp25 = getelementptr inbounds i16, i16* %tmp20, i32 %var_1
380 %tmp26 = load i16, i16* %tmp25
381 %tmp29 = icmp eq i16 %tmp26, 0
382 br i1 %tmp29, label %bb1, label %bb2
383
384 bb1:
385 %tmp30 = add i32 %var_1, 1
386 %tmp31 = icmp eq i32 %var_0, 0
387 br i1 %tmp31, label %bb3, label %bb0
388
389 bb2:
390 ret i1 false
391
392 bb3:
393 ret i1 true
394 }
395
396 !0 = !{i32 0, i32 2147483647}