Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -5845,7 +5845,10 @@ // See if ValueTracking can give us a useful range. const DataLayout &DL = getDataLayout(); - KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT); + const Instruction *CxtI = dyn_cast(U->getValue()); + if (!CxtI) + CxtI = &*(F.getEntryBlock().begin()); + KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, CxtI, &DT); if (Known.getBitWidth() != BitWidth) Known = Known.zextOrTrunc(BitWidth); // If Known does not result in full-set, intersect with it. @@ -5856,7 +5859,7 @@ // ValueTracking may be able to compute a tighter result for the number of // sign bits than for the value of those sign bits. - unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT); + unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, CxtI, &DT); // If the pointer size is larger than the index size type, this can cause // NS to be larger than BitWidth. So compensate for this. if (U->getType()->isPointerTy()) { Index: llvm/test/Analysis/ScalarEvolution/ranges.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/ranges.ll +++ llvm/test/Analysis/ScalarEvolution/ranges.ll @@ -15,9 +15,9 @@ ; CHECK-NEXT: --> %ashr U: [0,1) S: [0,1) ; CHECK-NEXT: Determining loop execution counts for: @ashr ; + %ashr = ashr i32 %a, 31 %pos = icmp sge i32 %a, 0 call void @llvm.assume(i1 %pos) - %ashr = ashr i32 %a, 31 ret i32 %ashr } @@ -28,9 +28,9 @@ ; CHECK-NEXT: --> (4 * %a) U: [0,-3) S: [-2147483648,2147483645) ; CHECK-NEXT: Determining loop execution counts for: @shl ; + %res = shl i32 %a, 2 %pos = icmp slt i32 %a, 1024 call void @llvm.assume(i1 %pos) - %res = shl i32 %a, 2 ret i32 %res } @@ -38,12 +38,12 @@ ; CHECK-LABEL: 'lshr' ; CHECK-NEXT: Classifying expressions for: @lshr ; CHECK-NEXT: %res = lshr i32 %a, 31 -; CHECK-NEXT: --> (%a /u -2147483648) U: [0,2) S: [0,2) +; CHECK-NEXT: --> (%a /u -2147483648) U: [0,1) S: [0,1) ; CHECK-NEXT: Determining loop execution counts for: @lshr ; + %res = lshr i32 %a, 31 %pos = icmp sge i32 %a, 0 call void @llvm.assume(i1 %pos) - %res = lshr i32 %a, 31 ret i32 %res } @@ -52,37 +52,37 @@ ; CHECK-LABEL: 'udiv' ; CHECK-NEXT: Classifying expressions for: @udiv ; CHECK-NEXT: %res = udiv i32 %a, -2147483648 -; CHECK-NEXT: --> (%a /u -2147483648) U: [0,2) S: [0,2) +; CHECK-NEXT: --> (%a /u -2147483648) U: [0,1) S: [0,1) ; CHECK-NEXT: Determining loop execution counts for: @udiv ; + %res = udiv i32 %a, 2147483648 %pos = icmp sge i32 %a, 0 call void @llvm.assume(i1 %pos) - %res = udiv i32 %a, 2147483648 ret i32 %res } -define i64 @sext(i32 %a) { +define i64 @sext(i8 %a) { ; CHECK-LABEL: 'sext' ; CHECK-NEXT: Classifying expressions for: @sext -; CHECK-NEXT: %res = sext i32 %a to i64 -; CHECK-NEXT: --> (sext i32 %a to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) +; CHECK-NEXT: %res = sext i8 %a to i64 +; CHECK-NEXT: --> (zext i8 %a to i64) U: [0,128) S: [0,128) ; CHECK-NEXT: Determining loop execution counts for: @sext ; - %pos = icmp sge i32 %a, 0 + %res = sext i8 %a to i64 + %pos = icmp sge i8 %a, 0 call void @llvm.assume(i1 %pos) - %res = sext i32 %a to i64 ret i64 %res } -define i64 @zext(i32 %a) { +define i64 @zext(i8 %a) { ; CHECK-LABEL: 'zext' ; CHECK-NEXT: Classifying expressions for: @zext -; CHECK-NEXT: %res = zext i32 %a to i64 -; CHECK-NEXT: --> (zext i32 %a to i64) U: [0,4294967296) S: [0,4294967296) +; CHECK-NEXT: %res = zext i8 %a to i64 +; CHECK-NEXT: --> (zext i8 %a to i64) U: [0,128) S: [0,128) ; CHECK-NEXT: Determining loop execution counts for: @zext ; - %pos = icmp sge i32 %a, 0 + %res = zext i8 %a to i64 + %pos = icmp sge i8 %a, 0 call void @llvm.assume(i1 %pos) - %res = zext i32 %a to i64 ret i64 %res }