Use aliasing instead of copying in simple_preopt;

This commit is contained in:
Benjamin Bouvier
2019-08-16 14:55:34 +02:00
committed by Dan Gohman
parent 2ee35b7ea1
commit b659262d2a
4 changed files with 84 additions and 76 deletions

View File

@@ -18,6 +18,20 @@ use crate::ir::{
};
use crate::timing;
#[inline]
/// Replaces the result_index nth result of instruction inst to an alias of the given value, and
/// replaces the instruction with a nop.
fn replace_with_alias(dfg: &mut DataFlowGraph, inst: Inst, result_index: usize, value: Value) {
// Replace the result value by an alias.
let results = dfg.detach_results(inst);
debug_assert!(results.len(&dfg.value_lists) > result_index);
let result = results.get(result_index, &dfg.value_lists).unwrap();
dfg.change_to_alias(result, value);
// Replace instruction by a nop.
dfg.replace(inst).nop();
}
//----------------------------------------------------------------------
//
// Pattern-match helpers and transformation for div and rem by constants.
@@ -171,7 +185,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
if is_rem {
pos.func.dfg.replace(inst).iconst(I32, 0);
} else {
pos.func.dfg.replace(inst).copy(n1);
replace_with_alias(&mut pos.func.dfg, inst, 0, n1);
}
}
@@ -226,7 +240,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
let tt = pos.ins().imul_imm(qf, d as i64);
pos.func.dfg.replace(inst).isub(n1, tt);
} else {
pos.func.dfg.replace(inst).copy(qf);
replace_with_alias(&mut pos.func.dfg, inst, 0, qf);
}
}
@@ -241,7 +255,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
if is_rem {
pos.func.dfg.replace(inst).iconst(I64, 0);
} else {
pos.func.dfg.replace(inst).copy(n1);
replace_with_alias(&mut pos.func.dfg, inst, 0, n1);
}
}
@@ -296,7 +310,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
let tt = pos.ins().imul_imm(qf, d as i64);
pos.func.dfg.replace(inst).isub(n1, tt);
} else {
pos.func.dfg.replace(inst).copy(qf);
replace_with_alias(&mut pos.func.dfg, inst, 0, qf);
}
}
@@ -314,7 +328,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
if is_rem {
pos.func.dfg.replace(inst).iconst(I32, 0);
} else {
pos.func.dfg.replace(inst).copy(n1);
replace_with_alias(&mut pos.func.dfg, inst, 0, n1);
}
}
@@ -340,7 +354,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
if is_negative {
pos.func.dfg.replace(inst).irsub_imm(t4, 0);
} else {
pos.func.dfg.replace(inst).copy(t4);
replace_with_alias(&mut pos.func.dfg, inst, 0, t4);
}
}
} else {
@@ -370,7 +384,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
let tt = pos.ins().imul_imm(qf, d as i64);
pos.func.dfg.replace(inst).isub(n1, tt);
} else {
pos.func.dfg.replace(inst).copy(qf);
replace_with_alias(&mut pos.func.dfg, inst, 0, qf);
}
}
}
@@ -389,7 +403,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
if is_rem {
pos.func.dfg.replace(inst).iconst(I64, 0);
} else {
pos.func.dfg.replace(inst).copy(n1);
replace_with_alias(&mut pos.func.dfg, inst, 0, n1);
}
}
@@ -415,7 +429,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
if is_negative {
pos.func.dfg.replace(inst).irsub_imm(t4, 0);
} else {
pos.func.dfg.replace(inst).copy(t4);
replace_with_alias(&mut pos.func.dfg, inst, 0, t4);
}
}
} else {
@@ -445,7 +459,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso
let tt = pos.ins().imul_imm(qf, d);
pos.func.dfg.replace(inst).isub(n1, tt);
} else {
pos.func.dfg.replace(inst).copy(qf);
replace_with_alias(&mut pos.func.dfg, inst, 0, qf);
}
}
}
@@ -651,13 +665,7 @@ fn simplify(pos: &mut FuncCursor, inst: Inst) {
| (Opcode::UshrImm, 0)
| (Opcode::SshrImm, 0) => {
// Alias the result value with the original argument.
let results = pos.func.dfg.detach_results(inst);
debug_assert!(results.len(&pos.func.dfg.value_lists) == 1);
let first_result = results.get(0, &pos.func.dfg.value_lists).unwrap();
pos.func.dfg.change_to_alias(first_result, arg);
// Replace instruction by a nop.
pos.func.dfg.replace(inst).nop();
replace_with_alias(&mut pos.func.dfg, inst, 0, arg);
return;
}
(Opcode::ImulImm, 0) | (Opcode::BandImm, 0) => {