blob: b18379deb0c26289af12c880232b2d40ab7b027f [file] [log] [blame]
Chris Lattner10f2d132009-11-11 00:22:30 +00001//===- LazyValueInfo.cpp - Value constraint analysis ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interface for lazy computation of value constraint
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LazyValueInfo.h"
Chris Lattnercc4d3b22009-11-11 02:08:33 +000016#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"
Chris Lattner10f2d132009-11-11 00:22:30 +000021using namespace llvm;
22
23char LazyValueInfo::ID = 0;
24static RegisterPass<LazyValueInfo>
25X("lazy-value-info", "Lazy Value Information Analysis", false, true);
26
27namespace llvm {
28 FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); }
29}
30
Chris Lattnercc4d3b22009-11-11 02:08:33 +000031
32//===----------------------------------------------------------------------===//
33// LVILatticeVal
34//===----------------------------------------------------------------------===//
35
36/// LVILatticeVal - This is the information tracked by LazyValueInfo for each
37/// value.
38///
39/// FIXME: This is basically just for bringup, this can be made a lot more rich
40/// in the future.
41///
42namespace {
43class LVILatticeVal {
44 enum LatticeValueTy {
45 /// undefined - This LLVM Value has no known value yet.
46 undefined,
47 /// constant - This LLVM Value has a specific constant value.
48 constant,
49 /// overdefined - This instruction is not known to be constant, and we know
50 /// it has a value.
51 overdefined
52 };
53
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;
57
58public:
59 LVILatticeVal() : Val(0, undefined) {}
60
61 bool isUndefined() const { return Val.getInt() == undefined; }
62 bool isConstant() const { return Val.getInt() == constant; }
63 bool isOverdefined() const { return Val.getInt() == overdefined; }
64
65 Constant *getConstant() const {
66 assert(isConstant() && "Cannot get the constant of a non-constant!");
67 return Val.getPointer();
68 }
69
70 /// getConstantInt - If this is a constant with a ConstantInt value, return it
71 /// otherwise return null.
72 ConstantInt *getConstantInt() const {
73 if (isConstant())
74 return dyn_cast<ConstantInt>(getConstant());
75 return 0;
76 }
77
78 /// markOverdefined - Return true if this is a change in status.
79 bool markOverdefined() {
80 if (isOverdefined())
81 return false;
82 Val.setInt(overdefined);
83 return true;
84 }
85
86 /// markConstant - Return true if this is a change in status.
87 bool markConstant(Constant *V) {
88 if (isConstant()) {
89 assert(getConstant() == V && "Marking constant with different value");
90 return false;
91 }
92
93 assert(isUndefined());
94 Val.setInt(constant);
95 assert(V && "Marking constant with NULL");
96 Val.setPointer(V);
97 }
98
99};
100
101} // end anonymous namespace.
102
103
104//===----------------------------------------------------------------------===//
105// LazyValueInfo Impl
106//===----------------------------------------------------------------------===//
107
108bool LazyValueInfo::runOnFunction(Function &F) {
109 TD = getAnalysisIfAvailable<TargetData>();
110 // Fully lazy.
111 return false;
Chris Lattner10f2d132009-11-11 00:22:30 +0000112}
113
114void LazyValueInfo::releaseMemory() {
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000115 // No caching yet.
Chris Lattner10f2d132009-11-11 00:22:30 +0000116}
Chris Lattnercc4d3b22009-11-11 02:08:33 +0000117
118
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.
121LazyValueInfo::Tristate
122LazyValueInfo::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())
127 return Unknown;
128
129 Constant *Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, VC,C,TD);
130 if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res))
131 return ResCI->isZero() ? No : Yes;
132 }
133
134 // Not a very good implementation.
135 return Unknown;
136}
137
138Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) {
139 // If already a constant, return it.
140 if (Constant *VC = dyn_cast<Constant>(V))
141 return VC;
142
143 // Not a very good implementation.
144 return 0;
145}
146