-
Notifications
You must be signed in to change notification settings - Fork 140
/
Effect.cpp
655 lines (588 loc) · 13.1 KB
/
Effect.cpp
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2010, 2011, 2013, 2015, 2017 The University of Utah
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <cassert>
#include "Effect.h"
#include "Variable.h"
#include "ExpressionVariable.h"
#include "Block.h"
#include "Type.h"
#include "util.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////////
/*
* The empty effect.
*/
const Effect Effect::empty_effect;
///////////////////////////////////////////////////////////////////////////////
/*
* Stuff that so badly wants to be rewritten using decent STL containers and
* algorithms.
*/
static bool
non_empty_intersection(const vector<const Variable *> &va,
const vector<const Variable *> &vb)
{
vector<const Variable *>::size_type va_len = va.size();
vector<const Variable *>::size_type vb_len = vb.size();
vector<const Variable *>::size_type i;
vector<const Variable *>::size_type j;
for (i = 0; i < va_len; ++i) {
for (j = 0; j < vb_len; ++j) {
if (va[i]->match(vb[j]) || vb[j]->match(va[i])) {
return true;
}
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
/*
*
*/
Effect::Effect(void) :
read_vars(0),
write_vars(0),
pure(true),
side_effect_free(true)
{
// Nothing else to do.
}
/*
*
*/
Effect::Effect(const Effect &e) :
read_vars(e.read_vars),
write_vars(e.write_vars),
pure(e.pure),
side_effect_free(e.side_effect_free)
{
// Nothing else to do.
}
/*
*
*/
Effect::~Effect(void)
{
// Nothing else to do.
}
/*
*
*/
Effect &
Effect::operator=(const Effect &e)
{
if (this == &e) {
return *this;
}
read_vars = e.read_vars;
write_vars = e.write_vars;
pure = e.pure;
side_effect_free = e.side_effect_free;
return *this;
}
/*
*
*/
void
Effect::read_var(const Variable *v)
{
if (!is_read(v)) {
read_vars.push_back(v);
}
pure &= (v->is_const() && !v->is_volatile() && !v->is_access_once());
side_effect_free &= (!v->is_volatile() && !v->is_access_once());
}
void
Effect::access_deref_volatile(const Variable *v, int deref_level)
{
assert(v && "NULL Variable!");
if (!CGOptions::strict_volatile_rule())
return;
while (deref_level > 0) {
if (v->is_volatile_after_deref(deref_level)) {
side_effect_free = false;
return;
}
deref_level--;
}
}
void
Effect::write_var(const Variable *v)
{
if (!is_written(v)) {
write_vars.push_back(v);
}
// pure = pure;
// TODO: not quite correct below ---
// --- but I think we want "side_effect_free" to mean "volatile_free".
side_effect_free &= (!v->is_volatile() && !v->is_access_once());
// side_effect_free = false;
}
void
Effect::write_var_set(const std::vector<const Variable *>& vars)
{
for (size_t i=0; i<vars.size(); i++) {
write_var(vars[i]);
}
}
/*
*
*/
void
Effect::add_effect(const Effect &e, bool include_lhs_effects)
{
if (this == &e) {
return;
}
// Stuff that so badly wants to be rewritten using decent STL containers
// and algorithms... compute the union effect.
vector<const Variable *>::size_type len;
vector<const Variable *>::size_type i;
len = e.read_vars.size();
for (i = 0; i < len; ++i) {
// this->read_var(e.read_vars[i]);
if (!is_read(e.read_vars[i])) {
this->read_vars.push_back(e.read_vars[i]);
}
}
len = e.write_vars.size();
for (i = 0; i < len; ++i) {
// this->write_var(e.write_vars[i]);
if (!is_written(e.write_vars[i])) {
this->write_vars.push_back(e.write_vars[i]);
}
}
if (include_lhs_effects) {
add_variables_to_set(lhs_write_vars, e.get_lhs_write_vars());
}
pure &= e.pure;
side_effect_free &= e.side_effect_free;
}
/*
*
*/
void
Effect::add_external_effect(const Effect &e)
{
if (this == &e) {
return;
}
vector<Variable *>::size_type len;
vector<Variable *>::size_type i;
len = e.read_vars.size();
for (i = 0; i < len; ++i) {
if (e.read_vars[i]->is_global()) {
this->read_var(e.read_vars[i]);
}
}
len = e.write_vars.size();
for (i = 0; i < len; ++i) {
if (e.write_vars[i]->is_global()) {
this->write_var(e.write_vars[i]);
//Make sure the "purity" is set correctly
this->pure = false;
}
}
side_effect_free &= e.side_effect_free;
}
/*
* with call chains, we want to track write/read to stack
* variables of caller(s)
*/
void
Effect::add_external_effect(const Effect &e, std::vector<const Block*> call_chain)
{
if (this == &e) {
return;
}
vector<Variable *>::size_type len;
vector<Variable *>::size_type i, j;
len = e.read_vars.size();
for (i = 0; i < len; ++i) {
const Variable* var = e.read_vars[i];
if (var->is_global()) {
read_var(var);
}
else {
for (j=0; j<call_chain.size(); j++) {
const Block* b = call_chain[j];
if (b->is_var_on_stack(var)) {
break;
}
}
if (j < call_chain.size()) {
read_var(var);
}
}
}
len = e.write_vars.size();
for (i = 0; i < len; ++i) {
const Variable* var = e.write_vars[i];
if (var->is_global()) {
write_var(var);
//Make sure the "purity" is set correctly
pure = false;
}
else {
for (j=0; j<call_chain.size(); j++) {
const Block* b = call_chain[j];
if (b->is_var_on_stack(var)) {
break;
}
}
if (j < call_chain.size()) {
write_var(var);
pure = false;
}
}
}
side_effect_free &= e.side_effect_free;
}
/*
*
*
*/
bool
Effect::is_read(const Variable *v) const
{
if (std::find(read_vars.begin(), read_vars.end(), v) != read_vars.end()) {
return true;
}
// if we read a struct, presumingly all the fields are read too
// however we can not say the same thing for unions: reading a particular
// unions field can cause unspecified behaviors, while reading the whole
// union won't
if (v->field_var_of && v->field_var_of->type->eType == eStruct) {
return is_read(v->field_var_of);
}
return false;
}
/*
*
*/
bool
Effect::is_read(string vname) const
{
vector<Variable *>::size_type len = read_vars.size();
vector<Variable *>::size_type i;
for (i = 0; i < len; ++i) {
const string& rname = read_vars[i]->name;
size_t pos;
if (rname == vname) {
return true;
}
// match name of a struct field with struct
if (vname.find(".") != string::npos) {
string tmp = vname;
do {
pos = tmp.find_last_of(".");
tmp = tmp.substr(0, pos);
if (tmp == rname) {
return true;
}
} while (pos != string::npos);
}
// match name of a struct with struct field
if (rname.find(".") != string::npos) {
string tmp = rname;
do {
pos = tmp.find_last_of(".");
tmp = tmp.substr(0, pos);
if (tmp == vname) {
return true;
}
} while (pos != string::npos);
}
}
return false;
}
/*
*
*/
bool
Effect::is_written(const Variable *v) const
{
vector<Variable *>::size_type len = write_vars.size();
vector<Variable *>::size_type i;
for (i = 0; i < len; ++i) {
if (write_vars[i] == v) {
return true;
}
}
// if we write a struct/union, presumingly all the fields are written too
if (v->field_var_of) {
return is_written(v->field_var_of);
}
return false;
}
/*
*
*/
bool
Effect::is_written(string vname) const
{
vector<Variable *>::size_type len = write_vars.size();
vector<Variable *>::size_type i;
for (i = 0; i < len; ++i) {
const string& wname = write_vars[i]->name;
size_t pos;
if (wname == vname) {
return true;
}
// match name of a struct field with struct
if (vname.find(".") != string::npos) {
string tmp = vname;
do {
pos = tmp.find_last_of(".");
tmp = tmp.substr(0, pos);
if (tmp == wname) {
return true;
}
} while (pos != string::npos);
}
// match name of a struct with struct field
if (wname.find(".") != string::npos) {
string tmp = wname;
do {
pos = tmp.find_last_of(".");
tmp = tmp.substr(0, pos);
if (tmp == vname) {
return true;
}
} while (pos != string::npos);
}
}
return false;
}
/*
* whether any field of a struct/union is been read
*/
bool
Effect::field_is_read(const Variable *v) const
{
size_t j;
if (v->is_aggregate()) {
for (j=0; j<v->field_vars.size(); j++) {
Variable* field_var = v->field_vars[j];
if (is_read(field_var) || field_is_read(field_var)) {
return true;
}
}
}
return false;
}
/*
* whether any field of a struct/union is been written
*/
bool
Effect::field_is_written(const Variable *v) const
{
size_t j;
if (v->is_aggregate()) {
for (j=0; j<v->field_vars.size(); j++) {
Variable* field_var = v->field_vars[j];
if (is_written(field_var) || field_is_written(field_var)) {
return true;
}
}
}
return false;
}
bool
Effect::sibling_union_field_is_read(const Variable *v) const
{
const Variable* you = v->get_collective()->get_container_union();
if (you) {
for (size_t i=0; i<read_vars.size(); i++) {
const Variable* me = read_vars[i]->get_collective()->get_container_union();
if (you == me) {
return true;
}
}
}
return false;
}
bool
Effect::sibling_union_field_is_written(const Variable *v) const
{
const Variable* you = v->get_collective()->get_container_union();
if (you) {
for (size_t i=0; i<write_vars.size(); i++) {
const Variable* me = write_vars[i]->get_collective()->get_container_union();
if (you == me) {
return true;
}
}
}
return false;
}
bool
Effect::is_read_partially(const Variable* v) const
{
return is_read(v) || field_is_read(v) || sibling_union_field_is_read(v);
}
bool
Effect::is_written_partially(const Variable* v) const
{
return is_written(v) || field_is_written(v) || sibling_union_field_is_written(v);
}
/*
* consolidate the read/write set
*/
void
Effect::consolidate(void)
{
size_t i;
size_t len = read_vars.size();
for (i=0; i<len; i++) {
const Variable* tmp = read_vars[i];
if (tmp->is_field_var() && is_read(tmp->field_var_of)) {
read_vars.erase(read_vars.begin() + i);
i--;
len--;
}
}
len = write_vars.size();
for (i=0; i<len; i++) {
const Variable* tmp = write_vars[i];
if (tmp->is_field_var() && is_written(tmp->field_var_of)) {
write_vars.erase(write_vars.begin() + i);
i--;
len--;
}
}
}
/*
*
*/
bool
Effect::has_race_with(const Effect &e) const
{
return (non_empty_intersection(this->read_vars, e.write_vars)
|| non_empty_intersection(this->write_vars, e.read_vars)
|| non_empty_intersection(this->write_vars, e.write_vars));
}
/*
*
*/
bool
Effect::is_empty(void) const
{
return read_vars.size()==0 && write_vars.size()==0;
}
/*
*
*/
void
Effect::clear(void)
{
read_vars.clear();
write_vars.clear();
pure = side_effect_free = true;
}
///////////////////////////////////////////////////////////////////////////////
/*
*
*/
void
Effect::Output(std::ostream &out) const
{
vector<Variable *>::size_type len;
vector<Variable *>::size_type i;
std::ostringstream ss;
ss << std::endl;
ss << " * reads :";
len = read_vars.size();
for (i = 0; i < len; ++i) {
ss << " ";
read_vars[i]->OutputForComment(ss);
}
ss << endl;
ss << " * writes:";
len = write_vars.size();
for (i = 0; i < len; ++i) {
ss << " ";
write_vars[i]->OutputForComment(ss);
}
ss << endl;
output_comment_line(out, ss.str());
}
/*
*
*/
void
Effect::update_purity(void)
{
if (has_global_effect())
pure = false;
}
/*
* Return true if read_vars or write_vars has global variables.
*/
bool
Effect::has_global_effect(void) const
{
vector<Variable *>::size_type len;
vector<Variable *>::size_type i;
len = read_vars.size();
for (i = 0; i < len; ++i) {
if (read_vars[i]->is_global()) {
return true;
}
}
len = write_vars.size();
for (i = 0; i < len; ++i) {
if (write_vars[i]->is_global()) {
return true;
}
}
return false;
}
/*
* whether any field of an union is been read
*/
bool
Effect::union_field_is_read(void) const
{
for (size_t i=0; i<read_vars.size(); i++) {
if (read_vars[i]->is_inside_union_field()) {
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End:
// End of file.