1 //===- LazyValueInfo.cpp - Value constraint analysis ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface for lazy computation of value constraint
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/LazyValueInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/ADT/PointerIntPair.h"
23 char LazyValueInfo::ID = 0;
24 static RegisterPass<LazyValueInfo>
25 X("lazy-value-info", "Lazy Value Information Analysis", false, true);
28 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 /// LVILatticeVal - This is the information tracked by LazyValueInfo for each
39 /// FIXME: This is basically just for bringup, this can be made a lot more rich
45 /// undefined - This LLVM Value has no known value yet.
47 /// constant - This LLVM Value has a specific constant value.
49 /// overdefined - This instruction is not known to be constant, and we know
54 /// Val: This stores the current lattice value along with the Constant* for
55 /// the constant if this is a 'constant' value.
56 PointerIntPair<Constant *, 2, LatticeValueTy> Val;
59 LVILatticeVal() : Val(0, undefined) {}
61 bool isUndefined() const { return Val.getInt() == undefined; }
62 bool isConstant() const { return Val.getInt() == constant; }
63 bool isOverdefined() const { return Val.getInt() == overdefined; }
65 Constant *getConstant() const {
66 assert(isConstant() && "Cannot get the constant of a non-constant!");
67 return Val.getPointer();
70 /// getConstantInt - If this is a constant with a ConstantInt value, return it
71 /// otherwise return null.
72 ConstantInt *getConstantInt() const {
74 return dyn_cast<ConstantInt>(getConstant());
78 /// markOverdefined - Return true if this is a change in status.
79 bool markOverdefined() {
82 Val.setInt(overdefined);
86 /// markConstant - Return true if this is a change in status.
87 bool markConstant(Constant *V) {
89 assert(getConstant() == V && "Marking constant with different value");
93 assert(isUndefined());
95 assert(V && "Marking constant with NULL");
101 } // end anonymous namespace.
104 //===----------------------------------------------------------------------===//
105 // LazyValueInfo Impl
106 //===----------------------------------------------------------------------===//
108 bool LazyValueInfo::runOnFunction(Function &F) {
109 TD = getAnalysisIfAvailable<TargetData>();
114 void LazyValueInfo::releaseMemory() {
119 /// isEqual - Determine whether the specified value is known to be equal or
120 /// not-equal to the specified constant at the end of the specified block.
121 LazyValueInfo::Tristate
122 LazyValueInfo::isEqual(Value *V, Constant *C, BasicBlock *BB) {
123 // If already a constant, we can use constant folding.
124 if (Constant *VC = dyn_cast<Constant>(V)) {
125 // Ignore FP for now. TODO, consider what form of equality we want.
126 if (C->getType()->isFPOrFPVector())
129 Constant *Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, VC,C,TD);
130 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
131 return ResCI->isZero() ? No : Yes;
134 // Not a very good implementation.
138 Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
139 // If already a constant, return it.
140 if (Constant *VC = dyn_cast<Constant>(V))
143 // Not a very good implementation.