-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveness.c
202 lines (166 loc) · 5.51 KB
/
liveness.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
202
#include "liveness.h"
#include "alloc.h"
#include "bit_twiddle.h"
#include "bitset.h"
#include "ir/ir.h"
#include "log.h"
#include "util.h"
static void op_used_callback(struct ir_op **op, void *cb_metadata) {
struct interval_callback_data *cb = cb_metadata;
struct interval *interval = &cb->data->intervals[(*op)->id];
size_t op_end = ((*op)->flags & IR_OP_FLAG_READS_DEST) ? cb->op->id + 1 : cb->op->id;
interval->end = MAX(interval->end, op_end);
}
/* Builds the intervals for each value in the SSA representation
- IDs are rebuilt before calling this so that op ID can be used as an
inreasing inex
- indexes can be non-sequential but must be increasing
*/
struct interval_data construct_intervals(struct ir_func *irb) {
// first rebuild ids so they are sequential and increasing
rebuild_ids(irb);
struct interval_data data;
data.intervals =
arena_alloc(irb->arena, sizeof(*data.intervals) * irb->op_count);
data.num_intervals = 0;
memset(data.intervals, 0, sizeof(*data.intervals) * irb->op_count);
// NOTE: this logic relies on MOV <PARAM> instructions existing for all params
// AND being in order of params
size_t arg_regs = 0;
struct ir_basicblock *basicblock = irb->first;
while (basicblock) {
struct ir_stmt *stmt = basicblock->first;
while (stmt) {
struct ir_op *op = stmt->first;
while (op) {
struct interval *interval = &data.intervals[op->id];
if (op->ty == IR_OP_TY_MOV && op->mov.value == NULL) {
op->reg =
(struct ir_reg){.ty = IR_REG_TY_INTEGRAL, .idx = arg_regs++};
} else {
// // reset registers unless flags because flags is never allocated
// if (op->reg != REG_FLAGS && !(op->flags &
// IR_OP_FLAG_DONT_GIVE_SLOT)) {
// op->reg = NO_REG;
// }
}
DEBUG_ASSERT(op->id < irb->op_count,
"out of range! (id %zu with opcount %zu)", op->id,
irb->op_count);
interval->op = op;
interval->start = op->id;
// we can get intervals with an end before their start if the value is
// unused fix them up to be valid
if (interval->end < interval->start) {
interval->end = interval->start;
}
DEBUG_ASSERT(op->metadata == NULL,
"metadata left over in op during liveness analysis, will "
"be overwritten");
op->metadata = interval;
struct interval_callback_data cb_data = {.op = op, .data = &data};
walk_op_uses(op, op_used_callback, &cb_data);
data.num_intervals++;
op = op->succ;
}
stmt = stmt->succ;
}
basicblock = basicblock->succ;
}
// now we use each phi to set it (and its dependent intervals) to the min/max
// of the dependents
basicblock = irb->first;
while (basicblock) {
struct ir_stmt *stmt = basicblock->first;
while (stmt) {
struct ir_op *op = stmt->first;
while (op) {
if (op->ty == IR_OP_TY_PHI) {
for (size_t i = 0; i < op->phi.num_values; i++) {
struct ir_op *dependent = op->phi.values[i].value;
struct interval *dependent_interval =
&data.intervals[dependent->id];
// force dependent to live until end of the bb
dependent_interval->end =
op->phi.values[i].basicblock->last->last->id;
}
}
op = op->succ;
}
stmt = stmt->succ;
}
basicblock = basicblock->succ;
}
return data;
}
void print_live_regs(FILE *file, const struct ir_reg_usage *reg_usage) {
fslogsl(file, " - LIVE REGS (");
struct bitset_iter gp_iter =
bitset_iter(reg_usage->gp_registers_used, 0, true);
struct bitset_iter fp_iter =
bitset_iter(reg_usage->fp_registers_used, 0, true);
size_t i;
bool first = true;
while (bitset_iter_next(&gp_iter, &i)) {
if (first) {
first = false;
fslogsl(file, ", ");
}
fslogsl(file, "R%zu", i);
}
if (bitset_any(reg_usage->gp_registers_used, true) &&
bitset_any(reg_usage->fp_registers_used, true)) {
fslogsl(file, ", ");
}
first = true;
while (bitset_iter_next(&fp_iter, &i)) {
if (first) {
first = false;
fslogsl(file, ", ");
}
fslogsl(file, "F%zu", i);
}
fslogsl(file, ")");
}
void print_ir_intervals(FILE *file, struct ir_op *op,
UNUSED_ARG(void *metadata)) {
struct interval *interval = op->metadata;
if (interval) {
invariant_assert(interval->op->id == op->id, "intervals are not ID keyed");
fslogsl(file, "start=%05zu, end=%05zu | ", interval->start, interval->end);
} else {
fslogsl(file, "no associated interval | ");
}
switch (op->reg.ty) {
case IR_REG_TY_NONE:
fslogsl(file, " (UNASSIGNED)");
break;
case IR_REG_TY_SPILLED:
if (op->lcl) {
fslogsl(file, " (SPILLED), LCL=%zu", op->lcl->id);
} else {
fslogsl(file, " (SPILLED), LCL=(UNASSIGNED)");
}
break;
case IR_REG_TY_FLAGS:
fslogsl(file, " (FLAGS)");
break;
case IR_REG_TY_INTEGRAL:
if (op->flags & IR_OP_FLAG_DONT_GIVE_REG) {
fslogsl(file, " (DONT)");
} else {
fslogsl(file, " register=R%zu", op->reg.idx);
}
break;
case IR_REG_TY_FP:
if (op->flags & IR_OP_FLAG_DONT_GIVE_REG) {
fslogsl(file, " (DONT)");
} else {
fslogsl(file, " register=F%zu", op->reg.idx);
}
break;
}
// if (interval && interval->op) {
// print_live_regs(file, &interval->op->reg_usage);
// }
}