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 {
34 // FIXME: To much similar iterators typedefs, similar names.
35 // - Rename RangeIterator to the cluster iterator.
36 // - Remove unused "add" methods.
37 // - Class contents needs cleaning.
40 typedef IntRange<IntTy> RangeTy;
42 struct RangeEx : public RangeTy {
43 RangeEx() : Weight(1) {}
44 RangeEx(const RangeTy &R) : RangeTy(R), Weight(1) {}
45 RangeEx(const IntTy &C) : RangeTy(C), Weight(1) {}
46 RangeEx(const IntTy &L, const IntTy &H) : RangeTy(L, H), Weight(1) {}
47 RangeEx(const IntTy &L, const IntTy &H, unsigned W) :
48 RangeTy(L, H), Weight(W) {}
52 typedef std::pair<RangeEx, SuccessorClass*> Cluster;
54 typedef std::list<RangeTy> RangesCollection;
55 typedef typename RangesCollection::iterator RangesCollectionIt;
56 typedef typename RangesCollection::const_iterator RangesCollectionConstIt;
57 typedef IntegersSubsetMapping<SuccessorClass, IntegersSubsetTy, IntTy> self;
61 typedef std::list<Cluster> CaseItems;
62 typedef typename CaseItems::iterator CaseItemIt;
63 typedef typename CaseItems::const_iterator CaseItemConstIt;
65 // TODO: Change unclean CRS prefixes to SubsetMap for example.
66 typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
67 typedef typename CRSMap::iterator CRSMapIt;
70 bool operator()(const Cluster &C1, const Cluster &C2) {
71 return C1.first < C2.first;
77 bool SingleNumbersOnly;
79 bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) {
80 return LItem->first.getHigh() >= RItem->first.getLow();
83 bool isJoinable(CaseItemIt& LItem, CaseItemIt& RItem) {
84 if (LItem->second != RItem->second) {
85 assert(!isIntersected(LItem, RItem) &&
86 "Intersected items with different successors!");
89 APInt RLow = RItem->first.getLow();
90 if (RLow != APInt::getNullValue(RLow.getBitWidth()))
92 return LItem->first.getHigh() >= RLow;
97 std::vector<Cluster> clustersVector;
98 clustersVector.reserve(Items.size());
99 clustersVector.insert(clustersVector.begin(), Items.begin(), Items.end());
100 std::sort(clustersVector.begin(), clustersVector.end(), ClustersCmp());
102 Items.insert(Items.begin(), clustersVector.begin(), clustersVector.end());
107 enum DiffProcessState {
114 class DiffStateMachine {
116 DiffProcessState State;
118 SuccessorClass *CurrentLSuccessor;
119 SuccessorClass *CurrentRSuccessor;
122 self *IntersectionMapping;
128 IntegersSubsetMapping<SuccessorClass, IntegersSubsetTy, IntTy> MappingTy;
130 DiffStateMachine(MappingTy *L,
131 MappingTy *Intersection,
133 State(ALL_IS_CLOSED),
135 IntersectionMapping(Intersection),
139 void onLOpen(const IntTy &Pt, SuccessorClass *S) {
142 if (Pt > OpenPt/*Don't add empty ranges.*/ && RightMapping)
143 RightMapping->add(OpenPt, Pt-1, CurrentRSuccessor);
144 State = INTERSECT_OPENED;
150 assert(0 && "Got unexpected point.");
153 CurrentLSuccessor = S;
157 void onLClose(const IntTy &Pt) {
160 assert(Pt >= OpenPt &&
161 "Subset is not sorted or contains overlapped ranges");
163 LeftMapping->add(OpenPt, Pt, CurrentLSuccessor);
164 State = ALL_IS_CLOSED;
166 case INTERSECT_OPENED:
167 if (IntersectionMapping)
168 IntersectionMapping->add(OpenPt, Pt, CurrentLSuccessor);
173 assert(0 && "Got unexpected point.");
178 void onROpen(const IntTy &Pt, SuccessorClass *S) {
181 if (Pt > OpenPt && LeftMapping)
182 LeftMapping->add(OpenPt, Pt-1, CurrentLSuccessor);
183 State = INTERSECT_OPENED;
189 assert(0 && "Got unexpected point.");
192 CurrentRSuccessor = S;
196 void onRClose(const IntTy &Pt) {
199 assert(Pt >= OpenPt &&
200 "Subset is not sorted or contains overlapped ranges");
202 RightMapping->add(OpenPt, Pt, CurrentRSuccessor);
203 State = ALL_IS_CLOSED;
205 case INTERSECT_OPENED:
206 if (IntersectionMapping)
207 IntersectionMapping->add(OpenPt, Pt, CurrentLSuccessor);
212 assert(0 && "Got unexpected point.");
217 void onRLOpen(const IntTy &Pt,
219 SuccessorClass *RS) {
222 State = INTERSECT_OPENED;
225 assert(0 && "Got unexpected point.");
228 CurrentLSuccessor = LS;
229 CurrentRSuccessor = RS;
233 void onRLClose(const IntTy &Pt) {
235 case INTERSECT_OPENED:
236 if (IntersectionMapping)
237 IntersectionMapping->add(OpenPt, Pt, CurrentLSuccessor);
238 State = ALL_IS_CLOSED;
241 assert(0 && "Got unexpected point.");
246 bool isLOpened() { return State == L_OPENED; }
247 bool isROpened() { return State == R_OPENED; }
250 void diff_single_numbers(self *LExclude, self *Intersection, self *RExclude,
253 CaseItemConstIt L = Items.begin(), R = RHS.Items.begin();
254 CaseItemConstIt el = Items.end(), er = RHS.Items.end();
255 while (L != el && R != er) {
256 const Cluster &LCluster = *L;
257 const RangeEx &LRange = LCluster.first;
258 const Cluster &RCluster = *R;
259 const RangeEx &RRange = RCluster.first;
261 if (LRange.getLow() < RRange.getLow()) {
263 LExclude->add(LRange.getLow(), LCluster.second);
265 } else if (LRange.getLow() > RRange.getLow()) {
267 RExclude->add(RRange.getLow(), RCluster.second);
271 Intersection->add(LRange.getLow(), LCluster.second);
277 if (L != Items.end()) {
280 LExclude->add(L->first, L->second);
282 } while (L != Items.end());
283 } else if (R != RHS.Items.end()) {
286 RExclude->add(R->first, R->second);
288 } while (R != RHS.Items.end());
294 // Don't public CaseItems itself. Don't allow edit the Items directly.
295 // Just present the user way to iterate over the internal collection
296 // sharing iterator, begin() and end(). Editing should be controlled by
298 typedef CaseItemIt RangeIterator;
300 typedef std::pair<SuccessorClass*, IntegersSubsetTy> Case;
301 typedef std::list<Case> Cases;
303 IntegersSubsetMapping() {
305 SingleNumbersOnly = true;
308 bool verify(RangeIterator& errItem) {
312 for (CaseItemIt j = Items.begin(), i = j++, e = Items.end();
314 if (isIntersected(i, j) && i->second != j->second) {
323 if (Items.size() < 2)
326 CaseItems OldItems = Items;
328 const IntTy *Low = &OldItems.begin()->first.getLow();
329 const IntTy *High = &OldItems.begin()->first.getHigh();
331 SuccessorClass *Successor = OldItems.begin()->second;
332 for (CaseItemIt j = OldItems.begin(), i = j++, e = OldItems.end();
334 if (isJoinable(i, j)) {
335 const IntTy *CurHigh = &j->first.getHigh();
337 if (*CurHigh > *High)
340 RangeEx R(*Low, *High, Weight);
342 Low = &j->first.getLow();
343 High = &j->first.getHigh();
345 Successor = j->second;
348 RangeEx R(*Low, *High, Weight);
350 // We recollected the Items, but we kept it sorted.
354 /// Adds a constant value.
355 void add(const IntTy &C, SuccessorClass *S = 0) {
361 void add(const IntTy &Low, const IntTy &High, SuccessorClass *S = 0) {
362 RangeTy R(Low, High);
365 void add(const RangeTy &R, SuccessorClass *S = 0) {
369 void add(const RangeEx &R, SuccessorClass *S = 0) {
370 Items.push_back(std::make_pair(R, S));
371 if (!R.isSingleNumber())
372 SingleNumbersOnly = false;
375 /// Adds all ranges and values from given ranges set to the current
377 void add(const IntegersSubsetTy &CRS, SuccessorClass *S = 0) {
378 for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
379 RangeTy R = CRS.getItem(i);
384 void add(self& RHS) {
385 Items.insert(Items.end(), RHS.Items.begin(), RHS.Items.end());
386 if (!RHS.SingleNumbersOnly)
387 SingleNumbersOnly = false;
390 void add(const RangesCollection& RHS, SuccessorClass *S = 0) {
391 for (RangesCollectionConstIt i = RHS.begin(), e = RHS.end(); i != e; ++i)
395 /// Removes items from set.
396 void removeItem(RangeIterator i) { Items.erase(i); }
398 /// Calculates the difference between this mapping and RHS.
399 /// THIS without RHS is placed into LExclude,
400 /// RHS without THIS is placed into RExclude,
401 /// THIS intersect RHS is placed into Intersection.
402 void diff(self *LExclude, self *Intersection, self *RExclude,
405 if (SingleNumbersOnly && RHS.SingleNumbersOnly) {
406 diff_single_numbers(LExclude, Intersection, RExclude, RHS);
410 DiffStateMachine Machine(LExclude, Intersection, RExclude);
412 CaseItemConstIt L = Items.begin(), R = RHS.Items.begin();
413 CaseItemConstIt el = Items.end(), er = RHS.Items.end();
414 while (L != el && R != er) {
415 const Cluster &LCluster = *L;
416 const RangeEx &LRange = LCluster.first;
417 const Cluster &RCluster = *R;
418 const RangeEx &RRange = RCluster.first;
420 if (LRange.getHigh() < RRange.getLow()) {
421 Machine.onLOpen(LRange.getLow(), LCluster.second);
422 Machine.onLClose(LRange.getHigh());
427 if (LRange.getLow() > RRange.getHigh()) {
428 Machine.onROpen(RRange.getLow(), RCluster.second);
429 Machine.onRClose(RRange.getHigh());
434 if (LRange.isSingleNumber() && RRange.isSingleNumber()) {
435 Machine.onRLOpen(LRange.getLow(), LCluster.second, RCluster.second);
436 Machine.onRLClose(LRange.getLow());
442 if (LRange.isSingleNumber()) {
443 Machine.onLOpen(LRange.getLow(), LCluster.second);
444 Machine.onLClose(LRange.getLow());
446 while(L != Items.end() && L->first.getHigh() < RRange.getHigh()) {
447 Machine.onLOpen(LRange.getLow(), LCluster.second);
448 Machine.onLClose(LRange.getLow());
452 } else if (RRange.isSingleNumber()) {
453 Machine.onROpen(R->first.getLow(), R->second);
454 Machine.onRClose(R->first.getHigh());
456 while(R != RHS.Items.end() && R->first.getHigh() < LRange.getHigh()) {
457 Machine.onROpen(R->first.getLow(), R->second);
458 Machine.onRClose(R->first.getHigh());
463 if (LRange.getLow() < RRange.getLow()) {
464 // May be opened in previous iteration.
465 if (!Machine.isLOpened())
466 Machine.onLOpen(LRange.getLow(), LCluster.second);
467 Machine.onROpen(RRange.getLow(), RCluster.second);
469 else if (RRange.getLow() < LRange.getLow()) {
470 if (!Machine.isROpened())
471 Machine.onROpen(RRange.getLow(), RCluster.second);
472 Machine.onLOpen(LRange.getLow(), LCluster.second);
475 Machine.onRLOpen(LRange.getLow(), LCluster.second, RCluster.second);
477 if (LRange.getHigh() < RRange.getHigh()) {
478 Machine.onLClose(LRange.getHigh());
480 while(L != Items.end() && L->first.getHigh() < RRange.getHigh()) {
481 Machine.onLOpen(L->first.getLow(), L->second);
482 Machine.onLClose(L->first.getHigh());
486 else if (RRange.getHigh() < LRange.getHigh()) {
487 Machine.onRClose(RRange.getHigh());
489 while(R != RHS.Items.end() && R->first.getHigh() < LRange.getHigh()) {
490 Machine.onROpen(R->first.getLow(), R->second);
491 Machine.onRClose(R->first.getHigh());
496 Machine.onRLClose(LRange.getHigh());
502 if (L != Items.end()) {
503 if (Machine.isLOpened()) {
504 Machine.onLClose(L->first.getHigh());
508 while (L != Items.end()) {
509 LExclude->add(L->first, L->second);
512 } else if (R != RHS.Items.end()) {
513 if (Machine.isROpened()) {
514 Machine.onRClose(R->first.getHigh());
518 while (R != RHS.Items.end()) {
519 RExclude->add(R->first, R->second);
525 /// Builds the finalized case objects.
526 void getCases(Cases& TheCases) {
528 for (RangeIterator i = this->begin(); i != this->end(); ++i)
529 TheCRSMap[i->second].push_back(i->first);
530 for (CRSMapIt i = TheCRSMap.begin(), e = TheCRSMap.end(); i != e; ++i)
531 TheCases.push_back(std::make_pair(i->first, IntegersSubsetTy(i->second)));
534 /// Builds the finalized case objects ignoring successor values, as though
535 /// all ranges belongs to the same successor.
536 IntegersSubsetTy getCase() {
537 RangesCollection Ranges;
538 for (RangeIterator i = this->begin(); i != this->end(); ++i)
539 Ranges.push_back(i->first);
540 return IntegersSubsetTy(Ranges);
543 /// Returns true if there is no ranges and values inside.
544 bool empty() const { return Items.empty(); }
548 // Don't reset Sorted flag:
549 // 1. For empty mapping it matters nothing.
550 // 2. After first item will added Sorted flag will cleared.
553 // Returns number of clusters
554 unsigned size() const {
558 RangeIterator begin() { return Items.begin(); }
559 RangeIterator end() { return Items.end(); }
563 typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
567 #endif /* CRSBUILDER_H_ */