1 use crate::intrinsics::*;
4 use rustc_middle::ty::subst::SubstsRef;
6 pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
7 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
9 substs: SubstsRef<'tcx>,
10 args: &[mir::Operand<'tcx>],
11 destination: Option<(CPlace<'tcx>, BasicBlock)>,
13 let ret = match destination {
14 Some((place, _)) => place,
16 // Insert non returning intrinsics here
19 trap_panic(fx, "Called intrinsic::abort.");
22 trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
24 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
31 fx, intrinsic, substs, args,
33 fx.codegen_cx.tcx.sess.warn(&format!("unsupported llvm intrinsic {}; replacing with trap", intrinsic));
34 crate::trap::trap_unimplemented(fx, intrinsic);
37 // Used by `_mm_movemask_epi8` and `_mm256_movemask_epi8`
38 llvm.x86.sse2.pmovmskb.128 | llvm.x86.avx2.pmovmskb | llvm.x86.sse2.movmsk.pd, (c a) {
39 let (lane_layout, lane_count) = lane_type_and_count(fx.codegen_cx.tcx, a.layout());
40 let lane_ty = fx.clif_type(lane_layout.ty).unwrap();
41 assert!(lane_count <= 32);
43 let mut res = fx.bcx.ins().iconst(types::I32, 0);
45 for lane in (0..lane_count).rev() {
46 let a_lane = a.value_field(fx, mir::Field::new(lane.try_into().unwrap())).load_scalar(fx);
49 let a_lane = match lane_ty {
50 types::F32 => fx.bcx.ins().bitcast(types::I32, a_lane),
51 types::F64 => fx.bcx.ins().bitcast(types::I64, a_lane),
55 // extract sign bit of an int
56 let a_lane_sign = fx.bcx.ins().ushr_imm(a_lane, i64::from(lane_ty.bits() - 1));
58 // shift sign bit into result
59 let a_lane_sign = clif_intcast(fx, a_lane_sign, types::I32, false);
60 res = fx.bcx.ins().ishl_imm(res, 1);
61 res = fx.bcx.ins().bor(res, a_lane_sign);
64 let res = CValue::by_val(res, fx.layout_of(fx.codegen_cx.tcx.types.i32));
65 ret.write_cvalue(fx, res);
67 llvm.x86.sse2.cmp.ps | llvm.x86.sse2.cmp.pd, (c x, c y, o kind) {
68 let kind_const = crate::constant::mir_operand_get_const_val(fx, kind).expect("llvm.x86.sse2.cmp.* kind not const");
69 let flt_cc = match kind_const.val.try_to_bits(Size::from_bytes(1)).expect(&format!("kind not scalar: {:?}", kind_const)) {
71 1 => FloatCC::LessThan,
72 2 => FloatCC::LessThanOrEqual,
74 unimplemented!("Compares corresponding elements in `a` and `b` to see if neither is `NaN`.");
77 unimplemented!("Compares corresponding elements in `a` and `b` to see if either is `NaN`.");
79 4 => FloatCC::NotEqual,
81 unimplemented!("not less than");
84 unimplemented!("not less than or equal");
86 kind => unreachable!("kind {:?}", kind),
89 simd_pair_for_each_lane(fx, x, y, ret, |fx, lane_layout, res_lane_layout, x_lane, y_lane| {
90 let res_lane = match lane_layout.ty.kind {
91 ty::Float(_) => fx.bcx.ins().fcmp(flt_cc, x_lane, y_lane),
92 _ => unreachable!("{:?}", lane_layout.ty),
94 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
97 llvm.x86.sse2.psrli.d, (c a, o imm8) {
98 let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8).expect("llvm.x86.sse2.psrli.d imm8 not const");
99 simd_for_each_lane(fx, a, ret, |fx, _lane_layout, res_lane_layout, lane| {
100 let res_lane = match imm8.val.try_to_bits(Size::from_bytes(4)).expect(&format!("imm8 not scalar: {:?}", imm8)) {
101 imm8 if imm8 < 32 => fx.bcx.ins().ushr_imm(lane, i64::from(imm8 as u8)),
102 _ => fx.bcx.ins().iconst(types::I32, 0),
104 CValue::by_val(res_lane, res_lane_layout)
107 llvm.x86.sse2.pslli.d, (c a, o imm8) {
108 let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8).expect("llvm.x86.sse2.psrli.d imm8 not const");
109 simd_for_each_lane(fx, a, ret, |fx, _lane_layout, res_lane_layout, lane| {
110 let res_lane = match imm8.val.try_to_bits(Size::from_bytes(4)).expect(&format!("imm8 not scalar: {:?}", imm8)) {
111 imm8 if imm8 < 32 => fx.bcx.ins().ishl_imm(lane, i64::from(imm8 as u8)),
112 _ => fx.bcx.ins().iconst(types::I32, 0),
114 CValue::by_val(res_lane, res_lane_layout)
117 llvm.x86.sse2.storeu.dq, (v mem_addr, c a) {
118 // FIXME correctly handle the unalignment
119 let dest = CPlace::for_ptr(Pointer::new(mem_addr), a.layout());
120 dest.write_cvalue(fx, a);
124 if let Some((_, dest)) = destination {
125 let ret_block = fx.get_block(dest);
126 fx.bcx.ins().jump(ret_block, &[]);
128 trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
132 // llvm.x86.avx2.vperm2i128
133 // llvm.x86.ssse3.pshuf.b.128
134 // llvm.x86.avx2.pshuf.b
135 // llvm.x86.avx2.psrli.w
136 // llvm.x86.sse2.psrli.w