[PATCH] D145327: [InstSimplify] Simplify `(shl nsw nuw X, BitWidth - 1)` -> `0`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 5 12:18:05 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.

https://alive2.llvm.org/ce/z/uFy5zT


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145327

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/shift.ll


Index: llvm/test/Transforms/InstSimplify/shift.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/shift.ll
+++ llvm/test/Transforms/InstSimplify/shift.ll
@@ -350,3 +350,51 @@
   %vc = lshr <vscale x 4 x i16> %va, shufflevector (<vscale x 4 x i16> insertelement (<vscale x 4 x i16> poison, i16 16, i32 0), <vscale x 4 x i16> poison, <vscale x 4 x i32> zeroinitializer)
   ret <vscale x 4 x i16> %vc
 }
+
+; shl nsw+nuw is 0
+define i8 @shl_nsw_nuw_7_eq_0(i8 %x) {
+; CHECK-LABEL: @shl_nsw_nuw_7_eq_0(
+; CHECK-NEXT:    ret i8 0
+;
+  %y = shl nsw nuw i8 %x, 7
+  ret i8 %y
+}
+
+; Make sure we are match element width
+define <2 x i8> @shl_vec_nsw_nuw_7_eq_0(<2 x i8> %x) {
+; CHECK-LABEL: @shl_vec_nsw_nuw_7_eq_0(
+; CHECK-NEXT:    ret <2 x i8> zeroinitializer
+;
+  %y = shl nsw nuw <2 x i8> %x, <i8 7, i8 7>
+  ret <2 x i8> %y
+}
+
+; negative test (missing nuw)
+define i8 @shl_nsw_7_fail_missing_nuw(i8 %x) {
+; CHECK-LABEL: @shl_nsw_7_fail_missing_nuw(
+; CHECK-NEXT:    [[Y:%.*]] = shl nsw i8 [[X:%.*]], 7
+; CHECK-NEXT:    ret i8 [[Y]]
+;
+  %y = shl nsw i8 %x, 7
+  ret i8 %y
+}
+
+; negative test (missing nsw)
+define i8 @shl_nuw_7_fail_missing_nsw(i8 %x) {
+; CHECK-LABEL: @shl_nuw_7_fail_missing_nsw(
+; CHECK-NEXT:    [[Y:%.*]] = shl nuw i8 [[X:%.*]], 7
+; CHECK-NEXT:    ret i8 [[Y]]
+;
+  %y = shl nuw i8 %x, 7
+  ret i8 %y
+}
+
+; negative test (shift value != bitwdth - 1)
+define i8 @shl_nsw_nuw_6_do_nothing(i8 %x) {
+; CHECK-LABEL: @shl_nsw_nuw_6_do_nothing(
+; CHECK-NEXT:    [[Y:%.*]] = shl nuw nsw i8 [[X:%.*]], 6
+; CHECK-NEXT:    ret i8 [[Y]]
+;
+  %y = shl nsw nuw i8 %x, 6
+  ret i8 %y
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1445,10 +1445,11 @@
           simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
     return V;
 
+  Type * Ty = Op0->getType();
   // undef << X -> 0
   // undef << X -> undef if (if it's NSW/NUW)
   if (Q.isUndefValue(Op0))
-    return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Op0->getType());
+    return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Ty);
 
   // (X >> A) << A -> X
   Value *X;
@@ -1462,6 +1463,9 @@
   // NOTE: could use computeKnownBits() / LazyValueInfo,
   // but the cost-benefit analysis suggests it isn't worth it.
 
+  if (IsNSW && IsNUW && match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1)))
+    return Constant::getNullValue(Ty);
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145327.502455.patch
Type: text/x-patch
Size: 2601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230305/a188b760/attachment.bin>


More information about the llvm-commits mailing list