-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.c
201 lines (196 loc) · 8.18 KB
/
opt.c
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
/*
* File: opt.c
* Project: lab3
* File Created: 2021/11/27
* Author: Tianjie Shen (lawrshen@smail.nju.edu.cn)
* -----
* Copyright 2021 NJU, Tianjie Shen
*/
#include "opt.h"
#include "ir.h"
#include<string.h>
#include<stdlib.h>
extern InterCodes ir_head;
extern int temp_num;
bool* tmpactualconst,*tmpactualvar;
int* tmp2const;
char tmp2var[100000][64];
#define TEMP_VAR_TO_CONST(OP_TYPE) \
do{ \
if (ic->u.OP_TYPE.var->kind == OP_TEMP){ \
if (tmpactualconst[ic->u.OP_TYPE.var->u.temp_no]){ \
ic->u.OP_TYPE.var->kind = OP_CONST; \
ic->u.OP_TYPE.var->u.value = tmp2const[ic->u.OP_TYPE.var->u.temp_no]; \
} \
} \
return false; \
} while (0) \
#define TEMP_VAR_TO_VAR(OP_TYPE) \
do{ \
if (ic->u.OP_TYPE.var->kind == OP_TEMP){ \
if (tmpactualvar[ic->u.OP_TYPE.var->u.temp_no]){ \
ic->u.OP_TYPE.var->kind = OP_VAR; \
strcpy(ic->u.OP_TYPE.var->u.var_name,tmp2var[ic->u.OP_TYPE.var->u.temp_no]);\
} \
} \
return false; \
} while (0) \
// return true if it can evaluate
bool simple_eval(InterCode ic){
switch (ic->kind){
case IR_ASSIGN: {
if(ic->u.assign.left->kind==OP_TEMP&&ic->u.assign.right->kind==OP_CONST){
tmpactualconst[ic->u.assign.left->u.temp_no]=true;
tmp2const[ic->u.assign.left->u.temp_no]=ic->u.assign.right->u.value;
return true;
}else if(ic->u.assign.right->kind==OP_TEMP){
if(tmpactualconst[ic->u.assign.right->u.temp_no]){
ic->u.assign.right->kind=OP_CONST;
ic->u.assign.right->u.value=tmp2const[ic->u.assign.right->u.temp_no];
}
}
return false;
}
case IR_ADD:
case IR_SUB:
case IR_MUL:
case IR_DIV:{
Operand op1=ic->u.binop.op1,op2=ic->u.binop.op2;
//if t1 + t2 actual is 3+4
if(op1->kind==OP_TEMP&&tmpactualconst[op1->u.temp_no]){
op1->kind=OP_CONST;
op1->u.value=tmp2const[op1->u.temp_no];
}
if(op2->kind==OP_TEMP&&tmpactualconst[op2->u.temp_no]){
op2->kind=OP_CONST;
op2->u.value=tmp2const[op2->u.temp_no];
}
if(op1->kind==OP_CONST&&op2->kind==OP_CONST){// #3 + #4
Operand res=ic->u.binop.result;
if(res->kind==OP_TEMP){
tmpactualconst[res->u.temp_no]=true;
switch (ic->kind) {
case IR_ADD:
tmp2const[res->u.temp_no]=op1->u.value+op2->u.value;break;
case IR_SUB:
tmp2const[res->u.temp_no]=op1->u.value-op2->u.value;break;
case IR_MUL:
tmp2const[res->u.temp_no]=op1->u.value*op2->u.value;break;
case IR_DIV:
tmp2const[res->u.temp_no]=op1->u.value/op2->u.value;break;
default:break;
}
return true;
}
}
return false;
}
case IR_JUMP_COND:{
Operand op1=ic->u.jump_cond.op1,op2=ic->u.jump_cond.op2;
//if t1 + t2 actual is 3+4
if(op1->kind==OP_TEMP&&tmpactualconst[op1->u.temp_no]){
op1->kind=OP_CONST;
op1->u.value=tmp2const[op1->u.temp_no];
}
if(op2->kind==OP_TEMP&&tmpactualconst[op2->u.temp_no]){
op2->kind=OP_CONST;
op2->u.value=tmp2const[op2->u.temp_no];
}
return false;
}
case IR_RET:
TEMP_VAR_TO_CONST(ret);
case IR_WRITE:
TEMP_VAR_TO_CONST(write);
case IR_ARG:
TEMP_VAR_TO_CONST(arg);
case IR_SAVE:{
if(ic->u.save.right->kind==OP_TEMP){
if(tmpactualconst[ic->u.save.right->u.temp_no]){
ic->u.save.right->kind=OP_CONST;
ic->u.save.right->u.value=tmp2const[ic->u.save.right->u.temp_no];
}
}
return false;
}
default:
return false;
}
}
// return true if tmp is actual variable
bool simple_temp_val(InterCode ic){
switch (ic->kind){
case IR_ASSIGN: {
if(ic->u.assign.left->kind==OP_TEMP&&ic->u.assign.right->kind==OP_VAR){
tmpactualvar[ic->u.assign.left->u.temp_no]=true;
strcpy(tmp2var[ic->u.assign.left->u.temp_no],ic->u.assign.right->u.var_name);
return true;
}else if(ic->u.assign.right->kind==OP_TEMP){
if(tmpactualvar[ic->u.assign.right->u.temp_no]){
ic->u.assign.right->kind=OP_VAR;
strcpy(ic->u.assign.right->u.var_name,tmp2var[ic->u.assign.right->u.temp_no]);
}
}
return false;
}
case IR_ADD:
case IR_SUB:
case IR_MUL:
case IR_DIV:{
Operand op1=ic->u.binop.op1,op2=ic->u.binop.op2;
if(op1->kind==OP_TEMP&&tmpactualvar[op1->u.temp_no]){
op1->kind=OP_VAR;
strcpy(op1->u.var_name,tmp2var[op1->u.temp_no]);
}
if(op2->kind==OP_TEMP&&tmpactualvar[op2->u.temp_no]){
op2->kind=OP_VAR;
strcpy(op2->u.var_name,tmp2var[op2->u.temp_no]);
}
return false;
}
case IR_JUMP_COND:{
Operand op1=ic->u.jump_cond.op1,op2=ic->u.jump_cond.op2;
if(op1->kind==OP_TEMP&&tmpactualvar[op1->u.temp_no]){
op1->kind=OP_VAR;
strcpy(op1->u.var_name,tmp2var[op1->u.temp_no]);
}
if(op2->kind==OP_TEMP&&tmpactualvar[op2->u.temp_no]){
op2->kind=OP_VAR;
strcpy(op2->u.var_name,tmp2var[op2->u.temp_no]);
}
return false;
}
case IR_RET:
TEMP_VAR_TO_VAR(ret);
case IR_WRITE:
TEMP_VAR_TO_VAR(write);
case IR_ARG:
TEMP_VAR_TO_VAR(arg);
case IR_SAVE:{
if(ic->u.save.right->kind==OP_TEMP){
if(tmpactualvar[ic->u.save.right->u.temp_no]){
ic->u.save.right->kind=OP_VAR;
strcpy(ic->u.save.right->u.var_name,tmp2var[ic->u.save.right->u.temp_no]);
}
}
return false;
}
default:
return false;
}
}
// linear scanning IR to optimize actual const/variable temp
void LinearOptIC() {
tmpactualconst=(bool*)calloc(temp_num,sizeof(bool)),
tmpactualvar=(bool*)calloc(temp_num,sizeof(bool));
tmp2const=(int*)calloc(temp_num,sizeof(int)),
memset(tmp2var,0,sizeof(tmp2var));
for(InterCodes p=ir_head;p;p=p->next){
if(p->prev&&p->prev->code->kind!=IR_LABEL){
InterCode ic=p->code;
if(simple_eval(ic)|| simple_temp_val(ic)){
p->dead=true;
}
}
}
}