diff options
author | Ramkumar Ramachandra <ramkumar.ramachandra@codasip.com> | 2025-01-10 14:26:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-10 14:26:49 +0000 |
commit | cfee344dda7394631f2177a15e56cfeee1d61fc4 (patch) | |
tree | 548449441903b8f67192b34915969f6b42718599 /llvm/lib/Analysis/ValueTracking.cpp | |
parent | 7b0536794349734c8862fc140808e4e5a2ab8f8d (diff) | |
download | llvm-cfee344dda7394631f2177a15e56cfeee1d61fc4.zip llvm-cfee344dda7394631f2177a15e56cfeee1d61fc4.tar.gz llvm-cfee344dda7394631f2177a15e56cfeee1d61fc4.tar.bz2 |
VT: teach implied-cond-cr about samesign (#122447)
Teach isImpliedCondCommonOperandWithCR about samesign, noting that the
only case we need to handle is when exactly one of the icmps have
samesign.
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index db24414..9a61b36 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -9388,17 +9388,32 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred, /// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true. /// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false. /// Otherwise, return std::nullopt if we can't infer anything. -static std::optional<bool> isImpliedCondCommonOperandWithCR( - CmpInst::Predicate LPred, const ConstantRange &LCR, - CmpInst::Predicate RPred, const ConstantRange &RCR) { - ConstantRange DomCR = ConstantRange::makeAllowedICmpRegion(LPred, LCR); - // If all true values for lhs and true for rhs, lhs implies rhs - if (DomCR.icmp(RPred, RCR)) - return true; +static std::optional<bool> +isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, + CmpPredicate RPred, const ConstantRange &RCR) { + auto CRImpliesPred = [&](ConstantRange CR, + CmpInst::Predicate Pred) -> std::optional<bool> { + // If all true values for lhs and true for rhs, lhs implies rhs + if (CR.icmp(Pred, RCR)) + return true; - // If there is no overlap, lhs implies not rhs - if (DomCR.icmp(CmpInst::getInversePredicate(RPred), RCR)) - return false; + // If there is no overlap, lhs implies not rhs + if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR)) + return false; + + return std::nullopt; + }; + if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR), + RPred)) + return Res; + if (LPred.hasSameSign() ^ RPred.hasSameSign()) { + LPred = LPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(LPred) + : static_cast<CmpInst::Predicate>(LPred); + RPred = RPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(RPred) + : static_cast<CmpInst::Predicate>(RPred); + return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR), + RPred); + } return std::nullopt; } |