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.
This commit is contained in:
Nikita Popov 2023-07-25 16:49:54 +02:00
parent 47ba908a5e
commit 90d825be70

View File

@ -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)) {