-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataflow.c
151 lines (130 loc) · 3.99 KB
/
dataflow.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
/**
* Author: Humberto Naves ([email protected])
*/
#include "code.h"
#include "utils.h"
const uint32 regmask_localvars[NUM_REGMASK] = { 0x43FFFFFE, 0x00000003 };
static
void mark_ssavar (struct ssavar *var, enum ssavartype type, int num)
{
element useel, phiel;
struct value *val;
var->info = num;
var->type = type;
useel = list_head (var->uses);
while (useel) {
struct operation *use = element_getvalue (useel);
if (use->type == OP_PHI) {
phiel = list_head (use->operands);
while (phiel) {
struct value *val = element_getvalue (phiel);
if (val->val.variable->type == SSAVAR_UNK) {
mark_ssavar (val->val.variable, type, num);
}
phiel = element_next (phiel);
}
val = list_headvalue (use->results);
if (val->val.variable->type == SSAVAR_UNK)
mark_ssavar (val->val.variable, type, num);
}
useel = element_next (useel);
}
if (var->def->type == OP_PHI) {
phiel = list_head (var->def->operands);
while (phiel) {
struct value *val = element_getvalue (phiel);
if (val->val.variable->type == SSAVAR_UNK) {
mark_ssavar (val->val.variable, type, num);
}
phiel = element_next (phiel);
}
}
}
static
int check_regs (list l)
{
struct value *val;
element operel;
int reg;
operel = list_head (l);
while (operel) {
val = element_getvalue (operel);
operel = element_next (operel);
if (val->type == VAL_REGISTER) {
reg = val->val.intval;
} else if (val->type == VAL_SSAVAR) {
reg = val->val.variable->name.val.intval;
} else continue;
if (!IS_BIT_SET (regmask_localvars, reg)) return TRUE;
}
return FALSE;
}
static
void check_special_regs (struct subroutine *sub)
{
element blockel;
element opel;
blockel = list_head (sub->blocks);
while (blockel) {
struct basicblock *block = element_getvalue (blockel);
opel = list_head (block->operations);
while (opel) {
struct operation *op = element_getvalue (opel);
if (op->type == OP_INSTRUCTION || op->type == OP_MOVE) {
if (check_regs (op->operands) || check_regs (op->results)) {
op->status |= OP_STAT_SPECIALREGS;
}
}
opel = element_next (opel);
}
blockel = element_next (blockel);
}
}
void extract_variables (struct subroutine *sub)
{
element varel;
int count = 0;
check_special_regs (sub);
varel = list_head (sub->ssavars);
while (varel) {
struct ssavar *var = element_getvalue (varel);
struct operation *op = var->def;
if (var->type == SSAVAR_UNK) {
if (IS_BIT_SET (regmask_localvars, var->name.val.intval)) {
if (op->type == OP_START) {
mark_ssavar (var, SSAVAR_ARGUMENT, var->name.val.intval);
} else if (op->type == OP_CALL && var->name.val.intval != REGISTER_GPR_V0 &&
var->name.val.intval != REGISTER_GPR_V1) {
mark_ssavar (var, SSAVAR_INVALID, 0);
} else {
if (op->type == OP_MOVE || op->type == OP_INSTRUCTION) {
if (!(var->status & (VAR_STAT_PHIARG | VAR_STAT_ASMARG))) {
if (list_size (var->uses) <= 1) {
op->status |= OP_STAT_DEFERRED;
}
}
if (op->type == OP_INSTRUCTION) {
if (op->info.iop.loc->insn->flags & (INSN_LOAD | INSN_STORE))
op->status &= ~OP_STAT_DEFERRED;
else if ((op->info.iop.loc->insn->flags & (INSN_BRANCH)) &&
!op->info.iop.loc->branchalways)
op->status &= ~OP_STAT_DEFERRED;
}
}
if (op->status & OP_STAT_SPECIALREGS) {
op->status &= ~OP_STAT_DEFERRED;
}
if (op->status & OP_STAT_DEFERRED) {
var->type = SSAVAR_TEMP;
var->info = 0;
} else {
mark_ssavar (var, SSAVAR_LOCAL, ++count);
}
}
} else {
mark_ssavar (var, SSAVAR_ARGUMENT, var->name.val.intval);
}
}
varel = element_next (varel);
}
}