Index: llvm/lib/IR/ConstantRange.cpp =================================================================== --- llvm/lib/IR/ConstantRange.cpp +++ llvm/lib/IR/ConstantRange.cpp @@ -1478,6 +1478,13 @@ APInt OtherMax = Other.getUnsignedMax(); + bool Neg = false; + if (getSingleElement() && isAllNegative()) { + Neg = true; + Min = Min.abs(); + Max = Max.abs(); + } + // There's overflow! if (OtherMax.ugt(Max.countl_zero())) return getFull(); @@ -1487,6 +1494,11 @@ Min <<= Other.getUnsignedMin(); Max <<= OtherMax; + if (Neg) { + Min = -Min; + Max = -Max; + return ConstantRange::getNonEmpty(std::move(Max), std::move(Min) + 1); + } return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1); } Index: llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll =================================================================== --- llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll +++ llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll @@ -30,8 +30,7 @@ ; CHECK-NEXT: call void @llvm.assume(i1 [[OP1_P2]]) ; CHECK-NEXT: [[SHIFT:%.*]] = shl nsw i8 -1, [[X]] ; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[SHIFT]], -1 -; CHECK-NEXT: [[R:%.*]] = and i8 [[NOT]], 32 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 0 ; %op1_p2 = icmp ule i8 %x, 5 call void @llvm.assume(i1 %op1_p2) @@ -48,8 +47,7 @@ ; CHECK-NEXT: call void @llvm.assume(i1 [[OP1_P2]]) ; CHECK-NEXT: [[SHIFT:%.*]] = shl nsw i8 -1, [[X]] ; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[SHIFT]], -1 -; CHECK-NEXT: [[R:%.*]] = and i8 [[NOT]], 48 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 0 ; %op1_p2 = icmp ule i8 %x, 4 call void @llvm.assume(i1 %op1_p2)