Commit 546ea4b1 authored by qinsoon's avatar qinsoon

a peephole opt to remove unncessary jmps

a jump that directly is directly followed by a label of its target
can be removed.
parent e905a7af
Pipeline #204 failed with stage
in 25 minutes and 4 seconds
......@@ -107,6 +107,8 @@ pub struct MuFunctionVersion {
pub content: Option<FunctionContent>,
pub context: FunctionContext,
pub force_inline: bool,
pub block_trace: Option<Vec<MuID>> // only available after Trace Generation Pass
}
......@@ -142,7 +144,8 @@ impl MuFunctionVersion {
sig: sig,
content: None,
context: FunctionContext::new(),
block_trace: None
block_trace: None,
force_inline: false
}
}
......
......@@ -403,6 +403,30 @@ impl MachineCode for ASMCode {
fn is_using_mem_op(&self, index: usize) -> bool {
self.code[index].is_mem_op_used
}
fn is_jmp(&self, index: usize) -> Option<MuName> {
let inst = self.code.get(index);
match inst {
Some(inst) if inst.code.starts_with("jmp") => {
let split : Vec<&str> = inst.code.split(' ').collect();
Some(String::from(split[1]))
}
_ => None
}
}
fn is_label(&self, index: usize) -> Option<MuName> {
let inst = self.code.get(index);
match inst {
Some(inst) if inst.code.ends_with(':') => {
let split : Vec<&str> = inst.code.split(':').collect();
Some(String::from(split[0]))
}
_ => None
}
}
fn get_succs(&self, index: usize) -> &Vec<usize> {
&self.code[index].succs
......
......@@ -53,6 +53,35 @@ impl PeepholeOptimization {
}
}
}
pub fn remove_unnecessary_jump(&mut self, inst: usize, cf: &mut CompiledFunction) {
let mut mc = cf.mc_mut();
// if this is last instruction, return
if inst == mc.number_of_insts() - 1 {
return;
}
// if this inst jumps to a label that directly follows it, we can set it to nop
let opt_dest = mc.is_jmp(inst);
match opt_dest {
Some(ref dest) => {
let opt_label = mc.is_label(inst + 1);
match opt_label {
Some(ref label) if dest == label => {
mc.set_inst_nop(inst);
}
_ => {
// do nothing
}
}
}
None => {
// do nothing
}
}
}
}
impl CompilerPass for PeepholeOptimization {
......@@ -70,6 +99,7 @@ impl CompilerPass for PeepholeOptimization {
for i in 0..cf.mc().number_of_insts() {
self.remove_redundant_move(i, &mut cf);
self.remove_unnecessary_jump(i, &mut cf);
}
trace!("after peephole optimization:");
......
......@@ -105,6 +105,8 @@ pub trait MachineCode {
fn is_move(&self, index: usize) -> bool;
fn is_using_mem_op(&self, index: usize) -> bool;
fn is_jmp(&self, index: usize) -> Option<MuName>;
fn is_label(&self, index: usize) -> Option<MuName>;
fn get_succs(&self, index: usize) -> &Vec<usize>;
fn get_preds(&self, index: usize) -> &Vec<usize>;
......
use ast::ir::*;
use ast::ptr::*;
use ast::inst::*;
use vm::VM;
use compiler::CompilerPass;
use std::any::Any;
use std::sync::RwLock;
use std::collections::HashMap;
pub struct Inlining {
name: &'static str,
// whether a function version should be inlined
should_inline: HashMap<MuID, bool>
}
impl Inlining {
pub fn new() -> Inlining {
Inlining{
name: "Inlining",
should_inline: HashMap::new()
}
}
fn check(&mut self, vm: &VM, func: &mut MuFunctionVersion) -> bool {
unimplemented!()
}
fn inline(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
unimplemented!()
}
}
impl CompilerPass for Inlining {
fn name(&self) -> &'static str {
self.name
}
fn as_any(&self) -> &Any {
self
}
fn visit_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
if self.check(vm, func) {
self.inline(vm, func);
}
}
}
\ No newline at end of file
......@@ -6,7 +6,9 @@ mod tree_gen;
mod control_flow;
mod trace_gen;
mod gen_mov_phi;
mod inlining;
pub use compiler::passes::inlining::Inlining;
pub use compiler::passes::def_use::DefUse;
pub use compiler::passes::tree_gen::TreeGen;
pub use compiler::passes::control_flow::ControlFlowAnalysis;
......
......@@ -1150,6 +1150,7 @@ impl<'lb, 'lvm> BundleLoader<'lb, 'lvm> {
content: Some(ctn),
context: fcb.ctx,
block_trace: None,
force_inline: false
};
trace!("Function version built {} {:?}", id, impl_fv);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment