[PATCH] D145326: [InstCombine] Transform `(shift X, Or(Y, BitWidth-1))` -> `(shift X,BitWidth-1)`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 5 12:17:42 PST 2023


goldstein.w.n created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

shl : https://alive2.llvm.org/ce/z/_B7Qca
lshr: https://alive2.llvm.org/ce/z/6eXz_W
ashr: https://alive2.llvm.org/ce/z/oGEx-q


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145326

Files:
  llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
  llvm/test/Transforms/InstCombine/shift.ll


Index: llvm/test/Transforms/InstCombine/shift.ll
===================================================================
--- llvm/test/Transforms/InstCombine/shift.ll
+++ llvm/test/Transforms/InstCombine/shift.ll
@@ -2067,3 +2067,47 @@
   %shl = shl i32 2, %tz
   ret i32 %shl
 }
+
+; shift (X, amt | bitwidth - 1) -> shift (X, bitwidth - 1)
+define i6 @shl_or7_eq_shl7(i6 %x, i6 %c) {
+; CHECK-LABEL: @shl_or7_eq_shl7(
+; CHECK-NEXT:    [[Y:%.*]] = shl nsw i6 [[X:%.*]], 5
+; CHECK-NEXT:    ret i6 [[Y]]
+;
+  %amt = or i6 %c, 5
+  ;; nsw not needed for transform, just check that we propegate.
+  %y = shl nsw i6 %x, %amt
+  ret i6 %y
+}
+
+define <2 x i8> @lshr_vec_or7_eq_shl7(<2 x i8> %x, <2 x i8> %c) {
+; CHECK-LABEL: @lshr_vec_or7_eq_shl7(
+; CHECK-NEXT:    [[Y:%.*]] = lshr <2 x i8> [[X:%.*]], <i8 7, i8 7>
+; CHECK-NEXT:    ret <2 x i8> [[Y]]
+;
+  %amt = or <2 x i8> %c, <i8 7, i8 7>
+  %y = lshr <2 x i8> %x, %amt
+  ret <2 x i8> %y
+}
+
+define <2 x i8> @ashr_vec_or7_eq_ashr7(<2 x i8> %x, <2 x i8> %c) {
+; CHECK-LABEL: @ashr_vec_or7_eq_ashr7(
+; CHECK-NEXT:    [[Y:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 7, i8 7>
+; CHECK-NEXT:    ret <2 x i8> [[Y]]
+;
+  %amt = or <2 x i8> %c, <i8 7, i8 7>
+  %y = ashr <2 x i8> %x, %amt
+  ret <2 x i8> %y
+}
+
+; Negative test not bitwidth - 1
+define <2 x i8> @ashr_vec_or6_fail(<2 x i8> %x, <2 x i8> %c) {
+; CHECK-LABEL: @ashr_vec_or6_fail(
+; CHECK-NEXT:    [[AMT:%.*]] = or <2 x i8> [[C:%.*]], <i8 6, i8 6>
+; CHECK-NEXT:    [[Y:%.*]] = ashr <2 x i8> [[X:%.*]], [[AMT]]
+; CHECK-NEXT:    ret <2 x i8> [[Y]]
+;
+  %amt = or <2 x i8> %c, <i8 6, i8 6>
+  %y = ashr <2 x i8> %x, %amt
+  ret <2 x i8> %y
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -483,6 +483,16 @@
   if (Instruction *Logic = foldShiftOfShiftedBinOp(I, Builder))
     return Logic;
 
+  if (match(Op1, m_Or(m_Value(), m_SpecificInt(BitWidth - 1)))) {
+    BinaryOperator *NewShiftOp = BinaryOperator::Create(
+        I.getOpcode(), Op0, ConstantInt::get(Ty, BitWidth - 1));
+    if (I.getOpcode() == Instruction::Shl) {
+      NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
+      NewShiftOp->setHasNoSignedWrap(I.hasNoSignedWrap());
+    }
+    return NewShiftOp;
+  }
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145326.502454.patch
Type: text/x-patch
Size: 2428 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230305/2d5f829b/attachment.bin>


More information about the llvm-commits mailing list