[InstCombine] Fold (1 << a) to (a + 1) and (2 >> a) to (2 - a) when a is in [0,1]
Fixes #207089
Adds two InstCombine folds for shifts where the shift amount is known to be 0 or 1 (e.g. constrained by range metadata, zext i1, or llvm.assume). This enables further simplification/reassociation once the shift is turned into simple arithmetic.
1 << X→X + 1(nuw nsw)2 >> X→2 - X(nuw nsw)
@llvm/pr-subscribers-llvm-transforms
Author: Aayush Shrivastava (iamaayushrivastava)
Changes
Fixes #207089
Adds two InstCombine folds for shifts where the shift amount is known to be 0 or 1 (e.g. constrained by range metadata, zext i1, or llvm.assume). This enables further simplification/reassociation once the shift is turned into simple arithmetic.
1 << X→X + 1(nuw nsw)2 >> X→2 - X(nuw nsw)
Full diff: https://github.com/llvm/llvm-project/pull/207108.diff
3 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp (+17)
- (modified) llvm/test/Transforms/InstCombine/lshr.ll (+40)
- (modified) llvm/test/Transforms/InstCombine/shift.ll (+40)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 605d80f2be7de..0ca626b694e96 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1380,6 +1380,14 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
Value *NegX = Builder.CreateNeg(X, "neg");
return BinaryOperator::CreateAnd(NegX, X);
}
+
+ // 1 << X --> X + 1 if 0 <= X <= 1.
+ if (computeKnownBits(Op1, &I).countMaxActiveBits() <= 1) {
+ auto *Add = BinaryOperator::CreateAdd(Op1, ConstantInt::get(Ty, 1));
+ Add->setHasNoUnsignedWrap();
+ Add->setHasNoSignedWrap();
+ return Add;
+ }
}
return nullptr;
@@ -1406,6 +1414,15 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
if (match(Op0, m_One()))
return new ZExtInst(Builder.CreateIsNull(Op1), Ty);
+ // 2 >> X --> 2 - X if 0 <= X <= 1.
+ if (match(Op0, m_SpecificInt(2)) &&
+ computeKnownBits(Op1, &I).countMaxActiveBits() <= 1) {
+ auto *Sub = BinaryOperator::CreateSub(ConstantInt::get(Ty, 2), Op1);
+ Sub->setHasNoUnsignedWrap();
+ Sub->setHasNoSignedWrap();
+ return Sub;
+ }
+
// (iN (~X) u>> (N - 1)) --> zext (X > -1)
if (match(Op0, m_OneUse(m_Not(m_Value(X)))) &&
match(Op1, m_SpecificIntAllowPoison(BitWidth - 1)))
diff --git a/llvm/test/Transforms/InstCombine/lshr.ll b/llvm/test/Transforms/InstCombine/lshr.ll
index be78220ee47ea..5b50c66a4bc1d 100644
--- a/llvm/test/Transforms/InstCombine/lshr.ll
+++ b/llvm/test/Transforms/InstCombine/lshr.ll
@@ -1627,3 +1627,43 @@ define i32 @lshr_two_i32(i32 %x) {
%shr = lshr i32 2, %x
ret i32 %shr
}
+
+; 2 >> X --> 2 - X if 0 <= X <= 1.
+
+define i32 @lshr_two_range_zero_one(i32 range(i32 0, 2) %x) {
+; CHECK-LABEL: @lshr_two_range_zero_one(
+; CHECK-NEXT: [[SHR:%.*]] = sub nuw nsw i32 2, [[X:%.*]]
+; CHECK-NEXT: ret i32 [[SHR]]
+;
+ %shr = lshr i32 2, %x
+ ret i32 %shr
+}
+
+define i32 @lshr_two_zext_i1(i1 %b) {
+; CHECK-LABEL: @lshr_two_zext_i1(
+; CHECK-NEXT: [[SHR:%.*]] = select i1 [[B:%.*]], i32 1, i32 2
+; CHECK-NEXT: ret i32 [[SHR]]
+;
+ %x = zext i1 %b to i32
+ %shr = lshr i32 2, %x
+ ret i32 %shr
+}
+
+define <4 x i32> @lshr_two_range_zero_one_vec(<4 x i32> range(i32 0, 2) %x) {
+; CHECK-LABEL: @lshr_two_range_zero_one_vec(
+; CHECK-NEXT: [[SHR:%.*]] = sub nuw nsw <4 x i32> splat (i32 2), [[X:%.*]]
+; CHECK-NEXT: ret <4 x i32> [[SHR]]
+;
+ %shr = lshr <4 x i32> splat (i32 2), %x
+ ret <4 x i32> %shr
+}
+
+; Negative test - x is not known to be 0 or 1.
+define i32 @lshr_two_range_zero_two_fail(i32 range(i32 0, 3) %x) {
+; CHECK-LABEL: @lshr_two_range_zero_two_fail(
+; CHECK-NEXT: [[SHR:%.*]] = lshr i32 2, [[X:%.*]]
+; CHECK-NEXT: ret i32 [[SHR]]
+;
+ %shr = lshr i32 2, %x
+ ret i32 %shr
+}
diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll
index d63ba48830068..3b802edadc3a5 100644
--- a/llvm/test/Transforms/InstCombine/shift.ll
+++ b/llvm/test/Transforms/InstCombine/shift.ll
@@ -2321,4 +2321,44 @@ define i8 @src_ashr_exact_fail(i8 %x) {
ret i8 %r
}
+; 1 << X --> X + 1 if 0 <= X <= 1.
+
+define i32 @shl1_range_zero_one(i32 range(i32 0, 2) %x) {
+; CHECK-LABEL: @shl1_range_zero_one(
+; CHECK-NEXT: [[SHL:%.*]] = add nuw nsw i32 [[X:%.*]], 1
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %shl = shl i32 1, %x
+ ret i32 %shl
+}
+
+define i32 @shl1_zext_i1(i1 %b) {
+; CHECK-LABEL: @shl1_zext_i1(
+; CHECK-NEXT: [[SHL:%.*]] = select i1 [[B:%.*]], i32 2, i32 1
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %x = zext i1 %b to i32
+ %shl = shl i32 1, %x
+ ret i32 %shl
+}
+
+define <2 x i32> @shl1_range_zero_one_vec(<2 x i32> range(i32 0, 2) %x) {
+; CHECK-LABEL: @shl1_range_zero_one_vec(
+; CHECK-NEXT: [[SHL:%.*]] = add nuw nsw <2 x i32> [[X:%.*]], splat (i32 1)
+; CHECK-NEXT: ret <2 x i32> [[SHL]]
+;
+ %shl = shl <2 x i32> splat (i32 1), %x
+ ret <2 x i32> %shl
+}
+
+; Negative test - x is not known to be 0 or 1.
+define i32 @shl1_range_zero_two_fail(i32 range(i32 0, 3) %x) {
+; CHECK-LABEL: @shl1_range_zero_two_fail(
+; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i32 1, [[X:%.*]]
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %shl = shl i32 1, %x
+ ret i32 %shl
+}
+
declare i16 @llvm.umax.i16(i16, i16)
The following correctness issue was found by llvm-hackme.
This comment is generated by an automated correctness checking service designed to help identify critical correctness bugs (opt crashes or Alive2 miscompilations) and improve PR review efficiency under limited reviewer bandwidth.
Status: Previously reported issue no longer reproduces
The correctness bug previously reported by llvm-hackme no longer reproduces with the current PR update, and fuzzing did not identify any new correctness bugs (opt crashes or Alive2 miscompilations).
Baseline Revision: a8166e1c52c774129e23f5cee37c26161c116a84
PR Head SHA: c47c8e30eaecd18d87798837d223e5cd42c11575
Patch SHA256: 0476fe2b08d16c571c793a9897287f10d4dcc09c8cbb7e529d58f7d7684cdd3e
A correctness issue was identified by llvm-hackme. Please see the issue comment for details.
Thanks for catching this, @dtcxzyw! You're right, for narrow types like i2, the constant 2 is itself negative when interpreted as signed, so unconditionally setting nsw on the replacement add/sub was unsound.
Fixed by only setting nsw when BitWidth > 2 (nuw remains safe unconditionally). Also added regression tests that reproduce the exact i2 case and pushed the fix. Please take another look when you get a chance.