From b659262d2a6e6c04e149f427ba009ed0786b4e34 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 16 Aug 2019 14:55:34 +0200 Subject: [PATCH] Use aliasing instead of copying in simple_preopt; --- cranelift/codegen/src/simple_preopt.rs | 42 ++++++---- .../simple_preopt/div_by_const_indirect.clif | 16 ++-- .../div_by_const_non_power_of_2.clif | 80 +++++++++---------- .../div_by_const_power_of_2.clif | 22 ++--- 4 files changed, 84 insertions(+), 76 deletions(-) diff --git a/cranelift/codegen/src/simple_preopt.rs b/cranelift/codegen/src/simple_preopt.rs index 73c6a56148..92cbb4c44f 100644 --- a/cranelift/codegen/src/simple_preopt.rs +++ b/cranelift/codegen/src/simple_preopt.rs @@ -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) => { diff --git a/cranelift/filetests/filetests/simple_preopt/div_by_const_indirect.clif b/cranelift/filetests/filetests/simple_preopt/div_by_const_indirect.clif index fa66337fc2..5833b0f371 100644 --- a/cranelift/filetests/filetests/simple_preopt/div_by_const_indirect.clif +++ b/cranelift/filetests/filetests/simple_preopt/div_by_const_indirect.clif @@ -13,8 +13,8 @@ ebb0(v0: i32): ; check: isub v0, v4 ; check: ushr_imm v5, 1 ; check: iadd v6, v4 - ; check: ushr_imm v7, 2 - ; check: copy v8 + ; check: v8 = ushr_imm v7, 2 + ; check: v2 -> v8 return v2 } @@ -27,8 +27,8 @@ ebb0(v0: i32): ; check: smulhi v0, v3 ; check: sshr_imm v4, 3 ; check: ushr_imm v5, 31 - ; check: iadd v5, v6 - ; check: copy v7 + ; check: v7 = iadd v5, v6 + ; check: v2 -> v7 return v2 } @@ -39,8 +39,8 @@ ebb0(v0: i64): ; check: iconst.i64 1337 ; check: iconst.i64 0xc411_9d95_2866_a139 ; check: umulhi v0, v3 - ; check: ushr_imm v4, 10 - ; check: copy v5 + ; check: v5 = ushr_imm v4, 10 + ; check: v2 -> v5 return v2 } @@ -53,7 +53,7 @@ ebb0(v0: i64): ; check: smulhi v0, v3 ; check: sshr_imm v4, 14 ; check: ushr_imm v5, 63 - ; check: iadd v5, v6 - ; check: copy v7 + ; check: v7 = iadd v5, v6 + ; check: v2 -> v7 return v2 } diff --git a/cranelift/filetests/filetests/simple_preopt/div_by_const_non_power_of_2.clif b/cranelift/filetests/filetests/simple_preopt/div_by_const_non_power_of_2.clif index fa0ac41bff..2a16699aae 100644 --- a/cranelift/filetests/filetests/simple_preopt/div_by_const_non_power_of_2.clif +++ b/cranelift/filetests/filetests/simple_preopt/div_by_const_non_power_of_2.clif @@ -12,8 +12,8 @@ ebb0(v0: i32): ; check: isub v0, v3 ; check: ushr_imm v4, 1 ; check: iadd v5, v3 - ; check: ushr_imm v6, 2 - ; check: copy v7 + ; check: v7 = ushr_imm v6, 2 + ; check: v1 -> v7 return v1 } @@ -23,8 +23,8 @@ ebb0(v0: i32): v1 = udiv_imm v0, 125 ; check: iconst.i32 0x1062_4dd3 ; check: umulhi v0, v2 - ; check: ushr_imm v3, 3 - ; check: copy v4 + ; check: v4 = ushr_imm v3, 3 + ; check: v1 -> v4 return v1 } @@ -33,8 +33,8 @@ function %t_udiv32_p641(i32) -> i32 { ebb0(v0: i32): v1 = udiv_imm v0, 641 ; check: iconst.i32 0x0066_3d81 - ; check: umulhi v0, v2 - ; check: copy v3 + ; check: v3 = umulhi v0, v2 + ; check: v1 -> v3 return v1 } @@ -48,8 +48,8 @@ ebb0(v0: i32): ; check: iconst.i32 0xffff_ffff_d555_5555 ; check: smulhi v0, v2 ; check: ushr_imm v3, 31 - ; check: iadd v3, v4 - ; check: copy v5 + ; check: v5 = iadd v3, v4 + ; check: v1 -> v5 return v1 } @@ -61,8 +61,8 @@ ebb0(v0: i32): ; check: smulhi v0, v2 ; check: sshr_imm v3, 1 ; check: ushr_imm v4, 31 - ; check: iadd v4, v5 - ; check: copy v6 + ; check: v6 = iadd v4, v5 + ; check: v1 -> v6 return v1 } @@ -75,8 +75,8 @@ ebb0(v0: i32): ; check: isub v3, v0 ; check: sshr_imm v4, 1 ; check: ushr_imm v5, 31 - ; check: iadd v5, v6 - ; check: copy v7 + ; check: v7 = iadd v5, v6 + ; check: v1 -> v7 return v1 } @@ -87,8 +87,8 @@ ebb0(v0: i32): ; check: iconst.i32 0x2aaa_aaab ; check: smulhi v0, v2 ; check: ushr_imm v3, 31 - ; check: iadd v3, v4 - ; check: copy v5 + ; check: v5 = iadd v3, v4 + ; check: v1 -> v5 return v1 } @@ -101,8 +101,8 @@ ebb0(v0: i32): ; check: iadd v3, v0 ; check: sshr_imm v4, 2 ; check: ushr_imm v5, 31 - ; check: iadd v5, v6 - ; check: copy v7 + ; check: v7 = iadd v5, v6 + ; check: v1 -> v7 return v1 } @@ -114,8 +114,8 @@ ebb0(v0: i32): ; check: smulhi v0, v2 ; check: sshr_imm v3, 8 ; check: ushr_imm v4, 31 - ; check: iadd v4, v5 - ; check: copy v6 + ; check: v6 = iadd v4, v5 + ; check: v1 -> v6 return v1 } @@ -131,8 +131,8 @@ ebb0(v0: i64): ; check: isub v0, v3 ; check: ushr_imm v4, 1 ; check: iadd v5, v3 - ; check: ushr_imm v6, 2 - ; check: copy v7 + ; check: v7 = ushr_imm v6, 2 + ; check: v1 -> v7 return v1 } @@ -142,8 +142,8 @@ ebb0(v0: i64): v1 = udiv_imm v0, 9 ; check: iconst.i64 0xe38e_38e3_8e38_e38f ; check: umulhi v0, v2 - ; check: ushr_imm v3, 3 - ; check: copy v4 + ; check: v4 = ushr_imm v3, 3 + ; check: v1 -> v4 return v1 } @@ -156,8 +156,8 @@ ebb0(v0: i64): ; check: isub v0, v3 ; check: ushr_imm v4, 1 ; check: iadd v5, v3 - ; check: ushr_imm v6, 6 - ; check: copy v7 + ; check: v7 = ushr_imm v6, 6 + ; check: v1 -> v7 return v1 } @@ -166,8 +166,8 @@ function %t_udiv64_p274177(i64) -> i64 { ebb0(v0: i64): v1 = udiv_imm v0, 274177 ; check: iconst.i64 0x3d30_f19c_d101 - ; check: umulhi v0, v2 - ; check: copy v3 + ; check: v3 = umulhi v0, v2 + ; check: v1 -> v3 return v1 } @@ -182,8 +182,8 @@ ebb0(v0: i64): ; check: smulhi v0, v2 ; check: sshr_imm v3, 7 ; check: ushr_imm v4, 63 - ; check: iadd v4, v5 - ; check: copy v6 + ; check: v6 = iadd v4, v5 + ; check: v1 -> v6 return v1 } @@ -194,8 +194,8 @@ ebb0(v0: i64): ; check: iconst.i64 0xd555_5555_5555_5555 ; check: smulhi v0, v2 ; check: ushr_imm v3, 63 - ; check: iadd v3, v4 - ; check: copy v5 + ; check: v5 = iadd v3, v4 + ; check: v1 -> v5 return v1 } @@ -207,8 +207,8 @@ ebb0(v0: i64): ; check: smulhi v0, v2 ; check: sshr_imm v3, 1 ; check: ushr_imm v4, 63 - ; check: iadd v4, v5 - ; check: copy v6 + ; check: v6 = iadd v4, v5 + ; check: v1 -> v6 return v1 } @@ -221,8 +221,8 @@ ebb0(v0: i64): ; check: isub v3, v0 ; check: sshr_imm v4, 1 ; check: ushr_imm v5, 63 - ; check: iadd v5, v6 - ; check: copy v7 + ; check: v7 = iadd v5, v6 + ; check: v1 -> v7 return v1 } @@ -233,8 +233,8 @@ ebb0(v0: i64): ; check: iconst.i64 0x2aaa_aaaa_aaaa_aaab ; check: smulhi v0, v2 ; check: ushr_imm v3, 63 - ; check: iadd v3, v4 - ; check: copy v5 + ; check: v5 = iadd v3, v4 + ; check: v1 -> v5 return v1 } @@ -247,8 +247,8 @@ ebb0(v0: i64): ; check: iadd v3, v0 ; check: sshr_imm v4, 3 ; check: ushr_imm v5, 63 - ; check: iadd v5, v6 - ; check: copy v7 + ; check: v7 = iadd v5, v6 + ; check: v1 -> v7 return v1 } @@ -260,7 +260,7 @@ ebb0(v0: i64): ; check: smulhi v0, v2 ; check: sshr_imm v3, 7 ; check: ushr_imm v4, 63 - ; check: iadd v4, v5 - ; check: copy v6 + ; check: v6 = iadd v4, v5 + ; check: v1 -> v6 return v1 } diff --git a/cranelift/filetests/filetests/simple_preopt/div_by_const_power_of_2.clif b/cranelift/filetests/filetests/simple_preopt/div_by_const_power_of_2.clif index 7e6bf7fd14..fb9c1744f5 100644 --- a/cranelift/filetests/filetests/simple_preopt/div_by_const_power_of_2.clif +++ b/cranelift/filetests/filetests/simple_preopt/div_by_const_power_of_2.clif @@ -104,7 +104,7 @@ ebb0(v0: i32): ; check: ushr_imm v0, 31 ; check: iadd v0, v2 ; check: sshr_imm v3, 1 - ; check: copy v4 + ; check: v1 -> v4 return v1 } @@ -126,8 +126,8 @@ ebb0(v0: i32): ; check: v2 = sshr_imm v0, 1 ; check: ushr_imm v2, 30 ; check: iadd v0, v3 - ; check: sshr_imm v4, 2 - ; check: copy v5 + ; check: v5 = sshr_imm v4, 2 + ; check: v1 -> v5 return v1 } @@ -151,8 +151,8 @@ ebb0(v0: i32): ; check: sshr_imm v0, 29 ; check: ushr_imm v2, 2 ; check: iadd v0, v3 - ; check: sshr_imm v4, 30 - ; check: copy v5 + ; check: v5 = sshr_imm v4, 30 + ; check: v1 -> v5 return v1 } @@ -214,8 +214,8 @@ ebb0(v0: i64): v1 = sdiv_imm v0, 2 ; check: ushr_imm v0, 63 ; check: iadd v0, v2 - ; check: sshr_imm v3, 1 - ; check: copy v4 + ; check: v4 = sshr_imm v3, 1 + ; check: v1 -> v4 return v1 } @@ -237,8 +237,8 @@ ebb0(v0: i64): ; check: sshr_imm v0, 1 ; check: ushr_imm v2, 62 ; check: iadd v0, v3 - ; check: sshr_imm v4, 2 - ; check: copy v5 + ; check: v5 = sshr_imm v4, 2 + ; check: v1 -> v5 return v1 } @@ -261,8 +261,8 @@ ebb0(v0: i64): ; check: sshr_imm v0, 61 ; check: ushr_imm v2, 2 ; check: iadd v0, v3 - ; check: sshr_imm v4, 62 - ; check: copy v5 + ; check: v5 = sshr_imm v4, 62 + ; check: v1 -> v5 return v1 }