1 //===- IntegersSubsetMapping.h - Mapping subset ==> Successor ---*- 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 /// IntegersSubsetMapping is mapping from A to B, where
12 /// Items in A is subsets of integers,
13 /// Items in B some pointers (Successors).
14 /// If user which to add another subset for successor that is already
15 /// exists in mapping, IntegersSubsetMapping merges existing subset with
18 //===----------------------------------------------------------------------===//
23 #include "llvm/Support/IntegersSubset.h"
30 template <class SuccessorClass,
31 class IntegersSubsetTy = IntegersSubset,
32 class IntTy = IntItem>
33 class IntegersSubsetMapping {
36 typedef IntRange<IntTy> RangeTy;
38 struct RangeEx : public RangeTy {
39 RangeEx() : Weight(1) {}
40 RangeEx(const RangeTy &R) : RangeTy(R), Weight(1) {}
41 RangeEx(const IntTy &C) : RangeTy(C), Weight(1) {}
42 RangeEx(const IntTy &L, const IntTy &H) : RangeTy(L, H), Weight(1) {}
43 RangeEx(const IntTy &L, const IntTy &H, unsigned W) :
44 RangeTy(L, H), Weight(W) {}
48 typedef std::pair<RangeEx, SuccessorClass*> Cluster;
52 typedef std::vector<Cluster> CaseItems;
53 typedef typename CaseItems::iterator CaseItemIt;
54 typedef typename CaseItems::const_iterator CaseItemConstIt;
56 typedef std::list<RangeTy> RangesCollection;
57 typedef typename RangesCollection::iterator RangesCollectionIt;
59 // TODO: Change unclean CRS prefixes to SubsetMap for example.
60 typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
61 typedef typename CRSMap::iterator CRSMapIt;
64 bool operator()(const Cluster &C1, const Cluster &C2) {
65 return C1.first < C2.first;
72 bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) {
73 return LItem->first.getHigh() >= RItem->first.getLow();
76 bool isJoinable(CaseItemIt& LItem, CaseItemIt& RItem) {
77 if (LItem->second != RItem->second) {
78 assert(!isIntersected(LItem, RItem) &&
79 "Intersected items with different successors!");
82 APInt RLow = RItem->first.getLow();
83 if (RLow != APInt::getNullValue(RLow.getBitWidth()))
85 return LItem->first.getHigh() >= RLow;
90 std::sort(Items.begin(), Items.end(), ClustersCmp());
97 // Don't public CaseItems itself. Don't allow edit the Items directly.
98 // Just present the user way to iterate over the internal collection
99 // sharing iterator, begin() and end(). Editing should be controlled by
101 typedef CaseItemIt RangeIterator;
103 typedef std::pair<SuccessorClass*, IntegersSubsetTy> Case;
104 typedef std::list<Case> Cases;
106 IntegersSubsetMapping() {
111 bool verify(RangeIterator& errItem) {
115 for (CaseItemIt i = Items.begin(), j = i+1, e = Items.end();
117 if (isIntersected(i, j) && i->second != j->second) {
126 if (Items.size() < 2)
129 CaseItems OldItems = Items;
131 const IntTy *Low = &OldItems.begin()->first.getLow();
132 const IntTy *High = &OldItems.begin()->first.getHigh();
134 SuccessorClass *Successor = OldItems.begin()->second;
135 for (CaseItemIt i = OldItems.begin(), j = i+1, e = OldItems.end();
137 if (isJoinable(i, j)) {
138 const IntTy *CurHigh = &j->first.getHigh();
140 if (*CurHigh > *High)
143 RangeEx R(*Low, *High, Weight);
145 Low = &j->first.getLow();
146 High = &j->first.getHigh();
148 Successor = j->second;
151 RangeEx R(*Low, *High, Weight);
153 // We recollected the Items, but we kept it sorted.
157 /// Adds a constant value.
158 void add(const IntTy &C, SuccessorClass *S = 0) {
164 void add(const IntTy &Low, const IntTy &High, SuccessorClass *S = 0) {
165 RangeTy R(Low, High);
168 void add(const RangeTy &R, SuccessorClass *S = 0) {
172 void add(const RangeEx &R, SuccessorClass *S = 0) {
173 Items.push_back(std::make_pair(R, S));
177 /// Adds all ranges and values from given ranges set to the current
179 void add(const IntegersSubset &CRS, SuccessorClass *S = 0) {
180 for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
181 RangeTy R = CRS.getItem(i);
186 /// Removes items from set.
187 void removeItem(RangeIterator i) { Items.erase(i); }
189 /// Builds the finalized case objects.
190 void getCases(Cases& TheCases) {
192 for (RangeIterator i = this->begin(); i != this->end(); ++i)
193 TheCRSMap[i->second].push_back(i->first);
194 for (CRSMapIt i = TheCRSMap.begin(), e = TheCRSMap.end(); i != e; ++i)
195 TheCases.push_back(std::make_pair(i->first, IntegersSubsetTy(i->second)));
198 /// Builds the finalized case objects ignoring successor values, as though
199 /// all ranges belongs to the same successor.
200 IntegersSubset getCase() {
201 RangesCollection Ranges;
202 for (RangeIterator i = this->begin(); i != this->end(); ++i)
203 Ranges.push_back(i->first);
204 return IntegersSubsetTy(Ranges);
207 /// Returns true if there is no ranges and values inside.
208 bool empty() const { return Items.empty(); }
210 RangeIterator begin() { return Items.begin(); }
211 RangeIterator end() { return Items.end(); }
215 typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
219 #endif /* CRSBUILDER_H_ */