-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.h
419 lines (323 loc) · 9.91 KB
/
analysis.h
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#pragma once
#include <ciso646>
#include <cassert>
#include <cstdint>
#include "patterns.h"
namespace analysis {
class Range;
class CallTarget;
class TypeShape;
class Constants;
}
/** A simple abstract value for range & constant analysis.
Works only with integers, and contains min & max values.
*/
class RangeValue {
public:
int min() const {
return min_;
}
int max() const {
return max_;
}
bool isTop() const {
return min_ == INT_MIN and max_ == INT_MAX;
}
bool isBottom() const {
return min_ == INT_MAX and max_ == INT_MIN;
}
/** Equality, because we do not want to assume identities */
bool operator == (RangeValue const & other) const {
return min_ == other.min_ and max_ == other.max_;
}
/** Lattice ordering implementation. */
bool operator <= (RangeValue const & other) const {
if (isBottom() or other.isTop())
return true;
return min_ >= other.min_ and max_ <= other.max_;
}
bool mergeWith(RangeValue const & other) {
bool result = false;
if (other.min_ < min_) {
result = true;
min_ = other.min_;
}
if (other.max_ > max_) {
result = true;
max_ = other.max_;
}
return result;
}
private:
friend std::ostream & operator << (std::ostream & stream, RangeValue const & value) {
stream << "[ " << value.min_ << " ; " << value.max_ << " ]";
return stream;
}
friend class analysis::Range;
RangeValue(int min, int max) :
min_(min),
max_(max) {}
int min_;
int max_;
};
/** A simple call target abstract value as discussed with Jan.
Target specifies the called function (actual target, not a symbol, char * is chosen here only for printing purposes of the mockup example). The call target can either be certain, in which case we are statically sure it will call the required function, or possible, which can have many meanings, but in the simplest form it is a hint to the optimizer that a fastpath to that target might be useful.
*/
class CallTargetValue {
public:
enum class Accuracy {
Possible,
Certain,
};
Accuracy accuracy() const {
return accuracy_;
}
char const * target() const {
return target_;
}
bool isTop() const {
return target_ == nullptr and accuracy_ == Accuracy::Possible;
}
bool isBottom() const {
return target_ == nullptr and accuracy_ == Accuracy::Certain;
}
bool operator == (CallTargetValue const & other) const {
return target_ == other.target_ and accuracy_ == other.accuracy_;
}
bool operator <= (CallTargetValue const & other) const {
if (isBottom() or other.isTop())
return true;
if (target_ != other.target_)
return false;
return (accuracy_ == Accuracy::Possible or other.accuracy_ == Accuracy::Certain);
}
bool mergeWith(CallTargetValue const & other) {
bool result = false;
if (target_ != other.target_) {
result = true;
target_ = nullptr;
}
if (accuracy_ == Accuracy::Certain and other.accuracy_ == Accuracy::Possible) {
result = true;
accuracy_ = Accuracy::Possible;
}
return result;
}
private:
friend std::ostream & operator << (std::ostream & stream, CallTargetValue const & value) {
if (value.target_ == nullptr)
stream << "top or bottom";
else
if (value.accuracy_ == CallTargetValue::Accuracy::Possible)
stream << "possible " << value.target_;
else
stream << value.target_;
return stream;
}
friend class analysis::CallTarget;
CallTargetValue(Accuracy accuracy, char const * target) :
accuracy_(accuracy),
target_(target) {}
Accuracy accuracy_;
char const * target_;
};
/** Mockup type and shape value to accomodate the naked int in Jan's example.
*/
class TypeShapeValue {
public:
enum class Type {
Bottom,
Int,
Double,
Any,
};
enum class Shape {
Naked,
Any,
};
Type type() const {
return type_;
}
Shape shape() const {
return shape_;
}
bool isBottom() const {
return type_ == Type::Bottom;
}
bool isTop() const {
return type_ == Type::Any and shape_ == Shape::Any;
}
bool operator == (TypeShapeValue const & other) const {
return type_ == other.type_ and shape_ == other.shape_;
}
bool operator <= (TypeShapeValue const & other) const {
if (isBottom() or other.isTop())
return true;
// TODO fill this in based on the lattice
return false;
}
bool mergeWith(TypeShapeValue const & other) {
return false;
}
private:
friend std::ostream & operator << (std::ostream & stream, TypeShapeValue const & value) {
if (value.shape_ == TypeShapeValue::Shape::Naked)
stream << "naked ";
switch (value.type_) {
case TypeShapeValue::Type::Bottom:
stream << "bottom";
break;
case TypeShapeValue::Type::Any:
stream << "any";
break;
case TypeShapeValue::Type::Int:
stream << "Int";
break;
case TypeShapeValue::Type::Double:
stream << "Double";
break;
}
return stream;
}
friend class analysis::TypeShape;
TypeShapeValue(Type type, Shape shape) :
type_(type),
shape_(shape) {}
Type type_;
Shape shape_;
};
/** Simply tells us whether it is a constant, or not. The int * is stupid, but assumes that we will have a pointer to the constant SEXP. It can of course be implemented in myriads of ther ways and is not important now.
*/
class ConstantsValue {
public:
int * value() const {
return value_;
}
bool isConstant() const {
return (uintptr_t) value_ > 1;
}
bool isTop() const {
return value_ == nullptr;
}
bool isBottom() const {
return value_ == (int*)1;
}
bool operator == (ConstantsValue const & other) const {
return value_ == other.value_;
}
bool operator <= (ConstantsValue const & other) const {
if (isBottom() or other.isTop())
return true;
return value_ == other.value_;
}
bool mergeWith(ConstantsValue const & other) {
return false;
}
private:
friend std::ostream & operator << (std::ostream & stream, ConstantsValue const & value) {
if (value.value_ != nullptr)
stream << "constant";
else
stream << "top or bottom";
return stream;
}
friend class analysis::Constants;
ConstantsValue(int * value):
value_(value) {
}
int * value_;
};
/** Mockup analyses, I am only concerned with their vaye types */
namespace analysis {
class Analysis {
};
class Range : public Analysis {
public:
typedef RangeValue Value;
typedef TypePattern<Range> Pattern;
static Value constant(int value) {
return Value(value, value);
}
static Value range(int min, int max) {
assert(min <= max and "min must be smaller or equal to max");
return Value(min, max);
}
static Value top() {
return Value(INT_MIN, INT_MAX);
}
static Value bottom() {
return Value(INT_MAX, INT_MIN);
}
Value const & operator [] (::Pattern * p) {
static Value v(top());
return v;
}
};
class CallTarget : public Analysis {
public:
typedef CallTargetValue Value;
typedef TypePattern<CallTarget> Pattern;
static Pattern * builtin(char const * target) {
return new Pattern(certain(target));
}
static Value certain(char const * target) {
return Value(Value::Accuracy::Certain, target);
}
static Value possible(char const * target) {
return Value(Value::Accuracy::Possible, target);
}
static Value top() {
return Value(Value::Accuracy::Possible, nullptr);
}
static Value bottom() {
return Value(Value::Accuracy::Certain, nullptr);
}
Value const & operator [] (::Pattern * p) {
static Value v(top());
return v;
}
};
class TypeShape : public Analysis {
public:
typedef TypeShapeValue Value;
typedef TypePattern<TypeShape> Pattern;
// creating the patterns can be simplified like this
static Pattern * nakedInt() {
return new Pattern(naked(Value::Type::Int));
}
static Value create(Value::Type type, Value::Shape shape = Value::Shape::Any) {
return Value(type, shape);
}
static Value naked(Value::Type type) {
return Value(type, Value::Shape::Naked);
}
static Value top() {
return Value(Value::Type::Any, Value::Shape::Any);
}
static Value bottom() {
return Value(Value::Type::Bottom, Value::Shape::Any);
}
Value const & operator [] (::Pattern * p) {
static Value v(top());
return v;
}
};
class Constants : public Analysis {
public:
typedef ConstantsValue Value;
typedef TypePattern<Constants> Pattern;
static Value constant(int * what) {
return Value(what);
}
static Value top() {
return Value(nullptr);
}
static Value bottom() {
return Value((int*)1);
}
Value const & operator [] (::Pattern * p) const {
static Value v(top());
return v;
}
};
}