[InstCombine] Avoid ConstantExpr::get()
authorNikita Popov <npopov@redhat.com>
Thu, 20 Jul 2023 13:21:19 +0000 (15:21 +0200)
committerNikita Popov <npopov@redhat.com>
Thu, 20 Jul 2023 13:24:02 +0000 (15:24 +0200)
Use ConstantFoldBinaryOpOperands() instead of ConstantExpr::get().
This will continue working with binary operands that are not
supported as constant expressions.

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

index 4e73139..3fdd919 100644 (file)
@@ -360,7 +360,11 @@ static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1,
   // (op (cast (op X, C2)), C1) --> (op (cast X), FoldedC)
   Type *DestTy = C1->getType();
   Constant *CastC2 = ConstantExpr::getCast(CastOpcode, C2, DestTy);
-  Constant *FoldedC = ConstantExpr::get(AssocOpcode, C1, CastC2);
+  Constant *FoldedC =
+      ConstantFoldBinaryOpOperands(AssocOpcode, C1, CastC2, IC.getDataLayout());
+  if (!FoldedC)
+    return false;
+
   IC.replaceOperand(*Cast, 0, BinOp2->getOperand(0));
   IC.replaceOperand(*BinOp1, 1, FoldedC);
   return true;