[llvm] [InstCombine] Don't mix X << Y / Z << Y with X << Y / X << Z (PR #69302)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 02:03:17 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: XChy (XChy)

<details>
<summary>Changes</summary>

Fixes #<!-- -->69291.
This patch adds `else` for different patterns to avoid mixing these pattern. 

---
Full diff: https://github.com/llvm/llvm-project/pull/69302.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (+4-4) 
- (modified) llvm/test/Transforms/InstCombine/div-shift.ll (+14) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 26e0a6700042ef0..81c6c3f88e148e2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -959,8 +959,8 @@ static Instruction *foldIDivShl(BinaryOperator &I,
 
   // With appropriate no-wrap constraints, remove a common factor in the
   // dividend and divisor that is disguised as a left-shift amount.
-  if (match(Op0, m_Shl(m_Value(X), m_Value(Z))) &&
-      match(Op1, m_Shl(m_Value(Y), m_Specific(Z)))) {
+  else if (match(Op0, m_Shl(m_Value(X), m_Value(Z))) &&
+           match(Op1, m_Shl(m_Value(Y), m_Specific(Z)))) {
     auto *Shl0 = cast<OverflowingBinaryOperator>(Op0);
     auto *Shl1 = cast<OverflowingBinaryOperator>(Op1);
 
@@ -982,8 +982,8 @@ static Instruction *foldIDivShl(BinaryOperator &I,
 
   // If X << Y and X << Z does not overflow, then:
   // (X << Y) / (X << Z) -> (1 << Y) / (1 << Z) -> 1 << Y >> Z
-  if (match(Op0, m_Shl(m_Value(X), m_Value(Y))) &&
-      match(Op1, m_Shl(m_Specific(X), m_Value(Z)))) {
+  else if (match(Op0, m_Shl(m_Value(X), m_Value(Y))) &&
+           match(Op1, m_Shl(m_Specific(X), m_Value(Z)))) {
     auto *Shl0 = cast<OverflowingBinaryOperator>(Op0);
     auto *Shl1 = cast<OverflowingBinaryOperator>(Op1);
 
diff --git a/llvm/test/Transforms/InstCombine/div-shift.ll b/llvm/test/Transforms/InstCombine/div-shift.ll
index 635c01d84441d8a..d208837f04594ab 100644
--- a/llvm/test/Transforms/InstCombine/div-shift.ll
+++ b/llvm/test/Transforms/InstCombine/div-shift.ll
@@ -1280,3 +1280,17 @@ entry:
   %div = sdiv i32 %lhs, %rhs
   ret i32 %div
 }
+
+ at a = external global i32
+define i32 @pr69291() {
+; CHECK-LABEL: @pr69291(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret i32 1
+;
+entry:
+  %conv = load i32, ptr @a, align 1
+  %add = shl nuw nsw i32 %conv, 1
+  %add2 = shl nuw nsw i32 %conv, 1
+  %div = sdiv i32 %add, %add2
+  ret i32 %div
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/69302


More information about the llvm-commits mailing list