Skip to content

Commit 9c98027

Browse files
committed
Directly use Cranelift instructions for 128bit int shifts
1 parent c5f98b5 commit 9c98027

File tree

2 files changed

+9
-103
lines changed

2 files changed

+9
-103
lines changed

src/codegen_i128.rs

+9-56
Original file line numberDiff line numberDiff line change
@@ -97,70 +97,23 @@ pub(crate) fn maybe_codegen<'tcx>(
9797
None
9898
};
9999

100-
// Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
101-
// integer into its lsb and msb.
102-
// https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
103-
if resolve_value_imm(fx.bcx.func, rhs_val) == Some(64) {
104-
let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
105-
let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
106-
let val = match (bin_op, is_signed) {
107-
(BinOp::Shr, false) => {
108-
let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
109-
Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
110-
}
111-
(BinOp::Shr, true) => {
112-
let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
113-
let all_ones = fx.bcx.ins().iconst(types::I64, u64::MAX as i64);
114-
let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
115-
116-
let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
117-
Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
118-
}
119-
(BinOp::Shl, _) => {
120-
let val_ty = if is_signed {
121-
fx.tcx.types.i128
122-
} else {
123-
fx.tcx.types.u128
124-
};
125-
let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
126-
Some(CValue::by_val(val, fx.layout_of(val_ty)))
127-
}
128-
_ => None,
129-
};
130-
if let Some(val) = val {
131-
if let Some(is_overflow) = is_overflow {
132-
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
133-
let val = val.load_scalar(fx);
134-
return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)));
100+
let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
101+
let val = match bin_op {
102+
BinOp::Shl => fx.bcx.ins().ishl(lhs_val, truncated_rhs),
103+
BinOp::Shr => {
104+
if is_signed {
105+
fx.bcx.ins().sshr(lhs_val, truncated_rhs)
135106
} else {
136-
return Some(val);
107+
fx.bcx.ins().ushr(lhs_val, truncated_rhs)
137108
}
138109
}
139-
}
140-
141-
let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
142-
let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
143-
let val = match (bin_op, is_signed) {
144-
(BinOp::Shl, false) => {
145-
fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
146-
}
147-
(BinOp::Shl, true) => {
148-
fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
149-
}
150-
(BinOp::Shr, false) => {
151-
fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
152-
}
153-
(BinOp::Shr, true) => {
154-
fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
155-
}
156-
(_, _) => unreachable!(),
110+
_ => unreachable!(),
157111
};
158112
if let Some(is_overflow) = is_overflow {
159113
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
160-
let val = val.load_scalar(fx);
161114
Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
162115
} else {
163-
Some(val)
116+
Some(CValue::by_val(val, lhs.layout()))
164117
}
165118
}
166119
}

src/common.rs

-47
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use rustc_target::abi::call::FnAbi;
33
use rustc_target::abi::{Integer, Primitive};
44
use rustc_target::spec::{HasTargetSpec, Target};
55

6-
use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
7-
86
use crate::prelude::*;
97

108
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
@@ -175,51 +173,6 @@ pub(crate) fn codegen_icmp_imm(
175173
}
176174
}
177175

178-
fn resolve_normal_value_imm(func: &Function, val: Value) -> Option<i64> {
179-
if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
180-
if let InstructionData::UnaryImm {
181-
opcode: Opcode::Iconst,
182-
imm,
183-
} = func.dfg[inst]
184-
{
185-
Some(imm.into())
186-
} else {
187-
None
188-
}
189-
} else {
190-
None
191-
}
192-
}
193-
194-
fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option<u128> {
195-
let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
196-
if let InstructionData::Binary {
197-
opcode: Opcode::Iconcat,
198-
args: [lsb, msb],
199-
} = func.dfg[inst]
200-
{
201-
(lsb, msb)
202-
} else {
203-
return None;
204-
}
205-
} else {
206-
return None;
207-
};
208-
209-
let lsb = u128::from(resolve_normal_value_imm(func, lsb)? as u64);
210-
let msb = u128::from(resolve_normal_value_imm(func, msb)? as u64);
211-
212-
Some(msb << 64 | lsb)
213-
}
214-
215-
pub(crate) fn resolve_value_imm(func: &Function, val: Value) -> Option<u128> {
216-
if func.dfg.value_type(val) == types::I128 {
217-
resolve_128bit_value_imm(func, val)
218-
} else {
219-
resolve_normal_value_imm(func, val).map(|imm| u128::from(imm as u64))
220-
}
221-
}
222-
223176
pub(crate) fn type_min_max_value(
224177
bcx: &mut FunctionBuilder<'_>,
225178
ty: Type,

0 commit comments

Comments
 (0)