diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 4e731390533fe..3fdd9195ed86c 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -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;