1 //===-- llvm/IntegersSubset.h - The subset of integers ----------*- 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 //===----------------------------------------------------------------------===//
11 /// This file contains class that implements constant set of ranges:
12 /// [<Low0,High0>,...,<LowN,HighN>]. Initially, this class was created for
13 /// SwitchInst and was used for case value representation that may contain
14 /// multiple ranges for a single successor.
16 //===----------------------------------------------------------------------===//
18 #ifndef CONSTANTRANGESSET_H_
19 #define CONSTANTRANGESSET_H_
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/LLVMContext.h"
29 // The IntItem is a wrapper for APInt.
30 // 1. It determines sign of integer, it allows to use
31 // comparison operators >,<,>=,<=, and as result we got shorter and cleaner
33 // 2. It helps to implement PR1255 (case ranges) as a series of small patches.
34 // 3. Currently we can interpret IntItem both as ConstantInt and as APInt.
35 // It allows to provide SwitchInst methods that works with ConstantInt for
36 // non-updated passes. And it allows to use APInt interface for new methods.
37 // 4. IntItem can be easily replaced with APInt.
39 // The set of macros that allows to propagate APInt operators to the IntItem.
41 #define INT_ITEM_DEFINE_COMPARISON(op,func) \
42 bool operator op (const APInt& RHS) const { \
43 return ConstantIntVal->getValue().func(RHS); \
46 #define INT_ITEM_DEFINE_UNARY_OP(op) \
47 IntItem operator op () const { \
48 APInt res = op(ConstantIntVal->getValue()); \
49 Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
50 return IntItem(cast<ConstantInt>(NewVal)); \
53 #define INT_ITEM_DEFINE_BINARY_OP(op) \
54 IntItem operator op (const APInt& RHS) const { \
55 APInt res = ConstantIntVal->getValue() op RHS; \
56 Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
57 return IntItem(cast<ConstantInt>(NewVal)); \
60 #define INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(op) \
61 IntItem& operator op (const APInt& RHS) {\
62 APInt res = ConstantIntVal->getValue();\
64 Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
65 ConstantIntVal = cast<ConstantInt>(NewVal); \
69 #define INT_ITEM_DEFINE_PREINCDEC(op) \
70 IntItem& operator op () { \
71 APInt res = ConstantIntVal->getValue(); \
73 Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
74 ConstantIntVal = cast<ConstantInt>(NewVal); \
78 #define INT_ITEM_DEFINE_POSTINCDEC(op) \
79 IntItem& operator op (int) { \
80 APInt res = ConstantIntVal->getValue();\
82 Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
83 OldConstantIntVal = ConstantIntVal; \
84 ConstantIntVal = cast<ConstantInt>(NewVal); \
85 return IntItem(OldConstantIntVal); \
88 #define INT_ITEM_DEFINE_OP_STANDARD_INT(RetTy, op, IntTy) \
89 RetTy operator op (IntTy RHS) const { \
90 return (*this) op APInt(ConstantIntVal->getValue().getBitWidth(), RHS); \
94 ConstantInt *ConstantIntVal;
95 IntItem(const ConstantInt *V) : ConstantIntVal(const_cast<ConstantInt*>(V)) {}
100 operator const APInt&() const {
101 return (const APInt&)ConstantIntVal->getValue();
104 // Propagate APInt operators.
106 // /,/=,>>,>>= are not implemented in APInt.
107 // <<= is implemented for unsigned RHS, but not implemented for APInt RHS.
109 INT_ITEM_DEFINE_COMPARISON(<, ult)
110 INT_ITEM_DEFINE_COMPARISON(>, ugt)
111 INT_ITEM_DEFINE_COMPARISON(<=, ule)
112 INT_ITEM_DEFINE_COMPARISON(>=, uge)
114 INT_ITEM_DEFINE_COMPARISON(==, eq)
115 INT_ITEM_DEFINE_OP_STANDARD_INT(bool,==,uint64_t)
117 INT_ITEM_DEFINE_COMPARISON(!=, ne)
118 INT_ITEM_DEFINE_OP_STANDARD_INT(bool,!=,uint64_t)
120 INT_ITEM_DEFINE_BINARY_OP(*)
121 INT_ITEM_DEFINE_BINARY_OP(+)
122 INT_ITEM_DEFINE_OP_STANDARD_INT(IntItem,+,uint64_t)
123 INT_ITEM_DEFINE_BINARY_OP(-)
124 INT_ITEM_DEFINE_OP_STANDARD_INT(IntItem,-,uint64_t)
125 INT_ITEM_DEFINE_BINARY_OP(<<)
126 INT_ITEM_DEFINE_OP_STANDARD_INT(IntItem,<<,unsigned)
127 INT_ITEM_DEFINE_BINARY_OP(&)
128 INT_ITEM_DEFINE_BINARY_OP(^)
129 INT_ITEM_DEFINE_BINARY_OP(|)
131 INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(*=)
132 INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(+=)
133 INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(-=)
134 INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(&=)
135 INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(^=)
136 INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(|=)
138 // Special case for <<=
139 IntItem& operator <<= (unsigned RHS) {
140 APInt res = ConstantIntVal->getValue();
142 Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res);
143 ConstantIntVal = cast<ConstantInt>(NewVal);
147 INT_ITEM_DEFINE_UNARY_OP(-)
148 INT_ITEM_DEFINE_UNARY_OP(~)
150 INT_ITEM_DEFINE_PREINCDEC(++)
151 INT_ITEM_DEFINE_PREINCDEC(--)
153 // The set of workarounds, since currently we use ConstantInt implemented
156 static IntItem fromConstantInt(const ConstantInt *V) {
159 static IntItem fromType(Type* Ty, const APInt& V) {
160 ConstantInt *C = cast<ConstantInt>(ConstantInt::get(Ty, V));
161 return fromConstantInt(C);
163 static IntItem withImplLikeThis(const IntItem& LikeThis, const APInt& V) {
164 ConstantInt *C = cast<ConstantInt>(ConstantInt::get(
165 LikeThis.ConstantIntVal->getContext(), V));
166 return fromConstantInt(C);
168 ConstantInt *toConstantInt() const {
169 return ConstantIntVal;
173 template<class IntType>
179 bool IsSingleNumber : 1;
182 typedef IntRange<IntType> self;
183 typedef std::pair<self, self> SubRes;
185 IntRange() : IsEmpty(true) {}
187 IntRange(const IntType &C) :
188 Low(C), High(C), IsEmpty(false), IsSingleNumber(true) {}
190 IntRange(const IntType &L, const IntType &H) : Low(L), High(H),
191 IsEmpty(false), IsSingleNumber(Low == High) {}
193 bool isEmpty() const { return IsEmpty; }
194 bool isSingleNumber() const { return IsSingleNumber; }
196 const IntType& getLow() const {
197 assert(!IsEmpty && "Range is empty.");
200 const IntType& getHigh() const {
201 assert(!IsEmpty && "Range is empty.");
205 bool operator<(const self &RHS) const {
206 assert(!IsEmpty && "Left range is empty.");
207 assert(!RHS.IsEmpty && "Right range is empty.");
208 if (Low == RHS.Low) {
218 bool operator==(const self &RHS) const {
219 assert(!IsEmpty && "Left range is empty.");
220 assert(!RHS.IsEmpty && "Right range is empty.");
221 return Low == RHS.Low && High == RHS.High;
224 bool operator!=(const self &RHS) const {
225 return !operator ==(RHS);
228 static bool LessBySize(const self &LHS, const self &RHS) {
229 return (LHS.High - LHS.Low) < (RHS.High - RHS.Low);
232 bool isInRange(const IntType &IntVal) const {
233 assert(!IsEmpty && "Range is empty.");
234 return IntVal >= Low && IntVal <= High;
237 SubRes sub(const self &RHS) const {
240 // RHS is either more global and includes this range or
241 // if it doesn't intersected with this range.
242 if (!isInRange(RHS.Low) && !isInRange(RHS.High)) {
244 // If RHS more global (it is enough to check
245 // only one border in this case.
246 if (RHS.isInRange(Low))
247 return std::make_pair(self(Low, High), self());
254 IntType NewHigh = RHS.Low;
256 Res.first.High = NewHigh;
258 if (High > RHS.High) {
259 IntType NewLow = RHS.High;
261 Res.second.Low = NewLow;
262 Res.second.High = High;
268 //===----------------------------------------------------------------------===//
269 /// IntegersSubsetGeneric - class that implements the subset of integers. It
270 /// consists from ranges and single numbers.
271 template <class IntTy>
272 class IntegersSubsetGeneric {
274 // Use Chris Lattner idea, that was initially described here:
275 // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120213/136954.html
276 // In short, for more compact memory consumption we can store flat
277 // numbers collection, and define range as pair of indices.
278 // In that case we can safe some memory on 32 bit machines.
279 typedef std::list<IntTy> FlatCollectionTy;
280 typedef std::pair<IntTy*, IntTy*> RangeLinkTy;
281 typedef SmallVector<RangeLinkTy, 64> RangeLinksTy;
282 typedef typename RangeLinksTy::const_iterator RangeLinksConstIt;
284 typedef IntegersSubsetGeneric<IntTy> self;
288 FlatCollectionTy FlatCollection;
289 RangeLinksTy RangeLinks;
293 template<class RangesCollectionTy>
294 IntegersSubsetGeneric(const RangesCollectionTy& Links) {
295 assert(Links.size() && "Empty ranges are not allowed.");
296 for (typename RangesCollectionTy::const_iterator i = Links.begin(),
297 e = Links.end(); i != e; ++i) {
298 RangeLinkTy RangeLink;
299 FlatCollection.push_back(i->getLow());
300 RangeLink.first = &FlatCollection.back();
301 if (i->getLow() != i->getHigh())
302 FlatCollection.push_back(i->getHigh());
303 RangeLink.second = &FlatCollection.back();
304 RangeLinks.push_back(RangeLink);
308 IntegersSubsetGeneric(const self& RHS) {
312 self& operator=(const self& RHS) {
313 FlatCollection.clear();
315 for (RangeLinksConstIt i = RHS.RangeLinks.begin(), e = RHS.RangeLinks.end();
317 RangeLinkTy RangeLink;
318 FlatCollection.push_back(*(i->first));
319 RangeLink.first = &FlatCollection.back();
320 if (i->first != i->second)
321 FlatCollection.push_back(*(i->second));
322 RangeLink.second = &FlatCollection.back();
323 RangeLinks.push_back(RangeLink);
328 typedef IntRange<IntTy> Range;
330 /// Checks is the given constant satisfies this case. Returns
331 /// true if it equals to one of contained values or belongs to the one of
332 /// contained ranges.
333 bool isSatisfies(const IntTy &CheckingVal) const {
334 for (unsigned i = 0, e = getNumItems(); i < e; ++i) {
335 if (RangeLinks[i].first == RangeLinks[i].second) {
336 if (*RangeLinks[i].first == CheckingVal)
338 } else if (*RangeLinks[i].first <= CheckingVal &&
339 *RangeLinks[i].second >= CheckingVal)
345 /// Returns set's item with given index.
346 Range getItem(unsigned idx) const {
347 const RangeLinkTy &Link = RangeLinks[idx];
348 if (Link.first != Link.second)
349 return Range(*Link.first, *Link.second);
351 return Range(*Link.first);
354 /// Return number of items (ranges) stored in set.
355 unsigned getNumItems() const {
356 return RangeLinks.size();
359 /// Returns true if whole subset contains single element.
360 bool isSingleNumber() const {
361 return RangeLinks.size() == 1 &&
362 RangeLinks[0].first == RangeLinks[0].second;
365 /// Does the same like getItem(idx).isSingleNumber(), but
366 /// works faster, since we avoid creation of temporary range object.
367 bool isSingleNumber(unsigned idx) const {
368 return RangeLinks[idx].first == RangeLinks[idx].second;
371 /// Returns set the size, that equals number of all values + sizes of all
373 /// Ranges set is considered as flat numbers collection.
374 /// E.g.: for range [<0>, <1>, <4,8>] the size will 7;
375 /// for range [<0>, <1>, <5>] the size will 3
376 unsigned getSize() const {
377 APInt sz(((const APInt&)getItem(0).getLow()).getBitWidth(), 0);
378 for (unsigned i = 0, e = getNumItems(); i != e; ++i) {
379 const APInt &Low = getItem(i).getLow();
380 const APInt &High = getItem(i).getHigh();
381 APInt S = High - Low + 1;
384 return sz.getZExtValue();
387 /// Allows to access single value even if it belongs to some range.
388 /// Ranges set is considered as flat numbers collection.
389 /// [<1>, <4,8>] is considered as [1,4,5,6,7,8]
390 /// For range [<1>, <4,8>] getSingleValue(3) returns 6.
391 APInt getSingleValue(unsigned idx) const {
392 APInt sz(((const APInt&)getItem(0).getLow()).getBitWidth(), 0);
393 for (unsigned i = 0, e = getNumItems(); i != e; ++i) {
394 const APInt &Low = getItem(i).getLow();
395 const APInt &High = getItem(i).getHigh();
396 APInt S = High - Low + 1;
401 APInt Offset(oldSz.getBitWidth(), idx);
407 assert(0 && "Index exceeds high border.");
412 //===----------------------------------------------------------------------===//
413 /// IntegersSubset - currently is extension of IntegersSubsetGeneric
414 /// that also supports conversion to/from Constant* object.
415 class IntegersSubset : public IntegersSubsetGeneric<IntItem> {
417 typedef IntegersSubsetGeneric<IntItem> ParentTy;
421 static unsigned getNumItemsFromConstant(Constant *C) {
422 return cast<ArrayType>(C->getType())->getNumElements();
425 static Range getItemFromConstant(Constant *C, unsigned idx) {
426 const Constant *CV = C->getAggregateElement(idx);
428 unsigned NumEls = cast<VectorType>(CV->getType())->getNumElements();
431 return Range(IntItem::fromConstantInt(
432 cast<ConstantInt>(CV->getAggregateElement(0U))),
433 IntItem::fromConstantInt(cast<ConstantInt>(
434 cast<ConstantInt>(CV->getAggregateElement(0U)))));
436 return Range(IntItem::fromConstantInt(
437 cast<ConstantInt>(CV->getAggregateElement(0U))),
438 IntItem::fromConstantInt(
439 cast<ConstantInt>(CV->getAggregateElement(1))));
441 assert(0 && "Only pairs and single numbers are allowed here.");
446 std::vector<Range> rangesFromConstant(Constant *C) {
447 unsigned NumItems = getNumItemsFromConstant(C);
448 std::vector<Range> r;
450 for (unsigned i = 0, e = NumItems; i != e; ++i)
451 r.push_back(getItemFromConstant(C, i));
457 IntegersSubset(Constant *C) : ParentTy(rangesFromConstant(C)),
461 template<class RangesCollectionTy>
462 IntegersSubset(const RangesCollectionTy& Src) : ParentTy(Src) {
463 std::vector<Constant*> Elts;
464 Elts.reserve(Src.size());
465 for (typename RangesCollectionTy::const_iterator i = Src.begin(),
466 e = Src.end(); i != e; ++i) {
468 std::vector<Constant*> r;
469 if (R.isSingleNumber()) {
471 // FIXME: Since currently we have ConstantInt based numbers
472 // use hack-conversion of IntItem to ConstantInt
473 r.push_back(R.getLow().toConstantInt());
474 r.push_back(R.getHigh().toConstantInt());
477 r.push_back(R.getLow().toConstantInt());
479 Constant *CV = ConstantVector::get(r);
483 ArrayType::get(Elts.front()->getType(), (uint64_t)Elts.size());
484 Holder = ConstantArray::get(ArrTy, Elts);
487 operator Constant*() { return Holder; }
488 operator const Constant*() const { return Holder; }
489 Constant *operator->() { return Holder; }
490 const Constant *operator->() const { return Holder; }
495 #endif /* CONSTANTRANGESSET_H_ */