Skip to content

Commit 3e1d2c3

Browse files
committed
[InstCombine] Fix or of commuted foldable predicates
1d90e53 switch this code to store the predicates and operands in variables, but retained a swapOperands() call here. Thus the commuted cases were no longer folded. Additionally, as the change was not reported, the next InstCombine iteration would not pick it up either.
1 parent 2b252c1 commit 3e1d2c3

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,11 +2580,12 @@ Value *InstCombinerImpl::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
25802580

25812581
// (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
25822582
if (predicatesFoldable(PredL, PredR)) {
2583-
if (LHS0 == RHS1 && LHS1 == RHS0)
2584-
LHS->swapOperands();
2583+
if (LHS0 == RHS1 && LHS1 == RHS0) {
2584+
PredL = ICmpInst::getSwappedPredicate(PredL);
2585+
std::swap(LHS0, LHS1);
2586+
}
25852587
if (LHS0 == RHS0 && LHS1 == RHS1) {
2586-
unsigned Code =
2587-
getICmpCode(LHS->getPredicate()) | getICmpCode(RHS->getPredicate());
2588+
unsigned Code = getICmpCode(PredL) | getICmpCode(PredR);
25882589
bool IsSigned = LHS->isSigned() || RHS->isSigned();
25892590
return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
25902591
}

llvm/test/Transforms/InstCombine/or.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ define i1 @test14(i32 %A, i32 %B) {
3939

4040
define i1 @test14_commuted(i32 %A, i32 %B) {
4141
; CHECK-LABEL: @test14_commuted(
42-
; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[B:%.*]], [[A:%.*]]
43-
; CHECK-NEXT: [[C2:%.*]] = icmp ult i32 [[B]], [[A]]
44-
; CHECK-NEXT: [[D:%.*]] = or i1 [[C1]], [[C2]]
45-
; CHECK-NEXT: ret i1 [[D]]
42+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[B:%.*]], [[A:%.*]]
43+
; CHECK-NEXT: ret i1 [[TMP1]]
4644
;
4745
%C1 = icmp ult i32 %A, %B
4846
%C2 = icmp ult i32 %B, %A

0 commit comments

Comments
 (0)