-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.cpp
638 lines (574 loc) · 18 KB
/
opt.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
/******************************************************************************
* C++ code file : opt.cpp
* Project : Pazcal Compiler
* Written by : Lolos Konstantinos, Podimata Charikleia
* Date : 2014-2015
* Description : Intermediate code optimizations
******************************************************************************/
#include "opt.h"
extern vector<quad> quads;
using namespace std;
/* Function declarations */
int find_first_no_op();
void remove_quad(unsigned int i);
bool is_quad_num(const char *name);
unsigned int quad_num(const char *name);
void decr_quad_num(int i);
bool is_jump(int i);
bool is_comparison(int i);
void simplify_jump(int i);
void reverse_comparison(int i);
bool jump_simplification();
bool remove_inconsequential_jumps();
bool redirect_chained_jumps();
bool dead_code_elimination();
bool eliminate_dead_code(unsigned int i, int *reachable);
void mark_reachable(unsigned int i, int *reachable);
bool eliminate_unreachable(unsigned int i, int *reachable);
bool evaluate_comparison(unsigned int i);
void redirect_jump(int i, int target);
void make_no_op(int i);
bool produces_result(unsigned int i);
bool rev_copy_propagation();
bool is_also_operand(unsigned int i);
bool appears_elsewhere(unsigned int i);
bool appears_elsewhere_but_next(unsigned int i);
bool is_jumped_upon(unsigned int i);
bool is_assignment(unsigned int i);
bool is_cast(unsigned int i);
bool inconsequential_code_elimination();
bool algebraic_transformations();
bool is_zero_val(SymbolEntry *e);
bool algebraic_plus(unsigned int i);
bool algebraic_minus(unsigned int i);
bool algebraic_mult(unsigned int i);
bool algebraic_div(unsigned int i);
bool algebraic_mod(unsigned int i);
bool is_one_val(SymbolEntry *e);
bool remove_self_assignment();
/* Performs all the intermediate code optimizations */
void optimize()
{
bool did_opt = true;
while (did_opt) {
did_opt = false;
did_opt |= jump_simplification();
did_opt |= remove_inconsequential_jumps();
did_opt |= redirect_chained_jumps();
did_opt |= dead_code_elimination();
did_opt |= rev_copy_propagation();
did_opt |= inconsequential_code_elimination();
did_opt |= algebraic_transformations();
did_opt |= remove_self_assignment();
remove_no_ops();
}
}
/* Removes all the no_op instructions from the intermediate code */
void remove_no_ops()
{
int i = find_first_no_op();
while (i != -1) {
remove_quad(i);
i = find_first_no_op();
}
}
/* Remove quads in the form : :=, x, -, x */
bool remove_self_assignment()
{
bool did_opt = false;
unsigned int i;
for (i = 0; i < quads.size(); i++)
if (equal(quads[i].op, ":=") && (quads[i].xe == quads[i].ze)) {
make_no_op(i);
did_opt = true;
}
return did_opt;
}
/* Algebraic transformations */
bool algebraic_transformations()
{
bool did_opt = false;
unsigned int i;
for (i = 0; i < quads.size(); i++) {
const char *op = quads[i].op;
if (equal(op, "+")) did_opt |= algebraic_plus(i);
else if (equal(op, "-")) did_opt |= algebraic_minus(i);
else if (equal(op, "*")) did_opt |= algebraic_mult(i);
else if (equal(op, "/")) did_opt |= algebraic_div(i);
else if (equal(op, "%")) did_opt |= algebraic_mod(i);
}
return did_opt;
}
/* Algebraic transformations for multiplication */
bool algebraic_mult(unsigned int i)
{
bool did_opt = false;
// *, a, 0, b => :=, 0, -, b
if (is_zero_val(quads[i].ye) && !is_real_type(quads[i].xe) && !is_real_type(quads[i].ye)) {
quads[i].op = ":=";
quads[i].x = quads[i].y;
quads[i].xe = quads[i].ye;
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
// *, 0, a, b => :=, 0, -, b
else if (is_zero_val(quads[i].xe) && !is_real_type(quads[i].xe) && !is_real_type(quads[i].ye)) {
quads[i].op = ":=";
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
// *, a, 1, b => :=, a, -, b
else if (is_one_val(quads[i].ye)) {
quads[i].op = ":=";
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
// *, 1, a, b => :=, a, -, b
else if (is_one_val(quads[i].xe)) {
quads[i].op = ":=";
quads[i].x = quads[i].y;
quads[i].xe = quads[i].ye;
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
return did_opt;
}
/* Algebraic transformations for division */
bool algebraic_div(unsigned int i)
{
bool did_opt = false;
// /, a, 1, b => :=, a, -, b
if (is_one_val(quads[i].ye)) {
quads[i].op = ":=";
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
return did_opt;
}
/* Algebraic transformations for subtraction */
bool algebraic_minus(unsigned int i)
{
bool did_opt = false;
// -, a, 0, b => :=, a, -, b
if (is_zero_val(quads[i].ye)) {
quads[i].op = ":=";
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
return did_opt;
}
/* Algebraic transformations for addition */
bool algebraic_plus(unsigned int i)
{
bool did_opt = false;
// +, a, 0, b => :=, a, -, b
if (is_zero_val(quads[i].ye)) {
quads[i].op = ":=";
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
// +, 0, a, b => :=, a, -, b
else if (is_zero_val(quads[i].xe)) {
quads[i].op = ":=";
quads[i].x = quads[i].y;
quads[i].xe = quads[i].ye;
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
return did_opt;
}
/* Algebraic transformations for modulo */
bool algebraic_mod(unsigned int i)
{
bool did_opt = false;
// %, a, 1, b => :=, a, -, b
if (is_one_val(quads[i].ye)) {
quads[i].op = ":=";
quads[i].y = "-";
quads[i].ye = NULL;
did_opt = true;
}
return did_opt;
}
/* Returns true if the given symbol entry is a constant value equal to 0 */
bool is_zero_val(SymbolEntry *e)
{
if (!e || !isConst(e))
return false;
KIND kind = var_kind(e);
switch (kind) {
case TYPE_CHAR:
return !char_val(e);
case TYPE_INTEGER:
return !int_val(e);
case TYPE_REAL:
return !real_val(e);
default:
internal("is_zero_val called with invalid kind variable");
return false;
}
}
/* Returns true if the given symbol entry is a constant value equal to 1 */
bool is_one_val(SymbolEntry *e)
{
if (!e || !isConst(e))
return false;
KIND kind = var_kind(e);
switch (kind) {
case TYPE_CHAR:
return char_val(e) == 1;
case TYPE_INTEGER:
return int_val(e) == 1;
case TYPE_REAL:
return real_val(e) == 1;
default:
internal("is_one_val called with invalid kind variable");
return false;
}
}
/* Removes quads whose result is not used anywhere in the code */
bool inconsequential_code_elimination()
{
bool did_opt = false;
unsigned int i;
for (i = 0; i < quads.size(); i++)
if (produces_result(i) && !is_by_ref(quads[i].ze) && !appears_elsewhere(i)) {
make_no_op(i);
did_opt = true;
}
return did_opt;
}
/* Removes intermediate results that get immediately assigned */
bool rev_copy_propagation()
{
bool did_opt = false;
unsigned int i;
for (i = 0; i < quads.size() - 1; i++) {
if ((!produces_result(i)) ||
(quads[i].ze != quads[i+1].xe) ||
(is_also_operand(i)) ||
(!is_assignment(i+1)) ||
(is_cast(i) && is_cast(i+1)) ||
(appears_elsewhere_but_next(i)) ||
(is_jumped_upon(i+1))
)
continue;
quads[i].z = quads[i+1].z;
quads[i].ze = quads[i+1].ze;
make_no_op(i+1);
did_opt = true;
}
return did_opt;
}
/* Returns true if the given quad num performs type casting */
bool is_cast(unsigned int i)
{
if (is_assignment(i))
return !equal_types(var_type(quads[i].xe), var_type(quads[i].ze));
else {
KIND x_kind = var_kind(quads[i].xe);
KIND y_kind = var_kind(quads[i].ye);
KIND z_kind = var_kind(quads[i].ze);
if (x_kind == TYPE_CHAR && y_kind == TYPE_CHAR) {
if (equal(quads[i].op, "%"))
return z_kind != TYPE_INTEGER;
else
return z_kind != TYPE_CHAR;
}
if (x_kind == TYPE_CHAR && y_kind == TYPE_INTEGER)
return z_kind != TYPE_INTEGER;
if (x_kind == TYPE_CHAR && y_kind == TYPE_REAL)
return z_kind != TYPE_REAL;
if (x_kind == TYPE_INTEGER && y_kind == TYPE_CHAR)
return z_kind != TYPE_INTEGER;
if (x_kind == TYPE_INTEGER && y_kind == TYPE_INTEGER)
return z_kind != TYPE_INTEGER;
if (x_kind == TYPE_INTEGER && y_kind == TYPE_REAL)
return z_kind != TYPE_REAL;
if (x_kind == TYPE_REAL)
return z_kind != TYPE_REAL;
return false;
}
}
/* Returns true if the given quad num is a value assignment */
bool is_assignment(unsigned int i)
{
return equal(quads[i].op, ":=");
}
/* Returns true if the result of the quad is also an operand (for example :=, x, 1, x) */
bool is_also_operand(unsigned int i)
{
return equal(quads[i].x, quads[i].z) || equal(quads[i].y, quads[i].z);
}
/* Returns true if the result of the quad is an operand in quads other than i */
bool appears_elsewhere(unsigned int i)
{
SymbolEntry *ze = quads[i].ze;
unsigned int j;
for (j = 0; j < quads.size(); j++) {
if (i == j)
continue;
if (quads[j].xe == ze || quads[j].ye == ze)
return true;
}
return false;
}
/* Returns true if the result of the quad is an operand in quads other than i and i+1 */
bool appears_elsewhere_but_next(unsigned int i)
{
SymbolEntry *ze = quads[i].ze;
unsigned int j;
for (j = 0; j < quads.size(); j++) {
if (i == j || i+1 == j)
continue;
if (quads[j].xe == ze || quads[j].ye == ze)
return true;
}
return false;
}
/* Returns true if there is a jump to the given quad num */
bool is_jumped_upon(unsigned int i)
{
unsigned int j;
for (j = 0; j < quads.size(); j++)
if (is_jump(j) || (is_comparison(j) && is_quad_num(quads[j].z)))
if (quad_num(quads[j].z) == i)
return true;
return false;
}
/* Returns true if the quad produces a result */
bool produces_result(unsigned int i)
{
if (!quads[i].ze)
return false;
const char * op = quads[i].op;
return (equal(op, "+") ||
equal(op, "-") ||
equal(op, "*") ||
equal(op, "/") ||
equal(op, "%") ||
equal(op, "==") ||
equal(op, "not") ||
equal(op, "!=") ||
equal(op, ">") ||
equal(op, ">=") ||
equal(op, "<") ||
equal(op, "<=") ||
equal(op, ":=")
);
}
/* Remove unreachable code */
bool dead_code_elimination()
{
bool did_opt = false;
int *reachable = (int *) newAlloc(quads.size() * sizeof(int));
unsigned int i;
for (i = 0; i < quads.size(); i++)
reachable[i] = 0;
for (i = 0; i < quads.size(); i++)
if (equal(quads[i].op, "unit"))
did_opt |= eliminate_dead_code(i, reachable);
return did_opt;
}
/* Eliminates all unreachable code from the given quad to the end of the current function */
bool eliminate_dead_code(unsigned int i, int *reachable)
{
mark_reachable(i+1, reachable);
return eliminate_unreachable(i+1, reachable);
}
/* Marks all the quads that are reachable from the given quad (aka DFS) */
void mark_reachable(unsigned int i, int *reachable)
{
if (equal(quads[i].op, "endu") || reachable[i])
return;
reachable[i] = 1;
if (is_jump(i))
mark_reachable(quad_num(quads[i].z), reachable);
else if (is_comparison(i) && is_quad_num(quads[i].z)) {
if (isConst(quads[i].xe) && isConst(quads[i].ye)) {
VALUE vx = get_val(quads[i].xe);
VALUE vy = get_val(quads[i].ye);
KIND kx = var_kind(quads[i].xe);
KIND ky = var_kind(quads[i].ye);
bool will_jump = eval_comp_expr(vx, vy, kx, ky, quads[i].op);
if (will_jump) {
int target = quad_num(quads[i].z);
quads[i].op = "jump";
quads[i].x = "-";
quads[i].y = "-";
mark_reachable(target, reachable);
}
else {
make_no_op(i);
mark_reachable(i+1, reachable);
}
}
else {
int target = quad_num(quads[i].z);
mark_reachable(target, reachable);
mark_reachable(i+1, reachable);
}
}
else if (!equal(quads[i].op, "ret") && !equal(quads[i].op, "retv"))
mark_reachable(i+1, reachable);
}
/* Turns all code that has not been marked as reachable to no_op quads */
bool eliminate_unreachable(unsigned int i, int *reachable)
{
bool did_opt = false;
while (!equal(quads[i].op, "endu")) {
if (!reachable[i]) {
make_no_op(i);
did_opt = true;
}
i++;
}
return did_opt;
}
/* Remove jumps to the next quad */
bool remove_inconsequential_jumps()
{
bool did_opt = false;
for (unsigned int i = 0; i < quads.size(); i++)
if (is_jump(i) || (is_comparison(i) && is_quad_num(quads[i].z))) {
unsigned int target = quad_num(quads[i].z);
if (target == i+1) {
make_no_op(i);
did_opt = true;
}
}
return did_opt;
}
/* Redirect jumps that lead to jumps */
bool redirect_chained_jumps()
{
bool did_opt = false;
for (unsigned int i = 0; i < quads.size(); i++) {
if (is_jump(i) || (is_comparison(i) && is_quad_num(quads[i].z))) {
unsigned int target = quad_num(quads[i].z);
while (is_jump(target) && target != i)
target = quad_num(quads[target].z);
if (target != i) // if it is not an infinite loop
redirect_jump(i, target);
}
}
return did_opt;
}
/* Redirects a jump to the given target */
void redirect_jump(int i, int target)
{
char *c = (char *) newAlloc(20);
sprintf(c, "%d", target);
quads[i].z = c;
}
/* Turns the given quad to a no_op quad */
void make_no_op(int i)
{
quads[i].op = "no_op";
quads[i].x = "-";
quads[i].y = "-";
quads[i].z = "-";
quads[i].xe = NULL;
quads[i].ye = NULL;
quads[i].ze = NULL;
}
/* Turns comparisons that just avoid jumps to a single instuction.
For example:
3: ==, x, y, 5
4: jump, -, -, 42
is converted to:
3: !=, x, y, 42
*/
bool jump_simplification()
{
bool did_opt = false;
for (unsigned int i = 0; i < quads.size() - 1; i++)
if (is_comparison(i) && (is_quad_num(quads[i].z)) && (quad_num(quads[i].z) == i + 2))
if (is_jump(i+1)) {
simplify_jump(i);
did_opt = true;
}
return did_opt;
}
/* Performs the jump simplification for quad i */
void simplify_jump(int i)
{
reverse_comparison(i);
quads[i].z = quads[i+1].z;
make_no_op(i+1);
}
/* Returns true if quad i is a jump */
bool is_jump(int i)
{
return equal(quads[i].op, "jump");
}
/* Returns true if quad i is a comparison */
bool is_comparison(int i)
{
if (equal(quads[i].op, "==")) return true;
else if (equal(quads[i].op, "!=")) return true;
else if (equal(quads[i].op, ">")) return true;
else if (equal(quads[i].op, "<")) return true;
else if (equal(quads[i].op, ">=")) return true;
else if (equal(quads[i].op, "<=")) return true;
else return false;
}
/* Reverses the comparison operator for quad i */
void reverse_comparison(int i)
{
if (equal(quads[i].op, "==")) quads[i].op = "!=";
else if (equal(quads[i].op, "!=")) quads[i].op = "==";
else if (equal(quads[i].op, ">")) quads[i].op = "<=";
else if (equal(quads[i].op, "<")) quads[i].op = ">=";
else if (equal(quads[i].op, ">=")) quads[i].op = "<";
else if (equal(quads[i].op, "<=")) quads[i].op = ">";
}
/* Returns the index of the first no_op instruction in the code, -1 if none exists */
int find_first_no_op()
{
for (unsigned int i = 0; i < quads.size(); i++)
if (equal(quads[i].op, "no_op"))
return i;
return -1;
}
/* Removes the given quad and adjusts the quad numbers accordingly */
void remove_quad(unsigned int index)
{
quads.erase(quads.begin() + index);
for (unsigned int i = 0; i < quads.size(); i++)
if (is_quad_num(quads[i].z) && quad_num(quads[i].z) > index)
decr_quad_num(i);
}
/* Returns true if the given string in a quad number.
(Actually it just checks if it is an integer) */
bool is_quad_num(const char *name)
{
while (*name) {
if (*name < '0' || *name > '9')
return false;
name++;
}
return true;
}
/* Returns the quad number this instruction points to */
unsigned int quad_num(const char *name)
{
return atoi(name);
}
/* Decrements the quad number this instructin points to by 1 */
void decr_quad_num(int i)
{
int old_index = quad_num(quads[i].z);
char *c = (char *) newAlloc(20);
sprintf(c, "%d", old_index - 1);
quads[i].z = c;
}