Coverage Report

Created: 2020-02-25 14:32

/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.8k
  explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl() {}
53
  CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
54
4.27k
      : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
55
  CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
56
276
      : CalleeProtoTy(calleeProtoTy), CalleeDecl() {}
57
  CGCalleeInfo(GlobalDecl calleeDecl)
58
558k
      : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
59
60
575k
  const FunctionProtoType *getCalleeFunctionProtoType() const {
61
575k
    return CalleeProtoTy;
62
575k
  }
63
1.30M
  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
42.0k
  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
87.8k
  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
305k
      : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
114
305k
    AbstractInfo = abstractInfo;
115
305k
    assert(functionPtr && "configuring callee without function pointer");
116
305k
    assert(functionPtr->getType()->isPointerTy());
117
305k
    assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
118
305k
  }
119
120
  static CGCallee forBuiltin(unsigned builtinID,
121
40.9k
                             const FunctionDecl *builtinDecl) {
122
40.9k
    CGCallee result(SpecialKind::Builtin);
123
40.9k
    result.BuiltinInfo.Decl = builtinDecl;
124
40.9k
    result.BuiltinInfo.ID = builtinID;
125
40.9k
    return result;
126
40.9k
  }
127
128
164
  static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
129
164
    CGCallee result(SpecialKind::PseudoDestructor);
130
164
    result.PseudoDestructorInfo.Expr = E;
131
164
    return result;
132
164
  }
133
134
  static CGCallee forDirect(llvm::Constant *functionPtr,
135
297k
                            const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
136
297k
    return CGCallee(abstractInfo, functionPtr);
137
297k
  }
138
139
  static CGCallee forDirect(llvm::FunctionCallee functionPtr,
140
1.46k
                            const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
141
1.46k
    return CGCallee(abstractInfo, functionPtr.getCallee());
142
1.46k
  }
143
144
  static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
145
946
                             llvm::FunctionType *FTy) {
146
946
    CGCallee result(SpecialKind::Virtual);
147
946
    result.VirtualInfo.CE = CE;
148
946
    result.VirtualInfo.MD = MD;
149
946
    result.VirtualInfo.Addr = Addr;
150
946
    result.VirtualInfo.FTy = FTy;
151
946
    return result;
152
946
  }
153
154
271k
  bool isBuiltin() const {
155
271k
    return KindOrFunctionPointer == SpecialKind::Builtin;
156
271k
  }
157
40.9k
  const FunctionDecl *getBuiltinDecl() const {
158
40.9k
    assert(isBuiltin());
159
40.9k
    return BuiltinInfo.Decl;
160
40.9k
  }
161
40.9k
  unsigned getBuiltinID() const {
162
40.9k
    assert(isBuiltin());
163
40.9k
    return BuiltinInfo.ID;
164
40.9k
  }
165
166
148k
  bool isPseudoDestructor() const {
167
148k
    return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
168
148k
  }
169
164
  const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
170
164
    assert(isPseudoDestructor());
171
164
    return PseudoDestructorInfo.Expr;
172
164
  }
173
174
1.67M
  bool isOrdinary() const {
175
1.67M
    return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
176
1.67M
  }
177
761k
  CGCalleeInfo getAbstractInfo() const {
178
761k
    if (isVirtual())
179
1.89k
      return VirtualInfo.MD;
180
759k
    assert(isOrdinary());
181
759k
    return AbstractInfo;
182
759k
  }
183
610k
  llvm::Value *getFunctionPointer() const {
184
610k
    assert(isOrdinary());
185
610k
    return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
186
610k
  }
187
817
  void setFunctionPointer(llvm::Value *functionPtr) {
188
817
    assert(isOrdinary());
189
817
    KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
190
817
  }
191
192
1.37M
  bool isVirtual() const {
193
1.37M
    return KindOrFunctionPointer == SpecialKind::Virtual;
194
1.37M
  }
195
946
  const CallExpr *getVirtualCallExpr() const {
196
946
    assert(isVirtual());
197
946
    return VirtualInfo.CE;
198
946
  }
199
946
  GlobalDecl getVirtualMethodDecl() const {
200
946
    assert(isVirtual());
201
946
    return VirtualInfo.MD;
202
946
  }
203
946
  Address getThisAddress() const {
204
946
    assert(isVirtual());
205
946
    return VirtualInfo.Addr;
206
946
  }
207
1.89k
  llvm::FunctionType *getVirtualFunctionType() const {
208
1.89k
    assert(isVirtual());
209
1.89k
    return VirtualInfo.FTy;
210
1.89k
  }
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
525k
      : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
233
  CallArg(LValue lv, QualType ty)
234
660
      : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
235
338k
  bool hasLValue() const { return HasLV; }
236
37
  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
933
  LValue getKnownLValue() const {
243
933
    assert(HasLV && !IsUsed);
244
933
    return LV;
245
933
  }
246
840k
  RValue getKnownRValue() const {
247
840k
    assert(!HasLV && !IsUsed);
248
840k
    return RV;
249
840k
  }
250
15
  void setRValue(RValue _RV) {
251
15
    assert(!HasLV);
252
15
    RV = _RV;
253
15
  }
254
255
511k
  bool isAggregate() const { return HasLV || 
RV.isAggregate()511k
; }
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
636k
  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
524k
  void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
287
288
660
  void addUncopiedAggregate(LValue LV, QualType type) {
289
660
    push_back(CallArg(LV, type));
290
660
  }
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.9k
  void addFrom(const CallArgList &other) {
296
12.9k
    insert(end(), other.begin(), other.end());
297
12.9k
    Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
298
12.9k
                      other.Writebacks.end());
299
12.9k
    CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
300
12.9k
                                other.CleanupsToDeactivate.begin(),
301
12.9k
                                other.CleanupsToDeactivate.end());
302
12.9k
    assert(!(StackBase && other.StackBase) && "can't merge stackbases");
303
12.9k
    if (!StackBase)
304
12.9k
      StackBase = other.StackBase;
305
12.9k
  }
306
307
26
  void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
308
26
    Writeback writeback = {srcLV, temporary, toUse};
309
26
    Writebacks.push_back(writeback);
310
26
  }
311
312
303k
  bool hasWritebacks() const { return !Writebacks.empty(); }
313
314
  typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
315
      writeback_const_range;
316
317
26
  writeback_const_range writebacks() const {
318
26
    return writeback_const_range(Writebacks.begin(), Writebacks.end());
319
26
  }
320
321
  void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
322
173
                                 llvm::Instruction *IsActiveIP) {
323
173
    CallArgCleanup ArgCleanup;
324
173
    ArgCleanup.Cleanup = Cleanup;
325
173
    ArgCleanup.IsActiveIP = IsActiveIP;
326
173
    CleanupsToDeactivate.push_back(ArgCleanup);
327
173
  }
328
329
305k
  ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
330
305k
    return CleanupsToDeactivate;
331
305k
  }
332
333
  void allocateArgumentMemory(CodeGenFunction &CGF);
334
69
  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
333
  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
  llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
362
  CharUnits Alignment;
363
364
  // Return value slot flags
365
  enum Flags {
366
    IS_VOLATILE = 0x1,
367
    IS_UNUSED = 0x2,
368
  };
369
370
public:
371
344k
  ReturnValueSlot() {}
372
  ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
373
      : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
374
              (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
375
6.49k
        Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
376
377
2.24k
  bool isNull() const { return !getValue().isValid(); }
378
379
4.88k
  bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
380
8.72k
  Address getValue() const { return Address(Value.getPointer(), Alignment); }
381
12.4k
  bool isUnused() const { return Value.getInt() & IS_UNUSED; }
382
};
383
384
} // end namespace CodeGen
385
} // end namespace clang
386
387
#endif