Skip to content

Commit

Permalink
[ValueTracking] Extract isKnownPositive [NFCI]
Browse files Browse the repository at this point in the history
Extract out a generic interface from a recently landed patch and document a TODO in case compile time becomes a problem.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263062 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
preames committed Mar 9, 2016
1 parent 37f4f50 commit 352b004
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions include/llvm/Analysis/ValueTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConstantInt>(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,
Expand Down
4 changes: 2 additions & 2 deletions lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3178,9 +3178,9 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
if (auto *SI = dyn_cast<SelectInst>(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);
}
}
Expand Down

0 comments on commit 352b004

Please sign in to comment.