|
| 1 | +use crate::jit::analyzer::basic_block::BasicBlock; |
| 2 | +use crate::jit::inst_info::InstInfo; |
| 3 | +use crate::jit::op::Op; |
| 4 | +use crate::jit::reg::{reg_reserve, Reg, RegReserve}; |
| 5 | +use crate::jit::Cond; |
| 6 | +use crate::logging::block_asm_println; |
| 7 | +use bilge::prelude::*; |
| 8 | + |
| 9 | +pub enum JitBranchInfo { |
| 10 | + Idle(usize), |
| 11 | + Local(usize), |
| 12 | + None, |
| 13 | +} |
| 14 | + |
| 15 | +// Taken from https://github.com/melonDS-emu/melonDS/blob/24c402af51fe9c0537582173fc48d1ad3daff459/src/ARMJIT.cpp#L352 |
| 16 | +fn is_idle_loop(insts: &[InstInfo]) -> bool { |
| 17 | + let mut regs_written_to = RegReserve::new(); |
| 18 | + let mut regs_disallowed_to_write = RegReserve::new(); |
| 19 | + for (i, inst) in insts.iter().enumerate() { |
| 20 | + if (inst.is_branch() && i < insts.len() - 1) |
| 21 | + || matches!(inst.op, Op::Swi | Op::SwiT | Op::Mcr | Op::Mrc | Op::MrsRc | Op::MrsRs | Op::MsrIc | Op::MsrIs | Op::MsrRc | Op::MsrRs) |
| 22 | + || inst.op.is_write_mem_transfer() |
| 23 | + { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + let src_regs = inst.src_regs & !reg_reserve!(Reg::PC); |
| 28 | + let out_regs = inst.out_regs & !reg_reserve!(Reg::PC); |
| 29 | + regs_disallowed_to_write |= src_regs & !regs_written_to; |
| 30 | + |
| 31 | + if !(out_regs & regs_disallowed_to_write).is_empty() { |
| 32 | + return false; |
| 33 | + } |
| 34 | + regs_written_to |= out_regs; |
| 35 | + } |
| 36 | + true |
| 37 | +} |
| 38 | + |
| 39 | +fn analyze_branch_label(insts: &[InstInfo], thumb: bool, branch_index: usize, cond: Cond, pc: u32, target_pc: u32) -> JitBranchInfo { |
| 40 | + if (cond as u8) < (Cond::AL as u8) && target_pc < pc { |
| 41 | + let diff = (pc - target_pc) >> if thumb { 1 } else { 2 }; |
| 42 | + if diff as usize <= branch_index { |
| 43 | + let jump_to_index = branch_index - diff as usize; |
| 44 | + if is_idle_loop(&insts[jump_to_index..branch_index + 1]) { |
| 45 | + return JitBranchInfo::Idle(jump_to_index); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + let relative_index = (target_pc as i32 - pc as i32) >> if thumb { 1 } else { 2 }; |
| 51 | + let target_index = branch_index as i32 + relative_index; |
| 52 | + if target_index >= 0 && (target_index as usize) < insts.len() { |
| 53 | + JitBranchInfo::Local(target_index as usize) |
| 54 | + } else { |
| 55 | + JitBranchInfo::None |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[bitsize(8)] |
| 60 | +#[derive(Copy, Clone, FromBits)] |
| 61 | +pub struct InstMetadata { |
| 62 | + pub idle_loop: bool, |
| 63 | + pub external_branch: bool, |
| 64 | + pub local_branch_entry: bool, |
| 65 | + not_used: u5, |
| 66 | +} |
| 67 | + |
| 68 | +impl Default for InstMetadata { |
| 69 | + fn default() -> Self { |
| 70 | + InstMetadata::from(0) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Default)] |
| 75 | +pub struct AsmAnalyzer { |
| 76 | + pub basic_blocks: Vec<BasicBlock>, |
| 77 | + pub insts_metadata: Vec<InstMetadata>, |
| 78 | +} |
| 79 | + |
| 80 | +impl AsmAnalyzer { |
| 81 | + fn create_basic_blocks(&mut self, start_pc: u32, insts: &[InstInfo], thumb: bool) { |
| 82 | + self.basic_blocks.clear(); |
| 83 | + self.insts_metadata.clear(); |
| 84 | + self.insts_metadata.resize(insts.len(), InstMetadata::default()); |
| 85 | + |
| 86 | + let pc_shift = if thumb { 1 } else { 2 }; |
| 87 | + for i in 0..insts.len() { |
| 88 | + if insts[i].op.is_labelled_branch() && !insts[i].out_regs.is_reserved(Reg::LR) { |
| 89 | + let cond = insts[i].get_branch_cond(); |
| 90 | + let pc = start_pc + ((i as u32) << pc_shift); |
| 91 | + let relative_pc = insts[i].operands()[0].as_imm().unwrap() as i32 + (2 << pc_shift); |
| 92 | + let target_pc = (pc as i32 + relative_pc) as u32; |
| 93 | + |
| 94 | + match analyze_branch_label(insts, thumb, i, cond, pc, target_pc) { |
| 95 | + JitBranchInfo::Idle(target_index) => { |
| 96 | + self.insts_metadata[i].set_idle_loop(true); |
| 97 | + self.insts_metadata[target_index].set_local_branch_entry(true); |
| 98 | + } |
| 99 | + JitBranchInfo::Local(target_index) => { |
| 100 | + self.insts_metadata[target_index].set_local_branch_entry(true); |
| 101 | + } |
| 102 | + JitBranchInfo::None => { |
| 103 | + self.insts_metadata[i].set_external_branch(true); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + let mut block_start = 0; |
| 110 | + for i in 0..insts.len() { |
| 111 | + if self.insts_metadata[i].local_branch_entry() { |
| 112 | + if i > block_start { |
| 113 | + self.basic_blocks.push(BasicBlock::new(start_pc + ((block_start as u32) << pc_shift), block_start, i - 1)); |
| 114 | + } |
| 115 | + block_start = i; |
| 116 | + } |
| 117 | + |
| 118 | + if insts[i].op.is_labelled_branch() && !insts[i].out_regs.is_reserved(Reg::LR) { |
| 119 | + self.basic_blocks.push(BasicBlock::new(start_pc + ((block_start as u32) << pc_shift), block_start, i)); |
| 120 | + block_start = i + 1; |
| 121 | + } |
| 122 | + } |
| 123 | + if block_start < insts.len() { |
| 124 | + self.basic_blocks.push(BasicBlock::new(start_pc + ((block_start as u32) << pc_shift), block_start, insts.len() - 1)); |
| 125 | + } |
| 126 | + |
| 127 | + for basic_block in &mut self.basic_blocks { |
| 128 | + basic_block.resolve_live_regs(insts); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + pub fn get_basic_block_metadata(&self, basic_block_index: usize) -> InstMetadata { |
| 133 | + self.insts_metadata[self.basic_blocks[basic_block_index].start_index] |
| 134 | + } |
| 135 | + |
| 136 | + pub fn get_next_live_regs(&self, basic_block_index: usize, inst_index: usize) -> RegReserve { |
| 137 | + let basic_block = &self.basic_blocks[basic_block_index]; |
| 138 | + basic_block.live_regs[inst_index - basic_block.start_index + 1] |
| 139 | + } |
| 140 | + |
| 141 | + pub fn get_basic_block_from_inst(&self, inst_index: usize) -> usize { |
| 142 | + for (i, basic_block) in self.basic_blocks.iter().enumerate() { |
| 143 | + if inst_index >= basic_block.start_index && inst_index <= basic_block.end_index { |
| 144 | + return i; |
| 145 | + } |
| 146 | + } |
| 147 | + unreachable!() |
| 148 | + } |
| 149 | + |
| 150 | + pub fn analyze(&mut self, start_pc: u32, insts: &[InstInfo], thumb: bool) { |
| 151 | + self.create_basic_blocks(start_pc, insts, thumb); |
| 152 | + |
| 153 | + for (i, basic_block) in self.basic_blocks.iter().enumerate() { |
| 154 | + block_asm_println!("basic block {i} start inst {} - {}", basic_block.start_index, basic_block.end_index); |
| 155 | + block_asm_println!("{:?}", basic_block.debug(insts, thumb)); |
| 156 | + block_asm_println!("basic block {i} end"); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments