15 #define DEBUG_TYPE "lazy-value-info"
35 using namespace PatternMatch;
39 "Lazy Value Information Analysis",
false,
true)
85 LVILatticeVal() : Tag(undefined), Val(0), Range(1,
true) {}
89 if (!isa<UndefValue>(
C))
93 static LVILatticeVal getNot(
Constant *
C) {
95 if (!isa<UndefValue>(C))
96 Res.markNotConstant(C);
101 Res.markConstantRange(CR);
105 bool isUndefined()
const {
return Tag == undefined; }
106 bool isConstant()
const {
return Tag == constant; }
107 bool isNotConstant()
const {
return Tag == notconstant; }
108 bool isConstantRange()
const {
return Tag == constantrange; }
109 bool isOverdefined()
const {
return Tag == overdefined; }
112 assert(isConstant() &&
"Cannot get the constant of a non-constant!");
117 assert(isNotConstant() &&
"Cannot get the constant of a non-notconstant!");
122 assert(isConstantRange() &&
123 "Cannot get the constant-range of a non-constant-range!");
128 bool markOverdefined() {
137 assert(V &&
"Marking constant with NULL");
140 if (isa<UndefValue>(V))
143 assert((!isConstant() || getConstant() == V) &&
144 "Marking constant with different value");
145 assert(isUndefined());
153 assert(V &&
"Marking constant with NULL");
155 return markConstantRange(
ConstantRange(CI->getValue()+1, CI->getValue()));
156 if (isa<UndefValue>(V))
159 assert((!isConstant() || getConstant() != V) &&
160 "Marking constant !constant with same value");
161 assert((!isNotConstant() || getNotConstant() == V) &&
162 "Marking !constant with different value");
163 assert(isUndefined() || isConstant());
171 if (isConstantRange()) {
173 return markOverdefined();
175 bool changed = Range != NewR;
180 assert(isUndefined());
182 return markOverdefined();
191 bool mergeIn(
const LVILatticeVal &RHS) {
192 if (RHS.isUndefined() || isOverdefined())
return false;
193 if (RHS.isOverdefined())
return markOverdefined();
203 if (RHS.isConstant()) {
206 return markOverdefined();
209 if (RHS.isNotConstant()) {
211 return markOverdefined();
219 RHS.getNotConstant())))
221 return markNotConstant(RHS.getNotConstant());
223 return markOverdefined();
231 return markOverdefined();
234 if (isNotConstant()) {
235 if (RHS.isConstant()) {
237 return markOverdefined();
249 return markOverdefined();
252 if (RHS.isNotConstant()) {
255 return markOverdefined();
258 return markOverdefined();
261 assert(isConstantRange() &&
"New LVILattice type?");
262 if (!RHS.isConstantRange())
263 return markOverdefined();
267 return markOverdefined();
268 return markConstantRange(NewR);
278 if (Val.isUndefined())
279 return OS <<
"undefined";
280 if (Val.isOverdefined())
281 return OS <<
"overdefined";
283 if (Val.isNotConstant())
284 return OS <<
"notconstant<" << *Val.getNotConstant() <<
'>';
285 else if (Val.isConstantRange())
286 return OS <<
"constantrange<" << Val.getConstantRange().getLower() <<
", "
287 << Val.getConstantRange().getUpper() <<
'>';
288 return OS <<
"constant<" << *Val.getConstant() <<
'>';
299 class LazyValueInfoCache;
301 LazyValueInfoCache *Parent;
303 LVIValueHandle(
Value *V, LazyValueInfoCache *
P)
307 void allUsesReplacedWith(
Value *V) {
316 class LazyValueInfoCache {
320 typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy;
324 std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache;
329 typedef std::pair<AssertingVH<BasicBlock>,
Value*> OverDefinedPairTy;
339 std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack;
341 friend struct LVIValueHandle;
345 struct OverDefinedCacheUpdater {
346 LazyValueInfoCache *Parent;
352 LazyValueInfoCache *
P)
353 : Parent(P), Val(V), BB(B), BBLV(LV) { }
355 bool markResult(
bool changed) {
356 if (changed && BBLV.isOverdefined())
357 Parent->OverDefinedCache.insert(std::make_pair(BB, Val));
366 LVILatticeVal &Result);
373 bool solveBlockValueNonLocal(LVILatticeVal &BBLV,
375 bool solveBlockValuePHINode(LVILatticeVal &BBLV,
377 bool solveBlockValueConstantRange(LVILatticeVal &BBLV,
383 return ValueCache[LVIValueHandle(V,
this)];
408 OverDefinedCache.clear();
413 void LVIValueHandle::deleted() {
414 typedef std::pair<AssertingVH<BasicBlock>,
Value*> OverDefinedPairTy;
418 I = Parent->OverDefinedCache.begin(),
419 E = Parent->OverDefinedCache.end();
421 if (
I->second == getValPtr())
426 E = ToErase.
end();
I != E; ++
I)
427 Parent->OverDefinedCache.erase(*
I);
431 Parent->ValueCache.erase(*
this);
434 void LazyValueInfoCache::eraseBlock(
BasicBlock *BB) {
437 if (I == SeenBlocks.
end())
443 E = OverDefinedCache.end(); I != E; ++
I) {
449 E = ToErase.
end(); I != E; ++
I)
450 OverDefinedCache.erase(*I);
452 for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator
453 I = ValueCache.
begin(), E = ValueCache.end(); I != E; ++
I)
457 void LazyValueInfoCache::solve() {
458 while (!BlockValueStack.empty()) {
459 std::pair<BasicBlock*, Value*> &e = BlockValueStack.top();
460 if (solveBlockValue(e.second, e.first)) {
461 assert(BlockValueStack.top() == e);
462 BlockValueStack.pop();
469 if (isa<Constant>(Val))
472 LVIValueHandle ValHandle(Val,
this);
473 std::map<LVIValueHandle, ValueCacheEntryTy>::iterator I =
474 ValueCache.
find(ValHandle);
475 if (I == ValueCache.end())
return false;
476 return I->second.count(BB);
479 LVILatticeVal LazyValueInfoCache::getBlockValue(
Value *Val,
BasicBlock *BB) {
482 return LVILatticeVal::get(
VC);
484 SeenBlocks.insert(BB);
489 if (isa<Constant>(Val))
492 ValueCacheEntryTy &Cache =
lookup(Val);
493 SeenBlocks.insert(BB);
494 LVILatticeVal &BBLV = Cache[BB];
500 OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV,
this);
503 if (!BBLV.isUndefined()) {
509 ODCacheUpdater.markResult(
false);
516 BBLV.markOverdefined();
519 if (BBI == 0 || BBI->
getParent() != BB) {
520 return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB));
523 if (
PHINode *PN = dyn_cast<PHINode>(BBI)) {
524 return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB));
527 if (
AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) {
529 return ODCacheUpdater.markResult(
true);
534 LVILatticeVal Result;
535 if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) ||
538 <<
"' - overdefined because inst def found.\n");
539 BBLV.markOverdefined();
540 return ODCacheUpdater.markResult(
true);
546 if (BO && !isa<ConstantInt>(BO->
getOperand(1))) {
548 <<
"' - overdefined because inst def found.\n");
550 BBLV.markOverdefined();
551 return ODCacheUpdater.markResult(
true);
554 return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB));
558 if (
LoadInst *L = dyn_cast<LoadInst>(I)) {
559 return L->getPointerAddressSpace() == 0 &&
562 if (
StoreInst *S = dyn_cast<StoreInst>(I)) {
563 return S->getPointerAddressSpace() == 0 &&
567 if (
MI->isVolatile())
return false;
571 if (!Len || Len->
isZero())
return false;
573 if (
MI->getDestAddressSpace() == 0)
577 if (MTI->getSourceAddressSpace() == 0)
584 bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV,
586 LVILatticeVal Result;
590 bool NotNull =
false;
613 assert(isa<Argument>(Val) &&
"Unknown live-in to the entry block");
618 Result.markOverdefined();
626 bool EdgesMissing =
false;
628 LVILatticeVal EdgeResult;
629 EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult);
633 Result.mergeIn(EdgeResult);
637 if (Result.isOverdefined()) {
639 <<
"' - overdefined because of pred.\n");
655 assert(!Result.isOverdefined());
660 bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV,
662 LVILatticeVal Result;
666 bool EdgesMissing =
false;
670 LVILatticeVal EdgeResult;
671 EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult);
675 Result.mergeIn(EdgeResult);
679 if (Result.isOverdefined()) {
681 <<
"' - overdefined because of pred.\n");
691 assert(!Result.isOverdefined() &&
"Possible PHI in entry block?");
696 bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
701 BlockValueStack.push(std::make_pair(BB, BBI->
getOperand(0)));
705 LVILatticeVal LHSVal = getBlockValue(BBI->
getOperand(0), BB);
706 if (!LHSVal.isConstantRange()) {
707 BBLV.markOverdefined();
714 if (isa<BinaryOperator>(BBI)) {
718 BBLV.markOverdefined();
726 LVILatticeVal Result;
728 case Instruction::Add:
729 Result.markConstantRange(LHSRange.
add(RHSRange));
731 case Instruction::Sub:
732 Result.markConstantRange(LHSRange.
sub(RHSRange));
734 case Instruction::Mul:
735 Result.markConstantRange(LHSRange.
multiply(RHSRange));
737 case Instruction::UDiv:
738 Result.markConstantRange(LHSRange.
udiv(RHSRange));
740 case Instruction::Shl:
741 Result.markConstantRange(LHSRange.
shl(RHSRange));
743 case Instruction::LShr:
744 Result.markConstantRange(LHSRange.
lshr(RHSRange));
746 case Instruction::Trunc:
749 case Instruction::SExt:
752 case Instruction::ZExt:
755 case Instruction::BitCast:
756 Result.markConstantRange(LHSRange);
759 Result.markConstantRange(LHSRange.
binaryAnd(RHSRange));
762 Result.markConstantRange(LHSRange.
binaryOr(RHSRange));
768 <<
"' - overdefined because inst def found.\n");
769 Result.markOverdefined();
786 if (BI->isConditional() &&
787 BI->getSuccessor(0) != BI->getSuccessor(1)) {
788 bool isTrueDest = BI->getSuccessor(0) == BBTo;
789 assert(BI->getSuccessor(!isTrueDest) == BBTo &&
790 "BBTo isn't a successor of BBFrom");
794 if (BI->getCondition() == Val) {
803 if (ICI && isa<Constant>(ICI->
getOperand(1))) {
808 Result = LVILatticeVal::get(cast<Constant>(ICI->
getOperand(1)));
810 Result = LVILatticeVal::getNot(cast<Constant>(ICI->
getOperand(1)));
822 if (CI && (ICI->
getOperand(0) == Val || NegOffset)) {
832 if (!isTrueDest) TrueValues = TrueValues.
inverse();
834 Result = LVILatticeVal::getRange(TrueValues);
844 if (SI->getCondition() != Val)
847 bool DefaultCase = SI->getDefaultDest() == BBTo;
857 if (i.getCaseSuccessor() != BBTo)
859 }
else if (i.getCaseSuccessor() == BBTo)
860 EdgesVals = EdgesVals.
unionWith(EdgeVal);
862 Result = LVILatticeVal::getRange(EdgesVals);
874 Result = LVILatticeVal::get(
VC);
879 if (!Result.isConstantRange() ||
880 Result.getConstantRange().getSingleElement())
886 if (!hasBlockValue(Val, BBFrom)) {
887 BlockValueStack.push(std::make_pair(BBFrom, Val));
892 LVILatticeVal
InBlock = getBlockValue(Val, BBFrom);
893 if (!InBlock.isConstantRange())
897 Result.getConstantRange().intersectWith(InBlock.getConstantRange());
898 Result = LVILatticeVal::getRange(Range);
902 if (!hasBlockValue(Val, BBFrom)) {
903 BlockValueStack.push(std::make_pair(BBFrom, Val));
908 Result = getBlockValue(Val, BBFrom);
912 LVILatticeVal LazyValueInfoCache::getValueInBlock(
Value *V,
BasicBlock *BB) {
913 DEBUG(
dbgs() <<
"LVI Getting block end value " << *V <<
" at '"
916 BlockValueStack.push(std::make_pair(BB, V));
918 LVILatticeVal Result = getBlockValue(V, BB);
920 DEBUG(
dbgs() <<
" Result = " << Result <<
"\n");
924 LVILatticeVal LazyValueInfoCache::
926 DEBUG(
dbgs() <<
"LVI Getting edge value " << *V <<
" from '"
929 LVILatticeVal Result;
930 if (!getEdgeValue(V, FromBB, ToBB, Result)) {
932 bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result);
934 assert(WasFastQuery &&
"More work to do after problem solved?");
937 DEBUG(
dbgs() <<
" Result = " << Result <<
"\n");
953 std::vector<BasicBlock*> worklist;
954 worklist.push_back(OldSucc);
958 E = OverDefinedCache.end(); I != E; ++
I) {
959 if (I->first == OldSucc)
960 ClearSet.
insert(I->second);
967 while (!worklist.empty()) {
972 if (ToUpdate == NewSucc)
continue;
974 bool changed =
false;
979 OverDefinedCache.find(std::make_pair(ToUpdate, *I));
980 if (OI == OverDefinedCache.end())
continue;
983 ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I,
this)];
984 ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate);
986 assert(CI != Entry.end() &&
"Couldn't find entry to update?");
988 OverDefinedCache.erase(OI);
995 if (!changed)
continue;
1008 PImpl =
new LazyValueInfoCache();
1009 return *
static_cast<LazyValueInfoCache*
>(PImpl);
1016 TD = getAnalysisIfAvailable<DataLayout>();
1017 TLI = &getAnalysis<TargetLibraryInfo>();
1037 LVILatticeVal Result =
getCache(PImpl).getValueInBlock(V, BB);
1039 if (Result.isConstant())
1040 return Result.getConstant();
1041 if (Result.isConstantRange()) {
1053 LVILatticeVal Result =
getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
1055 if (Result.isConstant())
1056 return Result.getConstant();
1057 if (Result.isConstantRange()) {
1071 LVILatticeVal Result =
getCache(PImpl).getValueOnEdge(V, FromBB, ToBB);
1075 if (Result.isConstant()) {
1078 if (
ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
1079 return ResCI->isZero() ? False : True;
1083 if (Result.isConstantRange()) {
1112 if (Result.isNotConstant()) {
1118 Result.getNotConstant(),
C,
TD,
1125 Result.getNotConstant(),
C,
TD,
1138 if (PImpl)
getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
1142 if (PImpl)
getCache(PImpl).eraseBlock(BB);
void push_back(const T &Elt)
static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr)
static IntegerType * getInt1Ty(LLVMContext &C)
const Instruction & back() const
const APInt * getSingleElement() const
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI=0)
Constant * getConstant(Value *V, BasicBlock *BB)
static bool isEquality(Predicate P)
const Function * getParent() const
Return the enclosing method, or null if none.
bool isSingleElement() const
static LazyValueInfoCache & getCache(void *&PImpl)
getCache - This lazily constructs the LazyValueInfoCache.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
StringRef getName() const
bool match(Val *V, const Pattern &P)
Value * GetUnderlyingObject(Value *V, const DataLayout *TD=0, unsigned MaxLookup=6)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
ConstantRange truncate(uint32_t BitWidth) const
bool erase(const ValueT &V)
const APInt & getValue() const
Return the constant's value.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
lazy value Lazy Value Information Analysis
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, BasicBlock *FromBB, BasicBlock *ToBB)
ConstantRange signExtend(uint32_t BitWidth) const
ConstantRange multiply(const ConstantRange &Other) const
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Interval::succ_iterator succ_begin(Interval *I)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
bool contains(const APInt &Val) const
static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, BasicBlock *BBTo, LVILatticeVal &Result)
Compute the value of Val on the edge BBFrom -> BBTo. Returns false if Val is not constrained on the e...
virtual void releaseMemory()
ConstantRange unionWith(const ConstantRange &CR) const
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
ConstantRange subtract(const APInt &CI) const
FunctionPass * createLazyValueInfoPass()
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
Constant * getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB)
static ConstantPointerNull * get(PointerType *T)
get() - Static factory methods - Return objects of the specified value
iterator find(const ValueT &V)
ConstantRange lshr(const ConstantRange &Other) const
LLVM Basic Block Representation.
LLVM Constant Representation.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Interval::pred_iterator pred_begin(Interval *I)
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
ConstantRange udiv(const ConstantRange &Other) const
BasicBlock * getIncomingBlock(unsigned i) const
Represent an integer comparison operator.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
ConstantRange sub(const ConstantRange &Other) const
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
Integer representation type.
Predicate getPredicate() const
Return the predicate for this instruction.
ConstantRange zeroExtend(uint32_t BitWidth) const
ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
LLVMContext & getContext() const
All values hold a context through their type.
ConstantRange add(const ConstantRange &Other) const
Tristate
Tristate - This is used to return true/false/dunno results.
virtual bool runOnFunction(Function &F)
std::pair< iterator, bool > insert(const ValueT &V)
unsigned getIntegerBitWidth() const
Class for constant integers.
Value * getIncomingValue(unsigned i) const
ConstantRange inverse() const
INITIALIZE_PASS_BEGIN(LazyValueInfo,"lazy-value-info","Lazy Value Information Analysis", false, true) INITIALIZE_PASS_END(LazyValueInfo
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
const BasicBlock & getEntryBlock() const
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
ConstantRange binaryOr(const ConstantRange &Other) const
Class for arbitrary precision integers.
ConstantRange shl(const ConstantRange &Other) const
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static const uint16_t * lookup(unsigned opcode, unsigned domain)
void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc)
static ConstantRange makeICmpRegion(unsigned Pred, const ConstantRange &Other)
ConstantRange binaryAnd(const ConstantRange &Other) const
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static ConstantRange makeConstantRange(Predicate pred, const APInt &C)
Make a ConstantRange for a relation with a constant value.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
lazy value Lazy Value Information false
void eraseBlock(BasicBlock *BB)
eraseBlock - Inform the analysis cache that we have erased a block.
LLVM Value Representation.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
const BasicBlock * getParent() const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
static bool InBlock(const Value *V, const BasicBlock *BB)
#define LLVM_ATTRIBUTE_USED