/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.36k | : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {} |
51 | | CGCalleeInfo(const FunctionProtoType *calleeProtoTy) |
52 | 345 | : CalleeProtoTy(calleeProtoTy) {} |
53 | | CGCalleeInfo(GlobalDecl calleeDecl) |
54 | 689k | : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {} |
55 | | |
56 | 709k | const FunctionProtoType *getCalleeFunctionProtoType() const { |
57 | 709k | return CalleeProtoTy; |
58 | 709k | } |
59 | 1.59M | 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 | 97.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 | 359k | SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) { |
111 | 359k | AbstractInfo = abstractInfo; |
112 | 359k | assert(functionPtr && "configuring callee without function pointer"); |
113 | 0 | assert(functionPtr->getType()->isPointerTy()); |
114 | 0 | assert(functionPtr->getType()->isOpaquePointerTy() || |
115 | 359k | functionPtr->getType()->getNonOpaquePointerElementType() |
116 | 359k | ->isFunctionTy()); |
117 | 359k | } |
118 | | |
119 | | static CGCallee forBuiltin(unsigned builtinID, |
120 | 96.1k | const FunctionDecl *builtinDecl) { |
121 | 96.1k | CGCallee result(SpecialKind::Builtin); |
122 | 96.1k | result.BuiltinInfo.Decl = builtinDecl; |
123 | 96.1k | result.BuiltinInfo.ID = builtinID; |
124 | 96.1k | return result; |
125 | 96.1k | } |
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 | 347k | const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { |
135 | 347k | return CGCallee(abstractInfo, functionPtr); |
136 | 347k | } |
137 | | |
138 | | static CGCallee forDirect(llvm::FunctionCallee functionPtr, |
139 | 1.58k | const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { |
140 | 1.58k | return CGCallee(abstractInfo, functionPtr.getCallee()); |
141 | 1.58k | } |
142 | | |
143 | | static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, |
144 | 1.01k | llvm::FunctionType *FTy) { |
145 | 1.01k | CGCallee result(SpecialKind::Virtual); |
146 | 1.01k | result.VirtualInfo.CE = CE; |
147 | 1.01k | result.VirtualInfo.MD = MD; |
148 | 1.01k | result.VirtualInfo.Addr = Addr; |
149 | 1.01k | result.VirtualInfo.FTy = FTy; |
150 | 1.01k | return result; |
151 | 1.01k | } |
152 | | |
153 | 451k | bool isBuiltin() const { |
154 | 451k | return KindOrFunctionPointer == SpecialKind::Builtin; |
155 | 451k | } |
156 | 96.1k | const FunctionDecl *getBuiltinDecl() const { |
157 | 96.1k | assert(isBuiltin()); |
158 | 0 | return BuiltinInfo.Decl; |
159 | 96.1k | } |
160 | 96.1k | unsigned getBuiltinID() const { |
161 | 96.1k | assert(isBuiltin()); |
162 | 0 | return BuiltinInfo.ID; |
163 | 96.1k | } |
164 | | |
165 | 162k | bool isPseudoDestructor() const { |
166 | 162k | return KindOrFunctionPointer == SpecialKind::PseudoDestructor; |
167 | 162k | } |
168 | 195 | const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const { |
169 | 195 | assert(isPseudoDestructor()); |
170 | 0 | return PseudoDestructorInfo.Expr; |
171 | 195 | } |
172 | | |
173 | 1.97M | bool isOrdinary() const { |
174 | 1.97M | return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last); |
175 | 1.97M | } |
176 | 897k | CGCalleeInfo getAbstractInfo() const { |
177 | 897k | if (isVirtual()) |
178 | 2.02k | return VirtualInfo.MD; |
179 | 894k | assert(isOrdinary()); |
180 | 0 | return AbstractInfo; |
181 | 897k | } |
182 | 718k | llvm::Value *getFunctionPointer() const { |
183 | 718k | assert(isOrdinary()); |
184 | 0 | return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer)); |
185 | 718k | } |
186 | 541 | void setFunctionPointer(llvm::Value *functionPtr) { |
187 | 541 | assert(isOrdinary()); |
188 | 0 | KindOrFunctionPointer = |
189 | 541 | SpecialKind(reinterpret_cast<uintptr_t>(functionPtr)); |
190 | 541 | } |
191 | | |
192 | 1.62M | bool isVirtual() const { |
193 | 1.62M | return KindOrFunctionPointer == SpecialKind::Virtual; |
194 | 1.62M | } |
195 | 1.01k | const CallExpr *getVirtualCallExpr() const { |
196 | 1.01k | assert(isVirtual()); |
197 | 0 | return VirtualInfo.CE; |
198 | 1.01k | } |
199 | 1.01k | GlobalDecl getVirtualMethodDecl() const { |
200 | 1.01k | assert(isVirtual()); |
201 | 0 | return VirtualInfo.MD; |
202 | 1.01k | } |
203 | 1.01k | Address getThisAddress() const { |
204 | 1.01k | assert(isVirtual()); |
205 | 0 | return VirtualInfo.Addr; |
206 | 1.01k | } |
207 | 2.02k | llvm::FunctionType *getVirtualFunctionType() const { |
208 | 2.02k | assert(isVirtual()); |
209 | 0 | return VirtualInfo.FTy; |
210 | 2.02k | } |
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 | 630k | : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {} |
233 | | CallArg(LValue lv, QualType ty) |
234 | 810 | : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {} |
235 | 405k | bool hasLValue() const { return HasLV; } |
236 | 507k | 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.11k | LValue getKnownLValue() const { |
243 | 1.11k | assert(HasLV && !IsUsed); |
244 | 0 | return LV; |
245 | 1.11k | } |
246 | 1.00M | RValue getKnownRValue() const { |
247 | 1.00M | assert(!HasLV && !IsUsed); |
248 | 0 | return RV; |
249 | 1.00M | } |
250 | 16 | void setRValue(RValue _RV) { |
251 | 16 | assert(!HasLV); |
252 | 0 | RV = _RV; |
253 | 16 | } |
254 | | |
255 | 606k | bool isAggregate() const { return HasLV || RV.isAggregate()605k ; } |
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 | 773k | 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 | 618k | void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); } |
287 | | |
288 | 810 | void addUncopiedAggregate(LValue LV, QualType type) { |
289 | 810 | push_back(CallArg(LV, type)); |
290 | 810 | } |
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 | 356k | 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 | 359k | ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const { |
330 | 359k | return CleanupsToDeactivate; |
331 | 359k | } |
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 | 452k | : 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.83k | IsExternallyDestructed(IsExternallyDestructed) {} |
375 | | |
376 | 3.19k | bool isNull() const { return !Addr.isValid(); } |
377 | 5.97k | bool isVolatile() const { return IsVolatile; } |
378 | 7.90k | Address getValue() const { return Addr; } |
379 | 12.6k | bool isUnused() const { return IsUnused; } |
380 | 356k | bool isExternallyDestructed() const { return IsExternallyDestructed; } |
381 | | }; |
382 | | |
383 | | } // end namespace CodeGen |
384 | | } // end namespace clang |
385 | | |
386 | | #endif |