]> git.lizzy.rs Git - rust.git/blob - src/intrinsics/llvm.rs
FunctionCx: WIP: Fix syntax error on sed codegen_cx -> .codegen_cx
[rust.git] / src / intrinsics / llvm.rs
1 use crate::intrinsics::*;
2 use crate::prelude::*;
3
4 use rustc_middle::ty::subst::SubstsRef;
5
6 pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
7     fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
8     intrinsic: &str,
9     substs: SubstsRef<'tcx>,
10     args: &[mir::Operand<'tcx>],
11     destination: Option<(CPlace<'tcx>, BasicBlock)>,
12 ) {
13     let ret = match destination {
14         Some((place, _)) => place,
15         None => {
16             // Insert non returning intrinsics here
17             match intrinsic {
18                 "abort" => {
19                     trap_panic(fx, "Called intrinsic::abort.");
20                 }
21                 "unreachable" => {
22                     trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
23                 }
24                 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
25             }
26             return;
27         }
28     };
29
30     intrinsic_match! {
31         fx, intrinsic, substs, args,
32         _ => {
33             fx.codegen_cx.tcx.sess.warn(&format!("unsupported llvm intrinsic {}; replacing with trap", intrinsic));
34             crate::trap::trap_unimplemented(fx, intrinsic);
35         };
36
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);
42
43             let mut res = fx.bcx.ins().iconst(types::I32, 0);
44
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);
47
48                 // cast float to int
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),
52                     _ => a_lane,
53                 };
54
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));
57
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);
62             }
63
64             let res = CValue::by_val(res, fx.layout_of(fx.codegen_cx.tcx.types.i32));
65             ret.write_cvalue(fx, res);
66         };
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)) {
70                 0 => FloatCC::Equal,
71                 1 => FloatCC::LessThan,
72                 2 => FloatCC::LessThanOrEqual,
73                 7 => {
74                     unimplemented!("Compares corresponding elements in `a` and `b` to see if neither is `NaN`.");
75                 }
76                 3 => {
77                     unimplemented!("Compares corresponding elements in `a` and `b` to see if either is `NaN`.");
78                 }
79                 4 => FloatCC::NotEqual,
80                 5 => {
81                     unimplemented!("not less than");
82                 }
83                 6 => {
84                     unimplemented!("not less than or equal");
85                 }
86                 kind => unreachable!("kind {:?}", kind),
87             };
88
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),
93                 };
94                 bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
95             });
96         };
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),
103                 };
104                 CValue::by_val(res_lane, res_lane_layout)
105             });
106         };
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),
113                 };
114                 CValue::by_val(res_lane, res_lane_layout)
115             });
116         };
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);
121         };
122     }
123
124     if let Some((_, dest)) = destination {
125         let ret_block = fx.get_block(dest);
126         fx.bcx.ins().jump(ret_block, &[]);
127     } else {
128         trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
129     }
130 }
131
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