Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 15 | #include "CGBlocks.h" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 16 | #include "CGCUDARuntime.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "CGCall.h" |
| 19 | #include "CGDebugInfo.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 20 | #include "CGObjCRuntime.h" |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 21 | #include "CGOpenCLRuntime.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 22 | #include "CGOpenMPRuntime.h" |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 23 | #include "CGOpenMPRuntimeNVPTX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "CodeGenFunction.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 25 | #include "CodeGenPGO.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 26 | #include "CodeGenTBAA.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 27 | #include "CoverageMappingGen.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 28 | #include "TargetInfo.h" |
Chris Lattner | 2ccb73b | 2007-06-16 00:16:26 +0000 | [diff] [blame] | 29 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 30 | #include "clang/AST/CharUnits.h" |
Chris Lattner | b8c18fa | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 31 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 5dd3474 | 2010-06-21 18:41:26 +0000 | [diff] [blame] | 33 | #include "clang/AST/DeclTemplate.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 34 | #include "clang/AST/Mangle.h" |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 35 | #include "clang/AST/RecordLayout.h" |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 36 | #include "clang/AST/RecursiveASTVisitor.h" |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 37 | #include "clang/Basic/Builtins.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 38 | #include "clang/Basic/CharInfo.h" |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 39 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 40 | #include "clang/Basic/Module.h" |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 41 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 42 | #include "clang/Basic/TargetInfo.h" |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 43 | #include "clang/Basic/Version.h" |
John McCall | 5ad7407 | 2017-03-02 20:04:19 +0000 | [diff] [blame] | 44 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 45 | #include "clang/Frontend/CodeGenOptions.h" |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 46 | #include "clang/Sema/SemaDiagnostic.h" |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/Triple.h" |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 49 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 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" |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 55 | #include "llvm/ProfileData/InstrProfReader.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ConvertUTF.h" |
Chris Lattner | 15275e5 | 2009-11-07 09:22:46 +0000 | [diff] [blame] | 57 | #include "llvm/Support/ErrorHandling.h" |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 58 | #include "llvm/Support/MD5.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 59 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 60 | using namespace clang; |
| 61 | using namespace CodeGen; |
| 62 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 63 | static const char AnnotationSection[] = "llvm.metadata"; |
| 64 | |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 65 | static CGCXXABI *createCXXABI(CodeGenModule &CGM) { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 66 | switch (CGM.getTarget().getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 67 | case TargetCXXABI::GenericAArch64: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 68 | case TargetCXXABI::GenericARM: |
| 69 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 70 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 71 | case TargetCXXABI::WatchOS: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 72 | case TargetCXXABI::GenericMIPS: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 73 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 74 | case TargetCXXABI::WebAssembly: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 75 | return CreateItaniumCXXABI(CGM); |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 76 | case TargetCXXABI::Microsoft: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 77 | return CreateMicrosoftCXXABI(CGM); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | llvm_unreachable("invalid C++ ABI kind"); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 83 | CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO, |
| 84 | const PreprocessorOptions &PPO, |
| 85 | const CodeGenOptions &CGO, llvm::Module &M, |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 86 | DiagnosticsEngine &diags, |
| 87 | CoverageSourceInfo *CoverageInfo) |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 88 | : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO), |
| 89 | PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 90 | Target(C.getTargetInfo()), ABI(createCXXABI(*this)), |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 91 | VMContext(M.getContext()), Types(*this), VTables(*this), |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 92 | SanitizerMD(new SanitizerMetadata(*this)) { |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 93 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 94 | // Initialize the type cache. |
| 95 | llvm::LLVMContext &LLVMContext = M.getContext(); |
| 96 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
| 97 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
| 98 | Int16Ty = llvm::Type::getInt16Ty(LLVMContext); |
| 99 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 100 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
| 101 | FloatTy = llvm::Type::getFloatTy(LLVMContext); |
| 102 | DoubleTy = llvm::Type::getDoubleTy(LLVMContext); |
| 103 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
| 104 | PointerAlignInBytes = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 105 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
Yaxun Liu | 26f7566 | 2016-08-19 05:17:25 +0000 | [diff] [blame] | 106 | SizeSizeInBytes = |
| 107 | C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 108 | IntAlignInBytes = |
| 109 | C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 110 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
Yaxun Liu | 26f7566 | 2016-08-19 05:17:25 +0000 | [diff] [blame] | 111 | IntPtrTy = llvm::IntegerType::get(LLVMContext, |
| 112 | C.getTargetInfo().getMaxPointerWidth()); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 113 | Int8PtrTy = Int8Ty->getPointerTo(0); |
| 114 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 115 | AllocaInt8PtrTy = Int8Ty->getPointerTo( |
| 116 | M.getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 117 | ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 118 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 119 | RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 120 | BuiltinCC = getTargetCodeGenInfo().getABIInfo().getBuiltinCC(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 121 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 122 | if (LangOpts.ObjC1) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 123 | createObjCRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 124 | if (LangOpts.OpenCL) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 125 | createOpenCLRuntime(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 126 | if (LangOpts.OpenMP) |
| 127 | createOpenMPRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 128 | if (LangOpts.CUDA) |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 129 | createCUDARuntime(); |
Sanjiv Gupta | 15cb669 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 130 | |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 131 | // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 132 | if (LangOpts.Sanitize.has(SanitizerKind::Thread) || |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 133 | (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 134 | TBAA.reset(new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(), |
| 135 | getCXXABI().getMangleContext())); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 136 | |
Nick Lewycky | 207bce3 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 137 | // If debug info or coverage generation is enabled, create the CGDebugInfo |
| 138 | // object. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 139 | if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo || |
| 140 | CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 141 | DebugInfo.reset(new CGDebugInfo(*this)); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 142 | |
| 143 | Block.GlobalUniqueCount = 0; |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 144 | |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 145 | if (C.getLangOpts().ObjC1) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 146 | ObjCData.reset(new ObjCEntrypoints()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 147 | |
Rong Xu | 9c6f153 | 2016-03-02 20:59:36 +0000 | [diff] [blame] | 148 | if (CodeGenOpts.hasProfileClangUse()) { |
| 149 | auto ReaderOrErr = llvm::IndexedInstrProfReader::create( |
| 150 | CodeGenOpts.ProfileInstrumentUsePath); |
Vedant Kumar | fa2d595 | 2016-05-19 03:54:54 +0000 | [diff] [blame] | 151 | if (auto E = ReaderOrErr.takeError()) { |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 152 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
Diego Novillo | 5aecacb | 2015-06-25 22:56:00 +0000 | [diff] [blame] | 153 | "Could not read profile %0: %1"); |
Vedant Kumar | fa2d595 | 2016-05-19 03:54:54 +0000 | [diff] [blame] | 154 | llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) { |
| 155 | getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath |
| 156 | << EI.message(); |
| 157 | }); |
Nico Weber | 176efac | 2015-04-06 04:16:48 +0000 | [diff] [blame] | 158 | } else |
| 159 | PGOReader = std::move(ReaderOrErr.get()); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 160 | } |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 161 | |
| 162 | // If coverage mapping generation is enabled, create the |
| 163 | // CoverageMappingModuleGen object. |
| 164 | if (CodeGenOpts.CoverageMapping) |
| 165 | CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo)); |
Chris Lattner | a087ff9 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 168 | CodeGenModule::~CodeGenModule() {} |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 169 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 170 | void CodeGenModule::createObjCRuntime() { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 171 | // This is just isGNUFamily(), but we want to force implementors of |
| 172 | // new ABIs to decide how best to do this. |
| 173 | switch (LangOpts.ObjCRuntime.getKind()) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 174 | case ObjCRuntime::GNUstep: |
| 175 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 176 | case ObjCRuntime::ObjFW: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 177 | ObjCRuntime.reset(CreateGNUObjCRuntime(*this)); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 178 | return; |
| 179 | |
| 180 | case ObjCRuntime::FragileMacOSX: |
| 181 | case ObjCRuntime::MacOSX: |
| 182 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 183 | case ObjCRuntime::WatchOS: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 184 | ObjCRuntime.reset(CreateMacObjCRuntime(*this)); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | llvm_unreachable("bad runtime kind"); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 190 | void CodeGenModule::createOpenCLRuntime() { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 191 | OpenCLRuntime.reset(new CGOpenCLRuntime(*this)); |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 194 | void CodeGenModule::createOpenMPRuntime() { |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 195 | // Select a specialized code generation class based on the target, if any. |
| 196 | // If it does not exist use the default implementation. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 197 | switch (getTriple().getArch()) { |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 198 | case llvm::Triple::nvptx: |
| 199 | case llvm::Triple::nvptx64: |
| 200 | assert(getLangOpts().OpenMPIsDevice && |
| 201 | "OpenMP NVPTX is only prepared to deal with device code."); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 202 | OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this)); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 203 | break; |
| 204 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 205 | OpenMPRuntime.reset(new CGOpenMPRuntime(*this)); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 210 | void CodeGenModule::createCUDARuntime() { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 211 | CUDARuntime.reset(CreateNVCUDARuntime(*this)); |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 214 | void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) { |
| 215 | Replacements[Name] = C; |
| 216 | } |
| 217 | |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 218 | void CodeGenModule::applyReplacements() { |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 219 | for (auto &I : Replacements) { |
| 220 | StringRef MangledName = I.first(); |
| 221 | llvm::Constant *Replacement = I.second; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 222 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 223 | if (!Entry) |
| 224 | continue; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 225 | auto *OldF = cast<llvm::Function>(Entry); |
| 226 | auto *NewF = dyn_cast<llvm::Function>(Replacement); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 227 | if (!NewF) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 228 | if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) { |
Rafael Espindola | 0fcad88 | 2014-05-16 22:20:18 +0000 | [diff] [blame] | 229 | NewF = dyn_cast<llvm::Function>(Alias->getAliasee()); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 230 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 231 | auto *CE = cast<llvm::ConstantExpr>(Replacement); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 232 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 233 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
| 234 | NewF = dyn_cast<llvm::Function>(CE->getOperand(0)); |
| 235 | } |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Replace old with new, but keep the old order. |
| 239 | OldF->replaceAllUsesWith(Replacement); |
| 240 | if (NewF) { |
| 241 | NewF->removeFromParent(); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 242 | OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(), |
| 243 | NewF); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 244 | } |
| 245 | OldF->eraseFromParent(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 249 | void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) { |
| 250 | GlobalValReplacements.push_back(std::make_pair(GV, C)); |
| 251 | } |
| 252 | |
| 253 | void CodeGenModule::applyGlobalValReplacements() { |
| 254 | for (auto &I : GlobalValReplacements) { |
| 255 | llvm::GlobalValue *GV = I.first; |
| 256 | llvm::Constant *C = I.second; |
| 257 | |
| 258 | GV->replaceAllUsesWith(C); |
| 259 | GV->eraseFromParent(); |
| 260 | } |
| 261 | } |
| 262 | |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 263 | // This is only used in aliases that we created and we know they have a |
| 264 | // linear structure. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 265 | static const llvm::GlobalObject *getAliasedGlobal( |
| 266 | const llvm::GlobalIndirectSymbol &GIS) { |
| 267 | llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited; |
| 268 | const llvm::Constant *C = &GIS; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 269 | for (;;) { |
Rafael Espindola | e11f40a | 2014-06-04 19:03:20 +0000 | [diff] [blame] | 270 | C = C->stripPointerCasts(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 271 | if (auto *GO = dyn_cast<llvm::GlobalObject>(C)) |
| 272 | return GO; |
| 273 | // stripPointerCasts will not walk over weak aliases. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 274 | auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C); |
| 275 | if (!GIS2) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 276 | return nullptr; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 277 | if (!Visited.insert(GIS2).second) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 278 | return nullptr; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 279 | C = GIS2->getIndirectSymbol(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 283 | void CodeGenModule::checkAliases() { |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 284 | // Check if the constructed aliases are well formed. It is really unfortunate |
| 285 | // that we have to do this in CodeGen, but we only construct mangled names |
| 286 | // and aliases during codegen. |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 287 | bool Error = false; |
Rafael Espindola | c67b815 | 2014-05-05 19:33:09 +0000 | [diff] [blame] | 288 | DiagnosticsEngine &Diags = getDiags(); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 289 | for (const GlobalDecl &GD : Aliases) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 290 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 291 | SourceLocation Location; |
| 292 | bool IsIFunc = D->hasAttr<IFuncAttr>(); |
| 293 | if (const Attr *A = D->getDefiningAttr()) |
| 294 | Location = A->getLocation(); |
| 295 | else |
| 296 | llvm_unreachable("Not an alias or ifunc?"); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 297 | StringRef MangledName = getMangledName(GD); |
| 298 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 299 | auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 300 | const llvm::GlobalValue *GV = getAliasedGlobal(*Alias); |
| 301 | if (!GV) { |
| 302 | Error = true; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 303 | Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 304 | } else if (GV->isDeclaration()) { |
Rafael Espindola | 93c7a65 | 2014-03-13 15:47:50 +0000 | [diff] [blame] | 305 | Error = true; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 306 | Diags.Report(Location, diag::err_alias_to_undefined) |
| 307 | << IsIFunc << IsIFunc; |
| 308 | } else if (IsIFunc) { |
| 309 | // Check resolver function type. |
| 310 | llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>( |
| 311 | GV->getType()->getPointerElementType()); |
| 312 | assert(FTy); |
| 313 | if (!FTy->getReturnType()->isPointerTy()) |
| 314 | Diags.Report(Location, diag::err_ifunc_resolver_return); |
| 315 | if (FTy->getNumParams()) |
| 316 | Diags.Report(Location, diag::err_ifunc_resolver_params); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 317 | } |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 318 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 319 | llvm::Constant *Aliasee = Alias->getIndirectSymbol(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 320 | llvm::GlobalValue *AliaseeGV; |
| 321 | if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee)) |
| 322 | AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0)); |
| 323 | else |
| 324 | AliaseeGV = cast<llvm::GlobalValue>(Aliasee); |
| 325 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 326 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 327 | StringRef AliasSection = SA->getName(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 328 | if (AliasSection != AliaseeGV->getSection()) |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 329 | Diags.Report(SA->getLocation(), diag::warn_alias_with_section) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 330 | << AliasSection << IsIFunc << IsIFunc; |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 331 | } |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 332 | |
| 333 | // We have to handle alias to weak aliases in here. LLVM itself disallows |
| 334 | // this since the object semantics would not match the IL one. For |
| 335 | // compatibility with gcc we implement it by just pointing the alias |
| 336 | // to its aliasee's aliasee. We also warn, since the user is probably |
| 337 | // expecting the link to be weak. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 338 | if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) { |
Sanjoy Das | f60a0d7 | 2016-04-08 01:31:02 +0000 | [diff] [blame] | 339 | if (GA->isInterposable()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 340 | Diags.Report(Location, diag::warn_alias_to_weak_alias) |
| 341 | << GV->getName() << GA->getName() << IsIFunc; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 342 | Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 343 | GA->getIndirectSymbol(), Alias->getType()); |
| 344 | Alias->setIndirectSymbol(Aliasee); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 347 | } |
| 348 | if (!Error) |
| 349 | return; |
| 350 | |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 351 | for (const GlobalDecl &GD : Aliases) { |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 352 | StringRef MangledName = getMangledName(GD); |
| 353 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 354 | auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 355 | Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType())); |
| 356 | Alias->eraseFromParent(); |
| 357 | } |
| 358 | } |
| 359 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 360 | void CodeGenModule::clear() { |
| 361 | DeferredDeclsToEmit.clear(); |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 362 | if (OpenMPRuntime) |
| 363 | OpenMPRuntime->clear(); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 366 | void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags, |
| 367 | StringRef MainFile) { |
| 368 | if (!hasDiagnostics()) |
| 369 | return; |
| 370 | if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) { |
| 371 | if (MainFile.empty()) |
| 372 | MainFile = "<stdin>"; |
| 373 | Diags.Report(diag::warn_profile_data_unprofiled) << MainFile; |
Vedant Kumar | 96d6ca7 | 2017-04-27 17:30:58 +0000 | [diff] [blame] | 374 | } else { |
| 375 | if (Mismatched > 0) |
| 376 | Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched; |
| 377 | |
| 378 | if (Missing > 0) |
| 379 | Diags.Report(diag::warn_profile_data_missing) << Visited << Missing; |
| 380 | } |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 383 | void CodeGenModule::Release() { |
Chris Lattner | a5ae54a | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 384 | EmitDeferred(); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 385 | EmitVTablesOpportunistically(); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 386 | applyGlobalValReplacements(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 387 | applyReplacements(); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 388 | checkAliases(); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 389 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | fe06df4 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 390 | EmitCXXGlobalDtorFunc(); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 391 | EmitCXXThreadLocalInitFunc(); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 392 | if (ObjCRuntime) |
| 393 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 394 | AddGlobalCtor(ObjCInitFunction); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 395 | if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice && |
| 396 | CUDARuntime) { |
| 397 | if (llvm::Function *CudaCtorFunction = CUDARuntime->makeModuleCtorFunction()) |
| 398 | AddGlobalCtor(CudaCtorFunction); |
| 399 | if (llvm::Function *CudaDtorFunction = CUDARuntime->makeModuleDtorFunction()) |
| 400 | AddGlobalDtor(CudaDtorFunction); |
| 401 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 402 | if (OpenMPRuntime) |
| 403 | if (llvm::Function *OpenMPRegistrationFunction = |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 404 | OpenMPRuntime->emitRegistrationFunction()) { |
| 405 | auto ComdatKey = OpenMPRegistrationFunction->hasComdat() ? |
| 406 | OpenMPRegistrationFunction : nullptr; |
| 407 | AddGlobalCtor(OpenMPRegistrationFunction, 0, ComdatKey); |
| 408 | } |
Easwaran Raman | 695890c | 2015-12-17 19:14:27 +0000 | [diff] [blame] | 409 | if (PGOReader) { |
Easwaran Raman | 8160812 | 2016-03-24 21:32:25 +0000 | [diff] [blame] | 410 | getModule().setProfileSummary(PGOReader->getSummary().getMD(VMContext)); |
Easwaran Raman | 695890c | 2015-12-17 19:14:27 +0000 | [diff] [blame] | 411 | if (PGOStats.hasDiagnostics()) |
| 412 | PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName); |
| 413 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 414 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 415 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 416 | EmitGlobalAnnotations(); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 417 | EmitStaticExternCAliases(); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 418 | EmitDeferredUnusedCoverageMappings(); |
| 419 | if (CoverageMapping) |
| 420 | CoverageMapping->emit(); |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 421 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 422 | CodeGenFunction(*this).EmitCfiCheckFail(); |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 423 | CodeGenFunction(*this).EmitCfiCheckStub(); |
| 424 | } |
Alex Lorenz | a8fbef4 | 2017-03-23 11:14:27 +0000 | [diff] [blame] | 425 | emitAtAvailableLinkGuard(); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 426 | emitLLVMUsed(); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 427 | if (SanStats) |
| 428 | SanStats->finish(); |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 429 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 430 | if (CodeGenOpts.Autolink && |
| 431 | (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 432 | EmitModuleLinkOptions(); |
| 433 | } |
Nirav Dave | 8497ef4 | 2017-03-18 00:43:39 +0000 | [diff] [blame] | 434 | |
Nirav Dave | 741dea0 | 2017-03-30 13:41:44 +0000 | [diff] [blame] | 435 | // Record mregparm value now so it is visible through rest of codegen. |
| 436 | if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 437 | getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters", |
| 438 | CodeGenOpts.NumRegisterParameters); |
| 439 | |
Reid Kleckner | 124955a | 2015-08-05 18:51:13 +0000 | [diff] [blame] | 440 | if (CodeGenOpts.DwarfVersion) { |
Manman Ren | 9691f7f | 2013-06-19 01:46:49 +0000 | [diff] [blame] | 441 | // We actually want the latest version when there are conflicts. |
| 442 | // We can change from Warning to Latest if such mode is supported. |
| 443 | getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version", |
| 444 | CodeGenOpts.DwarfVersion); |
Reid Kleckner | 124955a | 2015-08-05 18:51:13 +0000 | [diff] [blame] | 445 | } |
| 446 | if (CodeGenOpts.EmitCodeView) { |
| 447 | // Indicate that we want CodeView in the metadata. |
| 448 | getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1); |
| 449 | } |
Piotr Padlewski | 9d0ecf2 | 2015-09-15 21:46:50 +0000 | [diff] [blame] | 450 | if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) { |
| 451 | // We don't support LTO with 2 with different StrictVTablePointers |
| 452 | // FIXME: we could support it by stripping all the information introduced |
| 453 | // by StrictVTablePointers. |
| 454 | |
| 455 | getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1); |
| 456 | |
| 457 | llvm::Metadata *Ops[2] = { |
| 458 | llvm::MDString::get(VMContext, "StrictVTablePointers"), |
| 459 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 460 | llvm::Type::getInt32Ty(VMContext), 1))}; |
| 461 | |
| 462 | getModule().addModuleFlag(llvm::Module::Require, |
| 463 | "StrictVTablePointersRequirement", |
| 464 | llvm::MDNode::get(VMContext, Ops)); |
| 465 | } |
Manman Ren | 4b7f23d | 2013-11-22 19:42:45 +0000 | [diff] [blame] | 466 | if (DebugInfo) |
Adrian Prantl | 2dbdd20 | 2014-05-19 23:40:06 +0000 | [diff] [blame] | 467 | // We support a single version in the linked module. The LLVM |
| 468 | // parser will drop debug info with a different version number |
| 469 | // (and warn about it, too). |
| 470 | getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version", |
Manman Ren | 61fd594 | 2013-12-02 20:10:37 +0000 | [diff] [blame] | 471 | llvm::DEBUG_METADATA_VERSION); |
Douglas Gregor | ea02f26 | 2013-01-14 18:28:43 +0000 | [diff] [blame] | 472 | |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 473 | // Width of wchar_t in bytes |
| 474 | uint64_t WCharWidth = |
| 475 | Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity(); |
Davide Italiano | 5e458ad | 2017-05-31 18:51:36 +0000 | [diff] [blame] | 476 | assert((LangOpts.ShortWChar || |
| 477 | llvm::TargetLibraryInfoImpl::getTargetWCharSize(Target.getTriple()) == |
| 478 | Target.getWCharWidth() / 8) && |
| 479 | "LLVM wchar_t size out of sync"); |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 480 | |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 481 | // We need to record the widths of enums and wchar_t, so that we can generate |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 482 | // the correct build attributes in the ARM backend. wchar_size is also used by |
| 483 | // TargetLibraryInfo. |
| 484 | getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth); |
| 485 | |
Justin Bogner | 9bb7f15 | 2014-06-23 20:03:28 +0000 | [diff] [blame] | 486 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 487 | if ( Arch == llvm::Triple::arm |
| 488 | || Arch == llvm::Triple::armeb |
| 489 | || Arch == llvm::Triple::thumb |
| 490 | || Arch == llvm::Triple::thumbeb) { |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 491 | // The minimum width of an enum in bytes |
| 492 | uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4; |
| 493 | getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth); |
| 494 | } |
| 495 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 496 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
| 497 | // Indicate that we want cross-DSO control flow integrity checks. |
| 498 | getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1); |
| 499 | } |
| 500 | |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 501 | if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) { |
Justin Lebar | d3a44f6 | 2016-04-05 18:26:20 +0000 | [diff] [blame] | 502 | // Indicate whether __nvvm_reflect should be configured to flush denormal |
| 503 | // floating point values to 0. (This corresponds to its "__CUDA_FTZ" |
| 504 | // property.) |
| 505 | getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz", |
| 506 | LangOpts.CUDADeviceFlushDenormalsToZero ? 1 : 0); |
| 507 | } |
| 508 | |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 509 | // Emit OpenCL specific module metadata: OpenCL/SPIR version. |
| 510 | if (LangOpts.OpenCL) { |
| 511 | EmitOpenCLMetadata(); |
| 512 | // Emit SPIR version. |
| 513 | if (getTriple().getArch() == llvm::Triple::spir || |
| 514 | getTriple().getArch() == llvm::Triple::spir64) { |
| 515 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 516 | // opencl.spir.version named metadata. |
| 517 | llvm::Metadata *SPIRVerElts[] = { |
| 518 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 519 | Int32Ty, LangOpts.OpenCLVersion / 100)), |
| 520 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 521 | Int32Ty, (LangOpts.OpenCLVersion / 100 > 1) ? 0 : 2))}; |
| 522 | llvm::NamedMDNode *SPIRVerMD = |
| 523 | TheModule.getOrInsertNamedMetadata("opencl.spir.version"); |
| 524 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 525 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
| 526 | } |
| 527 | } |
| 528 | |
Justin Hibbits | 90ca05e | 2014-11-18 06:17:20 +0000 | [diff] [blame] | 529 | if (uint32_t PLevel = Context.getLangOpts().PICLevel) { |
Sriraman Tallam | 70e70e6 | 2016-04-28 22:34:00 +0000 | [diff] [blame] | 530 | assert(PLevel < 3 && "Invalid PIC Level"); |
| 531 | getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel)); |
Rafael Espindola | c9d336e | 2016-06-23 15:07:32 +0000 | [diff] [blame] | 532 | if (Context.getLangOpts().PIE) |
| 533 | getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel)); |
Justin Hibbits | 90ca05e | 2014-11-18 06:17:20 +0000 | [diff] [blame] | 534 | } |
| 535 | |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 536 | SimplifyPersonality(); |
| 537 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 538 | if (getCodeGenOpts().EmitDeclMetadata) |
| 539 | EmitDeclMetadata(); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 540 | |
| 541 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 542 | EmitCoverageFile(); |
Devang Patel | ffa30ab | 2011-08-16 20:58:22 +0000 | [diff] [blame] | 543 | |
| 544 | if (DebugInfo) |
| 545 | DebugInfo->finalize(); |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 546 | |
| 547 | EmitVersionIdentMetadata(); |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 548 | |
| 549 | EmitTargetMetadata(); |
Daniel Dunbar | 23fd462 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 552 | void CodeGenModule::EmitOpenCLMetadata() { |
| 553 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 554 | // opencl.ocl.version named metadata node. |
| 555 | llvm::Metadata *OCLVerElts[] = { |
| 556 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 557 | Int32Ty, LangOpts.OpenCLVersion / 100)), |
| 558 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 559 | Int32Ty, (LangOpts.OpenCLVersion % 100) / 10))}; |
| 560 | llvm::NamedMDNode *OCLVerMD = |
| 561 | TheModule.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 562 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 563 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 564 | } |
| 565 | |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 566 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 567 | // Make sure that this type is translated. |
| 568 | Types.UpdateCompletedType(TD); |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 569 | } |
| 570 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 571 | void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { |
| 572 | // Make sure that this type is translated. |
| 573 | Types.RefreshTypeCacheForClass(RD); |
| 574 | } |
| 575 | |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 576 | llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) { |
| 577 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 578 | return nullptr; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 579 | return TBAA->getTBAAInfo(QTy); |
| 580 | } |
| 581 | |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 582 | llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() { |
| 583 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 584 | return nullptr; |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 585 | return TBAA->getTBAAInfoForVTablePtr(); |
| 586 | } |
| 587 | |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 588 | llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { |
| 589 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 590 | return nullptr; |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 591 | return TBAA->getTBAAStructInfo(QTy); |
| 592 | } |
| 593 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 594 | llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy, |
| 595 | llvm::MDNode *AccessN, |
| 596 | uint64_t O) { |
| 597 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 598 | return nullptr; |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 599 | return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O); |
| 600 | } |
| 601 | |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 602 | /// Decorate the instruction with a TBAA tag. For both scalar TBAA |
| 603 | /// and struct-path aware TBAA, the tag has the same format: |
| 604 | /// base type, access type and offset. |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 605 | /// When ConvertTypeToTag is true, we create a tag based on the scalar type. |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 606 | void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst, |
| 607 | llvm::MDNode *TBAAInfo, |
| 608 | bool ConvertTypeToTag) { |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 609 | if (ConvertTypeToTag && TBAA) |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 610 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, |
| 611 | TBAA->getTBAAScalarTagInfo(TBAAInfo)); |
| 612 | else |
| 613 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 616 | void CodeGenModule::DecorateInstructionWithInvariantGroup( |
| 617 | llvm::Instruction *I, const CXXRecordDecl *RD) { |
Piotr Padlewski | 34046bd | 2017-04-24 12:58:43 +0000 | [diff] [blame] | 618 | I->setMetadata(llvm::LLVMContext::MD_invariant_group, |
| 619 | llvm::MDNode::get(getLLVMContext(), {})); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 622 | void CodeGenModule::Error(SourceLocation loc, StringRef message) { |
| 623 | unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
| 624 | getDiags().Report(Context.getFullLoc(loc), diagID) << message; |
John McCall | 7ef5cb3 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 627 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 628 | /// specified stmt yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 629 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 630 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 631 | "cannot compile this %0 yet"); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 632 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 633 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 634 | << Msg << S->getSourceRange(); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 635 | } |
Chris Lattner | 41af818 | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 636 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 637 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 638 | /// specified decl yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 639 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 640 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 641 | "cannot compile this %0 yet"); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 642 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 643 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 644 | } |
| 645 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 646 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
| 647 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 648 | } |
| 649 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Anders Carlsson | c6a4789 | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 651 | const NamedDecl *D) const { |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 652 | // Internal definitions always have default visibility. |
Chris Lattner | 6a0f907 | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 653 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 654 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 655 | return; |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 656 | } |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 657 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 658 | // Set visibility for definitions. |
Rafael Espindola | a8fbdab | 2013-02-27 02:15:29 +0000 | [diff] [blame] | 659 | LinkageInfo LV = D->getLinkageAndVisibility(); |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 660 | if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage()) |
| 661 | GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 664 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) { |
| 665 | return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S) |
| 666 | .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel) |
| 667 | .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel) |
| 668 | .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel) |
| 669 | .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel); |
| 670 | } |
| 671 | |
| 672 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel( |
| 673 | CodeGenOptions::TLSModel M) { |
| 674 | switch (M) { |
| 675 | case CodeGenOptions::GeneralDynamicTLSModel: |
| 676 | return llvm::GlobalVariable::GeneralDynamicTLSModel; |
| 677 | case CodeGenOptions::LocalDynamicTLSModel: |
| 678 | return llvm::GlobalVariable::LocalDynamicTLSModel; |
| 679 | case CodeGenOptions::InitialExecTLSModel: |
| 680 | return llvm::GlobalVariable::InitialExecTLSModel; |
| 681 | case CodeGenOptions::LocalExecTLSModel: |
| 682 | return llvm::GlobalVariable::LocalExecTLSModel; |
| 683 | } |
| 684 | llvm_unreachable("Invalid TLS model!"); |
| 685 | } |
| 686 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 687 | void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const { |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 688 | assert(D.getTLSKind() && "setting TLS mode on non-TLS var!"); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 689 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 690 | llvm::GlobalValue::ThreadLocalMode TLM; |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 691 | TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel()); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 692 | |
| 693 | // Override the TLS model if it is explicitly specified. |
Aaron Ballman | c432799 | 2013-12-19 03:09:10 +0000 | [diff] [blame] | 694 | if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) { |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 695 | TLM = GetLLVMTLSModel(Attr->getModel()); |
| 696 | } |
| 697 | |
| 698 | GV->setThreadLocalMode(TLM); |
| 699 | } |
| 700 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 701 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
David Majnemer | d2385c0 | 2016-01-08 20:48:26 +0000 | [diff] [blame] | 702 | GlobalDecl CanonicalGD = GD.getCanonicalDecl(); |
| 703 | |
| 704 | // Some ABIs don't have constructor variants. Make sure that base and |
| 705 | // complete constructors get mangled the same. |
| 706 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) { |
| 707 | if (!getTarget().getCXXABI().hasConstructorVariants()) { |
| 708 | CXXCtorType OrigCtorType = GD.getCtorType(); |
| 709 | assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete); |
| 710 | if (OrigCtorType == Ctor_Base) |
| 711 | CanonicalGD = GlobalDecl(CD, Ctor_Complete); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | StringRef &FoundStr = MangledDeclNames[CanonicalGD]; |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 716 | if (!FoundStr.empty()) |
| 717 | return FoundStr; |
| 718 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 719 | const auto *ND = cast<NamedDecl>(GD.getDecl()); |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 720 | SmallString<256> Buffer; |
| 721 | StringRef Str; |
| 722 | if (getCXXABI().getMangleContext().shouldMangleDeclName(ND)) { |
| 723 | llvm::raw_svector_ostream Out(Buffer); |
| 724 | if (const auto *D = dyn_cast<CXXConstructorDecl>(ND)) |
| 725 | getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out); |
| 726 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND)) |
| 727 | getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out); |
| 728 | else |
| 729 | getCXXABI().getMangleContext().mangleName(ND, Out); |
| 730 | Str = Out.str(); |
| 731 | } else { |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 732 | IdentifierInfo *II = ND->getIdentifier(); |
| 733 | assert(II && "Attempt to mangle unnamed decl."); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 734 | const auto *FD = dyn_cast<FunctionDecl>(ND); |
| 735 | |
| 736 | if (FD && |
| 737 | FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) { |
| 738 | llvm::raw_svector_ostream Out(Buffer); |
| 739 | Out << "__regcall3__" << II->getName(); |
| 740 | Str = Out.str(); |
| 741 | } else { |
| 742 | Str = II->getName(); |
| 743 | } |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 744 | } |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 745 | |
Richard Smith | 6d0e97a | 2014-08-02 00:50:16 +0000 | [diff] [blame] | 746 | // Keep the first result in the case of a mangling collision. |
| 747 | auto Result = Manglings.insert(std::make_pair(Str, GD)); |
| 748 | return FoundStr = Result.first->first(); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 751 | StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD, |
| 752 | const BlockDecl *BD) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 753 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
| 754 | const Decl *D = GD.getDecl(); |
Alp Toker | 0e64e0d | 2014-06-03 02:13:57 +0000 | [diff] [blame] | 755 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 756 | SmallString<256> Buffer; |
| 757 | llvm::raw_svector_ostream Out(Buffer); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 758 | if (!D) |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 759 | MangleCtx.mangleGlobalBlock(BD, |
| 760 | dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 761 | else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 762 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 763 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 764 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 765 | else |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 766 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
Alp Toker | 0e64e0d | 2014-06-03 02:13:57 +0000 | [diff] [blame] | 767 | |
Richard Smith | 6d0e97a | 2014-08-02 00:50:16 +0000 | [diff] [blame] | 768 | auto Result = Manglings.insert(std::make_pair(Out.str(), BD)); |
| 769 | return Result.first->first(); |
Anders Carlsson | 635186a | 2010-06-09 02:36:32 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 772 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 773 | return getModule().getNamedValue(Name); |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 776 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 777 | /// main() runs. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 778 | void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority, |
| 779 | llvm::Constant *AssociatedData) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 780 | // FIXME: Type coercion of void()* types. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 781 | GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 784 | /// AddGlobalDtor - Add a function to the list that will be called |
| 785 | /// when the module is unloaded. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 786 | void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 787 | // FIXME: Type coercion of void()* types. |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 788 | GlobalDtors.push_back(Structor(Priority, Dtor, nullptr)); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Vassil Vassilev | 188ad3a | 2016-10-27 09:12:20 +0000 | [diff] [blame] | 791 | void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 792 | if (Fns.empty()) return; |
| 793 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 794 | // Ctor function type is void()*. |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 795 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 796 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 797 | |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 798 | // Get the type of a ctor entry, { i32, void ()*, i8* }. |
| 799 | llvm::StructType *CtorStructTy = llvm::StructType::get( |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 800 | Int32Ty, llvm::PointerType::getUnqual(CtorFTy), VoidPtrTy); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 801 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 802 | // Construct the constructor and destructor arrays. |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 803 | ConstantInitBuilder builder(*this); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 804 | auto ctors = builder.beginArray(CtorStructTy); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 805 | for (const auto &I : Fns) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 806 | auto ctor = ctors.beginStruct(CtorStructTy); |
| 807 | ctor.addInt(Int32Ty, I.Priority); |
| 808 | ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy)); |
| 809 | if (I.AssociatedData) |
| 810 | ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy)); |
| 811 | else |
| 812 | ctor.addNullPointer(VoidPtrTy); |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 813 | ctor.finishAndAddTo(ctors); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 814 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 815 | |
John McCall | 18081af | 2016-11-19 20:12:25 +0000 | [diff] [blame] | 816 | auto list = |
| 817 | ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(), |
| 818 | /*constant*/ false, |
| 819 | llvm::GlobalValue::AppendingLinkage); |
| 820 | |
| 821 | // The LTO linker doesn't seem to like it when we set an alignment |
| 822 | // on appending variables. Take it off as a workaround. |
| 823 | list->setAlignment(0); |
| 824 | |
Vassil Vassilev | 188ad3a | 2016-10-27 09:12:20 +0000 | [diff] [blame] | 825 | Fns.clear(); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 826 | } |
| 827 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 828 | llvm::GlobalValue::LinkageTypes |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 829 | CodeGenModule::getFunctionLinkage(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 830 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 831 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 832 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 833 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 834 | if (isa<CXXDestructorDecl>(D) && |
Reid Kleckner | a12cd28 | 2013-12-11 19:21:27 +0000 | [diff] [blame] | 835 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 836 | GD.getDtorType())) { |
| 837 | // Destructor variants in the Microsoft C++ ABI are always internal or |
| 838 | // linkonce_odr thunks emitted on an as-needed basis. |
| 839 | return Linkage == GVA_Internal ? llvm::GlobalValue::InternalLinkage |
| 840 | : llvm::GlobalValue::LinkOnceODRLinkage; |
| 841 | } |
Reid Kleckner | a12cd28 | 2013-12-11 19:21:27 +0000 | [diff] [blame] | 842 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 843 | if (isa<CXXConstructorDecl>(D) && |
| 844 | cast<CXXConstructorDecl>(D)->isInheritingConstructor() && |
| 845 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 846 | // Our approach to inheriting constructors is fundamentally different from |
| 847 | // that used by the MS ABI, so keep our inheriting constructor thunks |
| 848 | // internal rather than trying to pick an unambiguous mangling for them. |
| 849 | return llvm::GlobalValue::InternalLinkage; |
| 850 | } |
| 851 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 852 | return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 853 | } |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 854 | |
Hans Wennborg | bb4f962 | 2015-05-28 17:44:56 +0000 | [diff] [blame] | 855 | void CodeGenModule::setFunctionDLLStorageClass(GlobalDecl GD, llvm::Function *F) { |
| 856 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
| 857 | |
| 858 | if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(FD)) { |
| 859 | if (getCXXABI().useThunkForDtorVariant(Dtor, GD.getDtorType())) { |
| 860 | // Don't dllexport/import destructor thunks. |
| 861 | F->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 862 | return; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (FD->hasAttr<DLLImportAttr>()) |
| 867 | F->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 868 | else if (FD->hasAttr<DLLExportAttr>()) |
| 869 | F->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
| 870 | else |
| 871 | F->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); |
| 872 | } |
| 873 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 874 | llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 875 | llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD); |
| 876 | if (!MDS) return nullptr; |
| 877 | |
Benjamin Kramer | 630cf8c | 2016-11-23 11:20:27 +0000 | [diff] [blame] | 878 | return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString())); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 881 | void CodeGenModule::setFunctionDefinitionAttributes(const FunctionDecl *D, |
| 882 | llvm::Function *F) { |
| 883 | setNonAliasAttributes(D, F); |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 886 | void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | const CGFunctionInfo &Info, |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 888 | llvm::Function *F) { |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 889 | unsigned CallingConv; |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 890 | llvm::AttributeList PAL; |
| 891 | ConstructAttributeList(F->getName(), Info, D, PAL, CallingConv, false); |
| 892 | F->setAttributes(PAL); |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 893 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 894 | } |
| 895 | |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 896 | /// Determines whether the language options require us to model |
| 897 | /// unwind exceptions. We treat -fexceptions as mandating this |
| 898 | /// except under the fragile ObjC ABI with only ObjC exceptions |
| 899 | /// enabled. This means, for example, that C with -fexceptions |
| 900 | /// enables this. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 901 | static bool hasUnwindExceptions(const LangOptions &LangOpts) { |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 902 | // If exceptions are completely disabled, obviously this is false. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 903 | if (!LangOpts.Exceptions) return false; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 904 | |
| 905 | // If C++ exceptions are enabled, this is true. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 906 | if (LangOpts.CXXExceptions) return true; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 907 | |
| 908 | // If ObjC exceptions are enabled, this depends on the ABI. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 909 | if (LangOpts.ObjCExceptions) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 910 | return LangOpts.ObjCRuntime.hasUnwindExceptions(); |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | return true; |
| 914 | } |
| 915 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 916 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 917 | llvm::Function *F) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 918 | llvm::AttrBuilder B; |
| 919 | |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 920 | if (CodeGenOpts.UnwindTables) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 921 | B.addAttribute(llvm::Attribute::UWTable); |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 922 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 923 | if (!hasUnwindExceptions(LangOpts)) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 924 | B.addAttribute(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 925 | |
Akira Hatanaka | 200500d | 2015-10-08 19:30:57 +0000 | [diff] [blame] | 926 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
| 927 | B.addAttribute(llvm::Attribute::StackProtect); |
| 928 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
| 929 | B.addAttribute(llvm::Attribute::StackProtectStrong); |
| 930 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
| 931 | B.addAttribute(llvm::Attribute::StackProtectReq); |
| 932 | |
| 933 | if (!D) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 934 | // If we don't have a declaration to control inlining, the function isn't |
| 935 | // explicitly marked as alwaysinline for semantic reasons, and inlining is |
| 936 | // disabled, mark the function as noinline. |
| 937 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
| 938 | CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) |
| 939 | B.addAttribute(llvm::Attribute::NoInline); |
| 940 | |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 941 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Akira Hatanaka | 200500d | 2015-10-08 19:30:57 +0000 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 945 | // Track whether we need to add the optnone LLVM attribute, |
| 946 | // starting with the default for this optimization level. |
| 947 | bool ShouldAddOptNone = |
| 948 | !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0; |
| 949 | // We can't add optnone in the following cases, it won't pass the verifier. |
| 950 | ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>(); |
| 951 | ShouldAddOptNone &= !F->hasFnAttribute(llvm::Attribute::AlwaysInline); |
| 952 | ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>(); |
| 953 | |
| 954 | if (ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 955 | B.addAttribute(llvm::Attribute::OptimizeNone); |
| 956 | |
| 957 | // OptimizeNone implies noinline; we should not be inlining such functions. |
| 958 | B.addAttribute(llvm::Attribute::NoInline); |
| 959 | assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
| 960 | "OptimizeNone and AlwaysInline on same function!"); |
| 961 | |
| 962 | // We still need to handle naked functions even though optnone subsumes |
| 963 | // much of their semantics. |
| 964 | if (D->hasAttr<NakedAttr>()) |
| 965 | B.addAttribute(llvm::Attribute::Naked); |
| 966 | |
| 967 | // OptimizeNone wins over OptimizeForSize and MinSize. |
| 968 | F->removeFnAttr(llvm::Attribute::OptimizeForSize); |
| 969 | F->removeFnAttr(llvm::Attribute::MinSize); |
| 970 | } else if (D->hasAttr<NakedAttr>()) { |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 971 | // Naked implies noinline: we should not be inlining such functions. |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 972 | B.addAttribute(llvm::Attribute::Naked); |
| 973 | B.addAttribute(llvm::Attribute::NoInline); |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 974 | } else if (D->hasAttr<NoDuplicateAttr>()) { |
| 975 | B.addAttribute(llvm::Attribute::NoDuplicate); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 976 | } else if (D->hasAttr<NoInlineAttr>()) { |
| 977 | B.addAttribute(llvm::Attribute::NoInline); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 978 | } else if (D->hasAttr<AlwaysInlineAttr>() && |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 979 | !F->hasFnAttribute(llvm::Attribute::NoInline)) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 980 | // (noinline wins over always_inline, and we can't specify both in IR) |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 981 | B.addAttribute(llvm::Attribute::AlwaysInline); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 982 | } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) { |
| 983 | // If we're not inlining, then force everything that isn't always_inline to |
| 984 | // carry an explicit noinline attribute. |
| 985 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline)) |
| 986 | B.addAttribute(llvm::Attribute::NoInline); |
| 987 | } else { |
| 988 | // Otherwise, propagate the inline hint attribute and potentially use its |
| 989 | // absence to mark things as noinline. |
| 990 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 991 | if (any_of(FD->redecls(), [&](const FunctionDecl *Redecl) { |
| 992 | return Redecl->isInlineSpecified(); |
| 993 | })) { |
| 994 | B.addAttribute(llvm::Attribute::InlineHint); |
| 995 | } else if (CodeGenOpts.getInlining() == |
| 996 | CodeGenOptions::OnlyHintInlining && |
| 997 | !FD->isInlined() && |
| 998 | !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { |
| 999 | B.addAttribute(llvm::Attribute::NoInline); |
| 1000 | } |
| 1001 | } |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 1002 | } |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1003 | |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1004 | // Add other optimization related attributes if we are optimizing this |
| 1005 | // function. |
| 1006 | if (!D->hasAttr<OptimizeNoneAttr>()) { |
| 1007 | if (D->hasAttr<ColdAttr>()) { |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1008 | if (!ShouldAddOptNone) |
| 1009 | B.addAttribute(llvm::Attribute::OptimizeForSize); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1010 | B.addAttribute(llvm::Attribute::Cold); |
| 1011 | } |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1012 | |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1013 | if (D->hasAttr<MinSizeAttr>()) |
| 1014 | B.addAttribute(llvm::Attribute::MinSize); |
| 1015 | } |
Richard Smith | b555a76 | 2012-09-28 22:46:07 +0000 | [diff] [blame] | 1016 | |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1017 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1018 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1019 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
| 1020 | if (alignment) |
| 1021 | F->setAlignment(alignment); |
| 1022 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 1023 | // Some C++ ABIs require 2-byte alignment for member functions, in order to |
| 1024 | // reserve a bit for differentiating between virtual and non-virtual member |
| 1025 | // functions. If the current target's C++ ABI requires this and this is a |
| 1026 | // member function, set its alignment accordingly. |
| 1027 | if (getTarget().getCXXABI().areMemberFunctionsAligned()) { |
| 1028 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 1029 | F->setAlignment(2); |
| 1030 | } |
Evgeniy Stepanov | f754309 | 2016-10-31 22:28:10 +0000 | [diff] [blame] | 1031 | |
| 1032 | // In the cross-dso CFI mode, we want !type attributes on definitions only. |
| 1033 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 1034 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1035 | CreateFunctionTypeMetadata(FD, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | void CodeGenModule::SetCommonAttributes(const Decl *D, |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1039 | llvm::GlobalValue *GV) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1040 | if (const auto *ND = dyn_cast_or_null<NamedDecl>(D)) |
Anders Carlsson | 072ef74 | 2011-01-29 19:41:00 +0000 | [diff] [blame] | 1041 | setGlobalVisibility(GV, ND); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1042 | else |
| 1043 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1044 | |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1045 | if (D && D->hasAttr<UsedAttr>()) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1046 | addUsedGlobal(GV); |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Dario Domizioli | c4fb8ca7 | 2014-09-19 22:06:24 +0000 | [diff] [blame] | 1049 | void CodeGenModule::setAliasAttributes(const Decl *D, |
| 1050 | llvm::GlobalValue *GV) { |
| 1051 | SetCommonAttributes(D, GV); |
| 1052 | |
| 1053 | // Process the dllexport attribute based on whether the original definition |
| 1054 | // (not necessarily the aliasee) was exported. |
| 1055 | if (D->hasAttr<DLLExportAttr>()) |
| 1056 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1057 | } |
| 1058 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 1059 | void CodeGenModule::setNonAliasAttributes(const Decl *D, |
Rafael Espindola | ee076a2 | 2014-05-13 18:45:53 +0000 | [diff] [blame] | 1060 | llvm::GlobalObject *GO) { |
| 1061 | SetCommonAttributes(D, GO); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1062 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1063 | if (D) { |
| 1064 | if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) { |
| 1065 | if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>()) |
| 1066 | GV->addAttribute("bss-section", SA->getName()); |
| 1067 | if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>()) |
| 1068 | GV->addAttribute("data-section", SA->getName()); |
| 1069 | if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>()) |
| 1070 | GV->addAttribute("rodata-section", SA->getName()); |
| 1071 | } |
| 1072 | |
| 1073 | if (auto *F = dyn_cast<llvm::Function>(GO)) { |
| 1074 | if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>()) |
| 1075 | if (!D->getAttr<SectionAttr>()) |
| 1076 | F->addFnAttr("implicit-section-name", SA->getName()); |
| 1077 | } |
| 1078 | |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1079 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 1080 | GO->setSection(SA->getName()); |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1081 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1082 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1083 | getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 1086 | void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, |
| 1087 | llvm::Function *F, |
| 1088 | const CGFunctionInfo &FI) { |
| 1089 | SetLLVMFunctionAttributes(D, FI, F); |
| 1090 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1091 | |
| 1092 | F->setLinkage(llvm::Function::InternalLinkage); |
| 1093 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 1094 | setNonAliasAttributes(D, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1097 | static void setLinkageAndVisibilityForGV(llvm::GlobalValue *GV, |
| 1098 | const NamedDecl *ND) { |
| 1099 | // Set linkage and visibility in case we never see a definition. |
| 1100 | LinkageInfo LV = ND->getLinkageAndVisibility(); |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1101 | if (!isExternallyVisible(LV.getLinkage())) { |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1102 | // Don't set internal linkage on declarations. |
| 1103 | } else { |
| 1104 | if (ND->hasAttr<DLLImportAttr>()) { |
| 1105 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 1106 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1107 | } else if (ND->hasAttr<DLLExportAttr>()) { |
| 1108 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1109 | } else if (ND->hasAttr<WeakAttr>() || ND->isWeakImported()) { |
| 1110 | // "extern_weak" is overloaded in LLVM; we probably should have |
| 1111 | // separate linkage types for this. |
| 1112 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 1113 | } |
| 1114 | |
| 1115 | // Set visibility on a declaration only if it's explicit. |
| 1116 | if (LV.isVisibilityExplicit()) |
| 1117 | GV->setVisibility(CodeGenModule::GetLLVMVisibility(LV.getVisibility())); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1121 | void CodeGenModule::CreateFunctionTypeMetadata(const FunctionDecl *FD, |
| 1122 | llvm::Function *F) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1123 | // Only if we are checking indirect calls. |
| 1124 | if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall)) |
| 1125 | return; |
| 1126 | |
| 1127 | // Non-static class methods are handled via vtable pointer checks elsewhere. |
| 1128 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 1129 | return; |
| 1130 | |
| 1131 | // Additionally, if building with cross-DSO support... |
| 1132 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1133 | // Skip available_externally functions. They won't be codegen'ed in the |
| 1134 | // current module anyway. |
| 1135 | if (getContext().GetGVALinkageForFunction(FD) == GVA_AvailableExternally) |
| 1136 | return; |
| 1137 | } |
| 1138 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1139 | llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1140 | F->addTypeMetadata(0, MD); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Emit a hash-based bit set entry for cross-DSO calls. |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1143 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 1144 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
| 1145 | F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 1148 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, |
| 1149 | bool IsIncompleteFunction, |
| 1150 | bool IsThunk) { |
Pete Cooper | 2f1637a | 2015-05-20 17:17:45 +0000 | [diff] [blame] | 1151 | if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) { |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 1152 | // If this is an intrinsic function, set the function's attributes |
| 1153 | // to the intrinsic's attributes. |
Pete Cooper | 2f1637a | 2015-05-20 17:17:45 +0000 | [diff] [blame] | 1154 | F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID)); |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 1155 | return; |
| 1156 | } |
| 1157 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1158 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 1159 | |
Eli Friedman | 895771a | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 1160 | if (!IsIncompleteFunction) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1161 | SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Bob Wilson | fc6297f | 2014-04-01 01:38:16 +0000 | [diff] [blame] | 1163 | // Add the Returned attribute for "this", except for iOS 5 and earlier |
| 1164 | // where substantial code, including the libstdc++ dylib, was compiled with |
| 1165 | // GCC and does not actually return "this". |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 1166 | if (!IsThunk && getCXXABI().HasThisReturn(GD) && |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 1167 | !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1168 | assert(!F->arg_empty() && |
| 1169 | F->arg_begin()->getType() |
| 1170 | ->canLosslesslyBitCastTo(F->getReturnType()) && |
| 1171 | "unexpected this return"); |
| 1172 | F->addAttribute(1, llvm::Attribute::Returned); |
| 1173 | } |
| 1174 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1175 | // Only a few attributes are set on declarations; these may later be |
| 1176 | // overridden by a definition. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1178 | setLinkageAndVisibilityForGV(F, FD); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1179 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1180 | if (FD->getAttr<PragmaClangTextSectionAttr>()) { |
| 1181 | F->addFnAttr("implicit-section-name"); |
| 1182 | } |
| 1183 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1184 | if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1185 | F->setSection(SA->getName()); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1186 | |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1187 | if (FD->isReplaceableGlobalAllocationFunction()) { |
| 1188 | // A replaceable global allocation function does not act like a builtin by |
| 1189 | // default, only if it is invoked by a new-expression or delete-expression. |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 1190 | F->addAttribute(llvm::AttributeList::FunctionIndex, |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1191 | llvm::Attribute::NoBuiltin); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 1192 | |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1193 | // A sane operator new returns a non-aliasing pointer. |
| 1194 | // FIXME: Also add NonNull attribute to the return value |
| 1195 | // for the non-nothrow forms? |
| 1196 | auto Kind = FD->getDeclName().getCXXOverloadedOperator(); |
| 1197 | if (getCodeGenOpts().AssumeSaneOperatorNew && |
| 1198 | (Kind == OO_New || Kind == OO_Array_New)) |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 1199 | F->addAttribute(llvm::AttributeList::ReturnIndex, |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1200 | llvm::Attribute::NoAlias); |
| 1201 | } |
| 1202 | |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1203 | if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD)) |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1204 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1205 | else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1206 | if (MD->isVirtual()) |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1207 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1208 | |
Evgeniy Stepanov | f754309 | 2016-10-31 22:28:10 +0000 | [diff] [blame] | 1209 | // Don't emit entries for function declarations in the cross-DSO mode. This |
| 1210 | // is handled with better precision by the receiving DSO. |
| 1211 | if (!CodeGenOpts.SanitizeCfiCrossDso) |
| 1212 | CreateFunctionTypeMetadata(FD, F); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1215 | void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1217 | "Only globals with definition can force usage."); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1218 | LLVMUsed.emplace_back(GV); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1221 | void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { |
| 1222 | assert(!GV->isDeclaration() && |
| 1223 | "Only globals with definition can force usage."); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1224 | LLVMCompilerUsed.emplace_back(GV); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | static void emitUsed(CodeGenModule &CGM, StringRef Name, |
Sanjoy Das | e369bd9 | 2017-05-01 17:08:00 +0000 | [diff] [blame] | 1228 | std::vector<llvm::WeakTrackingVH> &List) { |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1229 | // Don't create llvm.used if there is no need. |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1230 | if (List.empty()) |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1231 | return; |
| 1232 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1233 | // Convert List to what ConstantArray needs. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 1234 | SmallVector<llvm::Constant*, 8> UsedArray; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1235 | UsedArray.resize(List.size()); |
| 1236 | for (unsigned i = 0, e = List.size(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | UsedArray[i] = |
Justin Holewinski | f37f3d3 | 2015-02-02 21:05:49 +0000 | [diff] [blame] | 1238 | llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
| 1239 | cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 1240 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
Fariborz Jahanian | 248c719 | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 1242 | if (UsedArray.empty()) |
| 1243 | return; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1244 | llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1246 | auto *GV = new llvm::GlobalVariable( |
| 1247 | CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, |
| 1248 | llvm::ConstantArray::get(ATy, UsedArray), Name); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1249 | |
| 1250 | GV->setSection("llvm.metadata"); |
| 1251 | } |
| 1252 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1253 | void CodeGenModule::emitLLVMUsed() { |
| 1254 | emitUsed(*this, "llvm.used", LLVMUsed); |
| 1255 | emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); |
| 1256 | } |
| 1257 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1258 | void CodeGenModule::AppendLinkerOptions(StringRef Opts) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1259 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1260 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1261 | } |
| 1262 | |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1263 | void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { |
| 1264 | llvm::SmallString<32> Opt; |
| 1265 | getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1266 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1267 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1268 | } |
| 1269 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1270 | void CodeGenModule::AddDependentLib(StringRef Lib) { |
| 1271 | llvm::SmallString<24> Opt; |
| 1272 | getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1273 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1274 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1275 | } |
| 1276 | |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1277 | /// \brief Add link options implied by the given module, including modules |
| 1278 | /// it depends on, using a postorder walk. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1279 | static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1280 | SmallVectorImpl<llvm::MDNode *> &Metadata, |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1281 | llvm::SmallPtrSet<Module *, 16> &Visited) { |
| 1282 | // Import this module's parent. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1283 | if (Mod->Parent && Visited.insert(Mod->Parent).second) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1284 | addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | // Import this module's dependencies. |
| 1288 | for (unsigned I = Mod->Imports.size(); I > 0; --I) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1289 | if (Visited.insert(Mod->Imports[I - 1]).second) |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1290 | addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // Add linker options to link against the libraries/frameworks |
| 1294 | // described by this module. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1295 | llvm::LLVMContext &Context = CGM.getLLVMContext(); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1296 | for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1297 | // Link against a framework. Frameworks are currently Darwin only, so we |
| 1298 | // don't to ask TargetCodeGenInfo for the spelling of the linker option. |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1299 | if (Mod->LinkLibraries[I-1].IsFramework) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1300 | llvm::Metadata *Args[2] = { |
| 1301 | llvm::MDString::get(Context, "-framework"), |
| 1302 | llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)}; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1303 | |
| 1304 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | // Link against a library. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1309 | llvm::SmallString<24> Opt; |
| 1310 | CGM.getTargetCodeGenInfo().getDependentLibraryOption( |
| 1311 | Mod->LinkLibraries[I-1].Library, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1312 | auto *OptString = llvm::MDString::get(Context, Opt); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1313 | Metadata.push_back(llvm::MDNode::get(Context, OptString)); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | void CodeGenModule::EmitModuleLinkOptions() { |
| 1318 | // Collect the set of all of the modules we want to visit to emit link |
| 1319 | // options, which is essentially the imported modules and all of their |
| 1320 | // non-explicit child modules. |
| 1321 | llvm::SetVector<clang::Module *> LinkModules; |
| 1322 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 1323 | SmallVector<clang::Module *, 16> Stack; |
| 1324 | |
| 1325 | // Seed the stack with imported modules. |
Manman Ren | 9803ee8 | 2017-01-11 18:47:38 +0000 | [diff] [blame] | 1326 | for (Module *M : ImportedModules) { |
| 1327 | // Do not add any link flags when an implementation TU of a module imports |
| 1328 | // a header of that same module. |
| 1329 | if (M->getTopLevelModuleName() == getLangOpts().CurrentModule && |
| 1330 | !getLangOpts().isCompilingModule()) |
| 1331 | continue; |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 1332 | if (Visited.insert(M).second) |
| 1333 | Stack.push_back(M); |
Manman Ren | 9803ee8 | 2017-01-11 18:47:38 +0000 | [diff] [blame] | 1334 | } |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1335 | |
| 1336 | // Find all of the modules to import, making a little effort to prune |
| 1337 | // non-leaf modules. |
| 1338 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1339 | clang::Module *Mod = Stack.pop_back_val(); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1340 | |
| 1341 | bool AnyChildren = false; |
| 1342 | |
| 1343 | // Visit the submodules of this module. |
| 1344 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 1345 | SubEnd = Mod->submodule_end(); |
| 1346 | Sub != SubEnd; ++Sub) { |
| 1347 | // Skip explicit children; they need to be explicitly imported to be |
| 1348 | // linked against. |
| 1349 | if ((*Sub)->IsExplicit) |
| 1350 | continue; |
| 1351 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1352 | if (Visited.insert(*Sub).second) { |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1353 | Stack.push_back(*Sub); |
| 1354 | AnyChildren = true; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | // We didn't find any children, so add this module to the list of |
| 1359 | // modules to link against. |
| 1360 | if (!AnyChildren) { |
| 1361 | LinkModules.insert(Mod); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | // Add link options for all of the imported modules in reverse topological |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1366 | // order. We don't do anything to try to order import link flags with respect |
| 1367 | // to linker options inserted by things like #pragma comment(). |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1368 | SmallVector<llvm::MDNode *, 16> MetadataArgs; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1369 | Visited.clear(); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 1370 | for (Module *M : LinkModules) |
| 1371 | if (Visited.insert(M).second) |
| 1372 | addLinkOptionsPostorder(*this, M, MetadataArgs, Visited); |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 1373 | std::reverse(MetadataArgs.begin(), MetadataArgs.end()); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1374 | LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end()); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1375 | |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 1376 | // Add the linker options metadata flag. |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1377 | auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options"); |
| 1378 | for (auto *MD : LinkerOptionsMetadata) |
| 1379 | NMD->addOperand(MD); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1382 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1383 | // Emit code for any potentially referenced deferred decls. Since a |
| 1384 | // previously unused static decl may become used during the generation of code |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1385 | // for a static function, iterate until no changes are made. |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 1386 | |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1387 | if (!DeferredVTables.empty()) { |
| 1388 | EmitDeferredVTables(); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1389 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 1390 | // Emitting a vtable doesn't directly cause more vtables to |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1391 | // become deferred, although it can cause functions to be |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 1392 | // emitted that then need those vtables. |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1393 | assert(DeferredVTables.empty()); |
| 1394 | } |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 1395 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 1396 | // Stop if we're out of both deferred vtables and deferred declarations. |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1397 | if (DeferredDeclsToEmit.empty()) |
| 1398 | return; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1399 | |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1400 | // Grab the list of decls to emit. If EmitGlobalDefinition schedules more |
| 1401 | // work, it will not interfere with this. |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1402 | std::vector<GlobalDecl> CurDeclsToEmit; |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1403 | CurDeclsToEmit.swap(DeferredDeclsToEmit); |
| 1404 | |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1405 | for (GlobalDecl &D : CurDeclsToEmit) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 1406 | // We should call GetAddrOfGlobal with IsForDefinition set to true in order |
| 1407 | // to get GlobalValue with exactly the type we need, not something that |
| 1408 | // might had been created for another decl with the same mangled name but |
| 1409 | // different type. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 1410 | llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>( |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 1411 | GetAddrOfGlobal(D, ForDefinition)); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 1412 | |
| 1413 | // In case of different address spaces, we may still get a cast, even with |
| 1414 | // IsForDefinition equal to true. Query mangled names table to get |
| 1415 | // GlobalValue. |
| 1416 | if (!GV) |
| 1417 | GV = GetGlobalValue(getMangledName(D)); |
| 1418 | |
| 1419 | // Make sure GetGlobalValue returned non-null. |
| 1420 | assert(GV); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1421 | |
John McCall | c2af939 | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 1422 | // Check to see if we've already emitted this. This is necessary |
| 1423 | // for a couple of reasons: first, decls can end up in the |
| 1424 | // deferred-decls queue multiple times, and second, decls can end |
| 1425 | // up with definitions in unusual ways (e.g. by an extern inline |
| 1426 | // function acquiring a strong function redefinition). Just |
| 1427 | // ignore these cases. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 1428 | if (!GV->isDeclaration()) |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1429 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1431 | // Otherwise, emit the definition and move on to the next one. |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1432 | EmitGlobalDefinition(D, GV); |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1433 | |
| 1434 | // If we found out that we need to emit more decls, do that recursively. |
| 1435 | // This has the advantage that the decls are emitted in a DFS and related |
| 1436 | // ones are close together, which is convenient for testing. |
| 1437 | if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) { |
| 1438 | EmitDeferred(); |
| 1439 | assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty()); |
| 1440 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1441 | } |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1442 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1443 | |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 1444 | void CodeGenModule::EmitVTablesOpportunistically() { |
| 1445 | // Try to emit external vtables as available_externally if they have emitted |
| 1446 | // all inlined virtual functions. It runs after EmitDeferred() and therefore |
| 1447 | // is not allowed to create new references to things that need to be emitted |
| 1448 | // lazily. Note that it also uses fact that we eagerly emitting RTTI. |
| 1449 | |
Piotr Padlewski | 0554774 | 2017-06-01 09:24:36 +0000 | [diff] [blame] | 1450 | assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) |
| 1451 | && "Only emit opportunistic vtables with optimizations"); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 1452 | |
| 1453 | for (const CXXRecordDecl *RD : OpportunisticVTables) { |
| 1454 | assert(getVTables().isVTableExternal(RD) && |
| 1455 | "This queue should only contain external vtables"); |
| 1456 | if (getCXXABI().canSpeculativelyEmitVTable(RD)) |
| 1457 | VTables.GenerateClassData(RD); |
| 1458 | } |
| 1459 | OpportunisticVTables.clear(); |
| 1460 | } |
| 1461 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1462 | void CodeGenModule::EmitGlobalAnnotations() { |
| 1463 | if (Annotations.empty()) |
| 1464 | return; |
| 1465 | |
| 1466 | // Create a new global variable for the ConstantStruct in the Module. |
| 1467 | llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( |
| 1468 | Annotations[0]->getType(), Annotations.size()), Annotations); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1469 | auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false, |
| 1470 | llvm::GlobalValue::AppendingLinkage, |
| 1471 | Array, "llvm.global.annotations"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1472 | gv->setSection(AnnotationSection); |
| 1473 | } |
| 1474 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1475 | llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) { |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 1476 | llvm::Constant *&AStr = AnnotationStrings[Str]; |
| 1477 | if (AStr) |
| 1478 | return AStr; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Not found yet, create a new global. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 1481 | llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1482 | auto *gv = |
| 1483 | new llvm::GlobalVariable(getModule(), s->getType(), true, |
| 1484 | llvm::GlobalValue::PrivateLinkage, s, ".str"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1485 | gv->setSection(AnnotationSection); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1486 | gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 1487 | AStr = gv; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1488 | return gv; |
| 1489 | } |
| 1490 | |
| 1491 | llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { |
| 1492 | SourceManager &SM = getContext().getSourceManager(); |
| 1493 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 1494 | if (PLoc.isValid()) |
| 1495 | return EmitAnnotationString(PLoc.getFilename()); |
| 1496 | return EmitAnnotationString(SM.getBufferName(Loc)); |
| 1497 | } |
| 1498 | |
| 1499 | llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { |
| 1500 | SourceManager &SM = getContext().getSourceManager(); |
| 1501 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 1502 | unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : |
| 1503 | SM.getExpansionLineNumber(L); |
| 1504 | return llvm::ConstantInt::get(Int32Ty, LineNo); |
| 1505 | } |
| 1506 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1508 | const AnnotateAttr *AA, |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1509 | SourceLocation L) { |
| 1510 | // Get the globals for file name, annotation, and the line number. |
| 1511 | llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), |
| 1512 | *UnitGV = EmitAnnotationUnit(L), |
| 1513 | *LineNoCst = EmitAnnotationLineNo(L); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1514 | |
Daniel Dunbar | 346892a | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 1515 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1516 | llvm::Constant *Fields[4] = { |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1517 | llvm::ConstantExpr::getBitCast(GV, Int8PtrTy), |
| 1518 | llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), |
| 1519 | llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), |
| 1520 | LineNoCst |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1521 | }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1522 | return llvm::ConstantStruct::getAnon(Fields); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1525 | void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, |
| 1526 | llvm::GlobalValue *GV) { |
| 1527 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 1528 | // Get the struct elements for these annotations. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1529 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) |
| 1530 | Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1533 | bool CodeGenModule::isInSanitizerBlacklist(llvm::Function *Fn, |
| 1534 | SourceLocation Loc) const { |
| 1535 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
| 1536 | // Blacklist by function name. |
| 1537 | if (SanitizerBL.isBlacklistedFunction(Fn->getName())) |
| 1538 | return true; |
| 1539 | // Blacklist by location. |
Yaron Keren | ed1fe5d | 2015-10-03 05:15:57 +0000 | [diff] [blame] | 1540 | if (Loc.isValid()) |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1541 | return SanitizerBL.isBlacklistedLocation(Loc); |
| 1542 | // If location is unknown, this may be a compiler-generated function. Assume |
| 1543 | // it's located in the main file. |
| 1544 | auto &SM = Context.getSourceManager(); |
| 1545 | if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
| 1546 | return SanitizerBL.isBlacklistedFile(MainFile->getName()); |
| 1547 | } |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1551 | bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV, |
| 1552 | SourceLocation Loc, QualType Ty, |
| 1553 | StringRef Category) const { |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 1554 | // For now globals can be blacklisted only in ASan and KASan. |
| 1555 | if (!LangOpts.Sanitize.hasOneOf( |
| 1556 | SanitizerKind::Address | SanitizerKind::KernelAddress)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1557 | return false; |
| 1558 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
| 1559 | if (SanitizerBL.isBlacklistedGlobal(GV->getName(), Category)) |
| 1560 | return true; |
| 1561 | if (SanitizerBL.isBlacklistedLocation(Loc, Category)) |
| 1562 | return true; |
| 1563 | // Check global type. |
| 1564 | if (!Ty.isNull()) { |
| 1565 | // Drill down the array types: if global variable of a fixed type is |
| 1566 | // blacklisted, we also don't instrument arrays of them. |
| 1567 | while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr())) |
| 1568 | Ty = AT->getElementType(); |
| 1569 | Ty = Ty.getCanonicalType().getUnqualifiedType(); |
| 1570 | // We allow to blacklist only record types (classes, structs etc.) |
| 1571 | if (Ty->isRecordType()) { |
| 1572 | std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy()); |
| 1573 | if (SanitizerBL.isBlacklistedType(TypeStr, Category)) |
| 1574 | return true; |
| 1575 | } |
| 1576 | } |
| 1577 | return false; |
| 1578 | } |
| 1579 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 1580 | bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, |
| 1581 | StringRef Category) const { |
| 1582 | if (!LangOpts.XRayInstrument) |
| 1583 | return false; |
| 1584 | const auto &XRayFilter = getContext().getXRayFilter(); |
| 1585 | using ImbueAttr = XRayFunctionFilter::ImbueAttribute; |
| 1586 | auto Attr = XRayFunctionFilter::ImbueAttribute::NONE; |
| 1587 | if (Loc.isValid()) |
| 1588 | Attr = XRayFilter.shouldImbueLocation(Loc, Category); |
| 1589 | if (Attr == ImbueAttr::NONE) |
| 1590 | Attr = XRayFilter.shouldImbueFunction(Fn->getName()); |
| 1591 | switch (Attr) { |
| 1592 | case ImbueAttr::NONE: |
| 1593 | return false; |
| 1594 | case ImbueAttr::ALWAYS: |
| 1595 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 1596 | break; |
Dean Michael Berris | 170429e | 2017-05-24 05:46:36 +0000 | [diff] [blame] | 1597 | case ImbueAttr::ALWAYS_ARG1: |
| 1598 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 1599 | Fn->addFnAttr("xray-log-args", "1"); |
| 1600 | break; |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 1601 | case ImbueAttr::NEVER: |
| 1602 | Fn->addFnAttr("function-instrument", "xray-never"); |
| 1603 | break; |
| 1604 | } |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1608 | bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 1609 | // Never defer when EmitAllDecls is specified. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1610 | if (LangOpts.EmitAllDecls) |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1611 | return true; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1612 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1613 | return getContext().DeclMustBeEmitted(Global); |
| 1614 | } |
| 1615 | |
| 1616 | bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) { |
| 1617 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) |
| 1618 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1619 | // Implicit template instantiations may change linkage if they are later |
| 1620 | // explicitly instantiated, so they should not be emitted eagerly. |
| 1621 | return false; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 1622 | if (const auto *VD = dyn_cast<VarDecl>(Global)) |
| 1623 | if (Context.getInlineVariableDefinitionKind(VD) == |
| 1624 | ASTContext::InlineVariableDefinitionKind::WeakUnknown) |
| 1625 | // A definition of an inline constexpr static data member may change |
| 1626 | // linkage later if it's redeclared outside the class. |
| 1627 | return false; |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1628 | // If OpenMP is enabled and threadprivates must be generated like TLS, delay |
| 1629 | // codegen for global variables, because they may be marked as threadprivate. |
| 1630 | if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS && |
| 1631 | getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global)) |
| 1632 | return false; |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1633 | |
| 1634 | return true; |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1637 | ConstantAddress CodeGenModule::GetAddrOfUuidDescriptor( |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1638 | const CXXUuidofExpr* E) { |
| 1639 | // Sema has verified that IIDSource has a __declspec(uuid()), and that its |
| 1640 | // well-formed. |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 1641 | StringRef Uuid = E->getUuidStr(); |
David Majnemer | cf963ce | 2013-08-09 08:35:59 +0000 | [diff] [blame] | 1642 | std::string Name = "_GUID_" + Uuid.lower(); |
| 1643 | std::replace(Name.begin(), Name.end(), '-', '_'); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1644 | |
David Majnemer | 6568760 | 2016-03-27 04:46:14 +0000 | [diff] [blame] | 1645 | // The UUID descriptor should be pointer aligned. |
| 1646 | CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1647 | |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1648 | // Look for an existing global. |
| 1649 | if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1650 | return ConstantAddress(GV, Alignment); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1651 | |
Nico Weber | 17d3a2c | 2014-09-08 16:26:36 +0000 | [diff] [blame] | 1652 | llvm::Constant *Init = EmitUuidofInitializer(Uuid); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1653 | assert(Init && "failed to initialize as constant"); |
| 1654 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1655 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 1656 | getModule(), Init->getType(), |
Reid Kleckner | b9921df | 2013-09-03 21:49:32 +0000 | [diff] [blame] | 1657 | /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 1658 | if (supportsCOMDAT()) |
| 1659 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1660 | return ConstantAddress(GV, Alignment); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1663 | ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1664 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
| 1665 | assert(AA && "No alias?"); |
| 1666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1667 | CharUnits Alignment = getContext().getDeclAlign(VD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1668 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1669 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1670 | // See if there is already something with the target's name in the module. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1671 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1672 | if (Entry) { |
| 1673 | unsigned AS = getContext().getTargetAddressSpace(VD->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1674 | auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS)); |
| 1675 | return ConstantAddress(Ptr, Alignment); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1676 | } |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1677 | |
| 1678 | llvm::Constant *Aliasee; |
| 1679 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 1680 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, |
| 1681 | GlobalDecl(cast<FunctionDecl>(VD)), |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 1682 | /*ForVTable=*/false); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1683 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1684 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1685 | llvm::PointerType::getUnqual(DeclTy), |
| 1686 | nullptr); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1687 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1688 | auto *F = cast<llvm::GlobalValue>(Aliasee); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1689 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 1690 | WeakRefReferences.insert(F); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1691 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1692 | return ConstantAddress(Aliasee, Alignment); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1695 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1696 | const auto *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1698 | // Weak references don't produce any output by themselves. |
| 1699 | if (Global->hasAttr<WeakRefAttr>()) |
| 1700 | return; |
| 1701 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1702 | // If this is an alias definition (which otherwise looks like a declaration) |
| 1703 | // emit it now. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1704 | if (Global->hasAttr<AliasAttr>()) |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 1705 | return EmitAliasDefinition(GD); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1706 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1707 | // IFunc like an alias whose value is resolved at runtime by calling resolver. |
| 1708 | if (Global->hasAttr<IFuncAttr>()) |
| 1709 | return emitIFuncDefinition(GD); |
| 1710 | |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1711 | // If this is CUDA, be selective about which declarations we emit. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1712 | if (LangOpts.CUDA) { |
Artem Belevich | f3d3db6 | 2015-03-19 18:58:18 +0000 | [diff] [blame] | 1713 | if (LangOpts.CUDAIsDevice) { |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1714 | if (!Global->hasAttr<CUDADeviceAttr>() && |
| 1715 | !Global->hasAttr<CUDAGlobalAttr>() && |
| 1716 | !Global->hasAttr<CUDAConstantAttr>() && |
| 1717 | !Global->hasAttr<CUDASharedAttr>()) |
| 1718 | return; |
| 1719 | } else { |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 1720 | // We need to emit host-side 'shadows' for all global |
| 1721 | // device-side variables because the CUDA runtime needs their |
| 1722 | // size and host-side address in order to provide access to |
| 1723 | // their device-side incarnations. |
| 1724 | |
| 1725 | // So device-only functions are the only things we skip. |
| 1726 | if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() && |
| 1727 | Global->hasAttr<CUDADeviceAttr>()) |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1728 | return; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 1729 | |
| 1730 | assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && |
| 1731 | "Expected Variable or Function"); |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1735 | if (LangOpts.OpenMP) { |
| 1736 | // If this is OpenMP device, check if it is legal to emit this global |
| 1737 | // normally. |
| 1738 | if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD)) |
| 1739 | return; |
| 1740 | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) { |
| 1741 | if (MustBeEmitted(Global)) |
| 1742 | EmitOMPDeclareReduction(DRD); |
| 1743 | return; |
| 1744 | } |
| 1745 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1746 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1747 | // Ignore declarations, they will be emitted on their first use. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1748 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1749 | // Forward declarations are emitted lazily on first use. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1750 | if (!FD->doesThisDeclarationHaveABody()) { |
| 1751 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
| 1752 | return; |
| 1753 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1754 | StringRef MangledName = getMangledName(GD); |
David Majnemer | af36980 | 2014-03-29 14:19:55 +0000 | [diff] [blame] | 1755 | |
| 1756 | // Compute the function info and LLVM type. |
| 1757 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 1758 | llvm::Type *Ty = getTypes().GetFunctionType(FI); |
| 1759 | |
| 1760 | GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false, |
| 1761 | /*DontDefer=*/false); |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1762 | return; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1763 | } |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1764 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1765 | const auto *VD = cast<VarDecl>(Global); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1766 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 1767 | // We need to emit device-side global CUDA variables even if a |
| 1768 | // variable does not have a definition -- we still need to define |
| 1769 | // host-side shadow for it. |
| 1770 | bool MustEmitForCuda = LangOpts.CUDA && !LangOpts.CUDAIsDevice && |
| 1771 | !VD->hasDefinition() && |
| 1772 | (VD->hasAttr<CUDAConstantAttr>() || |
| 1773 | VD->hasAttr<CUDADeviceAttr>()); |
| 1774 | if (!MustEmitForCuda && |
| 1775 | VD->isThisDeclarationADefinition() != VarDecl::Definition && |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 1776 | !Context.isMSStaticDataMemberInlineDefinition(VD)) { |
| 1777 | // If this declaration may have caused an inline variable definition to |
| 1778 | // change linkage, make sure that it's emitted. |
| 1779 | if (Context.getInlineVariableDefinitionKind(VD) == |
| 1780 | ASTContext::InlineVariableDefinitionKind::Strong) |
| 1781 | GetAddrOfGlobalVar(VD); |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 1782 | return; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 1783 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1786 | // Defer code generation to first use when possible, e.g. if this is an inline |
| 1787 | // function. If the global must always be emitted, do it eagerly if possible |
| 1788 | // to benefit from cache locality. |
| 1789 | if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) { |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 1790 | // Emit the definition if it can't be deferred. |
| 1791 | EmitGlobalDefinition(GD); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1792 | return; |
| 1793 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1794 | |
| 1795 | // If we're deferring emission of a C++ variable with an |
| 1796 | // initializer, remember the order in which it appeared in the file. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1797 | if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) && |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1798 | cast<VarDecl>(Global)->hasInit()) { |
| 1799 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1800 | CXXGlobalInits.push_back(nullptr); |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1801 | } |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1802 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1803 | StringRef MangledName = getMangledName(GD); |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1804 | if (GetGlobalValue(MangledName) != nullptr) { |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1805 | // The value has already been used and should therefore be emitted. |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1806 | addDeferredDeclToEmit(GD); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1807 | } else if (MustBeEmitted(Global)) { |
| 1808 | // The value must be emitted, but cannot be emitted eagerly. |
| 1809 | assert(!MayBeEmittedEagerly(Global)); |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1810 | addDeferredDeclToEmit(GD); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1811 | } else { |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 1812 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 1813 | // first use of the mangled name will cause it to move into |
| 1814 | // DeferredDeclsToEmit. |
| 1815 | DeferredDecls[MangledName] = GD; |
| 1816 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1819 | // Check if T is a class type with a destructor that's not dllimport. |
| 1820 | static bool HasNonDllImportDtor(QualType T) { |
| 1821 | if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>()) |
| 1822 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 1823 | if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>()) |
| 1824 | return true; |
| 1825 | |
| 1826 | return false; |
| 1827 | } |
| 1828 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1829 | namespace { |
| 1830 | struct FunctionIsDirectlyRecursive : |
| 1831 | public RecursiveASTVisitor<FunctionIsDirectlyRecursive> { |
| 1832 | const StringRef Name; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1833 | const Builtin::Context &BI; |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1834 | bool Result; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1835 | FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) : |
| 1836 | Name(N), BI(C), Result(false) { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1837 | } |
| 1838 | typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base; |
| 1839 | |
| 1840 | bool TraverseCallExpr(CallExpr *E) { |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1841 | const FunctionDecl *FD = E->getDirectCallee(); |
| 1842 | if (!FD) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1843 | return true; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1844 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 1845 | if (Attr && Name == Attr->getLabel()) { |
| 1846 | Result = true; |
| 1847 | return false; |
| 1848 | } |
| 1849 | unsigned BuiltinID = FD->getBuiltinID(); |
David Majnemer | 41011f6 | 2015-06-25 23:50:40 +0000 | [diff] [blame] | 1850 | if (!BuiltinID || !BI.isLibFunction(BuiltinID)) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1851 | return true; |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 1852 | StringRef BuiltinName = BI.getName(BuiltinID); |
Nick Lewycky | 9ed5b15 | 2012-01-18 03:41:19 +0000 | [diff] [blame] | 1853 | if (BuiltinName.startswith("__builtin_") && |
| 1854 | Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1855 | Result = true; |
| 1856 | return false; |
| 1857 | } |
| 1858 | return true; |
| 1859 | } |
| 1860 | }; |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1861 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1862 | // Make sure we're not referencing non-imported vars or functions. |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1863 | struct DLLImportFunctionVisitor |
| 1864 | : public RecursiveASTVisitor<DLLImportFunctionVisitor> { |
| 1865 | bool SafeToInline = true; |
| 1866 | |
Hans Wennborg | 1b3aee7 | 2016-09-13 22:51:42 +0000 | [diff] [blame] | 1867 | bool shouldVisitImplicitCode() const { return true; } |
| 1868 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1869 | bool VisitVarDecl(VarDecl *VD) { |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1870 | if (VD->getTLSKind()) { |
| 1871 | // A thread-local variable cannot be imported. |
| 1872 | SafeToInline = false; |
| 1873 | return SafeToInline; |
| 1874 | } |
| 1875 | |
| 1876 | // A variable definition might imply a destructor call. |
| 1877 | if (VD->isThisDeclarationADefinition()) |
| 1878 | SafeToInline = !HasNonDllImportDtor(VD->getType()); |
| 1879 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1880 | return SafeToInline; |
| 1881 | } |
| 1882 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1883 | bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 1884 | if (const auto *D = E->getTemporary()->getDestructor()) |
| 1885 | SafeToInline = D->hasAttr<DLLImportAttr>(); |
| 1886 | return SafeToInline; |
| 1887 | } |
| 1888 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1889 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1890 | ValueDecl *VD = E->getDecl(); |
| 1891 | if (isa<FunctionDecl>(VD)) |
| 1892 | SafeToInline = VD->hasAttr<DLLImportAttr>(); |
| 1893 | else if (VarDecl *V = dyn_cast<VarDecl>(VD)) |
| 1894 | SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>(); |
| 1895 | return SafeToInline; |
| 1896 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1897 | |
Hans Wennborg | 1b3aee7 | 2016-09-13 22:51:42 +0000 | [diff] [blame] | 1898 | bool VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 1899 | SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>(); |
| 1900 | return SafeToInline; |
| 1901 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1902 | |
Hans Wennborg | 251c204 | 2017-01-23 23:57:50 +0000 | [diff] [blame] | 1903 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 1904 | CXXMethodDecl *M = E->getMethodDecl(); |
| 1905 | if (!M) { |
| 1906 | // Call through a pointer to member function. This is safe to inline. |
| 1907 | SafeToInline = true; |
| 1908 | } else { |
| 1909 | SafeToInline = M->hasAttr<DLLImportAttr>(); |
| 1910 | } |
| 1911 | return SafeToInline; |
| 1912 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1913 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1914 | bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 1915 | SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>(); |
| 1916 | return SafeToInline; |
| 1917 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 1918 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1919 | bool VisitCXXNewExpr(CXXNewExpr *E) { |
| 1920 | SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>(); |
| 1921 | return SafeToInline; |
| 1922 | } |
| 1923 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1924 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1925 | |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1926 | // isTriviallyRecursive - Check if this function calls another |
| 1927 | // decl that, because of the asm attribute or the other decl being a builtin, |
| 1928 | // ends up pointing to itself. |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1929 | bool |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1930 | CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) { |
| 1931 | StringRef Name; |
| 1932 | if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) { |
Nick Lewycky | 72cd229 | 2012-01-18 01:50:13 +0000 | [diff] [blame] | 1933 | // asm labels are a special kind of mangling we have to support. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1934 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 1935 | if (!Attr) |
| 1936 | return false; |
| 1937 | Name = Attr->getLabel(); |
| 1938 | } else { |
| 1939 | Name = FD->getName(); |
| 1940 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1941 | |
David Majnemer | 64b0bdf | 2015-06-30 04:41:18 +0000 | [diff] [blame] | 1942 | FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo); |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1943 | Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD)); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1944 | return Walker.Result; |
| 1945 | } |
| 1946 | |
Hans Wennborg | 93f7547 | 2016-09-13 21:08:20 +0000 | [diff] [blame] | 1947 | bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) { |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1948 | if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1949 | return true; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1950 | const auto *F = cast<FunctionDecl>(GD.getDecl()); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 1951 | if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>()) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1952 | return false; |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1953 | |
| 1954 | if (F->hasAttr<DLLImportAttr>()) { |
| 1955 | // Check whether it would be safe to inline this dllimport function. |
| 1956 | DLLImportFunctionVisitor Visitor; |
| 1957 | Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F)); |
| 1958 | if (!Visitor.SafeToInline) |
| 1959 | return false; |
Hans Wennborg | 93f7547 | 2016-09-13 21:08:20 +0000 | [diff] [blame] | 1960 | |
| 1961 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) { |
| 1962 | // Implicit destructor invocations aren't captured in the AST, so the |
| 1963 | // check above can't see them. Check for them manually here. |
| 1964 | for (const Decl *Member : Dtor->getParent()->decls()) |
| 1965 | if (isa<FieldDecl>(Member)) |
| 1966 | if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType())) |
| 1967 | return false; |
| 1968 | for (const CXXBaseSpecifier &B : Dtor->getParent()->bases()) |
| 1969 | if (HasNonDllImportDtor(B.getType())) |
| 1970 | return false; |
| 1971 | } |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1974 | // PR9614. Avoid cases where the source code is lying to us. An available |
| 1975 | // externally function should have an equivalent function somewhere else, |
| 1976 | // but a function that calls itself is clearly not equivalent to the real |
| 1977 | // implementation. |
| 1978 | // This happens in glibc's btowc and in some configure checks. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1979 | return !isTriviallyRecursive(F); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 1982 | bool CodeGenModule::shouldOpportunisticallyEmitVTables() { |
| 1983 | return CodeGenOpts.OptimizationLevel > 0; |
| 1984 | } |
| 1985 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1986 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1987 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1989 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
Anders Carlsson | 29295bf | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 1990 | Context.getSourceManager(), |
| 1991 | "Generating code for declaration"); |
| 1992 | |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1993 | if (isa<FunctionDecl>(D)) { |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1994 | // At -O0, don't generate IR for functions with available_externally |
| 1995 | // linkage. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1996 | if (!shouldEmitFunction(GD)) |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1997 | return; |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 1998 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1999 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2000 | // Make sure to emit the definition(s) before we emit the thunks. |
| 2001 | // This is necessary for the generation of certain thunks. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2002 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(Method)) |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 2003 | ABI->emitCXXStructor(CD, getFromCtorType(GD.getCtorType())); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2004 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(Method)) |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 2005 | ABI->emitCXXStructor(DD, getFromDtorType(GD.getDtorType())); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2006 | else |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2007 | EmitGlobalFunctionDefinition(GD, GV); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2009 | if (Method->isVirtual()) |
| 2010 | getVTables().EmitThunks(GD); |
| 2011 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2012 | return; |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2013 | } |
Chris Lattner | d8d760c | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 2014 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2015 | return EmitGlobalFunctionDefinition(GD, GV); |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2016 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2019 | return EmitGlobalVarDefinition(VD, !VD->hasDefinition()); |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 2020 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2021 | llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2024 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 2025 | llvm::Function *NewFn); |
| 2026 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2027 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 2028 | /// module, create and return an llvm Function with the specified type. If there |
| 2029 | /// is something in the module with the specified name, return it potentially |
| 2030 | /// bitcasted to the right type. |
| 2031 | /// |
| 2032 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 2033 | /// to set the attributes on the function when it is first created. |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2034 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction( |
| 2035 | StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable, |
| 2036 | bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs, |
| 2037 | ForDefinition_t IsForDefinition) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2038 | const Decl *D = GD.getDecl(); |
| 2039 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2040 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2041 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2042 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 2043 | if (WeakRefReferences.erase(Entry)) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2044 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2045 | if (FD && !FD->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 2046 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 2049 | // Handle dropped DLL attributes. |
| 2050 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) |
| 2051 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 2052 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2053 | // If there are two attempts to define the same mangled name, issue an |
| 2054 | // error. |
| 2055 | if (IsForDefinition && !Entry->isDeclaration()) { |
| 2056 | GlobalDecl OtherGD; |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2057 | // Check that GD is not yet in DiagnosedConflictingDefinitions is required |
| 2058 | // to make sure that we issue an error only once. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2059 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
| 2060 | (GD.getCanonicalDecl().getDecl() != |
| 2061 | OtherGD.getCanonicalDecl().getDecl()) && |
| 2062 | DiagnosedConflictingDefinitions.insert(GD).second) { |
| 2063 | getDiags().Report(D->getLocation(), |
| 2064 | diag::err_duplicate_mangled_name); |
| 2065 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
| 2066 | diag::note_previous_definition); |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) && |
| 2071 | (Entry->getType()->getElementType() == Ty)) { |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2072 | return Entry; |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2073 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2075 | // Make sure the result is of the correct type. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2076 | // (If function is requested for a definition, we always need to create a new |
| 2077 | // function, not just return a bitcast.) |
| 2078 | if (!IsForDefinition) |
| 2079 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2080 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 2082 | // This function doesn't have a complete type (for example, the return |
| 2083 | // type is an incomplete struct). Use a fake type instead, and make |
| 2084 | // sure not to try to set attributes. |
| 2085 | bool IsIncompleteFunction = false; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2086 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2087 | llvm::FunctionType *FTy; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2088 | if (isa<llvm::FunctionType>(Ty)) { |
| 2089 | FTy = cast<llvm::FunctionType>(Ty); |
| 2090 | } else { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 2091 | FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 2092 | IsIncompleteFunction = true; |
| 2093 | } |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2094 | |
| 2095 | llvm::Function *F = |
| 2096 | llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, |
| 2097 | Entry ? StringRef() : MangledName, &getModule()); |
| 2098 | |
| 2099 | // If we already created a function with the same mangled name (but different |
| 2100 | // type) before, take its name and add it to the list of functions to be |
| 2101 | // replaced with F at the end of CodeGen. |
| 2102 | // |
| 2103 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 2104 | // then a definition of a different type (e.g. "int f(int x)"). |
| 2105 | if (Entry) { |
| 2106 | F->takeName(Entry); |
| 2107 | |
| 2108 | // This might be an implementation of a function without a prototype, in |
| 2109 | // which case, try to do special replacement of calls which match the new |
| 2110 | // prototype. The really key thing here is that we also potentially drop |
| 2111 | // arguments from the call site so as to make a direct call, which makes the |
| 2112 | // inliner happier and suppresses a number of optimizer warnings (!) about |
| 2113 | // dropping arguments. |
| 2114 | if (!Entry->use_empty()) { |
| 2115 | ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F); |
| 2116 | Entry->removeDeadConstantUsers(); |
| 2117 | } |
| 2118 | |
| 2119 | llvm::Constant *BC = llvm::ConstantExpr::getBitCast( |
| 2120 | F, Entry->getType()->getElementType()->getPointerTo()); |
| 2121 | addGlobalValReplacement(Entry, BC); |
| 2122 | } |
| 2123 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2124 | assert(F->getName() == MangledName && "name was uniqued!"); |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2125 | if (D) |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 2126 | SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk); |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2127 | if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) { |
| 2128 | llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 2129 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 2130 | } |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 2131 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2132 | if (!DontDefer) { |
| 2133 | // All MSVC dtors other than the base dtor are linkonce_odr and delegate to |
| 2134 | // each other bottoming out with the base dtor. Therefore we emit non-base |
| 2135 | // dtors on usage, even if there is no dtor definition in the TU. |
| 2136 | if (D && isa<CXXDestructorDecl>(D) && |
| 2137 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
| 2138 | GD.getDtorType())) |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2139 | addDeferredDeclToEmit(GD); |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 2140 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2141 | // This is the first use or definition of a mangled name. If there is a |
| 2142 | // deferred decl with this name, remember that we need to emit it at the end |
| 2143 | // of the file. |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 2144 | auto DDI = DeferredDecls.find(MangledName); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2145 | if (DDI != DeferredDecls.end()) { |
| 2146 | // Move the potentially referenced deferred decl to the |
| 2147 | // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we |
| 2148 | // don't need it anymore). |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2149 | addDeferredDeclToEmit(DDI->second); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2150 | DeferredDecls.erase(DDI); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2151 | |
| 2152 | // Otherwise, there are cases we have to worry about where we're |
| 2153 | // using a declaration for which we must emit a definition but where |
| 2154 | // we might not find a top-level definition: |
| 2155 | // - member functions defined inline in their classes |
| 2156 | // - friend functions defined inline in some class |
| 2157 | // - special member functions with implicit definitions |
| 2158 | // If we ever change our AST traversal to walk into class methods, |
| 2159 | // this will be unnecessary. |
| 2160 | // |
| 2161 | // We also don't emit a definition for a function if it's going to be an |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2162 | // entry in a vtable, unless it's already marked as used. |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2163 | } else if (getLangOpts().CPlusPlus && D) { |
| 2164 | // Look for a declaration that's lexically in a record. |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2165 | for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD; |
| 2166 | FD = FD->getPreviousDecl()) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2167 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2168 | if (FD->doesThisDeclarationHaveABody()) { |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2169 | addDeferredDeclToEmit(GD.getWithDecl(FD)); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | } |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 2172 | } |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2173 | } |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2174 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2175 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2177 | // Make sure the result is of the requested type. |
| 2178 | if (!IsIncompleteFunction) { |
| 2179 | assert(F->getType()->getElementType() == Ty); |
| 2180 | return F; |
| 2181 | } |
| 2182 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2183 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2184 | return llvm::ConstantExpr::getBitCast(F, PTy); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2187 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 2188 | /// non-null, then this function will use the specified type if it has to |
| 2189 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 2190 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2191 | llvm::Type *Ty, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2192 | bool ForVTable, |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2193 | bool DontDefer, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2194 | ForDefinition_t IsForDefinition) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2195 | // If there was no specific requested type, just convert it now. |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2196 | if (!Ty) { |
| 2197 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
| 2198 | auto CanonTy = Context.getCanonicalType(FD->getType()); |
| 2199 | Ty = getTypes().ConvertFunctionType(CanonTy, FD); |
| 2200 | } |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2201 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2202 | StringRef MangledName = getMangledName(GD); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2203 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2204 | /*IsThunk=*/false, llvm::AttributeList(), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2205 | IsForDefinition); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2206 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2207 | |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2208 | static const FunctionDecl * |
| 2209 | GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { |
| 2210 | TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
| 2211 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 2212 | |
| 2213 | IdentifierInfo &CII = C.Idents.get(Name); |
| 2214 | for (const auto &Result : DC->lookup(&CII)) |
| 2215 | if (const auto FD = dyn_cast<FunctionDecl>(Result)) |
| 2216 | return FD; |
| 2217 | |
| 2218 | if (!C.getLangOpts().CPlusPlus) |
| 2219 | return nullptr; |
| 2220 | |
| 2221 | // Demangle the premangled name from getTerminateFn() |
| 2222 | IdentifierInfo &CXXII = |
| 2223 | (Name == "_ZSt9terminatev" || Name == "\01?terminate@@YAXXZ") |
| 2224 | ? C.Idents.get("terminate") |
| 2225 | : C.Idents.get(Name); |
| 2226 | |
| 2227 | for (const auto &N : {"__cxxabiv1", "std"}) { |
| 2228 | IdentifierInfo &NS = C.Idents.get(N); |
| 2229 | for (const auto &Result : DC->lookup(&NS)) { |
| 2230 | NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result); |
| 2231 | if (auto LSD = dyn_cast<LinkageSpecDecl>(Result)) |
| 2232 | for (const auto &Result : LSD->lookup(&NS)) |
| 2233 | if ((ND = dyn_cast<NamespaceDecl>(Result))) |
| 2234 | break; |
| 2235 | |
| 2236 | if (ND) |
| 2237 | for (const auto &Result : ND->lookup(&CXXII)) |
| 2238 | if (const auto *FD = dyn_cast<FunctionDecl>(Result)) |
| 2239 | return FD; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | return nullptr; |
| 2244 | } |
| 2245 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2246 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 2247 | /// type and name. |
| 2248 | llvm::Constant * |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2249 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2250 | llvm::AttributeList ExtraAttrs, |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2251 | bool Local) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2252 | llvm::Constant *C = |
| 2253 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2254 | /*DontDefer=*/false, /*IsThunk=*/false, |
| 2255 | ExtraAttrs); |
| 2256 | |
| 2257 | if (auto *F = dyn_cast<llvm::Function>(C)) { |
| 2258 | if (F->empty()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2259 | F->setCallingConv(getRuntimeCC()); |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (!Local && getTriple().isOSBinFormatCOFF() && |
| 2262 | !getCodeGenOpts().LTOVisibilityPublicStd) { |
| 2263 | const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); |
| 2264 | if (!FD || FD->hasAttr<DLLImportAttr>()) { |
| 2265 | F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 2266 | F->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2267 | } |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2272 | return C; |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2273 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2274 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 2275 | /// CreateBuiltinFunction - Create a new builtin function with the specified |
| 2276 | /// type and name. |
| 2277 | llvm::Constant * |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2278 | CodeGenModule::CreateBuiltinFunction(llvm::FunctionType *FTy, StringRef Name, |
| 2279 | llvm::AttributeList ExtraAttrs) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 2280 | llvm::Constant *C = |
| 2281 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
| 2282 | /*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs); |
| 2283 | if (auto *F = dyn_cast<llvm::Function>(C)) |
| 2284 | if (F->empty()) |
| 2285 | F->setCallingConv(getBuiltinCC()); |
| 2286 | return C; |
| 2287 | } |
| 2288 | |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2289 | /// isTypeConstant - Determine whether an object of this type can be emitted |
| 2290 | /// as a constant. |
| 2291 | /// |
| 2292 | /// If ExcludeCtor is true, the duration when the object's constructor runs |
| 2293 | /// will not be considered. The caller will need to verify that the object is |
| 2294 | /// not written to during its construction. |
| 2295 | bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) { |
| 2296 | if (!Ty.isConstant(Context) && !Ty->isReferenceType()) |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 2297 | return false; |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2298 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2299 | if (Context.getLangOpts().CPlusPlus) { |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2300 | if (const CXXRecordDecl *Record |
| 2301 | = Context.getBaseElementType(Ty)->getAsCXXRecordDecl()) |
| 2302 | return ExcludeCtor && !Record->hasMutableFields() && |
| 2303 | Record->hasTrivialDestructor(); |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 2304 | } |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2305 | |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 2306 | return true; |
| 2307 | } |
| 2308 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2309 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 2310 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 2311 | /// is something in the module with the specified name, return it potentially |
| 2312 | /// bitcasted to the right type. |
| 2313 | /// |
| 2314 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 2315 | /// to set the attributes on the global when it is first created. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2316 | /// |
| 2317 | /// If IsForDefinition is true, it is guranteed that an actual global with |
| 2318 | /// type Ty will be returned, not conversion of a variable with the same |
| 2319 | /// mangled name but some other type. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2320 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2321 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2322 | llvm::PointerType *Ty, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2323 | const VarDecl *D, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2324 | ForDefinition_t IsForDefinition) { |
Daniel Dunbar | 829e988 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 2325 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2326 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2327 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 2328 | if (WeakRefReferences.erase(Entry)) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2329 | if (D && !D->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 2330 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 2333 | // Handle dropped DLL attributes. |
| 2334 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) |
| 2335 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 2336 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2337 | if (Entry->getType() == Ty) |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2338 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2340 | // If there are two attempts to define the same mangled name, issue an |
| 2341 | // error. |
| 2342 | if (IsForDefinition && !Entry->isDeclaration()) { |
| 2343 | GlobalDecl OtherGD; |
| 2344 | const VarDecl *OtherD; |
| 2345 | |
| 2346 | // Check that D is not yet in DiagnosedConflictingDefinitions is required |
| 2347 | // to make sure that we issue an error only once. |
Artem Belevich | 2c323a0 | 2016-05-19 18:00:18 +0000 | [diff] [blame] | 2348 | if (D && lookupRepresentativeDecl(MangledName, OtherGD) && |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2349 | (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) && |
| 2350 | (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) && |
| 2351 | OtherD->hasInit() && |
| 2352 | DiagnosedConflictingDefinitions.insert(D).second) { |
| 2353 | getDiags().Report(D->getLocation(), |
| 2354 | diag::err_duplicate_mangled_name); |
| 2355 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
| 2356 | diag::note_previous_definition); |
| 2357 | } |
| 2358 | } |
| 2359 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2360 | // Make sure the result is of the correct type. |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 2361 | if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) |
| 2362 | return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); |
| 2363 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2364 | // (If global is requested for a definition, we always need to create a new |
| 2365 | // global, not just return a bitcast.) |
| 2366 | if (!IsForDefinition) |
| 2367 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 2368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2370 | auto AddrSpace = GetGlobalVarAddressSpace(D); |
| 2371 | auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace); |
| 2372 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2373 | auto *GV = new llvm::GlobalVariable( |
| 2374 | getModule(), Ty->getElementType(), false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2375 | llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2376 | llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 2377 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2378 | // If we already created a global with the same mangled name (but different |
| 2379 | // type) before, take its name and remove it from its parent. |
| 2380 | if (Entry) { |
| 2381 | GV->takeName(Entry); |
| 2382 | |
| 2383 | if (!Entry->use_empty()) { |
| 2384 | llvm::Constant *NewPtrForOldDecl = |
| 2385 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
| 2386 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 2387 | } |
| 2388 | |
| 2389 | Entry->eraseFromParent(); |
| 2390 | } |
| 2391 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2392 | // This is the first use or definition of a mangled name. If there is a |
| 2393 | // deferred decl with this name, remember that we need to emit it at the end |
| 2394 | // of the file. |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 2395 | auto DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2396 | if (DDI != DeferredDecls.end()) { |
| 2397 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 2398 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2399 | addDeferredDeclToEmit(DDI->second); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2400 | DeferredDecls.erase(DDI); |
| 2401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2403 | // Handle things which are present even on external declarations. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2404 | if (D) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2405 | // FIXME: This code is overly simple and should be merged with other global |
| 2406 | // handling. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2407 | GV->setConstant(isTypeConstant(D->getType(), false)); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2408 | |
Ulrich Weigand | 4608438 | 2015-04-21 17:27:59 +0000 | [diff] [blame] | 2409 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
| 2410 | |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 2411 | setLinkageAndVisibilityForGV(GV, D); |
Eli Friedman | 4f85674 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 2412 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2413 | if (D->getTLSKind()) { |
Richard Smith | 1847baa | 2013-04-22 08:06:17 +0000 | [diff] [blame] | 2414 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
Richard Smith | 5a99c49 | 2015-12-01 01:10:48 +0000 | [diff] [blame] | 2415 | CXXThreadLocals.push_back(D); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 2416 | setTLSMode(GV, *D); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2417 | } |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 2418 | |
| 2419 | // If required by the ABI, treat declarations of static data members with |
| 2420 | // inline initializers as definitions. |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 2421 | if (getContext().isMSStaticDataMemberInlineDefinition(D)) { |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 2422 | EmitGlobalVarDefinition(D); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 2423 | } |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 2424 | |
| 2425 | // Handle XCore specific ABI requirements. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 2426 | if (getTriple().getArch() == llvm::Triple::xcore && |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 2427 | D->getLanguageLinkage() == CLanguageLinkage && |
| 2428 | D->getType().isConstant(Context) && |
| 2429 | isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) |
| 2430 | GV->setSection(".cp.rodata"); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2431 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2433 | auto ExpectedAS = |
| 2434 | D ? D->getType().getAddressSpace() |
Yaxun Liu | 7bce642 | 2017-07-08 19:13:41 +0000 | [diff] [blame] | 2435 | : static_cast<unsigned>(LangOpts.OpenCL ? LangAS::opencl_global |
| 2436 | : LangAS::Default); |
Benjamin Kramer | 6fbfdec | 2017-07-08 14:14:57 +0000 | [diff] [blame] | 2437 | assert(getContext().getTargetAddressSpace(ExpectedAS) == |
| 2438 | Ty->getPointerAddressSpace()); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2439 | if (AddrSpace != ExpectedAS) |
| 2440 | return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace, |
| 2441 | ExpectedAS, Ty); |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 2442 | |
| 2443 | return GV; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2446 | llvm::Constant * |
| 2447 | CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2448 | ForDefinition_t IsForDefinition) { |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2449 | const Decl *D = GD.getDecl(); |
| 2450 | if (isa<CXXConstructorDecl>(D)) |
| 2451 | return getAddrOfCXXStructor(cast<CXXConstructorDecl>(D), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2452 | getFromCtorType(GD.getCtorType()), |
| 2453 | /*FnInfo=*/nullptr, /*FnType=*/nullptr, |
| 2454 | /*DontDefer=*/false, IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2455 | else if (isa<CXXDestructorDecl>(D)) |
| 2456 | return getAddrOfCXXStructor(cast<CXXDestructorDecl>(D), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2457 | getFromDtorType(GD.getDtorType()), |
| 2458 | /*FnInfo=*/nullptr, /*FnType=*/nullptr, |
| 2459 | /*DontDefer=*/false, IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2460 | else if (isa<CXXMethodDecl>(D)) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2461 | auto FInfo = &getTypes().arrangeCXXMethodDeclaration( |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2462 | cast<CXXMethodDecl>(D)); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2463 | auto Ty = getTypes().GetFunctionType(*FInfo); |
| 2464 | return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, |
| 2465 | IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2466 | } else if (isa<FunctionDecl>(D)) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2467 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 2468 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
| 2469 | return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, |
| 2470 | IsForDefinition); |
| 2471 | } else |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2472 | return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2473 | IsForDefinition); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2474 | } |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2475 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2476 | llvm::GlobalVariable * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2477 | CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2478 | llvm::Type *Ty, |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2479 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 2480 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2481 | llvm::GlobalVariable *OldGV = nullptr; |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2482 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2483 | if (GV) { |
| 2484 | // Check if the variable has the right type. |
| 2485 | if (GV->getType()->getElementType() == Ty) |
| 2486 | return GV; |
| 2487 | |
| 2488 | // Because C++ name mangling, the only way we can end up with an already |
| 2489 | // existing global with the same name is if it has been declared extern "C". |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2490 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2491 | OldGV = GV; |
| 2492 | } |
| 2493 | |
| 2494 | // Create a new variable. |
| 2495 | GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2496 | Linkage, nullptr, Name); |
| 2497 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2498 | if (OldGV) { |
| 2499 | // Replace occurrences of the old variable if needed. |
| 2500 | GV->takeName(OldGV); |
| 2501 | |
| 2502 | if (!OldGV->use_empty()) { |
| 2503 | llvm::Constant *NewPtrForOldDecl = |
| 2504 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 2505 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 2506 | } |
| 2507 | |
| 2508 | OldGV->eraseFromParent(); |
| 2509 | } |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 2510 | |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 2511 | if (supportsCOMDAT() && GV->isWeakForLinker() && |
| 2512 | !GV->hasAvailableExternallyLinkage()) |
| 2513 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 2514 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2515 | return GV; |
| 2516 | } |
| 2517 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2518 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 2519 | /// given global variable. If Ty is non-null and if the global doesn't exist, |
Eric Christopher | 365e309 | 2012-04-16 23:55:04 +0000 | [diff] [blame] | 2520 | /// then it will be created with the specified type instead of whatever the |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2521 | /// normal requested type would be. If IsForDefinition is true, it is guranteed |
| 2522 | /// that an actual global with type Ty will be returned, not conversion of a |
| 2523 | /// variable with the same mangled name but some other type. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2524 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2525 | llvm::Type *Ty, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2526 | ForDefinition_t IsForDefinition) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2527 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 2528 | QualType ASTTy = D->getType(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2529 | if (!Ty) |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2530 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2532 | llvm::PointerType *PTy = |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2533 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2534 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2535 | StringRef MangledName = getMangledName(D); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2536 | return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 2540 | /// specified type and name. |
| 2541 | llvm::Constant * |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2542 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2543 | StringRef Name) { |
Rafael Espindola | b73c973 | 2014-05-22 23:33:27 +0000 | [diff] [blame] | 2544 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 2547 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 2548 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 2549 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2550 | StringRef MangledName = getMangledName(D); |
| 2551 | llvm::GlobalValue *GV = GetGlobalValue(MangledName); |
| 2552 | |
| 2553 | // We already have a definition, not declaration, with the same mangled name. |
| 2554 | // Emitting of declaration is not required (and actually overwrites emitted |
| 2555 | // definition). |
| 2556 | if (GV && !GV->isDeclaration()) |
| 2557 | return; |
| 2558 | |
| 2559 | // If we have not seen a reference to this variable yet, place it into the |
| 2560 | // deferred declarations table to be emitted if needed later. |
| 2561 | if (!MustBeEmitted(D) && !GV) { |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 2562 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 2563 | return; |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | // The tentative definition is the only definition. |
Reid Kleckner | 953fe03 | 2015-12-05 01:52:14 +0000 | [diff] [blame] | 2567 | EmitGlobalVarDefinition(D); |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2570 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 2571 | return Context.toCharUnitsFromBits( |
| 2572 | getDataLayout().getTypeStoreSizeInBits(Ty)); |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2575 | unsigned CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { |
| 2576 | unsigned AddrSpace; |
| 2577 | if (LangOpts.OpenCL) { |
Yaxun Liu | 7bce642 | 2017-07-08 19:13:41 +0000 | [diff] [blame] | 2578 | AddrSpace = D ? D->getType().getAddressSpace() |
| 2579 | : static_cast<unsigned>(LangAS::opencl_global); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2580 | assert(AddrSpace == LangAS::opencl_global || |
| 2581 | AddrSpace == LangAS::opencl_constant || |
| 2582 | AddrSpace == LangAS::opencl_local || |
| 2583 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 2584 | return AddrSpace; |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2587 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) { |
| 2588 | if (D && D->hasAttr<CUDAConstantAttr>()) |
| 2589 | return LangAS::cuda_constant; |
| 2590 | else if (D && D->hasAttr<CUDASharedAttr>()) |
| 2591 | return LangAS::cuda_shared; |
| 2592 | else |
| 2593 | return LangAS::cuda_device; |
| 2594 | } |
| 2595 | |
| 2596 | return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2599 | template<typename SomeDecl> |
| 2600 | void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, |
| 2601 | llvm::GlobalValue *GV) { |
| 2602 | if (!getLangOpts().CPlusPlus) |
| 2603 | return; |
| 2604 | |
| 2605 | // Must have 'used' attribute, or else inline assembly can't rely on |
| 2606 | // the name existing. |
| 2607 | if (!D->template hasAttr<UsedAttr>()) |
| 2608 | return; |
| 2609 | |
| 2610 | // Must have internal linkage and an ordinary name. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2611 | if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2612 | return; |
| 2613 | |
| 2614 | // Must be in an extern "C" context. Entities declared directly within |
| 2615 | // a record are not extern "C" even if the record is in such a context. |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2616 | const SomeDecl *First = D->getFirstDecl(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2617 | if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2618 | return; |
| 2619 | |
| 2620 | // OK, this is an internal linkage entity inside an extern "C" linkage |
| 2621 | // specification. Make a note of that so we can give it the "expected" |
| 2622 | // mangled name if nothing else is using that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 2623 | std::pair<StaticExternCMap::iterator, bool> R = |
| 2624 | StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2625 | |
| 2626 | // If we have multiple internal linkage entities with the same name |
| 2627 | // in extern "C" regions, none of them gets that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 2628 | if (!R.second) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2629 | R.first->second = nullptr; |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 2632 | static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) { |
| 2633 | if (!CGM.supportsCOMDAT()) |
| 2634 | return false; |
| 2635 | |
| 2636 | if (D.hasAttr<SelectAnyAttr>()) |
| 2637 | return true; |
| 2638 | |
| 2639 | GVALinkage Linkage; |
| 2640 | if (auto *VD = dyn_cast<VarDecl>(&D)) |
| 2641 | Linkage = CGM.getContext().GetGVALinkageForVariable(VD); |
| 2642 | else |
| 2643 | Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D)); |
| 2644 | |
| 2645 | switch (Linkage) { |
| 2646 | case GVA_Internal: |
| 2647 | case GVA_AvailableExternally: |
| 2648 | case GVA_StrongExternal: |
| 2649 | return false; |
| 2650 | case GVA_DiscardableODR: |
| 2651 | case GVA_StrongODR: |
| 2652 | return true; |
| 2653 | } |
| 2654 | llvm_unreachable("No such linkage"); |
| 2655 | } |
| 2656 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 2657 | void CodeGenModule::maybeSetTrivialComdat(const Decl &D, |
| 2658 | llvm::GlobalObject &GO) { |
| 2659 | if (!shouldBeInCOMDAT(*this, D)) |
| 2660 | return; |
| 2661 | GO.setComdat(TheModule.getOrInsertComdat(GO.getName())); |
| 2662 | } |
| 2663 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2664 | /// Pass IsTentative as true if you want to create a tentative definition. |
| 2665 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, |
| 2666 | bool IsTentative) { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 2667 | // OpenCL global variables of sampler type are translated to function calls, |
| 2668 | // therefore no need to be translated. |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2669 | QualType ASTTy = D->getType(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 2670 | if (getLangOpts().OpenCL && ASTTy->isSamplerT()) |
| 2671 | return; |
| 2672 | |
| 2673 | llvm::Constant *Init = nullptr; |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2674 | CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2675 | bool NeedsGlobalCtor = false; |
| 2676 | bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2678 | const VarDecl *InitDecl; |
| 2679 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 2680 | |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 2681 | // CUDA E.2.4.1 "__shared__ variables cannot have an initialization |
| 2682 | // as part of their declaration." Sema has already checked for |
| 2683 | // error cases, so we just need to set Init to UndefValue. |
| 2684 | if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice && |
| 2685 | D->hasAttr<CUDASharedAttr>()) |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 2686 | Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy)); |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 2687 | else if (!InitExpr) { |
Eli Friedman | 6859a1b | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 2688 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 2689 | // implicitly initialized with { 0 }. |
| 2690 | // |
| 2691 | // Note that tentative definitions are only emitted at the end of |
| 2692 | // a translation unit, so they should never have incomplete |
| 2693 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 2694 | // never attempt to emit a tentative definition if a real one |
| 2695 | // exists. A use may still exists, however, so we still may need |
| 2696 | // to do a RAUW. |
| 2697 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | f18318c | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 2698 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2699 | } else { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2700 | initializedGlobalDecl = GlobalDecl(D); |
| 2701 | Init = EmitConstantInit(*InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 2702 | |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 2703 | if (!Init) { |
Anders Carlsson | ca4a545 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 2704 | QualType T = InitExpr->getType(); |
Douglas Gregor | d450f06 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 2705 | if (D->getType()->isReferenceType()) |
| 2706 | T = D->getType(); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2707 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2708 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 2709 | Init = EmitNullConstant(T); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2710 | NeedsGlobalCtor = true; |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 2711 | } else { |
| 2712 | ErrorUnsupported(D, "static initializer"); |
| 2713 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 2714 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2715 | } else { |
| 2716 | // We don't need an initializer, so remove the entry for the delayed |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2717 | // initializer position (just in case this entry was delayed) if we |
| 2718 | // also don't need to register a destructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2719 | if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2720 | DelayedCXXInitPosition.erase(D); |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 2721 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2722 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2723 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2724 | llvm::Type* InitType = Init->getType(); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2725 | llvm::Constant *Entry = |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2726 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2728 | // Strip off a bitcast if we got one back. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2729 | if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 2730 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 2731 | CE->getOpcode() == llvm::Instruction::AddrSpaceCast || |
| 2732 | // All zero index gep. |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 2733 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2734 | Entry = CE->getOperand(0); |
| 2735 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2737 | // Entry is now either a Function or GlobalVariable. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2738 | auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2740 | // We have a definition after a declaration with the wrong type. |
| 2741 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 2742 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 2743 | // (which will be a definition). |
| 2744 | // |
| 2745 | // This happens if there is a prototype for a global (e.g. |
| 2746 | // "extern int x[];") and then a definition of a different type (e.g. |
| 2747 | // "int x[10];"). This also happens when an initializer has a different type |
| 2748 | // from the type of the global (this happens with unions). |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2749 | if (!GV || GV->getType()->getElementType() != InitType || |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2750 | GV->getType()->getAddressSpace() != |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2751 | getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2753 | // Move the old entry aside so that we'll create a new one. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2754 | Entry->setName(StringRef()); |
Daniel Dunbar | b2f4cdb | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2756 | // Make a new global with the correct type, this is now guaranteed to work. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2757 | GV = cast<llvm::GlobalVariable>( |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2758 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2759 | |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2760 | // Replace all uses of the old global with the new global |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2762 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2763 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Erase the old global, since it is no longer used. |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2766 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 2767 | } |
Devang Patel | 19c2b9a | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 2768 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2769 | MaybeHandleStaticInExternC(D, GV); |
| 2770 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2771 | if (D->hasAttr<AnnotateAttr>()) |
| 2772 | AddGlobalAnnotations(D, GV); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 2773 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2774 | // Set the llvm linkage type as appropriate. |
| 2775 | llvm::GlobalValue::LinkageTypes Linkage = |
| 2776 | getLLVMLinkageVarDefinition(D, GV->isConstant()); |
| 2777 | |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 2778 | // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on |
| 2779 | // the device. [...]" |
| 2780 | // CUDA B.2.2 "The __constant__ qualifier, optionally used together with |
| 2781 | // __device__, declares a variable that: [...] |
| 2782 | // Is accessible from all the threads within the grid and from the host |
| 2783 | // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize() |
| 2784 | // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())." |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2785 | if (GV && LangOpts.CUDA) { |
| 2786 | if (LangOpts.CUDAIsDevice) { |
| 2787 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) |
| 2788 | GV->setExternallyInitialized(true); |
| 2789 | } else { |
| 2790 | // Host-side shadows of external declarations of device-side |
| 2791 | // global variables become internal definitions. These have to |
| 2792 | // be internal in order to prevent name conflicts with global |
| 2793 | // host variables with the same name in a different TUs. |
| 2794 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) { |
| 2795 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 2796 | |
| 2797 | // Shadow variables and their properties must be registered |
| 2798 | // with CUDA runtime. |
| 2799 | unsigned Flags = 0; |
| 2800 | if (!D->hasDefinition()) |
| 2801 | Flags |= CGCUDARuntime::ExternDeviceVar; |
| 2802 | if (D->hasAttr<CUDAConstantAttr>()) |
| 2803 | Flags |= CGCUDARuntime::ConstantDeviceVar; |
| 2804 | getCUDARuntime().registerDeviceVar(*GV, Flags); |
| 2805 | } else if (D->hasAttr<CUDASharedAttr>()) |
| 2806 | // __shared__ variables are odd. Shadows do get created, but |
| 2807 | // they are not registered with the CUDA runtime, so they |
| 2808 | // can't really be used to access their device-side |
| 2809 | // counterparts. It's not clear yet whether it's nvcc's bug or |
| 2810 | // a feature, but we've got to do the same for compatibility. |
| 2811 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 2812 | } |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 2813 | } |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 2814 | GV->setInitializer(Init); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 2815 | |
| 2816 | // If it is safe to mark the global 'constant', do so now. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2817 | GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && |
| 2818 | isTypeConstant(D->getType(), true)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 2820 | // If it is in a read-only section, mark it 'constant'. |
| 2821 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 2822 | const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()]; |
| 2823 | if ((SI.SectionFlags & ASTContext::PSF_Write) == 0) |
| 2824 | GV->setConstant(true); |
| 2825 | } |
| 2826 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2827 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2828 | |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 2829 | |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 2830 | // On Darwin, if the normal linkage of a C++ thread_local variable is |
| 2831 | // LinkOnce or Weak, we keep the normal linkage to prevent multiple |
| 2832 | // copies within a linkage unit; otherwise, the backing variable has |
| 2833 | // internal linkage and all accesses should just be calls to the |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 2834 | // Itanium-specified entry point, which has the normal linkage of the |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 2835 | // variable. This is to preserve the ability to change the implementation |
| 2836 | // behind the scenes. |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 2837 | if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic && |
Manman Ren | f93fff2 | 2015-11-11 23:08:18 +0000 | [diff] [blame] | 2838 | Context.getTargetInfo().getTriple().isOSDarwin() && |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 2839 | !llvm::GlobalVariable::isLinkOnceLinkage(Linkage) && |
| 2840 | !llvm::GlobalVariable::isWeakLinkage(Linkage)) |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 2841 | Linkage = llvm::GlobalValue::InternalLinkage; |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 2842 | |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 2843 | GV->setLinkage(Linkage); |
Nico Rieck | bb0554f | 2014-01-14 15:23:53 +0000 | [diff] [blame] | 2844 | if (D->hasAttr<DLLImportAttr>()) |
| 2845 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 2846 | else if (D->hasAttr<DLLExportAttr>()) |
| 2847 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 2848 | else |
| 2849 | GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 2850 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2851 | if (Linkage == llvm::GlobalVariable::CommonLinkage) { |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 2852 | // common vars aren't constant even if declared const. |
| 2853 | GV->setConstant(false); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2854 | // Tentative definition of global variables may be initialized with |
| 2855 | // non-zero null pointers. In this case they should have weak linkage |
| 2856 | // since common linkage must have zero initializer and must not have |
| 2857 | // explicit section therefore cannot have non-zero initial value. |
| 2858 | if (!GV->getInitializer()->isNullValue()) |
| 2859 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
| 2860 | } |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 2861 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 2862 | setNonAliasAttributes(D, GV); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2863 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 2864 | if (D->getTLSKind() && !GV->isThreadLocal()) { |
| 2865 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
Richard Smith | 5a99c49 | 2015-12-01 01:10:48 +0000 | [diff] [blame] | 2866 | CXXThreadLocals.push_back(D); |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 2867 | setTLSMode(GV, *D); |
| 2868 | } |
| 2869 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 2870 | maybeSetTrivialComdat(*D, *GV); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 2871 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 2872 | // Emit the initializer function if necessary. |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 2873 | if (NeedsGlobalCtor || NeedsGlobalDtor) |
| 2874 | EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 2875 | |
Alexey Samsonov | 4b8de11 | 2014-08-01 21:35:28 +0000 | [diff] [blame] | 2876 | SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor); |
Kostya Serebryany | 28a26c8 | 2012-08-21 06:53:28 +0000 | [diff] [blame] | 2877 | |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 2878 | // Emit global variable debug information. |
Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 2879 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2880 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 2881 | DI->EmitGlobalVariable(GV, D); |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 2882 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 2883 | |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 2884 | static bool isVarDeclStrongDefinition(const ASTContext &Context, |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2885 | CodeGenModule &CGM, const VarDecl *D, |
| 2886 | bool NoCommon) { |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 2887 | // Don't give variables common linkage if -fno-common was specified unless it |
| 2888 | // was overridden by a NoCommon attribute. |
| 2889 | if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) |
| 2890 | return true; |
| 2891 | |
| 2892 | // C11 6.9.2/2: |
| 2893 | // A declaration of an identifier for an object that has file scope without |
| 2894 | // an initializer, and without a storage-class specifier or with the |
| 2895 | // storage-class specifier static, constitutes a tentative definition. |
| 2896 | if (D->getInit() || D->hasExternalStorage()) |
| 2897 | return true; |
| 2898 | |
| 2899 | // A variable cannot be both common and exist in a section. |
| 2900 | if (D->hasAttr<SectionAttr>()) |
| 2901 | return true; |
| 2902 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 2903 | // A variable cannot be both common and exist in a section. |
| 2904 | // We dont try to determine which is the right section in the front-end. |
| 2905 | // If no specialized section name is applicable, it will resort to default. |
| 2906 | if (D->hasAttr<PragmaClangBSSSectionAttr>() || |
| 2907 | D->hasAttr<PragmaClangDataSectionAttr>() || |
| 2908 | D->hasAttr<PragmaClangRodataSectionAttr>()) |
| 2909 | return true; |
| 2910 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 2911 | // Thread local vars aren't considered common linkage. |
| 2912 | if (D->getTLSKind()) |
| 2913 | return true; |
| 2914 | |
| 2915 | // Tentative definitions marked with WeakImportAttr are true definitions. |
| 2916 | if (D->hasAttr<WeakImportAttr>()) |
| 2917 | return true; |
| 2918 | |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2919 | // A variable cannot be both common and exist in a comdat. |
| 2920 | if (shouldBeInCOMDAT(CGM, *D)) |
| 2921 | return true; |
| 2922 | |
Sanjay Patel | 2f9303a | 2016-06-30 21:02:40 +0000 | [diff] [blame] | 2923 | // Declarations with a required alignment do not have common linkage in MSVC |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 2924 | // mode. |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 2925 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | e1a0b2e | 2015-02-03 08:49:32 +0000 | [diff] [blame] | 2926 | if (D->hasAttr<AlignedAttr>()) |
| 2927 | return true; |
| 2928 | QualType VarType = D->getType(); |
| 2929 | if (Context.isAlignmentRequired(VarType)) |
| 2930 | return true; |
| 2931 | |
| 2932 | if (const auto *RT = VarType->getAs<RecordType>()) { |
| 2933 | const RecordDecl *RD = RT->getDecl(); |
| 2934 | for (const FieldDecl *FD : RD->fields()) { |
| 2935 | if (FD->isBitField()) |
| 2936 | continue; |
| 2937 | if (FD->hasAttr<AlignedAttr>()) |
| 2938 | return true; |
| 2939 | if (Context.isAlignmentRequired(FD->getType())) |
| 2940 | return true; |
| 2941 | } |
| 2942 | } |
| 2943 | } |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 2944 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 2945 | return false; |
| 2946 | } |
| 2947 | |
Hans Wennborg | 1a3a074 | 2014-05-14 19:54:53 +0000 | [diff] [blame] | 2948 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 2949 | const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) { |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 2950 | if (Linkage == GVA_Internal) |
| 2951 | return llvm::Function::InternalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2952 | |
| 2953 | if (D->hasAttr<WeakAttr>()) { |
| 2954 | if (IsConstantVariable) |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 2955 | return llvm::GlobalVariable::WeakODRLinkage; |
| 2956 | else |
| 2957 | return llvm::GlobalVariable::WeakAnyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | // We are guaranteed to have a strong definition somewhere else, |
| 2961 | // so we can use available_externally linkage. |
| 2962 | if (Linkage == GVA_AvailableExternally) |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 2963 | return llvm::GlobalValue::AvailableExternallyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2964 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2965 | // Note that Apple's kernel linker doesn't support symbol |
| 2966 | // coalescing, so we need to avoid linkonce and weak linkages there. |
| 2967 | // Normally, this means we just map to internal, but for explicit |
| 2968 | // instantiations we'll map to external. |
| 2969 | |
| 2970 | // In C++, the compiler has to emit a definition in every translation unit |
| 2971 | // that references the function. We should use linkonce_odr because |
| 2972 | // a) if all references in this translation unit are optimized away, we |
| 2973 | // don't need to codegen it. b) if the function persists, it needs to be |
| 2974 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 2975 | // definition is dependable. |
| 2976 | if (Linkage == GVA_DiscardableODR) |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 2977 | return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2978 | : llvm::Function::InternalLinkage; |
| 2979 | |
| 2980 | // An explicit instantiation of a template has weak linkage, since |
| 2981 | // explicit instantiations can occur in multiple translation units |
| 2982 | // and must all be equivalent. However, we are not allowed to |
| 2983 | // throw away these explicit instantiations. |
Justin Lebar | 27ee130 | 2016-06-30 18:41:33 +0000 | [diff] [blame] | 2984 | // |
| 2985 | // We don't currently support CUDA device code spread out across multiple TUs, |
| 2986 | // so say that CUDA templates are either external (for kernels) or internal. |
| 2987 | // This lets llvm perform aggressive inter-procedural optimizations. |
| 2988 | if (Linkage == GVA_StrongODR) { |
| 2989 | if (Context.getLangOpts().AppleKext) |
| 2990 | return llvm::Function::ExternalLinkage; |
| 2991 | if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) |
| 2992 | return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage |
| 2993 | : llvm::Function::InternalLinkage; |
| 2994 | return llvm::Function::WeakODRLinkage; |
| 2995 | } |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2996 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2997 | // C++ doesn't have tentative definitions and thus cannot have common |
| 2998 | // linkage. |
| 2999 | if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 3000 | !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D), |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 3001 | CodeGenOpts.NoCommon)) |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 3002 | return llvm::GlobalVariable::CommonLinkage; |
| 3003 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3004 | // selectany symbols are externally visible, so use weak instead of |
| 3005 | // linkonce. MSVC optimizes away references to const selectany globals, so |
| 3006 | // all definitions should be the same and ODR linkage should be used. |
| 3007 | // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx |
| 3008 | if (D->hasAttr<SelectAnyAttr>()) |
| 3009 | return llvm::GlobalVariable::WeakODRLinkage; |
| 3010 | |
| 3011 | // Otherwise, we have strong external linkage. |
| 3012 | assert(Linkage == GVA_StrongExternal); |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 3013 | return llvm::GlobalVariable::ExternalLinkage; |
| 3014 | } |
| 3015 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3016 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition( |
| 3017 | const VarDecl *VD, bool IsConstant) { |
| 3018 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 3019 | return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant); |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3022 | /// Replace the uses of a function that was declared with a non-proto type. |
| 3023 | /// We want to silently drop extra arguments from call sites |
| 3024 | static void replaceUsesOfNonProtoConstant(llvm::Constant *old, |
| 3025 | llvm::Function *newFn) { |
| 3026 | // Fast path. |
| 3027 | if (old->use_empty()) return; |
| 3028 | |
| 3029 | llvm::Type *newRetTy = newFn->getReturnType(); |
| 3030 | SmallVector<llvm::Value*, 4> newArgs; |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3031 | SmallVector<llvm::OperandBundleDef, 1> newBundles; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3032 | |
| 3033 | for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); |
| 3034 | ui != ue; ) { |
| 3035 | llvm::Value::use_iterator use = ui++; // Increment before the use is erased. |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 3036 | llvm::User *user = use->getUser(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3037 | |
| 3038 | // Recognize and replace uses of bitcasts. Most calls to |
| 3039 | // unprototyped functions will use bitcasts. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3040 | if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3041 | if (bitcast->getOpcode() == llvm::Instruction::BitCast) |
| 3042 | replaceUsesOfNonProtoConstant(bitcast, newFn); |
| 3043 | continue; |
| 3044 | } |
| 3045 | |
| 3046 | // Recognize calls to the function. |
| 3047 | llvm::CallSite callSite(user); |
| 3048 | if (!callSite) continue; |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 3049 | if (!callSite.isCallee(&*use)) continue; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3050 | |
| 3051 | // If the return types don't match exactly, then we can't |
| 3052 | // transform this call unless it's dead. |
| 3053 | if (callSite->getType() != newRetTy && !callSite->use_empty()) |
| 3054 | continue; |
| 3055 | |
| 3056 | // Get the call site's attribute list. |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3057 | SmallVector<llvm::AttributeSet, 8> newArgAttrs; |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 3058 | llvm::AttributeList oldAttrs = callSite.getAttributes(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3059 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3060 | // If the function was passed too few arguments, don't transform. |
| 3061 | unsigned newNumArgs = newFn->arg_size(); |
| 3062 | if (callSite.arg_size() < newNumArgs) continue; |
| 3063 | |
| 3064 | // If extra arguments were passed, we silently drop them. |
| 3065 | // If any of the types mismatch, we don't transform. |
| 3066 | unsigned argNo = 0; |
| 3067 | bool dontTransform = false; |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3068 | for (llvm::Argument &A : newFn->args()) { |
| 3069 | if (callSite.getArgument(argNo)->getType() != A.getType()) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3070 | dontTransform = true; |
| 3071 | break; |
| 3072 | } |
| 3073 | |
| 3074 | // Add any parameter attributes. |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 3075 | newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo)); |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3076 | argNo++; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3077 | } |
| 3078 | if (dontTransform) |
| 3079 | continue; |
| 3080 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3081 | // Okay, we can transform this. Create the new call instruction and copy |
| 3082 | // over the required information. |
| 3083 | newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo); |
| 3084 | |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3085 | // Copy over any operand bundles. |
| 3086 | callSite.getOperandBundlesAsDefs(newBundles); |
| 3087 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3088 | llvm::CallSite newCall; |
| 3089 | if (callSite.isCall()) { |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3090 | newCall = llvm::CallInst::Create(newFn, newArgs, newBundles, "", |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3091 | callSite.getInstruction()); |
| 3092 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3093 | auto *oldInvoke = cast<llvm::InvokeInst>(callSite.getInstruction()); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3094 | newCall = llvm::InvokeInst::Create(newFn, |
| 3095 | oldInvoke->getNormalDest(), |
| 3096 | oldInvoke->getUnwindDest(), |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3097 | newArgs, newBundles, "", |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3098 | callSite.getInstruction()); |
| 3099 | } |
| 3100 | newArgs.clear(); // for the next iteration |
| 3101 | |
| 3102 | if (!newCall->getType()->isVoidTy()) |
| 3103 | newCall->takeName(callSite.getInstruction()); |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3104 | newCall.setAttributes(llvm::AttributeList::get( |
| 3105 | newFn->getContext(), oldAttrs.getFnAttributes(), |
| 3106 | oldAttrs.getRetAttributes(), newArgAttrs)); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3107 | newCall.setCallingConv(callSite.getCallingConv()); |
| 3108 | |
| 3109 | // Finally, remove the old call, replacing any uses with the new one. |
| 3110 | if (!callSite->use_empty()) |
| 3111 | callSite->replaceAllUsesWith(newCall.getInstruction()); |
| 3112 | |
| 3113 | // Copy debug location attached to CI. |
Duncan P. N. Exon Smith | 2809cc7 | 2015-03-30 20:01:41 +0000 | [diff] [blame] | 3114 | if (callSite->getDebugLoc()) |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3115 | newCall->setDebugLoc(callSite->getDebugLoc()); |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3116 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3117 | callSite->eraseFromParent(); |
| 3118 | } |
| 3119 | } |
| 3120 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 3121 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 3122 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 3123 | /// existing call uses of the old function in the module, this adjusts them to |
| 3124 | /// call the new function directly. |
| 3125 | /// |
| 3126 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 3127 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 3128 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 3129 | /// run at -O0. |
| 3130 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 3131 | llvm::Function *NewFn) { |
| 3132 | // If we're redefining a global as a function, don't transform it. |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3133 | if (!isa<llvm::Function>(Old)) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3135 | replaceUsesOfNonProtoConstant(Old, NewFn); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3138 | void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
David Majnemer | 60e5bdc | 2016-07-11 04:28:21 +0000 | [diff] [blame] | 3139 | auto DK = VD->isThisDeclarationADefinition(); |
| 3140 | if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) |
| 3141 | return; |
| 3142 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3143 | TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); |
| 3144 | // If we have a definition, this might be a deferred decl. If the |
| 3145 | // instantiation is explicit, make sure we emit it at the end. |
| 3146 | if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) |
| 3147 | GetAddrOfGlobalVar(VD); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3148 | |
| 3149 | EmitTopLevelDecl(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3150 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3151 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 3152 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, |
| 3153 | llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3154 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 3155 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 3156 | // Compute the function info and LLVM type. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3157 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 3158 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 3159 | |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 3160 | // Get or create the prototype for the function. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3161 | if (!GV || (GV->getType()->getElementType() != Ty)) |
| 3162 | GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, |
| 3163 | /*DontDefer=*/true, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3164 | ForDefinition)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3166 | // Already emitted. |
| 3167 | if (!GV->isDeclaration()) |
Renato Golin | c640ff6 | 2015-04-15 08:44:40 +0000 | [diff] [blame] | 3168 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 3170 | // We need to set linkage and visibility on the function before |
| 3171 | // generating code for it because various parts of IR generation |
| 3172 | // want to propagate this information down (e.g. to local static |
| 3173 | // declarations). |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3174 | auto *Fn = cast<llvm::Function>(GV); |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 3175 | setFunctionLinkage(GD, Fn); |
Hans Wennborg | bb4f962 | 2015-05-28 17:44:56 +0000 | [diff] [blame] | 3176 | setFunctionDLLStorageClass(GD, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3177 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 3178 | // FIXME: this is redundant with part of setFunctionDefinitionAttributes |
Anders Carlsson | c6a4789 | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 3179 | setGlobalVisibility(Fn, D); |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 3180 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3181 | MaybeHandleStaticInExternC(D, Fn); |
| 3182 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3183 | maybeSetTrivialComdat(*D, *Fn); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3184 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 3185 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 3186 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 3187 | setFunctionDefinitionAttributes(D, Fn); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 3188 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3190 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 3191 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3192 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 3193 | AddGlobalDtor(Fn, DA->getPriority()); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3194 | if (D->hasAttr<AnnotateAttr>()) |
| 3195 | AddGlobalAnnotations(D, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3198 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3199 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3200 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3201 | assert(AA && "Not an alias?"); |
| 3202 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3203 | StringRef MangledName = getMangledName(GD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Hal Finkel | 0e2b975 | 2015-09-04 21:49:21 +0000 | [diff] [blame] | 3205 | if (AA->getAliasee() == MangledName) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3206 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
Hal Finkel | 0e2b975 | 2015-09-04 21:49:21 +0000 | [diff] [blame] | 3207 | return; |
| 3208 | } |
| 3209 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3210 | // If there is a definition in the module, then it wins over the alias. |
| 3211 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 3212 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 3213 | if (Entry && !Entry->isDeclaration()) |
| 3214 | return; |
| 3215 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 3216 | Aliases.push_back(GD); |
| 3217 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3218 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3219 | |
| 3220 | // Create a reference to the named value. This ensures that it is emitted |
| 3221 | // if a deferred decl. |
| 3222 | llvm::Constant *Aliasee; |
| 3223 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 3224 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 3225 | /*ForVTable=*/false); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3226 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3227 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3228 | llvm::PointerType::getUnqual(DeclTy), |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3229 | /*D=*/nullptr); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3230 | |
| 3231 | // Create the new alias itself, but don't set a name yet. |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 3232 | auto *GA = llvm::GlobalAlias::create( |
David Blaikie | 2a791d7 | 2015-09-14 18:38:22 +0000 | [diff] [blame] | 3233 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Aliasee, &getModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3234 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3235 | if (Entry) { |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 3236 | if (GA->getAliasee() == Entry) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3237 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 3238 | return; |
| 3239 | } |
| 3240 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3241 | assert(Entry->isDeclaration()); |
| 3242 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3243 | // If there is a declaration in the module, then we had an extern followed |
| 3244 | // by the alias, as in: |
| 3245 | // extern int test6(); |
| 3246 | // ... |
| 3247 | // int test6() __attribute__((alias("test7"))); |
| 3248 | // |
| 3249 | // Remove it and replace uses of it with the alias. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3250 | GA->takeName(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3251 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3252 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3253 | Entry->getType())); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3254 | Entry->eraseFromParent(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3255 | } else { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 3256 | GA->setName(MangledName); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3257 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 3259 | // Set attributes which are particular to an alias; this is a |
| 3260 | // specialization of the attributes which may be set on a global |
| 3261 | // variable/function. |
Rafael Espindola | fcfbcc6 | 2014-10-08 00:00:09 +0000 | [diff] [blame] | 3262 | if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || |
| 3263 | D->isWeakImported()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 3264 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 3265 | } |
| 3266 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3267 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
| 3268 | if (VD->getTLSKind()) |
| 3269 | setTLSMode(GA, *VD); |
| 3270 | |
Rafael Espindola | fcfbcc6 | 2014-10-08 00:00:09 +0000 | [diff] [blame] | 3271 | setAliasAttributes(D, GA); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3274 | void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { |
| 3275 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
| 3276 | const IFuncAttr *IFA = D->getAttr<IFuncAttr>(); |
| 3277 | assert(IFA && "Not an ifunc?"); |
| 3278 | |
| 3279 | StringRef MangledName = getMangledName(GD); |
| 3280 | |
| 3281 | if (IFA->getResolver() == MangledName) { |
| 3282 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
| 3283 | return; |
| 3284 | } |
| 3285 | |
| 3286 | // Report an error if some definition overrides ifunc. |
| 3287 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 3288 | if (Entry && !Entry->isDeclaration()) { |
| 3289 | GlobalDecl OtherGD; |
| 3290 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
| 3291 | DiagnosedConflictingDefinitions.insert(GD).second) { |
| 3292 | Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name); |
| 3293 | Diags.Report(OtherGD.getDecl()->getLocation(), |
| 3294 | diag::note_previous_definition); |
| 3295 | } |
| 3296 | return; |
| 3297 | } |
| 3298 | |
| 3299 | Aliases.push_back(GD); |
| 3300 | |
| 3301 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
| 3302 | llvm::Constant *Resolver = |
| 3303 | GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD, |
| 3304 | /*ForVTable=*/false); |
| 3305 | llvm::GlobalIFunc *GIF = |
| 3306 | llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, |
| 3307 | "", Resolver, &getModule()); |
| 3308 | if (Entry) { |
| 3309 | if (GIF->getResolver() == Entry) { |
| 3310 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
| 3311 | return; |
| 3312 | } |
| 3313 | assert(Entry->isDeclaration()); |
| 3314 | |
| 3315 | // If there is a declaration in the module, then we had an extern followed |
| 3316 | // by the ifunc, as in: |
| 3317 | // extern int test(); |
| 3318 | // ... |
| 3319 | // int test() __attribute__((ifunc("resolver"))); |
| 3320 | // |
| 3321 | // Remove it and replace uses of it with the ifunc. |
| 3322 | GIF->takeName(Entry); |
| 3323 | |
| 3324 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF, |
| 3325 | Entry->getType())); |
| 3326 | Entry->eraseFromParent(); |
| 3327 | } else |
| 3328 | GIF->setName(MangledName); |
| 3329 | |
| 3330 | SetCommonAttributes(D, GIF); |
| 3331 | } |
| 3332 | |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 3333 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 3334 | ArrayRef<llvm::Type*> Tys) { |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3335 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 3336 | Tys); |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 3337 | } |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 3338 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3339 | static llvm::StringMapEntry<llvm::GlobalVariable *> & |
| 3340 | GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map, |
| 3341 | const StringLiteral *Literal, bool TargetIsLSB, |
| 3342 | bool &IsUTF16, unsigned &StringLength) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3343 | StringRef String = Literal->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 3344 | unsigned NumBytes = String.size(); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3345 | |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 3346 | // Check for simple case. |
| 3347 | if (!Literal->containsNonAsciiOrNull()) { |
| 3348 | StringLength = NumBytes; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3349 | return *Map.insert(std::make_pair(String, nullptr)).first; |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3352 | // Otherwise, convert the UTF8 literals into a string of shorts. |
| 3353 | IsUTF16 = true; |
| 3354 | |
Justin Lebar | 9091055 | 2016-09-30 00:38:45 +0000 | [diff] [blame] | 3355 | SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls. |
| 3356 | const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); |
| 3357 | llvm::UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | |
Justin Lebar | 9091055 | 2016-09-30 00:38:45 +0000 | [diff] [blame] | 3359 | (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, |
| 3360 | ToPtr + NumBytes, llvm::strictConversion); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3361 | |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 3362 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3363 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 3364 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3365 | // Add an explicit null. |
| 3366 | *ToPtr = 0; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3367 | return *Map.insert(std::make_pair( |
| 3368 | StringRef(reinterpret_cast<const char *>(ToBuf.data()), |
| 3369 | (StringLength + 1) * 2), |
| 3370 | nullptr)).first; |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3373 | ConstantAddress |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3374 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 3375 | unsigned StringLength = 0; |
| 3376 | bool isUTF16 = false; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3377 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
| 3378 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
| 3379 | getDataLayout().isLittleEndian(), isUTF16, |
| 3380 | StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3382 | if (auto *C = Entry.second) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3383 | return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3385 | llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 3386 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3387 | |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 3388 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3389 | if (!CFConstantStringClassRef) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3390 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3391 | Ty = llvm::ArrayType::get(Ty, 0); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3392 | llvm::Constant *GV = |
| 3393 | CreateRuntimeVariable(Ty, "__CFConstantStringClassReference"); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3394 | |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3395 | if (getTriple().isOSBinFormatCOFF()) { |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3396 | IdentifierInfo &II = getContext().Idents.get(GV->getName()); |
| 3397 | TranslationUnitDecl *TUDecl = getContext().getTranslationUnitDecl(); |
| 3398 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 3399 | llvm::GlobalValue *CGV = cast<llvm::GlobalValue>(GV); |
| 3400 | |
| 3401 | const VarDecl *VD = nullptr; |
| 3402 | for (const auto &Result : DC->lookup(&II)) |
| 3403 | if ((VD = dyn_cast<VarDecl>(Result))) |
| 3404 | break; |
| 3405 | |
| 3406 | if (!VD || !VD->hasAttr<DLLExportAttr>()) { |
| 3407 | CGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 3408 | CGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 3409 | } else { |
| 3410 | CGV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 3411 | CGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 3412 | } |
| 3413 | } |
| 3414 | |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 3415 | // Decay array -> ptr |
Saleem Abdulrasool | 4fab745 | 2016-09-11 01:25:15 +0000 | [diff] [blame] | 3416 | CFConstantStringClassRef = |
| 3417 | llvm::ConstantExpr::getGetElementPtr(Ty, GV, Zeros); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 3420 | QualType CFTy = getContext().getCFConstantStringType(); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 3421 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3422 | auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 3423 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3424 | ConstantInitBuilder Builder(*this); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3425 | auto Fields = Builder.beginStruct(STy); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3426 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3427 | // Class pointer. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3428 | Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3430 | // Flags. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3431 | Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 3432 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3433 | // String pointer. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3434 | llvm::Constant *C = nullptr; |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3435 | if (isUTF16) { |
Craig Topper | c005cc0 | 2015-09-27 03:44:08 +0000 | [diff] [blame] | 3436 | auto Arr = llvm::makeArrayRef( |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3437 | reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())), |
| 3438 | Entry.first().size() / 2); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3439 | C = llvm::ConstantDataArray::get(VMContext, Arr); |
| 3440 | } else { |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3441 | C = llvm::ConstantDataArray::getString(VMContext, Entry.first()); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3442 | } |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 3443 | |
Bill Wendling | 86131f2 | 2012-01-10 08:46:39 +0000 | [diff] [blame] | 3444 | // Note: -fwritable-strings doesn't make the backing store strings of |
| 3445 | // CFStrings writable. (See <rdar://problem/10657500>) |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3446 | auto *GV = |
Rafael Espindola | e1bd71f | 2014-01-21 02:57:56 +0000 | [diff] [blame] | 3447 | new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true, |
| 3448 | llvm::GlobalValue::PrivateLinkage, C, ".str"); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3449 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 3450 | // Don't enforce the target's minimum global alignment, since the only use |
| 3451 | // of the string is via this class initializer. |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3452 | CharUnits Align = isUTF16 |
| 3453 | ? getContext().getTypeAlignInChars(getContext().ShortTy) |
| 3454 | : getContext().getTypeAlignInChars(getContext().CharTy); |
| 3455 | GV->setAlignment(Align.getQuantity()); |
| 3456 | |
| 3457 | // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. |
| 3458 | // Without it LLVM can merge the string with a non unnamed_addr one during |
| 3459 | // LTO. Doing that changes the section it ends in, which surprises ld64. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3460 | if (getTriple().isOSBinFormatMachO()) |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3461 | GV->setSection(isUTF16 ? "__TEXT,__ustring" |
| 3462 | : "__TEXT,__cstring,cstring_literals"); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3463 | |
| 3464 | // String. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3465 | llvm::Constant *Str = |
David Blaikie | d3c127e | 2015-05-07 17:27:56 +0000 | [diff] [blame] | 3466 | llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 3467 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3468 | if (isUTF16) |
| 3469 | // Cast the UTF16 string to the correct type. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3470 | Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy); |
| 3471 | Fields.add(Str); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3472 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3473 | // String length. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3474 | auto Ty = getTypes().ConvertType(getContext().LongTy); |
| 3475 | Fields.addInt(cast<llvm::IntegerType>(Ty), StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3476 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3477 | CharUnits Alignment = getPointerAlign(); |
| 3478 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3479 | // The struct. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3480 | GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment, |
| 3481 | /*isConstant=*/false, |
| 3482 | llvm::GlobalVariable::PrivateLinkage); |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3483 | switch (getTriple().getObjectFormat()) { |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3484 | case llvm::Triple::UnknownObjectFormat: |
| 3485 | llvm_unreachable("unknown file format"); |
| 3486 | case llvm::Triple::COFF: |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3487 | case llvm::Triple::ELF: |
Dan Gohman | 839f215 | 2017-01-17 21:46:38 +0000 | [diff] [blame] | 3488 | case llvm::Triple::Wasm: |
Saleem Abdulrasool | 0295f8c | 2016-07-09 01:59:51 +0000 | [diff] [blame] | 3489 | GV->setSection("cfstring"); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3490 | break; |
| 3491 | case llvm::Triple::MachO: |
| 3492 | GV->setSection("__DATA,__cfstring"); |
| 3493 | break; |
| 3494 | } |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3495 | Entry.second = GV; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3497 | return ConstantAddress(GV, Alignment); |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3498 | } |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 3499 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3500 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
| 3501 | if (ObjCFastEnumerationStateType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 3502 | RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3503 | D->startDefinition(); |
| 3504 | |
| 3505 | QualType FieldTypes[] = { |
| 3506 | Context.UnsignedLongTy, |
| 3507 | Context.getPointerType(Context.getObjCIdType()), |
| 3508 | Context.getPointerType(Context.UnsignedLongTy), |
| 3509 | Context.getConstantArrayType(Context.UnsignedLongTy, |
| 3510 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3511 | }; |
| 3512 | |
| 3513 | for (size_t i = 0; i < 4; ++i) { |
| 3514 | FieldDecl *Field = FieldDecl::Create(Context, |
| 3515 | D, |
| 3516 | SourceLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3517 | SourceLocation(), nullptr, |
| 3518 | FieldTypes[i], /*TInfo=*/nullptr, |
| 3519 | /*BitWidth=*/nullptr, |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3520 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3521 | ICIS_NoInit); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3522 | Field->setAccess(AS_public); |
| 3523 | D->addDecl(Field); |
| 3524 | } |
| 3525 | |
| 3526 | D->completeDefinition(); |
| 3527 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
| 3528 | } |
| 3529 | |
| 3530 | return ObjCFastEnumerationStateType; |
| 3531 | } |
| 3532 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 3533 | llvm::Constant * |
| 3534 | CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { |
| 3535 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
| 3536 | |
| 3537 | // Don't emit it as the address of the string, emit the string data itself |
| 3538 | // as an inline array. |
Chris Lattner | 00a10ca | 2012-02-06 22:47:00 +0000 | [diff] [blame] | 3539 | if (E->getCharByteWidth() == 1) { |
| 3540 | SmallString<64> Str(E->getString()); |
| 3541 | |
| 3542 | // Resize the string to the right size, which is indicated by its type. |
| 3543 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); |
| 3544 | Str.resize(CAT->getSize().getZExtValue()); |
| 3545 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
| 3546 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3547 | |
| 3548 | auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3549 | llvm::Type *ElemTy = AType->getElementType(); |
| 3550 | unsigned NumElements = AType->getNumElements(); |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 3551 | |
| 3552 | // Wide strings have either 2-byte or 4-byte elements. |
| 3553 | if (ElemTy->getPrimitiveSizeInBits() == 16) { |
| 3554 | SmallVector<uint16_t, 32> Elements; |
| 3555 | Elements.reserve(NumElements); |
| 3556 | |
| 3557 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 3558 | Elements.push_back(E->getCodeUnit(i)); |
| 3559 | Elements.resize(NumElements); |
| 3560 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 3563 | assert(ElemTy->getPrimitiveSizeInBits() == 32); |
| 3564 | SmallVector<uint32_t, 32> Elements; |
| 3565 | Elements.reserve(NumElements); |
| 3566 | |
| 3567 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 3568 | Elements.push_back(E->getCodeUnit(i)); |
| 3569 | Elements.resize(NumElements); |
| 3570 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3573 | static llvm::GlobalVariable * |
| 3574 | GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, |
| 3575 | CodeGenModule &CGM, StringRef GlobalName, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3576 | CharUnits Alignment) { |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3577 | // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. |
| 3578 | unsigned AddrSpace = 0; |
| 3579 | if (CGM.getLangOpts().OpenCL) |
| 3580 | AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant); |
| 3581 | |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 3582 | llvm::Module &M = CGM.getModule(); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3583 | // Create a global variable for this string |
| 3584 | auto *GV = new llvm::GlobalVariable( |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 3585 | M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName, |
| 3586 | nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3587 | GV->setAlignment(Alignment.getQuantity()); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3588 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 3589 | if (GV->isWeakForLinker()) { |
| 3590 | assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals"); |
| 3591 | GV->setComdat(M.getOrInsertComdat(GV->getName())); |
| 3592 | } |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 3593 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3594 | return GV; |
| 3595 | } |
| 3596 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 3597 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 3598 | /// constant array for the given string literal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3599 | ConstantAddress |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3600 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S, |
| 3601 | StringRef Name) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3602 | CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType()); |
James Molloy | e735b2b | 2014-05-09 16:28:56 +0000 | [diff] [blame] | 3603 | |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3604 | llvm::Constant *C = GetConstantArrayFromStringLiteral(S); |
| 3605 | llvm::GlobalVariable **Entry = nullptr; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 3606 | if (!LangOpts.WritableStrings) { |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3607 | Entry = &ConstantStringMap[C]; |
| 3608 | if (auto GV = *Entry) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3609 | if (Alignment.getQuantity() > GV->getAlignment()) |
| 3610 | GV->setAlignment(Alignment.getQuantity()); |
| 3611 | return ConstantAddress(GV, Alignment); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 3612 | } |
Eli Friedman | 49ddc5f | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 3613 | } |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 3614 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3615 | SmallString<256> MangledNameBuffer; |
| 3616 | StringRef GlobalVariableName; |
| 3617 | llvm::GlobalValue::LinkageTypes LT; |
David Majnemer | 3843a05 | 2014-03-23 17:47:16 +0000 | [diff] [blame] | 3618 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3619 | // Mangle the string literal if the ABI allows for it. However, we cannot |
| 3620 | // do this if we are compiling with ASan or -fwritable-strings because they |
| 3621 | // rely on strings having normal linkage. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 3622 | if (!LangOpts.WritableStrings && |
| 3623 | !LangOpts.Sanitize.has(SanitizerKind::Address) && |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3624 | getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) { |
| 3625 | llvm::raw_svector_ostream Out(MangledNameBuffer); |
| 3626 | getCXXABI().getMangleContext().mangleStringLiteral(S, Out); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 3627 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3628 | LT = llvm::GlobalValue::LinkOnceODRLinkage; |
| 3629 | GlobalVariableName = MangledNameBuffer; |
| 3630 | } else { |
| 3631 | LT = llvm::GlobalValue::PrivateLinkage; |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3632 | GlobalVariableName = Name; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3635 | auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); |
| 3636 | if (Entry) |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3637 | *Entry = GV; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 3638 | |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 3639 | SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>", |
| 3640 | QualType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3641 | return ConstantAddress(GV, Alignment); |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 3644 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 3645 | /// array for the given ObjCEncodeExpr node. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3646 | ConstantAddress |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 3647 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 3648 | std::string Str; |
| 3649 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | 4663a33 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 3650 | |
| 3651 | return GetAddrOfConstantCString(Str); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Alexey Samsonov | adc9037 | 2014-06-04 20:25:57 +0000 | [diff] [blame] | 3654 | /// GetAddrOfConstantCString - Returns a pointer to a character array containing |
| 3655 | /// the literal and a terminating '\0' character. |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 3656 | /// The result has pointer to array type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3657 | ConstantAddress CodeGenModule::GetAddrOfConstantCString( |
| 3658 | const std::string &Str, const char *GlobalName) { |
Alexey Samsonov | adc9037 | 2014-06-04 20:25:57 +0000 | [diff] [blame] | 3659 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3660 | CharUnits Alignment = |
| 3661 | getContext().getAlignOfGlobalVarInChars(getContext().CharTy); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3662 | |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3663 | llvm::Constant *C = |
| 3664 | llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false); |
| 3665 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3666 | // Don't share any string literals if strings aren't constant. |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3667 | llvm::GlobalVariable **Entry = nullptr; |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3668 | if (!LangOpts.WritableStrings) { |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3669 | Entry = &ConstantStringMap[C]; |
| 3670 | if (auto GV = *Entry) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3671 | if (Alignment.getQuantity() > GV->getAlignment()) |
| 3672 | GV->setAlignment(Alignment.getQuantity()); |
| 3673 | return ConstantAddress(GV, Alignment); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 3677 | // Get the default prefix if a name wasn't specified. |
| 3678 | if (!GlobalName) |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 3679 | GlobalName = ".str"; |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 3680 | // Create a global variable for this. |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 3681 | auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, |
| 3682 | GlobalName, Alignment); |
| 3683 | if (Entry) |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 3684 | *Entry = GV; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3685 | return ConstantAddress(GV, Alignment); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 3686 | } |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 3687 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3688 | ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3689 | const MaterializeTemporaryExpr *E, const Expr *Init) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3690 | assert((E->getStorageDuration() == SD_Static || |
| 3691 | E->getStorageDuration() == SD_Thread) && "not a global temporary"); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3692 | const auto *VD = cast<VarDecl>(E->getExtendingDecl()); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3693 | |
| 3694 | // If we're not materializing a subobject of the temporary, keep the |
| 3695 | // cv-qualifiers from the type of the MaterializeTemporaryExpr. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3696 | QualType MaterializedType = Init->getType(); |
| 3697 | if (Init == E->GetTemporaryExpr()) |
| 3698 | MaterializedType = E->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3699 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3700 | CharUnits Align = getContext().getTypeAlignInChars(MaterializedType); |
| 3701 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 3702 | if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E]) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3703 | return ConstantAddress(Slot, Align); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3704 | |
| 3705 | // FIXME: If an externally-visible declaration extends multiple temporaries, |
| 3706 | // we need to give each temporary the same name in every translation unit (and |
| 3707 | // we also need to make the temporaries externally-visible). |
| 3708 | SmallString<256> Name; |
| 3709 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 3710 | getCXXABI().getMangleContext().mangleReferenceTemporary( |
| 3711 | VD, E->getManglingNumber(), Out); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3712 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3713 | APValue *Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3714 | if (E->getStorageDuration() == SD_Static) { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3715 | // We might have a cached constant initializer for this temporary. Note |
| 3716 | // that this might have a different value from the value computed by |
| 3717 | // evaluating the initializer if the surrounding constant expression |
| 3718 | // modifies the temporary. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3719 | Value = getContext().getMaterializedTemporaryValue(E, false); |
| 3720 | if (Value && Value->isUninit()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3721 | Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3724 | // Try evaluating it now, it might have a constant initializer. |
| 3725 | Expr::EvalResult EvalResult; |
| 3726 | if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && |
| 3727 | !EvalResult.hasSideEffects()) |
| 3728 | Value = &EvalResult.Val; |
| 3729 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3730 | llvm::Constant *InitialValue = nullptr; |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3731 | bool Constant = false; |
| 3732 | llvm::Type *Type; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3733 | if (Value) { |
| 3734 | // The temporary has a constant initializer, use it. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3735 | InitialValue = EmitConstantValue(*Value, MaterializedType, nullptr); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3736 | Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value); |
| 3737 | Type = InitialValue->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3738 | } else { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3739 | // No initializer, the initialization will be provided when we |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3740 | // initialize the declaration which performed lifetime extension. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 3741 | Type = getTypes().ConvertTypeForMem(MaterializedType); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | // Create a global variable for this lifetime-extended temporary. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3745 | llvm::GlobalValue::LinkageTypes Linkage = |
| 3746 | getLLVMLinkageVarDefinition(VD, Constant); |
David Majnemer | c9a9c7a | 2015-02-19 19:25:17 +0000 | [diff] [blame] | 3747 | if (Linkage == llvm::GlobalVariable::ExternalLinkage) { |
| 3748 | const VarDecl *InitVD; |
| 3749 | if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) && |
| 3750 | isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) { |
| 3751 | // Temporaries defined inside a class get linkonce_odr linkage because the |
| 3752 | // class can be defined in multipe translation units. |
| 3753 | Linkage = llvm::GlobalVariable::LinkOnceODRLinkage; |
| 3754 | } else { |
| 3755 | // There is no need for this temporary to have external linkage if the |
| 3756 | // VarDecl has external linkage. |
| 3757 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 3758 | } |
| 3759 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3760 | unsigned AddrSpace = |
| 3761 | VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace(); |
| 3762 | auto TargetAS = getContext().getTargetAddressSpace(AddrSpace); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3763 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3764 | getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3765 | /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); |
David Majnemer | 846c283 | 2014-04-29 06:18:53 +0000 | [diff] [blame] | 3766 | setGlobalVisibility(GV, VD); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3767 | GV->setAlignment(Align.getQuantity()); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 3768 | if (supportsCOMDAT() && GV->isWeakForLinker()) |
| 3769 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3770 | if (VD->getTLSKind()) |
| 3771 | setTLSMode(GV, *VD); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3772 | llvm::Constant *CV = GV; |
| 3773 | if (AddrSpace != LangAS::Default) |
| 3774 | CV = getTargetCodeGenInfo().performAddrSpaceCast( |
| 3775 | *this, GV, AddrSpace, LangAS::Default, |
| 3776 | Type->getPointerTo( |
| 3777 | getContext().getTargetAddressSpace(LangAS::Default))); |
| 3778 | MaterializedGlobalTemporaryMap[E] = CV; |
| 3779 | return ConstantAddress(CV, Align); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3782 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 3783 | /// properties for an implementation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3785 | ObjCImplementationDecl *D) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 3786 | for (const auto *PID : D->property_impls()) { |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3787 | // Dynamic is just for type-checking. |
| 3788 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 3789 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3790 | |
| 3791 | // Determine which methods need to be implemented, some may have |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3792 | // been overridden. Note that ::isPropertyAccessor is not the method |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3793 | // we want, that just indicates if the decl came from a |
| 3794 | // property. What we want to know is if the method is defined in |
| 3795 | // this implementation. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3796 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 3797 | CodeGenFunction(*this).GenerateObjCGetter( |
| 3798 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3799 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3800 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 3801 | CodeGenFunction(*this).GenerateObjCSetter( |
| 3802 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3803 | } |
| 3804 | } |
| 3805 | } |
| 3806 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 3807 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3808 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 3809 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 3810 | ivar; ivar = ivar->getNextIvar()) |
| 3811 | if (ivar->getType().isDestructedType()) |
| 3812 | return true; |
| 3813 | |
| 3814 | return false; |
| 3815 | } |
| 3816 | |
Fariborz Jahanian | 800821a | 2014-11-12 22:37:43 +0000 | [diff] [blame] | 3817 | static bool AllTrivialInitializers(CodeGenModule &CGM, |
| 3818 | ObjCImplementationDecl *D) { |
| 3819 | CodeGenFunction CGF(CGM); |
| 3820 | for (ObjCImplementationDecl::init_iterator B = D->init_begin(), |
| 3821 | E = D->init_end(); B != E; ++B) { |
| 3822 | CXXCtorInitializer *CtorInitExp = *B; |
| 3823 | Expr *Init = CtorInitExp->getInit(); |
| 3824 | if (!CGF.isTrivialInitializer(Init)) |
| 3825 | return false; |
| 3826 | } |
| 3827 | return true; |
| 3828 | } |
| 3829 | |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3830 | /// EmitObjCIvarInitializations - Emit information for ivar initialization |
| 3831 | /// for an implementation. |
| 3832 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 3833 | // We might need a .cxx_destruct even if we don't have any ivar initializers. |
| 3834 | if (needsDestructMethod(D)) { |
| 3835 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
| 3836 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
| 3837 | ObjCMethodDecl *DTORMethod = |
| 3838 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3839 | cxxSelector, getContext().VoidTy, nullptr, D, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 3840 | /*isInstance=*/true, /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3841 | /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 3842 | /*isDefined=*/false, ObjCMethodDecl::Required); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 3843 | D->addInstanceMethod(DTORMethod); |
| 3844 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3845 | D->setHasDestructors(true); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | // If the implementation doesn't have any ivar initializers, we don't need |
| 3849 | // a .cxx_construct. |
Fariborz Jahanian | 800821a | 2014-11-12 22:37:43 +0000 | [diff] [blame] | 3850 | if (D->getNumIvarInitializers() == 0 || |
| 3851 | AllTrivialInitializers(*this, D)) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3852 | return; |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3853 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 3854 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
| 3855 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3856 | // The constructor returns 'self'. |
| 3857 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
| 3858 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3859 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3860 | cxxSelector, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3861 | getContext().getObjCIdType(), |
| 3862 | nullptr, D, /*isInstance=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 3863 | /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3864 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 3865 | /*isImplicitlyDeclared=*/true, |
| 3866 | /*isDefined=*/false, |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3867 | ObjCMethodDecl::Required); |
| 3868 | D->addInstanceMethod(CTORMethod); |
| 3869 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3870 | D->setHasNonZeroConstructors(true); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 3873 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 3874 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | e480ce3 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 3875 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 3876 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 3877 | ErrorUnsupported(LSD, "linkage spec"); |
| 3878 | return; |
| 3879 | } |
| 3880 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 3881 | EmitDeclContext(LSD); |
| 3882 | } |
| 3883 | |
| 3884 | void CodeGenModule::EmitDeclContext(const DeclContext *DC) { |
| 3885 | for (auto *I : DC->decls()) { |
| 3886 | // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope |
| 3887 | // are themselves considered "top-level", so EmitTopLevelDecl on an |
| 3888 | // ObjCImplDecl does not recursively visit them. We need to do that in |
| 3889 | // case they're nested inside another construct (LinkageSpecDecl / |
| 3890 | // ExportDecl) that does stop them from being considered "top-level". |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3891 | if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 3892 | for (auto *M : OID->methods()) |
| 3893 | EmitTopLevelDecl(M); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 3894 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 3895 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3896 | EmitTopLevelDecl(I); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 3897 | } |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3900 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 3901 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3902 | // Ignore dependent declarations. |
| 3903 | if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) |
| 3904 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3906 | switch (D->getKind()) { |
Anders Carlsson | 6c0a6e4 | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 3907 | case Decl::CXXConversion: |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 3908 | case Decl::CXXMethod: |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3909 | case Decl::Function: |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3910 | // Skip function templates |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3911 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 3912 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3913 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3915 | EmitGlobal(cast<FunctionDecl>(D)); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 3916 | // Always provide some coverage mapping |
| 3917 | // even for the functions that aren't emitted. |
| 3918 | AddDeferredUnusedCoverageMapping(D); |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3919 | break; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3920 | |
Richard Smith | ada0888 | 2017-04-19 21:15:45 +0000 | [diff] [blame] | 3921 | case Decl::CXXDeductionGuide: |
| 3922 | // Function-like, but does not result in code emission. |
| 3923 | break; |
| 3924 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3925 | case Decl::Var: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 3926 | case Decl::Decomposition: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3927 | // Skip variable templates |
| 3928 | if (cast<VarDecl>(D)->getDescribedVarTemplate()) |
| 3929 | return; |
Galina Kistanova | 0872d6c | 2017-06-03 06:30:46 +0000 | [diff] [blame] | 3930 | LLVM_FALLTHROUGH; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3931 | case Decl::VarTemplateSpecialization: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3932 | EmitGlobal(cast<VarDecl>(D)); |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 3933 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) |
| 3934 | for (auto *B : DD->bindings()) |
| 3935 | if (auto *HD = B->getHoldingVar()) |
| 3936 | EmitGlobal(HD); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3937 | break; |
| 3938 | |
John McCall | 32f44bd | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 3939 | // Indirect fields from global anonymous structs and unions can be |
| 3940 | // ignored; only the actual variable requires IR gen support. |
| 3941 | case Decl::IndirectField: |
| 3942 | break; |
| 3943 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3944 | // C++ Decls |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3945 | case Decl::Namespace: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 3946 | EmitDeclContext(cast<NamespaceDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3947 | break; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 3948 | case Decl::CXXRecord: |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 3949 | if (DebugInfo) { |
| 3950 | if (auto *ES = D->getASTContext().getExternalSource()) |
| 3951 | if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) |
| 3952 | DebugInfo->completeUnusedClass(cast<CXXRecordDecl>(*D)); |
| 3953 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 3954 | // Emit any static data members, they may be definitions. |
| 3955 | for (auto *I : cast<CXXRecordDecl>(D)->decls()) |
| 3956 | if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I)) |
| 3957 | EmitTopLevelDecl(I); |
| 3958 | break; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3959 | // No code generation needed. |
John McCall | 1e9de05 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 3960 | case Decl::UsingShadow: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3961 | case Decl::ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3962 | case Decl::VarTemplate: |
| 3963 | case Decl::VarTemplatePartialSpecialization: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3964 | case Decl::FunctionTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3965 | case Decl::TypeAliasTemplate: |
Douglas Gregor | 0aa91e0 | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 3966 | case Decl::Block: |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 3967 | case Decl::Empty: |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3968 | break; |
David Blaikie | 3e65d36a | 2014-02-15 21:03:07 +0000 | [diff] [blame] | 3969 | case Decl::Using: // using X; [C++] |
| 3970 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 3971 | DI->EmitUsingDecl(cast<UsingDecl>(*D)); |
| 3972 | return; |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3973 | case Decl::NamespaceAlias: |
| 3974 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 3975 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); |
| 3976 | return; |
David Blaikie | 60a9fbf | 2013-04-26 05:41:06 +0000 | [diff] [blame] | 3977 | case Decl::UsingDirective: // using namespace X; [C++] |
| 3978 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 3979 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); |
| 3980 | return; |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3981 | case Decl::CXXConstructor: |
Anders Carlsson | 0ade971 | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 3982 | // Skip function templates |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3983 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 3984 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Anders Carlsson | 0ade971 | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 3985 | return; |
| 3986 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 3987 | getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3988 | break; |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 3989 | case Decl::CXXDestructor: |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3990 | if (cast<FunctionDecl>(D)->isLateTemplateParsed()) |
| 3991 | return; |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 3992 | getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 3993 | break; |
Anders Carlsson | 8783543 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 3994 | |
| 3995 | case Decl::StaticAssert: |
| 3996 | // Nothing to do. |
| 3997 | break; |
| 3998 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3999 | // Objective-C Decls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 4001 | // Forward declarations, no (immediate) code generation. |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 4002 | case Decl::ObjCInterface: |
Eric Christopher | f8378ca | 2012-07-19 22:22:55 +0000 | [diff] [blame] | 4003 | case Decl::ObjCCategory: |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 4004 | break; |
| 4005 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 4006 | case Decl::ObjCProtocol: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4007 | auto *Proto = cast<ObjCProtocolDecl>(D); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 4008 | if (Proto->isThisDeclarationADefinition()) |
| 4009 | ObjCRuntime->GenerateProtocol(Proto); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4010 | break; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4013 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4014 | // Categories have properties but don't support synthesize so we |
| 4015 | // can ignore them here. |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 4016 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4017 | break; |
| 4018 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4019 | case Decl::ObjCImplementation: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4020 | auto *OMD = cast<ObjCImplementationDecl>(D); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4021 | EmitObjCPropertyImplementations(OMD); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4022 | EmitObjCIvarInitializations(OMD); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 4023 | ObjCRuntime->GenerateClass(OMD); |
Eric Christopher | 3d19de9 | 2012-04-11 05:56:05 +0000 | [diff] [blame] | 4024 | // Emit global variable debug information. |
| 4025 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 4026 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
Alexey Samsonov | 9c1b9f6 | 2012-12-03 18:28:12 +0000 | [diff] [blame] | 4027 | DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( |
| 4028 | OMD->getClassInterface()), OMD->getLocation()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4029 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4031 | case Decl::ObjCMethod: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4032 | auto *OMD = cast<ObjCMethodDecl>(D); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4033 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4034 | if (OMD->getBody()) |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4035 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 4036 | break; |
| 4037 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | case Decl::ObjCCompatibleAlias: |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 4039 | ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4040 | break; |
| 4041 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4042 | case Decl::PragmaComment: { |
| 4043 | const auto *PCD = cast<PragmaCommentDecl>(D); |
| 4044 | switch (PCD->getCommentKind()) { |
| 4045 | case PCK_Unknown: |
| 4046 | llvm_unreachable("unexpected pragma comment kind"); |
| 4047 | case PCK_Linker: |
| 4048 | AppendLinkerOptions(PCD->getArg()); |
| 4049 | break; |
| 4050 | case PCK_Lib: |
| 4051 | AddDependentLib(PCD->getArg()); |
| 4052 | break; |
| 4053 | case PCK_Compiler: |
| 4054 | case PCK_ExeStr: |
| 4055 | case PCK_User: |
| 4056 | break; // We ignore all of these. |
| 4057 | } |
| 4058 | break; |
| 4059 | } |
| 4060 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 4061 | case Decl::PragmaDetectMismatch: { |
| 4062 | const auto *PDMD = cast<PragmaDetectMismatchDecl>(D); |
| 4063 | AddDetectMismatch(PDMD->getName(), PDMD->getValue()); |
| 4064 | break; |
| 4065 | } |
| 4066 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 4067 | case Decl::LinkageSpec: |
| 4068 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4069 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4070 | |
| 4071 | case Decl::FileScopeAsm: { |
Artem Belevich | 0488d1e | 2015-04-27 18:52:00 +0000 | [diff] [blame] | 4072 | // File-scope asm is ignored during device-side CUDA compilation. |
| 4073 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) |
| 4074 | break; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4075 | // File-scope asm is ignored during device-side OpenMP compilation. |
| 4076 | if (LangOpts.OpenMPIsDevice) |
| 4077 | break; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4078 | auto *AD = cast<FileScopeAsmDecl>(D); |
Joerg Sonnenberger | dfd511e | 2015-03-13 00:54:30 +0000 | [diff] [blame] | 4079 | getModule().appendModuleInlineAsm(AD->getAsmString()->getString()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4080 | break; |
| 4081 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4082 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4083 | case Decl::Import: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4084 | auto *Import = cast<ImportDecl>(D); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4085 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 4086 | // If we've already imported this module, we're done. |
| 4087 | if (!ImportedModules.insert(Import->getImportedModule())) |
Richard Smith | c52efa7 | 2015-08-19 02:30:28 +0000 | [diff] [blame] | 4088 | break; |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4089 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 4090 | // Emit debug information for direct imports. |
| 4091 | if (!Import->getImportedOwningModule()) { |
| 4092 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 4093 | DI->EmitImportDecl(*Import); |
| 4094 | } |
| 4095 | |
Manman Ren | 3b5dbf2 | 2016-10-14 18:55:44 +0000 | [diff] [blame] | 4096 | // Find all of the submodules and emit the module initializers. |
| 4097 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 4098 | SmallVector<clang::Module *, 16> Stack; |
| 4099 | Visited.insert(Import->getImportedModule()); |
| 4100 | Stack.push_back(Import->getImportedModule()); |
| 4101 | |
| 4102 | while (!Stack.empty()) { |
| 4103 | clang::Module *Mod = Stack.pop_back_val(); |
| 4104 | if (!EmittedModuleInitializers.insert(Mod).second) |
| 4105 | continue; |
| 4106 | |
| 4107 | for (auto *D : Context.getModuleInitializers(Mod)) |
| 4108 | EmitTopLevelDecl(D); |
| 4109 | |
| 4110 | // Visit the submodules of this module. |
| 4111 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 4112 | SubEnd = Mod->submodule_end(); |
| 4113 | Sub != SubEnd; ++Sub) { |
| 4114 | // Skip explicit children; they need to be explicitly imported to emit |
| 4115 | // the initializers. |
| 4116 | if ((*Sub)->IsExplicit) |
| 4117 | continue; |
| 4118 | |
| 4119 | if (Visited.insert(*Sub).second) |
| 4120 | Stack.push_back(*Sub); |
| 4121 | } |
| 4122 | } |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4123 | break; |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4126 | case Decl::Export: |
| 4127 | EmitDeclContext(cast<ExportDecl>(D)); |
| 4128 | break; |
| 4129 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4130 | case Decl::OMPThreadPrivate: |
| 4131 | EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D)); |
| 4132 | break; |
| 4133 | |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 4134 | case Decl::ClassTemplateSpecialization: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4135 | const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 4136 | if (DebugInfo && |
David Blaikie | 0317bc9 | 2014-12-16 23:49:18 +0000 | [diff] [blame] | 4137 | Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition && |
| 4138 | Spec->hasDefinition()) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 4139 | DebugInfo->completeTemplateDefinition(*Spec); |
Richard Smith | a634ff2 | 2014-12-01 18:59:10 +0000 | [diff] [blame] | 4140 | break; |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 4141 | } |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4142 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 4143 | case Decl::OMPDeclareReduction: |
| 4144 | EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D)); |
| 4145 | break; |
| 4146 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | default: |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4148 | // Make sure we handled everything we should, every other kind is a |
| 4149 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 4150 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4151 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
Richard Smith | a634ff2 | 2014-12-01 18:59:10 +0000 | [diff] [blame] | 4152 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4153 | } |
| 4154 | } |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4155 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4156 | void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { |
| 4157 | // Do we need to generate coverage mapping? |
| 4158 | if (!CodeGenOpts.CoverageMapping) |
| 4159 | return; |
| 4160 | switch (D->getKind()) { |
| 4161 | case Decl::CXXConversion: |
| 4162 | case Decl::CXXMethod: |
| 4163 | case Decl::Function: |
| 4164 | case Decl::ObjCMethod: |
| 4165 | case Decl::CXXConstructor: |
| 4166 | case Decl::CXXDestructor: { |
Justin Bogner | 203f092 | 2015-07-28 00:41:51 +0000 | [diff] [blame] | 4167 | if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4168 | return; |
| 4169 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
| 4170 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
| 4171 | DeferredEmptyCoverageMappingDecls[D] = true; |
| 4172 | break; |
| 4173 | } |
| 4174 | default: |
| 4175 | break; |
| 4176 | }; |
| 4177 | } |
| 4178 | |
| 4179 | void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) { |
| 4180 | // Do we need to generate coverage mapping? |
| 4181 | if (!CodeGenOpts.CoverageMapping) |
| 4182 | return; |
| 4183 | if (const auto *Fn = dyn_cast<FunctionDecl>(D)) { |
| 4184 | if (Fn->isTemplateInstantiation()) |
| 4185 | ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern()); |
| 4186 | } |
| 4187 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
| 4188 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
| 4189 | DeferredEmptyCoverageMappingDecls[D] = false; |
| 4190 | else |
| 4191 | I->second = false; |
| 4192 | } |
| 4193 | |
| 4194 | void CodeGenModule::EmitDeferredUnusedCoverageMappings() { |
Alex Lorenz | f2cf38e | 2014-08-08 23:41:24 +0000 | [diff] [blame] | 4195 | std::vector<const Decl *> DeferredDecls; |
Richard Trieu | b584133 | 2015-04-15 01:21:42 +0000 | [diff] [blame] | 4196 | for (const auto &I : DeferredEmptyCoverageMappingDecls) { |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4197 | if (!I.second) |
| 4198 | continue; |
Alex Lorenz | f2cf38e | 2014-08-08 23:41:24 +0000 | [diff] [blame] | 4199 | DeferredDecls.push_back(I.first); |
| 4200 | } |
| 4201 | // Sort the declarations by their location to make sure that the tests get a |
| 4202 | // predictable order for the coverage mapping for the unused declarations. |
| 4203 | if (CodeGenOpts.DumpCoverageMapping) |
| 4204 | std::sort(DeferredDecls.begin(), DeferredDecls.end(), |
| 4205 | [] (const Decl *LHS, const Decl *RHS) { |
| 4206 | return LHS->getLocStart() < RHS->getLocStart(); |
| 4207 | }); |
| 4208 | for (const auto *D : DeferredDecls) { |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4209 | switch (D->getKind()) { |
| 4210 | case Decl::CXXConversion: |
| 4211 | case Decl::CXXMethod: |
| 4212 | case Decl::Function: |
| 4213 | case Decl::ObjCMethod: { |
| 4214 | CodeGenPGO PGO(*this); |
| 4215 | GlobalDecl GD(cast<FunctionDecl>(D)); |
| 4216 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 4217 | getFunctionLinkage(GD)); |
| 4218 | break; |
| 4219 | } |
| 4220 | case Decl::CXXConstructor: { |
| 4221 | CodeGenPGO PGO(*this); |
| 4222 | GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base); |
| 4223 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 4224 | getFunctionLinkage(GD)); |
| 4225 | break; |
| 4226 | } |
| 4227 | case Decl::CXXDestructor: { |
| 4228 | CodeGenPGO PGO(*this); |
| 4229 | GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base); |
| 4230 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 4231 | getFunctionLinkage(GD)); |
| 4232 | break; |
| 4233 | } |
| 4234 | default: |
| 4235 | break; |
| 4236 | }; |
| 4237 | } |
| 4238 | } |
| 4239 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4240 | /// Turns the given pointer into a constant. |
| 4241 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
| 4242 | const void *Ptr) { |
| 4243 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4244 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4245 | return llvm::ConstantInt::get(i64, PtrInt); |
| 4246 | } |
| 4247 | |
| 4248 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
| 4249 | llvm::NamedMDNode *&GlobalMetadata, |
| 4250 | GlobalDecl D, |
| 4251 | llvm::GlobalValue *Addr) { |
| 4252 | if (!GlobalMetadata) |
| 4253 | GlobalMetadata = |
| 4254 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
| 4255 | |
| 4256 | // TODO: should we report variant information for ctors/dtors? |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4257 | llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr), |
| 4258 | llvm::ConstantAsMetadata::get(GetPointerConstant( |
| 4259 | CGM.getLLVMContext(), D.getDecl()))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 4260 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 4263 | /// For each function which is declared within an extern "C" region and marked |
| 4264 | /// as 'used', but has internal linkage, create an alias from the unmangled |
| 4265 | /// name to the mangled name if possible. People expect to be able to refer |
| 4266 | /// to such functions with an unmangled name from inline assembly within the |
| 4267 | /// same translation unit. |
| 4268 | void CodeGenModule::EmitStaticExternCAliases() { |
Justin Lebar | cd2f6bb | 2016-01-25 22:36:37 +0000 | [diff] [blame] | 4269 | // Don't do anything if we're generating CUDA device code -- the NVPTX |
| 4270 | // assembly target doesn't support aliases. |
| 4271 | if (Context.getTargetInfo().getTriple().isNVPTX()) |
| 4272 | return; |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 4273 | for (auto &I : StaticExternCValues) { |
| 4274 | IdentifierInfo *Name = I.first; |
| 4275 | llvm::GlobalValue *Val = I.second; |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 4276 | if (Val && !getModule().getNamedValue(Name->getName())) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 4277 | addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 4278 | } |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 4281 | bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName, |
| 4282 | GlobalDecl &Result) const { |
| 4283 | auto Res = Manglings.find(MangledName); |
| 4284 | if (Res == Manglings.end()) |
| 4285 | return false; |
| 4286 | Result = Res->getValue(); |
| 4287 | return true; |
| 4288 | } |
| 4289 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4290 | /// Emits metadata nodes associating all the global values in the |
| 4291 | /// current module with the Decls they came from. This is useful for |
| 4292 | /// projects using IR gen as a subroutine. |
| 4293 | /// |
| 4294 | /// Since there's currently no way to associate an MDNode directly |
| 4295 | /// with an llvm::GlobalValue, we create a global named metadata |
| 4296 | /// with the name 'clang.global.decl.ptrs'. |
| 4297 | void CodeGenModule::EmitDeclMetadata() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4298 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4299 | |
Robert Lytton | 5b88f78f | 2014-07-03 09:30:29 +0000 | [diff] [blame] | 4300 | for (auto &I : MangledDeclNames) { |
| 4301 | llvm::GlobalValue *Addr = getModule().getNamedValue(I.second); |
Keno Fischer | 0e2d422 | 2015-11-05 23:18:44 +0000 | [diff] [blame] | 4302 | // Some mangled names don't necessarily have an associated GlobalValue |
| 4303 | // in this module, e.g. if we mangled it for DebugInfo. |
| 4304 | if (Addr) |
| 4305 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | /// Emits metadata nodes for all the local variables in the current |
| 4310 | /// function. |
| 4311 | void CodeGenFunction::EmitDeclMetadata() { |
| 4312 | if (LocalDeclMap.empty()) return; |
| 4313 | |
| 4314 | llvm::LLVMContext &Context = getLLVMContext(); |
| 4315 | |
| 4316 | // Find the unique metadata ID for this name. |
| 4317 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
| 4318 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4319 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4320 | |
Robert Lytton | 5b88f78f | 2014-07-03 09:30:29 +0000 | [diff] [blame] | 4321 | for (auto &I : LocalDeclMap) { |
| 4322 | const Decl *D = I.first; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4323 | llvm::Value *Addr = I.second.getPointer(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4324 | if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4325 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4326 | Alloca->setMetadata( |
| 4327 | DeclPtrKind, llvm::MDNode::get( |
| 4328 | Context, llvm::ValueAsMetadata::getConstant(DAddr))); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4329 | } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4330 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
| 4331 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
| 4332 | } |
| 4333 | } |
| 4334 | } |
Daniel Dunbar | 900546d | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 4335 | |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 4336 | void CodeGenModule::EmitVersionIdentMetadata() { |
| 4337 | llvm::NamedMDNode *IdentMetadata = |
| 4338 | TheModule.getOrInsertNamedMetadata("llvm.ident"); |
| 4339 | std::string Version = getClangFullVersion(); |
| 4340 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 4341 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4342 | llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)}; |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 4343 | IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); |
| 4344 | } |
| 4345 | |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 4346 | void CodeGenModule::EmitTargetMetadata() { |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 4347 | // Warning, new MangledDeclNames may be appended within this loop. |
| 4348 | // We rely on MapVector insertions adding new elements to the end |
| 4349 | // of the container. |
| 4350 | // FIXME: Move this loop into the one target that needs it, and only |
| 4351 | // loop over those declarations for which we couldn't emit the target |
| 4352 | // metadata when we emitted the declaration. |
| 4353 | for (unsigned I = 0; I != MangledDeclNames.size(); ++I) { |
Richard Smith | 570706d | 2014-08-01 22:17:28 +0000 | [diff] [blame] | 4354 | auto Val = *(MangledDeclNames.begin() + I); |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 4355 | const Decl *D = Val.first.getDecl()->getMostRecentDecl(); |
| 4356 | llvm::GlobalValue *GV = GetGlobalValue(Val.second); |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 4357 | getTargetCodeGenInfo().emitTargetMD(D, GV, *this); |
| 4358 | } |
| 4359 | } |
| 4360 | |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 4361 | void CodeGenModule::EmitCoverageFile() { |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 4362 | if (getCodeGenOpts().CoverageDataFile.empty() && |
| 4363 | getCodeGenOpts().CoverageNotesFile.empty()) |
| 4364 | return; |
| 4365 | |
| 4366 | llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu"); |
| 4367 | if (!CUNode) |
| 4368 | return; |
| 4369 | |
| 4370 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
| 4371 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 4372 | auto *CoverageDataFile = |
| 4373 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile); |
| 4374 | auto *CoverageNotesFile = |
| 4375 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile); |
| 4376 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
| 4377 | llvm::MDNode *CU = CUNode->getOperand(i); |
| 4378 | llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU}; |
| 4379 | GCov->addOperand(llvm::MDNode::get(Ctx, Elts)); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4382 | |
Nico Weber | 17d3a2c | 2014-09-08 16:26:36 +0000 | [diff] [blame] | 4383 | llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) { |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4384 | // Sema has checked that all uuid strings are of the form |
| 4385 | // "12345678-1234-1234-1234-1234567890ab". |
| 4386 | assert(Uuid.size() == 36); |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 4387 | for (unsigned i = 0; i < 36; ++i) { |
| 4388 | if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-'); |
| 4389 | else assert(isHexDigit(Uuid[i])); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4390 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4391 | |
Nico Weber | dfa274e | 2014-09-08 16:11:15 +0000 | [diff] [blame] | 4392 | // The starts of all bytes of Field3 in Uuid. Field 3 is "1234-1234567890ab". |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 4393 | const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 }; |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4394 | |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 4395 | llvm::Constant *Field3[8]; |
| 4396 | for (unsigned Idx = 0; Idx < 8; ++Idx) |
| 4397 | Field3[Idx] = llvm::ConstantInt::get( |
| 4398 | Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16); |
| 4399 | |
| 4400 | llvm::Constant *Fields[4] = { |
| 4401 | llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16), |
| 4402 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16), |
| 4403 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16), |
| 4404 | llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3) |
| 4405 | }; |
| 4406 | |
| 4407 | return llvm::ConstantStruct::getAnon(Fields); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4408 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 4409 | |
| 4410 | llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, |
| 4411 | bool ForEH) { |
| 4412 | // Return a bogus pointer if RTTI is disabled, unless it's for EH. |
| 4413 | // FIXME: should we even be calling this method if RTTI is disabled |
| 4414 | // and it's not for EH? |
| 4415 | if (!ForEH && !getLangOpts().RTTI) |
| 4416 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 4417 | |
| 4418 | if (ForEH && Ty->isObjCObjectPointerType() && |
| 4419 | LangOpts.ObjCRuntime.isGNUFamily()) |
| 4420 | return ObjCRuntime->GetEHType(Ty); |
| 4421 | |
David Majnemer | 443250f | 2015-03-17 20:35:00 +0000 | [diff] [blame] | 4422 | return getCXXABI().getAddrOfRTTIDescriptor(Ty); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4425 | void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { |
| 4426 | for (auto RefExpr : D->varlists()) { |
| 4427 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl()); |
| 4428 | bool PerformInit = |
| 4429 | VD->getAnyInitializer() && |
| 4430 | !VD->getAnyInitializer()->isConstantInitializer(getContext(), |
| 4431 | /*ForRef=*/false); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4432 | |
| 4433 | Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD)); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 4434 | if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4435 | VD, Addr, RefExpr->getLocStart(), PerformInit)) |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4436 | CXXGlobalInits.push_back(InitFunction); |
| 4437 | } |
| 4438 | } |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 4439 | |
Peter Collingbourne | 25a80bf | 2015-09-08 23:01:30 +0000 | [diff] [blame] | 4440 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { |
| 4441 | llvm::Metadata *&InternalId = MetadataIdMap[T.getCanonicalType()]; |
| 4442 | if (InternalId) |
| 4443 | return InternalId; |
| 4444 | |
| 4445 | if (isExternallyVisible(T->getLinkage())) { |
| 4446 | std::string OutName; |
| 4447 | llvm::raw_string_ostream Out(OutName); |
| 4448 | getCXXABI().getMangleContext().mangleTypeName(T, Out); |
| 4449 | |
| 4450 | InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); |
| 4451 | } else { |
| 4452 | InternalId = llvm::MDNode::getDistinct(getLLVMContext(), |
| 4453 | llvm::ArrayRef<llvm::Metadata *>()); |
| 4454 | } |
| 4455 | |
| 4456 | return InternalId; |
| 4457 | } |
| 4458 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4459 | /// Returns whether this module needs the "all-vtables" type identifier. |
| 4460 | bool CodeGenModule::NeedAllVtablesTypeId() const { |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4461 | // Returns true if at least one of vtable-based CFI checkers is enabled and |
| 4462 | // is not in the trapping mode. |
| 4463 | return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) && |
| 4464 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) || |
| 4465 | (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) && |
| 4466 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) || |
| 4467 | (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) && |
| 4468 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) || |
| 4469 | (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) && |
| 4470 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast))); |
| 4471 | } |
| 4472 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4473 | void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable, |
| 4474 | CharUnits Offset, |
| 4475 | const CXXRecordDecl *RD) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4476 | llvm::Metadata *MD = |
| 4477 | CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4478 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4479 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4480 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 4481 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
| 4482 | VTable->addTypeMetadata(Offset.getQuantity(), |
| 4483 | llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4484 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4485 | if (NeedAllVtablesTypeId()) { |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4486 | llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables"); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4487 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4488 | } |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 4489 | } |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 4490 | |
| 4491 | // Fills in the supplied string map with the set of target features for the |
| 4492 | // passed in function. |
| 4493 | void CodeGenModule::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
| 4494 | const FunctionDecl *FD) { |
| 4495 | StringRef TargetCPU = Target.getTargetOpts().CPU; |
| 4496 | if (const auto *TD = FD->getAttr<TargetAttr>()) { |
| 4497 | // If we have a TargetAttr build up the feature map based on that. |
| 4498 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
| 4499 | |
| 4500 | // Make a copy of the features as passed on the command line into the |
| 4501 | // beginning of the additional features from the function to override. |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame^] | 4502 | ParsedAttr.Features.insert(ParsedAttr.Features.begin(), |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 4503 | Target.getTargetOpts().FeaturesAsWritten.begin(), |
| 4504 | Target.getTargetOpts().FeaturesAsWritten.end()); |
| 4505 | |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame^] | 4506 | if (ParsedAttr.Architecture != "") |
| 4507 | TargetCPU = ParsedAttr.Architecture ; |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 4508 | |
| 4509 | // Now populate the feature map, first with the TargetCPU which is either |
| 4510 | // the default or a new one from the target attribute string. Then we'll use |
| 4511 | // the passed in features (FeaturesAsWritten) along with the new ones from |
| 4512 | // the attribute. |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame^] | 4513 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
| 4514 | ParsedAttr.Features); |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 4515 | } else { |
| 4516 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
| 4517 | Target.getTargetOpts().Features); |
| 4518 | } |
| 4519 | } |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 4520 | |
| 4521 | llvm::SanitizerStatReport &CodeGenModule::getSanStats() { |
| 4522 | if (!SanStats) |
| 4523 | SanStats = llvm::make_unique<llvm::SanitizerStatReport>(&getModule()); |
| 4524 | |
| 4525 | return *SanStats; |
| 4526 | } |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 4527 | llvm::Value * |
| 4528 | CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, |
| 4529 | CodeGenFunction &CGF) { |
| 4530 | llvm::Constant *C = EmitConstantExpr(E, E->getType(), &CGF); |
| 4531 | auto SamplerT = getOpenCLRuntime().getSamplerType(); |
| 4532 | auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); |
| 4533 | return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy, |
| 4534 | "__translate_sampler_initializer"), |
| 4535 | {C}); |
| 4536 | } |