Coverage Report

Created: 2022-01-18 06:27

/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.23k
      : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
51
  CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
52
342
      : CalleeProtoTy(calleeProtoTy) {}
53
  CGCalleeInfo(GlobalDecl calleeDecl)
54
696k
      : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
55
56
716k
  const FunctionProtoType *getCalleeFunctionProtoType() const {
57
716k
    return CalleeProtoTy;
58
716k
  }
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
66.3k
  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
105k
  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
368k
            SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
111
368k
    AbstractInfo = abstractInfo;
112
368k
    assert(functionPtr && "configuring callee without function pointer");
113
0
    assert(functionPtr->getType()->isPointerTy());
114
0
    assert(functionPtr->getType()->isOpaquePointerTy() ||
115
368k
           functionPtr->getType()->getPointerElementType()->isFunctionTy());
116
368k
  }
117
118
  static CGCallee forBuiltin(unsigned builtinID,
119
65.1k
                             const FunctionDecl *builtinDecl) {
120
65.1k
    CGCallee result(SpecialKind::Builtin);
121
65.1k
    result.BuiltinInfo.Decl = builtinDecl;
122
65.1k
    result.BuiltinInfo.ID = builtinID;
123
65.1k
    return result;
124
65.1k
  }
125
126
204
  static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
127
204
    CGCallee result(SpecialKind::PseudoDestructor);
128
204
    result.PseudoDestructorInfo.Expr = E;
129
204
    return result;
130
204
  }
131
132
  static CGCallee forDirect(llvm::Constant *functionPtr,
133
357k
                            const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
134
357k
    return CGCallee(abstractInfo, functionPtr);
135
357k
  }
136
137
  static CGCallee forDirect(llvm::FunctionCallee functionPtr,
138
1.56k
                            const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
139
1.56k
    return CGCallee(abstractInfo, functionPtr.getCallee());
140
1.56k
  }
141
142
  static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
143
1.00k
                             llvm::FunctionType *FTy) {
144
1.00k
    CGCallee result(SpecialKind::Virtual);
145
1.00k
    result.VirtualInfo.CE = CE;
146
1.00k
    result.VirtualInfo.MD = MD;
147
1.00k
    result.VirtualInfo.Addr = Addr;
148
1.00k
    result.VirtualInfo.FTy = FTy;
149
1.00k
    return result;
150
1.00k
  }
151
152
380k
  bool isBuiltin() const {
153
380k
    return KindOrFunctionPointer == SpecialKind::Builtin;
154
380k
  }
155
65.1k
  const FunctionDecl *getBuiltinDecl() const {
156
65.1k
    assert(isBuiltin());
157
0
    return BuiltinInfo.Decl;
158
65.1k
  }
159
65.1k
  unsigned getBuiltinID() const {
160
65.1k
    assert(isBuiltin());
161
0
    return BuiltinInfo.ID;
162
65.1k
  }
163
164
185k
  bool isPseudoDestructor() const {
165
185k
    return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
166
185k
  }
167
204
  const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
168
204
    assert(isPseudoDestructor());
169
0
    return PseudoDestructorInfo.Expr;
170
204
  }
171
172
2.03M
  bool isOrdinary() const {
173
2.03M
    return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
174
2.03M
  }
175
925k
  CGCalleeInfo getAbstractInfo() const {
176
925k
    if (isVirtual())
177
2.01k
      return VirtualInfo.MD;
178
923k
    assert(isOrdinary());
179
0
    return AbstractInfo;
180
925k
  }
181
738k
  llvm::Value *getFunctionPointer() const {
182
738k
    assert(isOrdinary());
183
0
    return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
184
738k
  }
185
902
  void setFunctionPointer(llvm::Value *functionPtr) {
186
902
    assert(isOrdinary());
187
0
    KindOrFunctionPointer =
188
902
        SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
189
902
  }
190
191
1.66M
  bool isVirtual() const {
192
1.66M
    return KindOrFunctionPointer == SpecialKind::Virtual;
193
1.66M
  }
194
1.00k
  const CallExpr *getVirtualCallExpr() const {
195
1.00k
    assert(isVirtual());
196
0
    return VirtualInfo.CE;
197
1.00k
  }
198
1.00k
  GlobalDecl getVirtualMethodDecl() const {
199
1.00k
    assert(isVirtual());
200
0
    return VirtualInfo.MD;
201
1.00k
  }
202
1.00k
  Address getThisAddress() const {
203
1.00k
    assert(isVirtual());
204
0
    return VirtualInfo.Addr;
205
1.00k
  }
206
2.01k
  llvm::FunctionType *getVirtualFunctionType() const {
207
2.01k
    assert(isVirtual());
208
0
    return VirtualInfo.FTy;
209
2.01k
  }
210
211
  /// If this is a delayed callee computation of some sort, prepare
212
  /// a concrete callee.
213
  CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
214
};
215
216
struct CallArg {
217
private:
218
  union {
219
    RValue RV;
220
    LValue LV; /// The argument is semantically a load from this l-value.
221
  };
222
  bool HasLV;
223
224
  /// A data-flow flag to make sure getRValue and/or copyInto are not
225
  /// called twice for duplicated IR emission.
226
  mutable bool IsUsed;
227
228
public:
229
  QualType Ty;
230
  CallArg(RValue rv, QualType ty)
231
624k
      : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
232
  CallArg(LValue lv, QualType ty)
233
800
      : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
234
410k
  bool hasLValue() const { return HasLV; }
235
517k
  QualType getType() const { return Ty; }
236
237
  /// \returns an independent RValue. If the CallArg contains an LValue,
238
  /// a temporary copy is returned.
239
  RValue getRValue(CodeGenFunction &CGF) const;
240
241
1.10k
  LValue getKnownLValue() const {
242
1.10k
    assert(HasLV && !IsUsed);
243
0
    return LV;
244
1.10k
  }
245
1.01M
  RValue getKnownRValue() const {
246
1.01M
    assert(!HasLV && !IsUsed);
247
0
    return RV;
248
1.01M
  }
249
16
  void setRValue(RValue _RV) {
250
16
    assert(!HasLV);
251
0
    RV = _RV;
252
16
  }
253
254
611k
  bool isAggregate() const { return HasLV || 
RV.isAggregate()610k
; }
255
256
  void copyInto(CodeGenFunction &CGF, Address A) const;
257
};
258
259
/// CallArgList - Type for representing both the value and type of
260
/// arguments in a call.
261
class CallArgList : public SmallVector<CallArg, 8> {
262
public:
263
781k
  CallArgList() : StackBase(nullptr) {}
264
265
  struct Writeback {
266
    /// The original argument.  Note that the argument l-value
267
    /// is potentially null.
268
    LValue Source;
269
270
    /// The temporary alloca.
271
    Address Temporary;
272
273
    /// A value to "use" after the writeback, or null.
274
    llvm::Value *ToUse;
275
  };
276
277
  struct CallArgCleanup {
278
    EHScopeStack::stable_iterator Cleanup;
279
280
    /// The "is active" insertion point.  This instruction is temporary and
281
    /// will be removed after insertion.
282
    llvm::Instruction *IsActiveIP;
283
  };
284
285
623k
  void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
286
287
800
  void addUncopiedAggregate(LValue LV, QualType type) {
288
800
    push_back(CallArg(LV, type));
289
800
  }
290
291
  /// Add all the arguments from another CallArgList to this one. After doing
292
  /// this, the old CallArgList retains its list of arguments, but must not
293
  /// be used to emit a call.
294
12.8k
  void addFrom(const CallArgList &other) {
295
12.8k
    insert(end(), other.begin(), other.end());
296
12.8k
    Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
297
12.8k
                      other.Writebacks.end());
298
12.8k
    CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
299
12.8k
                                other.CleanupsToDeactivate.begin(),
300
12.8k
                                other.CleanupsToDeactivate.end());
301
12.8k
    assert(!(StackBase && other.StackBase) && "can't merge stackbases");
302
12.8k
    if (!StackBase)
303
12.8k
      StackBase = other.StackBase;
304
12.8k
  }
305
306
27
  void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
307
27
    Writeback writeback = {srcLV, temporary, toUse};
308
27
    Writebacks.push_back(writeback);
309
27
  }
310
311
366k
  bool hasWritebacks() const { return !Writebacks.empty(); }
312
313
  typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
314
      writeback_const_range;
315
316
27
  writeback_const_range writebacks() const {
317
27
    return writeback_const_range(Writebacks.begin(), Writebacks.end());
318
27
  }
319
320
  void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
321
188
                                 llvm::Instruction *IsActiveIP) {
322
188
    CallArgCleanup ArgCleanup;
323
188
    ArgCleanup.Cleanup = Cleanup;
324
188
    ArgCleanup.IsActiveIP = IsActiveIP;
325
188
    CleanupsToDeactivate.push_back(ArgCleanup);
326
188
  }
327
328
369k
  ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
329
369k
    return CleanupsToDeactivate;
330
369k
  }
331
332
  void allocateArgumentMemory(CodeGenFunction &CGF);
333
72
  llvm::Instruction *getStackBase() const { return StackBase; }
334
  void freeArgumentMemory(CodeGenFunction &CGF) const;
335
336
  /// Returns if we're using an inalloca struct to pass arguments in
337
  /// memory.
338
406
  bool isUsingInAlloca() const { return StackBase; }
339
340
private:
341
  SmallVector<Writeback, 1> Writebacks;
342
343
  /// Deactivate these cleanups immediately before making the call.  This
344
  /// is used to cleanup objects that are owned by the callee once the call
345
  /// occurs.
346
  SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
347
348
  /// The stacksave call.  It dominates all of the argument evaluation.
349
  llvm::CallInst *StackBase;
350
};
351
352
/// FunctionArgList - Type for representing both the decl and type
353
/// of parameters to a function. The decl must be either a
354
/// ParmVarDecl or ImplicitParamDecl.
355
class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
356
357
/// ReturnValueSlot - Contains the address where the return value of a
358
/// function can be stored, and whether the address is volatile or not.
359
class ReturnValueSlot {
360
  Address Addr = Address::invalid();
361
362
  // Return value slot flags
363
  unsigned IsVolatile : 1;
364
  unsigned IsUnused : 1;
365
  unsigned IsExternallyDestructed : 1;
366
367
public:
368
  ReturnValueSlot()
369
431k
      : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
370
  ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
371
                  bool IsExternallyDestructed = false)
372
      : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
373
7.60k
        IsExternallyDestructed(IsExternallyDestructed) {}
374
375
2.65k
  bool isNull() const { return !Addr.isValid(); }
376
5.41k
  bool isVolatile() const { return IsVolatile; }
377
7.34k
  Address getValue() const { return Addr; }
378
12.1k
  bool isUnused() const { return IsUnused; }
379
366k
  bool isExternallyDestructed() const { return IsExternallyDestructed; }
380
};
381
382
} // end namespace CodeGen
383
} // end namespace clang
384
385
#endif