LLVM 24.0.0git
FunctionInfo.h
Go to the documentation of this file.
1//===----- FunctionInfo.h - ABI Function Information --------- C++ --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines FunctionInfo and associated types used in representing the
10// ABI-coerced types for function arguments and return values.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ABI_FUNCTIONINFO_H
15#define LLVM_ABI_FUNCTIONINFO_H
16
17#include "llvm/ABI/Types.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/IR/CallingConv.h"
23#include <optional>
24
25namespace llvm {
26namespace abi {
27
28/// Helper class to encapsulate information about how a specific type should be
29/// passed to or returned from a function.
30class ArgInfo {
31public:
32 enum Kind {
33 /// Pass the argument directly using the normal converted LLVM type, or by
34 /// coercing to another specified type stored in 'CoerceToType'.
36 /// Valid only for integer argument types. Same as 'direct' but also emit a
37 /// zero/sign extension attribute.
39 /// Pass the argument indirectly via a hidden pointer with the specified
40 /// alignment and address space.
42 /// Like Indirect, but the pointer may alias an object referenced
43 /// elsewhere. The callee must not modify it and never treats it as byval.
45 /// Ignore the argument (treat as void). Useful for void and empty structs.
47
48 /// Only valid for aggregate argument types. The value is expanded into
49 /// one argument per non-padding element of CoerceToType. Arrays of i8 are
50 /// assumed to be padding.
52 };
53
54private:
55 const Type *CoercionType = nullptr;
56 const Type *UnpaddedCoerceAndExpandType = nullptr;
57 // Alignment is optional for direct arguments, but required for indirect
58 // arguments. This invariant is enforced by the methods of this class.
59 //
60 // The field is not part of DirectAttrInfo/IndirectAttrInfo because it would
61 // make the union non-trivial, disabling implicit copy/move constructors and
62 // assignment operators for the entire class.
63 MaybeAlign Alignment;
64
65 struct DirectAttrInfo {
66 unsigned Offset;
67 };
68
69 struct IndirectAttrInfo {
70 unsigned AddrSpace;
71 };
72
73 union {
74 DirectAttrInfo DirectAttr;
75 IndirectAttrInfo IndirectAttr;
76 };
77
78 Kind TheKind;
79 bool SignExt : 1;
80 bool ZeroExt : 1;
81 bool Conservative : 1;
82 bool IndirectByVal : 1;
83 bool IndirectRealign : 1;
84 bool CanBeFlattened : 1;
85 unsigned NeededIntRegs : 3;
86 unsigned NeededSseRegs : 3;
87
88 ArgInfo(Kind K = Direct)
89 : TheKind(K), SignExt(false), ZeroExt(false), Conservative(false),
90 IndirectByVal(false), IndirectRealign(false), CanBeFlattened(false),
91 NeededIntRegs(0), NeededSseRegs(0) {}
92
93public:
94 /// \param T The type to coerce to. If null, the argument's original type is
95 /// used directly.
96 /// \param Offset Byte offset into the memory representation at which the
97 /// coerced type begins. Used when only part of a larger value
98 /// is passed directly (e.g. the high word of a multi-eightbyte
99 /// return value on x86-64).
100 /// \param Align Override for the argument's alignment. If absent, the
101 /// default alignment for \p T is used.
102 /// \param CanBeFlattened Whether a record coercion may be split into one
103 /// wire argument per field. See getCanBeFlattened.
104 static ArgInfo getDirect(const Type *T = nullptr, unsigned Offset = 0,
105 MaybeAlign Align = std::nullopt,
106 bool CanBeFlattened = true) {
107 ArgInfo AI(Direct);
108 AI.CoercionType = T;
109 AI.Alignment = Align;
110 AI.DirectAttr.Offset = Offset;
111 AI.CanBeFlattened = CanBeFlattened;
112 return AI;
113 }
114
115 static ArgInfo getExtend(const Type *T) {
116 assert(T && "Type cannot be null");
117 assert(T->isInteger() && "Unexpected type - only integers can be extended");
118
119 ArgInfo AI(Extend);
120 AI.CoercionType = T;
121 AI.Alignment = std::nullopt;
122 AI.DirectAttr.Offset = 0;
123
124 const IntegerType *IntTy = cast<IntegerType>(T);
125 if (IntTy->isSigned())
126 AI.setSignExt();
127 else
128 AI.setZeroExt();
129
130 return AI;
131 }
132
133 static ArgInfo getConservativeExtend(const Type *T) {
134 ArgInfo AI = getExtend(T);
135 AI.Conservative = true;
136 return AI;
137 }
138
139 /// Realign: the caller couldn't guarantee sufficient alignment - the callee
140 /// must copy the argument to a properly aligned temporary before use.
141 static ArgInfo getIndirect(Align Align, bool ByVal, unsigned AddrSpace = 0,
142 bool Realign = false) {
143 ArgInfo AI(Indirect);
144 AI.Alignment = Align;
145 AI.IndirectAttr.AddrSpace = AddrSpace;
146 AI.IndirectByVal = ByVal;
147 AI.IndirectRealign = Realign;
148 return AI;
149 }
150
151 /// An aliased indirect argument. It carries an address space but no byval,
152 /// since the pointer refers to an object the caller owns.
153 static ArgInfo getIndirectAliased(Align Align, unsigned AddrSpace,
154 bool Realign = false) {
155 ArgInfo AI(IndirectAliased);
156 AI.Alignment = Align;
157 AI.IndirectAttr.AddrSpace = AddrSpace;
158 AI.IndirectRealign = Realign;
159 return AI;
160 }
161
162 static ArgInfo getIgnore() { return ArgInfo(Ignore); }
163
164 /// \param CoerceToType Aggregate type whose non-padding elements are the
165 /// in-memory pieces of the value, in order. Padding elements are
166 /// arrays of i8.
167 /// \param UnpaddedCoerceToType \p CoerceToType with padding elements
168 /// removed. A single element is stored as that element; two or more
169 /// are a packed record.
170 static ArgInfo getCoerceAndExpand(const Type *CoerceToType,
171 const Type *UnpaddedCoerceToType) {
172 assert(CoerceToType && UnpaddedCoerceToType &&
173 "coerce-and-expand types cannot be null");
174 assert(isa<RecordType>(CoerceToType) &&
175 "coerce-and-expand coerce type must be a record");
176 ArgInfo AI(CoerceAndExpand);
177 AI.CoercionType = CoerceToType;
178 AI.UnpaddedCoerceAndExpandType = UnpaddedCoerceToType;
179 return AI;
180 }
181
182 ArgInfo &setSignExt(bool SignExtend = true) {
183 this->SignExt = SignExtend;
184 if (SignExtend)
185 this->ZeroExt = false;
186 return *this;
187 }
188
189 ArgInfo &setZeroExt(bool ZeroExtend = true) {
190 this->ZeroExt = ZeroExtend;
191 if (ZeroExtend)
192 this->SignExt = false;
193 return *this;
194 }
195
196 /// See getCanBeFlattened.
197 ArgInfo &setCanBeFlattened(bool Flatten) {
198 assert(isDirect() && "Invalid Kind!");
199 CanBeFlattened = Flatten;
200 return *this;
201 }
202
203 Kind getKind() const { return TheKind; }
204 bool isDirect() const { return TheKind == Direct; }
205 bool isIndirect() const { return TheKind == Indirect; }
206 bool isIndirectAliased() const { return TheKind == IndirectAliased; }
207 bool isIgnore() const { return TheKind == Ignore; }
208 bool isExtend() const { return TheKind == Extend; }
209 bool isCoerceAndExpand() const { return TheKind == CoerceAndExpand; }
210
211 unsigned getDirectOffset() const {
212 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
213 return DirectAttr.Offset;
214 }
215
216 /// How many integer and vector argument registers this argument occupies.
217 /// Both zero means it occupies none and travels in memory, which is also
218 /// what a target whose classifier does not record the demand reports.
219 unsigned getNeededIntRegs() const { return NeededIntRegs; }
220 unsigned getNeededSseRegs() const { return NeededSseRegs; }
221
222 void setNeededRegs(unsigned IntRegs, unsigned SseRegs) {
223 assert(IntRegs <= 7 && SseRegs <= 7 && "Register demand does not fit");
224 NeededIntRegs = IntRegs;
225 NeededSseRegs = SseRegs;
226 }
227
229 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
230 return Alignment;
231 }
232
234 assert((isIndirect() || isIndirectAliased()) && "Invalid Kind!");
235 assert(Alignment.has_value() &&
236 "Indirect arguments must have an alignment");
237 return *Alignment;
238 }
239
240 unsigned getIndirectAddrSpace() const {
241 assert((isIndirect() || isIndirectAliased()) && "Invalid Kind!");
242 return IndirectAttr.AddrSpace;
243 }
244
245 bool getIndirectByVal() const {
246 // Aliased pointers are never byval.
247 assert(isIndirect() && "Invalid Kind!");
248 return IndirectByVal;
249 }
250
251 bool getIndirectRealign() const {
252 assert((isIndirect() || isIndirectAliased()) && "Invalid Kind!");
253 return IndirectRealign;
254 }
255
256 /// Whether a Direct record coercion may be split into one wire argument
257 /// per field. Mirrors clang::CodeGen::ABIArgInfo::CanBeFlattened.
258 bool getCanBeFlattened() const {
259 assert(isDirect() && "Invalid Kind!");
260 return CanBeFlattened;
261 }
262
263 bool isSignExt() const {
264 assert(isExtend() && "Invalid Kind!");
265 return SignExt;
266 }
267
268 bool isZeroExt() const {
269 assert(isExtend() && "Invalid Kind!");
270 return ZeroExt;
271 }
272
273 bool isNoExt() const {
274 assert(isExtend() && "Invalid Kind!");
275 return !SignExt && !ZeroExt;
276 }
277
278 /// Whether the extension is conservative, i.e. the sender extends, but the
279 /// receiver does not assume the extension. Currently only supported for
280 /// arguments, i.e. caller extends, but callee does not assume extension.
281 bool isConservative() const {
282 assert(isExtend() && "Invalid Kind!");
283 return Conservative;
284 }
285
286 const Type *getCoerceToType() const {
288 "Invalid Kind!");
289 return CoercionType;
290 }
291
293 assert(isCoerceAndExpand() && "Invalid Kind!");
294 return UnpaddedCoerceAndExpandType;
295 }
296
297 /// True if \p Ty is an array of i8, the padding element of a
298 /// coerce-and-expand type. A matrix is an array in this type system
299 /// and lowers to a vector, so it is not padding.
300 static bool isPaddingForCoerceAndExpand(const Type *Ty) {
301 const auto *AT = dyn_cast<ArrayType>(Ty);
302 if (!AT || AT->isMatrixType())
303 return false;
304 const auto *Elt = dyn_cast<IntegerType>(AT->getElementType());
305 return Elt && Elt->getSizeInBits().getFixedValue() == 8;
306 }
307};
308
309struct ArgEntry {
310 const Type *ABIType;
312
313 ArgEntry(const Type *T) : ABIType(T), Info(ArgInfo::getDirect()) {}
314 ArgEntry(const Type *T, ArgInfo A) : ABIType(T), Info(A) {}
315};
316
317/// Whether a signature accepts arguments beyond its declared parameters, and
318/// where the declared ones end when it does. An argument past an ellipsis is
319/// unnamed, and some ABI rules pass an unnamed type differently.
320///
321/// A count and All are not interchangeable when the count equals the number of
322/// arguments. FunctionInfo::isVariadic() is true whenever a count was given,
323/// so a signature with no ellipsis has to be spelled All.
325 /// The number of leading arguments that are declared parameters, or ~0U if
326 /// the signature accepts no optional arguments.
327 unsigned NumRequired;
328
329public:
330 enum All_t { All };
331
332 /// A signature with no ellipsis, where every argument is declared.
333 RequiredArgs(All_t) : NumRequired(~0U) {}
334
335 /// A signature whose leading \p N arguments are declared and whose remaining
336 /// arguments pass through an ellipsis.
337 explicit RequiredArgs(unsigned N) : NumRequired(N) {
338 assert(N != ~0U && "~0U is reserved for a signature with no ellipsis");
339 }
340
341 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
342
343 unsigned getNumRequiredArgs() const {
344 assert(allowsOptionalArgs() && "signature accepts no optional arguments");
345 return NumRequired;
346 }
347};
348
349class FunctionInfo final : private TrailingObjects<FunctionInfo, ArgEntry> {
350private:
351 const Type *ReturnType;
352 ArgInfo ReturnInfo;
353 unsigned NumArgs;
355 RequiredArgs Required;
356
357 FunctionInfo(CallingConv::ID CC, const Type *RetTy, unsigned NumArguments,
358 RequiredArgs Required)
359 : ReturnType(RetTy), ReturnInfo(ArgInfo::getDirect()),
360 NumArgs(NumArguments), CC(CC), Required(Required) {}
361
362 friend class TrailingObjects;
363
364public:
367
368 void operator delete(void *p) { ::operator delete(p); }
370 const_arg_iterator arg_end() const { return getTrailingObjects() + NumArgs; }
372 arg_iterator arg_end() { return getTrailingObjects() + NumArgs; }
373
374 unsigned arg_size() const { return NumArgs; }
375
376 LLVM_ABI static std::unique_ptr<FunctionInfo>
377 create(CallingConv::ID CC, const Type *ReturnType,
378 ArrayRef<const Type *> ArgTypes,
380
381 const Type *getReturnType() const { return ReturnType; }
382 ArgInfo &getReturnInfo() { return ReturnInfo; }
383 const ArgInfo &getReturnInfo() const { return ReturnInfo; }
384
386
387 bool isVariadic() const { return Required.allowsOptionalArgs(); }
388
389 unsigned getNumRequiredArgs() const {
390 return isVariadic() ? Required.getNumRequiredArgs() : arg_size();
391 }
392
394 return {getTrailingObjects(), NumArgs};
395 }
396
398 return {getTrailingObjects(), NumArgs};
399 }
400
401 ArgEntry &getArgInfo(unsigned Index) {
402 assert(Index < NumArgs && "Invalid argument index");
403 return arguments()[Index];
404 }
405
406 const ArgEntry &getArgInfo(unsigned Index) const {
407 assert(Index < NumArgs && "Invalid argument index");
408 return arguments()[Index];
409 }
410};
411
412} // namespace abi
413} // namespace llvm
414
415#endif // LLVM_ABI_FUNCTIONINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:211
#define T
RISCVCC::CondCode CC
static const MCPhysReg IntRegs[32]
This header defines support for implementing classes that have some trailing object (or arrays of obj...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Helper class to encapsulate information about how a specific type should be passed to or returned fro...
bool isDirect() const
bool getCanBeFlattened() const
Whether a Direct record coercion may be split into one wire argument per field.
const Type * getCoerceToType() const
static ArgInfo getConservativeExtend(const Type *T)
bool isCoerceAndExpand() const
ArgInfo & setCanBeFlattened(bool Flatten)
See getCanBeFlattened.
static bool isPaddingForCoerceAndExpand(const Type *Ty)
True if Ty is an array of i8, the padding element of a coerce-and-expand type.
Align getIndirectAlign() const
const Type * getUnpaddedCoerceAndExpandType() const
DirectAttrInfo DirectAttr
bool isSignExt() const
ArgInfo & setZeroExt(bool ZeroExtend=true)
static ArgInfo getIgnore()
bool isIgnore() const
static ArgInfo getIndirectAliased(Align Align, unsigned AddrSpace, bool Realign=false)
An aliased indirect argument.
void setNeededRegs(unsigned IntRegs, unsigned SseRegs)
bool isConservative() const
Whether the extension is conservative, i.e.
unsigned getIndirectAddrSpace() const
static ArgInfo getCoerceAndExpand(const Type *CoerceToType, const Type *UnpaddedCoerceToType)
IndirectAttrInfo IndirectAttr
Kind getKind() const
@ IndirectAliased
Like Indirect, but the pointer may alias an object referenced elsewhere.
@ Extend
Valid only for integer argument types.
@ CoerceAndExpand
Only valid for aggregate argument types.
@ Direct
Pass the argument directly using the normal converted LLVM type, or by coercing to another specified ...
@ Ignore
Ignore the argument (treat as void). Useful for void and empty structs.
@ Indirect
Pass the argument indirectly via a hidden pointer with the specified alignment and address space.
static ArgInfo getExtend(const Type *T)
static ArgInfo getIndirect(Align Align, bool ByVal, unsigned AddrSpace=0, bool Realign=false)
Realign: the caller couldn't guarantee sufficient alignment - the callee must copy the argument to a ...
MaybeAlign getDirectAlign() const
bool isExtend() const
static ArgInfo getDirect(const Type *T=nullptr, unsigned Offset=0, MaybeAlign Align=std::nullopt, bool CanBeFlattened=true)
unsigned getNeededIntRegs() const
How many integer and vector argument registers this argument occupies.
bool getIndirectRealign() const
ArgInfo & setSignExt(bool SignExtend=true)
bool isNoExt() const
bool getIndirectByVal() const
bool isIndirect() const
unsigned getNeededSseRegs() const
bool isIndirectAliased() const
unsigned getDirectOffset() const
bool isZeroExt() const
ArrayRef< ArgEntry > arguments() const
unsigned getNumRequiredArgs() const
const ArgInfo & getReturnInfo() const
const ArgEntry * const_arg_iterator
MutableArrayRef< ArgEntry > arguments()
CallingConv::ID getCallingConvention() const
const_arg_iterator arg_begin() const
const ArgEntry & getArgInfo(unsigned Index) const
friend class TrailingObjects
const_arg_iterator arg_end() const
unsigned arg_size() const
arg_iterator arg_begin()
ArgEntry & getArgInfo(unsigned Index)
const Type * getReturnType() const
static LLVM_ABI std::unique_ptr< FunctionInfo > create(CallingConv::ID CC, const Type *ReturnType, ArrayRef< const Type * > ArgTypes, RequiredArgs Required=RequiredArgs::All)
Whether a signature accepts arguments beyond its declared parameters, and where the declared ones end...
bool allowsOptionalArgs() const
RequiredArgs(unsigned N)
A signature whose leading N arguments are declared and whose remaining arguments pass through an elli...
unsigned getNumRequiredArgs() const
RequiredArgs(All_t)
A signature with no ellipsis, where every argument is declared.
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:48
This file defines the type system for the LLVMABI library, which mirrors ABI-relevant aspects of fron...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:111
ArgEntry(const Type *T)
ArgEntry(const Type *T, ArgInfo A)
const Type * ABIType