Skip to content

Commit 460faa0

Browse files
committed
[InstSimplify] Check common operand with constant earlier
If both icmps have the same operands and the RHS is constant, we would currently go into the isImpliedCondMatchingOperands() code path, instead of the isImpliedCondCommonOperandWithConstants() path. Both are correct, but the latter can produce more accurate results if the implication is dependent on the sign.
1 parent 89b0044 commit 460faa0

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8352,17 +8352,17 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
83528352
CmpInst::Predicate LPred =
83538353
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
83548354

8355-
// Can we infer anything when the two compares have matching operands?
8356-
bool AreSwappedOps;
8357-
if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps))
8358-
return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
8359-
83608355
// Can we infer anything when the 0-operands match and the 1-operands are
83618356
// constants (not necessarily matching)?
83628357
const APInt *LC, *RC;
83638358
if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
83648359
return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
83658360

8361+
// Can we infer anything when the two compares have matching operands?
8362+
bool AreSwappedOps;
8363+
if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps))
8364+
return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
8365+
83668366
// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
83678367
if (ICmpInst::isUnsigned(LPred) && ICmpInst::isUnsigned(RPred)) {
83688368
if (L0 == R1) {

llvm/test/Transforms/InstSimplify/implies.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,7 @@ define i1 @lshr_value(i32 %length.i, i32 %i, i32 %v) {
501501

502502
define i1 @same_ops_with_constant(i8 %x) {
503503
; CHECK-LABEL: @same_ops_with_constant(
504-
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[X:%.*]], 5
505-
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i8 [[X]], 5
506-
; CHECK-NEXT: [[RES:%.*]] = icmp ule i1 [[CMP1]], [[CMP2]]
507-
; CHECK-NEXT: ret i1 [[RES]]
504+
; CHECK-NEXT: ret i1 true
508505
;
509506
%cmp1 = icmp sgt i8 %x, 5
510507
%cmp2 = icmp ugt i8 %x, 5

0 commit comments

Comments
 (0)