Skip to content

Commit 673a467

Browse files
committed
[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.
1 parent b6cf0ea commit 673a467

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
10841084
} else if (isa<ConstantInt>(C1)) {
10851085
// If C1 is a ConstantInt and C2 is not, swap the operands.
10861086
if (Instruction::isCommutative(Opcode))
1087-
return ConstantExpr::get(Opcode, C2, C1);
1087+
return ConstantExpr::isDesirableBinOp(Opcode)
1088+
? ConstantExpr::get(Opcode, C2, C1)
1089+
: ConstantFoldBinaryInstruction(Opcode, C2, C1);
10881090
}
10891091

10901092
if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {

llvm/test/Transforms/InstCombine/pr32686.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ define void @tinkywinky() {
88
; CHECK-LABEL: @tinkywinky(
99
; CHECK-NEXT: [[PATATINO:%.*]] = load i8, ptr @a, align 1
1010
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[PATATINO]], 0
11-
; CHECK-NEXT: [[TMP1:%.*]] = zext i1 [[TOBOOL_NOT]] to i32
12-
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], or (i32 zext (i1 icmp ne (ptr @a, ptr @b) to i32), i32 2)
11+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[TOBOOL_NOT]], icmp ne (ptr @a, ptr @b)
12+
; CHECK-NEXT: [[TMP2:%.*]] = zext i1 [[TMP1]] to i32
13+
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP2]], 2
1314
; CHECK-NEXT: store i32 [[OR1]], ptr @b, align 4
1415
; CHECK-NEXT: ret void
1516
;

0 commit comments

Comments
 (0)