/src/llvm-project/clang/include/clang/CodeGen/CGFunctionInfo.h
Line | Count | Source (jump to first uncovered line) |
1 | | //==-- CGFunctionInfo.h - Representation of function argument/return types -==// |
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 CGFunctionInfo and associated types used in representing the |
10 | | // LLVM source types and ABI-coerced types for function arguments and |
11 | | // return values. |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #ifndef LLVM_CLANG_CODEGEN_CGFUNCTIONINFO_H |
16 | | #define LLVM_CLANG_CODEGEN_CGFUNCTIONINFO_H |
17 | | |
18 | | #include "clang/AST/CanonicalType.h" |
19 | | #include "clang/AST/CharUnits.h" |
20 | | #include "clang/AST/Decl.h" |
21 | | #include "clang/AST/Type.h" |
22 | | #include "llvm/IR/DerivedTypes.h" |
23 | | #include "llvm/ADT/FoldingSet.h" |
24 | | #include "llvm/Support/TrailingObjects.h" |
25 | | #include <cassert> |
26 | | |
27 | | namespace clang { |
28 | | namespace CodeGen { |
29 | | |
30 | | /// ABIArgInfo - Helper class to encapsulate information about how a |
31 | | /// specific C type should be passed to or returned from a function. |
32 | | class ABIArgInfo { |
33 | | public: |
34 | | enum Kind : uint8_t { |
35 | | /// Direct - Pass the argument directly using the normal converted LLVM |
36 | | /// type, or by coercing to another specified type stored in |
37 | | /// 'CoerceToType'). If an offset is specified (in UIntData), then the |
38 | | /// argument passed is offset by some number of bytes in the memory |
39 | | /// representation. A dummy argument is emitted before the real argument |
40 | | /// if the specified type stored in "PaddingType" is not zero. |
41 | | Direct, |
42 | | |
43 | | /// Extend - Valid only for integer argument types. Same as 'direct' |
44 | | /// but also emit a zero/sign extension attribute. |
45 | | Extend, |
46 | | |
47 | | /// Indirect - Pass the argument indirectly via a hidden pointer with the |
48 | | /// specified alignment (0 indicates default alignment) and address space. |
49 | | Indirect, |
50 | | |
51 | | /// IndirectAliased - Similar to Indirect, but the pointer may be to an |
52 | | /// object that is otherwise referenced. The object is known to not be |
53 | | /// modified through any other references for the duration of the call, and |
54 | | /// the callee must not itself modify the object. Because C allows |
55 | | /// parameter variables to be modified and guarantees that they have unique |
56 | | /// addresses, the callee must defensively copy the object into a local |
57 | | /// variable if it might be modified or its address might be compared. |
58 | | /// Since those are uncommon, in principle this convention allows programs |
59 | | /// to avoid copies in more situations. However, it may introduce *extra* |
60 | | /// copies if the callee fails to prove that a copy is unnecessary and the |
61 | | /// caller naturally produces an unaliased object for the argument. |
62 | | IndirectAliased, |
63 | | |
64 | | /// Ignore - Ignore the argument (treat as void). Useful for void and |
65 | | /// empty structs. |
66 | | Ignore, |
67 | | |
68 | | /// Expand - Only valid for aggregate argument types. The structure should |
69 | | /// be expanded into consecutive arguments for its constituent fields. |
70 | | /// Currently expand is only allowed on structures whose fields |
71 | | /// are all scalar types or are themselves expandable types. |
72 | | Expand, |
73 | | |
74 | | /// CoerceAndExpand - Only valid for aggregate argument types. The |
75 | | /// structure should be expanded into consecutive arguments corresponding |
76 | | /// to the non-array elements of the type stored in CoerceToType. |
77 | | /// Array elements in the type are assumed to be padding and skipped. |
78 | | CoerceAndExpand, |
79 | | |
80 | | /// InAlloca - Pass the argument directly using the LLVM inalloca attribute. |
81 | | /// This is similar to indirect with byval, except it only applies to |
82 | | /// arguments stored in memory and forbids any implicit copies. When |
83 | | /// applied to a return type, it means the value is returned indirectly via |
84 | | /// an implicit sret parameter stored in the argument struct. |
85 | | InAlloca, |
86 | | KindFirst = Direct, |
87 | | KindLast = InAlloca |
88 | | }; |
89 | | |
90 | | private: |
91 | | llvm::Type *TypeData; // canHaveCoerceToType() |
92 | | union { |
93 | | llvm::Type *PaddingType; // canHavePaddingType() |
94 | | llvm::Type *UnpaddedCoerceAndExpandType; // isCoerceAndExpand() |
95 | | }; |
96 | | struct DirectAttrInfo { |
97 | | unsigned Offset; |
98 | | unsigned Align; |
99 | | }; |
100 | | struct IndirectAttrInfo { |
101 | | unsigned Align; |
102 | | unsigned AddrSpace; |
103 | | }; |
104 | | union { |
105 | | DirectAttrInfo DirectAttr; // isDirect() || isExtend() |
106 | | IndirectAttrInfo IndirectAttr; // isIndirect() |
107 | | unsigned AllocaFieldIndex; // isInAlloca() |
108 | | }; |
109 | | Kind TheKind; |
110 | | bool PaddingInReg : 1; |
111 | | bool InAllocaSRet : 1; // isInAlloca() |
112 | | bool InAllocaIndirect : 1;// isInAlloca() |
113 | | bool IndirectByVal : 1; // isIndirect() |
114 | | bool IndirectRealign : 1; // isIndirect() |
115 | | bool SRetAfterThis : 1; // isIndirect() |
116 | | bool InReg : 1; // isDirect() || isExtend() || isIndirect() |
117 | | bool CanBeFlattened: 1; // isDirect() |
118 | | bool SignExt : 1; // isExtend() |
119 | | |
120 | 0 | bool canHavePaddingType() const { |
121 | 0 | return isDirect() || isExtend() || isIndirect() || isIndirectAliased() || |
122 | 0 | isExpand(); |
123 | 0 | } |
124 | 0 | void setPaddingType(llvm::Type *T) { |
125 | 0 | assert(canHavePaddingType()); |
126 | 0 | PaddingType = T; |
127 | 0 | } |
128 | | |
129 | 0 | void setUnpaddedCoerceToType(llvm::Type *T) { |
130 | 0 | assert(isCoerceAndExpand()); |
131 | 0 | UnpaddedCoerceAndExpandType = T; |
132 | 0 | } |
133 | | |
134 | | public: |
135 | | ABIArgInfo(Kind K = Direct) |
136 | | : TypeData(nullptr), PaddingType(nullptr), DirectAttr{0, 0}, TheKind(K), |
137 | | PaddingInReg(false), InAllocaSRet(false), |
138 | | InAllocaIndirect(false), IndirectByVal(false), IndirectRealign(false), |
139 | | SRetAfterThis(false), InReg(false), CanBeFlattened(false), |
140 | 0 | SignExt(false) {} |
141 | | |
142 | | static ABIArgInfo getDirect(llvm::Type *T = nullptr, unsigned Offset = 0, |
143 | | llvm::Type *Padding = nullptr, |
144 | 0 | bool CanBeFlattened = true, unsigned Align = 0) { |
145 | 0 | auto AI = ABIArgInfo(Direct); |
146 | 0 | AI.setCoerceToType(T); |
147 | 0 | AI.setPaddingType(Padding); |
148 | 0 | AI.setDirectOffset(Offset); |
149 | 0 | AI.setDirectAlign(Align); |
150 | 0 | AI.setCanBeFlattened(CanBeFlattened); |
151 | 0 | return AI; |
152 | 0 | } |
153 | 0 | static ABIArgInfo getDirectInReg(llvm::Type *T = nullptr) { |
154 | 0 | auto AI = getDirect(T); |
155 | 0 | AI.setInReg(true); |
156 | 0 | return AI; |
157 | 0 | } |
158 | | |
159 | 0 | static ABIArgInfo getSignExtend(QualType Ty, llvm::Type *T = nullptr) { |
160 | 0 | assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType"); |
161 | 0 | auto AI = ABIArgInfo(Extend); |
162 | 0 | AI.setCoerceToType(T); |
163 | 0 | AI.setPaddingType(nullptr); |
164 | 0 | AI.setDirectOffset(0); |
165 | 0 | AI.setDirectAlign(0); |
166 | 0 | AI.setSignExt(true); |
167 | 0 | return AI; |
168 | 0 | } |
169 | | |
170 | 0 | static ABIArgInfo getZeroExtend(QualType Ty, llvm::Type *T = nullptr) { |
171 | 0 | assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType"); |
172 | 0 | auto AI = ABIArgInfo(Extend); |
173 | 0 | AI.setCoerceToType(T); |
174 | 0 | AI.setPaddingType(nullptr); |
175 | 0 | AI.setDirectOffset(0); |
176 | 0 | AI.setDirectAlign(0); |
177 | 0 | AI.setSignExt(false); |
178 | 0 | return AI; |
179 | 0 | } |
180 | | |
181 | | // ABIArgInfo will record the argument as being extended based on the sign |
182 | | // of its type. |
183 | 0 | static ABIArgInfo getExtend(QualType Ty, llvm::Type *T = nullptr) { |
184 | 0 | assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType"); |
185 | 0 | if (Ty->hasSignedIntegerRepresentation()) |
186 | 0 | return getSignExtend(Ty, T); |
187 | 0 | return getZeroExtend(Ty, T); |
188 | 0 | } |
189 | | |
190 | 0 | static ABIArgInfo getExtendInReg(QualType Ty, llvm::Type *T = nullptr) { |
191 | 0 | auto AI = getExtend(Ty, T); |
192 | 0 | AI.setInReg(true); |
193 | 0 | return AI; |
194 | 0 | } |
195 | 0 | static ABIArgInfo getIgnore() { |
196 | 0 | return ABIArgInfo(Ignore); |
197 | 0 | } |
198 | | static ABIArgInfo getIndirect(CharUnits Alignment, bool ByVal = true, |
199 | | bool Realign = false, |
200 | 0 | llvm::Type *Padding = nullptr) { |
201 | 0 | auto AI = ABIArgInfo(Indirect); |
202 | 0 | AI.setIndirectAlign(Alignment); |
203 | 0 | AI.setIndirectByVal(ByVal); |
204 | 0 | AI.setIndirectRealign(Realign); |
205 | 0 | AI.setSRetAfterThis(false); |
206 | 0 | AI.setPaddingType(Padding); |
207 | 0 | return AI; |
208 | 0 | } |
209 | | |
210 | | /// Pass this in memory using the IR byref attribute. |
211 | | static ABIArgInfo getIndirectAliased(CharUnits Alignment, unsigned AddrSpace, |
212 | | bool Realign = false, |
213 | 0 | llvm::Type *Padding = nullptr) { |
214 | 0 | auto AI = ABIArgInfo(IndirectAliased); |
215 | 0 | AI.setIndirectAlign(Alignment); |
216 | 0 | AI.setIndirectRealign(Realign); |
217 | 0 | AI.setPaddingType(Padding); |
218 | 0 | AI.setIndirectAddrSpace(AddrSpace); |
219 | 0 | return AI; |
220 | 0 | } |
221 | | |
222 | | static ABIArgInfo getIndirectInReg(CharUnits Alignment, bool ByVal = true, |
223 | 0 | bool Realign = false) { |
224 | 0 | auto AI = getIndirect(Alignment, ByVal, Realign); |
225 | 0 | AI.setInReg(true); |
226 | 0 | return AI; |
227 | 0 | } |
228 | 0 | static ABIArgInfo getInAlloca(unsigned FieldIndex, bool Indirect = false) { |
229 | 0 | auto AI = ABIArgInfo(InAlloca); |
230 | 0 | AI.setInAllocaFieldIndex(FieldIndex); |
231 | 0 | AI.setInAllocaIndirect(Indirect); |
232 | 0 | return AI; |
233 | 0 | } |
234 | 0 | static ABIArgInfo getExpand() { |
235 | 0 | auto AI = ABIArgInfo(Expand); |
236 | 0 | AI.setPaddingType(nullptr); |
237 | 0 | return AI; |
238 | 0 | } |
239 | | static ABIArgInfo getExpandWithPadding(bool PaddingInReg, |
240 | 0 | llvm::Type *Padding) { |
241 | 0 | auto AI = getExpand(); |
242 | 0 | AI.setPaddingInReg(PaddingInReg); |
243 | 0 | AI.setPaddingType(Padding); |
244 | 0 | return AI; |
245 | 0 | } |
246 | | |
247 | | /// \param unpaddedCoerceToType The coerce-to type with padding elements |
248 | | /// removed, canonicalized to a single element if it would otherwise |
249 | | /// have exactly one element. |
250 | | static ABIArgInfo getCoerceAndExpand(llvm::StructType *coerceToType, |
251 | 0 | llvm::Type *unpaddedCoerceToType) { |
252 | 0 | #ifndef NDEBUG |
253 | | // Check that unpaddedCoerceToType has roughly the right shape. |
254 | | |
255 | | // Assert that we only have a struct type if there are multiple elements. |
256 | 0 | auto unpaddedStruct = dyn_cast<llvm::StructType>(unpaddedCoerceToType); |
257 | 0 | assert(!unpaddedStruct || unpaddedStruct->getNumElements() != 1); |
258 | | |
259 | | // Assert that all the non-padding elements have a corresponding element |
260 | | // in the unpadded type. |
261 | 0 | unsigned unpaddedIndex = 0; |
262 | 0 | for (auto eltType : coerceToType->elements()) { |
263 | 0 | if (isPaddingForCoerceAndExpand(eltType)) continue; |
264 | 0 | if (unpaddedStruct) { |
265 | 0 | assert(unpaddedStruct->getElementType(unpaddedIndex) == eltType); |
266 | 0 | } else { |
267 | 0 | assert(unpaddedIndex == 0 && unpaddedCoerceToType == eltType); |
268 | 0 | } |
269 | 0 | unpaddedIndex++; |
270 | 0 | } |
271 | | |
272 | | // Assert that there aren't extra elements in the unpadded type. |
273 | 0 | if (unpaddedStruct) { |
274 | 0 | assert(unpaddedStruct->getNumElements() == unpaddedIndex); |
275 | 0 | } else { |
276 | 0 | assert(unpaddedIndex == 1); |
277 | 0 | } |
278 | 0 | #endif |
279 | | |
280 | 0 | auto AI = ABIArgInfo(CoerceAndExpand); |
281 | 0 | AI.setCoerceToType(coerceToType); |
282 | 0 | AI.setUnpaddedCoerceToType(unpaddedCoerceToType); |
283 | 0 | return AI; |
284 | 0 | } |
285 | | |
286 | 0 | static bool isPaddingForCoerceAndExpand(llvm::Type *eltType) { |
287 | 0 | if (eltType->isArrayTy()) { |
288 | 0 | assert(eltType->getArrayElementType()->isIntegerTy(8)); |
289 | 0 | return true; |
290 | 0 | } else { |
291 | 0 | return false; |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | 0 | Kind getKind() const { return TheKind; } |
296 | 0 | bool isDirect() const { return TheKind == Direct; } |
297 | 0 | bool isInAlloca() const { return TheKind == InAlloca; } |
298 | 0 | bool isExtend() const { return TheKind == Extend; } |
299 | 0 | bool isIgnore() const { return TheKind == Ignore; } |
300 | 0 | bool isIndirect() const { return TheKind == Indirect; } |
301 | 0 | bool isIndirectAliased() const { return TheKind == IndirectAliased; } |
302 | 0 | bool isExpand() const { return TheKind == Expand; } |
303 | 0 | bool isCoerceAndExpand() const { return TheKind == CoerceAndExpand; } |
304 | | |
305 | 0 | bool canHaveCoerceToType() const { |
306 | 0 | return isDirect() || isExtend() || isCoerceAndExpand(); |
307 | 0 | } |
308 | | |
309 | | // Direct/Extend accessors |
310 | 0 | unsigned getDirectOffset() const { |
311 | 0 | assert((isDirect() || isExtend()) && "Not a direct or extend kind"); |
312 | 0 | return DirectAttr.Offset; |
313 | 0 | } |
314 | 0 | void setDirectOffset(unsigned Offset) { |
315 | 0 | assert((isDirect() || isExtend()) && "Not a direct or extend kind"); |
316 | 0 | DirectAttr.Offset = Offset; |
317 | 0 | } |
318 | | |
319 | 0 | unsigned getDirectAlign() const { |
320 | 0 | assert((isDirect() || isExtend()) && "Not a direct or extend kind"); |
321 | 0 | return DirectAttr.Align; |
322 | 0 | } |
323 | 0 | void setDirectAlign(unsigned Align) { |
324 | 0 | assert((isDirect() || isExtend()) && "Not a direct or extend kind"); |
325 | 0 | DirectAttr.Align = Align; |
326 | 0 | } |
327 | | |
328 | 0 | bool isSignExt() const { |
329 | 0 | assert(isExtend() && "Invalid kind!"); |
330 | 0 | return SignExt; |
331 | 0 | } |
332 | 0 | void setSignExt(bool SExt) { |
333 | 0 | assert(isExtend() && "Invalid kind!"); |
334 | 0 | SignExt = SExt; |
335 | 0 | } |
336 | | |
337 | 0 | llvm::Type *getPaddingType() const { |
338 | 0 | return (canHavePaddingType() ? PaddingType : nullptr); |
339 | 0 | } |
340 | | |
341 | 0 | bool getPaddingInReg() const { |
342 | 0 | return PaddingInReg; |
343 | 0 | } |
344 | 0 | void setPaddingInReg(bool PIR) { |
345 | 0 | PaddingInReg = PIR; |
346 | 0 | } |
347 | | |
348 | 0 | llvm::Type *getCoerceToType() const { |
349 | 0 | assert(canHaveCoerceToType() && "Invalid kind!"); |
350 | 0 | return TypeData; |
351 | 0 | } |
352 | | |
353 | 0 | void setCoerceToType(llvm::Type *T) { |
354 | 0 | assert(canHaveCoerceToType() && "Invalid kind!"); |
355 | 0 | TypeData = T; |
356 | 0 | } |
357 | | |
358 | 0 | llvm::StructType *getCoerceAndExpandType() const { |
359 | 0 | assert(isCoerceAndExpand()); |
360 | 0 | return cast<llvm::StructType>(TypeData); |
361 | 0 | } |
362 | | |
363 | 0 | llvm::Type *getUnpaddedCoerceAndExpandType() const { |
364 | 0 | assert(isCoerceAndExpand()); |
365 | 0 | return UnpaddedCoerceAndExpandType; |
366 | 0 | } |
367 | | |
368 | 0 | ArrayRef<llvm::Type *>getCoerceAndExpandTypeSequence() const { |
369 | 0 | assert(isCoerceAndExpand()); |
370 | 0 | if (auto structTy = |
371 | 0 | dyn_cast<llvm::StructType>(UnpaddedCoerceAndExpandType)) { |
372 | 0 | return structTy->elements(); |
373 | 0 | } else { |
374 | 0 | return llvm::ArrayRef(&UnpaddedCoerceAndExpandType, 1); |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | 0 | bool getInReg() const { |
379 | 0 | assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!"); |
380 | 0 | return InReg; |
381 | 0 | } |
382 | | |
383 | 0 | void setInReg(bool IR) { |
384 | 0 | assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!"); |
385 | 0 | InReg = IR; |
386 | 0 | } |
387 | | |
388 | | // Indirect accessors |
389 | 0 | CharUnits getIndirectAlign() const { |
390 | 0 | assert((isIndirect() || isIndirectAliased()) && "Invalid kind!"); |
391 | 0 | return CharUnits::fromQuantity(IndirectAttr.Align); |
392 | 0 | } |
393 | 0 | void setIndirectAlign(CharUnits IA) { |
394 | 0 | assert((isIndirect() || isIndirectAliased()) && "Invalid kind!"); |
395 | 0 | IndirectAttr.Align = IA.getQuantity(); |
396 | 0 | } |
397 | | |
398 | 0 | bool getIndirectByVal() const { |
399 | 0 | assert(isIndirect() && "Invalid kind!"); |
400 | 0 | return IndirectByVal; |
401 | 0 | } |
402 | 0 | void setIndirectByVal(bool IBV) { |
403 | 0 | assert(isIndirect() && "Invalid kind!"); |
404 | 0 | IndirectByVal = IBV; |
405 | 0 | } |
406 | | |
407 | 0 | unsigned getIndirectAddrSpace() const { |
408 | 0 | assert(isIndirectAliased() && "Invalid kind!"); |
409 | 0 | return IndirectAttr.AddrSpace; |
410 | 0 | } |
411 | | |
412 | 0 | void setIndirectAddrSpace(unsigned AddrSpace) { |
413 | 0 | assert(isIndirectAliased() && "Invalid kind!"); |
414 | 0 | IndirectAttr.AddrSpace = AddrSpace; |
415 | 0 | } |
416 | | |
417 | 0 | bool getIndirectRealign() const { |
418 | 0 | assert((isIndirect() || isIndirectAliased()) && "Invalid kind!"); |
419 | 0 | return IndirectRealign; |
420 | 0 | } |
421 | 0 | void setIndirectRealign(bool IR) { |
422 | 0 | assert((isIndirect() || isIndirectAliased()) && "Invalid kind!"); |
423 | 0 | IndirectRealign = IR; |
424 | 0 | } |
425 | | |
426 | 0 | bool isSRetAfterThis() const { |
427 | 0 | assert(isIndirect() && "Invalid kind!"); |
428 | 0 | return SRetAfterThis; |
429 | 0 | } |
430 | 0 | void setSRetAfterThis(bool AfterThis) { |
431 | 0 | assert(isIndirect() && "Invalid kind!"); |
432 | 0 | SRetAfterThis = AfterThis; |
433 | 0 | } |
434 | | |
435 | 0 | unsigned getInAllocaFieldIndex() const { |
436 | 0 | assert(isInAlloca() && "Invalid kind!"); |
437 | 0 | return AllocaFieldIndex; |
438 | 0 | } |
439 | 0 | void setInAllocaFieldIndex(unsigned FieldIndex) { |
440 | 0 | assert(isInAlloca() && "Invalid kind!"); |
441 | 0 | AllocaFieldIndex = FieldIndex; |
442 | 0 | } |
443 | | |
444 | 0 | unsigned getInAllocaIndirect() const { |
445 | 0 | assert(isInAlloca() && "Invalid kind!"); |
446 | 0 | return InAllocaIndirect; |
447 | 0 | } |
448 | 0 | void setInAllocaIndirect(bool Indirect) { |
449 | 0 | assert(isInAlloca() && "Invalid kind!"); |
450 | 0 | InAllocaIndirect = Indirect; |
451 | 0 | } |
452 | | |
453 | | /// Return true if this field of an inalloca struct should be returned |
454 | | /// to implement a struct return calling convention. |
455 | 0 | bool getInAllocaSRet() const { |
456 | 0 | assert(isInAlloca() && "Invalid kind!"); |
457 | 0 | return InAllocaSRet; |
458 | 0 | } |
459 | | |
460 | 0 | void setInAllocaSRet(bool SRet) { |
461 | 0 | assert(isInAlloca() && "Invalid kind!"); |
462 | 0 | InAllocaSRet = SRet; |
463 | 0 | } |
464 | | |
465 | 0 | bool getCanBeFlattened() const { |
466 | 0 | assert(isDirect() && "Invalid kind!"); |
467 | 0 | return CanBeFlattened; |
468 | 0 | } |
469 | | |
470 | 0 | void setCanBeFlattened(bool Flatten) { |
471 | 0 | assert(isDirect() && "Invalid kind!"); |
472 | 0 | CanBeFlattened = Flatten; |
473 | 0 | } |
474 | | |
475 | | void dump() const; |
476 | | }; |
477 | | |
478 | | /// A class for recording the number of arguments that a function |
479 | | /// signature requires. |
480 | | class RequiredArgs { |
481 | | /// The number of required arguments, or ~0 if the signature does |
482 | | /// not permit optional arguments. |
483 | | unsigned NumRequired; |
484 | | public: |
485 | | enum All_t { All }; |
486 | | |
487 | 0 | RequiredArgs(All_t _) : NumRequired(~0U) {} |
488 | 0 | explicit RequiredArgs(unsigned n) : NumRequired(n) { |
489 | 0 | assert(n != ~0U); |
490 | 0 | } |
491 | | |
492 | | /// Compute the arguments required by the given formal prototype, |
493 | | /// given that there may be some additional, non-formal arguments |
494 | | /// in play. |
495 | | /// |
496 | | /// If FD is not null, this will consider pass_object_size params in FD. |
497 | | static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype, |
498 | 0 | unsigned additional) { |
499 | 0 | if (!prototype->isVariadic()) return All; |
500 | | |
501 | 0 | if (prototype->hasExtParameterInfos()) |
502 | 0 | additional += llvm::count_if( |
503 | 0 | prototype->getExtParameterInfos(), |
504 | 0 | [](const FunctionProtoType::ExtParameterInfo &ExtInfo) { |
505 | 0 | return ExtInfo.hasPassObjectSize(); |
506 | 0 | }); |
507 | |
|
508 | 0 | return RequiredArgs(prototype->getNumParams() + additional); |
509 | 0 | } |
510 | | |
511 | | static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype, |
512 | 0 | unsigned additional) { |
513 | 0 | return forPrototypePlus(prototype.getTypePtr(), additional); |
514 | 0 | } |
515 | | |
516 | 0 | static RequiredArgs forPrototype(const FunctionProtoType *prototype) { |
517 | 0 | return forPrototypePlus(prototype, 0); |
518 | 0 | } |
519 | | |
520 | 0 | static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) { |
521 | 0 | return forPrototypePlus(prototype.getTypePtr(), 0); |
522 | 0 | } |
523 | | |
524 | 0 | bool allowsOptionalArgs() const { return NumRequired != ~0U; } |
525 | 0 | unsigned getNumRequiredArgs() const { |
526 | 0 | assert(allowsOptionalArgs()); |
527 | 0 | return NumRequired; |
528 | 0 | } |
529 | | |
530 | | /// Return true if the argument at a given index is required. |
531 | 0 | bool isRequiredArg(unsigned argIdx) const { |
532 | 0 | return argIdx == ~0U || argIdx < NumRequired; |
533 | 0 | } |
534 | | |
535 | 0 | unsigned getOpaqueData() const { return NumRequired; } |
536 | 0 | static RequiredArgs getFromOpaqueData(unsigned value) { |
537 | 0 | if (value == ~0U) return All; |
538 | 0 | return RequiredArgs(value); |
539 | 0 | } |
540 | | }; |
541 | | |
542 | | // Implementation detail of CGFunctionInfo, factored out so it can be named |
543 | | // in the TrailingObjects base class of CGFunctionInfo. |
544 | | struct CGFunctionInfoArgInfo { |
545 | | CanQualType type; |
546 | | ABIArgInfo info; |
547 | | }; |
548 | | |
549 | | /// CGFunctionInfo - Class to encapsulate the information about a |
550 | | /// function definition. |
551 | | class CGFunctionInfo final |
552 | | : public llvm::FoldingSetNode, |
553 | | private llvm::TrailingObjects<CGFunctionInfo, CGFunctionInfoArgInfo, |
554 | | FunctionProtoType::ExtParameterInfo> { |
555 | | typedef CGFunctionInfoArgInfo ArgInfo; |
556 | | typedef FunctionProtoType::ExtParameterInfo ExtParameterInfo; |
557 | | |
558 | | /// The LLVM::CallingConv to use for this function (as specified by the |
559 | | /// user). |
560 | | unsigned CallingConvention : 8; |
561 | | |
562 | | /// The LLVM::CallingConv to actually use for this function, which may |
563 | | /// depend on the ABI. |
564 | | unsigned EffectiveCallingConvention : 8; |
565 | | |
566 | | /// The clang::CallingConv that this was originally created with. |
567 | | unsigned ASTCallingConvention : 6; |
568 | | |
569 | | /// Whether this is an instance method. |
570 | | unsigned InstanceMethod : 1; |
571 | | |
572 | | /// Whether this is a chain call. |
573 | | unsigned ChainCall : 1; |
574 | | |
575 | | /// Whether this function is called by forwarding arguments. |
576 | | /// This doesn't support inalloca or varargs. |
577 | | unsigned DelegateCall : 1; |
578 | | |
579 | | /// Whether this function is a CMSE nonsecure call |
580 | | unsigned CmseNSCall : 1; |
581 | | |
582 | | /// Whether this function is noreturn. |
583 | | unsigned NoReturn : 1; |
584 | | |
585 | | /// Whether this function is returns-retained. |
586 | | unsigned ReturnsRetained : 1; |
587 | | |
588 | | /// Whether this function saved caller registers. |
589 | | unsigned NoCallerSavedRegs : 1; |
590 | | |
591 | | /// How many arguments to pass inreg. |
592 | | unsigned HasRegParm : 1; |
593 | | unsigned RegParm : 3; |
594 | | |
595 | | /// Whether this function has nocf_check attribute. |
596 | | unsigned NoCfCheck : 1; |
597 | | |
598 | | /// Log 2 of the maximum vector width. |
599 | | unsigned MaxVectorWidth : 4; |
600 | | |
601 | | RequiredArgs Required; |
602 | | |
603 | | /// The struct representing all arguments passed in memory. Only used when |
604 | | /// passing non-trivial types with inalloca. Not part of the profile. |
605 | | llvm::StructType *ArgStruct; |
606 | | unsigned ArgStructAlign : 31; |
607 | | unsigned HasExtParameterInfos : 1; |
608 | | |
609 | | unsigned NumArgs; |
610 | | |
611 | 0 | ArgInfo *getArgsBuffer() { |
612 | 0 | return getTrailingObjects<ArgInfo>(); |
613 | 0 | } |
614 | 0 | const ArgInfo *getArgsBuffer() const { |
615 | 0 | return getTrailingObjects<ArgInfo>(); |
616 | 0 | } |
617 | | |
618 | 0 | ExtParameterInfo *getExtParameterInfosBuffer() { |
619 | 0 | return getTrailingObjects<ExtParameterInfo>(); |
620 | 0 | } |
621 | 0 | const ExtParameterInfo *getExtParameterInfosBuffer() const{ |
622 | 0 | return getTrailingObjects<ExtParameterInfo>(); |
623 | 0 | } |
624 | | |
625 | 0 | CGFunctionInfo() : Required(RequiredArgs::All) {} |
626 | | |
627 | | public: |
628 | | static CGFunctionInfo * |
629 | | create(unsigned llvmCC, bool instanceMethod, bool chainCall, |
630 | | bool delegateCall, const FunctionType::ExtInfo &extInfo, |
631 | | ArrayRef<ExtParameterInfo> paramInfos, CanQualType resultType, |
632 | | ArrayRef<CanQualType> argTypes, RequiredArgs required); |
633 | 0 | void operator delete(void *p) { ::operator delete(p); } |
634 | | |
635 | | // Friending class TrailingObjects is apparently not good enough for MSVC, |
636 | | // so these have to be public. |
637 | | friend class TrailingObjects; |
638 | 0 | size_t numTrailingObjects(OverloadToken<ArgInfo>) const { |
639 | 0 | return NumArgs + 1; |
640 | 0 | } |
641 | 0 | size_t numTrailingObjects(OverloadToken<ExtParameterInfo>) const { |
642 | 0 | return (HasExtParameterInfos ? NumArgs : 0); |
643 | 0 | } |
644 | | |
645 | | typedef const ArgInfo *const_arg_iterator; |
646 | | typedef ArgInfo *arg_iterator; |
647 | | |
648 | 0 | MutableArrayRef<ArgInfo> arguments() { |
649 | 0 | return MutableArrayRef<ArgInfo>(arg_begin(), NumArgs); |
650 | 0 | } |
651 | 0 | ArrayRef<ArgInfo> arguments() const { |
652 | 0 | return ArrayRef<ArgInfo>(arg_begin(), NumArgs); |
653 | 0 | } |
654 | | |
655 | 0 | const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; } |
656 | 0 | const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; } |
657 | 0 | arg_iterator arg_begin() { return getArgsBuffer() + 1; } |
658 | 0 | arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; } |
659 | | |
660 | 0 | unsigned arg_size() const { return NumArgs; } |
661 | | |
662 | 0 | bool isVariadic() const { return Required.allowsOptionalArgs(); } |
663 | 0 | RequiredArgs getRequiredArgs() const { return Required; } |
664 | 0 | unsigned getNumRequiredArgs() const { |
665 | 0 | return isVariadic() ? getRequiredArgs().getNumRequiredArgs() : arg_size(); |
666 | 0 | } |
667 | | |
668 | 0 | bool isInstanceMethod() const { return InstanceMethod; } |
669 | | |
670 | 0 | bool isChainCall() const { return ChainCall; } |
671 | | |
672 | 0 | bool isDelegateCall() const { return DelegateCall; } |
673 | | |
674 | 0 | bool isCmseNSCall() const { return CmseNSCall; } |
675 | | |
676 | 0 | bool isNoReturn() const { return NoReturn; } |
677 | | |
678 | | /// In ARC, whether this function retains its return value. This |
679 | | /// is not always reliable for call sites. |
680 | 0 | bool isReturnsRetained() const { return ReturnsRetained; } |
681 | | |
682 | | /// Whether this function no longer saves caller registers. |
683 | 0 | bool isNoCallerSavedRegs() const { return NoCallerSavedRegs; } |
684 | | |
685 | | /// Whether this function has nocf_check attribute. |
686 | 0 | bool isNoCfCheck() const { return NoCfCheck; } |
687 | | |
688 | | /// getASTCallingConvention() - Return the AST-specified calling |
689 | | /// convention. |
690 | 0 | CallingConv getASTCallingConvention() const { |
691 | 0 | return CallingConv(ASTCallingConvention); |
692 | 0 | } |
693 | | |
694 | | /// getCallingConvention - Return the user specified calling |
695 | | /// convention, which has been translated into an LLVM CC. |
696 | 0 | unsigned getCallingConvention() const { return CallingConvention; } |
697 | | |
698 | | /// getEffectiveCallingConvention - Return the actual calling convention to |
699 | | /// use, which may depend on the ABI. |
700 | 0 | unsigned getEffectiveCallingConvention() const { |
701 | 0 | return EffectiveCallingConvention; |
702 | 0 | } |
703 | 0 | void setEffectiveCallingConvention(unsigned Value) { |
704 | 0 | EffectiveCallingConvention = Value; |
705 | 0 | } |
706 | | |
707 | 0 | bool getHasRegParm() const { return HasRegParm; } |
708 | 0 | unsigned getRegParm() const { return RegParm; } |
709 | | |
710 | 0 | FunctionType::ExtInfo getExtInfo() const { |
711 | 0 | return FunctionType::ExtInfo(isNoReturn(), getHasRegParm(), getRegParm(), |
712 | 0 | getASTCallingConvention(), isReturnsRetained(), |
713 | 0 | isNoCallerSavedRegs(), isNoCfCheck(), |
714 | 0 | isCmseNSCall()); |
715 | 0 | } |
716 | | |
717 | 0 | CanQualType getReturnType() const { return getArgsBuffer()[0].type; } |
718 | | |
719 | 0 | ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; } |
720 | 0 | const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; } |
721 | | |
722 | 0 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { |
723 | 0 | if (!HasExtParameterInfos) return {}; |
724 | 0 | return llvm::ArrayRef(getExtParameterInfosBuffer(), NumArgs); |
725 | 0 | } |
726 | 0 | ExtParameterInfo getExtParameterInfo(unsigned argIndex) const { |
727 | 0 | assert(argIndex <= NumArgs); |
728 | 0 | if (!HasExtParameterInfos) return ExtParameterInfo(); |
729 | 0 | return getExtParameterInfos()[argIndex]; |
730 | 0 | } |
731 | | |
732 | | /// Return true if this function uses inalloca arguments. |
733 | 0 | bool usesInAlloca() const { return ArgStruct; } |
734 | | |
735 | | /// Get the struct type used to represent all the arguments in memory. |
736 | 0 | llvm::StructType *getArgStruct() const { return ArgStruct; } |
737 | 0 | CharUnits getArgStructAlignment() const { |
738 | 0 | return CharUnits::fromQuantity(ArgStructAlign); |
739 | 0 | } |
740 | 0 | void setArgStruct(llvm::StructType *Ty, CharUnits Align) { |
741 | 0 | ArgStruct = Ty; |
742 | 0 | ArgStructAlign = Align.getQuantity(); |
743 | 0 | } |
744 | | |
745 | | /// Return the maximum vector width in the arguments. |
746 | 0 | unsigned getMaxVectorWidth() const { |
747 | 0 | return MaxVectorWidth ? 1U << (MaxVectorWidth - 1) : 0; |
748 | 0 | } |
749 | | |
750 | | /// Set the maximum vector width in the arguments. |
751 | 0 | void setMaxVectorWidth(unsigned Width) { |
752 | 0 | assert(llvm::isPowerOf2_32(Width) && "Expected power of 2 vector"); |
753 | 0 | MaxVectorWidth = llvm::countr_zero(Width) + 1; |
754 | 0 | } |
755 | | |
756 | 0 | void Profile(llvm::FoldingSetNodeID &ID) { |
757 | 0 | ID.AddInteger(getASTCallingConvention()); |
758 | 0 | ID.AddBoolean(InstanceMethod); |
759 | 0 | ID.AddBoolean(ChainCall); |
760 | 0 | ID.AddBoolean(DelegateCall); |
761 | 0 | ID.AddBoolean(NoReturn); |
762 | 0 | ID.AddBoolean(ReturnsRetained); |
763 | 0 | ID.AddBoolean(NoCallerSavedRegs); |
764 | 0 | ID.AddBoolean(HasRegParm); |
765 | 0 | ID.AddInteger(RegParm); |
766 | 0 | ID.AddBoolean(NoCfCheck); |
767 | 0 | ID.AddBoolean(CmseNSCall); |
768 | 0 | ID.AddInteger(Required.getOpaqueData()); |
769 | 0 | ID.AddBoolean(HasExtParameterInfos); |
770 | 0 | if (HasExtParameterInfos) { |
771 | 0 | for (auto paramInfo : getExtParameterInfos()) |
772 | 0 | ID.AddInteger(paramInfo.getOpaqueValue()); |
773 | 0 | } |
774 | 0 | getReturnType().Profile(ID); |
775 | 0 | for (const auto &I : arguments()) |
776 | 0 | I.type.Profile(ID); |
777 | 0 | } |
778 | | static void Profile(llvm::FoldingSetNodeID &ID, bool InstanceMethod, |
779 | | bool ChainCall, bool IsDelegateCall, |
780 | | const FunctionType::ExtInfo &info, |
781 | | ArrayRef<ExtParameterInfo> paramInfos, |
782 | | RequiredArgs required, CanQualType resultType, |
783 | 0 | ArrayRef<CanQualType> argTypes) { |
784 | 0 | ID.AddInteger(info.getCC()); |
785 | 0 | ID.AddBoolean(InstanceMethod); |
786 | 0 | ID.AddBoolean(ChainCall); |
787 | 0 | ID.AddBoolean(IsDelegateCall); |
788 | 0 | ID.AddBoolean(info.getNoReturn()); |
789 | 0 | ID.AddBoolean(info.getProducesResult()); |
790 | 0 | ID.AddBoolean(info.getNoCallerSavedRegs()); |
791 | 0 | ID.AddBoolean(info.getHasRegParm()); |
792 | 0 | ID.AddInteger(info.getRegParm()); |
793 | 0 | ID.AddBoolean(info.getNoCfCheck()); |
794 | 0 | ID.AddBoolean(info.getCmseNSCall()); |
795 | 0 | ID.AddInteger(required.getOpaqueData()); |
796 | 0 | ID.AddBoolean(!paramInfos.empty()); |
797 | 0 | if (!paramInfos.empty()) { |
798 | 0 | for (auto paramInfo : paramInfos) |
799 | 0 | ID.AddInteger(paramInfo.getOpaqueValue()); |
800 | 0 | } |
801 | 0 | resultType.Profile(ID); |
802 | 0 | for (ArrayRef<CanQualType>::iterator |
803 | 0 | i = argTypes.begin(), e = argTypes.end(); i != e; ++i) { |
804 | 0 | i->Profile(ID); |
805 | 0 | } |
806 | 0 | } |
807 | | }; |
808 | | |
809 | | } // end namespace CodeGen |
810 | | } // end namespace clang |
811 | | |
812 | | #endif |