From 8f12eba78d7ef7cd849af8e6658e6a20c6f6f772 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Wed, 9 Mar 2016 21:31:47 +0000 Subject: [PATCH] [ValueTracking] Extract isKnownPositive [NFCI] Extract out a generic interface from a recently landed patch and document a TODO in case compile time becomes a problem. llvm-svn: 263062 --- llvm/include/llvm/Analysis/ValueTracking.h | 7 +++++++ llvm/lib/Analysis/ValueTracking.cpp | 12 ++++++++++++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h index b5f1204..6357e1f 100644 --- a/llvm/include/llvm/Analysis/ValueTracking.h +++ b/llvm/include/llvm/Analysis/ValueTracking.h @@ -92,6 +92,13 @@ namespace llvm { const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr); + /// Returns true if the given value is known be positive (i.e. non-negative + /// and non-zero). + bool isKnownPositive(Value *V, const DataLayout &DL, unsigned Depth = 0, + AssumptionCache *AC = nullptr, + const Instruction *CxtI = nullptr, + const DominatorTree *DT = nullptr); + /// isKnownNonEqual - Return true if the given values are known to be /// non-equal when defined. Supports scalar integer types only. bool isKnownNonEqual(Value *V1, Value *V2, const DataLayout &DL, diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 098da0a..79529e5 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -182,6 +182,18 @@ bool llvm::isKnownNonNegative(Value *V, const DataLayout &DL, unsigned Depth, return NonNegative; } +bool llvm::isKnownPositive(Value *V, const DataLayout &DL, unsigned Depth, + AssumptionCache *AC, const Instruction *CxtI, + const DominatorTree *DT) { + if (auto *CI = dyn_cast(V)) + return CI->getValue().isStrictlyPositive(); + + // TODO: We'd doing two recursive queries here. We should factor this such + // that only a single query is needed. + return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT) && + isKnownNonZero(V, DL, Depth, AC, CxtI, DT); +} + static bool isKnownNonEqual(Value *V1, Value *V2, const Query &Q); bool llvm::isKnownNonEqual(Value *V1, Value *V2, const DataLayout &DL, diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 703506d..7401cb6 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3178,9 +3178,9 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { if (auto *SI = dyn_cast(Op0)) { SelectPatternResult SPR = matchSelectPattern(SI, A, B); if (SPR.Flavor == SPF_SMIN) { - if (isKnownNonNegative(A, DL) && isKnownNonZero(A, DL)) + if (isKnownPositive(A, DL)) return new ICmpInst(I.getPredicate(), B, CI); - if (isKnownNonNegative(B, DL) && isKnownNonZero(B, DL)) + if (isKnownPositive(B, DL)) return new ICmpInst(I.getPredicate(), A, CI); } } -- 2.7.4