Skip to content

Commit

Permalink
[InstCombine] Change operand order in recursive and/or of icmps fold
Browse files Browse the repository at this point in the history
The order obviously doesn't matter for bitwise and/or, but would
matter for logical and/or, so change it to preserve the original
order.
  • Loading branch information
nikic committed May 23, 2022
1 parent 131249c commit f45c1e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Expand Up @@ -1995,15 +1995,15 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
if (auto *Cmp = dyn_cast<ICmpInst>(Y))
if (Value *Res = foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ true))
return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
return replaceInstUsesWith(I, Builder.CreateAnd(X, Res));
}
if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
if (auto *Cmp = dyn_cast<ICmpInst>(X))
if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ true))
return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
if (auto *Cmp = dyn_cast<ICmpInst>(Y))
if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ true))
return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
return replaceInstUsesWith(I, Builder.CreateAnd(X, Res));
}
}

Expand Down Expand Up @@ -2794,15 +2794,15 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
if (auto *Cmp = dyn_cast<ICmpInst>(Y))
if (Value *Res = foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ false))
return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
return replaceInstUsesWith(I, Builder.CreateOr(X, Res));
}
if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
if (auto *Cmp = dyn_cast<ICmpInst>(X))
if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ false))
return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
if (auto *Cmp = dyn_cast<ICmpInst>(Y))
if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ false))
return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
return replaceInstUsesWith(I, Builder.CreateOr(X, Res));
}
}

Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Transforms/InstCombine/and-or-icmps.ll
Expand Up @@ -1281,7 +1281,7 @@ define i1 @bitwise_and_bitwise_and_icmps(i8 %x, i8 %y) {
; CHECK-LABEL: @bitwise_and_bitwise_and_icmps(
; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[Y:%.*]], 42
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[C1]]
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[C1]], [[TMP1]]
; CHECK-NEXT: ret i1 [[TMP2]]
;
%c1 = icmp eq i8 %y, 42
Expand All @@ -1296,7 +1296,7 @@ define i1 @bitwise_and_bitwise_and_icmps_comm1(i8 %x, i8 %y) {
; CHECK-LABEL: @bitwise_and_bitwise_and_icmps_comm1(
; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[Y:%.*]], 42
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[C1]]
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[C1]], [[TMP1]]
; CHECK-NEXT: ret i1 [[TMP2]]
;
%c1 = icmp eq i8 %y, 42
Expand Down Expand Up @@ -1409,7 +1409,7 @@ define i1 @logical_and_bitwise_and_icmps(i8 %x, i8 %y) {
; CHECK-LABEL: @logical_and_bitwise_and_icmps(
; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[Y:%.*]], 42
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[C1]]
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[C1]], [[TMP1]]
; CHECK-NEXT: ret i1 [[TMP2]]
;
%c1 = icmp eq i8 %y, 42
Expand Down Expand Up @@ -1539,7 +1539,7 @@ define i1 @bitwise_or_bitwise_or_icmps(i8 %x, i8 %y) {
; CHECK-LABEL: @bitwise_or_bitwise_or_icmps(
; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[Y:%.*]], 42
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[TMP1]], [[C1]]
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[C1]], [[TMP1]]
; CHECK-NEXT: ret i1 [[TMP2]]
;
%c1 = icmp eq i8 %y, 42
Expand All @@ -1554,7 +1554,7 @@ define i1 @bitwise_or_bitwise_or_icmps_comm1(i8 %x, i8 %y) {
; CHECK-LABEL: @bitwise_or_bitwise_or_icmps_comm1(
; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[Y:%.*]], 42
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[TMP1]], [[C1]]
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[C1]], [[TMP1]]
; CHECK-NEXT: ret i1 [[TMP2]]
;
%c1 = icmp eq i8 %y, 42
Expand Down Expand Up @@ -1667,7 +1667,7 @@ define i1 @logical_or_bitwise_or_icmps(i8 %x, i8 %y) {
; CHECK-LABEL: @logical_or_bitwise_or_icmps(
; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[Y:%.*]], 42
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[TMP1]], [[C1]]
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[C1]], [[TMP1]]
; CHECK-NEXT: ret i1 [[TMP2]]
;
%c1 = icmp eq i8 %y, 42
Expand Down

0 comments on commit f45c1e4

Please sign in to comment.