1 //===- CRSBuilder.h - ConstantRangesSet Builder -----------------*- 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 /// CRSBuilder allows to build and parse ConstantRangesSet objects.
12 /// There is such features like add/remove range, or combine
13 /// Two ConstantRangesSet object with neighboring ranges merging.
14 /// Set IsReadonly=true if you want to operate with "const ConstantInt" and
15 /// "const ConstantRangesSet" objects.
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Support/IntegersSubset.h"
29 template <class SuccessorClass>
30 class IntegersSubsetMapping {
33 typedef IntegersSubset::Range RangeTy;
35 struct RangeEx : public RangeTy {
36 typedef IntegersSubset::Range RangeTy;
37 RangeEx() : Weight(1) {}
38 RangeEx(const RangeTy &R) : RangeTy(R.Low, R.High), Weight(1) {}
39 RangeEx(const IntItem &C) : RangeTy(C), Weight(1) {}
40 RangeEx(const IntItem &L, const IntItem &H) : RangeTy(L, H), Weight(1) {}
41 RangeEx(const IntItem &L, const IntItem &H, unsigned W) :
42 RangeTy(L, H), Weight(W) {}
46 typedef std::pair<RangeEx, SuccessorClass*> Cluster;
50 typedef std::vector<Cluster> CaseItems;
51 typedef typename CaseItems::iterator CaseItemIt;
52 typedef typename CaseItems::const_iterator CaseItemConstIt;
54 typedef std::list<RangeTy> RangesCollection;
55 typedef typename RangesCollection::iterator RangesCollectionIt;
57 typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
58 typedef typename CRSMap::iterator CRSMapIt;
61 bool operator()(const Cluster &C1, const Cluster &C2) {
62 return C1.first < C2.first;
69 bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) {
70 return LItem->first.High->uge(RItem->first.Low);
73 bool isJoinable(CaseItemIt& LItem, CaseItemIt& RItem) {
74 if (LItem->second != RItem->second) {
75 assert(!isIntersected(LItem, RItem) &&
76 "Intersected items with different successors!");
79 APInt RLow = RItem->first.Low;
80 if (RLow != APInt::getNullValue(RLow.getBitWidth()))
82 return LItem->first.High->uge(RLow);
87 std::sort(Items.begin(), Items.end(), ClustersCmp());
92 IntegersSubset getCase(RangesCollection& Src) {
93 std::vector<Constant*> Elts;
94 Elts.reserve(Src.size());
95 for (RangesCollectionIt i = Src.begin(), e = Src.end(); i != e; ++i) {
97 std::vector<Constant*> r;
98 if (R.isSingleNumber()) {
100 // FIXME: Since currently we have ConstantInt based numbers
101 // use hack-conversion of IntItem to ConstantInt
102 r.push_back(R.Low.toConstantInt());
103 r.push_back(R.High.toConstantInt());
106 r.push_back(R.Low.toConstantInt());
108 Constant *CV = ConstantVector::get(r);
112 ArrayType::get(Elts.front()->getType(), (uint64_t)Elts.size());
113 Constant *Array = ConstantArray::get(ArrTy, Elts);
114 return IntegersSubset(Array);
119 // Don't public CaseItems itself. Don't allow edit the Items directly.
120 // Just present the user way to iterate over the internal collection
121 // sharing iterator, begin() and end(). Editing should be controlled by
123 typedef CaseItemIt RangeIterator;
125 typedef std::pair<SuccessorClass*, IntegersSubset> Case;
126 typedef std::list<Case> Cases;
128 IntegersSubsetMapping() {
133 bool verify(RangeIterator& errItem) {
137 for (CaseItemIt i = Items.begin(), j = i+1, e = Items.end();
139 if (isIntersected(j, i) && j->second != i->second) {
148 if (Items.size() < 2)
151 CaseItems OldItems = Items;
153 IntItem *Low = &OldItems.begin()->first.Low;
154 IntItem *High = &OldItems.begin()->first.High;
156 SuccessorClass *Successor = OldItems.begin()->second;
157 for (CaseItemIt i = OldItems.begin(), j = i+1, e = OldItems.end();
159 if (isJoinable(i, j)) {
160 IntItem *CurHigh = &j->first.High;
162 if ((*CurHigh)->ugt(*High))
165 RangeEx R(*Low, *High, Weight);
168 High = &j->first.High;
170 Successor = j->second;
173 RangeEx R(*Low, *High, Weight);
175 // We recollected the Items, but we kept it sorted.
179 /// Adds a constant value.
180 void add(const IntItem &C, SuccessorClass *S = 0) {
186 void add(const IntItem &Low, const IntItem &High, SuccessorClass *S = 0) {
187 RangeTy R(Low, High);
190 void add(const RangeTy &R, SuccessorClass *S = 0) {
194 void add(const RangeEx &R, SuccessorClass *S = 0) {
195 Items.push_back(std::make_pair(R, S));
199 /// Adds all ranges and values from given ranges set to the current
200 /// CRSBuilder object.
201 void add(const IntegersSubset &CRS, SuccessorClass *S = 0) {
202 for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
203 RangeTy R = CRS.getItem(i);
208 /// Removes items from set.
209 void removeItem(RangeIterator i) { Items.erase(i); }
211 /// Builds the finalized case objects.
212 void getCases(Cases& TheCases) {
214 for (RangeIterator i = this->begin(); i != this->end(); ++i)
215 TheCRSMap[i->second].push_back(i->first);
216 for (CRSMapIt i = TheCRSMap.begin(), e = TheCRSMap.end(); i != e; ++i)
217 TheCases.push_back(std::make_pair(i->first, getCase(i->second)));
220 /// Builds the finalized case objects ignoring successor values, as though
221 /// all ranges belongs to the same successor.
222 IntegersSubset getCase() {
223 RangesCollection Ranges;
224 for (RangeIterator i = this->begin(); i != this->end(); ++i)
225 Ranges.push_back(i->first);
226 return getCase(Ranges);
229 /// Returns true if there is no ranges and values inside.
230 bool empty() const { return Items.empty(); }
232 RangeIterator begin() { return Items.begin(); }
233 RangeIterator end() { return Items.end(); }
237 typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
241 #endif /* CRSBUILDER_H_ */