]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/abi.rs
Auto merge of #101250 - klensy:bump-deps-08-22, r=Dylan-DPC
[rust.git] / compiler / rustc_codegen_llvm / src / abi.rs
1 use crate::attributes;
2 use crate::builder::Builder;
3 use crate::context::CodegenCx;
4 use crate::llvm::{self, Attribute, AttributePlace};
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::value::Value;
8
9 use rustc_codegen_ssa::mir::operand::OperandValue;
10 use rustc_codegen_ssa::mir::place::PlaceRef;
11 use rustc_codegen_ssa::traits::*;
12 use rustc_codegen_ssa::MemFlags;
13 use rustc_middle::bug;
14 use rustc_middle::ty::layout::LayoutOf;
15 pub use rustc_middle::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
16 use rustc_middle::ty::Ty;
17 use rustc_session::config;
18 use rustc_target::abi::call::ArgAbi;
19 pub use rustc_target::abi::call::*;
20 use rustc_target::abi::{self, HasDataLayout, Int};
21 pub use rustc_target::spec::abi::Abi;
22
23 use libc::c_uint;
24 use smallvec::SmallVec;
25
26 pub trait ArgAttributesExt {
27     fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value);
28     fn apply_attrs_to_callsite(
29         &self,
30         idx: AttributePlace,
31         cx: &CodegenCx<'_, '_>,
32         callsite: &Value,
33     );
34 }
35
36 fn should_use_mutable_noalias(cx: &CodegenCx<'_, '_>) -> bool {
37     // LLVM prior to version 12 had known miscompiles in the presence of
38     // noalias attributes (see #54878), but we don't support earlier
39     // versions at all anymore. We now enable mutable noalias by default.
40     cx.tcx.sess.opts.unstable_opts.mutable_noalias.unwrap_or(true)
41 }
42
43 const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] =
44     [(ArgAttribute::InReg, llvm::AttributeKind::InReg)];
45
46 const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 5] = [
47     (ArgAttribute::NoAlias, llvm::AttributeKind::NoAlias),
48     (ArgAttribute::NoCapture, llvm::AttributeKind::NoCapture),
49     (ArgAttribute::NonNull, llvm::AttributeKind::NonNull),
50     (ArgAttribute::ReadOnly, llvm::AttributeKind::ReadOnly),
51     (ArgAttribute::NoUndef, llvm::AttributeKind::NoUndef),
52 ];
53
54 fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'ll Attribute; 8]> {
55     let mut regular = this.regular;
56
57     let mut attrs = SmallVec::new();
58
59     // ABI-affecting attributes must always be applied
60     for (attr, llattr) in ABI_AFFECTING_ATTRIBUTES {
61         if regular.contains(attr) {
62             attrs.push(llattr.create_attr(cx.llcx));
63         }
64     }
65     if let Some(align) = this.pointee_align {
66         attrs.push(llvm::CreateAlignmentAttr(cx.llcx, align.bytes()));
67     }
68     match this.arg_ext {
69         ArgExtension::None => {}
70         ArgExtension::Zext => attrs.push(llvm::AttributeKind::ZExt.create_attr(cx.llcx)),
71         ArgExtension::Sext => attrs.push(llvm::AttributeKind::SExt.create_attr(cx.llcx)),
72     }
73
74     // Only apply remaining attributes when optimizing
75     if cx.sess().opts.optimize != config::OptLevel::No {
76         let deref = this.pointee_size.bytes();
77         if deref != 0 {
78             if regular.contains(ArgAttribute::NonNull) {
79                 attrs.push(llvm::CreateDereferenceableAttr(cx.llcx, deref));
80             } else {
81                 attrs.push(llvm::CreateDereferenceableOrNullAttr(cx.llcx, deref));
82             }
83             regular -= ArgAttribute::NonNull;
84         }
85         for (attr, llattr) in OPTIMIZATION_ATTRIBUTES {
86             if regular.contains(attr) {
87                 attrs.push(llattr.create_attr(cx.llcx));
88             }
89         }
90         if regular.contains(ArgAttribute::NoAliasMutRef) && should_use_mutable_noalias(cx) {
91             attrs.push(llvm::AttributeKind::NoAlias.create_attr(cx.llcx));
92         }
93     }
94
95     attrs
96 }
97
98 impl ArgAttributesExt for ArgAttributes {
99     fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value) {
100         let attrs = get_attrs(self, cx);
101         attributes::apply_to_llfn(llfn, idx, &attrs);
102     }
103
104     fn apply_attrs_to_callsite(
105         &self,
106         idx: AttributePlace,
107         cx: &CodegenCx<'_, '_>,
108         callsite: &Value,
109     ) {
110         let attrs = get_attrs(self, cx);
111         attributes::apply_to_callsite(callsite, idx, &attrs);
112     }
113 }
114
115 pub trait LlvmType {
116     fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type;
117 }
118
119 impl LlvmType for Reg {
120     fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
121         match self.kind {
122             RegKind::Integer => cx.type_ix(self.size.bits()),
123             RegKind::Float => match self.size.bits() {
124                 32 => cx.type_f32(),
125                 64 => cx.type_f64(),
126                 _ => bug!("unsupported float: {:?}", self),
127             },
128             RegKind::Vector => cx.type_vector(cx.type_i8(), self.size.bytes()),
129         }
130     }
131 }
132
133 impl LlvmType for CastTarget {
134     fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
135         let rest_ll_unit = self.rest.unit.llvm_type(cx);
136         let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
137             (0, 0)
138         } else {
139             (
140                 self.rest.total.bytes() / self.rest.unit.size.bytes(),
141                 self.rest.total.bytes() % self.rest.unit.size.bytes(),
142             )
143         };
144
145         if self.prefix.iter().all(|x| x.is_none()) {
146             // Simplify to a single unit when there is no prefix and size <= unit size
147             if self.rest.total <= self.rest.unit.size {
148                 return rest_ll_unit;
149             }
150
151             // Simplify to array when all chunks are the same size and type
152             if rem_bytes == 0 {
153                 return cx.type_array(rest_ll_unit, rest_count);
154             }
155         }
156
157         // Create list of fields in the main structure
158         let mut args: Vec<_> = self
159             .prefix
160             .iter()
161             .flat_map(|option_reg| option_reg.map(|reg| reg.llvm_type(cx)))
162             .chain((0..rest_count).map(|_| rest_ll_unit))
163             .collect();
164
165         // Append final integer
166         if rem_bytes != 0 {
167             // Only integers can be really split further.
168             assert_eq!(self.rest.unit.kind, RegKind::Integer);
169             args.push(cx.type_ix(rem_bytes * 8));
170         }
171
172         cx.type_struct(&args, false)
173     }
174 }
175
176 pub trait ArgAbiExt<'ll, 'tcx> {
177     fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
178     fn store(
179         &self,
180         bx: &mut Builder<'_, 'll, 'tcx>,
181         val: &'ll Value,
182         dst: PlaceRef<'tcx, &'ll Value>,
183     );
184     fn store_fn_arg(
185         &self,
186         bx: &mut Builder<'_, 'll, 'tcx>,
187         idx: &mut usize,
188         dst: PlaceRef<'tcx, &'ll Value>,
189     );
190 }
191
192 impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
193     /// Gets the LLVM type for a place of the original Rust type of
194     /// this argument/return, i.e., the result of `type_of::type_of`.
195     fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
196         self.layout.llvm_type(cx)
197     }
198
199     /// Stores a direct/indirect value described by this ArgAbi into a
200     /// place for the original Rust type of this argument/return.
201     /// Can be used for both storing formal arguments into Rust variables
202     /// or results of call/invoke instructions into their destinations.
203     fn store(
204         &self,
205         bx: &mut Builder<'_, 'll, 'tcx>,
206         val: &'ll Value,
207         dst: PlaceRef<'tcx, &'ll Value>,
208     ) {
209         if self.is_ignore() {
210             return;
211         }
212         if self.is_sized_indirect() {
213             OperandValue::Ref(val, None, self.layout.align.abi).store(bx, dst)
214         } else if self.is_unsized_indirect() {
215             bug!("unsized `ArgAbi` must be handled through `store_fn_arg`");
216         } else if let PassMode::Cast(cast, _) = &self.mode {
217             // FIXME(eddyb): Figure out when the simpler Store is safe, clang
218             // uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
219             let can_store_through_cast_ptr = false;
220             if can_store_through_cast_ptr {
221                 let cast_ptr_llty = bx.type_ptr_to(cast.llvm_type(bx));
222                 let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
223                 bx.store(val, cast_dst, self.layout.align.abi);
224             } else {
225                 // The actual return type is a struct, but the ABI
226                 // adaptation code has cast it into some scalar type.  The
227                 // code that follows is the only reliable way I have
228                 // found to do a transform like i64 -> {i32,i32}.
229                 // Basically we dump the data onto the stack then memcpy it.
230                 //
231                 // Other approaches I tried:
232                 // - Casting rust ret pointer to the foreign type and using Store
233                 //   is (a) unsafe if size of foreign type > size of rust type and
234                 //   (b) runs afoul of strict aliasing rules, yielding invalid
235                 //   assembly under -O (specifically, the store gets removed).
236                 // - Truncating foreign type to correct integral type and then
237                 //   bitcasting to the struct type yields invalid cast errors.
238
239                 // We instead thus allocate some scratch space...
240                 let scratch_size = cast.size(bx);
241                 let scratch_align = cast.align(bx);
242                 let llscratch = bx.alloca(cast.llvm_type(bx), scratch_align);
243                 bx.lifetime_start(llscratch, scratch_size);
244
245                 // ... where we first store the value...
246                 bx.store(val, llscratch, scratch_align);
247
248                 // ... and then memcpy it to the intended destination.
249                 bx.memcpy(
250                     dst.llval,
251                     self.layout.align.abi,
252                     llscratch,
253                     scratch_align,
254                     bx.const_usize(self.layout.size.bytes()),
255                     MemFlags::empty(),
256                 );
257
258                 bx.lifetime_end(llscratch, scratch_size);
259             }
260         } else {
261             OperandValue::Immediate(val).store(bx, dst);
262         }
263     }
264
265     fn store_fn_arg(
266         &self,
267         bx: &mut Builder<'_, 'll, 'tcx>,
268         idx: &mut usize,
269         dst: PlaceRef<'tcx, &'ll Value>,
270     ) {
271         let mut next = || {
272             let val = llvm::get_param(bx.llfn(), *idx as c_uint);
273             *idx += 1;
274             val
275         };
276         match self.mode {
277             PassMode::Ignore => {}
278             PassMode::Pair(..) => {
279                 OperandValue::Pair(next(), next()).store(bx, dst);
280             }
281             PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
282                 OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
283             }
284             PassMode::Direct(_)
285             | PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ }
286             | PassMode::Cast(..) => {
287                 let next_arg = next();
288                 self.store(bx, next_arg, dst);
289             }
290         }
291     }
292 }
293
294 impl<'ll, 'tcx> ArgAbiMethods<'tcx> for Builder<'_, 'll, 'tcx> {
295     fn store_fn_arg(
296         &mut self,
297         arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
298         idx: &mut usize,
299         dst: PlaceRef<'tcx, Self::Value>,
300     ) {
301         arg_abi.store_fn_arg(self, idx, dst)
302     }
303     fn store_arg(
304         &mut self,
305         arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
306         val: &'ll Value,
307         dst: PlaceRef<'tcx, &'ll Value>,
308     ) {
309         arg_abi.store(self, val, dst)
310     }
311     fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
312         arg_abi.memory_ty(self)
313     }
314 }
315
316 pub trait FnAbiLlvmExt<'ll, 'tcx> {
317     fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
318     fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
319     fn llvm_cconv(&self) -> llvm::CallConv;
320     fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value);
321     fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
322 }
323
324 impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
325     fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
326         // Ignore "extra" args from the call site for C variadic functions.
327         // Only the "fixed" args are part of the LLVM function signature.
328         let args =
329             if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args };
330
331         // This capacity calculation is approximate.
332         let mut llargument_tys = Vec::with_capacity(
333             self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
334         );
335
336         let llreturn_ty = match &self.ret.mode {
337             PassMode::Ignore => cx.type_void(),
338             PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
339             PassMode::Cast(cast, _) => cast.llvm_type(cx),
340             PassMode::Indirect { .. } => {
341                 llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
342                 cx.type_void()
343             }
344         };
345
346         for arg in args {
347             let llarg_ty = match &arg.mode {
348                 PassMode::Ignore => continue,
349                 PassMode::Direct(_) => arg.layout.immediate_llvm_type(cx),
350                 PassMode::Pair(..) => {
351                     llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 0, true));
352                     llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1, true));
353                     continue;
354                 }
355                 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
356                     let ptr_ty = cx.tcx.mk_mut_ptr(arg.layout.ty);
357                     let ptr_layout = cx.layout_of(ptr_ty);
358                     llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 0, true));
359                     llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
360                     continue;
361                 }
362                 PassMode::Cast(cast, pad_i32) => {
363                     // add padding
364                     if *pad_i32 {
365                         llargument_tys.push(Reg::i32().llvm_type(cx));
366                     }
367                     cast.llvm_type(cx)
368                 }
369                 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
370                     cx.type_ptr_to(arg.memory_ty(cx))
371                 }
372             };
373             llargument_tys.push(llarg_ty);
374         }
375
376         if self.c_variadic {
377             cx.type_variadic_func(&llargument_tys, llreturn_ty)
378         } else {
379             cx.type_func(&llargument_tys, llreturn_ty)
380         }
381     }
382
383     fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
384         unsafe {
385             llvm::LLVMPointerType(
386                 self.llvm_type(cx),
387                 cx.data_layout().instruction_address_space.0 as c_uint,
388             )
389         }
390     }
391
392     fn llvm_cconv(&self) -> llvm::CallConv {
393         match self.conv {
394             Conv::C | Conv::Rust | Conv::CCmseNonSecureCall => llvm::CCallConv,
395             Conv::RustCold => llvm::ColdCallConv,
396             Conv::AmdGpuKernel => llvm::AmdGpuKernel,
397             Conv::AvrInterrupt => llvm::AvrInterrupt,
398             Conv::AvrNonBlockingInterrupt => llvm::AvrNonBlockingInterrupt,
399             Conv::ArmAapcs => llvm::ArmAapcsCallConv,
400             Conv::Msp430Intr => llvm::Msp430Intr,
401             Conv::PtxKernel => llvm::PtxKernel,
402             Conv::X86Fastcall => llvm::X86FastcallCallConv,
403             Conv::X86Intr => llvm::X86_Intr,
404             Conv::X86Stdcall => llvm::X86StdcallCallConv,
405             Conv::X86ThisCall => llvm::X86_ThisCall,
406             Conv::X86VectorCall => llvm::X86_VectorCall,
407             Conv::X86_64SysV => llvm::X86_64_SysV,
408             Conv::X86_64Win64 => llvm::X86_64_Win64,
409         }
410     }
411
412     fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value) {
413         let mut func_attrs = SmallVec::<[_; 2]>::new();
414         if self.ret.layout.abi.is_uninhabited() {
415             func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(cx.llcx));
416         }
417         if !self.can_unwind {
418             func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(cx.llcx));
419         }
420         attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &{ func_attrs });
421
422         let mut i = 0;
423         let mut apply = |attrs: &ArgAttributes| {
424             attrs.apply_attrs_to_llfn(llvm::AttributePlace::Argument(i), cx, llfn);
425             i += 1;
426             i - 1
427         };
428         match &self.ret.mode {
429             PassMode::Direct(attrs) => {
430                 attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
431             }
432             PassMode::Indirect { attrs, extra_attrs: _, on_stack } => {
433                 assert!(!on_stack);
434                 let i = apply(attrs);
435                 let sret = llvm::CreateStructRetAttr(cx.llcx, self.ret.layout.llvm_type(cx));
436                 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
437             }
438             PassMode::Cast(cast, _) => {
439                 cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
440             }
441             _ => {}
442         }
443         for arg in self.args.iter() {
444             match &arg.mode {
445                 PassMode::Ignore => {}
446                 PassMode::Indirect { attrs, extra_attrs: None, on_stack: true } => {
447                     let i = apply(attrs);
448                     let byval = llvm::CreateByValAttr(cx.llcx, arg.layout.llvm_type(cx));
449                     attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[byval]);
450                 }
451                 PassMode::Direct(attrs)
452                 | PassMode::Indirect { attrs, extra_attrs: None, on_stack: false } => {
453                     apply(attrs);
454                 }
455                 PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack } => {
456                     assert!(!on_stack);
457                     apply(attrs);
458                     apply(extra_attrs);
459                 }
460                 PassMode::Pair(a, b) => {
461                     apply(a);
462                     apply(b);
463                 }
464                 PassMode::Cast(cast, pad_i32) => {
465                     if *pad_i32 {
466                         apply(&ArgAttributes::new());
467                     }
468                     apply(&cast.attrs);
469                 }
470             }
471         }
472     }
473
474     fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
475         let mut func_attrs = SmallVec::<[_; 2]>::new();
476         if self.ret.layout.abi.is_uninhabited() {
477             func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));
478         }
479         if !self.can_unwind {
480             func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(bx.cx.llcx));
481         }
482         attributes::apply_to_callsite(callsite, llvm::AttributePlace::Function, &{ func_attrs });
483
484         let mut i = 0;
485         let mut apply = |cx: &CodegenCx<'_, '_>, attrs: &ArgAttributes| {
486             attrs.apply_attrs_to_callsite(llvm::AttributePlace::Argument(i), cx, callsite);
487             i += 1;
488             i - 1
489         };
490         match &self.ret.mode {
491             PassMode::Direct(attrs) => {
492                 attrs.apply_attrs_to_callsite(llvm::AttributePlace::ReturnValue, bx.cx, callsite);
493             }
494             PassMode::Indirect { attrs, extra_attrs: _, on_stack } => {
495                 assert!(!on_stack);
496                 let i = apply(bx.cx, attrs);
497                 let sret = llvm::CreateStructRetAttr(bx.cx.llcx, self.ret.layout.llvm_type(bx));
498                 attributes::apply_to_callsite(callsite, llvm::AttributePlace::Argument(i), &[sret]);
499             }
500             PassMode::Cast(cast, _) => {
501                 cast.attrs.apply_attrs_to_callsite(
502                     llvm::AttributePlace::ReturnValue,
503                     &bx.cx,
504                     callsite,
505                 );
506             }
507             _ => {}
508         }
509         if let abi::Abi::Scalar(scalar) = self.ret.layout.abi {
510             // If the value is a boolean, the range is 0..2 and that ultimately
511             // become 0..0 when the type becomes i1, which would be rejected
512             // by the LLVM verifier.
513             if let Int(..) = scalar.primitive() {
514                 if !scalar.is_bool() && !scalar.is_always_valid(bx) {
515                     bx.range_metadata(callsite, scalar.valid_range(bx));
516                 }
517             }
518         }
519         for arg in self.args.iter() {
520             match &arg.mode {
521                 PassMode::Ignore => {}
522                 PassMode::Indirect { attrs, extra_attrs: None, on_stack: true } => {
523                     let i = apply(bx.cx, attrs);
524                     let byval = llvm::CreateByValAttr(bx.cx.llcx, arg.layout.llvm_type(bx));
525                     attributes::apply_to_callsite(
526                         callsite,
527                         llvm::AttributePlace::Argument(i),
528                         &[byval],
529                     );
530                 }
531                 PassMode::Direct(attrs)
532                 | PassMode::Indirect { attrs, extra_attrs: None, on_stack: false } => {
533                     apply(bx.cx, attrs);
534                 }
535                 PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack: _ } => {
536                     apply(bx.cx, attrs);
537                     apply(bx.cx, extra_attrs);
538                 }
539                 PassMode::Pair(a, b) => {
540                     apply(bx.cx, a);
541                     apply(bx.cx, b);
542                 }
543                 PassMode::Cast(cast, pad_i32) => {
544                     if *pad_i32 {
545                         apply(bx.cx, &ArgAttributes::new());
546                     }
547                     apply(bx.cx, &cast.attrs);
548                 }
549             }
550         }
551
552         let cconv = self.llvm_cconv();
553         if cconv != llvm::CCallConv {
554             llvm::SetInstructionCallConv(callsite, cconv);
555         }
556
557         if self.conv == Conv::CCmseNonSecureCall {
558             // This will probably get ignored on all targets but those supporting the TrustZone-M
559             // extension (thumbv8m targets).
560             let cmse_nonsecure_call = llvm::CreateAttrString(bx.cx.llcx, "cmse_nonsecure_call");
561             attributes::apply_to_callsite(
562                 callsite,
563                 llvm::AttributePlace::Function,
564                 &[cmse_nonsecure_call],
565             );
566         }
567
568         // Some intrinsics require that an elementtype attribute (with the pointee type of a
569         // pointer argument) is added to the callsite.
570         let element_type_index = unsafe { llvm::LLVMRustGetElementTypeArgIndex(callsite) };
571         if element_type_index >= 0 {
572             let arg_ty = self.args[element_type_index as usize].layout.ty;
573             let pointee_ty = arg_ty.builtin_deref(true).expect("Must be pointer argument").ty;
574             let element_type_attr = unsafe {
575                 llvm::LLVMRustCreateElementTypeAttr(bx.llcx, bx.layout_of(pointee_ty).llvm_type(bx))
576             };
577             attributes::apply_to_callsite(
578                 callsite,
579                 llvm::AttributePlace::Argument(element_type_index as u32),
580                 &[element_type_attr],
581             );
582         }
583     }
584 }
585
586 impl<'tcx> AbiBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
587     fn apply_attrs_callsite(&mut self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, callsite: Self::Value) {
588         fn_abi.apply_attrs_callsite(self, callsite)
589     }
590
591     fn get_param(&mut self, index: usize) -> Self::Value {
592         llvm::get_param(self.llfn(), index as c_uint)
593     }
594 }