[llvm] f95a6ae - [InstCombine] avoid splitting a constant expression with div/rem fold

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 30 07:16:55 PDT 2022


Author: Sanjay Patel
Date: 2022-07-30T09:45:25-04:00
New Revision: f95a6aea1b10505293016bbe7b33e0fdc1a51ccb

URL: https://github.com/llvm/llvm-project/commit/f95a6aea1b10505293016bbe7b33e0fdc1a51ccb
DIFF: https://github.com/llvm/llvm-project/commit/f95a6aea1b10505293016bbe7b33e0fdc1a51ccb.diff

LOG: [InstCombine] avoid splitting a constant expression with div/rem fold

Follow-up to d4940c0f3d43 to further limit the transform
to avoid an unintended pattern/fold of a constant expression.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/test/Transforms/InstCombine/udivrem-change-width.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index abe15e2bda24..173700970504 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1008,7 +1008,8 @@ static Instruction *narrowUDivURem(BinaryOperator &I,
   }
 
   Constant *C;
-  if (match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) {
+  if (isa<Instruction>(N) && match(N, m_OneUse(m_ZExt(m_Value(X)))) &&
+      match(D, m_Constant(C))) {
     // If the constant is the same in the smaller type, use the narrow version.
     Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
     if (ConstantExpr::getZExt(TruncC, Ty) != C)
@@ -1018,7 +1019,8 @@ static Instruction *narrowUDivURem(BinaryOperator &I,
     // urem (zext X), C --> zext (urem X, C')
     return new ZExtInst(Builder.CreateBinOp(Opcode, X, TruncC), Ty);
   }
-  if (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C))) {
+  if (isa<Instruction>(D) && match(D, m_OneUse(m_ZExt(m_Value(X)))) &&
+      match(N, m_Constant(C))) {
     // If the constant is the same in the smaller type, use the narrow version.
     Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
     if (ConstantExpr::getZExt(TruncC, Ty) != C)

diff  --git a/llvm/test/Transforms/InstCombine/udivrem-change-width.ll b/llvm/test/Transforms/InstCombine/udivrem-change-width.ll
index 88e31a29aa0f..1131e1393c48 100644
--- a/llvm/test/Transforms/InstCombine/udivrem-change-width.ll
+++ b/llvm/test/Transforms/InstCombine/udivrem-change-width.ll
@@ -293,8 +293,7 @@ define i32 @udiv_constexpr(i8 %a) {
 
 define i32 @udiv_const_constexpr(i8 %a) {
 ; CHECK-LABEL: @udiv_const_constexpr(
-; CHECK-NEXT:    [[TMP1:%.*]] = udiv i8 42, ptrtoint ([1 x i8]* @g1 to i8)
-; CHECK-NEXT:    [[D:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT:    [[D:%.*]] = udiv i32 42, zext (i8 ptrtoint ([1 x i8]* @g1 to i8) to i32)
 ; CHECK-NEXT:    ret i32 [[D]]
 ;
   %d = udiv i32 42, zext (i8 ptrtoint ([1 x i8]* @g1 to i8) to i32)
@@ -307,8 +306,7 @@ define i32 @udiv_const_constexpr(i8 %a) {
 
 define i32 @urem_const_constexpr(i8 %a) {
 ; CHECK-LABEL: @urem_const_constexpr(
-; CHECK-NEXT:    [[TMP1:%.*]] = urem i8 42, ptrtoint ([1 x i8]* @g2 to i8)
-; CHECK-NEXT:    [[D:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT:    [[D:%.*]] = urem i32 42, zext (i8 ptrtoint ([1 x i8]* @g2 to i8) to i32)
 ; CHECK-NEXT:    ret i32 [[D]]
 ;
   %d = urem i32 42, zext (i8 ptrtoint ([1 x i8]* @g2 to i8) to i32)
@@ -319,8 +317,7 @@ define i32 @urem_const_constexpr(i8 %a) {
 
 define i32 @udiv_constexpr_const(i8 %a) {
 ; CHECK-LABEL: @udiv_constexpr_const(
-; CHECK-NEXT:    [[TMP1:%.*]] = udiv i8 ptrtoint ([1 x i8]* @g3 to i8), 42
-; CHECK-NEXT:    [[D:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT:    [[D:%.*]] = udiv i32 zext (i8 ptrtoint ([1 x i8]* @g3 to i8) to i32), 42
 ; CHECK-NEXT:    ret i32 [[D]]
 ;
   %d = udiv i32 zext (i8 ptrtoint ([1 x i8]* @g3 to i8) to i32), 42
@@ -331,8 +328,7 @@ define i32 @udiv_constexpr_const(i8 %a) {
 
 define i32 @urem_constexpr_const(i8 %a) {
 ; CHECK-LABEL: @urem_constexpr_const(
-; CHECK-NEXT:    [[TMP1:%.*]] = urem i8 ptrtoint ([1 x i8]* @g4 to i8), 42
-; CHECK-NEXT:    [[D:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT:    [[D:%.*]] = urem i32 zext (i8 ptrtoint ([1 x i8]* @g4 to i8) to i32), 42
 ; CHECK-NEXT:    ret i32 [[D]]
 ;
   %d = urem i32 zext (i8 ptrtoint ([1 x i8]* @g4 to i8) to i32), 42


        


More information about the llvm-commits mailing list