From 2e19f593040cfa77aec58b0816bff0e3235799e3 Mon Sep 17 00:00:00 2001 From: Artur Pilipenko Date: Tue, 2 Aug 2016 16:20:48 +0000 Subject: [PATCH] [LVI] NFC. Sink a condition type check from the caller down to getValueFromCondition This is a preparatory refactoring to support conditions other than ICmpInst. llvm-svn: 277479 --- llvm/lib/Analysis/LazyValueInfo.cpp | 64 +++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp index 8013e236..2f5fbec 100644 --- a/llvm/lib/Analysis/LazyValueInfo.cpp +++ b/llvm/lib/Analysis/LazyValueInfo.cpp @@ -859,7 +859,7 @@ bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV, return true; } -static bool getValueFromCondition(Value *Val, ICmpInst *ICI, +static bool getValueFromCondition(Value *Val, Value *Cond, LVILatticeVal &Result, bool isTrueDest = true); @@ -879,12 +879,9 @@ void LazyValueInfoCache::intersectAssumeBlockValueConstantRange(Value *Val, if (!isValidAssumeForContext(I, BBI, DT)) continue; - Value *C = I->getArgOperand(0); - if (ICmpInst *ICI = dyn_cast(C)) { - LVILatticeVal Result; - if (getValueFromCondition(Val, ICI, Result)) - BBLV = intersect(BBLV, Result); - } + LVILatticeVal Result; + if (getValueFromCondition(Val, I->getArgOperand(0), Result)) + BBLV = intersect(BBLV, Result); } } @@ -954,25 +951,25 @@ bool LazyValueInfoCache::solveBlockValueSelect(LVILatticeVal &BBLV, // Can we constrain the facts about the true and false values by using the // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5). // TODO: We could potentially refine an overdefined true value above. - if (auto *ICI = dyn_cast(SI->getCondition())) { - LVILatticeVal TrueValTaken, FalseValTaken; - if (!getValueFromCondition(SI->getTrueValue(), ICI, TrueValTaken, true)) - TrueValTaken.markOverdefined(); - if (!getValueFromCondition(SI->getFalseValue(), ICI, FalseValTaken, false)) - FalseValTaken.markOverdefined(); - - TrueVal = intersect(TrueVal, TrueValTaken); - FalseVal = intersect(FalseVal, FalseValTaken); - - - // Handle clamp idioms such as: - // %24 = constantrange<0, 17> - // %39 = icmp eq i32 %24, 0 - // %40 = add i32 %24, -1 - // %siv.next = select i1 %39, i32 16, i32 %40 - // %siv.next = constantrange<0, 17> not <-1, 17> - // In general, this can handle any clamp idiom which tests the edge - // condition via an equality or inequality. + Value *Cond = SI->getCondition(); + LVILatticeVal TrueValTaken, FalseValTaken; + if (!getValueFromCondition(SI->getTrueValue(), Cond, TrueValTaken, true)) + TrueValTaken.markOverdefined(); + if (!getValueFromCondition(SI->getFalseValue(), Cond, FalseValTaken, false)) + FalseValTaken.markOverdefined(); + + TrueVal = intersect(TrueVal, TrueValTaken); + FalseVal = intersect(FalseVal, FalseValTaken); + + // Handle clamp idioms such as: + // %24 = constantrange<0, 17> + // %39 = icmp eq i32 %24, 0 + // %40 = add i32 %24, -1 + // %siv.next = select i1 %39, i32 16, i32 %40 + // %siv.next = constantrange<0, 17> not <-1, 17> + // In general, this can handle any clamp idiom which tests the edge + // condition via an equality or inequality. + if (auto *ICI = dyn_cast(Cond)) { ICmpInst::Predicate Pred = ICI->getPredicate(); Value *A = ICI->getOperand(0); if (ConstantInt *CIBase = dyn_cast(ICI->getOperand(1))) { @@ -1177,9 +1174,15 @@ bool LazyValueInfoCache::solveBlockValueBinaryOp(LVILatticeVal &BBLV, return true; } -bool getValueFromCondition(Value *Val, ICmpInst *ICI, LVILatticeVal &Result, +bool getValueFromCondition(Value *Val, Value *Cond, LVILatticeVal &Result, bool isTrueDest) { - assert(ICI && "precondition"); + assert(Cond && "precondition"); + + // For now we only support ICmpInst conditions + ICmpInst *ICI = dyn_cast(Cond); + if (!ICI) + return false; + if (isa(ICI->getOperand(1))) { if (ICI->isEquality() && ICI->getOperand(0) == Val) { // We know that V has the RHS constant if this is a true SETEQ or @@ -1245,9 +1248,8 @@ static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, // If the condition of the branch is an equality comparison, we may be // able to infer the value. - if (ICmpInst *ICI = dyn_cast(BI->getCondition())) - if (getValueFromCondition(Val, ICI, Result, isTrueDest)) - return true; + if (getValueFromCondition(Val, BI->getCondition(), Result, isTrueDest)) + return true; } } -- 2.7.4