Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

540 lines
17 KiB

  1. //===-- llvm/IntegersSubset.h - The subset of integers ----------*- C++ -*-===//
  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. /// @file
  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.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_INTEGERSSUBSET_H
  18. #define LLVM_SUPPORT_INTEGERSSUBSET_H
  19. #include "llvm/IR/Constants.h"
  20. #include "llvm/IR/DerivedTypes.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include <list>
  23. namespace llvm {
  24. // The IntItem is a wrapper for APInt.
  25. // 1. It determines sign of integer, it allows to use
  26. // comparison operators >,<,>=,<=, and as result we got shorter and cleaner
  27. // constructions.
  28. // 2. It helps to implement PR1255 (case ranges) as a series of small patches.
  29. // 3. Currently we can interpret IntItem both as ConstantInt and as APInt.
  30. // It allows to provide SwitchInst methods that works with ConstantInt for
  31. // non-updated passes. And it allows to use APInt interface for new methods.
  32. // 4. IntItem can be easily replaced with APInt.
  33. // The set of macros that allows to propagate APInt operators to the IntItem.
  34. #define INT_ITEM_DEFINE_COMPARISON(op,func) \
  35. bool operator op (const APInt& RHS) const { \
  36. return getAPIntValue().func(RHS); \
  37. }
  38. #define INT_ITEM_DEFINE_UNARY_OP(op) \
  39. IntItem operator op () const { \
  40. APInt res = op(getAPIntValue()); \
  41. Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
  42. return IntItem(cast<ConstantInt>(NewVal)); \
  43. }
  44. #define INT_ITEM_DEFINE_BINARY_OP(op) \
  45. IntItem operator op (const APInt& RHS) const { \
  46. APInt res = getAPIntValue() op RHS; \
  47. Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
  48. return IntItem(cast<ConstantInt>(NewVal)); \
  49. }
  50. #define INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(op) \
  51. IntItem& operator op (const APInt& RHS) {\
  52. APInt res = getAPIntValue();\
  53. res op RHS; \
  54. Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
  55. ConstantIntVal = cast<ConstantInt>(NewVal); \
  56. return *this; \
  57. }
  58. #define INT_ITEM_DEFINE_PREINCDEC(op) \
  59. IntItem& operator op () { \
  60. APInt res = getAPIntValue(); \
  61. op(res); \
  62. Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
  63. ConstantIntVal = cast<ConstantInt>(NewVal); \
  64. return *this; \
  65. }
  66. #define INT_ITEM_DEFINE_POSTINCDEC(op) \
  67. IntItem& operator op (int) { \
  68. APInt res = getAPIntValue();\
  69. op(res); \
  70. Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res); \
  71. OldConstantIntVal = ConstantIntVal; \
  72. ConstantIntVal = cast<ConstantInt>(NewVal); \
  73. return IntItem(OldConstantIntVal); \
  74. }
  75. #define INT_ITEM_DEFINE_OP_STANDARD_INT(RetTy, op, IntTy) \
  76. RetTy operator op (IntTy RHS) const { \
  77. return (*this) op APInt(getAPIntValue().getBitWidth(), RHS); \
  78. }
  79. class IntItem {
  80. ConstantInt *ConstantIntVal;
  81. const APInt* APIntVal;
  82. IntItem(const ConstantInt *V) :
  83. ConstantIntVal(const_cast<ConstantInt*>(V)),
  84. APIntVal(&ConstantIntVal->getValue()){}
  85. const APInt& getAPIntValue() const {
  86. return *APIntVal;
  87. }
  88. public:
  89. IntItem() {}
  90. operator const APInt&() const {
  91. return getAPIntValue();
  92. }
  93. // Propagate APInt operators.
  94. // Note, that
  95. // /,/=,>>,>>= are not implemented in APInt.
  96. // <<= is implemented for unsigned RHS, but not implemented for APInt RHS.
  97. INT_ITEM_DEFINE_COMPARISON(<, ult)
  98. INT_ITEM_DEFINE_COMPARISON(>, ugt)
  99. INT_ITEM_DEFINE_COMPARISON(<=, ule)
  100. INT_ITEM_DEFINE_COMPARISON(>=, uge)
  101. INT_ITEM_DEFINE_COMPARISON(==, eq)
  102. INT_ITEM_DEFINE_OP_STANDARD_INT(bool,==,uint64_t)
  103. INT_ITEM_DEFINE_COMPARISON(!=, ne)
  104. INT_ITEM_DEFINE_OP_STANDARD_INT(bool,!=,uint64_t)
  105. INT_ITEM_DEFINE_BINARY_OP(*)
  106. INT_ITEM_DEFINE_BINARY_OP(+)
  107. INT_ITEM_DEFINE_OP_STANDARD_INT(IntItem,+,uint64_t)
  108. INT_ITEM_DEFINE_BINARY_OP(-)
  109. INT_ITEM_DEFINE_OP_STANDARD_INT(IntItem,-,uint64_t)
  110. INT_ITEM_DEFINE_BINARY_OP(<<)
  111. INT_ITEM_DEFINE_OP_STANDARD_INT(IntItem,<<,unsigned)
  112. INT_ITEM_DEFINE_BINARY_OP(&)
  113. INT_ITEM_DEFINE_BINARY_OP(^)
  114. INT_ITEM_DEFINE_BINARY_OP(|)
  115. INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(*=)
  116. INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(+=)
  117. INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(-=)
  118. INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(&=)
  119. INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(^=)
  120. INT_ITEM_DEFINE_ASSIGNMENT_BY_OP(|=)
  121. // Special case for <<=
  122. IntItem& operator <<= (unsigned RHS) {
  123. APInt res = getAPIntValue();
  124. res <<= RHS;
  125. Constant *NewVal = ConstantInt::get(ConstantIntVal->getContext(), res);
  126. ConstantIntVal = cast<ConstantInt>(NewVal);
  127. return *this;
  128. }
  129. INT_ITEM_DEFINE_UNARY_OP(-)
  130. INT_ITEM_DEFINE_UNARY_OP(~)
  131. INT_ITEM_DEFINE_PREINCDEC(++)
  132. INT_ITEM_DEFINE_PREINCDEC(--)
  133. // The set of workarounds, since currently we use ConstantInt implemented
  134. // integer.
  135. static IntItem fromConstantInt(const ConstantInt *V) {
  136. return IntItem(V);
  137. }
  138. static IntItem fromType(Type* Ty, const APInt& V) {
  139. ConstantInt *C = cast<ConstantInt>(ConstantInt::get(Ty, V));
  140. return fromConstantInt(C);
  141. }
  142. static IntItem withImplLikeThis(const IntItem& LikeThis, const APInt& V) {
  143. ConstantInt *C = cast<ConstantInt>(ConstantInt::get(
  144. LikeThis.ConstantIntVal->getContext(), V));
  145. return fromConstantInt(C);
  146. }
  147. ConstantInt *toConstantInt() const {
  148. return ConstantIntVal;
  149. }
  150. };
  151. template<class IntType>
  152. class IntRange {
  153. protected:
  154. IntType Low;
  155. IntType High;
  156. bool IsEmpty : 1;
  157. bool IsSingleNumber : 1;
  158. public:
  159. typedef IntRange<IntType> self;
  160. typedef std::pair<self, self> SubRes;
  161. IntRange() : IsEmpty(true) {}
  162. IntRange(const self &RHS) :
  163. Low(RHS.Low), High(RHS.High),
  164. IsEmpty(RHS.IsEmpty), IsSingleNumber(RHS.IsSingleNumber) {}
  165. IntRange(const IntType &C) :
  166. Low(C), High(C), IsEmpty(false), IsSingleNumber(true) {}
  167. IntRange(const IntType &L, const IntType &H) : Low(L), High(H),
  168. IsEmpty(false), IsSingleNumber(Low == High) {}
  169. bool isEmpty() const { return IsEmpty; }
  170. bool isSingleNumber() const { return IsSingleNumber; }
  171. const IntType& getLow() const {
  172. assert(!IsEmpty && "Range is empty.");
  173. return Low;
  174. }
  175. const IntType& getHigh() const {
  176. assert(!IsEmpty && "Range is empty.");
  177. return High;
  178. }
  179. bool operator<(const self &RHS) const {
  180. assert(!IsEmpty && "Left range is empty.");
  181. assert(!RHS.IsEmpty && "Right range is empty.");
  182. if (Low == RHS.Low) {
  183. if (High > RHS.High)
  184. return true;
  185. return false;
  186. }
  187. if (Low < RHS.Low)
  188. return true;
  189. return false;
  190. }
  191. bool operator==(const self &RHS) const {
  192. assert(!IsEmpty && "Left range is empty.");
  193. assert(!RHS.IsEmpty && "Right range is empty.");
  194. return Low == RHS.Low && High == RHS.High;
  195. }
  196. bool operator!=(const self &RHS) const {
  197. return !operator ==(RHS);
  198. }
  199. static bool LessBySize(const self &LHS, const self &RHS) {
  200. return (LHS.High - LHS.Low) < (RHS.High - RHS.Low);
  201. }
  202. bool isInRange(const IntType &IntVal) const {
  203. assert(!IsEmpty && "Range is empty.");
  204. return IntVal >= Low && IntVal <= High;
  205. }
  206. SubRes sub(const self &RHS) const {
  207. SubRes Res;
  208. // RHS is either more global and includes this range or
  209. // if it doesn't intersected with this range.
  210. if (!isInRange(RHS.Low) && !isInRange(RHS.High)) {
  211. // If RHS more global (it is enough to check
  212. // only one border in this case.
  213. if (RHS.isInRange(Low))
  214. return std::make_pair(self(Low, High), self());
  215. return Res;
  216. }
  217. if (Low < RHS.Low) {
  218. Res.first.Low = Low;
  219. IntType NewHigh = RHS.Low;
  220. --NewHigh;
  221. Res.first.High = NewHigh;
  222. }
  223. if (High > RHS.High) {
  224. IntType NewLow = RHS.High;
  225. ++NewLow;
  226. Res.second.Low = NewLow;
  227. Res.second.High = High;
  228. }
  229. return Res;
  230. }
  231. };
  232. //===----------------------------------------------------------------------===//
  233. /// IntegersSubsetGeneric - class that implements the subset of integers. It
  234. /// consists from ranges and single numbers.
  235. template <class IntTy>
  236. class IntegersSubsetGeneric {
  237. public:
  238. // Use Chris Lattner idea, that was initially described here:
  239. // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120213/136954.html
  240. // In short, for more compact memory consumption we can store flat
  241. // numbers collection, and define range as pair of indices.
  242. // In that case we can safe some memory on 32 bit machines.
  243. typedef std::vector<IntTy> FlatCollectionTy;
  244. typedef std::pair<IntTy*, IntTy*> RangeLinkTy;
  245. typedef std::vector<RangeLinkTy> RangeLinksTy;
  246. typedef typename RangeLinksTy::const_iterator RangeLinksConstIt;
  247. typedef IntegersSubsetGeneric<IntTy> self;
  248. protected:
  249. FlatCollectionTy FlatCollection;
  250. RangeLinksTy RangeLinks;
  251. bool IsSingleNumber;
  252. bool IsSingleNumbersOnly;
  253. public:
  254. template<class RangesCollectionTy>
  255. explicit IntegersSubsetGeneric(const RangesCollectionTy& Links) {
  256. assert(Links.size() && "Empty ranges are not allowed.");
  257. // In case of big set of single numbers consumes additional RAM space,
  258. // but allows to avoid additional reallocation.
  259. FlatCollection.reserve(Links.size() * 2);
  260. RangeLinks.reserve(Links.size());
  261. IsSingleNumbersOnly = true;
  262. for (typename RangesCollectionTy::const_iterator i = Links.begin(),
  263. e = Links.end(); i != e; ++i) {
  264. RangeLinkTy RangeLink;
  265. FlatCollection.push_back(i->getLow());
  266. RangeLink.first = &FlatCollection.back();
  267. if (i->getLow() != i->getHigh()) {
  268. FlatCollection.push_back(i->getHigh());
  269. IsSingleNumbersOnly = false;
  270. }
  271. RangeLink.second = &FlatCollection.back();
  272. RangeLinks.push_back(RangeLink);
  273. }
  274. IsSingleNumber = IsSingleNumbersOnly && RangeLinks.size() == 1;
  275. }
  276. IntegersSubsetGeneric(const self& RHS) {
  277. *this = RHS;
  278. }
  279. self& operator=(const self& RHS) {
  280. FlatCollection.clear();
  281. RangeLinks.clear();
  282. FlatCollection.reserve(RHS.RangeLinks.size() * 2);
  283. RangeLinks.reserve(RHS.RangeLinks.size());
  284. for (RangeLinksConstIt i = RHS.RangeLinks.begin(), e = RHS.RangeLinks.end();
  285. i != e; ++i) {
  286. RangeLinkTy RangeLink;
  287. FlatCollection.push_back(*(i->first));
  288. RangeLink.first = &FlatCollection.back();
  289. if (i->first != i->second)
  290. FlatCollection.push_back(*(i->second));
  291. RangeLink.second = &FlatCollection.back();
  292. RangeLinks.push_back(RangeLink);
  293. }
  294. IsSingleNumber = RHS.IsSingleNumber;
  295. IsSingleNumbersOnly = RHS.IsSingleNumbersOnly;
  296. return *this;
  297. }
  298. typedef IntRange<IntTy> Range;
  299. /// Checks is the given constant satisfies this case. Returns
  300. /// true if it equals to one of contained values or belongs to the one of
  301. /// contained ranges.
  302. bool isSatisfies(const IntTy &CheckingVal) const {
  303. if (IsSingleNumber)
  304. return FlatCollection.front() == CheckingVal;
  305. if (IsSingleNumbersOnly)
  306. return std::find(FlatCollection.begin(),
  307. FlatCollection.end(),
  308. CheckingVal) != FlatCollection.end();
  309. for (unsigned i = 0, e = getNumItems(); i < e; ++i) {
  310. if (RangeLinks[i].first == RangeLinks[i].second) {
  311. if (*RangeLinks[i].first == CheckingVal)
  312. return true;
  313. } else if (*RangeLinks[i].first <= CheckingVal &&
  314. *RangeLinks[i].second >= CheckingVal)
  315. return true;
  316. }
  317. return false;
  318. }
  319. /// Returns set's item with given index.
  320. Range getItem(unsigned idx) const {
  321. const RangeLinkTy &Link = RangeLinks[idx];
  322. if (Link.first != Link.second)
  323. return Range(*Link.first, *Link.second);
  324. else
  325. return Range(*Link.first);
  326. }
  327. /// Return number of items (ranges) stored in set.
  328. unsigned getNumItems() const {
  329. return RangeLinks.size();
  330. }
  331. /// Returns true if whole subset contains single element.
  332. bool isSingleNumber() const {
  333. return IsSingleNumber;
  334. }
  335. /// Returns true if whole subset contains only single numbers, no ranges.
  336. bool isSingleNumbersOnly() const {
  337. return IsSingleNumbersOnly;
  338. }
  339. /// Does the same like getItem(idx).isSingleNumber(), but
  340. /// works faster, since we avoid creation of temporary range object.
  341. bool isSingleNumber(unsigned idx) const {
  342. return RangeLinks[idx].first == RangeLinks[idx].second;
  343. }
  344. /// Returns set the size, that equals number of all values + sizes of all
  345. /// ranges.
  346. /// Ranges set is considered as flat numbers collection.
  347. /// E.g.: for range [<0>, <1>, <4,8>] the size will 7;
  348. /// for range [<0>, <1>, <5>] the size will 3
  349. unsigned getSize() const {
  350. APInt sz(((const APInt&)getItem(0).getLow()).getBitWidth(), 0);
  351. for (unsigned i = 0, e = getNumItems(); i != e; ++i) {
  352. const APInt Low = getItem(i).getLow();
  353. const APInt High = getItem(i).getHigh();
  354. APInt S = High - Low + 1;
  355. sz += S;
  356. }
  357. return sz.getZExtValue();
  358. }
  359. /// Allows to access single value even if it belongs to some range.
  360. /// Ranges set is considered as flat numbers collection.
  361. /// [<1>, <4,8>] is considered as [1,4,5,6,7,8]
  362. /// For range [<1>, <4,8>] getSingleValue(3) returns 6.
  363. APInt getSingleValue(unsigned idx) const {
  364. APInt sz(((const APInt&)getItem(0).getLow()).getBitWidth(), 0);
  365. for (unsigned i = 0, e = getNumItems(); i != e; ++i) {
  366. const APInt Low = getItem(i).getLow();
  367. const APInt High = getItem(i).getHigh();
  368. APInt S = High - Low + 1;
  369. APInt oldSz = sz;
  370. sz += S;
  371. if (sz.ugt(idx)) {
  372. APInt Res = Low;
  373. APInt Offset(oldSz.getBitWidth(), idx);
  374. Offset -= oldSz;
  375. Res += Offset;
  376. return Res;
  377. }
  378. }
  379. assert(0 && "Index exceeds high border.");
  380. return sz;
  381. }
  382. /// Does the same as getSingleValue, but works only if subset contains
  383. /// single numbers only.
  384. const IntTy& getSingleNumber(unsigned idx) const {
  385. assert(IsSingleNumbersOnly && "This method works properly if subset "
  386. "contains single numbers only.");
  387. return FlatCollection[idx];
  388. }
  389. };
  390. //===----------------------------------------------------------------------===//
  391. /// IntegersSubset - currently is extension of IntegersSubsetGeneric
  392. /// that also supports conversion to/from Constant* object.
  393. class IntegersSubset : public IntegersSubsetGeneric<IntItem> {
  394. typedef IntegersSubsetGeneric<IntItem> ParentTy;
  395. Constant *Holder;
  396. static unsigned getNumItemsFromConstant(Constant *C) {
  397. return cast<ArrayType>(C->getType())->getNumElements();
  398. }
  399. static Range getItemFromConstant(Constant *C, unsigned idx) {
  400. const Constant *CV = C->getAggregateElement(idx);
  401. unsigned NumEls = cast<VectorType>(CV->getType())->getNumElements();
  402. switch (NumEls) {
  403. case 1:
  404. return Range(IntItem::fromConstantInt(
  405. cast<ConstantInt>(CV->getAggregateElement(0U))),
  406. IntItem::fromConstantInt(cast<ConstantInt>(
  407. cast<ConstantInt>(CV->getAggregateElement(0U)))));
  408. case 2:
  409. return Range(IntItem::fromConstantInt(
  410. cast<ConstantInt>(CV->getAggregateElement(0U))),
  411. IntItem::fromConstantInt(
  412. cast<ConstantInt>(CV->getAggregateElement(1))));
  413. default:
  414. assert(0 && "Only pairs and single numbers are allowed here.");
  415. return Range();
  416. }
  417. }
  418. std::vector<Range> rangesFromConstant(Constant *C) {
  419. unsigned NumItems = getNumItemsFromConstant(C);
  420. std::vector<Range> r;
  421. r.reserve(NumItems);
  422. for (unsigned i = 0, e = NumItems; i != e; ++i)
  423. r.push_back(getItemFromConstant(C, i));
  424. return r;
  425. }
  426. public:
  427. explicit IntegersSubset(Constant *C) : ParentTy(rangesFromConstant(C)),
  428. Holder(C) {}
  429. IntegersSubset(const IntegersSubset& RHS) :
  430. ParentTy(*(const ParentTy *)&RHS), // FIXME: tweak for msvc.
  431. Holder(RHS.Holder) {}
  432. template<class RangesCollectionTy>
  433. explicit IntegersSubset(const RangesCollectionTy& Src) : ParentTy(Src) {
  434. std::vector<Constant*> Elts;
  435. Elts.reserve(Src.size());
  436. for (typename RangesCollectionTy::const_iterator i = Src.begin(),
  437. e = Src.end(); i != e; ++i) {
  438. const Range &R = *i;
  439. std::vector<Constant*> r;
  440. if (R.isSingleNumber()) {
  441. r.reserve(2);
  442. // FIXME: Since currently we have ConstantInt based numbers
  443. // use hack-conversion of IntItem to ConstantInt
  444. r.push_back(R.getLow().toConstantInt());
  445. r.push_back(R.getHigh().toConstantInt());
  446. } else {
  447. r.reserve(1);
  448. r.push_back(R.getLow().toConstantInt());
  449. }
  450. Constant *CV = ConstantVector::get(r);
  451. Elts.push_back(CV);
  452. }
  453. ArrayType *ArrTy =
  454. ArrayType::get(Elts.front()->getType(), (uint64_t)Elts.size());
  455. Holder = ConstantArray::get(ArrTy, Elts);
  456. }
  457. operator Constant*() { return Holder; }
  458. operator const Constant*() const { return Holder; }
  459. Constant *operator->() { return Holder; }
  460. const Constant *operator->() const { return Holder; }
  461. };
  462. }
  463. #endif /* CLLVM_SUPPORT_INTEGERSSUBSET_H */