xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/ABIInfoImpl.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 //===- ABIInfoImpl.h --------------------------------------------*- 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 #ifndef LLVM_CLANG_LIB_CODEGEN_ABIINFOIMPL_H
10 #define LLVM_CLANG_LIB_CODEGEN_ABIINFOIMPL_H
11 
12 #include "ABIInfo.h"
13 #include "CGCXXABI.h"
14 
15 namespace clang::CodeGen {
16 
17 /// DefaultABIInfo - The default implementation for ABI specific
18 /// details. This implementation provides information which results in
19 /// self-consistent and sensible LLVM IR generation, but does not
20 /// conform to any particular ABI.
21 class DefaultABIInfo : public ABIInfo {
22 public:
23   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
24 
25   virtual ~DefaultABIInfo();
26 
27   ABIArgInfo classifyReturnType(QualType RetTy) const;
28   ABIArgInfo classifyArgumentType(QualType RetTy) const;
29 
30   void computeInfo(CGFunctionInfo &FI) const override;
31 
32   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
33                     QualType Ty) const override;
34 };
35 
36 // Helper for coercing an aggregate argument or return value into an integer
37 // array of the same size (including padding) and alignment.  This alternate
38 // coercion happens only for the RenderScript ABI and can be removed after
39 // runtimes that rely on it are no longer supported.
40 //
41 // RenderScript assumes that the size of the argument / return value in the IR
42 // is the same as the size of the corresponding qualified type. This helper
43 // coerces the aggregate type into an array of the same size (including
44 // padding).  This coercion is used in lieu of expansion of struct members or
45 // other canonical coercions that return a coerced-type of larger size.
46 //
47 // Ty          - The argument / return value type
48 // Context     - The associated ASTContext
49 // LLVMContext - The associated LLVMContext
50 ABIArgInfo coerceToIntArray(QualType Ty, ASTContext &Context,
51                             llvm::LLVMContext &LLVMContext);
52 
53 void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, llvm::Value *Array,
54                         llvm::Value *Value, unsigned FirstIndex,
55                         unsigned LastIndex);
56 
57 bool isAggregateTypeForABI(QualType T);
58 
59 llvm::Type *getVAListElementType(CodeGenFunction &CGF);
60 
61 CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI);
62 
63 CGCXXABI::RecordArgABI getRecordArgABI(QualType T, CGCXXABI &CXXABI);
64 
65 bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
66                         const ABIInfo &Info);
67 
68 /// Pass transparent unions as if they were the type of the first element. Sema
69 /// should ensure that all elements of the union have the same "machine type".
70 QualType useFirstFieldIfTransparentUnion(QualType Ty);
71 
72 // Dynamically round a pointer up to a multiple of the given alignment.
73 llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
74                                            llvm::Value *Ptr, CharUnits Align);
75 
76 /// Emit va_arg for a platform using the common void* representation,
77 /// where arguments are simply emitted in an array of slots on the stack.
78 ///
79 /// This version implements the core direct-value passing rules.
80 ///
81 /// \param SlotSize - The size and alignment of a stack slot.
82 ///   Each argument will be allocated to a multiple of this number of
83 ///   slots, and all the slots will be aligned to this value.
84 /// \param AllowHigherAlign - The slot alignment is not a cap;
85 ///   an argument type with an alignment greater than the slot size
86 ///   will be emitted on a higher-alignment address, potentially
87 ///   leaving one or more empty slots behind as padding.  If this
88 ///   is false, the returned address might be less-aligned than
89 ///   DirectAlign.
90 /// \param ForceRightAdjust - Default is false. On big-endian platform and
91 ///   if the argument is smaller than a slot, set this flag will force
92 ///   right-adjust the argument in its slot irrespective of the type.
93 Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, Address VAListAddr,
94                                llvm::Type *DirectTy, CharUnits DirectSize,
95                                CharUnits DirectAlign, CharUnits SlotSize,
96                                bool AllowHigherAlign,
97                                bool ForceRightAdjust = false);
98 
99 /// Emit va_arg for a platform using the common void* representation,
100 /// where arguments are simply emitted in an array of slots on the stack.
101 ///
102 /// \param IsIndirect - Values of this type are passed indirectly.
103 /// \param ValueInfo - The size and alignment of this type, generally
104 ///   computed with getContext().getTypeInfoInChars(ValueTy).
105 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
106 ///   Each argument will be allocated to a multiple of this number of
107 ///   slots, and all the slots will be aligned to this value.
108 /// \param AllowHigherAlign - The slot alignment is not a cap;
109 ///   an argument type with an alignment greater than the slot size
110 ///   will be emitted on a higher-alignment address, potentially
111 ///   leaving one or more empty slots behind as padding.
112 /// \param ForceRightAdjust - Default is false. On big-endian platform and
113 ///   if the argument is smaller than a slot, set this flag will force
114 ///   right-adjust the argument in its slot irrespective of the type.
115 Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
116                          QualType ValueTy, bool IsIndirect,
117                          TypeInfoChars ValueInfo, CharUnits SlotSizeAndAlign,
118                          bool AllowHigherAlign, bool ForceRightAdjust = false);
119 
120 Address emitMergePHI(CodeGenFunction &CGF, Address Addr1,
121                      llvm::BasicBlock *Block1, Address Addr2,
122                      llvm::BasicBlock *Block2, const llvm::Twine &Name = "");
123 
124 /// isEmptyField - Return true iff a the field is "empty", that is it
125 /// is an unnamed bit-field or an (array of) empty record(s). If
126 /// AsIfNoUniqueAddr is true, then C++ record fields are considered empty if
127 /// the [[no_unique_address]] attribute would have made them empty.
128 bool isEmptyField(ASTContext &Context, const FieldDecl *FD, bool AllowArrays,
129                   bool AsIfNoUniqueAddr = false);
130 
131 /// isEmptyRecord - Return true iff a structure contains only empty
132 /// fields. Note that a structure with a flexible array member is not
133 /// considered empty. If AsIfNoUniqueAddr is true, then C++ record fields are
134 /// considered empty if the [[no_unique_address]] attribute would have made
135 /// them empty.
136 bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays,
137                    bool AsIfNoUniqueAddr = false);
138 
139 /// isSingleElementStruct - Determine if a structure is a "single
140 /// element struct", i.e. it has exactly one non-empty field or
141 /// exactly one field which is itself a single element
142 /// struct. Structures with flexible array members are never
143 /// considered single element structs.
144 ///
145 /// \return The field declaration for the single non-empty field, if
146 /// it exists.
147 const Type *isSingleElementStruct(QualType T, ASTContext &Context);
148 
149 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
150                        const ABIArgInfo &AI);
151 
152 bool isSIMDVectorType(ASTContext &Context, QualType Ty);
153 
154 bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty);
155 
156 } // namespace clang::CodeGen
157 
158 #endif // LLVM_CLANG_LIB_CODEGEN_ABIINFOIMPL_H
159