Skip to content

Commit 9e43e77

Browse files
committed
[ValueTracking] Handle non-canonical operand order in isImpliedCondICmps
We don't always have canonical order here, so do it manually.
1 parent 192be3c commit 9e43e77

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8532,6 +8532,15 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
85328532
CmpInst::Predicate LPred =
85338533
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
85348534

8535+
// We can have non-canonical operands here so canonicalize constant to L1/R1.
8536+
if (match(L0, m_ImmConstant())) {
8537+
std::swap(L0, L1);
8538+
LPred = ICmpInst::getSwappedPredicate(LPred);
8539+
}
8540+
if (match(R0, m_ImmConstant())) {
8541+
std::swap(R0, R1);
8542+
RPred = ICmpInst::getSwappedPredicate(RPred);
8543+
}
85358544
// Can we infer anything when the 0-operands match and the 1-operands are
85368545
// constants (not necessarily matching)?
85378546
const APInt *LC, *RC;

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ define i1 @nonnull5(ptr %a) {
386386
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
387387
; CHECK-LABEL: @assumption_conflicts_with_known_bits(
388388
; CHECK-NEXT: store i1 true, ptr poison, align 1
389-
; CHECK-NEXT: ret i32 1
389+
; CHECK-NEXT: ret i32 poison
390390
;
391391
%and1 = and i32 %b, 3
392392
%B1 = lshr i32 %and1, %and1

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,10 +2925,8 @@ define i8 @select_replacement_loop3(i32 noundef %x) {
29252925

29262926
define i16 @select_replacement_loop4(i16 noundef %p_12) {
29272927
; CHECK-LABEL: @select_replacement_loop4(
2928-
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i16 [[P_12:%.*]], 2
2929-
; CHECK-NEXT: [[AND1:%.*]] = and i16 [[P_12]], 1
2930-
; CHECK-NEXT: [[AND2:%.*]] = select i1 [[CMP1]], i16 [[AND1]], i16 0
2931-
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i16 [[AND2]], [[P_12]]
2928+
; CHECK-NEXT: [[AND1:%.*]] = and i16 [[P_12:%.*]], 1
2929+
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i16 [[P_12]], 2
29322930
; CHECK-NEXT: [[AND3:%.*]] = select i1 [[CMP2]], i16 [[AND1]], i16 0
29332931
; CHECK-NEXT: ret i16 [[AND3]]
29342932
;

llvm/test/Transforms/InstCombine/shift.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,12 +1751,11 @@ define void @ashr_out_of_range_1(ptr %A) {
17511751
; CHECK-NEXT: [[L:%.*]] = load i177, ptr [[A:%.*]], align 4
17521752
; CHECK-NEXT: [[L_FROZEN:%.*]] = freeze i177 [[L]]
17531753
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i177 [[L_FROZEN]], -1
1754-
; CHECK-NEXT: [[B:%.*]] = select i1 [[TMP1]], i177 0, i177 [[L_FROZEN]]
1755-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i177 [[B]] to i64
1754+
; CHECK-NEXT: [[TMP6:%.*]] = trunc i177 [[L_FROZEN]] to i64
1755+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP6]]
17561756
; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i177, ptr [[A]], i64 [[TMP2]]
17571757
; CHECK-NEXT: [[G11:%.*]] = getelementptr i8, ptr [[TMP3]], i64 -24
1758-
; CHECK-NEXT: [[C17:%.*]] = icmp sgt i177 [[B]], [[L_FROZEN]]
1759-
; CHECK-NEXT: [[TMP4:%.*]] = sext i1 [[C17]] to i64
1758+
; CHECK-NEXT: [[TMP4:%.*]] = sext i1 [[TMP1]] to i64
17601759
; CHECK-NEXT: [[G62:%.*]] = getelementptr i177, ptr [[G11]], i64 [[TMP4]]
17611760
; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i177 [[L_FROZEN]], -1
17621761
; CHECK-NEXT: [[B28:%.*]] = select i1 [[TMP5]], i177 0, i177 [[L_FROZEN]]

0 commit comments

Comments
 (0)