Skip to content

Commit c65b291

Browse files
committed
Cache io of instructions
1 parent 26a2e05 commit c65b291

File tree

2 files changed

+16
-58
lines changed

2 files changed

+16
-58
lines changed

src/jit/assembler/basic_block.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,8 @@ impl BasicBlock {
328328
BlockInstKind::GuestPc(pc) => {
329329
println!("(0x{:x}, 0x{pc:x}),", opcodes.len() + opcodes_offset);
330330
}
331-
BlockInstKind::Label { guest_pc, .. } => {
332-
if let Some(pc) = guest_pc {
333-
println!("(0x{:x}, 0x{pc:x}),", opcodes.len() + opcodes_offset);
334-
}
331+
BlockInstKind::Label { guest_pc: Some(pc), .. } => {
332+
println!("(0x{:x}, 0x{pc:x}),", opcodes.len() + opcodes_offset);
335333
}
336334
_ => {}
337335
}

src/jit/assembler/block_inst.rs

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::jit::inst_info::{InstInfo, Operand, Shift, ShiftValue};
99
use crate::jit::reg::{Reg, RegReserve};
1010
use crate::jit::{Cond, MemoryAmount, ShiftType};
1111
use bilge::prelude::*;
12+
use std::cell::RefCell;
1213
use std::fmt::{Debug, Formatter};
1314
use std::ops::{Deref, DerefMut};
1415
use std::ptr::NonNull;
@@ -75,28 +76,35 @@ pub struct BranchEncoding {
7576
#[derive(Clone)]
7677
pub struct BlockInst {
7778
pub kind: BlockInstKind,
79+
io_cache: RefCell<Option<(BlockRegSet, BlockRegSet)>>,
7880
}
7981

8082
impl BlockInst {
8183
pub fn new(kind: BlockInstKind) -> Self {
82-
BlockInst { kind }
84+
BlockInst { kind, io_cache: RefCell::new(None) }
8385
}
8486

8587
pub fn get_io(&self) -> (BlockRegSet, BlockRegSet) {
86-
self.kind.get_io()
88+
let mut cached_io = self.io_cache.borrow_mut();
89+
match *cached_io {
90+
None => {
91+
let io = self.kind.get_io();
92+
*cached_io = Some(io);
93+
io
94+
}
95+
Some(cache) => cache,
96+
}
8797
}
8898

8999
pub fn replace_input_regs(&mut self, old: BlockReg, new: BlockReg) {
100+
*self.io_cache.borrow_mut() = None;
90101
self.kind.replace_input_regs(old, new);
91102
}
92103

93104
pub fn replace_output_regs(&mut self, old: BlockReg, new: BlockReg) {
105+
*self.io_cache.borrow_mut() = None;
94106
self.kind.replace_output_regs(old, new);
95107
}
96-
97-
pub fn replace_regs(&mut self, old: BlockReg, new: BlockReg) {
98-
self.kind.replace_regs(old, new);
99-
}
100108
}
101109

102110
impl From<BlockInstKind> for BlockInst {
@@ -515,54 +523,6 @@ impl BlockInstKind {
515523
}
516524
}
517525

518-
fn replace_regs(&mut self, old: BlockReg, new: BlockReg) {
519-
match self {
520-
BlockInstKind::Alu3 { operands, .. } | BlockInstKind::Mul { operands, .. } => Self::replace_shift_operands(operands, old, new),
521-
BlockInstKind::Alu2Op1 { operands, .. } => Self::replace_shift_operands(operands, old, new),
522-
BlockInstKind::Alu2Op0 { operands, .. } => Self::replace_shift_operands(operands, old, new),
523-
BlockInstKind::Transfer { operands, .. } => Self::replace_shift_operands(operands, old, new),
524-
BlockInstKind::TransferMultiple { operand, .. } => Self::replace_reg(operand, old, new),
525-
BlockInstKind::SystemReg { operand, .. } => Self::replace_operand(operand, old, new),
526-
BlockInstKind::Bfc { operand, .. } => Self::replace_reg(operand, old, new),
527-
BlockInstKind::Bfi { operands, .. } => {
528-
Self::replace_reg(&mut operands[0], old, new);
529-
Self::replace_reg(&mut operands[1], old, new);
530-
}
531-
532-
BlockInstKind::SaveContext { .. } => {
533-
unreachable!()
534-
}
535-
BlockInstKind::SaveReg { reg_mapped, thread_regs_addr_reg, .. } => {
536-
Self::replace_reg(reg_mapped, old, new);
537-
Self::replace_reg(thread_regs_addr_reg, old, new);
538-
}
539-
BlockInstKind::RestoreReg {
540-
reg_mapped,
541-
thread_regs_addr_reg,
542-
tmp_guest_cpsr_reg,
543-
..
544-
} => {
545-
Self::replace_reg(reg_mapped, old, new);
546-
Self::replace_reg(thread_regs_addr_reg, old, new);
547-
Self::replace_reg(tmp_guest_cpsr_reg, old, new);
548-
}
549-
550-
BlockInstKind::Call { func_reg, .. } => Self::replace_reg(func_reg, old, new),
551-
BlockInstKind::GenericGuestInst { regs_mapping, .. } => {
552-
for reg_mapping in regs_mapping {
553-
Self::replace_reg(reg_mapping, old, new);
554-
}
555-
}
556-
BlockInstKind::CallCommon { .. }
557-
| BlockInstKind::Label { .. }
558-
| BlockInstKind::Branch { .. }
559-
| BlockInstKind::GuestPc(_)
560-
| BlockInstKind::Bkpt(_)
561-
| BlockInstKind::Prologue
562-
| BlockInstKind::Epilogue { .. } => {}
563-
}
564-
}
565-
566526
fn save_guest_cpsr(opcodes: &mut Vec<u32>, thread_regs_addr_reg: Reg, host_reg: Reg) {
567527
opcodes.push(Mrs::cpsr(host_reg, Cond::AL));
568528
// Only copy the cond flags from host cpsr

0 commit comments

Comments
 (0)