|
1 |
| -use bytecode::instructions::HLInstr; |
| 1 | +use bytecode::instructions::{HLInstr, Opcode}; |
2 | 2 | use irgen::{constants_map::ConstantsMap, register_map::Lifetime};
|
3 | 3 |
|
4 | 4 | /// Represents an IR in which all instructions are in SSA form.
|
5 | 5 | pub struct LuaIR {
|
6 | 6 | pub instrs: Vec<HLInstr>,
|
7 | 7 | pub const_map: ConstantsMap,
|
8 |
| - pub lifetimes: Vec<Lifetime>, |
| 8 | + pub lifetimes: Vec<Option<Lifetime>>, |
| 9 | + pub non_env_lookups: Vec<usize>, |
9 | 10 | }
|
10 | 11 |
|
11 | 12 | impl LuaIR {
|
12 | 13 | pub fn new(
|
13 | 14 | instrs: Vec<HLInstr>,
|
14 | 15 | const_map: ConstantsMap,
|
15 | 16 | mut lifetimes: Vec<Lifetime>,
|
| 17 | + non_env_lookups: Vec<usize>, |
16 | 18 | ) -> LuaIR {
|
17 | 19 | lifetimes.sort_by(|x, y| x.start_point().cmp(&y.start_point()));
|
18 | 20 | LuaIR {
|
19 | 21 | instrs,
|
20 | 22 | const_map,
|
21 |
| - lifetimes, |
| 23 | + lifetimes: lifetimes.into_iter().map(|l| Some(l)).collect(), |
| 24 | + non_env_lookups, |
22 | 25 | }
|
23 | 26 | }
|
| 27 | + |
| 28 | + /// Optimizes the IR and removes unnecessary `SetAttr` instructions. |
| 29 | + /// For example consider the following Lua program: `x = 2` |
| 30 | + /// The compiler generates: |
| 31 | + /// ------------- |
| 32 | + /// LDI 1 0 0 -- load IntTable[0] into register 1, R(1) = 2 |
| 33 | + /// LDS 2 0 0 -- load StringTable[0] into register 2, R(2) = "x" |
| 34 | + /// SetAttr 0 2 1 -- _ENV["x"] = 2 |
| 35 | + /// ------------- |
| 36 | + /// If the user does not query _ENV["x"] directly, that means we can optimize |
| 37 | + /// away the `LDS` and `SetAttr` instructions, and we can treat "x" like a local |
| 38 | + /// variable instead. |
| 39 | + pub fn optimize_env_lookups(&mut self) { |
| 40 | + let mut new_instrs = vec![]; |
| 41 | + let mut new_lifetimes: Vec<Option<Lifetime>> = Vec::with_capacity(self.lifetimes.len()); |
| 42 | + // push _ENV |
| 43 | + new_lifetimes.push(Some(Lifetime::new(0))); |
| 44 | + for _ in 1..self.lifetimes.len() { |
| 45 | + new_lifetimes.push(None); |
| 46 | + } |
| 47 | + let len = self.instrs.len(); |
| 48 | + let mut i = 0; |
| 49 | + while i < len { |
| 50 | + let instr = self.instrs[i]; |
| 51 | + if i < len - 2 && self.non_env_lookups.binary_search(&instr.1).is_ok() { |
| 52 | + // skip the attribute load |
| 53 | + i += 1; |
| 54 | + let attr = self.instrs[i]; |
| 55 | + self.lifetimes[attr.1] = None; |
| 56 | + // skip the SetAttr |
| 57 | + i += 1; |
| 58 | + } |
| 59 | + new_instrs.push(instr); |
| 60 | + let regs_to_update = Self::get_register_operands(instr); |
| 61 | + for r in regs_to_update { |
| 62 | + Self::update_end_point_to(&mut new_lifetimes[r], new_instrs.len()); |
| 63 | + } |
| 64 | + i += 1; |
| 65 | + } |
| 66 | + self.instrs = new_instrs; |
| 67 | + self.lifetimes = new_lifetimes; |
| 68 | + } |
| 69 | + |
| 70 | + /// Gets all the register numbers that are used by the given instruction. |
| 71 | + fn get_register_operands(instr: HLInstr) -> Vec<usize> { |
| 72 | + match instr.0 { |
| 73 | + Opcode::LDI | Opcode::LDS | Opcode::LDF => vec![instr.1], |
| 74 | + Opcode::MOV => vec![instr.1, instr.2], |
| 75 | + _ => vec![instr.1, instr.2, instr.3], |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Update the endpoint of a lifetime. If it doesn't exist, then create it. |
| 80 | + fn update_end_point_to(lifetime: &mut Option<Lifetime>, ep: usize) { |
| 81 | + match lifetime { |
| 82 | + Some(ref mut l) => l.set_end_point(ep), |
| 83 | + None => *lifetime = Some(Lifetime::new(ep - 1)), |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + use bytecode::instructions::Opcode; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn optimize_env_lookups_generates_correct_code() { |
| 95 | + let instrs = vec![ |
| 96 | + HLInstr(Opcode::LDI, 1, 0, 0), |
| 97 | + HLInstr(Opcode::LDS, 2, 0, 0), |
| 98 | + HLInstr(Opcode::SetAttr, 0, 2, 1), |
| 99 | + HLInstr(Opcode::LDI, 3, 0, 0), |
| 100 | + ]; |
| 101 | + let lifetimes = vec![ |
| 102 | + Lifetime::with_end_point(0, 3), |
| 103 | + Lifetime::with_end_point(0, 3), |
| 104 | + Lifetime::with_end_point(1, 3), |
| 105 | + Lifetime::with_end_point(3, 4), |
| 106 | + ]; |
| 107 | + let non_env_lookups = vec![1]; |
| 108 | + let mut ir = LuaIR::new(instrs, ConstantsMap::new(), lifetimes, non_env_lookups); |
| 109 | + ir.optimize_env_lookups(); |
| 110 | + let expected_instrs = vec![HLInstr(Opcode::LDI, 1, 0, 0), HLInstr(Opcode::LDI, 3, 0, 0)]; |
| 111 | + assert_eq!(ir.instrs, expected_instrs); |
| 112 | + let expected_lifetimes = vec![ |
| 113 | + Some(Lifetime::with_end_point(0, 1)), |
| 114 | + Some(Lifetime::with_end_point(0, 1)), |
| 115 | + None, |
| 116 | + Some(Lifetime::with_end_point(1, 2)), |
| 117 | + ]; |
| 118 | + assert_eq!(ir.lifetimes, expected_lifetimes); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn optimize_env_lookups_empty_non_env_lookups() { |
| 123 | + let instrs = vec![ |
| 124 | + HLInstr(Opcode::LDI, 1, 0, 0), |
| 125 | + HLInstr(Opcode::LDS, 2, 0, 0), |
| 126 | + HLInstr(Opcode::SetAttr, 0, 2, 1), |
| 127 | + HLInstr(Opcode::LDI, 3, 0, 0), |
| 128 | + ]; |
| 129 | + let lifetimes = vec![ |
| 130 | + Lifetime::with_end_point(0, 3), |
| 131 | + Lifetime::with_end_point(0, 3), |
| 132 | + Lifetime::with_end_point(1, 3), |
| 133 | + Lifetime::with_end_point(3, 4), |
| 134 | + ]; |
| 135 | + let mut ir = LuaIR::new(instrs.clone(), ConstantsMap::new(), lifetimes, vec![]); |
| 136 | + ir.optimize_env_lookups(); |
| 137 | + assert_eq!(ir.instrs, instrs); |
| 138 | + let expected_lifetimes = vec![ |
| 139 | + Some(Lifetime::with_end_point(0, 3)), |
| 140 | + Some(Lifetime::with_end_point(0, 3)), |
| 141 | + Some(Lifetime::with_end_point(1, 3)), |
| 142 | + Some(Lifetime::with_end_point(3, 4)), |
| 143 | + ]; |
| 144 | + assert_eq!(ir.lifetimes, expected_lifetimes); |
| 145 | + } |
24 | 146 | }
|
0 commit comments