From 036b9aea946ce956c33d0ddc9ad55e86fd407fd0 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Mon, 19 Aug 2019 14:59:19 +0200 Subject: [PATCH] [codegen] Rename and explicit usage of replace_with_alias; It can actually only replace one result; don't try to make it generic yet, since there's no point in doing so right now, and make it do the dumb thing so it's not surprising to use. --- cranelift/codegen/src/simple_preopt.rs | 33 +++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/cranelift/codegen/src/simple_preopt.rs b/cranelift/codegen/src/simple_preopt.rs index 92cbb4c44f..8219f6627c 100644 --- a/cranelift/codegen/src/simple_preopt.rs +++ b/cranelift/codegen/src/simple_preopt.rs @@ -19,13 +19,14 @@ 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) { +/// Replaces the unique result of the instruction inst to an alias of the given value, and +/// replaces the instruction with a nop. Can be used only on instructions producing one unique +/// result, otherwise will assert. +fn replace_single_result_with_alias(dfg: &mut DataFlowGraph, inst: Inst, 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(); + debug_assert!(results.len(&dfg.value_lists) == 1); + let result = results.get(0, &dfg.value_lists).unwrap(); dfg.change_to_alias(result, value); // Replace instruction by a nop. @@ -185,7 +186,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso if is_rem { pos.func.dfg.replace(inst).iconst(I32, 0); } else { - replace_with_alias(&mut pos.func.dfg, inst, 0, n1); + replace_single_result_with_alias(&mut pos.func.dfg, inst, n1); } } @@ -240,7 +241,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 { - replace_with_alias(&mut pos.func.dfg, inst, 0, qf); + replace_single_result_with_alias(&mut pos.func.dfg, inst, qf); } } @@ -255,7 +256,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso if is_rem { pos.func.dfg.replace(inst).iconst(I64, 0); } else { - replace_with_alias(&mut pos.func.dfg, inst, 0, n1); + replace_single_result_with_alias(&mut pos.func.dfg, inst, n1); } } @@ -310,7 +311,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 { - replace_with_alias(&mut pos.func.dfg, inst, 0, qf); + replace_single_result_with_alias(&mut pos.func.dfg, inst, qf); } } @@ -328,7 +329,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso if is_rem { pos.func.dfg.replace(inst).iconst(I32, 0); } else { - replace_with_alias(&mut pos.func.dfg, inst, 0, n1); + replace_single_result_with_alias(&mut pos.func.dfg, inst, n1); } } @@ -354,7 +355,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso if is_negative { pos.func.dfg.replace(inst).irsub_imm(t4, 0); } else { - replace_with_alias(&mut pos.func.dfg, inst, 0, t4); + replace_single_result_with_alias(&mut pos.func.dfg, inst, t4); } } } else { @@ -384,7 +385,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 { - replace_with_alias(&mut pos.func.dfg, inst, 0, qf); + replace_single_result_with_alias(&mut pos.func.dfg, inst, qf); } } } @@ -403,7 +404,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso if is_rem { pos.func.dfg.replace(inst).iconst(I64, 0); } else { - replace_with_alias(&mut pos.func.dfg, inst, 0, n1); + replace_single_result_with_alias(&mut pos.func.dfg, inst, n1); } } @@ -429,7 +430,7 @@ fn do_divrem_transformation(divrem_info: &DivRemByConstInfo, pos: &mut FuncCurso if is_negative { pos.func.dfg.replace(inst).irsub_imm(t4, 0); } else { - replace_with_alias(&mut pos.func.dfg, inst, 0, t4); + replace_single_result_with_alias(&mut pos.func.dfg, inst, t4); } } } else { @@ -459,7 +460,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 { - replace_with_alias(&mut pos.func.dfg, inst, 0, qf); + replace_single_result_with_alias(&mut pos.func.dfg, inst, qf); } } } @@ -665,7 +666,7 @@ fn simplify(pos: &mut FuncCursor, inst: Inst) { | (Opcode::UshrImm, 0) | (Opcode::SshrImm, 0) => { // Alias the result value with the original argument. - replace_with_alias(&mut pos.func.dfg, inst, 0, arg); + replace_single_result_with_alias(&mut pos.func.dfg, inst, arg); return; } (Opcode::ImulImm, 0) | (Opcode::BandImm, 0) => {