[PATCH] D149001: [InstSimplify] sdiv a (1 srem b) --> a

Zhu Siyuan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 22 11:14:57 PDT 2023


floatshadow created this revision.
floatshadow added reviewers: RKSimon, nikic.
Herald added subscribers: hoy, StephenFan, hiraditya.
Herald added a project: All.
floatshadow requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Try to fix issue https://github.com/llvm/llvm-project/issues/62163
as (1 sdiv X) is not equivalent with zext (X != 1), pattern sdiv a (1 srem b) miss the optimization.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149001

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


Index: llvm/test/Transforms/InstSimplify/div.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/div.ll
+++ llvm/test/Transforms/InstSimplify/div.ll
@@ -434,3 +434,21 @@
 }
 
 !0 = !{i32 0, i32 3}
+
+define i32 @sdiv_one_srem_divisor(i32 %a, i32 %b) {
+; CHECK-LABEL: @sdiv_one_srem_divisor(
+; CHECK-NEXT:    ret i32 [[O:%.*]]
+;
+  %srem = srem i32 1, %b
+  %sdiv = sdiv i32 %a, %srem
+  ret i32 %sdiv
+}
+
+define <2 x i8> @sdiv_one_vec_srem_divisor(<2 x i8> %a, <2 x i8> %b) {
+; CHECK-LABEL: @sdiv_one_vec_srem_divisor(
+; CHECK-NEXT:    ret <2 x i8> [[O:%.*]]
+;
+  %srem = srem <2 x i8> <i8 1, i8 1>, %b 
+  %sdiv = sdiv <2 x i8> %a, %srem
+  ret <2 x i8> %sdiv
+}
\ No newline at end of file
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1255,6 +1255,11 @@
 /// If not, this returns null.
 static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
                                const SimplifyQuery &Q, unsigned MaxRecurse) {
+  // If the divisor is 0, the result is undefined, so assume the divisor is 1.
+  // sdiv Op0, (1 srem X) --> sdiv Op0, 1 --> Op1
+  if (match(Op1, m_SRem(m_One(), m_Value())))
+    return Op0;
+
   // If two operands are negated and no signed overflow, return -1.
   if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
     return Constant::getAllOnesValue(Op0->getType());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149001.516089.patch
Type: text/x-patch
Size: 1543 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230422/0051bb0c/attachment.bin>


More information about the llvm-commits mailing list