]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/abi/call/aarch64.rs
Auto merge of #106959 - tmiasko:opt-funclets, r=davidtwco
[rust.git] / compiler / rustc_target / src / abi / call / aarch64.rs
1 use crate::abi::call::{ArgAbi, FnAbi, Reg, RegKind, Uniform};
2 use crate::abi::{HasDataLayout, TyAbiInterface};
3
4 /// Given integer-types M and register width N (e.g. M=u16 and N=32 bits), the
5 /// `ParamExtension` policy specifies how a uM value should be treated when
6 /// passed via register or stack-slot of width N. See also rust-lang/rust#97463.
7 #[derive(Copy, Clone, PartialEq)]
8 pub enum ParamExtension {
9     /// Indicates that when passing an i8/i16, either as a function argument or
10     /// as a return value, it must be sign-extended to 32 bits, and likewise a
11     /// u8/u16 must be zero-extended to 32-bits. (This variant is here to
12     /// accommodate Apple's deviation from the usual AArch64 ABI as defined by
13     /// ARM.)
14     ///
15     /// See also: <https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Pass-Arguments-to-Functions-Correctly>
16     ExtendTo32Bits,
17
18     /// Indicates that no sign- nor zero-extension is performed: if a value of
19     /// type with bitwidth M is passed as function argument or return value,
20     /// then M bits are copied into the least significant M bits, and the
21     /// remaining bits of the register (or word of memory) are untouched.
22     NoExtension,
23 }
24
25 fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>) -> Option<Uniform>
26 where
27     Ty: TyAbiInterface<'a, C> + Copy,
28     C: HasDataLayout,
29 {
30     arg.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()).and_then(|unit| {
31         let size = arg.layout.size;
32
33         // Ensure we have at most four uniquely addressable members.
34         if size > unit.size.checked_mul(4, cx).unwrap() {
35             return None;
36         }
37
38         let valid_unit = match unit.kind {
39             RegKind::Integer => false,
40             RegKind::Float => true,
41             RegKind::Vector => size.bits() == 64 || size.bits() == 128,
42         };
43
44         valid_unit.then_some(Uniform { unit, total: size })
45     })
46 }
47
48 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, param_policy: ParamExtension)
49 where
50     Ty: TyAbiInterface<'a, C> + Copy,
51     C: HasDataLayout,
52 {
53     if !ret.layout.is_aggregate() {
54         match param_policy {
55             ParamExtension::ExtendTo32Bits => ret.extend_integer_width_to(32),
56             ParamExtension::NoExtension => {}
57         }
58         return;
59     }
60     if let Some(uniform) = is_homogeneous_aggregate(cx, ret) {
61         ret.cast_to(uniform);
62         return;
63     }
64     let size = ret.layout.size;
65     let bits = size.bits();
66     if bits <= 128 {
67         ret.cast_to(Uniform { unit: Reg::i64(), total: size });
68         return;
69     }
70     ret.make_indirect();
71 }
72
73 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, param_policy: ParamExtension)
74 where
75     Ty: TyAbiInterface<'a, C> + Copy,
76     C: HasDataLayout,
77 {
78     if !arg.layout.is_aggregate() {
79         match param_policy {
80             ParamExtension::ExtendTo32Bits => arg.extend_integer_width_to(32),
81             ParamExtension::NoExtension => {}
82         }
83         return;
84     }
85     if let Some(uniform) = is_homogeneous_aggregate(cx, arg) {
86         arg.cast_to(uniform);
87         return;
88     }
89     let size = arg.layout.size;
90     let bits = size.bits();
91     if bits <= 128 {
92         arg.cast_to(Uniform { unit: Reg::i64(), total: size });
93         return;
94     }
95     arg.make_indirect();
96 }
97
98 pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, param_policy: ParamExtension)
99 where
100     Ty: TyAbiInterface<'a, C> + Copy,
101     C: HasDataLayout,
102 {
103     if !fn_abi.ret.is_ignore() {
104         classify_ret(cx, &mut fn_abi.ret, param_policy);
105     }
106
107     for arg in fn_abi.args.iter_mut() {
108         if arg.is_ignore() {
109             continue;
110         }
111         classify_arg(cx, arg, param_policy);
112     }
113 }