1 //===- ValueLattice.h - Value constraint analysis ---------------*- C++ -*-===//
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 #ifndef LLVM_ANALYSIS_VALUELATTICE_H
11 #define LLVM_ANALYSIS_VALUELATTICE_H
13 #include "llvm/IR/ConstantRange.h"
14 #include "llvm/IR/Constants.h"
16 //===----------------------------------------------------------------------===//
17 // ValueLatticeElement
18 //===----------------------------------------------------------------------===//
20 /// This class represents lattice values for constants.
22 /// FIXME: This is basically just for bringup, this can be made a lot more rich
27 class ValueLatticeElement {
28 enum ValueLatticeElementTy {
29 /// This Value has no known value yet. As a result, this implies the
30 /// producing instruction is dead. Caution: We use this as the starting
31 /// state in our local meet rules. In this usage, it's taken to mean
32 /// "nothing known yet".
35 /// This Value has a specific constant value. (For constant integers,
36 /// constantrange is used instead. Integer typed constantexprs can appear
40 /// This Value is known to not have the specified value. (For constant
41 /// integers, constantrange is used instead. As above, integer typed
42 /// constantexprs can appear here.)
45 /// The Value falls within this range. (Used only for integer typed values.)
48 /// We can not precisely model the dynamic values this value might take.
52 ValueLatticeElementTy Tag;
54 /// The union either stores a pointer to a constant or a constant range,
55 /// associated to the lattice element. We have to ensure that Range is
56 /// initialized or destroyed when changing state to or from constantrange.
63 // Const and Range are initialized on-demand.
64 ValueLatticeElement() : Tag(undefined) {}
66 /// Custom destructor to ensure Range is properly destroyed, when the object
68 ~ValueLatticeElement() {
76 Range.~ConstantRange();
81 /// Custom copy constructor, to ensure Range gets initialized when
82 /// copying a constant range lattice element.
83 ValueLatticeElement(const ValueLatticeElement &Other) : Tag(undefined) {
87 /// Custom assignment operator, to ensure Range gets initialized when
88 /// assigning a constant range lattice element.
89 ValueLatticeElement &operator=(const ValueLatticeElement &Other) {
90 // If we change the state of this from constant range to non constant range,
92 if (isConstantRange() && !Other.isConstantRange())
93 Range.~ConstantRange();
95 // If we change the state of this from a valid ConstVal to another a state
96 // without a valid ConstVal, zero the pointer.
97 if ((isConstant() || isNotConstant()) && !Other.isConstant() &&
98 !Other.isNotConstant())
103 if (!isConstantRange())
104 new (&Range) ConstantRange(Other.Range);
110 ConstVal = Other.ConstVal;
120 static ValueLatticeElement get(Constant *C) {
121 ValueLatticeElement Res;
122 if (!isa<UndefValue>(C))
126 static ValueLatticeElement getNot(Constant *C) {
127 ValueLatticeElement Res;
128 if (!isa<UndefValue>(C))
129 Res.markNotConstant(C);
132 static ValueLatticeElement getRange(ConstantRange CR) {
133 ValueLatticeElement Res;
134 Res.markConstantRange(std::move(CR));
137 static ValueLatticeElement getOverdefined() {
138 ValueLatticeElement Res;
139 Res.markOverdefined();
143 bool isUndefined() const { return Tag == undefined; }
144 bool isConstant() const { return Tag == constant; }
145 bool isNotConstant() const { return Tag == notconstant; }
146 bool isConstantRange() const { return Tag == constantrange; }
147 bool isOverdefined() const { return Tag == overdefined; }
149 Constant *getConstant() const {
150 assert(isConstant() && "Cannot get the constant of a non-constant!");
154 Constant *getNotConstant() const {
155 assert(isNotConstant() && "Cannot get the constant of a non-notconstant!");
159 const ConstantRange &getConstantRange() const {
160 assert(isConstantRange() &&
161 "Cannot get the constant-range of a non-constant-range!");
165 Optional<APInt> asConstantInteger() const {
166 if (isConstant() && isa<ConstantInt>(getConstant())) {
167 return cast<ConstantInt>(getConstant())->getValue();
168 } else if (isConstantRange() && getConstantRange().isSingleElement()) {
169 return *getConstantRange().getSingleElement();
175 void markOverdefined() {
178 if (isConstant() || isNotConstant())
180 if (isConstantRange())
181 Range.~ConstantRange();
185 void markConstant(Constant *V) {
186 assert(V && "Marking constant with NULL");
187 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
188 markConstantRange(ConstantRange(CI->getValue()));
191 if (isa<UndefValue>(V))
194 assert((!isConstant() || getConstant() == V) &&
195 "Marking constant with different value");
196 assert(isUndefined());
201 void markNotConstant(Constant *V) {
202 assert(V && "Marking constant with NULL");
203 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
204 markConstantRange(ConstantRange(CI->getValue() + 1, CI->getValue()));
207 if (isa<UndefValue>(V))
210 assert((!isConstant() || getConstant() != V) &&
211 "Marking constant !constant with same value");
212 assert((!isNotConstant() || getNotConstant() == V) &&
213 "Marking !constant with different value");
214 assert(isUndefined() || isConstant());
219 void markConstantRange(ConstantRange NewR) {
220 if (isConstantRange()) {
221 if (NewR.isEmptySet())
224 Range = std::move(NewR);
229 assert(isUndefined());
230 if (NewR.isEmptySet())
234 new (&Range) ConstantRange(std::move(NewR));
239 /// Updates this object to approximate both this object and RHS. Returns
240 /// true if this object has been changed.
241 bool mergeIn(const ValueLatticeElement &RHS, const DataLayout &DL) {
242 if (RHS.isUndefined() || isOverdefined())
244 if (RHS.isOverdefined()) {
251 return !RHS.isUndefined();
255 if (RHS.isConstant() && getConstant() == RHS.getConstant())
261 if (isNotConstant()) {
262 if (RHS.isNotConstant() && getNotConstant() == RHS.getNotConstant())
268 assert(isConstantRange() && "New ValueLattice type?");
269 if (!RHS.isConstantRange()) {
270 // We can get here if we've encountered a constantexpr of integer type
271 // and merge it with a constantrange.
275 ConstantRange NewR = getConstantRange().unionWith(RHS.getConstantRange());
276 if (NewR.isFullSet())
278 else if (NewR == getConstantRange())
281 markConstantRange(std::move(NewR));
285 ConstantInt *getConstantInt() const {
286 assert(isConstant() && isa<ConstantInt>(getConstant()) &&
287 "No integer constant");
288 return cast<ConstantInt>(getConstant());
291 /// Compares this symbolic value with Other using Pred and returns either
292 /// true, false or undef constants, or nullptr if the comparison cannot be
294 Constant *getCompare(CmpInst::Predicate Pred, Type *Ty,
295 const ValueLatticeElement &Other) const {
296 if (isUndefined() || Other.isUndefined())
297 return UndefValue::get(Ty);
299 if (isConstant() && Other.isConstant())
300 return ConstantExpr::getCompare(Pred, getConstant(), Other.getConstant());
302 // Integer constants are represented as ConstantRanges with single
304 if (!isConstantRange() || !Other.isConstantRange())
307 const auto &CR = getConstantRange();
308 const auto &OtherCR = Other.getConstantRange();
309 if (ConstantRange::makeSatisfyingICmpRegion(Pred, OtherCR).contains(CR))
310 return ConstantInt::getTrue(Ty);
311 if (ConstantRange::makeSatisfyingICmpRegion(
312 CmpInst::getInversePredicate(Pred), OtherCR)
314 return ConstantInt::getFalse(Ty);
320 raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val);
322 } // end namespace llvm