[llvm] Add a situattion that mul instruction should not be replaced. (PR #72876)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 06:16:27 PST 2023


https://github.com/shaojingzhi created https://github.com/llvm/llvm-project/pull/72876

Check the operands of I are used in no more than one place, which can not be deleted, cause a mul instruction has far more weight than add and shl instruction in IR, thus this method cannot achieve the goal of simplifying instructions, just return null.

>From 91c77fc4e6784a1e35f2be5e6e4d5bf7ea950827 Mon Sep 17 00:00:00 2001
From: shaojingzhi <28193696+shaojingzhi at users.noreply.github.com>
Date: Mon, 20 Nov 2023 22:09:43 +0800
Subject: [PATCH 1/2] Update InstCombineAddSub.cpp

Add a situation that mul cannot be replaced by add and shl.
---
 llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 90b1c133461a408..5b82c3179792f64 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1405,6 +1405,14 @@ static Instruction *foldBoxMultiply(BinaryOperator &I) {
   // ResLo = (CrossSum << HalfBits) + (YLo * XLo)
   Value *XLo, *YLo;
   Value *CrossSum;
+  
+  // Checking the operands of I is used in no more than one place,
+  // which can not be deleted, cause a mul instruction has far more weight than
+  // add and shl instruction in IR, thus this method cannot achieve the goal of
+  // simplifying instructions, just return null.
+  if ((!I.getOperand(0)->hasOneUser() || !I.getOperand(1)->hasOneUser()))
+    return nullptr;
+
   if (!match(&I, m_c_Add(m_Shl(m_Value(CrossSum), m_SpecificInt(HalfBits)),
                          m_Mul(m_Value(YLo), m_Value(XLo)))))
     return nullptr;

>From c6c87dbab6cba862cd0da4c6bf72b6e24fa0f613 Mon Sep 17 00:00:00 2001
From: shaojingzhi <28193696+shaojingzhi at users.noreply.github.com>
Date: Mon, 20 Nov 2023 22:12:34 +0800
Subject: [PATCH 2/2] Update mul_full_64.ll

---
 llvm/test/Transforms/InstCombine/mul_full_64.ll | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/InstCombine/mul_full_64.ll b/llvm/test/Transforms/InstCombine/mul_full_64.ll
index 8a57b548cd14b9e..5c57270fb147db8 100644
--- a/llvm/test/Transforms/InstCombine/mul_full_64.ll
+++ b/llvm/test/Transforms/InstCombine/mul_full_64.ll
@@ -177,6 +177,7 @@ define i64 @mul_full_64_variant2(i64 %a, i64 %b, ptr nocapture %rhi) {
   ret i64 %add27
 }
 
+; Negative test case for mul_fold function: MUL7 is used in more than one place
 define i64 @mul_full_64_variant3(i64 %a, i64 %b, ptr nocapture %rhi) {
 ; CHECK-LABEL: @mul_full_64_variant3(
 ; CHECK-NEXT:    [[CONV:%.*]] = and i64 [[A:%.*]], 4294967295
@@ -196,7 +197,9 @@ define i64 @mul_full_64_variant3(i64 %a, i64 %b, ptr nocapture %rhi) {
 ; CHECK-NEXT:    [[SHR_I:%.*]] = lshr i64 [[ADD15]], 32
 ; CHECK-NEXT:    [[ADD17:%.*]] = add i64 [[ADD10]], [[SHR_I]]
 ; CHECK-NEXT:    store i64 [[ADD17]], ptr [[RHI:%.*]], align 8
-; CHECK-NEXT:    [[ADD19:%.*]] = mul i64 [[A]], [[B]]
+; CHECK-NEXT:    [[ADD18:%.*]] = add i64 [[MUL6]], [[MUL5]]
+; CHECK-NEXT:    [[SHL:%.*]] = shl i64 [[ADD18]], 32
+; CHECK-NEXT:    [[ADD19:%.*]] = add i64 [[SHL]], [[MUL7]]
 ; CHECK-NEXT:    ret i64 [[ADD19]]
 ;
   %conv = and i64 %a, 4294967295



More information about the llvm-commits mailing list