From: Chad Rosier Date: Thu, 5 May 2016 17:41:19 +0000 (+0000) Subject: [ValueTracking] Early exit when further analysis won't be fruitful. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=668a46a0d3fa0d020769777c7b2ac8f7dd5e9cd4;p=llvm [ValueTracking] Early exit when further analysis won't be fruitful. This should have NFC in the context of codegen, but may have positive implications on compile-time. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268651 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index fa5c491554e..e354cb89129 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -3922,19 +3922,24 @@ isImpliedCondOperands(CmpInst::Predicate Pred, Value *ALHS, Value *ARHS, } } +/// Return true if the operands of the two compares match. IsSwappedOps is true +/// when the operands match, but are swapped. +static bool isMatchingOps(Value *ALHS, Value *ARHS, Value *BLHS, Value *BRHS, + bool &IsSwappedOps) { + + bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS); + IsSwappedOps = (ALHS == BRHS && ARHS == BLHS); + return IsMatchingOps || IsSwappedOps; +} + /// Return true if "icmp1 APred ALHS ARHS" implies "icmp2 BPred BLHS BRHS" is /// true. Return false if "icmp1 APred ALHS ARHS" implies "icmp2 BPred BLHS /// BRHS" is false. Otherwise, return None if we can't infer anything. static Optional isImpliedCondMatchingOperands(CmpInst::Predicate APred, Value *ALHS, Value *ARHS, CmpInst::Predicate BPred, - Value *BLHS, Value *BRHS) { - // The operands of the two compares must match. - bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS); - bool IsSwappedOps = (ALHS == BRHS && ARHS == BLHS); - if (!IsMatchingOps && !IsSwappedOps) - return None; - + Value *BLHS, Value *BRHS, + bool IsSwappedOps) { // Canonicalize the operands so they're matching. if (IsSwappedOps) { std::swap(BLHS, BRHS); @@ -4001,17 +4006,27 @@ Optional llvm::isImpliedCondition(Value *LHS, Value *RHS, if (InvertAPred) APred = CmpInst::getInversePredicate(APred); - Optional Implication = - isImpliedCondMatchingOperands(APred, ALHS, ARHS, BPred, BLHS, BRHS); - if (Implication) - return Implication; + // Can we infer anything when the two compares have matching operands? + bool IsSwappedOps; + if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, IsSwappedOps)) { + if (Optional Implication = isImpliedCondMatchingOperands( + APred, ALHS, ARHS, BPred, BLHS, BRHS, IsSwappedOps)) + return Implication; + // No amount of additional analysis will infer the second condition, so + // early exit. + return None; + } + // Can we infer anything when the LHS operands match and the RHS operands are + // constants (not necessarily matching)? if (ALHS == BLHS && isa(ARHS) && isa(BRHS)) { - Implication = - isImpliedCondMatchingImmOperands(APred, ALHS, cast(ARHS), - BPred, BLHS, cast(BRHS)); - if (Implication) + if (Optional Implication = isImpliedCondMatchingImmOperands( + APred, ALHS, cast(ARHS), BPred, BLHS, + cast(BRHS))) return Implication; + // No amount of additional analysis will infer the second condition, so + // early exit. + return None; } if (APred == BPred)