File: | tools/clang/lib/CodeGen/CGDecl.cpp |
Warning: | line 2398, column 9 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// | ||||
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 to emit Decl nodes as LLVM code. | ||||
10 | // | ||||
11 | //===----------------------------------------------------------------------===// | ||||
12 | |||||
13 | #include "CGBlocks.h" | ||||
14 | #include "CGCXXABI.h" | ||||
15 | #include "CGCleanup.h" | ||||
16 | #include "CGDebugInfo.h" | ||||
17 | #include "CGOpenCLRuntime.h" | ||||
18 | #include "CGOpenMPRuntime.h" | ||||
19 | #include "CodeGenFunction.h" | ||||
20 | #include "CodeGenModule.h" | ||||
21 | #include "ConstantEmitter.h" | ||||
22 | #include "PatternInit.h" | ||||
23 | #include "TargetInfo.h" | ||||
24 | #include "clang/AST/ASTContext.h" | ||||
25 | #include "clang/AST/CharUnits.h" | ||||
26 | #include "clang/AST/Decl.h" | ||||
27 | #include "clang/AST/DeclObjC.h" | ||||
28 | #include "clang/AST/DeclOpenMP.h" | ||||
29 | #include "clang/Basic/CodeGenOptions.h" | ||||
30 | #include "clang/Basic/SourceManager.h" | ||||
31 | #include "clang/Basic/TargetInfo.h" | ||||
32 | #include "clang/CodeGen/CGFunctionInfo.h" | ||||
33 | #include "llvm/Analysis/ValueTracking.h" | ||||
34 | #include "llvm/IR/DataLayout.h" | ||||
35 | #include "llvm/IR/GlobalVariable.h" | ||||
36 | #include "llvm/IR/Intrinsics.h" | ||||
37 | #include "llvm/IR/Type.h" | ||||
38 | |||||
39 | using namespace clang; | ||||
40 | using namespace CodeGen; | ||||
41 | |||||
42 | void CodeGenFunction::EmitDecl(const Decl &D) { | ||||
43 | switch (D.getKind()) { | ||||
44 | case Decl::BuiltinTemplate: | ||||
45 | case Decl::TranslationUnit: | ||||
46 | case Decl::ExternCContext: | ||||
47 | case Decl::Namespace: | ||||
48 | case Decl::UnresolvedUsingTypename: | ||||
49 | case Decl::ClassTemplateSpecialization: | ||||
50 | case Decl::ClassTemplatePartialSpecialization: | ||||
51 | case Decl::VarTemplateSpecialization: | ||||
52 | case Decl::VarTemplatePartialSpecialization: | ||||
53 | case Decl::TemplateTypeParm: | ||||
54 | case Decl::UnresolvedUsingValue: | ||||
55 | case Decl::NonTypeTemplateParm: | ||||
56 | case Decl::CXXDeductionGuide: | ||||
57 | case Decl::CXXMethod: | ||||
58 | case Decl::CXXConstructor: | ||||
59 | case Decl::CXXDestructor: | ||||
60 | case Decl::CXXConversion: | ||||
61 | case Decl::Field: | ||||
62 | case Decl::MSProperty: | ||||
63 | case Decl::IndirectField: | ||||
64 | case Decl::ObjCIvar: | ||||
65 | case Decl::ObjCAtDefsField: | ||||
66 | case Decl::ParmVar: | ||||
67 | case Decl::ImplicitParam: | ||||
68 | case Decl::ClassTemplate: | ||||
69 | case Decl::VarTemplate: | ||||
70 | case Decl::FunctionTemplate: | ||||
71 | case Decl::TypeAliasTemplate: | ||||
72 | case Decl::TemplateTemplateParm: | ||||
73 | case Decl::ObjCMethod: | ||||
74 | case Decl::ObjCCategory: | ||||
75 | case Decl::ObjCProtocol: | ||||
76 | case Decl::ObjCInterface: | ||||
77 | case Decl::ObjCCategoryImpl: | ||||
78 | case Decl::ObjCImplementation: | ||||
79 | case Decl::ObjCProperty: | ||||
80 | case Decl::ObjCCompatibleAlias: | ||||
81 | case Decl::PragmaComment: | ||||
82 | case Decl::PragmaDetectMismatch: | ||||
83 | case Decl::AccessSpec: | ||||
84 | case Decl::LinkageSpec: | ||||
85 | case Decl::Export: | ||||
86 | case Decl::ObjCPropertyImpl: | ||||
87 | case Decl::FileScopeAsm: | ||||
88 | case Decl::Friend: | ||||
89 | case Decl::FriendTemplate: | ||||
90 | case Decl::Block: | ||||
91 | case Decl::Captured: | ||||
92 | case Decl::ClassScopeFunctionSpecialization: | ||||
93 | case Decl::UsingShadow: | ||||
94 | case Decl::ConstructorUsingShadow: | ||||
95 | case Decl::ObjCTypeParam: | ||||
96 | case Decl::Binding: | ||||
97 | llvm_unreachable("Declaration should not be in declstmts!")::llvm::llvm_unreachable_internal("Declaration should not be in declstmts!" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 97); | ||||
98 | case Decl::Function: // void X(); | ||||
99 | case Decl::Record: // struct/union/class X; | ||||
100 | case Decl::Enum: // enum X; | ||||
101 | case Decl::EnumConstant: // enum ? { X = ? } | ||||
102 | case Decl::CXXRecord: // struct/union/class X; [C++] | ||||
103 | case Decl::StaticAssert: // static_assert(X, ""); [C++0x] | ||||
104 | case Decl::Label: // __label__ x; | ||||
105 | case Decl::Import: | ||||
106 | case Decl::OMPThreadPrivate: | ||||
107 | case Decl::OMPAllocate: | ||||
108 | case Decl::OMPCapturedExpr: | ||||
109 | case Decl::OMPRequires: | ||||
110 | case Decl::Empty: | ||||
111 | case Decl::Concept: | ||||
112 | // None of these decls require codegen support. | ||||
113 | return; | ||||
114 | |||||
115 | case Decl::NamespaceAlias: | ||||
116 | if (CGDebugInfo *DI = getDebugInfo()) | ||||
117 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D)); | ||||
118 | return; | ||||
119 | case Decl::Using: // using X; [C++] | ||||
120 | if (CGDebugInfo *DI = getDebugInfo()) | ||||
121 | DI->EmitUsingDecl(cast<UsingDecl>(D)); | ||||
122 | return; | ||||
123 | case Decl::UsingPack: | ||||
124 | for (auto *Using : cast<UsingPackDecl>(D).expansions()) | ||||
125 | EmitDecl(*Using); | ||||
126 | return; | ||||
127 | case Decl::UsingDirective: // using namespace X; [C++] | ||||
128 | if (CGDebugInfo *DI = getDebugInfo()) | ||||
129 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D)); | ||||
130 | return; | ||||
131 | case Decl::Var: | ||||
132 | case Decl::Decomposition: { | ||||
133 | const VarDecl &VD = cast<VarDecl>(D); | ||||
134 | assert(VD.isLocalVarDecl() &&((VD.isLocalVarDecl() && "Should not see file-scope variables inside a function!" ) ? static_cast<void> (0) : __assert_fail ("VD.isLocalVarDecl() && \"Should not see file-scope variables inside a function!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 135, __PRETTY_FUNCTION__)) | ||||
135 | "Should not see file-scope variables inside a function!")((VD.isLocalVarDecl() && "Should not see file-scope variables inside a function!" ) ? static_cast<void> (0) : __assert_fail ("VD.isLocalVarDecl() && \"Should not see file-scope variables inside a function!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 135, __PRETTY_FUNCTION__)); | ||||
136 | EmitVarDecl(VD); | ||||
137 | if (auto *DD = dyn_cast<DecompositionDecl>(&VD)) | ||||
138 | for (auto *B : DD->bindings()) | ||||
139 | if (auto *HD = B->getHoldingVar()) | ||||
140 | EmitVarDecl(*HD); | ||||
141 | return; | ||||
142 | } | ||||
143 | |||||
144 | case Decl::OMPDeclareReduction: | ||||
145 | return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this); | ||||
146 | |||||
147 | case Decl::OMPDeclareMapper: | ||||
148 | return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this); | ||||
149 | |||||
150 | case Decl::Typedef: // typedef int X; | ||||
151 | case Decl::TypeAlias: { // using X = int; [C++0x] | ||||
152 | const TypedefNameDecl &TD = cast<TypedefNameDecl>(D); | ||||
153 | QualType Ty = TD.getUnderlyingType(); | ||||
154 | |||||
155 | if (Ty->isVariablyModifiedType()) | ||||
156 | EmitVariablyModifiedType(Ty); | ||||
157 | |||||
158 | return; | ||||
159 | } | ||||
160 | } | ||||
161 | } | ||||
162 | |||||
163 | /// EmitVarDecl - This method handles emission of any variable declaration | ||||
164 | /// inside a function, including static vars etc. | ||||
165 | void CodeGenFunction::EmitVarDecl(const VarDecl &D) { | ||||
166 | if (D.hasExternalStorage()) | ||||
167 | // Don't emit it now, allow it to be emitted lazily on its first use. | ||||
168 | return; | ||||
169 | |||||
170 | // Some function-scope variable does not have static storage but still | ||||
171 | // needs to be emitted like a static variable, e.g. a function-scope | ||||
172 | // variable in constant address space in OpenCL. | ||||
173 | if (D.getStorageDuration() != SD_Automatic) { | ||||
174 | // Static sampler variables translated to function calls. | ||||
175 | if (D.getType()->isSamplerT()) | ||||
176 | return; | ||||
177 | |||||
178 | llvm::GlobalValue::LinkageTypes Linkage = | ||||
179 | CGM.getLLVMLinkageVarDefinition(&D, /*IsConstant=*/false); | ||||
180 | |||||
181 | // FIXME: We need to force the emission/use of a guard variable for | ||||
182 | // some variables even if we can constant-evaluate them because | ||||
183 | // we can't guarantee every translation unit will constant-evaluate them. | ||||
184 | |||||
185 | return EmitStaticVarDecl(D, Linkage); | ||||
186 | } | ||||
187 | |||||
188 | if (D.getType().getAddressSpace() == LangAS::opencl_local) | ||||
189 | return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D); | ||||
190 | |||||
191 | assert(D.hasLocalStorage())((D.hasLocalStorage()) ? static_cast<void> (0) : __assert_fail ("D.hasLocalStorage()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 191, __PRETTY_FUNCTION__)); | ||||
192 | return EmitAutoVarDecl(D); | ||||
193 | } | ||||
194 | |||||
195 | static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) { | ||||
196 | if (CGM.getLangOpts().CPlusPlus) | ||||
197 | return CGM.getMangledName(&D).str(); | ||||
198 | |||||
199 | // If this isn't C++, we don't need a mangled name, just a pretty one. | ||||
200 | assert(!D.isExternallyVisible() && "name shouldn't matter")((!D.isExternallyVisible() && "name shouldn't matter" ) ? static_cast<void> (0) : __assert_fail ("!D.isExternallyVisible() && \"name shouldn't matter\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 200, __PRETTY_FUNCTION__)); | ||||
201 | std::string ContextName; | ||||
202 | const DeclContext *DC = D.getDeclContext(); | ||||
203 | if (auto *CD = dyn_cast<CapturedDecl>(DC)) | ||||
204 | DC = cast<DeclContext>(CD->getNonClosureContext()); | ||||
205 | if (const auto *FD = dyn_cast<FunctionDecl>(DC)) | ||||
206 | ContextName = CGM.getMangledName(FD); | ||||
207 | else if (const auto *BD = dyn_cast<BlockDecl>(DC)) | ||||
208 | ContextName = CGM.getBlockMangledName(GlobalDecl(), BD); | ||||
209 | else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC)) | ||||
210 | ContextName = OMD->getSelector().getAsString(); | ||||
211 | else | ||||
212 | llvm_unreachable("Unknown context for static var decl")::llvm::llvm_unreachable_internal("Unknown context for static var decl" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 212); | ||||
213 | |||||
214 | ContextName += "." + D.getNameAsString(); | ||||
215 | return ContextName; | ||||
216 | } | ||||
217 | |||||
218 | llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl( | ||||
219 | const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) { | ||||
220 | // In general, we don't always emit static var decls once before we reference | ||||
221 | // them. It is possible to reference them before emitting the function that | ||||
222 | // contains them, and it is possible to emit the containing function multiple | ||||
223 | // times. | ||||
224 | if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D]) | ||||
225 | return ExistingGV; | ||||
226 | |||||
227 | QualType Ty = D.getType(); | ||||
228 | assert(Ty->isConstantSizeType() && "VLAs can't be static")((Ty->isConstantSizeType() && "VLAs can't be static" ) ? static_cast<void> (0) : __assert_fail ("Ty->isConstantSizeType() && \"VLAs can't be static\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 228, __PRETTY_FUNCTION__)); | ||||
229 | |||||
230 | // Use the label if the variable is renamed with the asm-label extension. | ||||
231 | std::string Name; | ||||
232 | if (D.hasAttr<AsmLabelAttr>()) | ||||
233 | Name = getMangledName(&D); | ||||
234 | else | ||||
235 | Name = getStaticDeclName(*this, D); | ||||
236 | |||||
237 | llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty); | ||||
238 | LangAS AS = GetGlobalVarAddressSpace(&D); | ||||
239 | unsigned TargetAS = getContext().getTargetAddressSpace(AS); | ||||
240 | |||||
241 | // OpenCL variables in local address space and CUDA shared | ||||
242 | // variables cannot have an initializer. | ||||
243 | llvm::Constant *Init = nullptr; | ||||
244 | if (Ty.getAddressSpace() == LangAS::opencl_local || | ||||
245 | D.hasAttr<CUDASharedAttr>()) | ||||
246 | Init = llvm::UndefValue::get(LTy); | ||||
247 | else | ||||
248 | Init = EmitNullConstant(Ty); | ||||
249 | |||||
250 | llvm::GlobalVariable *GV = new llvm::GlobalVariable( | ||||
251 | getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name, | ||||
252 | nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); | ||||
253 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); | ||||
254 | |||||
255 | if (supportsCOMDAT() && GV->isWeakForLinker()) | ||||
256 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); | ||||
257 | |||||
258 | if (D.getTLSKind()) | ||||
259 | setTLSMode(GV, D); | ||||
260 | |||||
261 | setGVProperties(GV, &D); | ||||
262 | |||||
263 | // Make sure the result is of the correct type. | ||||
264 | LangAS ExpectedAS = Ty.getAddressSpace(); | ||||
265 | llvm::Constant *Addr = GV; | ||||
266 | if (AS != ExpectedAS) { | ||||
267 | Addr = getTargetCodeGenInfo().performAddrSpaceCast( | ||||
268 | *this, GV, AS, ExpectedAS, | ||||
269 | LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS))); | ||||
270 | } | ||||
271 | |||||
272 | setStaticLocalDeclAddress(&D, Addr); | ||||
273 | |||||
274 | // Ensure that the static local gets initialized by making sure the parent | ||||
275 | // function gets emitted eventually. | ||||
276 | const Decl *DC = cast<Decl>(D.getDeclContext()); | ||||
277 | |||||
278 | // We can't name blocks or captured statements directly, so try to emit their | ||||
279 | // parents. | ||||
280 | if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) { | ||||
281 | DC = DC->getNonClosureContext(); | ||||
282 | // FIXME: Ensure that global blocks get emitted. | ||||
283 | if (!DC) | ||||
284 | return Addr; | ||||
285 | } | ||||
286 | |||||
287 | GlobalDecl GD; | ||||
288 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC)) | ||||
289 | GD = GlobalDecl(CD, Ctor_Base); | ||||
290 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC)) | ||||
291 | GD = GlobalDecl(DD, Dtor_Base); | ||||
292 | else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) | ||||
293 | GD = GlobalDecl(FD); | ||||
294 | else { | ||||
295 | // Don't do anything for Obj-C method decls or global closures. We should | ||||
296 | // never defer them. | ||||
297 | assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl")((isa<ObjCMethodDecl>(DC) && "unexpected parent code decl" ) ? static_cast<void> (0) : __assert_fail ("isa<ObjCMethodDecl>(DC) && \"unexpected parent code decl\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 297, __PRETTY_FUNCTION__)); | ||||
298 | } | ||||
299 | if (GD.getDecl()) { | ||||
300 | // Disable emission of the parent function for the OpenMP device codegen. | ||||
301 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII NoDeclTarget(*this); | ||||
302 | (void)GetAddrOfGlobal(GD); | ||||
303 | } | ||||
304 | |||||
305 | return Addr; | ||||
306 | } | ||||
307 | |||||
308 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the | ||||
309 | /// global variable that has already been created for it. If the initializer | ||||
310 | /// has a different type than GV does, this may free GV and return a different | ||||
311 | /// one. Otherwise it just returns GV. | ||||
312 | llvm::GlobalVariable * | ||||
313 | CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D, | ||||
314 | llvm::GlobalVariable *GV) { | ||||
315 | ConstantEmitter emitter(*this); | ||||
316 | llvm::Constant *Init = emitter.tryEmitForInitializer(D); | ||||
317 | |||||
318 | // If constant emission failed, then this should be a C++ static | ||||
319 | // initializer. | ||||
320 | if (!Init) { | ||||
321 | if (!getLangOpts().CPlusPlus) | ||||
322 | CGM.ErrorUnsupported(D.getInit(), "constant l-value expression"); | ||||
323 | else if (HaveInsertPoint()) { | ||||
324 | // Since we have a static initializer, this global variable can't | ||||
325 | // be constant. | ||||
326 | GV->setConstant(false); | ||||
327 | |||||
328 | EmitCXXGuardedInit(D, GV, /*PerformInit*/true); | ||||
329 | } | ||||
330 | return GV; | ||||
331 | } | ||||
332 | |||||
333 | // The initializer may differ in type from the global. Rewrite | ||||
334 | // the global to match the initializer. (We have to do this | ||||
335 | // because some types, like unions, can't be completely represented | ||||
336 | // in the LLVM type system.) | ||||
337 | if (GV->getType()->getElementType() != Init->getType()) { | ||||
338 | llvm::GlobalVariable *OldGV = GV; | ||||
339 | |||||
340 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), | ||||
341 | OldGV->isConstant(), | ||||
342 | OldGV->getLinkage(), Init, "", | ||||
343 | /*InsertBefore*/ OldGV, | ||||
344 | OldGV->getThreadLocalMode(), | ||||
345 | CGM.getContext().getTargetAddressSpace(D.getType())); | ||||
346 | GV->setVisibility(OldGV->getVisibility()); | ||||
347 | GV->setDSOLocal(OldGV->isDSOLocal()); | ||||
348 | GV->setComdat(OldGV->getComdat()); | ||||
349 | |||||
350 | // Steal the name of the old global | ||||
351 | GV->takeName(OldGV); | ||||
352 | |||||
353 | // Replace all uses of the old global with the new global | ||||
354 | llvm::Constant *NewPtrForOldDecl = | ||||
355 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); | ||||
356 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); | ||||
357 | |||||
358 | // Erase the old global, since it is no longer used. | ||||
359 | OldGV->eraseFromParent(); | ||||
360 | } | ||||
361 | |||||
362 | GV->setConstant(CGM.isTypeConstant(D.getType(), true)); | ||||
363 | GV->setInitializer(Init); | ||||
364 | |||||
365 | emitter.finalize(GV); | ||||
366 | |||||
367 | if (D.needsDestruction(getContext()) && HaveInsertPoint()) { | ||||
368 | // We have a constant initializer, but a nontrivial destructor. We still | ||||
369 | // need to perform a guarded "initialization" in order to register the | ||||
370 | // destructor. | ||||
371 | EmitCXXGuardedInit(D, GV, /*PerformInit*/false); | ||||
372 | } | ||||
373 | |||||
374 | return GV; | ||||
375 | } | ||||
376 | |||||
377 | void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, | ||||
378 | llvm::GlobalValue::LinkageTypes Linkage) { | ||||
379 | // Check to see if we already have a global variable for this | ||||
380 | // declaration. This can happen when double-emitting function | ||||
381 | // bodies, e.g. with complete and base constructors. | ||||
382 | llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage); | ||||
383 | CharUnits alignment = getContext().getDeclAlign(&D); | ||||
384 | |||||
385 | // Store into LocalDeclMap before generating initializer to handle | ||||
386 | // circular references. | ||||
387 | setAddrOfLocalVar(&D, Address(addr, alignment)); | ||||
388 | |||||
389 | // We can't have a VLA here, but we can have a pointer to a VLA, | ||||
390 | // even though that doesn't really make any sense. | ||||
391 | // Make sure to evaluate VLA bounds now so that we have them for later. | ||||
392 | if (D.getType()->isVariablyModifiedType()) | ||||
393 | EmitVariablyModifiedType(D.getType()); | ||||
394 | |||||
395 | // Save the type in case adding the initializer forces a type change. | ||||
396 | llvm::Type *expectedType = addr->getType(); | ||||
397 | |||||
398 | llvm::GlobalVariable *var = | ||||
399 | cast<llvm::GlobalVariable>(addr->stripPointerCasts()); | ||||
400 | |||||
401 | // CUDA's local and local static __shared__ variables should not | ||||
402 | // have any non-empty initializers. This is ensured by Sema. | ||||
403 | // Whatever initializer such variable may have when it gets here is | ||||
404 | // a no-op and should not be emitted. | ||||
405 | bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice && | ||||
406 | D.hasAttr<CUDASharedAttr>(); | ||||
407 | // If this value has an initializer, emit it. | ||||
408 | if (D.getInit() && !isCudaSharedVar) | ||||
409 | var = AddInitializerToStaticVarDecl(D, var); | ||||
410 | |||||
411 | var->setAlignment(alignment.getQuantity()); | ||||
412 | |||||
413 | if (D.hasAttr<AnnotateAttr>()) | ||||
414 | CGM.AddGlobalAnnotations(&D, var); | ||||
415 | |||||
416 | if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>()) | ||||
417 | var->addAttribute("bss-section", SA->getName()); | ||||
418 | if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>()) | ||||
419 | var->addAttribute("data-section", SA->getName()); | ||||
420 | if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>()) | ||||
421 | var->addAttribute("rodata-section", SA->getName()); | ||||
422 | |||||
423 | if (const SectionAttr *SA = D.getAttr<SectionAttr>()) | ||||
424 | var->setSection(SA->getName()); | ||||
425 | |||||
426 | if (D.hasAttr<UsedAttr>()) | ||||
427 | CGM.addUsedGlobal(var); | ||||
428 | |||||
429 | // We may have to cast the constant because of the initializer | ||||
430 | // mismatch above. | ||||
431 | // | ||||
432 | // FIXME: It is really dangerous to store this in the map; if anyone | ||||
433 | // RAUW's the GV uses of this constant will be invalid. | ||||
434 | llvm::Constant *castedAddr = | ||||
435 | llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType); | ||||
436 | if (var != castedAddr) | ||||
437 | LocalDeclMap.find(&D)->second = Address(castedAddr, alignment); | ||||
438 | CGM.setStaticLocalDeclAddress(&D, castedAddr); | ||||
439 | |||||
440 | CGM.getSanitizerMetadata()->reportGlobalToASan(var, D); | ||||
441 | |||||
442 | // Emit global variable debug descriptor for static vars. | ||||
443 | CGDebugInfo *DI = getDebugInfo(); | ||||
444 | if (DI && | ||||
445 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) { | ||||
446 | DI->setLocation(D.getLocation()); | ||||
447 | DI->EmitGlobalVariable(var, &D); | ||||
448 | } | ||||
449 | } | ||||
450 | |||||
451 | namespace { | ||||
452 | struct DestroyObject final : EHScopeStack::Cleanup { | ||||
453 | DestroyObject(Address addr, QualType type, | ||||
454 | CodeGenFunction::Destroyer *destroyer, | ||||
455 | bool useEHCleanupForArray) | ||||
456 | : addr(addr), type(type), destroyer(destroyer), | ||||
457 | useEHCleanupForArray(useEHCleanupForArray) {} | ||||
458 | |||||
459 | Address addr; | ||||
460 | QualType type; | ||||
461 | CodeGenFunction::Destroyer *destroyer; | ||||
462 | bool useEHCleanupForArray; | ||||
463 | |||||
464 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
465 | // Don't use an EH cleanup recursively from an EH cleanup. | ||||
466 | bool useEHCleanupForArray = | ||||
467 | flags.isForNormalCleanup() && this->useEHCleanupForArray; | ||||
468 | |||||
469 | CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray); | ||||
470 | } | ||||
471 | }; | ||||
472 | |||||
473 | template <class Derived> | ||||
474 | struct DestroyNRVOVariable : EHScopeStack::Cleanup { | ||||
475 | DestroyNRVOVariable(Address addr, QualType type, llvm::Value *NRVOFlag) | ||||
476 | : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {} | ||||
477 | |||||
478 | llvm::Value *NRVOFlag; | ||||
479 | Address Loc; | ||||
480 | QualType Ty; | ||||
481 | |||||
482 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
483 | // Along the exceptions path we always execute the dtor. | ||||
484 | bool NRVO = flags.isForNormalCleanup() && NRVOFlag; | ||||
485 | |||||
486 | llvm::BasicBlock *SkipDtorBB = nullptr; | ||||
487 | if (NRVO) { | ||||
488 | // If we exited via NRVO, we skip the destructor call. | ||||
489 | llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused"); | ||||
490 | SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor"); | ||||
491 | llvm::Value *DidNRVO = | ||||
492 | CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val"); | ||||
493 | CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB); | ||||
494 | CGF.EmitBlock(RunDtorBB); | ||||
495 | } | ||||
496 | |||||
497 | static_cast<Derived *>(this)->emitDestructorCall(CGF); | ||||
498 | |||||
499 | if (NRVO) CGF.EmitBlock(SkipDtorBB); | ||||
500 | } | ||||
501 | |||||
502 | virtual ~DestroyNRVOVariable() = default; | ||||
503 | }; | ||||
504 | |||||
505 | struct DestroyNRVOVariableCXX final | ||||
506 | : DestroyNRVOVariable<DestroyNRVOVariableCXX> { | ||||
507 | DestroyNRVOVariableCXX(Address addr, QualType type, | ||||
508 | const CXXDestructorDecl *Dtor, llvm::Value *NRVOFlag) | ||||
509 | : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, NRVOFlag), | ||||
510 | Dtor(Dtor) {} | ||||
511 | |||||
512 | const CXXDestructorDecl *Dtor; | ||||
513 | |||||
514 | void emitDestructorCall(CodeGenFunction &CGF) { | ||||
515 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, | ||||
516 | /*ForVirtualBase=*/false, | ||||
517 | /*Delegating=*/false, Loc, Ty); | ||||
518 | } | ||||
519 | }; | ||||
520 | |||||
521 | struct DestroyNRVOVariableC final | ||||
522 | : DestroyNRVOVariable<DestroyNRVOVariableC> { | ||||
523 | DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty) | ||||
524 | : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {} | ||||
525 | |||||
526 | void emitDestructorCall(CodeGenFunction &CGF) { | ||||
527 | CGF.destroyNonTrivialCStruct(CGF, Loc, Ty); | ||||
528 | } | ||||
529 | }; | ||||
530 | |||||
531 | struct CallStackRestore final : EHScopeStack::Cleanup { | ||||
532 | Address Stack; | ||||
533 | CallStackRestore(Address Stack) : Stack(Stack) {} | ||||
534 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
535 | llvm::Value *V = CGF.Builder.CreateLoad(Stack); | ||||
536 | llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); | ||||
537 | CGF.Builder.CreateCall(F, V); | ||||
538 | } | ||||
539 | }; | ||||
540 | |||||
541 | struct ExtendGCLifetime final : EHScopeStack::Cleanup { | ||||
542 | const VarDecl &Var; | ||||
543 | ExtendGCLifetime(const VarDecl *var) : Var(*var) {} | ||||
544 | |||||
545 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
546 | // Compute the address of the local variable, in case it's a | ||||
547 | // byref or something. | ||||
548 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false, | ||||
549 | Var.getType(), VK_LValue, SourceLocation()); | ||||
550 | llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE), | ||||
551 | SourceLocation()); | ||||
552 | CGF.EmitExtendGCLifetime(value); | ||||
553 | } | ||||
554 | }; | ||||
555 | |||||
556 | struct CallCleanupFunction final : EHScopeStack::Cleanup { | ||||
557 | llvm::Constant *CleanupFn; | ||||
558 | const CGFunctionInfo &FnInfo; | ||||
559 | const VarDecl &Var; | ||||
560 | |||||
561 | CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info, | ||||
562 | const VarDecl *Var) | ||||
563 | : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {} | ||||
564 | |||||
565 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
566 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false, | ||||
567 | Var.getType(), VK_LValue, SourceLocation()); | ||||
568 | // Compute the address of the local variable, in case it's a byref | ||||
569 | // or something. | ||||
570 | llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer(); | ||||
571 | |||||
572 | // In some cases, the type of the function argument will be different from | ||||
573 | // the type of the pointer. An example of this is | ||||
574 | // void f(void* arg); | ||||
575 | // __attribute__((cleanup(f))) void *g; | ||||
576 | // | ||||
577 | // To fix this we insert a bitcast here. | ||||
578 | QualType ArgTy = FnInfo.arg_begin()->type; | ||||
579 | llvm::Value *Arg = | ||||
580 | CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy)); | ||||
581 | |||||
582 | CallArgList Args; | ||||
583 | Args.add(RValue::get(Arg), | ||||
584 | CGF.getContext().getPointerType(Var.getType())); | ||||
585 | auto Callee = CGCallee::forDirect(CleanupFn); | ||||
586 | CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args); | ||||
587 | } | ||||
588 | }; | ||||
589 | } // end anonymous namespace | ||||
590 | |||||
591 | /// EmitAutoVarWithLifetime - Does the setup required for an automatic | ||||
592 | /// variable with lifetime. | ||||
593 | static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var, | ||||
594 | Address addr, | ||||
595 | Qualifiers::ObjCLifetime lifetime) { | ||||
596 | switch (lifetime) { | ||||
597 | case Qualifiers::OCL_None: | ||||
598 | llvm_unreachable("present but none")::llvm::llvm_unreachable_internal("present but none", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 598); | ||||
599 | |||||
600 | case Qualifiers::OCL_ExplicitNone: | ||||
601 | // nothing to do | ||||
602 | break; | ||||
603 | |||||
604 | case Qualifiers::OCL_Strong: { | ||||
605 | CodeGenFunction::Destroyer *destroyer = | ||||
606 | (var.hasAttr<ObjCPreciseLifetimeAttr>() | ||||
607 | ? CodeGenFunction::destroyARCStrongPrecise | ||||
608 | : CodeGenFunction::destroyARCStrongImprecise); | ||||
609 | |||||
610 | CleanupKind cleanupKind = CGF.getARCCleanupKind(); | ||||
611 | CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer, | ||||
612 | cleanupKind & EHCleanup); | ||||
613 | break; | ||||
614 | } | ||||
615 | case Qualifiers::OCL_Autoreleasing: | ||||
616 | // nothing to do | ||||
617 | break; | ||||
618 | |||||
619 | case Qualifiers::OCL_Weak: | ||||
620 | // __weak objects always get EH cleanups; otherwise, exceptions | ||||
621 | // could cause really nasty crashes instead of mere leaks. | ||||
622 | CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(), | ||||
623 | CodeGenFunction::destroyARCWeak, | ||||
624 | /*useEHCleanup*/ true); | ||||
625 | break; | ||||
626 | } | ||||
627 | } | ||||
628 | |||||
629 | static bool isAccessedBy(const VarDecl &var, const Stmt *s) { | ||||
630 | if (const Expr *e = dyn_cast<Expr>(s)) { | ||||
631 | // Skip the most common kinds of expressions that make | ||||
632 | // hierarchy-walking expensive. | ||||
633 | s = e = e->IgnoreParenCasts(); | ||||
634 | |||||
635 | if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) | ||||
636 | return (ref->getDecl() == &var); | ||||
637 | if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { | ||||
638 | const BlockDecl *block = be->getBlockDecl(); | ||||
639 | for (const auto &I : block->captures()) { | ||||
640 | if (I.getVariable() == &var) | ||||
641 | return true; | ||||
642 | } | ||||
643 | } | ||||
644 | } | ||||
645 | |||||
646 | for (const Stmt *SubStmt : s->children()) | ||||
647 | // SubStmt might be null; as in missing decl or conditional of an if-stmt. | ||||
648 | if (SubStmt && isAccessedBy(var, SubStmt)) | ||||
649 | return true; | ||||
650 | |||||
651 | return false; | ||||
652 | } | ||||
653 | |||||
654 | static bool isAccessedBy(const ValueDecl *decl, const Expr *e) { | ||||
655 | if (!decl) return false; | ||||
656 | if (!isa<VarDecl>(decl)) return false; | ||||
657 | const VarDecl *var = cast<VarDecl>(decl); | ||||
658 | return isAccessedBy(*var, e); | ||||
659 | } | ||||
660 | |||||
661 | static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, | ||||
662 | const LValue &destLV, const Expr *init) { | ||||
663 | bool needsCast = false; | ||||
664 | |||||
665 | while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) { | ||||
666 | switch (castExpr->getCastKind()) { | ||||
667 | // Look through casts that don't require representation changes. | ||||
668 | case CK_NoOp: | ||||
669 | case CK_BitCast: | ||||
670 | case CK_BlockPointerToObjCPointerCast: | ||||
671 | needsCast = true; | ||||
672 | break; | ||||
673 | |||||
674 | // If we find an l-value to r-value cast from a __weak variable, | ||||
675 | // emit this operation as a copy or move. | ||||
676 | case CK_LValueToRValue: { | ||||
677 | const Expr *srcExpr = castExpr->getSubExpr(); | ||||
678 | if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak) | ||||
679 | return false; | ||||
680 | |||||
681 | // Emit the source l-value. | ||||
682 | LValue srcLV = CGF.EmitLValue(srcExpr); | ||||
683 | |||||
684 | // Handle a formal type change to avoid asserting. | ||||
685 | auto srcAddr = srcLV.getAddress(); | ||||
686 | if (needsCast) { | ||||
687 | srcAddr = CGF.Builder.CreateElementBitCast(srcAddr, | ||||
688 | destLV.getAddress().getElementType()); | ||||
689 | } | ||||
690 | |||||
691 | // If it was an l-value, use objc_copyWeak. | ||||
692 | if (srcExpr->getValueKind() == VK_LValue) { | ||||
693 | CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr); | ||||
694 | } else { | ||||
695 | assert(srcExpr->getValueKind() == VK_XValue)((srcExpr->getValueKind() == VK_XValue) ? static_cast<void > (0) : __assert_fail ("srcExpr->getValueKind() == VK_XValue" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 695, __PRETTY_FUNCTION__)); | ||||
696 | CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr); | ||||
697 | } | ||||
698 | return true; | ||||
699 | } | ||||
700 | |||||
701 | // Stop at anything else. | ||||
702 | default: | ||||
703 | return false; | ||||
704 | } | ||||
705 | |||||
706 | init = castExpr->getSubExpr(); | ||||
707 | } | ||||
708 | return false; | ||||
709 | } | ||||
710 | |||||
711 | static void drillIntoBlockVariable(CodeGenFunction &CGF, | ||||
712 | LValue &lvalue, | ||||
713 | const VarDecl *var) { | ||||
714 | lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var)); | ||||
715 | } | ||||
716 | |||||
717 | void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, | ||||
718 | SourceLocation Loc) { | ||||
719 | if (!SanOpts.has(SanitizerKind::NullabilityAssign)) | ||||
720 | return; | ||||
721 | |||||
722 | auto Nullability = LHS.getType()->getNullability(getContext()); | ||||
723 | if (!Nullability || *Nullability != NullabilityKind::NonNull) | ||||
724 | return; | ||||
725 | |||||
726 | // Check if the right hand side of the assignment is nonnull, if the left | ||||
727 | // hand side must be nonnull. | ||||
728 | SanitizerScope SanScope(this); | ||||
729 | llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS); | ||||
730 | llvm::Constant *StaticData[] = { | ||||
731 | EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()), | ||||
732 | llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused. | ||||
733 | llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)}; | ||||
734 | EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}}, | ||||
735 | SanitizerHandler::TypeMismatch, StaticData, RHS); | ||||
736 | } | ||||
737 | |||||
738 | void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D, | ||||
739 | LValue lvalue, bool capturedByInit) { | ||||
740 | Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime(); | ||||
741 | if (!lifetime) { | ||||
742 | llvm::Value *value = EmitScalarExpr(init); | ||||
743 | if (capturedByInit) | ||||
744 | drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); | ||||
745 | EmitNullabilityCheck(lvalue, value, init->getExprLoc()); | ||||
746 | EmitStoreThroughLValue(RValue::get(value), lvalue, true); | ||||
747 | return; | ||||
748 | } | ||||
749 | |||||
750 | if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init)) | ||||
751 | init = DIE->getExpr(); | ||||
752 | |||||
753 | // If we're emitting a value with lifetime, we have to do the | ||||
754 | // initialization *before* we leave the cleanup scopes. | ||||
755 | if (const FullExpr *fe = dyn_cast<FullExpr>(init)) { | ||||
756 | enterFullExpression(fe); | ||||
757 | init = fe->getSubExpr(); | ||||
758 | } | ||||
759 | CodeGenFunction::RunCleanupsScope Scope(*this); | ||||
760 | |||||
761 | // We have to maintain the illusion that the variable is | ||||
762 | // zero-initialized. If the variable might be accessed in its | ||||
763 | // initializer, zero-initialize before running the initializer, then | ||||
764 | // actually perform the initialization with an assign. | ||||
765 | bool accessedByInit = false; | ||||
766 | if (lifetime != Qualifiers::OCL_ExplicitNone) | ||||
767 | accessedByInit = (capturedByInit || isAccessedBy(D, init)); | ||||
768 | if (accessedByInit) { | ||||
769 | LValue tempLV = lvalue; | ||||
770 | // Drill down to the __block object if necessary. | ||||
771 | if (capturedByInit) { | ||||
772 | // We can use a simple GEP for this because it can't have been | ||||
773 | // moved yet. | ||||
774 | tempLV.setAddress(emitBlockByrefAddress(tempLV.getAddress(), | ||||
775 | cast<VarDecl>(D), | ||||
776 | /*follow*/ false)); | ||||
777 | } | ||||
778 | |||||
779 | auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType()); | ||||
780 | llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType()); | ||||
781 | |||||
782 | // If __weak, we want to use a barrier under certain conditions. | ||||
783 | if (lifetime == Qualifiers::OCL_Weak) | ||||
784 | EmitARCInitWeak(tempLV.getAddress(), zero); | ||||
785 | |||||
786 | // Otherwise just do a simple store. | ||||
787 | else | ||||
788 | EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true); | ||||
789 | } | ||||
790 | |||||
791 | // Emit the initializer. | ||||
792 | llvm::Value *value = nullptr; | ||||
793 | |||||
794 | switch (lifetime) { | ||||
795 | case Qualifiers::OCL_None: | ||||
796 | llvm_unreachable("present but none")::llvm::llvm_unreachable_internal("present but none", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 796); | ||||
797 | |||||
798 | case Qualifiers::OCL_Strong: { | ||||
799 | if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) { | ||||
800 | value = EmitARCRetainScalarExpr(init); | ||||
801 | break; | ||||
802 | } | ||||
803 | // If D is pseudo-strong, treat it like __unsafe_unretained here. This means | ||||
804 | // that we omit the retain, and causes non-autoreleased return values to be | ||||
805 | // immediately released. | ||||
806 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||
807 | } | ||||
808 | |||||
809 | case Qualifiers::OCL_ExplicitNone: | ||||
810 | value = EmitARCUnsafeUnretainedScalarExpr(init); | ||||
811 | break; | ||||
812 | |||||
813 | case Qualifiers::OCL_Weak: { | ||||
814 | // If it's not accessed by the initializer, try to emit the | ||||
815 | // initialization with a copy or move. | ||||
816 | if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) { | ||||
817 | return; | ||||
818 | } | ||||
819 | |||||
820 | // No way to optimize a producing initializer into this. It's not | ||||
821 | // worth optimizing for, because the value will immediately | ||||
822 | // disappear in the common case. | ||||
823 | value = EmitScalarExpr(init); | ||||
824 | |||||
825 | if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); | ||||
826 | if (accessedByInit) | ||||
827 | EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true); | ||||
828 | else | ||||
829 | EmitARCInitWeak(lvalue.getAddress(), value); | ||||
830 | return; | ||||
831 | } | ||||
832 | |||||
833 | case Qualifiers::OCL_Autoreleasing: | ||||
834 | value = EmitARCRetainAutoreleaseScalarExpr(init); | ||||
835 | break; | ||||
836 | } | ||||
837 | |||||
838 | if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); | ||||
839 | |||||
840 | EmitNullabilityCheck(lvalue, value, init->getExprLoc()); | ||||
841 | |||||
842 | // If the variable might have been accessed by its initializer, we | ||||
843 | // might have to initialize with a barrier. We have to do this for | ||||
844 | // both __weak and __strong, but __weak got filtered out above. | ||||
845 | if (accessedByInit && lifetime == Qualifiers::OCL_Strong) { | ||||
846 | llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc()); | ||||
847 | EmitStoreOfScalar(value, lvalue, /* isInitialization */ true); | ||||
848 | EmitARCRelease(oldValue, ARCImpreciseLifetime); | ||||
849 | return; | ||||
850 | } | ||||
851 | |||||
852 | EmitStoreOfScalar(value, lvalue, /* isInitialization */ true); | ||||
853 | } | ||||
854 | |||||
855 | /// Decide whether we can emit the non-zero parts of the specified initializer | ||||
856 | /// with equal or fewer than NumStores scalar stores. | ||||
857 | static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init, | ||||
858 | unsigned &NumStores) { | ||||
859 | // Zero and Undef never requires any extra stores. | ||||
860 | if (isa<llvm::ConstantAggregateZero>(Init) || | ||||
861 | isa<llvm::ConstantPointerNull>(Init) || | ||||
862 | isa<llvm::UndefValue>(Init)) | ||||
863 | return true; | ||||
864 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || | ||||
865 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || | ||||
866 | isa<llvm::ConstantExpr>(Init)) | ||||
867 | return Init->isNullValue() || NumStores--; | ||||
868 | |||||
869 | // See if we can emit each element. | ||||
870 | if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) { | ||||
871 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { | ||||
872 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); | ||||
873 | if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores)) | ||||
874 | return false; | ||||
875 | } | ||||
876 | return true; | ||||
877 | } | ||||
878 | |||||
879 | if (llvm::ConstantDataSequential *CDS = | ||||
880 | dyn_cast<llvm::ConstantDataSequential>(Init)) { | ||||
881 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { | ||||
882 | llvm::Constant *Elt = CDS->getElementAsConstant(i); | ||||
883 | if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores)) | ||||
884 | return false; | ||||
885 | } | ||||
886 | return true; | ||||
887 | } | ||||
888 | |||||
889 | // Anything else is hard and scary. | ||||
890 | return false; | ||||
891 | } | ||||
892 | |||||
893 | /// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit | ||||
894 | /// the scalar stores that would be required. | ||||
895 | static void emitStoresForInitAfterBZero(CodeGenModule &CGM, | ||||
896 | llvm::Constant *Init, Address Loc, | ||||
897 | bool isVolatile, CGBuilderTy &Builder) { | ||||
898 | assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&((!Init->isNullValue() && !isa<llvm::UndefValue >(Init) && "called emitStoresForInitAfterBZero for zero or undef value." ) ? static_cast<void> (0) : __assert_fail ("!Init->isNullValue() && !isa<llvm::UndefValue>(Init) && \"called emitStoresForInitAfterBZero for zero or undef value.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 899, __PRETTY_FUNCTION__)) | ||||
899 | "called emitStoresForInitAfterBZero for zero or undef value.")((!Init->isNullValue() && !isa<llvm::UndefValue >(Init) && "called emitStoresForInitAfterBZero for zero or undef value." ) ? static_cast<void> (0) : __assert_fail ("!Init->isNullValue() && !isa<llvm::UndefValue>(Init) && \"called emitStoresForInitAfterBZero for zero or undef value.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 899, __PRETTY_FUNCTION__)); | ||||
900 | |||||
901 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || | ||||
902 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || | ||||
903 | isa<llvm::ConstantExpr>(Init)) { | ||||
904 | Builder.CreateStore(Init, Loc, isVolatile); | ||||
905 | return; | ||||
906 | } | ||||
907 | |||||
908 | if (llvm::ConstantDataSequential *CDS = | ||||
909 | dyn_cast<llvm::ConstantDataSequential>(Init)) { | ||||
910 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { | ||||
911 | llvm::Constant *Elt = CDS->getElementAsConstant(i); | ||||
912 | |||||
913 | // If necessary, get a pointer to the element and emit it. | ||||
914 | if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt)) | ||||
915 | emitStoresForInitAfterBZero( | ||||
916 | CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile, | ||||
917 | Builder); | ||||
918 | } | ||||
919 | return; | ||||
920 | } | ||||
921 | |||||
922 | assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&(((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray >(Init)) && "Unknown value type!") ? static_cast< void> (0) : __assert_fail ("(isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && \"Unknown value type!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 923, __PRETTY_FUNCTION__)) | ||||
923 | "Unknown value type!")(((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray >(Init)) && "Unknown value type!") ? static_cast< void> (0) : __assert_fail ("(isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && \"Unknown value type!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 923, __PRETTY_FUNCTION__)); | ||||
924 | |||||
925 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { | ||||
926 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); | ||||
927 | |||||
928 | // If necessary, get a pointer to the element and emit it. | ||||
929 | if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt)) | ||||
930 | emitStoresForInitAfterBZero(CGM, Elt, | ||||
931 | Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), | ||||
932 | isVolatile, Builder); | ||||
933 | } | ||||
934 | } | ||||
935 | |||||
936 | /// Decide whether we should use bzero plus some stores to initialize a local | ||||
937 | /// variable instead of using a memcpy from a constant global. It is beneficial | ||||
938 | /// to use bzero if the global is all zeros, or mostly zeros and large. | ||||
939 | static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init, | ||||
940 | uint64_t GlobalSize) { | ||||
941 | // If a global is all zeros, always use a bzero. | ||||
942 | if (isa<llvm::ConstantAggregateZero>(Init)) return true; | ||||
943 | |||||
944 | // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large, | ||||
945 | // do it if it will require 6 or fewer scalar stores. | ||||
946 | // TODO: Should budget depends on the size? Avoiding a large global warrants | ||||
947 | // plopping in more stores. | ||||
948 | unsigned StoreBudget = 6; | ||||
949 | uint64_t SizeLimit = 32; | ||||
950 | |||||
951 | return GlobalSize > SizeLimit && | ||||
952 | canEmitInitWithFewStoresAfterBZero(Init, StoreBudget); | ||||
953 | } | ||||
954 | |||||
955 | /// Decide whether we should use memset to initialize a local variable instead | ||||
956 | /// of using a memcpy from a constant global. Assumes we've already decided to | ||||
957 | /// not user bzero. | ||||
958 | /// FIXME We could be more clever, as we are for bzero above, and generate | ||||
959 | /// memset followed by stores. It's unclear that's worth the effort. | ||||
960 | static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init, | ||||
961 | uint64_t GlobalSize, | ||||
962 | const llvm::DataLayout &DL) { | ||||
963 | uint64_t SizeLimit = 32; | ||||
964 | if (GlobalSize <= SizeLimit) | ||||
965 | return nullptr; | ||||
966 | return llvm::isBytewiseValue(Init, DL); | ||||
967 | } | ||||
968 | |||||
969 | /// Decide whether we want to split a constant structure or array store into a | ||||
970 | /// sequence of its fields' stores. This may cost us code size and compilation | ||||
971 | /// speed, but plays better with store optimizations. | ||||
972 | static bool shouldSplitConstantStore(CodeGenModule &CGM, | ||||
973 | uint64_t GlobalByteSize) { | ||||
974 | // Don't break things that occupy more than one cacheline. | ||||
975 | uint64_t ByteSizeLimit = 64; | ||||
976 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) | ||||
977 | return false; | ||||
978 | if (GlobalByteSize <= ByteSizeLimit) | ||||
979 | return true; | ||||
980 | return false; | ||||
981 | } | ||||
982 | |||||
983 | enum class IsPattern { No, Yes }; | ||||
984 | |||||
985 | /// Generate a constant filled with either a pattern or zeroes. | ||||
986 | static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern, | ||||
987 | llvm::Type *Ty) { | ||||
988 | if (isPattern == IsPattern::Yes) | ||||
989 | return initializationPatternFor(CGM, Ty); | ||||
990 | else | ||||
991 | return llvm::Constant::getNullValue(Ty); | ||||
992 | } | ||||
993 | |||||
994 | static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern, | ||||
995 | llvm::Constant *constant); | ||||
996 | |||||
997 | /// Helper function for constWithPadding() to deal with padding in structures. | ||||
998 | static llvm::Constant *constStructWithPadding(CodeGenModule &CGM, | ||||
999 | IsPattern isPattern, | ||||
1000 | llvm::StructType *STy, | ||||
1001 | llvm::Constant *constant) { | ||||
1002 | const llvm::DataLayout &DL = CGM.getDataLayout(); | ||||
1003 | const llvm::StructLayout *Layout = DL.getStructLayout(STy); | ||||
1004 | llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext()); | ||||
1005 | unsigned SizeSoFar = 0; | ||||
1006 | SmallVector<llvm::Constant *, 8> Values; | ||||
1007 | bool NestedIntact = true; | ||||
1008 | for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { | ||||
1009 | unsigned CurOff = Layout->getElementOffset(i); | ||||
1010 | if (SizeSoFar < CurOff) { | ||||
1011 | assert(!STy->isPacked())((!STy->isPacked()) ? static_cast<void> (0) : __assert_fail ("!STy->isPacked()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1011, __PRETTY_FUNCTION__)); | ||||
1012 | auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar); | ||||
1013 | Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy)); | ||||
1014 | } | ||||
1015 | llvm::Constant *CurOp; | ||||
1016 | if (constant->isZeroValue()) | ||||
1017 | CurOp = llvm::Constant::getNullValue(STy->getElementType(i)); | ||||
1018 | else | ||||
1019 | CurOp = cast<llvm::Constant>(constant->getAggregateElement(i)); | ||||
1020 | auto *NewOp = constWithPadding(CGM, isPattern, CurOp); | ||||
1021 | if (CurOp != NewOp) | ||||
1022 | NestedIntact = false; | ||||
1023 | Values.push_back(NewOp); | ||||
1024 | SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType()); | ||||
1025 | } | ||||
1026 | unsigned TotalSize = Layout->getSizeInBytes(); | ||||
1027 | if (SizeSoFar < TotalSize) { | ||||
1028 | auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar); | ||||
1029 | Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy)); | ||||
1030 | } | ||||
1031 | if (NestedIntact && Values.size() == STy->getNumElements()) | ||||
1032 | return constant; | ||||
1033 | return llvm::ConstantStruct::getAnon(Values, STy->isPacked()); | ||||
1034 | } | ||||
1035 | |||||
1036 | /// Replace all padding bytes in a given constant with either a pattern byte or | ||||
1037 | /// 0x00. | ||||
1038 | static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern, | ||||
1039 | llvm::Constant *constant) { | ||||
1040 | llvm::Type *OrigTy = constant->getType(); | ||||
1041 | if (const auto STy = dyn_cast<llvm::StructType>(OrigTy)) | ||||
1042 | return constStructWithPadding(CGM, isPattern, STy, constant); | ||||
1043 | if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) { | ||||
1044 | llvm::SmallVector<llvm::Constant *, 8> Values; | ||||
1045 | unsigned Size = STy->getNumElements(); | ||||
1046 | if (!Size) | ||||
1047 | return constant; | ||||
1048 | llvm::Type *ElemTy = STy->getElementType(); | ||||
1049 | bool ZeroInitializer = constant->isZeroValue(); | ||||
1050 | llvm::Constant *OpValue, *PaddedOp; | ||||
1051 | if (ZeroInitializer) { | ||||
1052 | OpValue = llvm::Constant::getNullValue(ElemTy); | ||||
1053 | PaddedOp = constWithPadding(CGM, isPattern, OpValue); | ||||
1054 | } | ||||
1055 | for (unsigned Op = 0; Op != Size; ++Op) { | ||||
1056 | if (!ZeroInitializer) { | ||||
1057 | OpValue = constant->getAggregateElement(Op); | ||||
1058 | PaddedOp = constWithPadding(CGM, isPattern, OpValue); | ||||
1059 | } | ||||
1060 | Values.push_back(PaddedOp); | ||||
1061 | } | ||||
1062 | auto *NewElemTy = Values[0]->getType(); | ||||
1063 | if (NewElemTy == ElemTy) | ||||
1064 | return constant; | ||||
1065 | if (OrigTy->isArrayTy()) { | ||||
1066 | auto *ArrayTy = llvm::ArrayType::get(NewElemTy, Size); | ||||
1067 | return llvm::ConstantArray::get(ArrayTy, Values); | ||||
1068 | } else { | ||||
1069 | return llvm::ConstantVector::get(Values); | ||||
1070 | } | ||||
1071 | } | ||||
1072 | return constant; | ||||
1073 | } | ||||
1074 | |||||
1075 | Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D, | ||||
1076 | llvm::Constant *Constant, | ||||
1077 | CharUnits Align) { | ||||
1078 | auto FunctionName = [&](const DeclContext *DC) -> std::string { | ||||
1079 | if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { | ||||
1080 | if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD)) | ||||
1081 | return CC->getNameAsString(); | ||||
1082 | if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD)) | ||||
1083 | return CD->getNameAsString(); | ||||
1084 | return getMangledName(FD); | ||||
1085 | } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) { | ||||
1086 | return OM->getNameAsString(); | ||||
1087 | } else if (isa<BlockDecl>(DC)) { | ||||
1088 | return "<block>"; | ||||
1089 | } else if (isa<CapturedDecl>(DC)) { | ||||
1090 | return "<captured>"; | ||||
1091 | } else { | ||||
1092 | llvm_unreachable("expected a function or method")::llvm::llvm_unreachable_internal("expected a function or method" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1092); | ||||
1093 | } | ||||
1094 | }; | ||||
1095 | |||||
1096 | // Form a simple per-variable cache of these values in case we find we | ||||
1097 | // want to reuse them. | ||||
1098 | llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D]; | ||||
1099 | if (!CacheEntry || CacheEntry->getInitializer() != Constant) { | ||||
1100 | auto *Ty = Constant->getType(); | ||||
1101 | bool isConstant = true; | ||||
1102 | llvm::GlobalVariable *InsertBefore = nullptr; | ||||
1103 | unsigned AS = | ||||
1104 | getContext().getTargetAddressSpace(getStringLiteralAddressSpace()); | ||||
1105 | std::string Name; | ||||
1106 | if (D.hasGlobalStorage()) | ||||
1107 | Name = getMangledName(&D).str() + ".const"; | ||||
1108 | else if (const DeclContext *DC = D.getParentFunctionOrMethod()) | ||||
1109 | Name = ("__const." + FunctionName(DC) + "." + D.getName()).str(); | ||||
1110 | else | ||||
1111 | llvm_unreachable("local variable has no parent function or method")::llvm::llvm_unreachable_internal("local variable has no parent function or method" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1111); | ||||
1112 | llvm::GlobalVariable *GV = new llvm::GlobalVariable( | ||||
1113 | getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage, | ||||
1114 | Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS); | ||||
1115 | GV->setAlignment(Align.getQuantity()); | ||||
1116 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); | ||||
1117 | CacheEntry = GV; | ||||
1118 | } else if (CacheEntry->getAlignment() < Align.getQuantity()) { | ||||
1119 | CacheEntry->setAlignment(Align.getQuantity()); | ||||
1120 | } | ||||
1121 | |||||
1122 | return Address(CacheEntry, Align); | ||||
1123 | } | ||||
1124 | |||||
1125 | static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM, | ||||
1126 | const VarDecl &D, | ||||
1127 | CGBuilderTy &Builder, | ||||
1128 | llvm::Constant *Constant, | ||||
1129 | CharUnits Align) { | ||||
1130 | Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align); | ||||
1131 | llvm::Type *BP = llvm::PointerType::getInt8PtrTy(CGM.getLLVMContext(), | ||||
1132 | SrcPtr.getAddressSpace()); | ||||
1133 | if (SrcPtr.getType() != BP) | ||||
1134 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP); | ||||
1135 | return SrcPtr; | ||||
1136 | } | ||||
1137 | |||||
1138 | static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, | ||||
1139 | Address Loc, bool isVolatile, | ||||
1140 | CGBuilderTy &Builder, | ||||
1141 | llvm::Constant *constant) { | ||||
1142 | auto *Ty = constant->getType(); | ||||
1143 | uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty); | ||||
1144 | if (!ConstantSize) | ||||
1145 | return; | ||||
1146 | |||||
1147 | bool canDoSingleStore = Ty->isIntOrIntVectorTy() || | ||||
1148 | Ty->isPtrOrPtrVectorTy() || Ty->isFPOrFPVectorTy(); | ||||
1149 | if (canDoSingleStore) { | ||||
1150 | Builder.CreateStore(constant, Loc, isVolatile); | ||||
1151 | return; | ||||
1152 | } | ||||
1153 | |||||
1154 | auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, ConstantSize); | ||||
1155 | |||||
1156 | // If the initializer is all or mostly the same, codegen with bzero / memset | ||||
1157 | // then do a few stores afterward. | ||||
1158 | if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) { | ||||
1159 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0), SizeVal, | ||||
1160 | isVolatile); | ||||
1161 | |||||
1162 | bool valueAlreadyCorrect = | ||||
1163 | constant->isNullValue() || isa<llvm::UndefValue>(constant); | ||||
1164 | if (!valueAlreadyCorrect) { | ||||
1165 | Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo(Loc.getAddressSpace())); | ||||
1166 | emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder); | ||||
1167 | } | ||||
1168 | return; | ||||
1169 | } | ||||
1170 | |||||
1171 | // If the initializer is a repeated byte pattern, use memset. | ||||
1172 | llvm::Value *Pattern = | ||||
1173 | shouldUseMemSetToInitialize(constant, ConstantSize, CGM.getDataLayout()); | ||||
1174 | if (Pattern) { | ||||
1175 | uint64_t Value = 0x00; | ||||
1176 | if (!isa<llvm::UndefValue>(Pattern)) { | ||||
1177 | const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue(); | ||||
1178 | assert(AP.getBitWidth() <= 8)((AP.getBitWidth() <= 8) ? static_cast<void> (0) : __assert_fail ("AP.getBitWidth() <= 8", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1178, __PRETTY_FUNCTION__)); | ||||
1179 | Value = AP.getLimitedValue(); | ||||
1180 | } | ||||
1181 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal, | ||||
1182 | isVolatile); | ||||
1183 | return; | ||||
1184 | } | ||||
1185 | |||||
1186 | // If the initializer is small, use a handful of stores. | ||||
1187 | if (shouldSplitConstantStore(CGM, ConstantSize)) { | ||||
1188 | if (auto *STy = dyn_cast<llvm::StructType>(Ty)) { | ||||
1189 | // FIXME: handle the case when STy != Loc.getElementType(). | ||||
1190 | if (STy == Loc.getElementType()) { | ||||
1191 | for (unsigned i = 0; i != constant->getNumOperands(); i++) { | ||||
1192 | Address EltPtr = Builder.CreateStructGEP(Loc, i); | ||||
1193 | emitStoresForConstant( | ||||
1194 | CGM, D, EltPtr, isVolatile, Builder, | ||||
1195 | cast<llvm::Constant>(Builder.CreateExtractValue(constant, i))); | ||||
1196 | } | ||||
1197 | return; | ||||
1198 | } | ||||
1199 | } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) { | ||||
1200 | // FIXME: handle the case when ATy != Loc.getElementType(). | ||||
1201 | if (ATy == Loc.getElementType()) { | ||||
1202 | for (unsigned i = 0; i != ATy->getNumElements(); i++) { | ||||
1203 | Address EltPtr = Builder.CreateConstArrayGEP(Loc, i); | ||||
1204 | emitStoresForConstant( | ||||
1205 | CGM, D, EltPtr, isVolatile, Builder, | ||||
1206 | cast<llvm::Constant>(Builder.CreateExtractValue(constant, i))); | ||||
1207 | } | ||||
1208 | return; | ||||
1209 | } | ||||
1210 | } | ||||
1211 | } | ||||
1212 | |||||
1213 | // Copy from a global. | ||||
1214 | Builder.CreateMemCpy(Loc, | ||||
1215 | createUnnamedGlobalForMemcpyFrom( | ||||
1216 | CGM, D, Builder, constant, Loc.getAlignment()), | ||||
1217 | SizeVal, isVolatile); | ||||
1218 | } | ||||
1219 | |||||
1220 | static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D, | ||||
1221 | Address Loc, bool isVolatile, | ||||
1222 | CGBuilderTy &Builder) { | ||||
1223 | llvm::Type *ElTy = Loc.getElementType(); | ||||
1224 | llvm::Constant *constant = | ||||
1225 | constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy)); | ||||
1226 | emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); | ||||
1227 | } | ||||
1228 | |||||
1229 | static void emitStoresForPatternInit(CodeGenModule &CGM, const VarDecl &D, | ||||
1230 | Address Loc, bool isVolatile, | ||||
1231 | CGBuilderTy &Builder) { | ||||
1232 | llvm::Type *ElTy = Loc.getElementType(); | ||||
1233 | llvm::Constant *constant = constWithPadding( | ||||
1234 | CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy)); | ||||
1235 | assert(!isa<llvm::UndefValue>(constant))((!isa<llvm::UndefValue>(constant)) ? static_cast<void > (0) : __assert_fail ("!isa<llvm::UndefValue>(constant)" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1235, __PRETTY_FUNCTION__)); | ||||
1236 | emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); | ||||
1237 | } | ||||
1238 | |||||
1239 | static bool containsUndef(llvm::Constant *constant) { | ||||
1240 | auto *Ty = constant->getType(); | ||||
1241 | if (isa<llvm::UndefValue>(constant)) | ||||
1242 | return true; | ||||
1243 | if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) | ||||
1244 | for (llvm::Use &Op : constant->operands()) | ||||
1245 | if (containsUndef(cast<llvm::Constant>(Op))) | ||||
1246 | return true; | ||||
1247 | return false; | ||||
1248 | } | ||||
1249 | |||||
1250 | static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern, | ||||
1251 | llvm::Constant *constant) { | ||||
1252 | auto *Ty = constant->getType(); | ||||
1253 | if (isa<llvm::UndefValue>(constant)) | ||||
1254 | return patternOrZeroFor(CGM, isPattern, Ty); | ||||
1255 | if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())) | ||||
1256 | return constant; | ||||
1257 | if (!containsUndef(constant)) | ||||
1258 | return constant; | ||||
1259 | llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands()); | ||||
1260 | for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) { | ||||
1261 | auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op)); | ||||
1262 | Values[Op] = replaceUndef(CGM, isPattern, OpValue); | ||||
1263 | } | ||||
1264 | if (Ty->isStructTy()) | ||||
1265 | return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values); | ||||
1266 | if (Ty->isArrayTy()) | ||||
1267 | return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values); | ||||
1268 | assert(Ty->isVectorTy())((Ty->isVectorTy()) ? static_cast<void> (0) : __assert_fail ("Ty->isVectorTy()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1268, __PRETTY_FUNCTION__)); | ||||
1269 | return llvm::ConstantVector::get(Values); | ||||
1270 | } | ||||
1271 | |||||
1272 | /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a | ||||
1273 | /// variable declaration with auto, register, or no storage class specifier. | ||||
1274 | /// These turn into simple stack objects, or GlobalValues depending on target. | ||||
1275 | void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { | ||||
1276 | AutoVarEmission emission = EmitAutoVarAlloca(D); | ||||
1277 | EmitAutoVarInit(emission); | ||||
1278 | EmitAutoVarCleanups(emission); | ||||
1279 | } | ||||
1280 | |||||
1281 | /// Emit a lifetime.begin marker if some criteria are satisfied. | ||||
1282 | /// \return a pointer to the temporary size Value if a marker was emitted, null | ||||
1283 | /// otherwise | ||||
1284 | llvm::Value *CodeGenFunction::EmitLifetimeStart(uint64_t Size, | ||||
1285 | llvm::Value *Addr) { | ||||
1286 | if (!ShouldEmitLifetimeMarkers) | ||||
1287 | return nullptr; | ||||
1288 | |||||
1289 | assert(Addr->getType()->getPointerAddressSpace() ==((Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout ().getAllocaAddrSpace() && "Pointer should be in alloca address space" ) ? static_cast<void> (0) : __assert_fail ("Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && \"Pointer should be in alloca address space\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1291, __PRETTY_FUNCTION__)) | ||||
1290 | CGM.getDataLayout().getAllocaAddrSpace() &&((Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout ().getAllocaAddrSpace() && "Pointer should be in alloca address space" ) ? static_cast<void> (0) : __assert_fail ("Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && \"Pointer should be in alloca address space\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1291, __PRETTY_FUNCTION__)) | ||||
1291 | "Pointer should be in alloca address space")((Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout ().getAllocaAddrSpace() && "Pointer should be in alloca address space" ) ? static_cast<void> (0) : __assert_fail ("Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && \"Pointer should be in alloca address space\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1291, __PRETTY_FUNCTION__)); | ||||
1292 | llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size); | ||||
1293 | Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy); | ||||
1294 | llvm::CallInst *C = | ||||
1295 | Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr}); | ||||
1296 | C->setDoesNotThrow(); | ||||
1297 | return SizeV; | ||||
1298 | } | ||||
1299 | |||||
1300 | void CodeGenFunction::EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr) { | ||||
1301 | assert(Addr->getType()->getPointerAddressSpace() ==((Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout ().getAllocaAddrSpace() && "Pointer should be in alloca address space" ) ? static_cast<void> (0) : __assert_fail ("Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && \"Pointer should be in alloca address space\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1303, __PRETTY_FUNCTION__)) | ||||
1302 | CGM.getDataLayout().getAllocaAddrSpace() &&((Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout ().getAllocaAddrSpace() && "Pointer should be in alloca address space" ) ? static_cast<void> (0) : __assert_fail ("Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && \"Pointer should be in alloca address space\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1303, __PRETTY_FUNCTION__)) | ||||
1303 | "Pointer should be in alloca address space")((Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout ().getAllocaAddrSpace() && "Pointer should be in alloca address space" ) ? static_cast<void> (0) : __assert_fail ("Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && \"Pointer should be in alloca address space\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1303, __PRETTY_FUNCTION__)); | ||||
1304 | Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy); | ||||
1305 | llvm::CallInst *C = | ||||
1306 | Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr}); | ||||
1307 | C->setDoesNotThrow(); | ||||
1308 | } | ||||
1309 | |||||
1310 | void CodeGenFunction::EmitAndRegisterVariableArrayDimensions( | ||||
1311 | CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) { | ||||
1312 | // For each dimension stores its QualType and corresponding | ||||
1313 | // size-expression Value. | ||||
1314 | SmallVector<CodeGenFunction::VlaSizePair, 4> Dimensions; | ||||
1315 | SmallVector<IdentifierInfo *, 4> VLAExprNames; | ||||
1316 | |||||
1317 | // Break down the array into individual dimensions. | ||||
1318 | QualType Type1D = D.getType(); | ||||
1319 | while (getContext().getAsVariableArrayType(Type1D)) { | ||||
1320 | auto VlaSize = getVLAElements1D(Type1D); | ||||
1321 | if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts)) | ||||
1322 | Dimensions.emplace_back(C, Type1D.getUnqualifiedType()); | ||||
1323 | else { | ||||
1324 | // Generate a locally unique name for the size expression. | ||||
1325 | Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++); | ||||
1326 | SmallString<12> Buffer; | ||||
1327 | StringRef NameRef = Name.toStringRef(Buffer); | ||||
1328 | auto &Ident = getContext().Idents.getOwn(NameRef); | ||||
1329 | VLAExprNames.push_back(&Ident); | ||||
1330 | auto SizeExprAddr = | ||||
1331 | CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef); | ||||
1332 | Builder.CreateStore(VlaSize.NumElts, SizeExprAddr); | ||||
1333 | Dimensions.emplace_back(SizeExprAddr.getPointer(), | ||||
1334 | Type1D.getUnqualifiedType()); | ||||
1335 | } | ||||
1336 | Type1D = VlaSize.Type; | ||||
1337 | } | ||||
1338 | |||||
1339 | if (!EmitDebugInfo) | ||||
1340 | return; | ||||
1341 | |||||
1342 | // Register each dimension's size-expression with a DILocalVariable, | ||||
1343 | // so that it can be used by CGDebugInfo when instantiating a DISubrange | ||||
1344 | // to describe this array. | ||||
1345 | unsigned NameIdx = 0; | ||||
1346 | for (auto &VlaSize : Dimensions) { | ||||
1347 | llvm::Metadata *MD; | ||||
1348 | if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts)) | ||||
1349 | MD = llvm::ConstantAsMetadata::get(C); | ||||
1350 | else { | ||||
1351 | // Create an artificial VarDecl to generate debug info for. | ||||
1352 | IdentifierInfo *NameIdent = VLAExprNames[NameIdx++]; | ||||
1353 | auto VlaExprTy = VlaSize.NumElts->getType()->getPointerElementType(); | ||||
1354 | auto QT = getContext().getIntTypeForBitwidth( | ||||
1355 | VlaExprTy->getScalarSizeInBits(), false); | ||||
1356 | auto *ArtificialDecl = VarDecl::Create( | ||||
1357 | getContext(), const_cast<DeclContext *>(D.getDeclContext()), | ||||
1358 | D.getLocation(), D.getLocation(), NameIdent, QT, | ||||
1359 | getContext().CreateTypeSourceInfo(QT), SC_Auto); | ||||
1360 | ArtificialDecl->setImplicit(); | ||||
1361 | |||||
1362 | MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts, | ||||
1363 | Builder); | ||||
1364 | } | ||||
1365 | assert(MD && "No Size expression debug node created")((MD && "No Size expression debug node created") ? static_cast <void> (0) : __assert_fail ("MD && \"No Size expression debug node created\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1365, __PRETTY_FUNCTION__)); | ||||
1366 | DI->registerVLASizeExpression(VlaSize.Type, MD); | ||||
1367 | } | ||||
1368 | } | ||||
1369 | |||||
1370 | /// EmitAutoVarAlloca - Emit the alloca and debug information for a | ||||
1371 | /// local variable. Does not emit initialization or destruction. | ||||
1372 | CodeGenFunction::AutoVarEmission | ||||
1373 | CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { | ||||
1374 | QualType Ty = D.getType(); | ||||
1375 | assert(((Ty.getAddressSpace() == LangAS::Default || (Ty.getAddressSpace () == LangAS::opencl_private && getLangOpts().OpenCL) ) ? static_cast<void> (0) : __assert_fail ("Ty.getAddressSpace() == LangAS::Default || (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL)" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1377, __PRETTY_FUNCTION__)) | ||||
1376 | Ty.getAddressSpace() == LangAS::Default ||((Ty.getAddressSpace() == LangAS::Default || (Ty.getAddressSpace () == LangAS::opencl_private && getLangOpts().OpenCL) ) ? static_cast<void> (0) : __assert_fail ("Ty.getAddressSpace() == LangAS::Default || (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL)" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1377, __PRETTY_FUNCTION__)) | ||||
1377 | (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL))((Ty.getAddressSpace() == LangAS::Default || (Ty.getAddressSpace () == LangAS::opencl_private && getLangOpts().OpenCL) ) ? static_cast<void> (0) : __assert_fail ("Ty.getAddressSpace() == LangAS::Default || (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL)" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1377, __PRETTY_FUNCTION__)); | ||||
1378 | |||||
1379 | AutoVarEmission emission(D); | ||||
1380 | |||||
1381 | bool isEscapingByRef = D.isEscapingByref(); | ||||
1382 | emission.IsEscapingByRef = isEscapingByRef; | ||||
1383 | |||||
1384 | CharUnits alignment = getContext().getDeclAlign(&D); | ||||
1385 | |||||
1386 | // If the type is variably-modified, emit all the VLA sizes for it. | ||||
1387 | if (Ty->isVariablyModifiedType()) | ||||
1388 | EmitVariablyModifiedType(Ty); | ||||
1389 | |||||
1390 | auto *DI = getDebugInfo(); | ||||
1391 | bool EmitDebugInfo = DI && CGM.getCodeGenOpts().getDebugInfo() >= | ||||
1392 | codegenoptions::LimitedDebugInfo; | ||||
1393 | |||||
1394 | Address address = Address::invalid(); | ||||
1395 | Address AllocaAddr = Address::invalid(); | ||||
1396 | Address OpenMPLocalAddr = | ||||
1397 | getLangOpts().OpenMP | ||||
1398 | ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D) | ||||
1399 | : Address::invalid(); | ||||
1400 | bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable(); | ||||
1401 | |||||
1402 | if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) { | ||||
1403 | address = OpenMPLocalAddr; | ||||
1404 | } else if (Ty->isConstantSizeType()) { | ||||
1405 | // If this value is an array or struct with a statically determinable | ||||
1406 | // constant initializer, there are optimizations we can do. | ||||
1407 | // | ||||
1408 | // TODO: We should constant-evaluate the initializer of any variable, | ||||
1409 | // as long as it is initialized by a constant expression. Currently, | ||||
1410 | // isConstantInitializer produces wrong answers for structs with | ||||
1411 | // reference or bitfield members, and a few other cases, and checking | ||||
1412 | // for POD-ness protects us from some of these. | ||||
1413 | if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) && | ||||
1414 | (D.isConstexpr() || | ||||
1415 | ((Ty.isPODType(getContext()) || | ||||
1416 | getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) && | ||||
1417 | D.getInit()->isConstantInitializer(getContext(), false)))) { | ||||
1418 | |||||
1419 | // If the variable's a const type, and it's neither an NRVO | ||||
1420 | // candidate nor a __block variable and has no mutable members, | ||||
1421 | // emit it as a global instead. | ||||
1422 | // Exception is if a variable is located in non-constant address space | ||||
1423 | // in OpenCL. | ||||
1424 | if ((!getLangOpts().OpenCL || | ||||
1425 | Ty.getAddressSpace() == LangAS::opencl_constant) && | ||||
1426 | (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && | ||||
1427 | !isEscapingByRef && CGM.isTypeConstant(Ty, true))) { | ||||
1428 | EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); | ||||
1429 | |||||
1430 | // Signal this condition to later callbacks. | ||||
1431 | emission.Addr = Address::invalid(); | ||||
1432 | assert(emission.wasEmittedAsGlobal())((emission.wasEmittedAsGlobal()) ? static_cast<void> (0 ) : __assert_fail ("emission.wasEmittedAsGlobal()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1432, __PRETTY_FUNCTION__)); | ||||
1433 | return emission; | ||||
1434 | } | ||||
1435 | |||||
1436 | // Otherwise, tell the initialization code that we're in this case. | ||||
1437 | emission.IsConstantAggregate = true; | ||||
1438 | } | ||||
1439 | |||||
1440 | // A normal fixed sized variable becomes an alloca in the entry block, | ||||
1441 | // unless: | ||||
1442 | // - it's an NRVO variable. | ||||
1443 | // - we are compiling OpenMP and it's an OpenMP local variable. | ||||
1444 | if (NRVO) { | ||||
1445 | // The named return value optimization: allocate this variable in the | ||||
1446 | // return slot, so that we can elide the copy when returning this | ||||
1447 | // variable (C++0x [class.copy]p34). | ||||
1448 | address = ReturnValue; | ||||
1449 | |||||
1450 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { | ||||
1451 | const auto *RD = RecordTy->getDecl(); | ||||
1452 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); | ||||
1453 | if ((CXXRD && !CXXRD->hasTrivialDestructor()) || | ||||
1454 | RD->isNonTrivialToPrimitiveDestroy()) { | ||||
1455 | // Create a flag that is used to indicate when the NRVO was applied | ||||
1456 | // to this variable. Set it to zero to indicate that NRVO was not | ||||
1457 | // applied. | ||||
1458 | llvm::Value *Zero = Builder.getFalse(); | ||||
1459 | Address NRVOFlag = | ||||
1460 | CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo"); | ||||
1461 | EnsureInsertPoint(); | ||||
1462 | Builder.CreateStore(Zero, NRVOFlag); | ||||
1463 | |||||
1464 | // Record the NRVO flag for this variable. | ||||
1465 | NRVOFlags[&D] = NRVOFlag.getPointer(); | ||||
1466 | emission.NRVOFlag = NRVOFlag.getPointer(); | ||||
1467 | } | ||||
1468 | } | ||||
1469 | } else { | ||||
1470 | CharUnits allocaAlignment; | ||||
1471 | llvm::Type *allocaTy; | ||||
1472 | if (isEscapingByRef) { | ||||
1473 | auto &byrefInfo = getBlockByrefInfo(&D); | ||||
1474 | allocaTy = byrefInfo.Type; | ||||
1475 | allocaAlignment = byrefInfo.ByrefAlignment; | ||||
1476 | } else { | ||||
1477 | allocaTy = ConvertTypeForMem(Ty); | ||||
1478 | allocaAlignment = alignment; | ||||
1479 | } | ||||
1480 | |||||
1481 | // Create the alloca. Note that we set the name separately from | ||||
1482 | // building the instruction so that it's there even in no-asserts | ||||
1483 | // builds. | ||||
1484 | address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(), | ||||
1485 | /*ArraySize=*/nullptr, &AllocaAddr); | ||||
1486 | |||||
1487 | // Don't emit lifetime markers for MSVC catch parameters. The lifetime of | ||||
1488 | // the catch parameter starts in the catchpad instruction, and we can't | ||||
1489 | // insert code in those basic blocks. | ||||
1490 | bool IsMSCatchParam = | ||||
1491 | D.isExceptionVariable() && getTarget().getCXXABI().isMicrosoft(); | ||||
1492 | |||||
1493 | // Emit a lifetime intrinsic if meaningful. There's no point in doing this | ||||
1494 | // if we don't have a valid insertion point (?). | ||||
1495 | if (HaveInsertPoint() && !IsMSCatchParam) { | ||||
1496 | // If there's a jump into the lifetime of this variable, its lifetime | ||||
1497 | // gets broken up into several regions in IR, which requires more work | ||||
1498 | // to handle correctly. For now, just omit the intrinsics; this is a | ||||
1499 | // rare case, and it's better to just be conservatively correct. | ||||
1500 | // PR28267. | ||||
1501 | // | ||||
1502 | // We have to do this in all language modes if there's a jump past the | ||||
1503 | // declaration. We also have to do it in C if there's a jump to an | ||||
1504 | // earlier point in the current block because non-VLA lifetimes begin as | ||||
1505 | // soon as the containing block is entered, not when its variables | ||||
1506 | // actually come into scope; suppressing the lifetime annotations | ||||
1507 | // completely in this case is unnecessarily pessimistic, but again, this | ||||
1508 | // is rare. | ||||
1509 | if (!Bypasses.IsBypassed(&D) && | ||||
1510 | !(!getLangOpts().CPlusPlus && hasLabelBeenSeenInCurrentScope())) { | ||||
1511 | uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy); | ||||
1512 | emission.SizeForLifetimeMarkers = | ||||
1513 | EmitLifetimeStart(size, AllocaAddr.getPointer()); | ||||
1514 | } | ||||
1515 | } else { | ||||
1516 | assert(!emission.useLifetimeMarkers())((!emission.useLifetimeMarkers()) ? static_cast<void> ( 0) : __assert_fail ("!emission.useLifetimeMarkers()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1516, __PRETTY_FUNCTION__)); | ||||
1517 | } | ||||
1518 | } | ||||
1519 | } else { | ||||
1520 | EnsureInsertPoint(); | ||||
1521 | |||||
1522 | if (!DidCallStackSave) { | ||||
1523 | // Save the stack. | ||||
1524 | Address Stack = | ||||
1525 | CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack"); | ||||
1526 | |||||
1527 | llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); | ||||
1528 | llvm::Value *V = Builder.CreateCall(F); | ||||
1529 | Builder.CreateStore(V, Stack); | ||||
1530 | |||||
1531 | DidCallStackSave = true; | ||||
1532 | |||||
1533 | // Push a cleanup block and restore the stack there. | ||||
1534 | // FIXME: in general circumstances, this should be an EH cleanup. | ||||
1535 | pushStackRestore(NormalCleanup, Stack); | ||||
1536 | } | ||||
1537 | |||||
1538 | auto VlaSize = getVLASize(Ty); | ||||
1539 | llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type); | ||||
1540 | |||||
1541 | // Allocate memory for the array. | ||||
1542 | address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts, | ||||
1543 | &AllocaAddr); | ||||
1544 | |||||
1545 | // If we have debug info enabled, properly describe the VLA dimensions for | ||||
1546 | // this type by registering the vla size expression for each of the | ||||
1547 | // dimensions. | ||||
1548 | EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo); | ||||
1549 | } | ||||
1550 | |||||
1551 | setAddrOfLocalVar(&D, address); | ||||
1552 | emission.Addr = address; | ||||
1553 | emission.AllocaAddr = AllocaAddr; | ||||
1554 | |||||
1555 | // Emit debug info for local var declaration. | ||||
1556 | if (EmitDebugInfo && HaveInsertPoint()) { | ||||
1557 | Address DebugAddr = address; | ||||
1558 | bool UsePointerValue = NRVO && ReturnValuePointer.isValid(); | ||||
1559 | DI->setLocation(D.getLocation()); | ||||
1560 | |||||
1561 | // If NRVO, use a pointer to the return address. | ||||
1562 | if (UsePointerValue) | ||||
1563 | DebugAddr = ReturnValuePointer; | ||||
1564 | |||||
1565 | (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder, | ||||
1566 | UsePointerValue); | ||||
1567 | } | ||||
1568 | |||||
1569 | if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint()) | ||||
1570 | EmitVarAnnotations(&D, address.getPointer()); | ||||
1571 | |||||
1572 | // Make sure we call @llvm.lifetime.end. | ||||
1573 | if (emission.useLifetimeMarkers()) | ||||
1574 | EHStack.pushCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, | ||||
1575 | emission.getOriginalAllocatedAddress(), | ||||
1576 | emission.getSizeForLifetimeMarkers()); | ||||
1577 | |||||
1578 | return emission; | ||||
1579 | } | ||||
1580 | |||||
1581 | static bool isCapturedBy(const VarDecl &, const Expr *); | ||||
1582 | |||||
1583 | /// Determines whether the given __block variable is potentially | ||||
1584 | /// captured by the given statement. | ||||
1585 | static bool isCapturedBy(const VarDecl &Var, const Stmt *S) { | ||||
1586 | if (const Expr *E = dyn_cast<Expr>(S)) | ||||
1587 | return isCapturedBy(Var, E); | ||||
1588 | for (const Stmt *SubStmt : S->children()) | ||||
1589 | if (isCapturedBy(Var, SubStmt)) | ||||
1590 | return true; | ||||
1591 | return false; | ||||
1592 | } | ||||
1593 | |||||
1594 | /// Determines whether the given __block variable is potentially | ||||
1595 | /// captured by the given expression. | ||||
1596 | static bool isCapturedBy(const VarDecl &Var, const Expr *E) { | ||||
1597 | // Skip the most common kinds of expressions that make | ||||
1598 | // hierarchy-walking expensive. | ||||
1599 | E = E->IgnoreParenCasts(); | ||||
1600 | |||||
1601 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) { | ||||
1602 | const BlockDecl *Block = BE->getBlockDecl(); | ||||
1603 | for (const auto &I : Block->captures()) { | ||||
1604 | if (I.getVariable() == &Var) | ||||
1605 | return true; | ||||
1606 | } | ||||
1607 | |||||
1608 | // No need to walk into the subexpressions. | ||||
1609 | return false; | ||||
1610 | } | ||||
1611 | |||||
1612 | if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) { | ||||
1613 | const CompoundStmt *CS = SE->getSubStmt(); | ||||
1614 | for (const auto *BI : CS->body()) | ||||
1615 | if (const auto *BIE = dyn_cast<Expr>(BI)) { | ||||
1616 | if (isCapturedBy(Var, BIE)) | ||||
1617 | return true; | ||||
1618 | } | ||||
1619 | else if (const auto *DS = dyn_cast<DeclStmt>(BI)) { | ||||
1620 | // special case declarations | ||||
1621 | for (const auto *I : DS->decls()) { | ||||
1622 | if (const auto *VD = dyn_cast<VarDecl>((I))) { | ||||
1623 | const Expr *Init = VD->getInit(); | ||||
1624 | if (Init && isCapturedBy(Var, Init)) | ||||
1625 | return true; | ||||
1626 | } | ||||
1627 | } | ||||
1628 | } | ||||
1629 | else | ||||
1630 | // FIXME. Make safe assumption assuming arbitrary statements cause capturing. | ||||
1631 | // Later, provide code to poke into statements for capture analysis. | ||||
1632 | return true; | ||||
1633 | return false; | ||||
1634 | } | ||||
1635 | |||||
1636 | for (const Stmt *SubStmt : E->children()) | ||||
1637 | if (isCapturedBy(Var, SubStmt)) | ||||
1638 | return true; | ||||
1639 | |||||
1640 | return false; | ||||
1641 | } | ||||
1642 | |||||
1643 | /// Determine whether the given initializer is trivial in the sense | ||||
1644 | /// that it requires no code to be generated. | ||||
1645 | bool CodeGenFunction::isTrivialInitializer(const Expr *Init) { | ||||
1646 | if (!Init) | ||||
1647 | return true; | ||||
1648 | |||||
1649 | if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) | ||||
1650 | if (CXXConstructorDecl *Constructor = Construct->getConstructor()) | ||||
1651 | if (Constructor->isTrivial() && | ||||
1652 | Constructor->isDefaultConstructor() && | ||||
1653 | !Construct->requiresZeroInitialization()) | ||||
1654 | return true; | ||||
1655 | |||||
1656 | return false; | ||||
1657 | } | ||||
1658 | |||||
1659 | void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type, | ||||
1660 | const VarDecl &D, | ||||
1661 | Address Loc) { | ||||
1662 | auto trivialAutoVarInit = getContext().getLangOpts().getTrivialAutoVarInit(); | ||||
1663 | CharUnits Size = getContext().getTypeSizeInChars(type); | ||||
1664 | bool isVolatile = type.isVolatileQualified(); | ||||
1665 | if (!Size.isZero()) { | ||||
1666 | switch (trivialAutoVarInit) { | ||||
1667 | case LangOptions::TrivialAutoVarInitKind::Uninitialized: | ||||
1668 | llvm_unreachable("Uninitialized handled by caller")::llvm::llvm_unreachable_internal("Uninitialized handled by caller" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1668); | ||||
1669 | case LangOptions::TrivialAutoVarInitKind::Zero: | ||||
1670 | emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder); | ||||
1671 | break; | ||||
1672 | case LangOptions::TrivialAutoVarInitKind::Pattern: | ||||
1673 | emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder); | ||||
1674 | break; | ||||
1675 | } | ||||
1676 | return; | ||||
1677 | } | ||||
1678 | |||||
1679 | // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to | ||||
1680 | // them, so emit a memcpy with the VLA size to initialize each element. | ||||
1681 | // Technically zero-sized or negative-sized VLAs are undefined, and UBSan | ||||
1682 | // will catch that code, but there exists code which generates zero-sized | ||||
1683 | // VLAs. Be nice and initialize whatever they requested. | ||||
1684 | const auto *VlaType = getContext().getAsVariableArrayType(type); | ||||
1685 | if (!VlaType) | ||||
1686 | return; | ||||
1687 | auto VlaSize = getVLASize(VlaType); | ||||
1688 | auto SizeVal = VlaSize.NumElts; | ||||
1689 | CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type); | ||||
1690 | switch (trivialAutoVarInit) { | ||||
1691 | case LangOptions::TrivialAutoVarInitKind::Uninitialized: | ||||
1692 | llvm_unreachable("Uninitialized handled by caller")::llvm::llvm_unreachable_internal("Uninitialized handled by caller" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1692); | ||||
1693 | |||||
1694 | case LangOptions::TrivialAutoVarInitKind::Zero: | ||||
1695 | if (!EltSize.isOne()) | ||||
1696 | SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize)); | ||||
1697 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal, | ||||
1698 | isVolatile); | ||||
1699 | break; | ||||
1700 | |||||
1701 | case LangOptions::TrivialAutoVarInitKind::Pattern: { | ||||
1702 | llvm::Type *ElTy = Loc.getElementType(); | ||||
1703 | llvm::Constant *Constant = constWithPadding( | ||||
1704 | CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy)); | ||||
1705 | CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type); | ||||
1706 | llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop"); | ||||
1707 | llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop"); | ||||
1708 | llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont"); | ||||
1709 | llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ( | ||||
1710 | SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0), | ||||
1711 | "vla.iszerosized"); | ||||
1712 | Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB); | ||||
1713 | EmitBlock(SetupBB); | ||||
1714 | if (!EltSize.isOne()) | ||||
1715 | SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize)); | ||||
1716 | llvm::Value *BaseSizeInChars = | ||||
1717 | llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity()); | ||||
1718 | Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin"); | ||||
1719 | llvm::Value *End = | ||||
1720 | Builder.CreateInBoundsGEP(Begin.getPointer(), SizeVal, "vla.end"); | ||||
1721 | llvm::BasicBlock *OriginBB = Builder.GetInsertBlock(); | ||||
1722 | EmitBlock(LoopBB); | ||||
1723 | llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur"); | ||||
1724 | Cur->addIncoming(Begin.getPointer(), OriginBB); | ||||
1725 | CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize); | ||||
1726 | Builder.CreateMemCpy(Address(Cur, CurAlign), | ||||
1727 | createUnnamedGlobalForMemcpyFrom( | ||||
1728 | CGM, D, Builder, Constant, ConstantAlign), | ||||
1729 | BaseSizeInChars, isVolatile); | ||||
1730 | llvm::Value *Next = | ||||
1731 | Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next"); | ||||
1732 | llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone"); | ||||
1733 | Builder.CreateCondBr(Done, ContBB, LoopBB); | ||||
1734 | Cur->addIncoming(Next, LoopBB); | ||||
1735 | EmitBlock(ContBB); | ||||
1736 | } break; | ||||
1737 | } | ||||
1738 | } | ||||
1739 | |||||
1740 | void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { | ||||
1741 | assert(emission.Variable && "emission was not valid!")((emission.Variable && "emission was not valid!") ? static_cast <void> (0) : __assert_fail ("emission.Variable && \"emission was not valid!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1741, __PRETTY_FUNCTION__)); | ||||
1742 | |||||
1743 | // If this was emitted as a global constant, we're done. | ||||
1744 | if (emission.wasEmittedAsGlobal()) return; | ||||
1745 | |||||
1746 | const VarDecl &D = *emission.Variable; | ||||
1747 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, D.getLocation()); | ||||
1748 | QualType type = D.getType(); | ||||
1749 | |||||
1750 | // If this local has an initializer, emit it now. | ||||
1751 | const Expr *Init = D.getInit(); | ||||
1752 | |||||
1753 | // If we are at an unreachable point, we don't need to emit the initializer | ||||
1754 | // unless it contains a label. | ||||
1755 | if (!HaveInsertPoint()) { | ||||
1756 | if (!Init || !ContainsLabel(Init)) return; | ||||
1757 | EnsureInsertPoint(); | ||||
1758 | } | ||||
1759 | |||||
1760 | // Initialize the structure of a __block variable. | ||||
1761 | if (emission.IsEscapingByRef) | ||||
1762 | emitByrefStructureInit(emission); | ||||
1763 | |||||
1764 | // Initialize the variable here if it doesn't have a initializer and it is a | ||||
1765 | // C struct that is non-trivial to initialize or an array containing such a | ||||
1766 | // struct. | ||||
1767 | if (!Init && | ||||
1768 | type.isNonTrivialToPrimitiveDefaultInitialize() == | ||||
1769 | QualType::PDIK_Struct) { | ||||
1770 | LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type); | ||||
1771 | if (emission.IsEscapingByRef) | ||||
1772 | drillIntoBlockVariable(*this, Dst, &D); | ||||
1773 | defaultInitNonTrivialCStructVar(Dst); | ||||
1774 | return; | ||||
1775 | } | ||||
1776 | |||||
1777 | // Check whether this is a byref variable that's potentially | ||||
1778 | // captured and moved by its own initializer. If so, we'll need to | ||||
1779 | // emit the initializer first, then copy into the variable. | ||||
1780 | bool capturedByInit = | ||||
1781 | Init && emission.IsEscapingByRef && isCapturedBy(D, Init); | ||||
1782 | |||||
1783 | bool locIsByrefHeader = !capturedByInit; | ||||
1784 | const Address Loc = | ||||
1785 | locIsByrefHeader ? emission.getObjectAddress(*this) : emission.Addr; | ||||
1786 | |||||
1787 | // Note: constexpr already initializes everything correctly. | ||||
1788 | LangOptions::TrivialAutoVarInitKind trivialAutoVarInit = | ||||
1789 | (D.isConstexpr() | ||||
1790 | ? LangOptions::TrivialAutoVarInitKind::Uninitialized | ||||
1791 | : (D.getAttr<UninitializedAttr>() | ||||
1792 | ? LangOptions::TrivialAutoVarInitKind::Uninitialized | ||||
1793 | : getContext().getLangOpts().getTrivialAutoVarInit())); | ||||
1794 | |||||
1795 | auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) { | ||||
1796 | if (trivialAutoVarInit == | ||||
1797 | LangOptions::TrivialAutoVarInitKind::Uninitialized) | ||||
1798 | return; | ||||
1799 | |||||
1800 | // Only initialize a __block's storage: we always initialize the header. | ||||
1801 | if (emission.IsEscapingByRef && !locIsByrefHeader) | ||||
1802 | Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false); | ||||
1803 | |||||
1804 | return emitZeroOrPatternForAutoVarInit(type, D, Loc); | ||||
1805 | }; | ||||
1806 | |||||
1807 | if (isTrivialInitializer(Init)) | ||||
1808 | return initializeWhatIsTechnicallyUninitialized(Loc); | ||||
1809 | |||||
1810 | llvm::Constant *constant = nullptr; | ||||
1811 | if (emission.IsConstantAggregate || | ||||
1812 | D.mightBeUsableInConstantExpressions(getContext())) { | ||||
1813 | assert(!capturedByInit && "constant init contains a capturing block?")((!capturedByInit && "constant init contains a capturing block?" ) ? static_cast<void> (0) : __assert_fail ("!capturedByInit && \"constant init contains a capturing block?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1813, __PRETTY_FUNCTION__)); | ||||
1814 | constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D); | ||||
1815 | if (constant && !constant->isZeroValue() && | ||||
1816 | (trivialAutoVarInit != | ||||
1817 | LangOptions::TrivialAutoVarInitKind::Uninitialized)) { | ||||
1818 | IsPattern isPattern = | ||||
1819 | (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern) | ||||
1820 | ? IsPattern::Yes | ||||
1821 | : IsPattern::No; | ||||
1822 | // C guarantees that brace-init with fewer initializers than members in | ||||
1823 | // the aggregate will initialize the rest of the aggregate as-if it were | ||||
1824 | // static initialization. In turn static initialization guarantees that | ||||
1825 | // padding is initialized to zero bits. We could instead pattern-init if D | ||||
1826 | // has any ImplicitValueInitExpr, but that seems to be unintuitive | ||||
1827 | // behavior. | ||||
1828 | constant = constWithPadding(CGM, IsPattern::No, | ||||
1829 | replaceUndef(CGM, isPattern, constant)); | ||||
1830 | } | ||||
1831 | } | ||||
1832 | |||||
1833 | if (!constant) { | ||||
1834 | initializeWhatIsTechnicallyUninitialized(Loc); | ||||
1835 | LValue lv = MakeAddrLValue(Loc, type); | ||||
1836 | lv.setNonGC(true); | ||||
1837 | return EmitExprAsInit(Init, &D, lv, capturedByInit); | ||||
1838 | } | ||||
1839 | |||||
1840 | if (!emission.IsConstantAggregate) { | ||||
1841 | // For simple scalar/complex initialization, store the value directly. | ||||
1842 | LValue lv = MakeAddrLValue(Loc, type); | ||||
1843 | lv.setNonGC(true); | ||||
1844 | return EmitStoreThroughLValue(RValue::get(constant), lv, true); | ||||
1845 | } | ||||
1846 | |||||
1847 | llvm::Type *BP = CGM.Int8Ty->getPointerTo(Loc.getAddressSpace()); | ||||
1848 | emitStoresForConstant( | ||||
1849 | CGM, D, (Loc.getType() == BP) ? Loc : Builder.CreateBitCast(Loc, BP), | ||||
1850 | type.isVolatileQualified(), Builder, constant); | ||||
1851 | } | ||||
1852 | |||||
1853 | /// Emit an expression as an initializer for an object (variable, field, etc.) | ||||
1854 | /// at the given location. The expression is not necessarily the normal | ||||
1855 | /// initializer for the object, and the address is not necessarily | ||||
1856 | /// its normal location. | ||||
1857 | /// | ||||
1858 | /// \param init the initializing expression | ||||
1859 | /// \param D the object to act as if we're initializing | ||||
1860 | /// \param loc the address to initialize; its type is a pointer | ||||
1861 | /// to the LLVM mapping of the object's type | ||||
1862 | /// \param alignment the alignment of the address | ||||
1863 | /// \param capturedByInit true if \p D is a __block variable | ||||
1864 | /// whose address is potentially changed by the initializer | ||||
1865 | void CodeGenFunction::EmitExprAsInit(const Expr *init, const ValueDecl *D, | ||||
1866 | LValue lvalue, bool capturedByInit) { | ||||
1867 | QualType type = D->getType(); | ||||
1868 | |||||
1869 | if (type->isReferenceType()) { | ||||
1870 | RValue rvalue = EmitReferenceBindingToExpr(init); | ||||
1871 | if (capturedByInit) | ||||
1872 | drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); | ||||
1873 | EmitStoreThroughLValue(rvalue, lvalue, true); | ||||
1874 | return; | ||||
1875 | } | ||||
1876 | switch (getEvaluationKind(type)) { | ||||
1877 | case TEK_Scalar: | ||||
1878 | EmitScalarInit(init, D, lvalue, capturedByInit); | ||||
1879 | return; | ||||
1880 | case TEK_Complex: { | ||||
1881 | ComplexPairTy complex = EmitComplexExpr(init); | ||||
1882 | if (capturedByInit) | ||||
1883 | drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); | ||||
1884 | EmitStoreOfComplex(complex, lvalue, /*init*/ true); | ||||
1885 | return; | ||||
1886 | } | ||||
1887 | case TEK_Aggregate: | ||||
1888 | if (type->isAtomicType()) { | ||||
1889 | EmitAtomicInit(const_cast<Expr*>(init), lvalue); | ||||
1890 | } else { | ||||
1891 | AggValueSlot::Overlap_t Overlap = AggValueSlot::MayOverlap; | ||||
1892 | if (isa<VarDecl>(D)) | ||||
1893 | Overlap = AggValueSlot::DoesNotOverlap; | ||||
1894 | else if (auto *FD = dyn_cast<FieldDecl>(D)) | ||||
1895 | Overlap = getOverlapForFieldInit(FD); | ||||
1896 | // TODO: how can we delay here if D is captured by its initializer? | ||||
1897 | EmitAggExpr(init, AggValueSlot::forLValue(lvalue, | ||||
1898 | AggValueSlot::IsDestructed, | ||||
1899 | AggValueSlot::DoesNotNeedGCBarriers, | ||||
1900 | AggValueSlot::IsNotAliased, | ||||
1901 | Overlap)); | ||||
1902 | } | ||||
1903 | return; | ||||
1904 | } | ||||
1905 | llvm_unreachable("bad evaluation kind")::llvm::llvm_unreachable_internal("bad evaluation kind", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1905); | ||||
1906 | } | ||||
1907 | |||||
1908 | /// Enter a destroy cleanup for the given local variable. | ||||
1909 | void CodeGenFunction::emitAutoVarTypeCleanup( | ||||
1910 | const CodeGenFunction::AutoVarEmission &emission, | ||||
1911 | QualType::DestructionKind dtorKind) { | ||||
1912 | assert(dtorKind != QualType::DK_none)((dtorKind != QualType::DK_none) ? static_cast<void> (0 ) : __assert_fail ("dtorKind != QualType::DK_none", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1912, __PRETTY_FUNCTION__)); | ||||
1913 | |||||
1914 | // Note that for __block variables, we want to destroy the | ||||
1915 | // original stack object, not the possibly forwarded object. | ||||
1916 | Address addr = emission.getObjectAddress(*this); | ||||
1917 | |||||
1918 | const VarDecl *var = emission.Variable; | ||||
1919 | QualType type = var->getType(); | ||||
1920 | |||||
1921 | CleanupKind cleanupKind = NormalAndEHCleanup; | ||||
1922 | CodeGenFunction::Destroyer *destroyer = nullptr; | ||||
1923 | |||||
1924 | switch (dtorKind) { | ||||
1925 | case QualType::DK_none: | ||||
1926 | llvm_unreachable("no cleanup for trivially-destructible variable")::llvm::llvm_unreachable_internal("no cleanup for trivially-destructible variable" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1926); | ||||
1927 | |||||
1928 | case QualType::DK_cxx_destructor: | ||||
1929 | // If there's an NRVO flag on the emission, we need a different | ||||
1930 | // cleanup. | ||||
1931 | if (emission.NRVOFlag) { | ||||
1932 | assert(!type->isArrayType())((!type->isArrayType()) ? static_cast<void> (0) : __assert_fail ("!type->isArrayType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1932, __PRETTY_FUNCTION__)); | ||||
1933 | CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor(); | ||||
1934 | EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor, | ||||
1935 | emission.NRVOFlag); | ||||
1936 | return; | ||||
1937 | } | ||||
1938 | break; | ||||
1939 | |||||
1940 | case QualType::DK_objc_strong_lifetime: | ||||
1941 | // Suppress cleanups for pseudo-strong variables. | ||||
1942 | if (var->isARCPseudoStrong()) return; | ||||
1943 | |||||
1944 | // Otherwise, consider whether to use an EH cleanup or not. | ||||
1945 | cleanupKind = getARCCleanupKind(); | ||||
1946 | |||||
1947 | // Use the imprecise destroyer by default. | ||||
1948 | if (!var->hasAttr<ObjCPreciseLifetimeAttr>()) | ||||
1949 | destroyer = CodeGenFunction::destroyARCStrongImprecise; | ||||
1950 | break; | ||||
1951 | |||||
1952 | case QualType::DK_objc_weak_lifetime: | ||||
1953 | break; | ||||
1954 | |||||
1955 | case QualType::DK_nontrivial_c_struct: | ||||
1956 | destroyer = CodeGenFunction::destroyNonTrivialCStruct; | ||||
1957 | if (emission.NRVOFlag) { | ||||
1958 | assert(!type->isArrayType())((!type->isArrayType()) ? static_cast<void> (0) : __assert_fail ("!type->isArrayType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1958, __PRETTY_FUNCTION__)); | ||||
1959 | EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr, | ||||
1960 | emission.NRVOFlag, type); | ||||
1961 | return; | ||||
1962 | } | ||||
1963 | break; | ||||
1964 | } | ||||
1965 | |||||
1966 | // If we haven't chosen a more specific destroyer, use the default. | ||||
1967 | if (!destroyer) destroyer = getDestroyer(dtorKind); | ||||
1968 | |||||
1969 | // Use an EH cleanup in array destructors iff the destructor itself | ||||
1970 | // is being pushed as an EH cleanup. | ||||
1971 | bool useEHCleanup = (cleanupKind & EHCleanup); | ||||
1972 | EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer, | ||||
1973 | useEHCleanup); | ||||
1974 | } | ||||
1975 | |||||
1976 | void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) { | ||||
1977 | assert(emission.Variable && "emission was not valid!")((emission.Variable && "emission was not valid!") ? static_cast <void> (0) : __assert_fail ("emission.Variable && \"emission was not valid!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 1977, __PRETTY_FUNCTION__)); | ||||
1978 | |||||
1979 | // If this was emitted as a global constant, we're done. | ||||
1980 | if (emission.wasEmittedAsGlobal()) return; | ||||
1981 | |||||
1982 | // If we don't have an insertion point, we're done. Sema prevents | ||||
1983 | // us from jumping into any of these scopes anyway. | ||||
1984 | if (!HaveInsertPoint()) return; | ||||
1985 | |||||
1986 | const VarDecl &D = *emission.Variable; | ||||
1987 | |||||
1988 | // Check the type for a cleanup. | ||||
1989 | if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext())) | ||||
1990 | emitAutoVarTypeCleanup(emission, dtorKind); | ||||
1991 | |||||
1992 | // In GC mode, honor objc_precise_lifetime. | ||||
1993 | if (getLangOpts().getGC() != LangOptions::NonGC && | ||||
1994 | D.hasAttr<ObjCPreciseLifetimeAttr>()) { | ||||
1995 | EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D); | ||||
1996 | } | ||||
1997 | |||||
1998 | // Handle the cleanup attribute. | ||||
1999 | if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { | ||||
2000 | const FunctionDecl *FD = CA->getFunctionDecl(); | ||||
2001 | |||||
2002 | llvm::Constant *F = CGM.GetAddrOfFunction(FD); | ||||
2003 | assert(F && "Could not find function!")((F && "Could not find function!") ? static_cast<void > (0) : __assert_fail ("F && \"Could not find function!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2003, __PRETTY_FUNCTION__)); | ||||
2004 | |||||
2005 | const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD); | ||||
2006 | EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D); | ||||
2007 | } | ||||
2008 | |||||
2009 | // If this is a block variable, call _Block_object_destroy | ||||
2010 | // (on the unforwarded address). Don't enter this cleanup if we're in pure-GC | ||||
2011 | // mode. | ||||
2012 | if (emission.IsEscapingByRef && | ||||
2013 | CGM.getLangOpts().getGC() != LangOptions::GCOnly) { | ||||
2014 | BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF; | ||||
2015 | if (emission.Variable->getType().isObjCGCWeak()) | ||||
2016 | Flags |= BLOCK_FIELD_IS_WEAK; | ||||
2017 | enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags, | ||||
2018 | /*LoadBlockVarAddr*/ false, | ||||
2019 | cxxDestructorCanThrow(emission.Variable->getType())); | ||||
2020 | } | ||||
2021 | } | ||||
2022 | |||||
2023 | CodeGenFunction::Destroyer * | ||||
2024 | CodeGenFunction::getDestroyer(QualType::DestructionKind kind) { | ||||
2025 | switch (kind) { | ||||
2026 | case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor")::llvm::llvm_unreachable_internal("no destroyer for trivial dtor" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2026); | ||||
2027 | case QualType::DK_cxx_destructor: | ||||
2028 | return destroyCXXObject; | ||||
2029 | case QualType::DK_objc_strong_lifetime: | ||||
2030 | return destroyARCStrongPrecise; | ||||
2031 | case QualType::DK_objc_weak_lifetime: | ||||
2032 | return destroyARCWeak; | ||||
2033 | case QualType::DK_nontrivial_c_struct: | ||||
2034 | return destroyNonTrivialCStruct; | ||||
2035 | } | ||||
2036 | llvm_unreachable("Unknown DestructionKind")::llvm::llvm_unreachable_internal("Unknown DestructionKind", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2036); | ||||
2037 | } | ||||
2038 | |||||
2039 | /// pushEHDestroy - Push the standard destructor for the given type as | ||||
2040 | /// an EH-only cleanup. | ||||
2041 | void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind, | ||||
2042 | Address addr, QualType type) { | ||||
2043 | assert(dtorKind && "cannot push destructor for trivial type")((dtorKind && "cannot push destructor for trivial type" ) ? static_cast<void> (0) : __assert_fail ("dtorKind && \"cannot push destructor for trivial type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2043, __PRETTY_FUNCTION__)); | ||||
2044 | assert(needsEHCleanup(dtorKind))((needsEHCleanup(dtorKind)) ? static_cast<void> (0) : __assert_fail ("needsEHCleanup(dtorKind)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2044, __PRETTY_FUNCTION__)); | ||||
2045 | |||||
2046 | pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true); | ||||
2047 | } | ||||
2048 | |||||
2049 | /// pushDestroy - Push the standard destructor for the given type as | ||||
2050 | /// at least a normal cleanup. | ||||
2051 | void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind, | ||||
2052 | Address addr, QualType type) { | ||||
2053 | assert(dtorKind && "cannot push destructor for trivial type")((dtorKind && "cannot push destructor for trivial type" ) ? static_cast<void> (0) : __assert_fail ("dtorKind && \"cannot push destructor for trivial type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2053, __PRETTY_FUNCTION__)); | ||||
2054 | |||||
2055 | CleanupKind cleanupKind = getCleanupKind(dtorKind); | ||||
2056 | pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind), | ||||
2057 | cleanupKind & EHCleanup); | ||||
2058 | } | ||||
2059 | |||||
2060 | void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, Address addr, | ||||
2061 | QualType type, Destroyer *destroyer, | ||||
2062 | bool useEHCleanupForArray) { | ||||
2063 | pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type, | ||||
2064 | destroyer, useEHCleanupForArray); | ||||
2065 | } | ||||
2066 | |||||
2067 | void CodeGenFunction::pushStackRestore(CleanupKind Kind, Address SPMem) { | ||||
2068 | EHStack.pushCleanup<CallStackRestore>(Kind, SPMem); | ||||
2069 | } | ||||
2070 | |||||
2071 | void CodeGenFunction::pushLifetimeExtendedDestroy( | ||||
2072 | CleanupKind cleanupKind, Address addr, QualType type, | ||||
2073 | Destroyer *destroyer, bool useEHCleanupForArray) { | ||||
2074 | // Push an EH-only cleanup for the object now. | ||||
2075 | // FIXME: When popping normal cleanups, we need to keep this EH cleanup | ||||
2076 | // around in case a temporary's destructor throws an exception. | ||||
2077 | if (cleanupKind & EHCleanup) | ||||
2078 | EHStack.pushCleanup<DestroyObject>( | ||||
2079 | static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type, | ||||
2080 | destroyer, useEHCleanupForArray); | ||||
2081 | |||||
2082 | // Remember that we need to push a full cleanup for the object at the | ||||
2083 | // end of the full-expression. | ||||
2084 | pushCleanupAfterFullExpr<DestroyObject>( | ||||
2085 | cleanupKind, addr, type, destroyer, useEHCleanupForArray); | ||||
2086 | } | ||||
2087 | |||||
2088 | /// emitDestroy - Immediately perform the destruction of the given | ||||
2089 | /// object. | ||||
2090 | /// | ||||
2091 | /// \param addr - the address of the object; a type* | ||||
2092 | /// \param type - the type of the object; if an array type, all | ||||
2093 | /// objects are destroyed in reverse order | ||||
2094 | /// \param destroyer - the function to call to destroy individual | ||||
2095 | /// elements | ||||
2096 | /// \param useEHCleanupForArray - whether an EH cleanup should be | ||||
2097 | /// used when destroying array elements, in case one of the | ||||
2098 | /// destructions throws an exception | ||||
2099 | void CodeGenFunction::emitDestroy(Address addr, QualType type, | ||||
2100 | Destroyer *destroyer, | ||||
2101 | bool useEHCleanupForArray) { | ||||
2102 | const ArrayType *arrayType = getContext().getAsArrayType(type); | ||||
2103 | if (!arrayType) | ||||
2104 | return destroyer(*this, addr, type); | ||||
2105 | |||||
2106 | llvm::Value *length = emitArrayLength(arrayType, type, addr); | ||||
2107 | |||||
2108 | CharUnits elementAlign = | ||||
2109 | addr.getAlignment() | ||||
2110 | .alignmentOfArrayElement(getContext().getTypeSizeInChars(type)); | ||||
2111 | |||||
2112 | // Normally we have to check whether the array is zero-length. | ||||
2113 | bool checkZeroLength = true; | ||||
2114 | |||||
2115 | // But if the array length is constant, we can suppress that. | ||||
2116 | if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) { | ||||
2117 | // ...and if it's constant zero, we can just skip the entire thing. | ||||
2118 | if (constLength->isZero()) return; | ||||
2119 | checkZeroLength = false; | ||||
2120 | } | ||||
2121 | |||||
2122 | llvm::Value *begin = addr.getPointer(); | ||||
2123 | llvm::Value *end = Builder.CreateInBoundsGEP(begin, length); | ||||
2124 | emitArrayDestroy(begin, end, type, elementAlign, destroyer, | ||||
2125 | checkZeroLength, useEHCleanupForArray); | ||||
2126 | } | ||||
2127 | |||||
2128 | /// emitArrayDestroy - Destroys all the elements of the given array, | ||||
2129 | /// beginning from last to first. The array cannot be zero-length. | ||||
2130 | /// | ||||
2131 | /// \param begin - a type* denoting the first element of the array | ||||
2132 | /// \param end - a type* denoting one past the end of the array | ||||
2133 | /// \param elementType - the element type of the array | ||||
2134 | /// \param destroyer - the function to call to destroy elements | ||||
2135 | /// \param useEHCleanup - whether to push an EH cleanup to destroy | ||||
2136 | /// the remaining elements in case the destruction of a single | ||||
2137 | /// element throws | ||||
2138 | void CodeGenFunction::emitArrayDestroy(llvm::Value *begin, | ||||
2139 | llvm::Value *end, | ||||
2140 | QualType elementType, | ||||
2141 | CharUnits elementAlign, | ||||
2142 | Destroyer *destroyer, | ||||
2143 | bool checkZeroLength, | ||||
2144 | bool useEHCleanup) { | ||||
2145 | assert(!elementType->isArrayType())((!elementType->isArrayType()) ? static_cast<void> ( 0) : __assert_fail ("!elementType->isArrayType()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2145, __PRETTY_FUNCTION__)); | ||||
2146 | |||||
2147 | // The basic structure here is a do-while loop, because we don't | ||||
2148 | // need to check for the zero-element case. | ||||
2149 | llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body"); | ||||
2150 | llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done"); | ||||
2151 | |||||
2152 | if (checkZeroLength) { | ||||
2153 | llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end, | ||||
2154 | "arraydestroy.isempty"); | ||||
2155 | Builder.CreateCondBr(isEmpty, doneBB, bodyBB); | ||||
2156 | } | ||||
2157 | |||||
2158 | // Enter the loop body, making that address the current address. | ||||
2159 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); | ||||
2160 | EmitBlock(bodyBB); | ||||
2161 | llvm::PHINode *elementPast = | ||||
2162 | Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast"); | ||||
2163 | elementPast->addIncoming(end, entryBB); | ||||
2164 | |||||
2165 | // Shift the address back by one element. | ||||
2166 | llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true); | ||||
2167 | llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne, | ||||
2168 | "arraydestroy.element"); | ||||
2169 | |||||
2170 | if (useEHCleanup) | ||||
2171 | pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign, | ||||
2172 | destroyer); | ||||
2173 | |||||
2174 | // Perform the actual destruction there. | ||||
2175 | destroyer(*this, Address(element, elementAlign), elementType); | ||||
2176 | |||||
2177 | if (useEHCleanup) | ||||
2178 | PopCleanupBlock(); | ||||
2179 | |||||
2180 | // Check whether we've reached the end. | ||||
2181 | llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done"); | ||||
2182 | Builder.CreateCondBr(done, doneBB, bodyBB); | ||||
2183 | elementPast->addIncoming(element, Builder.GetInsertBlock()); | ||||
2184 | |||||
2185 | // Done. | ||||
2186 | EmitBlock(doneBB); | ||||
2187 | } | ||||
2188 | |||||
2189 | /// Perform partial array destruction as if in an EH cleanup. Unlike | ||||
2190 | /// emitArrayDestroy, the element type here may still be an array type. | ||||
2191 | static void emitPartialArrayDestroy(CodeGenFunction &CGF, | ||||
2192 | llvm::Value *begin, llvm::Value *end, | ||||
2193 | QualType type, CharUnits elementAlign, | ||||
2194 | CodeGenFunction::Destroyer *destroyer) { | ||||
2195 | // If the element type is itself an array, drill down. | ||||
2196 | unsigned arrayDepth = 0; | ||||
2197 | while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) { | ||||
2198 | // VLAs don't require a GEP index to walk into. | ||||
2199 | if (!isa<VariableArrayType>(arrayType)) | ||||
2200 | arrayDepth++; | ||||
2201 | type = arrayType->getElementType(); | ||||
2202 | } | ||||
2203 | |||||
2204 | if (arrayDepth) { | ||||
2205 | llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); | ||||
2206 | |||||
2207 | SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero); | ||||
2208 | begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin"); | ||||
2209 | end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend"); | ||||
2210 | } | ||||
2211 | |||||
2212 | // Destroy the array. We don't ever need an EH cleanup because we | ||||
2213 | // assume that we're in an EH cleanup ourselves, so a throwing | ||||
2214 | // destructor causes an immediate terminate. | ||||
2215 | CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer, | ||||
2216 | /*checkZeroLength*/ true, /*useEHCleanup*/ false); | ||||
2217 | } | ||||
2218 | |||||
2219 | namespace { | ||||
2220 | /// RegularPartialArrayDestroy - a cleanup which performs a partial | ||||
2221 | /// array destroy where the end pointer is regularly determined and | ||||
2222 | /// does not need to be loaded from a local. | ||||
2223 | class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup { | ||||
2224 | llvm::Value *ArrayBegin; | ||||
2225 | llvm::Value *ArrayEnd; | ||||
2226 | QualType ElementType; | ||||
2227 | CodeGenFunction::Destroyer *Destroyer; | ||||
2228 | CharUnits ElementAlign; | ||||
2229 | public: | ||||
2230 | RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd, | ||||
2231 | QualType elementType, CharUnits elementAlign, | ||||
2232 | CodeGenFunction::Destroyer *destroyer) | ||||
2233 | : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd), | ||||
2234 | ElementType(elementType), Destroyer(destroyer), | ||||
2235 | ElementAlign(elementAlign) {} | ||||
2236 | |||||
2237 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
2238 | emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd, | ||||
2239 | ElementType, ElementAlign, Destroyer); | ||||
2240 | } | ||||
2241 | }; | ||||
2242 | |||||
2243 | /// IrregularPartialArrayDestroy - a cleanup which performs a | ||||
2244 | /// partial array destroy where the end pointer is irregularly | ||||
2245 | /// determined and must be loaded from a local. | ||||
2246 | class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup { | ||||
2247 | llvm::Value *ArrayBegin; | ||||
2248 | Address ArrayEndPointer; | ||||
2249 | QualType ElementType; | ||||
2250 | CodeGenFunction::Destroyer *Destroyer; | ||||
2251 | CharUnits ElementAlign; | ||||
2252 | public: | ||||
2253 | IrregularPartialArrayDestroy(llvm::Value *arrayBegin, | ||||
2254 | Address arrayEndPointer, | ||||
2255 | QualType elementType, | ||||
2256 | CharUnits elementAlign, | ||||
2257 | CodeGenFunction::Destroyer *destroyer) | ||||
2258 | : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer), | ||||
2259 | ElementType(elementType), Destroyer(destroyer), | ||||
2260 | ElementAlign(elementAlign) {} | ||||
2261 | |||||
2262 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
2263 | llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer); | ||||
2264 | emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd, | ||||
2265 | ElementType, ElementAlign, Destroyer); | ||||
2266 | } | ||||
2267 | }; | ||||
2268 | } // end anonymous namespace | ||||
2269 | |||||
2270 | /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy | ||||
2271 | /// already-constructed elements of the given array. The cleanup | ||||
2272 | /// may be popped with DeactivateCleanupBlock or PopCleanupBlock. | ||||
2273 | /// | ||||
2274 | /// \param elementType - the immediate element type of the array; | ||||
2275 | /// possibly still an array type | ||||
2276 | void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, | ||||
2277 | Address arrayEndPointer, | ||||
2278 | QualType elementType, | ||||
2279 | CharUnits elementAlign, | ||||
2280 | Destroyer *destroyer) { | ||||
2281 | pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup, | ||||
2282 | arrayBegin, arrayEndPointer, | ||||
2283 | elementType, elementAlign, | ||||
2284 | destroyer); | ||||
2285 | } | ||||
2286 | |||||
2287 | /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy | ||||
2288 | /// already-constructed elements of the given array. The cleanup | ||||
2289 | /// may be popped with DeactivateCleanupBlock or PopCleanupBlock. | ||||
2290 | /// | ||||
2291 | /// \param elementType - the immediate element type of the array; | ||||
2292 | /// possibly still an array type | ||||
2293 | void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, | ||||
2294 | llvm::Value *arrayEnd, | ||||
2295 | QualType elementType, | ||||
2296 | CharUnits elementAlign, | ||||
2297 | Destroyer *destroyer) { | ||||
2298 | pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup, | ||||
2299 | arrayBegin, arrayEnd, | ||||
2300 | elementType, elementAlign, | ||||
2301 | destroyer); | ||||
2302 | } | ||||
2303 | |||||
2304 | /// Lazily declare the @llvm.lifetime.start intrinsic. | ||||
2305 | llvm::Function *CodeGenModule::getLLVMLifetimeStartFn() { | ||||
2306 | if (LifetimeStartFn) | ||||
2307 | return LifetimeStartFn; | ||||
2308 | LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(), | ||||
2309 | llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy); | ||||
2310 | return LifetimeStartFn; | ||||
2311 | } | ||||
2312 | |||||
2313 | /// Lazily declare the @llvm.lifetime.end intrinsic. | ||||
2314 | llvm::Function *CodeGenModule::getLLVMLifetimeEndFn() { | ||||
2315 | if (LifetimeEndFn) | ||||
2316 | return LifetimeEndFn; | ||||
2317 | LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(), | ||||
2318 | llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy); | ||||
2319 | return LifetimeEndFn; | ||||
2320 | } | ||||
2321 | |||||
2322 | namespace { | ||||
2323 | /// A cleanup to perform a release of an object at the end of a | ||||
2324 | /// function. This is used to balance out the incoming +1 of a | ||||
2325 | /// ns_consumed argument when we can't reasonably do that just by | ||||
2326 | /// not doing the initial retain for a __block argument. | ||||
2327 | struct ConsumeARCParameter final : EHScopeStack::Cleanup { | ||||
2328 | ConsumeARCParameter(llvm::Value *param, | ||||
2329 | ARCPreciseLifetime_t precise) | ||||
2330 | : Param(param), Precise(precise) {} | ||||
2331 | |||||
2332 | llvm::Value *Param; | ||||
2333 | ARCPreciseLifetime_t Precise; | ||||
2334 | |||||
2335 | void Emit(CodeGenFunction &CGF, Flags flags) override { | ||||
2336 | CGF.EmitARCRelease(Param, Precise); | ||||
2337 | } | ||||
2338 | }; | ||||
2339 | } // end anonymous namespace | ||||
2340 | |||||
2341 | /// Emit an alloca (or GlobalValue depending on target) | ||||
2342 | /// for the specified parameter and set up LocalDeclMap. | ||||
2343 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg, | ||||
2344 | unsigned ArgNo) { | ||||
2345 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? | ||||
2346 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&(((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>( D)) && "Invalid argument to EmitParmDecl") ? static_cast <void> (0) : __assert_fail ("(isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && \"Invalid argument to EmitParmDecl\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2347, __PRETTY_FUNCTION__)) | ||||
| |||||
2347 | "Invalid argument to EmitParmDecl")(((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>( D)) && "Invalid argument to EmitParmDecl") ? static_cast <void> (0) : __assert_fail ("(isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && \"Invalid argument to EmitParmDecl\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2347, __PRETTY_FUNCTION__)); | ||||
2348 | |||||
2349 | Arg.getAnyValue()->setName(D.getName()); | ||||
2350 | |||||
2351 | QualType Ty = D.getType(); | ||||
2352 | |||||
2353 | // Use better IR generation for certain implicit parameters. | ||||
2354 | if (auto IPD
| ||||
2355 | // The only implicit argument a block has is its literal. | ||||
2356 | // This may be passed as an inalloca'ed value on Windows x86. | ||||
2357 | if (BlockInfo) { | ||||
2358 | llvm::Value *V = Arg.isIndirect() | ||||
2359 | ? Builder.CreateLoad(Arg.getIndirectAddress()) | ||||
2360 | : Arg.getDirectValue(); | ||||
2361 | setBlockContextParameter(IPD, ArgNo, V); | ||||
2362 | return; | ||||
2363 | } | ||||
2364 | } | ||||
2365 | |||||
2366 | Address DeclPtr = Address::invalid(); | ||||
2367 | bool DoStore = false; | ||||
2368 | bool IsScalar = hasScalarEvaluationKind(Ty); | ||||
2369 | // If we already have a pointer to the argument, reuse the input pointer. | ||||
2370 | if (Arg.isIndirect()) { | ||||
2371 | DeclPtr = Arg.getIndirectAddress(); | ||||
2372 | // If we have a prettier pointer type at this point, bitcast to that. | ||||
2373 | unsigned AS = DeclPtr.getType()->getAddressSpace(); | ||||
2374 | llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS); | ||||
2375 | if (DeclPtr.getType() != IRTy) | ||||
2376 | DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName()); | ||||
2377 | // Indirect argument is in alloca address space, which may be different | ||||
2378 | // from the default address space. | ||||
2379 | auto AllocaAS = CGM.getASTAllocaAddressSpace(); | ||||
2380 | auto *V = DeclPtr.getPointer(); | ||||
2381 | auto SrcLangAS = getLangOpts().OpenCL ? LangAS::opencl_private : AllocaAS; | ||||
2382 | auto DestLangAS = | ||||
2383 | getLangOpts().OpenCL
| ||||
2384 | if (SrcLangAS
| ||||
2385 | assert(getContext().getTargetAddressSpace(SrcLangAS) ==((getContext().getTargetAddressSpace(SrcLangAS) == CGM.getDataLayout ().getAllocaAddrSpace()) ? static_cast<void> (0) : __assert_fail ("getContext().getTargetAddressSpace(SrcLangAS) == CGM.getDataLayout().getAllocaAddrSpace()" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2386, __PRETTY_FUNCTION__)) | ||||
2386 | CGM.getDataLayout().getAllocaAddrSpace())((getContext().getTargetAddressSpace(SrcLangAS) == CGM.getDataLayout ().getAllocaAddrSpace()) ? static_cast<void> (0) : __assert_fail ("getContext().getTargetAddressSpace(SrcLangAS) == CGM.getDataLayout().getAllocaAddrSpace()" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2386, __PRETTY_FUNCTION__)); | ||||
2387 | auto DestAS = getContext().getTargetAddressSpace(DestLangAS); | ||||
2388 | auto *T = V->getType()->getPointerElementType()->getPointerTo(DestAS); | ||||
2389 | DeclPtr = Address(getTargetHooks().performAddrSpaceCast( | ||||
2390 | *this, V, SrcLangAS, DestLangAS, T, true), | ||||
2391 | DeclPtr.getAlignment()); | ||||
2392 | } | ||||
2393 | |||||
2394 | // Push a destructor cleanup for this parameter if the ABI requires it. | ||||
2395 | // Don't push a cleanup in a thunk for a method that will also emit a | ||||
2396 | // cleanup. | ||||
2397 | if (hasAggregateEvaluationKind(Ty) && !CurFuncIsThunk && | ||||
2398 | Ty->getAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) { | ||||
| |||||
2399 | if (QualType::DestructionKind DtorKind = | ||||
2400 | D.needsDestruction(getContext())) { | ||||
2401 | assert((DtorKind == QualType::DK_cxx_destructor ||(((DtorKind == QualType::DK_cxx_destructor || DtorKind == QualType ::DK_nontrivial_c_struct) && "unexpected destructor type" ) ? static_cast<void> (0) : __assert_fail ("(DtorKind == QualType::DK_cxx_destructor || DtorKind == QualType::DK_nontrivial_c_struct) && \"unexpected destructor type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2403, __PRETTY_FUNCTION__)) | ||||
2402 | DtorKind == QualType::DK_nontrivial_c_struct) &&(((DtorKind == QualType::DK_cxx_destructor || DtorKind == QualType ::DK_nontrivial_c_struct) && "unexpected destructor type" ) ? static_cast<void> (0) : __assert_fail ("(DtorKind == QualType::DK_cxx_destructor || DtorKind == QualType::DK_nontrivial_c_struct) && \"unexpected destructor type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2403, __PRETTY_FUNCTION__)) | ||||
2403 | "unexpected destructor type")(((DtorKind == QualType::DK_cxx_destructor || DtorKind == QualType ::DK_nontrivial_c_struct) && "unexpected destructor type" ) ? static_cast<void> (0) : __assert_fail ("(DtorKind == QualType::DK_cxx_destructor || DtorKind == QualType::DK_nontrivial_c_struct) && \"unexpected destructor type\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2403, __PRETTY_FUNCTION__)); | ||||
2404 | pushDestroy(DtorKind, DeclPtr, Ty); | ||||
2405 | CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] = | ||||
2406 | EHStack.stable_begin(); | ||||
2407 | } | ||||
2408 | } | ||||
2409 | } else { | ||||
2410 | // Check if the parameter address is controlled by OpenMP runtime. | ||||
2411 | Address OpenMPLocalAddr = | ||||
2412 | getLangOpts().OpenMP | ||||
2413 | ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D) | ||||
2414 | : Address::invalid(); | ||||
2415 | if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) { | ||||
2416 | DeclPtr = OpenMPLocalAddr; | ||||
2417 | } else { | ||||
2418 | // Otherwise, create a temporary to hold the value. | ||||
2419 | DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D), | ||||
2420 | D.getName() + ".addr"); | ||||
2421 | } | ||||
2422 | DoStore = true; | ||||
2423 | } | ||||
2424 | |||||
2425 | llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr); | ||||
2426 | |||||
2427 | LValue lv = MakeAddrLValue(DeclPtr, Ty); | ||||
2428 | if (IsScalar) { | ||||
2429 | Qualifiers qs = Ty.getQualifiers(); | ||||
2430 | if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) { | ||||
2431 | // We honor __attribute__((ns_consumed)) for types with lifetime. | ||||
2432 | // For __strong, it's handled by just skipping the initial retain; | ||||
2433 | // otherwise we have to balance out the initial +1 with an extra | ||||
2434 | // cleanup to do the release at the end of the function. | ||||
2435 | bool isConsumed = D.hasAttr<NSConsumedAttr>(); | ||||
2436 | |||||
2437 | // If a parameter is pseudo-strong then we can omit the implicit retain. | ||||
2438 | if (D.isARCPseudoStrong()) { | ||||
2439 | assert(lt == Qualifiers::OCL_Strong &&((lt == Qualifiers::OCL_Strong && "pseudo-strong variable isn't strong?" ) ? static_cast<void> (0) : __assert_fail ("lt == Qualifiers::OCL_Strong && \"pseudo-strong variable isn't strong?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2440, __PRETTY_FUNCTION__)) | ||||
2440 | "pseudo-strong variable isn't strong?")((lt == Qualifiers::OCL_Strong && "pseudo-strong variable isn't strong?" ) ? static_cast<void> (0) : __assert_fail ("lt == Qualifiers::OCL_Strong && \"pseudo-strong variable isn't strong?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2440, __PRETTY_FUNCTION__)); | ||||
2441 | assert(qs.hasConst() && "pseudo-strong variable should be const!")((qs.hasConst() && "pseudo-strong variable should be const!" ) ? static_cast<void> (0) : __assert_fail ("qs.hasConst() && \"pseudo-strong variable should be const!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CGDecl.cpp" , 2441, __PRETTY_FUNCTION__)); | ||||
2442 | lt = Qualifiers::OCL_ExplicitNone; | ||||
2443 | } | ||||
2444 | |||||
2445 | // Load objects passed indirectly. | ||||
2446 | if (Arg.isIndirect() && !ArgVal) | ||||
2447 | ArgVal = Builder.CreateLoad(DeclPtr); | ||||
2448 | |||||
2449 | if (lt == Qualifiers::OCL_Strong) { | ||||
2450 | if (!isConsumed) { | ||||
2451 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { | ||||
2452 | // use objc_storeStrong(&dest, value) for retaining the | ||||
2453 | // object. But first, store a null into 'dest' because | ||||
2454 | // objc_storeStrong attempts to release its old value. | ||||
2455 | llvm::Value *Null = CGM.EmitNullConstant(D.getType()); | ||||
2456 | EmitStoreOfScalar(Null, lv, /* isInitialization */ true); | ||||
2457 | EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true); | ||||
2458 | DoStore = false; | ||||
2459 | } | ||||
2460 | else | ||||
2461 | // Don't use objc_retainBlock for block pointers, because we | ||||
2462 | // don't want to Block_copy something just because we got it | ||||
2463 | // as a parameter. | ||||
2464 | ArgVal = EmitARCRetainNonBlock(ArgVal); | ||||
2465 | } | ||||
2466 | } else { | ||||
2467 | // Push the cleanup for a consumed parameter. | ||||
2468 | if (isConsumed) { | ||||
2469 | ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>() | ||||
2470 | ? ARCPreciseLifetime : ARCImpreciseLifetime); | ||||
2471 | EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal, | ||||
2472 | precise); | ||||
2473 | } | ||||
2474 | |||||
2475 | if (lt == Qualifiers::OCL_Weak) { | ||||
2476 | EmitARCInitWeak(DeclPtr, ArgVal); | ||||
2477 | DoStore = false; // The weak init is a store, no need to do two. | ||||
2478 | } | ||||
2479 | } | ||||
2480 | |||||
2481 | // Enter the cleanup scope. | ||||
2482 | EmitAutoVarWithLifetime(*this, D, DeclPtr, lt); | ||||
2483 | } | ||||
2484 | } | ||||
2485 | |||||
2486 | // Store the initial value into the alloca. | ||||
2487 | if (DoStore) | ||||
2488 | EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true); | ||||
2489 | |||||
2490 | setAddrOfLocalVar(&D, DeclPtr); | ||||
2491 | |||||
2492 | // Emit debug info for param declarations in non-thunk functions. | ||||
2493 | if (CGDebugInfo *DI = getDebugInfo()) { | ||||
2494 | if (CGM.getCodeGenOpts().getDebugInfo() >= | ||||
2495 | codegenoptions::LimitedDebugInfo && | ||||
2496 | !CurFuncIsThunk) { | ||||
2497 | DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder); | ||||
2498 | } | ||||
2499 | } | ||||
2500 | |||||
2501 | if (D.hasAttr<AnnotateAttr>()) | ||||
2502 | EmitVarAnnotations(&D, DeclPtr.getPointer()); | ||||
2503 | |||||
2504 | // We can only check return value nullability if all arguments to the | ||||
2505 | // function satisfy their nullability preconditions. This makes it necessary | ||||
2506 | // to emit null checks for args in the function body itself. | ||||
2507 | if (requiresReturnValueNullabilityCheck()) { | ||||
2508 | auto Nullability = Ty->getNullability(getContext()); | ||||
2509 | if (Nullability && *Nullability == NullabilityKind::NonNull) { | ||||
2510 | SanitizerScope SanScope(this); | ||||
2511 | RetValNullabilityPrecondition = | ||||
2512 | Builder.CreateAnd(RetValNullabilityPrecondition, | ||||
2513 | Builder.CreateIsNotNull(Arg.getAnyValue())); | ||||
2514 | } | ||||
2515 | } | ||||
2516 | } | ||||
2517 | |||||
2518 | void CodeGenModule::EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, | ||||
2519 | CodeGenFunction *CGF) { | ||||
2520 | if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed())) | ||||
2521 | return; | ||||
2522 | getOpenMPRuntime().emitUserDefinedReduction(CGF, D); | ||||
2523 | } | ||||
2524 | |||||
2525 | void CodeGenModule::EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, | ||||
2526 | CodeGenFunction *CGF) { | ||||
2527 | if (!LangOpts.OpenMP || LangOpts.OpenMPSimd || | ||||
2528 | (!LangOpts.EmitAllDecls && !D->isUsed())) | ||||
2529 | return; | ||||
2530 | getOpenMPRuntime().emitUserDefinedMapper(D, CGF); | ||||
2531 | } | ||||
2532 | |||||
2533 | void CodeGenModule::EmitOMPRequiresDecl(const OMPRequiresDecl *D) { | ||||
2534 | getOpenMPRuntime().checkArchForUnifiedAddressing(D); | ||||
2535 | } |
1 | //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This is the internal per-function state used for llvm translation. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H |
14 | #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H |
15 | |
16 | #include "CGBuilder.h" |
17 | #include "CGDebugInfo.h" |
18 | #include "CGLoopInfo.h" |
19 | #include "CGValue.h" |
20 | #include "CodeGenModule.h" |
21 | #include "CodeGenPGO.h" |
22 | #include "EHScopeStack.h" |
23 | #include "VarBypassDetector.h" |
24 | #include "clang/AST/CharUnits.h" |
25 | #include "clang/AST/CurrentSourceLocExprScope.h" |
26 | #include "clang/AST/ExprCXX.h" |
27 | #include "clang/AST/ExprObjC.h" |
28 | #include "clang/AST/ExprOpenMP.h" |
29 | #include "clang/AST/Type.h" |
30 | #include "clang/Basic/ABI.h" |
31 | #include "clang/Basic/CapturedStmt.h" |
32 | #include "clang/Basic/CodeGenOptions.h" |
33 | #include "clang/Basic/OpenMPKinds.h" |
34 | #include "clang/Basic/TargetInfo.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/DenseMap.h" |
37 | #include "llvm/ADT/MapVector.h" |
38 | #include "llvm/ADT/SmallVector.h" |
39 | #include "llvm/IR/ValueHandle.h" |
40 | #include "llvm/Support/Debug.h" |
41 | #include "llvm/Transforms/Utils/SanitizerStats.h" |
42 | |
43 | namespace llvm { |
44 | class BasicBlock; |
45 | class LLVMContext; |
46 | class MDNode; |
47 | class Module; |
48 | class SwitchInst; |
49 | class Twine; |
50 | class Value; |
51 | } |
52 | |
53 | namespace clang { |
54 | class ASTContext; |
55 | class BlockDecl; |
56 | class CXXDestructorDecl; |
57 | class CXXForRangeStmt; |
58 | class CXXTryStmt; |
59 | class Decl; |
60 | class LabelDecl; |
61 | class EnumConstantDecl; |
62 | class FunctionDecl; |
63 | class FunctionProtoType; |
64 | class LabelStmt; |
65 | class ObjCContainerDecl; |
66 | class ObjCInterfaceDecl; |
67 | class ObjCIvarDecl; |
68 | class ObjCMethodDecl; |
69 | class ObjCImplementationDecl; |
70 | class ObjCPropertyImplDecl; |
71 | class TargetInfo; |
72 | class VarDecl; |
73 | class ObjCForCollectionStmt; |
74 | class ObjCAtTryStmt; |
75 | class ObjCAtThrowStmt; |
76 | class ObjCAtSynchronizedStmt; |
77 | class ObjCAutoreleasePoolStmt; |
78 | |
79 | namespace analyze_os_log { |
80 | class OSLogBufferLayout; |
81 | } |
82 | |
83 | namespace CodeGen { |
84 | class CodeGenTypes; |
85 | class CGCallee; |
86 | class CGFunctionInfo; |
87 | class CGRecordLayout; |
88 | class CGBlockInfo; |
89 | class CGCXXABI; |
90 | class BlockByrefHelpers; |
91 | class BlockByrefInfo; |
92 | class BlockFlags; |
93 | class BlockFieldFlags; |
94 | class RegionCodeGenTy; |
95 | class TargetCodeGenInfo; |
96 | struct OMPTaskDataTy; |
97 | struct CGCoroData; |
98 | |
99 | /// The kind of evaluation to perform on values of a particular |
100 | /// type. Basically, is the code in CGExprScalar, CGExprComplex, or |
101 | /// CGExprAgg? |
102 | /// |
103 | /// TODO: should vectors maybe be split out into their own thing? |
104 | enum TypeEvaluationKind { |
105 | TEK_Scalar, |
106 | TEK_Complex, |
107 | TEK_Aggregate |
108 | }; |
109 | |
110 | #define LIST_SANITIZER_CHECKSSANITIZER_CHECK(AddOverflow, add_overflow, 0) SANITIZER_CHECK (BuiltinUnreachable, builtin_unreachable, 0) SANITIZER_CHECK( CFICheckFail, cfi_check_fail, 0) SANITIZER_CHECK(DivremOverflow , divrem_overflow, 0) SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss , 0) SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0 ) SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch , 1) SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0) SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) SANITIZER_CHECK (LoadInvalidValue, load_invalid_value, 0) SANITIZER_CHECK(MissingReturn , missing_return, 0) SANITIZER_CHECK(MulOverflow, mul_overflow , 0) SANITIZER_CHECK(NegateOverflow, negate_overflow, 0) SANITIZER_CHECK (NullabilityArg, nullability_arg, 0) SANITIZER_CHECK(NullabilityReturn , nullability_return, 1) SANITIZER_CHECK(NonnullArg, nonnull_arg , 0) SANITIZER_CHECK(NonnullReturn, nonnull_return, 1) SANITIZER_CHECK (OutOfBounds, out_of_bounds, 0) SANITIZER_CHECK(PointerOverflow , pointer_overflow, 0) SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds , 0) SANITIZER_CHECK(SubOverflow, sub_overflow, 0) SANITIZER_CHECK (TypeMismatch, type_mismatch, 1) SANITIZER_CHECK(AlignmentAssumption , alignment_assumption, 0) SANITIZER_CHECK(VLABoundNotPositive , vla_bound_not_positive, 0) \ |
111 | SANITIZER_CHECK(AddOverflow, add_overflow, 0) \ |
112 | SANITIZER_CHECK(BuiltinUnreachable, builtin_unreachable, 0) \ |
113 | SANITIZER_CHECK(CFICheckFail, cfi_check_fail, 0) \ |
114 | SANITIZER_CHECK(DivremOverflow, divrem_overflow, 0) \ |
115 | SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss, 0) \ |
116 | SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0) \ |
117 | SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch, 1) \ |
118 | SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0) \ |
119 | SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) \ |
120 | SANITIZER_CHECK(LoadInvalidValue, load_invalid_value, 0) \ |
121 | SANITIZER_CHECK(MissingReturn, missing_return, 0) \ |
122 | SANITIZER_CHECK(MulOverflow, mul_overflow, 0) \ |
123 | SANITIZER_CHECK(NegateOverflow, negate_overflow, 0) \ |
124 | SANITIZER_CHECK(NullabilityArg, nullability_arg, 0) \ |
125 | SANITIZER_CHECK(NullabilityReturn, nullability_return, 1) \ |
126 | SANITIZER_CHECK(NonnullArg, nonnull_arg, 0) \ |
127 | SANITIZER_CHECK(NonnullReturn, nonnull_return, 1) \ |
128 | SANITIZER_CHECK(OutOfBounds, out_of_bounds, 0) \ |
129 | SANITIZER_CHECK(PointerOverflow, pointer_overflow, 0) \ |
130 | SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds, 0) \ |
131 | SANITIZER_CHECK(SubOverflow, sub_overflow, 0) \ |
132 | SANITIZER_CHECK(TypeMismatch, type_mismatch, 1) \ |
133 | SANITIZER_CHECK(AlignmentAssumption, alignment_assumption, 0) \ |
134 | SANITIZER_CHECK(VLABoundNotPositive, vla_bound_not_positive, 0) |
135 | |
136 | enum SanitizerHandler { |
137 | #define SANITIZER_CHECK(Enum, Name, Version) Enum, |
138 | LIST_SANITIZER_CHECKSSANITIZER_CHECK(AddOverflow, add_overflow, 0) SANITIZER_CHECK (BuiltinUnreachable, builtin_unreachable, 0) SANITIZER_CHECK( CFICheckFail, cfi_check_fail, 0) SANITIZER_CHECK(DivremOverflow , divrem_overflow, 0) SANITIZER_CHECK(DynamicTypeCacheMiss, dynamic_type_cache_miss , 0) SANITIZER_CHECK(FloatCastOverflow, float_cast_overflow, 0 ) SANITIZER_CHECK(FunctionTypeMismatch, function_type_mismatch , 1) SANITIZER_CHECK(ImplicitConversion, implicit_conversion, 0) SANITIZER_CHECK(InvalidBuiltin, invalid_builtin, 0) SANITIZER_CHECK (LoadInvalidValue, load_invalid_value, 0) SANITIZER_CHECK(MissingReturn , missing_return, 0) SANITIZER_CHECK(MulOverflow, mul_overflow , 0) SANITIZER_CHECK(NegateOverflow, negate_overflow, 0) SANITIZER_CHECK (NullabilityArg, nullability_arg, 0) SANITIZER_CHECK(NullabilityReturn , nullability_return, 1) SANITIZER_CHECK(NonnullArg, nonnull_arg , 0) SANITIZER_CHECK(NonnullReturn, nonnull_return, 1) SANITIZER_CHECK (OutOfBounds, out_of_bounds, 0) SANITIZER_CHECK(PointerOverflow , pointer_overflow, 0) SANITIZER_CHECK(ShiftOutOfBounds, shift_out_of_bounds , 0) SANITIZER_CHECK(SubOverflow, sub_overflow, 0) SANITIZER_CHECK (TypeMismatch, type_mismatch, 1) SANITIZER_CHECK(AlignmentAssumption , alignment_assumption, 0) SANITIZER_CHECK(VLABoundNotPositive , vla_bound_not_positive, 0) |
139 | #undef SANITIZER_CHECK |
140 | }; |
141 | |
142 | /// Helper class with most of the code for saving a value for a |
143 | /// conditional expression cleanup. |
144 | struct DominatingLLVMValue { |
145 | typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type; |
146 | |
147 | /// Answer whether the given value needs extra work to be saved. |
148 | static bool needsSaving(llvm::Value *value) { |
149 | // If it's not an instruction, we don't need to save. |
150 | if (!isa<llvm::Instruction>(value)) return false; |
151 | |
152 | // If it's an instruction in the entry block, we don't need to save. |
153 | llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent(); |
154 | return (block != &block->getParent()->getEntryBlock()); |
155 | } |
156 | |
157 | static saved_type save(CodeGenFunction &CGF, llvm::Value *value); |
158 | static llvm::Value *restore(CodeGenFunction &CGF, saved_type value); |
159 | }; |
160 | |
161 | /// A partial specialization of DominatingValue for llvm::Values that |
162 | /// might be llvm::Instructions. |
163 | template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue { |
164 | typedef T *type; |
165 | static type restore(CodeGenFunction &CGF, saved_type value) { |
166 | return static_cast<T*>(DominatingLLVMValue::restore(CGF, value)); |
167 | } |
168 | }; |
169 | |
170 | /// A specialization of DominatingValue for Address. |
171 | template <> struct DominatingValue<Address> { |
172 | typedef Address type; |
173 | |
174 | struct saved_type { |
175 | DominatingLLVMValue::saved_type SavedValue; |
176 | CharUnits Alignment; |
177 | }; |
178 | |
179 | static bool needsSaving(type value) { |
180 | return DominatingLLVMValue::needsSaving(value.getPointer()); |
181 | } |
182 | static saved_type save(CodeGenFunction &CGF, type value) { |
183 | return { DominatingLLVMValue::save(CGF, value.getPointer()), |
184 | value.getAlignment() }; |
185 | } |
186 | static type restore(CodeGenFunction &CGF, saved_type value) { |
187 | return Address(DominatingLLVMValue::restore(CGF, value.SavedValue), |
188 | value.Alignment); |
189 | } |
190 | }; |
191 | |
192 | /// A specialization of DominatingValue for RValue. |
193 | template <> struct DominatingValue<RValue> { |
194 | typedef RValue type; |
195 | class saved_type { |
196 | enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral, |
197 | AggregateAddress, ComplexAddress }; |
198 | |
199 | llvm::Value *Value; |
200 | unsigned K : 3; |
201 | unsigned Align : 29; |
202 | saved_type(llvm::Value *v, Kind k, unsigned a = 0) |
203 | : Value(v), K(k), Align(a) {} |
204 | |
205 | public: |
206 | static bool needsSaving(RValue value); |
207 | static saved_type save(CodeGenFunction &CGF, RValue value); |
208 | RValue restore(CodeGenFunction &CGF); |
209 | |
210 | // implementations in CGCleanup.cpp |
211 | }; |
212 | |
213 | static bool needsSaving(type value) { |
214 | return saved_type::needsSaving(value); |
215 | } |
216 | static saved_type save(CodeGenFunction &CGF, type value) { |
217 | return saved_type::save(CGF, value); |
218 | } |
219 | static type restore(CodeGenFunction &CGF, saved_type value) { |
220 | return value.restore(CGF); |
221 | } |
222 | }; |
223 | |
224 | /// CodeGenFunction - This class organizes the per-function state that is used |
225 | /// while generating LLVM code. |
226 | class CodeGenFunction : public CodeGenTypeCache { |
227 | CodeGenFunction(const CodeGenFunction &) = delete; |
228 | void operator=(const CodeGenFunction &) = delete; |
229 | |
230 | friend class CGCXXABI; |
231 | public: |
232 | /// A jump destination is an abstract label, branching to which may |
233 | /// require a jump out through normal cleanups. |
234 | struct JumpDest { |
235 | JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {} |
236 | JumpDest(llvm::BasicBlock *Block, |
237 | EHScopeStack::stable_iterator Depth, |
238 | unsigned Index) |
239 | : Block(Block), ScopeDepth(Depth), Index(Index) {} |
240 | |
241 | bool isValid() const { return Block != nullptr; } |
242 | llvm::BasicBlock *getBlock() const { return Block; } |
243 | EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; } |
244 | unsigned getDestIndex() const { return Index; } |
245 | |
246 | // This should be used cautiously. |
247 | void setScopeDepth(EHScopeStack::stable_iterator depth) { |
248 | ScopeDepth = depth; |
249 | } |
250 | |
251 | private: |
252 | llvm::BasicBlock *Block; |
253 | EHScopeStack::stable_iterator ScopeDepth; |
254 | unsigned Index; |
255 | }; |
256 | |
257 | CodeGenModule &CGM; // Per-module state. |
258 | const TargetInfo &Target; |
259 | |
260 | typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; |
261 | LoopInfoStack LoopStack; |
262 | CGBuilderTy Builder; |
263 | |
264 | // Stores variables for which we can't generate correct lifetime markers |
265 | // because of jumps. |
266 | VarBypassDetector Bypasses; |
267 | |
268 | // CodeGen lambda for loops and support for ordered clause |
269 | typedef llvm::function_ref<void(CodeGenFunction &, const OMPLoopDirective &, |
270 | JumpDest)> |
271 | CodeGenLoopTy; |
272 | typedef llvm::function_ref<void(CodeGenFunction &, SourceLocation, |
273 | const unsigned, const bool)> |
274 | CodeGenOrderedTy; |
275 | |
276 | // Codegen lambda for loop bounds in worksharing loop constructs |
277 | typedef llvm::function_ref<std::pair<LValue, LValue>( |
278 | CodeGenFunction &, const OMPExecutableDirective &S)> |
279 | CodeGenLoopBoundsTy; |
280 | |
281 | // Codegen lambda for loop bounds in dispatch-based loop implementation |
282 | typedef llvm::function_ref<std::pair<llvm::Value *, llvm::Value *>( |
283 | CodeGenFunction &, const OMPExecutableDirective &S, Address LB, |
284 | Address UB)> |
285 | CodeGenDispatchBoundsTy; |
286 | |
287 | /// CGBuilder insert helper. This function is called after an |
288 | /// instruction is created using Builder. |
289 | void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, |
290 | llvm::BasicBlock *BB, |
291 | llvm::BasicBlock::iterator InsertPt) const; |
292 | |
293 | /// CurFuncDecl - Holds the Decl for the current outermost |
294 | /// non-closure context. |
295 | const Decl *CurFuncDecl; |
296 | /// CurCodeDecl - This is the inner-most code context, which includes blocks. |
297 | const Decl *CurCodeDecl; |
298 | const CGFunctionInfo *CurFnInfo; |
299 | QualType FnRetTy; |
300 | llvm::Function *CurFn = nullptr; |
301 | |
302 | // Holds coroutine data if the current function is a coroutine. We use a |
303 | // wrapper to manage its lifetime, so that we don't have to define CGCoroData |
304 | // in this header. |
305 | struct CGCoroInfo { |
306 | std::unique_ptr<CGCoroData> Data; |
307 | CGCoroInfo(); |
308 | ~CGCoroInfo(); |
309 | }; |
310 | CGCoroInfo CurCoro; |
311 | |
312 | bool isCoroutine() const { |
313 | return CurCoro.Data != nullptr; |
314 | } |
315 | |
316 | /// CurGD - The GlobalDecl for the current function being compiled. |
317 | GlobalDecl CurGD; |
318 | |
319 | /// PrologueCleanupDepth - The cleanup depth enclosing all the |
320 | /// cleanups associated with the parameters. |
321 | EHScopeStack::stable_iterator PrologueCleanupDepth; |
322 | |
323 | /// ReturnBlock - Unified return block. |
324 | JumpDest ReturnBlock; |
325 | |
326 | /// ReturnValue - The temporary alloca to hold the return |
327 | /// value. This is invalid iff the function has no return value. |
328 | Address ReturnValue = Address::invalid(); |
329 | |
330 | /// ReturnValuePointer - The temporary alloca to hold a pointer to sret. |
331 | /// This is invalid if sret is not in use. |
332 | Address ReturnValuePointer = Address::invalid(); |
333 | |
334 | /// Return true if a label was seen in the current scope. |
335 | bool hasLabelBeenSeenInCurrentScope() const { |
336 | if (CurLexicalScope) |
337 | return CurLexicalScope->hasLabels(); |
338 | return !LabelMap.empty(); |
339 | } |
340 | |
341 | /// AllocaInsertPoint - This is an instruction in the entry block before which |
342 | /// we prefer to insert allocas. |
343 | llvm::AssertingVH<llvm::Instruction> AllocaInsertPt; |
344 | |
345 | /// API for captured statement code generation. |
346 | class CGCapturedStmtInfo { |
347 | public: |
348 | explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default) |
349 | : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {} |
350 | explicit CGCapturedStmtInfo(const CapturedStmt &S, |
351 | CapturedRegionKind K = CR_Default) |
352 | : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) { |
353 | |
354 | RecordDecl::field_iterator Field = |
355 | S.getCapturedRecordDecl()->field_begin(); |
356 | for (CapturedStmt::const_capture_iterator I = S.capture_begin(), |
357 | E = S.capture_end(); |
358 | I != E; ++I, ++Field) { |
359 | if (I->capturesThis()) |
360 | CXXThisFieldDecl = *Field; |
361 | else if (I->capturesVariable()) |
362 | CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field; |
363 | else if (I->capturesVariableByCopy()) |
364 | CaptureFields[I->getCapturedVar()->getCanonicalDecl()] = *Field; |
365 | } |
366 | } |
367 | |
368 | virtual ~CGCapturedStmtInfo(); |
369 | |
370 | CapturedRegionKind getKind() const { return Kind; } |
371 | |
372 | virtual void setContextValue(llvm::Value *V) { ThisValue = V; } |
373 | // Retrieve the value of the context parameter. |
374 | virtual llvm::Value *getContextValue() const { return ThisValue; } |
375 | |
376 | /// Lookup the captured field decl for a variable. |
377 | virtual const FieldDecl *lookup(const VarDecl *VD) const { |
378 | return CaptureFields.lookup(VD->getCanonicalDecl()); |
379 | } |
380 | |
381 | bool isCXXThisExprCaptured() const { return getThisFieldDecl() != nullptr; } |
382 | virtual FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; } |
383 | |
384 | static bool classof(const CGCapturedStmtInfo *) { |
385 | return true; |
386 | } |
387 | |
388 | /// Emit the captured statement body. |
389 | virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) { |
390 | CGF.incrementProfileCounter(S); |
391 | CGF.EmitStmt(S); |
392 | } |
393 | |
394 | /// Get the name of the capture helper. |
395 | virtual StringRef getHelperName() const { return "__captured_stmt"; } |
396 | |
397 | private: |
398 | /// The kind of captured statement being generated. |
399 | CapturedRegionKind Kind; |
400 | |
401 | /// Keep the map between VarDecl and FieldDecl. |
402 | llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields; |
403 | |
404 | /// The base address of the captured record, passed in as the first |
405 | /// argument of the parallel region function. |
406 | llvm::Value *ThisValue; |
407 | |
408 | /// Captured 'this' type. |
409 | FieldDecl *CXXThisFieldDecl; |
410 | }; |
411 | CGCapturedStmtInfo *CapturedStmtInfo = nullptr; |
412 | |
413 | /// RAII for correct setting/restoring of CapturedStmtInfo. |
414 | class CGCapturedStmtRAII { |
415 | private: |
416 | CodeGenFunction &CGF; |
417 | CGCapturedStmtInfo *PrevCapturedStmtInfo; |
418 | public: |
419 | CGCapturedStmtRAII(CodeGenFunction &CGF, |
420 | CGCapturedStmtInfo *NewCapturedStmtInfo) |
421 | : CGF(CGF), PrevCapturedStmtInfo(CGF.CapturedStmtInfo) { |
422 | CGF.CapturedStmtInfo = NewCapturedStmtInfo; |
423 | } |
424 | ~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; } |
425 | }; |
426 | |
427 | /// An abstract representation of regular/ObjC call/message targets. |
428 | class AbstractCallee { |
429 | /// The function declaration of the callee. |
430 | const Decl *CalleeDecl; |
431 | |
432 | public: |
433 | AbstractCallee() : CalleeDecl(nullptr) {} |
434 | AbstractCallee(const FunctionDecl *FD) : CalleeDecl(FD) {} |
435 | AbstractCallee(const ObjCMethodDecl *OMD) : CalleeDecl(OMD) {} |
436 | bool hasFunctionDecl() const { |
437 | return dyn_cast_or_null<FunctionDecl>(CalleeDecl); |
438 | } |
439 | const Decl *getDecl() const { return CalleeDecl; } |
440 | unsigned getNumParams() const { |
441 | if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) |
442 | return FD->getNumParams(); |
443 | return cast<ObjCMethodDecl>(CalleeDecl)->param_size(); |
444 | } |
445 | const ParmVarDecl *getParamDecl(unsigned I) const { |
446 | if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) |
447 | return FD->getParamDecl(I); |
448 | return *(cast<ObjCMethodDecl>(CalleeDecl)->param_begin() + I); |
449 | } |
450 | }; |
451 | |
452 | /// Sanitizers enabled for this function. |
453 | SanitizerSet SanOpts; |
454 | |
455 | /// True if CodeGen currently emits code implementing sanitizer checks. |
456 | bool IsSanitizerScope = false; |
457 | |
458 | /// RAII object to set/unset CodeGenFunction::IsSanitizerScope. |
459 | class SanitizerScope { |
460 | CodeGenFunction *CGF; |
461 | public: |
462 | SanitizerScope(CodeGenFunction *CGF); |
463 | ~SanitizerScope(); |
464 | }; |
465 | |
466 | /// In C++, whether we are code generating a thunk. This controls whether we |
467 | /// should emit cleanups. |
468 | bool CurFuncIsThunk = false; |
469 | |
470 | /// In ARC, whether we should autorelease the return value. |
471 | bool AutoreleaseResult = false; |
472 | |
473 | /// Whether we processed a Microsoft-style asm block during CodeGen. These can |
474 | /// potentially set the return value. |
475 | bool SawAsmBlock = false; |
476 | |
477 | const NamedDecl *CurSEHParent = nullptr; |
478 | |
479 | /// True if the current function is an outlined SEH helper. This can be a |
480 | /// finally block or filter expression. |
481 | bool IsOutlinedSEHHelper = false; |
482 | |
483 | /// True if CodeGen currently emits code inside presereved access index |
484 | /// region. |
485 | bool IsInPreservedAIRegion = false; |
486 | |
487 | const CodeGen::CGBlockInfo *BlockInfo = nullptr; |
488 | llvm::Value *BlockPointer = nullptr; |
489 | |
490 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
491 | FieldDecl *LambdaThisCaptureField = nullptr; |
492 | |
493 | /// A mapping from NRVO variables to the flags used to indicate |
494 | /// when the NRVO has been applied to this variable. |
495 | llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags; |
496 | |
497 | EHScopeStack EHStack; |
498 | llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack; |
499 | llvm::SmallVector<const JumpDest *, 2> SEHTryEpilogueStack; |
500 | |
501 | llvm::Instruction *CurrentFuncletPad = nullptr; |
502 | |
503 | class CallLifetimeEnd final : public EHScopeStack::Cleanup { |
504 | llvm::Value *Addr; |
505 | llvm::Value *Size; |
506 | |
507 | public: |
508 | CallLifetimeEnd(Address addr, llvm::Value *size) |
509 | : Addr(addr.getPointer()), Size(size) {} |
510 | |
511 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
512 | CGF.EmitLifetimeEnd(Size, Addr); |
513 | } |
514 | }; |
515 | |
516 | /// Header for data within LifetimeExtendedCleanupStack. |
517 | struct LifetimeExtendedCleanupHeader { |
518 | /// The size of the following cleanup object. |
519 | unsigned Size; |
520 | /// The kind of cleanup to push: a value from the CleanupKind enumeration. |
521 | unsigned Kind : 31; |
522 | /// Whether this is a conditional cleanup. |
523 | unsigned IsConditional : 1; |
524 | |
525 | size_t getSize() const { return Size; } |
526 | CleanupKind getKind() const { return (CleanupKind)Kind; } |
527 | bool isConditional() const { return IsConditional; } |
528 | }; |
529 | |
530 | /// i32s containing the indexes of the cleanup destinations. |
531 | Address NormalCleanupDest = Address::invalid(); |
532 | |
533 | unsigned NextCleanupDestIndex = 1; |
534 | |
535 | /// FirstBlockInfo - The head of a singly-linked-list of block layouts. |
536 | CGBlockInfo *FirstBlockInfo = nullptr; |
537 | |
538 | /// EHResumeBlock - Unified block containing a call to llvm.eh.resume. |
539 | llvm::BasicBlock *EHResumeBlock = nullptr; |
540 | |
541 | /// The exception slot. All landing pads write the current exception pointer |
542 | /// into this alloca. |
543 | llvm::Value *ExceptionSlot = nullptr; |
544 | |
545 | /// The selector slot. Under the MandatoryCleanup model, all landing pads |
546 | /// write the current selector value into this alloca. |
547 | llvm::AllocaInst *EHSelectorSlot = nullptr; |
548 | |
549 | /// A stack of exception code slots. Entering an __except block pushes a slot |
550 | /// on the stack and leaving pops one. The __exception_code() intrinsic loads |
551 | /// a value from the top of the stack. |
552 | SmallVector<Address, 1> SEHCodeSlotStack; |
553 | |
554 | /// Value returned by __exception_info intrinsic. |
555 | llvm::Value *SEHInfo = nullptr; |
556 | |
557 | /// Emits a landing pad for the current EH stack. |
558 | llvm::BasicBlock *EmitLandingPad(); |
559 | |
560 | llvm::BasicBlock *getInvokeDestImpl(); |
561 | |
562 | template <class T> |
563 | typename DominatingValue<T>::saved_type saveValueInCond(T value) { |
564 | return DominatingValue<T>::save(*this, value); |
565 | } |
566 | |
567 | public: |
568 | /// ObjCEHValueStack - Stack of Objective-C exception values, used for |
569 | /// rethrows. |
570 | SmallVector<llvm::Value*, 8> ObjCEHValueStack; |
571 | |
572 | /// A class controlling the emission of a finally block. |
573 | class FinallyInfo { |
574 | /// Where the catchall's edge through the cleanup should go. |
575 | JumpDest RethrowDest; |
576 | |
577 | /// A function to call to enter the catch. |
578 | llvm::FunctionCallee BeginCatchFn; |
579 | |
580 | /// An i1 variable indicating whether or not the @finally is |
581 | /// running for an exception. |
582 | llvm::AllocaInst *ForEHVar; |
583 | |
584 | /// An i8* variable into which the exception pointer to rethrow |
585 | /// has been saved. |
586 | llvm::AllocaInst *SavedExnVar; |
587 | |
588 | public: |
589 | void enter(CodeGenFunction &CGF, const Stmt *Finally, |
590 | llvm::FunctionCallee beginCatchFn, |
591 | llvm::FunctionCallee endCatchFn, llvm::FunctionCallee rethrowFn); |
592 | void exit(CodeGenFunction &CGF); |
593 | }; |
594 | |
595 | /// Returns true inside SEH __try blocks. |
596 | bool isSEHTryScope() const { return !SEHTryEpilogueStack.empty(); } |
597 | |
598 | /// Returns true while emitting a cleanuppad. |
599 | bool isCleanupPadScope() const { |
600 | return CurrentFuncletPad && isa<llvm::CleanupPadInst>(CurrentFuncletPad); |
601 | } |
602 | |
603 | /// pushFullExprCleanup - Push a cleanup to be run at the end of the |
604 | /// current full-expression. Safe against the possibility that |
605 | /// we're currently inside a conditionally-evaluated expression. |
606 | template <class T, class... As> |
607 | void pushFullExprCleanup(CleanupKind kind, As... A) { |
608 | // If we're not in a conditional branch, or if none of the |
609 | // arguments requires saving, then use the unconditional cleanup. |
610 | if (!isInConditionalBranch()) |
611 | return EHStack.pushCleanup<T>(kind, A...); |
612 | |
613 | // Stash values in a tuple so we can guarantee the order of saves. |
614 | typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple; |
615 | SavedTuple Saved{saveValueInCond(A)...}; |
616 | |
617 | typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType; |
618 | EHStack.pushCleanupTuple<CleanupType>(kind, Saved); |
619 | initFullExprCleanup(); |
620 | } |
621 | |
622 | /// Queue a cleanup to be pushed after finishing the current |
623 | /// full-expression. |
624 | template <class T, class... As> |
625 | void pushCleanupAfterFullExpr(CleanupKind Kind, As... A) { |
626 | if (!isInConditionalBranch()) |
627 | return pushCleanupAfterFullExprImpl<T>(Kind, Address::invalid(), A...); |
628 | |
629 | Address ActiveFlag = createCleanupActiveFlag(); |
630 | assert(!DominatingValue<Address>::needsSaving(ActiveFlag) &&((!DominatingValue<Address>::needsSaving(ActiveFlag) && "cleanup active flag should never need saving") ? static_cast <void> (0) : __assert_fail ("!DominatingValue<Address>::needsSaving(ActiveFlag) && \"cleanup active flag should never need saving\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 631, __PRETTY_FUNCTION__)) |
631 | "cleanup active flag should never need saving")((!DominatingValue<Address>::needsSaving(ActiveFlag) && "cleanup active flag should never need saving") ? static_cast <void> (0) : __assert_fail ("!DominatingValue<Address>::needsSaving(ActiveFlag) && \"cleanup active flag should never need saving\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 631, __PRETTY_FUNCTION__)); |
632 | |
633 | typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple; |
634 | SavedTuple Saved{saveValueInCond(A)...}; |
635 | |
636 | typedef EHScopeStack::ConditionalCleanup<T, As...> CleanupType; |
637 | pushCleanupAfterFullExprImpl<CleanupType>(Kind, ActiveFlag, Saved); |
638 | } |
639 | |
640 | template <class T, class... As> |
641 | void pushCleanupAfterFullExprImpl(CleanupKind Kind, Address ActiveFlag, |
642 | As... A) { |
643 | LifetimeExtendedCleanupHeader Header = {sizeof(T), Kind, |
644 | ActiveFlag.isValid()}; |
645 | |
646 | size_t OldSize = LifetimeExtendedCleanupStack.size(); |
647 | LifetimeExtendedCleanupStack.resize( |
648 | LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size + |
649 | (Header.IsConditional ? sizeof(ActiveFlag) : 0)); |
650 | |
651 | static_assert(sizeof(Header) % alignof(T) == 0, |
652 | "Cleanup will be allocated on misaligned address"); |
653 | char *Buffer = &LifetimeExtendedCleanupStack[OldSize]; |
654 | new (Buffer) LifetimeExtendedCleanupHeader(Header); |
655 | new (Buffer + sizeof(Header)) T(A...); |
656 | if (Header.IsConditional) |
657 | new (Buffer + sizeof(Header) + sizeof(T)) Address(ActiveFlag); |
658 | } |
659 | |
660 | /// Set up the last cleanup that was pushed as a conditional |
661 | /// full-expression cleanup. |
662 | void initFullExprCleanup() { |
663 | initFullExprCleanupWithFlag(createCleanupActiveFlag()); |
664 | } |
665 | |
666 | void initFullExprCleanupWithFlag(Address ActiveFlag); |
667 | Address createCleanupActiveFlag(); |
668 | |
669 | /// PushDestructorCleanup - Push a cleanup to call the |
670 | /// complete-object destructor of an object of the given type at the |
671 | /// given address. Does nothing if T is not a C++ class type with a |
672 | /// non-trivial destructor. |
673 | void PushDestructorCleanup(QualType T, Address Addr); |
674 | |
675 | /// PushDestructorCleanup - Push a cleanup to call the |
676 | /// complete-object variant of the given destructor on the object at |
677 | /// the given address. |
678 | void PushDestructorCleanup(const CXXDestructorDecl *Dtor, QualType T, |
679 | Address Addr); |
680 | |
681 | /// PopCleanupBlock - Will pop the cleanup entry on the stack and |
682 | /// process all branch fixups. |
683 | void PopCleanupBlock(bool FallThroughIsBranchThrough = false); |
684 | |
685 | /// DeactivateCleanupBlock - Deactivates the given cleanup block. |
686 | /// The block cannot be reactivated. Pops it if it's the top of the |
687 | /// stack. |
688 | /// |
689 | /// \param DominatingIP - An instruction which is known to |
690 | /// dominate the current IP (if set) and which lies along |
691 | /// all paths of execution between the current IP and the |
692 | /// the point at which the cleanup comes into scope. |
693 | void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, |
694 | llvm::Instruction *DominatingIP); |
695 | |
696 | /// ActivateCleanupBlock - Activates an initially-inactive cleanup. |
697 | /// Cannot be used to resurrect a deactivated cleanup. |
698 | /// |
699 | /// \param DominatingIP - An instruction which is known to |
700 | /// dominate the current IP (if set) and which lies along |
701 | /// all paths of execution between the current IP and the |
702 | /// the point at which the cleanup comes into scope. |
703 | void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, |
704 | llvm::Instruction *DominatingIP); |
705 | |
706 | /// Enters a new scope for capturing cleanups, all of which |
707 | /// will be executed once the scope is exited. |
708 | class RunCleanupsScope { |
709 | EHScopeStack::stable_iterator CleanupStackDepth, OldCleanupScopeDepth; |
710 | size_t LifetimeExtendedCleanupStackSize; |
711 | bool OldDidCallStackSave; |
712 | protected: |
713 | bool PerformCleanup; |
714 | private: |
715 | |
716 | RunCleanupsScope(const RunCleanupsScope &) = delete; |
717 | void operator=(const RunCleanupsScope &) = delete; |
718 | |
719 | protected: |
720 | CodeGenFunction& CGF; |
721 | |
722 | public: |
723 | /// Enter a new cleanup scope. |
724 | explicit RunCleanupsScope(CodeGenFunction &CGF) |
725 | : PerformCleanup(true), CGF(CGF) |
726 | { |
727 | CleanupStackDepth = CGF.EHStack.stable_begin(); |
728 | LifetimeExtendedCleanupStackSize = |
729 | CGF.LifetimeExtendedCleanupStack.size(); |
730 | OldDidCallStackSave = CGF.DidCallStackSave; |
731 | CGF.DidCallStackSave = false; |
732 | OldCleanupScopeDepth = CGF.CurrentCleanupScopeDepth; |
733 | CGF.CurrentCleanupScopeDepth = CleanupStackDepth; |
734 | } |
735 | |
736 | /// Exit this cleanup scope, emitting any accumulated cleanups. |
737 | ~RunCleanupsScope() { |
738 | if (PerformCleanup) |
739 | ForceCleanup(); |
740 | } |
741 | |
742 | /// Determine whether this scope requires any cleanups. |
743 | bool requiresCleanups() const { |
744 | return CGF.EHStack.stable_begin() != CleanupStackDepth; |
745 | } |
746 | |
747 | /// Force the emission of cleanups now, instead of waiting |
748 | /// until this object is destroyed. |
749 | /// \param ValuesToReload - A list of values that need to be available at |
750 | /// the insertion point after cleanup emission. If cleanup emission created |
751 | /// a shared cleanup block, these value pointers will be rewritten. |
752 | /// Otherwise, they not will be modified. |
753 | void ForceCleanup(std::initializer_list<llvm::Value**> ValuesToReload = {}) { |
754 | assert(PerformCleanup && "Already forced cleanup")((PerformCleanup && "Already forced cleanup") ? static_cast <void> (0) : __assert_fail ("PerformCleanup && \"Already forced cleanup\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 754, __PRETTY_FUNCTION__)); |
755 | CGF.DidCallStackSave = OldDidCallStackSave; |
756 | CGF.PopCleanupBlocks(CleanupStackDepth, LifetimeExtendedCleanupStackSize, |
757 | ValuesToReload); |
758 | PerformCleanup = false; |
759 | CGF.CurrentCleanupScopeDepth = OldCleanupScopeDepth; |
760 | } |
761 | }; |
762 | |
763 | // Cleanup stack depth of the RunCleanupsScope that was pushed most recently. |
764 | EHScopeStack::stable_iterator CurrentCleanupScopeDepth = |
765 | EHScopeStack::stable_end(); |
766 | |
767 | class LexicalScope : public RunCleanupsScope { |
768 | SourceRange Range; |
769 | SmallVector<const LabelDecl*, 4> Labels; |
770 | LexicalScope *ParentScope; |
771 | |
772 | LexicalScope(const LexicalScope &) = delete; |
773 | void operator=(const LexicalScope &) = delete; |
774 | |
775 | public: |
776 | /// Enter a new cleanup scope. |
777 | explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range) |
778 | : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) { |
779 | CGF.CurLexicalScope = this; |
780 | if (CGDebugInfo *DI = CGF.getDebugInfo()) |
781 | DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin()); |
782 | } |
783 | |
784 | void addLabel(const LabelDecl *label) { |
785 | assert(PerformCleanup && "adding label to dead scope?")((PerformCleanup && "adding label to dead scope?") ? static_cast <void> (0) : __assert_fail ("PerformCleanup && \"adding label to dead scope?\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 785, __PRETTY_FUNCTION__)); |
786 | Labels.push_back(label); |
787 | } |
788 | |
789 | /// Exit this cleanup scope, emitting any accumulated |
790 | /// cleanups. |
791 | ~LexicalScope() { |
792 | if (CGDebugInfo *DI = CGF.getDebugInfo()) |
793 | DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd()); |
794 | |
795 | // If we should perform a cleanup, force them now. Note that |
796 | // this ends the cleanup scope before rescoping any labels. |
797 | if (PerformCleanup) { |
798 | ApplyDebugLocation DL(CGF, Range.getEnd()); |
799 | ForceCleanup(); |
800 | } |
801 | } |
802 | |
803 | /// Force the emission of cleanups now, instead of waiting |
804 | /// until this object is destroyed. |
805 | void ForceCleanup() { |
806 | CGF.CurLexicalScope = ParentScope; |
807 | RunCleanupsScope::ForceCleanup(); |
808 | |
809 | if (!Labels.empty()) |
810 | rescopeLabels(); |
811 | } |
812 | |
813 | bool hasLabels() const { |
814 | return !Labels.empty(); |
815 | } |
816 | |
817 | void rescopeLabels(); |
818 | }; |
819 | |
820 | typedef llvm::DenseMap<const Decl *, Address> DeclMapTy; |
821 | |
822 | /// The class used to assign some variables some temporarily addresses. |
823 | class OMPMapVars { |
824 | DeclMapTy SavedLocals; |
825 | DeclMapTy SavedTempAddresses; |
826 | OMPMapVars(const OMPMapVars &) = delete; |
827 | void operator=(const OMPMapVars &) = delete; |
828 | |
829 | public: |
830 | explicit OMPMapVars() = default; |
831 | ~OMPMapVars() { |
832 | assert(SavedLocals.empty() && "Did not restored original addresses.")((SavedLocals.empty() && "Did not restored original addresses." ) ? static_cast<void> (0) : __assert_fail ("SavedLocals.empty() && \"Did not restored original addresses.\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 832, __PRETTY_FUNCTION__)); |
833 | }; |
834 | |
835 | /// Sets the address of the variable \p LocalVD to be \p TempAddr in |
836 | /// function \p CGF. |
837 | /// \return true if at least one variable was set already, false otherwise. |
838 | bool setVarAddr(CodeGenFunction &CGF, const VarDecl *LocalVD, |
839 | Address TempAddr) { |
840 | LocalVD = LocalVD->getCanonicalDecl(); |
841 | // Only save it once. |
842 | if (SavedLocals.count(LocalVD)) return false; |
843 | |
844 | // Copy the existing local entry to SavedLocals. |
845 | auto it = CGF.LocalDeclMap.find(LocalVD); |
846 | if (it != CGF.LocalDeclMap.end()) |
847 | SavedLocals.try_emplace(LocalVD, it->second); |
848 | else |
849 | SavedLocals.try_emplace(LocalVD, Address::invalid()); |
850 | |
851 | // Generate the private entry. |
852 | QualType VarTy = LocalVD->getType(); |
853 | if (VarTy->isReferenceType()) { |
854 | Address Temp = CGF.CreateMemTemp(VarTy); |
855 | CGF.Builder.CreateStore(TempAddr.getPointer(), Temp); |
856 | TempAddr = Temp; |
857 | } |
858 | SavedTempAddresses.try_emplace(LocalVD, TempAddr); |
859 | |
860 | return true; |
861 | } |
862 | |
863 | /// Applies new addresses to the list of the variables. |
864 | /// \return true if at least one variable is using new address, false |
865 | /// otherwise. |
866 | bool apply(CodeGenFunction &CGF) { |
867 | copyInto(SavedTempAddresses, CGF.LocalDeclMap); |
868 | SavedTempAddresses.clear(); |
869 | return !SavedLocals.empty(); |
870 | } |
871 | |
872 | /// Restores original addresses of the variables. |
873 | void restore(CodeGenFunction &CGF) { |
874 | if (!SavedLocals.empty()) { |
875 | copyInto(SavedLocals, CGF.LocalDeclMap); |
876 | SavedLocals.clear(); |
877 | } |
878 | } |
879 | |
880 | private: |
881 | /// Copy all the entries in the source map over the corresponding |
882 | /// entries in the destination, which must exist. |
883 | static void copyInto(const DeclMapTy &Src, DeclMapTy &Dest) { |
884 | for (auto &Pair : Src) { |
885 | if (!Pair.second.isValid()) { |
886 | Dest.erase(Pair.first); |
887 | continue; |
888 | } |
889 | |
890 | auto I = Dest.find(Pair.first); |
891 | if (I != Dest.end()) |
892 | I->second = Pair.second; |
893 | else |
894 | Dest.insert(Pair); |
895 | } |
896 | } |
897 | }; |
898 | |
899 | /// The scope used to remap some variables as private in the OpenMP loop body |
900 | /// (or other captured region emitted without outlining), and to restore old |
901 | /// vars back on exit. |
902 | class OMPPrivateScope : public RunCleanupsScope { |
903 | OMPMapVars MappedVars; |
904 | OMPPrivateScope(const OMPPrivateScope &) = delete; |
905 | void operator=(const OMPPrivateScope &) = delete; |
906 | |
907 | public: |
908 | /// Enter a new OpenMP private scope. |
909 | explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {} |
910 | |
911 | /// Registers \p LocalVD variable as a private and apply \p PrivateGen |
912 | /// function for it to generate corresponding private variable. \p |
913 | /// PrivateGen returns an address of the generated private variable. |
914 | /// \return true if the variable is registered as private, false if it has |
915 | /// been privatized already. |
916 | bool addPrivate(const VarDecl *LocalVD, |
917 | const llvm::function_ref<Address()> PrivateGen) { |
918 | assert(PerformCleanup && "adding private to dead scope")((PerformCleanup && "adding private to dead scope") ? static_cast<void> (0) : __assert_fail ("PerformCleanup && \"adding private to dead scope\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 918, __PRETTY_FUNCTION__)); |
919 | return MappedVars.setVarAddr(CGF, LocalVD, PrivateGen()); |
920 | } |
921 | |
922 | /// Privatizes local variables previously registered as private. |
923 | /// Registration is separate from the actual privatization to allow |
924 | /// initializers use values of the original variables, not the private one. |
925 | /// This is important, for example, if the private variable is a class |
926 | /// variable initialized by a constructor that references other private |
927 | /// variables. But at initialization original variables must be used, not |
928 | /// private copies. |
929 | /// \return true if at least one variable was privatized, false otherwise. |
930 | bool Privatize() { return MappedVars.apply(CGF); } |
931 | |
932 | void ForceCleanup() { |
933 | RunCleanupsScope::ForceCleanup(); |
934 | MappedVars.restore(CGF); |
935 | } |
936 | |
937 | /// Exit scope - all the mapped variables are restored. |
938 | ~OMPPrivateScope() { |
939 | if (PerformCleanup) |
940 | ForceCleanup(); |
941 | } |
942 | |
943 | /// Checks if the global variable is captured in current function. |
944 | bool isGlobalVarCaptured(const VarDecl *VD) const { |
945 | VD = VD->getCanonicalDecl(); |
946 | return !VD->isLocalVarDeclOrParm() && CGF.LocalDeclMap.count(VD) > 0; |
947 | } |
948 | }; |
949 | |
950 | /// Takes the old cleanup stack size and emits the cleanup blocks |
951 | /// that have been added. |
952 | void |
953 | PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, |
954 | std::initializer_list<llvm::Value **> ValuesToReload = {}); |
955 | |
956 | /// Takes the old cleanup stack size and emits the cleanup blocks |
957 | /// that have been added, then adds all lifetime-extended cleanups from |
958 | /// the given position to the stack. |
959 | void |
960 | PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize, |
961 | size_t OldLifetimeExtendedStackSize, |
962 | std::initializer_list<llvm::Value **> ValuesToReload = {}); |
963 | |
964 | void ResolveBranchFixups(llvm::BasicBlock *Target); |
965 | |
966 | /// The given basic block lies in the current EH scope, but may be a |
967 | /// target of a potentially scope-crossing jump; get a stable handle |
968 | /// to which we can perform this jump later. |
969 | JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) { |
970 | return JumpDest(Target, |
971 | EHStack.getInnermostNormalCleanup(), |
972 | NextCleanupDestIndex++); |
973 | } |
974 | |
975 | /// The given basic block lies in the current EH scope, but may be a |
976 | /// target of a potentially scope-crossing jump; get a stable handle |
977 | /// to which we can perform this jump later. |
978 | JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) { |
979 | return getJumpDestInCurrentScope(createBasicBlock(Name)); |
980 | } |
981 | |
982 | /// EmitBranchThroughCleanup - Emit a branch from the current insert |
983 | /// block through the normal cleanup handling code (if any) and then |
984 | /// on to \arg Dest. |
985 | void EmitBranchThroughCleanup(JumpDest Dest); |
986 | |
987 | /// isObviouslyBranchWithoutCleanups - Return true if a branch to the |
988 | /// specified destination obviously has no cleanups to run. 'false' is always |
989 | /// a conservatively correct answer for this method. |
990 | bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const; |
991 | |
992 | /// popCatchScope - Pops the catch scope at the top of the EHScope |
993 | /// stack, emitting any required code (other than the catch handlers |
994 | /// themselves). |
995 | void popCatchScope(); |
996 | |
997 | llvm::BasicBlock *getEHResumeBlock(bool isCleanup); |
998 | llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope); |
999 | llvm::BasicBlock * |
1000 | getFuncletEHDispatchBlock(EHScopeStack::stable_iterator scope); |
1001 | |
1002 | /// An object to manage conditionally-evaluated expressions. |
1003 | class ConditionalEvaluation { |
1004 | llvm::BasicBlock *StartBB; |
1005 | |
1006 | public: |
1007 | ConditionalEvaluation(CodeGenFunction &CGF) |
1008 | : StartBB(CGF.Builder.GetInsertBlock()) {} |
1009 | |
1010 | void begin(CodeGenFunction &CGF) { |
1011 | assert(CGF.OutermostConditional != this)((CGF.OutermostConditional != this) ? static_cast<void> (0) : __assert_fail ("CGF.OutermostConditional != this", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1011, __PRETTY_FUNCTION__)); |
1012 | if (!CGF.OutermostConditional) |
1013 | CGF.OutermostConditional = this; |
1014 | } |
1015 | |
1016 | void end(CodeGenFunction &CGF) { |
1017 | assert(CGF.OutermostConditional != nullptr)((CGF.OutermostConditional != nullptr) ? static_cast<void> (0) : __assert_fail ("CGF.OutermostConditional != nullptr", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1017, __PRETTY_FUNCTION__)); |
1018 | if (CGF.OutermostConditional == this) |
1019 | CGF.OutermostConditional = nullptr; |
1020 | } |
1021 | |
1022 | /// Returns a block which will be executed prior to each |
1023 | /// evaluation of the conditional code. |
1024 | llvm::BasicBlock *getStartingBlock() const { |
1025 | return StartBB; |
1026 | } |
1027 | }; |
1028 | |
1029 | /// isInConditionalBranch - Return true if we're currently emitting |
1030 | /// one branch or the other of a conditional expression. |
1031 | bool isInConditionalBranch() const { return OutermostConditional != nullptr; } |
1032 | |
1033 | void setBeforeOutermostConditional(llvm::Value *value, Address addr) { |
1034 | assert(isInConditionalBranch())((isInConditionalBranch()) ? static_cast<void> (0) : __assert_fail ("isInConditionalBranch()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1034, __PRETTY_FUNCTION__)); |
1035 | llvm::BasicBlock *block = OutermostConditional->getStartingBlock(); |
1036 | auto store = new llvm::StoreInst(value, addr.getPointer(), &block->back()); |
1037 | store->setAlignment(addr.getAlignment().getQuantity()); |
1038 | } |
1039 | |
1040 | /// An RAII object to record that we're evaluating a statement |
1041 | /// expression. |
1042 | class StmtExprEvaluation { |
1043 | CodeGenFunction &CGF; |
1044 | |
1045 | /// We have to save the outermost conditional: cleanups in a |
1046 | /// statement expression aren't conditional just because the |
1047 | /// StmtExpr is. |
1048 | ConditionalEvaluation *SavedOutermostConditional; |
1049 | |
1050 | public: |
1051 | StmtExprEvaluation(CodeGenFunction &CGF) |
1052 | : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) { |
1053 | CGF.OutermostConditional = nullptr; |
1054 | } |
1055 | |
1056 | ~StmtExprEvaluation() { |
1057 | CGF.OutermostConditional = SavedOutermostConditional; |
1058 | CGF.EnsureInsertPoint(); |
1059 | } |
1060 | }; |
1061 | |
1062 | /// An object which temporarily prevents a value from being |
1063 | /// destroyed by aggressive peephole optimizations that assume that |
1064 | /// all uses of a value have been realized in the IR. |
1065 | class PeepholeProtection { |
1066 | llvm::Instruction *Inst; |
1067 | friend class CodeGenFunction; |
1068 | |
1069 | public: |
1070 | PeepholeProtection() : Inst(nullptr) {} |
1071 | }; |
1072 | |
1073 | /// A non-RAII class containing all the information about a bound |
1074 | /// opaque value. OpaqueValueMapping, below, is a RAII wrapper for |
1075 | /// this which makes individual mappings very simple; using this |
1076 | /// class directly is useful when you have a variable number of |
1077 | /// opaque values or don't want the RAII functionality for some |
1078 | /// reason. |
1079 | class OpaqueValueMappingData { |
1080 | const OpaqueValueExpr *OpaqueValue; |
1081 | bool BoundLValue; |
1082 | CodeGenFunction::PeepholeProtection Protection; |
1083 | |
1084 | OpaqueValueMappingData(const OpaqueValueExpr *ov, |
1085 | bool boundLValue) |
1086 | : OpaqueValue(ov), BoundLValue(boundLValue) {} |
1087 | public: |
1088 | OpaqueValueMappingData() : OpaqueValue(nullptr) {} |
1089 | |
1090 | static bool shouldBindAsLValue(const Expr *expr) { |
1091 | // gl-values should be bound as l-values for obvious reasons. |
1092 | // Records should be bound as l-values because IR generation |
1093 | // always keeps them in memory. Expressions of function type |
1094 | // act exactly like l-values but are formally required to be |
1095 | // r-values in C. |
1096 | return expr->isGLValue() || |
1097 | expr->getType()->isFunctionType() || |
1098 | hasAggregateEvaluationKind(expr->getType()); |
1099 | } |
1100 | |
1101 | static OpaqueValueMappingData bind(CodeGenFunction &CGF, |
1102 | const OpaqueValueExpr *ov, |
1103 | const Expr *e) { |
1104 | if (shouldBindAsLValue(ov)) |
1105 | return bind(CGF, ov, CGF.EmitLValue(e)); |
1106 | return bind(CGF, ov, CGF.EmitAnyExpr(e)); |
1107 | } |
1108 | |
1109 | static OpaqueValueMappingData bind(CodeGenFunction &CGF, |
1110 | const OpaqueValueExpr *ov, |
1111 | const LValue &lv) { |
1112 | assert(shouldBindAsLValue(ov))((shouldBindAsLValue(ov)) ? static_cast<void> (0) : __assert_fail ("shouldBindAsLValue(ov)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1112, __PRETTY_FUNCTION__)); |
1113 | CGF.OpaqueLValues.insert(std::make_pair(ov, lv)); |
1114 | return OpaqueValueMappingData(ov, true); |
1115 | } |
1116 | |
1117 | static OpaqueValueMappingData bind(CodeGenFunction &CGF, |
1118 | const OpaqueValueExpr *ov, |
1119 | const RValue &rv) { |
1120 | assert(!shouldBindAsLValue(ov))((!shouldBindAsLValue(ov)) ? static_cast<void> (0) : __assert_fail ("!shouldBindAsLValue(ov)", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1120, __PRETTY_FUNCTION__)); |
1121 | CGF.OpaqueRValues.insert(std::make_pair(ov, rv)); |
1122 | |
1123 | OpaqueValueMappingData data(ov, false); |
1124 | |
1125 | // Work around an extremely aggressive peephole optimization in |
1126 | // EmitScalarConversion which assumes that all other uses of a |
1127 | // value are extant. |
1128 | data.Protection = CGF.protectFromPeepholes(rv); |
1129 | |
1130 | return data; |
1131 | } |
1132 | |
1133 | bool isValid() const { return OpaqueValue != nullptr; } |
1134 | void clear() { OpaqueValue = nullptr; } |
1135 | |
1136 | void unbind(CodeGenFunction &CGF) { |
1137 | assert(OpaqueValue && "no data to unbind!")((OpaqueValue && "no data to unbind!") ? static_cast< void> (0) : __assert_fail ("OpaqueValue && \"no data to unbind!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1137, __PRETTY_FUNCTION__)); |
1138 | |
1139 | if (BoundLValue) { |
1140 | CGF.OpaqueLValues.erase(OpaqueValue); |
1141 | } else { |
1142 | CGF.OpaqueRValues.erase(OpaqueValue); |
1143 | CGF.unprotectFromPeepholes(Protection); |
1144 | } |
1145 | } |
1146 | }; |
1147 | |
1148 | /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr. |
1149 | class OpaqueValueMapping { |
1150 | CodeGenFunction &CGF; |
1151 | OpaqueValueMappingData Data; |
1152 | |
1153 | public: |
1154 | static bool shouldBindAsLValue(const Expr *expr) { |
1155 | return OpaqueValueMappingData::shouldBindAsLValue(expr); |
1156 | } |
1157 | |
1158 | /// Build the opaque value mapping for the given conditional |
1159 | /// operator if it's the GNU ?: extension. This is a common |
1160 | /// enough pattern that the convenience operator is really |
1161 | /// helpful. |
1162 | /// |
1163 | OpaqueValueMapping(CodeGenFunction &CGF, |
1164 | const AbstractConditionalOperator *op) : CGF(CGF) { |
1165 | if (isa<ConditionalOperator>(op)) |
1166 | // Leave Data empty. |
1167 | return; |
1168 | |
1169 | const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op); |
1170 | Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(), |
1171 | e->getCommon()); |
1172 | } |
1173 | |
1174 | /// Build the opaque value mapping for an OpaqueValueExpr whose source |
1175 | /// expression is set to the expression the OVE represents. |
1176 | OpaqueValueMapping(CodeGenFunction &CGF, const OpaqueValueExpr *OV) |
1177 | : CGF(CGF) { |
1178 | if (OV) { |
1179 | assert(OV->getSourceExpr() && "wrong form of OpaqueValueMapping used "((OV->getSourceExpr() && "wrong form of OpaqueValueMapping used " "for OVE with no source expression") ? static_cast<void> (0) : __assert_fail ("OV->getSourceExpr() && \"wrong form of OpaqueValueMapping used \" \"for OVE with no source expression\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1180, __PRETTY_FUNCTION__)) |
1180 | "for OVE with no source expression")((OV->getSourceExpr() && "wrong form of OpaqueValueMapping used " "for OVE with no source expression") ? static_cast<void> (0) : __assert_fail ("OV->getSourceExpr() && \"wrong form of OpaqueValueMapping used \" \"for OVE with no source expression\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1180, __PRETTY_FUNCTION__)); |
1181 | Data = OpaqueValueMappingData::bind(CGF, OV, OV->getSourceExpr()); |
1182 | } |
1183 | } |
1184 | |
1185 | OpaqueValueMapping(CodeGenFunction &CGF, |
1186 | const OpaqueValueExpr *opaqueValue, |
1187 | LValue lvalue) |
1188 | : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) { |
1189 | } |
1190 | |
1191 | OpaqueValueMapping(CodeGenFunction &CGF, |
1192 | const OpaqueValueExpr *opaqueValue, |
1193 | RValue rvalue) |
1194 | : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) { |
1195 | } |
1196 | |
1197 | void pop() { |
1198 | Data.unbind(CGF); |
1199 | Data.clear(); |
1200 | } |
1201 | |
1202 | ~OpaqueValueMapping() { |
1203 | if (Data.isValid()) Data.unbind(CGF); |
1204 | } |
1205 | }; |
1206 | |
1207 | private: |
1208 | CGDebugInfo *DebugInfo; |
1209 | /// Used to create unique names for artificial VLA size debug info variables. |
1210 | unsigned VLAExprCounter = 0; |
1211 | bool DisableDebugInfo = false; |
1212 | |
1213 | /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid |
1214 | /// calling llvm.stacksave for multiple VLAs in the same scope. |
1215 | bool DidCallStackSave = false; |
1216 | |
1217 | /// IndirectBranch - The first time an indirect goto is seen we create a block |
1218 | /// with an indirect branch. Every time we see the address of a label taken, |
1219 | /// we add the label to the indirect goto. Every subsequent indirect goto is |
1220 | /// codegen'd as a jump to the IndirectBranch's basic block. |
1221 | llvm::IndirectBrInst *IndirectBranch = nullptr; |
1222 | |
1223 | /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C |
1224 | /// decls. |
1225 | DeclMapTy LocalDeclMap; |
1226 | |
1227 | // Keep track of the cleanups for callee-destructed parameters pushed to the |
1228 | // cleanup stack so that they can be deactivated later. |
1229 | llvm::DenseMap<const ParmVarDecl *, EHScopeStack::stable_iterator> |
1230 | CalleeDestructedParamCleanups; |
1231 | |
1232 | /// SizeArguments - If a ParmVarDecl had the pass_object_size attribute, this |
1233 | /// will contain a mapping from said ParmVarDecl to its implicit "object_size" |
1234 | /// parameter. |
1235 | llvm::SmallDenseMap<const ParmVarDecl *, const ImplicitParamDecl *, 2> |
1236 | SizeArguments; |
1237 | |
1238 | /// Track escaped local variables with auto storage. Used during SEH |
1239 | /// outlining to produce a call to llvm.localescape. |
1240 | llvm::DenseMap<llvm::AllocaInst *, int> EscapedLocals; |
1241 | |
1242 | /// LabelMap - This keeps track of the LLVM basic block for each C label. |
1243 | llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap; |
1244 | |
1245 | // BreakContinueStack - This keeps track of where break and continue |
1246 | // statements should jump to. |
1247 | struct BreakContinue { |
1248 | BreakContinue(JumpDest Break, JumpDest Continue) |
1249 | : BreakBlock(Break), ContinueBlock(Continue) {} |
1250 | |
1251 | JumpDest BreakBlock; |
1252 | JumpDest ContinueBlock; |
1253 | }; |
1254 | SmallVector<BreakContinue, 8> BreakContinueStack; |
1255 | |
1256 | /// Handles cancellation exit points in OpenMP-related constructs. |
1257 | class OpenMPCancelExitStack { |
1258 | /// Tracks cancellation exit point and join point for cancel-related exit |
1259 | /// and normal exit. |
1260 | struct CancelExit { |
1261 | CancelExit() = default; |
1262 | CancelExit(OpenMPDirectiveKind Kind, JumpDest ExitBlock, |
1263 | JumpDest ContBlock) |
1264 | : Kind(Kind), ExitBlock(ExitBlock), ContBlock(ContBlock) {} |
1265 | OpenMPDirectiveKind Kind = OMPD_unknown; |
1266 | /// true if the exit block has been emitted already by the special |
1267 | /// emitExit() call, false if the default codegen is used. |
1268 | bool HasBeenEmitted = false; |
1269 | JumpDest ExitBlock; |
1270 | JumpDest ContBlock; |
1271 | }; |
1272 | |
1273 | SmallVector<CancelExit, 8> Stack; |
1274 | |
1275 | public: |
1276 | OpenMPCancelExitStack() : Stack(1) {} |
1277 | ~OpenMPCancelExitStack() = default; |
1278 | /// Fetches the exit block for the current OpenMP construct. |
1279 | JumpDest getExitBlock() const { return Stack.back().ExitBlock; } |
1280 | /// Emits exit block with special codegen procedure specific for the related |
1281 | /// OpenMP construct + emits code for normal construct cleanup. |
1282 | void emitExit(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, |
1283 | const llvm::function_ref<void(CodeGenFunction &)> CodeGen) { |
1284 | if (Stack.back().Kind == Kind && getExitBlock().isValid()) { |
1285 | assert(CGF.getOMPCancelDestination(Kind).isValid())((CGF.getOMPCancelDestination(Kind).isValid()) ? static_cast< void> (0) : __assert_fail ("CGF.getOMPCancelDestination(Kind).isValid()" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1285, __PRETTY_FUNCTION__)); |
1286 | assert(CGF.HaveInsertPoint())((CGF.HaveInsertPoint()) ? static_cast<void> (0) : __assert_fail ("CGF.HaveInsertPoint()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1286, __PRETTY_FUNCTION__)); |
1287 | assert(!Stack.back().HasBeenEmitted)((!Stack.back().HasBeenEmitted) ? static_cast<void> (0) : __assert_fail ("!Stack.back().HasBeenEmitted", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1287, __PRETTY_FUNCTION__)); |
1288 | auto IP = CGF.Builder.saveAndClearIP(); |
1289 | CGF.EmitBlock(Stack.back().ExitBlock.getBlock()); |
1290 | CodeGen(CGF); |
1291 | CGF.EmitBranch(Stack.back().ContBlock.getBlock()); |
1292 | CGF.Builder.restoreIP(IP); |
1293 | Stack.back().HasBeenEmitted = true; |
1294 | } |
1295 | CodeGen(CGF); |
1296 | } |
1297 | /// Enter the cancel supporting \a Kind construct. |
1298 | /// \param Kind OpenMP directive that supports cancel constructs. |
1299 | /// \param HasCancel true, if the construct has inner cancel directive, |
1300 | /// false otherwise. |
1301 | void enter(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, bool HasCancel) { |
1302 | Stack.push_back({Kind, |
1303 | HasCancel ? CGF.getJumpDestInCurrentScope("cancel.exit") |
1304 | : JumpDest(), |
1305 | HasCancel ? CGF.getJumpDestInCurrentScope("cancel.cont") |
1306 | : JumpDest()}); |
1307 | } |
1308 | /// Emits default exit point for the cancel construct (if the special one |
1309 | /// has not be used) + join point for cancel/normal exits. |
1310 | void exit(CodeGenFunction &CGF) { |
1311 | if (getExitBlock().isValid()) { |
1312 | assert(CGF.getOMPCancelDestination(Stack.back().Kind).isValid())((CGF.getOMPCancelDestination(Stack.back().Kind).isValid()) ? static_cast<void> (0) : __assert_fail ("CGF.getOMPCancelDestination(Stack.back().Kind).isValid()" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1312, __PRETTY_FUNCTION__)); |
1313 | bool HaveIP = CGF.HaveInsertPoint(); |
1314 | if (!Stack.back().HasBeenEmitted) { |
1315 | if (HaveIP) |
1316 | CGF.EmitBranchThroughCleanup(Stack.back().ContBlock); |
1317 | CGF.EmitBlock(Stack.back().ExitBlock.getBlock()); |
1318 | CGF.EmitBranchThroughCleanup(Stack.back().ContBlock); |
1319 | } |
1320 | CGF.EmitBlock(Stack.back().ContBlock.getBlock()); |
1321 | if (!HaveIP) { |
1322 | CGF.Builder.CreateUnreachable(); |
1323 | CGF.Builder.ClearInsertionPoint(); |
1324 | } |
1325 | } |
1326 | Stack.pop_back(); |
1327 | } |
1328 | }; |
1329 | OpenMPCancelExitStack OMPCancelStack; |
1330 | |
1331 | CodeGenPGO PGO; |
1332 | |
1333 | /// Calculate branch weights appropriate for PGO data |
1334 | llvm::MDNode *createProfileWeights(uint64_t TrueCount, uint64_t FalseCount); |
1335 | llvm::MDNode *createProfileWeights(ArrayRef<uint64_t> Weights); |
1336 | llvm::MDNode *createProfileWeightsForLoop(const Stmt *Cond, |
1337 | uint64_t LoopCount); |
1338 | |
1339 | public: |
1340 | /// Increment the profiler's counter for the given statement by \p StepV. |
1341 | /// If \p StepV is null, the default increment is 1. |
1342 | void incrementProfileCounter(const Stmt *S, llvm::Value *StepV = nullptr) { |
1343 | if (CGM.getCodeGenOpts().hasProfileClangInstr()) |
1344 | PGO.emitCounterIncrement(Builder, S, StepV); |
1345 | PGO.setCurrentStmt(S); |
1346 | } |
1347 | |
1348 | /// Get the profiler's count for the given statement. |
1349 | uint64_t getProfileCount(const Stmt *S) { |
1350 | Optional<uint64_t> Count = PGO.getStmtCount(S); |
1351 | if (!Count.hasValue()) |
1352 | return 0; |
1353 | return *Count; |
1354 | } |
1355 | |
1356 | /// Set the profiler's current count. |
1357 | void setCurrentProfileCount(uint64_t Count) { |
1358 | PGO.setCurrentRegionCount(Count); |
1359 | } |
1360 | |
1361 | /// Get the profiler's current count. This is generally the count for the most |
1362 | /// recently incremented counter. |
1363 | uint64_t getCurrentProfileCount() { |
1364 | return PGO.getCurrentRegionCount(); |
1365 | } |
1366 | |
1367 | private: |
1368 | |
1369 | /// SwitchInsn - This is nearest current switch instruction. It is null if |
1370 | /// current context is not in a switch. |
1371 | llvm::SwitchInst *SwitchInsn = nullptr; |
1372 | /// The branch weights of SwitchInsn when doing instrumentation based PGO. |
1373 | SmallVector<uint64_t, 16> *SwitchWeights = nullptr; |
1374 | |
1375 | /// CaseRangeBlock - This block holds if condition check for last case |
1376 | /// statement range in current switch instruction. |
1377 | llvm::BasicBlock *CaseRangeBlock = nullptr; |
1378 | |
1379 | /// OpaqueLValues - Keeps track of the current set of opaque value |
1380 | /// expressions. |
1381 | llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues; |
1382 | llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues; |
1383 | |
1384 | // VLASizeMap - This keeps track of the associated size for each VLA type. |
1385 | // We track this by the size expression rather than the type itself because |
1386 | // in certain situations, like a const qualifier applied to an VLA typedef, |
1387 | // multiple VLA types can share the same size expression. |
1388 | // FIXME: Maybe this could be a stack of maps that is pushed/popped as we |
1389 | // enter/leave scopes. |
1390 | llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap; |
1391 | |
1392 | /// A block containing a single 'unreachable' instruction. Created |
1393 | /// lazily by getUnreachableBlock(). |
1394 | llvm::BasicBlock *UnreachableBlock = nullptr; |
1395 | |
1396 | /// Counts of the number return expressions in the function. |
1397 | unsigned NumReturnExprs = 0; |
1398 | |
1399 | /// Count the number of simple (constant) return expressions in the function. |
1400 | unsigned NumSimpleReturnExprs = 0; |
1401 | |
1402 | /// The last regular (non-return) debug location (breakpoint) in the function. |
1403 | SourceLocation LastStopPoint; |
1404 | |
1405 | public: |
1406 | /// Source location information about the default argument or member |
1407 | /// initializer expression we're evaluating, if any. |
1408 | CurrentSourceLocExprScope CurSourceLocExprScope; |
1409 | using SourceLocExprScopeGuard = |
1410 | CurrentSourceLocExprScope::SourceLocExprScopeGuard; |
1411 | |
1412 | /// A scope within which we are constructing the fields of an object which |
1413 | /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use |
1414 | /// if we need to evaluate a CXXDefaultInitExpr within the evaluation. |
1415 | class FieldConstructionScope { |
1416 | public: |
1417 | FieldConstructionScope(CodeGenFunction &CGF, Address This) |
1418 | : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) { |
1419 | CGF.CXXDefaultInitExprThis = This; |
1420 | } |
1421 | ~FieldConstructionScope() { |
1422 | CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis; |
1423 | } |
1424 | |
1425 | private: |
1426 | CodeGenFunction &CGF; |
1427 | Address OldCXXDefaultInitExprThis; |
1428 | }; |
1429 | |
1430 | /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this' |
1431 | /// is overridden to be the object under construction. |
1432 | class CXXDefaultInitExprScope { |
1433 | public: |
1434 | CXXDefaultInitExprScope(CodeGenFunction &CGF, const CXXDefaultInitExpr *E) |
1435 | : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue), |
1436 | OldCXXThisAlignment(CGF.CXXThisAlignment), |
1437 | SourceLocScope(E, CGF.CurSourceLocExprScope) { |
1438 | CGF.CXXThisValue = CGF.CXXDefaultInitExprThis.getPointer(); |
1439 | CGF.CXXThisAlignment = CGF.CXXDefaultInitExprThis.getAlignment(); |
1440 | } |
1441 | ~CXXDefaultInitExprScope() { |
1442 | CGF.CXXThisValue = OldCXXThisValue; |
1443 | CGF.CXXThisAlignment = OldCXXThisAlignment; |
1444 | } |
1445 | |
1446 | public: |
1447 | CodeGenFunction &CGF; |
1448 | llvm::Value *OldCXXThisValue; |
1449 | CharUnits OldCXXThisAlignment; |
1450 | SourceLocExprScopeGuard SourceLocScope; |
1451 | }; |
1452 | |
1453 | struct CXXDefaultArgExprScope : SourceLocExprScopeGuard { |
1454 | CXXDefaultArgExprScope(CodeGenFunction &CGF, const CXXDefaultArgExpr *E) |
1455 | : SourceLocExprScopeGuard(E, CGF.CurSourceLocExprScope) {} |
1456 | }; |
1457 | |
1458 | /// The scope of an ArrayInitLoopExpr. Within this scope, the value of the |
1459 | /// current loop index is overridden. |
1460 | class ArrayInitLoopExprScope { |
1461 | public: |
1462 | ArrayInitLoopExprScope(CodeGenFunction &CGF, llvm::Value *Index) |
1463 | : CGF(CGF), OldArrayInitIndex(CGF.ArrayInitIndex) { |
1464 | CGF.ArrayInitIndex = Index; |
1465 | } |
1466 | ~ArrayInitLoopExprScope() { |
1467 | CGF.ArrayInitIndex = OldArrayInitIndex; |
1468 | } |
1469 | |
1470 | private: |
1471 | CodeGenFunction &CGF; |
1472 | llvm::Value *OldArrayInitIndex; |
1473 | }; |
1474 | |
1475 | class InlinedInheritingConstructorScope { |
1476 | public: |
1477 | InlinedInheritingConstructorScope(CodeGenFunction &CGF, GlobalDecl GD) |
1478 | : CGF(CGF), OldCurGD(CGF.CurGD), OldCurFuncDecl(CGF.CurFuncDecl), |
1479 | OldCurCodeDecl(CGF.CurCodeDecl), |
1480 | OldCXXABIThisDecl(CGF.CXXABIThisDecl), |
1481 | OldCXXABIThisValue(CGF.CXXABIThisValue), |
1482 | OldCXXThisValue(CGF.CXXThisValue), |
1483 | OldCXXABIThisAlignment(CGF.CXXABIThisAlignment), |
1484 | OldCXXThisAlignment(CGF.CXXThisAlignment), |
1485 | OldReturnValue(CGF.ReturnValue), OldFnRetTy(CGF.FnRetTy), |
1486 | OldCXXInheritedCtorInitExprArgs( |
1487 | std::move(CGF.CXXInheritedCtorInitExprArgs)) { |
1488 | CGF.CurGD = GD; |
1489 | CGF.CurFuncDecl = CGF.CurCodeDecl = |
1490 | cast<CXXConstructorDecl>(GD.getDecl()); |
1491 | CGF.CXXABIThisDecl = nullptr; |
1492 | CGF.CXXABIThisValue = nullptr; |
1493 | CGF.CXXThisValue = nullptr; |
1494 | CGF.CXXABIThisAlignment = CharUnits(); |
1495 | CGF.CXXThisAlignment = CharUnits(); |
1496 | CGF.ReturnValue = Address::invalid(); |
1497 | CGF.FnRetTy = QualType(); |
1498 | CGF.CXXInheritedCtorInitExprArgs.clear(); |
1499 | } |
1500 | ~InlinedInheritingConstructorScope() { |
1501 | CGF.CurGD = OldCurGD; |
1502 | CGF.CurFuncDecl = OldCurFuncDecl; |
1503 | CGF.CurCodeDecl = OldCurCodeDecl; |
1504 | CGF.CXXABIThisDecl = OldCXXABIThisDecl; |
1505 | CGF.CXXABIThisValue = OldCXXABIThisValue; |
1506 | CGF.CXXThisValue = OldCXXThisValue; |
1507 | CGF.CXXABIThisAlignment = OldCXXABIThisAlignment; |
1508 | CGF.CXXThisAlignment = OldCXXThisAlignment; |
1509 | CGF.ReturnValue = OldReturnValue; |
1510 | CGF.FnRetTy = OldFnRetTy; |
1511 | CGF.CXXInheritedCtorInitExprArgs = |
1512 | std::move(OldCXXInheritedCtorInitExprArgs); |
1513 | } |
1514 | |
1515 | private: |
1516 | CodeGenFunction &CGF; |
1517 | GlobalDecl OldCurGD; |
1518 | const Decl *OldCurFuncDecl; |
1519 | const Decl *OldCurCodeDecl; |
1520 | ImplicitParamDecl *OldCXXABIThisDecl; |
1521 | llvm::Value *OldCXXABIThisValue; |
1522 | llvm::Value *OldCXXThisValue; |
1523 | CharUnits OldCXXABIThisAlignment; |
1524 | CharUnits OldCXXThisAlignment; |
1525 | Address OldReturnValue; |
1526 | QualType OldFnRetTy; |
1527 | CallArgList OldCXXInheritedCtorInitExprArgs; |
1528 | }; |
1529 | |
1530 | private: |
1531 | /// CXXThisDecl - When generating code for a C++ member function, |
1532 | /// this will hold the implicit 'this' declaration. |
1533 | ImplicitParamDecl *CXXABIThisDecl = nullptr; |
1534 | llvm::Value *CXXABIThisValue = nullptr; |
1535 | llvm::Value *CXXThisValue = nullptr; |
1536 | CharUnits CXXABIThisAlignment; |
1537 | CharUnits CXXThisAlignment; |
1538 | |
1539 | /// The value of 'this' to use when evaluating CXXDefaultInitExprs within |
1540 | /// this expression. |
1541 | Address CXXDefaultInitExprThis = Address::invalid(); |
1542 | |
1543 | /// The current array initialization index when evaluating an |
1544 | /// ArrayInitIndexExpr within an ArrayInitLoopExpr. |
1545 | llvm::Value *ArrayInitIndex = nullptr; |
1546 | |
1547 | /// The values of function arguments to use when evaluating |
1548 | /// CXXInheritedCtorInitExprs within this context. |
1549 | CallArgList CXXInheritedCtorInitExprArgs; |
1550 | |
1551 | /// CXXStructorImplicitParamDecl - When generating code for a constructor or |
1552 | /// destructor, this will hold the implicit argument (e.g. VTT). |
1553 | ImplicitParamDecl *CXXStructorImplicitParamDecl = nullptr; |
1554 | llvm::Value *CXXStructorImplicitParamValue = nullptr; |
1555 | |
1556 | /// OutermostConditional - Points to the outermost active |
1557 | /// conditional control. This is used so that we know if a |
1558 | /// temporary should be destroyed conditionally. |
1559 | ConditionalEvaluation *OutermostConditional = nullptr; |
1560 | |
1561 | /// The current lexical scope. |
1562 | LexicalScope *CurLexicalScope = nullptr; |
1563 | |
1564 | /// The current source location that should be used for exception |
1565 | /// handling code. |
1566 | SourceLocation CurEHLocation; |
1567 | |
1568 | /// BlockByrefInfos - For each __block variable, contains |
1569 | /// information about the layout of the variable. |
1570 | llvm::DenseMap<const ValueDecl *, BlockByrefInfo> BlockByrefInfos; |
1571 | |
1572 | /// Used by -fsanitize=nullability-return to determine whether the return |
1573 | /// value can be checked. |
1574 | llvm::Value *RetValNullabilityPrecondition = nullptr; |
1575 | |
1576 | /// Check if -fsanitize=nullability-return instrumentation is required for |
1577 | /// this function. |
1578 | bool requiresReturnValueNullabilityCheck() const { |
1579 | return RetValNullabilityPrecondition; |
1580 | } |
1581 | |
1582 | /// Used to store precise source locations for return statements by the |
1583 | /// runtime return value checks. |
1584 | Address ReturnLocation = Address::invalid(); |
1585 | |
1586 | /// Check if the return value of this function requires sanitization. |
1587 | bool requiresReturnValueCheck() const { |
1588 | return requiresReturnValueNullabilityCheck() || |
1589 | (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) && |
1590 | CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>()); |
1591 | } |
1592 | |
1593 | llvm::BasicBlock *TerminateLandingPad = nullptr; |
1594 | llvm::BasicBlock *TerminateHandler = nullptr; |
1595 | llvm::BasicBlock *TrapBB = nullptr; |
1596 | |
1597 | /// Terminate funclets keyed by parent funclet pad. |
1598 | llvm::MapVector<llvm::Value *, llvm::BasicBlock *> TerminateFunclets; |
1599 | |
1600 | /// Largest vector width used in ths function. Will be used to create a |
1601 | /// function attribute. |
1602 | unsigned LargestVectorWidth = 0; |
1603 | |
1604 | /// True if we need emit the life-time markers. |
1605 | const bool ShouldEmitLifetimeMarkers; |
1606 | |
1607 | /// Add OpenCL kernel arg metadata and the kernel attribute metadata to |
1608 | /// the function metadata. |
1609 | void EmitOpenCLKernelMetadata(const FunctionDecl *FD, |
1610 | llvm::Function *Fn); |
1611 | |
1612 | public: |
1613 | CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false); |
1614 | ~CodeGenFunction(); |
1615 | |
1616 | CodeGenTypes &getTypes() const { return CGM.getTypes(); } |
1617 | ASTContext &getContext() const { return CGM.getContext(); } |
1618 | CGDebugInfo *getDebugInfo() { |
1619 | if (DisableDebugInfo) |
1620 | return nullptr; |
1621 | return DebugInfo; |
1622 | } |
1623 | void disableDebugInfo() { DisableDebugInfo = true; } |
1624 | void enableDebugInfo() { DisableDebugInfo = false; } |
1625 | |
1626 | bool shouldUseFusedARCCalls() { |
1627 | return CGM.getCodeGenOpts().OptimizationLevel == 0; |
1628 | } |
1629 | |
1630 | const LangOptions &getLangOpts() const { return CGM.getLangOpts(); } |
1631 | |
1632 | /// Returns a pointer to the function's exception object and selector slot, |
1633 | /// which is assigned in every landing pad. |
1634 | Address getExceptionSlot(); |
1635 | Address getEHSelectorSlot(); |
1636 | |
1637 | /// Returns the contents of the function's exception object and selector |
1638 | /// slots. |
1639 | llvm::Value *getExceptionFromSlot(); |
1640 | llvm::Value *getSelectorFromSlot(); |
1641 | |
1642 | Address getNormalCleanupDestSlot(); |
1643 | |
1644 | llvm::BasicBlock *getUnreachableBlock() { |
1645 | if (!UnreachableBlock) { |
1646 | UnreachableBlock = createBasicBlock("unreachable"); |
1647 | new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock); |
1648 | } |
1649 | return UnreachableBlock; |
1650 | } |
1651 | |
1652 | llvm::BasicBlock *getInvokeDest() { |
1653 | if (!EHStack.requiresLandingPad()) return nullptr; |
1654 | return getInvokeDestImpl(); |
1655 | } |
1656 | |
1657 | bool currentFunctionUsesSEHTry() const { return CurSEHParent != nullptr; } |
1658 | |
1659 | const TargetInfo &getTarget() const { return Target; } |
1660 | llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); } |
1661 | const TargetCodeGenInfo &getTargetHooks() const { |
1662 | return CGM.getTargetCodeGenInfo(); |
1663 | } |
1664 | |
1665 | //===--------------------------------------------------------------------===// |
1666 | // Cleanups |
1667 | //===--------------------------------------------------------------------===// |
1668 | |
1669 | typedef void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty); |
1670 | |
1671 | void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, |
1672 | Address arrayEndPointer, |
1673 | QualType elementType, |
1674 | CharUnits elementAlignment, |
1675 | Destroyer *destroyer); |
1676 | void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, |
1677 | llvm::Value *arrayEnd, |
1678 | QualType elementType, |
1679 | CharUnits elementAlignment, |
1680 | Destroyer *destroyer); |
1681 | |
1682 | void pushDestroy(QualType::DestructionKind dtorKind, |
1683 | Address addr, QualType type); |
1684 | void pushEHDestroy(QualType::DestructionKind dtorKind, |
1685 | Address addr, QualType type); |
1686 | void pushDestroy(CleanupKind kind, Address addr, QualType type, |
1687 | Destroyer *destroyer, bool useEHCleanupForArray); |
1688 | void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, |
1689 | QualType type, Destroyer *destroyer, |
1690 | bool useEHCleanupForArray); |
1691 | void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, |
1692 | llvm::Value *CompletePtr, |
1693 | QualType ElementType); |
1694 | void pushStackRestore(CleanupKind kind, Address SPMem); |
1695 | void emitDestroy(Address addr, QualType type, Destroyer *destroyer, |
1696 | bool useEHCleanupForArray); |
1697 | llvm::Function *generateDestroyHelper(Address addr, QualType type, |
1698 | Destroyer *destroyer, |
1699 | bool useEHCleanupForArray, |
1700 | const VarDecl *VD); |
1701 | void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, |
1702 | QualType elementType, CharUnits elementAlign, |
1703 | Destroyer *destroyer, |
1704 | bool checkZeroLength, bool useEHCleanup); |
1705 | |
1706 | Destroyer *getDestroyer(QualType::DestructionKind destructionKind); |
1707 | |
1708 | /// Determines whether an EH cleanup is required to destroy a type |
1709 | /// with the given destruction kind. |
1710 | bool needsEHCleanup(QualType::DestructionKind kind) { |
1711 | switch (kind) { |
1712 | case QualType::DK_none: |
1713 | return false; |
1714 | case QualType::DK_cxx_destructor: |
1715 | case QualType::DK_objc_weak_lifetime: |
1716 | case QualType::DK_nontrivial_c_struct: |
1717 | return getLangOpts().Exceptions; |
1718 | case QualType::DK_objc_strong_lifetime: |
1719 | return getLangOpts().Exceptions && |
1720 | CGM.getCodeGenOpts().ObjCAutoRefCountExceptions; |
1721 | } |
1722 | llvm_unreachable("bad destruction kind")::llvm::llvm_unreachable_internal("bad destruction kind", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 1722); |
1723 | } |
1724 | |
1725 | CleanupKind getCleanupKind(QualType::DestructionKind kind) { |
1726 | return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup); |
1727 | } |
1728 | |
1729 | //===--------------------------------------------------------------------===// |
1730 | // Objective-C |
1731 | //===--------------------------------------------------------------------===// |
1732 | |
1733 | void GenerateObjCMethod(const ObjCMethodDecl *OMD); |
1734 | |
1735 | void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD); |
1736 | |
1737 | /// GenerateObjCGetter - Synthesize an Objective-C property getter function. |
1738 | void GenerateObjCGetter(ObjCImplementationDecl *IMP, |
1739 | const ObjCPropertyImplDecl *PID); |
1740 | void generateObjCGetterBody(const ObjCImplementationDecl *classImpl, |
1741 | const ObjCPropertyImplDecl *propImpl, |
1742 | const ObjCMethodDecl *GetterMothodDecl, |
1743 | llvm::Constant *AtomicHelperFn); |
1744 | |
1745 | void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
1746 | ObjCMethodDecl *MD, bool ctor); |
1747 | |
1748 | /// GenerateObjCSetter - Synthesize an Objective-C property setter function |
1749 | /// for the given property. |
1750 | void GenerateObjCSetter(ObjCImplementationDecl *IMP, |
1751 | const ObjCPropertyImplDecl *PID); |
1752 | void generateObjCSetterBody(const ObjCImplementationDecl *classImpl, |
1753 | const ObjCPropertyImplDecl *propImpl, |
1754 | llvm::Constant *AtomicHelperFn); |
1755 | |
1756 | //===--------------------------------------------------------------------===// |
1757 | // Block Bits |
1758 | //===--------------------------------------------------------------------===// |
1759 | |
1760 | /// Emit block literal. |
1761 | /// \return an LLVM value which is a pointer to a struct which contains |
1762 | /// information about the block, including the block invoke function, the |
1763 | /// captured variables, etc. |
1764 | llvm::Value *EmitBlockLiteral(const BlockExpr *); |
1765 | static void destroyBlockInfos(CGBlockInfo *info); |
1766 | |
1767 | llvm::Function *GenerateBlockFunction(GlobalDecl GD, |
1768 | const CGBlockInfo &Info, |
1769 | const DeclMapTy &ldm, |
1770 | bool IsLambdaConversionToBlock, |
1771 | bool BuildGlobalBlock); |
1772 | |
1773 | /// Check if \p T is a C++ class that has a destructor that can throw. |
1774 | static bool cxxDestructorCanThrow(QualType T); |
1775 | |
1776 | llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo); |
1777 | llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo); |
1778 | llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction( |
1779 | const ObjCPropertyImplDecl *PID); |
1780 | llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction( |
1781 | const ObjCPropertyImplDecl *PID); |
1782 | llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty); |
1783 | |
1784 | void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags, |
1785 | bool CanThrow); |
1786 | |
1787 | class AutoVarEmission; |
1788 | |
1789 | void emitByrefStructureInit(const AutoVarEmission &emission); |
1790 | |
1791 | /// Enter a cleanup to destroy a __block variable. Note that this |
1792 | /// cleanup should be a no-op if the variable hasn't left the stack |
1793 | /// yet; if a cleanup is required for the variable itself, that needs |
1794 | /// to be done externally. |
1795 | /// |
1796 | /// \param Kind Cleanup kind. |
1797 | /// |
1798 | /// \param Addr When \p LoadBlockVarAddr is false, the address of the __block |
1799 | /// structure that will be passed to _Block_object_dispose. When |
1800 | /// \p LoadBlockVarAddr is true, the address of the field of the block |
1801 | /// structure that holds the address of the __block structure. |
1802 | /// |
1803 | /// \param Flags The flag that will be passed to _Block_object_dispose. |
1804 | /// |
1805 | /// \param LoadBlockVarAddr Indicates whether we need to emit a load from |
1806 | /// \p Addr to get the address of the __block structure. |
1807 | void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, |
1808 | bool LoadBlockVarAddr, bool CanThrow); |
1809 | |
1810 | void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, |
1811 | llvm::Value *ptr); |
1812 | |
1813 | Address LoadBlockStruct(); |
1814 | Address GetAddrOfBlockDecl(const VarDecl *var); |
1815 | |
1816 | /// BuildBlockByrefAddress - Computes the location of the |
1817 | /// data in a variable which is declared as __block. |
1818 | Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, |
1819 | bool followForward = true); |
1820 | Address emitBlockByrefAddress(Address baseAddr, |
1821 | const BlockByrefInfo &info, |
1822 | bool followForward, |
1823 | const llvm::Twine &name); |
1824 | |
1825 | const BlockByrefInfo &getBlockByrefInfo(const VarDecl *var); |
1826 | |
1827 | QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args); |
1828 | |
1829 | void GenerateCode(GlobalDecl GD, llvm::Function *Fn, |
1830 | const CGFunctionInfo &FnInfo); |
1831 | |
1832 | /// Annotate the function with an attribute that disables TSan checking at |
1833 | /// runtime. |
1834 | void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn); |
1835 | |
1836 | /// Emit code for the start of a function. |
1837 | /// \param Loc The location to be associated with the function. |
1838 | /// \param StartLoc The location of the function body. |
1839 | void StartFunction(GlobalDecl GD, |
1840 | QualType RetTy, |
1841 | llvm::Function *Fn, |
1842 | const CGFunctionInfo &FnInfo, |
1843 | const FunctionArgList &Args, |
1844 | SourceLocation Loc = SourceLocation(), |
1845 | SourceLocation StartLoc = SourceLocation()); |
1846 | |
1847 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor); |
1848 | |
1849 | void EmitConstructorBody(FunctionArgList &Args); |
1850 | void EmitDestructorBody(FunctionArgList &Args); |
1851 | void emitImplicitAssignmentOperatorBody(FunctionArgList &Args); |
1852 | void EmitFunctionBody(const Stmt *Body); |
1853 | void EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S); |
1854 | |
1855 | void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator, |
1856 | CallArgList &CallArgs); |
1857 | void EmitLambdaBlockInvokeBody(); |
1858 | void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD); |
1859 | void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD); |
1860 | void EmitLambdaVLACapture(const VariableArrayType *VAT, LValue LV) { |
1861 | EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV); |
1862 | } |
1863 | void EmitAsanPrologueOrEpilogue(bool Prologue); |
1864 | |
1865 | /// Emit the unified return block, trying to avoid its emission when |
1866 | /// possible. |
1867 | /// \return The debug location of the user written return statement if the |
1868 | /// return block is is avoided. |
1869 | llvm::DebugLoc EmitReturnBlock(); |
1870 | |
1871 | /// FinishFunction - Complete IR generation of the current function. It is |
1872 | /// legal to call this function even if there is no current insertion point. |
1873 | void FinishFunction(SourceLocation EndLoc=SourceLocation()); |
1874 | |
1875 | void StartThunk(llvm::Function *Fn, GlobalDecl GD, |
1876 | const CGFunctionInfo &FnInfo, bool IsUnprototyped); |
1877 | |
1878 | void EmitCallAndReturnForThunk(llvm::FunctionCallee Callee, |
1879 | const ThunkInfo *Thunk, bool IsUnprototyped); |
1880 | |
1881 | void FinishThunk(); |
1882 | |
1883 | /// Emit a musttail call for a thunk with a potentially adjusted this pointer. |
1884 | void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr, |
1885 | llvm::FunctionCallee Callee); |
1886 | |
1887 | /// Generate a thunk for the given method. |
1888 | void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, |
1889 | GlobalDecl GD, const ThunkInfo &Thunk, |
1890 | bool IsUnprototyped); |
1891 | |
1892 | llvm::Function *GenerateVarArgsThunk(llvm::Function *Fn, |
1893 | const CGFunctionInfo &FnInfo, |
1894 | GlobalDecl GD, const ThunkInfo &Thunk); |
1895 | |
1896 | void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, |
1897 | FunctionArgList &Args); |
1898 | |
1899 | void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init); |
1900 | |
1901 | /// Struct with all information about dynamic [sub]class needed to set vptr. |
1902 | struct VPtr { |
1903 | BaseSubobject Base; |
1904 | const CXXRecordDecl *NearestVBase; |
1905 | CharUnits OffsetFromNearestVBase; |
1906 | const CXXRecordDecl *VTableClass; |
1907 | }; |
1908 | |
1909 | /// Initialize the vtable pointer of the given subobject. |
1910 | void InitializeVTablePointer(const VPtr &vptr); |
1911 | |
1912 | typedef llvm::SmallVector<VPtr, 4> VPtrsVector; |
1913 | |
1914 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
1915 | VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass); |
1916 | |
1917 | void getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, |
1918 | CharUnits OffsetFromNearestVBase, |
1919 | bool BaseIsNonVirtualPrimaryBase, |
1920 | const CXXRecordDecl *VTableClass, |
1921 | VisitedVirtualBasesSetTy &VBases, VPtrsVector &vptrs); |
1922 | |
1923 | void InitializeVTablePointers(const CXXRecordDecl *ClassDecl); |
1924 | |
1925 | /// GetVTablePtr - Return the Value of the vtable pointer member pointed |
1926 | /// to by This. |
1927 | llvm::Value *GetVTablePtr(Address This, llvm::Type *VTableTy, |
1928 | const CXXRecordDecl *VTableClass); |
1929 | |
1930 | enum CFITypeCheckKind { |
1931 | CFITCK_VCall, |
1932 | CFITCK_NVCall, |
1933 | CFITCK_DerivedCast, |
1934 | CFITCK_UnrelatedCast, |
1935 | CFITCK_ICall, |
1936 | CFITCK_NVMFCall, |
1937 | CFITCK_VMFCall, |
1938 | }; |
1939 | |
1940 | /// Derived is the presumed address of an object of type T after a |
1941 | /// cast. If T is a polymorphic class type, emit a check that the virtual |
1942 | /// table for Derived belongs to a class derived from T. |
1943 | void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived, |
1944 | bool MayBeNull, CFITypeCheckKind TCK, |
1945 | SourceLocation Loc); |
1946 | |
1947 | /// EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable. |
1948 | /// If vptr CFI is enabled, emit a check that VTable is valid. |
1949 | void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable, |
1950 | CFITypeCheckKind TCK, SourceLocation Loc); |
1951 | |
1952 | /// EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for |
1953 | /// RD using llvm.type.test. |
1954 | void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable, |
1955 | CFITypeCheckKind TCK, SourceLocation Loc); |
1956 | |
1957 | /// If whole-program virtual table optimization is enabled, emit an assumption |
1958 | /// that VTable is a member of RD's type identifier. Or, if vptr CFI is |
1959 | /// enabled, emit a check that VTable is a member of RD's type identifier. |
1960 | void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, |
1961 | llvm::Value *VTable, SourceLocation Loc); |
1962 | |
1963 | /// Returns whether we should perform a type checked load when loading a |
1964 | /// virtual function for virtual calls to members of RD. This is generally |
1965 | /// true when both vcall CFI and whole-program-vtables are enabled. |
1966 | bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD); |
1967 | |
1968 | /// Emit a type checked load from the given vtable. |
1969 | llvm::Value *EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable, |
1970 | uint64_t VTableByteOffset); |
1971 | |
1972 | /// EnterDtorCleanups - Enter the cleanups necessary to complete the |
1973 | /// given phase of destruction for a destructor. The end result |
1974 | /// should call destructors on members and base classes in reverse |
1975 | /// order of their construction. |
1976 | void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type); |
1977 | |
1978 | /// ShouldInstrumentFunction - Return true if the current function should be |
1979 | /// instrumented with __cyg_profile_func_* calls |
1980 | bool ShouldInstrumentFunction(); |
1981 | |
1982 | /// ShouldXRayInstrument - Return true if the current function should be |
1983 | /// instrumented with XRay nop sleds. |
1984 | bool ShouldXRayInstrumentFunction() const; |
1985 | |
1986 | /// AlwaysEmitXRayCustomEvents - Return true if we must unconditionally emit |
1987 | /// XRay custom event handling calls. |
1988 | bool AlwaysEmitXRayCustomEvents() const; |
1989 | |
1990 | /// AlwaysEmitXRayTypedEvents - Return true if clang must unconditionally emit |
1991 | /// XRay typed event handling calls. |
1992 | bool AlwaysEmitXRayTypedEvents() const; |
1993 | |
1994 | /// Encode an address into a form suitable for use in a function prologue. |
1995 | llvm::Constant *EncodeAddrForUseInPrologue(llvm::Function *F, |
1996 | llvm::Constant *Addr); |
1997 | |
1998 | /// Decode an address used in a function prologue, encoded by \c |
1999 | /// EncodeAddrForUseInPrologue. |
2000 | llvm::Value *DecodeAddrUsedInPrologue(llvm::Value *F, |
2001 | llvm::Value *EncodedAddr); |
2002 | |
2003 | /// EmitFunctionProlog - Emit the target specific LLVM code to load the |
2004 | /// arguments for the given function. This is also responsible for naming the |
2005 | /// LLVM function arguments. |
2006 | void EmitFunctionProlog(const CGFunctionInfo &FI, |
2007 | llvm::Function *Fn, |
2008 | const FunctionArgList &Args); |
2009 | |
2010 | /// EmitFunctionEpilog - Emit the target specific LLVM code to return the |
2011 | /// given temporary. |
2012 | void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc, |
2013 | SourceLocation EndLoc); |
2014 | |
2015 | /// Emit a test that checks if the return value \p RV is nonnull. |
2016 | void EmitReturnValueCheck(llvm::Value *RV); |
2017 | |
2018 | /// EmitStartEHSpec - Emit the start of the exception spec. |
2019 | void EmitStartEHSpec(const Decl *D); |
2020 | |
2021 | /// EmitEndEHSpec - Emit the end of the exception spec. |
2022 | void EmitEndEHSpec(const Decl *D); |
2023 | |
2024 | /// getTerminateLandingPad - Return a landing pad that just calls terminate. |
2025 | llvm::BasicBlock *getTerminateLandingPad(); |
2026 | |
2027 | /// getTerminateLandingPad - Return a cleanup funclet that just calls |
2028 | /// terminate. |
2029 | llvm::BasicBlock *getTerminateFunclet(); |
2030 | |
2031 | /// getTerminateHandler - Return a handler (not a landing pad, just |
2032 | /// a catch handler) that just calls terminate. This is used when |
2033 | /// a terminate scope encloses a try. |
2034 | llvm::BasicBlock *getTerminateHandler(); |
2035 | |
2036 | llvm::Type *ConvertTypeForMem(QualType T); |
2037 | llvm::Type *ConvertType(QualType T); |
2038 | llvm::Type *ConvertType(const TypeDecl *T) { |
2039 | return ConvertType(getContext().getTypeDeclType(T)); |
2040 | } |
2041 | |
2042 | /// LoadObjCSelf - Load the value of self. This function is only valid while |
2043 | /// generating code for an Objective-C method. |
2044 | llvm::Value *LoadObjCSelf(); |
2045 | |
2046 | /// TypeOfSelfObject - Return type of object that this self represents. |
2047 | QualType TypeOfSelfObject(); |
2048 | |
2049 | /// getEvaluationKind - Return the TypeEvaluationKind of QualType \c T. |
2050 | static TypeEvaluationKind getEvaluationKind(QualType T); |
2051 | |
2052 | static bool hasScalarEvaluationKind(QualType T) { |
2053 | return getEvaluationKind(T) == TEK_Scalar; |
2054 | } |
2055 | |
2056 | static bool hasAggregateEvaluationKind(QualType T) { |
2057 | return getEvaluationKind(T) == TEK_Aggregate; |
2058 | } |
2059 | |
2060 | /// createBasicBlock - Create an LLVM basic block. |
2061 | llvm::BasicBlock *createBasicBlock(const Twine &name = "", |
2062 | llvm::Function *parent = nullptr, |
2063 | llvm::BasicBlock *before = nullptr) { |
2064 | return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before); |
2065 | } |
2066 | |
2067 | /// getBasicBlockForLabel - Return the LLVM basicblock that the specified |
2068 | /// label maps to. |
2069 | JumpDest getJumpDestForLabel(const LabelDecl *S); |
2070 | |
2071 | /// SimplifyForwardingBlocks - If the given basic block is only a branch to |
2072 | /// another basic block, simplify it. This assumes that no other code could |
2073 | /// potentially reference the basic block. |
2074 | void SimplifyForwardingBlocks(llvm::BasicBlock *BB); |
2075 | |
2076 | /// EmitBlock - Emit the given block \arg BB and set it as the insert point, |
2077 | /// adding a fall-through branch from the current insert block if |
2078 | /// necessary. It is legal to call this function even if there is no current |
2079 | /// insertion point. |
2080 | /// |
2081 | /// IsFinished - If true, indicates that the caller has finished emitting |
2082 | /// branches to the given block and does not expect to emit code into it. This |
2083 | /// means the block can be ignored if it is unreachable. |
2084 | void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false); |
2085 | |
2086 | /// EmitBlockAfterUses - Emit the given block somewhere hopefully |
2087 | /// near its uses, and leave the insertion point in it. |
2088 | void EmitBlockAfterUses(llvm::BasicBlock *BB); |
2089 | |
2090 | /// EmitBranch - Emit a branch to the specified basic block from the current |
2091 | /// insert block, taking care to avoid creation of branches from dummy |
2092 | /// blocks. It is legal to call this function even if there is no current |
2093 | /// insertion point. |
2094 | /// |
2095 | /// This function clears the current insertion point. The caller should follow |
2096 | /// calls to this function with calls to Emit*Block prior to generation new |
2097 | /// code. |
2098 | void EmitBranch(llvm::BasicBlock *Block); |
2099 | |
2100 | /// HaveInsertPoint - True if an insertion point is defined. If not, this |
2101 | /// indicates that the current code being emitted is unreachable. |
2102 | bool HaveInsertPoint() const { |
2103 | return Builder.GetInsertBlock() != nullptr; |
2104 | } |
2105 | |
2106 | /// EnsureInsertPoint - Ensure that an insertion point is defined so that |
2107 | /// emitted IR has a place to go. Note that by definition, if this function |
2108 | /// creates a block then that block is unreachable; callers may do better to |
2109 | /// detect when no insertion point is defined and simply skip IR generation. |
2110 | void EnsureInsertPoint() { |
2111 | if (!HaveInsertPoint()) |
2112 | EmitBlock(createBasicBlock()); |
2113 | } |
2114 | |
2115 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
2116 | /// specified stmt yet. |
2117 | void ErrorUnsupported(const Stmt *S, const char *Type); |
2118 | |
2119 | //===--------------------------------------------------------------------===// |
2120 | // Helpers |
2121 | //===--------------------------------------------------------------------===// |
2122 | |
2123 | LValue MakeAddrLValue(Address Addr, QualType T, |
2124 | AlignmentSource Source = AlignmentSource::Type) { |
2125 | return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source), |
2126 | CGM.getTBAAAccessInfo(T)); |
2127 | } |
2128 | |
2129 | LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo, |
2130 | TBAAAccessInfo TBAAInfo) { |
2131 | return LValue::MakeAddr(Addr, T, getContext(), BaseInfo, TBAAInfo); |
2132 | } |
2133 | |
2134 | LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment, |
2135 | AlignmentSource Source = AlignmentSource::Type) { |
2136 | return LValue::MakeAddr(Address(V, Alignment), T, getContext(), |
2137 | LValueBaseInfo(Source), CGM.getTBAAAccessInfo(T)); |
2138 | } |
2139 | |
2140 | LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment, |
2141 | LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) { |
2142 | return LValue::MakeAddr(Address(V, Alignment), T, getContext(), |
2143 | BaseInfo, TBAAInfo); |
2144 | } |
2145 | |
2146 | LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T); |
2147 | LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T); |
2148 | CharUnits getNaturalTypeAlignment(QualType T, |
2149 | LValueBaseInfo *BaseInfo = nullptr, |
2150 | TBAAAccessInfo *TBAAInfo = nullptr, |
2151 | bool forPointeeType = false); |
2152 | CharUnits getNaturalPointeeTypeAlignment(QualType T, |
2153 | LValueBaseInfo *BaseInfo = nullptr, |
2154 | TBAAAccessInfo *TBAAInfo = nullptr); |
2155 | |
2156 | Address EmitLoadOfReference(LValue RefLVal, |
2157 | LValueBaseInfo *PointeeBaseInfo = nullptr, |
2158 | TBAAAccessInfo *PointeeTBAAInfo = nullptr); |
2159 | LValue EmitLoadOfReferenceLValue(LValue RefLVal); |
2160 | LValue EmitLoadOfReferenceLValue(Address RefAddr, QualType RefTy, |
2161 | AlignmentSource Source = |
2162 | AlignmentSource::Type) { |
2163 | LValue RefLVal = MakeAddrLValue(RefAddr, RefTy, LValueBaseInfo(Source), |
2164 | CGM.getTBAAAccessInfo(RefTy)); |
2165 | return EmitLoadOfReferenceLValue(RefLVal); |
2166 | } |
2167 | |
2168 | Address EmitLoadOfPointer(Address Ptr, const PointerType *PtrTy, |
2169 | LValueBaseInfo *BaseInfo = nullptr, |
2170 | TBAAAccessInfo *TBAAInfo = nullptr); |
2171 | LValue EmitLoadOfPointerLValue(Address Ptr, const PointerType *PtrTy); |
2172 | |
2173 | /// CreateTempAlloca - This creates an alloca and inserts it into the entry |
2174 | /// block if \p ArraySize is nullptr, otherwise inserts it at the current |
2175 | /// insertion point of the builder. The caller is responsible for setting an |
2176 | /// appropriate alignment on |
2177 | /// the alloca. |
2178 | /// |
2179 | /// \p ArraySize is the number of array elements to be allocated if it |
2180 | /// is not nullptr. |
2181 | /// |
2182 | /// LangAS::Default is the address space of pointers to local variables and |
2183 | /// temporaries, as exposed in the source language. In certain |
2184 | /// configurations, this is not the same as the alloca address space, and a |
2185 | /// cast is needed to lift the pointer from the alloca AS into |
2186 | /// LangAS::Default. This can happen when the target uses a restricted |
2187 | /// address space for the stack but the source language requires |
2188 | /// LangAS::Default to be a generic address space. The latter condition is |
2189 | /// common for most programming languages; OpenCL is an exception in that |
2190 | /// LangAS::Default is the private address space, which naturally maps |
2191 | /// to the stack. |
2192 | /// |
2193 | /// Because the address of a temporary is often exposed to the program in |
2194 | /// various ways, this function will perform the cast. The original alloca |
2195 | /// instruction is returned through \p Alloca if it is not nullptr. |
2196 | /// |
2197 | /// The cast is not performaed in CreateTempAllocaWithoutCast. This is |
2198 | /// more efficient if the caller knows that the address will not be exposed. |
2199 | llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty, const Twine &Name = "tmp", |
2200 | llvm::Value *ArraySize = nullptr); |
2201 | Address CreateTempAlloca(llvm::Type *Ty, CharUnits align, |
2202 | const Twine &Name = "tmp", |
2203 | llvm::Value *ArraySize = nullptr, |
2204 | Address *Alloca = nullptr); |
2205 | Address CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align, |
2206 | const Twine &Name = "tmp", |
2207 | llvm::Value *ArraySize = nullptr); |
2208 | |
2209 | /// CreateDefaultAlignedTempAlloca - This creates an alloca with the |
2210 | /// default ABI alignment of the given LLVM type. |
2211 | /// |
2212 | /// IMPORTANT NOTE: This is *not* generally the right alignment for |
2213 | /// any given AST type that happens to have been lowered to the |
2214 | /// given IR type. This should only ever be used for function-local, |
2215 | /// IR-driven manipulations like saving and restoring a value. Do |
2216 | /// not hand this address off to arbitrary IRGen routines, and especially |
2217 | /// do not pass it as an argument to a function that might expect a |
2218 | /// properly ABI-aligned value. |
2219 | Address CreateDefaultAlignTempAlloca(llvm::Type *Ty, |
2220 | const Twine &Name = "tmp"); |
2221 | |
2222 | /// InitTempAlloca - Provide an initial value for the given alloca which |
2223 | /// will be observable at all locations in the function. |
2224 | /// |
2225 | /// The address should be something that was returned from one of |
2226 | /// the CreateTempAlloca or CreateMemTemp routines, and the |
2227 | /// initializer must be valid in the entry block (i.e. it must |
2228 | /// either be a constant or an argument value). |
2229 | void InitTempAlloca(Address Alloca, llvm::Value *Value); |
2230 | |
2231 | /// CreateIRTemp - Create a temporary IR object of the given type, with |
2232 | /// appropriate alignment. This routine should only be used when an temporary |
2233 | /// value needs to be stored into an alloca (for example, to avoid explicit |
2234 | /// PHI construction), but the type is the IR type, not the type appropriate |
2235 | /// for storing in memory. |
2236 | /// |
2237 | /// That is, this is exactly equivalent to CreateMemTemp, but calling |
2238 | /// ConvertType instead of ConvertTypeForMem. |
2239 | Address CreateIRTemp(QualType T, const Twine &Name = "tmp"); |
2240 | |
2241 | /// CreateMemTemp - Create a temporary memory object of the given type, with |
2242 | /// appropriate alignmen and cast it to the default address space. Returns |
2243 | /// the original alloca instruction by \p Alloca if it is not nullptr. |
2244 | Address CreateMemTemp(QualType T, const Twine &Name = "tmp", |
2245 | Address *Alloca = nullptr); |
2246 | Address CreateMemTemp(QualType T, CharUnits Align, const Twine &Name = "tmp", |
2247 | Address *Alloca = nullptr); |
2248 | |
2249 | /// CreateMemTemp - Create a temporary memory object of the given type, with |
2250 | /// appropriate alignmen without casting it to the default address space. |
2251 | Address CreateMemTempWithoutCast(QualType T, const Twine &Name = "tmp"); |
2252 | Address CreateMemTempWithoutCast(QualType T, CharUnits Align, |
2253 | const Twine &Name = "tmp"); |
2254 | |
2255 | /// CreateAggTemp - Create a temporary memory object for the given |
2256 | /// aggregate type. |
2257 | AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") { |
2258 | return AggValueSlot::forAddr(CreateMemTemp(T, Name), |
2259 | T.getQualifiers(), |
2260 | AggValueSlot::IsNotDestructed, |
2261 | AggValueSlot::DoesNotNeedGCBarriers, |
2262 | AggValueSlot::IsNotAliased, |
2263 | AggValueSlot::DoesNotOverlap); |
2264 | } |
2265 | |
2266 | /// Emit a cast to void* in the appropriate address space. |
2267 | llvm::Value *EmitCastToVoidPtr(llvm::Value *value); |
2268 | |
2269 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
2270 | /// expression and compare the result against zero, returning an Int1Ty value. |
2271 | llvm::Value *EvaluateExprAsBool(const Expr *E); |
2272 | |
2273 | /// EmitIgnoredExpr - Emit an expression in a context which ignores the result. |
2274 | void EmitIgnoredExpr(const Expr *E); |
2275 | |
2276 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
2277 | /// any type. The result is returned as an RValue struct. If this is an |
2278 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where |
2279 | /// the result should be returned. |
2280 | /// |
2281 | /// \param ignoreResult True if the resulting value isn't used. |
2282 | RValue EmitAnyExpr(const Expr *E, |
2283 | AggValueSlot aggSlot = AggValueSlot::ignored(), |
2284 | bool ignoreResult = false); |
2285 | |
2286 | // EmitVAListRef - Emit a "reference" to a va_list; this is either the address |
2287 | // or the value of the expression, depending on how va_list is defined. |
2288 | Address EmitVAListRef(const Expr *E); |
2289 | |
2290 | /// Emit a "reference" to a __builtin_ms_va_list; this is |
2291 | /// always the value of the expression, because a __builtin_ms_va_list is a |
2292 | /// pointer to a char. |
2293 | Address EmitMSVAListRef(const Expr *E); |
2294 | |
2295 | /// EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will |
2296 | /// always be accessible even if no aggregate location is provided. |
2297 | RValue EmitAnyExprToTemp(const Expr *E); |
2298 | |
2299 | /// EmitAnyExprToMem - Emits the code necessary to evaluate an |
2300 | /// arbitrary expression into the given memory location. |
2301 | void EmitAnyExprToMem(const Expr *E, Address Location, |
2302 | Qualifiers Quals, bool IsInitializer); |
2303 | |
2304 | void EmitAnyExprToExn(const Expr *E, Address Addr); |
2305 | |
2306 | /// EmitExprAsInit - Emits the code necessary to initialize a |
2307 | /// location in memory with the given initializer. |
2308 | void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, |
2309 | bool capturedByInit); |
2310 | |
2311 | /// hasVolatileMember - returns true if aggregate type has a volatile |
2312 | /// member. |
2313 | bool hasVolatileMember(QualType T) { |
2314 | if (const RecordType *RT = T->getAs<RecordType>()) { |
2315 | const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); |
2316 | return RD->hasVolatileMember(); |
2317 | } |
2318 | return false; |
2319 | } |
2320 | |
2321 | /// Determine whether a return value slot may overlap some other object. |
2322 | AggValueSlot::Overlap_t getOverlapForReturnValue() { |
2323 | // FIXME: Assuming no overlap here breaks guaranteed copy elision for base |
2324 | // class subobjects. These cases may need to be revisited depending on the |
2325 | // resolution of the relevant core issue. |
2326 | return AggValueSlot::DoesNotOverlap; |
2327 | } |
2328 | |
2329 | /// Determine whether a field initialization may overlap some other object. |
2330 | AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD); |
2331 | |
2332 | /// Determine whether a base class initialization may overlap some other |
2333 | /// object. |
2334 | AggValueSlot::Overlap_t getOverlapForBaseInit(const CXXRecordDecl *RD, |
2335 | const CXXRecordDecl *BaseRD, |
2336 | bool IsVirtual); |
2337 | |
2338 | /// Emit an aggregate assignment. |
2339 | void EmitAggregateAssign(LValue Dest, LValue Src, QualType EltTy) { |
2340 | bool IsVolatile = hasVolatileMember(EltTy); |
2341 | EmitAggregateCopy(Dest, Src, EltTy, AggValueSlot::MayOverlap, IsVolatile); |
2342 | } |
2343 | |
2344 | void EmitAggregateCopyCtor(LValue Dest, LValue Src, |
2345 | AggValueSlot::Overlap_t MayOverlap) { |
2346 | EmitAggregateCopy(Dest, Src, Src.getType(), MayOverlap); |
2347 | } |
2348 | |
2349 | /// EmitAggregateCopy - Emit an aggregate copy. |
2350 | /// |
2351 | /// \param isVolatile \c true iff either the source or the destination is |
2352 | /// volatile. |
2353 | /// \param MayOverlap Whether the tail padding of the destination might be |
2354 | /// occupied by some other object. More efficient code can often be |
2355 | /// generated if not. |
2356 | void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy, |
2357 | AggValueSlot::Overlap_t MayOverlap, |
2358 | bool isVolatile = false); |
2359 | |
2360 | /// GetAddrOfLocalVar - Return the address of a local variable. |
2361 | Address GetAddrOfLocalVar(const VarDecl *VD) { |
2362 | auto it = LocalDeclMap.find(VD); |
2363 | assert(it != LocalDeclMap.end() &&((it != LocalDeclMap.end() && "Invalid argument to GetAddrOfLocalVar(), no decl!" ) ? static_cast<void> (0) : __assert_fail ("it != LocalDeclMap.end() && \"Invalid argument to GetAddrOfLocalVar(), no decl!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2364, __PRETTY_FUNCTION__)) |
2364 | "Invalid argument to GetAddrOfLocalVar(), no decl!")((it != LocalDeclMap.end() && "Invalid argument to GetAddrOfLocalVar(), no decl!" ) ? static_cast<void> (0) : __assert_fail ("it != LocalDeclMap.end() && \"Invalid argument to GetAddrOfLocalVar(), no decl!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2364, __PRETTY_FUNCTION__)); |
2365 | return it->second; |
2366 | } |
2367 | |
2368 | /// Given an opaque value expression, return its LValue mapping if it exists, |
2369 | /// otherwise create one. |
2370 | LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e); |
2371 | |
2372 | /// Given an opaque value expression, return its RValue mapping if it exists, |
2373 | /// otherwise create one. |
2374 | RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e); |
2375 | |
2376 | /// Get the index of the current ArrayInitLoopExpr, if any. |
2377 | llvm::Value *getArrayInitIndex() { return ArrayInitIndex; } |
2378 | |
2379 | /// getAccessedFieldNo - Given an encoded value and a result number, return |
2380 | /// the input field number being accessed. |
2381 | static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); |
2382 | |
2383 | llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L); |
2384 | llvm::BasicBlock *GetIndirectGotoBlock(); |
2385 | |
2386 | /// Check if \p E is a C++ "this" pointer wrapped in value-preserving casts. |
2387 | static bool IsWrappedCXXThis(const Expr *E); |
2388 | |
2389 | /// EmitNullInitialization - Generate code to set a value of the given type to |
2390 | /// null, If the type contains data member pointers, they will be initialized |
2391 | /// to -1 in accordance with the Itanium C++ ABI. |
2392 | void EmitNullInitialization(Address DestPtr, QualType Ty); |
2393 | |
2394 | /// Emits a call to an LLVM variable-argument intrinsic, either |
2395 | /// \c llvm.va_start or \c llvm.va_end. |
2396 | /// \param ArgValue A reference to the \c va_list as emitted by either |
2397 | /// \c EmitVAListRef or \c EmitMSVAListRef. |
2398 | /// \param IsStart If \c true, emits a call to \c llvm.va_start; otherwise, |
2399 | /// calls \c llvm.va_end. |
2400 | llvm::Value *EmitVAStartEnd(llvm::Value *ArgValue, bool IsStart); |
2401 | |
2402 | /// Generate code to get an argument from the passed in pointer |
2403 | /// and update it accordingly. |
2404 | /// \param VE The \c VAArgExpr for which to generate code. |
2405 | /// \param VAListAddr Receives a reference to the \c va_list as emitted by |
2406 | /// either \c EmitVAListRef or \c EmitMSVAListRef. |
2407 | /// \returns A pointer to the argument. |
2408 | // FIXME: We should be able to get rid of this method and use the va_arg |
2409 | // instruction in LLVM instead once it works well enough. |
2410 | Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr); |
2411 | |
2412 | /// emitArrayLength - Compute the length of an array, even if it's a |
2413 | /// VLA, and drill down to the base element type. |
2414 | llvm::Value *emitArrayLength(const ArrayType *arrayType, |
2415 | QualType &baseType, |
2416 | Address &addr); |
2417 | |
2418 | /// EmitVLASize - Capture all the sizes for the VLA expressions in |
2419 | /// the given variably-modified type and store them in the VLASizeMap. |
2420 | /// |
2421 | /// This function can be called with a null (unreachable) insert point. |
2422 | void EmitVariablyModifiedType(QualType Ty); |
2423 | |
2424 | struct VlaSizePair { |
2425 | llvm::Value *NumElts; |
2426 | QualType Type; |
2427 | |
2428 | VlaSizePair(llvm::Value *NE, QualType T) : NumElts(NE), Type(T) {} |
2429 | }; |
2430 | |
2431 | /// Return the number of elements for a single dimension |
2432 | /// for the given array type. |
2433 | VlaSizePair getVLAElements1D(const VariableArrayType *vla); |
2434 | VlaSizePair getVLAElements1D(QualType vla); |
2435 | |
2436 | /// Returns an LLVM value that corresponds to the size, |
2437 | /// in non-variably-sized elements, of a variable length array type, |
2438 | /// plus that largest non-variably-sized element type. Assumes that |
2439 | /// the type has already been emitted with EmitVariablyModifiedType. |
2440 | VlaSizePair getVLASize(const VariableArrayType *vla); |
2441 | VlaSizePair getVLASize(QualType vla); |
2442 | |
2443 | /// LoadCXXThis - Load the value of 'this'. This function is only valid while |
2444 | /// generating code for an C++ member function. |
2445 | llvm::Value *LoadCXXThis() { |
2446 | assert(CXXThisValue && "no 'this' value for this function")((CXXThisValue && "no 'this' value for this function" ) ? static_cast<void> (0) : __assert_fail ("CXXThisValue && \"no 'this' value for this function\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2446, __PRETTY_FUNCTION__)); |
2447 | return CXXThisValue; |
2448 | } |
2449 | Address LoadCXXThisAddress(); |
2450 | |
2451 | /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have |
2452 | /// virtual bases. |
2453 | // FIXME: Every place that calls LoadCXXVTT is something |
2454 | // that needs to be abstracted properly. |
2455 | llvm::Value *LoadCXXVTT() { |
2456 | assert(CXXStructorImplicitParamValue && "no VTT value for this function")((CXXStructorImplicitParamValue && "no VTT value for this function" ) ? static_cast<void> (0) : __assert_fail ("CXXStructorImplicitParamValue && \"no VTT value for this function\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2456, __PRETTY_FUNCTION__)); |
2457 | return CXXStructorImplicitParamValue; |
2458 | } |
2459 | |
2460 | /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a |
2461 | /// complete class to the given direct base. |
2462 | Address |
2463 | GetAddressOfDirectBaseInCompleteClass(Address Value, |
2464 | const CXXRecordDecl *Derived, |
2465 | const CXXRecordDecl *Base, |
2466 | bool BaseIsVirtual); |
2467 | |
2468 | static bool ShouldNullCheckClassCastValue(const CastExpr *Cast); |
2469 | |
2470 | /// GetAddressOfBaseClass - This function will add the necessary delta to the |
2471 | /// load of 'this' and returns address of the base class. |
2472 | Address GetAddressOfBaseClass(Address Value, |
2473 | const CXXRecordDecl *Derived, |
2474 | CastExpr::path_const_iterator PathBegin, |
2475 | CastExpr::path_const_iterator PathEnd, |
2476 | bool NullCheckValue, SourceLocation Loc); |
2477 | |
2478 | Address GetAddressOfDerivedClass(Address Value, |
2479 | const CXXRecordDecl *Derived, |
2480 | CastExpr::path_const_iterator PathBegin, |
2481 | CastExpr::path_const_iterator PathEnd, |
2482 | bool NullCheckValue); |
2483 | |
2484 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
2485 | /// base constructor/destructor with virtual bases. |
2486 | /// FIXME: VTTs are Itanium ABI-specific, so the definition should move |
2487 | /// to ItaniumCXXABI.cpp together with all the references to VTT. |
2488 | llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, |
2489 | bool Delegating); |
2490 | |
2491 | void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
2492 | CXXCtorType CtorType, |
2493 | const FunctionArgList &Args, |
2494 | SourceLocation Loc); |
2495 | // It's important not to confuse this and the previous function. Delegating |
2496 | // constructors are the C++0x feature. The constructor delegate optimization |
2497 | // is used to reduce duplication in the base and complete consturctors where |
2498 | // they are substantially the same. |
2499 | void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
2500 | const FunctionArgList &Args); |
2501 | |
2502 | /// Emit a call to an inheriting constructor (that is, one that invokes a |
2503 | /// constructor inherited from a base class) by inlining its definition. This |
2504 | /// is necessary if the ABI does not support forwarding the arguments to the |
2505 | /// base class constructor (because they're variadic or similar). |
2506 | void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
2507 | CXXCtorType CtorType, |
2508 | bool ForVirtualBase, |
2509 | bool Delegating, |
2510 | CallArgList &Args); |
2511 | |
2512 | /// Emit a call to a constructor inherited from a base class, passing the |
2513 | /// current constructor's arguments along unmodified (without even making |
2514 | /// a copy). |
2515 | void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D, |
2516 | bool ForVirtualBase, Address This, |
2517 | bool InheritedFromVBase, |
2518 | const CXXInheritedCtorInitExpr *E); |
2519 | |
2520 | void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, |
2521 | bool ForVirtualBase, bool Delegating, |
2522 | AggValueSlot ThisAVS, const CXXConstructExpr *E); |
2523 | |
2524 | void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, |
2525 | bool ForVirtualBase, bool Delegating, |
2526 | Address This, CallArgList &Args, |
2527 | AggValueSlot::Overlap_t Overlap, |
2528 | SourceLocation Loc, bool NewPointerIsChecked); |
2529 | |
2530 | /// Emit assumption load for all bases. Requires to be be called only on |
2531 | /// most-derived class and not under construction of the object. |
2532 | void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This); |
2533 | |
2534 | /// Emit assumption that vptr load == global vtable. |
2535 | void EmitVTableAssumptionLoad(const VPtr &vptr, Address This); |
2536 | |
2537 | void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
2538 | Address This, Address Src, |
2539 | const CXXConstructExpr *E); |
2540 | |
2541 | void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
2542 | const ArrayType *ArrayTy, |
2543 | Address ArrayPtr, |
2544 | const CXXConstructExpr *E, |
2545 | bool NewPointerIsChecked, |
2546 | bool ZeroInitialization = false); |
2547 | |
2548 | void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
2549 | llvm::Value *NumElements, |
2550 | Address ArrayPtr, |
2551 | const CXXConstructExpr *E, |
2552 | bool NewPointerIsChecked, |
2553 | bool ZeroInitialization = false); |
2554 | |
2555 | static Destroyer destroyCXXObject; |
2556 | |
2557 | void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, |
2558 | bool ForVirtualBase, bool Delegating, Address This, |
2559 | QualType ThisTy); |
2560 | |
2561 | void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType, |
2562 | llvm::Type *ElementTy, Address NewPtr, |
2563 | llvm::Value *NumElements, |
2564 | llvm::Value *AllocSizeWithoutCookie); |
2565 | |
2566 | void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType, |
2567 | Address Ptr); |
2568 | |
2569 | llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr); |
2570 | void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr); |
2571 | |
2572 | llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); |
2573 | void EmitCXXDeleteExpr(const CXXDeleteExpr *E); |
2574 | |
2575 | void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, |
2576 | QualType DeleteTy, llvm::Value *NumElements = nullptr, |
2577 | CharUnits CookieSize = CharUnits()); |
2578 | |
2579 | RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, |
2580 | const CallExpr *TheCallExpr, bool IsDelete); |
2581 | |
2582 | llvm::Value *EmitCXXTypeidExpr(const CXXTypeidExpr *E); |
2583 | llvm::Value *EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE); |
2584 | Address EmitCXXUuidofExpr(const CXXUuidofExpr *E); |
2585 | |
2586 | /// Situations in which we might emit a check for the suitability of a |
2587 | /// pointer or glvalue. |
2588 | enum TypeCheckKind { |
2589 | /// Checking the operand of a load. Must be suitably sized and aligned. |
2590 | TCK_Load, |
2591 | /// Checking the destination of a store. Must be suitably sized and aligned. |
2592 | TCK_Store, |
2593 | /// Checking the bound value in a reference binding. Must be suitably sized |
2594 | /// and aligned, but is not required to refer to an object (until the |
2595 | /// reference is used), per core issue 453. |
2596 | TCK_ReferenceBinding, |
2597 | /// Checking the object expression in a non-static data member access. Must |
2598 | /// be an object within its lifetime. |
2599 | TCK_MemberAccess, |
2600 | /// Checking the 'this' pointer for a call to a non-static member function. |
2601 | /// Must be an object within its lifetime. |
2602 | TCK_MemberCall, |
2603 | /// Checking the 'this' pointer for a constructor call. |
2604 | TCK_ConstructorCall, |
2605 | /// Checking the operand of a static_cast to a derived pointer type. Must be |
2606 | /// null or an object within its lifetime. |
2607 | TCK_DowncastPointer, |
2608 | /// Checking the operand of a static_cast to a derived reference type. Must |
2609 | /// be an object within its lifetime. |
2610 | TCK_DowncastReference, |
2611 | /// Checking the operand of a cast to a base object. Must be suitably sized |
2612 | /// and aligned. |
2613 | TCK_Upcast, |
2614 | /// Checking the operand of a cast to a virtual base object. Must be an |
2615 | /// object within its lifetime. |
2616 | TCK_UpcastToVirtualBase, |
2617 | /// Checking the value assigned to a _Nonnull pointer. Must not be null. |
2618 | TCK_NonnullAssign, |
2619 | /// Checking the operand of a dynamic_cast or a typeid expression. Must be |
2620 | /// null or an object within its lifetime. |
2621 | TCK_DynamicOperation |
2622 | }; |
2623 | |
2624 | /// Determine whether the pointer type check \p TCK permits null pointers. |
2625 | static bool isNullPointerAllowed(TypeCheckKind TCK); |
2626 | |
2627 | /// Determine whether the pointer type check \p TCK requires a vptr check. |
2628 | static bool isVptrCheckRequired(TypeCheckKind TCK, QualType Ty); |
2629 | |
2630 | /// Whether any type-checking sanitizers are enabled. If \c false, |
2631 | /// calls to EmitTypeCheck can be skipped. |
2632 | bool sanitizePerformTypeCheck() const; |
2633 | |
2634 | /// Emit a check that \p V is the address of storage of the |
2635 | /// appropriate size and alignment for an object of type \p Type |
2636 | /// (or if ArraySize is provided, for an array of that bound). |
2637 | void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, |
2638 | QualType Type, CharUnits Alignment = CharUnits::Zero(), |
2639 | SanitizerSet SkippedChecks = SanitizerSet(), |
2640 | llvm::Value *ArraySize = nullptr); |
2641 | |
2642 | /// Emit a check that \p Base points into an array object, which |
2643 | /// we can access at index \p Index. \p Accessed should be \c false if we |
2644 | /// this expression is used as an lvalue, for instance in "&Arr[Idx]". |
2645 | void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index, |
2646 | QualType IndexType, bool Accessed); |
2647 | |
2648 | llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
2649 | bool isInc, bool isPre); |
2650 | ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, |
2651 | bool isInc, bool isPre); |
2652 | |
2653 | /// Converts Location to a DebugLoc, if debug information is enabled. |
2654 | llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Location); |
2655 | |
2656 | /// Get the record field index as represented in debug info. |
2657 | unsigned getDebugInfoFIndex(const RecordDecl *Rec, unsigned FieldIndex); |
2658 | |
2659 | |
2660 | //===--------------------------------------------------------------------===// |
2661 | // Declaration Emission |
2662 | //===--------------------------------------------------------------------===// |
2663 | |
2664 | /// EmitDecl - Emit a declaration. |
2665 | /// |
2666 | /// This function can be called with a null (unreachable) insert point. |
2667 | void EmitDecl(const Decl &D); |
2668 | |
2669 | /// EmitVarDecl - Emit a local variable declaration. |
2670 | /// |
2671 | /// This function can be called with a null (unreachable) insert point. |
2672 | void EmitVarDecl(const VarDecl &D); |
2673 | |
2674 | void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, |
2675 | bool capturedByInit); |
2676 | |
2677 | typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D, |
2678 | llvm::Value *Address); |
2679 | |
2680 | /// Determine whether the given initializer is trivial in the sense |
2681 | /// that it requires no code to be generated. |
2682 | bool isTrivialInitializer(const Expr *Init); |
2683 | |
2684 | /// EmitAutoVarDecl - Emit an auto variable declaration. |
2685 | /// |
2686 | /// This function can be called with a null (unreachable) insert point. |
2687 | void EmitAutoVarDecl(const VarDecl &D); |
2688 | |
2689 | class AutoVarEmission { |
2690 | friend class CodeGenFunction; |
2691 | |
2692 | const VarDecl *Variable; |
2693 | |
2694 | /// The address of the alloca for languages with explicit address space |
2695 | /// (e.g. OpenCL) or alloca casted to generic pointer for address space |
2696 | /// agnostic languages (e.g. C++). Invalid if the variable was emitted |
2697 | /// as a global constant. |
2698 | Address Addr; |
2699 | |
2700 | llvm::Value *NRVOFlag; |
2701 | |
2702 | /// True if the variable is a __block variable that is captured by an |
2703 | /// escaping block. |
2704 | bool IsEscapingByRef; |
2705 | |
2706 | /// True if the variable is of aggregate type and has a constant |
2707 | /// initializer. |
2708 | bool IsConstantAggregate; |
2709 | |
2710 | /// Non-null if we should use lifetime annotations. |
2711 | llvm::Value *SizeForLifetimeMarkers; |
2712 | |
2713 | /// Address with original alloca instruction. Invalid if the variable was |
2714 | /// emitted as a global constant. |
2715 | Address AllocaAddr; |
2716 | |
2717 | struct Invalid {}; |
2718 | AutoVarEmission(Invalid) |
2719 | : Variable(nullptr), Addr(Address::invalid()), |
2720 | AllocaAddr(Address::invalid()) {} |
2721 | |
2722 | AutoVarEmission(const VarDecl &variable) |
2723 | : Variable(&variable), Addr(Address::invalid()), NRVOFlag(nullptr), |
2724 | IsEscapingByRef(false), IsConstantAggregate(false), |
2725 | SizeForLifetimeMarkers(nullptr), AllocaAddr(Address::invalid()) {} |
2726 | |
2727 | bool wasEmittedAsGlobal() const { return !Addr.isValid(); } |
2728 | |
2729 | public: |
2730 | static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); } |
2731 | |
2732 | bool useLifetimeMarkers() const { |
2733 | return SizeForLifetimeMarkers != nullptr; |
2734 | } |
2735 | llvm::Value *getSizeForLifetimeMarkers() const { |
2736 | assert(useLifetimeMarkers())((useLifetimeMarkers()) ? static_cast<void> (0) : __assert_fail ("useLifetimeMarkers()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2736, __PRETTY_FUNCTION__)); |
2737 | return SizeForLifetimeMarkers; |
2738 | } |
2739 | |
2740 | /// Returns the raw, allocated address, which is not necessarily |
2741 | /// the address of the object itself. It is casted to default |
2742 | /// address space for address space agnostic languages. |
2743 | Address getAllocatedAddress() const { |
2744 | return Addr; |
2745 | } |
2746 | |
2747 | /// Returns the address for the original alloca instruction. |
2748 | Address getOriginalAllocatedAddress() const { return AllocaAddr; } |
2749 | |
2750 | /// Returns the address of the object within this declaration. |
2751 | /// Note that this does not chase the forwarding pointer for |
2752 | /// __block decls. |
2753 | Address getObjectAddress(CodeGenFunction &CGF) const { |
2754 | if (!IsEscapingByRef) return Addr; |
2755 | |
2756 | return CGF.emitBlockByrefAddress(Addr, Variable, /*forward*/ false); |
2757 | } |
2758 | }; |
2759 | AutoVarEmission EmitAutoVarAlloca(const VarDecl &var); |
2760 | void EmitAutoVarInit(const AutoVarEmission &emission); |
2761 | void EmitAutoVarCleanups(const AutoVarEmission &emission); |
2762 | void emitAutoVarTypeCleanup(const AutoVarEmission &emission, |
2763 | QualType::DestructionKind dtorKind); |
2764 | |
2765 | /// Emits the alloca and debug information for the size expressions for each |
2766 | /// dimension of an array. It registers the association of its (1-dimensional) |
2767 | /// QualTypes and size expression's debug node, so that CGDebugInfo can |
2768 | /// reference this node when creating the DISubrange object to describe the |
2769 | /// array types. |
2770 | void EmitAndRegisterVariableArrayDimensions(CGDebugInfo *DI, |
2771 | const VarDecl &D, |
2772 | bool EmitDebugInfo); |
2773 | |
2774 | void EmitStaticVarDecl(const VarDecl &D, |
2775 | llvm::GlobalValue::LinkageTypes Linkage); |
2776 | |
2777 | class ParamValue { |
2778 | llvm::Value *Value; |
2779 | unsigned Alignment; |
2780 | ParamValue(llvm::Value *V, unsigned A) : Value(V), Alignment(A) {} |
2781 | public: |
2782 | static ParamValue forDirect(llvm::Value *value) { |
2783 | return ParamValue(value, 0); |
2784 | } |
2785 | static ParamValue forIndirect(Address addr) { |
2786 | assert(!addr.getAlignment().isZero())((!addr.getAlignment().isZero()) ? static_cast<void> (0 ) : __assert_fail ("!addr.getAlignment().isZero()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2786, __PRETTY_FUNCTION__)); |
2787 | return ParamValue(addr.getPointer(), addr.getAlignment().getQuantity()); |
2788 | } |
2789 | |
2790 | bool isIndirect() const { return Alignment != 0; } |
2791 | llvm::Value *getAnyValue() const { return Value; } |
2792 | |
2793 | llvm::Value *getDirectValue() const { |
2794 | assert(!isIndirect())((!isIndirect()) ? static_cast<void> (0) : __assert_fail ("!isIndirect()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2794, __PRETTY_FUNCTION__)); |
2795 | return Value; |
2796 | } |
2797 | |
2798 | Address getIndirectAddress() const { |
2799 | assert(isIndirect())((isIndirect()) ? static_cast<void> (0) : __assert_fail ("isIndirect()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 2799, __PRETTY_FUNCTION__)); |
2800 | return Address(Value, CharUnits::fromQuantity(Alignment)); |
2801 | } |
2802 | }; |
2803 | |
2804 | /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. |
2805 | void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo); |
2806 | |
2807 | /// protectFromPeepholes - Protect a value that we're intending to |
2808 | /// store to the side, but which will probably be used later, from |
2809 | /// aggressive peepholing optimizations that might delete it. |
2810 | /// |
2811 | /// Pass the result to unprotectFromPeepholes to declare that |
2812 | /// protection is no longer required. |
2813 | /// |
2814 | /// There's no particular reason why this shouldn't apply to |
2815 | /// l-values, it's just that no existing peepholes work on pointers. |
2816 | PeepholeProtection protectFromPeepholes(RValue rvalue); |
2817 | void unprotectFromPeepholes(PeepholeProtection protection); |
2818 | |
2819 | void EmitAlignmentAssumptionCheck(llvm::Value *Ptr, QualType Ty, |
2820 | SourceLocation Loc, |
2821 | SourceLocation AssumptionLoc, |
2822 | llvm::Value *Alignment, |
2823 | llvm::Value *OffsetValue, |
2824 | llvm::Value *TheCheck, |
2825 | llvm::Instruction *Assumption); |
2826 | |
2827 | void EmitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, |
2828 | SourceLocation Loc, SourceLocation AssumptionLoc, |
2829 | llvm::Value *Alignment, |
2830 | llvm::Value *OffsetValue = nullptr); |
2831 | |
2832 | void EmitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, |
2833 | SourceLocation Loc, SourceLocation AssumptionLoc, |
2834 | unsigned Alignment, |
2835 | llvm::Value *OffsetValue = nullptr); |
2836 | |
2837 | void EmitAlignmentAssumption(llvm::Value *PtrValue, const Expr *E, |
2838 | SourceLocation AssumptionLoc, unsigned Alignment, |
2839 | llvm::Value *OffsetValue = nullptr); |
2840 | |
2841 | //===--------------------------------------------------------------------===// |
2842 | // Statement Emission |
2843 | //===--------------------------------------------------------------------===// |
2844 | |
2845 | /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info. |
2846 | void EmitStopPoint(const Stmt *S); |
2847 | |
2848 | /// EmitStmt - Emit the code for the statement \arg S. It is legal to call |
2849 | /// this function even if there is no current insertion point. |
2850 | /// |
2851 | /// This function may clear the current insertion point; callers should use |
2852 | /// EnsureInsertPoint if they wish to subsequently generate code without first |
2853 | /// calling EmitBlock, EmitBranch, or EmitStmt. |
2854 | void EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs = None); |
2855 | |
2856 | /// EmitSimpleStmt - Try to emit a "simple" statement which does not |
2857 | /// necessarily require an insertion point or debug information; typically |
2858 | /// because the statement amounts to a jump or a container of other |
2859 | /// statements. |
2860 | /// |
2861 | /// \return True if the statement was handled. |
2862 | bool EmitSimpleStmt(const Stmt *S); |
2863 | |
2864 | Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, |
2865 | AggValueSlot AVS = AggValueSlot::ignored()); |
2866 | Address EmitCompoundStmtWithoutScope(const CompoundStmt &S, |
2867 | bool GetLast = false, |
2868 | AggValueSlot AVS = |
2869 | AggValueSlot::ignored()); |
2870 | |
2871 | /// EmitLabel - Emit the block for the given label. It is legal to call this |
2872 | /// function even if there is no current insertion point. |
2873 | void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt. |
2874 | |
2875 | void EmitLabelStmt(const LabelStmt &S); |
2876 | void EmitAttributedStmt(const AttributedStmt &S); |
2877 | void EmitGotoStmt(const GotoStmt &S); |
2878 | void EmitIndirectGotoStmt(const IndirectGotoStmt &S); |
2879 | void EmitIfStmt(const IfStmt &S); |
2880 | |
2881 | void EmitWhileStmt(const WhileStmt &S, |
2882 | ArrayRef<const Attr *> Attrs = None); |
2883 | void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None); |
2884 | void EmitForStmt(const ForStmt &S, |
2885 | ArrayRef<const Attr *> Attrs = None); |
2886 | void EmitReturnStmt(const ReturnStmt &S); |
2887 | void EmitDeclStmt(const DeclStmt &S); |
2888 | void EmitBreakStmt(const BreakStmt &S); |
2889 | void EmitContinueStmt(const ContinueStmt &S); |
2890 | void EmitSwitchStmt(const SwitchStmt &S); |
2891 | void EmitDefaultStmt(const DefaultStmt &S); |
2892 | void EmitCaseStmt(const CaseStmt &S); |
2893 | void EmitCaseStmtRange(const CaseStmt &S); |
2894 | void EmitAsmStmt(const AsmStmt &S); |
2895 | |
2896 | void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); |
2897 | void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); |
2898 | void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); |
2899 | void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); |
2900 | void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S); |
2901 | |
2902 | void EmitCoroutineBody(const CoroutineBodyStmt &S); |
2903 | void EmitCoreturnStmt(const CoreturnStmt &S); |
2904 | RValue EmitCoawaitExpr(const CoawaitExpr &E, |
2905 | AggValueSlot aggSlot = AggValueSlot::ignored(), |
2906 | bool ignoreResult = false); |
2907 | LValue EmitCoawaitLValue(const CoawaitExpr *E); |
2908 | RValue EmitCoyieldExpr(const CoyieldExpr &E, |
2909 | AggValueSlot aggSlot = AggValueSlot::ignored(), |
2910 | bool ignoreResult = false); |
2911 | LValue EmitCoyieldLValue(const CoyieldExpr *E); |
2912 | RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID); |
2913 | |
2914 | void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); |
2915 | void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false); |
2916 | |
2917 | void EmitCXXTryStmt(const CXXTryStmt &S); |
2918 | void EmitSEHTryStmt(const SEHTryStmt &S); |
2919 | void EmitSEHLeaveStmt(const SEHLeaveStmt &S); |
2920 | void EnterSEHTryStmt(const SEHTryStmt &S); |
2921 | void ExitSEHTryStmt(const SEHTryStmt &S); |
2922 | |
2923 | void pushSEHCleanup(CleanupKind kind, |
2924 | llvm::Function *FinallyFunc); |
2925 | void startOutlinedSEHHelper(CodeGenFunction &ParentCGF, bool IsFilter, |
2926 | const Stmt *OutlinedStmt); |
2927 | |
2928 | llvm::Function *GenerateSEHFilterFunction(CodeGenFunction &ParentCGF, |
2929 | const SEHExceptStmt &Except); |
2930 | |
2931 | llvm::Function *GenerateSEHFinallyFunction(CodeGenFunction &ParentCGF, |
2932 | const SEHFinallyStmt &Finally); |
2933 | |
2934 | void EmitSEHExceptionCodeSave(CodeGenFunction &ParentCGF, |
2935 | llvm::Value *ParentFP, |
2936 | llvm::Value *EntryEBP); |
2937 | llvm::Value *EmitSEHExceptionCode(); |
2938 | llvm::Value *EmitSEHExceptionInfo(); |
2939 | llvm::Value *EmitSEHAbnormalTermination(); |
2940 | |
2941 | /// Emit simple code for OpenMP directives in Simd-only mode. |
2942 | void EmitSimpleOMPExecutableDirective(const OMPExecutableDirective &D); |
2943 | |
2944 | /// Scan the outlined statement for captures from the parent function. For |
2945 | /// each capture, mark the capture as escaped and emit a call to |
2946 | /// llvm.localrecover. Insert the localrecover result into the LocalDeclMap. |
2947 | void EmitCapturedLocals(CodeGenFunction &ParentCGF, const Stmt *OutlinedStmt, |
2948 | bool IsFilter); |
2949 | |
2950 | /// Recovers the address of a local in a parent function. ParentVar is the |
2951 | /// address of the variable used in the immediate parent function. It can |
2952 | /// either be an alloca or a call to llvm.localrecover if there are nested |
2953 | /// outlined functions. ParentFP is the frame pointer of the outermost parent |
2954 | /// frame. |
2955 | Address recoverAddrOfEscapedLocal(CodeGenFunction &ParentCGF, |
2956 | Address ParentVar, |
2957 | llvm::Value *ParentFP); |
2958 | |
2959 | void EmitCXXForRangeStmt(const CXXForRangeStmt &S, |
2960 | ArrayRef<const Attr *> Attrs = None); |
2961 | |
2962 | /// Controls insertion of cancellation exit blocks in worksharing constructs. |
2963 | class OMPCancelStackRAII { |
2964 | CodeGenFunction &CGF; |
2965 | |
2966 | public: |
2967 | OMPCancelStackRAII(CodeGenFunction &CGF, OpenMPDirectiveKind Kind, |
2968 | bool HasCancel) |
2969 | : CGF(CGF) { |
2970 | CGF.OMPCancelStack.enter(CGF, Kind, HasCancel); |
2971 | } |
2972 | ~OMPCancelStackRAII() { CGF.OMPCancelStack.exit(CGF); } |
2973 | }; |
2974 | |
2975 | /// Returns calculated size of the specified type. |
2976 | llvm::Value *getTypeSize(QualType Ty); |
2977 | LValue InitCapturedStruct(const CapturedStmt &S); |
2978 | llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K); |
2979 | llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S); |
2980 | Address GenerateCapturedStmtArgument(const CapturedStmt &S); |
2981 | llvm::Function *GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S); |
2982 | void GenerateOpenMPCapturedVars(const CapturedStmt &S, |
2983 | SmallVectorImpl<llvm::Value *> &CapturedVars); |
2984 | void emitOMPSimpleStore(LValue LVal, RValue RVal, QualType RValTy, |
2985 | SourceLocation Loc); |
2986 | /// Perform element by element copying of arrays with type \a |
2987 | /// OriginalType from \a SrcAddr to \a DestAddr using copying procedure |
2988 | /// generated by \a CopyGen. |
2989 | /// |
2990 | /// \param DestAddr Address of the destination array. |
2991 | /// \param SrcAddr Address of the source array. |
2992 | /// \param OriginalType Type of destination and source arrays. |
2993 | /// \param CopyGen Copying procedure that copies value of single array element |
2994 | /// to another single array element. |
2995 | void EmitOMPAggregateAssign( |
2996 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
2997 | const llvm::function_ref<void(Address, Address)> CopyGen); |
2998 | /// Emit proper copying of data from one variable to another. |
2999 | /// |
3000 | /// \param OriginalType Original type of the copied variables. |
3001 | /// \param DestAddr Destination address. |
3002 | /// \param SrcAddr Source address. |
3003 | /// \param DestVD Destination variable used in \a CopyExpr (for arrays, has |
3004 | /// type of the base array element). |
3005 | /// \param SrcVD Source variable used in \a CopyExpr (for arrays, has type of |
3006 | /// the base array element). |
3007 | /// \param Copy Actual copygin expression for copying data from \a SrcVD to \a |
3008 | /// DestVD. |
3009 | void EmitOMPCopy(QualType OriginalType, |
3010 | Address DestAddr, Address SrcAddr, |
3011 | const VarDecl *DestVD, const VarDecl *SrcVD, |
3012 | const Expr *Copy); |
3013 | /// Emit atomic update code for constructs: \a X = \a X \a BO \a E or |
3014 | /// \a X = \a E \a BO \a E. |
3015 | /// |
3016 | /// \param X Value to be updated. |
3017 | /// \param E Update value. |
3018 | /// \param BO Binary operation for update operation. |
3019 | /// \param IsXLHSInRHSPart true if \a X is LHS in RHS part of the update |
3020 | /// expression, false otherwise. |
3021 | /// \param AO Atomic ordering of the generated atomic instructions. |
3022 | /// \param CommonGen Code generator for complex expressions that cannot be |
3023 | /// expressed through atomicrmw instruction. |
3024 | /// \returns <true, OldAtomicValue> if simple 'atomicrmw' instruction was |
3025 | /// generated, <false, RValue::get(nullptr)> otherwise. |
3026 | std::pair<bool, RValue> EmitOMPAtomicSimpleUpdateExpr( |
3027 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
3028 | llvm::AtomicOrdering AO, SourceLocation Loc, |
3029 | const llvm::function_ref<RValue(RValue)> CommonGen); |
3030 | bool EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
3031 | OMPPrivateScope &PrivateScope); |
3032 | void EmitOMPPrivateClause(const OMPExecutableDirective &D, |
3033 | OMPPrivateScope &PrivateScope); |
3034 | void EmitOMPUseDevicePtrClause( |
3035 | const OMPClause &C, OMPPrivateScope &PrivateScope, |
3036 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap); |
3037 | /// Emit code for copyin clause in \a D directive. The next code is |
3038 | /// generated at the start of outlined functions for directives: |
3039 | /// \code |
3040 | /// threadprivate_var1 = master_threadprivate_var1; |
3041 | /// operator=(threadprivate_var2, master_threadprivate_var2); |
3042 | /// ... |
3043 | /// __kmpc_barrier(&loc, global_tid); |
3044 | /// \endcode |
3045 | /// |
3046 | /// \param D OpenMP directive possibly with 'copyin' clause(s). |
3047 | /// \returns true if at least one copyin variable is found, false otherwise. |
3048 | bool EmitOMPCopyinClause(const OMPExecutableDirective &D); |
3049 | /// Emit initial code for lastprivate variables. If some variable is |
3050 | /// not also firstprivate, then the default initialization is used. Otherwise |
3051 | /// initialization of this variable is performed by EmitOMPFirstprivateClause |
3052 | /// method. |
3053 | /// |
3054 | /// \param D Directive that may have 'lastprivate' directives. |
3055 | /// \param PrivateScope Private scope for capturing lastprivate variables for |
3056 | /// proper codegen in internal captured statement. |
3057 | /// |
3058 | /// \returns true if there is at least one lastprivate variable, false |
3059 | /// otherwise. |
3060 | bool EmitOMPLastprivateClauseInit(const OMPExecutableDirective &D, |
3061 | OMPPrivateScope &PrivateScope); |
3062 | /// Emit final copying of lastprivate values to original variables at |
3063 | /// the end of the worksharing or simd directive. |
3064 | /// |
3065 | /// \param D Directive that has at least one 'lastprivate' directives. |
3066 | /// \param IsLastIterCond Boolean condition that must be set to 'i1 true' if |
3067 | /// it is the last iteration of the loop code in associated directive, or to |
3068 | /// 'i1 false' otherwise. If this item is nullptr, no final check is required. |
3069 | void EmitOMPLastprivateClauseFinal(const OMPExecutableDirective &D, |
3070 | bool NoFinals, |
3071 | llvm::Value *IsLastIterCond = nullptr); |
3072 | /// Emit initial code for linear clauses. |
3073 | void EmitOMPLinearClause(const OMPLoopDirective &D, |
3074 | CodeGenFunction::OMPPrivateScope &PrivateScope); |
3075 | /// Emit final code for linear clauses. |
3076 | /// \param CondGen Optional conditional code for final part of codegen for |
3077 | /// linear clause. |
3078 | void EmitOMPLinearClauseFinal( |
3079 | const OMPLoopDirective &D, |
3080 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen); |
3081 | /// Emit initial code for reduction variables. Creates reduction copies |
3082 | /// and initializes them with the values according to OpenMP standard. |
3083 | /// |
3084 | /// \param D Directive (possibly) with the 'reduction' clause. |
3085 | /// \param PrivateScope Private scope for capturing reduction variables for |
3086 | /// proper codegen in internal captured statement. |
3087 | /// |
3088 | void EmitOMPReductionClauseInit(const OMPExecutableDirective &D, |
3089 | OMPPrivateScope &PrivateScope); |
3090 | /// Emit final update of reduction values to original variables at |
3091 | /// the end of the directive. |
3092 | /// |
3093 | /// \param D Directive that has at least one 'reduction' directives. |
3094 | /// \param ReductionKind The kind of reduction to perform. |
3095 | void EmitOMPReductionClauseFinal(const OMPExecutableDirective &D, |
3096 | const OpenMPDirectiveKind ReductionKind); |
3097 | /// Emit initial code for linear variables. Creates private copies |
3098 | /// and initializes them with the values according to OpenMP standard. |
3099 | /// |
3100 | /// \param D Directive (possibly) with the 'linear' clause. |
3101 | /// \return true if at least one linear variable is found that should be |
3102 | /// initialized with the value of the original variable, false otherwise. |
3103 | bool EmitOMPLinearClauseInit(const OMPLoopDirective &D); |
3104 | |
3105 | typedef const llvm::function_ref<void(CodeGenFunction & /*CGF*/, |
3106 | llvm::Function * /*OutlinedFn*/, |
3107 | const OMPTaskDataTy & /*Data*/)> |
3108 | TaskGenTy; |
3109 | void EmitOMPTaskBasedDirective(const OMPExecutableDirective &S, |
3110 | const OpenMPDirectiveKind CapturedRegion, |
3111 | const RegionCodeGenTy &BodyGen, |
3112 | const TaskGenTy &TaskGen, OMPTaskDataTy &Data); |
3113 | struct OMPTargetDataInfo { |
3114 | Address BasePointersArray = Address::invalid(); |
3115 | Address PointersArray = Address::invalid(); |
3116 | Address SizesArray = Address::invalid(); |
3117 | unsigned NumberOfTargetItems = 0; |
3118 | explicit OMPTargetDataInfo() = default; |
3119 | OMPTargetDataInfo(Address BasePointersArray, Address PointersArray, |
3120 | Address SizesArray, unsigned NumberOfTargetItems) |
3121 | : BasePointersArray(BasePointersArray), PointersArray(PointersArray), |
3122 | SizesArray(SizesArray), NumberOfTargetItems(NumberOfTargetItems) {} |
3123 | }; |
3124 | void EmitOMPTargetTaskBasedDirective(const OMPExecutableDirective &S, |
3125 | const RegionCodeGenTy &BodyGen, |
3126 | OMPTargetDataInfo &InputInfo); |
3127 | |
3128 | void EmitOMPParallelDirective(const OMPParallelDirective &S); |
3129 | void EmitOMPSimdDirective(const OMPSimdDirective &S); |
3130 | void EmitOMPForDirective(const OMPForDirective &S); |
3131 | void EmitOMPForSimdDirective(const OMPForSimdDirective &S); |
3132 | void EmitOMPSectionsDirective(const OMPSectionsDirective &S); |
3133 | void EmitOMPSectionDirective(const OMPSectionDirective &S); |
3134 | void EmitOMPSingleDirective(const OMPSingleDirective &S); |
3135 | void EmitOMPMasterDirective(const OMPMasterDirective &S); |
3136 | void EmitOMPCriticalDirective(const OMPCriticalDirective &S); |
3137 | void EmitOMPParallelForDirective(const OMPParallelForDirective &S); |
3138 | void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S); |
3139 | void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S); |
3140 | void EmitOMPTaskDirective(const OMPTaskDirective &S); |
3141 | void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S); |
3142 | void EmitOMPBarrierDirective(const OMPBarrierDirective &S); |
3143 | void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S); |
3144 | void EmitOMPTaskgroupDirective(const OMPTaskgroupDirective &S); |
3145 | void EmitOMPFlushDirective(const OMPFlushDirective &S); |
3146 | void EmitOMPOrderedDirective(const OMPOrderedDirective &S); |
3147 | void EmitOMPAtomicDirective(const OMPAtomicDirective &S); |
3148 | void EmitOMPTargetDirective(const OMPTargetDirective &S); |
3149 | void EmitOMPTargetDataDirective(const OMPTargetDataDirective &S); |
3150 | void EmitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &S); |
3151 | void EmitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &S); |
3152 | void EmitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &S); |
3153 | void EmitOMPTargetParallelDirective(const OMPTargetParallelDirective &S); |
3154 | void |
3155 | EmitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &S); |
3156 | void EmitOMPTeamsDirective(const OMPTeamsDirective &S); |
3157 | void |
3158 | EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S); |
3159 | void EmitOMPCancelDirective(const OMPCancelDirective &S); |
3160 | void EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S); |
3161 | void EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S); |
3162 | void EmitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective &S); |
3163 | void EmitOMPDistributeDirective(const OMPDistributeDirective &S); |
3164 | void EmitOMPDistributeParallelForDirective( |
3165 | const OMPDistributeParallelForDirective &S); |
3166 | void EmitOMPDistributeParallelForSimdDirective( |
3167 | const OMPDistributeParallelForSimdDirective &S); |
3168 | void EmitOMPDistributeSimdDirective(const OMPDistributeSimdDirective &S); |
3169 | void EmitOMPTargetParallelForSimdDirective( |
3170 | const OMPTargetParallelForSimdDirective &S); |
3171 | void EmitOMPTargetSimdDirective(const OMPTargetSimdDirective &S); |
3172 | void EmitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective &S); |
3173 | void |
3174 | EmitOMPTeamsDistributeSimdDirective(const OMPTeamsDistributeSimdDirective &S); |
3175 | void EmitOMPTeamsDistributeParallelForSimdDirective( |
3176 | const OMPTeamsDistributeParallelForSimdDirective &S); |
3177 | void EmitOMPTeamsDistributeParallelForDirective( |
3178 | const OMPTeamsDistributeParallelForDirective &S); |
3179 | void EmitOMPTargetTeamsDirective(const OMPTargetTeamsDirective &S); |
3180 | void EmitOMPTargetTeamsDistributeDirective( |
3181 | const OMPTargetTeamsDistributeDirective &S); |
3182 | void EmitOMPTargetTeamsDistributeParallelForDirective( |
3183 | const OMPTargetTeamsDistributeParallelForDirective &S); |
3184 | void EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
3185 | const OMPTargetTeamsDistributeParallelForSimdDirective &S); |
3186 | void EmitOMPTargetTeamsDistributeSimdDirective( |
3187 | const OMPTargetTeamsDistributeSimdDirective &S); |
3188 | |
3189 | /// Emit device code for the target directive. |
3190 | static void EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
3191 | StringRef ParentName, |
3192 | const OMPTargetDirective &S); |
3193 | static void |
3194 | EmitOMPTargetParallelDeviceFunction(CodeGenModule &CGM, StringRef ParentName, |
3195 | const OMPTargetParallelDirective &S); |
3196 | /// Emit device code for the target parallel for directive. |
3197 | static void EmitOMPTargetParallelForDeviceFunction( |
3198 | CodeGenModule &CGM, StringRef ParentName, |
3199 | const OMPTargetParallelForDirective &S); |
3200 | /// Emit device code for the target parallel for simd directive. |
3201 | static void EmitOMPTargetParallelForSimdDeviceFunction( |
3202 | CodeGenModule &CGM, StringRef ParentName, |
3203 | const OMPTargetParallelForSimdDirective &S); |
3204 | /// Emit device code for the target teams directive. |
3205 | static void |
3206 | EmitOMPTargetTeamsDeviceFunction(CodeGenModule &CGM, StringRef ParentName, |
3207 | const OMPTargetTeamsDirective &S); |
3208 | /// Emit device code for the target teams distribute directive. |
3209 | static void EmitOMPTargetTeamsDistributeDeviceFunction( |
3210 | CodeGenModule &CGM, StringRef ParentName, |
3211 | const OMPTargetTeamsDistributeDirective &S); |
3212 | /// Emit device code for the target teams distribute simd directive. |
3213 | static void EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
3214 | CodeGenModule &CGM, StringRef ParentName, |
3215 | const OMPTargetTeamsDistributeSimdDirective &S); |
3216 | /// Emit device code for the target simd directive. |
3217 | static void EmitOMPTargetSimdDeviceFunction(CodeGenModule &CGM, |
3218 | StringRef ParentName, |
3219 | const OMPTargetSimdDirective &S); |
3220 | /// Emit device code for the target teams distribute parallel for simd |
3221 | /// directive. |
3222 | static void EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
3223 | CodeGenModule &CGM, StringRef ParentName, |
3224 | const OMPTargetTeamsDistributeParallelForSimdDirective &S); |
3225 | |
3226 | static void EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
3227 | CodeGenModule &CGM, StringRef ParentName, |
3228 | const OMPTargetTeamsDistributeParallelForDirective &S); |
3229 | /// Emit inner loop of the worksharing/simd construct. |
3230 | /// |
3231 | /// \param S Directive, for which the inner loop must be emitted. |
3232 | /// \param RequiresCleanup true, if directive has some associated private |
3233 | /// variables. |
3234 | /// \param LoopCond Bollean condition for loop continuation. |
3235 | /// \param IncExpr Increment expression for loop control variable. |
3236 | /// \param BodyGen Generator for the inner body of the inner loop. |
3237 | /// \param PostIncGen Genrator for post-increment code (required for ordered |
3238 | /// loop directvies). |
3239 | void EmitOMPInnerLoop( |
3240 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
3241 | const Expr *IncExpr, |
3242 | const llvm::function_ref<void(CodeGenFunction &)> BodyGen, |
3243 | const llvm::function_ref<void(CodeGenFunction &)> PostIncGen); |
3244 | |
3245 | JumpDest getOMPCancelDestination(OpenMPDirectiveKind Kind); |
3246 | /// Emit initial code for loop counters of loop-based directives. |
3247 | void EmitOMPPrivateLoopCounters(const OMPLoopDirective &S, |
3248 | OMPPrivateScope &LoopScope); |
3249 | |
3250 | /// Helper for the OpenMP loop directives. |
3251 | void EmitOMPLoopBody(const OMPLoopDirective &D, JumpDest LoopExit); |
3252 | |
3253 | /// Emit code for the worksharing loop-based directive. |
3254 | /// \return true, if this construct has any lastprivate clause, false - |
3255 | /// otherwise. |
3256 | bool EmitOMPWorksharingLoop(const OMPLoopDirective &S, Expr *EUB, |
3257 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
3258 | const CodeGenDispatchBoundsTy &CGDispatchBounds); |
3259 | |
3260 | /// Emit code for the distribute loop-based directive. |
3261 | void EmitOMPDistributeLoop(const OMPLoopDirective &S, |
3262 | const CodeGenLoopTy &CodeGenLoop, Expr *IncExpr); |
3263 | |
3264 | /// Helpers for the OpenMP loop directives. |
3265 | void EmitOMPSimdInit(const OMPLoopDirective &D, bool IsMonotonic = false); |
3266 | void EmitOMPSimdFinal( |
3267 | const OMPLoopDirective &D, |
3268 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen); |
3269 | |
3270 | /// Emits the lvalue for the expression with possibly captured variable. |
3271 | LValue EmitOMPSharedLValue(const Expr *E); |
3272 | |
3273 | private: |
3274 | /// Helpers for blocks. |
3275 | llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info); |
3276 | |
3277 | /// struct with the values to be passed to the OpenMP loop-related functions |
3278 | struct OMPLoopArguments { |
3279 | /// loop lower bound |
3280 | Address LB = Address::invalid(); |
3281 | /// loop upper bound |
3282 | Address UB = Address::invalid(); |
3283 | /// loop stride |
3284 | Address ST = Address::invalid(); |
3285 | /// isLastIteration argument for runtime functions |
3286 | Address IL = Address::invalid(); |
3287 | /// Chunk value generated by sema |
3288 | llvm::Value *Chunk = nullptr; |
3289 | /// EnsureUpperBound |
3290 | Expr *EUB = nullptr; |
3291 | /// IncrementExpression |
3292 | Expr *IncExpr = nullptr; |
3293 | /// Loop initialization |
3294 | Expr *Init = nullptr; |
3295 | /// Loop exit condition |
3296 | Expr *Cond = nullptr; |
3297 | /// Update of LB after a whole chunk has been executed |
3298 | Expr *NextLB = nullptr; |
3299 | /// Update of UB after a whole chunk has been executed |
3300 | Expr *NextUB = nullptr; |
3301 | OMPLoopArguments() = default; |
3302 | OMPLoopArguments(Address LB, Address UB, Address ST, Address IL, |
3303 | llvm::Value *Chunk = nullptr, Expr *EUB = nullptr, |
3304 | Expr *IncExpr = nullptr, Expr *Init = nullptr, |
3305 | Expr *Cond = nullptr, Expr *NextLB = nullptr, |
3306 | Expr *NextUB = nullptr) |
3307 | : LB(LB), UB(UB), ST(ST), IL(IL), Chunk(Chunk), EUB(EUB), |
3308 | IncExpr(IncExpr), Init(Init), Cond(Cond), NextLB(NextLB), |
3309 | NextUB(NextUB) {} |
3310 | }; |
3311 | void EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic, |
3312 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, |
3313 | const OMPLoopArguments &LoopArgs, |
3314 | const CodeGenLoopTy &CodeGenLoop, |
3315 | const CodeGenOrderedTy &CodeGenOrdered); |
3316 | void EmitOMPForOuterLoop(const OpenMPScheduleTy &ScheduleKind, |
3317 | bool IsMonotonic, const OMPLoopDirective &S, |
3318 | OMPPrivateScope &LoopScope, bool Ordered, |
3319 | const OMPLoopArguments &LoopArgs, |
3320 | const CodeGenDispatchBoundsTy &CGDispatchBounds); |
3321 | void EmitOMPDistributeOuterLoop(OpenMPDistScheduleClauseKind ScheduleKind, |
3322 | const OMPLoopDirective &S, |
3323 | OMPPrivateScope &LoopScope, |
3324 | const OMPLoopArguments &LoopArgs, |
3325 | const CodeGenLoopTy &CodeGenLoopContent); |
3326 | /// Emit code for sections directive. |
3327 | void EmitSections(const OMPExecutableDirective &S); |
3328 | |
3329 | public: |
3330 | |
3331 | //===--------------------------------------------------------------------===// |
3332 | // LValue Expression Emission |
3333 | //===--------------------------------------------------------------------===// |
3334 | |
3335 | /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type. |
3336 | RValue GetUndefRValue(QualType Ty); |
3337 | |
3338 | /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E |
3339 | /// and issue an ErrorUnsupported style diagnostic (using the |
3340 | /// provided Name). |
3341 | RValue EmitUnsupportedRValue(const Expr *E, |
3342 | const char *Name); |
3343 | |
3344 | /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue |
3345 | /// an ErrorUnsupported style diagnostic (using the provided Name). |
3346 | LValue EmitUnsupportedLValue(const Expr *E, |
3347 | const char *Name); |
3348 | |
3349 | /// EmitLValue - Emit code to compute a designator that specifies the location |
3350 | /// of the expression. |
3351 | /// |
3352 | /// This can return one of two things: a simple address or a bitfield |
3353 | /// reference. In either case, the LLVM Value* in the LValue structure is |
3354 | /// guaranteed to be an LLVM pointer type. |
3355 | /// |
3356 | /// If this returns a bitfield reference, nothing about the pointee type of |
3357 | /// the LLVM value is known: For example, it may not be a pointer to an |
3358 | /// integer. |
3359 | /// |
3360 | /// If this returns a normal address, and if the lvalue's C type is fixed |
3361 | /// size, this method guarantees that the returned pointer type will point to |
3362 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
3363 | /// variable length type, this is not possible. |
3364 | /// |
3365 | LValue EmitLValue(const Expr *E); |
3366 | |
3367 | /// Same as EmitLValue but additionally we generate checking code to |
3368 | /// guard against undefined behavior. This is only suitable when we know |
3369 | /// that the address will be used to access the object. |
3370 | LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK); |
3371 | |
3372 | RValue convertTempToRValue(Address addr, QualType type, |
3373 | SourceLocation Loc); |
3374 | |
3375 | void EmitAtomicInit(Expr *E, LValue lvalue); |
3376 | |
3377 | bool LValueIsSuitableForInlineAtomic(LValue Src); |
3378 | |
3379 | RValue EmitAtomicLoad(LValue LV, SourceLocation SL, |
3380 | AggValueSlot Slot = AggValueSlot::ignored()); |
3381 | |
3382 | RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc, |
3383 | llvm::AtomicOrdering AO, bool IsVolatile = false, |
3384 | AggValueSlot slot = AggValueSlot::ignored()); |
3385 | |
3386 | void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit); |
3387 | |
3388 | void EmitAtomicStore(RValue rvalue, LValue lvalue, llvm::AtomicOrdering AO, |
3389 | bool IsVolatile, bool isInit); |
3390 | |
3391 | std::pair<RValue, llvm::Value *> EmitAtomicCompareExchange( |
3392 | LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc, |
3393 | llvm::AtomicOrdering Success = |
3394 | llvm::AtomicOrdering::SequentiallyConsistent, |
3395 | llvm::AtomicOrdering Failure = |
3396 | llvm::AtomicOrdering::SequentiallyConsistent, |
3397 | bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored()); |
3398 | |
3399 | void EmitAtomicUpdate(LValue LVal, llvm::AtomicOrdering AO, |
3400 | const llvm::function_ref<RValue(RValue)> &UpdateOp, |
3401 | bool IsVolatile); |
3402 | |
3403 | /// EmitToMemory - Change a scalar value from its value |
3404 | /// representation to its in-memory representation. |
3405 | llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty); |
3406 | |
3407 | /// EmitFromMemory - Change a scalar value from its memory |
3408 | /// representation to its value representation. |
3409 | llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty); |
3410 | |
3411 | /// Check if the scalar \p Value is within the valid range for the given |
3412 | /// type \p Ty. |
3413 | /// |
3414 | /// Returns true if a check is needed (even if the range is unknown). |
3415 | bool EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, |
3416 | SourceLocation Loc); |
3417 | |
3418 | /// EmitLoadOfScalar - Load a scalar value from an address, taking |
3419 | /// care to appropriately convert from the memory representation to |
3420 | /// the LLVM value representation. |
3421 | llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, |
3422 | SourceLocation Loc, |
3423 | AlignmentSource Source = AlignmentSource::Type, |
3424 | bool isNontemporal = false) { |
3425 | return EmitLoadOfScalar(Addr, Volatile, Ty, Loc, LValueBaseInfo(Source), |
3426 | CGM.getTBAAAccessInfo(Ty), isNontemporal); |
3427 | } |
3428 | |
3429 | llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, |
3430 | SourceLocation Loc, LValueBaseInfo BaseInfo, |
3431 | TBAAAccessInfo TBAAInfo, |
3432 | bool isNontemporal = false); |
3433 | |
3434 | /// EmitLoadOfScalar - Load a scalar value from an address, taking |
3435 | /// care to appropriately convert from the memory representation to |
3436 | /// the LLVM value representation. The l-value must be a simple |
3437 | /// l-value. |
3438 | llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc); |
3439 | |
3440 | /// EmitStoreOfScalar - Store a scalar value to an address, taking |
3441 | /// care to appropriately convert from the memory representation to |
3442 | /// the LLVM value representation. |
3443 | void EmitStoreOfScalar(llvm::Value *Value, Address Addr, |
3444 | bool Volatile, QualType Ty, |
3445 | AlignmentSource Source = AlignmentSource::Type, |
3446 | bool isInit = false, bool isNontemporal = false) { |
3447 | EmitStoreOfScalar(Value, Addr, Volatile, Ty, LValueBaseInfo(Source), |
3448 | CGM.getTBAAAccessInfo(Ty), isInit, isNontemporal); |
3449 | } |
3450 | |
3451 | void EmitStoreOfScalar(llvm::Value *Value, Address Addr, |
3452 | bool Volatile, QualType Ty, |
3453 | LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo, |
3454 | bool isInit = false, bool isNontemporal = false); |
3455 | |
3456 | /// EmitStoreOfScalar - Store a scalar value to an address, taking |
3457 | /// care to appropriately convert from the memory representation to |
3458 | /// the LLVM value representation. The l-value must be a simple |
3459 | /// l-value. The isInit flag indicates whether this is an initialization. |
3460 | /// If so, atomic qualifiers are ignored and the store is always non-atomic. |
3461 | void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false); |
3462 | |
3463 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
3464 | /// this method emits the address of the lvalue, then loads the result as an |
3465 | /// rvalue, returning the rvalue. |
3466 | RValue EmitLoadOfLValue(LValue V, SourceLocation Loc); |
3467 | RValue EmitLoadOfExtVectorElementLValue(LValue V); |
3468 | RValue EmitLoadOfBitfieldLValue(LValue LV, SourceLocation Loc); |
3469 | RValue EmitLoadOfGlobalRegLValue(LValue LV); |
3470 | |
3471 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
3472 | /// lvalue, where both are guaranteed to the have the same type, and that type |
3473 | /// is 'Ty'. |
3474 | void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false); |
3475 | void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst); |
3476 | void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst); |
3477 | |
3478 | /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints |
3479 | /// as EmitStoreThroughLValue. |
3480 | /// |
3481 | /// \param Result [out] - If non-null, this will be set to a Value* for the |
3482 | /// bit-field contents after the store, appropriate for use as the result of |
3483 | /// an assignment to the bit-field. |
3484 | void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, |
3485 | llvm::Value **Result=nullptr); |
3486 | |
3487 | /// Emit an l-value for an assignment (simple or compound) of complex type. |
3488 | LValue EmitComplexAssignmentLValue(const BinaryOperator *E); |
3489 | LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E); |
3490 | LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, |
3491 | llvm::Value *&Result); |
3492 | |
3493 | // Note: only available for agg return types |
3494 | LValue EmitBinaryOperatorLValue(const BinaryOperator *E); |
3495 | LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E); |
3496 | // Note: only available for agg return types |
3497 | LValue EmitCallExprLValue(const CallExpr *E); |
3498 | // Note: only available for agg return types |
3499 | LValue EmitVAArgExprLValue(const VAArgExpr *E); |
3500 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
3501 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
3502 | LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); |
3503 | LValue EmitPredefinedLValue(const PredefinedExpr *E); |
3504 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
3505 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E, |
3506 | bool Accessed = false); |
3507 | LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, |
3508 | bool IsLowerBound = true); |
3509 | LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); |
3510 | LValue EmitMemberExpr(const MemberExpr *E); |
3511 | LValue EmitObjCIsaExpr(const ObjCIsaExpr *E); |
3512 | LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); |
3513 | LValue EmitInitListLValue(const InitListExpr *E); |
3514 | LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E); |
3515 | LValue EmitCastLValue(const CastExpr *E); |
3516 | LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
3517 | LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e); |
3518 | |
3519 | Address EmitExtVectorElementLValue(LValue V); |
3520 | |
3521 | RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc); |
3522 | |
3523 | Address EmitArrayToPointerDecay(const Expr *Array, |
3524 | LValueBaseInfo *BaseInfo = nullptr, |
3525 | TBAAAccessInfo *TBAAInfo = nullptr); |
3526 | |
3527 | class ConstantEmission { |
3528 | llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference; |
3529 | ConstantEmission(llvm::Constant *C, bool isReference) |
3530 | : ValueAndIsReference(C, isReference) {} |
3531 | public: |
3532 | ConstantEmission() {} |
3533 | static ConstantEmission forReference(llvm::Constant *C) { |
3534 | return ConstantEmission(C, true); |
3535 | } |
3536 | static ConstantEmission forValue(llvm::Constant *C) { |
3537 | return ConstantEmission(C, false); |
3538 | } |
3539 | |
3540 | explicit operator bool() const { |
3541 | return ValueAndIsReference.getOpaqueValue() != nullptr; |
3542 | } |
3543 | |
3544 | bool isReference() const { return ValueAndIsReference.getInt(); } |
3545 | LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const { |
3546 | assert(isReference())((isReference()) ? static_cast<void> (0) : __assert_fail ("isReference()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 3546, __PRETTY_FUNCTION__)); |
3547 | return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(), |
3548 | refExpr->getType()); |
3549 | } |
3550 | |
3551 | llvm::Constant *getValue() const { |
3552 | assert(!isReference())((!isReference()) ? static_cast<void> (0) : __assert_fail ("!isReference()", "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 3552, __PRETTY_FUNCTION__)); |
3553 | return ValueAndIsReference.getPointer(); |
3554 | } |
3555 | }; |
3556 | |
3557 | ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr); |
3558 | ConstantEmission tryEmitAsConstant(const MemberExpr *ME); |
3559 | llvm::Value *emitScalarConstant(const ConstantEmission &Constant, Expr *E); |
3560 | |
3561 | RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, |
3562 | AggValueSlot slot = AggValueSlot::ignored()); |
3563 | LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e); |
3564 | |
3565 | llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface, |
3566 | const ObjCIvarDecl *Ivar); |
3567 | LValue EmitLValueForField(LValue Base, const FieldDecl* Field); |
3568 | LValue EmitLValueForLambdaField(const FieldDecl *Field); |
3569 | |
3570 | /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that |
3571 | /// if the Field is a reference, this will return the address of the reference |
3572 | /// and not the address of the value stored in the reference. |
3573 | LValue EmitLValueForFieldInitialization(LValue Base, |
3574 | const FieldDecl* Field); |
3575 | |
3576 | LValue EmitLValueForIvar(QualType ObjectTy, |
3577 | llvm::Value* Base, const ObjCIvarDecl *Ivar, |
3578 | unsigned CVRQualifiers); |
3579 | |
3580 | LValue EmitCXXConstructLValue(const CXXConstructExpr *E); |
3581 | LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E); |
3582 | LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E); |
3583 | LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E); |
3584 | |
3585 | LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); |
3586 | LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); |
3587 | LValue EmitStmtExprLValue(const StmtExpr *E); |
3588 | LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E); |
3589 | LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E); |
3590 | void EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init); |
3591 | |
3592 | //===--------------------------------------------------------------------===// |
3593 | // Scalar Expression Emission |
3594 | //===--------------------------------------------------------------------===// |
3595 | |
3596 | /// EmitCall - Generate a call of the given function, expecting the given |
3597 | /// result type, and using the given argument list which specifies both the |
3598 | /// LLVM arguments and the types they were derived from. |
3599 | RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, |
3600 | ReturnValueSlot ReturnValue, const CallArgList &Args, |
3601 | llvm::CallBase **callOrInvoke, SourceLocation Loc); |
3602 | RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, |
3603 | ReturnValueSlot ReturnValue, const CallArgList &Args, |
3604 | llvm::CallBase **callOrInvoke = nullptr) { |
3605 | return EmitCall(CallInfo, Callee, ReturnValue, Args, callOrInvoke, |
3606 | SourceLocation()); |
3607 | } |
3608 | RValue EmitCall(QualType FnType, const CGCallee &Callee, const CallExpr *E, |
3609 | ReturnValueSlot ReturnValue, llvm::Value *Chain = nullptr); |
3610 | RValue EmitCallExpr(const CallExpr *E, |
3611 | ReturnValueSlot ReturnValue = ReturnValueSlot()); |
3612 | RValue EmitSimpleCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue); |
3613 | CGCallee EmitCallee(const Expr *E); |
3614 | |
3615 | void checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl); |
3616 | void checkTargetFeatures(SourceLocation Loc, const FunctionDecl *TargetDecl); |
3617 | |
3618 | llvm::CallInst *EmitRuntimeCall(llvm::FunctionCallee callee, |
3619 | const Twine &name = ""); |
3620 | llvm::CallInst *EmitRuntimeCall(llvm::FunctionCallee callee, |
3621 | ArrayRef<llvm::Value *> args, |
3622 | const Twine &name = ""); |
3623 | llvm::CallInst *EmitNounwindRuntimeCall(llvm::FunctionCallee callee, |
3624 | const Twine &name = ""); |
3625 | llvm::CallInst *EmitNounwindRuntimeCall(llvm::FunctionCallee callee, |
3626 | ArrayRef<llvm::Value *> args, |
3627 | const Twine &name = ""); |
3628 | |
3629 | SmallVector<llvm::OperandBundleDef, 1> |
3630 | getBundlesForFunclet(llvm::Value *Callee); |
3631 | |
3632 | llvm::CallBase *EmitCallOrInvoke(llvm::FunctionCallee Callee, |
3633 | ArrayRef<llvm::Value *> Args, |
3634 | const Twine &Name = ""); |
3635 | llvm::CallBase *EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, |
3636 | ArrayRef<llvm::Value *> args, |
3637 | const Twine &name = ""); |
3638 | llvm::CallBase *EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, |
3639 | const Twine &name = ""); |
3640 | void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee, |
3641 | ArrayRef<llvm::Value *> args); |
3642 | |
3643 | CGCallee BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
3644 | NestedNameSpecifier *Qual, |
3645 | llvm::Type *Ty); |
3646 | |
3647 | CGCallee BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD, |
3648 | CXXDtorType Type, |
3649 | const CXXRecordDecl *RD); |
3650 | |
3651 | // Return the copy constructor name with the prefix "__copy_constructor_" |
3652 | // removed. |
3653 | static std::string getNonTrivialCopyConstructorStr(QualType QT, |
3654 | CharUnits Alignment, |
3655 | bool IsVolatile, |
3656 | ASTContext &Ctx); |
3657 | |
3658 | // Return the destructor name with the prefix "__destructor_" removed. |
3659 | static std::string getNonTrivialDestructorStr(QualType QT, |
3660 | CharUnits Alignment, |
3661 | bool IsVolatile, |
3662 | ASTContext &Ctx); |
3663 | |
3664 | // These functions emit calls to the special functions of non-trivial C |
3665 | // structs. |
3666 | void defaultInitNonTrivialCStructVar(LValue Dst); |
3667 | void callCStructDefaultConstructor(LValue Dst); |
3668 | void callCStructDestructor(LValue Dst); |
3669 | void callCStructCopyConstructor(LValue Dst, LValue Src); |
3670 | void callCStructMoveConstructor(LValue Dst, LValue Src); |
3671 | void callCStructCopyAssignmentOperator(LValue Dst, LValue Src); |
3672 | void callCStructMoveAssignmentOperator(LValue Dst, LValue Src); |
3673 | |
3674 | RValue |
3675 | EmitCXXMemberOrOperatorCall(const CXXMethodDecl *Method, |
3676 | const CGCallee &Callee, |
3677 | ReturnValueSlot ReturnValue, llvm::Value *This, |
3678 | llvm::Value *ImplicitParam, |
3679 | QualType ImplicitParamTy, const CallExpr *E, |
3680 | CallArgList *RtlArgs); |
3681 | RValue EmitCXXDestructorCall(GlobalDecl Dtor, const CGCallee &Callee, |
3682 | llvm::Value *This, QualType ThisTy, |
3683 | llvm::Value *ImplicitParam, |
3684 | QualType ImplicitParamTy, const CallExpr *E); |
3685 | RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, |
3686 | ReturnValueSlot ReturnValue); |
3687 | RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE, |
3688 | const CXXMethodDecl *MD, |
3689 | ReturnValueSlot ReturnValue, |
3690 | bool HasQualifier, |
3691 | NestedNameSpecifier *Qualifier, |
3692 | bool IsArrow, const Expr *Base); |
3693 | // Compute the object pointer. |
3694 | Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, |
3695 | llvm::Value *memberPtr, |
3696 | const MemberPointerType *memberPtrType, |
3697 | LValueBaseInfo *BaseInfo = nullptr, |
3698 | TBAAAccessInfo *TBAAInfo = nullptr); |
3699 | RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, |
3700 | ReturnValueSlot ReturnValue); |
3701 | |
3702 | RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
3703 | const CXXMethodDecl *MD, |
3704 | ReturnValueSlot ReturnValue); |
3705 | RValue EmitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
3706 | |
3707 | RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, |
3708 | ReturnValueSlot ReturnValue); |
3709 | |
3710 | RValue EmitNVPTXDevicePrintfCallExpr(const CallExpr *E, |
3711 | ReturnValueSlot ReturnValue); |
3712 | |
3713 | RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, |
3714 | const CallExpr *E, ReturnValueSlot ReturnValue); |
3715 | |
3716 | RValue emitRotate(const CallExpr *E, bool IsRotateRight); |
3717 | |
3718 | /// Emit IR for __builtin_os_log_format. |
3719 | RValue emitBuiltinOSLogFormat(const CallExpr &E); |
3720 | |
3721 | llvm::Function *generateBuiltinOSLogHelperFunction( |
3722 | const analyze_os_log::OSLogBufferLayout &Layout, |
3723 | CharUnits BufferAlignment); |
3724 | |
3725 | RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue); |
3726 | |
3727 | /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call |
3728 | /// is unhandled by the current target. |
3729 | llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3730 | |
3731 | llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty, |
3732 | const llvm::CmpInst::Predicate Fp, |
3733 | const llvm::CmpInst::Predicate Ip, |
3734 | const llvm::Twine &Name = ""); |
3735 | llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E, |
3736 | llvm::Triple::ArchType Arch); |
3737 | |
3738 | llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID, |
3739 | unsigned LLVMIntrinsic, |
3740 | unsigned AltLLVMIntrinsic, |
3741 | const char *NameHint, |
3742 | unsigned Modifier, |
3743 | const CallExpr *E, |
3744 | SmallVectorImpl<llvm::Value *> &Ops, |
3745 | Address PtrOp0, Address PtrOp1, |
3746 | llvm::Triple::ArchType Arch); |
3747 | |
3748 | llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID, |
3749 | unsigned Modifier, llvm::Type *ArgTy, |
3750 | const CallExpr *E); |
3751 | llvm::Value *EmitNeonCall(llvm::Function *F, |
3752 | SmallVectorImpl<llvm::Value*> &O, |
3753 | const char *name, |
3754 | unsigned shift = 0, bool rightshift = false); |
3755 | llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx); |
3756 | llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty, |
3757 | bool negateForRightShift); |
3758 | llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt, |
3759 | llvm::Type *Ty, bool usgn, const char *name); |
3760 | llvm::Value *vectorWrapScalar16(llvm::Value *Op); |
3761 | llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E, |
3762 | llvm::Triple::ArchType Arch); |
3763 | |
3764 | llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops); |
3765 | llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3766 | llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3767 | llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3768 | llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3769 | llvm::Value *EmitNVPTXBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3770 | llvm::Value *EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, |
3771 | const CallExpr *E); |
3772 | llvm::Value *EmitHexagonBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
3773 | |
3774 | private: |
3775 | enum class MSVCIntrin; |
3776 | |
3777 | public: |
3778 | llvm::Value *EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID, const CallExpr *E); |
3779 | |
3780 | llvm::Value *EmitBuiltinAvailable(ArrayRef<llvm::Value *> Args); |
3781 | |
3782 | llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); |
3783 | llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); |
3784 | llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E); |
3785 | llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E); |
3786 | llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E); |
3787 | llvm::Value *EmitObjCCollectionLiteral(const Expr *E, |
3788 | const ObjCMethodDecl *MethodWithObjects); |
3789 | llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); |
3790 | RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, |
3791 | ReturnValueSlot Return = ReturnValueSlot()); |
3792 | |
3793 | /// Retrieves the default cleanup kind for an ARC cleanup. |
3794 | /// Except under -fobjc-arc-eh, ARC cleanups are normal-only. |
3795 | CleanupKind getARCCleanupKind() { |
3796 | return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions |
3797 | ? NormalAndEHCleanup : NormalCleanup; |
3798 | } |
3799 | |
3800 | // ARC primitives. |
3801 | void EmitARCInitWeak(Address addr, llvm::Value *value); |
3802 | void EmitARCDestroyWeak(Address addr); |
3803 | llvm::Value *EmitARCLoadWeak(Address addr); |
3804 | llvm::Value *EmitARCLoadWeakRetained(Address addr); |
3805 | llvm::Value *EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored); |
3806 | void emitARCCopyAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr); |
3807 | void emitARCMoveAssignWeak(QualType Ty, Address DstAddr, Address SrcAddr); |
3808 | void EmitARCCopyWeak(Address dst, Address src); |
3809 | void EmitARCMoveWeak(Address dst, Address src); |
3810 | llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value); |
3811 | llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value); |
3812 | llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value, |
3813 | bool resultIgnored); |
3814 | llvm::Value *EmitARCStoreStrongCall(Address addr, llvm::Value *value, |
3815 | bool resultIgnored); |
3816 | llvm::Value *EmitARCRetain(QualType type, llvm::Value *value); |
3817 | llvm::Value *EmitARCRetainNonBlock(llvm::Value *value); |
3818 | llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory); |
3819 | void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise); |
3820 | void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise); |
3821 | llvm::Value *EmitARCAutorelease(llvm::Value *value); |
3822 | llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value); |
3823 | llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value); |
3824 | llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value); |
3825 | llvm::Value *EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value); |
3826 | |
3827 | llvm::Value *EmitObjCAutorelease(llvm::Value *value, llvm::Type *returnType); |
3828 | llvm::Value *EmitObjCRetainNonBlock(llvm::Value *value, |
3829 | llvm::Type *returnType); |
3830 | void EmitObjCRelease(llvm::Value *value, ARCPreciseLifetime_t precise); |
3831 | |
3832 | std::pair<LValue,llvm::Value*> |
3833 | EmitARCStoreAutoreleasing(const BinaryOperator *e); |
3834 | std::pair<LValue,llvm::Value*> |
3835 | EmitARCStoreStrong(const BinaryOperator *e, bool ignored); |
3836 | std::pair<LValue,llvm::Value*> |
3837 | EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored); |
3838 | |
3839 | llvm::Value *EmitObjCAlloc(llvm::Value *value, |
3840 | llvm::Type *returnType); |
3841 | llvm::Value *EmitObjCAllocWithZone(llvm::Value *value, |
3842 | llvm::Type *returnType); |
3843 | llvm::Value *EmitObjCAllocInit(llvm::Value *value, llvm::Type *resultType); |
3844 | |
3845 | llvm::Value *EmitObjCThrowOperand(const Expr *expr); |
3846 | llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr); |
3847 | llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr); |
3848 | |
3849 | llvm::Value *EmitARCExtendBlockObject(const Expr *expr); |
3850 | llvm::Value *EmitARCReclaimReturnedObject(const Expr *e, |
3851 | bool allowUnsafeClaim); |
3852 | llvm::Value *EmitARCRetainScalarExpr(const Expr *expr); |
3853 | llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr); |
3854 | llvm::Value *EmitARCUnsafeUnretainedScalarExpr(const Expr *expr); |
3855 | |
3856 | void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values); |
3857 | |
3858 | static Destroyer destroyARCStrongImprecise; |
3859 | static Destroyer destroyARCStrongPrecise; |
3860 | static Destroyer destroyARCWeak; |
3861 | static Destroyer emitARCIntrinsicUse; |
3862 | static Destroyer destroyNonTrivialCStruct; |
3863 | |
3864 | void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr); |
3865 | llvm::Value *EmitObjCAutoreleasePoolPush(); |
3866 | llvm::Value *EmitObjCMRRAutoreleasePoolPush(); |
3867 | void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr); |
3868 | void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr); |
3869 | |
3870 | /// Emits a reference binding to the passed in expression. |
3871 | RValue EmitReferenceBindingToExpr(const Expr *E); |
3872 | |
3873 | //===--------------------------------------------------------------------===// |
3874 | // Expression Emission |
3875 | //===--------------------------------------------------------------------===// |
3876 | |
3877 | // Expressions are broken into three classes: scalar, complex, aggregate. |
3878 | |
3879 | /// EmitScalarExpr - Emit the computation of the specified expression of LLVM |
3880 | /// scalar type, returning the result. |
3881 | llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false); |
3882 | |
3883 | /// Emit a conversion from the specified type to the specified destination |
3884 | /// type, both of which are LLVM scalar types. |
3885 | llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, |
3886 | QualType DstTy, SourceLocation Loc); |
3887 | |
3888 | /// Emit a conversion from the specified complex type to the specified |
3889 | /// destination type, where the destination type is an LLVM scalar type. |
3890 | llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, |
3891 | QualType DstTy, |
3892 | SourceLocation Loc); |
3893 | |
3894 | /// EmitAggExpr - Emit the computation of the specified expression |
3895 | /// of aggregate type. The result is computed into the given slot, |
3896 | /// which may be null to indicate that the value is not needed. |
3897 | void EmitAggExpr(const Expr *E, AggValueSlot AS); |
3898 | |
3899 | /// EmitAggExprToLValue - Emit the computation of the specified expression of |
3900 | /// aggregate type into a temporary LValue. |
3901 | LValue EmitAggExprToLValue(const Expr *E); |
3902 | |
3903 | /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, |
3904 | /// make sure it survives garbage collection until this point. |
3905 | void EmitExtendGCLifetime(llvm::Value *object); |
3906 | |
3907 | /// EmitComplexExpr - Emit the computation of the specified expression of |
3908 | /// complex type, returning the result. |
3909 | ComplexPairTy EmitComplexExpr(const Expr *E, |
3910 | bool IgnoreReal = false, |
3911 | bool IgnoreImag = false); |
3912 | |
3913 | /// EmitComplexExprIntoLValue - Emit the given expression of complex |
3914 | /// type and place its result into the specified l-value. |
3915 | void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit); |
3916 | |
3917 | /// EmitStoreOfComplex - Store a complex number into the specified l-value. |
3918 | void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit); |
3919 | |
3920 | /// EmitLoadOfComplex - Load a complex number from the specified l-value. |
3921 | ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc); |
3922 | |
3923 | Address emitAddrOfRealComponent(Address complex, QualType complexType); |
3924 | Address emitAddrOfImagComponent(Address complex, QualType complexType); |
3925 | |
3926 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the |
3927 | /// global variable that has already been created for it. If the initializer |
3928 | /// has a different type than GV does, this may free GV and return a different |
3929 | /// one. Otherwise it just returns GV. |
3930 | llvm::GlobalVariable * |
3931 | AddInitializerToStaticVarDecl(const VarDecl &D, |
3932 | llvm::GlobalVariable *GV); |
3933 | |
3934 | // Emit an @llvm.invariant.start call for the given memory region. |
3935 | void EmitInvariantStart(llvm::Constant *Addr, CharUnits Size); |
3936 | |
3937 | /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++ |
3938 | /// variable with global storage. |
3939 | void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr, |
3940 | bool PerformInit); |
3941 | |
3942 | llvm::Function *createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, |
3943 | llvm::Constant *Addr); |
3944 | |
3945 | /// Call atexit() with a function that passes the given argument to |
3946 | /// the given function. |
3947 | void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn, |
3948 | llvm::Constant *addr); |
3949 | |
3950 | /// Call atexit() with function dtorStub. |
3951 | void registerGlobalDtorWithAtExit(llvm::Constant *dtorStub); |
3952 | |
3953 | /// Emit code in this function to perform a guarded variable |
3954 | /// initialization. Guarded initializations are used when it's not |
3955 | /// possible to prove that an initialization will be done exactly |
3956 | /// once, e.g. with a static local variable or a static data member |
3957 | /// of a class template. |
3958 | void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, |
3959 | bool PerformInit); |
3960 | |
3961 | enum class GuardKind { VariableGuard, TlsGuard }; |
3962 | |
3963 | /// Emit a branch to select whether or not to perform guarded initialization. |
3964 | void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, |
3965 | llvm::BasicBlock *InitBlock, |
3966 | llvm::BasicBlock *NoInitBlock, |
3967 | GuardKind Kind, const VarDecl *D); |
3968 | |
3969 | /// GenerateCXXGlobalInitFunc - Generates code for initializing global |
3970 | /// variables. |
3971 | void |
3972 | GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
3973 | ArrayRef<llvm::Function *> CXXThreadLocals, |
3974 | ConstantAddress Guard = ConstantAddress::invalid()); |
3975 | |
3976 | /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global |
3977 | /// variables. |
3978 | void GenerateCXXGlobalDtorsFunc( |
3979 | llvm::Function *Fn, |
3980 | const std::vector<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH, |
3981 | llvm::Constant *>> &DtorsAndObjects); |
3982 | |
3983 | void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
3984 | const VarDecl *D, |
3985 | llvm::GlobalVariable *Addr, |
3986 | bool PerformInit); |
3987 | |
3988 | void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest); |
3989 | |
3990 | void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp); |
3991 | |
3992 | void enterFullExpression(const FullExpr *E) { |
3993 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(E)) |
3994 | if (EWC->getNumObjects() == 0) |
3995 | return; |
3996 | enterNonTrivialFullExpression(E); |
3997 | } |
3998 | void enterNonTrivialFullExpression(const FullExpr *E); |
3999 | |
4000 | void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true); |
4001 | |
4002 | RValue EmitAtomicExpr(AtomicExpr *E); |
4003 | |
4004 | //===--------------------------------------------------------------------===// |
4005 | // Annotations Emission |
4006 | //===--------------------------------------------------------------------===// |
4007 | |
4008 | /// Emit an annotation call (intrinsic). |
4009 | llvm::Value *EmitAnnotationCall(llvm::Function *AnnotationFn, |
4010 | llvm::Value *AnnotatedVal, |
4011 | StringRef AnnotationStr, |
4012 | SourceLocation Location); |
4013 | |
4014 | /// Emit local annotations for the local variable V, declared by D. |
4015 | void EmitVarAnnotations(const VarDecl *D, llvm::Value *V); |
4016 | |
4017 | /// Emit field annotations for the given field & value. Returns the |
4018 | /// annotation result. |
4019 | Address EmitFieldAnnotations(const FieldDecl *D, Address V); |
4020 | |
4021 | //===--------------------------------------------------------------------===// |
4022 | // Internal Helpers |
4023 | //===--------------------------------------------------------------------===// |
4024 | |
4025 | /// ContainsLabel - Return true if the statement contains a label in it. If |
4026 | /// this statement is not executed normally, it not containing a label means |
4027 | /// that we can just remove the code. |
4028 | static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false); |
4029 | |
4030 | /// containsBreak - Return true if the statement contains a break out of it. |
4031 | /// If the statement (recursively) contains a switch or loop with a break |
4032 | /// inside of it, this is fine. |
4033 | static bool containsBreak(const Stmt *S); |
4034 | |
4035 | /// Determine if the given statement might introduce a declaration into the |
4036 | /// current scope, by being a (possibly-labelled) DeclStmt. |
4037 | static bool mightAddDeclToScope(const Stmt *S); |
4038 | |
4039 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
4040 | /// to a constant, or if it does but contains a label, return false. If it |
4041 | /// constant folds return true and set the boolean result in Result. |
4042 | bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, |
4043 | bool AllowLabels = false); |
4044 | |
4045 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
4046 | /// to a constant, or if it does but contains a label, return false. If it |
4047 | /// constant folds return true and set the folded value. |
4048 | bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result, |
4049 | bool AllowLabels = false); |
4050 | |
4051 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an |
4052 | /// if statement) to the specified blocks. Based on the condition, this might |
4053 | /// try to simplify the codegen of the conditional based on the branch. |
4054 | /// TrueCount should be the number of times we expect the condition to |
4055 | /// evaluate to true based on PGO data. |
4056 | void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, |
4057 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount); |
4058 | |
4059 | /// Given an assignment `*LHS = RHS`, emit a test that checks if \p RHS is |
4060 | /// nonnull, if \p LHS is marked _Nonnull. |
4061 | void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc); |
4062 | |
4063 | /// An enumeration which makes it easier to specify whether or not an |
4064 | /// operation is a subtraction. |
4065 | enum { NotSubtraction = false, IsSubtraction = true }; |
4066 | |
4067 | /// Same as IRBuilder::CreateInBoundsGEP, but additionally emits a check to |
4068 | /// detect undefined behavior when the pointer overflow sanitizer is enabled. |
4069 | /// \p SignedIndices indicates whether any of the GEP indices are signed. |
4070 | /// \p IsSubtraction indicates whether the expression used to form the GEP |
4071 | /// is a subtraction. |
4072 | llvm::Value *EmitCheckedInBoundsGEP(llvm::Value *Ptr, |
4073 | ArrayRef<llvm::Value *> IdxList, |
4074 | bool SignedIndices, |
4075 | bool IsSubtraction, |
4076 | SourceLocation Loc, |
4077 | const Twine &Name = ""); |
4078 | |
4079 | /// Specifies which type of sanitizer check to apply when handling a |
4080 | /// particular builtin. |
4081 | enum BuiltinCheckKind { |
4082 | BCK_CTZPassedZero, |
4083 | BCK_CLZPassedZero, |
4084 | }; |
4085 | |
4086 | /// Emits an argument for a call to a builtin. If the builtin sanitizer is |
4087 | /// enabled, a runtime check specified by \p Kind is also emitted. |
4088 | llvm::Value *EmitCheckedArgForBuiltin(const Expr *E, BuiltinCheckKind Kind); |
4089 | |
4090 | /// Emit a description of a type in a format suitable for passing to |
4091 | /// a runtime sanitizer handler. |
4092 | llvm::Constant *EmitCheckTypeDescriptor(QualType T); |
4093 | |
4094 | /// Convert a value into a format suitable for passing to a runtime |
4095 | /// sanitizer handler. |
4096 | llvm::Value *EmitCheckValue(llvm::Value *V); |
4097 | |
4098 | /// Emit a description of a source location in a format suitable for |
4099 | /// passing to a runtime sanitizer handler. |
4100 | llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc); |
4101 | |
4102 | /// Create a basic block that will either trap or call a handler function in |
4103 | /// the UBSan runtime with the provided arguments, and create a conditional |
4104 | /// branch to it. |
4105 | void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked, |
4106 | SanitizerHandler Check, ArrayRef<llvm::Constant *> StaticArgs, |
4107 | ArrayRef<llvm::Value *> DynamicArgs); |
4108 | |
4109 | /// Emit a slow path cross-DSO CFI check which calls __cfi_slowpath |
4110 | /// if Cond if false. |
4111 | void EmitCfiSlowPathCheck(SanitizerMask Kind, llvm::Value *Cond, |
4112 | llvm::ConstantInt *TypeId, llvm::Value *Ptr, |
4113 | ArrayRef<llvm::Constant *> StaticArgs); |
4114 | |
4115 | /// Emit a reached-unreachable diagnostic if \p Loc is valid and runtime |
4116 | /// checking is enabled. Otherwise, just emit an unreachable instruction. |
4117 | void EmitUnreachable(SourceLocation Loc); |
4118 | |
4119 | /// Create a basic block that will call the trap intrinsic, and emit a |
4120 | /// conditional branch to it, for the -ftrapv checks. |
4121 | void EmitTrapCheck(llvm::Value *Checked); |
4122 | |
4123 | /// Emit a call to trap or debugtrap and attach function attribute |
4124 | /// "trap-func-name" if specified. |
4125 | llvm::CallInst *EmitTrapCall(llvm::Intrinsic::ID IntrID); |
4126 | |
4127 | /// Emit a stub for the cross-DSO CFI check function. |
4128 | void EmitCfiCheckStub(); |
4129 | |
4130 | /// Emit a cross-DSO CFI failure handling function. |
4131 | void EmitCfiCheckFail(); |
4132 | |
4133 | /// Create a check for a function parameter that may potentially be |
4134 | /// declared as non-null. |
4135 | void EmitNonNullArgCheck(RValue RV, QualType ArgType, SourceLocation ArgLoc, |
4136 | AbstractCallee AC, unsigned ParmNum); |
4137 | |
4138 | /// EmitCallArg - Emit a single call argument. |
4139 | void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType); |
4140 | |
4141 | /// EmitDelegateCallArg - We are performing a delegate call; that |
4142 | /// is, the current function is delegating to another one. Produce |
4143 | /// a r-value suitable for passing the given parameter. |
4144 | void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, |
4145 | SourceLocation loc); |
4146 | |
4147 | /// SetFPAccuracy - Set the minimum required accuracy of the given floating |
4148 | /// point operation, expressed as the maximum relative error in ulp. |
4149 | void SetFPAccuracy(llvm::Value *Val, float Accuracy); |
4150 | |
4151 | private: |
4152 | llvm::MDNode *getRangeForLoadFromType(QualType Ty); |
4153 | void EmitReturnOfRValue(RValue RV, QualType Ty); |
4154 | |
4155 | void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New); |
4156 | |
4157 | llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4> |
4158 | DeferredReplacements; |
4159 | |
4160 | /// Set the address of a local variable. |
4161 | void setAddrOfLocalVar(const VarDecl *VD, Address Addr) { |
4162 | assert(!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!")((!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!" ) ? static_cast<void> (0) : __assert_fail ("!LocalDeclMap.count(VD) && \"Decl already exists in LocalDeclMap!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4162, __PRETTY_FUNCTION__)); |
4163 | LocalDeclMap.insert({VD, Addr}); |
4164 | } |
4165 | |
4166 | /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty |
4167 | /// from function arguments into \arg Dst. See ABIArgInfo::Expand. |
4168 | /// |
4169 | /// \param AI - The first function argument of the expansion. |
4170 | void ExpandTypeFromArgs(QualType Ty, LValue Dst, |
4171 | SmallVectorImpl<llvm::Value *>::iterator &AI); |
4172 | |
4173 | /// ExpandTypeToArgs - Expand an CallArg \arg Arg, with the LLVM type for \arg |
4174 | /// Ty, into individual arguments on the provided vector \arg IRCallArgs, |
4175 | /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand. |
4176 | void ExpandTypeToArgs(QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy, |
4177 | SmallVectorImpl<llvm::Value *> &IRCallArgs, |
4178 | unsigned &IRCallArgPos); |
4179 | |
4180 | llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info, |
4181 | const Expr *InputExpr, std::string &ConstraintStr); |
4182 | |
4183 | llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, |
4184 | LValue InputValue, QualType InputType, |
4185 | std::string &ConstraintStr, |
4186 | SourceLocation Loc); |
4187 | |
4188 | /// Attempts to statically evaluate the object size of E. If that |
4189 | /// fails, emits code to figure the size of E out for us. This is |
4190 | /// pass_object_size aware. |
4191 | /// |
4192 | /// If EmittedExpr is non-null, this will use that instead of re-emitting E. |
4193 | llvm::Value *evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type, |
4194 | llvm::IntegerType *ResType, |
4195 | llvm::Value *EmittedE, |
4196 | bool IsDynamic); |
4197 | |
4198 | /// Emits the size of E, as required by __builtin_object_size. This |
4199 | /// function is aware of pass_object_size parameters, and will act accordingly |
4200 | /// if E is a parameter with the pass_object_size attribute. |
4201 | llvm::Value *emitBuiltinObjectSize(const Expr *E, unsigned Type, |
4202 | llvm::IntegerType *ResType, |
4203 | llvm::Value *EmittedE, |
4204 | bool IsDynamic); |
4205 | |
4206 | void emitZeroOrPatternForAutoVarInit(QualType type, const VarDecl &D, |
4207 | Address Loc); |
4208 | |
4209 | public: |
4210 | #ifndef NDEBUG |
4211 | // Determine whether the given argument is an Objective-C method |
4212 | // that may have type parameters in its signature. |
4213 | static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) { |
4214 | const DeclContext *dc = method->getDeclContext(); |
4215 | if (const ObjCInterfaceDecl *classDecl= dyn_cast<ObjCInterfaceDecl>(dc)) { |
4216 | return classDecl->getTypeParamListAsWritten(); |
4217 | } |
4218 | |
4219 | if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) { |
4220 | return catDecl->getTypeParamList(); |
4221 | } |
4222 | |
4223 | return false; |
4224 | } |
4225 | |
4226 | template<typename T> |
4227 | static bool isObjCMethodWithTypeParams(const T *) { return false; } |
4228 | #endif |
4229 | |
4230 | enum class EvaluationOrder { |
4231 | ///! No language constraints on evaluation order. |
4232 | Default, |
4233 | ///! Language semantics require left-to-right evaluation. |
4234 | ForceLeftToRight, |
4235 | ///! Language semantics require right-to-left evaluation. |
4236 | ForceRightToLeft |
4237 | }; |
4238 | |
4239 | /// EmitCallArgs - Emit call arguments for a function. |
4240 | template <typename T> |
4241 | void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo, |
4242 | llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange, |
4243 | AbstractCallee AC = AbstractCallee(), |
4244 | unsigned ParamsToSkip = 0, |
4245 | EvaluationOrder Order = EvaluationOrder::Default) { |
4246 | SmallVector<QualType, 16> ArgTypes; |
4247 | CallExpr::const_arg_iterator Arg = ArgRange.begin(); |
4248 | |
4249 | assert((ParamsToSkip == 0 || CallArgTypeInfo) &&(((ParamsToSkip == 0 || CallArgTypeInfo) && "Can't skip parameters if type info is not provided" ) ? static_cast<void> (0) : __assert_fail ("(ParamsToSkip == 0 || CallArgTypeInfo) && \"Can't skip parameters if type info is not provided\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4250, __PRETTY_FUNCTION__)) |
4250 | "Can't skip parameters if type info is not provided")(((ParamsToSkip == 0 || CallArgTypeInfo) && "Can't skip parameters if type info is not provided" ) ? static_cast<void> (0) : __assert_fail ("(ParamsToSkip == 0 || CallArgTypeInfo) && \"Can't skip parameters if type info is not provided\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4250, __PRETTY_FUNCTION__)); |
4251 | if (CallArgTypeInfo) { |
4252 | #ifndef NDEBUG |
4253 | bool isGenericMethod = isObjCMethodWithTypeParams(CallArgTypeInfo); |
4254 | #endif |
4255 | |
4256 | // First, use the argument types that the type info knows about |
4257 | for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip, |
4258 | E = CallArgTypeInfo->param_type_end(); |
4259 | I != E; ++I, ++Arg) { |
4260 | assert(Arg != ArgRange.end() && "Running over edge of argument list!")((Arg != ArgRange.end() && "Running over edge of argument list!" ) ? static_cast<void> (0) : __assert_fail ("Arg != ArgRange.end() && \"Running over edge of argument list!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4260, __PRETTY_FUNCTION__)); |
4261 | assert((isGenericMethod ||(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4262 | ((*I)->isVariablyModifiedType() ||(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4263 | (*I).getNonReferenceType()->isObjCRetainableType() ||(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4264 | getContext()(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4265 | .getCanonicalType((*I).getNonReferenceType())(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4266 | .getTypePtr() ==(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4267 | getContext()(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4268 | .getCanonicalType((*Arg)->getType())(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4269 | .getTypePtr())) &&(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)) |
4270 | "type mismatch in call argument!")(((isGenericMethod || ((*I)->isVariablyModifiedType() || ( *I).getNonReferenceType()->isObjCRetainableType() || getContext () .getCanonicalType((*I).getNonReferenceType()) .getTypePtr( ) == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr ())) && "type mismatch in call argument!") ? static_cast <void> (0) : __assert_fail ("(isGenericMethod || ((*I)->isVariablyModifiedType() || (*I).getNonReferenceType()->isObjCRetainableType() || getContext() .getCanonicalType((*I).getNonReferenceType()) .getTypePtr() == getContext() .getCanonicalType((*Arg)->getType()) .getTypePtr())) && \"type mismatch in call argument!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4270, __PRETTY_FUNCTION__)); |
4271 | ArgTypes.push_back(*I); |
4272 | } |
4273 | } |
4274 | |
4275 | // Either we've emitted all the call args, or we have a call to variadic |
4276 | // function. |
4277 | assert((Arg == ArgRange.end() || !CallArgTypeInfo ||(((Arg == ArgRange.end() || !CallArgTypeInfo || CallArgTypeInfo ->isVariadic()) && "Extra arguments in non-variadic function!" ) ? static_cast<void> (0) : __assert_fail ("(Arg == ArgRange.end() || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) && \"Extra arguments in non-variadic function!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4279, __PRETTY_FUNCTION__)) |
4278 | CallArgTypeInfo->isVariadic()) &&(((Arg == ArgRange.end() || !CallArgTypeInfo || CallArgTypeInfo ->isVariadic()) && "Extra arguments in non-variadic function!" ) ? static_cast<void> (0) : __assert_fail ("(Arg == ArgRange.end() || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) && \"Extra arguments in non-variadic function!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4279, __PRETTY_FUNCTION__)) |
4279 | "Extra arguments in non-variadic function!")(((Arg == ArgRange.end() || !CallArgTypeInfo || CallArgTypeInfo ->isVariadic()) && "Extra arguments in non-variadic function!" ) ? static_cast<void> (0) : __assert_fail ("(Arg == ArgRange.end() || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) && \"Extra arguments in non-variadic function!\"" , "/build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/CodeGen/CodeGenFunction.h" , 4279, __PRETTY_FUNCTION__)); |
4280 | |
4281 | // If we still have any arguments, emit them using the type of the argument. |
4282 | for (auto *A : llvm::make_range(Arg, ArgRange.end())) |
4283 | ArgTypes.push_back(CallArgTypeInfo ? getVarArgType(A) : A->getType()); |
4284 | |
4285 | EmitCallArgs(Args, ArgTypes, ArgRange, AC, ParamsToSkip, Order); |
4286 | } |
4287 | |
4288 | void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes, |
4289 | llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange, |
4290 | AbstractCallee AC = AbstractCallee(), |
4291 | unsigned ParamsToSkip = 0, |
4292 | EvaluationOrder Order = EvaluationOrder::Default); |
4293 | |
4294 | /// EmitPointerWithAlignment - Given an expression with a pointer type, |
4295 | /// emit the value and compute our best estimate of the alignment of the |
4296 | /// pointee. |
4297 | /// |
4298 | /// \param BaseInfo - If non-null, this will be initialized with |
4299 | /// information about the source of the alignment and the may-alias |
4300 | /// attribute. Note that this function will conservatively fall back on |
4301 | /// the type when it doesn't recognize the expression and may-alias will |
4302 | /// be set to false. |
4303 | /// |
4304 | /// One reasonable way to use this information is when there's a language |
4305 | /// guarantee that the pointer must be aligned to some stricter value, and |
4306 | /// we're simply trying to ensure that sufficiently obvious uses of under- |
4307 | /// aligned objects don't get miscompiled; for example, a placement new |
4308 | /// into the address of a local variable. In such a case, it's quite |
4309 | /// reasonable to just ignore the returned alignment when it isn't from an |
4310 | /// explicit source. |
4311 | Address EmitPointerWithAlignment(const Expr *Addr, |
4312 | LValueBaseInfo *BaseInfo = nullptr, |
4313 | TBAAAccessInfo *TBAAInfo = nullptr); |
4314 | |
4315 | /// If \p E references a parameter with pass_object_size info or a constant |
4316 | /// array size modifier, emit the object size divided by the size of \p EltTy. |
4317 | /// Otherwise return null. |
4318 | llvm::Value *LoadPassedObjectSize(const Expr *E, QualType EltTy); |
4319 | |
4320 | void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK); |
4321 | |
4322 | struct MultiVersionResolverOption { |
4323 | llvm::Function *Function; |
4324 | FunctionDecl *FD; |
4325 | struct Conds { |
4326 | StringRef Architecture; |
4327 | llvm::SmallVector<StringRef, 8> Features; |
4328 | |
4329 | Conds(StringRef Arch, ArrayRef<StringRef> Feats) |
4330 | : Architecture(Arch), Features(Feats.begin(), Feats.end()) {} |
4331 | } Conditions; |
4332 | |
4333 | MultiVersionResolverOption(llvm::Function *F, StringRef Arch, |
4334 | ArrayRef<StringRef> Feats) |
4335 | : Function(F), Conditions(Arch, Feats) {} |
4336 | }; |
4337 | |
4338 | // Emits the body of a multiversion function's resolver. Assumes that the |
4339 | // options are already sorted in the proper order, with the 'default' option |
4340 | // last (if it exists). |
4341 | void EmitMultiVersionResolver(llvm::Function *Resolver, |
4342 | ArrayRef<MultiVersionResolverOption> Options); |
4343 | |
4344 | static uint64_t GetX86CpuSupportsMask(ArrayRef<StringRef> FeatureStrs); |
4345 | |
4346 | private: |
4347 | QualType getVarArgType(const Expr *Arg); |
4348 | |
4349 | void EmitDeclMetadata(); |
4350 | |
4351 | BlockByrefHelpers *buildByrefHelpers(llvm::StructType &byrefType, |
4352 | const AutoVarEmission &emission); |
4353 | |
4354 | void AddObjCARCExceptionMetadata(llvm::Instruction *Inst); |
4355 | |
4356 | llvm::Value *GetValueForARMHint(unsigned BuiltinID); |
4357 | llvm::Value *EmitX86CpuIs(const CallExpr *E); |
4358 | llvm::Value *EmitX86CpuIs(StringRef CPUStr); |
4359 | llvm::Value *EmitX86CpuSupports(const CallExpr *E); |
4360 | llvm::Value *EmitX86CpuSupports(ArrayRef<StringRef> FeatureStrs); |
4361 | llvm::Value *EmitX86CpuSupports(uint64_t Mask); |
4362 | llvm::Value *EmitX86CpuInit(); |
4363 | llvm::Value *FormResolverCondition(const MultiVersionResolverOption &RO); |
4364 | }; |
4365 | |
4366 | inline DominatingLLVMValue::saved_type |
4367 | DominatingLLVMValue::save(CodeGenFunction &CGF, llvm::Value *value) { |
4368 | if (!needsSaving(value)) return saved_type(value, false); |
4369 | |
4370 | // Otherwise, we need an alloca. |
4371 | auto align = CharUnits::fromQuantity( |
4372 | CGF.CGM.getDataLayout().getPrefTypeAlignment(value->getType())); |
4373 | Address alloca = |
4374 | CGF.CreateTempAlloca(value->getType(), align, "cond-cleanup.save"); |
4375 | CGF.Builder.CreateStore(value, alloca); |
4376 | |
4377 | return saved_type(alloca.getPointer(), true); |
4378 | } |
4379 | |
4380 | inline llvm::Value *DominatingLLVMValue::restore(CodeGenFunction &CGF, |
4381 | saved_type value) { |
4382 | // If the value says it wasn't saved, trust that it's still dominating. |
4383 | if (!value.getInt()) return value.getPointer(); |
4384 | |
4385 | // Otherwise, it should be an alloca instruction, as set up in save(). |
4386 | auto alloca = cast<llvm::AllocaInst>(value.getPointer()); |
4387 | return CGF.Builder.CreateAlignedLoad(alloca, alloca->getAlignment()); |
4388 | } |
4389 | |
4390 | } // end namespace CodeGen |
4391 | } // end namespace clang |
4392 | |
4393 | #endif |