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