[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:02:14 PDT 2023


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

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

>From c2a2c85364936bb95581fbbc7448c52f0cd48c88 Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Tue, 17 Oct 2023 16:58:45 +0800
Subject: [PATCH] [InstCombine] Don't mix X << Y / Z << Y with X << Y / X << Z

---
 .../InstCombine/InstCombineMulDivRem.cpp           |  8 ++++----
 llvm/test/Transforms/InstCombine/div-shift.ll      | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

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
+}



More information about the llvm-commits mailing list