From 9c980276d81b727bb8efcbe63f51866ba19793a0 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 12:09:13 +0100 Subject: [PATCH] Directly use Cranelift instructions for 128bit int shifts --- src/codegen_i128.rs | 67 +++++++-------------------------------------- src/common.rs | 47 ------------------------------- 2 files changed, 10 insertions(+), 104 deletions(-) diff --git a/src/codegen_i128.rs b/src/codegen_i128.rs index 866ba90e4ae..7b674df446a 100644 --- a/src/codegen_i128.rs +++ b/src/codegen_i128.rs @@ -97,70 +97,23 @@ pub(crate) fn maybe_codegen<'tcx>( None }; - // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit - // integer into its lsb and msb. - // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217 - if resolve_value_imm(fx.bcx.func, rhs_val) == Some(64) { - let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val); - let all_zeros = fx.bcx.ins().iconst(types::I64, 0); - let val = match (bin_op, is_signed) { - (BinOp::Shr, false) => { - let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros); - Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128))) - } - (BinOp::Shr, true) => { - let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0); - let all_ones = fx.bcx.ins().iconst(types::I64, u64::MAX as i64); - let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones); - - let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits); - Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128))) - } - (BinOp::Shl, _) => { - let val_ty = if is_signed { - fx.tcx.types.i128 - } else { - fx.tcx.types.u128 - }; - let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb); - Some(CValue::by_val(val, fx.layout_of(val_ty))) - } - _ => None, - }; - if let Some(val) = val { - if let Some(is_overflow) = is_overflow { - let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - let val = val.load_scalar(fx); - return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty))); - } else { - return Some(val); - } - } - } - let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false); - let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32)); - let val = match (bin_op, is_signed) { - (BinOp::Shl, false) => { - fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128) + let val = match bin_op { + BinOp::Shl => fx.bcx.ins().ishl(lhs_val, truncated_rhs), + BinOp::Shr => { + if is_signed { + fx.bcx.ins().sshr(lhs_val, truncated_rhs) + } else { + fx.bcx.ins().ushr(lhs_val, truncated_rhs) + } } - (BinOp::Shl, true) => { - fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128) - } - (BinOp::Shr, false) => { - fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128) - } - (BinOp::Shr, true) => { - fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128) - } - (_, _) => unreachable!(), + _ => unreachable!(), }; if let Some(is_overflow) = is_overflow { let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - let val = val.load_scalar(fx); Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty))) } else { - Some(val) + Some(CValue::by_val(val, lhs.layout())) } } } diff --git a/src/common.rs b/src/common.rs index fbee84e09f7..1443aedb3b8 100644 --- a/src/common.rs +++ b/src/common.rs @@ -3,8 +3,6 @@ use rustc_target::abi::call::FnAbi; use rustc_target::abi::{Integer, Primitive}; use rustc_target::spec::{HasTargetSpec, Target}; -use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef}; - use crate::prelude::*; pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type { @@ -175,51 +173,6 @@ pub(crate) fn codegen_icmp_imm( } } -fn resolve_normal_value_imm(func: &Function, val: Value) -> Option { - if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) { - if let InstructionData::UnaryImm { - opcode: Opcode::Iconst, - imm, - } = func.dfg[inst] - { - Some(imm.into()) - } else { - None - } - } else { - None - } -} - -fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option { - let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) { - if let InstructionData::Binary { - opcode: Opcode::Iconcat, - args: [lsb, msb], - } = func.dfg[inst] - { - (lsb, msb) - } else { - return None; - } - } else { - return None; - }; - - let lsb = u128::from(resolve_normal_value_imm(func, lsb)? as u64); - let msb = u128::from(resolve_normal_value_imm(func, msb)? as u64); - - Some(msb << 64 | lsb) -} - -pub(crate) fn resolve_value_imm(func: &Function, val: Value) -> Option { - if func.dfg.value_type(val) == types::I128 { - resolve_128bit_value_imm(func, val) - } else { - resolve_normal_value_imm(func, val).map(|imm| u128::from(imm as u64)) - } -} - pub(crate) fn type_min_max_value( bcx: &mut FunctionBuilder<'_>, ty: Type,