[llvm] [ValueTracking] Improve `isImpliedCondCommonOperandWithConstants` to handle truncated LHS (PR #69829)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 4 02:01:20 PDT 2023
================
@@ -8231,14 +8231,30 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
return std::nullopt;
}
-/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
-/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
-/// Otherwise, return std::nullopt if we can't infer anything.
+/// Return true if "icmp LPred X, LC" implies "icmp RPred cast(X), RC" is true.
+/// Return false if "icmp LPred X, LC" implies "icmp RPred cast(X), RC" is
+/// false. Otherwise, return std::nullopt if we can't infer anything.
static std::optional<bool> isImpliedCondCommonOperandWithConstants(
- CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
- const APInt &RC) {
+ const Value *L0, CmpInst::Predicate LPred, const APInt &LC, const Value *R0,
+ CmpInst::Predicate RPred, const APInt &RC) {
ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC);
ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC);
+
+ if (L0 == R0)
+ ; // noop
+ // Example: icmp eq X, 3 --> icmp sgt trunc(X), 2
+ else if (match(R0, m_Trunc(m_Specific(L0))))
+ DomCR = DomCR.truncate(RC.getBitWidth());
+ // Example: icmp slt trunc(X), 3 --> icmp ne X, 3
+ else if (match(L0, m_Trunc(m_Specific(R0)))) {
+ // Try to prove by negation
+ DomCR = DomCR.inverse();
+ CR = CR.inverse();
+ std::swap(DomCR, CR);
----------------
goldsteinn wrote:
err I mean following logic doesn't seem to care about `DomCR` vs `CR`.
But guess swap is clearer.
https://github.com/llvm/llvm-project/pull/69829
More information about the llvm-commits
mailing list