Skip to content

Commit 22ff7c5

Browse files
authored
[ValueTracking][NFC] move isKnownInversion to ValueTracking (#95321)
I am using `isKnownInversion` in the following pr #94915 it is useful to have the method in a shared class so I can reuse it. I am not sure if `ValueTracking` is the correct place but it looks like most of the methods with the pattern `isKnownX` belong there.
1 parent 3bcd80a commit 22ff7c5

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth = 0);
136136
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false,
137137
bool AllowPoison = true);
138138

139+
/// Return true iff:
140+
/// 1. X is poison implies Y is poison.
141+
/// 2. X is true implies Y is false.
142+
/// 3. X is false implies Y is true.
143+
/// Otherwise, return false.
144+
bool isKnownInversion(const Value *X, const Value *Y);
145+
139146
/// Returns true if the give value is known to be non-negative.
140147
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
141148
unsigned Depth = 0);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8176,6 +8176,28 @@ bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
81768176
match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
81778177
}
81788178

8179+
bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8180+
// Handle X = icmp pred A, B, Y = icmp pred A, C.
8181+
Value *A, *B, *C;
8182+
ICmpInst::Predicate Pred1, Pred2;
8183+
if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8184+
!match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8185+
return false;
8186+
8187+
if (B == C)
8188+
return Pred1 == ICmpInst::getInversePredicate(Pred2);
8189+
8190+
// Try to infer the relationship from constant ranges.
8191+
const APInt *RHSC1, *RHSC2;
8192+
if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8193+
return false;
8194+
8195+
const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8196+
const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8197+
8198+
return CR1.inverse() == CR2;
8199+
}
8200+
81798201
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
81808202
FastMathFlags FMF,
81818203
Value *CmpLHS, Value *CmpRHS,

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,33 +3520,6 @@ static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
35203520
return false;
35213521
}
35223522

3523-
/// Return true iff:
3524-
/// 1. X is poison implies Y is poison.
3525-
/// 2. X is true implies Y is false.
3526-
/// 3. X is false implies Y is true.
3527-
/// Otherwise, return false.
3528-
static bool isKnownInversion(Value *X, Value *Y) {
3529-
// Handle X = icmp pred A, B, Y = icmp pred A, C.
3530-
Value *A, *B, *C;
3531-
ICmpInst::Predicate Pred1, Pred2;
3532-
if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
3533-
!match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
3534-
return false;
3535-
3536-
if (B == C)
3537-
return Pred1 == ICmpInst::getInversePredicate(Pred2);
3538-
3539-
// Try to infer the relationship from constant ranges.
3540-
const APInt *RHSC1, *RHSC2;
3541-
if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
3542-
return false;
3543-
3544-
const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
3545-
const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
3546-
3547-
return CR1.inverse() == CR2;
3548-
}
3549-
35503523
Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
35513524
Value *CondVal = SI.getCondition();
35523525
Value *TrueVal = SI.getTrueValue();

0 commit comments

Comments
 (0)