1 //==-- CGFunctionInfo.h - Representation of function argument/return types -==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines CGFunctionInfo and associated types used in representing the
11 // LLVM source types and ABI-coerced types for function arguments and
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_CODEGEN_CGFUNCTIONINFO_H
17 #define LLVM_CLANG_CODEGEN_CGFUNCTIONINFO_H
19 #include "clang/AST/CanonicalType.h"
20 #include "clang/AST/CharUnits.h"
21 #include "clang/AST/Type.h"
22 #include "llvm/ADT/FoldingSet.h"
35 /// ABIArgInfo - Helper class to encapsulate information about how a
36 /// specific C type should be passed to or returned from a function.
40 /// Direct - Pass the argument directly using the normal converted LLVM
41 /// type, or by coercing to another specified type stored in
42 /// 'CoerceToType'). If an offset is specified (in UIntData), then the
43 /// argument passed is offset by some number of bytes in the memory
44 /// representation. A dummy argument is emitted before the real argument
45 /// if the specified type stored in "PaddingType" is not zero.
48 /// Extend - Valid only for integer argument types. Same as 'direct'
49 /// but also emit a zero/sign extension attribute.
52 /// Indirect - Pass the argument indirectly via a hidden pointer
53 /// with the specified alignment (0 indicates default alignment).
56 /// Ignore - Ignore the argument (treat as void). Useful for void and
60 /// Expand - Only valid for aggregate argument types. The structure should
61 /// be expanded into consecutive arguments for its constituent fields.
62 /// Currently expand is only allowed on structures whose fields
63 /// are all scalar types or are themselves expandable types.
66 /// InAlloca - Pass the argument directly using the LLVM inalloca attribute.
67 /// This is similar to indirect with byval, except it only applies to
68 /// arguments stored in memory and forbids any implicit copies. When
69 /// applied to a return type, it means the value is returned indirectly via
70 /// an implicit sret parameter stored in the argument struct.
77 llvm::Type *TypeData; // isDirect() || isExtend()
78 llvm::Type *PaddingType;
80 unsigned DirectOffset; // isDirect() || isExtend()
81 unsigned IndirectAlign; // isIndirect()
82 unsigned AllocaFieldIndex; // isInAlloca()
85 bool PaddingInReg : 1;
86 bool InAllocaSRet : 1; // isInAlloca()
87 bool IndirectByVal : 1; // isIndirect()
88 bool IndirectRealign : 1; // isIndirect()
89 bool SRetAfterThis : 1; // isIndirect()
90 bool InReg : 1; // isDirect() || isExtend() || isIndirect()
91 bool CanBeFlattened: 1; // isDirect()
94 : PaddingType(nullptr), TheKind(K), PaddingInReg(false), InReg(false) {}
98 : TypeData(nullptr), PaddingType(nullptr), DirectOffset(0),
99 TheKind(Direct), PaddingInReg(false), InReg(false) {}
101 static ABIArgInfo getDirect(llvm::Type *T = nullptr, unsigned Offset = 0,
102 llvm::Type *Padding = nullptr,
103 bool CanBeFlattened = true) {
104 auto AI = ABIArgInfo(Direct);
105 AI.setCoerceToType(T);
106 AI.setDirectOffset(Offset);
107 AI.setPaddingType(Padding);
108 AI.setCanBeFlattened(CanBeFlattened);
111 static ABIArgInfo getDirectInReg(llvm::Type *T = nullptr) {
112 auto AI = getDirect(T);
116 static ABIArgInfo getExtend(llvm::Type *T = nullptr) {
117 auto AI = ABIArgInfo(Extend);
118 AI.setCoerceToType(T);
119 AI.setDirectOffset(0);
122 static ABIArgInfo getExtendInReg(llvm::Type *T = nullptr) {
123 auto AI = getExtend(T);
127 static ABIArgInfo getIgnore() {
128 return ABIArgInfo(Ignore);
130 static ABIArgInfo getIndirect(CharUnits Alignment, bool ByVal = true,
131 bool Realign = false,
132 llvm::Type *Padding = nullptr) {
133 auto AI = ABIArgInfo(Indirect);
134 AI.setIndirectAlign(Alignment);
135 AI.setIndirectByVal(ByVal);
136 AI.setIndirectRealign(Realign);
137 AI.setSRetAfterThis(false);
138 AI.setPaddingType(Padding);
141 static ABIArgInfo getIndirectInReg(CharUnits Alignment, bool ByVal = true,
142 bool Realign = false) {
143 auto AI = getIndirect(Alignment, ByVal, Realign);
147 static ABIArgInfo getInAlloca(unsigned FieldIndex) {
148 auto AI = ABIArgInfo(InAlloca);
149 AI.setInAllocaFieldIndex(FieldIndex);
152 static ABIArgInfo getExpand() {
153 return ABIArgInfo(Expand);
155 static ABIArgInfo getExpandWithPadding(bool PaddingInReg,
156 llvm::Type *Padding) {
157 auto AI = getExpand();
158 AI.setPaddingInReg(PaddingInReg);
159 AI.setPaddingType(Padding);
163 Kind getKind() const { return TheKind; }
164 bool isDirect() const { return TheKind == Direct; }
165 bool isInAlloca() const { return TheKind == InAlloca; }
166 bool isExtend() const { return TheKind == Extend; }
167 bool isIgnore() const { return TheKind == Ignore; }
168 bool isIndirect() const { return TheKind == Indirect; }
169 bool isExpand() const { return TheKind == Expand; }
171 bool canHaveCoerceToType() const { return isDirect() || isExtend(); }
173 // Direct/Extend accessors
174 unsigned getDirectOffset() const {
175 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
178 void setDirectOffset(unsigned Offset) {
179 assert((isDirect() || isExtend()) && "Not a direct or extend kind");
180 DirectOffset = Offset;
183 llvm::Type *getPaddingType() const { return PaddingType; }
185 void setPaddingType(llvm::Type *T) { PaddingType = T; }
187 bool getPaddingInReg() const {
190 void setPaddingInReg(bool PIR) {
194 llvm::Type *getCoerceToType() const {
195 assert(canHaveCoerceToType() && "Invalid kind!");
199 void setCoerceToType(llvm::Type *T) {
200 assert(canHaveCoerceToType() && "Invalid kind!");
204 bool getInReg() const {
205 assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
209 void setInReg(bool IR) {
210 assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
214 // Indirect accessors
215 CharUnits getIndirectAlign() const {
216 assert(isIndirect() && "Invalid kind!");
217 return CharUnits::fromQuantity(IndirectAlign);
219 void setIndirectAlign(CharUnits IA) {
220 assert(isIndirect() && "Invalid kind!");
221 IndirectAlign = IA.getQuantity();
224 bool getIndirectByVal() const {
225 assert(isIndirect() && "Invalid kind!");
226 return IndirectByVal;
228 void setIndirectByVal(bool IBV) {
229 assert(isIndirect() && "Invalid kind!");
233 bool getIndirectRealign() const {
234 assert(isIndirect() && "Invalid kind!");
235 return IndirectRealign;
237 void setIndirectRealign(bool IR) {
238 assert(isIndirect() && "Invalid kind!");
239 IndirectRealign = IR;
242 bool isSRetAfterThis() const {
243 assert(isIndirect() && "Invalid kind!");
244 return SRetAfterThis;
246 void setSRetAfterThis(bool AfterThis) {
247 assert(isIndirect() && "Invalid kind!");
248 SRetAfterThis = AfterThis;
251 unsigned getInAllocaFieldIndex() const {
252 assert(isInAlloca() && "Invalid kind!");
253 return AllocaFieldIndex;
255 void setInAllocaFieldIndex(unsigned FieldIndex) {
256 assert(isInAlloca() && "Invalid kind!");
257 AllocaFieldIndex = FieldIndex;
260 /// \brief Return true if this field of an inalloca struct should be returned
261 /// to implement a struct return calling convention.
262 bool getInAllocaSRet() const {
263 assert(isInAlloca() && "Invalid kind!");
267 void setInAllocaSRet(bool SRet) {
268 assert(isInAlloca() && "Invalid kind!");
272 bool getCanBeFlattened() const {
273 assert(isDirect() && "Invalid kind!");
274 return CanBeFlattened;
277 void setCanBeFlattened(bool Flatten) {
278 assert(isDirect() && "Invalid kind!");
279 CanBeFlattened = Flatten;
285 /// A class for recording the number of arguments that a function
286 /// signature requires.
288 /// The number of required arguments, or ~0 if the signature does
289 /// not permit optional arguments.
290 unsigned NumRequired;
294 RequiredArgs(All_t _) : NumRequired(~0U) {}
295 explicit RequiredArgs(unsigned n) : NumRequired(n) {
299 /// Compute the arguments required by the given formal prototype,
300 /// given that there may be some additional, non-formal arguments
302 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
303 unsigned additional) {
304 if (!prototype->isVariadic()) return All;
305 return RequiredArgs(prototype->getNumParams() + additional);
308 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
309 return forPrototypePlus(prototype, 0);
312 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
313 return forPrototype(prototype.getTypePtr());
316 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
317 unsigned additional) {
318 return forPrototypePlus(prototype.getTypePtr(), additional);
321 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
322 unsigned getNumRequiredArgs() const {
323 assert(allowsOptionalArgs());
327 unsigned getOpaqueData() const { return NumRequired; }
328 static RequiredArgs getFromOpaqueData(unsigned value) {
329 if (value == ~0U) return All;
330 return RequiredArgs(value);
334 /// CGFunctionInfo - Class to encapsulate the information about a
335 /// function definition.
336 class CGFunctionInfo : public llvm::FoldingSetNode {
342 /// The LLVM::CallingConv to use for this function (as specified by the
344 unsigned CallingConvention : 8;
346 /// The LLVM::CallingConv to actually use for this function, which may
347 /// depend on the ABI.
348 unsigned EffectiveCallingConvention : 8;
350 /// The clang::CallingConv that this was originally created with.
351 unsigned ASTCallingConvention : 8;
353 /// Whether this is an instance method.
354 unsigned InstanceMethod : 1;
356 /// Whether this is a chain call.
357 unsigned ChainCall : 1;
359 /// Whether this function is noreturn.
360 unsigned NoReturn : 1;
362 /// Whether this function is returns-retained.
363 unsigned ReturnsRetained : 1;
365 /// How many arguments to pass inreg.
366 unsigned HasRegParm : 1;
367 unsigned RegParm : 3;
369 RequiredArgs Required;
371 /// The struct representing all arguments passed in memory. Only used when
372 /// passing non-trivial types with inalloca. Not part of the profile.
373 llvm::StructType *ArgStruct;
374 unsigned ArgStructAlign;
377 ArgInfo *getArgsBuffer() {
378 return reinterpret_cast<ArgInfo*>(this+1);
380 const ArgInfo *getArgsBuffer() const {
381 return reinterpret_cast<const ArgInfo*>(this + 1);
384 CGFunctionInfo() : Required(RequiredArgs::All) {}
387 static CGFunctionInfo *create(unsigned llvmCC,
390 const FunctionType::ExtInfo &extInfo,
391 CanQualType resultType,
392 ArrayRef<CanQualType> argTypes,
393 RequiredArgs required);
395 typedef const ArgInfo *const_arg_iterator;
396 typedef ArgInfo *arg_iterator;
398 typedef llvm::iterator_range<arg_iterator> arg_range;
399 typedef llvm::iterator_range<const_arg_iterator> arg_const_range;
401 arg_range arguments() { return arg_range(arg_begin(), arg_end()); }
402 arg_const_range arguments() const {
403 return arg_const_range(arg_begin(), arg_end());
406 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
407 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
408 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
409 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
411 unsigned arg_size() const { return NumArgs; }
413 bool isVariadic() const { return Required.allowsOptionalArgs(); }
414 RequiredArgs getRequiredArgs() const { return Required; }
415 unsigned getNumRequiredArgs() const {
416 return isVariadic() ? getRequiredArgs().getNumRequiredArgs() : arg_size();
419 bool isInstanceMethod() const { return InstanceMethod; }
421 bool isChainCall() const { return ChainCall; }
423 bool isNoReturn() const { return NoReturn; }
425 /// In ARC, whether this function retains its return value. This
426 /// is not always reliable for call sites.
427 bool isReturnsRetained() const { return ReturnsRetained; }
429 /// getASTCallingConvention() - Return the AST-specified calling
431 CallingConv getASTCallingConvention() const {
432 return CallingConv(ASTCallingConvention);
435 /// getCallingConvention - Return the user specified calling
436 /// convention, which has been translated into an LLVM CC.
437 unsigned getCallingConvention() const { return CallingConvention; }
439 /// getEffectiveCallingConvention - Return the actual calling convention to
440 /// use, which may depend on the ABI.
441 unsigned getEffectiveCallingConvention() const {
442 return EffectiveCallingConvention;
444 void setEffectiveCallingConvention(unsigned Value) {
445 EffectiveCallingConvention = Value;
448 bool getHasRegParm() const { return HasRegParm; }
449 unsigned getRegParm() const { return RegParm; }
451 FunctionType::ExtInfo getExtInfo() const {
452 return FunctionType::ExtInfo(isNoReturn(),
453 getHasRegParm(), getRegParm(),
454 getASTCallingConvention(),
455 isReturnsRetained());
458 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
460 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
461 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
463 /// \brief Return true if this function uses inalloca arguments.
464 bool usesInAlloca() const { return ArgStruct; }
466 /// \brief Get the struct type used to represent all the arguments in memory.
467 llvm::StructType *getArgStruct() const { return ArgStruct; }
468 CharUnits getArgStructAlignment() const {
469 return CharUnits::fromQuantity(ArgStructAlign);
471 void setArgStruct(llvm::StructType *Ty, CharUnits Align) {
473 ArgStructAlign = Align.getQuantity();
476 void Profile(llvm::FoldingSetNodeID &ID) {
477 ID.AddInteger(getASTCallingConvention());
478 ID.AddBoolean(InstanceMethod);
479 ID.AddBoolean(ChainCall);
480 ID.AddBoolean(NoReturn);
481 ID.AddBoolean(ReturnsRetained);
482 ID.AddBoolean(HasRegParm);
483 ID.AddInteger(RegParm);
484 ID.AddInteger(Required.getOpaqueData());
485 getReturnType().Profile(ID);
486 for (const auto &I : arguments())
489 static void Profile(llvm::FoldingSetNodeID &ID,
492 const FunctionType::ExtInfo &info,
493 RequiredArgs required,
494 CanQualType resultType,
495 ArrayRef<CanQualType> argTypes) {
496 ID.AddInteger(info.getCC());
497 ID.AddBoolean(InstanceMethod);
498 ID.AddBoolean(ChainCall);
499 ID.AddBoolean(info.getNoReturn());
500 ID.AddBoolean(info.getProducesResult());
501 ID.AddBoolean(info.getHasRegParm());
502 ID.AddInteger(info.getRegParm());
503 ID.AddInteger(required.getOpaqueData());
504 resultType.Profile(ID);
505 for (ArrayRef<CanQualType>::iterator
506 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
512 /// CGCalleeInfo - Class to encapsulate the information about a callee to be
513 /// used during the generation of call/invoke instructions.
515 /// \brief The function proto type of the callee.
516 const FunctionProtoType *CalleeProtoTy;
517 /// \brief The function declaration of the callee.
518 const Decl *CalleeDecl;
521 explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
522 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
523 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
524 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
525 : CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
526 CGCalleeInfo(const Decl *calleeDecl)
527 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
529 const FunctionProtoType *getCalleeFunctionProtoType() {
530 return CalleeProtoTy;
532 const Decl *getCalleeDecl() { return CalleeDecl; }
535 } // end namespace CodeGen
536 } // end namespace clang