enum LatticeValueTy {
/// This Value has no known value yet.
undefined,
-
+
/// This Value has a specific constant value.
constant,
-
+
/// This Value is known to not have the specified value.
notconstant,
/// This value is not known to be constant, and we know that it has a value.
overdefined
};
-
+
/// Val: This stores the current lattice value along with the Constant* for
/// the constant if this is a 'constant' or 'notconstant' value.
LatticeValueTy Tag;
Constant *Val;
ConstantRange Range;
-
+
public:
LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {}
Res.markConstantRange(CR);
return Res;
}
-
+
bool isUndefined() const { return Tag == undefined; }
bool isConstant() const { return Tag == constant; }
bool isNotConstant() const { return Tag == notconstant; }
bool isConstantRange() const { return Tag == constantrange; }
bool isOverdefined() const { return Tag == overdefined; }
-
+
Constant *getConstant() const {
assert(isConstant() && "Cannot get the constant of a non-constant!");
return Val;
}
-
+
Constant *getNotConstant() const {
assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
return Val;
}
-
+
ConstantRange getConstantRange() const {
assert(isConstantRange() &&
"Cannot get the constant-range of a non-constant-range!");
return Range;
}
-
+
/// Return true if this is a change in status.
bool markOverdefined() {
if (isOverdefined())
Val = V;
return true;
}
-
+
/// Return true if this is a change in status.
bool markNotConstant(Constant *V) {
assert(V && "Marking constant with NULL");
Val = V;
return true;
}
-
+
/// Return true if this is a change in status.
bool markConstantRange(const ConstantRange NewR) {
if (isConstantRange()) {
if (NewR.isEmptySet())
return markOverdefined();
-
+
bool changed = Range != NewR;
Range = NewR;
return changed;
}
-
+
assert(isUndefined());
if (NewR.isEmptySet())
return markOverdefined();
-
+
Tag = constantrange;
Range = NewR;
return true;
}
-
+
/// Merge the specified lattice value into this one, updating this
/// one and returning true if anything changed.
bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) {
return markConstantRange(NewR);
}
};
-
+
} // end anonymous namespace.
namespace llvm {
class LazyValueInfoCache;
struct LVIValueHandle : public CallbackVH {
LazyValueInfoCache *Parent;
-
+
LVIValueHandle(Value *V, LazyValueInfoCache *P)
: CallbackVH(V), Parent(P) { }
};
}
-namespace {
+namespace {
/// This is the cache kept by LazyValueInfo which
/// maintains information about queries across the clients' queries.
class LazyValueInfoCache {
/// This is all of the cached information for all values,
/// mapped from Value* to key information.
std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
-
+
/// This tracks, on a per-block basis, the set of values that are
/// over-defined at the end of that block. This is required
/// for cache updating.
Instruction *BBI);
void solve();
-
+
ValueCacheEntryTy &lookup(Value *V) {
return ValueCache[LVIValueHandle(V, this)];
}
/// value for the specified Value* that is true on the specified edge.
LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
Instruction *CxtI = nullptr);
-
+
/// This is the update interface to inform the cache that an edge from
/// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
-
+
/// This is part of the update interface to inform the cache
/// that a block has been deleted.
void eraseBlock(BasicBlock *BB);
-
+
/// clear - Empty the cache.
void clear() {
SeenBlocks.clear();
void LVIValueHandle::deleted() {
typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy;
-
+
SmallVector<OverDefinedPairTy, 4> ToErase;
for (const OverDefinedPairTy &P : Parent->OverDefinedCache)
if (P.second == getValPtr())
ToErase.push_back(P);
for (const OverDefinedPairTy &P : ToErase)
Parent->OverDefinedCache.erase(P);
-
+
// This erasure deallocates *this, so it MUST happen after we're done
// using any and all members of *this.
Parent->ValueCache.erase(*this);
// Hold off inserting this value into the Cache in case we have to return
// false and come back later.
LVILatticeVal Res;
-
+
Instruction *BBI = dyn_cast<Instruction>(Val);
if (!BBI || BBI->getParent() != BB) {
if (!solveBlockValueNonLocal(Res, Val, BB))
PointerType *PTy = cast<PointerType>(Val->getType());
Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy));
}
-
+
BBLV = Result;
return true;
}
BBLV = Result;
return true;
}
-
+
bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
PHINode *PN, BasicBlock *BB) {
LVILatticeVal Result; // Start Undefined.
if (Result.isOverdefined()) {
DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because of pred.\n");
-
+
BBLV = Result;
return true;
}
BBLV.markOverdefined();
return true;
}
-
+
ConstantRange LHSRange = LHSVal.getConstantRange();
ConstantRange RHSRange(1);
IntegerType *ResultTy = cast<IntegerType>(BBI->getType());
case Instruction::Or:
Result.markConstantRange(LHSRange.binaryOr(RHSRange));
break;
-
+
// Unhandled instructions are overdefined.
default:
DEBUG(dbgs() << " compute BB '" << BB->getName()
Result.markOverdefined();
break;
}
-
+
BBLV = Result;
return true;
}
/// Val is not constrained on the edge.
static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
BasicBlock *BBTo, LVILatticeVal &Result) {
- // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
+ // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
// know that v != 0.
if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
// If this is a conditional branch and only one successor goes to BBTo, then
bool isTrueDest = BI->getSuccessor(0) == BBTo;
assert(BI->getSuccessor(!isTrueDest) == BBTo &&
"BBTo isn't a successor of BBFrom");
-
+
// If V is the condition of the branch itself, then we know exactly what
// it is.
if (BI->getCondition() == Val) {
Type::getInt1Ty(Val->getContext()), isTrueDest));
return true;
}
-
+
// If the condition of the branch is an equality comparison, we may be
// able to infer the value.
if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
Instruction *CxtI) {
DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
<< BB->getName() << "'\n");
-
+
assert(BlockValueStack.empty() && BlockValueSet.empty());
pushBlockValue(std::make_pair(BB, V));
Instruction *CxtI) {
DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
<< FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
-
+
LVILatticeVal Result;
if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) {
solve();
void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
BasicBlock *NewSucc) {
- // When an edge in the graph has been threaded, values that we could not
- // determine a value for before (i.e. were marked overdefined) may be possible
- // to solve now. We do NOT try to proactively update these values. Instead,
- // we clear their entries from the cache, and allow lazy updating to recompute
- // them when needed.
-
+ // When an edge in the graph has been threaded, values that we could not
+ // determine a value for before (i.e. were marked overdefined) may be
+ // possible to solve now. We do NOT try to proactively update these values.
+ // Instead, we clear their entries from the cache, and allow lazy updating to
+ // recompute them when needed.
+
// The updating process is fairly simple: we need to drop cached info
// for all values that were marked overdefined in OldSucc, and for those same
// values in any successor of OldSucc (except NewSucc) in which they were
// also marked overdefined.
std::vector<BasicBlock*> worklist;
worklist.push_back(OldSucc);
-
+
DenseSet<Value*> ClearSet;
for (OverDefinedPairTy &P : OverDefinedCache)
if (P.first == OldSucc)
ClearSet.insert(P.second);
-
+
// Use a worklist to perform a depth-first search of OldSucc's successors.
// NOTE: We do not need a visited list since any blocks we have already
// visited will have had their overdefined markers cleared already, and we
while (!worklist.empty()) {
BasicBlock *ToUpdate = worklist.back();
worklist.pop_back();
-
+
// Skip blocks only accessible through NewSucc.
if (ToUpdate == NewSucc) continue;
-
+
bool changed = false;
for (Value *V : ClearSet) {
// If a value was marked overdefined in OldSucc, and is here too...
Entry.erase(CI);
OverDefinedCache.erase(OI);
- // If we removed anything, then we potentially need to update
+ // If we removed anything, then we potentially need to update
// blocks successors too.
changed = true;
}
if (!changed) continue;
-
+
worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
}
}
}
/// Determine whether the specified value is known to be a
-/// constant on the specified edge. Return null if not.
+/// constant on the specified edge. Return null if not.
Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
BasicBlock *ToBB,
Instruction *CxtI) {
return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
return LazyValueInfo::Unknown;
}
-
+
if (Result.isConstantRange()) {
ConstantInt *CI = dyn_cast<ConstantInt>(C);
if (!CI) return LazyValueInfo::Unknown;
-
+
ConstantRange CR = Result.getConstantRange();
if (Pred == ICmpInst::ICMP_EQ) {
if (!CR.contains(CI->getValue()))
return LazyValueInfo::False;
-
+
if (CR.isSingleElement() && CR.contains(CI->getValue()))
return LazyValueInfo::True;
} else if (Pred == ICmpInst::ICMP_NE) {
if (!CR.contains(CI->getValue()))
return LazyValueInfo::True;
-
+
if (CR.isSingleElement() && CR.contains(CI->getValue()))
return LazyValueInfo::False;
}
-
+
// Handle more complex predicates.
ConstantRange TrueValues =
ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue());
return LazyValueInfo::False;
return LazyValueInfo::Unknown;
}
-
+
if (Result.isNotConstant()) {
// If this is an equality comparison, we can try to fold it knowing that
// "V != C1".
}
return LazyValueInfo::Unknown;
}
-
+
return LazyValueInfo::Unknown;
}
return Ret;
// TODO: Move this logic inside getValueAt so that it can be cached rather
- // than re-queried on each call. This would also allow us to merge the
- // underlying lattice values to get more information
+ // than re-queried on each call. This would also allow us to merge the
+ // underlying lattice values to get more information.
if (CxtI) {
// For a comparison where the V is outside this block, it's possible
- // that we've branched on it before. Look to see if the value is known
+ // that we've branched on it before. Look to see if the value is known
// on all incoming edges.
BasicBlock *BB = CxtI->getParent();
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
(!isa<Instruction>(V) ||
cast<Instruction>(V)->getParent() != BB)) {
// For predecessor edge, determine if the comparison is true or false
- // on that edge. If they're all true or all false, we can conclude
+ // on that edge. If they're all true or all false, we can conclude
// the value of the comparison in this block.
Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
if (Baseline != Unknown) {