aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/Support/IntegersSubsetMapping.h
blob: c79b3c16845bbb663e40c5d6e12ba1ac18d1584a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//===- IntegersSubsetMapping.h - Mapping subset ==> Successor ---*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
/// @file
/// IntegersSubsetMapping is mapping from A to B, where
/// Items in A is subsets of integers,
/// Items in B some pointers (Successors).
/// If user which to add another subset for successor that is already
/// exists in mapping, IntegersSubsetMapping merges existing subset with
/// added one.
//
//===----------------------------------------------------------------------===//

#ifndef CRSBUILDER_H_
#define CRSBUILDER_H_

#include "llvm/Support/IntegersSubset.h"
#include <list>
#include <map>
#include <vector>

namespace llvm {

template <class SuccessorClass,
          class IntegersSubsetTy = IntegersSubset,
          class IntTy = IntItem>
class IntegersSubsetMapping {
public:
  
  typedef IntRange<IntTy> RangeTy;
  
  struct RangeEx : public RangeTy {
    RangeEx() : Weight(1) {}
    RangeEx(const RangeTy &R) : RangeTy(R), Weight(1) {}
    RangeEx(const IntTy &C) : RangeTy(C), Weight(1) {}
    RangeEx(const IntTy &L, const IntTy &H) : RangeTy(L, H), Weight(1) {}
    RangeEx(const IntTy &L, const IntTy &H, unsigned W) :
      RangeTy(L, H), Weight(W) {}
    unsigned Weight;
  };

  typedef std::pair<RangeEx, SuccessorClass*> Cluster;

protected:

  typedef std::list<Cluster> CaseItems;
  typedef typename CaseItems::iterator CaseItemIt;
  typedef typename CaseItems::const_iterator CaseItemConstIt;
  
  typedef std::list<RangeTy> RangesCollection;
  typedef typename RangesCollection::iterator RangesCollectionIt;
  
  // TODO: Change unclean CRS prefixes to SubsetMap for example.
  typedef std::map<SuccessorClass*, RangesCollection > CRSMap;
  typedef typename CRSMap::iterator CRSMapIt;

  struct ClustersCmp {
    bool operator()(const Cluster &C1, const Cluster &C2) {
      return C1.first < C2.first;
    }
  };
  
  struct ClusterLefterThan {
    bool operator()(const Cluster &C, const RangeTy &R) {
      return C.first.getHigh() < R.getLow();
    }
  };  
  
  CaseItems Items;
  bool Sorted;
  
  bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) {
    return LItem->first.getHigh() >= RItem->first.getLow();
  }

  bool isJoinable(CaseItemIt& LItem, CaseItemIt& RItem) {
    if (LItem->second != RItem->second) {
      assert(!isIntersected(LItem, RItem) &&
             "Intersected items with different successors!");
      return false;
    }
    APInt RLow = RItem->first.getLow();
    if (RLow != APInt::getNullValue(RLow.getBitWidth()))
      --RLow;
    return LItem->first.getHigh() >= RLow;
  }
  
  void sort() {
    if (!Sorted) {
      std::vector<Cluster> clustersVector;
      clustersVector.reserve(Items.size());
      clustersVector.insert(clustersVector.begin(), Items.begin(), Items.end());
      std::sort(clustersVector.begin(), clustersVector.end(), ClustersCmp());
      Items.clear();
      Items.insert(Items.begin(), clustersVector.begin(), clustersVector.end());
      Sorted = true;
    }
  }
  
  void exclude(CaseItemIt &beginIt, RangeTy &R) {
    
    std::list<CaseItemIt> ToBeErased;

    CaseItemIt endIt = Items.end();
    CaseItemIt It =
          std::lower_bound(beginIt, Items.end(), R, ClusterLefterThan());
    
    if (It == endIt)
      return;

    if (It->first.getLow() < R.getLow())
      Items.insert(It, std::make_pair(
          RangeTy(It->first.getLow(), R.getLow()-1),
          It->second));

    do
    ToBeErased.push_back(It++);
    while (It != endIt && It->first.getLow() <= R.getHigh());

    beginIt = It;
    
    CaseItemIt &LastRemoved = *(--ToBeErased.end());
    if (LastRemoved->first.getHigh() > R.getHigh())
      beginIt = Items.insert(LastRemoved, std::make_pair(
          RangeTy(R.getHigh() + 1, LastRemoved->first.getHigh()),
          LastRemoved->second
          ));
    
    for (typename std::list<CaseItemIt>::iterator i = ToBeErased.begin(),
         e = ToBeErased.end(); i != e; ++i)
      Items.erase(*i);
  }   

public:
  
  // Don't public CaseItems itself. Don't allow edit the Items directly. 
  // Just present the user way to iterate over the internal collection
  // sharing iterator, begin() and end(). Editing should be controlled by
  // factory.
  typedef CaseItemIt RangeIterator;
  
  typedef std::pair<SuccessorClass*, IntegersSubsetTy> Case;
  typedef std::list<Case> Cases;
  
  IntegersSubsetMapping() {
    Sorted = false;
  }
  
  bool verify(RangeIterator& errItem) {
    if (Items.empty())
      return true;
    sort();
    for (CaseItemIt j = Items.begin(), i = j++, e = Items.end();
         j != e; i = j++) {
      if (isIntersected(i, j) && i->second != j->second) {
        errItem = j;
        return false;
      }
    }
    return true;
  }
  
  void optimize() {
    if (Items.size() < 2)
      return;
    sort();
    CaseItems OldItems = Items;
    Items.clear();
    const IntTy *Low = &OldItems.begin()->first.getLow();
    const IntTy *High = &OldItems.begin()->first.getHigh();
    unsigned Weight = 1;
    SuccessorClass *Successor = OldItems.begin()->second;
    for (CaseItemIt j = OldItems.begin(), i = j++, e = OldItems.end();
         j != e; i = j++) {
      if (isJoinable(i, j)) {
        const IntTy *CurHigh = &j->first.getHigh();
        ++Weight;
        if (*CurHigh > *High)
          High = CurHigh;
      } else {
        RangeEx R(*Low, *High, Weight);
        add(R, Successor);
        Low = &j->first.getLow();
        High = &j->first.getHigh(); 
        Weight = 1;
        Successor = j->second;
      }
    }
    RangeEx R(*Low, *High, Weight);
    add(R, Successor);
    // We recollected the Items, but we kept it sorted.
    Sorted = true;
  }
  
  /// Adds a constant value.
  void add(const IntTy &C, SuccessorClass *S = 0) {
    RangeTy R(C);
    add(R, S);
  }
  
  /// Adds a range.
  void add(const IntTy &Low, const IntTy &High, SuccessorClass *S = 0) {
    RangeTy R(Low, High);
    add(R, S);
  }
  void add(const RangeTy &R, SuccessorClass *S = 0) {
    RangeEx REx = R;
    add(REx, S);
  }   
  void add(const RangeEx &R, SuccessorClass *S = 0) {
    Items.push_back(std::make_pair(R, S));
    Sorted = false;
  }  
  
  /// Adds all ranges and values from given ranges set to the current
  /// mapping.
  void add(const IntegersSubsetTy &CRS, SuccessorClass *S = 0) {
    for (unsigned i = 0, e = CRS.getNumItems(); i < e; ++i) {
      RangeTy R = CRS.getItem(i);
      add(R, S);
    }
  }
  
  /// Removes items from set.
  void removeItem(RangeIterator i) { Items.erase(i); }
  
  // Excludes RHS subset from current mapping. RHS should consists of non
  // overlapped ranges only and sorted from left to the right.
  // method will have unpredictional behaviour in another case. 
  void exclude(IntegersSubsetTy &RHS) {
    CaseItemIt startIt = begin();
    for (unsigned i = 0, e = RHS.getNumItems();
         i != e && startIt != end(); ++i) {
      RangeTy R = RHS.getItem(i);
      exclude(startIt, R);
    }
  }  
  
  /// Builds the finalized case objects.
  void getCases(Cases& TheCases) {
    CRSMap TheCRSMap;
    for (RangeIterator i = this->begin(); i != this->end(); ++i)
      TheCRSMap[i->second].push_back(i->first);
    for (CRSMapIt i = TheCRSMap.begin(), e = TheCRSMap.end(); i != e; ++i)
      TheCases.push_back(std::make_pair(i->first, IntegersSubsetTy(i->second)));
  }
  
  /// Builds the finalized case objects ignoring successor values, as though
  /// all ranges belongs to the same successor.
  IntegersSubsetTy getCase() {
    RangesCollection Ranges;
    for (RangeIterator i = this->begin(); i != this->end(); ++i)
      Ranges.push_back(i->first);
    return IntegersSubsetTy(Ranges);
  }  
  
  /// Returns true if there is no ranges and values inside.
  bool empty() const { return Items.empty(); }
  
  void clear() {
    Items.clear();
    // Don't reset Sorted flag:
    // 1. For empty mapping it matters nothing.
    // 2. After first item will added Sorted flag will cleared.
  }  
  
  RangeIterator begin() { return Items.begin(); }
  RangeIterator end() { return Items.end(); }
};

class BasicBlock;
typedef IntegersSubsetMapping<BasicBlock> IntegersSubsetToBB;

}

#endif /* CRSBUILDER_H_ */