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
//! Registers MIPS code reads after a call.
use std::collections::HashSet;
/// Takes a call to leave alone the argument and temporary registers the
/// code reads after it before writing them.
///
/// The convention lets a callee change those registers, and none of them
/// returns a value. Compiled code reads one after a call only where the
/// compiler knows the callee and that it leaves the register as it was,
/// which it does for the functions of its own module. Here the callee of
/// a call through a pointer is not known, and the summary of a known one
/// may say more than the compiler found, as when it calls through a
/// pointer itself; the read says what the compiler knew.
pub(super) fn keep_registers_read_after_calls(blocks: &mut [vlk_ir::ir::Block]) {
use vlk_ir::ir::Stmt;
use vlk_types::types::{CallKind, Convention, Registers};
// a0 to a3, t0 to t9 and at.
let candidates: Registers = (2..=15).chain([24]).collect();
let unknown = |stmt: &Stmt| {
matches!(
stmt,
Stmt::Call {
kind: CallKind::Invoke,
convention: Convention::O32,
..
}
) && !stmt.has_declared_call()
};
if !blocks.iter().flat_map(|block| &block.stmts).any(unknown) {
return;
}
// The candidates a statement reads, other than as what a call or a
// return passes on, and those it writes.
let reads = |stmt: &Stmt| -> Registers {
if matches!(stmt, Stmt::Call { .. } | Stmt::Ret { .. }) {
return Registers::default();
}
let mut read = HashSet::new();
for operand in stmt.operands() {
vlk_ir::ir::visit::collect_reads(operand, &mut read);
}
read.into_iter()
.filter(|register| candidates.contains(*register))
.collect()
};
let writes = |stmt: &Stmt| -> Registers {
vlk_ir::ir::visit::statement_registers(stmt)
.0
.into_iter()
.filter(|register| candidates.contains(*register))
.collect()
};
// The registers read before they are written from `position` on in
// a block, given those read after it.
let live_from = |block: &vlk_ir::ir::Block, position: usize, after: Registers| {
let mut live = after;
for stmt in block.stmts[position..].iter().rev() {
live = Registers(live.0 & !writes(stmt).0 | reads(stmt).0);
}
live
};
let successors: Vec<Vec<usize>> = (0..blocks.len())
.map(|index| vlk_ir::ir::cfg::block_successors(blocks, index))
.collect();
// A call that keeps a register lets a read after it reach the calls
// before it: once more for each call found to keep one.
while {
let mut entering = vec![Registers::default(); blocks.len()];
loop {
let mut changed = false;
for index in (0..blocks.len()).rev() {
let after = successors[index]
.iter()
.fold(Registers::default(), |all, next| {
Registers(all.0 | entering[*next].0)
});
let live = live_from(&blocks[index], 0, after);
if live != entering[index] {
entering[index] = live;
changed = true;
}
}
if !changed {
break;
}
}
let mut found = false;
for index in 0..blocks.len() {
let after = successors[index]
.iter()
.fold(Registers::default(), |all, next| {
Registers(all.0 | entering[*next].0)
});
for position in 0..blocks[index].stmts.len() {
if !unknown(&blocks[index].stmts[position]) {
continue;
}
let kept = live_from(&blocks[index], position + 1, after);
if kept.0 == 0 {
continue;
}
if let Stmt::Call {
convention,
clobbers,
..
} = &mut blocks[index].stmts[position]
{
let changed: Registers =
convention.clobbered_by(*clobbers).into_iter().collect();
if changed.0 & kept.0 != 0 {
*clobbers = Some(Registers(changed.0 & !kept.0));
found = true;
}
}
}
}
found
} {}
}