1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "CodeGenModule.h" |
14 | #include "CGBlocks.h" |
15 | #include "CGCUDARuntime.h" |
16 | #include "CGCXXABI.h" |
17 | #include "CGCall.h" |
18 | #include "CGDebugInfo.h" |
19 | #include "CGObjCRuntime.h" |
20 | #include "CGOpenCLRuntime.h" |
21 | #include "CGOpenMPRuntime.h" |
22 | #include "CGOpenMPRuntimeNVPTX.h" |
23 | #include "CodeGenFunction.h" |
24 | #include "CodeGenPGO.h" |
25 | #include "ConstantEmitter.h" |
26 | #include "CoverageMappingGen.h" |
27 | #include "TargetInfo.h" |
28 | #include "clang/AST/ASTContext.h" |
29 | #include "clang/AST/CharUnits.h" |
30 | #include "clang/AST/DeclCXX.h" |
31 | #include "clang/AST/DeclObjC.h" |
32 | #include "clang/AST/DeclTemplate.h" |
33 | #include "clang/AST/Mangle.h" |
34 | #include "clang/AST/RecordLayout.h" |
35 | #include "clang/AST/RecursiveASTVisitor.h" |
36 | #include "clang/AST/StmtVisitor.h" |
37 | #include "clang/Basic/Builtins.h" |
38 | #include "clang/Basic/CharInfo.h" |
39 | #include "clang/Basic/CodeGenOptions.h" |
40 | #include "clang/Basic/Diagnostic.h" |
41 | #include "clang/Basic/Module.h" |
42 | #include "clang/Basic/SourceManager.h" |
43 | #include "clang/Basic/TargetInfo.h" |
44 | #include "clang/Basic/Version.h" |
45 | #include "clang/CodeGen/ConstantInitBuilder.h" |
46 | #include "clang/Frontend/FrontendDiagnostic.h" |
47 | #include "llvm/ADT/StringSwitch.h" |
48 | #include "llvm/ADT/Triple.h" |
49 | #include "llvm/Analysis/TargetLibraryInfo.h" |
50 | #include "llvm/IR/CallingConv.h" |
51 | #include "llvm/IR/DataLayout.h" |
52 | #include "llvm/IR/Intrinsics.h" |
53 | #include "llvm/IR/LLVMContext.h" |
54 | #include "llvm/IR/Module.h" |
55 | #include "llvm/IR/ProfileSummary.h" |
56 | #include "llvm/ProfileData/InstrProfReader.h" |
57 | #include "llvm/Support/CodeGen.h" |
58 | #include "llvm/Support/ConvertUTF.h" |
59 | #include "llvm/Support/ErrorHandling.h" |
60 | #include "llvm/Support/MD5.h" |
61 | |
62 | using namespace clang; |
63 | using namespace CodeGen; |
64 | |
65 | static llvm::cl::opt<bool> LimitedCoverage( |
66 | "limited-coverage-experimental", llvm::cl::ZeroOrMore, llvm::cl::Hidden, |
67 | llvm::cl::desc("Emit limited coverage mapping information (experimental)"), |
68 | llvm::cl::init(false)); |
69 | |
70 | static const char AnnotationSection[] = "llvm.metadata"; |
71 | |
72 | static CGCXXABI *createCXXABI(CodeGenModule &CGM) { |
73 | switch (CGM.getTarget().getCXXABI().getKind()) { |
74 | case TargetCXXABI::GenericAArch64: |
75 | case TargetCXXABI::GenericARM: |
76 | case TargetCXXABI::iOS: |
77 | case TargetCXXABI::iOS64: |
78 | case TargetCXXABI::WatchOS: |
79 | case TargetCXXABI::GenericMIPS: |
80 | case TargetCXXABI::GenericItanium: |
81 | case TargetCXXABI::WebAssembly: |
82 | return CreateItaniumCXXABI(CGM); |
83 | case TargetCXXABI::Microsoft: |
84 | return CreateMicrosoftCXXABI(CGM); |
85 | } |
86 | |
87 | llvm_unreachable("invalid C++ ABI kind"); |
88 | } |
89 | |
90 | CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO, |
91 | const PreprocessorOptions &PPO, |
92 | const CodeGenOptions &CGO, llvm::Module &M, |
93 | DiagnosticsEngine &diags, |
94 | CoverageSourceInfo *CoverageInfo) |
95 | : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO), |
96 | PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), |
97 | Target(C.getTargetInfo()), ABI(createCXXABI(*this)), |
98 | VMContext(M.getContext()), Types(*this), VTables(*this), |
99 | SanitizerMD(new SanitizerMetadata(*this)) { |
100 | |
101 | |
102 | llvm::LLVMContext &LLVMContext = M.getContext(); |
103 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
104 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
105 | Int16Ty = llvm::Type::getInt16Ty(LLVMContext); |
106 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
107 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
108 | HalfTy = llvm::Type::getHalfTy(LLVMContext); |
109 | FloatTy = llvm::Type::getFloatTy(LLVMContext); |
110 | DoubleTy = llvm::Type::getDoubleTy(LLVMContext); |
111 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
112 | PointerAlignInBytes = |
113 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
114 | SizeSizeInBytes = |
115 | C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity(); |
116 | IntAlignInBytes = |
117 | C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity(); |
118 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
119 | IntPtrTy = llvm::IntegerType::get(LLVMContext, |
120 | C.getTargetInfo().getMaxPointerWidth()); |
121 | Int8PtrTy = Int8Ty->getPointerTo(0); |
122 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
123 | AllocaInt8PtrTy = Int8Ty->getPointerTo( |
124 | M.getDataLayout().getAllocaAddrSpace()); |
125 | ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace(); |
126 | |
127 | RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); |
128 | |
129 | if (LangOpts.ObjC) |
130 | createObjCRuntime(); |
131 | if (LangOpts.OpenCL) |
132 | createOpenCLRuntime(); |
133 | if (LangOpts.OpenMP) |
134 | createOpenMPRuntime(); |
135 | if (LangOpts.CUDA) |
136 | createCUDARuntime(); |
137 | |
138 | |
139 | if (LangOpts.Sanitize.has(SanitizerKind::Thread) || |
140 | (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)) |
141 | TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(), |
142 | getCXXABI().getMangleContext())); |
143 | |
144 | |
145 | |
146 | if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo || |
147 | CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) |
148 | DebugInfo.reset(new CGDebugInfo(*this)); |
149 | |
150 | Block.GlobalUniqueCount = 0; |
151 | |
152 | if (C.getLangOpts().ObjC) |
153 | ObjCData.reset(new ObjCEntrypoints()); |
154 | |
155 | if (CodeGenOpts.hasProfileClangUse()) { |
156 | auto ReaderOrErr = llvm::IndexedInstrProfReader::create( |
157 | CodeGenOpts.ProfileInstrumentUsePath, CodeGenOpts.ProfileRemappingFile); |
158 | if (auto E = ReaderOrErr.takeError()) { |
159 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
160 | "Could not read profile %0: %1"); |
161 | llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) { |
162 | getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath |
163 | << EI.message(); |
164 | }); |
165 | } else |
166 | PGOReader = std::move(ReaderOrErr.get()); |
167 | } |
168 | |
169 | |
170 | |
171 | if (CodeGenOpts.CoverageMapping) |
172 | CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo)); |
173 | } |
174 | |
175 | CodeGenModule::~CodeGenModule() {} |
176 | |
177 | void CodeGenModule::createObjCRuntime() { |
178 | |
179 | |
180 | switch (LangOpts.ObjCRuntime.getKind()) { |
181 | case ObjCRuntime::GNUstep: |
182 | case ObjCRuntime::GCC: |
183 | case ObjCRuntime::ObjFW: |
184 | ObjCRuntime.reset(CreateGNUObjCRuntime(*this)); |
185 | return; |
186 | |
187 | case ObjCRuntime::FragileMacOSX: |
188 | case ObjCRuntime::MacOSX: |
189 | case ObjCRuntime::iOS: |
190 | case ObjCRuntime::WatchOS: |
191 | ObjCRuntime.reset(CreateMacObjCRuntime(*this)); |
192 | return; |
193 | } |
194 | llvm_unreachable("bad runtime kind"); |
195 | } |
196 | |
197 | void CodeGenModule::createOpenCLRuntime() { |
198 | OpenCLRuntime.reset(new CGOpenCLRuntime(*this)); |
199 | } |
200 | |
201 | void CodeGenModule::createOpenMPRuntime() { |
202 | |
203 | |
204 | switch (getTriple().getArch()) { |
205 | case llvm::Triple::nvptx: |
206 | case llvm::Triple::nvptx64: |
207 | (0) . __assert_fail ("getLangOpts().OpenMPIsDevice && \"OpenMP NVPTX is only prepared to deal with device code.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 208, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getLangOpts().OpenMPIsDevice && |
208 | (0) . __assert_fail ("getLangOpts().OpenMPIsDevice && \"OpenMP NVPTX is only prepared to deal with device code.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 208, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "OpenMP NVPTX is only prepared to deal with device code."); |
209 | OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this)); |
210 | break; |
211 | default: |
212 | if (LangOpts.OpenMPSimd) |
213 | OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this)); |
214 | else |
215 | OpenMPRuntime.reset(new CGOpenMPRuntime(*this)); |
216 | break; |
217 | } |
218 | } |
219 | |
220 | void CodeGenModule::createCUDARuntime() { |
221 | CUDARuntime.reset(CreateNVCUDARuntime(*this)); |
222 | } |
223 | |
224 | void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) { |
225 | Replacements[Name] = C; |
226 | } |
227 | |
228 | void CodeGenModule::applyReplacements() { |
229 | for (auto &I : Replacements) { |
230 | StringRef MangledName = I.first(); |
231 | llvm::Constant *Replacement = I.second; |
232 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
233 | if (!Entry) |
234 | continue; |
235 | auto *OldF = cast<llvm::Function>(Entry); |
236 | auto *NewF = dyn_cast<llvm::Function>(Replacement); |
237 | if (!NewF) { |
238 | if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) { |
239 | NewF = dyn_cast<llvm::Function>(Alias->getAliasee()); |
240 | } else { |
241 | auto *CE = cast<llvm::ConstantExpr>(Replacement); |
242 | getOpcode() == llvm..Instruction..BitCast || CE->getOpcode() == llvm..Instruction..GetElementPtr", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 243, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CE->getOpcode() == llvm::Instruction::BitCast || |
243 | getOpcode() == llvm..Instruction..BitCast || CE->getOpcode() == llvm..Instruction..GetElementPtr", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 243, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CE->getOpcode() == llvm::Instruction::GetElementPtr); |
244 | NewF = dyn_cast<llvm::Function>(CE->getOperand(0)); |
245 | } |
246 | } |
247 | |
248 | |
249 | OldF->replaceAllUsesWith(Replacement); |
250 | if (NewF) { |
251 | NewF->removeFromParent(); |
252 | OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(), |
253 | NewF); |
254 | } |
255 | OldF->eraseFromParent(); |
256 | } |
257 | } |
258 | |
259 | void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) { |
260 | GlobalValReplacements.push_back(std::make_pair(GV, C)); |
261 | } |
262 | |
263 | void CodeGenModule::applyGlobalValReplacements() { |
264 | for (auto &I : GlobalValReplacements) { |
265 | llvm::GlobalValue *GV = I.first; |
266 | llvm::Constant *C = I.second; |
267 | |
268 | GV->replaceAllUsesWith(C); |
269 | GV->eraseFromParent(); |
270 | } |
271 | } |
272 | |
273 | |
274 | |
275 | static const llvm::GlobalObject *getAliasedGlobal( |
276 | const llvm::GlobalIndirectSymbol &GIS) { |
277 | llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited; |
278 | const llvm::Constant *C = &GIS; |
279 | for (;;) { |
280 | C = C->stripPointerCasts(); |
281 | if (auto *GO = dyn_cast<llvm::GlobalObject>(C)) |
282 | return GO; |
283 | |
284 | auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C); |
285 | if (!GIS2) |
286 | return nullptr; |
287 | if (!Visited.insert(GIS2).second) |
288 | return nullptr; |
289 | C = GIS2->getIndirectSymbol(); |
290 | } |
291 | } |
292 | |
293 | void CodeGenModule::checkAliases() { |
294 | |
295 | |
296 | |
297 | bool Error = false; |
298 | DiagnosticsEngine &Diags = getDiags(); |
299 | for (const GlobalDecl &GD : Aliases) { |
300 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
301 | SourceLocation Location; |
302 | bool IsIFunc = D->hasAttr<IFuncAttr>(); |
303 | if (const Attr *A = D->getDefiningAttr()) |
304 | Location = A->getLocation(); |
305 | else |
306 | llvm_unreachable("Not an alias or ifunc?"); |
307 | StringRef MangledName = getMangledName(GD); |
308 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
309 | auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry); |
310 | const llvm::GlobalValue *GV = getAliasedGlobal(*Alias); |
311 | if (!GV) { |
312 | Error = true; |
313 | Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc; |
314 | } else if (GV->isDeclaration()) { |
315 | Error = true; |
316 | Diags.Report(Location, diag::err_alias_to_undefined) |
317 | << IsIFunc << IsIFunc; |
318 | } else if (IsIFunc) { |
319 | |
320 | llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>( |
321 | GV->getType()->getPointerElementType()); |
322 | assert(FTy); |
323 | if (!FTy->getReturnType()->isPointerTy()) |
324 | Diags.Report(Location, diag::err_ifunc_resolver_return); |
325 | } |
326 | |
327 | llvm::Constant *Aliasee = Alias->getIndirectSymbol(); |
328 | llvm::GlobalValue *AliaseeGV; |
329 | if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee)) |
330 | AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0)); |
331 | else |
332 | AliaseeGV = cast<llvm::GlobalValue>(Aliasee); |
333 | |
334 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
335 | StringRef AliasSection = SA->getName(); |
336 | if (AliasSection != AliaseeGV->getSection()) |
337 | Diags.Report(SA->getLocation(), diag::warn_alias_with_section) |
338 | << AliasSection << IsIFunc << IsIFunc; |
339 | } |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) { |
347 | if (GA->isInterposable()) { |
348 | Diags.Report(Location, diag::warn_alias_to_weak_alias) |
349 | << GV->getName() << GA->getName() << IsIFunc; |
350 | Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
351 | GA->getIndirectSymbol(), Alias->getType()); |
352 | Alias->setIndirectSymbol(Aliasee); |
353 | } |
354 | } |
355 | } |
356 | if (!Error) |
357 | return; |
358 | |
359 | for (const GlobalDecl &GD : Aliases) { |
360 | StringRef MangledName = getMangledName(GD); |
361 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
362 | auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry); |
363 | Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType())); |
364 | Alias->eraseFromParent(); |
365 | } |
366 | } |
367 | |
368 | void CodeGenModule::clear() { |
369 | DeferredDeclsToEmit.clear(); |
370 | if (OpenMPRuntime) |
371 | OpenMPRuntime->clear(); |
372 | } |
373 | |
374 | void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags, |
375 | StringRef MainFile) { |
376 | if (!hasDiagnostics()) |
377 | return; |
378 | if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) { |
379 | if (MainFile.empty()) |
380 | MainFile = "<stdin>"; |
381 | Diags.Report(diag::warn_profile_data_unprofiled) << MainFile; |
382 | } else { |
383 | if (Mismatched > 0) |
384 | Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched; |
385 | |
386 | if (Missing > 0) |
387 | Diags.Report(diag::warn_profile_data_missing) << Visited << Missing; |
388 | } |
389 | } |
390 | |
391 | void CodeGenModule::Release() { |
392 | EmitDeferred(); |
393 | EmitVTablesOpportunistically(); |
394 | applyGlobalValReplacements(); |
395 | applyReplacements(); |
396 | checkAliases(); |
397 | emitMultiVersionFunctions(); |
398 | EmitCXXGlobalInitFunc(); |
399 | EmitCXXGlobalDtorFunc(); |
400 | registerGlobalDtorsWithAtExit(); |
401 | EmitCXXThreadLocalInitFunc(); |
402 | if (ObjCRuntime) |
403 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
404 | AddGlobalCtor(ObjCInitFunction); |
405 | if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice && |
406 | CUDARuntime) { |
407 | if (llvm::Function *CudaCtorFunction = |
408 | CUDARuntime->makeModuleCtorFunction()) |
409 | AddGlobalCtor(CudaCtorFunction); |
410 | } |
411 | if (OpenMPRuntime) { |
412 | if (llvm::Function *OpenMPRegistrationFunction = |
413 | OpenMPRuntime->emitRegistrationFunction()) { |
414 | auto ComdatKey = OpenMPRegistrationFunction->hasComdat() ? |
415 | OpenMPRegistrationFunction : nullptr; |
416 | AddGlobalCtor(OpenMPRegistrationFunction, 0, ComdatKey); |
417 | } |
418 | OpenMPRuntime->clear(); |
419 | } |
420 | if (PGOReader) { |
421 | getModule().setProfileSummary( |
422 | PGOReader->getSummary( false).getMD(VMContext), |
423 | llvm::ProfileSummary::PSK_Instr); |
424 | if (PGOStats.hasDiagnostics()) |
425 | PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName); |
426 | } |
427 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
428 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
429 | EmitGlobalAnnotations(); |
430 | EmitStaticExternCAliases(); |
431 | EmitDeferredUnusedCoverageMappings(); |
432 | if (CoverageMapping) |
433 | CoverageMapping->emit(); |
434 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
435 | CodeGenFunction(*this).EmitCfiCheckFail(); |
436 | CodeGenFunction(*this).EmitCfiCheckStub(); |
437 | } |
438 | emitAtAvailableLinkGuard(); |
439 | emitLLVMUsed(); |
440 | if (SanStats) |
441 | SanStats->finish(); |
442 | |
443 | if (CodeGenOpts.Autolink && |
444 | (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { |
445 | EmitModuleLinkOptions(); |
446 | } |
447 | |
448 | |
449 | if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
450 | getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters", |
451 | CodeGenOpts.NumRegisterParameters); |
452 | |
453 | if (CodeGenOpts.DwarfVersion) { |
454 | |
455 | |
456 | getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version", |
457 | CodeGenOpts.DwarfVersion); |
458 | } |
459 | if (CodeGenOpts.EmitCodeView) { |
460 | |
461 | getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1); |
462 | } |
463 | if (CodeGenOpts.CodeViewGHash) { |
464 | getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1); |
465 | } |
466 | if (CodeGenOpts.ControlFlowGuard) { |
467 | |
468 | getModule().addModuleFlag(llvm::Module::Warning, "cfguardtable", 1); |
469 | } |
470 | if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) { |
471 | |
472 | |
473 | |
474 | |
475 | getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1); |
476 | |
477 | llvm::Metadata *Ops[2] = { |
478 | llvm::MDString::get(VMContext, "StrictVTablePointers"), |
479 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
480 | llvm::Type::getInt32Ty(VMContext), 1))}; |
481 | |
482 | getModule().addModuleFlag(llvm::Module::Require, |
483 | "StrictVTablePointersRequirement", |
484 | llvm::MDNode::get(VMContext, Ops)); |
485 | } |
486 | if (DebugInfo) |
487 | |
488 | |
489 | |
490 | getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version", |
491 | llvm::DEBUG_METADATA_VERSION); |
492 | |
493 | |
494 | |
495 | |
496 | uint64_t WCharWidth = |
497 | Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity(); |
498 | getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth); |
499 | |
500 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
501 | if ( Arch == llvm::Triple::arm |
502 | || Arch == llvm::Triple::armeb |
503 | || Arch == llvm::Triple::thumb |
504 | || Arch == llvm::Triple::thumbeb) { |
505 | |
506 | uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4; |
507 | getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth); |
508 | } |
509 | |
510 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
511 | |
512 | getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1); |
513 | } |
514 | |
515 | if (CodeGenOpts.CFProtectionReturn && |
516 | Target.checkCFProtectionReturnSupported(getDiags())) { |
517 | |
518 | getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return", |
519 | 1); |
520 | } |
521 | |
522 | if (CodeGenOpts.CFProtectionBranch && |
523 | Target.checkCFProtectionBranchSupported(getDiags())) { |
524 | |
525 | getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch", |
526 | 1); |
527 | } |
528 | |
529 | if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) { |
530 | |
531 | |
532 | |
533 | getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz", |
534 | CodeGenOpts.FlushDenorm ? 1 : 0); |
535 | } |
536 | |
537 | |
538 | if (LangOpts.OpenCL) { |
539 | EmitOpenCLMetadata(); |
540 | |
541 | if (getTriple().getArch() == llvm::Triple::spir || |
542 | getTriple().getArch() == llvm::Triple::spir64) { |
543 | |
544 | |
545 | llvm::Metadata *SPIRVerElts[] = { |
546 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
547 | Int32Ty, LangOpts.OpenCLVersion / 100)), |
548 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
549 | Int32Ty, (LangOpts.OpenCLVersion / 100 > 1) ? 0 : 2))}; |
550 | llvm::NamedMDNode *SPIRVerMD = |
551 | TheModule.getOrInsertNamedMetadata("opencl.spir.version"); |
552 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
553 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
554 | } |
555 | } |
556 | |
557 | if (uint32_t PLevel = Context.getLangOpts().PICLevel) { |
558 | (0) . __assert_fail ("PLevel < 3 && \"Invalid PIC Level\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 558, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PLevel < 3 && "Invalid PIC Level"); |
559 | getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel)); |
560 | if (Context.getLangOpts().PIE) |
561 | getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel)); |
562 | } |
563 | |
564 | if (getCodeGenOpts().CodeModel.size() > 0) { |
565 | unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel) |
566 | .Case("tiny", llvm::CodeModel::Tiny) |
567 | .Case("small", llvm::CodeModel::Small) |
568 | .Case("kernel", llvm::CodeModel::Kernel) |
569 | .Case("medium", llvm::CodeModel::Medium) |
570 | .Case("large", llvm::CodeModel::Large) |
571 | .Default(~0u); |
572 | if (CM != ~0u) { |
573 | llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM); |
574 | getModule().setCodeModel(codeModel); |
575 | } |
576 | } |
577 | |
578 | if (CodeGenOpts.NoPLT) |
579 | getModule().setRtLibUseGOT(); |
580 | |
581 | SimplifyPersonality(); |
582 | |
583 | if (getCodeGenOpts().EmitDeclMetadata) |
584 | EmitDeclMetadata(); |
585 | |
586 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
587 | EmitCoverageFile(); |
588 | |
589 | if (DebugInfo) |
590 | DebugInfo->finalize(); |
591 | |
592 | if (getCodeGenOpts().EmitVersionIdentMetadata) |
593 | EmitVersionIdentMetadata(); |
594 | |
595 | if (!getCodeGenOpts().RecordCommandLine.empty()) |
596 | EmitCommandLineMetadata(); |
597 | |
598 | EmitTargetMetadata(); |
599 | } |
600 | |
601 | void CodeGenModule::EmitOpenCLMetadata() { |
602 | |
603 | |
604 | llvm::Metadata *OCLVerElts[] = { |
605 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
606 | Int32Ty, LangOpts.OpenCLVersion / 100)), |
607 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
608 | Int32Ty, (LangOpts.OpenCLVersion % 100) / 10))}; |
609 | llvm::NamedMDNode *OCLVerMD = |
610 | TheModule.getOrInsertNamedMetadata("opencl.ocl.version"); |
611 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
612 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
613 | } |
614 | |
615 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
616 | |
617 | Types.UpdateCompletedType(TD); |
618 | } |
619 | |
620 | void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { |
621 | |
622 | Types.RefreshTypeCacheForClass(RD); |
623 | } |
624 | |
625 | llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) { |
626 | if (!TBAA) |
627 | return nullptr; |
628 | return TBAA->getTypeInfo(QTy); |
629 | } |
630 | |
631 | TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) { |
632 | if (!TBAA) |
633 | return TBAAAccessInfo(); |
634 | return TBAA->getAccessInfo(AccessType); |
635 | } |
636 | |
637 | TBAAAccessInfo |
638 | CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) { |
639 | if (!TBAA) |
640 | return TBAAAccessInfo(); |
641 | return TBAA->getVTablePtrAccessInfo(VTablePtrType); |
642 | } |
643 | |
644 | llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { |
645 | if (!TBAA) |
646 | return nullptr; |
647 | return TBAA->getTBAAStructInfo(QTy); |
648 | } |
649 | |
650 | llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) { |
651 | if (!TBAA) |
652 | return nullptr; |
653 | return TBAA->getBaseTypeInfo(QTy); |
654 | } |
655 | |
656 | llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) { |
657 | if (!TBAA) |
658 | return nullptr; |
659 | return TBAA->getAccessTagInfo(Info); |
660 | } |
661 | |
662 | TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, |
663 | TBAAAccessInfo TargetInfo) { |
664 | if (!TBAA) |
665 | return TBAAAccessInfo(); |
666 | return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo); |
667 | } |
668 | |
669 | TBAAAccessInfo |
670 | CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, |
671 | TBAAAccessInfo InfoB) { |
672 | if (!TBAA) |
673 | return TBAAAccessInfo(); |
674 | return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB); |
675 | } |
676 | |
677 | TBAAAccessInfo |
678 | CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, |
679 | TBAAAccessInfo SrcInfo) { |
680 | if (!TBAA) |
681 | return TBAAAccessInfo(); |
682 | return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo); |
683 | } |
684 | |
685 | void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst, |
686 | TBAAAccessInfo TBAAInfo) { |
687 | if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo)) |
688 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag); |
689 | } |
690 | |
691 | void CodeGenModule::DecorateInstructionWithInvariantGroup( |
692 | llvm::Instruction *I, const CXXRecordDecl *RD) { |
693 | I->setMetadata(llvm::LLVMContext::MD_invariant_group, |
694 | llvm::MDNode::get(getLLVMContext(), {})); |
695 | } |
696 | |
697 | void CodeGenModule::Error(SourceLocation loc, StringRef message) { |
698 | unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
699 | getDiags().Report(Context.getFullLoc(loc), diagID) << message; |
700 | } |
701 | |
702 | |
703 | |
704 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { |
705 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
706 | "cannot compile this %0 yet"); |
707 | std::string Msg = Type; |
708 | getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID) |
709 | << Msg << S->getSourceRange(); |
710 | } |
711 | |
712 | |
713 | |
714 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { |
715 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
716 | "cannot compile this %0 yet"); |
717 | std::string Msg = Type; |
718 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
719 | } |
720 | |
721 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
722 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
723 | } |
724 | |
725 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
726 | const NamedDecl *D) const { |
727 | if (GV->hasDLLImportStorageClass()) |
728 | return; |
729 | |
730 | if (GV->hasLocalLinkage()) { |
731 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
732 | return; |
733 | } |
734 | if (!D) |
735 | return; |
736 | |
737 | |
738 | LinkageInfo LV = D->getLinkageAndVisibility(); |
739 | if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls || |
740 | !GV->isDeclarationForLinker()) |
741 | GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); |
742 | } |
743 | |
744 | static bool shouldAssumeDSOLocal(const CodeGenModule &CGM, |
745 | llvm::GlobalValue *GV) { |
746 | if (GV->hasLocalLinkage()) |
747 | return true; |
748 | |
749 | if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()) |
750 | return true; |
751 | |
752 | |
753 | if (GV->hasDLLImportStorageClass()) |
754 | return false; |
755 | |
756 | const llvm::Triple &TT = CGM.getTriple(); |
757 | if (TT.isWindowsGNUEnvironment()) { |
758 | |
759 | |
760 | |
761 | if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) && |
762 | !GV->isThreadLocal()) |
763 | return false; |
764 | } |
765 | |
766 | |
767 | |
768 | |
769 | |
770 | if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) |
771 | return true; |
772 | |
773 | |
774 | if (!TT.isOSBinFormatELF()) |
775 | return false; |
776 | |
777 | |
778 | const auto &CGOpts = CGM.getCodeGenOpts(); |
779 | llvm::Reloc::Model RM = CGOpts.RelocationModel; |
780 | const auto &LOpts = CGM.getLangOpts(); |
781 | if (RM != llvm::Reloc::Static && !LOpts.PIE) |
782 | return false; |
783 | |
784 | |
785 | if (!GV->isDeclarationForLinker()) |
786 | return true; |
787 | |
788 | |
789 | |
790 | |
791 | if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage()) |
792 | return false; |
793 | |
794 | |
795 | llvm::Triple::ArchType Arch = TT.getArch(); |
796 | if (Arch == llvm::Triple::ppc || Arch == llvm::Triple::ppc64 || |
797 | Arch == llvm::Triple::ppc64le) |
798 | return false; |
799 | |
800 | |
801 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV)) |
802 | if (!Var->isThreadLocal() && |
803 | (RM == llvm::Reloc::Static || CGOpts.PIECopyRelocations)) |
804 | return true; |
805 | |
806 | |
807 | |
808 | |
809 | if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static) |
810 | return true; |
811 | |
812 | |
813 | return false; |
814 | } |
815 | |
816 | void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const { |
817 | GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV)); |
818 | } |
819 | |
820 | void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, |
821 | GlobalDecl GD) const { |
822 | const auto *D = dyn_cast<NamedDecl>(GD.getDecl()); |
823 | |
824 | if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) { |
825 | getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType()); |
826 | return; |
827 | } |
828 | setDLLImportDLLExport(GV, D); |
829 | } |
830 | |
831 | void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, |
832 | const NamedDecl *D) const { |
833 | if (D && D->isExternallyVisible()) { |
834 | if (D->hasAttr<DLLImportAttr>()) |
835 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
836 | else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker()) |
837 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
838 | } |
839 | } |
840 | |
841 | void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, |
842 | GlobalDecl GD) const { |
843 | setDLLImportDLLExport(GV, GD); |
844 | setGlobalVisibilityAndLocal(GV, dyn_cast<NamedDecl>(GD.getDecl())); |
845 | } |
846 | |
847 | void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, |
848 | const NamedDecl *D) const { |
849 | setDLLImportDLLExport(GV, D); |
850 | setGlobalVisibilityAndLocal(GV, D); |
851 | } |
852 | |
853 | void CodeGenModule::setGlobalVisibilityAndLocal(llvm::GlobalValue *GV, |
854 | const NamedDecl *D) const { |
855 | setGlobalVisibility(GV, D); |
856 | setDSOLocal(GV); |
857 | } |
858 | |
859 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) { |
860 | return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S) |
861 | .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel) |
862 | .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel) |
863 | .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel) |
864 | .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel); |
865 | } |
866 | |
867 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel( |
868 | CodeGenOptions::TLSModel M) { |
869 | switch (M) { |
870 | case CodeGenOptions::GeneralDynamicTLSModel: |
871 | return llvm::GlobalVariable::GeneralDynamicTLSModel; |
872 | case CodeGenOptions::LocalDynamicTLSModel: |
873 | return llvm::GlobalVariable::LocalDynamicTLSModel; |
874 | case CodeGenOptions::InitialExecTLSModel: |
875 | return llvm::GlobalVariable::InitialExecTLSModel; |
876 | case CodeGenOptions::LocalExecTLSModel: |
877 | return llvm::GlobalVariable::LocalExecTLSModel; |
878 | } |
879 | llvm_unreachable("Invalid TLS model!"); |
880 | } |
881 | |
882 | void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const { |
883 | (0) . __assert_fail ("D.getTLSKind() && \"setting TLS mode on non-TLS var!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 883, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D.getTLSKind() && "setting TLS mode on non-TLS var!"); |
884 | |
885 | llvm::GlobalValue::ThreadLocalMode TLM; |
886 | TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel()); |
887 | |
888 | |
889 | if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) { |
890 | TLM = GetLLVMTLSModel(Attr->getModel()); |
891 | } |
892 | |
893 | GV->setThreadLocalMode(TLM); |
894 | } |
895 | |
896 | static std::string getCPUSpecificMangling(const CodeGenModule &CGM, |
897 | StringRef Name) { |
898 | const TargetInfo &Target = CGM.getTarget(); |
899 | return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str(); |
900 | } |
901 | |
902 | static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM, |
903 | const CPUSpecificAttr *Attr, |
904 | unsigned CPUIndex, |
905 | raw_ostream &Out) { |
906 | |
907 | |
908 | if (Attr) |
909 | Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName()); |
910 | else if (CGM.getTarget().supportsIFunc()) |
911 | Out << ".resolver"; |
912 | } |
913 | |
914 | static void AppendTargetMangling(const CodeGenModule &CGM, |
915 | const TargetAttr *Attr, raw_ostream &Out) { |
916 | if (Attr->isDefaultVersion()) |
917 | return; |
918 | |
919 | Out << '.'; |
920 | const TargetInfo &Target = CGM.getTarget(); |
921 | TargetAttr::ParsedTargetAttr Info = |
922 | Attr->parse([&Target](StringRef LHS, StringRef RHS) { |
923 | |
924 | |
925 | (0) . __assert_fail ("LHS.startswith(\"+\") && RHS.startswith(\"+\") && \"Features should always have a prefix.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 926, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(LHS.startswith("+") && RHS.startswith("+") && |
926 | (0) . __assert_fail ("LHS.startswith(\"+\") && RHS.startswith(\"+\") && \"Features should always have a prefix.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 926, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Features should always have a prefix."); |
927 | return Target.multiVersionSortPriority(LHS.substr(1)) > |
928 | Target.multiVersionSortPriority(RHS.substr(1)); |
929 | }); |
930 | |
931 | bool IsFirst = true; |
932 | |
933 | if (!Info.Architecture.empty()) { |
934 | IsFirst = false; |
935 | Out << "arch_" << Info.Architecture; |
936 | } |
937 | |
938 | for (StringRef Feat : Info.Features) { |
939 | if (!IsFirst) |
940 | Out << '_'; |
941 | IsFirst = false; |
942 | Out << Feat.substr(1); |
943 | } |
944 | } |
945 | |
946 | static std::string getMangledNameImpl(const CodeGenModule &CGM, GlobalDecl GD, |
947 | const NamedDecl *ND, |
948 | bool OmitMultiVersionMangling = false) { |
949 | SmallString<256> Buffer; |
950 | llvm::raw_svector_ostream Out(Buffer); |
951 | MangleContext &MC = CGM.getCXXABI().getMangleContext(); |
952 | if (MC.shouldMangleDeclName(ND)) { |
953 | llvm::raw_svector_ostream Out(Buffer); |
954 | if (const auto *D = dyn_cast<CXXConstructorDecl>(ND)) |
955 | MC.mangleCXXCtor(D, GD.getCtorType(), Out); |
956 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND)) |
957 | MC.mangleCXXDtor(D, GD.getDtorType(), Out); |
958 | else |
959 | MC.mangleName(ND, Out); |
960 | } else { |
961 | IdentifierInfo *II = ND->getIdentifier(); |
962 | (0) . __assert_fail ("II && \"Attempt to mangle unnamed decl.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 962, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(II && "Attempt to mangle unnamed decl."); |
963 | const auto *FD = dyn_cast<FunctionDecl>(ND); |
964 | |
965 | if (FD && |
966 | FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) { |
967 | llvm::raw_svector_ostream Out(Buffer); |
968 | Out << "__regcall3__" << II->getName(); |
969 | } else { |
970 | Out << II->getName(); |
971 | } |
972 | } |
973 | |
974 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
975 | if (FD->isMultiVersion() && !OmitMultiVersionMangling) { |
976 | switch (FD->getMultiVersionKind()) { |
977 | case MultiVersionKind::CPUDispatch: |
978 | case MultiVersionKind::CPUSpecific: |
979 | AppendCPUSpecificCPUDispatchMangling(CGM, |
980 | FD->getAttr<CPUSpecificAttr>(), |
981 | GD.getMultiVersionIndex(), Out); |
982 | break; |
983 | case MultiVersionKind::Target: |
984 | AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out); |
985 | break; |
986 | case MultiVersionKind::None: |
987 | llvm_unreachable("None multiversion type isn't valid here"); |
988 | } |
989 | } |
990 | |
991 | return Out.str(); |
992 | } |
993 | |
994 | void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD, |
995 | const FunctionDecl *FD) { |
996 | if (!FD->isMultiVersion()) |
997 | return; |
998 | |
999 | |
1000 | |
1001 | |
1002 | std::string NonTargetName = |
1003 | getMangledNameImpl(*this, GD, FD, ); |
1004 | GlobalDecl OtherGD; |
1005 | if (lookupRepresentativeDecl(NonTargetName, OtherGD)) { |
1006 | (0) . __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1010, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OtherGD.getCanonicalDecl() |
1007 | (0) . __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1010, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> .getDecl() |
1008 | (0) . __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1010, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ->getAsFunction() |
1009 | (0) . __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1010, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ->isMultiVersion() && |
1010 | (0) . __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1010, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Other GD should now be a multiversioned function"); |
1011 | |
1012 | |
1013 | const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl() |
1014 | .getDecl() |
1015 | ->getAsFunction() |
1016 | ->getMostRecentDecl(); |
1017 | std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD); |
1018 | |
1019 | |
1020 | if (OtherName != NonTargetName) { |
1021 | |
1022 | |
1023 | const auto ExistingRecord = Manglings.find(NonTargetName); |
1024 | if (ExistingRecord != std::end(Manglings)) |
1025 | Manglings.remove(&(*ExistingRecord)); |
1026 | auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD)); |
1027 | MangledDeclNames[OtherGD.getCanonicalDecl()] = Result.first->first(); |
1028 | if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName)) |
1029 | Entry->setName(OtherName); |
1030 | } |
1031 | } |
1032 | } |
1033 | |
1034 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
1035 | GlobalDecl CanonicalGD = GD.getCanonicalDecl(); |
1036 | |
1037 | |
1038 | |
1039 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) { |
1040 | if (!getTarget().getCXXABI().hasConstructorVariants()) { |
1041 | CXXCtorType OrigCtorType = GD.getCtorType(); |
1042 | assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete); |
1043 | if (OrigCtorType == Ctor_Base) |
1044 | CanonicalGD = GlobalDecl(CD, Ctor_Complete); |
1045 | } |
1046 | } |
1047 | |
1048 | auto FoundName = MangledDeclNames.find(CanonicalGD); |
1049 | if (FoundName != MangledDeclNames.end()) |
1050 | return FoundName->second; |
1051 | |
1052 | |
1053 | const auto *ND = cast<NamedDecl>(GD.getDecl()); |
1054 | std::string MangledName = getMangledNameImpl(*this, GD, ND); |
1055 | |
1056 | |
1057 | |
1058 | |
1059 | if (auto *FD = dyn_cast<FunctionDecl>(GD.getDecl())) |
1060 | if (getLangOpts().HIP && !getLangOpts().CUDAIsDevice && |
1061 | FD->hasAttr<CUDAGlobalAttr>()) |
1062 | MangledName = MangledName + ".stub"; |
1063 | |
1064 | auto Result = Manglings.insert(std::make_pair(MangledName, GD)); |
1065 | return MangledDeclNames[CanonicalGD] = Result.first->first(); |
1066 | } |
1067 | |
1068 | StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD, |
1069 | const BlockDecl *BD) { |
1070 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
1071 | const Decl *D = GD.getDecl(); |
1072 | |
1073 | SmallString<256> Buffer; |
1074 | llvm::raw_svector_ostream Out(Buffer); |
1075 | if (!D) |
1076 | MangleCtx.mangleGlobalBlock(BD, |
1077 | dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out); |
1078 | else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) |
1079 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
1080 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) |
1081 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
1082 | else |
1083 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
1084 | |
1085 | auto Result = Manglings.insert(std::make_pair(Out.str(), BD)); |
1086 | return Result.first->first(); |
1087 | } |
1088 | |
1089 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
1090 | return getModule().getNamedValue(Name); |
1091 | } |
1092 | |
1093 | |
1094 | |
1095 | void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority, |
1096 | llvm::Constant *AssociatedData) { |
1097 | |
1098 | GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData)); |
1099 | } |
1100 | |
1101 | |
1102 | |
1103 | void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) { |
1104 | if (CodeGenOpts.RegisterGlobalDtorsWithAtExit) { |
1105 | DtorsUsingAtExit[Priority].push_back(Dtor); |
1106 | return; |
1107 | } |
1108 | |
1109 | |
1110 | GlobalDtors.push_back(Structor(Priority, Dtor, nullptr)); |
1111 | } |
1112 | |
1113 | void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) { |
1114 | if (Fns.empty()) return; |
1115 | |
1116 | |
1117 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
1118 | llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy, |
1119 | TheModule.getDataLayout().getProgramAddressSpace()); |
1120 | |
1121 | |
1122 | llvm::StructType *CtorStructTy = llvm::StructType::get( |
1123 | Int32Ty, CtorPFTy, VoidPtrTy); |
1124 | |
1125 | |
1126 | ConstantInitBuilder builder(*this); |
1127 | auto ctors = builder.beginArray(CtorStructTy); |
1128 | for (const auto &I : Fns) { |
1129 | auto ctor = ctors.beginStruct(CtorStructTy); |
1130 | ctor.addInt(Int32Ty, I.Priority); |
1131 | ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy)); |
1132 | if (I.AssociatedData) |
1133 | ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy)); |
1134 | else |
1135 | ctor.addNullPointer(VoidPtrTy); |
1136 | ctor.finishAndAddTo(ctors); |
1137 | } |
1138 | |
1139 | auto list = |
1140 | ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(), |
1141 | false, |
1142 | llvm::GlobalValue::AppendingLinkage); |
1143 | |
1144 | |
1145 | |
1146 | list->setAlignment(0); |
1147 | |
1148 | Fns.clear(); |
1149 | } |
1150 | |
1151 | llvm::GlobalValue::LinkageTypes |
1152 | CodeGenModule::getFunctionLinkage(GlobalDecl GD) { |
1153 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
1154 | |
1155 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
1156 | |
1157 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D)) |
1158 | return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType()); |
1159 | |
1160 | if (isa<CXXConstructorDecl>(D) && |
1161 | cast<CXXConstructorDecl>(D)->isInheritingConstructor() && |
1162 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
1163 | |
1164 | |
1165 | |
1166 | return llvm::GlobalValue::InternalLinkage; |
1167 | } |
1168 | |
1169 | return getLLVMLinkageForDeclarator(D, Linkage, ); |
1170 | } |
1171 | |
1172 | llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { |
1173 | llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD); |
1174 | if (!MDS) return nullptr; |
1175 | |
1176 | return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString())); |
1177 | } |
1178 | |
1179 | void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD, |
1180 | const CGFunctionInfo &Info, |
1181 | llvm::Function *F) { |
1182 | unsigned CallingConv; |
1183 | llvm::AttributeList PAL; |
1184 | ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv, false); |
1185 | F->setAttributes(PAL); |
1186 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
1187 | } |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | static bool hasUnwindExceptions(const LangOptions &LangOpts) { |
1195 | |
1196 | if (!LangOpts.Exceptions) return false; |
1197 | |
1198 | |
1199 | if (LangOpts.CXXExceptions) return true; |
1200 | |
1201 | |
1202 | if (LangOpts.ObjCExceptions) { |
1203 | return LangOpts.ObjCRuntime.hasUnwindExceptions(); |
1204 | } |
1205 | |
1206 | return true; |
1207 | } |
1208 | |
1209 | static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM, |
1210 | const CXXMethodDecl *MD) { |
1211 | |
1212 | if (!CGM.getCodeGenOpts().LTOUnit || |
1213 | !CGM.HasHiddenLTOVisibility(MD->getParent())) |
1214 | return false; |
1215 | |
1216 | |
1217 | |
1218 | return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) && |
1219 | !isa<CXXDestructorDecl>(MD); |
1220 | } |
1221 | |
1222 | std::vector<const CXXRecordDecl *> |
1223 | CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) { |
1224 | llvm::SetVector<const CXXRecordDecl *> MostBases; |
1225 | |
1226 | std::function<void (const CXXRecordDecl *)> CollectMostBases; |
1227 | CollectMostBases = [&](const CXXRecordDecl *RD) { |
1228 | if (RD->getNumBases() == 0) |
1229 | MostBases.insert(RD); |
1230 | for (const CXXBaseSpecifier &B : RD->bases()) |
1231 | CollectMostBases(B.getType()->getAsCXXRecordDecl()); |
1232 | }; |
1233 | CollectMostBases(RD); |
1234 | return MostBases.takeVector(); |
1235 | } |
1236 | |
1237 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
1238 | llvm::Function *F) { |
1239 | llvm::AttrBuilder B; |
1240 | |
1241 | if (CodeGenOpts.UnwindTables) |
1242 | B.addAttribute(llvm::Attribute::UWTable); |
1243 | |
1244 | if (!hasUnwindExceptions(LangOpts)) |
1245 | B.addAttribute(llvm::Attribute::NoUnwind); |
1246 | |
1247 | if (!D || !D->hasAttr<NoStackProtectorAttr>()) { |
1248 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
1249 | B.addAttribute(llvm::Attribute::StackProtect); |
1250 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
1251 | B.addAttribute(llvm::Attribute::StackProtectStrong); |
1252 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
1253 | B.addAttribute(llvm::Attribute::StackProtectReq); |
1254 | } |
1255 | |
1256 | if (!D) { |
1257 | |
1258 | |
1259 | |
1260 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
1261 | CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) |
1262 | B.addAttribute(llvm::Attribute::NoInline); |
1263 | |
1264 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
1265 | return; |
1266 | } |
1267 | |
1268 | |
1269 | |
1270 | bool ShouldAddOptNone = |
1271 | !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0; |
1272 | |
1273 | ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>(); |
1274 | ShouldAddOptNone &= !F->hasFnAttribute(llvm::Attribute::AlwaysInline); |
1275 | ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>(); |
1276 | |
1277 | if (ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) { |
1278 | B.addAttribute(llvm::Attribute::OptimizeNone); |
1279 | |
1280 | |
1281 | B.addAttribute(llvm::Attribute::NoInline); |
1282 | (0) . __assert_fail ("!F->hasFnAttribute(llvm..Attribute..AlwaysInline) && \"OptimizeNone and AlwaysInline on same function!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1283, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
1283 | (0) . __assert_fail ("!F->hasFnAttribute(llvm..Attribute..AlwaysInline) && \"OptimizeNone and AlwaysInline on same function!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1283, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "OptimizeNone and AlwaysInline on same function!"); |
1284 | |
1285 | |
1286 | |
1287 | if (D->hasAttr<NakedAttr>()) |
1288 | B.addAttribute(llvm::Attribute::Naked); |
1289 | |
1290 | |
1291 | F->removeFnAttr(llvm::Attribute::OptimizeForSize); |
1292 | F->removeFnAttr(llvm::Attribute::MinSize); |
1293 | } else if (D->hasAttr<NakedAttr>()) { |
1294 | |
1295 | B.addAttribute(llvm::Attribute::Naked); |
1296 | B.addAttribute(llvm::Attribute::NoInline); |
1297 | } else if (D->hasAttr<NoDuplicateAttr>()) { |
1298 | B.addAttribute(llvm::Attribute::NoDuplicate); |
1299 | } else if (D->hasAttr<NoInlineAttr>()) { |
1300 | B.addAttribute(llvm::Attribute::NoInline); |
1301 | } else if (D->hasAttr<AlwaysInlineAttr>() && |
1302 | !F->hasFnAttribute(llvm::Attribute::NoInline)) { |
1303 | |
1304 | B.addAttribute(llvm::Attribute::AlwaysInline); |
1305 | } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) { |
1306 | |
1307 | |
1308 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline)) |
1309 | B.addAttribute(llvm::Attribute::NoInline); |
1310 | } else { |
1311 | |
1312 | |
1313 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
1314 | |
1315 | auto CheckForInline = [](const FunctionDecl *FD) { |
1316 | auto CheckRedeclForInline = [](const FunctionDecl *Redecl) { |
1317 | return Redecl->isInlineSpecified(); |
1318 | }; |
1319 | if (any_of(FD->redecls(), CheckRedeclForInline)) |
1320 | return true; |
1321 | const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern(); |
1322 | if (!Pattern) |
1323 | return false; |
1324 | return any_of(Pattern->redecls(), CheckRedeclForInline); |
1325 | }; |
1326 | if (CheckForInline(FD)) { |
1327 | B.addAttribute(llvm::Attribute::InlineHint); |
1328 | } else if (CodeGenOpts.getInlining() == |
1329 | CodeGenOptions::OnlyHintInlining && |
1330 | !FD->isInlined() && |
1331 | !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { |
1332 | B.addAttribute(llvm::Attribute::NoInline); |
1333 | } |
1334 | } |
1335 | } |
1336 | |
1337 | |
1338 | |
1339 | if (!D->hasAttr<OptimizeNoneAttr>()) { |
1340 | if (D->hasAttr<ColdAttr>()) { |
1341 | if (!ShouldAddOptNone) |
1342 | B.addAttribute(llvm::Attribute::OptimizeForSize); |
1343 | B.addAttribute(llvm::Attribute::Cold); |
1344 | } |
1345 | |
1346 | if (D->hasAttr<MinSizeAttr>()) |
1347 | B.addAttribute(llvm::Attribute::MinSize); |
1348 | } |
1349 | |
1350 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
1351 | |
1352 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
1353 | if (alignment) |
1354 | F->setAlignment(alignment); |
1355 | |
1356 | if (!D->hasAttr<AlignedAttr>()) |
1357 | if (LangOpts.FunctionAlignment) |
1358 | F->setAlignment(1 << LangOpts.FunctionAlignment); |
1359 | |
1360 | |
1361 | |
1362 | |
1363 | |
1364 | if (getTarget().getCXXABI().areMemberFunctionsAligned()) { |
1365 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
1366 | F->setAlignment(2); |
1367 | } |
1368 | |
1369 | |
1370 | if (CodeGenOpts.SanitizeCfiCrossDso) |
1371 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
1372 | CreateFunctionTypeMetadataForIcall(FD, F); |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | auto *MD = dyn_cast<CXXMethodDecl>(D); |
1378 | if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) { |
1379 | for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) { |
1380 | llvm::Metadata *Id = |
1381 | CreateMetadataIdentifierForType(Context.getMemberPointerType( |
1382 | MD->getType(), Context.getRecordType(Base).getTypePtr())); |
1383 | F->addTypeMetadata(0, Id); |
1384 | } |
1385 | } |
1386 | } |
1387 | |
1388 | void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) { |
1389 | const Decl *D = GD.getDecl(); |
1390 | if (dyn_cast_or_null<NamedDecl>(D)) |
1391 | setGVProperties(GV, GD); |
1392 | else |
1393 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
1394 | |
1395 | if (D && D->hasAttr<UsedAttr>()) |
1396 | addUsedGlobal(GV); |
1397 | |
1398 | if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) { |
1399 | const auto *VD = cast<VarDecl>(D); |
1400 | if (VD->getType().isConstQualified() && |
1401 | VD->getStorageDuration() == SD_Static) |
1402 | addUsedGlobal(GV); |
1403 | } |
1404 | } |
1405 | |
1406 | bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, |
1407 | llvm::AttrBuilder &Attrs) { |
1408 | |
1409 | |
1410 | |
1411 | StringRef TargetCPU = getTarget().getTargetOpts().CPU; |
1412 | std::vector<std::string> Features; |
1413 | const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl()); |
1414 | FD = FD ? FD->getMostRecentDecl() : FD; |
1415 | const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr; |
1416 | const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr; |
1417 | bool AddedAttr = false; |
1418 | if (TD || SD) { |
1419 | llvm::StringMap<bool> FeatureMap; |
1420 | getFunctionFeatureMap(FeatureMap, GD); |
1421 | |
1422 | |
1423 | for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap) |
1424 | Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str()); |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | |
1430 | if (TD) { |
1431 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
1432 | if (ParsedAttr.Architecture != "" && |
1433 | getTarget().isValidCPUName(ParsedAttr.Architecture)) |
1434 | TargetCPU = ParsedAttr.Architecture; |
1435 | } |
1436 | } else { |
1437 | |
1438 | |
1439 | Features = getTarget().getTargetOpts().Features; |
1440 | } |
1441 | |
1442 | if (TargetCPU != "") { |
1443 | Attrs.addAttribute("target-cpu", TargetCPU); |
1444 | AddedAttr = true; |
1445 | } |
1446 | if (!Features.empty()) { |
1447 | llvm::sort(Features); |
1448 | Attrs.addAttribute("target-features", llvm::join(Features, ",")); |
1449 | AddedAttr = true; |
1450 | } |
1451 | |
1452 | return AddedAttr; |
1453 | } |
1454 | |
1455 | void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, |
1456 | llvm::GlobalObject *GO) { |
1457 | const Decl *D = GD.getDecl(); |
1458 | SetCommonAttributes(GD, GO); |
1459 | |
1460 | if (D) { |
1461 | if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) { |
1462 | if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>()) |
1463 | GV->addAttribute("bss-section", SA->getName()); |
1464 | if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>()) |
1465 | GV->addAttribute("data-section", SA->getName()); |
1466 | if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>()) |
1467 | GV->addAttribute("rodata-section", SA->getName()); |
1468 | } |
1469 | |
1470 | if (auto *F = dyn_cast<llvm::Function>(GO)) { |
1471 | if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>()) |
1472 | if (!D->getAttr<SectionAttr>()) |
1473 | F->addFnAttr("implicit-section-name", SA->getName()); |
1474 | |
1475 | llvm::AttrBuilder Attrs; |
1476 | if (GetCPUAndFeaturesAttributes(GD, Attrs)) { |
1477 | |
1478 | |
1479 | |
1480 | F->removeFnAttr("target-cpu"); |
1481 | F->removeFnAttr("target-features"); |
1482 | F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs); |
1483 | } |
1484 | } |
1485 | |
1486 | if (const auto *CSA = D->getAttr<CodeSegAttr>()) |
1487 | GO->setSection(CSA->getName()); |
1488 | else if (const auto *SA = D->getAttr<SectionAttr>()) |
1489 | GO->setSection(SA->getName()); |
1490 | } |
1491 | |
1492 | getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); |
1493 | } |
1494 | |
1495 | void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD, |
1496 | llvm::Function *F, |
1497 | const CGFunctionInfo &FI) { |
1498 | const Decl *D = GD.getDecl(); |
1499 | SetLLVMFunctionAttributes(GD, FI, F); |
1500 | SetLLVMFunctionAttributesForDefinition(D, F); |
1501 | |
1502 | F->setLinkage(llvm::Function::InternalLinkage); |
1503 | |
1504 | setNonAliasAttributes(GD, F); |
1505 | } |
1506 | |
1507 | static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) { |
1508 | |
1509 | LinkageInfo LV = ND->getLinkageAndVisibility(); |
1510 | |
1511 | |
1512 | |
1513 | if (isExternallyVisible(LV.getLinkage()) && |
1514 | (ND->hasAttr<WeakAttr>() || ND->isWeakImported())) |
1515 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
1516 | } |
1517 | |
1518 | void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD, |
1519 | llvm::Function *F) { |
1520 | |
1521 | if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall)) |
1522 | return; |
1523 | |
1524 | |
1525 | |
1526 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
1527 | return; |
1528 | |
1529 | |
1530 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
1531 | |
1532 | |
1533 | if (getContext().GetGVALinkageForFunction(FD) == GVA_AvailableExternally) |
1534 | return; |
1535 | } |
1536 | |
1537 | llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); |
1538 | F->addTypeMetadata(0, MD); |
1539 | F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType())); |
1540 | |
1541 | |
1542 | if (CodeGenOpts.SanitizeCfiCrossDso) |
1543 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
1544 | F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
1545 | } |
1546 | |
1547 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, |
1548 | bool IsIncompleteFunction, |
1549 | bool IsThunk) { |
1550 | |
1551 | if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) { |
1552 | |
1553 | |
1554 | F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID)); |
1555 | return; |
1556 | } |
1557 | |
1558 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
1559 | |
1560 | if (!IsIncompleteFunction) { |
1561 | SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F); |
1562 | |
1563 | if (F->isDeclaration()) |
1564 | getTargetCodeGenInfo().setTargetAttributes(FD, F, *this); |
1565 | } |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | if (!IsThunk && getCXXABI().HasThisReturn(GD) && |
1571 | !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) { |
1572 | (0) . __assert_fail ("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1575, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!F->arg_empty() && |
1573 | (0) . __assert_fail ("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1575, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> F->arg_begin()->getType() |
1574 | (0) . __assert_fail ("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1575, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ->canLosslesslyBitCastTo(F->getReturnType()) && |
1575 | (0) . __assert_fail ("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1575, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "unexpected this return"); |
1576 | F->addAttribute(1, llvm::Attribute::Returned); |
1577 | } |
1578 | |
1579 | |
1580 | |
1581 | |
1582 | setLinkageForGV(F, FD); |
1583 | setGVProperties(F, FD); |
1584 | |
1585 | if (const auto *CSA = FD->getAttr<CodeSegAttr>()) |
1586 | F->setSection(CSA->getName()); |
1587 | else if (const auto *SA = FD->getAttr<SectionAttr>()) |
1588 | F->setSection(SA->getName()); |
1589 | |
1590 | if (FD->isReplaceableGlobalAllocationFunction()) { |
1591 | |
1592 | |
1593 | F->addAttribute(llvm::AttributeList::FunctionIndex, |
1594 | llvm::Attribute::NoBuiltin); |
1595 | |
1596 | |
1597 | |
1598 | |
1599 | auto Kind = FD->getDeclName().getCXXOverloadedOperator(); |
1600 | if (getCodeGenOpts().AssumeSaneOperatorNew && |
1601 | (Kind == OO_New || Kind == OO_Array_New)) |
1602 | F->addAttribute(llvm::AttributeList::ReturnIndex, |
1603 | llvm::Attribute::NoAlias); |
1604 | } |
1605 | |
1606 | if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD)) |
1607 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
1608 | else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
1609 | if (MD->isVirtual()) |
1610 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
1611 | |
1612 | |
1613 | |
1614 | if (!CodeGenOpts.SanitizeCfiCrossDso) |
1615 | CreateFunctionTypeMetadataForIcall(FD, F); |
1616 | |
1617 | if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>()) |
1618 | getOpenMPRuntime().emitDeclareSimdFunction(FD, F); |
1619 | |
1620 | if (const auto *CB = FD->getAttr<CallbackAttr>()) { |
1621 | |
1622 | |
1623 | |
1624 | llvm::LLVMContext &Ctx = F->getContext(); |
1625 | llvm::MDBuilder MDB(Ctx); |
1626 | |
1627 | |
1628 | |
1629 | int CalleeIdx = *CB->encoding_begin(); |
1630 | ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end()); |
1631 | F->addMetadata(llvm::LLVMContext::MD_callback, |
1632 | *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( |
1633 | CalleeIdx, PayloadIndices, |
1634 | false)})); |
1635 | } |
1636 | } |
1637 | |
1638 | void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { |
1639 | (0) . __assert_fail ("!GV->isDeclaration() && \"Only globals with definition can force usage.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1640, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!GV->isDeclaration() && |
1640 | (0) . __assert_fail ("!GV->isDeclaration() && \"Only globals with definition can force usage.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1640, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only globals with definition can force usage."); |
1641 | LLVMUsed.emplace_back(GV); |
1642 | } |
1643 | |
1644 | void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { |
1645 | (0) . __assert_fail ("!GV->isDeclaration() && \"Only globals with definition can force usage.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1646, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!GV->isDeclaration() && |
1646 | (0) . __assert_fail ("!GV->isDeclaration() && \"Only globals with definition can force usage.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1646, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only globals with definition can force usage."); |
1647 | LLVMCompilerUsed.emplace_back(GV); |
1648 | } |
1649 | |
1650 | static void emitUsed(CodeGenModule &CGM, StringRef Name, |
1651 | std::vector<llvm::WeakTrackingVH> &List) { |
1652 | |
1653 | if (List.empty()) |
1654 | return; |
1655 | |
1656 | |
1657 | SmallVector<llvm::Constant*, 8> UsedArray; |
1658 | UsedArray.resize(List.size()); |
1659 | for (unsigned i = 0, e = List.size(); i != e; ++i) { |
1660 | UsedArray[i] = |
1661 | llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
1662 | cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy); |
1663 | } |
1664 | |
1665 | if (UsedArray.empty()) |
1666 | return; |
1667 | llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); |
1668 | |
1669 | auto *GV = new llvm::GlobalVariable( |
1670 | CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, |
1671 | llvm::ConstantArray::get(ATy, UsedArray), Name); |
1672 | |
1673 | GV->setSection("llvm.metadata"); |
1674 | } |
1675 | |
1676 | void CodeGenModule::emitLLVMUsed() { |
1677 | emitUsed(*this, "llvm.used", LLVMUsed); |
1678 | emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); |
1679 | } |
1680 | |
1681 | void CodeGenModule::AppendLinkerOptions(StringRef Opts) { |
1682 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); |
1683 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
1684 | } |
1685 | |
1686 | void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { |
1687 | llvm::SmallString<32> Opt; |
1688 | getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt); |
1689 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
1690 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
1691 | } |
1692 | |
1693 | void CodeGenModule::AddELFLibDirective(StringRef Lib) { |
1694 | auto &C = getLLVMContext(); |
1695 | LinkerOptionsMetadata.push_back(llvm::MDNode::get( |
1696 | C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)})); |
1697 | } |
1698 | |
1699 | void CodeGenModule::AddDependentLib(StringRef Lib) { |
1700 | llvm::SmallString<24> Opt; |
1701 | getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); |
1702 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
1703 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
1704 | } |
1705 | |
1706 | |
1707 | |
1708 | static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, |
1709 | SmallVectorImpl<llvm::MDNode *> &Metadata, |
1710 | llvm::SmallPtrSet<Module *, 16> &Visited) { |
1711 | |
1712 | if (Mod->Parent && Visited.insert(Mod->Parent).second) { |
1713 | addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited); |
1714 | } |
1715 | |
1716 | |
1717 | for (unsigned I = Mod->Imports.size(); I > 0; --I) { |
1718 | if (Visited.insert(Mod->Imports[I - 1]).second) |
1719 | addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited); |
1720 | } |
1721 | |
1722 | |
1723 | |
1724 | llvm::LLVMContext &Context = CGM.getLLVMContext(); |
1725 | bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF(); |
1726 | bool IsPS4 = CGM.getTarget().getTriple().isPS4(); |
1727 | |
1728 | |
1729 | |
1730 | if (Mod->UseExportAsModuleLinkName) |
1731 | return; |
1732 | |
1733 | for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) { |
1734 | |
1735 | |
1736 | if (Mod->LinkLibraries[I-1].IsFramework) { |
1737 | llvm::Metadata *Args[2] = { |
1738 | llvm::MDString::get(Context, "-framework"), |
1739 | llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)}; |
1740 | |
1741 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
1742 | continue; |
1743 | } |
1744 | |
1745 | |
1746 | if (IsELF && !IsPS4) { |
1747 | llvm::Metadata *Args[2] = { |
1748 | llvm::MDString::get(Context, "lib"), |
1749 | llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library), |
1750 | }; |
1751 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
1752 | } else { |
1753 | llvm::SmallString<24> Opt; |
1754 | CGM.getTargetCodeGenInfo().getDependentLibraryOption( |
1755 | Mod->LinkLibraries[I - 1].Library, Opt); |
1756 | auto *OptString = llvm::MDString::get(Context, Opt); |
1757 | Metadata.push_back(llvm::MDNode::get(Context, OptString)); |
1758 | } |
1759 | } |
1760 | } |
1761 | |
1762 | void CodeGenModule::EmitModuleLinkOptions() { |
1763 | |
1764 | |
1765 | |
1766 | llvm::SetVector<clang::Module *> LinkModules; |
1767 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
1768 | SmallVector<clang::Module *, 16> Stack; |
1769 | |
1770 | |
1771 | for (Module *M : ImportedModules) { |
1772 | |
1773 | |
1774 | if (M->getTopLevelModuleName() == getLangOpts().CurrentModule && |
1775 | !getLangOpts().isCompilingModule()) |
1776 | continue; |
1777 | if (Visited.insert(M).second) |
1778 | Stack.push_back(M); |
1779 | } |
1780 | |
1781 | |
1782 | |
1783 | while (!Stack.empty()) { |
1784 | clang::Module *Mod = Stack.pop_back_val(); |
1785 | |
1786 | bool AnyChildren = false; |
1787 | |
1788 | |
1789 | for (const auto &SM : Mod->submodules()) { |
1790 | |
1791 | |
1792 | if (SM->IsExplicit) |
1793 | continue; |
1794 | |
1795 | if (Visited.insert(SM).second) { |
1796 | Stack.push_back(SM); |
1797 | AnyChildren = true; |
1798 | } |
1799 | } |
1800 | |
1801 | |
1802 | |
1803 | if (!AnyChildren) { |
1804 | LinkModules.insert(Mod); |
1805 | } |
1806 | } |
1807 | |
1808 | |
1809 | |
1810 | |
1811 | SmallVector<llvm::MDNode *, 16> MetadataArgs; |
1812 | Visited.clear(); |
1813 | for (Module *M : LinkModules) |
1814 | if (Visited.insert(M).second) |
1815 | addLinkOptionsPostorder(*this, M, MetadataArgs, Visited); |
1816 | std::reverse(MetadataArgs.begin(), MetadataArgs.end()); |
1817 | LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end()); |
1818 | |
1819 | |
1820 | auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options"); |
1821 | for (auto *MD : LinkerOptionsMetadata) |
1822 | NMD->addOperand(MD); |
1823 | } |
1824 | |
1825 | void CodeGenModule::EmitDeferred() { |
1826 | |
1827 | if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd) |
1828 | getOpenMPRuntime().emitDeferredTargetDecls(); |
1829 | |
1830 | |
1831 | |
1832 | |
1833 | |
1834 | if (!DeferredVTables.empty()) { |
1835 | EmitDeferredVTables(); |
1836 | |
1837 | |
1838 | |
1839 | |
1840 | assert(DeferredVTables.empty()); |
1841 | } |
1842 | |
1843 | |
1844 | if (DeferredDeclsToEmit.empty()) |
1845 | return; |
1846 | |
1847 | |
1848 | |
1849 | std::vector<GlobalDecl> CurDeclsToEmit; |
1850 | CurDeclsToEmit.swap(DeferredDeclsToEmit); |
1851 | |
1852 | for (GlobalDecl &D : CurDeclsToEmit) { |
1853 | |
1854 | |
1855 | |
1856 | |
1857 | llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>( |
1858 | GetAddrOfGlobal(D, ForDefinition)); |
1859 | |
1860 | |
1861 | |
1862 | |
1863 | if (!GV) |
1864 | GV = GetGlobalValue(getMangledName(D)); |
1865 | |
1866 | |
1867 | assert(GV); |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | if (!GV->isDeclaration()) |
1876 | continue; |
1877 | |
1878 | |
1879 | EmitGlobalDefinition(D, GV); |
1880 | |
1881 | |
1882 | |
1883 | |
1884 | if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) { |
1885 | EmitDeferred(); |
1886 | assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty()); |
1887 | } |
1888 | } |
1889 | } |
1890 | |
1891 | void CodeGenModule::EmitVTablesOpportunistically() { |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | |
1897 | (0) . __assert_fail ("(OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) && \"Only emit opportunistic vtables with optimizations\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1898, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) |
1898 | (0) . __assert_fail ("(OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) && \"Only emit opportunistic vtables with optimizations\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1898, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "Only emit opportunistic vtables with optimizations"); |
1899 | |
1900 | for (const CXXRecordDecl *RD : OpportunisticVTables) { |
1901 | (0) . __assert_fail ("getVTables().isVTableExternal(RD) && \"This queue should only contain external vtables\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1902, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getVTables().isVTableExternal(RD) && |
1902 | (0) . __assert_fail ("getVTables().isVTableExternal(RD) && \"This queue should only contain external vtables\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1902, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "This queue should only contain external vtables"); |
1903 | if (getCXXABI().canSpeculativelyEmitVTable(RD)) |
1904 | VTables.GenerateClassData(RD); |
1905 | } |
1906 | OpportunisticVTables.clear(); |
1907 | } |
1908 | |
1909 | void CodeGenModule::EmitGlobalAnnotations() { |
1910 | if (Annotations.empty()) |
1911 | return; |
1912 | |
1913 | |
1914 | llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( |
1915 | Annotations[0]->getType(), Annotations.size()), Annotations); |
1916 | auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false, |
1917 | llvm::GlobalValue::AppendingLinkage, |
1918 | Array, "llvm.global.annotations"); |
1919 | gv->setSection(AnnotationSection); |
1920 | } |
1921 | |
1922 | llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) { |
1923 | llvm::Constant *&AStr = AnnotationStrings[Str]; |
1924 | if (AStr) |
1925 | return AStr; |
1926 | |
1927 | |
1928 | llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str); |
1929 | auto *gv = |
1930 | new llvm::GlobalVariable(getModule(), s->getType(), true, |
1931 | llvm::GlobalValue::PrivateLinkage, s, ".str"); |
1932 | gv->setSection(AnnotationSection); |
1933 | gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
1934 | AStr = gv; |
1935 | return gv; |
1936 | } |
1937 | |
1938 | llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { |
1939 | SourceManager &SM = getContext().getSourceManager(); |
1940 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
1941 | if (PLoc.isValid()) |
1942 | return EmitAnnotationString(PLoc.getFilename()); |
1943 | return EmitAnnotationString(SM.getBufferName(Loc)); |
1944 | } |
1945 | |
1946 | llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { |
1947 | SourceManager &SM = getContext().getSourceManager(); |
1948 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
1949 | unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : |
1950 | SM.getExpansionLineNumber(L); |
1951 | return llvm::ConstantInt::get(Int32Ty, LineNo); |
1952 | } |
1953 | |
1954 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
1955 | const AnnotateAttr *AA, |
1956 | SourceLocation L) { |
1957 | |
1958 | llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), |
1959 | *UnitGV = EmitAnnotationUnit(L), |
1960 | *LineNoCst = EmitAnnotationLineNo(L); |
1961 | |
1962 | |
1963 | llvm::Constant *Fields[4] = { |
1964 | llvm::ConstantExpr::getBitCast(GV, Int8PtrTy), |
1965 | llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), |
1966 | llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), |
1967 | LineNoCst |
1968 | }; |
1969 | return llvm::ConstantStruct::getAnon(Fields); |
1970 | } |
1971 | |
1972 | void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, |
1973 | llvm::GlobalValue *GV) { |
1974 | (0) . __assert_fail ("D->hasAttr() && \"no annotate attribute\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 1974, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
1975 | |
1976 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) |
1977 | Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); |
1978 | } |
1979 | |
1980 | bool CodeGenModule::isInSanitizerBlacklist(SanitizerMask Kind, |
1981 | llvm::Function *Fn, |
1982 | SourceLocation Loc) const { |
1983 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
1984 | |
1985 | if (SanitizerBL.isBlacklistedFunction(Kind, Fn->getName())) |
1986 | return true; |
1987 | |
1988 | if (Loc.isValid()) |
1989 | return SanitizerBL.isBlacklistedLocation(Kind, Loc); |
1990 | |
1991 | |
1992 | auto &SM = Context.getSourceManager(); |
1993 | if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
1994 | return SanitizerBL.isBlacklistedFile(Kind, MainFile->getName()); |
1995 | } |
1996 | return false; |
1997 | } |
1998 | |
1999 | bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV, |
2000 | SourceLocation Loc, QualType Ty, |
2001 | StringRef Category) const { |
2002 | |
2003 | const SanitizerMask EnabledAsanMask = LangOpts.Sanitize.Mask & |
2004 | (SanitizerKind::Address | SanitizerKind::KernelAddress | |
2005 | SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress); |
2006 | if (!EnabledAsanMask) |
2007 | return false; |
2008 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
2009 | if (SanitizerBL.isBlacklistedGlobal(EnabledAsanMask, GV->getName(), Category)) |
2010 | return true; |
2011 | if (SanitizerBL.isBlacklistedLocation(EnabledAsanMask, Loc, Category)) |
2012 | return true; |
2013 | |
2014 | if (!Ty.isNull()) { |
2015 | |
2016 | |
2017 | while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr())) |
2018 | Ty = AT->getElementType(); |
2019 | Ty = Ty.getCanonicalType().getUnqualifiedType(); |
2020 | |
2021 | if (Ty->isRecordType()) { |
2022 | std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy()); |
2023 | if (SanitizerBL.isBlacklistedType(EnabledAsanMask, TypeStr, Category)) |
2024 | return true; |
2025 | } |
2026 | } |
2027 | return false; |
2028 | } |
2029 | |
2030 | bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, |
2031 | StringRef Category) const { |
2032 | const auto &XRayFilter = getContext().getXRayFilter(); |
2033 | using ImbueAttr = XRayFunctionFilter::ImbueAttribute; |
2034 | auto Attr = ImbueAttr::NONE; |
2035 | if (Loc.isValid()) |
2036 | Attr = XRayFilter.shouldImbueLocation(Loc, Category); |
2037 | if (Attr == ImbueAttr::NONE) |
2038 | Attr = XRayFilter.shouldImbueFunction(Fn->getName()); |
2039 | switch (Attr) { |
2040 | case ImbueAttr::NONE: |
2041 | return false; |
2042 | case ImbueAttr::ALWAYS: |
2043 | Fn->addFnAttr("function-instrument", "xray-always"); |
2044 | break; |
2045 | case ImbueAttr::ALWAYS_ARG1: |
2046 | Fn->addFnAttr("function-instrument", "xray-always"); |
2047 | Fn->addFnAttr("xray-log-args", "1"); |
2048 | break; |
2049 | case ImbueAttr::NEVER: |
2050 | Fn->addFnAttr("function-instrument", "xray-never"); |
2051 | break; |
2052 | } |
2053 | return true; |
2054 | } |
2055 | |
2056 | bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) { |
2057 | |
2058 | if (LangOpts.EmitAllDecls) |
2059 | return true; |
2060 | |
2061 | if (CodeGenOpts.KeepStaticConsts) { |
2062 | const auto *VD = dyn_cast<VarDecl>(Global); |
2063 | if (VD && VD->getType().isConstQualified() && |
2064 | VD->getStorageDuration() == SD_Static) |
2065 | return true; |
2066 | } |
2067 | |
2068 | return getContext().DeclMustBeEmitted(Global); |
2069 | } |
2070 | |
2071 | bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) { |
2072 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) |
2073 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
2074 | |
2075 | |
2076 | return false; |
2077 | if (const auto *VD = dyn_cast<VarDecl>(Global)) |
2078 | if (Context.getInlineVariableDefinitionKind(VD) == |
2079 | ASTContext::InlineVariableDefinitionKind::WeakUnknown) |
2080 | |
2081 | |
2082 | return false; |
2083 | |
2084 | |
2085 | if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS && |
2086 | getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) && |
2087 | !isTypeConstant(Global->getType(), false) && |
2088 | !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global)) |
2089 | return false; |
2090 | |
2091 | return true; |
2092 | } |
2093 | |
2094 | ConstantAddress CodeGenModule::GetAddrOfUuidDescriptor( |
2095 | const CXXUuidofExpr* E) { |
2096 | |
2097 | |
2098 | StringRef Uuid = E->getUuidStr(); |
2099 | std::string Name = "_GUID_" + Uuid.lower(); |
2100 | std::replace(Name.begin(), Name.end(), '-', '_'); |
2101 | |
2102 | |
2103 | CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes); |
2104 | |
2105 | |
2106 | if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) |
2107 | return ConstantAddress(GV, Alignment); |
2108 | |
2109 | llvm::Constant *Init = EmitUuidofInitializer(Uuid); |
2110 | (0) . __assert_fail ("Init && \"failed to initialize as constant\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2110, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Init && "failed to initialize as constant"); |
2111 | |
2112 | auto *GV = new llvm::GlobalVariable( |
2113 | getModule(), Init->getType(), |
2114 | , llvm::GlobalValue::LinkOnceODRLinkage, Init, Name); |
2115 | if (supportsCOMDAT()) |
2116 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
2117 | setDSOLocal(GV); |
2118 | return ConstantAddress(GV, Alignment); |
2119 | } |
2120 | |
2121 | ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
2122 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
2123 | (0) . __assert_fail ("AA && \"No alias?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2123, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AA && "No alias?"); |
2124 | |
2125 | CharUnits Alignment = getContext().getDeclAlign(VD); |
2126 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
2127 | |
2128 | |
2129 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
2130 | if (Entry) { |
2131 | unsigned AS = getContext().getTargetAddressSpace(VD->getType()); |
2132 | auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS)); |
2133 | return ConstantAddress(Ptr, Alignment); |
2134 | } |
2135 | |
2136 | llvm::Constant *Aliasee; |
2137 | if (isa<llvm::FunctionType>(DeclTy)) |
2138 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, |
2139 | GlobalDecl(cast<FunctionDecl>(VD)), |
2140 | ); |
2141 | else |
2142 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
2143 | llvm::PointerType::getUnqual(DeclTy), |
2144 | nullptr); |
2145 | |
2146 | auto *F = cast<llvm::GlobalValue>(Aliasee); |
2147 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
2148 | WeakRefReferences.insert(F); |
2149 | |
2150 | return ConstantAddress(Aliasee, Alignment); |
2151 | } |
2152 | |
2153 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
2154 | const auto *Global = cast<ValueDecl>(GD.getDecl()); |
2155 | |
2156 | |
2157 | if (Global->hasAttr<WeakRefAttr>()) |
2158 | return; |
2159 | |
2160 | |
2161 | |
2162 | if (Global->hasAttr<AliasAttr>()) |
2163 | return EmitAliasDefinition(GD); |
2164 | |
2165 | |
2166 | if (Global->hasAttr<IFuncAttr>()) |
2167 | return emitIFuncDefinition(GD); |
2168 | |
2169 | |
2170 | if (Global->hasAttr<CPUDispatchAttr>()) |
2171 | return emitCPUDispatchDefinition(GD); |
2172 | |
2173 | |
2174 | if (LangOpts.CUDA) { |
2175 | if (LangOpts.CUDAIsDevice) { |
2176 | if (!Global->hasAttr<CUDADeviceAttr>() && |
2177 | !Global->hasAttr<CUDAGlobalAttr>() && |
2178 | !Global->hasAttr<CUDAConstantAttr>() && |
2179 | !Global->hasAttr<CUDASharedAttr>()) |
2180 | return; |
2181 | } else { |
2182 | |
2183 | |
2184 | |
2185 | |
2186 | |
2187 | |
2188 | if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() && |
2189 | Global->hasAttr<CUDADeviceAttr>()) |
2190 | return; |
2191 | |
2192 | (0) . __assert_fail ("(isa(Global) || isa(Global)) && \"Expected Variable or Function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2193, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && |
2193 | (0) . __assert_fail ("(isa(Global) || isa(Global)) && \"Expected Variable or Function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2193, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expected Variable or Function"); |
2194 | } |
2195 | } |
2196 | |
2197 | if (LangOpts.OpenMP) { |
2198 | |
2199 | |
2200 | if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD)) |
2201 | return; |
2202 | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) { |
2203 | if (MustBeEmitted(Global)) |
2204 | EmitOMPDeclareReduction(DRD); |
2205 | return; |
2206 | } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) { |
2207 | if (MustBeEmitted(Global)) |
2208 | EmitOMPDeclareMapper(DMD); |
2209 | return; |
2210 | } |
2211 | } |
2212 | |
2213 | |
2214 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { |
2215 | |
2216 | if (!FD->doesThisDeclarationHaveABody()) { |
2217 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
2218 | return; |
2219 | |
2220 | StringRef MangledName = getMangledName(GD); |
2221 | |
2222 | |
2223 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
2224 | llvm::Type *Ty = getTypes().GetFunctionType(FI); |
2225 | |
2226 | GetOrCreateLLVMFunction(MangledName, Ty, GD, , |
2227 | ); |
2228 | return; |
2229 | } |
2230 | } else { |
2231 | const auto *VD = cast<VarDecl>(Global); |
2232 | (0) . __assert_fail ("VD->isFileVarDecl() && \"Cannot emit local var decl as global.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2232, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
2233 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition && |
2234 | !Context.isMSStaticDataMemberInlineDefinition(VD)) { |
2235 | if (LangOpts.OpenMP) { |
2236 | |
2237 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
2238 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { |
2239 | if (*Res == OMPDeclareTargetDeclAttr::MT_To) { |
2240 | (void)GetAddrOfGlobalVar(VD); |
2241 | } else { |
2242 | (0) . __assert_fail ("*Res == OMPDeclareTargetDeclAttr..MT_Link && \"link claue expected.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2243, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && |
2243 | (0) . __assert_fail ("*Res == OMPDeclareTargetDeclAttr..MT_Link && \"link claue expected.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2243, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "link claue expected."); |
2244 | (void)getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
2245 | } |
2246 | return; |
2247 | } |
2248 | } |
2249 | |
2250 | |
2251 | if (Context.getInlineVariableDefinitionKind(VD) == |
2252 | ASTContext::InlineVariableDefinitionKind::Strong) |
2253 | GetAddrOfGlobalVar(VD); |
2254 | return; |
2255 | } |
2256 | } |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) { |
2262 | |
2263 | EmitGlobalDefinition(GD); |
2264 | return; |
2265 | } |
2266 | |
2267 | |
2268 | |
2269 | if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) && |
2270 | cast<VarDecl>(Global)->hasInit()) { |
2271 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
2272 | CXXGlobalInits.push_back(nullptr); |
2273 | } |
2274 | |
2275 | StringRef MangledName = getMangledName(GD); |
2276 | if (GetGlobalValue(MangledName) != nullptr) { |
2277 | |
2278 | addDeferredDeclToEmit(GD); |
2279 | } else if (MustBeEmitted(Global)) { |
2280 | |
2281 | assert(!MayBeEmittedEagerly(Global)); |
2282 | addDeferredDeclToEmit(GD); |
2283 | } else { |
2284 | |
2285 | |
2286 | |
2287 | DeferredDecls[MangledName] = GD; |
2288 | } |
2289 | } |
2290 | |
2291 | |
2292 | static bool HasNonDllImportDtor(QualType T) { |
2293 | if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>()) |
2294 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
2295 | if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>()) |
2296 | return true; |
2297 | |
2298 | return false; |
2299 | } |
2300 | |
2301 | namespace { |
2302 | struct FunctionIsDirectlyRecursive |
2303 | : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> { |
2304 | const StringRef Name; |
2305 | const Builtin::Context &BI; |
2306 | FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) |
2307 | : Name(N), BI(C) {} |
2308 | |
2309 | bool VisitCallExpr(const CallExpr *E) { |
2310 | const FunctionDecl *FD = E->getDirectCallee(); |
2311 | if (!FD) |
2312 | return false; |
2313 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
2314 | if (Attr && Name == Attr->getLabel()) |
2315 | return true; |
2316 | unsigned BuiltinID = FD->getBuiltinID(); |
2317 | if (!BuiltinID || !BI.isLibFunction(BuiltinID)) |
2318 | return false; |
2319 | StringRef BuiltinName = BI.getName(BuiltinID); |
2320 | if (BuiltinName.startswith("__builtin_") && |
2321 | Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) { |
2322 | return true; |
2323 | } |
2324 | return false; |
2325 | } |
2326 | |
2327 | bool VisitStmt(const Stmt *S) { |
2328 | for (const Stmt *Child : S->children()) |
2329 | if (Child && this->Visit(Child)) |
2330 | return true; |
2331 | return false; |
2332 | } |
2333 | }; |
2334 | |
2335 | |
2336 | struct DLLImportFunctionVisitor |
2337 | : public RecursiveASTVisitor<DLLImportFunctionVisitor> { |
2338 | bool SafeToInline = true; |
2339 | |
2340 | bool shouldVisitImplicitCode() const { return true; } |
2341 | |
2342 | bool VisitVarDecl(VarDecl *VD) { |
2343 | if (VD->getTLSKind()) { |
2344 | |
2345 | SafeToInline = false; |
2346 | return SafeToInline; |
2347 | } |
2348 | |
2349 | |
2350 | if (VD->isThisDeclarationADefinition()) |
2351 | SafeToInline = !HasNonDllImportDtor(VD->getType()); |
2352 | |
2353 | return SafeToInline; |
2354 | } |
2355 | |
2356 | bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
2357 | if (const auto *D = E->getTemporary()->getDestructor()) |
2358 | SafeToInline = D->hasAttr<DLLImportAttr>(); |
2359 | return SafeToInline; |
2360 | } |
2361 | |
2362 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
2363 | ValueDecl *VD = E->getDecl(); |
2364 | if (isa<FunctionDecl>(VD)) |
2365 | SafeToInline = VD->hasAttr<DLLImportAttr>(); |
2366 | else if (VarDecl *V = dyn_cast<VarDecl>(VD)) |
2367 | SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>(); |
2368 | return SafeToInline; |
2369 | } |
2370 | |
2371 | bool VisitCXXConstructExpr(CXXConstructExpr *E) { |
2372 | SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>(); |
2373 | return SafeToInline; |
2374 | } |
2375 | |
2376 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
2377 | CXXMethodDecl *M = E->getMethodDecl(); |
2378 | if (!M) { |
2379 | |
2380 | SafeToInline = true; |
2381 | } else { |
2382 | SafeToInline = M->hasAttr<DLLImportAttr>(); |
2383 | } |
2384 | return SafeToInline; |
2385 | } |
2386 | |
2387 | bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
2388 | SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>(); |
2389 | return SafeToInline; |
2390 | } |
2391 | |
2392 | bool VisitCXXNewExpr(CXXNewExpr *E) { |
2393 | SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>(); |
2394 | return SafeToInline; |
2395 | } |
2396 | }; |
2397 | } |
2398 | |
2399 | |
2400 | |
2401 | |
2402 | bool |
2403 | CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) { |
2404 | StringRef Name; |
2405 | if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) { |
2406 | |
2407 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
2408 | if (!Attr) |
2409 | return false; |
2410 | Name = Attr->getLabel(); |
2411 | } else { |
2412 | Name = FD->getName(); |
2413 | } |
2414 | |
2415 | FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo); |
2416 | const Stmt *Body = FD->getBody(); |
2417 | return Body ? Walker.Visit(Body) : false; |
2418 | } |
2419 | |
2420 | bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) { |
2421 | if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage) |
2422 | return true; |
2423 | const auto *F = cast<FunctionDecl>(GD.getDecl()); |
2424 | if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>()) |
2425 | return false; |
2426 | |
2427 | if (F->hasAttr<DLLImportAttr>()) { |
2428 | |
2429 | DLLImportFunctionVisitor Visitor; |
2430 | Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F)); |
2431 | if (!Visitor.SafeToInline) |
2432 | return false; |
2433 | |
2434 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) { |
2435 | |
2436 | |
2437 | for (const Decl *Member : Dtor->getParent()->decls()) |
2438 | if (isa<FieldDecl>(Member)) |
2439 | if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType())) |
2440 | return false; |
2441 | for (const CXXBaseSpecifier &B : Dtor->getParent()->bases()) |
2442 | if (HasNonDllImportDtor(B.getType())) |
2443 | return false; |
2444 | } |
2445 | } |
2446 | |
2447 | |
2448 | |
2449 | |
2450 | |
2451 | |
2452 | return !isTriviallyRecursive(F); |
2453 | } |
2454 | |
2455 | bool CodeGenModule::shouldOpportunisticallyEmitVTables() { |
2456 | return CodeGenOpts.OptimizationLevel > 0; |
2457 | } |
2458 | |
2459 | void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD, |
2460 | llvm::GlobalValue *GV) { |
2461 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
2462 | |
2463 | if (FD->isCPUSpecificMultiVersion()) { |
2464 | auto *Spec = FD->getAttr<CPUSpecificAttr>(); |
2465 | for (unsigned I = 0; I < Spec->cpus_size(); ++I) |
2466 | EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr); |
2467 | |
2468 | } else |
2469 | EmitGlobalFunctionDefinition(GD, GV); |
2470 | } |
2471 | |
2472 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) { |
2473 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
2474 | |
2475 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
2476 | Context.getSourceManager(), |
2477 | "Generating code for declaration"); |
2478 | |
2479 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
2480 | |
2481 | |
2482 | if (!shouldEmitFunction(GD)) |
2483 | return; |
2484 | |
2485 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
2486 | |
2487 | |
2488 | if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method)) |
2489 | ABI->emitCXXStructor(GD); |
2490 | else if (FD->isMultiVersion()) |
2491 | EmitMultiVersionFunctionDefinition(GD, GV); |
2492 | else |
2493 | EmitGlobalFunctionDefinition(GD, GV); |
2494 | |
2495 | if (Method->isVirtual()) |
2496 | getVTables().EmitThunks(GD); |
2497 | |
2498 | return; |
2499 | } |
2500 | |
2501 | if (FD->isMultiVersion()) |
2502 | return EmitMultiVersionFunctionDefinition(GD, GV); |
2503 | return EmitGlobalFunctionDefinition(GD, GV); |
2504 | } |
2505 | |
2506 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
2507 | return EmitGlobalVarDefinition(VD, !VD->hasDefinition()); |
2508 | |
2509 | llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); |
2510 | } |
2511 | |
2512 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
2513 | llvm::Function *NewFn); |
2514 | |
2515 | static unsigned |
2516 | TargetMVPriority(const TargetInfo &TI, |
2517 | const CodeGenFunction::MultiVersionResolverOption &RO) { |
2518 | unsigned Priority = 0; |
2519 | for (StringRef Feat : RO.Conditions.Features) |
2520 | Priority = std::max(Priority, TI.multiVersionSortPriority(Feat)); |
2521 | |
2522 | if (!RO.Conditions.Architecture.empty()) |
2523 | Priority = std::max( |
2524 | Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture)); |
2525 | return Priority; |
2526 | } |
2527 | |
2528 | void CodeGenModule::emitMultiVersionFunctions() { |
2529 | for (GlobalDecl GD : MultiVersionFuncs) { |
2530 | SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; |
2531 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
2532 | getContext().forEachMultiversionedFunctionVersion( |
2533 | FD, [this, &GD, &Options](const FunctionDecl *CurFD) { |
2534 | GlobalDecl CurGD{ |
2535 | (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)}; |
2536 | StringRef MangledName = getMangledName(CurGD); |
2537 | llvm::Constant *Func = GetGlobalValue(MangledName); |
2538 | if (!Func) { |
2539 | if (CurFD->isDefined()) { |
2540 | EmitGlobalFunctionDefinition(CurGD, nullptr); |
2541 | Func = GetGlobalValue(MangledName); |
2542 | } else { |
2543 | const CGFunctionInfo &FI = |
2544 | getTypes().arrangeGlobalDeclaration(GD); |
2545 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
2546 | Func = GetAddrOfFunction(CurGD, Ty, , |
2547 | , ForDefinition); |
2548 | } |
2549 | (0) . __assert_fail ("Func && \"This should have just been created\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2549, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Func && "This should have just been created"); |
2550 | } |
2551 | |
2552 | const auto *TA = CurFD->getAttr<TargetAttr>(); |
2553 | llvm::SmallVector<StringRef, 8> Feats; |
2554 | TA->getAddedFeatures(Feats); |
2555 | |
2556 | Options.emplace_back(cast<llvm::Function>(Func), |
2557 | TA->getArchitecture(), Feats); |
2558 | }); |
2559 | |
2560 | llvm::Function *ResolverFunc; |
2561 | const TargetInfo &TI = getTarget(); |
2562 | |
2563 | if (TI.supportsIFunc() || FD->isTargetMultiVersion()) |
2564 | ResolverFunc = cast<llvm::Function>( |
2565 | GetGlobalValue((getMangledName(GD) + ".resolver").str())); |
2566 | else |
2567 | ResolverFunc = cast<llvm::Function>(GetGlobalValue(getMangledName(GD))); |
2568 | |
2569 | if (supportsCOMDAT()) |
2570 | ResolverFunc->setComdat( |
2571 | getModule().getOrInsertComdat(ResolverFunc->getName())); |
2572 | |
2573 | std::stable_sort( |
2574 | Options.begin(), Options.end(), |
2575 | [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS, |
2576 | const CodeGenFunction::MultiVersionResolverOption &RHS) { |
2577 | return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS); |
2578 | }); |
2579 | CodeGenFunction CGF(*this); |
2580 | CGF.EmitMultiVersionResolver(ResolverFunc, Options); |
2581 | } |
2582 | } |
2583 | |
2584 | void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) { |
2585 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
2586 | (0) . __assert_fail ("FD && \"Not a FunctionDecl?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2586, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FD && "Not a FunctionDecl?"); |
2587 | const auto *DD = FD->getAttr<CPUDispatchAttr>(); |
2588 | (0) . __assert_fail ("DD && \"Not a cpu_dispatch Function?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2588, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DD && "Not a cpu_dispatch Function?"); |
2589 | llvm::Type *DeclTy = getTypes().ConvertType(FD->getType()); |
2590 | |
2591 | if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { |
2592 | const CGFunctionInfo &FInfo = getTypes().arrangeCXXMethodDeclaration(CXXFD); |
2593 | DeclTy = getTypes().GetFunctionType(FInfo); |
2594 | } |
2595 | |
2596 | StringRef ResolverName = getMangledName(GD); |
2597 | |
2598 | llvm::Type *ResolverType; |
2599 | GlobalDecl ResolverGD; |
2600 | if (getTarget().supportsIFunc()) |
2601 | ResolverType = llvm::FunctionType::get( |
2602 | llvm::PointerType::get(DeclTy, |
2603 | Context.getTargetAddressSpace(FD->getType())), |
2604 | false); |
2605 | else { |
2606 | ResolverType = DeclTy; |
2607 | ResolverGD = GD; |
2608 | } |
2609 | |
2610 | auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction( |
2611 | ResolverName, ResolverType, ResolverGD, )); |
2612 | |
2613 | SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; |
2614 | const TargetInfo &Target = getTarget(); |
2615 | unsigned Index = 0; |
2616 | for (const IdentifierInfo *II : DD->cpus()) { |
2617 | |
2618 | std::string MangledName = getMangledNameImpl(*this, GD, FD, true) + |
2619 | getCPUSpecificMangling(*this, II->getName()); |
2620 | |
2621 | llvm::Constant *Func = GetGlobalValue(MangledName); |
2622 | |
2623 | if (!Func) { |
2624 | GlobalDecl ExistingDecl = Manglings.lookup(MangledName); |
2625 | if (ExistingDecl.getDecl() && |
2626 | ExistingDecl.getDecl()->getAsFunction()->isDefined()) { |
2627 | EmitGlobalFunctionDefinition(ExistingDecl, nullptr); |
2628 | Func = GetGlobalValue(MangledName); |
2629 | } else { |
2630 | if (!ExistingDecl.getDecl()) |
2631 | ExistingDecl = GD.getWithMultiVersionIndex(Index); |
2632 | |
2633 | Func = GetOrCreateLLVMFunction( |
2634 | MangledName, DeclTy, ExistingDecl, |
2635 | , , |
2636 | , llvm::AttributeList(), ForDefinition); |
2637 | } |
2638 | } |
2639 | |
2640 | llvm::SmallVector<StringRef, 32> Features; |
2641 | Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features); |
2642 | llvm::transform(Features, Features.begin(), |
2643 | [](StringRef Str) { return Str.substr(1); }); |
2644 | Features.erase(std::remove_if( |
2645 | Features.begin(), Features.end(), [&Target](StringRef Feat) { |
2646 | return !Target.validateCpuSupports(Feat); |
2647 | }), Features.end()); |
2648 | Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features); |
2649 | ++Index; |
2650 | } |
2651 | |
2652 | llvm::sort( |
2653 | Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS, |
2654 | const CodeGenFunction::MultiVersionResolverOption &RHS) { |
2655 | return CodeGenFunction::GetX86CpuSupportsMask(LHS.Conditions.Features) > |
2656 | CodeGenFunction::GetX86CpuSupportsMask(RHS.Conditions.Features); |
2657 | }); |
2658 | |
2659 | |
2660 | |
2661 | |
2662 | |
2663 | while (Options.size() > 1 && |
2664 | CodeGenFunction::GetX86CpuSupportsMask( |
2665 | (Options.end() - 2)->Conditions.Features) == 0) { |
2666 | StringRef LHSName = (Options.end() - 2)->Function->getName(); |
2667 | StringRef RHSName = (Options.end() - 1)->Function->getName(); |
2668 | if (LHSName.compare(RHSName) < 0) |
2669 | Options.erase(Options.end() - 2); |
2670 | else |
2671 | Options.erase(Options.end() - 1); |
2672 | } |
2673 | |
2674 | CodeGenFunction CGF(*this); |
2675 | CGF.EmitMultiVersionResolver(ResolverFunc, Options); |
2676 | } |
2677 | |
2678 | |
2679 | |
2680 | llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver( |
2681 | GlobalDecl GD, llvm::Type *DeclTy, const FunctionDecl *FD) { |
2682 | std::string MangledName = |
2683 | getMangledNameImpl(*this, GD, FD, ); |
2684 | |
2685 | |
2686 | |
2687 | std::string ResolverName = MangledName; |
2688 | if (getTarget().supportsIFunc()) |
2689 | ResolverName += ".ifunc"; |
2690 | else if (FD->isTargetMultiVersion()) |
2691 | ResolverName += ".resolver"; |
2692 | |
2693 | |
2694 | if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName)) |
2695 | return ResolverGV; |
2696 | |
2697 | |
2698 | |
2699 | |
2700 | if (!FD->isCPUDispatchMultiVersion() && !FD->isCPUSpecificMultiVersion()) |
2701 | MultiVersionFuncs.push_back(GD); |
2702 | |
2703 | if (getTarget().supportsIFunc()) { |
2704 | llvm::Type *ResolverType = llvm::FunctionType::get( |
2705 | llvm::PointerType::get( |
2706 | DeclTy, getContext().getTargetAddressSpace(FD->getType())), |
2707 | false); |
2708 | llvm::Constant *Resolver = GetOrCreateLLVMFunction( |
2709 | MangledName + ".resolver", ResolverType, GlobalDecl{}, |
2710 | ); |
2711 | llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create( |
2712 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Resolver, &getModule()); |
2713 | GIF->setName(ResolverName); |
2714 | SetCommonAttributes(FD, GIF); |
2715 | |
2716 | return GIF; |
2717 | } |
2718 | |
2719 | llvm::Constant *Resolver = GetOrCreateLLVMFunction( |
2720 | ResolverName, DeclTy, GlobalDecl{}, ); |
2721 | (0) . __assert_fail ("isa(Resolver) && \"Resolver should be created for the first time\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2722, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<llvm::GlobalValue>(Resolver) && |
2722 | (0) . __assert_fail ("isa(Resolver) && \"Resolver should be created for the first time\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2722, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Resolver should be created for the first time"); |
2723 | SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver)); |
2724 | return Resolver; |
2725 | } |
2726 | |
2727 | |
2728 | |
2729 | |
2730 | |
2731 | |
2732 | |
2733 | |
2734 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction( |
2735 | StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable, |
2736 | bool DontDefer, bool IsThunk, llvm::AttributeList , |
2737 | ForDefinition_t IsForDefinition) { |
2738 | const Decl *D = GD.getDecl(); |
2739 | |
2740 | |
2741 | |
2742 | if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) { |
2743 | |
2744 | if (getLangOpts().OpenMPIsDevice && OpenMPRuntime && |
2745 | !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() && |
2746 | !DontDefer && !IsForDefinition) { |
2747 | if (const FunctionDecl *FDDef = FD->getDefinition()) { |
2748 | GlobalDecl GDDef; |
2749 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef)) |
2750 | GDDef = GlobalDecl(CD, GD.getCtorType()); |
2751 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef)) |
2752 | GDDef = GlobalDecl(DD, GD.getDtorType()); |
2753 | else |
2754 | GDDef = GlobalDecl(FDDef); |
2755 | EmitGlobal(GDDef); |
2756 | } |
2757 | } |
2758 | |
2759 | if (FD->isMultiVersion()) { |
2760 | const auto *TA = FD->getAttr<TargetAttr>(); |
2761 | if (TA && TA->isDefaultVersion()) |
2762 | UpdateMultiVersionNames(GD, FD); |
2763 | if (!IsForDefinition) |
2764 | return GetOrCreateMultiVersionResolver(GD, Ty, FD); |
2765 | } |
2766 | } |
2767 | |
2768 | |
2769 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
2770 | if (Entry) { |
2771 | if (WeakRefReferences.erase(Entry)) { |
2772 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); |
2773 | if (FD && !FD->hasAttr<WeakAttr>()) |
2774 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
2775 | } |
2776 | |
2777 | |
2778 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) { |
2779 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
2780 | setDSOLocal(Entry); |
2781 | } |
2782 | |
2783 | |
2784 | |
2785 | if (IsForDefinition && !Entry->isDeclaration()) { |
2786 | GlobalDecl OtherGD; |
2787 | |
2788 | |
2789 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
2790 | (GD.getCanonicalDecl().getDecl() != |
2791 | OtherGD.getCanonicalDecl().getDecl()) && |
2792 | DiagnosedConflictingDefinitions.insert(GD).second) { |
2793 | getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) |
2794 | << MangledName; |
2795 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
2796 | diag::note_previous_definition); |
2797 | } |
2798 | } |
2799 | |
2800 | if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) && |
2801 | (Entry->getType()->getElementType() == Ty)) { |
2802 | return Entry; |
2803 | } |
2804 | |
2805 | |
2806 | |
2807 | |
2808 | if (!IsForDefinition) |
2809 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
2810 | } |
2811 | |
2812 | |
2813 | |
2814 | |
2815 | bool IsIncompleteFunction = false; |
2816 | |
2817 | llvm::FunctionType *FTy; |
2818 | if (isa<llvm::FunctionType>(Ty)) { |
2819 | FTy = cast<llvm::FunctionType>(Ty); |
2820 | } else { |
2821 | FTy = llvm::FunctionType::get(VoidTy, false); |
2822 | IsIncompleteFunction = true; |
2823 | } |
2824 | |
2825 | llvm::Function *F = |
2826 | llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, |
2827 | Entry ? StringRef() : MangledName, &getModule()); |
2828 | |
2829 | |
2830 | |
2831 | |
2832 | |
2833 | |
2834 | |
2835 | if (Entry) { |
2836 | F->takeName(Entry); |
2837 | |
2838 | |
2839 | |
2840 | |
2841 | |
2842 | |
2843 | |
2844 | if (!Entry->use_empty()) { |
2845 | ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F); |
2846 | Entry->removeDeadConstantUsers(); |
2847 | } |
2848 | |
2849 | llvm::Constant *BC = llvm::ConstantExpr::getBitCast( |
2850 | F, Entry->getType()->getElementType()->getPointerTo()); |
2851 | addGlobalValReplacement(Entry, BC); |
2852 | } |
2853 | |
2854 | (0) . __assert_fail ("F->getName() == MangledName && \"name was uniqued!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2854, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(F->getName() == MangledName && "name was uniqued!"); |
2855 | if (D) |
2856 | SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk); |
2857 | if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) { |
2858 | llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex); |
2859 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
2860 | } |
2861 | |
2862 | if (!DontDefer) { |
2863 | |
2864 | |
2865 | |
2866 | if (D && isa<CXXDestructorDecl>(D) && |
2867 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
2868 | GD.getDtorType())) |
2869 | addDeferredDeclToEmit(GD); |
2870 | |
2871 | |
2872 | |
2873 | |
2874 | auto DDI = DeferredDecls.find(MangledName); |
2875 | if (DDI != DeferredDecls.end()) { |
2876 | |
2877 | |
2878 | |
2879 | addDeferredDeclToEmit(DDI->second); |
2880 | DeferredDecls.erase(DDI); |
2881 | |
2882 | |
2883 | |
2884 | |
2885 | |
2886 | |
2887 | |
2888 | |
2889 | |
2890 | |
2891 | |
2892 | |
2893 | } else if (getLangOpts().CPlusPlus && D) { |
2894 | |
2895 | for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD; |
2896 | FD = FD->getPreviousDecl()) { |
2897 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
2898 | if (FD->doesThisDeclarationHaveABody()) { |
2899 | addDeferredDeclToEmit(GD.getWithDecl(FD)); |
2900 | break; |
2901 | } |
2902 | } |
2903 | } |
2904 | } |
2905 | } |
2906 | |
2907 | |
2908 | if (!IsIncompleteFunction) { |
2909 | getType()->getElementType() == Ty", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 2909, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(F->getType()->getElementType() == Ty); |
2910 | return F; |
2911 | } |
2912 | |
2913 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
2914 | return llvm::ConstantExpr::getBitCast(F, PTy); |
2915 | } |
2916 | |
2917 | |
2918 | |
2919 | |
2920 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
2921 | llvm::Type *Ty, |
2922 | bool ForVTable, |
2923 | bool DontDefer, |
2924 | ForDefinition_t IsForDefinition) { |
2925 | |
2926 | if (!Ty) { |
2927 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
2928 | Ty = getTypes().ConvertType(FD->getType()); |
2929 | } |
2930 | |
2931 | |
2932 | |
2933 | |
2934 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) { |
2935 | if (getTarget().getCXXABI().isMicrosoft() && |
2936 | GD.getDtorType() == Dtor_Complete && |
2937 | DD->getParent()->getNumVBases() == 0) |
2938 | GD = GlobalDecl(DD, Dtor_Base); |
2939 | } |
2940 | |
2941 | StringRef MangledName = getMangledName(GD); |
2942 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer, |
2943 | , llvm::AttributeList(), |
2944 | IsForDefinition); |
2945 | } |
2946 | |
2947 | static const FunctionDecl * |
2948 | GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { |
2949 | TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
2950 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
2951 | |
2952 | IdentifierInfo &CII = C.Idents.get(Name); |
2953 | for (const auto &Result : DC->lookup(&CII)) |
2954 | if (const auto FD = dyn_cast<FunctionDecl>(Result)) |
2955 | return FD; |
2956 | |
2957 | if (!C.getLangOpts().CPlusPlus) |
2958 | return nullptr; |
2959 | |
2960 | |
2961 | IdentifierInfo &CXXII = |
2962 | (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ") |
2963 | ? C.Idents.get("terminate") |
2964 | : C.Idents.get(Name); |
2965 | |
2966 | for (const auto &N : {"__cxxabiv1", "std"}) { |
2967 | IdentifierInfo &NS = C.Idents.get(N); |
2968 | for (const auto &Result : DC->lookup(&NS)) { |
2969 | NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result); |
2970 | if (auto LSD = dyn_cast<LinkageSpecDecl>(Result)) |
2971 | for (const auto &Result : LSD->lookup(&NS)) |
2972 | if ((ND = dyn_cast<NamespaceDecl>(Result))) |
2973 | break; |
2974 | |
2975 | if (ND) |
2976 | for (const auto &Result : ND->lookup(&CXXII)) |
2977 | if (const auto *FD = dyn_cast<FunctionDecl>(Result)) |
2978 | return FD; |
2979 | } |
2980 | } |
2981 | |
2982 | return nullptr; |
2983 | } |
2984 | |
2985 | |
2986 | |
2987 | llvm::FunctionCallee |
2988 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, |
2989 | llvm::AttributeList ExtraAttrs, |
2990 | bool Local) { |
2991 | llvm::Constant *C = |
2992 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), , |
2993 | , , |
2994 | ExtraAttrs); |
2995 | |
2996 | if (auto *F = dyn_cast<llvm::Function>(C)) { |
2997 | if (F->empty()) { |
2998 | F->setCallingConv(getRuntimeCC()); |
2999 | |
3000 | if (!Local && getTriple().isOSBinFormatCOFF() && |
3001 | !getCodeGenOpts().LTOVisibilityPublicStd && |
3002 | !getTriple().isWindowsGNUEnvironment()) { |
3003 | const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); |
3004 | if (!FD || FD->hasAttr<DLLImportAttr>()) { |
3005 | F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
3006 | F->setLinkage(llvm::GlobalValue::ExternalLinkage); |
3007 | } |
3008 | } |
3009 | setDSOLocal(F); |
3010 | } |
3011 | } |
3012 | |
3013 | return {FTy, C}; |
3014 | } |
3015 | |
3016 | |
3017 | |
3018 | |
3019 | |
3020 | |
3021 | |
3022 | bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) { |
3023 | if (!Ty.isConstant(Context) && !Ty->isReferenceType()) |
3024 | return false; |
3025 | |
3026 | if (Context.getLangOpts().CPlusPlus) { |
3027 | if (const CXXRecordDecl *Record |
3028 | = Context.getBaseElementType(Ty)->getAsCXXRecordDecl()) |
3029 | return ExcludeCtor && !Record->hasMutableFields() && |
3030 | Record->hasTrivialDestructor(); |
3031 | } |
3032 | |
3033 | return true; |
3034 | } |
3035 | |
3036 | |
3037 | |
3038 | |
3039 | |
3040 | |
3041 | |
3042 | |
3043 | |
3044 | |
3045 | |
3046 | |
3047 | llvm::Constant * |
3048 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
3049 | llvm::PointerType *Ty, |
3050 | const VarDecl *D, |
3051 | ForDefinition_t IsForDefinition) { |
3052 | |
3053 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
3054 | if (Entry) { |
3055 | if (WeakRefReferences.erase(Entry)) { |
3056 | if (D && !D->hasAttr<WeakAttr>()) |
3057 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
3058 | } |
3059 | |
3060 | |
3061 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) |
3062 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
3063 | |
3064 | if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D) |
3065 | getOpenMPRuntime().registerTargetGlobalVariable(D, Entry); |
3066 | |
3067 | if (Entry->getType() == Ty) |
3068 | return Entry; |
3069 | |
3070 | |
3071 | |
3072 | if (IsForDefinition && !Entry->isDeclaration()) { |
3073 | GlobalDecl OtherGD; |
3074 | const VarDecl *OtherD; |
3075 | |
3076 | |
3077 | |
3078 | if (D && lookupRepresentativeDecl(MangledName, OtherGD) && |
3079 | (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) && |
3080 | (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) && |
3081 | OtherD->hasInit() && |
3082 | DiagnosedConflictingDefinitions.insert(D).second) { |
3083 | getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) |
3084 | << MangledName; |
3085 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
3086 | diag::note_previous_definition); |
3087 | } |
3088 | } |
3089 | |
3090 | |
3091 | if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) |
3092 | return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); |
3093 | |
3094 | |
3095 | |
3096 | if (!IsForDefinition) |
3097 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
3098 | } |
3099 | |
3100 | auto AddrSpace = GetGlobalVarAddressSpace(D); |
3101 | auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace); |
3102 | |
3103 | auto *GV = new llvm::GlobalVariable( |
3104 | getModule(), Ty->getElementType(), false, |
3105 | llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr, |
3106 | llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace); |
3107 | |
3108 | |
3109 | |
3110 | if (Entry) { |
3111 | GV->takeName(Entry); |
3112 | |
3113 | if (!Entry->use_empty()) { |
3114 | llvm::Constant *NewPtrForOldDecl = |
3115 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
3116 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
3117 | } |
3118 | |
3119 | Entry->eraseFromParent(); |
3120 | } |
3121 | |
3122 | |
3123 | |
3124 | |
3125 | auto DDI = DeferredDecls.find(MangledName); |
3126 | if (DDI != DeferredDecls.end()) { |
3127 | |
3128 | |
3129 | addDeferredDeclToEmit(DDI->second); |
3130 | DeferredDecls.erase(DDI); |
3131 | } |
3132 | |
3133 | |
3134 | if (D) { |
3135 | if (LangOpts.OpenMP && !LangOpts.OpenMPSimd) |
3136 | getOpenMPRuntime().registerTargetGlobalVariable(D, GV); |
3137 | |
3138 | |
3139 | |
3140 | GV->setConstant(isTypeConstant(D->getType(), false)); |
3141 | |
3142 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
3143 | |
3144 | setLinkageForGV(GV, D); |
3145 | |
3146 | if (D->getTLSKind()) { |
3147 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
3148 | CXXThreadLocals.push_back(D); |
3149 | setTLSMode(GV, *D); |
3150 | } |
3151 | |
3152 | setGVProperties(GV, D); |
3153 | |
3154 | |
3155 | |
3156 | if (getContext().isMSStaticDataMemberInlineDefinition(D)) { |
3157 | EmitGlobalVarDefinition(D); |
3158 | } |
3159 | |
3160 | |
3161 | if (D->hasExternalStorage()) { |
3162 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
3163 | GV->setSection(SA->getName()); |
3164 | } |
3165 | |
3166 | |
3167 | if (getTriple().getArch() == llvm::Triple::xcore && |
3168 | D->getLanguageLinkage() == CLanguageLinkage && |
3169 | D->getType().isConstant(Context) && |
3170 | isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) |
3171 | GV->setSection(".cp.rodata"); |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() && |
3177 | D->getType().isConstQualified() && !GV->hasInitializer() && |
3178 | !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) { |
3179 | const auto *Record = |
3180 | Context.getBaseElementType(D->getType())->getAsCXXRecordDecl(); |
3181 | bool HasMutableFields = Record && Record->hasMutableFields(); |
3182 | if (!HasMutableFields) { |
3183 | const VarDecl *InitDecl; |
3184 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
3185 | if (InitExpr) { |
3186 | ConstantEmitter emitter(*this); |
3187 | llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl); |
3188 | if (Init) { |
3189 | auto *InitType = Init->getType(); |
3190 | if (GV->getType()->getElementType() != InitType) { |
3191 | |
3192 | |
3193 | |
3194 | |
3195 | GV->setName(StringRef()); |
3196 | |
3197 | |
3198 | auto *NewGV = cast<llvm::GlobalVariable>( |
3199 | GetAddrOfGlobalVar(D, InitType, IsForDefinition)); |
3200 | |
3201 | |
3202 | GV->eraseFromParent(); |
3203 | GV = NewGV; |
3204 | } else { |
3205 | GV->setInitializer(Init); |
3206 | GV->setConstant(true); |
3207 | GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
3208 | } |
3209 | emitter.finalize(GV); |
3210 | } |
3211 | } |
3212 | } |
3213 | } |
3214 | } |
3215 | |
3216 | LangAS ExpectedAS = |
3217 | D ? D->getType().getAddressSpace() |
3218 | : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); |
3219 | getPointerAddressSpace()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3220, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getContext().getTargetAddressSpace(ExpectedAS) == |
3220 | getPointerAddressSpace()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3220, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Ty->getPointerAddressSpace()); |
3221 | if (AddrSpace != ExpectedAS) |
3222 | return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace, |
3223 | ExpectedAS, Ty); |
3224 | |
3225 | if (GV->isDeclaration()) |
3226 | getTargetCodeGenInfo().setTargetAttributes(D, GV, *this); |
3227 | |
3228 | return GV; |
3229 | } |
3230 | |
3231 | llvm::Constant * |
3232 | CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, |
3233 | ForDefinition_t IsForDefinition) { |
3234 | const Decl *D = GD.getDecl(); |
3235 | if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) |
3236 | return getAddrOfCXXStructor(GD, , , |
3237 | , IsForDefinition); |
3238 | else if (isa<CXXMethodDecl>(D)) { |
3239 | auto FInfo = &getTypes().arrangeCXXMethodDeclaration( |
3240 | cast<CXXMethodDecl>(D)); |
3241 | auto Ty = getTypes().GetFunctionType(*FInfo); |
3242 | return GetAddrOfFunction(GD, Ty, , , |
3243 | IsForDefinition); |
3244 | } else if (isa<FunctionDecl>(D)) { |
3245 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
3246 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
3247 | return GetAddrOfFunction(GD, Ty, , , |
3248 | IsForDefinition); |
3249 | } else |
3250 | return GetAddrOfGlobalVar(cast<VarDecl>(D), , |
3251 | IsForDefinition); |
3252 | } |
3253 | |
3254 | llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable( |
3255 | StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, |
3256 | unsigned Alignment) { |
3257 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
3258 | llvm::GlobalVariable *OldGV = nullptr; |
3259 | |
3260 | if (GV) { |
3261 | |
3262 | if (GV->getType()->getElementType() == Ty) |
3263 | return GV; |
3264 | |
3265 | |
3266 | |
3267 | (0) . __assert_fail ("GV->isDeclaration() && \"Declaration has wrong type!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3267, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(GV->isDeclaration() && "Declaration has wrong type!"); |
3268 | OldGV = GV; |
3269 | } |
3270 | |
3271 | |
3272 | GV = new llvm::GlobalVariable(getModule(), Ty, , |
3273 | Linkage, nullptr, Name); |
3274 | |
3275 | if (OldGV) { |
3276 | |
3277 | GV->takeName(OldGV); |
3278 | |
3279 | if (!OldGV->use_empty()) { |
3280 | llvm::Constant *NewPtrForOldDecl = |
3281 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
3282 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
3283 | } |
3284 | |
3285 | OldGV->eraseFromParent(); |
3286 | } |
3287 | |
3288 | if (supportsCOMDAT() && GV->isWeakForLinker() && |
3289 | !GV->hasAvailableExternallyLinkage()) |
3290 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
3291 | |
3292 | GV->setAlignment(Alignment); |
3293 | |
3294 | return GV; |
3295 | } |
3296 | |
3297 | |
3298 | |
3299 | |
3300 | |
3301 | |
3302 | |
3303 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
3304 | llvm::Type *Ty, |
3305 | ForDefinition_t IsForDefinition) { |
3306 | (0) . __assert_fail ("D->hasGlobalStorage() && \"Not a global variable\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3306, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->hasGlobalStorage() && "Not a global variable"); |
3307 | QualType ASTTy = D->getType(); |
3308 | if (!Ty) |
3309 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
3310 | |
3311 | llvm::PointerType *PTy = |
3312 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
3313 | |
3314 | StringRef MangledName = getMangledName(D); |
3315 | return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition); |
3316 | } |
3317 | |
3318 | |
3319 | |
3320 | llvm::Constant * |
3321 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
3322 | StringRef Name) { |
3323 | auto *Ret = |
3324 | GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr); |
3325 | setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts())); |
3326 | return Ret; |
3327 | } |
3328 | |
3329 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
3330 | (0) . __assert_fail ("!D->getInit() && \"Cannot emit definite definitions here!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3330, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!D->getInit() && "Cannot emit definite definitions here!"); |
3331 | |
3332 | StringRef MangledName = getMangledName(D); |
3333 | llvm::GlobalValue *GV = GetGlobalValue(MangledName); |
3334 | |
3335 | |
3336 | |
3337 | |
3338 | if (GV && !GV->isDeclaration()) |
3339 | return; |
3340 | |
3341 | |
3342 | |
3343 | if (!MustBeEmitted(D) && !GV) { |
3344 | DeferredDecls[MangledName] = D; |
3345 | return; |
3346 | } |
3347 | |
3348 | |
3349 | EmitGlobalVarDefinition(D); |
3350 | } |
3351 | |
3352 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
3353 | return Context.toCharUnitsFromBits( |
3354 | getDataLayout().getTypeStoreSizeInBits(Ty)); |
3355 | } |
3356 | |
3357 | LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { |
3358 | LangAS AddrSpace = LangAS::Default; |
3359 | if (LangOpts.OpenCL) { |
3360 | AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global; |
3361 | = LangAS..FirstTargetAddressSpace", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3364, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AddrSpace == LangAS::opencl_global || |
3362 | = LangAS..FirstTargetAddressSpace", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3364, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> AddrSpace == LangAS::opencl_constant || |
3363 | = LangAS..FirstTargetAddressSpace", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3364, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> AddrSpace == LangAS::opencl_local || |
3364 | = LangAS..FirstTargetAddressSpace", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3364, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> AddrSpace >= LangAS::FirstTargetAddressSpace); |
3365 | return AddrSpace; |
3366 | } |
3367 | |
3368 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) { |
3369 | if (D && D->hasAttr<CUDAConstantAttr>()) |
3370 | return LangAS::cuda_constant; |
3371 | else if (D && D->hasAttr<CUDASharedAttr>()) |
3372 | return LangAS::cuda_shared; |
3373 | else if (D && D->hasAttr<CUDADeviceAttr>()) |
3374 | return LangAS::cuda_device; |
3375 | else if (D && D->getType().isConstQualified()) |
3376 | return LangAS::cuda_constant; |
3377 | else |
3378 | return LangAS::cuda_device; |
3379 | } |
3380 | |
3381 | if (LangOpts.OpenMP) { |
3382 | LangAS AS; |
3383 | if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS)) |
3384 | return AS; |
3385 | } |
3386 | return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); |
3387 | } |
3388 | |
3389 | LangAS CodeGenModule::getStringLiteralAddressSpace() const { |
3390 | |
3391 | if (LangOpts.OpenCL) |
3392 | return LangAS::opencl_constant; |
3393 | if (auto AS = getTarget().getConstantAddressSpace()) |
3394 | return AS.getValue(); |
3395 | return LangAS::Default; |
3396 | } |
3397 | |
3398 | |
3399 | |
3400 | |
3401 | |
3402 | |
3403 | |
3404 | |
3405 | |
3406 | static llvm::Constant * |
3407 | castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, |
3408 | llvm::GlobalVariable *GV) { |
3409 | llvm::Constant *Cast = GV; |
3410 | if (!CGM.getLangOpts().OpenCL) { |
3411 | if (auto AS = CGM.getTarget().getConstantAddressSpace()) { |
3412 | if (AS != LangAS::Default) |
3413 | Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast( |
3414 | CGM, GV, AS.getValue(), LangAS::Default, |
3415 | GV->getValueType()->getPointerTo( |
3416 | CGM.getContext().getTargetAddressSpace(LangAS::Default))); |
3417 | } |
3418 | } |
3419 | return Cast; |
3420 | } |
3421 | |
3422 | template<typename SomeDecl> |
3423 | void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, |
3424 | llvm::GlobalValue *GV) { |
3425 | if (!getLangOpts().CPlusPlus) |
3426 | return; |
3427 | |
3428 | |
3429 | |
3430 | if (!D->template hasAttr<UsedAttr>()) |
3431 | return; |
3432 | |
3433 | |
3434 | if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage) |
3435 | return; |
3436 | |
3437 | |
3438 | |
3439 | const SomeDecl *First = D->getFirstDecl(); |
3440 | if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) |
3441 | return; |
3442 | |
3443 | |
3444 | |
3445 | |
3446 | std::pair<StaticExternCMap::iterator, bool> R = |
3447 | StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); |
3448 | |
3449 | |
3450 | |
3451 | if (!R.second) |
3452 | R.first->second = nullptr; |
3453 | } |
3454 | |
3455 | static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) { |
3456 | if (!CGM.supportsCOMDAT()) |
3457 | return false; |
3458 | |
3459 | if (D.hasAttr<SelectAnyAttr>()) |
3460 | return true; |
3461 | |
3462 | GVALinkage Linkage; |
3463 | if (auto *VD = dyn_cast<VarDecl>(&D)) |
3464 | Linkage = CGM.getContext().GetGVALinkageForVariable(VD); |
3465 | else |
3466 | Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D)); |
3467 | |
3468 | switch (Linkage) { |
3469 | case GVA_Internal: |
3470 | case GVA_AvailableExternally: |
3471 | case GVA_StrongExternal: |
3472 | return false; |
3473 | case GVA_DiscardableODR: |
3474 | case GVA_StrongODR: |
3475 | return true; |
3476 | } |
3477 | llvm_unreachable("No such linkage"); |
3478 | } |
3479 | |
3480 | void CodeGenModule::maybeSetTrivialComdat(const Decl &D, |
3481 | llvm::GlobalObject &GO) { |
3482 | if (!shouldBeInCOMDAT(*this, D)) |
3483 | return; |
3484 | GO.setComdat(TheModule.getOrInsertComdat(GO.getName())); |
3485 | } |
3486 | |
3487 | |
3488 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, |
3489 | bool IsTentative) { |
3490 | |
3491 | |
3492 | QualType ASTTy = D->getType(); |
3493 | if (getLangOpts().OpenCL && ASTTy->isSamplerT()) |
3494 | return; |
3495 | |
3496 | |
3497 | |
3498 | if (LangOpts.OpenMPIsDevice && OpenMPRuntime && |
3499 | OpenMPRuntime->emitTargetGlobalVariable(D)) |
3500 | return; |
3501 | |
3502 | llvm::Constant *Init = nullptr; |
3503 | CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3504 | bool NeedsGlobalCtor = false; |
3505 | bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor(); |
3506 | |
3507 | const VarDecl *InitDecl; |
3508 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
3509 | |
3510 | Optional<ConstantEmitter> emitter; |
3511 | |
3512 | |
3513 | |
3514 | |
3515 | bool IsCUDASharedVar = |
3516 | getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>(); |
3517 | |
3518 | |
3519 | bool IsCUDAShadowVar = |
3520 | !getLangOpts().CUDAIsDevice && |
3521 | (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() || |
3522 | D->hasAttr<CUDASharedAttr>()); |
3523 | if (getLangOpts().CUDA && (IsCUDASharedVar || IsCUDAShadowVar)) |
3524 | Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy)); |
3525 | else if (!InitExpr) { |
3526 | |
3527 | |
3528 | |
3529 | |
3530 | |
3531 | |
3532 | |
3533 | |
3534 | |
3535 | (0) . __assert_fail ("!ASTTy->isIncompleteType() && \"Unexpected incomplete type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3535, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
3536 | Init = EmitNullConstant(D->getType()); |
3537 | } else { |
3538 | initializedGlobalDecl = GlobalDecl(D); |
3539 | emitter.emplace(*this); |
3540 | Init = emitter->tryEmitForInitializer(*InitDecl); |
3541 | |
3542 | if (!Init) { |
3543 | QualType T = InitExpr->getType(); |
3544 | if (D->getType()->isReferenceType()) |
3545 | T = D->getType(); |
3546 | |
3547 | if (getLangOpts().CPlusPlus) { |
3548 | Init = EmitNullConstant(T); |
3549 | NeedsGlobalCtor = true; |
3550 | } else { |
3551 | ErrorUnsupported(D, "static initializer"); |
3552 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
3553 | } |
3554 | } else { |
3555 | |
3556 | |
3557 | |
3558 | if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) |
3559 | DelayedCXXInitPosition.erase(D); |
3560 | } |
3561 | } |
3562 | |
3563 | llvm::Type* InitType = Init->getType(); |
3564 | llvm::Constant *Entry = |
3565 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)); |
3566 | |
3567 | |
3568 | if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
3569 | getOpcode() == llvm..Instruction..BitCast || CE->getOpcode() == llvm..Instruction..AddrSpaceCast || CE->getOpcode() == llvm..Instruction..GetElementPtr", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3572, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CE->getOpcode() == llvm::Instruction::BitCast || |
3570 | getOpcode() == llvm..Instruction..BitCast || CE->getOpcode() == llvm..Instruction..AddrSpaceCast || CE->getOpcode() == llvm..Instruction..GetElementPtr", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3572, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CE->getOpcode() == llvm::Instruction::AddrSpaceCast || |
3571 | getOpcode() == llvm..Instruction..BitCast || CE->getOpcode() == llvm..Instruction..AddrSpaceCast || CE->getOpcode() == llvm..Instruction..GetElementPtr", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3572, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> |
3572 | getOpcode() == llvm..Instruction..BitCast || CE->getOpcode() == llvm..Instruction..AddrSpaceCast || CE->getOpcode() == llvm..Instruction..GetElementPtr", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 3572, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CE->getOpcode() == llvm::Instruction::GetElementPtr); |
3573 | Entry = CE->getOperand(0); |
3574 | } |
3575 | |
3576 | |
3577 | auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
3578 | |
3579 | |
3580 | |
3581 | |
3582 | |
3583 | |
3584 | |
3585 | |
3586 | |
3587 | |
3588 | if (!GV || GV->getType()->getElementType() != InitType || |
3589 | GV->getType()->getAddressSpace() != |
3590 | getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) { |
3591 | |
3592 | |
3593 | Entry->setName(StringRef()); |
3594 | |
3595 | |
3596 | GV = cast<llvm::GlobalVariable>( |
3597 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))); |
3598 | |
3599 | |
3600 | llvm::Constant *NewPtrForOldDecl = |
3601 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
3602 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
3603 | |
3604 | |
3605 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
3606 | } |
3607 | |
3608 | MaybeHandleStaticInExternC(D, GV); |
3609 | |
3610 | if (D->hasAttr<AnnotateAttr>()) |
3611 | AddGlobalAnnotations(D, GV); |
3612 | |
3613 | |
3614 | llvm::GlobalValue::LinkageTypes Linkage = |
3615 | getLLVMLinkageVarDefinition(D, GV->isConstant()); |
3616 | |
3617 | |
3618 | |
3619 | |
3620 | |
3621 | |
3622 | |
3623 | |
3624 | if (GV && LangOpts.CUDA) { |
3625 | if (LangOpts.CUDAIsDevice) { |
3626 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) |
3627 | GV->setExternallyInitialized(true); |
3628 | } else { |
3629 | |
3630 | |
3631 | |
3632 | |
3633 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) { |
3634 | Linkage = llvm::GlobalValue::InternalLinkage; |
3635 | |
3636 | |
3637 | |
3638 | unsigned Flags = 0; |
3639 | if (!D->hasDefinition()) |
3640 | Flags |= CGCUDARuntime::ExternDeviceVar; |
3641 | if (D->hasAttr<CUDAConstantAttr>()) |
3642 | Flags |= CGCUDARuntime::ConstantDeviceVar; |
3643 | |
3644 | |
3645 | if (!D->hasExternalStorage()) |
3646 | getCUDARuntime().registerDeviceVar(D, *GV, Flags); |
3647 | } else if (D->hasAttr<CUDASharedAttr>()) |
3648 | |
3649 | |
3650 | |
3651 | |
3652 | |
3653 | Linkage = llvm::GlobalValue::InternalLinkage; |
3654 | } |
3655 | } |
3656 | |
3657 | GV->setInitializer(Init); |
3658 | if (emitter) emitter->finalize(GV); |
3659 | |
3660 | |
3661 | GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && |
3662 | isTypeConstant(D->getType(), true)); |
3663 | |
3664 | |
3665 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
3666 | const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()]; |
3667 | if ((SI.SectionFlags & ASTContext::PSF_Write) == 0) |
3668 | GV->setConstant(true); |
3669 | } |
3670 | |
3671 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
3672 | |
3673 | |
3674 | |
3675 | |
3676 | |
3677 | |
3678 | |
3679 | |
3680 | |
3681 | if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic && |
3682 | Context.getTargetInfo().getTriple().isOSDarwin() && |
3683 | !llvm::GlobalVariable::isLinkOnceLinkage(Linkage) && |
3684 | !llvm::GlobalVariable::isWeakLinkage(Linkage)) |
3685 | Linkage = llvm::GlobalValue::InternalLinkage; |
3686 | |
3687 | GV->setLinkage(Linkage); |
3688 | if (D->hasAttr<DLLImportAttr>()) |
3689 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
3690 | else if (D->hasAttr<DLLExportAttr>()) |
3691 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
3692 | else |
3693 | GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); |
3694 | |
3695 | if (Linkage == llvm::GlobalVariable::CommonLinkage) { |
3696 | |
3697 | GV->setConstant(false); |
3698 | |
3699 | |
3700 | |
3701 | |
3702 | if (!GV->getInitializer()->isNullValue()) |
3703 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
3704 | } |
3705 | |
3706 | setNonAliasAttributes(D, GV); |
3707 | |
3708 | if (D->getTLSKind() && !GV->isThreadLocal()) { |
3709 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
3710 | CXXThreadLocals.push_back(D); |
3711 | setTLSMode(GV, *D); |
3712 | } |
3713 | |
3714 | maybeSetTrivialComdat(*D, *GV); |
3715 | |
3716 | |
3717 | if (NeedsGlobalCtor || NeedsGlobalDtor) |
3718 | EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); |
3719 | |
3720 | SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor); |
3721 | |
3722 | |
3723 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
3724 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
3725 | DI->EmitGlobalVariable(GV, D); |
3726 | } |
3727 | |
3728 | static bool isVarDeclStrongDefinition(const ASTContext &Context, |
3729 | CodeGenModule &CGM, const VarDecl *D, |
3730 | bool NoCommon) { |
3731 | |
3732 | |
3733 | if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) |
3734 | return true; |
3735 | |
3736 | |
3737 | |
3738 | |
3739 | |
3740 | if (D->getInit() || D->hasExternalStorage()) |
3741 | return true; |
3742 | |
3743 | |
3744 | if (D->hasAttr<SectionAttr>()) |
3745 | return true; |
3746 | |
3747 | |
3748 | |
3749 | |
3750 | if (D->hasAttr<PragmaClangBSSSectionAttr>() || |
3751 | D->hasAttr<PragmaClangDataSectionAttr>() || |
3752 | D->hasAttr<PragmaClangRodataSectionAttr>()) |
3753 | return true; |
3754 | |
3755 | |
3756 | if (D->getTLSKind()) |
3757 | return true; |
3758 | |
3759 | |
3760 | if (D->hasAttr<WeakImportAttr>()) |
3761 | return true; |
3762 | |
3763 | |
3764 | if (shouldBeInCOMDAT(CGM, *D)) |
3765 | return true; |
3766 | |
3767 | |
3768 | |
3769 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
3770 | if (D->hasAttr<AlignedAttr>()) |
3771 | return true; |
3772 | QualType VarType = D->getType(); |
3773 | if (Context.isAlignmentRequired(VarType)) |
3774 | return true; |
3775 | |
3776 | if (const auto *RT = VarType->getAs<RecordType>()) { |
3777 | const RecordDecl *RD = RT->getDecl(); |
3778 | for (const FieldDecl *FD : RD->fields()) { |
3779 | if (FD->isBitField()) |
3780 | continue; |
3781 | if (FD->hasAttr<AlignedAttr>()) |
3782 | return true; |
3783 | if (Context.isAlignmentRequired(FD->getType())) |
3784 | return true; |
3785 | } |
3786 | } |
3787 | } |
3788 | |
3789 | |
3790 | |
3791 | |
3792 | |
3793 | |
3794 | |
3795 | if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() && |
3796 | Context.getTypeAlignIfKnown(D->getType()) > |
3797 | Context.toBits(CharUnits::fromQuantity(32))) |
3798 | return true; |
3799 | |
3800 | return false; |
3801 | } |
3802 | |
3803 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( |
3804 | const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) { |
3805 | if (Linkage == GVA_Internal) |
3806 | return llvm::Function::InternalLinkage; |
3807 | |
3808 | if (D->hasAttr<WeakAttr>()) { |
3809 | if (IsConstantVariable) |
3810 | return llvm::GlobalVariable::WeakODRLinkage; |
3811 | else |
3812 | return llvm::GlobalVariable::WeakAnyLinkage; |
3813 | } |
3814 | |
3815 | if (const auto *FD = D->getAsFunction()) |
3816 | if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally) |
3817 | return llvm::GlobalVariable::LinkOnceAnyLinkage; |
3818 | |
3819 | |
3820 | |
3821 | if (Linkage == GVA_AvailableExternally) |
3822 | return llvm::GlobalValue::AvailableExternallyLinkage; |
3823 | |
3824 | |
3825 | |
3826 | |
3827 | |
3828 | |
3829 | |
3830 | |
3831 | |
3832 | |
3833 | |
3834 | |
3835 | if (Linkage == GVA_DiscardableODR) |
3836 | return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage |
3837 | : llvm::Function::InternalLinkage; |
3838 | |
3839 | |
3840 | |
3841 | |
3842 | |
3843 | |
3844 | |
3845 | |
3846 | |
3847 | if (Linkage == GVA_StrongODR) { |
3848 | if (Context.getLangOpts().AppleKext) |
3849 | return llvm::Function::ExternalLinkage; |
3850 | if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) |
3851 | return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage |
3852 | : llvm::Function::InternalLinkage; |
3853 | return llvm::Function::WeakODRLinkage; |
3854 | } |
3855 | |
3856 | |
3857 | |
3858 | if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && |
3859 | !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D), |
3860 | CodeGenOpts.NoCommon)) |
3861 | return llvm::GlobalVariable::CommonLinkage; |
3862 | |
3863 | |
3864 | |
3865 | |
3866 | |
3867 | if (D->hasAttr<SelectAnyAttr>()) |
3868 | return llvm::GlobalVariable::WeakODRLinkage; |
3869 | |
3870 | |
3871 | assert(Linkage == GVA_StrongExternal); |
3872 | return llvm::GlobalVariable::ExternalLinkage; |
3873 | } |
3874 | |
3875 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition( |
3876 | const VarDecl *VD, bool IsConstant) { |
3877 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); |
3878 | return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant); |
3879 | } |
3880 | |
3881 | |
3882 | |
3883 | static void replaceUsesOfNonProtoConstant(llvm::Constant *old, |
3884 | llvm::Function *newFn) { |
3885 | |
3886 | if (old->use_empty()) return; |
3887 | |
3888 | llvm::Type *newRetTy = newFn->getReturnType(); |
3889 | SmallVector<llvm::Value*, 4> newArgs; |
3890 | SmallVector<llvm::OperandBundleDef, 1> newBundles; |
3891 | |
3892 | for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); |
3893 | ui != ue; ) { |
3894 | llvm::Value::use_iterator use = ui++; |
3895 | llvm::User *user = use->getUser(); |
3896 | |
3897 | |
3898 | |
3899 | if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { |
3900 | if (bitcast->getOpcode() == llvm::Instruction::BitCast) |
3901 | replaceUsesOfNonProtoConstant(bitcast, newFn); |
3902 | continue; |
3903 | } |
3904 | |
3905 | |
3906 | llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user); |
3907 | if (!callSite) continue; |
3908 | if (!callSite->isCallee(&*use)) |
3909 | continue; |
3910 | |
3911 | |
3912 | |
3913 | if (callSite->getType() != newRetTy && !callSite->use_empty()) |
3914 | continue; |
3915 | |
3916 | |
3917 | SmallVector<llvm::AttributeSet, 8> newArgAttrs; |
3918 | llvm::AttributeList oldAttrs = callSite->getAttributes(); |
3919 | |
3920 | |
3921 | unsigned newNumArgs = newFn->arg_size(); |
3922 | if (callSite->arg_size() < newNumArgs) |
3923 | continue; |
3924 | |
3925 | |
3926 | |
3927 | unsigned argNo = 0; |
3928 | bool dontTransform = false; |
3929 | for (llvm::Argument &A : newFn->args()) { |
3930 | if (callSite->getArgOperand(argNo)->getType() != A.getType()) { |
3931 | dontTransform = true; |
3932 | break; |
3933 | } |
3934 | |
3935 | |
3936 | newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo)); |
3937 | argNo++; |
3938 | } |
3939 | if (dontTransform) |
3940 | continue; |
3941 | |
3942 | |
3943 | |
3944 | newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo); |
3945 | |
3946 | |
3947 | callSite->getOperandBundlesAsDefs(newBundles); |
3948 | |
3949 | llvm::CallBase *newCall; |
3950 | if (dyn_cast<llvm::CallInst>(callSite)) { |
3951 | newCall = |
3952 | llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite); |
3953 | } else { |
3954 | auto *oldInvoke = cast<llvm::InvokeInst>(callSite); |
3955 | newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(), |
3956 | oldInvoke->getUnwindDest(), newArgs, |
3957 | newBundles, "", callSite); |
3958 | } |
3959 | newArgs.clear(); |
3960 | |
3961 | if (!newCall->getType()->isVoidTy()) |
3962 | newCall->takeName(callSite); |
3963 | newCall->setAttributes(llvm::AttributeList::get( |
3964 | newFn->getContext(), oldAttrs.getFnAttributes(), |
3965 | oldAttrs.getRetAttributes(), newArgAttrs)); |
3966 | newCall->setCallingConv(callSite->getCallingConv()); |
3967 | |
3968 | |
3969 | if (!callSite->use_empty()) |
3970 | callSite->replaceAllUsesWith(newCall); |
3971 | |
3972 | |
3973 | if (callSite->getDebugLoc()) |
3974 | newCall->setDebugLoc(callSite->getDebugLoc()); |
3975 | |
3976 | callSite->eraseFromParent(); |
3977 | } |
3978 | } |
3979 | |
3980 | |
3981 | |
3982 | |
3983 | |
3984 | |
3985 | |
3986 | |
3987 | |
3988 | |
3989 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
3990 | llvm::Function *NewFn) { |
3991 | |
3992 | if (!isa<llvm::Function>(Old)) return; |
3993 | |
3994 | replaceUsesOfNonProtoConstant(Old, NewFn); |
3995 | } |
3996 | |
3997 | void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
3998 | auto DK = VD->isThisDeclarationADefinition(); |
3999 | if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) |
4000 | return; |
4001 | |
4002 | TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); |
4003 | |
4004 | |
4005 | if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) |
4006 | GetAddrOfGlobalVar(VD); |
4007 | |
4008 | EmitTopLevelDecl(VD); |
4009 | } |
4010 | |
4011 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, |
4012 | llvm::GlobalValue *GV) { |
4013 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
4014 | |
4015 | |
4016 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
4017 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
4018 | |
4019 | |
4020 | if (!GV || (GV->getType()->getElementType() != Ty)) |
4021 | GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, , |
4022 | , |
4023 | ForDefinition)); |
4024 | |
4025 | |
4026 | if (!GV->isDeclaration()) |
4027 | return; |
4028 | |
4029 | |
4030 | |
4031 | |
4032 | |
4033 | auto *Fn = cast<llvm::Function>(GV); |
4034 | setFunctionLinkage(GD, Fn); |
4035 | |
4036 | |
4037 | setGVProperties(Fn, GD); |
4038 | |
4039 | MaybeHandleStaticInExternC(D, Fn); |
4040 | |
4041 | |
4042 | maybeSetTrivialComdat(*D, *Fn); |
4043 | |
4044 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
4045 | |
4046 | setNonAliasAttributes(GD, Fn); |
4047 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
4048 | |
4049 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
4050 | AddGlobalCtor(Fn, CA->getPriority()); |
4051 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
4052 | AddGlobalDtor(Fn, DA->getPriority()); |
4053 | if (D->hasAttr<AnnotateAttr>()) |
4054 | AddGlobalAnnotations(D, Fn); |
4055 | } |
4056 | |
4057 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
4058 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
4059 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
4060 | (0) . __assert_fail ("AA && \"Not an alias?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4060, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AA && "Not an alias?"); |
4061 | |
4062 | StringRef MangledName = getMangledName(GD); |
4063 | |
4064 | if (AA->getAliasee() == MangledName) { |
4065 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
4066 | return; |
4067 | } |
4068 | |
4069 | |
4070 | |
4071 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
4072 | if (Entry && !Entry->isDeclaration()) |
4073 | return; |
4074 | |
4075 | Aliases.push_back(GD); |
4076 | |
4077 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
4078 | |
4079 | |
4080 | |
4081 | llvm::Constant *Aliasee; |
4082 | if (isa<llvm::FunctionType>(DeclTy)) |
4083 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, |
4084 | ); |
4085 | else |
4086 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
4087 | llvm::PointerType::getUnqual(DeclTy), |
4088 | ); |
4089 | |
4090 | |
4091 | auto *GA = llvm::GlobalAlias::create( |
4092 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Aliasee, &getModule()); |
4093 | |
4094 | if (Entry) { |
4095 | if (GA->getAliasee() == Entry) { |
4096 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
4097 | return; |
4098 | } |
4099 | |
4100 | isDeclaration()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4100, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Entry->isDeclaration()); |
4101 | |
4102 | |
4103 | |
4104 | |
4105 | |
4106 | |
4107 | |
4108 | |
4109 | GA->takeName(Entry); |
4110 | |
4111 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
4112 | Entry->getType())); |
4113 | Entry->eraseFromParent(); |
4114 | } else { |
4115 | GA->setName(MangledName); |
4116 | } |
4117 | |
4118 | |
4119 | |
4120 | |
4121 | if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || |
4122 | D->isWeakImported()) { |
4123 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
4124 | } |
4125 | |
4126 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
4127 | if (VD->getTLSKind()) |
4128 | setTLSMode(GA, *VD); |
4129 | |
4130 | SetCommonAttributes(GD, GA); |
4131 | } |
4132 | |
4133 | void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { |
4134 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
4135 | const IFuncAttr *IFA = D->getAttr<IFuncAttr>(); |
4136 | (0) . __assert_fail ("IFA && \"Not an ifunc?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4136, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IFA && "Not an ifunc?"); |
4137 | |
4138 | StringRef MangledName = getMangledName(GD); |
4139 | |
4140 | if (IFA->getResolver() == MangledName) { |
4141 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
4142 | return; |
4143 | } |
4144 | |
4145 | |
4146 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
4147 | if (Entry && !Entry->isDeclaration()) { |
4148 | GlobalDecl OtherGD; |
4149 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
4150 | DiagnosedConflictingDefinitions.insert(GD).second) { |
4151 | Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name) |
4152 | << MangledName; |
4153 | Diags.Report(OtherGD.getDecl()->getLocation(), |
4154 | diag::note_previous_definition); |
4155 | } |
4156 | return; |
4157 | } |
4158 | |
4159 | Aliases.push_back(GD); |
4160 | |
4161 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
4162 | llvm::Constant *Resolver = |
4163 | GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD, |
4164 | ); |
4165 | llvm::GlobalIFunc *GIF = |
4166 | llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, |
4167 | "", Resolver, &getModule()); |
4168 | if (Entry) { |
4169 | if (GIF->getResolver() == Entry) { |
4170 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
4171 | return; |
4172 | } |
4173 | isDeclaration()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4173, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Entry->isDeclaration()); |
4174 | |
4175 | |
4176 | |
4177 | |
4178 | |
4179 | |
4180 | |
4181 | |
4182 | GIF->takeName(Entry); |
4183 | |
4184 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF, |
4185 | Entry->getType())); |
4186 | Entry->eraseFromParent(); |
4187 | } else |
4188 | GIF->setName(MangledName); |
4189 | |
4190 | SetCommonAttributes(GD, GIF); |
4191 | } |
4192 | |
4193 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
4194 | ArrayRef<llvm::Type*> Tys) { |
4195 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
4196 | Tys); |
4197 | } |
4198 | |
4199 | static llvm::StringMapEntry<llvm::GlobalVariable *> & |
4200 | GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map, |
4201 | const StringLiteral *Literal, bool TargetIsLSB, |
4202 | bool &IsUTF16, unsigned &StringLength) { |
4203 | StringRef String = Literal->getString(); |
4204 | unsigned NumBytes = String.size(); |
4205 | |
4206 | |
4207 | if (!Literal->containsNonAsciiOrNull()) { |
4208 | StringLength = NumBytes; |
4209 | return *Map.insert(std::make_pair(String, nullptr)).first; |
4210 | } |
4211 | |
4212 | |
4213 | IsUTF16 = true; |
4214 | |
4215 | SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); |
4216 | const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); |
4217 | llvm::UTF16 *ToPtr = &ToBuf[0]; |
4218 | |
4219 | (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, |
4220 | ToPtr + NumBytes, llvm::strictConversion); |
4221 | |
4222 | |
4223 | StringLength = ToPtr - &ToBuf[0]; |
4224 | |
4225 | |
4226 | *ToPtr = 0; |
4227 | return *Map.insert(std::make_pair( |
4228 | StringRef(reinterpret_cast<const char *>(ToBuf.data()), |
4229 | (StringLength + 1) * 2), |
4230 | nullptr)).first; |
4231 | } |
4232 | |
4233 | ConstantAddress |
4234 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
4235 | unsigned StringLength = 0; |
4236 | bool isUTF16 = false; |
4237 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
4238 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
4239 | getDataLayout().isLittleEndian(), isUTF16, |
4240 | StringLength); |
4241 | |
4242 | if (auto *C = Entry.second) |
4243 | return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment())); |
4244 | |
4245 | llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); |
4246 | llvm::Constant *Zeros[] = { Zero, Zero }; |
4247 | |
4248 | const ASTContext &Context = getContext(); |
4249 | const llvm::Triple &Triple = getTriple(); |
4250 | |
4251 | const auto CFRuntime = getLangOpts().CFRuntime; |
4252 | const bool IsSwiftABI = |
4253 | static_cast<unsigned>(CFRuntime) >= |
4254 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift); |
4255 | const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1; |
4256 | |
4257 | |
4258 | if (!CFConstantStringClassRef) { |
4259 | const char *CFConstantStringClassName = "__CFConstantStringClassReference"; |
4260 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
4261 | Ty = llvm::ArrayType::get(Ty, 0); |
4262 | |
4263 | switch (CFRuntime) { |
4264 | default: break; |
4265 | case LangOptions::CoreFoundationABI::Swift: LLVM_FALLTHROUGH; |
4266 | case LangOptions::CoreFoundationABI::Swift5_0: |
4267 | CFConstantStringClassName = |
4268 | Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN" |
4269 | : "$s10Foundation19_NSCFConstantStringCN"; |
4270 | Ty = IntPtrTy; |
4271 | break; |
4272 | case LangOptions::CoreFoundationABI::Swift4_2: |
4273 | CFConstantStringClassName = |
4274 | Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN" |
4275 | : "$S10Foundation19_NSCFConstantStringCN"; |
4276 | Ty = IntPtrTy; |
4277 | break; |
4278 | case LangOptions::CoreFoundationABI::Swift4_1: |
4279 | CFConstantStringClassName = |
4280 | Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN" |
4281 | : "__T010Foundation19_NSCFConstantStringCN"; |
4282 | Ty = IntPtrTy; |
4283 | break; |
4284 | } |
4285 | |
4286 | llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName); |
4287 | |
4288 | if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) { |
4289 | llvm::GlobalValue *GV = nullptr; |
4290 | |
4291 | if ((GV = dyn_cast<llvm::GlobalValue>(C))) { |
4292 | IdentifierInfo &II = Context.Idents.get(GV->getName()); |
4293 | TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl(); |
4294 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
4295 | |
4296 | const VarDecl *VD = nullptr; |
4297 | for (const auto &Result : DC->lookup(&II)) |
4298 | if ((VD = dyn_cast<VarDecl>(Result))) |
4299 | break; |
4300 | |
4301 | if (Triple.isOSBinFormatELF()) { |
4302 | if (!VD) |
4303 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
4304 | } else { |
4305 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
4306 | if (!VD || !VD->hasAttr<DLLExportAttr>()) |
4307 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
4308 | else |
4309 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
4310 | } |
4311 | |
4312 | setDSOLocal(GV); |
4313 | } |
4314 | } |
4315 | |
4316 | |
4317 | CFConstantStringClassRef = |
4318 | IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty) |
4319 | : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros); |
4320 | } |
4321 | |
4322 | QualType CFTy = Context.getCFConstantStringType(); |
4323 | |
4324 | auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
4325 | |
4326 | ConstantInitBuilder Builder(*this); |
4327 | auto Fields = Builder.beginStruct(STy); |
4328 | |
4329 | |
4330 | Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef)); |
4331 | |
4332 | |
4333 | if (IsSwiftABI) { |
4334 | Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01); |
4335 | Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8); |
4336 | } else { |
4337 | Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8); |
4338 | } |
4339 | |
4340 | |
4341 | llvm::Constant *C = nullptr; |
4342 | if (isUTF16) { |
4343 | auto Arr = llvm::makeArrayRef( |
4344 | reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())), |
4345 | Entry.first().size() / 2); |
4346 | C = llvm::ConstantDataArray::get(VMContext, Arr); |
4347 | } else { |
4348 | C = llvm::ConstantDataArray::getString(VMContext, Entry.first()); |
4349 | } |
4350 | |
4351 | |
4352 | |
4353 | auto *GV = |
4354 | new llvm::GlobalVariable(getModule(), C->getType(), , |
4355 | llvm::GlobalValue::PrivateLinkage, C, ".str"); |
4356 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
4357 | |
4358 | |
4359 | CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy) |
4360 | : Context.getTypeAlignInChars(Context.CharTy); |
4361 | GV->setAlignment(Align.getQuantity()); |
4362 | |
4363 | |
4364 | |
4365 | |
4366 | if (Triple.isOSBinFormatMachO()) |
4367 | GV->setSection(isUTF16 ? "__TEXT,__ustring" |
4368 | : "__TEXT,__cstring,cstring_literals"); |
4369 | |
4370 | |
4371 | else if (Triple.isOSBinFormatELF()) |
4372 | GV->setSection(".rodata"); |
4373 | |
4374 | |
4375 | llvm::Constant *Str = |
4376 | llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros); |
4377 | |
4378 | if (isUTF16) |
4379 | |
4380 | Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy); |
4381 | Fields.add(Str); |
4382 | |
4383 | |
4384 | llvm::IntegerType *LengthTy = |
4385 | llvm::IntegerType::get(getModule().getContext(), |
4386 | Context.getTargetInfo().getLongWidth()); |
4387 | if (IsSwiftABI) { |
4388 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || |
4389 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) |
4390 | LengthTy = Int32Ty; |
4391 | else |
4392 | LengthTy = IntPtrTy; |
4393 | } |
4394 | Fields.addInt(LengthTy, StringLength); |
4395 | |
4396 | CharUnits Alignment = getPointerAlign(); |
4397 | |
4398 | |
4399 | GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment, |
4400 | , |
4401 | llvm::GlobalVariable::PrivateLinkage); |
4402 | switch (Triple.getObjectFormat()) { |
4403 | case llvm::Triple::UnknownObjectFormat: |
4404 | llvm_unreachable("unknown file format"); |
4405 | case llvm::Triple::XCOFF: |
4406 | llvm_unreachable("XCOFF is not yet implemented"); |
4407 | case llvm::Triple::COFF: |
4408 | case llvm::Triple::ELF: |
4409 | case llvm::Triple::Wasm: |
4410 | GV->setSection("cfstring"); |
4411 | break; |
4412 | case llvm::Triple::MachO: |
4413 | GV->setSection("__DATA,__cfstring"); |
4414 | break; |
4415 | } |
4416 | Entry.second = GV; |
4417 | |
4418 | return ConstantAddress(GV, Alignment); |
4419 | } |
4420 | |
4421 | bool CodeGenModule::getExpressionLocationsEnabled() const { |
4422 | return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo; |
4423 | } |
4424 | |
4425 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
4426 | if (ObjCFastEnumerationStateType.isNull()) { |
4427 | RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); |
4428 | D->startDefinition(); |
4429 | |
4430 | QualType FieldTypes[] = { |
4431 | Context.UnsignedLongTy, |
4432 | Context.getPointerType(Context.getObjCIdType()), |
4433 | Context.getPointerType(Context.UnsignedLongTy), |
4434 | Context.getConstantArrayType(Context.UnsignedLongTy, |
4435 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
4436 | }; |
4437 | |
4438 | for (size_t i = 0; i < 4; ++i) { |
4439 | FieldDecl *Field = FieldDecl::Create(Context, |
4440 | D, |
4441 | SourceLocation(), |
4442 | SourceLocation(), nullptr, |
4443 | FieldTypes[i], , |
4444 | , |
4445 | , |
4446 | ICIS_NoInit); |
4447 | Field->setAccess(AS_public); |
4448 | D->addDecl(Field); |
4449 | } |
4450 | |
4451 | D->completeDefinition(); |
4452 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
4453 | } |
4454 | |
4455 | return ObjCFastEnumerationStateType; |
4456 | } |
4457 | |
4458 | llvm::Constant * |
4459 | CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { |
4460 | (0) . __assert_fail ("!E->getType()->isPointerType() && \"Strings are always arrays\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4460, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
4461 | |
4462 | |
4463 | |
4464 | if (E->getCharByteWidth() == 1) { |
4465 | SmallString<64> Str(E->getString()); |
4466 | |
4467 | |
4468 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); |
4469 | Str.resize(CAT->getSize().getZExtValue()); |
4470 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
4471 | } |
4472 | |
4473 | auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); |
4474 | llvm::Type *ElemTy = AType->getElementType(); |
4475 | unsigned NumElements = AType->getNumElements(); |
4476 | |
4477 | |
4478 | if (ElemTy->getPrimitiveSizeInBits() == 16) { |
4479 | SmallVector<uint16_t, 32> Elements; |
4480 | Elements.reserve(NumElements); |
4481 | |
4482 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
4483 | Elements.push_back(E->getCodeUnit(i)); |
4484 | Elements.resize(NumElements); |
4485 | return llvm::ConstantDataArray::get(VMContext, Elements); |
4486 | } |
4487 | |
4488 | getPrimitiveSizeInBits() == 32", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4488, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ElemTy->getPrimitiveSizeInBits() == 32); |
4489 | SmallVector<uint32_t, 32> Elements; |
4490 | Elements.reserve(NumElements); |
4491 | |
4492 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
4493 | Elements.push_back(E->getCodeUnit(i)); |
4494 | Elements.resize(NumElements); |
4495 | return llvm::ConstantDataArray::get(VMContext, Elements); |
4496 | } |
4497 | |
4498 | static llvm::GlobalVariable * |
4499 | GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, |
4500 | CodeGenModule &CGM, StringRef GlobalName, |
4501 | CharUnits Alignment) { |
4502 | unsigned AddrSpace = CGM.getContext().getTargetAddressSpace( |
4503 | CGM.getStringLiteralAddressSpace()); |
4504 | |
4505 | llvm::Module &M = CGM.getModule(); |
4506 | |
4507 | auto *GV = new llvm::GlobalVariable( |
4508 | M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName, |
4509 | nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
4510 | GV->setAlignment(Alignment.getQuantity()); |
4511 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
4512 | if (GV->isWeakForLinker()) { |
4513 | (0) . __assert_fail ("CGM.supportsCOMDAT() && \"Only COFF uses weak string literals\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4513, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals"); |
4514 | GV->setComdat(M.getOrInsertComdat(GV->getName())); |
4515 | } |
4516 | CGM.setDSOLocal(GV); |
4517 | |
4518 | return GV; |
4519 | } |
4520 | |
4521 | |
4522 | |
4523 | ConstantAddress |
4524 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S, |
4525 | StringRef Name) { |
4526 | CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType()); |
4527 | |
4528 | llvm::Constant *C = GetConstantArrayFromStringLiteral(S); |
4529 | llvm::GlobalVariable **Entry = nullptr; |
4530 | if (!LangOpts.WritableStrings) { |
4531 | Entry = &ConstantStringMap[C]; |
4532 | if (auto GV = *Entry) { |
4533 | if (Alignment.getQuantity() > GV->getAlignment()) |
4534 | GV->setAlignment(Alignment.getQuantity()); |
4535 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
4536 | Alignment); |
4537 | } |
4538 | } |
4539 | |
4540 | SmallString<256> MangledNameBuffer; |
4541 | StringRef GlobalVariableName; |
4542 | llvm::GlobalValue::LinkageTypes LT; |
4543 | |
4544 | |
4545 | |
4546 | |
4547 | if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) && |
4548 | !LangOpts.WritableStrings) { |
4549 | llvm::raw_svector_ostream Out(MangledNameBuffer); |
4550 | getCXXABI().getMangleContext().mangleStringLiteral(S, Out); |
4551 | LT = llvm::GlobalValue::LinkOnceODRLinkage; |
4552 | GlobalVariableName = MangledNameBuffer; |
4553 | } else { |
4554 | LT = llvm::GlobalValue::PrivateLinkage; |
4555 | GlobalVariableName = Name; |
4556 | } |
4557 | |
4558 | auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); |
4559 | if (Entry) |
4560 | *Entry = GV; |
4561 | |
4562 | SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>", |
4563 | QualType()); |
4564 | |
4565 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
4566 | Alignment); |
4567 | } |
4568 | |
4569 | |
4570 | |
4571 | ConstantAddress |
4572 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
4573 | std::string Str; |
4574 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
4575 | |
4576 | return GetAddrOfConstantCString(Str); |
4577 | } |
4578 | |
4579 | |
4580 | |
4581 | |
4582 | ConstantAddress CodeGenModule::GetAddrOfConstantCString( |
4583 | const std::string &Str, const char *GlobalName) { |
4584 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
4585 | CharUnits Alignment = |
4586 | getContext().getAlignOfGlobalVarInChars(getContext().CharTy); |
4587 | |
4588 | llvm::Constant *C = |
4589 | llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false); |
4590 | |
4591 | |
4592 | llvm::GlobalVariable **Entry = nullptr; |
4593 | if (!LangOpts.WritableStrings) { |
4594 | Entry = &ConstantStringMap[C]; |
4595 | if (auto GV = *Entry) { |
4596 | if (Alignment.getQuantity() > GV->getAlignment()) |
4597 | GV->setAlignment(Alignment.getQuantity()); |
4598 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
4599 | Alignment); |
4600 | } |
4601 | } |
4602 | |
4603 | |
4604 | if (!GlobalName) |
4605 | GlobalName = ".str"; |
4606 | |
4607 | auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, |
4608 | GlobalName, Alignment); |
4609 | if (Entry) |
4610 | *Entry = GV; |
4611 | |
4612 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
4613 | Alignment); |
4614 | } |
4615 | |
4616 | ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( |
4617 | const MaterializeTemporaryExpr *E, const Expr *Init) { |
4618 | (0) . __assert_fail ("(E->getStorageDuration() == SD_Static || E->getStorageDuration() == SD_Thread) && \"not a global temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4619, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((E->getStorageDuration() == SD_Static || |
4619 | (0) . __assert_fail ("(E->getStorageDuration() == SD_Static || E->getStorageDuration() == SD_Thread) && \"not a global temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 4619, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> E->getStorageDuration() == SD_Thread) && "not a global temporary"); |
4620 | const auto *VD = cast<VarDecl>(E->getExtendingDecl()); |
4621 | |
4622 | |
4623 | |
4624 | QualType MaterializedType = Init->getType(); |
4625 | if (Init == E->GetTemporaryExpr()) |
4626 | MaterializedType = E->getType(); |
4627 | |
4628 | CharUnits Align = getContext().getTypeAlignInChars(MaterializedType); |
4629 | |
4630 | if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E]) |
4631 | return ConstantAddress(Slot, Align); |
4632 | |
4633 | |
4634 | |
4635 | |
4636 | SmallString<256> Name; |
4637 | llvm::raw_svector_ostream Out(Name); |
4638 | getCXXABI().getMangleContext().mangleReferenceTemporary( |
4639 | VD, E->getManglingNumber(), Out); |
4640 | |
4641 | APValue *Value = nullptr; |
4642 | if (E->getStorageDuration() == SD_Static) { |
4643 | |
4644 | |
4645 | |
4646 | |
4647 | Value = getContext().getMaterializedTemporaryValue(E, false); |
4648 | if (Value && Value->isUninit()) |
4649 | Value = nullptr; |
4650 | } |
4651 | |
4652 | |
4653 | Expr::EvalResult EvalResult; |
4654 | if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && |
4655 | !EvalResult.hasSideEffects()) |
4656 | Value = &EvalResult.Val; |
4657 | |
4658 | LangAS AddrSpace = |
4659 | VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace(); |
4660 | |
4661 | Optional<ConstantEmitter> emitter; |
4662 | llvm::Constant *InitialValue = nullptr; |
4663 | bool Constant = false; |
4664 | llvm::Type *Type; |
4665 | if (Value) { |
4666 | |
4667 | emitter.emplace(*this); |
4668 | InitialValue = emitter->emitForInitializer(*Value, AddrSpace, |
4669 | MaterializedType); |
4670 | Constant = isTypeConstant(MaterializedType, Value); |
4671 | Type = InitialValue->getType(); |
4672 | } else { |
4673 | |
4674 | |
4675 | Type = getTypes().ConvertTypeForMem(MaterializedType); |
4676 | } |
4677 | |
4678 | |
4679 | llvm::GlobalValue::LinkageTypes Linkage = |
4680 | getLLVMLinkageVarDefinition(VD, Constant); |
4681 | if (Linkage == llvm::GlobalVariable::ExternalLinkage) { |
4682 | const VarDecl *InitVD; |
4683 | if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) && |
4684 | isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) { |
4685 | |
4686 | |
4687 | Linkage = llvm::GlobalVariable::LinkOnceODRLinkage; |
4688 | } else { |
4689 | |
4690 | |
4691 | Linkage = llvm::GlobalVariable::InternalLinkage; |
4692 | } |
4693 | } |
4694 | auto TargetAS = getContext().getTargetAddressSpace(AddrSpace); |
4695 | auto *GV = new llvm::GlobalVariable( |
4696 | getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), |
4697 | , llvm::GlobalVariable::NotThreadLocal, TargetAS); |
4698 | if (emitter) emitter->finalize(GV); |
4699 | setGVProperties(GV, VD); |
4700 | GV->setAlignment(Align.getQuantity()); |
4701 | if (supportsCOMDAT() && GV->isWeakForLinker()) |
4702 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
4703 | if (VD->getTLSKind()) |
4704 | setTLSMode(GV, *VD); |
4705 | llvm::Constant *CV = GV; |
4706 | if (AddrSpace != LangAS::Default) |
4707 | CV = getTargetCodeGenInfo().performAddrSpaceCast( |
4708 | *this, GV, AddrSpace, LangAS::Default, |
4709 | Type->getPointerTo( |
4710 | getContext().getTargetAddressSpace(LangAS::Default))); |
4711 | MaterializedGlobalTemporaryMap[E] = CV; |
4712 | return ConstantAddress(CV, Align); |
4713 | } |
4714 | |
4715 | |
4716 | |
4717 | void CodeGenModule::EmitObjCPropertyImplementations(const |
4718 | ObjCImplementationDecl *D) { |
4719 | for (const auto *PID : D->property_impls()) { |
4720 | |
4721 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
4722 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
4723 | |
4724 | |
4725 | |
4726 | |
4727 | |
4728 | |
4729 | if (!D->getInstanceMethod(PD->getGetterName())) |
4730 | CodeGenFunction(*this).GenerateObjCGetter( |
4731 | const_cast<ObjCImplementationDecl *>(D), PID); |
4732 | if (!PD->isReadOnly() && |
4733 | !D->getInstanceMethod(PD->getSetterName())) |
4734 | CodeGenFunction(*this).GenerateObjCSetter( |
4735 | const_cast<ObjCImplementationDecl *>(D), PID); |
4736 | } |
4737 | } |
4738 | } |
4739 | |
4740 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
4741 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
4742 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
4743 | ivar; ivar = ivar->getNextIvar()) |
4744 | if (ivar->getType().isDestructedType()) |
4745 | return true; |
4746 | |
4747 | return false; |
4748 | } |
4749 | |
4750 | static bool AllTrivialInitializers(CodeGenModule &CGM, |
4751 | ObjCImplementationDecl *D) { |
4752 | CodeGenFunction CGF(CGM); |
4753 | for (ObjCImplementationDecl::init_iterator B = D->init_begin(), |
4754 | E = D->init_end(); B != E; ++B) { |
4755 | CXXCtorInitializer *CtorInitExp = *B; |
4756 | Expr *Init = CtorInitExp->getInit(); |
4757 | if (!CGF.isTrivialInitializer(Init)) |
4758 | return false; |
4759 | } |
4760 | return true; |
4761 | } |
4762 | |
4763 | |
4764 | |
4765 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
4766 | |
4767 | if (needsDestructMethod(D)) { |
4768 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
4769 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
4770 | ObjCMethodDecl *DTORMethod = |
4771 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
4772 | cxxSelector, getContext().VoidTy, nullptr, D, |
4773 | , , |
4774 | , , |
4775 | , ObjCMethodDecl::Required); |
4776 | D->addInstanceMethod(DTORMethod); |
4777 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
4778 | D->setHasDestructors(true); |
4779 | } |
4780 | |
4781 | |
4782 | |
4783 | if (D->getNumIvarInitializers() == 0 || |
4784 | AllTrivialInitializers(*this, D)) |
4785 | return; |
4786 | |
4787 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
4788 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
4789 | |
4790 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
4791 | D->getLocation(), |
4792 | D->getLocation(), |
4793 | cxxSelector, |
4794 | getContext().getObjCIdType(), |
4795 | nullptr, D, , |
4796 | , |
4797 | , |
4798 | , |
4799 | , |
4800 | ObjCMethodDecl::Required); |
4801 | D->addInstanceMethod(CTORMethod); |
4802 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
4803 | D->setHasNonZeroConstructors(true); |
4804 | } |
4805 | |
4806 | |
4807 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
4808 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
4809 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
4810 | ErrorUnsupported(LSD, "linkage spec"); |
4811 | return; |
4812 | } |
4813 | |
4814 | EmitDeclContext(LSD); |
4815 | } |
4816 | |
4817 | void CodeGenModule::EmitDeclContext(const DeclContext *DC) { |
4818 | for (auto *I : DC->decls()) { |
4819 | |
4820 | |
4821 | |
4822 | |
4823 | |
4824 | if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { |
4825 | for (auto *M : OID->methods()) |
4826 | EmitTopLevelDecl(M); |
4827 | } |
4828 | |
4829 | EmitTopLevelDecl(I); |
4830 | } |
4831 | } |
4832 | |
4833 | |
4834 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
4835 | |
4836 | if (D->isTemplated()) |
4837 | return; |
4838 | |
4839 | switch (D->getKind()) { |
4840 | case Decl::CXXConversion: |
4841 | case Decl::CXXMethod: |
4842 | case Decl::Function: |
4843 | EmitGlobal(cast<FunctionDecl>(D)); |
4844 | |
4845 | |
4846 | AddDeferredUnusedCoverageMapping(D); |
4847 | break; |
4848 | |
4849 | case Decl::CXXDeductionGuide: |
4850 | |
4851 | break; |
4852 | |
4853 | case Decl::Var: |
4854 | case Decl::Decomposition: |
4855 | case Decl::VarTemplateSpecialization: |
4856 | EmitGlobal(cast<VarDecl>(D)); |
4857 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) |
4858 | for (auto *B : DD->bindings()) |
4859 | if (auto *HD = B->getHoldingVar()) |
4860 | EmitGlobal(HD); |
4861 | break; |
4862 | |
4863 | |
4864 | |
4865 | case Decl::IndirectField: |
4866 | break; |
4867 | |
4868 | |
4869 | case Decl::Namespace: |
4870 | EmitDeclContext(cast<NamespaceDecl>(D)); |
4871 | break; |
4872 | case Decl::ClassTemplateSpecialization: { |
4873 | const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); |
4874 | if (DebugInfo && |
4875 | Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition && |
4876 | Spec->hasDefinition()) |
4877 | DebugInfo->completeTemplateDefinition(*Spec); |
4878 | } LLVM_FALLTHROUGH; |
4879 | case Decl::CXXRecord: |
4880 | if (DebugInfo) { |
4881 | if (auto *ES = D->getASTContext().getExternalSource()) |
4882 | if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) |
4883 | DebugInfo->completeUnusedClass(cast<CXXRecordDecl>(*D)); |
4884 | } |
4885 | |
4886 | for (auto *I : cast<CXXRecordDecl>(D)->decls()) |
4887 | if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I)) |
4888 | EmitTopLevelDecl(I); |
4889 | break; |
4890 | |
4891 | case Decl::UsingShadow: |
4892 | case Decl::ClassTemplate: |
4893 | case Decl::VarTemplate: |
4894 | case Decl::VarTemplatePartialSpecialization: |
4895 | case Decl::FunctionTemplate: |
4896 | case Decl::TypeAliasTemplate: |
4897 | case Decl::Block: |
4898 | case Decl::Empty: |
4899 | case Decl::Binding: |
4900 | break; |
4901 | case Decl::Using: |
4902 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
4903 | DI->EmitUsingDecl(cast<UsingDecl>(*D)); |
4904 | return; |
4905 | case Decl::NamespaceAlias: |
4906 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
4907 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); |
4908 | return; |
4909 | case Decl::UsingDirective: |
4910 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
4911 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); |
4912 | return; |
4913 | case Decl::CXXConstructor: |
4914 | getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
4915 | break; |
4916 | case Decl::CXXDestructor: |
4917 | getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
4918 | break; |
4919 | |
4920 | case Decl::StaticAssert: |
4921 | |
4922 | break; |
4923 | |
4924 | |
4925 | |
4926 | |
4927 | case Decl::ObjCInterface: |
4928 | case Decl::ObjCCategory: |
4929 | break; |
4930 | |
4931 | case Decl::ObjCProtocol: { |
4932 | auto *Proto = cast<ObjCProtocolDecl>(D); |
4933 | if (Proto->isThisDeclarationADefinition()) |
4934 | ObjCRuntime->GenerateProtocol(Proto); |
4935 | break; |
4936 | } |
4937 | |
4938 | case Decl::ObjCCategoryImpl: |
4939 | |
4940 | |
4941 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
4942 | break; |
4943 | |
4944 | case Decl::ObjCImplementation: { |
4945 | auto *OMD = cast<ObjCImplementationDecl>(D); |
4946 | EmitObjCPropertyImplementations(OMD); |
4947 | EmitObjCIvarInitializations(OMD); |
4948 | ObjCRuntime->GenerateClass(OMD); |
4949 | |
4950 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
4951 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
4952 | DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( |
4953 | OMD->getClassInterface()), OMD->getLocation()); |
4954 | break; |
4955 | } |
4956 | case Decl::ObjCMethod: { |
4957 | auto *OMD = cast<ObjCMethodDecl>(D); |
4958 | |
4959 | if (OMD->getBody()) |
4960 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
4961 | break; |
4962 | } |
4963 | case Decl::ObjCCompatibleAlias: |
4964 | ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); |
4965 | break; |
4966 | |
4967 | case Decl::PragmaComment: { |
4968 | const auto *PCD = cast<PragmaCommentDecl>(D); |
4969 | switch (PCD->getCommentKind()) { |
4970 | case PCK_Unknown: |
4971 | llvm_unreachable("unexpected pragma comment kind"); |
4972 | case PCK_Linker: |
4973 | AppendLinkerOptions(PCD->getArg()); |
4974 | break; |
4975 | case PCK_Lib: |
4976 | if (getTarget().getTriple().isOSBinFormatELF() && |
4977 | !getTarget().getTriple().isPS4()) |
4978 | AddELFLibDirective(PCD->getArg()); |
4979 | else |
4980 | AddDependentLib(PCD->getArg()); |
4981 | break; |
4982 | case PCK_Compiler: |
4983 | case PCK_ExeStr: |
4984 | case PCK_User: |
4985 | break; |
4986 | } |
4987 | break; |
4988 | } |
4989 | |
4990 | case Decl::PragmaDetectMismatch: { |
4991 | const auto *PDMD = cast<PragmaDetectMismatchDecl>(D); |
4992 | AddDetectMismatch(PDMD->getName(), PDMD->getValue()); |
4993 | break; |
4994 | } |
4995 | |
4996 | case Decl::LinkageSpec: |
4997 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
4998 | break; |
4999 | |
5000 | case Decl::FileScopeAsm: { |
5001 | |
5002 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) |
5003 | break; |
5004 | |
5005 | if (LangOpts.OpenMPIsDevice) |
5006 | break; |
5007 | auto *AD = cast<FileScopeAsmDecl>(D); |
5008 | getModule().appendModuleInlineAsm(AD->getAsmString()->getString()); |
5009 | break; |
5010 | } |
5011 | |
5012 | case Decl::Import: { |
5013 | auto *Import = cast<ImportDecl>(D); |
5014 | |
5015 | |
5016 | if (!ImportedModules.insert(Import->getImportedModule())) |
5017 | break; |
5018 | |
5019 | |
5020 | if (!Import->getImportedOwningModule()) { |
5021 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
5022 | DI->EmitImportDecl(*Import); |
5023 | } |
5024 | |
5025 | |
5026 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
5027 | SmallVector<clang::Module *, 16> Stack; |
5028 | Visited.insert(Import->getImportedModule()); |
5029 | Stack.push_back(Import->getImportedModule()); |
5030 | |
5031 | while (!Stack.empty()) { |
5032 | clang::Module *Mod = Stack.pop_back_val(); |
5033 | if (!EmittedModuleInitializers.insert(Mod).second) |
5034 | continue; |
5035 | |
5036 | for (auto *D : Context.getModuleInitializers(Mod)) |
5037 | EmitTopLevelDecl(D); |
5038 | |
5039 | |
5040 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
5041 | SubEnd = Mod->submodule_end(); |
5042 | Sub != SubEnd; ++Sub) { |
5043 | |
5044 | |
5045 | if ((*Sub)->IsExplicit) |
5046 | continue; |
5047 | |
5048 | if (Visited.insert(*Sub).second) |
5049 | Stack.push_back(*Sub); |
5050 | } |
5051 | } |
5052 | break; |
5053 | } |
5054 | |
5055 | case Decl::Export: |
5056 | EmitDeclContext(cast<ExportDecl>(D)); |
5057 | break; |
5058 | |
5059 | case Decl::OMPThreadPrivate: |
5060 | EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D)); |
5061 | break; |
5062 | |
5063 | case Decl::OMPAllocate: |
5064 | break; |
5065 | |
5066 | case Decl::OMPDeclareReduction: |
5067 | EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D)); |
5068 | break; |
5069 | |
5070 | case Decl::OMPDeclareMapper: |
5071 | EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D)); |
5072 | break; |
5073 | |
5074 | case Decl::OMPRequires: |
5075 | EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D)); |
5076 | break; |
5077 | |
5078 | default: |
5079 | |
5080 | |
5081 | |
5082 | (0) . __assert_fail ("isa(D) && \"Unsupported decl kind\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenModule.cpp", 5082, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
5083 | break; |
5084 | } |
5085 | } |
5086 | |
5087 | void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { |
5088 | |
5089 | if (!CodeGenOpts.CoverageMapping) |
5090 | return; |
5091 | switch (D->getKind()) { |
5092 | case Decl::CXXConversion: |
5093 | case Decl::CXXMethod: |
5094 | case Decl::Function: |
5095 | case Decl::ObjCMethod: |
5096 | case Decl::CXXConstructor: |
5097 | case Decl::CXXDestructor: { |
5098 | if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) |
5099 | return; |
5100 | SourceManager &SM = getContext().getSourceManager(); |
5101 | if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc())) |
5102 | return; |
5103 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
5104 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
5105 | DeferredEmptyCoverageMappingDecls[D] = true; |
5106 | break; |
5107 | } |
5108 | default: |
5109 | break; |
5110 | }; |
5111 | } |
5112 | |
5113 | void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) { |
5114 | |
5115 | if (!CodeGenOpts.CoverageMapping) |
5116 | return; |
5117 | if (const auto *Fn = dyn_cast<FunctionDecl>(D)) { |
5118 | if (Fn->isTemplateInstantiation()) |
5119 | ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern()); |
5120 | } |
5121 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
5122 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
5123 | DeferredEmptyCoverageMappingDecls[D] = false; |
5124 | else |
5125 | I->second = false; |
5126 | } |
5127 | |
5128 | void CodeGenModule::EmitDeferredUnusedCoverageMappings() { |
5129 | |
5130 | |
5131 | |
5132 | |
5133 | for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) { |
5134 | if (!Entry.second) |
5135 | continue; |
5136 | const Decl *D = Entry.first; |
5137 | switch (D->getKind()) { |
5138 | case Decl::CXXConversion: |
5139 | case Decl::CXXMethod: |
5140 | case Decl::Function: |
5141 | case Decl::ObjCMethod: { |
5142 | CodeGenPGO PGO(*this); |
5143 | GlobalDecl GD(cast<FunctionDecl>(D)); |
5144 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
5145 | getFunctionLinkage(GD)); |
5146 | break; |
5147 | } |
5148 | case Decl::CXXConstructor: { |
5149 | CodeGenPGO PGO(*this); |
5150 | GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base); |
5151 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
5152 | getFunctionLinkage(GD)); |
5153 | break; |
5154 | } |
5155 | case Decl::CXXDestructor: { |
5156 | CodeGenPGO PGO(*this); |
5157 | GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base); |
5158 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
5159 | getFunctionLinkage(GD)); |
5160 | break; |
5161 | } |
5162 | default: |
5163 | break; |
5164 | }; |
5165 | } |
5166 | } |
5167 | |
5168 | |
5169 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
5170 | const void *Ptr) { |
5171 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
5172 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
5173 | return llvm::ConstantInt::get(i64, PtrInt); |
5174 | } |
5175 | |
5176 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
5177 | llvm::NamedMDNode *&GlobalMetadata, |
5178 | GlobalDecl D, |
5179 | llvm::GlobalValue *Addr) { |
5180 | if (!GlobalMetadata) |
5181 | GlobalMetadata = |
5182 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
5183 | |
5184 | |
5185 | llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr), |
5186 | llvm::ConstantAsMetadata::get(GetPointerConstant( |
5187 | CGM.getLLVMContext(), D.getDecl()))}; |
5188 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
5189 | } |
5190 | |
5191 | |
5192 | |
5193 | |
5194 | |
5195 | |
5196 | void CodeGenModule::EmitStaticExternCAliases() { |
5197 | if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases()) |
5198 | return; |
5199 | for (auto &I : StaticExternCValues) { |
5200 | IdentifierInfo *Name = I.first; |
5201 | llvm::GlobalValue *Val = I.second; |
5202 | if (Val && !getModule().getNamedValue(Name->getName())) |
5203 | addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); |
5204 | } |
5205 | } |
5206 | |
5207 | bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName, |
5208 | GlobalDecl &Result) const { |
5209 | auto Res = Manglings.find(MangledName); |
5210 | if (Res == Manglings.end()) |
5211 | return false; |
5212 | Result = Res->getValue(); |
5213 | return true; |
5214 | } |
5215 | |
5216 | |
5217 | |
5218 | |
5219 | |
5220 | |
5221 | |
5222 | |
5223 | void CodeGenModule::EmitDeclMetadata() { |
5224 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
5225 | |
5226 | for (auto &I : MangledDeclNames) { |
5227 | llvm::GlobalValue *Addr = getModule().getNamedValue(I.second); |
5228 | |
5229 | |
5230 | if (Addr) |
5231 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr); |
5232 | } |
5233 | } |
5234 | |
5235 | |
5236 | |
5237 | void CodeGenFunction::EmitDeclMetadata() { |
5238 | if (LocalDeclMap.empty()) return; |
5239 | |
5240 | llvm::LLVMContext &Context = getLLVMContext(); |
5241 | |
5242 | |
5243 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
5244 | |
5245 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
5246 | |
5247 | for (auto &I : LocalDeclMap) { |
5248 | const Decl *D = I.first; |
5249 | llvm::Value *Addr = I.second.getPointer(); |
5250 | if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
5251 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
5252 | Alloca->setMetadata( |
5253 | DeclPtrKind, llvm::MDNode::get( |
5254 | Context, llvm::ValueAsMetadata::getConstant(DAddr))); |
5255 | } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
5256 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
5257 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
5258 | } |
5259 | } |
5260 | } |
5261 | |
5262 | void CodeGenModule::EmitVersionIdentMetadata() { |
5263 | llvm::NamedMDNode *IdentMetadata = |
5264 | TheModule.getOrInsertNamedMetadata("llvm.ident"); |
5265 | std::string Version = getClangFullVersion(); |
5266 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
5267 | |
5268 | llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)}; |
5269 | IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); |
5270 | } |
5271 | |
5272 | void CodeGenModule::EmitCommandLineMetadata() { |
5273 | llvm::NamedMDNode *CommandLineMetadata = |
5274 | TheModule.getOrInsertNamedMetadata("llvm.commandline"); |
5275 | std::string CommandLine = getCodeGenOpts().RecordCommandLine; |
5276 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
5277 | |
5278 | llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)}; |
5279 | CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode)); |
5280 | } |
5281 | |
5282 | void CodeGenModule::EmitTargetMetadata() { |
5283 | |
5284 | |
5285 | |
5286 | |
5287 | |
5288 | |
5289 | for (unsigned I = 0; I != MangledDeclNames.size(); ++I) { |
5290 | auto Val = *(MangledDeclNames.begin() + I); |
5291 | const Decl *D = Val.first.getDecl()->getMostRecentDecl(); |
5292 | llvm::GlobalValue *GV = GetGlobalValue(Val.second); |
5293 | getTargetCodeGenInfo().emitTargetMD(D, GV, *this); |
5294 | } |
5295 | } |
5296 | |
5297 | void CodeGenModule::EmitCoverageFile() { |
5298 | if (getCodeGenOpts().CoverageDataFile.empty() && |
5299 | getCodeGenOpts().CoverageNotesFile.empty()) |
5300 | return; |
5301 | |
5302 | llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu"); |
5303 | if (!CUNode) |
5304 | return; |
5305 | |
5306 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
5307 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
5308 | auto *CoverageDataFile = |
5309 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile); |
5310 | auto *CoverageNotesFile = |
5311 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile); |
5312 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
5313 | llvm::MDNode *CU = CUNode->getOperand(i); |
5314 | llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU}; |
5315 | GCov->addOperand(llvm::MDNode::get(Ctx, Elts)); |
5316 | } |
5317 | } |
5318 | |
5319 | llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) { |
5320 | |
5321 | |
5322 | assert(Uuid.size() == 36); |
5323 | for (unsigned i = 0; i < 36; ++i) { |
5324 | if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-'); |
5325 | else assert(isHexDigit(Uuid[i])); |
5326 | } |
5327 | |
5328 | |
5329 | const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 }; |
5330 | |
5331 | llvm::Constant *Field3[8]; |
5332 | for (unsigned Idx = 0; Idx < 8; ++Idx) |
5333 | Field3[Idx] = llvm::ConstantInt::get( |
5334 | Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16); |
5335 | |
5336 | llvm::Constant *Fields[4] = { |
5337 | llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16), |
5338 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16), |
5339 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16), |
5340 | llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3) |
5341 | }; |
5342 | |
5343 | return llvm::ConstantStruct::getAnon(Fields); |
5344 | } |
5345 | |
5346 | llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, |
5347 | bool ForEH) { |
5348 | |
5349 | |
5350 | |
5351 | if ((!ForEH && !getLangOpts().RTTI) || getLangOpts().CUDAIsDevice) |
5352 | return llvm::Constant::getNullValue(Int8PtrTy); |
5353 | |
5354 | if (ForEH && Ty->isObjCObjectPointerType() && |
5355 | LangOpts.ObjCRuntime.isGNUFamily()) |
5356 | return ObjCRuntime->GetEHType(Ty); |
5357 | |
5358 | return getCXXABI().getAddrOfRTTIDescriptor(Ty); |
5359 | } |
5360 | |
5361 | void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { |
5362 | |
5363 | if (LangOpts.OpenMP && LangOpts.OpenMPSimd) |
5364 | return; |
5365 | for (auto RefExpr : D->varlists()) { |
5366 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl()); |
5367 | bool PerformInit = |
5368 | VD->getAnyInitializer() && |
5369 | !VD->getAnyInitializer()->isConstantInitializer(getContext(), |
5370 | ); |
5371 | |
5372 | Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD)); |
5373 | if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition( |
5374 | VD, Addr, RefExpr->getBeginLoc(), PerformInit)) |
5375 | CXXGlobalInits.push_back(InitFunction); |
5376 | } |
5377 | } |
5378 | |
5379 | llvm::Metadata * |
5380 | CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, |
5381 | StringRef Suffix) { |
5382 | llvm::Metadata *&InternalId = Map[T.getCanonicalType()]; |
5383 | if (InternalId) |
5384 | return InternalId; |
5385 | |
5386 | if (isExternallyVisible(T->getLinkage())) { |
5387 | std::string OutName; |
5388 | llvm::raw_string_ostream Out(OutName); |
5389 | getCXXABI().getMangleContext().mangleTypeName(T, Out); |
5390 | Out << Suffix; |
5391 | |
5392 | InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); |
5393 | } else { |
5394 | InternalId = llvm::MDNode::getDistinct(getLLVMContext(), |
5395 | llvm::ArrayRef<llvm::Metadata *>()); |
5396 | } |
5397 | |
5398 | return InternalId; |
5399 | } |
5400 | |
5401 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { |
5402 | return CreateMetadataIdentifierImpl(T, MetadataIdMap, ""); |
5403 | } |
5404 | |
5405 | llvm::Metadata * |
5406 | CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) { |
5407 | return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual"); |
5408 | } |
5409 | |
5410 | |
5411 | |
5412 | |
5413 | |
5414 | static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) { |
5415 | if (!Ty->isPointerType()) |
5416 | return Ty; |
5417 | |
5418 | return Ctx.getPointerType( |
5419 | QualType(Ctx.VoidTy).withCVRQualifiers( |
5420 | Ty->getPointeeType().getCVRQualifiers())); |
5421 | } |
5422 | |
5423 | |
5424 | static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) { |
5425 | if (auto *FnType = Ty->getAs<FunctionProtoType>()) { |
5426 | SmallVector<QualType, 8> GeneralizedParams; |
5427 | for (auto &Param : FnType->param_types()) |
5428 | GeneralizedParams.push_back(GeneralizeType(Ctx, Param)); |
5429 | |
5430 | return Ctx.getFunctionType( |
5431 | GeneralizeType(Ctx, FnType->getReturnType()), |
5432 | GeneralizedParams, FnType->getExtProtoInfo()); |
5433 | } |
5434 | |
5435 | if (auto *FnType = Ty->getAs<FunctionNoProtoType>()) |
5436 | return Ctx.getFunctionNoProtoType( |
5437 | GeneralizeType(Ctx, FnType->getReturnType())); |
5438 | |
5439 | llvm_unreachable("Encountered unknown FunctionType"); |
5440 | } |
5441 | |
5442 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { |
5443 | return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T), |
5444 | GeneralizedMetadataIdMap, ".generalized"); |
5445 | } |
5446 | |
5447 | |
5448 | bool CodeGenModule::NeedAllVtablesTypeId() const { |
5449 | |
5450 | |
5451 | return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) && |
5452 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) || |
5453 | (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) && |
5454 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) || |
5455 | (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) && |
5456 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) || |
5457 | (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) && |
5458 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast))); |
5459 | } |
5460 | |
5461 | void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable, |
5462 | CharUnits Offset, |
5463 | const CXXRecordDecl *RD) { |
5464 | llvm::Metadata *MD = |
5465 | CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
5466 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
5467 | |
5468 | if (CodeGenOpts.SanitizeCfiCrossDso) |
5469 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
5470 | VTable->addTypeMetadata(Offset.getQuantity(), |
5471 | llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
5472 | |
5473 | if (NeedAllVtablesTypeId()) { |
5474 | llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables"); |
5475 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
5476 | } |
5477 | } |
5478 | |
5479 | TargetAttr::ParsedTargetAttr CodeGenModule::filterFunctionTargetAttrs(const TargetAttr *TD) { |
5480 | assert(TD != nullptr); |
5481 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
5482 | |
5483 | ParsedAttr.Features.erase( |
5484 | llvm::remove_if(ParsedAttr.Features, |
5485 | [&](const std::string &Feat) { |
5486 | return !Target.isValidFeatureName( |
5487 | StringRef{Feat}.substr(1)); |
5488 | }), |
5489 | ParsedAttr.Features.end()); |
5490 | return ParsedAttr; |
5491 | } |
5492 | |
5493 | |
5494 | |
5495 | |
5496 | void CodeGenModule::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
5497 | GlobalDecl GD) { |
5498 | StringRef TargetCPU = Target.getTargetOpts().CPU; |
5499 | const FunctionDecl *FD = GD.getDecl()->getAsFunction(); |
5500 | if (const auto *TD = FD->getAttr<TargetAttr>()) { |
5501 | TargetAttr::ParsedTargetAttr ParsedAttr = filterFunctionTargetAttrs(TD); |
5502 | |
5503 | |
5504 | |
5505 | ParsedAttr.Features.insert(ParsedAttr.Features.begin(), |
5506 | Target.getTargetOpts().FeaturesAsWritten.begin(), |
5507 | Target.getTargetOpts().FeaturesAsWritten.end()); |
5508 | |
5509 | if (ParsedAttr.Architecture != "" && |
5510 | Target.isValidCPUName(ParsedAttr.Architecture)) |
5511 | TargetCPU = ParsedAttr.Architecture; |
5512 | |
5513 | |
5514 | |
5515 | |
5516 | |
5517 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
5518 | ParsedAttr.Features); |
5519 | } else if (const auto *SD = FD->getAttr<CPUSpecificAttr>()) { |
5520 | llvm::SmallVector<StringRef, 32> FeaturesTmp; |
5521 | Target.getCPUSpecificCPUDispatchFeatures( |
5522 | SD->getCPUName(GD.getMultiVersionIndex())->getName(), FeaturesTmp); |
5523 | std::vector<std::string> Features(FeaturesTmp.begin(), FeaturesTmp.end()); |
5524 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, Features); |
5525 | } else { |
5526 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
5527 | Target.getTargetOpts().Features); |
5528 | } |
5529 | } |
5530 | |
5531 | llvm::SanitizerStatReport &CodeGenModule::getSanStats() { |
5532 | if (!SanStats) |
5533 | SanStats = llvm::make_unique<llvm::SanitizerStatReport>(&getModule()); |
5534 | |
5535 | return *SanStats; |
5536 | } |
5537 | llvm::Value * |
5538 | CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, |
5539 | CodeGenFunction &CGF) { |
5540 | llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType()); |
5541 | auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr()); |
5542 | auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); |
5543 | return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy, |
5544 | "__translate_sampler_initializer"), |
5545 | {C}); |
5546 | } |
5547 | |