Skip to content

Commit 69ab890

Browse files
committed
[ValueTracking] Extend LHS/RHS with matching operand to work without constants.
Previously we only handled the `L0 == R0` case if both `L1` and `R1` where constant. We can get more out of the analysis using general constant ranges instead. For example, `X u> Y` implies `X != 0`. In general, any strict comparison on `X` implies that `X` is not equal to the boundary value for the sign and constant ranges with/without sign bits can be useful in deducing implications.
1 parent 8f878c5 commit 69ab890

File tree

7 files changed

+58
-46
lines changed

7 files changed

+58
-46
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8499,20 +8499,20 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
84998499
return std::nullopt;
85008500
}
85018501

8502-
/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
8503-
/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
8502+
/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
8503+
/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
85048504
/// Otherwise, return std::nullopt if we can't infer anything.
8505-
static std::optional<bool> isImpliedCondCommonOperandWithConstants(
8506-
CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
8507-
const APInt &RC) {
8508-
ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC);
8509-
ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC);
8510-
ConstantRange Intersection = DomCR.intersectWith(CR);
8511-
ConstantRange Difference = DomCR.difference(CR);
8512-
if (Intersection.isEmptySet())
8513-
return false;
8514-
if (Difference.isEmptySet())
8505+
static std::optional<bool>
8506+
isImpliedCondCommonOperandWithCR(CmpInst::Predicate LPred, ConstantRange LCR,
8507+
CmpInst::Predicate RPred, ConstantRange RCR) {
8508+
ConstantRange DomCR = ConstantRange::makeAllowedICmpRegion(LPred, LCR);
8509+
// If all true values for lhs and true for rhs, lhs implies rhs
8510+
if (DomCR.icmp(RPred, RCR))
85158511
return true;
8512+
8513+
// If there is no overlap, lhs implies not rhs
8514+
if (DomCR.icmp(CmpInst::getInversePredicate(RPred), RCR))
8515+
return false;
85168516
return std::nullopt;
85178517
}
85188518

@@ -8532,11 +8532,38 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
85328532
CmpInst::Predicate LPred =
85338533
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
85348534

8535-
// Can we infer anything when the 0-operands match and the 1-operands are
8536-
// constants (not necessarily matching)?
8537-
const APInt *LC, *RC;
8538-
if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
8539-
return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
8535+
if (L0 == R1) {
8536+
std::swap(R0, R1);
8537+
RPred = ICmpInst::getSwappedPredicate(RPred);
8538+
}
8539+
if (L1 == R0) {
8540+
std::swap(L0, L1);
8541+
LPred = ICmpInst::getSwappedPredicate(LPred);
8542+
}
8543+
8544+
// See if we can infer anything if operand-0 matches and we have at least one
8545+
// constant operand-1.
8546+
if (L0 == R0 && L0->getType()->isIntOrIntVectorTy()) {
8547+
// Potential TODO: We could also further use the constant range of L0/R0 to
8548+
// further constraint the constant ranges. At the moment this leads to
8549+
// several regressions related to not transforming `multi_use(A + C0) eq/ne
8550+
// C1` (see discussion: D58633).
8551+
ConstantRange LCR = computeConstantRange(
8552+
L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
8553+
/*CxtI=*/nullptr, /*DT=*/nullptr, Depth);
8554+
ConstantRange RCR = computeConstantRange(
8555+
R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
8556+
/*CxtI=*/nullptr, /*DT=*/nullptr, Depth);
8557+
// Even if L1/R1 are not both constant, we can still sometimes deduce
8558+
// relationship from a single constant. For example X u> Y implies X != 0.
8559+
if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
8560+
return R;
8561+
// If both L1/R1 where exact constant ranges and we didn't get anything
8562+
// here, we won't be able to deduce this.
8563+
const APInt *Unused;
8564+
if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
8565+
return std::nullopt;
8566+
}
85408567

85418568
// Can we infer anything when the two compares have matching operands?
85428569
bool AreSwappedOps;

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/range-check.ll

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,7 @@ define i1 @negative4_logical(i32 %x, i32 %n) {
340340

341341
define i1 @negative5(i32 %x, i32 %n) {
342342
; CHECK-LABEL: @negative5(
343-
; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
344-
; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
345-
; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
346-
; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]]
347-
; CHECK-NEXT: ret i1 [[C]]
343+
; CHECK-NEXT: ret i1 true
348344
;
349345
%nn = and i32 %n, 2147483647
350346
%a = icmp slt i32 %x, %nn
@@ -355,11 +351,7 @@ define i1 @negative5(i32 %x, i32 %n) {
355351

356352
define i1 @negative5_logical(i32 %x, i32 %n) {
357353
; CHECK-LABEL: @negative5_logical(
358-
; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
359-
; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
360-
; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
361-
; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]]
362-
; CHECK-NEXT: ret i1 [[C]]
354+
; CHECK-NEXT: ret i1 true
363355
;
364356
%nn = and i32 %n, 2147483647
365357
%a = icmp slt i32 %x, %nn

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]]

llvm/test/Transforms/LoopVectorize/X86/pr23997.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ define void @foo(ptr addrspace(1) align 8 dereferenceable_or_null(16), ptr addrs
1212
; CHECK: preheader:
1313
; CHECK-NEXT: [[DOT10:%.*]] = getelementptr inbounds i8, ptr addrspace(1) [[TMP0:%.*]], i64 16
1414
; CHECK-NEXT: [[DOT12:%.*]] = getelementptr inbounds i8, ptr addrspace(1) [[TMP1:%.*]], i64 16
15-
; CHECK-NEXT: [[UMAX2:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2:%.*]], i64 1)
16-
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP2]], 16
15+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP2:%.*]], 16
1716
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
1817
; CHECK: vector.memcheck:
1918
; CHECK-NEXT: [[TMP3:%.*]] = shl i64 [[TMP2]], 3
@@ -25,7 +24,7 @@ define void @foo(ptr addrspace(1) align 8 dereferenceable_or_null(16), ptr addrs
2524
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
2625
; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
2726
; CHECK: vector.ph:
28-
; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[UMAX2]], -16
27+
; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[TMP2]], -16
2928
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
3029
; CHECK: vector.body:
3130
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
@@ -49,7 +48,7 @@ define void @foo(ptr addrspace(1) align 8 dereferenceable_or_null(16), ptr addrs
4948
; CHECK-NEXT: [[TMP13:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
5049
; CHECK-NEXT: br i1 [[TMP13]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
5150
; CHECK: middle.block:
52-
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[UMAX2]], [[N_VEC]]
51+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N_VEC]], [[TMP2]]
5352
; CHECK-NEXT: br i1 [[CMP_N]], label [[LOOPEXIT:%.*]], label [[SCALAR_PH]]
5453
; CHECK: scalar.ph:
5554
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[PREHEADER]] ], [ 0, [[VECTOR_MEMCHECK]] ]

llvm/test/Transforms/NewGVN/pr35125.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ define i32 @main() #0 {
1818
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[STOREMERGE]], [[PHIOFOPS]]
1919
; CHECK-NEXT: br i1 [[CMP2]], label [[IF_THEN3:%.*]], label [[IF_END6:%.*]]
2020
; CHECK: if.then3:
21-
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[STOREMERGE]], -1
22-
; CHECK-NEXT: br i1 [[TOBOOL]], label [[LOR_RHS:%.*]], label [[LOR_END:%.*]]
21+
; CHECK-NEXT: br i1 false, label [[LOR_RHS:%.*]], label [[LOR_END:%.*]]
2322
; CHECK: lor.rhs:
24-
; CHECK-NEXT: [[TOBOOL5:%.*]] = icmp ne i32 [[TMP0]], 0
25-
; CHECK-NEXT: [[PHITMP:%.*]] = zext i1 [[TOBOOL5]] to i32
23+
; CHECK-NEXT: store i8 poison, ptr null, align 1
2624
; CHECK-NEXT: br label [[LOR_END]]
2725
; CHECK: lor.end:
28-
; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ 1, [[IF_THEN3]] ], [ [[PHITMP]], [[LOR_RHS]] ]
29-
; CHECK-NEXT: store i32 [[TMP1]], ptr @a, align 4
26+
; CHECK-NEXT: store i32 1, ptr @a, align 4
3027
; CHECK-NEXT: br label [[IF_END6]]
3128
; CHECK: if.end6:
3229
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr @a, align 4

0 commit comments

Comments
 (0)