LLVM: include/llvm/Analysis/ValueLattice.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9#ifndef LLVM_ANALYSIS_VALUELATTICE_H
10#define LLVM_ANALYSIS_VALUELATTICE_H
11
15
16
17
18
19
20namespace llvm {
21
22
23
24
25
26
28 enum ValueLatticeElementTy {
29
30
31
32
33
34 unknown,
35
36
37
38
39
40
41
42
43 undef,
44
45
46
47
48
49
50
51 constant,
52
53
54
55
56
57
58 notconstant,
59
60
61
62
63
64
65 constantrange,
66
67
68
69
70
71
72 constantrange_including_undef,
73
74
75
76 overdefined,
77 };
78
79 ValueLatticeElementTy Tag : 8;
80
81 unsigned NumRangeExtensions : 8;
82
83
84
85
86 union {
89 };
90
91
92 void destroy() {
93 switch (Tag) {
94 case overdefined:
95 case unknown:
96 case undef:
97 case constant:
98 case notconstant:
99 break;
100 case constantrange_including_undef:
101 case constantrange:
102 Range.~ConstantRange();
103 break;
104 };
105 }
106
107public:
108
110
112
113
114
116
117
118
120
122
127
132
137
143 };
144
145
147
149
151 : Tag(Other.Tag), NumRangeExtensions(0) {
152 switch (Other.Tag) {
153 case constantrange:
154 case constantrange_including_undef:
155 new (&Range) ConstantRange(Other.Range);
156 NumRangeExtensions = Other.NumRangeExtensions;
157 break;
158 case constant:
159 case notconstant:
160 ConstVal = Other.ConstVal;
161 break;
162 case overdefined:
163 case unknown:
164 case undef:
165 break;
166 }
167 }
168
170 : Tag(Other.Tag), NumRangeExtensions(0) {
171 switch (Other.Tag) {
172 case constantrange:
173 case constantrange_including_undef:
174 new (&Range) ConstantRange(std::move(Other.Range));
175 NumRangeExtensions = Other.NumRangeExtensions;
176 break;
177 case constant:
178 case notconstant:
179 ConstVal = Other.ConstVal;
180 break;
181 case overdefined:
182 case unknown:
183 case undef:
184 break;
185 }
186 Other.Tag = unknown;
187 }
188
190 destroy();
192 return *this;
193 }
194
196 destroy();
198 return *this;
199 }
200
213 bool MayIncludeUndef = false) {
216
219 if (MayIncludeUndef)
221 return Res;
222 }
223
226 MergeOptions().setMayIncludeUndef(MayIncludeUndef));
227 return Res;
228 }
234
235 bool isUndef() const { return Tag == undef; }
236 bool isUnknown() const { return Tag == unknown; }
238 bool isConstant() const { return Tag == constant; }
241 return Tag == constantrange_including_undef;
242 }
243
244
245
246
248 return Tag == constantrange || (Tag == constantrange_including_undef &&
249 (UndefAllowed || Range.isSingleElement()));
250 }
252
257
262
263
264
265
266
269 "Cannot get the constant-range of a non-constant-range!");
271 }
272
278 }
279 return std::nullopt;
280 }
281
288 return ConstantRange::getEmpty(BW);
289 return ConstantRange::getFull(BW);
290 }
291
293 assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
294 return asConstantRange(Ty->getScalarSizeInBits(), UndefAllowed);
295 }
296
299 return false;
300 destroy();
301 Tag = overdefined;
302 return true;
303 }
304
307 return false;
308
310 Tag = undef;
311 return true;
312 }
313
317
319 assert(getConstant() == V && "Marking constant with different value");
320 return false;
321 }
322
326 MergeOptions().setMayIncludeUndef(MayIncludeUndef));
327
329 Tag = constant;
331 return true;
332 }
333
335 assert(V && "Marking constant with NULL");
338 ConstantRange(CI->getValue() + 1, CI->getValue()));
339
341 return false;
342
345 return false;
346 }
347
349 Tag = notconstant;
351 return true;
352 }
353
354
355
356
357
358
359
362 assert(!NewR.isEmptySet() && "should only be called for non-empty sets");
363
366
367 ValueLatticeElementTy OldTag = Tag;
368 ValueLatticeElementTy NewTag =
370 ? constantrange_including_undef
371 : constantrange;
373 Tag = NewTag;
375 return Tag != OldTag;
376
377
378
379 if (Opts.CheckWiden && ++NumRangeExtensions > Opts.MaxWidenSteps)
381
383 "Existing range must be a subset of NewR");
384 Range = std::move(NewR);
385 return true;
386 }
387
390 "Constant must be subset of new range");
391
392 NumRangeExtensions = 0;
393 Tag = NewTag;
395 return true;
396 }
397
398
399
403 return false;
404 if (RHS.isOverdefined()) {
406 return true;
407 }
408
411 if (RHS.isUndef())
412 return false;
413 if (RHS.isConstant())
415 if (RHS.isConstantRange())
417 Opts.setMayIncludeUndef());
419 }
420
422 assert(.isUnknown() && "Unknow RHS should be handled earlier");
423 *this = RHS;
424 return true;
425 }
426
429 return false;
430 if (RHS.isUndef())
431 return false;
432
437 RHS.asConstantRange(L.getBitWidth(), true));
439 std::move(NewR),
440 Opts.setMayIncludeUndef(RHS.isConstantRangeIncludingUndef()));
441 }
443 return true;
444 }
445
448 return false;
450 return true;
451 }
452
453 auto OldTag = Tag;
455 if (RHS.isUndef()) {
456 Tag = constantrange_including_undef;
457 return OldTag != Tag;
458 }
459
462 RHS.asConstantRange(L.getBitWidth(), true));
464 std::move(NewR),
465 Opts.setMayIncludeUndef(RHS.isConstantRangeIncludingUndef()));
466 }
467
468
469
470
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
492
495};
496
497static_assert(sizeof(ValueLatticeElement) <= 40,
498 "size of ValueLatticeElement changed unexpectedly");
499
501 const ValueLatticeElement &Val);
502}
503#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
static SymbolRef::Type getType(const Symbol *Sym)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
This is the shared class of boolean and integer constants.
This class represents a range of values.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
LLVM_ABI bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
This is an important base class in LLVM.
LLVM_ABI ConstantRange toConstantRange() const
Convert constant to an approximate constant range.
A parsed version of the target data layout string in and methods for querying it.
The instances of the Type class are immutable: once they are created, they are never changed.
This class represents lattice values for constants.
Definition ValueLattice.h:27
bool isUndef() const
Definition ValueLattice.h:235
bool markNotConstant(Constant *V)
Definition ValueLattice.h:334
static ValueLatticeElement getRange(ConstantRange CR, bool MayIncludeUndef=false)
Definition ValueLattice.h:212
bool isOverdefined() const
Definition ValueLattice.h:251
LLVM_ABI Constant * getCompare(CmpInst::Predicate Pred, Type *Ty, const ValueLatticeElement &Other, const DataLayout &DL) const
true, false or undef constants, or nullptr if the comparison cannot be evaluated.
bool isConstantRangeIncludingUndef() const
Definition ValueLattice.h:240
~ValueLatticeElement()
Definition ValueLattice.h:148
ValueLatticeElement(const ValueLatticeElement &Other)
Definition ValueLattice.h:150
static ValueLatticeElement getNot(Constant *C)
Definition ValueLattice.h:206
ConstantRange asConstantRange(unsigned BW, bool UndefAllowed=false) const
Definition ValueLattice.h:282
bool isNotConstant() const
Definition ValueLattice.h:239
std::optional< APInt > asConstantInteger() const
Definition ValueLattice.h:273
ValueLatticeElement & operator=(const ValueLatticeElement &Other)
Definition ValueLattice.h:189
ConstantRange Range
Definition ValueLattice.h:88
ValueLatticeElement(ValueLatticeElement &&Other)
Definition ValueLattice.h:169
bool isConstant() const
Definition ValueLattice.h:238
void setNumRangeExtensions(unsigned N)
Definition ValueLattice.h:494
bool isUnknown() const
Definition ValueLattice.h:236
const ConstantRange & getConstantRange(bool UndefAllowed=true) const
Returns the constant range for this value.
Definition ValueLattice.h:267
bool isConstantRange(bool UndefAllowed=true) const
Returns true if this value is a constant range.
Definition ValueLattice.h:247
static ValueLatticeElement get(Constant *C)
Definition ValueLattice.h:201
unsigned getNumRangeExtensions() const
Definition ValueLattice.h:493
Constant * getNotConstant() const
Definition ValueLattice.h:258
LLVM_ABI ValueLatticeElement intersect(const ValueLatticeElement &Other) const
Combine two sets of facts about the same value into a single set of facts.
bool markOverdefined()
Definition ValueLattice.h:297
bool isUnknownOrUndef() const
Definition ValueLattice.h:237
bool markUndef()
Definition ValueLattice.h:305
ConstantRange asConstantRange(Type *Ty, bool UndefAllowed=false) const
Definition ValueLattice.h:292
Constant * getConstant() const
Definition ValueLattice.h:253
bool mergeIn(const ValueLatticeElement &RHS, MergeOptions Opts=MergeOptions())
Updates this object to approximate both this object and RHS.
Definition ValueLattice.h:400
ValueLatticeElement & operator=(ValueLatticeElement &&Other)
Definition ValueLattice.h:195
bool markConstant(Constant *V, bool MayIncludeUndef=false)
Definition ValueLattice.h:314
ValueLatticeElement()
Definition ValueLattice.h:146
Constant * ConstVal
Definition ValueLattice.h:87
static ValueLatticeElement getOverdefined()
Definition ValueLattice.h:229
bool markConstantRange(ConstantRange NewR, MergeOptions Opts=MergeOptions())
Mark the object as constant range with NewR.
Definition ValueLattice.h:360
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
Struct to control some aspects related to merging constant ranges.
Definition ValueLattice.h:109
bool MayIncludeUndef
The merge value may include undef.
Definition ValueLattice.h:111
MergeOptions & setMayIncludeUndef(bool V=true)
Definition ValueLattice.h:128
MergeOptions()
Definition ValueLattice.h:121
bool CheckWiden
Handle repeatedly extending a range by going to overdefined after a number of steps.
Definition ValueLattice.h:115
MergeOptions & setMaxWidenSteps(unsigned Steps=1)
Definition ValueLattice.h:138
MergeOptions & setCheckWiden(bool V=true)
Definition ValueLattice.h:133
MergeOptions(bool MayIncludeUndef, bool CheckWiden, unsigned MaxWidenSteps=1)
Definition ValueLattice.h:123
unsigned MaxWidenSteps
The number of allowed widening steps (including setting the range initially).
Definition ValueLattice.h:119