/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGExprCXX.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// |
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 | | // This contains code dealing with code generation of C++ expressions |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "CGCUDARuntime.h" |
14 | | #include "CGCXXABI.h" |
15 | | #include "CGDebugInfo.h" |
16 | | #include "CGObjCRuntime.h" |
17 | | #include "CodeGenFunction.h" |
18 | | #include "ConstantEmitter.h" |
19 | | #include "TargetInfo.h" |
20 | | #include "clang/Basic/CodeGenOptions.h" |
21 | | #include "clang/CodeGen/CGFunctionInfo.h" |
22 | | #include "llvm/IR/Intrinsics.h" |
23 | | |
24 | | using namespace clang; |
25 | | using namespace CodeGen; |
26 | | |
27 | | namespace { |
28 | | struct MemberCallInfo { |
29 | | RequiredArgs ReqArgs; |
30 | | // Number of prefix arguments for the call. Ignores the `this` pointer. |
31 | | unsigned PrefixSize; |
32 | | }; |
33 | | } |
34 | | |
35 | | static MemberCallInfo |
36 | | commonEmitCXXMemberOrOperatorCall(CodeGenFunction &CGF, const CXXMethodDecl *MD, |
37 | | llvm::Value *This, llvm::Value *ImplicitParam, |
38 | | QualType ImplicitParamTy, const CallExpr *CE, |
39 | 106k | CallArgList &Args, CallArgList *RtlArgs) { |
40 | 106k | assert(CE == nullptr || isa<CXXMemberCallExpr>(CE) || |
41 | 106k | isa<CXXOperatorCallExpr>(CE)); |
42 | 0 | assert(MD->isInstance() && |
43 | 106k | "Trying to emit a member or operator call expr on a static method!"); |
44 | | |
45 | | // Push the this ptr. |
46 | 0 | const CXXRecordDecl *RD = |
47 | 106k | CGF.CGM.getCXXABI().getThisArgumentTypeForMethod(MD); |
48 | 106k | Args.add(RValue::get(This), CGF.getTypes().DeriveThisType(RD, MD)); |
49 | | |
50 | | // If there is an implicit parameter (e.g. VTT), emit it. |
51 | 106k | if (ImplicitParam) { |
52 | 199 | Args.add(RValue::get(ImplicitParam), ImplicitParamTy); |
53 | 199 | } |
54 | | |
55 | 106k | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
56 | 106k | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size()); |
57 | 106k | unsigned PrefixSize = Args.size() - 1; |
58 | | |
59 | | // And the rest of the call args. |
60 | 106k | if (RtlArgs) { |
61 | | // Special case: if the caller emitted the arguments right-to-left already |
62 | | // (prior to emitting the *this argument), we're done. This happens for |
63 | | // assignment operators. |
64 | 947 | Args.addFrom(*RtlArgs); |
65 | 105k | } else if (CE) { |
66 | | // Special case: skip first argument of CXXOperatorCall (it is "this"). |
67 | 75.4k | unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 17.94k : 067.4k ; |
68 | 75.4k | CGF.EmitCallArgs(Args, FPT, drop_begin(CE->arguments(), ArgsToSkip), |
69 | 75.4k | CE->getDirectCallee()); |
70 | 75.4k | } else { |
71 | 30.0k | assert( |
72 | 30.0k | FPT->getNumParams() == 0 && |
73 | 30.0k | "No CallExpr specified for function with non-zero number of arguments"); |
74 | 30.0k | } |
75 | 0 | return {required, PrefixSize}; |
76 | 106k | } |
77 | | |
78 | | RValue CodeGenFunction::EmitCXXMemberOrOperatorCall( |
79 | | const CXXMethodDecl *MD, const CGCallee &Callee, |
80 | | ReturnValueSlot ReturnValue, |
81 | | llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, |
82 | 76.1k | const CallExpr *CE, CallArgList *RtlArgs) { |
83 | 76.1k | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
84 | 76.1k | CallArgList Args; |
85 | 76.1k | MemberCallInfo CallInfo = commonEmitCXXMemberOrOperatorCall( |
86 | 76.1k | *this, MD, This, ImplicitParam, ImplicitParamTy, CE, Args, RtlArgs); |
87 | 76.1k | auto &FnInfo = CGM.getTypes().arrangeCXXMethodCall( |
88 | 76.1k | Args, FPT, CallInfo.ReqArgs, CallInfo.PrefixSize); |
89 | 76.1k | return EmitCall(FnInfo, Callee, ReturnValue, Args, nullptr, |
90 | 76.1k | CE && CE == MustTailCall, |
91 | 76.1k | CE ? CE->getExprLoc() : SourceLocation()0 ); |
92 | 76.1k | } |
93 | | |
94 | | RValue CodeGenFunction::EmitCXXDestructorCall( |
95 | | GlobalDecl Dtor, const CGCallee &Callee, llvm::Value *This, QualType ThisTy, |
96 | 30.2k | llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE) { |
97 | 30.2k | const CXXMethodDecl *DtorDecl = cast<CXXMethodDecl>(Dtor.getDecl()); |
98 | | |
99 | 30.2k | assert(!ThisTy.isNull()); |
100 | 0 | assert(ThisTy->getAsCXXRecordDecl() == DtorDecl->getParent() && |
101 | 30.2k | "Pointer/Object mixup"); |
102 | | |
103 | 0 | LangAS SrcAS = ThisTy.getAddressSpace(); |
104 | 30.2k | LangAS DstAS = DtorDecl->getMethodQualifiers().getAddressSpace(); |
105 | 30.2k | if (SrcAS != DstAS) { |
106 | 2 | QualType DstTy = DtorDecl->getThisType(); |
107 | 2 | llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy); |
108 | 2 | This = getTargetHooks().performAddrSpaceCast(*this, This, SrcAS, DstAS, |
109 | 2 | NewType); |
110 | 2 | } |
111 | | |
112 | 30.2k | CallArgList Args; |
113 | 30.2k | commonEmitCXXMemberOrOperatorCall(*this, DtorDecl, This, ImplicitParam, |
114 | 30.2k | ImplicitParamTy, CE, Args, nullptr); |
115 | 30.2k | return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(Dtor), Callee, |
116 | 30.2k | ReturnValueSlot(), Args, nullptr, CE && CE == MustTailCall198 , |
117 | 30.2k | CE ? CE->getExprLoc()198 : SourceLocation{}30.0k ); |
118 | 30.2k | } |
119 | | |
120 | | RValue CodeGenFunction::EmitCXXPseudoDestructorExpr( |
121 | 195 | const CXXPseudoDestructorExpr *E) { |
122 | 195 | QualType DestroyedType = E->getDestroyedType(); |
123 | 195 | if (DestroyedType.hasStrongOrWeakObjCLifetime()) { |
124 | | // Automatic Reference Counting: |
125 | | // If the pseudo-expression names a retainable object with weak or |
126 | | // strong lifetime, the object shall be released. |
127 | 4 | Expr *BaseExpr = E->getBase(); |
128 | 4 | Address BaseValue = Address::invalid(); |
129 | 4 | Qualifiers BaseQuals; |
130 | | |
131 | | // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. |
132 | 4 | if (E->isArrow()) { |
133 | 2 | BaseValue = EmitPointerWithAlignment(BaseExpr); |
134 | 2 | const auto *PTy = BaseExpr->getType()->castAs<PointerType>(); |
135 | 2 | BaseQuals = PTy->getPointeeType().getQualifiers(); |
136 | 2 | } else { |
137 | 2 | LValue BaseLV = EmitLValue(BaseExpr); |
138 | 2 | BaseValue = BaseLV.getAddress(*this); |
139 | 2 | QualType BaseTy = BaseExpr->getType(); |
140 | 2 | BaseQuals = BaseTy.getQualifiers(); |
141 | 2 | } |
142 | | |
143 | 4 | switch (DestroyedType.getObjCLifetime()) { |
144 | 0 | case Qualifiers::OCL_None: |
145 | 0 | case Qualifiers::OCL_ExplicitNone: |
146 | 0 | case Qualifiers::OCL_Autoreleasing: |
147 | 0 | break; |
148 | | |
149 | 2 | case Qualifiers::OCL_Strong: |
150 | 2 | EmitARCRelease(Builder.CreateLoad(BaseValue, |
151 | 2 | DestroyedType.isVolatileQualified()), |
152 | 2 | ARCPreciseLifetime); |
153 | 2 | break; |
154 | | |
155 | 2 | case Qualifiers::OCL_Weak: |
156 | 2 | EmitARCDestroyWeak(BaseValue); |
157 | 2 | break; |
158 | 4 | } |
159 | 191 | } else { |
160 | | // C++ [expr.pseudo]p1: |
161 | | // The result shall only be used as the operand for the function call |
162 | | // operator (), and the result of such a call has type void. The only |
163 | | // effect is the evaluation of the postfix-expression before the dot or |
164 | | // arrow. |
165 | 191 | EmitIgnoredExpr(E->getBase()); |
166 | 191 | } |
167 | | |
168 | 195 | return RValue::get(nullptr); |
169 | 195 | } |
170 | | |
171 | 494 | static CXXRecordDecl *getCXXRecord(const Expr *E) { |
172 | 494 | QualType T = E->getType(); |
173 | 494 | if (const PointerType *PTy = T->getAs<PointerType>()) |
174 | 35 | T = PTy->getPointeeType(); |
175 | 494 | const RecordType *Ty = T->castAs<RecordType>(); |
176 | 494 | return cast<CXXRecordDecl>(Ty->getDecl()); |
177 | 494 | } |
178 | | |
179 | | // Note: This function also emit constructor calls to support a MSVC |
180 | | // extensions allowing explicit constructor function call. |
181 | | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, |
182 | 67.9k | ReturnValueSlot ReturnValue) { |
183 | 67.9k | const Expr *callee = CE->getCallee()->IgnoreParens(); |
184 | | |
185 | 67.9k | if (isa<BinaryOperator>(callee)) |
186 | 149 | return EmitCXXMemberPointerCallExpr(CE, ReturnValue); |
187 | | |
188 | 67.8k | const MemberExpr *ME = cast<MemberExpr>(callee); |
189 | 67.8k | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
190 | | |
191 | 67.8k | if (MD->isStatic()) { |
192 | | // The method is static, emit it as we would a regular call. |
193 | 0 | CGCallee callee = |
194 | 0 | CGCallee::forDirect(CGM.GetAddrOfFunction(MD), GlobalDecl(MD)); |
195 | 0 | return EmitCall(getContext().getPointerType(MD->getType()), callee, CE, |
196 | 0 | ReturnValue); |
197 | 0 | } |
198 | | |
199 | 67.8k | bool HasQualifier = ME->hasQualifier(); |
200 | 67.8k | NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier()1.44k : nullptr66.3k ; |
201 | 67.8k | bool IsArrow = ME->isArrow(); |
202 | 67.8k | const Expr *Base = ME->getBase(); |
203 | | |
204 | 67.8k | return EmitCXXMemberOrOperatorMemberCallExpr( |
205 | 67.8k | CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base); |
206 | 67.8k | } |
207 | | |
208 | | RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr( |
209 | | const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, |
210 | | bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow, |
211 | 79.2k | const Expr *Base) { |
212 | 79.2k | assert(isa<CXXMemberCallExpr>(CE) || isa<CXXOperatorCallExpr>(CE)); |
213 | | |
214 | | // Compute the object pointer. |
215 | 79.2k | bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier1.39k ; |
216 | | |
217 | 79.2k | const CXXMethodDecl *DevirtualizedMethod = nullptr; |
218 | 79.2k | if (CanUseVirtualCall && |
219 | 79.2k | MD->getDevirtualizedMethod(Base, getLangOpts().AppleKext)1.35k ) { |
220 | 422 | const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); |
221 | 422 | DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); |
222 | 422 | assert(DevirtualizedMethod); |
223 | 0 | const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); |
224 | 422 | const Expr *Inner = Base->IgnoreParenBaseCasts(); |
225 | 422 | if (DevirtualizedMethod->getReturnType().getCanonicalType() != |
226 | 422 | MD->getReturnType().getCanonicalType()) |
227 | | // If the return types are not the same, this might be a case where more |
228 | | // code needs to run to compensate for it. For example, the derived |
229 | | // method might return a type that inherits form from the return |
230 | | // type of MD and has a prefix. |
231 | | // For now we just avoid devirtualizing these covariant cases. |
232 | 2 | DevirtualizedMethod = nullptr; |
233 | 420 | else if (getCXXRecord(Inner) == DevirtualizedClass) |
234 | | // If the class of the Inner expression is where the dynamic method |
235 | | // is defined, build the this pointer from it. |
236 | 348 | Base = Inner; |
237 | 72 | else if (getCXXRecord(Base) != DevirtualizedClass) { |
238 | | // If the method is defined in a class that is not the best dynamic |
239 | | // one or the one of the full expression, we would have to build |
240 | | // a derived-to-base cast to compute the correct this pointer, but |
241 | | // we don't have support for that yet, so do a virtual call. |
242 | 6 | DevirtualizedMethod = nullptr; |
243 | 6 | } |
244 | 422 | } |
245 | | |
246 | 0 | bool TrivialForCodegen = |
247 | 79.2k | MD->isTrivial() || (76.3k MD->isDefaulted()76.3k && MD->getParent()->isUnion()215 ); |
248 | 79.2k | bool TrivialAssignment = |
249 | 79.2k | TrivialForCodegen && |
250 | 79.2k | (2.90k MD->isCopyAssignmentOperator()2.90k || MD->isMoveAssignmentOperator()840 ) && |
251 | 79.2k | !MD->getParent()->mayInsertExtraPadding()2.73k ; |
252 | | |
253 | | // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment |
254 | | // operator before the LHS. |
255 | 79.2k | CallArgList RtlArgStorage; |
256 | 79.2k | CallArgList *RtlArgs = nullptr; |
257 | 79.2k | LValue TrivialAssignmentRHS; |
258 | 79.2k | if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(CE)) { |
259 | 11.4k | if (OCE->isAssignmentOp()) { |
260 | 3.49k | if (TrivialAssignment) { |
261 | 2.54k | TrivialAssignmentRHS = EmitLValue(CE->getArg(1)); |
262 | 2.54k | } else { |
263 | 947 | RtlArgs = &RtlArgStorage; |
264 | 947 | EmitCallArgs(*RtlArgs, MD->getType()->castAs<FunctionProtoType>(), |
265 | 947 | drop_begin(CE->arguments(), 1), CE->getDirectCallee(), |
266 | 947 | /*ParamsToSkip*/0, EvaluationOrder::ForceRightToLeft); |
267 | 947 | } |
268 | 3.49k | } |
269 | 11.4k | } |
270 | | |
271 | 79.2k | LValue This; |
272 | 79.2k | if (IsArrow) { |
273 | 27.5k | LValueBaseInfo BaseInfo; |
274 | 27.5k | TBAAAccessInfo TBAAInfo; |
275 | 27.5k | Address ThisValue = EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo); |
276 | 27.5k | This = MakeAddrLValue(ThisValue, Base->getType(), BaseInfo, TBAAInfo); |
277 | 51.7k | } else { |
278 | 51.7k | This = EmitLValue(Base); |
279 | 51.7k | } |
280 | | |
281 | 79.2k | if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) { |
282 | | // This is the MSVC p->Ctor::Ctor(...) extension. We assume that's |
283 | | // constructing a new complete object of type Ctor. |
284 | 12 | assert(!RtlArgs); |
285 | 0 | assert(ReturnValue.isNull() && "Constructor shouldn't have return value"); |
286 | 0 | CallArgList Args; |
287 | 12 | commonEmitCXXMemberOrOperatorCall( |
288 | 12 | *this, Ctor, This.getPointer(*this), /*ImplicitParam=*/nullptr, |
289 | 12 | /*ImplicitParamTy=*/QualType(), CE, Args, nullptr); |
290 | | |
291 | 12 | EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
292 | 12 | /*Delegating=*/false, This.getAddress(*this), Args, |
293 | 12 | AggValueSlot::DoesNotOverlap, CE->getExprLoc(), |
294 | 12 | /*NewPointerIsChecked=*/false); |
295 | 12 | return RValue::get(nullptr); |
296 | 12 | } |
297 | | |
298 | 79.2k | if (TrivialForCodegen) { |
299 | 2.89k | if (isa<CXXDestructorDecl>(MD)) |
300 | 167 | return RValue::get(nullptr); |
301 | | |
302 | 2.73k | if (TrivialAssignment) { |
303 | | // We don't like to generate the trivial copy/move assignment operator |
304 | | // when it isn't necessary; just produce the proper effect here. |
305 | | // It's important that we use the result of EmitLValue here rather than |
306 | | // emitting call arguments, in order to preserve TBAA information from |
307 | | // the RHS. |
308 | 2.72k | LValue RHS = isa<CXXOperatorCallExpr>(CE) |
309 | 2.72k | ? TrivialAssignmentRHS2.54k |
310 | 2.72k | : EmitLValue(*CE->arg_begin())181 ; |
311 | 2.72k | EmitAggregateAssign(This, RHS, CE->getType()); |
312 | 2.72k | return RValue::get(This.getPointer(*this)); |
313 | 2.72k | } |
314 | | |
315 | 2 | assert(MD->getParent()->mayInsertExtraPadding() && |
316 | 2 | "unknown trivial member function"); |
317 | 2 | } |
318 | | |
319 | | // Compute the function type we're calling. |
320 | 76.3k | const CXXMethodDecl *CalleeDecl = |
321 | 76.3k | DevirtualizedMethod ? DevirtualizedMethod414 : MD75.9k ; |
322 | 76.3k | const CGFunctionInfo *FInfo = nullptr; |
323 | 76.3k | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) |
324 | 206 | FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
325 | 206 | GlobalDecl(Dtor, Dtor_Complete)); |
326 | 76.1k | else |
327 | 76.1k | FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); |
328 | | |
329 | 76.3k | llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo); |
330 | | |
331 | | // C++11 [class.mfct.non-static]p2: |
332 | | // If a non-static member function of a class X is called for an object that |
333 | | // is not of type X, or of a type derived from X, the behavior is undefined. |
334 | 76.3k | SourceLocation CallLoc; |
335 | 76.3k | ASTContext &C = getContext(); |
336 | 76.3k | if (CE) |
337 | 76.3k | CallLoc = CE->getExprLoc(); |
338 | | |
339 | 76.3k | SanitizerSet SkippedChecks; |
340 | 76.3k | if (const auto *CMCE = dyn_cast<CXXMemberCallExpr>(CE)) { |
341 | 67.4k | auto *IOA = CMCE->getImplicitObjectArgument(); |
342 | 67.4k | bool IsImplicitObjectCXXThis = IsWrappedCXXThis(IOA); |
343 | 67.4k | if (IsImplicitObjectCXXThis) |
344 | 24.0k | SkippedChecks.set(SanitizerKind::Alignment, true); |
345 | 67.4k | if (IsImplicitObjectCXXThis || isa<DeclRefExpr>(IOA)43.3k ) |
346 | 39.5k | SkippedChecks.set(SanitizerKind::Null, true); |
347 | 67.4k | } |
348 | 76.3k | EmitTypeCheck(CodeGenFunction::TCK_MemberCall, CallLoc, |
349 | 76.3k | This.getPointer(*this), |
350 | 76.3k | C.getRecordType(CalleeDecl->getParent()), |
351 | 76.3k | /*Alignment=*/CharUnits::Zero(), SkippedChecks); |
352 | | |
353 | | // C++ [class.virtual]p12: |
354 | | // Explicit qualification with the scope operator (5.1) suppresses the |
355 | | // virtual call mechanism. |
356 | | // |
357 | | // We also don't emit a virtual call if the base expression has a record type |
358 | | // because then we know what the type is. |
359 | 76.3k | bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod1.35k ; |
360 | | |
361 | 76.3k | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) { |
362 | 206 | assert(CE->arg_begin() == CE->arg_end() && |
363 | 206 | "Destructor shouldn't have explicit parameters"); |
364 | 0 | assert(ReturnValue.isNull() && "Destructor shouldn't have return value"); |
365 | 206 | if (UseVirtualCall) { |
366 | 12 | CGM.getCXXABI().EmitVirtualDestructorCall(*this, Dtor, Dtor_Complete, |
367 | 12 | This.getAddress(*this), |
368 | 12 | cast<CXXMemberCallExpr>(CE)); |
369 | 194 | } else { |
370 | 194 | GlobalDecl GD(Dtor, Dtor_Complete); |
371 | 194 | CGCallee Callee; |
372 | 194 | if (getLangOpts().AppleKext && Dtor->isVirtual()2 && HasQualifier2 ) |
373 | 2 | Callee = BuildAppleKextVirtualCall(Dtor, Qualifier, Ty); |
374 | 192 | else if (!DevirtualizedMethod) |
375 | 190 | Callee = |
376 | 190 | CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD, FInfo, Ty), GD); |
377 | 2 | else { |
378 | 2 | Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(GD, Ty), GD); |
379 | 2 | } |
380 | | |
381 | 194 | QualType ThisTy = |
382 | 194 | IsArrow ? Base->getType()->getPointeeType()172 : Base->getType()22 ; |
383 | 194 | EmitCXXDestructorCall(GD, Callee, This.getPointer(*this), ThisTy, |
384 | 194 | /*ImplicitParam=*/nullptr, |
385 | 194 | /*ImplicitParamTy=*/QualType(), CE); |
386 | 194 | } |
387 | 206 | return RValue::get(nullptr); |
388 | 206 | } |
389 | | |
390 | | // FIXME: Uses of 'MD' past this point need to be audited. We may need to use |
391 | | // 'CalleeDecl' instead. |
392 | | |
393 | 76.1k | CGCallee Callee; |
394 | 76.1k | if (UseVirtualCall) { |
395 | 933 | Callee = CGCallee::forVirtual(CE, MD, This.getAddress(*this), Ty); |
396 | 75.2k | } else { |
397 | 75.2k | if (SanOpts.has(SanitizerKind::CFINVCall) && |
398 | 75.2k | MD->getParent()->isDynamicClass()8 ) { |
399 | 8 | llvm::Value *VTable; |
400 | 8 | const CXXRecordDecl *RD; |
401 | 8 | std::tie(VTable, RD) = CGM.getCXXABI().LoadVTablePtr( |
402 | 8 | *this, This.getAddress(*this), CalleeDecl->getParent()); |
403 | 8 | EmitVTablePtrCheckForCall(RD, VTable, CFITCK_NVCall, CE->getBeginLoc()); |
404 | 8 | } |
405 | | |
406 | 75.2k | if (getLangOpts().AppleKext && MD->isVirtual()6 && HasQualifier6 ) |
407 | 6 | Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); |
408 | 75.1k | else if (!DevirtualizedMethod) |
409 | 74.7k | Callee = |
410 | 74.7k | CGCallee::forDirect(CGM.GetAddrOfFunction(MD, Ty), GlobalDecl(MD)); |
411 | 412 | else { |
412 | 412 | Callee = |
413 | 412 | CGCallee::forDirect(CGM.GetAddrOfFunction(DevirtualizedMethod, Ty), |
414 | 412 | GlobalDecl(DevirtualizedMethod)); |
415 | 412 | } |
416 | 75.2k | } |
417 | | |
418 | 76.1k | if (MD->isVirtual()) { |
419 | 1.37k | Address NewThisAddr = |
420 | 1.37k | CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall( |
421 | 1.37k | *this, CalleeDecl, This.getAddress(*this), UseVirtualCall); |
422 | 1.37k | This.setAddress(NewThisAddr); |
423 | 1.37k | } |
424 | | |
425 | 76.1k | return EmitCXXMemberOrOperatorCall( |
426 | 76.1k | CalleeDecl, Callee, ReturnValue, This.getPointer(*this), |
427 | 76.1k | /*ImplicitParam=*/nullptr, QualType(), CE, RtlArgs); |
428 | 76.3k | } |
429 | | |
430 | | RValue |
431 | | CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
432 | 149 | ReturnValueSlot ReturnValue) { |
433 | 149 | const BinaryOperator *BO = |
434 | 149 | cast<BinaryOperator>(E->getCallee()->IgnoreParens()); |
435 | 149 | const Expr *BaseExpr = BO->getLHS(); |
436 | 149 | const Expr *MemFnExpr = BO->getRHS(); |
437 | | |
438 | 149 | const auto *MPT = MemFnExpr->getType()->castAs<MemberPointerType>(); |
439 | 149 | const auto *FPT = MPT->getPointeeType()->castAs<FunctionProtoType>(); |
440 | 149 | const auto *RD = |
441 | 149 | cast<CXXRecordDecl>(MPT->getClass()->castAs<RecordType>()->getDecl()); |
442 | | |
443 | | // Emit the 'this' pointer. |
444 | 149 | Address This = Address::invalid(); |
445 | 149 | if (BO->getOpcode() == BO_PtrMemI) |
446 | 88 | This = EmitPointerWithAlignment(BaseExpr); |
447 | 61 | else |
448 | 61 | This = EmitLValue(BaseExpr).getAddress(*this); |
449 | | |
450 | 149 | EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This.getPointer(), |
451 | 149 | QualType(MPT->getClass(), 0)); |
452 | | |
453 | | // Get the member function pointer. |
454 | 149 | llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); |
455 | | |
456 | | // Ask the ABI to load the callee. Note that This is modified. |
457 | 149 | llvm::Value *ThisPtrForCall = nullptr; |
458 | 149 | CGCallee Callee = |
459 | 149 | CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This, |
460 | 149 | ThisPtrForCall, MemFnPtr, MPT); |
461 | | |
462 | 149 | CallArgList Args; |
463 | | |
464 | 149 | QualType ThisType = |
465 | 149 | getContext().getPointerType(getContext().getTagDeclType(RD)); |
466 | | |
467 | | // Push the this ptr. |
468 | 149 | Args.add(RValue::get(ThisPtrForCall), ThisType); |
469 | | |
470 | 149 | RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1); |
471 | | |
472 | | // And the rest of the call args |
473 | 149 | EmitCallArgs(Args, FPT, E->arguments()); |
474 | 149 | return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required, |
475 | 149 | /*PrefixSize=*/0), |
476 | 149 | Callee, ReturnValue, Args, nullptr, E == MustTailCall, |
477 | 149 | E->getExprLoc()); |
478 | 149 | } |
479 | | |
480 | | RValue |
481 | | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
482 | | const CXXMethodDecl *MD, |
483 | 11.4k | ReturnValueSlot ReturnValue) { |
484 | 11.4k | assert(MD->isInstance() && |
485 | 11.4k | "Trying to emit a member call expr on a static method!"); |
486 | 0 | return EmitCXXMemberOrOperatorMemberCallExpr( |
487 | 11.4k | E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr, |
488 | 11.4k | /*IsArrow=*/false, E->getArg(0)); |
489 | 11.4k | } |
490 | | |
491 | | RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
492 | 63 | ReturnValueSlot ReturnValue) { |
493 | 63 | return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); |
494 | 63 | } |
495 | | |
496 | | static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, |
497 | | Address DestPtr, |
498 | 530 | const CXXRecordDecl *Base) { |
499 | 530 | if (Base->isEmpty()) |
500 | 511 | return; |
501 | | |
502 | 19 | DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); |
503 | | |
504 | 19 | const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); |
505 | 19 | CharUnits NVSize = Layout.getNonVirtualSize(); |
506 | | |
507 | | // We cannot simply zero-initialize the entire base sub-object if vbptrs are |
508 | | // present, they are initialized by the most derived class before calling the |
509 | | // constructor. |
510 | 19 | SmallVector<std::pair<CharUnits, CharUnits>, 1> Stores; |
511 | 19 | Stores.emplace_back(CharUnits::Zero(), NVSize); |
512 | | |
513 | | // Each store is split by the existence of a vbptr. |
514 | 19 | CharUnits VBPtrWidth = CGF.getPointerSize(); |
515 | 19 | std::vector<CharUnits> VBPtrOffsets = |
516 | 19 | CGF.CGM.getCXXABI().getVBPtrOffsets(Base); |
517 | 19 | for (CharUnits VBPtrOffset : VBPtrOffsets) { |
518 | | // Stop before we hit any virtual base pointers located in virtual bases. |
519 | 6 | if (VBPtrOffset >= NVSize) |
520 | 2 | break; |
521 | 4 | std::pair<CharUnits, CharUnits> LastStore = Stores.pop_back_val(); |
522 | 4 | CharUnits LastStoreOffset = LastStore.first; |
523 | 4 | CharUnits LastStoreSize = LastStore.second; |
524 | | |
525 | 4 | CharUnits SplitBeforeOffset = LastStoreOffset; |
526 | 4 | CharUnits SplitBeforeSize = VBPtrOffset - SplitBeforeOffset; |
527 | 4 | assert(!SplitBeforeSize.isNegative() && "negative store size!"); |
528 | 4 | if (!SplitBeforeSize.isZero()) |
529 | 2 | Stores.emplace_back(SplitBeforeOffset, SplitBeforeSize); |
530 | | |
531 | 4 | CharUnits SplitAfterOffset = VBPtrOffset + VBPtrWidth; |
532 | 4 | CharUnits SplitAfterSize = LastStoreSize - SplitAfterOffset; |
533 | 4 | assert(!SplitAfterSize.isNegative() && "negative store size!"); |
534 | 4 | if (!SplitAfterSize.isZero()) |
535 | 4 | Stores.emplace_back(SplitAfterOffset, SplitAfterSize); |
536 | 4 | } |
537 | | |
538 | | // If the type contains a pointer to data member we can't memset it to zero. |
539 | | // Instead, create a null constant and copy it to the destination. |
540 | | // TODO: there are other patterns besides zero that we can usefully memset, |
541 | | // like -1, which happens to be the pattern used by member-pointers. |
542 | | // TODO: isZeroInitializable can be over-conservative in the case where a |
543 | | // virtual base contains a member pointer. |
544 | 19 | llvm::Constant *NullConstantForBase = CGF.CGM.EmitNullConstantForBase(Base); |
545 | 19 | if (!NullConstantForBase->isNullValue()) { |
546 | 4 | llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable( |
547 | 4 | CGF.CGM.getModule(), NullConstantForBase->getType(), |
548 | 4 | /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, |
549 | 4 | NullConstantForBase, Twine()); |
550 | | |
551 | 4 | CharUnits Align = |
552 | 4 | std::max(Layout.getNonVirtualAlignment(), DestPtr.getAlignment()); |
553 | 4 | NullVariable->setAlignment(Align.getAsAlign()); |
554 | | |
555 | 4 | Address SrcPtr = |
556 | 4 | Address(CGF.EmitCastToVoidPtr(NullVariable), CGF.Int8Ty, Align); |
557 | | |
558 | | // Get and call the appropriate llvm.memcpy overload. |
559 | 4 | for (std::pair<CharUnits, CharUnits> Store : Stores) { |
560 | 4 | CharUnits StoreOffset = Store.first; |
561 | 4 | CharUnits StoreSize = Store.second; |
562 | 4 | llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); |
563 | 4 | CGF.Builder.CreateMemCpy( |
564 | 4 | CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), |
565 | 4 | CGF.Builder.CreateConstInBoundsByteGEP(SrcPtr, StoreOffset), |
566 | 4 | StoreSizeVal); |
567 | 4 | } |
568 | | |
569 | | // Otherwise, just memset the whole thing to zero. This is legal |
570 | | // because in LLVM, all default initializers (other than the ones we just |
571 | | // handled above) are guaranteed to have a bit pattern of all zeros. |
572 | 15 | } else { |
573 | 17 | for (std::pair<CharUnits, CharUnits> Store : Stores) { |
574 | 17 | CharUnits StoreOffset = Store.first; |
575 | 17 | CharUnits StoreSize = Store.second; |
576 | 17 | llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); |
577 | 17 | CGF.Builder.CreateMemSet( |
578 | 17 | CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), |
579 | 17 | CGF.Builder.getInt8(0), StoreSizeVal); |
580 | 17 | } |
581 | 15 | } |
582 | 19 | } |
583 | | |
584 | | void |
585 | | CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, |
586 | 66.0k | AggValueSlot Dest) { |
587 | 66.0k | assert(!Dest.isIgnored() && "Must have a destination!"); |
588 | 0 | const CXXConstructorDecl *CD = E->getConstructor(); |
589 | | |
590 | | // If we require zero initialization before (or instead of) calling the |
591 | | // constructor, as can be the case with a non-user-provided default |
592 | | // constructor, emit the zero initialization now, unless destination is |
593 | | // already zeroed. |
594 | 66.0k | if (E->requiresZeroInitialization() && !Dest.isZeroed()7.13k ) { |
595 | 7.13k | switch (E->getConstructionKind()) { |
596 | 2 | case CXXConstructExpr::CK_Delegating: |
597 | 6.60k | case CXXConstructExpr::CK_Complete: |
598 | 6.60k | EmitNullInitialization(Dest.getAddress(), E->getType()); |
599 | 6.60k | break; |
600 | 0 | case CXXConstructExpr::CK_VirtualBase: |
601 | 530 | case CXXConstructExpr::CK_NonVirtualBase: |
602 | 530 | EmitNullBaseClassInitialization(*this, Dest.getAddress(), |
603 | 530 | CD->getParent()); |
604 | 530 | break; |
605 | 7.13k | } |
606 | 7.13k | } |
607 | | |
608 | | // If this is a call to a trivial default constructor, do nothing. |
609 | 66.0k | if (CD->isTrivial() && CD->isDefaultConstructor()24.0k ) |
610 | 7.36k | return; |
611 | | |
612 | | // Elide the constructor if we're constructing from a temporary. |
613 | 58.6k | if (getLangOpts().ElideConstructors && E->isElidable()58.6k ) { |
614 | | // FIXME: This only handles the simplest case, where the source object |
615 | | // is passed directly as the first argument to the constructor. |
616 | | // This should also handle stepping though implicit casts and |
617 | | // conversion sequences which involve two steps, with a |
618 | | // conversion operator followed by a converting constructor. |
619 | 13.8k | const Expr *SrcObj = E->getArg(0); |
620 | 13.8k | assert(SrcObj->isTemporaryObject(getContext(), CD->getParent())); |
621 | 0 | assert( |
622 | 13.8k | getContext().hasSameUnqualifiedType(E->getType(), SrcObj->getType())); |
623 | 0 | EmitAggExpr(SrcObj, Dest); |
624 | 13.8k | return; |
625 | 13.8k | } |
626 | | |
627 | 44.7k | if (const ArrayType *arrayType |
628 | 44.7k | = getContext().getAsArrayType(E->getType())) { |
629 | 899 | EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E, |
630 | 899 | Dest.isSanitizerChecked()); |
631 | 43.8k | } else { |
632 | 43.8k | CXXCtorType Type = Ctor_Complete; |
633 | 43.8k | bool ForVirtualBase = false; |
634 | 43.8k | bool Delegating = false; |
635 | | |
636 | 43.8k | switch (E->getConstructionKind()) { |
637 | 87 | case CXXConstructExpr::CK_Delegating: |
638 | | // We should be emitting a constructor; GlobalDecl will assert this |
639 | 87 | Type = CurGD.getCtorType(); |
640 | 87 | Delegating = true; |
641 | 87 | break; |
642 | | |
643 | 35.1k | case CXXConstructExpr::CK_Complete: |
644 | 35.1k | Type = Ctor_Complete; |
645 | 35.1k | break; |
646 | | |
647 | 553 | case CXXConstructExpr::CK_VirtualBase: |
648 | 553 | ForVirtualBase = true; |
649 | 553 | LLVM_FALLTHROUGH; |
650 | | |
651 | 8.67k | case CXXConstructExpr::CK_NonVirtualBase: |
652 | 8.67k | Type = Ctor_Base; |
653 | 43.8k | } |
654 | | |
655 | | // Call the constructor. |
656 | 43.8k | EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest, E); |
657 | 43.8k | } |
658 | 44.7k | } |
659 | | |
660 | | void CodeGenFunction::EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, |
661 | 62 | const Expr *Exp) { |
662 | 62 | if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp)) |
663 | 2 | Exp = E->getSubExpr(); |
664 | 62 | assert(isa<CXXConstructExpr>(Exp) && |
665 | 62 | "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); |
666 | 0 | const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp); |
667 | 62 | const CXXConstructorDecl *CD = E->getConstructor(); |
668 | 62 | RunCleanupsScope Scope(*this); |
669 | | |
670 | | // If we require zero initialization before (or instead of) calling the |
671 | | // constructor, as can be the case with a non-user-provided default |
672 | | // constructor, emit the zero initialization now. |
673 | | // FIXME. Do I still need this for a copy ctor synthesis? |
674 | 62 | if (E->requiresZeroInitialization()) |
675 | 0 | EmitNullInitialization(Dest, E->getType()); |
676 | | |
677 | 62 | assert(!getContext().getAsConstantArrayType(E->getType()) |
678 | 62 | && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); |
679 | 0 | EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E); |
680 | 62 | } |
681 | | |
682 | | static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, |
683 | 2.30k | const CXXNewExpr *E) { |
684 | 2.30k | if (!E->isArray()) |
685 | 1.60k | return CharUnits::Zero(); |
686 | | |
687 | | // No cookie is required if the operator new[] being used is the |
688 | | // reserved placement operator new[]. |
689 | 700 | if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) |
690 | 16 | return CharUnits::Zero(); |
691 | | |
692 | 684 | return CGF.CGM.getCXXABI().GetArrayCookieSize(E); |
693 | 700 | } |
694 | | |
695 | | static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, |
696 | | const CXXNewExpr *e, |
697 | | unsigned minElements, |
698 | | llvm::Value *&numElements, |
699 | 1.95k | llvm::Value *&sizeWithoutCookie) { |
700 | 1.95k | QualType type = e->getAllocatedType(); |
701 | | |
702 | 1.95k | if (!e->isArray()) { |
703 | 1.60k | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
704 | 1.60k | sizeWithoutCookie |
705 | 1.60k | = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); |
706 | 1.60k | return sizeWithoutCookie; |
707 | 1.60k | } |
708 | | |
709 | | // The width of size_t. |
710 | 350 | unsigned sizeWidth = CGF.SizeTy->getBitWidth(); |
711 | | |
712 | | // Figure out the cookie size. |
713 | 350 | llvm::APInt cookieSize(sizeWidth, |
714 | 350 | CalculateCookiePadding(CGF, e).getQuantity()); |
715 | | |
716 | | // Emit the array size expression. |
717 | | // We multiply the size of all dimensions for NumElements. |
718 | | // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. |
719 | 350 | numElements = |
720 | 350 | ConstantEmitter(CGF).tryEmitAbstract(*e->getArraySize(), e->getType()); |
721 | 350 | if (!numElements) |
722 | 125 | numElements = CGF.EmitScalarExpr(*e->getArraySize()); |
723 | 350 | assert(isa<llvm::IntegerType>(numElements->getType())); |
724 | | |
725 | | // The number of elements can be have an arbitrary integer type; |
726 | | // essentially, we need to multiply it by a constant factor, add a |
727 | | // cookie size, and verify that the result is representable as a |
728 | | // size_t. That's just a gloss, though, and it's wrong in one |
729 | | // important way: if the count is negative, it's an error even if |
730 | | // the cookie size would bring the total size >= 0. |
731 | 0 | bool isSigned |
732 | 350 | = (*e->getArraySize())->getType()->isSignedIntegerOrEnumerationType(); |
733 | 350 | llvm::IntegerType *numElementsType |
734 | 350 | = cast<llvm::IntegerType>(numElements->getType()); |
735 | 350 | unsigned numElementsWidth = numElementsType->getBitWidth(); |
736 | | |
737 | | // Compute the constant factor. |
738 | 350 | llvm::APInt arraySizeMultiplier(sizeWidth, 1); |
739 | 375 | while (const ConstantArrayType *CAT |
740 | 350 | = CGF.getContext().getAsConstantArrayType(type)) { |
741 | 25 | type = CAT->getElementType(); |
742 | 25 | arraySizeMultiplier *= CAT->getSize(); |
743 | 25 | } |
744 | | |
745 | 350 | CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); |
746 | 350 | llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); |
747 | 350 | typeSizeMultiplier *= arraySizeMultiplier; |
748 | | |
749 | | // This will be a size_t. |
750 | 350 | llvm::Value *size; |
751 | | |
752 | | // If someone is doing 'new int[42]' there is no need to do a dynamic check. |
753 | | // Don't bloat the -O0 code. |
754 | 350 | if (llvm::ConstantInt *numElementsC = |
755 | 350 | dyn_cast<llvm::ConstantInt>(numElements)) { |
756 | 225 | const llvm::APInt &count = numElementsC->getValue(); |
757 | | |
758 | 225 | bool hasAnyOverflow = false; |
759 | | |
760 | | // If 'count' was a negative number, it's an overflow. |
761 | 225 | if (isSigned && count.isNegative()155 ) |
762 | 0 | hasAnyOverflow = true; |
763 | | |
764 | | // We want to do all this arithmetic in size_t. If numElements is |
765 | | // wider than that, check whether it's already too big, and if so, |
766 | | // overflow. |
767 | 225 | else if (numElementsWidth > sizeWidth && |
768 | 225 | numElementsWidth - sizeWidth > count.countLeadingZeros()0 ) |
769 | 0 | hasAnyOverflow = true; |
770 | | |
771 | | // Okay, compute a count at the right width. |
772 | 225 | llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); |
773 | | |
774 | | // If there is a brace-initializer, we cannot allocate fewer elements than |
775 | | // there are initializers. If we do, that's treated like an overflow. |
776 | 225 | if (adjustedCount.ult(minElements)) |
777 | 0 | hasAnyOverflow = true; |
778 | | |
779 | | // Scale numElements by that. This might overflow, but we don't |
780 | | // care because it only overflows if allocationSize does, too, and |
781 | | // if that overflows then we shouldn't use this. |
782 | 225 | numElements = llvm::ConstantInt::get(CGF.SizeTy, |
783 | 225 | adjustedCount * arraySizeMultiplier); |
784 | | |
785 | | // Compute the size before cookie, and track whether it overflowed. |
786 | 225 | bool overflow; |
787 | 225 | llvm::APInt allocationSize |
788 | 225 | = adjustedCount.umul_ov(typeSizeMultiplier, overflow); |
789 | 225 | hasAnyOverflow |= overflow; |
790 | | |
791 | | // Add in the cookie, and check whether it's overflowed. |
792 | 225 | if (cookieSize != 0) { |
793 | | // Save the current size without a cookie. This shouldn't be |
794 | | // used if there was overflow. |
795 | 43 | sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
796 | | |
797 | 43 | allocationSize = allocationSize.uadd_ov(cookieSize, overflow); |
798 | 43 | hasAnyOverflow |= overflow; |
799 | 43 | } |
800 | | |
801 | | // On overflow, produce a -1 so operator new will fail. |
802 | 225 | if (hasAnyOverflow) { |
803 | 0 | size = llvm::Constant::getAllOnesValue(CGF.SizeTy); |
804 | 225 | } else { |
805 | 225 | size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); |
806 | 225 | } |
807 | | |
808 | | // Otherwise, we might need to use the overflow intrinsics. |
809 | 225 | } else { |
810 | | // There are up to five conditions we need to test for: |
811 | | // 1) if isSigned, we need to check whether numElements is negative; |
812 | | // 2) if numElementsWidth > sizeWidth, we need to check whether |
813 | | // numElements is larger than something representable in size_t; |
814 | | // 3) if minElements > 0, we need to check whether numElements is smaller |
815 | | // than that. |
816 | | // 4) we need to compute |
817 | | // sizeWithoutCookie := numElements * typeSizeMultiplier |
818 | | // and check whether it overflows; and |
819 | | // 5) if we need a cookie, we need to compute |
820 | | // size := sizeWithoutCookie + cookieSize |
821 | | // and check whether it overflows. |
822 | | |
823 | 125 | llvm::Value *hasOverflow = nullptr; |
824 | | |
825 | | // If numElementsWidth > sizeWidth, then one way or another, we're |
826 | | // going to have to do a comparison for (2), and this happens to |
827 | | // take care of (1), too. |
828 | 125 | if (numElementsWidth > sizeWidth) { |
829 | 0 | llvm::APInt threshold(numElementsWidth, 1); |
830 | 0 | threshold <<= sizeWidth; |
831 | |
|
832 | 0 | llvm::Value *thresholdV |
833 | 0 | = llvm::ConstantInt::get(numElementsType, threshold); |
834 | |
|
835 | 0 | hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); |
836 | 0 | numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); |
837 | | |
838 | | // Otherwise, if we're signed, we want to sext up to size_t. |
839 | 125 | } else if (isSigned) { |
840 | 28 | if (numElementsWidth < sizeWidth) |
841 | 8 | numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); |
842 | | |
843 | | // If there's a non-1 type size multiplier, then we can do the |
844 | | // signedness check at the same time as we do the multiply |
845 | | // because a negative number times anything will cause an |
846 | | // unsigned overflow. Otherwise, we have to do it here. But at least |
847 | | // in this case, we can subsume the >= minElements check. |
848 | 28 | if (typeSizeMultiplier == 1) |
849 | 8 | hasOverflow = CGF.Builder.CreateICmpSLT(numElements, |
850 | 8 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
851 | | |
852 | | // Otherwise, zext up to size_t if necessary. |
853 | 97 | } else if (numElementsWidth < sizeWidth) { |
854 | 0 | numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); |
855 | 0 | } |
856 | | |
857 | 125 | assert(numElements->getType() == CGF.SizeTy); |
858 | | |
859 | 125 | if (minElements) { |
860 | | // Don't allow allocation of fewer elements than we have initializers. |
861 | 9 | if (!hasOverflow) { |
862 | 6 | hasOverflow = CGF.Builder.CreateICmpULT(numElements, |
863 | 6 | llvm::ConstantInt::get(CGF.SizeTy, minElements)); |
864 | 6 | } else if (3 numElementsWidth > sizeWidth3 ) { |
865 | | // The other existing overflow subsumes this check. |
866 | | // We do an unsigned comparison, since any signed value < -1 is |
867 | | // taken care of either above or below. |
868 | 0 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, |
869 | 0 | CGF.Builder.CreateICmpULT(numElements, |
870 | 0 | llvm::ConstantInt::get(CGF.SizeTy, minElements))); |
871 | 0 | } |
872 | 9 | } |
873 | | |
874 | 125 | size = numElements; |
875 | | |
876 | | // Multiply by the type size if necessary. This multiplier |
877 | | // includes all the factors for nested arrays. |
878 | | // |
879 | | // This step also causes numElements to be scaled up by the |
880 | | // nested-array factor if necessary. Overflow on this computation |
881 | | // can be ignored because the result shouldn't be used if |
882 | | // allocation fails. |
883 | 125 | if (typeSizeMultiplier != 1) { |
884 | 53 | llvm::Function *umul_with_overflow |
885 | 53 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); |
886 | | |
887 | 53 | llvm::Value *tsmV = |
888 | 53 | llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); |
889 | 53 | llvm::Value *result = |
890 | 53 | CGF.Builder.CreateCall(umul_with_overflow, {size, tsmV}); |
891 | | |
892 | 53 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
893 | 53 | if (hasOverflow) |
894 | 6 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
895 | 47 | else |
896 | 47 | hasOverflow = overflowed; |
897 | | |
898 | 53 | size = CGF.Builder.CreateExtractValue(result, 0); |
899 | | |
900 | | // Also scale up numElements by the array size multiplier. |
901 | 53 | if (arraySizeMultiplier != 1) { |
902 | | // If the base element type size is 1, then we can re-use the |
903 | | // multiply we just did. |
904 | 10 | if (typeSize.isOne()) { |
905 | 0 | assert(arraySizeMultiplier == typeSizeMultiplier); |
906 | 0 | numElements = size; |
907 | | |
908 | | // Otherwise we need a separate multiply. |
909 | 10 | } else { |
910 | 10 | llvm::Value *asmV = |
911 | 10 | llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); |
912 | 10 | numElements = CGF.Builder.CreateMul(numElements, asmV); |
913 | 10 | } |
914 | 10 | } |
915 | 72 | } else { |
916 | | // numElements doesn't need to be scaled. |
917 | 72 | assert(arraySizeMultiplier == 1); |
918 | 72 | } |
919 | | |
920 | | // Add in the cookie size if necessary. |
921 | 125 | if (cookieSize != 0) { |
922 | 19 | sizeWithoutCookie = size; |
923 | | |
924 | 19 | llvm::Function *uadd_with_overflow |
925 | 19 | = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); |
926 | | |
927 | 19 | llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); |
928 | 19 | llvm::Value *result = |
929 | 19 | CGF.Builder.CreateCall(uadd_with_overflow, {size, cookieSizeV}); |
930 | | |
931 | 19 | llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); |
932 | 19 | if (hasOverflow) |
933 | 18 | hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); |
934 | 1 | else |
935 | 1 | hasOverflow = overflowed; |
936 | | |
937 | 19 | size = CGF.Builder.CreateExtractValue(result, 0); |
938 | 19 | } |
939 | | |
940 | | // If we had any possibility of dynamic overflow, make a select to |
941 | | // overwrite 'size' with an all-ones value, which should cause |
942 | | // operator new to throw. |
943 | 125 | if (hasOverflow) |
944 | 62 | size = CGF.Builder.CreateSelect(hasOverflow, |
945 | 62 | llvm::Constant::getAllOnesValue(CGF.SizeTy), |
946 | 62 | size); |
947 | 125 | } |
948 | | |
949 | 350 | if (cookieSize == 0) |
950 | 288 | sizeWithoutCookie = size; |
951 | 62 | else |
952 | 62 | assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); |
953 | | |
954 | 0 | return size; |
955 | 1.95k | } |
956 | | |
957 | | static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, |
958 | | QualType AllocType, Address NewPtr, |
959 | 1.60k | AggValueSlot::Overlap_t MayOverlap) { |
960 | | // FIXME: Refactor with EmitExprAsInit. |
961 | 1.60k | switch (CGF.getEvaluationKind(AllocType)) { |
962 | 274 | case TEK_Scalar: |
963 | 274 | CGF.EmitScalarInit(Init, nullptr, |
964 | 274 | CGF.MakeAddrLValue(NewPtr, AllocType), false); |
965 | 274 | return; |
966 | 1 | case TEK_Complex: |
967 | 1 | CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType), |
968 | 1 | /*isInit*/ true); |
969 | 1 | return; |
970 | 1.33k | case TEK_Aggregate: { |
971 | 1.33k | AggValueSlot Slot |
972 | 1.33k | = AggValueSlot::forAddr(NewPtr, AllocType.getQualifiers(), |
973 | 1.33k | AggValueSlot::IsDestructed, |
974 | 1.33k | AggValueSlot::DoesNotNeedGCBarriers, |
975 | 1.33k | AggValueSlot::IsNotAliased, |
976 | 1.33k | MayOverlap, AggValueSlot::IsNotZeroed, |
977 | 1.33k | AggValueSlot::IsSanitizerChecked); |
978 | 1.33k | CGF.EmitAggExpr(Init, Slot); |
979 | 1.33k | return; |
980 | 0 | } |
981 | 1.60k | } |
982 | 0 | llvm_unreachable("bad evaluation kind"); |
983 | 0 | } |
984 | | |
985 | | void CodeGenFunction::EmitNewArrayInitializer( |
986 | | const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, |
987 | | Address BeginPtr, llvm::Value *NumElements, |
988 | 350 | llvm::Value *AllocSizeWithoutCookie) { |
989 | | // If we have a type with trivial initialization and no initializer, |
990 | | // there's nothing to do. |
991 | 350 | if (!E->hasInitializer()) |
992 | 197 | return; |
993 | | |
994 | 153 | Address CurPtr = BeginPtr; |
995 | | |
996 | 153 | unsigned InitListElements = 0; |
997 | | |
998 | 153 | const Expr *Init = E->getInitializer(); |
999 | 153 | Address EndOfInit = Address::invalid(); |
1000 | 153 | QualType::DestructionKind DtorKind = ElementType.isDestructedType(); |
1001 | 153 | EHScopeStack::stable_iterator Cleanup; |
1002 | 153 | llvm::Instruction *CleanupDominator = nullptr; |
1003 | | |
1004 | 153 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementType); |
1005 | 153 | CharUnits ElementAlign = |
1006 | 153 | BeginPtr.getAlignment().alignmentOfArrayElement(ElementSize); |
1007 | | |
1008 | | // Attempt to perform zero-initialization using memset. |
1009 | 153 | auto TryMemsetInitialization = [&]() -> bool { |
1010 | | // FIXME: If the type is a pointer-to-data-member under the Itanium ABI, |
1011 | | // we can initialize with a memset to -1. |
1012 | 21 | if (!CGM.getTypes().isZeroInitializable(ElementType)) |
1013 | 4 | return false; |
1014 | | |
1015 | | // Optimization: since zero initialization will just set the memory |
1016 | | // to all zeroes, generate a single memset to do it in one shot. |
1017 | | |
1018 | | // Subtract out the size of any elements we've already initialized. |
1019 | 17 | auto *RemainingSize = AllocSizeWithoutCookie; |
1020 | 17 | if (InitListElements) { |
1021 | | // We know this can't overflow; we check this when doing the allocation. |
1022 | 9 | auto *InitializedSize = llvm::ConstantInt::get( |
1023 | 9 | RemainingSize->getType(), |
1024 | 9 | getContext().getTypeSizeInChars(ElementType).getQuantity() * |
1025 | 9 | InitListElements); |
1026 | 9 | RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize); |
1027 | 9 | } |
1028 | | |
1029 | | // Create the memset. |
1030 | 17 | Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize, false); |
1031 | 17 | return true; |
1032 | 21 | }; |
1033 | | |
1034 | | // If the initializer is an initializer list, first do the explicit elements. |
1035 | 153 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
1036 | | // Initializing from a (braced) string literal is a special case; the init |
1037 | | // list element does not initialize a (single) array element. |
1038 | 28 | if (ILE->isStringLiteralInit()) { |
1039 | | // Initialize the initial portion of length equal to that of the string |
1040 | | // literal. The allocation must be for at least this much; we emitted a |
1041 | | // check for that earlier. |
1042 | 8 | AggValueSlot Slot = |
1043 | 8 | AggValueSlot::forAddr(CurPtr, ElementType.getQualifiers(), |
1044 | 8 | AggValueSlot::IsDestructed, |
1045 | 8 | AggValueSlot::DoesNotNeedGCBarriers, |
1046 | 8 | AggValueSlot::IsNotAliased, |
1047 | 8 | AggValueSlot::DoesNotOverlap, |
1048 | 8 | AggValueSlot::IsNotZeroed, |
1049 | 8 | AggValueSlot::IsSanitizerChecked); |
1050 | 8 | EmitAggExpr(ILE->getInit(0), Slot); |
1051 | | |
1052 | | // Move past these elements. |
1053 | 8 | InitListElements = |
1054 | 8 | cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe()) |
1055 | 8 | ->getSize().getZExtValue(); |
1056 | 8 | CurPtr = Builder.CreateConstInBoundsGEP( |
1057 | 8 | CurPtr, InitListElements, "string.init.end"); |
1058 | | |
1059 | | // Zero out the rest, if any remain. |
1060 | 8 | llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); |
1061 | 8 | if (!ConstNum || !ConstNum->equalsInt(InitListElements)6 ) { |
1062 | 2 | bool OK = TryMemsetInitialization(); |
1063 | 2 | (void)OK; |
1064 | 2 | assert(OK && "couldn't memset character type?"); |
1065 | 2 | } |
1066 | 0 | return; |
1067 | 8 | } |
1068 | | |
1069 | 20 | InitListElements = ILE->getNumInits(); |
1070 | | |
1071 | | // If this is a multi-dimensional array new, we will initialize multiple |
1072 | | // elements with each init list element. |
1073 | 20 | QualType AllocType = E->getAllocatedType(); |
1074 | 20 | if (const ConstantArrayType *CAT = dyn_cast_or_null<ConstantArrayType>( |
1075 | 20 | AllocType->getAsArrayTypeUnsafe())) { |
1076 | 3 | ElementTy = ConvertTypeForMem(AllocType); |
1077 | 3 | CurPtr = Builder.CreateElementBitCast(CurPtr, ElementTy); |
1078 | 3 | InitListElements *= getContext().getConstantArrayElementCount(CAT); |
1079 | 3 | } |
1080 | | |
1081 | | // Enter a partial-destruction Cleanup if necessary. |
1082 | 20 | if (needsEHCleanup(DtorKind)) { |
1083 | | // In principle we could tell the Cleanup where we are more |
1084 | | // directly, but the control flow can get so varied here that it |
1085 | | // would actually be quite complex. Therefore we go through an |
1086 | | // alloca. |
1087 | 2 | EndOfInit = CreateTempAlloca(BeginPtr.getType(), getPointerAlign(), |
1088 | 2 | "array.init.end"); |
1089 | 2 | CleanupDominator = Builder.CreateStore(BeginPtr.getPointer(), EndOfInit); |
1090 | 2 | pushIrregularPartialArrayCleanup(BeginPtr.getPointer(), EndOfInit, |
1091 | 2 | ElementType, ElementAlign, |
1092 | 2 | getDestroyer(DtorKind)); |
1093 | 2 | Cleanup = EHStack.stable_begin(); |
1094 | 2 | } |
1095 | | |
1096 | 20 | CharUnits StartAlign = CurPtr.getAlignment(); |
1097 | 64 | for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i44 ) { |
1098 | | // Tell the cleanup that it needs to destroy up to this |
1099 | | // element. TODO: some of these stores can be trivially |
1100 | | // observed to be unnecessary. |
1101 | 44 | if (EndOfInit.isValid()) { |
1102 | 6 | auto FinishedPtr = |
1103 | 6 | Builder.CreateBitCast(CurPtr.getPointer(), BeginPtr.getType()); |
1104 | 6 | Builder.CreateStore(FinishedPtr, EndOfInit); |
1105 | 6 | } |
1106 | | // FIXME: If the last initializer is an incomplete initializer list for |
1107 | | // an array, and we have an array filler, we can fold together the two |
1108 | | // initialization loops. |
1109 | 44 | StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), |
1110 | 44 | ILE->getInit(i)->getType(), CurPtr, |
1111 | 44 | AggValueSlot::DoesNotOverlap); |
1112 | 44 | CurPtr = Address(Builder.CreateInBoundsGEP( |
1113 | 44 | CurPtr.getElementType(), CurPtr.getPointer(), |
1114 | 44 | Builder.getSize(1), "array.exp.next"), |
1115 | 44 | CurPtr.getElementType(), |
1116 | 44 | StartAlign.alignmentAtOffset((i + 1) * ElementSize)); |
1117 | 44 | } |
1118 | | |
1119 | | // The remaining elements are filled with the array filler expression. |
1120 | 20 | Init = ILE->getArrayFiller(); |
1121 | | |
1122 | | // Extract the initializer for the individual array elements by pulling |
1123 | | // out the array filler from all the nested initializer lists. This avoids |
1124 | | // generating a nested loop for the initialization. |
1125 | 22 | while (Init && Init->getType()->isConstantArrayType()14 ) { |
1126 | 2 | auto *SubILE = dyn_cast<InitListExpr>(Init); |
1127 | 2 | if (!SubILE) |
1128 | 0 | break; |
1129 | 2 | assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?"); |
1130 | 0 | Init = SubILE->getArrayFiller(); |
1131 | 2 | } |
1132 | | |
1133 | | // Switch back to initializing one base element at a time. |
1134 | 20 | CurPtr = Builder.CreateElementBitCast(CurPtr, BeginPtr.getElementType()); |
1135 | 20 | } |
1136 | | |
1137 | | // If all elements have already been initialized, skip any further |
1138 | | // initialization. |
1139 | 145 | llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements); |
1140 | 145 | if (ConstNum && ConstNum->getZExtValue() <= InitListElements96 ) { |
1141 | | // If there was a Cleanup, deactivate it. |
1142 | 10 | if (CleanupDominator) |
1143 | 1 | DeactivateCleanupBlock(Cleanup, CleanupDominator); |
1144 | 10 | return; |
1145 | 10 | } |
1146 | | |
1147 | 135 | assert(Init && "have trailing elements to initialize but no initializer"); |
1148 | | |
1149 | | // If this is a constructor call, try to optimize it out, and failing that |
1150 | | // emit a single loop to initialize all remaining elements. |
1151 | 135 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
1152 | 118 | CXXConstructorDecl *Ctor = CCE->getConstructor(); |
1153 | 118 | if (Ctor->isTrivial()) { |
1154 | | // If new expression did not specify value-initialization, then there |
1155 | | // is no initialization. |
1156 | 54 | if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()3 ) |
1157 | 52 | return; |
1158 | | |
1159 | 2 | if (TryMemsetInitialization()) |
1160 | 1 | return; |
1161 | 2 | } |
1162 | | |
1163 | | // Store the new Cleanup position for irregular Cleanups. |
1164 | | // |
1165 | | // FIXME: Share this cleanup with the constructor call emission rather than |
1166 | | // having it create a cleanup of its own. |
1167 | 65 | if (EndOfInit.isValid()) |
1168 | 1 | Builder.CreateStore(CurPtr.getPointer(), EndOfInit); |
1169 | | |
1170 | | // Emit a constructor call loop to initialize the remaining elements. |
1171 | 65 | if (InitListElements) |
1172 | 2 | NumElements = Builder.CreateSub( |
1173 | 2 | NumElements, |
1174 | 2 | llvm::ConstantInt::get(NumElements->getType(), InitListElements)); |
1175 | 65 | EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE, |
1176 | 65 | /*NewPointerIsChecked*/true, |
1177 | 65 | CCE->requiresZeroInitialization()); |
1178 | 65 | return; |
1179 | 118 | } |
1180 | | |
1181 | | // If this is value-initialization, we can usually use memset. |
1182 | 17 | ImplicitValueInitExpr IVIE(ElementType); |
1183 | 17 | if (isa<ImplicitValueInitExpr>(Init)) { |
1184 | 14 | if (TryMemsetInitialization()) |
1185 | 11 | return; |
1186 | | |
1187 | | // Switch to an ImplicitValueInitExpr for the element type. This handles |
1188 | | // only one case: multidimensional array new of pointers to members. In |
1189 | | // all other cases, we already have an initializer for the array element. |
1190 | 3 | Init = &IVIE; |
1191 | 3 | } |
1192 | | |
1193 | | // At this point we should have found an initializer for the individual |
1194 | | // elements of the array. |
1195 | 6 | assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) && |
1196 | 6 | "got wrong type of element to initialize"); |
1197 | | |
1198 | | // If we have an empty initializer list, we can usually use memset. |
1199 | 6 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) |
1200 | 3 | if (ILE->getNumInits() == 0 && TryMemsetInitialization()0 ) |
1201 | 0 | return; |
1202 | | |
1203 | | // If we have a struct whose every field is value-initialized, we can |
1204 | | // usually use memset. |
1205 | 6 | if (auto *ILE = dyn_cast<InitListExpr>(Init)) { |
1206 | 3 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
1207 | 3 | if (RType->getDecl()->isStruct()) { |
1208 | 3 | unsigned NumElements = 0; |
1209 | 3 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RType->getDecl())) |
1210 | 3 | NumElements = CXXRD->getNumBases(); |
1211 | 3 | for (auto *Field : RType->getDecl()->fields()) |
1212 | 5 | if (!Field->isUnnamedBitfield()) |
1213 | 5 | ++NumElements; |
1214 | | // FIXME: Recurse into nested InitListExprs. |
1215 | 3 | if (ILE->getNumInits() == NumElements) |
1216 | 8 | for (unsigned i = 0, e = ILE->getNumInits(); 3 i != e; ++i5 ) |
1217 | 5 | if (!isa<ImplicitValueInitExpr>(ILE->getInit(i))) |
1218 | 0 | --NumElements; |
1219 | 3 | if (ILE->getNumInits() == NumElements && TryMemsetInitialization()) |
1220 | 3 | return; |
1221 | 3 | } |
1222 | 3 | } |
1223 | 3 | } |
1224 | | |
1225 | | // Create the loop blocks. |
1226 | 3 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
1227 | 3 | llvm::BasicBlock *LoopBB = createBasicBlock("new.loop"); |
1228 | 3 | llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end"); |
1229 | | |
1230 | | // Find the end of the array, hoisted out of the loop. |
1231 | 3 | llvm::Value *EndPtr = |
1232 | 3 | Builder.CreateInBoundsGEP(BeginPtr.getElementType(), BeginPtr.getPointer(), |
1233 | 3 | NumElements, "array.end"); |
1234 | | |
1235 | | // If the number of elements isn't constant, we have to now check if there is |
1236 | | // anything left to initialize. |
1237 | 3 | if (!ConstNum) { |
1238 | 0 | llvm::Value *IsEmpty = |
1239 | 0 | Builder.CreateICmpEQ(CurPtr.getPointer(), EndPtr, "array.isempty"); |
1240 | 0 | Builder.CreateCondBr(IsEmpty, ContBB, LoopBB); |
1241 | 0 | } |
1242 | | |
1243 | | // Enter the loop. |
1244 | 3 | EmitBlock(LoopBB); |
1245 | | |
1246 | | // Set up the current-element phi. |
1247 | 3 | llvm::PHINode *CurPtrPhi = |
1248 | 3 | Builder.CreatePHI(CurPtr.getType(), 2, "array.cur"); |
1249 | 3 | CurPtrPhi->addIncoming(CurPtr.getPointer(), EntryBB); |
1250 | | |
1251 | 3 | CurPtr = Address(CurPtrPhi, CurPtr.getElementType(), ElementAlign); |
1252 | | |
1253 | | // Store the new Cleanup position for irregular Cleanups. |
1254 | 3 | if (EndOfInit.isValid()) |
1255 | 0 | Builder.CreateStore(CurPtr.getPointer(), EndOfInit); |
1256 | | |
1257 | | // Enter a partial-destruction Cleanup if necessary. |
1258 | 3 | if (!CleanupDominator && needsEHCleanup(DtorKind)) { |
1259 | 0 | pushRegularPartialArrayCleanup(BeginPtr.getPointer(), CurPtr.getPointer(), |
1260 | 0 | ElementType, ElementAlign, |
1261 | 0 | getDestroyer(DtorKind)); |
1262 | 0 | Cleanup = EHStack.stable_begin(); |
1263 | 0 | CleanupDominator = Builder.CreateUnreachable(); |
1264 | 0 | } |
1265 | | |
1266 | | // Emit the initializer into this element. |
1267 | 3 | StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr, |
1268 | 3 | AggValueSlot::DoesNotOverlap); |
1269 | | |
1270 | | // Leave the Cleanup if we entered one. |
1271 | 3 | if (CleanupDominator) { |
1272 | 0 | DeactivateCleanupBlock(Cleanup, CleanupDominator); |
1273 | 0 | CleanupDominator->eraseFromParent(); |
1274 | 0 | } |
1275 | | |
1276 | | // Advance to the next element by adjusting the pointer type as necessary. |
1277 | 3 | llvm::Value *NextPtr = |
1278 | 3 | Builder.CreateConstInBoundsGEP1_32(ElementTy, CurPtr.getPointer(), 1, |
1279 | 3 | "array.next"); |
1280 | | |
1281 | | // Check whether we've gotten to the end of the array and, if so, |
1282 | | // exit the loop. |
1283 | 3 | llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend"); |
1284 | 3 | Builder.CreateCondBr(IsEnd, ContBB, LoopBB); |
1285 | 3 | CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock()); |
1286 | | |
1287 | 3 | EmitBlock(ContBB); |
1288 | 3 | } |
1289 | | |
1290 | | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
1291 | | QualType ElementType, llvm::Type *ElementTy, |
1292 | | Address NewPtr, llvm::Value *NumElements, |
1293 | 1.95k | llvm::Value *AllocSizeWithoutCookie) { |
1294 | 1.95k | ApplyDebugLocation DL(CGF, E); |
1295 | 1.95k | if (E->isArray()) |
1296 | 350 | CGF.EmitNewArrayInitializer(E, ElementType, ElementTy, NewPtr, NumElements, |
1297 | 350 | AllocSizeWithoutCookie); |
1298 | 1.60k | else if (const Expr *Init = E->getInitializer()) |
1299 | 1.56k | StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr, |
1300 | 1.56k | AggValueSlot::DoesNotOverlap); |
1301 | 1.95k | } |
1302 | | |
1303 | | /// Emit a call to an operator new or operator delete function, as implicitly |
1304 | | /// created by new-expressions and delete-expressions. |
1305 | | static RValue EmitNewDeleteCall(CodeGenFunction &CGF, |
1306 | | const FunctionDecl *CalleeDecl, |
1307 | | const FunctionProtoType *CalleeType, |
1308 | 4.04k | const CallArgList &Args) { |
1309 | 4.04k | llvm::CallBase *CallOrInvoke; |
1310 | 4.04k | llvm::Constant *CalleePtr = CGF.CGM.GetAddrOfFunction(CalleeDecl); |
1311 | 4.04k | CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(CalleeDecl)); |
1312 | 4.04k | RValue RV = |
1313 | 4.04k | CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall( |
1314 | 4.04k | Args, CalleeType, /*ChainCall=*/false), |
1315 | 4.04k | Callee, ReturnValueSlot(), Args, &CallOrInvoke); |
1316 | | |
1317 | | /// C++1y [expr.new]p10: |
1318 | | /// [In a new-expression,] an implementation is allowed to omit a call |
1319 | | /// to a replaceable global allocation function. |
1320 | | /// |
1321 | | /// We model such elidable calls with the 'builtin' attribute. |
1322 | 4.04k | llvm::Function *Fn = dyn_cast<llvm::Function>(CalleePtr); |
1323 | 4.04k | if (CalleeDecl->isReplaceableGlobalAllocationFunction() && |
1324 | 4.04k | Fn3.72k && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)3.72k ) { |
1325 | 3.72k | CallOrInvoke->addFnAttr(llvm::Attribute::Builtin); |
1326 | 3.72k | } |
1327 | | |
1328 | 4.04k | return RV; |
1329 | 4.04k | } |
1330 | | |
1331 | | RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, |
1332 | | const CallExpr *TheCall, |
1333 | 668 | bool IsDelete) { |
1334 | 668 | CallArgList Args; |
1335 | 668 | EmitCallArgs(Args, Type, TheCall->arguments()); |
1336 | | // Find the allocation or deallocation function that we're calling. |
1337 | 668 | ASTContext &Ctx = getContext(); |
1338 | 668 | DeclarationName Name = Ctx.DeclarationNames |
1339 | 668 | .getCXXOperatorName(IsDelete ? OO_Delete351 : OO_New317 ); |
1340 | | |
1341 | 668 | for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name)) |
1342 | 690 | if (auto *FD = dyn_cast<FunctionDecl>(Decl)) |
1343 | 690 | if (Ctx.hasSameType(FD->getType(), QualType(Type, 0))) |
1344 | 668 | return EmitNewDeleteCall(*this, FD, Type, Args); |
1345 | 0 | llvm_unreachable("predeclared global operator new/delete is missing"); |
1346 | 0 | } |
1347 | | |
1348 | | namespace { |
1349 | | /// The parameters to pass to a usual operator delete. |
1350 | | struct UsualDeleteParams { |
1351 | | bool DestroyingDelete = false; |
1352 | | bool Size = false; |
1353 | | bool Alignment = false; |
1354 | | }; |
1355 | | } |
1356 | | |
1357 | 2.04k | static UsualDeleteParams getUsualDeleteParams(const FunctionDecl *FD) { |
1358 | 2.04k | UsualDeleteParams Params; |
1359 | | |
1360 | 2.04k | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
1361 | 2.04k | auto AI = FPT->param_type_begin(), AE = FPT->param_type_end(); |
1362 | | |
1363 | | // The first argument is always a void*. |
1364 | 2.04k | ++AI; |
1365 | | |
1366 | | // The next parameter may be a std::destroying_delete_t. |
1367 | 2.04k | if (FD->isDestroyingOperatorDelete()) { |
1368 | 30 | Params.DestroyingDelete = true; |
1369 | 30 | assert(AI != AE); |
1370 | 0 | ++AI; |
1371 | 30 | } |
1372 | | |
1373 | | // Figure out what other parameters we should be implicitly passing. |
1374 | 2.04k | if (AI != AE && (*AI)->isIntegerType()122 ) { |
1375 | 90 | Params.Size = true; |
1376 | 90 | ++AI; |
1377 | 90 | } |
1378 | | |
1379 | 2.04k | if (AI != AE && (*AI)->isAlignValT()58 ) { |
1380 | 58 | Params.Alignment = true; |
1381 | 58 | ++AI; |
1382 | 58 | } |
1383 | | |
1384 | 2.04k | assert(AI == AE && "unexpected usual deallocation function parameter"); |
1385 | 0 | return Params; |
1386 | 2.04k | } |
1387 | | |
1388 | | namespace { |
1389 | | /// A cleanup to call the given 'operator delete' function upon abnormal |
1390 | | /// exit from a new expression. Templated on a traits type that deals with |
1391 | | /// ensuring that the arguments dominate the cleanup if necessary. |
1392 | | template<typename Traits> |
1393 | | class CallDeleteDuringNew final : public EHScopeStack::Cleanup { |
1394 | | /// Type used to hold llvm::Value*s. |
1395 | | typedef typename Traits::ValueTy ValueTy; |
1396 | | /// Type used to hold RValues. |
1397 | | typedef typename Traits::RValueTy RValueTy; |
1398 | | struct PlacementArg { |
1399 | | RValueTy ArgValue; |
1400 | | QualType ArgType; |
1401 | | }; |
1402 | | |
1403 | | unsigned NumPlacementArgs : 31; |
1404 | | unsigned PassAlignmentToPlacementDelete : 1; |
1405 | | const FunctionDecl *OperatorDelete; |
1406 | | ValueTy Ptr; |
1407 | | ValueTy AllocSize; |
1408 | | CharUnits AllocAlign; |
1409 | | |
1410 | 80 | PlacementArg *getPlacementArgs() { |
1411 | 80 | return reinterpret_cast<PlacementArg *>(this + 1); |
1412 | 80 | } CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::DirectCleanupTraits>::getPlacementArgs() Line | Count | Source | 1410 | 72 | PlacementArg *getPlacementArgs() { | 1411 | 72 | return reinterpret_cast<PlacementArg *>(this + 1); | 1412 | 72 | } |
CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::ConditionalCleanupTraits>::getPlacementArgs() Line | Count | Source | 1410 | 8 | PlacementArg *getPlacementArgs() { | 1411 | 8 | return reinterpret_cast<PlacementArg *>(this + 1); | 1412 | 8 | } |
|
1413 | | |
1414 | | public: |
1415 | 828 | static size_t getExtraSize(size_t NumPlacementArgs) { |
1416 | 828 | return NumPlacementArgs * sizeof(PlacementArg); |
1417 | 828 | } CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::DirectCleanupTraits>::getExtraSize(unsigned long) Line | Count | Source | 1415 | 822 | static size_t getExtraSize(size_t NumPlacementArgs) { | 1416 | 822 | return NumPlacementArgs * sizeof(PlacementArg); | 1417 | 822 | } |
CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::ConditionalCleanupTraits>::getExtraSize(unsigned long) Line | Count | Source | 1415 | 6 | static size_t getExtraSize(size_t NumPlacementArgs) { | 1416 | 6 | return NumPlacementArgs * sizeof(PlacementArg); | 1417 | 6 | } |
|
1418 | | |
1419 | | CallDeleteDuringNew(size_t NumPlacementArgs, |
1420 | | const FunctionDecl *OperatorDelete, ValueTy Ptr, |
1421 | | ValueTy AllocSize, bool PassAlignmentToPlacementDelete, |
1422 | | CharUnits AllocAlign) |
1423 | | : NumPlacementArgs(NumPlacementArgs), |
1424 | | PassAlignmentToPlacementDelete(PassAlignmentToPlacementDelete), |
1425 | | OperatorDelete(OperatorDelete), Ptr(Ptr), AllocSize(AllocSize), |
1426 | 828 | AllocAlign(AllocAlign) {} CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::DirectCleanupTraits>::CallDeleteDuringNew(unsigned long, clang::FunctionDecl const*, llvm::Value*, llvm::Value*, bool, clang::CharUnits) Line | Count | Source | 1426 | 822 | AllocAlign(AllocAlign) {} |
CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::ConditionalCleanupTraits>::CallDeleteDuringNew(unsigned long, clang::FunctionDecl const*, clang::CodeGen::DominatingValue<clang::CodeGen::RValue>::saved_type, clang::CodeGen::DominatingValue<clang::CodeGen::RValue>::saved_type, bool, clang::CharUnits) Line | Count | Source | 1426 | 6 | AllocAlign(AllocAlign) {} |
|
1427 | | |
1428 | 40 | void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) { |
1429 | 40 | assert(I < NumPlacementArgs && "index out of range"); |
1430 | 0 | getPlacementArgs()[I] = {Arg, Type}; |
1431 | 40 | } CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::DirectCleanupTraits>::setPlacementArg(unsigned int, clang::CodeGen::RValue, clang::QualType) Line | Count | Source | 1428 | 36 | void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) { | 1429 | 36 | assert(I < NumPlacementArgs && "index out of range"); | 1430 | 0 | getPlacementArgs()[I] = {Arg, Type}; | 1431 | 36 | } |
CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::ConditionalCleanupTraits>::setPlacementArg(unsigned int, clang::CodeGen::DominatingValue<clang::CodeGen::RValue>::saved_type, clang::QualType) Line | Count | Source | 1428 | 4 | void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) { | 1429 | 4 | assert(I < NumPlacementArgs && "index out of range"); | 1430 | 0 | getPlacementArgs()[I] = {Arg, Type}; | 1431 | 4 | } |
|
1432 | | |
1433 | 402 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1434 | 402 | const auto *FPT = OperatorDelete->getType()->castAs<FunctionProtoType>(); |
1435 | 402 | CallArgList DeleteArgs; |
1436 | | |
1437 | | // The first argument is always a void* (or C* for a destroying operator |
1438 | | // delete for class type C). |
1439 | 402 | DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0)); |
1440 | | |
1441 | | // Figure out what other parameters we should be implicitly passing. |
1442 | 402 | UsualDeleteParams Params; |
1443 | 402 | if (NumPlacementArgs) { |
1444 | | // A placement deallocation function is implicitly passed an alignment |
1445 | | // if the placement allocation function was, but is never passed a size. |
1446 | 36 | Params.Alignment = PassAlignmentToPlacementDelete; |
1447 | 366 | } else { |
1448 | | // For a non-placement new-expression, 'operator delete' can take a |
1449 | | // size and/or an alignment if it has the right parameters. |
1450 | 366 | Params = getUsualDeleteParams(OperatorDelete); |
1451 | 366 | } |
1452 | | |
1453 | 402 | assert(!Params.DestroyingDelete && |
1454 | 402 | "should not call destroying delete in a new-expression"); |
1455 | | |
1456 | | // The second argument can be a std::size_t (for non-placement delete). |
1457 | 402 | if (Params.Size) |
1458 | 6 | DeleteArgs.add(Traits::get(CGF, AllocSize), |
1459 | 6 | CGF.getContext().getSizeType()); |
1460 | | |
1461 | | // The next (second or third) argument can be a std::align_val_t, which |
1462 | | // is an enum whose underlying type is std::size_t. |
1463 | | // FIXME: Use the right type as the parameter type. Note that in a call |
1464 | | // to operator delete(size_t, ...), we may not have it available. |
1465 | 402 | if (Params.Alignment) |
1466 | 36 | DeleteArgs.add(RValue::get(llvm::ConstantInt::get( |
1467 | 36 | CGF.SizeTy, AllocAlign.getQuantity())), |
1468 | 36 | CGF.getContext().getSizeType()); |
1469 | | |
1470 | | // Pass the rest of the arguments, which must match exactly. |
1471 | 442 | for (unsigned I = 0; I != NumPlacementArgs; ++I40 ) { |
1472 | 40 | auto Arg = getPlacementArgs()[I]; |
1473 | 40 | DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType); |
1474 | 40 | } |
1475 | | |
1476 | | // Call 'operator delete'. |
1477 | 402 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); |
1478 | 402 | } CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::DirectCleanupTraits>::Emit(clang::CodeGen::CodeGenFunction&, clang::CodeGen::EHScopeStack::Cleanup::Flags) Line | Count | Source | 1433 | 396 | void Emit(CodeGenFunction &CGF, Flags flags) override { | 1434 | 396 | const auto *FPT = OperatorDelete->getType()->castAs<FunctionProtoType>(); | 1435 | 396 | CallArgList DeleteArgs; | 1436 | | | 1437 | | // The first argument is always a void* (or C* for a destroying operator | 1438 | | // delete for class type C). | 1439 | 396 | DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0)); | 1440 | | | 1441 | | // Figure out what other parameters we should be implicitly passing. | 1442 | 396 | UsualDeleteParams Params; | 1443 | 396 | if (NumPlacementArgs) { | 1444 | | // A placement deallocation function is implicitly passed an alignment | 1445 | | // if the placement allocation function was, but is never passed a size. | 1446 | 34 | Params.Alignment = PassAlignmentToPlacementDelete; | 1447 | 362 | } else { | 1448 | | // For a non-placement new-expression, 'operator delete' can take a | 1449 | | // size and/or an alignment if it has the right parameters. | 1450 | 362 | Params = getUsualDeleteParams(OperatorDelete); | 1451 | 362 | } | 1452 | | | 1453 | 396 | assert(!Params.DestroyingDelete && | 1454 | 396 | "should not call destroying delete in a new-expression"); | 1455 | | | 1456 | | // The second argument can be a std::size_t (for non-placement delete). | 1457 | 396 | if (Params.Size) | 1458 | 6 | DeleteArgs.add(Traits::get(CGF, AllocSize), | 1459 | 6 | CGF.getContext().getSizeType()); | 1460 | | | 1461 | | // The next (second or third) argument can be a std::align_val_t, which | 1462 | | // is an enum whose underlying type is std::size_t. | 1463 | | // FIXME: Use the right type as the parameter type. Note that in a call | 1464 | | // to operator delete(size_t, ...), we may not have it available. | 1465 | 396 | if (Params.Alignment) | 1466 | 36 | DeleteArgs.add(RValue::get(llvm::ConstantInt::get( | 1467 | 36 | CGF.SizeTy, AllocAlign.getQuantity())), | 1468 | 36 | CGF.getContext().getSizeType()); | 1469 | | | 1470 | | // Pass the rest of the arguments, which must match exactly. | 1471 | 432 | for (unsigned I = 0; I != NumPlacementArgs; ++I36 ) { | 1472 | 36 | auto Arg = getPlacementArgs()[I]; | 1473 | 36 | DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType); | 1474 | 36 | } | 1475 | | | 1476 | | // Call 'operator delete'. | 1477 | 396 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); | 1478 | 396 | } |
CGExprCXX.cpp:(anonymous namespace)::CallDeleteDuringNew<EnterNewDeleteCleanup(clang::CodeGen::CodeGenFunction&, clang::CXXNewExpr const*, clang::CodeGen::Address, llvm::Value*, clang::CharUnits, clang::CodeGen::CallArgList const&)::ConditionalCleanupTraits>::Emit(clang::CodeGen::CodeGenFunction&, clang::CodeGen::EHScopeStack::Cleanup::Flags) Line | Count | Source | 1433 | 6 | void Emit(CodeGenFunction &CGF, Flags flags) override { | 1434 | 6 | const auto *FPT = OperatorDelete->getType()->castAs<FunctionProtoType>(); | 1435 | 6 | CallArgList DeleteArgs; | 1436 | | | 1437 | | // The first argument is always a void* (or C* for a destroying operator | 1438 | | // delete for class type C). | 1439 | 6 | DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0)); | 1440 | | | 1441 | | // Figure out what other parameters we should be implicitly passing. | 1442 | 6 | UsualDeleteParams Params; | 1443 | 6 | if (NumPlacementArgs) { | 1444 | | // A placement deallocation function is implicitly passed an alignment | 1445 | | // if the placement allocation function was, but is never passed a size. | 1446 | 2 | Params.Alignment = PassAlignmentToPlacementDelete; | 1447 | 4 | } else { | 1448 | | // For a non-placement new-expression, 'operator delete' can take a | 1449 | | // size and/or an alignment if it has the right parameters. | 1450 | 4 | Params = getUsualDeleteParams(OperatorDelete); | 1451 | 4 | } | 1452 | | | 1453 | 6 | assert(!Params.DestroyingDelete && | 1454 | 6 | "should not call destroying delete in a new-expression"); | 1455 | | | 1456 | | // The second argument can be a std::size_t (for non-placement delete). | 1457 | 6 | if (Params.Size) | 1458 | 0 | DeleteArgs.add(Traits::get(CGF, AllocSize), | 1459 | 0 | CGF.getContext().getSizeType()); | 1460 | | | 1461 | | // The next (second or third) argument can be a std::align_val_t, which | 1462 | | // is an enum whose underlying type is std::size_t. | 1463 | | // FIXME: Use the right type as the parameter type. Note that in a call | 1464 | | // to operator delete(size_t, ...), we may not have it available. | 1465 | 6 | if (Params.Alignment) | 1466 | 0 | DeleteArgs.add(RValue::get(llvm::ConstantInt::get( | 1467 | 0 | CGF.SizeTy, AllocAlign.getQuantity())), | 1468 | 0 | CGF.getContext().getSizeType()); | 1469 | | | 1470 | | // Pass the rest of the arguments, which must match exactly. | 1471 | 10 | for (unsigned I = 0; I != NumPlacementArgs; ++I4 ) { | 1472 | 4 | auto Arg = getPlacementArgs()[I]; | 1473 | 4 | DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType); | 1474 | 4 | } | 1475 | | | 1476 | | // Call 'operator delete'. | 1477 | 6 | EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); | 1478 | 6 | } |
|
1479 | | }; |
1480 | | } |
1481 | | |
1482 | | /// Enter a cleanup to call 'operator delete' if the initializer in a |
1483 | | /// new-expression throws. |
1484 | | static void EnterNewDeleteCleanup(CodeGenFunction &CGF, |
1485 | | const CXXNewExpr *E, |
1486 | | Address NewPtr, |
1487 | | llvm::Value *AllocSize, |
1488 | | CharUnits AllocAlign, |
1489 | 828 | const CallArgList &NewArgs) { |
1490 | 828 | unsigned NumNonPlacementArgs = E->passAlignment() ? 240 : 1788 ; |
1491 | | |
1492 | | // If we're not inside a conditional branch, then the cleanup will |
1493 | | // dominate and we can do the easier (and more efficient) thing. |
1494 | 828 | if (!CGF.isInConditionalBranch()) { |
1495 | 822 | struct DirectCleanupTraits { |
1496 | 822 | typedef llvm::Value *ValueTy; |
1497 | 822 | typedef RValue RValueTy; |
1498 | 822 | static RValue get(CodeGenFunction &, ValueTy V) { return RValue::get(V); }402 |
1499 | 822 | static RValue get(CodeGenFunction &, RValueTy V) { return V; }36 |
1500 | 822 | }; |
1501 | | |
1502 | 822 | typedef CallDeleteDuringNew<DirectCleanupTraits> DirectCleanup; |
1503 | | |
1504 | 822 | DirectCleanup *Cleanup = CGF.EHStack |
1505 | 822 | .pushCleanupWithExtra<DirectCleanup>(EHCleanup, |
1506 | 822 | E->getNumPlacementArgs(), |
1507 | 822 | E->getOperatorDelete(), |
1508 | 822 | NewPtr.getPointer(), |
1509 | 822 | AllocSize, |
1510 | 822 | E->passAlignment(), |
1511 | 822 | AllocAlign); |
1512 | 858 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I36 ) { |
1513 | 36 | auto &Arg = NewArgs[I + NumNonPlacementArgs]; |
1514 | 36 | Cleanup->setPlacementArg(I, Arg.getRValue(CGF), Arg.Ty); |
1515 | 36 | } |
1516 | | |
1517 | 822 | return; |
1518 | 822 | } |
1519 | | |
1520 | | // Otherwise, we need to save all this stuff. |
1521 | 6 | DominatingValue<RValue>::saved_type SavedNewPtr = |
1522 | 6 | DominatingValue<RValue>::save(CGF, RValue::get(NewPtr.getPointer())); |
1523 | 6 | DominatingValue<RValue>::saved_type SavedAllocSize = |
1524 | 6 | DominatingValue<RValue>::save(CGF, RValue::get(AllocSize)); |
1525 | | |
1526 | 6 | struct ConditionalCleanupTraits { |
1527 | 6 | typedef DominatingValue<RValue>::saved_type ValueTy; |
1528 | 6 | typedef DominatingValue<RValue>::saved_type RValueTy; |
1529 | 10 | static RValue get(CodeGenFunction &CGF, ValueTy V) { |
1530 | 10 | return V.restore(CGF); |
1531 | 10 | } |
1532 | 6 | }; |
1533 | 6 | typedef CallDeleteDuringNew<ConditionalCleanupTraits> ConditionalCleanup; |
1534 | | |
1535 | 6 | ConditionalCleanup *Cleanup = CGF.EHStack |
1536 | 6 | .pushCleanupWithExtra<ConditionalCleanup>(EHCleanup, |
1537 | 6 | E->getNumPlacementArgs(), |
1538 | 6 | E->getOperatorDelete(), |
1539 | 6 | SavedNewPtr, |
1540 | 6 | SavedAllocSize, |
1541 | 6 | E->passAlignment(), |
1542 | 6 | AllocAlign); |
1543 | 10 | for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I4 ) { |
1544 | 4 | auto &Arg = NewArgs[I + NumNonPlacementArgs]; |
1545 | 4 | Cleanup->setPlacementArg( |
1546 | 4 | I, DominatingValue<RValue>::save(CGF, Arg.getRValue(CGF)), Arg.Ty); |
1547 | 4 | } |
1548 | | |
1549 | 6 | CGF.initFullExprCleanup(); |
1550 | 6 | } |
1551 | | |
1552 | 1.95k | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
1553 | | // The element type being allocated. |
1554 | 1.95k | QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); |
1555 | | |
1556 | | // 1. Build a call to the allocation function. |
1557 | 1.95k | FunctionDecl *allocator = E->getOperatorNew(); |
1558 | | |
1559 | | // If there is a brace-initializer, cannot allocate fewer elements than inits. |
1560 | 1.95k | unsigned minElements = 0; |
1561 | 1.95k | if (E->isArray() && E->hasInitializer()350 ) { |
1562 | 153 | const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()); |
1563 | 153 | if (ILE && ILE->isStringLiteralInit()28 ) |
1564 | 8 | minElements = |
1565 | 8 | cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe()) |
1566 | 8 | ->getSize().getZExtValue(); |
1567 | 145 | else if (ILE) |
1568 | 20 | minElements = ILE->getNumInits(); |
1569 | 153 | } |
1570 | | |
1571 | 1.95k | llvm::Value *numElements = nullptr; |
1572 | 1.95k | llvm::Value *allocSizeWithoutCookie = nullptr; |
1573 | 1.95k | llvm::Value *allocSize = |
1574 | 1.95k | EmitCXXNewAllocSize(*this, E, minElements, numElements, |
1575 | 1.95k | allocSizeWithoutCookie); |
1576 | 1.95k | CharUnits allocAlign = getContext().getTypeAlignInChars(allocType); |
1577 | | |
1578 | | // Emit the allocation call. If the allocator is a global placement |
1579 | | // operator, just "inline" it directly. |
1580 | 1.95k | Address allocation = Address::invalid(); |
1581 | 1.95k | CallArgList allocatorArgs; |
1582 | 1.95k | if (allocator->isReservedGlobalPlacementOperator()) { |
1583 | 656 | assert(E->getNumPlacementArgs() == 1); |
1584 | 0 | const Expr *arg = *E->placement_arguments().begin(); |
1585 | | |
1586 | 656 | LValueBaseInfo BaseInfo; |
1587 | 656 | allocation = EmitPointerWithAlignment(arg, &BaseInfo); |
1588 | | |
1589 | | // The pointer expression will, in many cases, be an opaque void*. |
1590 | | // In these cases, discard the computed alignment and use the |
1591 | | // formal alignment of the allocated type. |
1592 | 656 | if (BaseInfo.getAlignmentSource() != AlignmentSource::Decl) |
1593 | 653 | allocation = allocation.withAlignment(allocAlign); |
1594 | | |
1595 | | // Set up allocatorArgs for the call to operator delete if it's not |
1596 | | // the reserved global operator. |
1597 | 656 | if (E->getOperatorDelete() && |
1598 | 656 | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()560 ) { |
1599 | 2 | allocatorArgs.add(RValue::get(allocSize), getContext().getSizeType()); |
1600 | 2 | allocatorArgs.add(RValue::get(allocation.getPointer()), arg->getType()); |
1601 | 2 | } |
1602 | | |
1603 | 1.29k | } else { |
1604 | 1.29k | const FunctionProtoType *allocatorType = |
1605 | 1.29k | allocator->getType()->castAs<FunctionProtoType>(); |
1606 | 1.29k | unsigned ParamsToSkip = 0; |
1607 | | |
1608 | | // The allocation size is the first argument. |
1609 | 1.29k | QualType sizeType = getContext().getSizeType(); |
1610 | 1.29k | allocatorArgs.add(RValue::get(allocSize), sizeType); |
1611 | 1.29k | ++ParamsToSkip; |
1612 | | |
1613 | 1.29k | if (allocSize != allocSizeWithoutCookie) { |
1614 | 62 | CharUnits cookieAlign = getSizeAlign(); // FIXME: Ask the ABI. |
1615 | 62 | allocAlign = std::max(allocAlign, cookieAlign); |
1616 | 62 | } |
1617 | | |
1618 | | // The allocation alignment may be passed as the second argument. |
1619 | 1.29k | if (E->passAlignment()) { |
1620 | 44 | QualType AlignValT = sizeType; |
1621 | 44 | if (allocatorType->getNumParams() > 1) { |
1622 | 36 | AlignValT = allocatorType->getParamType(1); |
1623 | 36 | assert(getContext().hasSameUnqualifiedType( |
1624 | 36 | AlignValT->castAs<EnumType>()->getDecl()->getIntegerType(), |
1625 | 36 | sizeType) && |
1626 | 36 | "wrong type for alignment parameter"); |
1627 | 0 | ++ParamsToSkip; |
1628 | 36 | } else { |
1629 | | // Corner case, passing alignment to 'operator new(size_t, ...)'. |
1630 | 8 | assert(allocator->isVariadic() && "can't pass alignment to allocator"); |
1631 | 8 | } |
1632 | 0 | allocatorArgs.add( |
1633 | 44 | RValue::get(llvm::ConstantInt::get(SizeTy, allocAlign.getQuantity())), |
1634 | 44 | AlignValT); |
1635 | 44 | } |
1636 | | |
1637 | | // FIXME: Why do we not pass a CalleeDecl here? |
1638 | 0 | EmitCallArgs(allocatorArgs, allocatorType, E->placement_arguments(), |
1639 | 1.29k | /*AC*/AbstractCallee(), /*ParamsToSkip*/ParamsToSkip); |
1640 | | |
1641 | 1.29k | RValue RV = |
1642 | 1.29k | EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs); |
1643 | | |
1644 | | // Set !heapallocsite metadata on the call to operator new. |
1645 | 1.29k | if (getDebugInfo()) |
1646 | 681 | if (auto *newCall = dyn_cast<llvm::CallBase>(RV.getScalarVal())) |
1647 | 681 | getDebugInfo()->addHeapAllocSiteMetadata(newCall, allocType, |
1648 | 681 | E->getExprLoc()); |
1649 | | |
1650 | | // If this was a call to a global replaceable allocation function that does |
1651 | | // not take an alignment argument, the allocator is known to produce |
1652 | | // storage that's suitably aligned for any object that fits, up to a known |
1653 | | // threshold. Otherwise assume it's suitably aligned for the allocated type. |
1654 | 1.29k | CharUnits allocationAlign = allocAlign; |
1655 | 1.29k | if (!E->passAlignment() && |
1656 | 1.29k | allocator->isReplaceableGlobalAllocationFunction()1.25k ) { |
1657 | 1.15k | unsigned AllocatorAlign = llvm::PowerOf2Floor(std::min<uint64_t>( |
1658 | 1.15k | Target.getNewAlign(), getContext().getTypeSize(allocType))); |
1659 | 1.15k | allocationAlign = std::max( |
1660 | 1.15k | allocationAlign, getContext().toCharUnitsFromBits(AllocatorAlign)); |
1661 | 1.15k | } |
1662 | | |
1663 | 1.29k | allocation = Address(RV.getScalarVal(), Int8Ty, allocationAlign); |
1664 | 1.29k | } |
1665 | | |
1666 | | // Emit a null check on the allocation result if the allocation |
1667 | | // function is allowed to return null (because it has a non-throwing |
1668 | | // exception spec or is the reserved placement new) and we have an |
1669 | | // interesting initializer will be running sanitizers on the initialization. |
1670 | 1.95k | bool nullCheck = E->shouldNullCheckAllocation() && |
1671 | 1.95k | (26 !allocType.isPODType(getContext())26 || E->hasInitializer()10 || |
1672 | 26 | sanitizePerformTypeCheck()10 ); |
1673 | | |
1674 | 1.95k | llvm::BasicBlock *nullCheckBB = nullptr; |
1675 | 1.95k | llvm::BasicBlock *contBB = nullptr; |
1676 | | |
1677 | | // The null-check means that the initializer is conditionally |
1678 | | // evaluated. |
1679 | 1.95k | ConditionalEvaluation conditional(*this); |
1680 | | |
1681 | 1.95k | if (nullCheck) { |
1682 | 22 | conditional.begin(*this); |
1683 | | |
1684 | 22 | nullCheckBB = Builder.GetInsertBlock(); |
1685 | 22 | llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); |
1686 | 22 | contBB = createBasicBlock("new.cont"); |
1687 | | |
1688 | 22 | llvm::Value *isNull = |
1689 | 22 | Builder.CreateIsNull(allocation.getPointer(), "new.isnull"); |
1690 | 22 | Builder.CreateCondBr(isNull, contBB, notNullBB); |
1691 | 22 | EmitBlock(notNullBB); |
1692 | 22 | } |
1693 | | |
1694 | | // If there's an operator delete, enter a cleanup to call it if an |
1695 | | // exception is thrown. |
1696 | 1.95k | EHScopeStack::stable_iterator operatorDeleteCleanup; |
1697 | 1.95k | llvm::Instruction *cleanupDominator = nullptr; |
1698 | 1.95k | if (E->getOperatorDelete() && |
1699 | 1.95k | !E->getOperatorDelete()->isReservedGlobalPlacementOperator()1.38k ) { |
1700 | 828 | EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocAlign, |
1701 | 828 | allocatorArgs); |
1702 | 828 | operatorDeleteCleanup = EHStack.stable_begin(); |
1703 | 828 | cleanupDominator = Builder.CreateUnreachable(); |
1704 | 828 | } |
1705 | | |
1706 | 1.95k | assert((allocSize == allocSizeWithoutCookie) == |
1707 | 1.95k | CalculateCookiePadding(*this, E).isZero()); |
1708 | 1.95k | if (allocSize != allocSizeWithoutCookie) { |
1709 | 62 | assert(E->isArray()); |
1710 | 0 | allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, |
1711 | 62 | numElements, |
1712 | 62 | E, allocType); |
1713 | 62 | } |
1714 | | |
1715 | 0 | llvm::Type *elementTy = ConvertTypeForMem(allocType); |
1716 | 1.95k | Address result = Builder.CreateElementBitCast(allocation, elementTy); |
1717 | | |
1718 | | // Passing pointer through launder.invariant.group to avoid propagation of |
1719 | | // vptrs information which may be included in previous type. |
1720 | | // To not break LTO with different optimizations levels, we do it regardless |
1721 | | // of optimization level. |
1722 | 1.95k | if (CGM.getCodeGenOpts().StrictVTablePointers && |
1723 | 1.95k | allocator->isReservedGlobalPlacementOperator()32 ) |
1724 | 5 | result = Builder.CreateLaunderInvariantGroup(result); |
1725 | | |
1726 | | // Emit sanitizer checks for pointer value now, so that in the case of an |
1727 | | // array it was checked only once and not at each constructor call. We may |
1728 | | // have already checked that the pointer is non-null. |
1729 | | // FIXME: If we have an array cookie and a potentially-throwing allocator, |
1730 | | // we'll null check the wrong pointer here. |
1731 | 1.95k | SanitizerSet SkippedChecks; |
1732 | 1.95k | SkippedChecks.set(SanitizerKind::Null, nullCheck); |
1733 | 1.95k | EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, |
1734 | 1.95k | E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(), |
1735 | 1.95k | result.getPointer(), allocType, result.getAlignment(), |
1736 | 1.95k | SkippedChecks, numElements); |
1737 | | |
1738 | 1.95k | EmitNewInitializer(*this, E, allocType, elementTy, result, numElements, |
1739 | 1.95k | allocSizeWithoutCookie); |
1740 | 1.95k | llvm::Value *resultPtr = result.getPointer(); |
1741 | 1.95k | if (E->isArray()) { |
1742 | | // NewPtr is a pointer to the base element type. If we're |
1743 | | // allocating an array of arrays, we'll need to cast back to the |
1744 | | // array pointer type. |
1745 | 350 | llvm::Type *resultType = ConvertTypeForMem(E->getType()); |
1746 | 350 | if (resultPtr->getType() != resultType) |
1747 | 20 | resultPtr = Builder.CreateBitCast(resultPtr, resultType); |
1748 | 350 | } |
1749 | | |
1750 | | // Deactivate the 'operator delete' cleanup if we finished |
1751 | | // initialization. |
1752 | 1.95k | if (operatorDeleteCleanup.isValid()) { |
1753 | 828 | DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); |
1754 | 828 | cleanupDominator->eraseFromParent(); |
1755 | 828 | } |
1756 | | |
1757 | 1.95k | if (nullCheck) { |
1758 | 22 | conditional.end(*this); |
1759 | | |
1760 | 22 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
1761 | 22 | EmitBlock(contBB); |
1762 | | |
1763 | 22 | llvm::PHINode *PHI = Builder.CreatePHI(resultPtr->getType(), 2); |
1764 | 22 | PHI->addIncoming(resultPtr, notNullBB); |
1765 | 22 | PHI->addIncoming(llvm::Constant::getNullValue(resultPtr->getType()), |
1766 | 22 | nullCheckBB); |
1767 | | |
1768 | 22 | resultPtr = PHI; |
1769 | 22 | } |
1770 | | |
1771 | 1.95k | return resultPtr; |
1772 | 1.95k | } |
1773 | | |
1774 | | void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, |
1775 | | llvm::Value *Ptr, QualType DeleteTy, |
1776 | | llvm::Value *NumElements, |
1777 | 1.68k | CharUnits CookieSize) { |
1778 | 1.68k | assert((!NumElements && CookieSize.isZero()) || |
1779 | 1.68k | DeleteFD->getOverloadedOperator() == OO_Array_Delete); |
1780 | | |
1781 | 0 | const auto *DeleteFTy = DeleteFD->getType()->castAs<FunctionProtoType>(); |
1782 | 1.68k | CallArgList DeleteArgs; |
1783 | | |
1784 | 1.68k | auto Params = getUsualDeleteParams(DeleteFD); |
1785 | 1.68k | auto ParamTypeIt = DeleteFTy->param_type_begin(); |
1786 | | |
1787 | | // Pass the pointer itself. |
1788 | 1.68k | QualType ArgTy = *ParamTypeIt++; |
1789 | 1.68k | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
1790 | 1.68k | DeleteArgs.add(RValue::get(DeletePtr), ArgTy); |
1791 | | |
1792 | | // Pass the std::destroying_delete tag if present. |
1793 | 1.68k | llvm::AllocaInst *DestroyingDeleteTag = nullptr; |
1794 | 1.68k | if (Params.DestroyingDelete) { |
1795 | 30 | QualType DDTag = *ParamTypeIt++; |
1796 | 30 | llvm::Type *Ty = getTypes().ConvertType(DDTag); |
1797 | 30 | CharUnits Align = CGM.getNaturalTypeAlignment(DDTag); |
1798 | 30 | DestroyingDeleteTag = CreateTempAlloca(Ty, "destroying.delete.tag"); |
1799 | 30 | DestroyingDeleteTag->setAlignment(Align.getAsAlign()); |
1800 | 30 | DeleteArgs.add( |
1801 | 30 | RValue::getAggregate(Address(DestroyingDeleteTag, Ty, Align)), DDTag); |
1802 | 30 | } |
1803 | | |
1804 | | // Pass the size if the delete function has a size_t parameter. |
1805 | 1.68k | if (Params.Size) { |
1806 | 84 | QualType SizeType = *ParamTypeIt++; |
1807 | 84 | CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); |
1808 | 84 | llvm::Value *Size = llvm::ConstantInt::get(ConvertType(SizeType), |
1809 | 84 | DeleteTypeSize.getQuantity()); |
1810 | | |
1811 | | // For array new, multiply by the number of elements. |
1812 | 84 | if (NumElements) |
1813 | 23 | Size = Builder.CreateMul(Size, NumElements); |
1814 | | |
1815 | | // If there is a cookie, add the cookie size. |
1816 | 84 | if (!CookieSize.isZero()) |
1817 | 23 | Size = Builder.CreateAdd( |
1818 | 23 | Size, llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity())); |
1819 | | |
1820 | 84 | DeleteArgs.add(RValue::get(Size), SizeType); |
1821 | 84 | } |
1822 | | |
1823 | | // Pass the alignment if the delete function has an align_val_t parameter. |
1824 | 1.68k | if (Params.Alignment) { |
1825 | 38 | QualType AlignValType = *ParamTypeIt++; |
1826 | 38 | CharUnits DeleteTypeAlign = |
1827 | 38 | getContext().toCharUnitsFromBits(getContext().getTypeAlignIfKnown( |
1828 | 38 | DeleteTy, true /* NeedsPreferredAlignment */)); |
1829 | 38 | llvm::Value *Align = llvm::ConstantInt::get(ConvertType(AlignValType), |
1830 | 38 | DeleteTypeAlign.getQuantity()); |
1831 | 38 | DeleteArgs.add(RValue::get(Align), AlignValType); |
1832 | 38 | } |
1833 | | |
1834 | 1.68k | assert(ParamTypeIt == DeleteFTy->param_type_end() && |
1835 | 1.68k | "unknown parameter to usual delete function"); |
1836 | | |
1837 | | // Emit the call to delete. |
1838 | 0 | EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs); |
1839 | | |
1840 | | // If call argument lowering didn't use the destroying_delete_t alloca, |
1841 | | // remove it again. |
1842 | 1.68k | if (DestroyingDeleteTag && DestroyingDeleteTag->use_empty()30 ) |
1843 | 10 | DestroyingDeleteTag->eraseFromParent(); |
1844 | 1.68k | } |
1845 | | |
1846 | | namespace { |
1847 | | /// Calls the given 'operator delete' on a single object. |
1848 | | struct CallObjectDelete final : EHScopeStack::Cleanup { |
1849 | | llvm::Value *Ptr; |
1850 | | const FunctionDecl *OperatorDelete; |
1851 | | QualType ElementType; |
1852 | | |
1853 | | CallObjectDelete(llvm::Value *Ptr, |
1854 | | const FunctionDecl *OperatorDelete, |
1855 | | QualType ElementType) |
1856 | 535 | : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} |
1857 | | |
1858 | 535 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1859 | 535 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); |
1860 | 535 | } |
1861 | | }; |
1862 | | } |
1863 | | |
1864 | | void |
1865 | | CodeGenFunction::pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, |
1866 | | llvm::Value *CompletePtr, |
1867 | 6 | QualType ElementType) { |
1868 | 6 | EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, CompletePtr, |
1869 | 6 | OperatorDelete, ElementType); |
1870 | 6 | } |
1871 | | |
1872 | | /// Emit the code for deleting a single object with a destroying operator |
1873 | | /// delete. If the element type has a non-virtual destructor, Ptr has already |
1874 | | /// been converted to the type of the parameter of 'operator delete'. Otherwise |
1875 | | /// Ptr points to an object of the static type. |
1876 | | static void EmitDestroyingObjectDelete(CodeGenFunction &CGF, |
1877 | | const CXXDeleteExpr *DE, Address Ptr, |
1878 | 30 | QualType ElementType) { |
1879 | 30 | auto *Dtor = ElementType->getAsCXXRecordDecl()->getDestructor(); |
1880 | 30 | if (Dtor && Dtor->isVirtual()) |
1881 | 12 | CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, |
1882 | 12 | Dtor); |
1883 | 18 | else |
1884 | 18 | CGF.EmitDeleteCall(DE->getOperatorDelete(), Ptr.getPointer(), ElementType); |
1885 | 30 | } |
1886 | | |
1887 | | /// Emit the code for deleting a single object. |
1888 | | /// \return \c true if we started emitting UnconditionalDeleteBlock, \c false |
1889 | | /// if not. |
1890 | | static bool EmitObjectDelete(CodeGenFunction &CGF, |
1891 | | const CXXDeleteExpr *DE, |
1892 | | Address Ptr, |
1893 | | QualType ElementType, |
1894 | 585 | llvm::BasicBlock *UnconditionalDeleteBlock) { |
1895 | | // C++11 [expr.delete]p3: |
1896 | | // If the static type of the object to be deleted is different from its |
1897 | | // dynamic type, the static type shall be a base class of the dynamic type |
1898 | | // of the object to be deleted and the static type shall have a virtual |
1899 | | // destructor or the behavior is undefined. |
1900 | 585 | CGF.EmitTypeCheck(CodeGenFunction::TCK_MemberCall, |
1901 | 585 | DE->getExprLoc(), Ptr.getPointer(), |
1902 | 585 | ElementType); |
1903 | | |
1904 | 585 | const FunctionDecl *OperatorDelete = DE->getOperatorDelete(); |
1905 | 585 | assert(!OperatorDelete->isDestroyingOperatorDelete()); |
1906 | | |
1907 | | // Find the destructor for the type, if applicable. If the |
1908 | | // destructor is virtual, we'll just emit the vcall and return. |
1909 | 0 | const CXXDestructorDecl *Dtor = nullptr; |
1910 | 585 | if (const RecordType *RT = ElementType->getAs<RecordType>()) { |
1911 | 523 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
1912 | 523 | if (RD->hasDefinition() && !RD->hasTrivialDestructor()521 ) { |
1913 | 413 | Dtor = RD->getDestructor(); |
1914 | | |
1915 | 413 | if (Dtor->isVirtual()) { |
1916 | 58 | bool UseVirtualCall = true; |
1917 | 58 | const Expr *Base = DE->getArgument(); |
1918 | 58 | if (auto *DevirtualizedDtor = |
1919 | 58 | dyn_cast_or_null<const CXXDestructorDecl>( |
1920 | 58 | Dtor->getDevirtualizedMethod( |
1921 | 58 | Base, CGF.CGM.getLangOpts().AppleKext))) { |
1922 | 2 | UseVirtualCall = false; |
1923 | 2 | const CXXRecordDecl *DevirtualizedClass = |
1924 | 2 | DevirtualizedDtor->getParent(); |
1925 | 2 | if (declaresSameEntity(getCXXRecord(Base), DevirtualizedClass)) { |
1926 | | // Devirtualized to the class of the base type (the type of the |
1927 | | // whole expression). |
1928 | 2 | Dtor = DevirtualizedDtor; |
1929 | 2 | } else { |
1930 | | // Devirtualized to some other type. Would need to cast the this |
1931 | | // pointer to that type but we don't have support for that yet, so |
1932 | | // do a virtual call. FIXME: handle the case where it is |
1933 | | // devirtualized to the derived type (the type of the inner |
1934 | | // expression) as in EmitCXXMemberOrOperatorMemberCallExpr. |
1935 | 0 | UseVirtualCall = true; |
1936 | 0 | } |
1937 | 2 | } |
1938 | 58 | if (UseVirtualCall) { |
1939 | 56 | CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, |
1940 | 56 | Dtor); |
1941 | 56 | return false; |
1942 | 56 | } |
1943 | 58 | } |
1944 | 413 | } |
1945 | 523 | } |
1946 | | |
1947 | | // Make sure that we call delete even if the dtor throws. |
1948 | | // This doesn't have to a conditional cleanup because we're going |
1949 | | // to pop it off in a second. |
1950 | 529 | CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, |
1951 | 529 | Ptr.getPointer(), |
1952 | 529 | OperatorDelete, ElementType); |
1953 | | |
1954 | 529 | if (Dtor) |
1955 | 357 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
1956 | 357 | /*ForVirtualBase=*/false, |
1957 | 357 | /*Delegating=*/false, |
1958 | 357 | Ptr, ElementType); |
1959 | 172 | else if (auto Lifetime = ElementType.getObjCLifetime()) { |
1960 | 4 | switch (Lifetime) { |
1961 | 0 | case Qualifiers::OCL_None: |
1962 | 0 | case Qualifiers::OCL_ExplicitNone: |
1963 | 0 | case Qualifiers::OCL_Autoreleasing: |
1964 | 0 | break; |
1965 | | |
1966 | 2 | case Qualifiers::OCL_Strong: |
1967 | 2 | CGF.EmitARCDestroyStrong(Ptr, ARCPreciseLifetime); |
1968 | 2 | break; |
1969 | | |
1970 | 2 | case Qualifiers::OCL_Weak: |
1971 | 2 | CGF.EmitARCDestroyWeak(Ptr); |
1972 | 2 | break; |
1973 | 4 | } |
1974 | 4 | } |
1975 | | |
1976 | | // When optimizing for size, call 'operator delete' unconditionally. |
1977 | 529 | if (CGF.CGM.getCodeGenOpts().OptimizeSize > 1) { |
1978 | 7 | CGF.EmitBlock(UnconditionalDeleteBlock); |
1979 | 7 | CGF.PopCleanupBlock(); |
1980 | 7 | return true; |
1981 | 7 | } |
1982 | | |
1983 | 522 | CGF.PopCleanupBlock(); |
1984 | 522 | return false; |
1985 | 529 | } |
1986 | | |
1987 | | namespace { |
1988 | | /// Calls the given 'operator delete' on an array of objects. |
1989 | | struct CallArrayDelete final : EHScopeStack::Cleanup { |
1990 | | llvm::Value *Ptr; |
1991 | | const FunctionDecl *OperatorDelete; |
1992 | | llvm::Value *NumElements; |
1993 | | QualType ElementType; |
1994 | | CharUnits CookieSize; |
1995 | | |
1996 | | CallArrayDelete(llvm::Value *Ptr, |
1997 | | const FunctionDecl *OperatorDelete, |
1998 | | llvm::Value *NumElements, |
1999 | | QualType ElementType, |
2000 | | CharUnits CookieSize) |
2001 | | : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), |
2002 | 265 | ElementType(ElementType), CookieSize(CookieSize) {} |
2003 | | |
2004 | 268 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
2005 | 268 | CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType, NumElements, |
2006 | 268 | CookieSize); |
2007 | 268 | } |
2008 | | }; |
2009 | | } |
2010 | | |
2011 | | /// Emit the code for deleting an array of objects. |
2012 | | static void EmitArrayDelete(CodeGenFunction &CGF, |
2013 | | const CXXDeleteExpr *E, |
2014 | | Address deletedPtr, |
2015 | 265 | QualType elementType) { |
2016 | 265 | llvm::Value *numElements = nullptr; |
2017 | 265 | llvm::Value *allocatedPtr = nullptr; |
2018 | 265 | CharUnits cookieSize; |
2019 | 265 | CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, |
2020 | 265 | numElements, allocatedPtr, cookieSize); |
2021 | | |
2022 | 265 | assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); |
2023 | | |
2024 | | // Make sure that we call delete even if one of the dtors throws. |
2025 | 0 | const FunctionDecl *operatorDelete = E->getOperatorDelete(); |
2026 | 265 | CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup, |
2027 | 265 | allocatedPtr, operatorDelete, |
2028 | 265 | numElements, elementType, |
2029 | 265 | cookieSize); |
2030 | | |
2031 | | // Destroy the elements. |
2032 | 265 | if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { |
2033 | 41 | assert(numElements && "no element count for a type with a destructor!"); |
2034 | | |
2035 | 0 | CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType); |
2036 | 41 | CharUnits elementAlign = |
2037 | 41 | deletedPtr.getAlignment().alignmentOfArrayElement(elementSize); |
2038 | | |
2039 | 41 | llvm::Value *arrayBegin = deletedPtr.getPointer(); |
2040 | 41 | llvm::Value *arrayEnd = CGF.Builder.CreateInBoundsGEP( |
2041 | 41 | deletedPtr.getElementType(), arrayBegin, numElements, "delete.end"); |
2042 | | |
2043 | | // Note that it is legal to allocate a zero-length array, and we |
2044 | | // can never fold the check away because the length should always |
2045 | | // come from a cookie. |
2046 | 41 | CGF.emitArrayDestroy(arrayBegin, arrayEnd, elementType, elementAlign, |
2047 | 41 | CGF.getDestroyer(dtorKind), |
2048 | 41 | /*checkZeroLength*/ true, |
2049 | 41 | CGF.needsEHCleanup(dtorKind)); |
2050 | 41 | } |
2051 | | |
2052 | | // Pop the cleanup block. |
2053 | 0 | CGF.PopCleanupBlock(); |
2054 | 265 | } |
2055 | | |
2056 | 880 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
2057 | 880 | const Expr *Arg = E->getArgument(); |
2058 | 880 | Address Ptr = EmitPointerWithAlignment(Arg); |
2059 | | |
2060 | | // Null check the pointer. |
2061 | | // |
2062 | | // We could avoid this null check if we can determine that the object |
2063 | | // destruction is trivial and doesn't require an array cookie; we can |
2064 | | // unconditionally perform the operator delete call in that case. For now, we |
2065 | | // assume that deleted pointers are null rarely enough that it's better to |
2066 | | // keep the branch. This might be worth revisiting for a -O0 code size win. |
2067 | 880 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
2068 | 880 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
2069 | | |
2070 | 880 | llvm::Value *IsNull = Builder.CreateIsNull(Ptr.getPointer(), "isnull"); |
2071 | | |
2072 | 880 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
2073 | 880 | EmitBlock(DeleteNotNull); |
2074 | | |
2075 | 880 | QualType DeleteTy = E->getDestroyedType(); |
2076 | | |
2077 | | // A destroying operator delete overrides the entire operation of the |
2078 | | // delete expression. |
2079 | 880 | if (E->getOperatorDelete()->isDestroyingOperatorDelete()) { |
2080 | 30 | EmitDestroyingObjectDelete(*this, E, Ptr, DeleteTy); |
2081 | 30 | EmitBlock(DeleteEnd); |
2082 | 30 | return; |
2083 | 30 | } |
2084 | | |
2085 | | // We might be deleting a pointer to array. If so, GEP down to the |
2086 | | // first non-array element. |
2087 | | // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) |
2088 | 850 | if (DeleteTy->isConstantArrayType()) { |
2089 | 10 | llvm::Value *Zero = Builder.getInt32(0); |
2090 | 10 | SmallVector<llvm::Value*,8> GEP; |
2091 | | |
2092 | 10 | GEP.push_back(Zero); // point at the outermost array |
2093 | | |
2094 | | // For each layer of array type we're pointing at: |
2095 | 22 | while (const ConstantArrayType *Arr |
2096 | 12 | = getContext().getAsConstantArrayType(DeleteTy)) { |
2097 | | // 1. Unpeel the array type. |
2098 | 12 | DeleteTy = Arr->getElementType(); |
2099 | | |
2100 | | // 2. GEP to the first element of the array. |
2101 | 12 | GEP.push_back(Zero); |
2102 | 12 | } |
2103 | | |
2104 | 10 | Ptr = Address(Builder.CreateInBoundsGEP(Ptr.getElementType(), |
2105 | 10 | Ptr.getPointer(), GEP, "del.first"), |
2106 | 10 | ConvertTypeForMem(DeleteTy), Ptr.getAlignment()); |
2107 | 10 | } |
2108 | | |
2109 | 850 | assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType()); |
2110 | | |
2111 | 850 | if (E->isArrayForm()) { |
2112 | 265 | EmitArrayDelete(*this, E, Ptr, DeleteTy); |
2113 | 265 | EmitBlock(DeleteEnd); |
2114 | 585 | } else { |
2115 | 585 | if (!EmitObjectDelete(*this, E, Ptr, DeleteTy, DeleteEnd)) |
2116 | 578 | EmitBlock(DeleteEnd); |
2117 | 585 | } |
2118 | 850 | } |
2119 | | |
2120 | 52 | static bool isGLValueFromPointerDeref(const Expr *E) { |
2121 | 52 | E = E->IgnoreParens(); |
2122 | | |
2123 | 52 | if (const auto *CE = dyn_cast<CastExpr>(E)) { |
2124 | 6 | if (!CE->getSubExpr()->isGLValue()) |
2125 | 0 | return false; |
2126 | 6 | return isGLValueFromPointerDeref(CE->getSubExpr()); |
2127 | 6 | } |
2128 | | |
2129 | 46 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) |
2130 | 4 | return isGLValueFromPointerDeref(OVE->getSourceExpr()); |
2131 | | |
2132 | 42 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) |
2133 | 1 | if (BO->getOpcode() == BO_Comma) |
2134 | 1 | return isGLValueFromPointerDeref(BO->getRHS()); |
2135 | | |
2136 | 41 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E)) |
2137 | 7 | return isGLValueFromPointerDeref(ACO->getTrueExpr()) || |
2138 | 7 | isGLValueFromPointerDeref(ACO->getFalseExpr())3 ; |
2139 | | |
2140 | | // C++11 [expr.sub]p1: |
2141 | | // The expression E1[E2] is identical (by definition) to *((E1)+(E2)) |
2142 | 34 | if (isa<ArraySubscriptExpr>(E)) |
2143 | 2 | return true; |
2144 | | |
2145 | 32 | if (const auto *UO = dyn_cast<UnaryOperator>(E)) |
2146 | 19 | if (UO->getOpcode() == UO_Deref) |
2147 | 19 | return true; |
2148 | | |
2149 | 13 | return false; |
2150 | 32 | } |
2151 | | |
2152 | | static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, |
2153 | 31 | llvm::Type *StdTypeInfoPtrTy) { |
2154 | | // Get the vtable pointer. |
2155 | 31 | Address ThisPtr = CGF.EmitLValue(E).getAddress(CGF); |
2156 | | |
2157 | 31 | QualType SrcRecordTy = E->getType(); |
2158 | | |
2159 | | // C++ [class.cdtor]p4: |
2160 | | // If the operand of typeid refers to the object under construction or |
2161 | | // destruction and the static type of the operand is neither the constructor |
2162 | | // or destructor’s class nor one of its bases, the behavior is undefined. |
2163 | 31 | CGF.EmitTypeCheck(CodeGenFunction::TCK_DynamicOperation, E->getExprLoc(), |
2164 | 31 | ThisPtr.getPointer(), SrcRecordTy); |
2165 | | |
2166 | | // C++ [expr.typeid]p2: |
2167 | | // If the glvalue expression is obtained by applying the unary * operator to |
2168 | | // a pointer and the pointer is a null pointer value, the typeid expression |
2169 | | // throws the std::bad_typeid exception. |
2170 | | // |
2171 | | // However, this paragraph's intent is not clear. We choose a very generous |
2172 | | // interpretation which implores us to consider comma operators, conditional |
2173 | | // operators, parentheses and other such constructs. |
2174 | 31 | if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked( |
2175 | 31 | isGLValueFromPointerDeref(E), SrcRecordTy)) { |
2176 | 20 | llvm::BasicBlock *BadTypeidBlock = |
2177 | 20 | CGF.createBasicBlock("typeid.bad_typeid"); |
2178 | 20 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end"); |
2179 | | |
2180 | 20 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr.getPointer()); |
2181 | 20 | CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); |
2182 | | |
2183 | 20 | CGF.EmitBlock(BadTypeidBlock); |
2184 | 20 | CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF); |
2185 | 20 | CGF.EmitBlock(EndBlock); |
2186 | 20 | } |
2187 | | |
2188 | 31 | return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr, |
2189 | 31 | StdTypeInfoPtrTy); |
2190 | 31 | } |
2191 | | |
2192 | 392 | llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
2193 | 392 | llvm::Type *StdTypeInfoPtrTy = |
2194 | 392 | ConvertType(E->getType())->getPointerTo(); |
2195 | | |
2196 | 392 | if (E->isTypeOperand()) { |
2197 | 296 | llvm::Constant *TypeInfo = |
2198 | 296 | CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); |
2199 | 296 | return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); |
2200 | 296 | } |
2201 | | |
2202 | | // C++ [expr.typeid]p2: |
2203 | | // When typeid is applied to a glvalue expression whose type is a |
2204 | | // polymorphic class type, the result refers to a std::type_info object |
2205 | | // representing the type of the most derived object (that is, the dynamic |
2206 | | // type) to which the glvalue refers. |
2207 | | // If the operand is already most derived object, no need to look up vtable. |
2208 | 96 | if (E->isPotentiallyEvaluated() && !E->isMostDerived(getContext())33 ) |
2209 | 31 | return EmitTypeidFromVTable(*this, E->getExprOperand(), |
2210 | 31 | StdTypeInfoPtrTy); |
2211 | | |
2212 | 65 | QualType OperandTy = E->getExprOperand()->getType(); |
2213 | 65 | return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), |
2214 | 65 | StdTypeInfoPtrTy); |
2215 | 96 | } |
2216 | | |
2217 | | static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, |
2218 | 2 | QualType DestTy) { |
2219 | 2 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
2220 | 2 | if (DestTy->isPointerType()) |
2221 | 1 | return llvm::Constant::getNullValue(DestLTy); |
2222 | | |
2223 | | /// C++ [expr.dynamic.cast]p9: |
2224 | | /// A failed cast to reference type throws std::bad_cast |
2225 | 1 | if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF)) |
2226 | 0 | return nullptr; |
2227 | | |
2228 | 1 | CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); |
2229 | 1 | return llvm::UndefValue::get(DestLTy); |
2230 | 1 | } |
2231 | | |
2232 | | llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr, |
2233 | 77 | const CXXDynamicCastExpr *DCE) { |
2234 | 77 | CGM.EmitExplicitCastExprType(DCE, this); |
2235 | 77 | QualType DestTy = DCE->getTypeAsWritten(); |
2236 | | |
2237 | 77 | QualType SrcTy = DCE->getSubExpr()->getType(); |
2238 | | |
2239 | | // C++ [expr.dynamic.cast]p7: |
2240 | | // If T is "pointer to cv void," then the result is a pointer to the most |
2241 | | // derived object pointed to by v. |
2242 | 77 | const PointerType *DestPTy = DestTy->getAs<PointerType>(); |
2243 | | |
2244 | 77 | bool isDynamicCastToVoid; |
2245 | 77 | QualType SrcRecordTy; |
2246 | 77 | QualType DestRecordTy; |
2247 | 77 | if (DestPTy) { |
2248 | 64 | isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType(); |
2249 | 64 | SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType(); |
2250 | 64 | DestRecordTy = DestPTy->getPointeeType(); |
2251 | 64 | } else { |
2252 | 13 | isDynamicCastToVoid = false; |
2253 | 13 | SrcRecordTy = SrcTy; |
2254 | 13 | DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType(); |
2255 | 13 | } |
2256 | | |
2257 | | // C++ [class.cdtor]p5: |
2258 | | // If the operand of the dynamic_cast refers to the object under |
2259 | | // construction or destruction and the static type of the operand is not a |
2260 | | // pointer to or object of the constructor or destructor’s own class or one |
2261 | | // of its bases, the dynamic_cast results in undefined behavior. |
2262 | 77 | EmitTypeCheck(TCK_DynamicOperation, DCE->getExprLoc(), ThisAddr.getPointer(), |
2263 | 77 | SrcRecordTy); |
2264 | | |
2265 | 77 | if (DCE->isAlwaysNull()) |
2266 | 2 | if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy)) |
2267 | 2 | return T; |
2268 | | |
2269 | 75 | assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); |
2270 | | |
2271 | | // C++ [expr.dynamic.cast]p4: |
2272 | | // If the value of v is a null pointer value in the pointer case, the result |
2273 | | // is the null pointer value of type T. |
2274 | 0 | bool ShouldNullCheckSrcValue = |
2275 | 75 | CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(), |
2276 | 75 | SrcRecordTy); |
2277 | | |
2278 | 75 | llvm::BasicBlock *CastNull = nullptr; |
2279 | 75 | llvm::BasicBlock *CastNotNull = nullptr; |
2280 | 75 | llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); |
2281 | | |
2282 | 75 | if (ShouldNullCheckSrcValue) { |
2283 | 59 | CastNull = createBasicBlock("dynamic_cast.null"); |
2284 | 59 | CastNotNull = createBasicBlock("dynamic_cast.notnull"); |
2285 | | |
2286 | 59 | llvm::Value *IsNull = Builder.CreateIsNull(ThisAddr.getPointer()); |
2287 | 59 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
2288 | 59 | EmitBlock(CastNotNull); |
2289 | 59 | } |
2290 | | |
2291 | 75 | llvm::Value *Value; |
2292 | 75 | if (isDynamicCastToVoid) { |
2293 | 6 | Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy, |
2294 | 6 | DestTy); |
2295 | 69 | } else { |
2296 | 69 | assert(DestRecordTy->isRecordType() && |
2297 | 69 | "destination type must be a record type!"); |
2298 | 0 | Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy, |
2299 | 69 | DestTy, DestRecordTy, CastEnd); |
2300 | 69 | CastNotNull = Builder.GetInsertBlock(); |
2301 | 69 | } |
2302 | | |
2303 | 75 | if (ShouldNullCheckSrcValue) { |
2304 | 59 | EmitBranch(CastEnd); |
2305 | | |
2306 | 59 | EmitBlock(CastNull); |
2307 | 59 | EmitBranch(CastEnd); |
2308 | 59 | } |
2309 | | |
2310 | 75 | EmitBlock(CastEnd); |
2311 | | |
2312 | 75 | if (ShouldNullCheckSrcValue) { |
2313 | 59 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
2314 | 59 | PHI->addIncoming(Value, CastNotNull); |
2315 | 59 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
2316 | | |
2317 | 59 | Value = PHI; |
2318 | 59 | } |
2319 | | |
2320 | 75 | return Value; |
2321 | 77 | } |