Skip to content

Commit 2e62516

Browse files
committed
Remove workarounds for things unimplemented in Cranelift
Many are now implemented, so it is much nicer to directly use the respective Cranelift instructions
1 parent 8a6ff90 commit 2e62516

File tree

7 files changed

+5
-113
lines changed

7 files changed

+5
-113
lines changed

src/base.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,6 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
334334
crate::optimize::peephole::maybe_unwrap_bool_not(&mut fx.bcx, discr);
335335
let test_zero = if is_inverted { !test_zero } else { test_zero };
336336
let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
337-
let discr =
338-
crate::optimize::peephole::make_branchable_value(&mut fx.bcx, discr);
339337
if let Some(taken) = crate::optimize::peephole::maybe_known_branch_taken(
340338
&fx.bcx, discr, test_zero,
341339
) {

src/cast.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ pub(crate) fn clif_intcast(
1414
(_, _) if from == to => val,
1515

1616
// extend
17-
(_, types::I128) => {
18-
let lo = if from == types::I64 {
19-
val
20-
} else if signed {
21-
fx.bcx.ins().sextend(types::I64, val)
22-
} else {
23-
fx.bcx.ins().uextend(types::I64, val)
24-
};
25-
let hi = if signed {
26-
fx.bcx.ins().sshr_imm(lo, 63)
27-
} else {
28-
fx.bcx.ins().iconst(types::I64, 0)
29-
};
30-
fx.bcx.ins().iconcat(lo, hi)
31-
}
3217
(_, _) if to.wider_or_equal(from) => {
3318
if signed {
3419
fx.bcx.ins().sextend(to, val)
@@ -38,10 +23,6 @@ pub(crate) fn clif_intcast(
3823
}
3924

4025
// reduce
41-
(types::I128, _) => {
42-
let (lsb, _msb) = fx.bcx.ins().isplit(val);
43-
if to == types::I64 { lsb } else { fx.bcx.ins().ireduce(to, lsb) }
44-
}
4526
(_, _) => fx.bcx.ins().ireduce(to, val),
4627
}
4728
}

src/debuginfo/emit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl Writer for WriterRelocate {
160160
let val = match eh_pe.application() {
161161
gimli::DW_EH_PE_absptr => val,
162162
gimli::DW_EH_PE_pcrel => {
163-
// TODO: better handling of sign
163+
// FIXME better handling of sign
164164
let offset = self.len() as u64;
165165
offset.wrapping_sub(val)
166166
}

src/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> DebugContext<'tcx> {
4646
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
4747
let encoding = Encoding {
4848
format: Format::Dwarf32,
49-
// TODO: this should be configurable
49+
// FIXME this should be configurable
5050
// macOS doesn't seem to support DWARF > 3
5151
// 5 version is required for md5 file hash
5252
version: if tcx.sess.target.is_like_osx {

src/intrinsics/mod.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,6 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
611611
let (val, has_overflow) = checked_res.load_scalar_pair(fx);
612612
let clif_ty = fx.clif_type(T).unwrap();
613613

614-
// `select.i8` is not implemented by Cranelift.
615-
let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
616-
617614
let (min, max) = type_min_max_value(&mut fx.bcx, clif_ty, signed);
618615

619616
let val = match (intrinsic, signed) {
@@ -640,21 +637,11 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
640637
};
641638
rotate_left, <T>(v x, v y) {
642639
let layout = fx.layout_of(T);
643-
let y = if fx.bcx.func.dfg.value_type(y) == types::I128 {
644-
fx.bcx.ins().ireduce(types::I64, y)
645-
} else {
646-
y
647-
};
648640
let res = fx.bcx.ins().rotl(x, y);
649641
ret.write_cvalue(fx, CValue::by_val(res, layout));
650642
};
651643
rotate_right, <T>(v x, v y) {
652644
let layout = fx.layout_of(T);
653-
let y = if fx.bcx.func.dfg.value_type(y) == types::I128 {
654-
fx.bcx.ins().ireduce(types::I64, y)
655-
} else {
656-
y
657-
};
658645
let res = fx.bcx.ins().rotr(x, y);
659646
ret.write_cvalue(fx, CValue::by_val(res, layout));
660647
};
@@ -692,35 +679,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
692679
};
693680
ctlz | ctlz_nonzero, <T> (v arg) {
694681
// FIXME trap on `ctlz_nonzero` with zero arg.
695-
let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
696-
// FIXME verify this algorithm is correct
697-
let (lsb, msb) = fx.bcx.ins().isplit(arg);
698-
let lsb_lz = fx.bcx.ins().clz(lsb);
699-
let msb_lz = fx.bcx.ins().clz(msb);
700-
let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
701-
let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
702-
let res = fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz);
703-
fx.bcx.ins().uextend(types::I128, res)
704-
} else {
705-
fx.bcx.ins().clz(arg)
706-
};
682+
let res = fx.bcx.ins().clz(arg);
707683
let res = CValue::by_val(res, fx.layout_of(T));
708684
ret.write_cvalue(fx, res);
709685
};
710686
cttz | cttz_nonzero, <T> (v arg) {
711687
// FIXME trap on `cttz_nonzero` with zero arg.
712-
let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
713-
// FIXME verify this algorithm is correct
714-
let (lsb, msb) = fx.bcx.ins().isplit(arg);
715-
let lsb_tz = fx.bcx.ins().ctz(lsb);
716-
let msb_tz = fx.bcx.ins().ctz(msb);
717-
let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
718-
let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
719-
let res = fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz);
720-
fx.bcx.ins().uextend(types::I128, res)
721-
} else {
722-
fx.bcx.ins().ctz(arg)
723-
};
688+
let res = fx.bcx.ins().ctz(arg);
724689
let res = CValue::by_val(res, fx.layout_of(T));
725690
ret.write_cvalue(fx, res);
726691
};

src/num.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,6 @@ pub(crate) fn codegen_binop<'tcx>(
6767
let lhs = in_lhs.load_scalar(fx);
6868
let rhs = in_rhs.load_scalar(fx);
6969

70-
let (lhs, rhs) = if (bin_op == BinOp::Eq || bin_op == BinOp::Ne)
71-
&& (in_lhs.layout().ty.kind() == fx.tcx.types.i8.kind()
72-
|| in_lhs.layout().ty.kind() == fx.tcx.types.i16.kind())
73-
{
74-
// FIXME(CraneStation/cranelift#896) icmp_imm.i8/i16 with eq/ne for signed ints is implemented wrong.
75-
(
76-
fx.bcx.ins().sextend(types::I32, lhs),
77-
fx.bcx.ins().sextend(types::I32, rhs),
78-
)
79-
} else {
80-
(lhs, rhs)
81-
};
82-
8370
return codegen_compare_bin_op(fx, bin_op, signed, lhs, rhs);
8471
}
8572
_ => {}

src/optimize/peephole.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Peephole optimizations that can be performed while creating clif ir.
22
3-
use cranelift_codegen::ir::{
4-
condcodes::IntCC, types, InstBuilder, InstructionData, Opcode, Value, ValueDef,
5-
};
3+
use cranelift_codegen::ir::{condcodes::IntCC, InstructionData, Opcode, Value, ValueDef};
64
use cranelift_frontend::FunctionBuilder;
75

86
/// If the given value was produced by a `bint` instruction, return it's input, otherwise return the
@@ -37,43 +35,6 @@ pub(crate) fn maybe_unwrap_bool_not(bcx: &mut FunctionBuilder<'_>, arg: Value) -
3735
}
3836
}
3937

40-
pub(crate) fn make_branchable_value(bcx: &mut FunctionBuilder<'_>, arg: Value) -> Value {
41-
if bcx.func.dfg.value_type(arg).is_bool() {
42-
return arg;
43-
}
44-
45-
(|| {
46-
let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
47-
arg_inst
48-
} else {
49-
return None;
50-
};
51-
52-
match bcx.func.dfg[arg_inst] {
53-
// This is the lowering of Rvalue::Not
54-
InstructionData::Load { opcode: Opcode::Load, arg: ptr, flags, offset } => {
55-
// Using `load.i8 + uextend.i32` would legalize to `uload8 + ireduce.i8 +
56-
// uextend.i32`. Just `uload8` is much faster.
57-
match bcx.func.dfg.ctrl_typevar(arg_inst) {
58-
types::I8 => Some(bcx.ins().uload8(types::I32, flags, ptr, offset)),
59-
types::I16 => Some(bcx.ins().uload16(types::I32, flags, ptr, offset)),
60-
_ => None,
61-
}
62-
}
63-
_ => None,
64-
}
65-
})()
66-
.unwrap_or_else(|| {
67-
match bcx.func.dfg.value_type(arg) {
68-
types::I8 | types::I16 => {
69-
// WORKAROUND for brz.i8 and brnz.i8 not yet being implemented
70-
bcx.ins().uextend(types::I32, arg)
71-
}
72-
_ => arg,
73-
}
74-
})
75-
}
76-
7738
/// Returns whether the branch is statically known to be taken or `None` if it isn't statically known.
7839
pub(crate) fn maybe_known_branch_taken(
7940
bcx: &FunctionBuilder<'_>,

0 commit comments

Comments
 (0)