[llvm] 673a467 - [ConstantFold] Avoid creation of undesirable binop

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 25 07:55:07 PDT 2023


Author: Nikita Popov
Date: 2023-07-25T16:54:57+02:00
New Revision: 673a4671f3e8b7158d990f6456428175a6eac38c

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

LOG: [ConstantFold] Avoid creation of undesirable binop

When commuting the operands, don't create a constant expression
for undesirable binops. Only invoke the constant folding function
in that case.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Transforms/InstCombine/pr32686.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 40b21876281142..04e4217beb0884 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1084,7 +1084,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
   } else if (isa<ConstantInt>(C1)) {
     // If C1 is a ConstantInt and C2 is not, swap the operands.
     if (Instruction::isCommutative(Opcode))
-      return ConstantExpr::get(Opcode, C2, C1);
+      return ConstantExpr::isDesirableBinOp(Opcode)
+                 ? ConstantExpr::get(Opcode, C2, C1)
+                 : ConstantFoldBinaryInstruction(Opcode, C2, C1);
   }
 
   if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {

diff  --git a/llvm/test/Transforms/InstCombine/pr32686.ll b/llvm/test/Transforms/InstCombine/pr32686.ll
index acce81a603d2d0..f981065bebeb80 100644
--- a/llvm/test/Transforms/InstCombine/pr32686.ll
+++ b/llvm/test/Transforms/InstCombine/pr32686.ll
@@ -8,8 +8,9 @@ define void @tinkywinky() {
 ; CHECK-LABEL: @tinkywinky(
 ; CHECK-NEXT:    [[PATATINO:%.*]] = load i8, ptr @a, align 1
 ; CHECK-NEXT:    [[TOBOOL_NOT:%.*]] = icmp eq i8 [[PATATINO]], 0
-; CHECK-NEXT:    [[TMP1:%.*]] = zext i1 [[TOBOOL_NOT]] to i32
-; CHECK-NEXT:    [[OR1:%.*]] = or i32 [[TMP1]], or (i32 zext (i1 icmp ne (ptr @a, ptr @b) to i32), i32 2)
+; CHECK-NEXT:    [[TMP1:%.*]] = or i1 [[TOBOOL_NOT]], icmp ne (ptr @a, ptr @b)
+; CHECK-NEXT:    [[TMP2:%.*]] = zext i1 [[TMP1]] to i32
+; CHECK-NEXT:    [[OR1:%.*]] = or i32 [[TMP2]], 2
 ; CHECK-NEXT:    store i32 [[OR1]], ptr @b, align 4
 ; CHECK-NEXT:    ret void
 ;


        


More information about the llvm-commits mailing list