Coverage Report

Created: 2021-08-24 07:12

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