@@ -2516,38 +2516,46 @@ Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
2516
2516
// And select either 1. or 2. depending on whether step is positive or
2517
2517
// negative. If Step is known to be positive or negative, only create
2518
2518
// either 1. or 2.
2519
- Value *Add = nullptr , *Sub = nullptr ;
2520
- bool NeedPosCheck = !SE.isKnownNegative (Step);
2521
- bool NeedNegCheck = !SE.isKnownPositive (Step);
2522
-
2523
- if (PointerType *ARPtrTy = dyn_cast<PointerType>(ARTy)) {
2524
- StartValue = InsertNoopCastOfTo (
2525
- StartValue, Builder.getInt8PtrTy (ARPtrTy->getAddressSpace ()));
2526
- Value *NegMulV = Builder.CreateNeg (MulV);
2527
- if (NeedPosCheck)
2528
- Add = Builder.CreateGEP (Builder.getInt8Ty (), StartValue, MulV);
2529
- if (NeedNegCheck)
2530
- Sub = Builder.CreateGEP (Builder.getInt8Ty (), StartValue, NegMulV);
2531
- } else {
2519
+ auto ComputeEndCheck = [&]() -> Value * {
2520
+ // Checking <u 0 is always false.
2521
+ if (!Signed && Start->isZero () && SE.isKnownPositive (Step))
2522
+ return ConstantInt::getFalse (Loc->getContext ());
2523
+
2524
+ Value *Add = nullptr , *Sub = nullptr ;
2525
+ bool NeedPosCheck = !SE.isKnownNegative (Step);
2526
+ bool NeedNegCheck = !SE.isKnownPositive (Step);
2527
+
2528
+ if (PointerType *ARPtrTy = dyn_cast<PointerType>(ARTy)) {
2529
+ StartValue = InsertNoopCastOfTo (
2530
+ StartValue, Builder.getInt8PtrTy (ARPtrTy->getAddressSpace ()));
2531
+ Value *NegMulV = Builder.CreateNeg (MulV);
2532
+ if (NeedPosCheck)
2533
+ Add = Builder.CreateGEP (Builder.getInt8Ty (), StartValue, MulV);
2534
+ if (NeedNegCheck)
2535
+ Sub = Builder.CreateGEP (Builder.getInt8Ty (), StartValue, NegMulV);
2536
+ } else {
2537
+ if (NeedPosCheck)
2538
+ Add = Builder.CreateAdd (StartValue, MulV);
2539
+ if (NeedNegCheck)
2540
+ Sub = Builder.CreateSub (StartValue, MulV);
2541
+ }
2542
+
2543
+ Value *EndCompareLT = nullptr ;
2544
+ Value *EndCompareGT = nullptr ;
2545
+ Value *EndCheck = nullptr ;
2532
2546
if (NeedPosCheck)
2533
- Add = Builder.CreateAdd (StartValue, MulV);
2547
+ EndCheck = EndCompareLT = Builder.CreateICmp (
2548
+ Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, Add, StartValue);
2534
2549
if (NeedNegCheck)
2535
- Sub = Builder.CreateSub (StartValue, MulV);
2536
- }
2537
-
2538
- Value *EndCompareLT = nullptr ;
2539
- Value *EndCompareGT = nullptr ;
2540
- Value *EndCheck = nullptr ;
2541
- if (NeedPosCheck)
2542
- EndCheck = EndCompareLT = Builder.CreateICmp (
2543
- Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, Add, StartValue);
2544
- if (NeedNegCheck)
2545
- EndCheck = EndCompareGT = Builder.CreateICmp (
2546
- Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, Sub, StartValue);
2547
- if (NeedPosCheck && NeedNegCheck) {
2548
- // Select the answer based on the sign of Step.
2549
- EndCheck = Builder.CreateSelect (StepCompare, EndCompareGT, EndCompareLT);
2550
- }
2550
+ EndCheck = EndCompareGT = Builder.CreateICmp (
2551
+ Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, Sub, StartValue);
2552
+ if (NeedPosCheck && NeedNegCheck) {
2553
+ // Select the answer based on the sign of Step.
2554
+ EndCheck = Builder.CreateSelect (StepCompare, EndCompareGT, EndCompareLT);
2555
+ }
2556
+ return EndCheck;
2557
+ };
2558
+ Value *EndCheck = ComputeEndCheck ();
2551
2559
2552
2560
// If the backedge taken count type is larger than the AR type,
2553
2561
// check that we don't drop any bits by truncating it. If we are
0 commit comments