Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/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
4.59M
  bool canHavePaddingType() const {
121
4.59M
    return isDirect() || 
isExtend()196k
||
isIndirect()84.7k
||
isIndirectAliased()60.7k
||
122
4.59M
           
isExpand()60.6k
;
123
4.59M
  }
124
571k
  void setPaddingType(llvm::Type *T) {
125
571k
    assert(canHavePaddingType());
126
0
    PaddingType = T;
127
571k
  }
128
129
1.11k
  void setUnpaddedCoerceToType(llvm::Type *T) {
130
1.11k
    assert(isCoerceAndExpand());
131
0
    UnpaddedCoerceAndExpandType = T;
132
1.11k
  }
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
677k
        SignExt(false) {}
141
142
  static ABIArgInfo getDirect(llvm::Type *T = nullptr, unsigned Offset = 0,
143
                              llvm::Type *Padding = nullptr,
144
548k
                              bool CanBeFlattened = true, unsigned Align = 0) {
145
548k
    auto AI = ABIArgInfo(Direct);
146
548k
    AI.setCoerceToType(T);
147
548k
    AI.setPaddingType(Padding);
148
548k
    AI.setDirectOffset(Offset);
149
548k
    AI.setDirectAlign(Align);
150
548k
    AI.setCanBeFlattened(CanBeFlattened);
151
548k
    return AI;
152
548k
  }
153
309
  static ABIArgInfo getDirectInReg(llvm::Type *T = nullptr) {
154
309
    auto AI = getDirect(T);
155
309
    AI.setInReg(true);
156
309
    return AI;
157
309
  }
158
159
9.43k
  static ABIArgInfo getSignExtend(QualType Ty, llvm::Type *T = nullptr) {
160
9.43k
    assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType");
161
0
    auto AI = ABIArgInfo(Extend);
162
9.43k
    AI.setCoerceToType(T);
163
9.43k
    AI.setPaddingType(nullptr);
164
9.43k
    AI.setDirectOffset(0);
165
9.43k
    AI.setDirectAlign(0);
166
9.43k
    AI.setSignExt(true);
167
9.43k
    return AI;
168
9.43k
  }
169
170
7.49k
  static ABIArgInfo getZeroExtend(QualType Ty, llvm::Type *T = nullptr) {
171
7.49k
    assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType");
172
0
    auto AI = ABIArgInfo(Extend);
173
7.49k
    AI.setCoerceToType(T);
174
7.49k
    AI.setPaddingType(nullptr);
175
7.49k
    AI.setDirectOffset(0);
176
7.49k
    AI.setDirectAlign(0);
177
7.49k
    AI.setSignExt(false);
178
7.49k
    return AI;
179
7.49k
  }
180
181
  // ABIArgInfo will record the argument as being extended based on the sign
182
  // of its type.
183
16.8k
  static ABIArgInfo getExtend(QualType Ty, llvm::Type *T = nullptr) {
184
16.8k
    assert(Ty->isIntegralOrEnumerationType() && "Unexpected QualType");
185
16.8k
    if (Ty->hasSignedIntegerRepresentation())
186
9.39k
      return getSignExtend(Ty, T);
187
7.49k
    return getZeroExtend(Ty, T);
188
16.8k
  }
189
190
33
  static ABIArgInfo getExtendInReg(QualType Ty, llvm::Type *T = nullptr) {
191
33
    auto AI = getExtend(Ty, T);
192
33
    AI.setInReg(true);
193
33
    return AI;
194
33
  }
195
105k
  static ABIArgInfo getIgnore() {
196
105k
    return ABIArgInfo(Ignore);
197
105k
  }
198
  static ABIArgInfo getIndirect(CharUnits Alignment, bool ByVal = true,
199
                                bool Realign = false,
200
5.86k
                                llvm::Type *Padding = nullptr) {
201
5.86k
    auto AI = ABIArgInfo(Indirect);
202
5.86k
    AI.setIndirectAlign(Alignment);
203
5.86k
    AI.setIndirectByVal(ByVal);
204
5.86k
    AI.setIndirectRealign(Realign);
205
5.86k
    AI.setSRetAfterThis(false);
206
5.86k
    AI.setPaddingType(Padding);
207
5.86k
    return AI;
208
5.86k
  }
209
210
  /// Pass this in memory using the IR byref attribute.
211
  static ABIArgInfo getIndirectAliased(CharUnits Alignment, unsigned AddrSpace,
212
                                       bool Realign = false,
213
17
                                       llvm::Type *Padding = nullptr) {
214
17
    auto AI = ABIArgInfo(IndirectAliased);
215
17
    AI.setIndirectAlign(Alignment);
216
17
    AI.setIndirectRealign(Realign);
217
17
    AI.setPaddingType(Padding);
218
17
    AI.setIndirectAddrSpace(AddrSpace);
219
17
    return AI;
220
17
  }
221
222
  static ABIArgInfo getIndirectInReg(CharUnits Alignment, bool ByVal = true,
223
25
                                     bool Realign = false) {
224
25
    auto AI = getIndirect(Alignment, ByVal, Realign);
225
25
    AI.setInReg(true);
226
25
    return AI;
227
25
  }
228
253
  static ABIArgInfo getInAlloca(unsigned FieldIndex, bool Indirect = false) {
229
253
    auto AI = ABIArgInfo(InAlloca);
230
253
    AI.setInAllocaFieldIndex(FieldIndex);
231
253
    AI.setInAllocaIndirect(Indirect);
232
253
    return AI;
233
253
  }
234
130
  static ABIArgInfo getExpand() {
235
130
    auto AI = ABIArgInfo(Expand);
236
130
    AI.setPaddingType(nullptr);
237
130
    return AI;
238
130
  }
239
  static ABIArgInfo getExpandWithPadding(bool PaddingInReg,
240
88
                                         llvm::Type *Padding) {
241
88
    auto AI = getExpand();
242
88
    AI.setPaddingInReg(PaddingInReg);
243
88
    AI.setPaddingType(Padding);
244
88
    return AI;
245
88
  }
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
1.11k
                                       llvm::Type *unpaddedCoerceToType) {
252
1.11k
#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
1.11k
    auto unpaddedStruct = dyn_cast<llvm::StructType>(unpaddedCoerceToType);
257
1.11k
    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
2.35k
    for (auto eltType : coerceToType->elements()) {
263
2.35k
      if (isPaddingForCoerceAndExpand(eltType)) 
continue20
;
264
2.33k
      if (unpaddedStruct) {
265
1.97k
        assert(unpaddedStruct->getElementType(unpaddedIndex) == eltType);
266
1.97k
      } else {
267
361
        assert(unpaddedIndex == 0 && unpaddedCoerceToType == eltType);
268
361
      }
269
0
      unpaddedIndex++;
270
2.33k
    }
271
272
    // Assert that there aren't extra elements in the unpadded type.
273
1.11k
    if (unpaddedStruct) {
274
751
      assert(unpaddedStruct->getNumElements() == unpaddedIndex);
275
751
    } else {
276
361
      assert(unpaddedIndex == 1);
277
361
    }
278
0
#endif
279
280
0
    auto AI = ABIArgInfo(CoerceAndExpand);
281
1.11k
    AI.setCoerceToType(coerceToType);
282
1.11k
    AI.setUnpaddedCoerceToType(unpaddedCoerceToType);
283
1.11k
    return AI;
284
1.11k
  }
285
286
6.66k
  static bool isPaddingForCoerceAndExpand(llvm::Type *eltType) {
287
6.66k
    if (eltType->isArrayTy()) {
288
54
      assert(eltType->getArrayElementType()->isIntegerTy(8));
289
0
      return true;
290
6.61k
    } else {
291
6.61k
      return false;
292
6.61k
    }
293
6.66k
  }
294
295
15.6M
  Kind getKind() const { return TheKind; }
296
33.0M
  bool isDirect() const { return TheKind == Direct; }
297
378k
  bool isInAlloca() const { return TheKind == InAlloca; }
298
925k
  bool isExtend() const { return TheKind == Extend; }
299
0
  bool isIgnore() const { return TheKind == Ignore; }
300
736k
  bool isIndirect() const { return TheKind == Indirect; }
301
63.1k
  bool isIndirectAliased() const { return TheKind == IndirectAliased; }
302
60.6k
  bool isExpand() const { return TheKind == Expand; }
303
497k
  bool isCoerceAndExpand() const { return TheKind == CoerceAndExpand; }
304
305
14.2M
  bool canHaveCoerceToType() const {
306
14.2M
    return isDirect() || 
isExtend()470k
||
isCoerceAndExpand()129k
;
307
14.2M
  }
308
309
  // Direct/Extend accessors
310
2.00M
  unsigned getDirectOffset() const {
311
2.00M
    assert((isDirect() || isExtend()) && "Not a direct or extend kind");
312
0
    return DirectAttr.Offset;
313
2.00M
  }
314
565k
  void setDirectOffset(unsigned Offset) {
315
565k
    assert((isDirect() || isExtend()) && "Not a direct or extend kind");
316
0
    DirectAttr.Offset = Offset;
317
565k
  }
318
319
1.16M
  unsigned getDirectAlign() const {
320
1.16M
    assert((isDirect() || isExtend()) && "Not a direct or extend kind");
321
0
    return DirectAttr.Align;
322
1.16M
  }
323
565k
  void setDirectAlign(unsigned Align) {
324
565k
    assert((isDirect() || isExtend()) && "Not a direct or extend kind");
325
0
    DirectAttr.Align = Align;
326
565k
  }
327
328
49.7k
  bool isSignExt() const {
329
49.7k
    assert(isExtend() && "Invalid kind!");
330
0
    return SignExt;
331
49.7k
  }
332
16.9k
  void setSignExt(bool SExt) {
333
16.9k
    assert(isExtend() && "Invalid kind!");
334
0
    SignExt = SExt;
335
16.9k
  }
336
337
4.02M
  llvm::Type *getPaddingType() const {
338
4.02M
    return (canHavePaddingType() ? 
PaddingType3.96M
:
nullptr59.9k
);
339
4.02M
  }
340
341
47
  bool getPaddingInReg() const {
342
47
    return PaddingInReg;
343
47
  }
344
88
  void setPaddingInReg(bool PIR) {
345
88
    PaddingInReg = PIR;
346
88
  }
347
348
11.7M
  llvm::Type *getCoerceToType() const {
349
11.7M
    assert(canHaveCoerceToType() && "Invalid kind!");
350
0
    return TypeData;
351
11.7M
  }
352
353
751k
  void setCoerceToType(llvm::Type *T) {
354
751k
    assert(canHaveCoerceToType() && "Invalid kind!");
355
0
    TypeData = T;
356
751k
  }
357
358
1.99k
  llvm::StructType *getCoerceAndExpandType() const {
359
1.99k
    assert(isCoerceAndExpand());
360
0
    return cast<llvm::StructType>(TypeData);
361
1.99k
  }
362
363
2.22k
  llvm::Type *getUnpaddedCoerceAndExpandType() const {
364
2.22k
    assert(isCoerceAndExpand());
365
0
    return UnpaddedCoerceAndExpandType;
366
2.22k
  }
367
368
4.92k
  ArrayRef<llvm::Type *>getCoerceAndExpandTypeSequence() const {
369
4.92k
    assert(isCoerceAndExpand());
370
4.92k
    if (auto structTy =
371
4.92k
          dyn_cast<llvm::StructType>(UnpaddedCoerceAndExpandType)) {
372
3.26k
      return structTy->elements();
373
3.26k
    } else {
374
1.66k
      return llvm::makeArrayRef(&UnpaddedCoerceAndExpandType, 1);
375
1.66k
    }
376
4.92k
  }
377
378
1.56M
  bool getInReg() const {
379
1.56M
    assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
380
0
    return InReg;
381
1.56M
  }
382
383
591
  void setInReg(bool IR) {
384
591
    assert((isDirect() || isExtend() || isIndirect()) && "Invalid kind!");
385
0
    InReg = IR;
386
591
  }
387
388
  // Indirect accessors
389
21.3k
  CharUnits getIndirectAlign() const {
390
21.3k
    assert((isIndirect() || isIndirectAliased()) && "Invalid kind!");
391
0
    return CharUnits::fromQuantity(IndirectAttr.Align);
392
21.3k
  }
393
5.88k
  void setIndirectAlign(CharUnits IA) {
394
5.88k
    assert((isIndirect() || isIndirectAliased()) && "Invalid kind!");
395
0
    IndirectAttr.Align = IA.getQuantity();
396
5.88k
  }
397
398
15.5k
  bool getIndirectByVal() const {
399
15.5k
    assert(isIndirect() && "Invalid kind!");
400
0
    return IndirectByVal;
401
15.5k
  }
402
5.86k
  void setIndirectByVal(bool IBV) {
403
5.86k
    assert(isIndirect() && "Invalid kind!");
404
0
    IndirectByVal = IBV;
405
5.86k
  }
406
407
35
  unsigned getIndirectAddrSpace() const {
408
35
    assert(isIndirectAliased() && "Invalid kind!");
409
0
    return IndirectAttr.AddrSpace;
410
35
  }
411
412
17
  void setIndirectAddrSpace(unsigned AddrSpace) {
413
17
    assert(isIndirectAliased() && "Invalid kind!");
414
0
    IndirectAttr.AddrSpace = AddrSpace;
415
17
  }
416
417
2.28k
  bool getIndirectRealign() const {
418
2.28k
    assert((isIndirect() || isIndirectAliased()) && "Invalid kind!");
419
0
    return IndirectRealign;
420
2.28k
  }
421
5.88k
  void setIndirectRealign(bool IR) {
422
5.88k
    assert((isIndirect() || isIndirectAliased()) && "Invalid kind!");
423
0
    IndirectRealign = IR;
424
5.88k
  }
425
426
31.3k
  bool isSRetAfterThis() const {
427
31.3k
    assert(isIndirect() && "Invalid kind!");
428
0
    return SRetAfterThis;
429
31.3k
  }
430
6.00k
  void setSRetAfterThis(bool AfterThis) {
431
6.00k
    assert(isIndirect() && "Invalid kind!");
432
0
    SRetAfterThis = AfterThis;
433
6.00k
  }
434
435
247
  unsigned getInAllocaFieldIndex() const {
436
247
    assert(isInAlloca() && "Invalid kind!");
437
0
    return AllocaFieldIndex;
438
247
  }
439
253
  void setInAllocaFieldIndex(unsigned FieldIndex) {
440
253
    assert(isInAlloca() && "Invalid kind!");
441
0
    AllocaFieldIndex = FieldIndex;
442
253
  }
443
444
233
  unsigned getInAllocaIndirect() const {
445
233
    assert(isInAlloca() && "Invalid kind!");
446
0
    return InAllocaIndirect;
447
233
  }
448
253
  void setInAllocaIndirect(bool Indirect) {
449
253
    assert(isInAlloca() && "Invalid kind!");
450
0
    InAllocaIndirect = Indirect;
451
253
  }
452
453
  /// Return true if this field of an inalloca struct should be returned
454
  /// to implement a struct return calling convention.
455
24
  bool getInAllocaSRet() const {
456
24
    assert(isInAlloca() && "Invalid kind!");
457
0
    return InAllocaSRet;
458
24
  }
459
460
8
  void setInAllocaSRet(bool SRet) {
461
8
    assert(isInAlloca() && "Invalid kind!");
462
0
    InAllocaSRet = SRet;
463
8
  }
464
465
3.85M
  bool getCanBeFlattened() const {
466
3.85M
    assert(isDirect() && "Invalid kind!");
467
0
    return CanBeFlattened;
468
3.85M
  }
469
470
548k
  void setCanBeFlattened(bool Flatten) {
471
548k
    assert(isDirect() && "Invalid kind!");
472
0
    CanBeFlattened = Flatten;
473
548k
  }
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
1.64M
  RequiredArgs(All_t _) : NumRequired(~0U) {}
488
90.5k
  explicit RequiredArgs(unsigned n) : NumRequired(n) {
489
90.5k
    assert(n != ~0U);
490
90.5k
  }
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
939k
                                       unsigned additional) {
499
939k
    if (!prototype->isVariadic()) 
return All850k
;
500
501
88.6k
    if (prototype->hasExtParameterInfos())
502
18
      additional += llvm::count_if(
503
18
          prototype->getExtParameterInfos(),
504
31
          [](const FunctionProtoType::ExtParameterInfo &ExtInfo) {
505
31
            return ExtInfo.hasPassObjectSize();
506
31
          });
507
508
88.6k
    return RequiredArgs(prototype->getNumParams() + additional);
509
939k
  }
510
511
  static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
512
753k
                                       unsigned additional) {
513
753k
    return forPrototypePlus(prototype.getTypePtr(), additional);
514
753k
  }
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
5.46M
  bool allowsOptionalArgs() const { return NumRequired != ~0U; }
525
311k
  unsigned getNumRequiredArgs() const {
526
311k
    assert(allowsOptionalArgs());
527
0
    return NumRequired;
528
311k
  }
529
530
2.58M
  unsigned getOpaqueData() const { return NumRequired; }
531
0
  static RequiredArgs getFromOpaqueData(unsigned value) {
532
0
    if (value == ~0U) return All;
533
0
    return RequiredArgs(value);
534
0
  }
535
};
536
537
// Implementation detail of CGFunctionInfo, factored out so it can be named
538
// in the TrailingObjects base class of CGFunctionInfo.
539
struct CGFunctionInfoArgInfo {
540
  CanQualType type;
541
  ABIArgInfo info;
542
};
543
544
/// CGFunctionInfo - Class to encapsulate the information about a
545
/// function definition.
546
class CGFunctionInfo final
547
    : public llvm::FoldingSetNode,
548
      private llvm::TrailingObjects<CGFunctionInfo, CGFunctionInfoArgInfo,
549
                                    FunctionProtoType::ExtParameterInfo> {
550
  typedef CGFunctionInfoArgInfo ArgInfo;
551
  typedef FunctionProtoType::ExtParameterInfo ExtParameterInfo;
552
553
  /// The LLVM::CallingConv to use for this function (as specified by the
554
  /// user).
555
  unsigned CallingConvention : 8;
556
557
  /// The LLVM::CallingConv to actually use for this function, which may
558
  /// depend on the ABI.
559
  unsigned EffectiveCallingConvention : 8;
560
561
  /// The clang::CallingConv that this was originally created with.
562
  unsigned ASTCallingConvention : 6;
563
564
  /// Whether this is an instance method.
565
  unsigned InstanceMethod : 1;
566
567
  /// Whether this is a chain call.
568
  unsigned ChainCall : 1;
569
570
  /// Whether this function is a CMSE nonsecure call
571
  unsigned CmseNSCall : 1;
572
573
  /// Whether this function is noreturn.
574
  unsigned NoReturn : 1;
575
576
  /// Whether this function is returns-retained.
577
  unsigned ReturnsRetained : 1;
578
579
  /// Whether this function saved caller registers.
580
  unsigned NoCallerSavedRegs : 1;
581
582
  /// How many arguments to pass inreg.
583
  unsigned HasRegParm : 1;
584
  unsigned RegParm : 3;
585
586
  /// Whether this function has nocf_check attribute.
587
  unsigned NoCfCheck : 1;
588
589
  /// Log 2 of the maximum vector width.
590
  unsigned MaxVectorWidth : 4;
591
592
  RequiredArgs Required;
593
594
  /// The struct representing all arguments passed in memory.  Only used when
595
  /// passing non-trivial types with inalloca.  Not part of the profile.
596
  llvm::StructType *ArgStruct;
597
  unsigned ArgStructAlign : 31;
598
  unsigned HasExtParameterInfos : 1;
599
600
  unsigned NumArgs;
601
602
3.15M
  ArgInfo *getArgsBuffer() {
603
3.15M
    return getTrailingObjects<ArgInfo>();
604
3.15M
  }
605
18.7M
  const ArgInfo *getArgsBuffer() const {
606
18.7M
    return getTrailingObjects<ArgInfo>();
607
18.7M
  }
608
609
20.1k
  ExtParameterInfo *getExtParameterInfosBuffer() {
610
20.1k
    return getTrailingObjects<ExtParameterInfo>();
611
20.1k
  }
612
141k
  const ExtParameterInfo *getExtParameterInfosBuffer() const{
613
141k
    return getTrailingObjects<ExtParameterInfo>();
614
141k
  }
615
616
237k
  CGFunctionInfo() : Required(RequiredArgs::All) {}
617
618
public:
619
  static CGFunctionInfo *create(unsigned llvmCC,
620
                                bool instanceMethod,
621
                                bool chainCall,
622
                                const FunctionType::ExtInfo &extInfo,
623
                                ArrayRef<ExtParameterInfo> paramInfos,
624
                                CanQualType resultType,
625
                                ArrayRef<CanQualType> argTypes,
626
                                RequiredArgs required);
627
237k
  void operator delete(void *p) { ::operator delete(p); }
628
629
  // Friending class TrailingObjects is apparently not good enough for MSVC,
630
  // so these have to be public.
631
  friend class TrailingObjects;
632
161k
  size_t numTrailingObjects(OverloadToken<ArgInfo>) const {
633
161k
    return NumArgs + 1;
634
161k
  }
635
0
  size_t numTrailingObjects(OverloadToken<ExtParameterInfo>) const {
636
0
    return (HasExtParameterInfos ? NumArgs : 0);
637
0
  }
638
639
  typedef const ArgInfo *const_arg_iterator;
640
  typedef ArgInfo *arg_iterator;
641
642
1.51M
  MutableArrayRef<ArgInfo> arguments() {
643
1.51M
    return MutableArrayRef<ArgInfo>(arg_begin(), NumArgs);
644
1.51M
  }
645
0
  ArrayRef<ArgInfo> arguments() const {
646
0
    return ArrayRef<ArgInfo>(arg_begin(), NumArgs);
647
0
  }
648
649
5.74M
  const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
650
4.73M
  const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
651
1.67M
  arg_iterator arg_begin() { return getArgsBuffer() + 1; }
652
163k
  arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
653
654
7.29M
  unsigned  arg_size() const { return NumArgs; }
655
656
4.91M
  bool isVariadic() const { return Required.allowsOptionalArgs(); }
657
287k
  RequiredArgs getRequiredArgs() const { return Required; }
658
3.38M
  unsigned getNumRequiredArgs() const {
659
3.38M
    return isVariadic() ? 
getRequiredArgs().getNumRequiredArgs()286k
:
arg_size()3.10M
;
660
3.38M
  }
661
662
710k
  bool isInstanceMethod() const { return InstanceMethod; }
663
664
818k
  bool isChainCall() const { return ChainCall; }
665
666
713k
  bool isCmseNSCall() const { return CmseNSCall; }
667
668
1.02M
  bool isNoReturn() const { return NoReturn; }
669
670
  /// In ARC, whether this function retains its return value.  This
671
  /// is not always reliable for call sites.
672
1.40k
  bool isReturnsRetained() const { return ReturnsRetained; }
673
674
  /// Whether this function no longer saves caller registers.
675
856
  bool isNoCallerSavedRegs() const { return NoCallerSavedRegs; }
676
677
  /// Whether this function has nocf_check attribute.
678
856
  bool isNoCfCheck() const { return NoCfCheck; }
679
680
  /// getASTCallingConvention() - Return the AST-specified calling
681
  /// convention.
682
1.20M
  CallingConv getASTCallingConvention() const {
683
1.20M
    return CallingConv(ASTCallingConvention);
684
1.20M
  }
685
686
  /// getCallingConvention - Return the user specified calling
687
  /// convention, which has been translated into an LLVM CC.
688
267k
  unsigned getCallingConvention() const { return CallingConvention; }
689
690
  /// getEffectiveCallingConvention - Return the actual calling convention to
691
  /// use, which may depend on the ABI.
692
709k
  unsigned getEffectiveCallingConvention() const {
693
709k
    return EffectiveCallingConvention;
694
709k
  }
695
4.49k
  void setEffectiveCallingConvention(unsigned Value) {
696
4.49k
    EffectiveCallingConvention = Value;
697
4.49k
  }
698
699
19.8k
  bool getHasRegParm() const { return HasRegParm; }
700
2.04k
  unsigned getRegParm() const { return RegParm; }
701
702
856
  FunctionType::ExtInfo getExtInfo() const {
703
856
    return FunctionType::ExtInfo(isNoReturn(), getHasRegParm(), getRegParm(),
704
856
                                 getASTCallingConvention(), isReturnsRetained(),
705
856
                                 isNoCallerSavedRegs(), isNoCfCheck(),
706
856
                                 isCmseNSCall());
707
856
  }
708
709
3.16M
  CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
710
711
639k
  ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
712
5.08M
  const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
713
714
141k
  ArrayRef<ExtParameterInfo> getExtParameterInfos() const {
715
141k
    if (!HasExtParameterInfos) 
return {}0
;
716
141k
    return llvm::makeArrayRef(getExtParameterInfosBuffer(), NumArgs);
717
141k
  }
718
3.47M
  ExtParameterInfo getExtParameterInfo(unsigned argIndex) const {
719
3.47M
    assert(argIndex <= NumArgs);
720
3.47M
    if (!HasExtParameterInfos) 
return ExtParameterInfo()3.35M
;
721
128k
    return getExtParameterInfos()[argIndex];
722
3.47M
  }
723
724
  /// Return true if this function uses inalloca arguments.
725
2.45M
  bool usesInAlloca() const { return ArgStruct; }
726
727
  /// Get the struct type used to represent all the arguments in memory.
728
438k
  llvm::StructType *getArgStruct() const { return ArgStruct; }
729
115
  CharUnits getArgStructAlignment() const {
730
115
    return CharUnits::fromQuantity(ArgStructAlign);
731
115
  }
732
73
  void setArgStruct(llvm::StructType *Ty, CharUnits Align) {
733
73
    ArgStruct = Ty;
734
73
    ArgStructAlign = Align.getQuantity();
735
73
  }
736
737
  /// Return the maximum vector width in the arguments.
738
606k
  unsigned getMaxVectorWidth() const {
739
606k
    return MaxVectorWidth ? 
1U << (MaxVectorWidth - 1)17
:
0606k
;
740
606k
  }
741
742
  /// Set the maximum vector width in the arguments.
743
7
  void setMaxVectorWidth(unsigned Width) {
744
7
    assert(llvm::isPowerOf2_32(Width) && "Expected power of 2 vector");
745
0
    MaxVectorWidth = llvm::countTrailingZeros(Width) + 1;
746
7
  }
747
748
1.20M
  void Profile(llvm::FoldingSetNodeID &ID) {
749
1.20M
    ID.AddInteger(getASTCallingConvention());
750
1.20M
    ID.AddBoolean(InstanceMethod);
751
1.20M
    ID.AddBoolean(ChainCall);
752
1.20M
    ID.AddBoolean(NoReturn);
753
1.20M
    ID.AddBoolean(ReturnsRetained);
754
1.20M
    ID.AddBoolean(NoCallerSavedRegs);
755
1.20M
    ID.AddBoolean(HasRegParm);
756
1.20M
    ID.AddInteger(RegParm);
757
1.20M
    ID.AddBoolean(NoCfCheck);
758
1.20M
    ID.AddBoolean(CmseNSCall);
759
1.20M
    ID.AddInteger(Required.getOpaqueData());
760
1.20M
    ID.AddBoolean(HasExtParameterInfos);
761
1.20M
    if (HasExtParameterInfos) {
762
11.9k
      for (auto paramInfo : getExtParameterInfos())
763
34.8k
        ID.AddInteger(paramInfo.getOpaqueValue());
764
11.9k
    }
765
1.20M
    getReturnType().Profile(ID);
766
1.20M
    for (const auto &I : arguments())
767
1.89M
      I.type.Profile(ID);
768
1.20M
  }
769
  static void Profile(llvm::FoldingSetNodeID &ID,
770
                      bool InstanceMethod,
771
                      bool ChainCall,
772
                      const FunctionType::ExtInfo &info,
773
                      ArrayRef<ExtParameterInfo> paramInfos,
774
                      RequiredArgs required,
775
                      CanQualType resultType,
776
1.38M
                      ArrayRef<CanQualType> argTypes) {
777
1.38M
    ID.AddInteger(info.getCC());
778
1.38M
    ID.AddBoolean(InstanceMethod);
779
1.38M
    ID.AddBoolean(ChainCall);
780
1.38M
    ID.AddBoolean(info.getNoReturn());
781
1.38M
    ID.AddBoolean(info.getProducesResult());
782
1.38M
    ID.AddBoolean(info.getNoCallerSavedRegs());
783
1.38M
    ID.AddBoolean(info.getHasRegParm());
784
1.38M
    ID.AddInteger(info.getRegParm());
785
1.38M
    ID.AddBoolean(info.getNoCfCheck());
786
1.38M
    ID.AddBoolean(info.getCmseNSCall());
787
1.38M
    ID.AddInteger(required.getOpaqueData());
788
1.38M
    ID.AddBoolean(!paramInfos.empty());
789
1.38M
    if (!paramInfos.empty()) {
790
18.4k
      for (auto paramInfo : paramInfos)
791
53.6k
        ID.AddInteger(paramInfo.getOpaqueValue());
792
18.4k
    }
793
1.38M
    resultType.Profile(ID);
794
1.38M
    for (ArrayRef<CanQualType>::iterator
795
3.62M
           i = argTypes.begin(), e = argTypes.end(); i != e; 
++i2.23M
) {
796
2.23M
      i->Profile(ID);
797
2.23M
    }
798
1.38M
  }
799
};
800
801
}  // end namespace CodeGen
802
}  // end namespace clang
803
804
#endif