-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionSummary.h
More file actions
470 lines (382 loc) · 11.4 KB
/
FunctionSummary.h
File metadata and controls
470 lines (382 loc) · 11.4 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#ifndef __FUNCTION_SUMMARY__
#define __FUNCTION_SUMMARY__
using namespace llvm;
enum class ParamStateType {
// TODO: Do we need to model the partial order?
EMPTY_KEY, // for hash table key
TOMBSTONE_KEY, // for hash table key
BOTTOM,
NON_PMEM,
DIRTY_CAPTURED,
DIRTY_ESCAPED, // 0x5
CLWB_CAPTURED,
CLWB_ESCAPED,
CLEAN_CAPTURED,
CLEAN_ESCAPED,
TOP // 0xa
};
class ParamState {
public:
ParamState() : state(ParamStateType::TOP) {}
ParamState(ParamStateType state) : state(state) {}
ParamStateType get_state() const {
return state;
}
void setState(ParamStateType s) {
state = s;
}
std::string print() {
switch (state) {
case ParamStateType::EMPTY_KEY:
return "EMPTY_KEY";
case ParamStateType::TOMBSTONE_KEY:
return "TOMBSTONE_KEY";
case ParamStateType::BOTTOM:
return "BOTTOM";
case ParamStateType::NON_PMEM:
return "NON_PMEM";
case ParamStateType::DIRTY_CAPTURED:
return "DIRTY_CAPTURED";
case ParamStateType::DIRTY_ESCAPED:
return "DIRTY_ESCAPED";
case ParamStateType::CLWB_CAPTURED:
return "CLWB_CAPTURED";
case ParamStateType::CLWB_ESCAPED:
return "CLWB_ESCAPED";
case ParamStateType::CLEAN_CAPTURED:
return "CLEAN_CAPTURED";
case ParamStateType::CLEAN_ESCAPED:
return "CLEAN_ESCAPED";
case ParamStateType::TOP:
return "TOP";
default:
return "UNKNOWN";
}
}
bool isDirty() {
return state == ParamStateType::DIRTY_CAPTURED || state == ParamStateType::DIRTY_ESCAPED;
}
bool isClwb() {
return state == ParamStateType::CLWB_CAPTURED || state == ParamStateType::CLWB_ESCAPED;
}
bool isClean() {
return state == ParamStateType::TOP ||
state == ParamStateType::CLEAN_CAPTURED || state == ParamStateType::CLEAN_ESCAPED;
}
bool isCaptured() {
return state == ParamStateType::TOP || state == ParamStateType::DIRTY_CAPTURED ||
state == ParamStateType::CLWB_CAPTURED || state == ParamStateType::CLEAN_CAPTURED;
}
bool isEscaped() {
return state == ParamStateType::DIRTY_ESCAPED || state == ParamStateType::CLWB_ESCAPED ||
state == ParamStateType::CLEAN_ESCAPED;
}
bool isDirtyEscaped() {
return state == ParamStateType::DIRTY_ESCAPED;
}
bool isNonPmem() {
return state == ParamStateType::NON_PMEM;
}
inline bool isLowerThan(const ParamState& other) const {
return isLowerThan(other.get_state());
}
// Check if `this` is strictly lower than `other` in lattice
// Caution: returning false could also mean that two states are uncomparable
inline bool isLowerThan(const ParamStateType other_state) const {
if (state == ParamStateType::EMPTY_KEY || state == ParamStateType::TOMBSTONE_KEY ||
state == ParamStateType::NON_PMEM || other_state == ParamStateType::EMPTY_KEY ||
other_state == ParamStateType::TOMBSTONE_KEY || other_state == ParamStateType::NON_PMEM) {
return false;
}
switch (state) {
case ParamStateType::BOTTOM:
if (other_state != ParamStateType::BOTTOM)
return true;
break;
case ParamStateType::DIRTY_ESCAPED:
if (other_state != ParamStateType::BOTTOM &&
other_state != ParamStateType::DIRTY_ESCAPED)
return true;
break;
case ParamStateType::DIRTY_CAPTURED:
if (other_state == ParamStateType::CLWB_CAPTURED ||
other_state == ParamStateType::CLEAN_CAPTURED ||
other_state == ParamStateType::TOP)
return true;
break;
case ParamStateType::CLWB_ESCAPED:
if (other_state == ParamStateType::CLWB_CAPTURED ||
other_state == ParamStateType::CLEAN_ESCAPED ||
other_state == ParamStateType::CLEAN_CAPTURED ||
other_state == ParamStateType::TOP)
return true;
break;
case ParamStateType::CLWB_CAPTURED:
case ParamStateType::CLEAN_ESCAPED:
if (other_state == ParamStateType::CLEAN_CAPTURED ||
other_state == ParamStateType::TOP)
return true;
break;
case ParamStateType::CLEAN_CAPTURED:
if (other_state == ParamStateType::TOP)
return true;
break;
case ParamStateType::TOP:
break;
default:
break;
}
return false;
}
inline bool operator ==(const ParamState& other) const {
return state == other.get_state();
}
inline bool operator !=(const ParamState& other) const {
return state != other.get_state();
}
private:
ParamStateType state;
};
struct DirtyBytesInfo {
// isComplex: when there are too many stores that are not consecutive in byte positions.
// simply approximate the object as dirty in all btyes
bool isComplex = false;
std::vector<std::pair<int, int>> lst;
void push(int i, int j) {
//errs() << "[" << i << ", " << j << ") is pushed\n";
lst.emplace_back(i, j);
}
std::vector<std::pair<int, int>> * getDirtyBytes() {
return &lst;
}
};
struct OutputState {
SmallVector<ParamState, 8> AbstractOutputState;
// So far it is only accurate when the input state is clean
// Because when the input state is dirty, all bytes are approximated as dirty.
std::vector<DirtyBytesInfo *> *DirtyBytesList;
// true if a parameter has no uses
std::vector<bool> UntouchedParamList;
bool hasRetVal;
ParamState retVal;
// Whether this function ever marks anything as escaped and dirty
bool marksEscDirObj;
// Whether this function ever marks anything as escaped and dirty, when all parameters are captured, clean, or non_pmem
bool marksEscDirObjConditional;
bool checkUntouched;
OutputState() :
AbstractOutputState(),
DirtyBytesList(NULL),
UntouchedParamList(),
hasRetVal(false),
marksEscDirObj(false),
marksEscDirObjConditional(false),
checkUntouched(false)
{
retVal.setState(ParamStateType::BOTTOM);
}
DirtyBytesInfo * getOrCreateDirtyBytesInfo(unsigned i) {
if (DirtyBytesList == NULL)
DirtyBytesList = new std::vector<DirtyBytesInfo *>();
if (DirtyBytesList->size() < i + 1)
DirtyBytesList->resize(i + 1);
DirtyBytesInfo *info = (*DirtyBytesList)[i];
if (info == NULL) {
info = new DirtyBytesInfo();
(*DirtyBytesList)[i] = info;
}
return info;
}
DirtyBytesInfo * getDirtyBytesInfo(unsigned i) {
return (*DirtyBytesList)[i];
}
ParamStateType getStateType(unsigned i) {
return AbstractOutputState[i].get_state();
}
ParamState& getState(unsigned i) {
return AbstractOutputState[i];
}
bool isUntouched(unsigned i) {
if (UntouchedParamList.size() <= i)
return false;
return UntouchedParamList[i];
}
void setUntouched(unsigned i) {
if (UntouchedParamList.size() <= i)
UntouchedParamList.resize(i+1);
UntouchedParamList[i] = true;
}
void dump() {
errs() << "Abstract output state: ";
for (ParamState &I : AbstractOutputState) {
errs() << I.print() << "\t";
}
errs() << "\n";
if (hasRetVal)
errs() << "Abstract return state: " << retVal.print() << "\n";
errs() << "\n";
}
};
struct CallingContext {
SmallVector<Value *, 8> parameters;
SmallVector<ParamState, 8> AbstractInputState;
CallingContext() {}
CallingContext(CallingContext *other) {
parameters = other->parameters;
AbstractInputState = other->AbstractInputState;
}
void addAbsInput(ParamStateType s) {
AbstractInputState.push_back(ParamState(s));
}
ParamStateType getStateType(unsigned i) {
return AbstractInputState[i].get_state();
}
ParamState& getState(unsigned i) {
return AbstractInputState[i];
}
void dump() {
/*
errs() << "Original parameters: ";
for (Value *V : parameters) {
errs() << *V << "\t";
}
errs() << "\n";
*/
errs() << "Abstract input state: ";
for (ParamState&I : AbstractInputState) {
errs() << I.print() << "\t";
}
errs() << "\n";
}
};
hash_code hash_value(const ParamState &value) {
return ::llvm::hashing::detail::hash_integer_value(
static_cast<uint64_t>(value.get_state()));
}
// lhs >= rhs ?
bool isStateVectorHigherOrEqual(SmallVector<ParamState, 8> &lhs, SmallVector<ParamState, 8> &rhs) {
assert(lhs.size() == rhs.size());
for (unsigned i = 0; i < lhs.size(); i++) {
if (rhs[i].isLowerThan(lhs[i]) != true &&
lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
class FunctionSummary {
public:
struct SummaryDenseMapInfo;
//using iterator = DenseMap<SmallVector<ParamState, 8>, OutputState *, SummaryDenseMapInfo>::iterator;
typedef DenseMap<SmallVector<ParamState, 8>, OutputState *, SummaryDenseMapInfo> result_map_t;
FunctionSummary() {
ArgSize = 0;
has_release_ops = false;
just_set_release = false;
is_fenced = false;
}
struct SummaryDenseMapInfo {
static SmallVector<ParamState, 8> getEmptyKey() {
return {ParamState(ParamStateType::EMPTY_KEY)};
}
static SmallVector<ParamState, 8> getTombstoneKey() {
return {ParamState(ParamStateType::TOMBSTONE_KEY)};
}
static unsigned getHashValue(const SmallVector<ParamState, 8> &V) {
return static_cast<unsigned>(hash_combine_range(V.begin(), V.end()));
}
static bool isEqual(const SmallVector<ParamState, 8> &LHS,
const SmallVector<ParamState, 8> &RHS) {
return LHS == RHS;
}
};
OutputState * getResult(CallingContext *Context) {
return ResultMap.lookup(Context->AbstractInputState);
}
OutputState * getOrCreateResult(CallingContext *Context) {
OutputState *state = ResultMap.lookup(Context->AbstractInputState);
if (state == NULL) {
state = new OutputState();
ResultMap[Context->AbstractInputState] = state;
}
return state;
}
OutputState * getLeastUpperResult(CallingContext *Context) {
SmallVector<ParamState, 8> *leastUpperContext = NULL;
for (result_map_t::iterator it = ResultMap.begin(); it != ResultMap.end(); it++) {
SmallVector<ParamState, 8> *cur = &it->first;
if (isStateVectorHigherOrEqual(*cur, Context->AbstractInputState)) {
if (leastUpperContext == NULL)
leastUpperContext = cur;
else if (isStateVectorHigherOrEqual(*leastUpperContext, *cur))
leastUpperContext = cur;
// TODO: Multiple higher contexts
}
}
if (leastUpperContext == NULL)
return NULL;
/*
errs() << "Using least upper context: \n";
errs() << "Abstract input state: ";
for (ParamState& I : *leastUpperContext) {
errs() << I.print() << "\t";
}
errs() << "\n";
*/
return ResultMap.lookup(*leastUpperContext);
}
result_map_t * getResultMap() {
return &ResultMap;
}
bool hasRelease() {
return has_release_ops;
}
void setRelease() {
if (has_release_ops == false)
just_set_release = true;
else
just_set_release = false;
has_release_ops = true;
}
bool justSetRelease() {
return just_set_release;
}
void setFenced() {
is_fenced = true;
}
bool isFenced() {
return is_fenced;
}
private:
result_map_t ResultMap;
unsigned ArgSize;
bool has_release_ops;
bool just_set_release;
// Whether this function has a memory fence through all paths to exits
bool is_fenced;
};
namespace llvm {
template<> struct DenseMapInfo<CallingContext *> {
static inline CallingContext * getEmptyKey() {
uintptr_t Val = static_cast<uintptr_t>(-1);
Val <<= PointerLikeTypeTraits<CallingContext *>::NumLowBitsAvailable;
return reinterpret_cast<CallingContext *>(Val);
}
static inline CallingContext * getTombstoneKey() {
uintptr_t Val = static_cast<uintptr_t>(~1U);
Val <<= PointerLikeTypeTraits<CallingContext *>::NumLowBitsAvailable;
return reinterpret_cast<CallingContext *>(Val);
}
static unsigned getHashValue(const CallingContext *V) {
return static_cast<unsigned>(hash_combine_range(V->AbstractInputState.begin(), V->AbstractInputState.end()));
}
static bool isEqual(const CallingContext *LHS,
const CallingContext *RHS) {
if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
LHS == getTombstoneKey() || RHS == getTombstoneKey())
return LHS == RHS;
return LHS->AbstractInputState == RHS->AbstractInputState;
}
};
}
#endif