[llvm] 90d825b - Reapply [ConstantFold] Avoid creation of undesirable binop

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 27 00:49:01 PDT 2023


Author: Nikita Popov
Date: 2023-07-27T09:48:54+02:00
New Revision: 90d825be708cd09ab7bfbb6b7a10a98a9847c7c4

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

LOG: Reapply [ConstantFold] Avoid creation of undesirable binop

This was reverted together with another commit due to a test
conflict. Reapply without functional changes.

-----

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 4c3325063c09d0..db1a83bf66204b 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)) {


        


More information about the llvm-commits mailing list