Coverage Report

Created: 2022-05-14 11:35

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGCall.h
Line
Count
Source (jump to first uncovered line)
1
//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// These classes wrap the information about a call or function
10
// definition used to handle ABI compliancy.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
15
#define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16
17
#include "CGValue.h"
18
#include "EHScopeStack.h"
19
#include "clang/AST/ASTFwd.h"
20
#include "clang/AST/CanonicalType.h"
21
#include "clang/AST/GlobalDecl.h"
22
#include "clang/AST/Type.h"
23
#include "llvm/IR/Value.h"
24
25
// FIXME: Restructure so we don't have to expose so much stuff.
26
#include "ABIInfo.h"
27
28
namespace llvm {
29
class Type;
30
class Value;
31
} // namespace llvm
32
33
namespace clang {
34
class Decl;
35
class FunctionDecl;
36
class VarDecl;
37
38
namespace CodeGen {
39
40
/// Abstract information about a function or function prototype.
41
class CGCalleeInfo {
42
  /// The function prototype of the callee.
43
  const FunctionProtoType *CalleeProtoTy;
44
  /// The function declaration of the callee.
45
  GlobalDecl CalleeDecl;
46
47
public:
48
13.5k
  explicit CGCalleeInfo() : CalleeProtoTy(nullptr) {}
49
  CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
50
8.27k
      : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
51
  CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
52
342
      : CalleeProtoTy(calleeProtoTy) {}
53
  CGCalleeInfo(GlobalDecl calleeDecl)
54
698k
      : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
55
56
718k
  const FunctionProtoType *getCalleeFunctionProtoType() const {
57
718k
    return CalleeProtoTy;
58
718k
  }
59
1.62M
  const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
60
};
61
62
/// All available information about a concrete callee.
63
class CGCallee {
64
  enum class SpecialKind : uintptr_t {
65
    Invalid,
66
    Builtin,
67
    PseudoDestructor,
68
    Virtual,
69
70
    Last = Virtual
71
  };
72
73
  struct BuiltinInfoStorage {
74
    const FunctionDecl *Decl;
75
    unsigned ID;
76
  };
77
  struct PseudoDestructorInfoStorage {
78
    const CXXPseudoDestructorExpr *Expr;
79
  };
80
  struct VirtualInfoStorage {
81
    const CallExpr *CE;
82
    GlobalDecl MD;
83
    Address Addr;
84
    llvm::FunctionType *FTy;
85
  };
86
87
  SpecialKind KindOrFunctionPointer;
88
  union {
89
    CGCalleeInfo AbstractInfo;
90
    BuiltinInfoStorage BuiltinInfo;
91
    PseudoDestructorInfoStorage PseudoDestructorInfo;
92
    VirtualInfoStorage VirtualInfo;
93
  };
94
95
69.6k
  explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
96
97
  CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
98
0
      : KindOrFunctionPointer(SpecialKind::Builtin) {
99
0
    BuiltinInfo.Decl = builtinDecl;
100
0
    BuiltinInfo.ID = builtinID;
101
0
  }
102
103
public:
104
103k
  CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
105
106
  /// Construct a callee.  Call this constructor directly when this
107
  /// isn't a direct call.
108
  CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
109
      : KindOrFunctionPointer(
110
367k
            SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
111
367k
    AbstractInfo = abstractInfo;
112
367k
    assert(functionPtr && "configuring callee without function pointer");
113
0
    assert(functionPtr->getType()->isPointerTy());
114
0
    assert(functionPtr->getType()->isOpaquePointerTy() ||
115
367k
           functionPtr->getType()->getNonOpaquePointerElementType()
116
367k
               ->isFunctionTy());
117
367k
  }
118
119
  static CGCallee forBuiltin(unsigned builtinID,
120
68.4k
                             const FunctionDecl *builtinDecl) {
121
68.4k
    CGCallee result(SpecialKind::Builtin);
122
68.4k
    result.BuiltinInfo.Decl = builtinDecl;
123
68.4k
    result.BuiltinInfo.ID = builtinID;
124
68.4k
    return result;
125
68.4k
  }
126
127
195
  static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
128
195
    CGCallee result(SpecialKind::PseudoDestructor);
129
195
    result.PseudoDestructorInfo.Expr = E;
130
195
    return result;
131
195
  }
132
133
  static CGCallee forDirect(llvm::Constant *functionPtr,
134
355k
                            const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
135
355k
    return CGCallee(abstractInfo, functionPtr);
136
355k
  }
137
138
  static CGCallee forDirect(llvm::FunctionCallee functionPtr,
139
1.57k
                            const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
140
1.57k
    return CGCallee(abstractInfo, functionPtr.getCallee());
141
1.57k
  }
142
143
  static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
144
957
                             llvm::FunctionType *FTy) {
145
957
    CGCallee result(SpecialKind::Virtual);
146
957
    result.VirtualInfo.CE = CE;
147
957
    result.VirtualInfo.MD = MD;
148
957
    result.VirtualInfo.Addr = Addr;
149
957
    result.VirtualInfo.FTy = FTy;
150
957
    return result;
151
957
  }
152
153
392k
  bool isBuiltin() const {
154
392k
    return KindOrFunctionPointer == SpecialKind::Builtin;
155
392k
  }
156
68.4k
  const FunctionDecl *getBuiltinDecl() const {
157
68.4k
    assert(isBuiltin());
158
0
    return BuiltinInfo.Decl;
159
68.4k
  }
160
68.4k
  unsigned getBuiltinID() const {
161
68.4k
    assert(isBuiltin());
162
0
    return BuiltinInfo.ID;
163
68.4k
  }
164
165
186k
  bool isPseudoDestructor() const {
166
186k
    return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
167
186k
  }
168
195
  const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
169
195
    assert(isPseudoDestructor());
170
0
    return PseudoDestructorInfo.Expr;
171
195
  }
172
173
2.02M
  bool isOrdinary() const {
174
2.02M
    return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
175
2.02M
  }
176
924k
  CGCalleeInfo getAbstractInfo() const {
177
924k
    if (isVirtual())
178
1.91k
      return VirtualInfo.MD;
179
922k
    assert(isOrdinary());
180
0
    return AbstractInfo;
181
924k
  }
182
734k
  llvm::Value *getFunctionPointer() const {
183
734k
    assert(isOrdinary());
184
0
    return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
185
734k
  }
186
536
  void setFunctionPointer(llvm::Value *functionPtr) {
187
536
    assert(isOrdinary());
188
0
    KindOrFunctionPointer =
189
536
        SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
190
536
  }
191
192
1.66M
  bool isVirtual() const {
193
1.66M
    return KindOrFunctionPointer == SpecialKind::Virtual;
194
1.66M
  }
195
957
  const CallExpr *getVirtualCallExpr() const {
196
957
    assert(isVirtual());
197
0
    return VirtualInfo.CE;
198
957
  }
199
957
  GlobalDecl getVirtualMethodDecl() const {
200
957
    assert(isVirtual());
201
0
    return VirtualInfo.MD;
202
957
  }
203
957
  Address getThisAddress() const {
204
957
    assert(isVirtual());
205
0
    return VirtualInfo.Addr;
206
957
  }
207
1.91k
  llvm::FunctionType *getVirtualFunctionType() const {
208
1.91k
    assert(isVirtual());
209
0
    return VirtualInfo.FTy;
210
1.91k
  }
211
212
  /// If this is a delayed callee computation of some sort, prepare
213
  /// a concrete callee.
214
  CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
215
};
216
217
struct CallArg {
218
private:
219
  union {
220
    RValue RV;
221
    LValue LV; /// The argument is semantically a load from this l-value.
222
  };
223
  bool HasLV;
224
225
  /// A data-flow flag to make sure getRValue and/or copyInto are not
226
  /// called twice for duplicated IR emission.
227
  mutable bool IsUsed;
228
229
public:
230
  QualType Ty;
231
  CallArg(RValue rv, QualType ty)
232
634k
      : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
233
  CallArg(LValue lv, QualType ty)
234
800
      : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
235
412k
  bool hasLValue() const { return HasLV; }
236
512k
  QualType getType() const { return Ty; }
237
238
  /// \returns an independent RValue. If the CallArg contains an LValue,
239
  /// a temporary copy is returned.
240
  RValue getRValue(CodeGenFunction &CGF) const;
241
242
1.10k
  LValue getKnownLValue() const {
243
1.10k
    assert(HasLV && !IsUsed);
244
0
    return LV;
245
1.10k
  }
246
1.01M
  RValue getKnownRValue() const {
247
1.01M
    assert(!HasLV && !IsUsed);
248
0
    return RV;
249
1.01M
  }
250
16
  void setRValue(RValue _RV) {
251
16
    assert(!HasLV);
252
0
    RV = _RV;
253
16
  }
254
255
610k
  bool isAggregate() const { return HasLV || 
RV.isAggregate()610k
; }
256
257
  void copyInto(CodeGenFunction &CGF, Address A) const;
258
};
259
260
/// CallArgList - Type for representing both the value and type of
261
/// arguments in a call.
262
class CallArgList : public SmallVector<CallArg, 8> {
263
public:
264
780k
  CallArgList() : StackBase(nullptr) {}
265
266
  struct Writeback {
267
    /// The original argument.  Note that the argument l-value
268
    /// is potentially null.
269
    LValue Source;
270
271
    /// The temporary alloca.
272
    Address Temporary;
273
274
    /// A value to "use" after the writeback, or null.
275
    llvm::Value *ToUse;
276
  };
277
278
  struct CallArgCleanup {
279
    EHScopeStack::stable_iterator Cleanup;
280
281
    /// The "is active" insertion point.  This instruction is temporary and
282
    /// will be removed after insertion.
283
    llvm::Instruction *IsActiveIP;
284
  };
285
286
622k
  void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
287
288
800
  void addUncopiedAggregate(LValue LV, QualType type) {
289
800
    push_back(CallArg(LV, type));
290
800
  }
291
292
  /// Add all the arguments from another CallArgList to this one. After doing
293
  /// this, the old CallArgList retains its list of arguments, but must not
294
  /// be used to emit a call.
295
12.8k
  void addFrom(const CallArgList &other) {
296
12.8k
    insert(end(), other.begin(), other.end());
297
12.8k
    Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
298
12.8k
                      other.Writebacks.end());
299
12.8k
    CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
300
12.8k
                                other.CleanupsToDeactivate.begin(),
301
12.8k
                                other.CleanupsToDeactivate.end());
302
12.8k
    assert(!(StackBase && other.StackBase) && "can't merge stackbases");
303
12.8k
    if (!StackBase)
304
12.8k
      StackBase = other.StackBase;
305
12.8k
  }
306
307
27
  void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
308
27
    Writeback writeback = {srcLV, temporary, toUse};
309
27
    Writebacks.push_back(writeback);
310
27
  }
311
312
364k
  bool hasWritebacks() const { return !Writebacks.empty(); }
313
314
  typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
315
      writeback_const_range;
316
317
27
  writeback_const_range writebacks() const {
318
27
    return writeback_const_range(Writebacks.begin(), Writebacks.end());
319
27
  }
320
321
  void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
322
192
                                 llvm::Instruction *IsActiveIP) {
323
192
    CallArgCleanup ArgCleanup;
324
192
    ArgCleanup.Cleanup = Cleanup;
325
192
    ArgCleanup.IsActiveIP = IsActiveIP;
326
192
    CleanupsToDeactivate.push_back(ArgCleanup);
327
192
  }
328
329
367k
  ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
330
367k
    return CleanupsToDeactivate;
331
367k
  }
332
333
  void allocateArgumentMemory(CodeGenFunction &CGF);
334
74
  llvm::Instruction *getStackBase() const { return StackBase; }
335
  void freeArgumentMemory(CodeGenFunction &CGF) const;
336
337
  /// Returns if we're using an inalloca struct to pass arguments in
338
  /// memory.
339
417
  bool isUsingInAlloca() const { return StackBase; }
340
341
private:
342
  SmallVector<Writeback, 1> Writebacks;
343
344
  /// Deactivate these cleanups immediately before making the call.  This
345
  /// is used to cleanup objects that are owned by the callee once the call
346
  /// occurs.
347
  SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
348
349
  /// The stacksave call.  It dominates all of the argument evaluation.
350
  llvm::CallInst *StackBase;
351
};
352
353
/// FunctionArgList - Type for representing both the decl and type
354
/// of parameters to a function. The decl must be either a
355
/// ParmVarDecl or ImplicitParamDecl.
356
class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
357
358
/// ReturnValueSlot - Contains the address where the return value of a
359
/// function can be stored, and whether the address is volatile or not.
360
class ReturnValueSlot {
361
  Address Addr = Address::invalid();
362
363
  // Return value slot flags
364
  unsigned IsVolatile : 1;
365
  unsigned IsUnused : 1;
366
  unsigned IsExternallyDestructed : 1;
367
368
public:
369
  ReturnValueSlot()
370
432k
      : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
371
  ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
372
                  bool IsExternallyDestructed = false)
373
      : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
374
7.75k
        IsExternallyDestructed(IsExternallyDestructed) {}
375
376
3.14k
  bool isNull() const { return !Addr.isValid(); }
377
5.93k
  bool isVolatile() const { return IsVolatile; }
378
7.82k
  Address getValue() const { return Addr; }
379
12.6k
  bool isUnused() const { return IsUnused; }
380
364k
  bool isExternallyDestructed() const { return IsExternallyDestructed; }
381
};
382
383
} // end namespace CodeGen
384
} // end namespace clang
385
386
#endif