Skip to content

Commit 52f1ff1

Browse files
committed
Revert "ir: load/store conversion, copy-elimination"
This reverts commit 6db8c79.
1 parent 6db8c79 commit 52f1ff1

File tree

8 files changed

+17
-145
lines changed

8 files changed

+17
-145
lines changed

generator/ir/ir_generator.c2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,7 @@ fn void Generator.collectLocalVars(Generator* gen, Stmt* s) {
664664
break;
665665
case Label:
666666
LabelStmt* ls = cast<LabelStmt*>(s);
667-
Stmt* inner = ls.getStmt();
668-
if (inner) gen.collectLocalVars(inner);
667+
gen.collectLocalVars(ls.getStmt());
669668
break;
670669
case Compound:
671670
CompoundStmt* c = cast<CompoundStmt*>(s);

generator/ir/ir_generator_stmt.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ fn bool Generator.emitStmt(Generator* gen, const Stmt* s) {
7777
c.addComment(gen.name_buf.data());
7878
}
7979
const Stmt* ls = l.getStmt();
80-
if (ls) return gen.emitStmt(ls);
81-
return true;
80+
assert(ls);
81+
return gen.emitStmt(ls);
8282
case Goto:
8383
const GotoStmt* g = cast<GotoStmt*>(s);
8484

ir/context.c2

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,13 +680,9 @@ fn void Context.finalizeFunction(Context* c, SymbolId id) {
680680
//c.dump_function(fi2, name);
681681
//c.generate_graphviz(fi2, name);
682682
//c.print_function(id, fi2);
683-
684683
c.create_ssa(fi2, name);
685-
686684
if (c.inserter.needsFixup()) c.fixup_function(fi2);
687685

688-
//c.print_function(id, fi2);
689-
c.eliminateCopies(fi2);
690686
//c.dump_function(fi2, name);
691687
//c.print_function(id, fi2);
692688
}

ir/instr.c2

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ fn bool Instr.hasResult(const Instr* i) {
9494
return i.instrBits.has_result;
9595
}
9696

97-
fn void Instr.clear(Instr* i) {
98-
memset(i, 0, sizeof(Instr));
99-
}
100-
10197
fn const char* Instr.getKindName(const Instr* i) {
10298
InstrKind k = i.getKind();
10399
return k.str();
@@ -108,16 +104,6 @@ fn bool Instr.isPseudo(const Instr* i) {
108104
return k >= InstrKind.Switch;
109105
}
110106

111-
fn bool Instr.isNone(const Instr* i) {
112-
InstrKind k = i.getKind();
113-
return k == InstrKind.None;
114-
}
115-
116-
fn bool Instr.isCopy(const Instr* i) {
117-
InstrKind k = i.getKind();
118-
return k == InstrKind.Copy;
119-
}
120-
121107
fn bool Instr.isLoad(const Instr* i) {
122108
InstrKind k = i.getKind();
123109
return k >= InstrKind.Load1 && k <= InstrKind.Load8;

ir/print.c2

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn void PrintHelper.printInstr(PrintHelper* ph, const Instr* blk_instrs, u32 blk
328328
InstrKind k = i.getKind();
329329

330330
// special case for comments
331-
if (i.isComment()) {
331+
if (i.getKind() == InstrKind.Comment) {
332332
out.resetToLastNewline();
333333
out.add(" ");
334334
out.color(col_Comment);
@@ -347,11 +347,6 @@ fn void PrintHelper.printInstr(PrintHelper* ph, const Instr* blk_instrs, u32 blk
347347

348348
out.indent(2);
349349

350-
if (i.isNone()) {
351-
out.add("-\n");
352-
return;
353-
}
354-
355350
if (i.hasResult()) {
356351
out.color(col_Temp);
357352
u32 temp_nr = ph.findTemp(ph.blk_instr_start + blk_instr_idx);
@@ -441,9 +436,7 @@ fn void PrintHelper.printRef(PrintHelper* ph, Ref r, bool print_type) {
441436
break;
442437
case Temp:
443438
out.color(col_Temp);
444-
u32 v = ph.findTemp(r.value);
445-
if (v == 0) out.color(col_Fixup);
446-
out.print("%%%d", v);
439+
out.print("%%%d", ph.findTemp(r.value));
447440
#if DebugIr
448441
out.color(color.Grey);
449442
out.print("(T%d)", r.value);

ir/slot_collector.c2

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,12 @@ fn const WriteSlotAccess* SlotCollector.findWrite(const SlotCollector* c, BlockI
117117
}
118118

119119
fn void SlotCollector.checkBlock(SlotCollector* c, BlockId blk_id) {
120-
/*
121-
Algorithm:
122-
- store first read per slot in block (if not after write)
123-
- store last write per slot
124-
- replace slot writes with None
125-
- replace slot read with last write
126-
*/
127120
Block* b = c.info.blocks.get(blk_id);
128121
BlockIndex* bi = &c.indexes[blk_id];
129122

130123
bi.rd_start = cast<u16>(c.read_idx);
131124
bi.wr_start = cast<u16>(c.write_idx);
132125

133-
// for each block, trace reads-after-write, replace Sx with Temp
134-
135126
Instr* ii = c.info.instructions.get(b.instr.start);
136127
for (u32 i=0; i<b.instr.count; i++) {
137128
Instr* cur = &ii[i];
@@ -147,13 +138,10 @@ fn void SlotCollector.checkBlock(SlotCollector* c, BlockId blk_id) {
147138
cur.instrBits.kind = InstrKind.Copy;
148139

149140
u16 slot = cast<u16>(cur.args[0].value);
150-
//printf("[%d] load %d (%s %d)\n", i, slot, cur.args[0].getKindName(), cur.args[0].value);
151141
// ignore if already written here. Cannot use findWrite yet!
152142
for (u32 j=bi.wr_start; j<c.write_idx; j++) {
153143
if (c.writes[j].slot == slot) {
154144
found = true;
155-
// replace slot-ref with write-ref
156-
cur.args[0] = c.writes[j].ref;
157145
break;
158146
}
159147
}
@@ -176,6 +164,9 @@ fn void SlotCollector.checkBlock(SlotCollector* c, BlockId blk_id) {
176164
} else {
177165
if (!cur.args[1].isSlot()) continue;
178166

167+
// convert to Copy
168+
cur.instrBits.kind = InstrKind.Copy;
169+
179170
u16 slot = cast<u16>(cur.args[1].value);
180171
WriteSlotAccess* wa = nil;
181172
// overwrite if already written (track last write)
@@ -189,14 +180,11 @@ fn void SlotCollector.checkBlock(SlotCollector* c, BlockId blk_id) {
189180
if (!wa) {
190181
assert(c.write_idx < SlotMax);
191182
wa = &c.writes[c.write_idx++];
192-
wa.slot = slot;
193183
c.num_writes++;
194184
}
195185

186+
wa.slot = slot;
196187
wa.ref = cur.args[0];
197-
//printf("[%d] store %d (%s %d)\n", i, slot, wa.ref.getKindName(), wa.ref.value);
198-
199-
cur.clear();
200188
}
201189
}
202190

@@ -243,6 +231,7 @@ fn void SlotCollector.dump(const SlotCollector* c) @(unused) {
243231
printf("Access: [%d reads, %d writes, %d blocks]\n", c.read_idx, c.write_idx, c.num_blocks);
244232
for (u32 i=0; i<c.num_blocks; i++) {
245233
const BlockIndex* bi = &c.indexes[i];
234+
//printf(" B %d R %d %d W %d %d\n", i, bi.rd_start, bi.rd_count, bi.wr_start, bi.wr_count);
246235
printf(" B%d [%2d %2d | %2d %2d] ", i, bi.rd_start, bi.rd_count, bi.wr_start, bi.wr_count);
247236
printf("R");
248237
if (bi.rd_count) {
@@ -251,7 +240,6 @@ fn void SlotCollector.dump(const SlotCollector* c) @(unused) {
251240
for (u32 j=start; j<end; j++) {
252241
const ReadSlotAccess* ra = &c.reads[j];
253242
printf(" %d", ra.slot);
254-
printf("(%d)", ra.instr_idx);
255243
}
256244
}
257245
printf(" | W");
@@ -261,11 +249,9 @@ fn void SlotCollector.dump(const SlotCollector* c) @(unused) {
261249
for (u32 j=start; j<end; j++) {
262250
const WriteSlotAccess* wa = &c.writes[j];
263251
printf(" %d", wa.slot);
264-
printf("(%s %d)", wa.ref.getKindName(), wa.ref.value);
265252
}
266253
}
267254
printf("\n");
268255
}
269-
printf("\n");
270256
}
271257

ir/ssa.c2

Lines changed: 7 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,18 @@ fn void Context.create_ssa(Context* c, FunctionInfo* fi, const char* name) {
144144
//printf("create SSA %s\n", name);
145145
const u32 num_blocks = fi.blocks.getCount();
146146

147+
147148
c.inserter.clear();
148149
c.collector.fill(fi);
149150
c.revlist.build(fi);
150151
c.active_blocks.reset(num_blocks);
151152

152-
//if (num_blocks == 1) return;
153+
if (num_blocks == 1) return;
153154
//c.collector.dump();
154155
//c.revlist.dump();
155156

156157
/*
157-
Algorithm for PHI insertion
158+
Algorithm:
158159

159160
foreach block
160161
foreach read
@@ -227,11 +228,6 @@ fn void Context.create_ssa(Context* c, FunctionInfo* fi, const char* name) {
227228
} else {
228229
ref = results.stash[0].ref;
229230
}
230-
231-
// modify read instruction with new ref (replaces slot)
232-
Instr* read_instr = fi.instructions.get(ra.instr_idx);
233-
read_instr.args[0] = ref;
234-
235231
const WriteSlotAccess* wr = c.collector.findWrite(blk_id, slot);
236232
if (!wr) {
237233
assert(ref.isValid());
@@ -248,85 +244,11 @@ fn void Context.create_ssa(Context* c, FunctionInfo* fi, const char* name) {
248244
//c.inserter.dump();
249245
}
250246

251-
252-
253-
type CopyInfo struct {
254-
u32 instr_idx;
255-
Ref ref;
256-
}
257-
258-
fn CopyInfo* find_copy(CopyInfo* copies, u32 num_copies, u32 idx) {
259-
for (u32 i=0; i<num_copies; i++) {
260-
if (copies[i].instr_idx == idx) return &copies[i];
261-
}
262-
return nil;
263-
}
264-
265-
fn void Context.eliminateCopies(Context* c, FunctionInfo* fi) {
266-
// do copy eliminination. Try 1 pass (if possible)
267-
// otherwise mark in prev run and apply here
268-
// TODO also need to apply to insertions? OR do afterwards
269-
// TODO extract to data structure
270-
// TODO can do binary search (since instr_idxs are ordered)
271-
CopyInfo* copies = stdlib.malloc(1024 * sizeof(CopyInfo));
272-
u32 num_copies = 0;
273-
274-
// Note: the filling could be done in previous loop already!
275-
Instr* all = fi.instructions.get(0);
276-
for (u32 i=0; i<fi.instructions.getCount(); i++) {
277-
Instr* instr = &all[i];
278-
if (instr.isCopy()) {
279-
assert(num_copies != 1024);
280-
copies[num_copies].instr_idx = i;
281-
copies[num_copies].ref = instr.args[0];
282-
// TODO also check if args is already in list for nested copies
283-
instr.clear();
284-
num_copies++;
285-
} else {
286-
if (instr.isPhi()) {
287-
PhiClause* clauses = fi.phis.get(instr.phi_clauses.start);
288-
for (u32 j=0; j<instr.phi_clauses.count; j++) {
289-
Ref* r = &clauses[j].ref;
290-
if (r.isTemp()) {
291-
CopyInfo* copy = find_copy(copies, num_copies, r.value);
292-
if (copy) *r = copy.ref;
293-
294-
}
295-
}
296-
} else {
297-
Ref* r = &instr.args[0];
298-
if (r.isTemp()) {
299-
CopyInfo* copy = find_copy(copies, num_copies, r.value);
300-
if (copy) instr.args[0] = copy.ref;
301-
}
302-
r = &instr.args[1];
303-
if (r.isTemp()) {
304-
CopyInfo* copy = find_copy(copies, num_copies, r.value);
305-
if (copy) instr.args[1] = copy.ref;
306-
} else if (r.isRefList()) { // can only occur on args[1]
307-
Ref* refs = fi.refs.get(r.value);
308-
u32 idx = 0;
309-
while (1) {
310-
Ref* r2 = &refs[idx];
311-
if (r2.isTemp()) {
312-
CopyInfo* copy = find_copy(copies, num_copies, r2.value);
313-
if (copy) refs[idx] = copy.ref;
314-
}
315-
if (!r2.isValid()) break;
316-
idx++;
317-
}
318-
}
319-
}
320-
}
321-
}
322-
stdlib.free(copies);
323-
}
324-
325247
// TODO pas Optimizer? (with Context, FunctionInfo, other?)
326248
// TODO dont pass info as separate arg, but combine with Context?
327249
fn Ref Context.checkPredBlock(Context* c, BlockId blk_id, u16 slot, u32 indent, FunctionInfo* fi) {
328250
// TODO do loop detection
329-
Ref ref = { }
251+
Ref ref = {}
330252

331253
c.active_blocks.set(blk_id);
332254

@@ -432,24 +354,16 @@ fn void Context.fixup_function(Context* c, FunctionInfo* fi) {
432354
new_count++;
433355
}
434356

435-
if (i_old[j].isNone()) {
436-
// removal causes crash, TODO fix
437-
//printf("SKIP %d\n", j);
438-
//continue;
439-
}
440-
441357
Instr* is = i_new.add();
442358
is.copy(&i_old[j]);
443359
c.conversion.set(j, new_index);
444360

445361
// update TEMP refs (do afterwards to include forward and backward)
362+
// TODO how to not rename NEW phi variables twice?
363+
// TODO extract to updateTempRefs()?(also use in Context.finalizeFunction()
446364
// can also do outside loop (inside better for cache)
447365
if (is.isPhi()) {
448-
PhiClause* clauses = fi.phis.get(is.phi_clauses.start);
449-
for (u32 p=0; p<is.phi_clauses.count; p++) {
450-
Ref* r = &clauses[p].ref;
451-
if (r.isTemp()) r.value = c.conversion.get(r.value);
452-
}
366+
// TODO
453367
} else {
454368
Ref* r = &is.args[0];
455369
if (r.isTemp()) r.value = c.conversion.get(r.value);

ir_examples/label.c2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ label:
2424
}
2525

2626
public fn i32 main() {
27-
test1(3);
28-
test2();
2927
return 0;
3028
}
3129

0 commit comments

Comments
 (0)