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 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_INTEGERSSUBSETMAPPING_H
21 #define LLVM_SUPPORT_INTEGERSSUBSETMAPPING_H
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 RangeTy &R, unsigned W) : RangeTy(R), Weight(W) {}
46 RangeEx(const IntTy &C) : RangeTy(C), Weight(1) {}
47 RangeEx(const IntTy &L, const IntTy &H) : RangeTy(L, H), Weight(1) {}
48 RangeEx(const IntTy &L, const IntTy &H, unsigned W) :
49 RangeTy(L, H), Weight(W) {}
53 typedef std::pair<RangeEx, SuccessorClass*> Cluster;
55 typedef std::list<RangeTy> RangesCollection;
56 typedef typename RangesCollection::iterator RangesCollectionIt;
57 typedef typename RangesCollection::const_iterator RangesCollectionConstIt;
58 typedef IntegersSubsetMapping<SuccessorClass, IntegersSubsetTy, IntTy> self;
62 typedef std::list<Cluster> CaseItems;
63 typedef typename CaseItems::iterator CaseItemIt;
64 typedef typename CaseItems::const_iterator CaseItemConstIt;
66 // TODO: Change unclean CRS prefixes to SubsetMap for example.
67 typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
68 typedef typename CRSMap::iterator CRSMapIt;
71 bool operator()(const Cluster &C1, const Cluster &C2) {
72 return C1.first < C2.first;
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 onLROpen(const IntTy &Pt,
219 SuccessorClass *RS) {
222 State = INTERSECT_OPENED;
225 assert(0 && "Got unexpected point.");
228 CurrentLSuccessor = LS;
229 CurrentRSuccessor = RS;
233 void onLRClose(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; }
252 // Don't public CaseItems itself. Don't allow edit the Items directly.
253 // Just present the user way to iterate over the internal collection
254 // sharing iterator, begin() and end(). Editing should be controlled by
256 typedef CaseItemIt RangeIterator;
258 typedef std::pair<SuccessorClass*, IntegersSubsetTy> Case;
259 typedef std::list<Case> Cases;
260 typedef typename Cases::iterator CasesIt;
262 IntegersSubsetMapping() {
267 RangeIterator DummyErrItem;
268 return verify(DummyErrItem);
271 bool verify(RangeIterator& errItem) {
275 for (CaseItemIt j = Items.begin(), i = j++, e = Items.end();
277 if (isIntersected(i, j) && i->second != j->second) {
285 bool isOverlapped(self &RHS) {
286 if (Items.empty() || RHS.empty())
289 for (CaseItemIt L = Items.begin(), R = RHS.Items.begin(),
290 el = Items.end(), er = RHS.Items.end(); L != el && R != er;) {
292 const RangeTy &LRange = L->first;
293 const RangeTy &RRange = R->first;
295 if (LRange.getLow() > RRange.getLow()) {
296 if (RRange.isSingleNumber() || LRange.getLow() > RRange.getHigh())
300 } else if (LRange.getLow() < RRange.getLow()) {
301 if (LRange.isSingleNumber() || LRange.getHigh() < RRange.getLow())
305 } else // iRange.getLow() == jRange.getLow()
313 if (Items.size() < 2)
316 CaseItems OldItems = Items;
318 const IntTy *Low = &OldItems.begin()->first.getLow();
319 const IntTy *High = &OldItems.begin()->first.getHigh();
320 unsigned Weight = OldItems.begin()->first.Weight;
321 SuccessorClass *Successor = OldItems.begin()->second;
322 for (CaseItemIt j = OldItems.begin(), i = j++, e = OldItems.end();
324 if (isJoinable(i, j)) {
325 const IntTy *CurHigh = &j->first.getHigh();
326 Weight += j->first.Weight;
327 if (*CurHigh > *High)
330 RangeEx R(*Low, *High, Weight);
332 Low = &j->first.getLow();
333 High = &j->first.getHigh();
334 Weight = j->first.Weight;
335 Successor = j->second;
338 RangeEx R(*Low, *High, Weight);
340 // We recollected the Items, but we kept it sorted.
344 /// Adds a constant value.
345 void add(const IntTy &C, SuccessorClass *S = 0) {
351 void add(const IntTy &Low, const IntTy &High, SuccessorClass *S = 0) {
352 RangeTy R(Low, High);
355 void add(const RangeTy &R, SuccessorClass *S = 0) {
359 void add(const RangeEx &R, SuccessorClass *S = 0) {
360 Items.push_back(std::make_pair(R, S));
364 /// Adds all ranges and values from given ranges set to the current
366 void add(const IntegersSubsetTy &CRS, SuccessorClass *S = 0,
367 unsigned Weight = 0) {
368 unsigned ItemWeight = 1;
370 // Weight is associated with CRS, for now we perform a division to
371 // get the weight for each item.
372 ItemWeight = Weight / CRS.getNumItems();
373 for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
374 RangeTy R = CRS.getItem(i);
375 RangeEx REx(R, ItemWeight);
380 void add(self& RHS) {
381 Items.insert(Items.end(), RHS.Items.begin(), RHS.Items.end());
384 void add(self& RHS, SuccessorClass *S) {
385 for (CaseItemIt i = RHS.Items.begin(), e = RHS.Items.end(); i != e; ++i)
389 void add(const RangesCollection& RHS, SuccessorClass *S = 0) {
390 for (RangesCollectionConstIt i = RHS.begin(), e = RHS.end(); i != e; ++i)
394 /// Removes items from set.
395 void removeItem(RangeIterator i) { Items.erase(i); }
397 /// Moves whole case from current mapping to the NewMapping object.
398 void detachCase(self& NewMapping, SuccessorClass *Succ) {
399 for (CaseItemIt i = Items.begin(); i != Items.end();)
400 if (i->second == Succ) {
401 NewMapping.add(i->first, i->second);
407 /// Removes all clusters for given successor.
408 void removeCase(SuccessorClass *Succ) {
409 for (CaseItemIt i = Items.begin(); i != Items.end();)
410 if (i->second == Succ) {
416 /// Find successor that satisfies given value.
417 SuccessorClass *findSuccessor(const IntTy& Val) {
418 for (CaseItemIt i = Items.begin(); i != Items.end(); ++i) {
419 if (i->first.isInRange(Val))
425 /// Calculates the difference between this mapping and RHS.
426 /// THIS without RHS is placed into LExclude,
427 /// RHS without THIS is placed into RExclude,
428 /// THIS intersect RHS is placed into Intersection.
429 void diff(self *LExclude, self *Intersection, self *RExclude,
432 DiffStateMachine Machine(LExclude, Intersection, RExclude);
434 CaseItemConstIt L = Items.begin(), R = RHS.Items.begin();
435 while (L != Items.end() && R != RHS.Items.end()) {
436 const Cluster &LCluster = *L;
437 const RangeEx &LRange = LCluster.first;
438 const Cluster &RCluster = *R;
439 const RangeEx &RRange = RCluster.first;
441 if (LRange.getHigh() < RRange.getLow()) {
442 Machine.onLOpen(LRange.getLow(), LCluster.second);
443 Machine.onLClose(LRange.getHigh());
448 if (LRange.getLow() > RRange.getHigh()) {
449 Machine.onROpen(RRange.getLow(), RCluster.second);
450 Machine.onRClose(RRange.getHigh());
455 if (LRange.getLow() < RRange.getLow()) {
456 // May be opened in previous iteration.
457 if (!Machine.isLOpened())
458 Machine.onLOpen(LRange.getLow(), LCluster.second);
459 Machine.onROpen(RRange.getLow(), RCluster.second);
461 else if (RRange.getLow() < LRange.getLow()) {
462 if (!Machine.isROpened())
463 Machine.onROpen(RRange.getLow(), RCluster.second);
464 Machine.onLOpen(LRange.getLow(), LCluster.second);
467 Machine.onLROpen(LRange.getLow(), LCluster.second, RCluster.second);
469 if (LRange.getHigh() < RRange.getHigh()) {
470 Machine.onLClose(LRange.getHigh());
472 while(L != Items.end() && L->first.getHigh() < RRange.getHigh()) {
473 Machine.onLOpen(L->first.getLow(), L->second);
474 Machine.onLClose(L->first.getHigh());
478 else if (RRange.getHigh() < LRange.getHigh()) {
479 Machine.onRClose(RRange.getHigh());
481 while(R != RHS.Items.end() && R->first.getHigh() < LRange.getHigh()) {
482 Machine.onROpen(R->first.getLow(), R->second);
483 Machine.onRClose(R->first.getHigh());
488 Machine.onLRClose(LRange.getHigh());
494 if (L != Items.end()) {
495 if (Machine.isLOpened()) {
496 Machine.onLClose(L->first.getHigh());
500 while (L != Items.end()) {
501 LExclude->add(L->first, L->second);
504 } else if (R != RHS.Items.end()) {
505 if (Machine.isROpened()) {
506 Machine.onRClose(R->first.getHigh());
510 while (R != RHS.Items.end()) {
511 RExclude->add(R->first, R->second);
517 /// Builds the finalized case objects.
518 void getCases(Cases& TheCases, bool PreventMerging = false) {
519 //FIXME: PreventMerging is a temporary parameter.
520 //Currently a set of passes is still knows nothing about
521 //switches with case ranges, and if these passes meet switch
522 //with complex case that crashs the application.
523 if (PreventMerging) {
524 for (RangeIterator i = this->begin(); i != this->end(); ++i) {
525 RangesCollection SingleRange;
526 SingleRange.push_back(i->first);
527 TheCases.push_back(std::make_pair(i->second,
528 IntegersSubsetTy(SingleRange)));
533 for (RangeIterator i = this->begin(); i != this->end(); ++i)
534 TheCRSMap[i->second].push_back(i->first);
535 for (CRSMapIt i = TheCRSMap.begin(), e = TheCRSMap.end(); i != e; ++i)
536 TheCases.push_back(std::make_pair(i->first, IntegersSubsetTy(i->second)));
539 /// Builds the finalized case objects ignoring successor values, as though
540 /// all ranges belongs to the same successor.
541 IntegersSubsetTy getCase() {
542 RangesCollection Ranges;
543 for (RangeIterator i = this->begin(); i != this->end(); ++i)
544 Ranges.push_back(i->first);
545 return IntegersSubsetTy(Ranges);
548 /// Returns pointer to value of case if it is single-numbered or 0
550 const IntTy* getCaseSingleNumber(SuccessorClass *Succ) {
551 const IntTy* Res = 0;
552 for (CaseItemIt i = Items.begin(); i != Items.end(); ++i)
553 if (i->second == Succ) {
554 if (!i->first.isSingleNumber())
559 Res = &(i->first.getLow());
564 /// Returns true if there is no ranges and values inside.
565 bool empty() const { return Items.empty(); }
569 // Don't reset Sorted flag:
570 // 1. For empty mapping it matters nothing.
571 // 2. After first item will added Sorted flag will cleared.
574 // Returns number of clusters
575 unsigned size() const {
579 RangeIterator begin() { return Items.begin(); }
580 RangeIterator end() { return Items.end(); }
584 typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;
588 #endif /* LLVM_SUPPORT_INTEGERSSUBSETMAPPING_CRSBUILDER_H */