1 use crate::llvm::{self, AttributePlace};
2 use crate::builder::Builder;
3 use crate::context::CodegenCx;
4 use crate::type_::Type;
5 use crate::type_of::{LayoutLlvmExt, PointerKind};
6 use crate::value::Value;
7 use rustc_codegen_ssa::MemFlags;
8 use rustc_codegen_ssa::mir::place::PlaceRef;
9 use rustc_codegen_ssa::mir::operand::OperandValue;
10 use rustc_target::abi::call::ArgType;
12 use rustc_codegen_ssa::traits::*;
14 use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TyLayout, Abi as LayoutAbi};
15 use rustc::ty::{self, Ty, Instance};
16 use rustc::ty::layout;
20 pub use rustc_target::spec::abi::Abi;
21 pub use rustc::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
22 pub use rustc_target::abi::call::*;
24 macro_rules! for_each_kind {
25 ($flags: ident, $f: ident, $($kind: ident),+) => ({
26 $(if $flags.contains(ArgAttribute::$kind) { $f(llvm::Attribute::$kind) })+
30 trait ArgAttributeExt {
31 fn for_each_kind<F>(&self, f: F) where F: FnMut(llvm::Attribute);
34 impl ArgAttributeExt for ArgAttribute {
35 fn for_each_kind<F>(&self, mut f: F) where F: FnMut(llvm::Attribute) {
36 for_each_kind!(self, f,
37 ByVal, NoAlias, NoCapture, NonNull, ReadOnly, SExt, StructRet, ZExt, InReg)
41 pub trait ArgAttributesExt {
42 fn apply_llfn(&self, idx: AttributePlace, llfn: &Value);
43 fn apply_callsite(&self, idx: AttributePlace, callsite: &Value);
46 impl ArgAttributesExt for ArgAttributes {
47 fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
48 let mut regular = self.regular;
50 let deref = self.pointee_size.bytes();
52 if regular.contains(ArgAttribute::NonNull) {
53 llvm::LLVMRustAddDereferenceableAttr(llfn,
57 llvm::LLVMRustAddDereferenceableOrNullAttr(llfn,
61 regular -= ArgAttribute::NonNull;
63 if let Some(align) = self.pointee_align {
64 llvm::LLVMRustAddAlignmentAttr(llfn,
66 align.bytes() as u32);
68 regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn));
72 fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
73 let mut regular = self.regular;
75 let deref = self.pointee_size.bytes();
77 if regular.contains(ArgAttribute::NonNull) {
78 llvm::LLVMRustAddDereferenceableCallSiteAttr(callsite,
82 llvm::LLVMRustAddDereferenceableOrNullCallSiteAttr(callsite,
86 regular -= ArgAttribute::NonNull;
88 if let Some(align) = self.pointee_align {
89 llvm::LLVMRustAddAlignmentCallSiteAttr(callsite,
91 align.bytes() as u32);
93 regular.for_each_kind(|attr| attr.apply_callsite(idx, callsite));
99 fn llvm_type(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type;
102 impl LlvmType for Reg {
103 fn llvm_type(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
105 RegKind::Integer => cx.type_ix(self.size.bits()),
107 match self.size.bits() {
110 _ => bug!("unsupported float: {:?}", self)
114 cx.type_vector(cx.type_i8(), self.size.bytes())
120 impl LlvmType for CastTarget {
121 fn llvm_type(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
122 let rest_ll_unit = self.rest.unit.llvm_type(cx);
123 let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
126 (self.rest.total.bytes() / self.rest.unit.size.bytes(),
127 self.rest.total.bytes() % self.rest.unit.size.bytes())
130 if self.prefix.iter().all(|x| x.is_none()) {
131 // Simplify to a single unit when there is no prefix and size <= unit size
132 if self.rest.total <= self.rest.unit.size {
136 // Simplify to array when all chunks are the same size and type
138 return cx.type_array(rest_ll_unit, rest_count);
142 // Create list of fields in the main structure
143 let mut args: Vec<_> =
144 self.prefix.iter().flat_map(|option_kind| option_kind.map(
145 |kind| Reg { kind: kind, size: self.prefix_chunk }.llvm_type(cx)))
146 .chain((0..rest_count).map(|_| rest_ll_unit))
149 // Append final integer
151 // Only integers can be really split further.
152 assert_eq!(self.rest.unit.kind, RegKind::Integer);
153 args.push(cx.type_ix(rem_bytes * 8));
156 cx.type_struct(&args, false)
160 pub trait ArgTypeExt<'ll, 'tcx> {
161 fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
164 bx: &mut Builder<'_, 'll, 'tcx>,
166 dst: PlaceRef<'tcx, &'ll Value>,
170 bx: &mut Builder<'_, 'll, 'tcx>,
172 dst: PlaceRef<'tcx, &'ll Value>,
176 impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
177 /// Gets the LLVM type for a place of the original Rust type of
178 /// this argument/return, i.e., the result of `type_of::type_of`.
179 fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
180 self.layout.llvm_type(cx)
183 /// Stores a direct/indirect value described by this ArgType into a
184 /// place for the original Rust type of this argument/return.
185 /// Can be used for both storing formal arguments into Rust variables
186 /// or results of call/invoke instructions into their destinations.
189 bx: &mut Builder<'_, 'll, 'tcx>,
191 dst: PlaceRef<'tcx, &'ll Value>,
193 if self.is_ignore() {
196 if self.is_sized_indirect() {
197 OperandValue::Ref(val, None, self.layout.align.abi).store(bx, dst)
198 } else if self.is_unsized_indirect() {
199 bug!("unsized ArgType must be handled through store_fn_arg");
200 } else if let PassMode::Cast(cast) = self.mode {
201 // FIXME(eddyb): Figure out when the simpler Store is safe, clang
202 // uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
203 let can_store_through_cast_ptr = false;
204 if can_store_through_cast_ptr {
205 let cast_ptr_llty = bx.type_ptr_to(cast.llvm_type(bx));
206 let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
207 bx.store(val, cast_dst, self.layout.align.abi);
209 // The actual return type is a struct, but the ABI
210 // adaptation code has cast it into some scalar type. The
211 // code that follows is the only reliable way I have
212 // found to do a transform like i64 -> {i32,i32}.
213 // Basically we dump the data onto the stack then memcpy it.
215 // Other approaches I tried:
216 // - Casting rust ret pointer to the foreign type and using Store
217 // is (a) unsafe if size of foreign type > size of rust type and
218 // (b) runs afoul of strict aliasing rules, yielding invalid
219 // assembly under -O (specifically, the store gets removed).
220 // - Truncating foreign type to correct integral type and then
221 // bitcasting to the struct type yields invalid cast errors.
223 // We instead thus allocate some scratch space...
224 let scratch_size = cast.size(bx);
225 let scratch_align = cast.align(bx);
226 let llscratch = bx.alloca(cast.llvm_type(bx), "abi_cast", scratch_align);
227 bx.lifetime_start(llscratch, scratch_size);
229 // ...where we first store the value...
230 bx.store(val, llscratch, scratch_align);
232 // ...and then memcpy it to the intended destination.
235 self.layout.align.abi,
238 bx.const_usize(self.layout.size.bytes()),
242 bx.lifetime_end(llscratch, scratch_size);
245 OperandValue::Immediate(val).store(bx, dst);
251 bx: &mut Builder<'a, 'll, 'tcx>,
253 dst: PlaceRef<'tcx, &'ll Value>,
256 let val = llvm::get_param(bx.llfn(), *idx as c_uint);
261 PassMode::Ignore => {},
262 PassMode::Pair(..) => {
263 OperandValue::Pair(next(), next()).store(bx, dst);
265 PassMode::Indirect(_, Some(_)) => {
266 OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
268 PassMode::Direct(_) | PassMode::Indirect(_, None) | PassMode::Cast(_) => {
269 self.store(bx, next(), dst);
275 impl ArgTypeMethods<'tcx> for Builder<'a, 'll, 'tcx> {
278 ty: &ArgType<'tcx, Ty<'tcx>>,
279 idx: &mut usize, dst: PlaceRef<'tcx, Self::Value>
281 ty.store_fn_arg(self, idx, dst)
285 ty: &ArgType<'tcx, Ty<'tcx>>,
287 dst: PlaceRef<'tcx, &'ll Value>
289 ty.store(self, val, dst)
291 fn memory_ty(&self, ty: &ArgType<'tcx, Ty<'tcx>>) -> &'ll Type {
296 pub trait FnTypeExt<'tcx> {
297 fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>) -> Self;
298 fn new(cx: &CodegenCx<'ll, 'tcx>,
299 sig: ty::FnSig<'tcx>,
300 extra_args: &[Ty<'tcx>]) -> Self;
301 fn new_vtable(cx: &CodegenCx<'ll, 'tcx>,
302 sig: ty::FnSig<'tcx>,
303 extra_args: &[Ty<'tcx>]) -> Self;
305 cx: &CodegenCx<'ll, 'tcx>,
306 sig: ty::FnSig<'tcx>,
307 extra_args: &[Ty<'tcx>],
308 mk_arg_type: impl Fn(Ty<'tcx>, Option<usize>) -> ArgType<'tcx, Ty<'tcx>>,
310 fn adjust_for_abi(&mut self,
311 cx: &CodegenCx<'ll, 'tcx>,
313 fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
314 fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
315 fn llvm_cconv(&self) -> llvm::CallConv;
316 fn apply_attrs_llfn(&self, llfn: &'ll Value);
317 fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value);
320 impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
321 fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>) -> Self {
322 let sig = instance.fn_sig(cx.tcx);
323 let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
324 FnType::new(cx, sig, &[])
327 fn new(cx: &CodegenCx<'ll, 'tcx>,
328 sig: ty::FnSig<'tcx>,
329 extra_args: &[Ty<'tcx>]) -> Self {
330 FnType::new_internal(cx, sig, extra_args, |ty, _| {
331 ArgType::new(cx.layout_of(ty))
335 fn new_vtable(cx: &CodegenCx<'ll, 'tcx>,
336 sig: ty::FnSig<'tcx>,
337 extra_args: &[Ty<'tcx>]) -> Self {
338 FnType::new_internal(cx, sig, extra_args, |ty, arg_idx| {
339 let mut layout = cx.layout_of(ty);
340 // Don't pass the vtable, it's not an argument of the virtual fn.
341 // Instead, pass just the data pointer, but give it the type `*const/mut dyn Trait`
342 // or `&/&mut dyn Trait` because this is special-cased elsewhere in codegen
343 if arg_idx == Some(0) {
344 let fat_pointer_ty = if layout.is_unsized() {
345 // unsized `self` is passed as a pointer to `self`
346 // FIXME (mikeyhew) change this to use &own if it is ever added to the language
347 cx.tcx.mk_mut_ptr(layout.ty)
350 LayoutAbi::ScalarPair(..) => (),
351 _ => bug!("receiver type has unsupported layout: {:?}", layout)
354 // In the case of Rc<Self>, we need to explicitly pass a *mut RcBox<Self>
355 // with a Scalar (not ScalarPair) ABI. This is a hack that is understood
356 // elsewhere in the compiler as a method on a `dyn Trait`.
357 // To get the type `*mut RcBox<Self>`, we just keep unwrapping newtypes until we
358 // get a built-in pointer type
359 let mut fat_pointer_layout = layout;
360 'descend_newtypes: while !fat_pointer_layout.ty.is_unsafe_ptr()
361 && !fat_pointer_layout.ty.is_region_ptr()
363 'iter_fields: for i in 0..fat_pointer_layout.fields.count() {
364 let field_layout = fat_pointer_layout.field(cx, i);
366 if !field_layout.is_zst() {
367 fat_pointer_layout = field_layout;
368 continue 'descend_newtypes
372 bug!("receiver has no non-zero-sized fields {:?}", fat_pointer_layout);
375 fat_pointer_layout.ty
378 // we now have a type like `*mut RcBox<dyn Trait>`
379 // change its layout to that of `*mut ()`, a thin pointer, but keep the same type
380 // this is understood as a special case elsewhere in the compiler
381 let unit_pointer_ty = cx.tcx.mk_mut_ptr(cx.tcx.mk_unit());
382 layout = cx.layout_of(unit_pointer_ty);
383 layout.ty = fat_pointer_ty;
390 cx: &CodegenCx<'ll, 'tcx>,
391 sig: ty::FnSig<'tcx>,
392 extra_args: &[Ty<'tcx>],
393 mk_arg_type: impl Fn(Ty<'tcx>, Option<usize>) -> ArgType<'tcx, Ty<'tcx>>,
395 debug!("FnType::new_internal({:?}, {:?})", sig, extra_args);
398 let conv = match cx.sess().target.target.adjust_abi(sig.abi) {
399 RustIntrinsic | PlatformIntrinsic |
400 Rust | RustCall => Conv::C,
402 // It's the ABI's job to select this, not ours.
403 System => bug!("system abi should be selected elsewhere"),
405 Stdcall => Conv::X86Stdcall,
406 Fastcall => Conv::X86Fastcall,
407 Vectorcall => Conv::X86VectorCall,
408 Thiscall => Conv::X86ThisCall,
410 Unadjusted => Conv::C,
411 Win64 => Conv::X86_64Win64,
412 SysV64 => Conv::X86_64SysV,
413 Aapcs => Conv::ArmAapcs,
414 PtxKernel => Conv::PtxKernel,
415 Msp430Interrupt => Conv::Msp430Intr,
416 X86Interrupt => Conv::X86Intr,
417 AmdGpuKernel => Conv::AmdGpuKernel,
419 // These API constants ought to be more specific...
423 let mut inputs = sig.inputs();
424 let extra_args = if sig.abi == RustCall {
425 assert!(!sig.variadic && extra_args.is_empty());
427 match sig.inputs().last().unwrap().sty {
428 ty::Tuple(ref tupled_arguments) => {
429 inputs = &sig.inputs()[0..sig.inputs().len() - 1];
433 bug!("argument to function with \"rust-call\" ABI \
438 assert!(sig.variadic || extra_args.is_empty());
442 let target = &cx.sess().target.target;
443 let win_x64_gnu = target.target_os == "windows"
444 && target.arch == "x86_64"
445 && target.target_env == "gnu";
446 let linux_s390x = target.target_os == "linux"
447 && target.arch == "s390x"
448 && target.target_env == "gnu";
449 let linux_sparc64 = target.target_os == "linux"
450 && target.arch == "sparc64"
451 && target.target_env == "gnu";
452 let rust_abi = match sig.abi {
453 RustIntrinsic | PlatformIntrinsic | Rust | RustCall => true,
457 // Handle safe Rust thin and fat pointers.
458 let adjust_for_rust_scalar = |attrs: &mut ArgAttributes,
459 scalar: &layout::Scalar,
460 layout: TyLayout<'tcx, Ty<'tcx>>,
463 // Booleans are always an i1 that needs to be zero-extended.
464 if scalar.is_bool() {
465 attrs.set(ArgAttribute::ZExt);
469 // Only pointer types handled below.
470 if scalar.value != layout::Pointer {
474 if scalar.valid_range.start() < scalar.valid_range.end() {
475 if *scalar.valid_range.start() > 0 {
476 attrs.set(ArgAttribute::NonNull);
480 if let Some(pointee) = layout.pointee_info_at(cx, offset) {
481 if let Some(kind) = pointee.safe {
482 attrs.pointee_size = pointee.size;
483 attrs.pointee_align = Some(pointee.align);
485 // `Box` pointer parameters never alias because ownership is transferred
486 // `&mut` pointer parameters never alias other parameters,
487 // or mutable global data
489 // `&T` where `T` contains no `UnsafeCell<U>` is immutable,
490 // and can be marked as both `readonly` and `noalias`, as
491 // LLVM's definition of `noalias` is based solely on memory
492 // dependencies rather than pointer equality
493 let no_alias = match kind {
494 PointerKind::Shared => false,
495 PointerKind::UniqueOwned => true,
496 PointerKind::Frozen |
497 PointerKind::UniqueBorrowed => !is_return
500 attrs.set(ArgAttribute::NoAlias);
503 if kind == PointerKind::Frozen && !is_return {
504 attrs.set(ArgAttribute::ReadOnly);
510 let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| {
511 let is_return = arg_idx.is_none();
512 let mut arg = mk_arg_type(ty, arg_idx);
513 if arg.layout.is_zst() {
514 // For some forsaken reason, x86_64-pc-windows-gnu
515 // doesn't ignore zero-sized struct arguments.
516 // The same is true for s390x-unknown-linux-gnu
517 // and sparc64-unknown-linux-gnu.
518 if is_return || rust_abi || (!win_x64_gnu && !linux_s390x && !linux_sparc64) {
519 arg.mode = PassMode::Ignore;
523 // FIXME(eddyb) other ABIs don't have logic for scalar pairs.
524 if !is_return && rust_abi {
525 if let layout::Abi::ScalarPair(ref a, ref b) = arg.layout.abi {
526 let mut a_attrs = ArgAttributes::new();
527 let mut b_attrs = ArgAttributes::new();
528 adjust_for_rust_scalar(&mut a_attrs,
533 adjust_for_rust_scalar(&mut b_attrs,
536 a.value.size(cx).align_to(b.value.align(cx).abi),
538 arg.mode = PassMode::Pair(a_attrs, b_attrs);
543 if let layout::Abi::Scalar(ref scalar) = arg.layout.abi {
544 if let PassMode::Direct(ref mut attrs) = arg.mode {
545 adjust_for_rust_scalar(attrs,
556 let mut fn_ty = FnType {
557 ret: arg_of(sig.output(), None),
558 args: inputs.iter().chain(extra_args).enumerate().map(|(i, ty)| {
561 variadic: sig.variadic,
564 fn_ty.adjust_for_abi(cx, sig.abi);
568 fn adjust_for_abi(&mut self,
569 cx: &CodegenCx<'ll, 'tcx>,
571 if abi == Abi::Unadjusted { return }
573 if abi == Abi::Rust || abi == Abi::RustCall ||
574 abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
575 let fixup = |arg: &mut ArgType<'tcx, Ty<'tcx>>| {
576 if arg.is_ignore() { return; }
578 match arg.layout.abi {
579 layout::Abi::Aggregate { .. } => {}
581 // This is a fun case! The gist of what this is doing is
582 // that we want callers and callees to always agree on the
583 // ABI of how they pass SIMD arguments. If we were to *not*
584 // make these arguments indirect then they'd be immediates
585 // in LLVM, which means that they'd used whatever the
586 // appropriate ABI is for the callee and the caller. That
587 // means, for example, if the caller doesn't have AVX
588 // enabled but the callee does, then passing an AVX argument
589 // across this boundary would cause corrupt data to show up.
591 // This problem is fixed by unconditionally passing SIMD
592 // arguments through memory between callers and callees
593 // which should get them all to agree on ABI regardless of
594 // target feature sets. Some more information about this
595 // issue can be found in #44367.
597 // Note that the platform intrinsic ABI is exempt here as
598 // that's how we connect up to LLVM and it's unstable
599 // anyway, we control all calls to it in libstd.
600 layout::Abi::Vector { .. }
601 if abi != Abi::PlatformIntrinsic &&
602 cx.sess().target.target.options.simd_types_indirect =>
611 let size = arg.layout.size;
612 if arg.layout.is_unsized() || size > layout::Pointer.size(cx) {
615 // We want to pass small aggregates as immediates, but using
616 // a LLVM aggregate type for this leads to bad optimizations,
617 // so we pick an appropriately sized integer type instead.
619 kind: RegKind::Integer,
624 fixup(&mut self.ret);
625 for arg in &mut self.args {
628 if let PassMode::Indirect(ref mut attrs, _) = self.ret.mode {
629 attrs.set(ArgAttribute::StructRet);
634 if let Err(msg) = self.adjust_for_cabi(cx, abi) {
635 cx.sess().fatal(&msg);
639 fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
640 let args_capacity: usize = self.args.iter().map(|arg|
641 if arg.pad.is_some() { 1 } else { 0 } +
642 if let PassMode::Pair(_, _) = arg.mode { 2 } else { 1 }
644 let mut llargument_tys = Vec::with_capacity(
645 if let PassMode::Indirect(..) = self.ret.mode { 1 } else { 0 } + args_capacity
648 let llreturn_ty = match self.ret.mode {
649 PassMode::Ignore => cx.type_void(),
650 PassMode::Direct(_) | PassMode::Pair(..) => {
651 self.ret.layout.immediate_llvm_type(cx)
653 PassMode::Cast(cast) => cast.llvm_type(cx),
654 PassMode::Indirect(..) => {
655 llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
660 for arg in &self.args {
662 if let Some(ty) = arg.pad {
663 llargument_tys.push(ty.llvm_type(cx));
666 let llarg_ty = match arg.mode {
667 PassMode::Ignore => continue,
668 PassMode::Direct(_) => arg.layout.immediate_llvm_type(cx),
669 PassMode::Pair(..) => {
670 llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 0, true));
671 llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1, true));
674 PassMode::Indirect(_, Some(_)) => {
675 let ptr_ty = cx.tcx.mk_mut_ptr(arg.layout.ty);
676 let ptr_layout = cx.layout_of(ptr_ty);
677 llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 0, true));
678 llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
681 PassMode::Cast(cast) => cast.llvm_type(cx),
682 PassMode::Indirect(_, None) => cx.type_ptr_to(arg.memory_ty(cx)),
684 llargument_tys.push(llarg_ty);
688 cx.type_variadic_func(&llargument_tys, llreturn_ty)
690 cx.type_func(&llargument_tys, llreturn_ty)
694 fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
696 llvm::LLVMPointerType(self.llvm_type(cx),
697 cx.data_layout().instruction_address_space as c_uint)
701 fn llvm_cconv(&self) -> llvm::CallConv {
703 Conv::C => llvm::CCallConv,
704 Conv::AmdGpuKernel => llvm::AmdGpuKernel,
705 Conv::ArmAapcs => llvm::ArmAapcsCallConv,
706 Conv::Msp430Intr => llvm::Msp430Intr,
707 Conv::PtxKernel => llvm::PtxKernel,
708 Conv::X86Fastcall => llvm::X86FastcallCallConv,
709 Conv::X86Intr => llvm::X86_Intr,
710 Conv::X86Stdcall => llvm::X86StdcallCallConv,
711 Conv::X86ThisCall => llvm::X86_ThisCall,
712 Conv::X86VectorCall => llvm::X86_VectorCall,
713 Conv::X86_64SysV => llvm::X86_64_SysV,
714 Conv::X86_64Win64 => llvm::X86_64_Win64,
718 fn apply_attrs_llfn(&self, llfn: &'ll Value) {
720 let mut apply = |attrs: &ArgAttributes| {
721 attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn);
724 match self.ret.mode {
725 PassMode::Direct(ref attrs) => {
726 attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
728 PassMode::Indirect(ref attrs, _) => apply(attrs),
731 for arg in &self.args {
732 if arg.pad.is_some() {
733 apply(&ArgAttributes::new());
736 PassMode::Ignore => {}
737 PassMode::Direct(ref attrs) |
738 PassMode::Indirect(ref attrs, None) => apply(attrs),
739 PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
743 PassMode::Pair(ref a, ref b) => {
747 PassMode::Cast(_) => apply(&ArgAttributes::new()),
752 fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value) {
754 let mut apply = |attrs: &ArgAttributes| {
755 attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite);
758 match self.ret.mode {
759 PassMode::Direct(ref attrs) => {
760 attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite);
762 PassMode::Indirect(ref attrs, _) => apply(attrs),
765 if let layout::Abi::Scalar(ref scalar) = self.ret.layout.abi {
766 // If the value is a boolean, the range is 0..2 and that ultimately
767 // become 0..0 when the type becomes i1, which would be rejected
768 // by the LLVM verifier.
769 if let layout::Int(..) = scalar.value {
770 if !scalar.is_bool() {
771 let range = scalar.valid_range_exclusive(bx);
772 if range.start != range.end {
773 bx.range_metadata(callsite, range);
778 for arg in &self.args {
779 if arg.pad.is_some() {
780 apply(&ArgAttributes::new());
783 PassMode::Ignore => {}
784 PassMode::Direct(ref attrs) |
785 PassMode::Indirect(ref attrs, None) => apply(attrs),
786 PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
790 PassMode::Pair(ref a, ref b) => {
794 PassMode::Cast(_) => apply(&ArgAttributes::new()),
798 let cconv = self.llvm_cconv();
799 if cconv != llvm::CCallConv {
800 llvm::SetInstructionCallConv(callsite, cconv);
805 impl AbiMethods<'tcx> for CodegenCx<'ll, 'tcx> {
806 fn new_fn_type(&self, sig: ty::FnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> FnType<'tcx, Ty<'tcx>> {
807 FnType::new(&self, sig, extra_args)
811 sig: ty::FnSig<'tcx>,
812 extra_args: &[Ty<'tcx>]
813 ) -> FnType<'tcx, Ty<'tcx>> {
814 FnType::new_vtable(&self, sig, extra_args)
816 fn fn_type_of_instance(&self, instance: &Instance<'tcx>) -> FnType<'tcx, Ty<'tcx>> {
817 FnType::of_instance(&self, instance)
821 impl AbiBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
822 fn apply_attrs_callsite(
824 ty: &FnType<'tcx, Ty<'tcx>>,
825 callsite: Self::Value
827 ty.apply_attrs_callsite(self, callsite)