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" |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 26 | #include "ConstantEmitter.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 | |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 63 | static llvm::cl::opt<bool> LimitedCoverage( |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 64 | "limited-coverage-experimental", llvm::cl::ZeroOrMore, llvm::cl::Hidden, |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 65 | llvm::cl::desc("Emit limited coverage mapping information (experimental)"), |
| 66 | llvm::cl::init(false)); |
| 67 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 68 | static const char AnnotationSection[] = "llvm.metadata"; |
| 69 | |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 70 | static CGCXXABI *createCXXABI(CodeGenModule &CGM) { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 71 | switch (CGM.getTarget().getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 72 | case TargetCXXABI::GenericAArch64: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 73 | case TargetCXXABI::GenericARM: |
| 74 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 75 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 76 | case TargetCXXABI::WatchOS: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 77 | case TargetCXXABI::GenericMIPS: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 78 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 79 | case TargetCXXABI::WebAssembly: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 80 | return CreateItaniumCXXABI(CGM); |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 81 | case TargetCXXABI::Microsoft: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 82 | return CreateMicrosoftCXXABI(CGM); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | llvm_unreachable("invalid C++ ABI kind"); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 88 | CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO, |
| 89 | const PreprocessorOptions &PPO, |
| 90 | const CodeGenOptions &CGO, llvm::Module &M, |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 91 | DiagnosticsEngine &diags, |
| 92 | CoverageSourceInfo *CoverageInfo) |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 93 | : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO), |
| 94 | PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 95 | Target(C.getTargetInfo()), ABI(createCXXABI(*this)), |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 96 | VMContext(M.getContext()), Types(*this), VTables(*this), |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 97 | SanitizerMD(new SanitizerMetadata(*this)) { |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 98 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 99 | // Initialize the type cache. |
| 100 | llvm::LLVMContext &LLVMContext = M.getContext(); |
| 101 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
| 102 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
| 103 | Int16Ty = llvm::Type::getInt16Ty(LLVMContext); |
| 104 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 105 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
Abderrazek Zaafrani | f58a132 | 2017-12-21 19:20:01 +0000 | [diff] [blame] | 106 | HalfTy = llvm::Type::getHalfTy(LLVMContext); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 107 | FloatTy = llvm::Type::getFloatTy(LLVMContext); |
| 108 | DoubleTy = llvm::Type::getDoubleTy(LLVMContext); |
| 109 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
| 110 | PointerAlignInBytes = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 111 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
Yaxun Liu | 26f7566 | 2016-08-19 05:17:25 +0000 | [diff] [blame] | 112 | SizeSizeInBytes = |
| 113 | C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 114 | IntAlignInBytes = |
| 115 | C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 116 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
Yaxun Liu | 26f7566 | 2016-08-19 05:17:25 +0000 | [diff] [blame] | 117 | IntPtrTy = llvm::IntegerType::get(LLVMContext, |
| 118 | C.getTargetInfo().getMaxPointerWidth()); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 119 | Int8PtrTy = Int8Ty->getPointerTo(0); |
| 120 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 121 | AllocaInt8PtrTy = Int8Ty->getPointerTo( |
| 122 | M.getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 123 | ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 124 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 125 | RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); |
| 126 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 127 | if (LangOpts.ObjC1) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 128 | createObjCRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 129 | if (LangOpts.OpenCL) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 130 | createOpenCLRuntime(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 131 | if (LangOpts.OpenMP) |
| 132 | createOpenMPRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 133 | if (LangOpts.CUDA) |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 134 | createCUDARuntime(); |
Sanjiv Gupta | 15cb669 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 135 | |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 136 | // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 137 | if (LangOpts.Sanitize.has(SanitizerKind::Thread) || |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 138 | (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 139 | TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(), |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 140 | getCXXABI().getMangleContext())); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 141 | |
Nick Lewycky | 207bce3 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 142 | // If debug info or coverage generation is enabled, create the CGDebugInfo |
| 143 | // object. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 144 | if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo || |
| 145 | CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 146 | DebugInfo.reset(new CGDebugInfo(*this)); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 147 | |
| 148 | Block.GlobalUniqueCount = 0; |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 149 | |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 150 | if (C.getLangOpts().ObjC1) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 151 | ObjCData.reset(new ObjCEntrypoints()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 152 | |
Rong Xu | 9c6f153 | 2016-03-02 20:59:36 +0000 | [diff] [blame] | 153 | if (CodeGenOpts.hasProfileClangUse()) { |
| 154 | auto ReaderOrErr = llvm::IndexedInstrProfReader::create( |
| 155 | CodeGenOpts.ProfileInstrumentUsePath); |
Vedant Kumar | fa2d595 | 2016-05-19 03:54:54 +0000 | [diff] [blame] | 156 | if (auto E = ReaderOrErr.takeError()) { |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 157 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
Diego Novillo | 5aecacb | 2015-06-25 22:56:00 +0000 | [diff] [blame] | 158 | "Could not read profile %0: %1"); |
Vedant Kumar | fa2d595 | 2016-05-19 03:54:54 +0000 | [diff] [blame] | 159 | llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) { |
| 160 | getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath |
| 161 | << EI.message(); |
| 162 | }); |
Nico Weber | 176efac | 2015-04-06 04:16:48 +0000 | [diff] [blame] | 163 | } else |
| 164 | PGOReader = std::move(ReaderOrErr.get()); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 165 | } |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 166 | |
| 167 | // If coverage mapping generation is enabled, create the |
| 168 | // CoverageMappingModuleGen object. |
| 169 | if (CodeGenOpts.CoverageMapping) |
| 170 | CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo)); |
Chris Lattner | a087ff9 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 173 | CodeGenModule::~CodeGenModule() {} |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 174 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 175 | void CodeGenModule::createObjCRuntime() { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 176 | // This is just isGNUFamily(), but we want to force implementors of |
| 177 | // new ABIs to decide how best to do this. |
| 178 | switch (LangOpts.ObjCRuntime.getKind()) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 179 | case ObjCRuntime::GNUstep: |
| 180 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 181 | case ObjCRuntime::ObjFW: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 182 | ObjCRuntime.reset(CreateGNUObjCRuntime(*this)); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 183 | return; |
| 184 | |
| 185 | case ObjCRuntime::FragileMacOSX: |
| 186 | case ObjCRuntime::MacOSX: |
| 187 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 188 | case ObjCRuntime::WatchOS: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 189 | ObjCRuntime.reset(CreateMacObjCRuntime(*this)); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | llvm_unreachable("bad runtime kind"); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 195 | void CodeGenModule::createOpenCLRuntime() { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 196 | OpenCLRuntime.reset(new CGOpenCLRuntime(*this)); |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 199 | void CodeGenModule::createOpenMPRuntime() { |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 200 | // Select a specialized code generation class based on the target, if any. |
| 201 | // If it does not exist use the default implementation. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 202 | switch (getTriple().getArch()) { |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 203 | case llvm::Triple::nvptx: |
| 204 | case llvm::Triple::nvptx64: |
| 205 | assert(getLangOpts().OpenMPIsDevice && |
| 206 | "OpenMP NVPTX is only prepared to deal with device code."); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 207 | OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this)); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 208 | break; |
| 209 | default: |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 210 | if (LangOpts.OpenMPSimd) |
| 211 | OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this)); |
| 212 | else |
| 213 | OpenMPRuntime.reset(new CGOpenMPRuntime(*this)); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 218 | void CodeGenModule::createCUDARuntime() { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 219 | CUDARuntime.reset(CreateNVCUDARuntime(*this)); |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 222 | void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) { |
| 223 | Replacements[Name] = C; |
| 224 | } |
| 225 | |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 226 | void CodeGenModule::applyReplacements() { |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 227 | for (auto &I : Replacements) { |
| 228 | StringRef MangledName = I.first(); |
| 229 | llvm::Constant *Replacement = I.second; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 230 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 231 | if (!Entry) |
| 232 | continue; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 233 | auto *OldF = cast<llvm::Function>(Entry); |
| 234 | auto *NewF = dyn_cast<llvm::Function>(Replacement); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 235 | if (!NewF) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 236 | if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) { |
Rafael Espindola | 0fcad88 | 2014-05-16 22:20:18 +0000 | [diff] [blame] | 237 | NewF = dyn_cast<llvm::Function>(Alias->getAliasee()); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 238 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 239 | auto *CE = cast<llvm::ConstantExpr>(Replacement); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 240 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 241 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
| 242 | NewF = dyn_cast<llvm::Function>(CE->getOperand(0)); |
| 243 | } |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Replace old with new, but keep the old order. |
| 247 | OldF->replaceAllUsesWith(Replacement); |
| 248 | if (NewF) { |
| 249 | NewF->removeFromParent(); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 250 | OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(), |
| 251 | NewF); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 252 | } |
| 253 | OldF->eraseFromParent(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 257 | void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) { |
| 258 | GlobalValReplacements.push_back(std::make_pair(GV, C)); |
| 259 | } |
| 260 | |
| 261 | void CodeGenModule::applyGlobalValReplacements() { |
| 262 | for (auto &I : GlobalValReplacements) { |
| 263 | llvm::GlobalValue *GV = I.first; |
| 264 | llvm::Constant *C = I.second; |
| 265 | |
| 266 | GV->replaceAllUsesWith(C); |
| 267 | GV->eraseFromParent(); |
| 268 | } |
| 269 | } |
| 270 | |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 271 | // This is only used in aliases that we created and we know they have a |
| 272 | // linear structure. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 273 | static const llvm::GlobalObject *getAliasedGlobal( |
| 274 | const llvm::GlobalIndirectSymbol &GIS) { |
| 275 | llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited; |
| 276 | const llvm::Constant *C = &GIS; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 277 | for (;;) { |
Rafael Espindola | e11f40a | 2014-06-04 19:03:20 +0000 | [diff] [blame] | 278 | C = C->stripPointerCasts(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 279 | if (auto *GO = dyn_cast<llvm::GlobalObject>(C)) |
| 280 | return GO; |
| 281 | // stripPointerCasts will not walk over weak aliases. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 282 | auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C); |
| 283 | if (!GIS2) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 284 | return nullptr; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 285 | if (!Visited.insert(GIS2).second) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 286 | return nullptr; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 287 | C = GIS2->getIndirectSymbol(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 291 | void CodeGenModule::checkAliases() { |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 292 | // Check if the constructed aliases are well formed. It is really unfortunate |
| 293 | // that we have to do this in CodeGen, but we only construct mangled names |
| 294 | // and aliases during codegen. |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 295 | bool Error = false; |
Rafael Espindola | c67b815 | 2014-05-05 19:33:09 +0000 | [diff] [blame] | 296 | DiagnosticsEngine &Diags = getDiags(); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 297 | for (const GlobalDecl &GD : Aliases) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 298 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 299 | SourceLocation Location; |
| 300 | bool IsIFunc = D->hasAttr<IFuncAttr>(); |
| 301 | if (const Attr *A = D->getDefiningAttr()) |
| 302 | Location = A->getLocation(); |
| 303 | else |
| 304 | llvm_unreachable("Not an alias or ifunc?"); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 305 | StringRef MangledName = getMangledName(GD); |
| 306 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 307 | auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 308 | const llvm::GlobalValue *GV = getAliasedGlobal(*Alias); |
| 309 | if (!GV) { |
| 310 | Error = true; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 311 | Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 312 | } else if (GV->isDeclaration()) { |
Rafael Espindola | 93c7a65 | 2014-03-13 15:47:50 +0000 | [diff] [blame] | 313 | Error = true; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 314 | Diags.Report(Location, diag::err_alias_to_undefined) |
| 315 | << IsIFunc << IsIFunc; |
| 316 | } else if (IsIFunc) { |
| 317 | // Check resolver function type. |
| 318 | llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>( |
| 319 | GV->getType()->getPointerElementType()); |
| 320 | assert(FTy); |
| 321 | if (!FTy->getReturnType()->isPointerTy()) |
| 322 | Diags.Report(Location, diag::err_ifunc_resolver_return); |
| 323 | if (FTy->getNumParams()) |
| 324 | Diags.Report(Location, diag::err_ifunc_resolver_params); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 325 | } |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 326 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 327 | llvm::Constant *Aliasee = Alias->getIndirectSymbol(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 328 | llvm::GlobalValue *AliaseeGV; |
| 329 | if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee)) |
| 330 | AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0)); |
| 331 | else |
| 332 | AliaseeGV = cast<llvm::GlobalValue>(Aliasee); |
| 333 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 334 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 335 | StringRef AliasSection = SA->getName(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 336 | if (AliasSection != AliaseeGV->getSection()) |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 337 | Diags.Report(SA->getLocation(), diag::warn_alias_with_section) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 338 | << AliasSection << IsIFunc << IsIFunc; |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 339 | } |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 340 | |
| 341 | // We have to handle alias to weak aliases in here. LLVM itself disallows |
| 342 | // this since the object semantics would not match the IL one. For |
| 343 | // compatibility with gcc we implement it by just pointing the alias |
| 344 | // to its aliasee's aliasee. We also warn, since the user is probably |
| 345 | // expecting the link to be weak. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 346 | if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) { |
Sanjoy Das | f60a0d7 | 2016-04-08 01:31:02 +0000 | [diff] [blame] | 347 | if (GA->isInterposable()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 348 | Diags.Report(Location, diag::warn_alias_to_weak_alias) |
| 349 | << GV->getName() << GA->getName() << IsIFunc; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 350 | Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 351 | GA->getIndirectSymbol(), Alias->getType()); |
| 352 | Alias->setIndirectSymbol(Aliasee); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 355 | } |
| 356 | if (!Error) |
| 357 | return; |
| 358 | |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 359 | for (const GlobalDecl &GD : Aliases) { |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 360 | StringRef MangledName = getMangledName(GD); |
| 361 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 362 | auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 363 | Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType())); |
| 364 | Alias->eraseFromParent(); |
| 365 | } |
| 366 | } |
| 367 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 368 | void CodeGenModule::clear() { |
| 369 | DeferredDeclsToEmit.clear(); |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 370 | if (OpenMPRuntime) |
| 371 | OpenMPRuntime->clear(); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 374 | void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags, |
| 375 | StringRef MainFile) { |
| 376 | if (!hasDiagnostics()) |
| 377 | return; |
| 378 | if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) { |
| 379 | if (MainFile.empty()) |
| 380 | MainFile = "<stdin>"; |
| 381 | Diags.Report(diag::warn_profile_data_unprofiled) << MainFile; |
Vedant Kumar | 96d6ca7 | 2017-04-27 17:30:58 +0000 | [diff] [blame] | 382 | } else { |
| 383 | if (Mismatched > 0) |
| 384 | Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched; |
| 385 | |
| 386 | if (Missing > 0) |
| 387 | Diags.Report(diag::warn_profile_data_missing) << Visited << Missing; |
| 388 | } |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 391 | void CodeGenModule::Release() { |
Chris Lattner | a5ae54a | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 392 | EmitDeferred(); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 393 | EmitVTablesOpportunistically(); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 394 | applyGlobalValReplacements(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 395 | applyReplacements(); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 396 | checkAliases(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 397 | emitMultiVersionFunctions(); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 398 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | fe06df4 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 399 | EmitCXXGlobalDtorFunc(); |
Akira Hatanaka | 617e261 | 2018-04-17 18:41:52 +0000 | [diff] [blame] | 400 | registerGlobalDtorsWithAtExit(); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 401 | EmitCXXThreadLocalInitFunc(); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 402 | if (ObjCRuntime) |
| 403 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 404 | AddGlobalCtor(ObjCInitFunction); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 405 | if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice && |
| 406 | CUDARuntime) { |
| 407 | if (llvm::Function *CudaCtorFunction = CUDARuntime->makeModuleCtorFunction()) |
| 408 | AddGlobalCtor(CudaCtorFunction); |
| 409 | if (llvm::Function *CudaDtorFunction = CUDARuntime->makeModuleDtorFunction()) |
| 410 | AddGlobalDtor(CudaDtorFunction); |
| 411 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 412 | if (OpenMPRuntime) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 413 | if (llvm::Function *OpenMPRegistrationFunction = |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 414 | OpenMPRuntime->emitRegistrationFunction()) { |
| 415 | auto ComdatKey = OpenMPRegistrationFunction->hasComdat() ? |
| 416 | OpenMPRegistrationFunction : nullptr; |
| 417 | AddGlobalCtor(OpenMPRegistrationFunction, 0, ComdatKey); |
| 418 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 419 | OpenMPRuntime->clear(); |
| 420 | } |
Easwaran Raman | 695890c | 2015-12-17 19:14:27 +0000 | [diff] [blame] | 421 | if (PGOReader) { |
Easwaran Raman | 8160812 | 2016-03-24 21:32:25 +0000 | [diff] [blame] | 422 | getModule().setProfileSummary(PGOReader->getSummary().getMD(VMContext)); |
Easwaran Raman | 695890c | 2015-12-17 19:14:27 +0000 | [diff] [blame] | 423 | if (PGOStats.hasDiagnostics()) |
| 424 | PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName); |
| 425 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 426 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 427 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 428 | EmitGlobalAnnotations(); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 429 | EmitStaticExternCAliases(); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 430 | EmitDeferredUnusedCoverageMappings(); |
| 431 | if (CoverageMapping) |
| 432 | CoverageMapping->emit(); |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 433 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 434 | CodeGenFunction(*this).EmitCfiCheckFail(); |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 435 | CodeGenFunction(*this).EmitCfiCheckStub(); |
| 436 | } |
Alex Lorenz | a8fbef4 | 2017-03-23 11:14:27 +0000 | [diff] [blame] | 437 | emitAtAvailableLinkGuard(); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 438 | emitLLVMUsed(); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 439 | if (SanStats) |
| 440 | SanStats->finish(); |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 441 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 442 | if (CodeGenOpts.Autolink && |
| 443 | (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 444 | EmitModuleLinkOptions(); |
| 445 | } |
Nirav Dave | 8497ef4 | 2017-03-18 00:43:39 +0000 | [diff] [blame] | 446 | |
Nirav Dave | 741dea0 | 2017-03-30 13:41:44 +0000 | [diff] [blame] | 447 | // Record mregparm value now so it is visible through rest of codegen. |
| 448 | if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 449 | getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters", |
| 450 | CodeGenOpts.NumRegisterParameters); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 451 | |
Reid Kleckner | 124955a | 2015-08-05 18:51:13 +0000 | [diff] [blame] | 452 | if (CodeGenOpts.DwarfVersion) { |
Manman Ren | 9691f7f | 2013-06-19 01:46:49 +0000 | [diff] [blame] | 453 | // We actually want the latest version when there are conflicts. |
| 454 | // We can change from Warning to Latest if such mode is supported. |
| 455 | getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version", |
| 456 | CodeGenOpts.DwarfVersion); |
Reid Kleckner | 124955a | 2015-08-05 18:51:13 +0000 | [diff] [blame] | 457 | } |
| 458 | if (CodeGenOpts.EmitCodeView) { |
| 459 | // Indicate that we want CodeView in the metadata. |
| 460 | getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1); |
| 461 | } |
Adrian McCarthy | db2736d | 2018-01-09 23:49:30 +0000 | [diff] [blame] | 462 | if (CodeGenOpts.ControlFlowGuard) { |
| 463 | // We want function ID tables for Control Flow Guard. |
| 464 | getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1); |
| 465 | } |
Piotr Padlewski | 9d0ecf2 | 2015-09-15 21:46:50 +0000 | [diff] [blame] | 466 | if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) { |
| 467 | // We don't support LTO with 2 with different StrictVTablePointers |
| 468 | // FIXME: we could support it by stripping all the information introduced |
| 469 | // by StrictVTablePointers. |
| 470 | |
| 471 | getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1); |
| 472 | |
| 473 | llvm::Metadata *Ops[2] = { |
| 474 | llvm::MDString::get(VMContext, "StrictVTablePointers"), |
| 475 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 476 | llvm::Type::getInt32Ty(VMContext), 1))}; |
| 477 | |
| 478 | getModule().addModuleFlag(llvm::Module::Require, |
| 479 | "StrictVTablePointersRequirement", |
| 480 | llvm::MDNode::get(VMContext, Ops)); |
| 481 | } |
Manman Ren | 4b7f23d | 2013-11-22 19:42:45 +0000 | [diff] [blame] | 482 | if (DebugInfo) |
Adrian Prantl | 2dbdd20 | 2014-05-19 23:40:06 +0000 | [diff] [blame] | 483 | // We support a single version in the linked module. The LLVM |
| 484 | // parser will drop debug info with a different version number |
| 485 | // (and warn about it, too). |
| 486 | getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version", |
Manman Ren | 61fd594 | 2013-12-02 20:10:37 +0000 | [diff] [blame] | 487 | llvm::DEBUG_METADATA_VERSION); |
Douglas Gregor | ea02f26 | 2013-01-14 18:28:43 +0000 | [diff] [blame] | 488 | |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 489 | // 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] | 490 | // the correct build attributes in the ARM backend. wchar_size is also used by |
| 491 | // TargetLibraryInfo. |
Matthias Braun | bffc1bb | 2017-09-26 02:37:23 +0000 | [diff] [blame] | 492 | uint64_t WCharWidth = |
| 493 | Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity(); |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 494 | getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth); |
| 495 | |
Justin Bogner | 9bb7f15 | 2014-06-23 20:03:28 +0000 | [diff] [blame] | 496 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 497 | if ( Arch == llvm::Triple::arm |
| 498 | || Arch == llvm::Triple::armeb |
| 499 | || Arch == llvm::Triple::thumb |
| 500 | || Arch == llvm::Triple::thumbeb) { |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 501 | // The minimum width of an enum in bytes |
| 502 | uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4; |
| 503 | getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth); |
| 504 | } |
| 505 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 506 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
| 507 | // Indicate that we want cross-DSO control flow integrity checks. |
| 508 | getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1); |
| 509 | } |
| 510 | |
Oren Ben Simhon | 57cc1a5 | 2018-01-09 08:53:59 +0000 | [diff] [blame] | 511 | if (CodeGenOpts.CFProtectionReturn && |
| 512 | Target.checkCFProtectionReturnSupported(getDiags())) { |
| 513 | // Indicate that we want to instrument return control flow protection. |
| 514 | getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return", |
| 515 | 1); |
| 516 | } |
| 517 | |
| 518 | if (CodeGenOpts.CFProtectionBranch && |
| 519 | Target.checkCFProtectionBranchSupported(getDiags())) { |
| 520 | // Indicate that we want to instrument branch control flow protection. |
| 521 | getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch", |
| 522 | 1); |
| 523 | } |
| 524 | |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 525 | if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) { |
Justin Lebar | d3a44f6 | 2016-04-05 18:26:20 +0000 | [diff] [blame] | 526 | // Indicate whether __nvvm_reflect should be configured to flush denormal |
| 527 | // floating point values to 0. (This corresponds to its "__CUDA_FTZ" |
| 528 | // property.) |
| 529 | getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz", |
| 530 | LangOpts.CUDADeviceFlushDenormalsToZero ? 1 : 0); |
| 531 | } |
| 532 | |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 533 | // Emit OpenCL specific module metadata: OpenCL/SPIR version. |
| 534 | if (LangOpts.OpenCL) { |
| 535 | EmitOpenCLMetadata(); |
| 536 | // Emit SPIR version. |
| 537 | if (getTriple().getArch() == llvm::Triple::spir || |
| 538 | getTriple().getArch() == llvm::Triple::spir64) { |
| 539 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 540 | // opencl.spir.version named metadata. |
| 541 | llvm::Metadata *SPIRVerElts[] = { |
| 542 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 543 | Int32Ty, LangOpts.OpenCLVersion / 100)), |
| 544 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 545 | Int32Ty, (LangOpts.OpenCLVersion / 100 > 1) ? 0 : 2))}; |
| 546 | llvm::NamedMDNode *SPIRVerMD = |
| 547 | TheModule.getOrInsertNamedMetadata("opencl.spir.version"); |
| 548 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 549 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
| 550 | } |
| 551 | } |
| 552 | |
Justin Hibbits | 90ca05e | 2014-11-18 06:17:20 +0000 | [diff] [blame] | 553 | if (uint32_t PLevel = Context.getLangOpts().PICLevel) { |
Sriraman Tallam | 70e70e6 | 2016-04-28 22:34:00 +0000 | [diff] [blame] | 554 | assert(PLevel < 3 && "Invalid PIC Level"); |
| 555 | getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel)); |
Rafael Espindola | c9d336e | 2016-06-23 15:07:32 +0000 | [diff] [blame] | 556 | if (Context.getLangOpts().PIE) |
| 557 | getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel)); |
Justin Hibbits | 90ca05e | 2014-11-18 06:17:20 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Sriraman Tallam | 80af005 | 2018-02-23 21:27:33 +0000 | [diff] [blame] | 560 | if (CodeGenOpts.NoPLT) |
| 561 | getModule().setRtLibUseGOT(); |
| 562 | |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 563 | SimplifyPersonality(); |
| 564 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 565 | if (getCodeGenOpts().EmitDeclMetadata) |
| 566 | EmitDeclMetadata(); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 567 | |
| 568 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 569 | EmitCoverageFile(); |
Devang Patel | ffa30ab | 2011-08-16 20:58:22 +0000 | [diff] [blame] | 570 | |
| 571 | if (DebugInfo) |
| 572 | DebugInfo->finalize(); |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 573 | |
Mikhail Maltsev | 4a4e7a3 | 2018-04-23 10:08:46 +0000 | [diff] [blame] | 574 | if (getCodeGenOpts().EmitVersionIdentMetadata) |
| 575 | EmitVersionIdentMetadata(); |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 576 | |
| 577 | EmitTargetMetadata(); |
Daniel Dunbar | 23fd462 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 580 | void CodeGenModule::EmitOpenCLMetadata() { |
| 581 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 582 | // opencl.ocl.version named metadata node. |
| 583 | llvm::Metadata *OCLVerElts[] = { |
| 584 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 585 | Int32Ty, LangOpts.OpenCLVersion / 100)), |
| 586 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 587 | Int32Ty, (LangOpts.OpenCLVersion % 100) / 10))}; |
| 588 | llvm::NamedMDNode *OCLVerMD = |
| 589 | TheModule.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 590 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 591 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 592 | } |
| 593 | |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 594 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 595 | // Make sure that this type is translated. |
| 596 | Types.UpdateCompletedType(TD); |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 597 | } |
| 598 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 599 | void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { |
| 600 | // Make sure that this type is translated. |
| 601 | Types.RefreshTypeCacheForClass(RD); |
| 602 | } |
| 603 | |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 604 | llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) { |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 605 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 606 | return nullptr; |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 607 | return TBAA->getTypeInfo(QTy); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 610 | TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) { |
Ivan A. Kosarev | 124a218 | 2018-02-20 12:33:04 +0000 | [diff] [blame] | 611 | if (!TBAA) |
| 612 | return TBAAAccessInfo(); |
| 613 | return TBAA->getAccessInfo(AccessType); |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 616 | TBAAAccessInfo |
| 617 | CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) { |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 618 | if (!TBAA) |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 619 | return TBAAAccessInfo(); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 620 | return TBAA->getVTablePtrAccessInfo(VTablePtrType); |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 623 | llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { |
| 624 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 625 | return nullptr; |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 626 | return TBAA->getTBAAStructInfo(QTy); |
| 627 | } |
| 628 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 629 | llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) { |
| 630 | if (!TBAA) |
| 631 | return nullptr; |
| 632 | return TBAA->getBaseTypeInfo(QTy); |
| 633 | } |
| 634 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 635 | llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) { |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 636 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 637 | return nullptr; |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 638 | return TBAA->getAccessTagInfo(Info); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Ivan A. Kosarev | ed141ba | 2017-10-17 09:12:13 +0000 | [diff] [blame] | 641 | TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, |
| 642 | TBAAAccessInfo TargetInfo) { |
| 643 | if (!TBAA) |
| 644 | return TBAAAccessInfo(); |
| 645 | return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo); |
| 646 | } |
| 647 | |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 648 | TBAAAccessInfo |
| 649 | CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, |
| 650 | TBAAAccessInfo InfoB) { |
| 651 | if (!TBAA) |
| 652 | return TBAAAccessInfo(); |
| 653 | return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB); |
| 654 | } |
| 655 | |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 656 | TBAAAccessInfo |
| 657 | CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, |
| 658 | TBAAAccessInfo SrcInfo) { |
| 659 | if (!TBAA) |
| 660 | return TBAAAccessInfo(); |
| 661 | return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo); |
| 662 | } |
| 663 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 664 | void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst, |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 665 | TBAAAccessInfo TBAAInfo) { |
| 666 | if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo)) |
| 667 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 670 | void CodeGenModule::DecorateInstructionWithInvariantGroup( |
| 671 | llvm::Instruction *I, const CXXRecordDecl *RD) { |
Piotr Padlewski | 34046bd | 2017-04-24 12:58:43 +0000 | [diff] [blame] | 672 | I->setMetadata(llvm::LLVMContext::MD_invariant_group, |
| 673 | llvm::MDNode::get(getLLVMContext(), {})); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 676 | void CodeGenModule::Error(SourceLocation loc, StringRef message) { |
| 677 | unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
| 678 | getDiags().Report(Context.getFullLoc(loc), diagID) << message; |
John McCall | 7ef5cb3 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 681 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 682 | /// specified stmt yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 683 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 684 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 685 | "cannot compile this %0 yet"); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 686 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 687 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 688 | << Msg << S->getSourceRange(); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 689 | } |
Chris Lattner | 41af818 | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 690 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 691 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 692 | /// specified decl yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 693 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 694 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 695 | "cannot compile this %0 yet"); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 696 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 697 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 698 | } |
| 699 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 700 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
| 701 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 702 | } |
| 703 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 705 | const NamedDecl *D) const { |
Rafael Espindola | 123ce97 | 2018-01-24 18:58:32 +0000 | [diff] [blame] | 706 | if (GV->hasDLLImportStorageClass()) |
| 707 | return; |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 708 | // Internal definitions always have default visibility. |
Chris Lattner | 6a0f907 | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 709 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 710 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 711 | return; |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 712 | } |
Rafael Espindola | 3f727a8 | 2018-03-14 18:14:46 +0000 | [diff] [blame] | 713 | if (!D) |
| 714 | return; |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 715 | // Set visibility for definitions. |
Rafael Espindola | a8fbdab | 2013-02-27 02:15:29 +0000 | [diff] [blame] | 716 | LinkageInfo LV = D->getLinkageAndVisibility(); |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 717 | if (LV.isVisibilityExplicit() || !GV->isDeclarationForLinker()) |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 718 | GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 721 | static bool shouldAssumeDSOLocal(const CodeGenModule &CGM, |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 722 | llvm::GlobalValue *GV) { |
Rafael Espindola | 796d4a8 | 2018-03-02 01:29:51 +0000 | [diff] [blame] | 723 | if (GV->hasLocalLinkage()) |
| 724 | return true; |
| 725 | |
| 726 | if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()) |
| 727 | return true; |
| 728 | |
Rafael Espindola | 922f2aa | 2018-02-23 19:30:48 +0000 | [diff] [blame] | 729 | // DLLImport explicitly marks the GV as external. |
| 730 | if (GV->hasDLLImportStorageClass()) |
| 731 | return false; |
| 732 | |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 733 | const llvm::Triple &TT = CGM.getTriple(); |
Rafael Espindola | 922f2aa | 2018-02-23 19:30:48 +0000 | [diff] [blame] | 734 | // Every other GV is local on COFF. |
| 735 | // Make an exception for windows OS in the triple: Some firmware builds use |
| 736 | // *-win32-macho triples. This (accidentally?) produced windows relocations |
| 737 | // without GOT tables in older clang versions; Keep this behaviour. |
| 738 | // FIXME: even thread local variables? |
| 739 | if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) |
| 740 | return true; |
| 741 | |
| 742 | // Only handle COFF and ELF for now. |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 743 | if (!TT.isOSBinFormatELF()) |
| 744 | return false; |
| 745 | |
| 746 | // If this is not an executable, don't assume anything is local. |
| 747 | const auto &CGOpts = CGM.getCodeGenOpts(); |
| 748 | llvm::Reloc::Model RM = CGOpts.RelocationModel; |
| 749 | const auto &LOpts = CGM.getLangOpts(); |
| 750 | if (RM != llvm::Reloc::Static && !LOpts.PIE) |
| 751 | return false; |
| 752 | |
| 753 | // A definition cannot be preempted from an executable. |
| 754 | if (!GV->isDeclarationForLinker()) |
| 755 | return true; |
| 756 | |
| 757 | // Most PIC code sequences that assume that a symbol is local cannot produce a |
| 758 | // 0 if it turns out the symbol is undefined. While this is ABI and relocation |
| 759 | // depended, it seems worth it to handle it here. |
| 760 | if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage()) |
| 761 | return false; |
| 762 | |
| 763 | // PPC has no copy relocations and cannot use a plt entry as a symbol address. |
| 764 | llvm::Triple::ArchType Arch = TT.getArch(); |
| 765 | if (Arch == llvm::Triple::ppc || Arch == llvm::Triple::ppc64 || |
| 766 | Arch == llvm::Triple::ppc64le) |
| 767 | return false; |
| 768 | |
| 769 | // If we can use copy relocations we can assume it is local. |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 770 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV)) |
| 771 | if (!Var->isThreadLocal() && |
Rafael Espindola | 75e5736 | 2018-02-07 23:04:06 +0000 | [diff] [blame] | 772 | (RM == llvm::Reloc::Static || CGOpts.PIECopyRelocations)) |
| 773 | return true; |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 774 | |
| 775 | // If we can use a plt entry as the symbol address we can assume it |
| 776 | // is local. |
| 777 | // FIXME: This should work for PIE, but the gold linker doesn't support it. |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 778 | if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static) |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 779 | return true; |
| 780 | |
| 781 | // Otherwise don't assue it is local. |
| 782 | return false; |
| 783 | } |
| 784 | |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 785 | void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const { |
Rafael Espindola | 796d4a8 | 2018-03-02 01:29:51 +0000 | [diff] [blame] | 786 | GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV)); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 789 | void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, |
| 790 | GlobalDecl GD) const { |
| 791 | const auto *D = dyn_cast<NamedDecl>(GD.getDecl()); |
Reid Kleckner | ae9b070 | 2018-03-16 19:40:50 +0000 | [diff] [blame] | 792 | // C++ destructors have a few C++ ABI specific special cases. |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 793 | if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) { |
Reid Kleckner | ae9b070 | 2018-03-16 19:40:50 +0000 | [diff] [blame] | 794 | getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType()); |
| 795 | return; |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 796 | } |
| 797 | setDLLImportDLLExport(GV, D); |
| 798 | } |
| 799 | |
| 800 | void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, |
| 801 | const NamedDecl *D) const { |
Rafael Espindola | 3f727a8 | 2018-03-14 18:14:46 +0000 | [diff] [blame] | 802 | if (D && D->isExternallyVisible()) { |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 803 | if (D->hasAttr<DLLImportAttr>()) |
| 804 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 805 | else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker()) |
| 806 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, |
| 811 | GlobalDecl GD) const { |
| 812 | setDLLImportDLLExport(GV, GD); |
| 813 | setGlobalVisibilityAndLocal(GV, dyn_cast<NamedDecl>(GD.getDecl())); |
| 814 | } |
| 815 | |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 816 | void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, |
| 817 | const NamedDecl *D) const { |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 818 | setDLLImportDLLExport(GV, D); |
| 819 | setGlobalVisibilityAndLocal(GV, D); |
| 820 | } |
| 821 | |
| 822 | void CodeGenModule::setGlobalVisibilityAndLocal(llvm::GlobalValue *GV, |
| 823 | const NamedDecl *D) const { |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 824 | setGlobalVisibility(GV, D); |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 825 | setDSOLocal(GV); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 828 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) { |
| 829 | return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S) |
| 830 | .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel) |
| 831 | .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel) |
| 832 | .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel) |
| 833 | .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel); |
| 834 | } |
| 835 | |
| 836 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel( |
| 837 | CodeGenOptions::TLSModel M) { |
| 838 | switch (M) { |
| 839 | case CodeGenOptions::GeneralDynamicTLSModel: |
| 840 | return llvm::GlobalVariable::GeneralDynamicTLSModel; |
| 841 | case CodeGenOptions::LocalDynamicTLSModel: |
| 842 | return llvm::GlobalVariable::LocalDynamicTLSModel; |
| 843 | case CodeGenOptions::InitialExecTLSModel: |
| 844 | return llvm::GlobalVariable::InitialExecTLSModel; |
| 845 | case CodeGenOptions::LocalExecTLSModel: |
| 846 | return llvm::GlobalVariable::LocalExecTLSModel; |
| 847 | } |
| 848 | llvm_unreachable("Invalid TLS model!"); |
| 849 | } |
| 850 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 851 | void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const { |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 852 | assert(D.getTLSKind() && "setting TLS mode on non-TLS var!"); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 853 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 854 | llvm::GlobalValue::ThreadLocalMode TLM; |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 855 | TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel()); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 856 | |
| 857 | // Override the TLS model if it is explicitly specified. |
Aaron Ballman | c432799 | 2013-12-19 03:09:10 +0000 | [diff] [blame] | 858 | if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) { |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 859 | TLM = GetLLVMTLSModel(Attr->getModel()); |
| 860 | } |
| 861 | |
| 862 | GV->setThreadLocalMode(TLM); |
| 863 | } |
| 864 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 865 | static void AppendTargetMangling(const CodeGenModule &CGM, |
| 866 | const TargetAttr *Attr, raw_ostream &Out) { |
| 867 | if (Attr->isDefaultVersion()) |
| 868 | return; |
| 869 | |
| 870 | Out << '.'; |
| 871 | const auto &Target = CGM.getTarget(); |
| 872 | TargetAttr::ParsedTargetAttr Info = |
| 873 | Attr->parse([&Target](StringRef LHS, StringRef RHS) { |
George Burgess IV | 1913115 | 2018-01-17 04:46:04 +0000 | [diff] [blame] | 874 | // Multiversioning doesn't allow "no-${feature}", so we can |
| 875 | // only have "+" prefixes here. |
| 876 | assert(LHS.startswith("+") && RHS.startswith("+") && |
| 877 | "Features should always have a prefix."); |
| 878 | return Target.multiVersionSortPriority(LHS.substr(1)) > |
| 879 | Target.multiVersionSortPriority(RHS.substr(1)); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 880 | }); |
| 881 | |
| 882 | bool IsFirst = true; |
| 883 | |
| 884 | if (!Info.Architecture.empty()) { |
| 885 | IsFirst = false; |
| 886 | Out << "arch_" << Info.Architecture; |
| 887 | } |
| 888 | |
| 889 | for (StringRef Feat : Info.Features) { |
| 890 | if (!IsFirst) |
| 891 | Out << '_'; |
| 892 | IsFirst = false; |
| 893 | Out << Feat.substr(1); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | static std::string getMangledNameImpl(const CodeGenModule &CGM, GlobalDecl GD, |
| 898 | const NamedDecl *ND, |
| 899 | bool OmitTargetMangling = false) { |
| 900 | SmallString<256> Buffer; |
| 901 | llvm::raw_svector_ostream Out(Buffer); |
| 902 | MangleContext &MC = CGM.getCXXABI().getMangleContext(); |
| 903 | if (MC.shouldMangleDeclName(ND)) { |
| 904 | llvm::raw_svector_ostream Out(Buffer); |
| 905 | if (const auto *D = dyn_cast<CXXConstructorDecl>(ND)) |
| 906 | MC.mangleCXXCtor(D, GD.getCtorType(), Out); |
| 907 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND)) |
| 908 | MC.mangleCXXDtor(D, GD.getDtorType(), Out); |
| 909 | else |
| 910 | MC.mangleName(ND, Out); |
| 911 | } else { |
| 912 | IdentifierInfo *II = ND->getIdentifier(); |
| 913 | assert(II && "Attempt to mangle unnamed decl."); |
| 914 | const auto *FD = dyn_cast<FunctionDecl>(ND); |
| 915 | |
| 916 | if (FD && |
| 917 | FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) { |
| 918 | llvm::raw_svector_ostream Out(Buffer); |
| 919 | Out << "__regcall3__" << II->getName(); |
| 920 | } else { |
| 921 | Out << II->getName(); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 926 | if (FD->isMultiVersion() && !OmitTargetMangling) |
| 927 | AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out); |
| 928 | return Out.str(); |
| 929 | } |
| 930 | |
| 931 | void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD, |
| 932 | const FunctionDecl *FD) { |
| 933 | if (!FD->isMultiVersion()) |
| 934 | return; |
| 935 | |
| 936 | // Get the name of what this would be without the 'target' attribute. This |
| 937 | // allows us to lookup the version that was emitted when this wasn't a |
| 938 | // multiversion function. |
| 939 | std::string NonTargetName = |
| 940 | getMangledNameImpl(*this, GD, FD, /*OmitTargetMangling=*/true); |
| 941 | GlobalDecl OtherGD; |
| 942 | if (lookupRepresentativeDecl(NonTargetName, OtherGD)) { |
| 943 | assert(OtherGD.getCanonicalDecl() |
| 944 | .getDecl() |
| 945 | ->getAsFunction() |
| 946 | ->isMultiVersion() && |
| 947 | "Other GD should now be a multiversioned function"); |
| 948 | // OtherFD is the version of this function that was mangled BEFORE |
| 949 | // becoming a MultiVersion function. It potentially needs to be updated. |
| 950 | const FunctionDecl *OtherFD = |
| 951 | OtherGD.getCanonicalDecl().getDecl()->getAsFunction(); |
| 952 | std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD); |
| 953 | // This is so that if the initial version was already the 'default' |
| 954 | // version, we don't try to update it. |
| 955 | if (OtherName != NonTargetName) { |
Erich Keane | bc40c5c | 2018-01-09 01:09:12 +0000 | [diff] [blame] | 956 | // Remove instead of erase, since others may have stored the StringRef |
| 957 | // to this. |
| 958 | const auto ExistingRecord = Manglings.find(NonTargetName); |
| 959 | if (ExistingRecord != std::end(Manglings)) |
| 960 | Manglings.remove(&(*ExistingRecord)); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 961 | auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD)); |
| 962 | MangledDeclNames[OtherGD.getCanonicalDecl()] = Result.first->first(); |
| 963 | if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName)) |
| 964 | Entry->setName(OtherName); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 969 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
David Majnemer | d2385c0 | 2016-01-08 20:48:26 +0000 | [diff] [blame] | 970 | GlobalDecl CanonicalGD = GD.getCanonicalDecl(); |
| 971 | |
| 972 | // Some ABIs don't have constructor variants. Make sure that base and |
| 973 | // complete constructors get mangled the same. |
| 974 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) { |
| 975 | if (!getTarget().getCXXABI().hasConstructorVariants()) { |
| 976 | CXXCtorType OrigCtorType = GD.getCtorType(); |
| 977 | assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete); |
| 978 | if (OrigCtorType == Ctor_Base) |
| 979 | CanonicalGD = GlobalDecl(CD, Ctor_Complete); |
| 980 | } |
| 981 | } |
| 982 | |
NAKAMURA Takumi | 6e4d774 | 2017-07-30 05:06:26 +0000 | [diff] [blame] | 983 | auto FoundName = MangledDeclNames.find(CanonicalGD); |
| 984 | if (FoundName != MangledDeclNames.end()) |
| 985 | return FoundName->second; |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 986 | |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 987 | |
Richard Smith | 6d0e97a | 2014-08-02 00:50:16 +0000 | [diff] [blame] | 988 | // Keep the first result in the case of a mangling collision. |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 989 | const auto *ND = cast<NamedDecl>(GD.getDecl()); |
| 990 | auto Result = |
| 991 | Manglings.insert(std::make_pair(getMangledNameImpl(*this, GD, ND), GD)); |
NAKAMURA Takumi | 6e4d774 | 2017-07-30 05:06:26 +0000 | [diff] [blame] | 992 | return MangledDeclNames[CanonicalGD] = Result.first->first(); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 995 | StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD, |
| 996 | const BlockDecl *BD) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 997 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
| 998 | const Decl *D = GD.getDecl(); |
Alp Toker | 0e64e0d | 2014-06-03 02:13:57 +0000 | [diff] [blame] | 999 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 1000 | SmallString<256> Buffer; |
| 1001 | llvm::raw_svector_ostream Out(Buffer); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1002 | if (!D) |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 1003 | MangleCtx.mangleGlobalBlock(BD, |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 1004 | dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1005 | else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1006 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1007 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1008 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1009 | else |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1010 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
Alp Toker | 0e64e0d | 2014-06-03 02:13:57 +0000 | [diff] [blame] | 1011 | |
Richard Smith | 6d0e97a | 2014-08-02 00:50:16 +0000 | [diff] [blame] | 1012 | auto Result = Manglings.insert(std::make_pair(Out.str(), BD)); |
| 1013 | return Result.first->first(); |
Anders Carlsson | 635186a | 2010-06-09 02:36:32 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1016 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1017 | return getModule().getNamedValue(Name); |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1020 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 1021 | /// main() runs. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1022 | void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority, |
| 1023 | llvm::Constant *AssociatedData) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1024 | // FIXME: Type coercion of void()* types. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1025 | GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1028 | /// AddGlobalDtor - Add a function to the list that will be called |
| 1029 | /// when the module is unloaded. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1030 | void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) { |
Akira Hatanaka | 617e261 | 2018-04-17 18:41:52 +0000 | [diff] [blame] | 1031 | if (CodeGenOpts.RegisterGlobalDtorsWithAtExit) { |
| 1032 | DtorsUsingAtExit[Priority].push_back(Dtor); |
| 1033 | return; |
| 1034 | } |
| 1035 | |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1036 | // FIXME: Type coercion of void()* types. |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 1037 | GlobalDtors.push_back(Structor(Priority, Dtor, nullptr)); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Vassil Vassilev | 188ad3a | 2016-10-27 09:12:20 +0000 | [diff] [blame] | 1040 | void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1041 | if (Fns.empty()) return; |
| 1042 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1043 | // Ctor function type is void()*. |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1044 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1045 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1046 | |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1047 | // Get the type of a ctor entry, { i32, void ()*, i8* }. |
| 1048 | llvm::StructType *CtorStructTy = llvm::StructType::get( |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 1049 | Int32Ty, llvm::PointerType::getUnqual(CtorFTy), VoidPtrTy); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1050 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1051 | // Construct the constructor and destructor arrays. |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 1052 | ConstantInitBuilder builder(*this); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1053 | auto ctors = builder.beginArray(CtorStructTy); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 1054 | for (const auto &I : Fns) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1055 | auto ctor = ctors.beginStruct(CtorStructTy); |
| 1056 | ctor.addInt(Int32Ty, I.Priority); |
| 1057 | ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy)); |
| 1058 | if (I.AssociatedData) |
| 1059 | ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy)); |
| 1060 | else |
| 1061 | ctor.addNullPointer(VoidPtrTy); |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 1062 | ctor.finishAndAddTo(ctors); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1063 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1064 | |
John McCall | 18081af | 2016-11-19 20:12:25 +0000 | [diff] [blame] | 1065 | auto list = |
| 1066 | ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(), |
| 1067 | /*constant*/ false, |
| 1068 | llvm::GlobalValue::AppendingLinkage); |
| 1069 | |
| 1070 | // The LTO linker doesn't seem to like it when we set an alignment |
| 1071 | // on appending variables. Take it off as a workaround. |
| 1072 | list->setAlignment(0); |
| 1073 | |
Vassil Vassilev | 188ad3a | 2016-10-27 09:12:20 +0000 | [diff] [blame] | 1074 | Fns.clear(); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 1077 | llvm::GlobalValue::LinkageTypes |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1078 | CodeGenModule::getFunctionLinkage(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1079 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1080 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 1081 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1082 | |
Reid Kleckner | ae9b070 | 2018-03-16 19:40:50 +0000 | [diff] [blame] | 1083 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D)) |
| 1084 | return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType()); |
Reid Kleckner | a283e48 | 2017-12-21 19:44:23 +0000 | [diff] [blame] | 1085 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 1086 | if (isa<CXXConstructorDecl>(D) && |
| 1087 | cast<CXXConstructorDecl>(D)->isInheritingConstructor() && |
| 1088 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 1089 | // Our approach to inheriting constructors is fundamentally different from |
| 1090 | // that used by the MS ABI, so keep our inheriting constructor thunks |
| 1091 | // internal rather than trying to pick an unambiguous mangling for them. |
| 1092 | return llvm::GlobalValue::InternalLinkage; |
| 1093 | } |
| 1094 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 1095 | return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 1096 | } |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 1097 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1098 | llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1099 | llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD); |
| 1100 | if (!MDS) return nullptr; |
| 1101 | |
Benjamin Kramer | 630cf8c | 2016-11-23 11:20:27 +0000 | [diff] [blame] | 1102 | return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString())); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 1105 | void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | const CGFunctionInfo &Info, |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 1107 | llvm::Function *F) { |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1108 | unsigned CallingConv; |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 1109 | llvm::AttributeList PAL; |
| 1110 | ConstructAttributeList(F->getName(), Info, D, PAL, CallingConv, false); |
| 1111 | F->setAttributes(PAL); |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1112 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1115 | /// Determines whether the language options require us to model |
| 1116 | /// unwind exceptions. We treat -fexceptions as mandating this |
| 1117 | /// except under the fragile ObjC ABI with only ObjC exceptions |
| 1118 | /// enabled. This means, for example, that C with -fexceptions |
| 1119 | /// enables this. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1120 | static bool hasUnwindExceptions(const LangOptions &LangOpts) { |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1121 | // If exceptions are completely disabled, obviously this is false. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1122 | if (!LangOpts.Exceptions) return false; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1123 | |
| 1124 | // If C++ exceptions are enabled, this is true. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1125 | if (LangOpts.CXXExceptions) return true; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1126 | |
| 1127 | // If ObjC exceptions are enabled, this depends on the ABI. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1128 | if (LangOpts.ObjCExceptions) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 1129 | return LangOpts.ObjCRuntime.hasUnwindExceptions(); |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | return true; |
| 1133 | } |
| 1134 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1135 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 1136 | llvm::Function *F) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1137 | llvm::AttrBuilder B; |
| 1138 | |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 1139 | if (CodeGenOpts.UnwindTables) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1140 | B.addAttribute(llvm::Attribute::UWTable); |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 1141 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1142 | if (!hasUnwindExceptions(LangOpts)) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1143 | B.addAttribute(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1144 | |
Akira Hatanaka | 200500d | 2015-10-08 19:30:57 +0000 | [diff] [blame] | 1145 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
| 1146 | B.addAttribute(llvm::Attribute::StackProtect); |
| 1147 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
| 1148 | B.addAttribute(llvm::Attribute::StackProtectStrong); |
| 1149 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
| 1150 | B.addAttribute(llvm::Attribute::StackProtectReq); |
| 1151 | |
| 1152 | if (!D) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1153 | // If we don't have a declaration to control inlining, the function isn't |
| 1154 | // explicitly marked as alwaysinline for semantic reasons, and inlining is |
| 1155 | // disabled, mark the function as noinline. |
| 1156 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
| 1157 | CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) |
| 1158 | B.addAttribute(llvm::Attribute::NoInline); |
| 1159 | |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1160 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Akira Hatanaka | 200500d | 2015-10-08 19:30:57 +0000 | [diff] [blame] | 1161 | return; |
| 1162 | } |
| 1163 | |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1164 | // Track whether we need to add the optnone LLVM attribute, |
| 1165 | // starting with the default for this optimization level. |
| 1166 | bool ShouldAddOptNone = |
| 1167 | !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0; |
| 1168 | // We can't add optnone in the following cases, it won't pass the verifier. |
| 1169 | ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>(); |
| 1170 | ShouldAddOptNone &= !F->hasFnAttribute(llvm::Attribute::AlwaysInline); |
| 1171 | ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>(); |
| 1172 | |
| 1173 | if (ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1174 | B.addAttribute(llvm::Attribute::OptimizeNone); |
| 1175 | |
| 1176 | // OptimizeNone implies noinline; we should not be inlining such functions. |
| 1177 | B.addAttribute(llvm::Attribute::NoInline); |
| 1178 | assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
| 1179 | "OptimizeNone and AlwaysInline on same function!"); |
| 1180 | |
| 1181 | // We still need to handle naked functions even though optnone subsumes |
| 1182 | // much of their semantics. |
| 1183 | if (D->hasAttr<NakedAttr>()) |
| 1184 | B.addAttribute(llvm::Attribute::Naked); |
| 1185 | |
| 1186 | // OptimizeNone wins over OptimizeForSize and MinSize. |
| 1187 | F->removeFnAttr(llvm::Attribute::OptimizeForSize); |
| 1188 | F->removeFnAttr(llvm::Attribute::MinSize); |
| 1189 | } else if (D->hasAttr<NakedAttr>()) { |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 1190 | // Naked implies noinline: we should not be inlining such functions. |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1191 | B.addAttribute(llvm::Attribute::Naked); |
| 1192 | B.addAttribute(llvm::Attribute::NoInline); |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 1193 | } else if (D->hasAttr<NoDuplicateAttr>()) { |
| 1194 | B.addAttribute(llvm::Attribute::NoDuplicate); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1195 | } else if (D->hasAttr<NoInlineAttr>()) { |
| 1196 | B.addAttribute(llvm::Attribute::NoInline); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 1197 | } else if (D->hasAttr<AlwaysInlineAttr>() && |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1198 | !F->hasFnAttribute(llvm::Attribute::NoInline)) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1199 | // (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] | 1200 | B.addAttribute(llvm::Attribute::AlwaysInline); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1201 | } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) { |
| 1202 | // If we're not inlining, then force everything that isn't always_inline to |
| 1203 | // carry an explicit noinline attribute. |
| 1204 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline)) |
| 1205 | B.addAttribute(llvm::Attribute::NoInline); |
| 1206 | } else { |
| 1207 | // Otherwise, propagate the inline hint attribute and potentially use its |
| 1208 | // absence to mark things as noinline. |
| 1209 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1210 | if (any_of(FD->redecls(), [&](const FunctionDecl *Redecl) { |
| 1211 | return Redecl->isInlineSpecified(); |
| 1212 | })) { |
| 1213 | B.addAttribute(llvm::Attribute::InlineHint); |
| 1214 | } else if (CodeGenOpts.getInlining() == |
| 1215 | CodeGenOptions::OnlyHintInlining && |
| 1216 | !FD->isInlined() && |
| 1217 | !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { |
| 1218 | B.addAttribute(llvm::Attribute::NoInline); |
| 1219 | } |
| 1220 | } |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 1221 | } |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1222 | |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1223 | // Add other optimization related attributes if we are optimizing this |
| 1224 | // function. |
| 1225 | if (!D->hasAttr<OptimizeNoneAttr>()) { |
| 1226 | if (D->hasAttr<ColdAttr>()) { |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1227 | if (!ShouldAddOptNone) |
| 1228 | B.addAttribute(llvm::Attribute::OptimizeForSize); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1229 | B.addAttribute(llvm::Attribute::Cold); |
| 1230 | } |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1231 | |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1232 | if (D->hasAttr<MinSizeAttr>()) |
| 1233 | B.addAttribute(llvm::Attribute::MinSize); |
| 1234 | } |
Richard Smith | b555a76 | 2012-09-28 22:46:07 +0000 | [diff] [blame] | 1235 | |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1236 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1237 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1238 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
| 1239 | if (alignment) |
| 1240 | F->setAlignment(alignment); |
| 1241 | |
Saleem Abdulrasool | 3fe5b7a | 2018-04-19 23:14:57 +0000 | [diff] [blame] | 1242 | if (!D->hasAttr<AlignedAttr>()) |
| 1243 | if (LangOpts.FunctionAlignment) |
| 1244 | F->setAlignment(1 << LangOpts.FunctionAlignment); |
| 1245 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 1246 | // Some C++ ABIs require 2-byte alignment for member functions, in order to |
| 1247 | // reserve a bit for differentiating between virtual and non-virtual member |
| 1248 | // functions. If the current target's C++ ABI requires this and this is a |
| 1249 | // member function, set its alignment accordingly. |
| 1250 | if (getTarget().getCXXABI().areMemberFunctionsAligned()) { |
| 1251 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 1252 | F->setAlignment(2); |
| 1253 | } |
Evgeniy Stepanov | f754309 | 2016-10-31 22:28:10 +0000 | [diff] [blame] | 1254 | |
| 1255 | // In the cross-dso CFI mode, we want !type attributes on definitions only. |
| 1256 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 1257 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 1258 | CreateFunctionTypeMetadata(FD, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Rafael Espindola | 75c649c | 2018-03-01 00:06:55 +0000 | [diff] [blame] | 1261 | void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) { |
| 1262 | const Decl *D = GD.getDecl(); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 1263 | if (dyn_cast_or_null<NamedDecl>(D)) |
| 1264 | setGVProperties(GV, GD); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1265 | else |
| 1266 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1267 | |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1268 | if (D && D->hasAttr<UsedAttr>()) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1269 | addUsedGlobal(GV); |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1272 | bool CodeGenModule::GetCPUAndFeaturesAttributes(const Decl *D, |
| 1273 | llvm::AttrBuilder &Attrs) { |
| 1274 | // Add target-cpu and target-features attributes to functions. If |
| 1275 | // we have a decl for the function and it has a target attribute then |
| 1276 | // parse that and add it to the feature set. |
| 1277 | StringRef TargetCPU = getTarget().getTargetOpts().CPU; |
| 1278 | std::vector<std::string> Features; |
| 1279 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 1280 | FD = FD ? FD->getMostRecentDecl() : FD; |
| 1281 | const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr; |
| 1282 | bool AddedAttr = false; |
| 1283 | if (TD) { |
| 1284 | llvm::StringMap<bool> FeatureMap; |
| 1285 | getFunctionFeatureMap(FeatureMap, FD); |
| 1286 | |
| 1287 | // Produce the canonical string for this set of features. |
| 1288 | for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap) |
| 1289 | Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str()); |
| 1290 | |
| 1291 | // Now add the target-cpu and target-features to the function. |
| 1292 | // While we populated the feature map above, we still need to |
| 1293 | // get and parse the target attribute so we can get the cpu for |
| 1294 | // the function. |
| 1295 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
| 1296 | if (ParsedAttr.Architecture != "" && |
| 1297 | getTarget().isValidCPUName(ParsedAttr.Architecture)) |
| 1298 | TargetCPU = ParsedAttr.Architecture; |
| 1299 | } else { |
| 1300 | // Otherwise just add the existing target cpu and target features to the |
| 1301 | // function. |
| 1302 | Features = getTarget().getTargetOpts().Features; |
| 1303 | } |
| 1304 | |
| 1305 | if (TargetCPU != "") { |
| 1306 | Attrs.addAttribute("target-cpu", TargetCPU); |
| 1307 | AddedAttr = true; |
| 1308 | } |
| 1309 | if (!Features.empty()) { |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 1310 | llvm::sort(Features.begin(), Features.end()); |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1311 | Attrs.addAttribute("target-features", llvm::join(Features, ",")); |
| 1312 | AddedAttr = true; |
| 1313 | } |
| 1314 | |
| 1315 | return AddedAttr; |
| 1316 | } |
| 1317 | |
Rafael Espindola | 285271c | 2018-02-28 23:54:23 +0000 | [diff] [blame] | 1318 | void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, |
Rafael Espindola | ee076a2 | 2014-05-13 18:45:53 +0000 | [diff] [blame] | 1319 | llvm::GlobalObject *GO) { |
Rafael Espindola | 285271c | 2018-02-28 23:54:23 +0000 | [diff] [blame] | 1320 | const Decl *D = GD.getDecl(); |
Rafael Espindola | 75c649c | 2018-03-01 00:06:55 +0000 | [diff] [blame] | 1321 | SetCommonAttributes(GD, GO); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1322 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1323 | if (D) { |
| 1324 | if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) { |
| 1325 | if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>()) |
| 1326 | GV->addAttribute("bss-section", SA->getName()); |
| 1327 | if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>()) |
| 1328 | GV->addAttribute("data-section", SA->getName()); |
| 1329 | if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>()) |
| 1330 | GV->addAttribute("rodata-section", SA->getName()); |
| 1331 | } |
| 1332 | |
| 1333 | if (auto *F = dyn_cast<llvm::Function>(GO)) { |
| 1334 | if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>()) |
Erich Keane | 30994d2 | 2018-02-08 20:04:22 +0000 | [diff] [blame] | 1335 | if (!D->getAttr<SectionAttr>()) |
| 1336 | F->addFnAttr("implicit-section-name", SA->getName()); |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1337 | |
| 1338 | llvm::AttrBuilder Attrs; |
| 1339 | if (GetCPUAndFeaturesAttributes(D, Attrs)) { |
| 1340 | // We know that GetCPUAndFeaturesAttributes will always have the |
| 1341 | // newest set, since it has the newest possible FunctionDecl, so the |
| 1342 | // new ones should replace the old. |
| 1343 | F->removeFnAttr("target-cpu"); |
| 1344 | F->removeFnAttr("target-features"); |
| 1345 | F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs); |
| 1346 | } |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1349 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 1350 | GO->setSection(SA->getName()); |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1351 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1352 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1353 | getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1356 | void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD, |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 1357 | llvm::Function *F, |
| 1358 | const CGFunctionInfo &FI) { |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1359 | const Decl *D = GD.getDecl(); |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 1360 | SetLLVMFunctionAttributes(D, FI, F); |
| 1361 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1362 | |
| 1363 | F->setLinkage(llvm::Function::InternalLinkage); |
| 1364 | |
Rafael Espindola | 285271c | 2018-02-28 23:54:23 +0000 | [diff] [blame] | 1365 | setNonAliasAttributes(GD, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 1368 | static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) { |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1369 | // Set linkage and visibility in case we never see a definition. |
| 1370 | LinkageInfo LV = ND->getLinkageAndVisibility(); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 1371 | // Don't set internal linkage on declarations. |
| 1372 | // "extern_weak" is overloaded in LLVM; we probably should have |
| 1373 | // separate linkage types for this. |
| 1374 | if (isExternallyVisible(LV.getLinkage()) && |
| 1375 | (ND->hasAttr<WeakAttr>() || ND->isWeakImported())) |
| 1376 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1379 | void CodeGenModule::CreateFunctionTypeMetadata(const FunctionDecl *FD, |
| 1380 | llvm::Function *F) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1381 | // Only if we are checking indirect calls. |
| 1382 | if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall)) |
| 1383 | return; |
| 1384 | |
| 1385 | // Non-static class methods are handled via vtable pointer checks elsewhere. |
| 1386 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 1387 | return; |
| 1388 | |
| 1389 | // Additionally, if building with cross-DSO support... |
| 1390 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1391 | // Skip available_externally functions. They won't be codegen'ed in the |
| 1392 | // current module anyway. |
| 1393 | if (getContext().GetGVALinkageForFunction(FD) == GVA_AvailableExternally) |
| 1394 | return; |
| 1395 | } |
| 1396 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1397 | llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1398 | F->addTypeMetadata(0, MD); |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 1399 | F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType())); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Emit a hash-based bit set entry for cross-DSO calls. |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1402 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 1403 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
| 1404 | F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 1407 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, |
| 1408 | bool IsIncompleteFunction, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1409 | bool IsThunk) { |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1410 | |
Pete Cooper | 2f1637a | 2015-05-20 17:17:45 +0000 | [diff] [blame] | 1411 | if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) { |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 1412 | // If this is an intrinsic function, set the function's attributes |
| 1413 | // to the intrinsic's attributes. |
Pete Cooper | 2f1637a | 2015-05-20 17:17:45 +0000 | [diff] [blame] | 1414 | F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID)); |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 1415 | return; |
| 1416 | } |
| 1417 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1418 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 1419 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1420 | if (!IsIncompleteFunction) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1421 | SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1422 | // Setup target-specific attributes. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1423 | if (F->isDeclaration()) |
| 1424 | getTargetCodeGenInfo().setTargetAttributes(FD, F, *this); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1425 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | |
Bob Wilson | fc6297f | 2014-04-01 01:38:16 +0000 | [diff] [blame] | 1427 | // Add the Returned attribute for "this", except for iOS 5 and earlier |
| 1428 | // where substantial code, including the libstdc++ dylib, was compiled with |
| 1429 | // GCC and does not actually return "this". |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 1430 | if (!IsThunk && getCXXABI().HasThisReturn(GD) && |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 1431 | !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1432 | assert(!F->arg_empty() && |
| 1433 | F->arg_begin()->getType() |
| 1434 | ->canLosslesslyBitCastTo(F->getReturnType()) && |
| 1435 | "unexpected this return"); |
| 1436 | F->addAttribute(1, llvm::Attribute::Returned); |
| 1437 | } |
| 1438 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1439 | // Only a few attributes are set on declarations; these may later be |
| 1440 | // overridden by a definition. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
Jake Ehrlich | ba874ad | 2017-11-29 00:54:20 +0000 | [diff] [blame] | 1442 | setLinkageForGV(F, FD); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 1443 | setGVProperties(F, FD); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1444 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1445 | if (FD->getAttr<PragmaClangTextSectionAttr>()) { |
| 1446 | F->addFnAttr("implicit-section-name"); |
| 1447 | } |
| 1448 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1449 | if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1450 | F->setSection(SA->getName()); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1451 | |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1452 | if (FD->isReplaceableGlobalAllocationFunction()) { |
| 1453 | // A replaceable global allocation function does not act like a builtin by |
| 1454 | // 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] | 1455 | F->addAttribute(llvm::AttributeList::FunctionIndex, |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1456 | llvm::Attribute::NoBuiltin); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 1457 | |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1458 | // A sane operator new returns a non-aliasing pointer. |
| 1459 | // FIXME: Also add NonNull attribute to the return value |
| 1460 | // for the non-nothrow forms? |
| 1461 | auto Kind = FD->getDeclName().getCXXOverloadedOperator(); |
| 1462 | if (getCodeGenOpts().AssumeSaneOperatorNew && |
| 1463 | (Kind == OO_New || Kind == OO_Array_New)) |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 1464 | F->addAttribute(llvm::AttributeList::ReturnIndex, |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1465 | llvm::Attribute::NoAlias); |
| 1466 | } |
| 1467 | |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1468 | if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD)) |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1469 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1470 | else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1471 | if (MD->isVirtual()) |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1472 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1473 | |
Evgeniy Stepanov | f754309 | 2016-10-31 22:28:10 +0000 | [diff] [blame] | 1474 | // Don't emit entries for function declarations in the cross-DSO mode. This |
| 1475 | // is handled with better precision by the receiving DSO. |
| 1476 | if (!CodeGenOpts.SanitizeCfiCrossDso) |
| 1477 | CreateFunctionTypeMetadata(FD, F); |
Alexey Bataev | b952e63 | 2017-12-15 16:28:31 +0000 | [diff] [blame] | 1478 | |
| 1479 | if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>()) |
| 1480 | getOpenMPRuntime().emitDeclareSimdFunction(FD, F); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1483 | void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1485 | "Only globals with definition can force usage."); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1486 | LLVMUsed.emplace_back(GV); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1489 | void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { |
| 1490 | assert(!GV->isDeclaration() && |
| 1491 | "Only globals with definition can force usage."); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1492 | LLVMCompilerUsed.emplace_back(GV); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static void emitUsed(CodeGenModule &CGM, StringRef Name, |
Sanjoy Das | e369bd9 | 2017-05-01 17:08:00 +0000 | [diff] [blame] | 1496 | std::vector<llvm::WeakTrackingVH> &List) { |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1497 | // Don't create llvm.used if there is no need. |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1498 | if (List.empty()) |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1501 | // Convert List to what ConstantArray needs. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 1502 | SmallVector<llvm::Constant*, 8> UsedArray; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1503 | UsedArray.resize(List.size()); |
| 1504 | for (unsigned i = 0, e = List.size(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | UsedArray[i] = |
Justin Holewinski | f37f3d3 | 2015-02-02 21:05:49 +0000 | [diff] [blame] | 1506 | llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
| 1507 | cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 1508 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Fariborz Jahanian | 248c719 | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 1510 | if (UsedArray.empty()) |
| 1511 | return; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1512 | llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1514 | auto *GV = new llvm::GlobalVariable( |
| 1515 | CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, |
| 1516 | llvm::ConstantArray::get(ATy, UsedArray), Name); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1517 | |
| 1518 | GV->setSection("llvm.metadata"); |
| 1519 | } |
| 1520 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1521 | void CodeGenModule::emitLLVMUsed() { |
| 1522 | emitUsed(*this, "llvm.used", LLVMUsed); |
| 1523 | emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); |
| 1524 | } |
| 1525 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1526 | void CodeGenModule::AppendLinkerOptions(StringRef Opts) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1527 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1528 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1529 | } |
| 1530 | |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1531 | void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { |
| 1532 | llvm::SmallString<32> Opt; |
| 1533 | getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1534 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1535 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1536 | } |
| 1537 | |
Saleem Abdulrasool | fd4db53 | 2018-02-07 01:46:46 +0000 | [diff] [blame] | 1538 | void CodeGenModule::AddELFLibDirective(StringRef Lib) { |
| 1539 | auto &C = getLLVMContext(); |
| 1540 | LinkerOptionsMetadata.push_back(llvm::MDNode::get( |
| 1541 | C, {llvm::MDString::get(C, "lib"), llvm::MDString::get(C, Lib)})); |
| 1542 | } |
| 1543 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1544 | void CodeGenModule::AddDependentLib(StringRef Lib) { |
| 1545 | llvm::SmallString<24> Opt; |
| 1546 | getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1547 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1548 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1549 | } |
| 1550 | |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1551 | /// \brief Add link options implied by the given module, including modules |
| 1552 | /// it depends on, using a postorder walk. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1553 | static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1554 | SmallVectorImpl<llvm::MDNode *> &Metadata, |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1555 | llvm::SmallPtrSet<Module *, 16> &Visited) { |
| 1556 | // Import this module's parent. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1557 | if (Mod->Parent && Visited.insert(Mod->Parent).second) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1558 | addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | // Import this module's dependencies. |
| 1562 | for (unsigned I = Mod->Imports.size(); I > 0; --I) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1563 | if (Visited.insert(Mod->Imports[I - 1]).second) |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1564 | addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | // Add linker options to link against the libraries/frameworks |
| 1568 | // described by this module. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1569 | llvm::LLVMContext &Context = CGM.getLLVMContext(); |
Bruno Cardoso Lopes | a3b5f71 | 2018-04-16 19:42:32 +0000 | [diff] [blame] | 1570 | |
| 1571 | // For modules that use export_as for linking, use that module |
| 1572 | // name instead. |
| 1573 | if (Mod->UseExportAsModuleLinkName) |
| 1574 | return; |
| 1575 | |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1576 | for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1577 | // Link against a framework. Frameworks are currently Darwin only, so we |
| 1578 | // don't to ask TargetCodeGenInfo for the spelling of the linker option. |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1579 | if (Mod->LinkLibraries[I-1].IsFramework) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1580 | llvm::Metadata *Args[2] = { |
| 1581 | llvm::MDString::get(Context, "-framework"), |
| 1582 | llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)}; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1583 | |
| 1584 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
| 1585 | continue; |
| 1586 | } |
| 1587 | |
| 1588 | // Link against a library. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1589 | llvm::SmallString<24> Opt; |
| 1590 | CGM.getTargetCodeGenInfo().getDependentLibraryOption( |
| 1591 | Mod->LinkLibraries[I-1].Library, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1592 | auto *OptString = llvm::MDString::get(Context, Opt); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1593 | Metadata.push_back(llvm::MDNode::get(Context, OptString)); |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | void CodeGenModule::EmitModuleLinkOptions() { |
| 1598 | // Collect the set of all of the modules we want to visit to emit link |
| 1599 | // options, which is essentially the imported modules and all of their |
| 1600 | // non-explicit child modules. |
| 1601 | llvm::SetVector<clang::Module *> LinkModules; |
| 1602 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 1603 | SmallVector<clang::Module *, 16> Stack; |
| 1604 | |
| 1605 | // Seed the stack with imported modules. |
Manman Ren | 9803ee8 | 2017-01-11 18:47:38 +0000 | [diff] [blame] | 1606 | for (Module *M : ImportedModules) { |
| 1607 | // Do not add any link flags when an implementation TU of a module imports |
| 1608 | // a header of that same module. |
| 1609 | if (M->getTopLevelModuleName() == getLangOpts().CurrentModule && |
| 1610 | !getLangOpts().isCompilingModule()) |
| 1611 | continue; |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 1612 | if (Visited.insert(M).second) |
| 1613 | Stack.push_back(M); |
Manman Ren | 9803ee8 | 2017-01-11 18:47:38 +0000 | [diff] [blame] | 1614 | } |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1615 | |
| 1616 | // Find all of the modules to import, making a little effort to prune |
| 1617 | // non-leaf modules. |
| 1618 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1619 | clang::Module *Mod = Stack.pop_back_val(); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1620 | |
| 1621 | bool AnyChildren = false; |
| 1622 | |
| 1623 | // Visit the submodules of this module. |
| 1624 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 1625 | SubEnd = Mod->submodule_end(); |
| 1626 | Sub != SubEnd; ++Sub) { |
| 1627 | // Skip explicit children; they need to be explicitly imported to be |
| 1628 | // linked against. |
| 1629 | if ((*Sub)->IsExplicit) |
| 1630 | continue; |
| 1631 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1632 | if (Visited.insert(*Sub).second) { |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1633 | Stack.push_back(*Sub); |
| 1634 | AnyChildren = true; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | // We didn't find any children, so add this module to the list of |
| 1639 | // modules to link against. |
| 1640 | if (!AnyChildren) { |
| 1641 | LinkModules.insert(Mod); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | // Add link options for all of the imported modules in reverse topological |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1646 | // order. We don't do anything to try to order import link flags with respect |
| 1647 | // to linker options inserted by things like #pragma comment(). |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1648 | SmallVector<llvm::MDNode *, 16> MetadataArgs; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1649 | Visited.clear(); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 1650 | for (Module *M : LinkModules) |
| 1651 | if (Visited.insert(M).second) |
| 1652 | addLinkOptionsPostorder(*this, M, MetadataArgs, Visited); |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 1653 | std::reverse(MetadataArgs.begin(), MetadataArgs.end()); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1654 | LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end()); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1655 | |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 1656 | // Add the linker options metadata flag. |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1657 | auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options"); |
| 1658 | for (auto *MD : LinkerOptionsMetadata) |
| 1659 | NMD->addOperand(MD); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1662 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1663 | // Emit code for any potentially referenced deferred decls. Since a |
| 1664 | // previously unused static decl may become used during the generation of code |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1665 | // for a static function, iterate until no changes are made. |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 1666 | |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1667 | if (!DeferredVTables.empty()) { |
| 1668 | EmitDeferredVTables(); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1669 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 1670 | // Emitting a vtable doesn't directly cause more vtables to |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1671 | // become deferred, although it can cause functions to be |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 1672 | // emitted that then need those vtables. |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1673 | assert(DeferredVTables.empty()); |
| 1674 | } |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 1675 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 1676 | // Stop if we're out of both deferred vtables and deferred declarations. |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1677 | if (DeferredDeclsToEmit.empty()) |
| 1678 | return; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 1679 | |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1680 | // Grab the list of decls to emit. If EmitGlobalDefinition schedules more |
| 1681 | // work, it will not interfere with this. |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1682 | std::vector<GlobalDecl> CurDeclsToEmit; |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1683 | CurDeclsToEmit.swap(DeferredDeclsToEmit); |
| 1684 | |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 1685 | for (GlobalDecl &D : CurDeclsToEmit) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 1686 | // We should call GetAddrOfGlobal with IsForDefinition set to true in order |
| 1687 | // to get GlobalValue with exactly the type we need, not something that |
| 1688 | // might had been created for another decl with the same mangled name but |
| 1689 | // different type. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 1690 | llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>( |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 1691 | GetAddrOfGlobal(D, ForDefinition)); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 1692 | |
| 1693 | // In case of different address spaces, we may still get a cast, even with |
| 1694 | // IsForDefinition equal to true. Query mangled names table to get |
| 1695 | // GlobalValue. |
| 1696 | if (!GV) |
| 1697 | GV = GetGlobalValue(getMangledName(D)); |
| 1698 | |
| 1699 | // Make sure GetGlobalValue returned non-null. |
| 1700 | assert(GV); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1701 | |
John McCall | c2af939 | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 1702 | // Check to see if we've already emitted this. This is necessary |
| 1703 | // for a couple of reasons: first, decls can end up in the |
| 1704 | // deferred-decls queue multiple times, and second, decls can end |
| 1705 | // up with definitions in unusual ways (e.g. by an extern inline |
| 1706 | // function acquiring a strong function redefinition). Just |
| 1707 | // ignore these cases. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 1708 | if (!GV->isDeclaration()) |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1709 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1711 | // Otherwise, emit the definition and move on to the next one. |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1712 | EmitGlobalDefinition(D, GV); |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 1713 | |
| 1714 | // If we found out that we need to emit more decls, do that recursively. |
| 1715 | // This has the advantage that the decls are emitted in a DFS and related |
| 1716 | // ones are close together, which is convenient for testing. |
| 1717 | if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) { |
| 1718 | EmitDeferred(); |
| 1719 | assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty()); |
| 1720 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1721 | } |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1722 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1723 | |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 1724 | void CodeGenModule::EmitVTablesOpportunistically() { |
| 1725 | // Try to emit external vtables as available_externally if they have emitted |
| 1726 | // all inlined virtual functions. It runs after EmitDeferred() and therefore |
| 1727 | // is not allowed to create new references to things that need to be emitted |
| 1728 | // lazily. Note that it also uses fact that we eagerly emitting RTTI. |
| 1729 | |
Piotr Padlewski | 0554774 | 2017-06-01 09:24:36 +0000 | [diff] [blame] | 1730 | assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) |
| 1731 | && "Only emit opportunistic vtables with optimizations"); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 1732 | |
| 1733 | for (const CXXRecordDecl *RD : OpportunisticVTables) { |
| 1734 | assert(getVTables().isVTableExternal(RD) && |
| 1735 | "This queue should only contain external vtables"); |
| 1736 | if (getCXXABI().canSpeculativelyEmitVTable(RD)) |
| 1737 | VTables.GenerateClassData(RD); |
| 1738 | } |
| 1739 | OpportunisticVTables.clear(); |
| 1740 | } |
| 1741 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1742 | void CodeGenModule::EmitGlobalAnnotations() { |
| 1743 | if (Annotations.empty()) |
| 1744 | return; |
| 1745 | |
| 1746 | // Create a new global variable for the ConstantStruct in the Module. |
| 1747 | llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( |
| 1748 | Annotations[0]->getType(), Annotations.size()), Annotations); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1749 | auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false, |
| 1750 | llvm::GlobalValue::AppendingLinkage, |
| 1751 | Array, "llvm.global.annotations"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1752 | gv->setSection(AnnotationSection); |
| 1753 | } |
| 1754 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1755 | llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) { |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 1756 | llvm::Constant *&AStr = AnnotationStrings[Str]; |
| 1757 | if (AStr) |
| 1758 | return AStr; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Not found yet, create a new global. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 1761 | llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1762 | auto *gv = |
| 1763 | new llvm::GlobalVariable(getModule(), s->getType(), true, |
| 1764 | llvm::GlobalValue::PrivateLinkage, s, ".str"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1765 | gv->setSection(AnnotationSection); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1766 | gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 1767 | AStr = gv; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1768 | return gv; |
| 1769 | } |
| 1770 | |
| 1771 | llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { |
| 1772 | SourceManager &SM = getContext().getSourceManager(); |
| 1773 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 1774 | if (PLoc.isValid()) |
| 1775 | return EmitAnnotationString(PLoc.getFilename()); |
| 1776 | return EmitAnnotationString(SM.getBufferName(Loc)); |
| 1777 | } |
| 1778 | |
| 1779 | llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { |
| 1780 | SourceManager &SM = getContext().getSourceManager(); |
| 1781 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 1782 | unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : |
| 1783 | SM.getExpansionLineNumber(L); |
| 1784 | return llvm::ConstantInt::get(Int32Ty, LineNo); |
| 1785 | } |
| 1786 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1788 | const AnnotateAttr *AA, |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1789 | SourceLocation L) { |
| 1790 | // Get the globals for file name, annotation, and the line number. |
| 1791 | llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), |
| 1792 | *UnitGV = EmitAnnotationUnit(L), |
| 1793 | *LineNoCst = EmitAnnotationLineNo(L); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1794 | |
Daniel Dunbar | 346892a | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 1795 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1796 | llvm::Constant *Fields[4] = { |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1797 | llvm::ConstantExpr::getBitCast(GV, Int8PtrTy), |
| 1798 | llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), |
| 1799 | llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), |
| 1800 | LineNoCst |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1801 | }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1802 | return llvm::ConstantStruct::getAnon(Fields); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1805 | void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, |
| 1806 | llvm::GlobalValue *GV) { |
| 1807 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 1808 | // Get the struct elements for these annotations. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1809 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) |
| 1810 | Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1813 | bool CodeGenModule::isInSanitizerBlacklist(SanitizerMask Kind, |
| 1814 | llvm::Function *Fn, |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1815 | SourceLocation Loc) const { |
| 1816 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
| 1817 | // Blacklist by function name. |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1818 | if (SanitizerBL.isBlacklistedFunction(Kind, Fn->getName())) |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1819 | return true; |
| 1820 | // Blacklist by location. |
Yaron Keren | ed1fe5d | 2015-10-03 05:15:57 +0000 | [diff] [blame] | 1821 | if (Loc.isValid()) |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1822 | return SanitizerBL.isBlacklistedLocation(Kind, Loc); |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1823 | // If location is unknown, this may be a compiler-generated function. Assume |
| 1824 | // it's located in the main file. |
| 1825 | auto &SM = Context.getSourceManager(); |
| 1826 | if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1827 | return SanitizerBL.isBlacklistedFile(Kind, MainFile->getName()); |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1828 | } |
| 1829 | return false; |
| 1830 | } |
| 1831 | |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1832 | bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV, |
| 1833 | SourceLocation Loc, QualType Ty, |
| 1834 | StringRef Category) const { |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 1835 | // For now globals can be blacklisted only in ASan and KASan. |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1836 | const SanitizerMask EnabledAsanMask = LangOpts.Sanitize.Mask & |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame] | 1837 | (SanitizerKind::Address | SanitizerKind::KernelAddress | |
| 1838 | SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1839 | if (!EnabledAsanMask) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1840 | return false; |
| 1841 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1842 | if (SanitizerBL.isBlacklistedGlobal(EnabledAsanMask, GV->getName(), Category)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1843 | return true; |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1844 | if (SanitizerBL.isBlacklistedLocation(EnabledAsanMask, Loc, Category)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1845 | return true; |
| 1846 | // Check global type. |
| 1847 | if (!Ty.isNull()) { |
| 1848 | // Drill down the array types: if global variable of a fixed type is |
| 1849 | // blacklisted, we also don't instrument arrays of them. |
| 1850 | while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr())) |
| 1851 | Ty = AT->getElementType(); |
| 1852 | Ty = Ty.getCanonicalType().getUnqualifiedType(); |
| 1853 | // We allow to blacklist only record types (classes, structs etc.) |
| 1854 | if (Ty->isRecordType()) { |
| 1855 | std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy()); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 1856 | if (SanitizerBL.isBlacklistedType(EnabledAsanMask, TypeStr, Category)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 1857 | return true; |
| 1858 | } |
| 1859 | } |
| 1860 | return false; |
| 1861 | } |
| 1862 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 1863 | bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, |
| 1864 | StringRef Category) const { |
| 1865 | if (!LangOpts.XRayInstrument) |
| 1866 | return false; |
Dean Michael Berris | 488f7c2 | 2018-04-13 02:31:58 +0000 | [diff] [blame] | 1867 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 1868 | const auto &XRayFilter = getContext().getXRayFilter(); |
| 1869 | using ImbueAttr = XRayFunctionFilter::ImbueAttribute; |
Dean Michael Berris | 488f7c2 | 2018-04-13 02:31:58 +0000 | [diff] [blame] | 1870 | auto Attr = ImbueAttr::NONE; |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 1871 | if (Loc.isValid()) |
| 1872 | Attr = XRayFilter.shouldImbueLocation(Loc, Category); |
| 1873 | if (Attr == ImbueAttr::NONE) |
| 1874 | Attr = XRayFilter.shouldImbueFunction(Fn->getName()); |
| 1875 | switch (Attr) { |
| 1876 | case ImbueAttr::NONE: |
| 1877 | return false; |
| 1878 | case ImbueAttr::ALWAYS: |
| 1879 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 1880 | break; |
Dean Michael Berris | 170429e | 2017-05-24 05:46:36 +0000 | [diff] [blame] | 1881 | case ImbueAttr::ALWAYS_ARG1: |
| 1882 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 1883 | Fn->addFnAttr("xray-log-args", "1"); |
| 1884 | break; |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 1885 | case ImbueAttr::NEVER: |
| 1886 | Fn->addFnAttr("function-instrument", "xray-never"); |
| 1887 | break; |
| 1888 | } |
| 1889 | return true; |
| 1890 | } |
| 1891 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1892 | bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 1893 | // Never defer when EmitAllDecls is specified. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1894 | if (LangOpts.EmitAllDecls) |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1895 | return true; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1896 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1897 | return getContext().DeclMustBeEmitted(Global); |
| 1898 | } |
| 1899 | |
| 1900 | bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) { |
| 1901 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) |
| 1902 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 1903 | // Implicit template instantiations may change linkage if they are later |
| 1904 | // explicitly instantiated, so they should not be emitted eagerly. |
| 1905 | return false; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 1906 | if (const auto *VD = dyn_cast<VarDecl>(Global)) |
| 1907 | if (Context.getInlineVariableDefinitionKind(VD) == |
| 1908 | ASTContext::InlineVariableDefinitionKind::WeakUnknown) |
| 1909 | // A definition of an inline constexpr static data member may change |
| 1910 | // linkage later if it's redeclared outside the class. |
| 1911 | return false; |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1912 | // If OpenMP is enabled and threadprivates must be generated like TLS, delay |
| 1913 | // codegen for global variables, because they may be marked as threadprivate. |
| 1914 | if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS && |
| 1915 | getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global)) |
| 1916 | return false; |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 1917 | |
| 1918 | return true; |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1921 | ConstantAddress CodeGenModule::GetAddrOfUuidDescriptor( |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1922 | const CXXUuidofExpr* E) { |
| 1923 | // Sema has verified that IIDSource has a __declspec(uuid()), and that its |
| 1924 | // well-formed. |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 1925 | StringRef Uuid = E->getUuidStr(); |
David Majnemer | cf963ce | 2013-08-09 08:35:59 +0000 | [diff] [blame] | 1926 | std::string Name = "_GUID_" + Uuid.lower(); |
| 1927 | std::replace(Name.begin(), Name.end(), '-', '_'); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1928 | |
David Majnemer | 6568760 | 2016-03-27 04:46:14 +0000 | [diff] [blame] | 1929 | // The UUID descriptor should be pointer aligned. |
| 1930 | CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1931 | |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1932 | // Look for an existing global. |
| 1933 | if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1934 | return ConstantAddress(GV, Alignment); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1935 | |
Nico Weber | 17d3a2c | 2014-09-08 16:26:36 +0000 | [diff] [blame] | 1936 | llvm::Constant *Init = EmitUuidofInitializer(Uuid); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1937 | assert(Init && "failed to initialize as constant"); |
| 1938 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1939 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 1940 | getModule(), Init->getType(), |
Reid Kleckner | b9921df | 2013-09-03 21:49:32 +0000 | [diff] [blame] | 1941 | /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 1942 | if (supportsCOMDAT()) |
| 1943 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Rafael Espindola | ca08d24 | 2018-03-20 15:42:58 +0000 | [diff] [blame] | 1944 | setDSOLocal(GV); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1945 | return ConstantAddress(GV, Alignment); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1948 | ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1949 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
| 1950 | assert(AA && "No alias?"); |
| 1951 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1952 | CharUnits Alignment = getContext().getDeclAlign(VD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1953 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1954 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1955 | // 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] | 1956 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1957 | if (Entry) { |
| 1958 | unsigned AS = getContext().getTargetAddressSpace(VD->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1959 | auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS)); |
| 1960 | return ConstantAddress(Ptr, Alignment); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1961 | } |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1962 | |
| 1963 | llvm::Constant *Aliasee; |
| 1964 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 1965 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, |
| 1966 | GlobalDecl(cast<FunctionDecl>(VD)), |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 1967 | /*ForVTable=*/false); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1968 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1969 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1970 | llvm::PointerType::getUnqual(DeclTy), |
| 1971 | nullptr); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1972 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1973 | auto *F = cast<llvm::GlobalValue>(Aliasee); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1974 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 1975 | WeakRefReferences.insert(F); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1976 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1977 | return ConstantAddress(Aliasee, Alignment); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1980 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1981 | const auto *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1983 | // Weak references don't produce any output by themselves. |
| 1984 | if (Global->hasAttr<WeakRefAttr>()) |
| 1985 | return; |
| 1986 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1987 | // If this is an alias definition (which otherwise looks like a declaration) |
| 1988 | // emit it now. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1989 | if (Global->hasAttr<AliasAttr>()) |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 1990 | return EmitAliasDefinition(GD); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1991 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1992 | // IFunc like an alias whose value is resolved at runtime by calling resolver. |
| 1993 | if (Global->hasAttr<IFuncAttr>()) |
| 1994 | return emitIFuncDefinition(GD); |
| 1995 | |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1996 | // If this is CUDA, be selective about which declarations we emit. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1997 | if (LangOpts.CUDA) { |
Artem Belevich | f3d3db6 | 2015-03-19 18:58:18 +0000 | [diff] [blame] | 1998 | if (LangOpts.CUDAIsDevice) { |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1999 | if (!Global->hasAttr<CUDADeviceAttr>() && |
| 2000 | !Global->hasAttr<CUDAGlobalAttr>() && |
| 2001 | !Global->hasAttr<CUDAConstantAttr>() && |
| 2002 | !Global->hasAttr<CUDASharedAttr>()) |
| 2003 | return; |
| 2004 | } else { |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2005 | // We need to emit host-side 'shadows' for all global |
| 2006 | // device-side variables because the CUDA runtime needs their |
| 2007 | // size and host-side address in order to provide access to |
| 2008 | // their device-side incarnations. |
| 2009 | |
| 2010 | // So device-only functions are the only things we skip. |
| 2011 | if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() && |
| 2012 | Global->hasAttr<CUDADeviceAttr>()) |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2013 | return; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2014 | |
| 2015 | assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && |
| 2016 | "Expected Variable or Function"); |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 2020 | if (LangOpts.OpenMP) { |
| 2021 | // If this is OpenMP device, check if it is legal to emit this global |
| 2022 | // normally. |
| 2023 | if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD)) |
| 2024 | return; |
| 2025 | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) { |
| 2026 | if (MustBeEmitted(Global)) |
| 2027 | EmitOMPDeclareReduction(DRD); |
| 2028 | return; |
| 2029 | } |
| 2030 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2032 | // Ignore declarations, they will be emitted on their first use. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2033 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 2034 | // Forward declarations are emitted lazily on first use. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2035 | if (!FD->doesThisDeclarationHaveABody()) { |
| 2036 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
| 2037 | return; |
| 2038 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2039 | StringRef MangledName = getMangledName(GD); |
David Majnemer | af36980 | 2014-03-29 14:19:55 +0000 | [diff] [blame] | 2040 | |
| 2041 | // Compute the function info and LLVM type. |
| 2042 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 2043 | llvm::Type *Ty = getTypes().GetFunctionType(FI); |
| 2044 | |
| 2045 | GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false, |
| 2046 | /*DontDefer=*/false); |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 2047 | return; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2048 | } |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 2049 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2050 | const auto *VD = cast<VarDecl>(Global); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2051 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2052 | // We need to emit device-side global CUDA variables even if a |
| 2053 | // variable does not have a definition -- we still need to define |
| 2054 | // host-side shadow for it. |
| 2055 | bool MustEmitForCuda = LangOpts.CUDA && !LangOpts.CUDAIsDevice && |
| 2056 | !VD->hasDefinition() && |
| 2057 | (VD->hasAttr<CUDAConstantAttr>() || |
| 2058 | VD->hasAttr<CUDADeviceAttr>()); |
| 2059 | if (!MustEmitForCuda && |
| 2060 | VD->isThisDeclarationADefinition() != VarDecl::Definition && |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 2061 | !Context.isMSStaticDataMemberInlineDefinition(VD)) { |
| 2062 | // If this declaration may have caused an inline variable definition to |
| 2063 | // change linkage, make sure that it's emitted. |
| 2064 | if (Context.getInlineVariableDefinitionKind(VD) == |
| 2065 | ASTContext::InlineVariableDefinitionKind::Strong) |
| 2066 | GetAddrOfGlobalVar(VD); |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 2067 | return; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 2068 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2071 | // Defer code generation to first use when possible, e.g. if this is an inline |
| 2072 | // function. If the global must always be emitted, do it eagerly if possible |
| 2073 | // to benefit from cache locality. |
| 2074 | if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) { |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 2075 | // Emit the definition if it can't be deferred. |
| 2076 | EmitGlobalDefinition(GD); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2079 | |
| 2080 | // If we're deferring emission of a C++ variable with an |
| 2081 | // initializer, remember the order in which it appeared in the file. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2082 | if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) && |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2083 | cast<VarDecl>(Global)->hasInit()) { |
| 2084 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2085 | CXXGlobalInits.push_back(nullptr); |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2086 | } |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2087 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2088 | StringRef MangledName = getMangledName(GD); |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2089 | if (GetGlobalValue(MangledName) != nullptr) { |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2090 | // The value has already been used and should therefore be emitted. |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2091 | addDeferredDeclToEmit(GD); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2092 | } else if (MustBeEmitted(Global)) { |
| 2093 | // The value must be emitted, but cannot be emitted eagerly. |
| 2094 | assert(!MayBeEmittedEagerly(Global)); |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2095 | addDeferredDeclToEmit(GD); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2096 | } else { |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 2097 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 2098 | // first use of the mangled name will cause it to move into |
| 2099 | // DeferredDeclsToEmit. |
| 2100 | DeferredDecls[MangledName] = GD; |
| 2101 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2104 | // Check if T is a class type with a destructor that's not dllimport. |
| 2105 | static bool HasNonDllImportDtor(QualType T) { |
| 2106 | if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>()) |
| 2107 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2108 | if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>()) |
| 2109 | return true; |
| 2110 | |
| 2111 | return false; |
| 2112 | } |
| 2113 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2114 | namespace { |
| 2115 | struct FunctionIsDirectlyRecursive : |
| 2116 | public RecursiveASTVisitor<FunctionIsDirectlyRecursive> { |
| 2117 | const StringRef Name; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2118 | const Builtin::Context &BI; |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2119 | bool Result; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2120 | FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) : |
| 2121 | Name(N), BI(C), Result(false) { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2122 | } |
| 2123 | typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base; |
| 2124 | |
| 2125 | bool TraverseCallExpr(CallExpr *E) { |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2126 | const FunctionDecl *FD = E->getDirectCallee(); |
| 2127 | if (!FD) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2128 | return true; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2129 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 2130 | if (Attr && Name == Attr->getLabel()) { |
| 2131 | Result = true; |
| 2132 | return false; |
| 2133 | } |
| 2134 | unsigned BuiltinID = FD->getBuiltinID(); |
David Majnemer | 41011f6 | 2015-06-25 23:50:40 +0000 | [diff] [blame] | 2135 | if (!BuiltinID || !BI.isLibFunction(BuiltinID)) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2136 | return true; |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 2137 | StringRef BuiltinName = BI.getName(BuiltinID); |
Nick Lewycky | 9ed5b15 | 2012-01-18 03:41:19 +0000 | [diff] [blame] | 2138 | if (BuiltinName.startswith("__builtin_") && |
| 2139 | Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2140 | Result = true; |
| 2141 | return false; |
| 2142 | } |
| 2143 | return true; |
| 2144 | } |
| 2145 | }; |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2146 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2147 | // Make sure we're not referencing non-imported vars or functions. |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2148 | struct DLLImportFunctionVisitor |
| 2149 | : public RecursiveASTVisitor<DLLImportFunctionVisitor> { |
| 2150 | bool SafeToInline = true; |
| 2151 | |
Hans Wennborg | 1b3aee7 | 2016-09-13 22:51:42 +0000 | [diff] [blame] | 2152 | bool shouldVisitImplicitCode() const { return true; } |
| 2153 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2154 | bool VisitVarDecl(VarDecl *VD) { |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2155 | if (VD->getTLSKind()) { |
| 2156 | // A thread-local variable cannot be imported. |
| 2157 | SafeToInline = false; |
| 2158 | return SafeToInline; |
| 2159 | } |
| 2160 | |
| 2161 | // A variable definition might imply a destructor call. |
| 2162 | if (VD->isThisDeclarationADefinition()) |
| 2163 | SafeToInline = !HasNonDllImportDtor(VD->getType()); |
| 2164 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2165 | return SafeToInline; |
| 2166 | } |
| 2167 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2168 | bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 2169 | if (const auto *D = E->getTemporary()->getDestructor()) |
| 2170 | SafeToInline = D->hasAttr<DLLImportAttr>(); |
| 2171 | return SafeToInline; |
| 2172 | } |
| 2173 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2174 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2175 | ValueDecl *VD = E->getDecl(); |
| 2176 | if (isa<FunctionDecl>(VD)) |
| 2177 | SafeToInline = VD->hasAttr<DLLImportAttr>(); |
| 2178 | else if (VarDecl *V = dyn_cast<VarDecl>(VD)) |
| 2179 | SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>(); |
| 2180 | return SafeToInline; |
| 2181 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2182 | |
Hans Wennborg | 1b3aee7 | 2016-09-13 22:51:42 +0000 | [diff] [blame] | 2183 | bool VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 2184 | SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>(); |
| 2185 | return SafeToInline; |
| 2186 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2187 | |
Hans Wennborg | 251c204 | 2017-01-23 23:57:50 +0000 | [diff] [blame] | 2188 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2189 | CXXMethodDecl *M = E->getMethodDecl(); |
| 2190 | if (!M) { |
| 2191 | // Call through a pointer to member function. This is safe to inline. |
| 2192 | SafeToInline = true; |
| 2193 | } else { |
| 2194 | SafeToInline = M->hasAttr<DLLImportAttr>(); |
| 2195 | } |
| 2196 | return SafeToInline; |
| 2197 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2198 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2199 | bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 2200 | SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>(); |
| 2201 | return SafeToInline; |
| 2202 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2203 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2204 | bool VisitCXXNewExpr(CXXNewExpr *E) { |
| 2205 | SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>(); |
| 2206 | return SafeToInline; |
| 2207 | } |
| 2208 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2209 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2210 | |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2211 | // isTriviallyRecursive - Check if this function calls another |
| 2212 | // decl that, because of the asm attribute or the other decl being a builtin, |
| 2213 | // ends up pointing to itself. |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2214 | bool |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2215 | CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) { |
| 2216 | StringRef Name; |
| 2217 | if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) { |
Nick Lewycky | 72cd229 | 2012-01-18 01:50:13 +0000 | [diff] [blame] | 2218 | // asm labels are a special kind of mangling we have to support. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2219 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 2220 | if (!Attr) |
| 2221 | return false; |
| 2222 | Name = Attr->getLabel(); |
| 2223 | } else { |
| 2224 | Name = FD->getName(); |
| 2225 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2226 | |
David Majnemer | 64b0bdf | 2015-06-30 04:41:18 +0000 | [diff] [blame] | 2227 | FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo); |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2228 | Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD)); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2229 | return Walker.Result; |
| 2230 | } |
| 2231 | |
Hans Wennborg | 93f7547 | 2016-09-13 21:08:20 +0000 | [diff] [blame] | 2232 | bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) { |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 2233 | if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2234 | return true; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2235 | const auto *F = cast<FunctionDecl>(GD.getDecl()); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 2236 | if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>()) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2237 | return false; |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2238 | |
| 2239 | if (F->hasAttr<DLLImportAttr>()) { |
| 2240 | // Check whether it would be safe to inline this dllimport function. |
| 2241 | DLLImportFunctionVisitor Visitor; |
| 2242 | Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F)); |
| 2243 | if (!Visitor.SafeToInline) |
| 2244 | return false; |
Hans Wennborg | 93f7547 | 2016-09-13 21:08:20 +0000 | [diff] [blame] | 2245 | |
| 2246 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) { |
| 2247 | // Implicit destructor invocations aren't captured in the AST, so the |
| 2248 | // check above can't see them. Check for them manually here. |
| 2249 | for (const Decl *Member : Dtor->getParent()->decls()) |
| 2250 | if (isa<FieldDecl>(Member)) |
| 2251 | if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType())) |
| 2252 | return false; |
| 2253 | for (const CXXBaseSpecifier &B : Dtor->getParent()->bases()) |
| 2254 | if (HasNonDllImportDtor(B.getType())) |
| 2255 | return false; |
| 2256 | } |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2259 | // PR9614. Avoid cases where the source code is lying to us. An available |
| 2260 | // externally function should have an equivalent function somewhere else, |
| 2261 | // but a function that calls itself is clearly not equivalent to the real |
| 2262 | // implementation. |
| 2263 | // This happens in glibc's btowc and in some configure checks. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2264 | return !isTriviallyRecursive(F); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 2267 | bool CodeGenModule::shouldOpportunisticallyEmitVTables() { |
| 2268 | return CodeGenOpts.OptimizationLevel > 0; |
| 2269 | } |
| 2270 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2271 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2272 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2274 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
Anders Carlsson | 29295bf | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 2275 | Context.getSourceManager(), |
| 2276 | "Generating code for declaration"); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2277 | |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 2278 | if (isa<FunctionDecl>(D)) { |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2279 | // At -O0, don't generate IR for functions with available_externally |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2280 | // linkage. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 2281 | if (!shouldEmitFunction(GD)) |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2282 | return; |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 2283 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2284 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2285 | // Make sure to emit the definition(s) before we emit the thunks. |
| 2286 | // This is necessary for the generation of certain thunks. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2287 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(Method)) |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 2288 | ABI->emitCXXStructor(CD, getFromCtorType(GD.getCtorType())); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2289 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(Method)) |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 2290 | ABI->emitCXXStructor(DD, getFromDtorType(GD.getDtorType())); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2291 | else |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2292 | EmitGlobalFunctionDefinition(GD, GV); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2294 | if (Method->isVirtual()) |
| 2295 | getVTables().EmitThunks(GD); |
| 2296 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2297 | return; |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2298 | } |
Chris Lattner | d8d760c | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 2299 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2300 | return EmitGlobalFunctionDefinition(GD, GV); |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2301 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2302 | |
| 2303 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2304 | return EmitGlobalVarDefinition(VD, !VD->hasDefinition()); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2305 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2306 | llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2309 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 2310 | llvm::Function *NewFn); |
| 2311 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2312 | void CodeGenModule::emitMultiVersionFunctions() { |
| 2313 | for (GlobalDecl GD : MultiVersionFuncs) { |
| 2314 | SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; |
| 2315 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 2316 | getContext().forEachMultiversionedFunctionVersion( |
| 2317 | FD, [this, &GD, &Options](const FunctionDecl *CurFD) { |
| 2318 | GlobalDecl CurGD{ |
| 2319 | (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)}; |
| 2320 | StringRef MangledName = getMangledName(CurGD); |
| 2321 | llvm::Constant *Func = GetGlobalValue(MangledName); |
| 2322 | if (!Func) { |
| 2323 | if (CurFD->isDefined()) { |
| 2324 | EmitGlobalFunctionDefinition(CurGD, nullptr); |
| 2325 | Func = GetGlobalValue(MangledName); |
| 2326 | } else { |
| 2327 | const CGFunctionInfo &FI = |
| 2328 | getTypes().arrangeGlobalDeclaration(GD); |
| 2329 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
| 2330 | Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false, |
| 2331 | /*DontDefer=*/false, ForDefinition); |
| 2332 | } |
| 2333 | assert(Func && "This should have just been created"); |
| 2334 | } |
| 2335 | Options.emplace_back(getTarget(), cast<llvm::Function>(Func), |
| 2336 | CurFD->getAttr<TargetAttr>()->parse()); |
| 2337 | }); |
| 2338 | |
| 2339 | llvm::Function *ResolverFunc = cast<llvm::Function>( |
| 2340 | GetGlobalValue((getMangledName(GD) + ".resolver").str())); |
Erich Keane | 0a6fde4 | 2018-01-16 19:49:52 +0000 | [diff] [blame] | 2341 | if (supportsCOMDAT()) |
| 2342 | ResolverFunc->setComdat( |
| 2343 | getModule().getOrInsertComdat(ResolverFunc->getName())); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2344 | std::stable_sort( |
| 2345 | Options.begin(), Options.end(), |
| 2346 | std::greater<CodeGenFunction::MultiVersionResolverOption>()); |
| 2347 | CodeGenFunction CGF(*this); |
| 2348 | CGF.EmitMultiVersionResolver(ResolverFunc, Options); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | /// If an ifunc for the specified mangled name is not in the module, create and |
| 2353 | /// return an llvm IFunc Function with the specified type. |
| 2354 | llvm::Constant * |
| 2355 | CodeGenModule::GetOrCreateMultiVersionIFunc(GlobalDecl GD, llvm::Type *DeclTy, |
| 2356 | StringRef MangledName, |
| 2357 | const FunctionDecl *FD) { |
| 2358 | std::string IFuncName = (MangledName + ".ifunc").str(); |
| 2359 | if (llvm::GlobalValue *IFuncGV = GetGlobalValue(IFuncName)) |
| 2360 | return IFuncGV; |
| 2361 | |
| 2362 | // Since this is the first time we've created this IFunc, make sure |
| 2363 | // that we put this multiversioned function into the list to be |
| 2364 | // replaced later. |
| 2365 | MultiVersionFuncs.push_back(GD); |
| 2366 | |
| 2367 | std::string ResolverName = (MangledName + ".resolver").str(); |
| 2368 | llvm::Type *ResolverType = llvm::FunctionType::get( |
| 2369 | llvm::PointerType::get(DeclTy, |
| 2370 | Context.getTargetAddressSpace(FD->getType())), |
| 2371 | false); |
| 2372 | llvm::Constant *Resolver = |
| 2373 | GetOrCreateLLVMFunction(ResolverName, ResolverType, GlobalDecl{}, |
| 2374 | /*ForVTable=*/false); |
| 2375 | llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create( |
| 2376 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Resolver, &getModule()); |
| 2377 | GIF->setName(IFuncName); |
| 2378 | SetCommonAttributes(FD, GIF); |
| 2379 | |
| 2380 | return GIF; |
| 2381 | } |
| 2382 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2383 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 2384 | /// module, create and return an llvm Function with the specified type. If there |
| 2385 | /// is something in the module with the specified name, return it potentially |
| 2386 | /// bitcasted to the right type. |
| 2387 | /// |
| 2388 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 2389 | /// to set the attributes on the function when it is first created. |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2390 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction( |
| 2391 | StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable, |
| 2392 | bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs, |
| 2393 | ForDefinition_t IsForDefinition) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2394 | const Decl *D = GD.getDecl(); |
| 2395 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2396 | // Any attempts to use a MultiVersion function should result in retrieving |
| 2397 | // the iFunc instead. Name Mangling will handle the rest of the changes. |
| 2398 | if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 2399 | // For the device mark the function as one that should be emitted. |
| 2400 | if (getLangOpts().OpenMPIsDevice && OpenMPRuntime && |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame^] | 2401 | !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() && |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 2402 | !DontDefer && !IsForDefinition) |
| 2403 | addDeferredDeclToEmit(GD); |
| 2404 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2405 | if (FD->isMultiVersion() && FD->getAttr<TargetAttr>()->isDefaultVersion()) { |
| 2406 | UpdateMultiVersionNames(GD, FD); |
| 2407 | if (!IsForDefinition) |
| 2408 | return GetOrCreateMultiVersionIFunc(GD, Ty, MangledName, FD); |
| 2409 | } |
| 2410 | } |
| 2411 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2412 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2413 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2414 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 2415 | if (WeakRefReferences.erase(Entry)) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2416 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2417 | if (FD && !FD->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 2418 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 2421 | // Handle dropped DLL attributes. |
Rafael Espindola | c9643d8 | 2018-03-29 16:45:18 +0000 | [diff] [blame] | 2422 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) { |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 2423 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
Rafael Espindola | c9643d8 | 2018-03-29 16:45:18 +0000 | [diff] [blame] | 2424 | setDSOLocal(Entry); |
| 2425 | } |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 2426 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2427 | // If there are two attempts to define the same mangled name, issue an |
| 2428 | // error. |
| 2429 | if (IsForDefinition && !Entry->isDeclaration()) { |
| 2430 | GlobalDecl OtherGD; |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2431 | // Check that GD is not yet in DiagnosedConflictingDefinitions is required |
| 2432 | // to make sure that we issue an error only once. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2433 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
| 2434 | (GD.getCanonicalDecl().getDecl() != |
| 2435 | OtherGD.getCanonicalDecl().getDecl()) && |
| 2436 | DiagnosedConflictingDefinitions.insert(GD).second) { |
| 2437 | getDiags().Report(D->getLocation(), |
| 2438 | diag::err_duplicate_mangled_name); |
| 2439 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
| 2440 | diag::note_previous_definition); |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) && |
| 2445 | (Entry->getType()->getElementType() == Ty)) { |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2446 | return Entry; |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2447 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2449 | // Make sure the result is of the correct type. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2450 | // (If function is requested for a definition, we always need to create a new |
| 2451 | // function, not just return a bitcast.) |
| 2452 | if (!IsForDefinition) |
| 2453 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2454 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 2456 | // This function doesn't have a complete type (for example, the return |
| 2457 | // type is an incomplete struct). Use a fake type instead, and make |
| 2458 | // sure not to try to set attributes. |
| 2459 | bool IsIncompleteFunction = false; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2460 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2461 | llvm::FunctionType *FTy; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2462 | if (isa<llvm::FunctionType>(Ty)) { |
| 2463 | FTy = cast<llvm::FunctionType>(Ty); |
| 2464 | } else { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 2465 | FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 2466 | IsIncompleteFunction = true; |
| 2467 | } |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2468 | |
| 2469 | llvm::Function *F = |
| 2470 | llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, |
| 2471 | Entry ? StringRef() : MangledName, &getModule()); |
| 2472 | |
| 2473 | // If we already created a function with the same mangled name (but different |
| 2474 | // type) before, take its name and add it to the list of functions to be |
| 2475 | // replaced with F at the end of CodeGen. |
| 2476 | // |
| 2477 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 2478 | // then a definition of a different type (e.g. "int f(int x)"). |
| 2479 | if (Entry) { |
| 2480 | F->takeName(Entry); |
| 2481 | |
| 2482 | // This might be an implementation of a function without a prototype, in |
| 2483 | // which case, try to do special replacement of calls which match the new |
| 2484 | // prototype. The really key thing here is that we also potentially drop |
| 2485 | // arguments from the call site so as to make a direct call, which makes the |
| 2486 | // inliner happier and suppresses a number of optimizer warnings (!) about |
| 2487 | // dropping arguments. |
| 2488 | if (!Entry->use_empty()) { |
| 2489 | ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F); |
| 2490 | Entry->removeDeadConstantUsers(); |
| 2491 | } |
| 2492 | |
| 2493 | llvm::Constant *BC = llvm::ConstantExpr::getBitCast( |
| 2494 | F, Entry->getType()->getElementType()->getPointerTo()); |
| 2495 | addGlobalValReplacement(Entry, BC); |
| 2496 | } |
| 2497 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2498 | assert(F->getName() == MangledName && "name was uniqued!"); |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2499 | if (D) |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2500 | SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk); |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2501 | if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) { |
| 2502 | llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 2503 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 2504 | } |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 2505 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2506 | if (!DontDefer) { |
| 2507 | // All MSVC dtors other than the base dtor are linkonce_odr and delegate to |
| 2508 | // each other bottoming out with the base dtor. Therefore we emit non-base |
| 2509 | // dtors on usage, even if there is no dtor definition in the TU. |
| 2510 | if (D && isa<CXXDestructorDecl>(D) && |
| 2511 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
| 2512 | GD.getDtorType())) |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2513 | addDeferredDeclToEmit(GD); |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 2514 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2515 | // This is the first use or definition of a mangled name. If there is a |
| 2516 | // deferred decl with this name, remember that we need to emit it at the end |
| 2517 | // of the file. |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 2518 | auto DDI = DeferredDecls.find(MangledName); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2519 | if (DDI != DeferredDecls.end()) { |
| 2520 | // Move the potentially referenced deferred decl to the |
| 2521 | // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we |
| 2522 | // don't need it anymore). |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2523 | addDeferredDeclToEmit(DDI->second); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2524 | DeferredDecls.erase(DDI); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2525 | |
| 2526 | // Otherwise, there are cases we have to worry about where we're |
| 2527 | // using a declaration for which we must emit a definition but where |
| 2528 | // we might not find a top-level definition: |
| 2529 | // - member functions defined inline in their classes |
| 2530 | // - friend functions defined inline in some class |
| 2531 | // - special member functions with implicit definitions |
| 2532 | // If we ever change our AST traversal to walk into class methods, |
| 2533 | // this will be unnecessary. |
| 2534 | // |
| 2535 | // 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] | 2536 | // entry in a vtable, unless it's already marked as used. |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2537 | } else if (getLangOpts().CPlusPlus && D) { |
| 2538 | // Look for a declaration that's lexically in a record. |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2539 | for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD; |
| 2540 | FD = FD->getPreviousDecl()) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2541 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2542 | if (FD->doesThisDeclarationHaveABody()) { |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2543 | addDeferredDeclToEmit(GD.getWithDecl(FD)); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2544 | break; |
| 2545 | } |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 2546 | } |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 2547 | } |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2548 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2549 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2551 | // Make sure the result is of the requested type. |
| 2552 | if (!IsIncompleteFunction) { |
| 2553 | assert(F->getType()->getElementType() == Ty); |
| 2554 | return F; |
| 2555 | } |
| 2556 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2557 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 2558 | return llvm::ConstantExpr::getBitCast(F, PTy); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2561 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 2562 | /// non-null, then this function will use the specified type if it has to |
| 2563 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 2564 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2565 | llvm::Type *Ty, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2566 | bool ForVTable, |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2567 | bool DontDefer, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2568 | ForDefinition_t IsForDefinition) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2569 | // If there was no specific requested type, just convert it now. |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2570 | if (!Ty) { |
| 2571 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
| 2572 | auto CanonTy = Context.getCanonicalType(FD->getType()); |
| 2573 | Ty = getTypes().ConvertFunctionType(CanonTy, FD); |
| 2574 | } |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2575 | |
Reid Kleckner | 2810325 | 2018-03-16 22:20:57 +0000 | [diff] [blame] | 2576 | // Devirtualized destructor calls may come through here instead of via |
| 2577 | // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead |
| 2578 | // of the complete destructor when necessary. |
| 2579 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) { |
| 2580 | if (getTarget().getCXXABI().isMicrosoft() && |
| 2581 | GD.getDtorType() == Dtor_Complete && |
| 2582 | DD->getParent()->getNumVBases() == 0) |
| 2583 | GD = GlobalDecl(DD, Dtor_Base); |
| 2584 | } |
| 2585 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2586 | StringRef MangledName = getMangledName(GD); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2587 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2588 | /*IsThunk=*/false, llvm::AttributeList(), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2589 | IsForDefinition); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2590 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 2591 | |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2592 | static const FunctionDecl * |
| 2593 | GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { |
| 2594 | TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
| 2595 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 2596 | |
| 2597 | IdentifierInfo &CII = C.Idents.get(Name); |
| 2598 | for (const auto &Result : DC->lookup(&CII)) |
| 2599 | if (const auto FD = dyn_cast<FunctionDecl>(Result)) |
| 2600 | return FD; |
| 2601 | |
| 2602 | if (!C.getLangOpts().CPlusPlus) |
| 2603 | return nullptr; |
| 2604 | |
| 2605 | // Demangle the premangled name from getTerminateFn() |
| 2606 | IdentifierInfo &CXXII = |
Reid Kleckner | fb93154 | 2018-03-16 20:36:49 +0000 | [diff] [blame] | 2607 | (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ") |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2608 | ? C.Idents.get("terminate") |
| 2609 | : C.Idents.get(Name); |
| 2610 | |
| 2611 | for (const auto &N : {"__cxxabiv1", "std"}) { |
| 2612 | IdentifierInfo &NS = C.Idents.get(N); |
| 2613 | for (const auto &Result : DC->lookup(&NS)) { |
| 2614 | NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result); |
| 2615 | if (auto LSD = dyn_cast<LinkageSpecDecl>(Result)) |
| 2616 | for (const auto &Result : LSD->lookup(&NS)) |
| 2617 | if ((ND = dyn_cast<NamespaceDecl>(Result))) |
| 2618 | break; |
| 2619 | |
| 2620 | if (ND) |
| 2621 | for (const auto &Result : ND->lookup(&CXXII)) |
| 2622 | if (const auto *FD = dyn_cast<FunctionDecl>(Result)) |
| 2623 | return FD; |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | return nullptr; |
| 2628 | } |
| 2629 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2630 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 2631 | /// type and name. |
| 2632 | llvm::Constant * |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2633 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2634 | llvm::AttributeList ExtraAttrs, |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2635 | bool Local) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 2636 | llvm::Constant *C = |
| 2637 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2638 | /*DontDefer=*/false, /*IsThunk=*/false, |
| 2639 | ExtraAttrs); |
| 2640 | |
| 2641 | if (auto *F = dyn_cast<llvm::Function>(C)) { |
| 2642 | if (F->empty()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2643 | F->setCallingConv(getRuntimeCC()); |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2644 | |
| 2645 | if (!Local && getTriple().isOSBinFormatCOFF() && |
Martell Malone | b22e600 | 2017-11-04 02:15:49 +0000 | [diff] [blame] | 2646 | !getCodeGenOpts().LTOVisibilityPublicStd && |
| 2647 | !getTriple().isWindowsGNUEnvironment()) { |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2648 | const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); |
| 2649 | if (!FD || FD->hasAttr<DLLImportAttr>()) { |
| 2650 | F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 2651 | F->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2652 | } |
| 2653 | } |
Rafael Espindola | 3c9be62 | 2018-03-20 20:27:30 +0000 | [diff] [blame] | 2654 | setDSOLocal(F); |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2658 | return C; |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2659 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2660 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 2661 | /// CreateBuiltinFunction - Create a new builtin function with the specified |
| 2662 | /// type and name. |
| 2663 | llvm::Constant * |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2664 | CodeGenModule::CreateBuiltinFunction(llvm::FunctionType *FTy, StringRef Name, |
| 2665 | llvm::AttributeList ExtraAttrs) { |
Rafael Espindola | 1193c37 | 2018-03-22 18:03:13 +0000 | [diff] [blame] | 2666 | return CreateRuntimeFunction(FTy, Name, ExtraAttrs, true); |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2669 | /// isTypeConstant - Determine whether an object of this type can be emitted |
| 2670 | /// as a constant. |
| 2671 | /// |
| 2672 | /// If ExcludeCtor is true, the duration when the object's constructor runs |
| 2673 | /// will not be considered. The caller will need to verify that the object is |
| 2674 | /// not written to during its construction. |
| 2675 | bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) { |
| 2676 | if (!Ty.isConstant(Context) && !Ty->isReferenceType()) |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 2677 | return false; |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2678 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2679 | if (Context.getLangOpts().CPlusPlus) { |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2680 | if (const CXXRecordDecl *Record |
| 2681 | = Context.getBaseElementType(Ty)->getAsCXXRecordDecl()) |
| 2682 | return ExcludeCtor && !Record->hasMutableFields() && |
| 2683 | Record->hasTrivialDestructor(); |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 2684 | } |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2685 | |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 2686 | return true; |
| 2687 | } |
| 2688 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2689 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 2690 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 2691 | /// is something in the module with the specified name, return it potentially |
| 2692 | /// bitcasted to the right type. |
| 2693 | /// |
| 2694 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 2695 | /// to set the attributes on the global when it is first created. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2696 | /// |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2697 | /// If IsForDefinition is true, it is guaranteed that an actual global with |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2698 | /// type Ty will be returned, not conversion of a variable with the same |
| 2699 | /// mangled name but some other type. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2700 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2701 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2702 | llvm::PointerType *Ty, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2703 | const VarDecl *D, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2704 | ForDefinition_t IsForDefinition) { |
Daniel Dunbar | 829e988 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 2705 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2706 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2707 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 2708 | if (WeakRefReferences.erase(Entry)) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2709 | if (D && !D->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 2710 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 2713 | // Handle dropped DLL attributes. |
| 2714 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) |
| 2715 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 2716 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2717 | if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D) |
| 2718 | getOpenMPRuntime().registerTargetGlobalVariable(D, Entry); |
| 2719 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2720 | if (Entry->getType() == Ty) |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 2721 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2723 | // If there are two attempts to define the same mangled name, issue an |
| 2724 | // error. |
| 2725 | if (IsForDefinition && !Entry->isDeclaration()) { |
| 2726 | GlobalDecl OtherGD; |
| 2727 | const VarDecl *OtherD; |
| 2728 | |
| 2729 | // Check that D is not yet in DiagnosedConflictingDefinitions is required |
| 2730 | // to make sure that we issue an error only once. |
Artem Belevich | 2c323a0 | 2016-05-19 18:00:18 +0000 | [diff] [blame] | 2731 | if (D && lookupRepresentativeDecl(MangledName, OtherGD) && |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2732 | (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) && |
| 2733 | (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) && |
| 2734 | OtherD->hasInit() && |
| 2735 | DiagnosedConflictingDefinitions.insert(D).second) { |
| 2736 | getDiags().Report(D->getLocation(), |
| 2737 | diag::err_duplicate_mangled_name); |
| 2738 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
| 2739 | diag::note_previous_definition); |
| 2740 | } |
| 2741 | } |
| 2742 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2743 | // Make sure the result is of the correct type. |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 2744 | if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) |
| 2745 | return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); |
| 2746 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2747 | // (If global is requested for a definition, we always need to create a new |
| 2748 | // global, not just return a bitcast.) |
| 2749 | if (!IsForDefinition) |
| 2750 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 2751 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2753 | auto AddrSpace = GetGlobalVarAddressSpace(D); |
| 2754 | auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace); |
| 2755 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2756 | auto *GV = new llvm::GlobalVariable( |
| 2757 | getModule(), Ty->getElementType(), false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2758 | llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2759 | llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 2760 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2761 | // If we already created a global with the same mangled name (but different |
| 2762 | // type) before, take its name and remove it from its parent. |
| 2763 | if (Entry) { |
| 2764 | GV->takeName(Entry); |
| 2765 | |
| 2766 | if (!Entry->use_empty()) { |
| 2767 | llvm::Constant *NewPtrForOldDecl = |
| 2768 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
| 2769 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 2770 | } |
| 2771 | |
| 2772 | Entry->eraseFromParent(); |
| 2773 | } |
| 2774 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2775 | // This is the first use or definition of a mangled name. If there is a |
| 2776 | // deferred decl with this name, remember that we need to emit it at the end |
| 2777 | // of the file. |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 2778 | auto DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2779 | if (DDI != DeferredDecls.end()) { |
| 2780 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 2781 | // 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] | 2782 | addDeferredDeclToEmit(DDI->second); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2783 | DeferredDecls.erase(DDI); |
| 2784 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2785 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2786 | // Handle things which are present even on external declarations. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2787 | if (D) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2788 | if (LangOpts.OpenMP && !LangOpts.OpenMPSimd) |
| 2789 | getOpenMPRuntime().registerTargetGlobalVariable(D, GV); |
| 2790 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2791 | // FIXME: This code is overly simple and should be merged with other global |
| 2792 | // handling. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 2793 | GV->setConstant(isTypeConstant(D->getType(), false)); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 2794 | |
Ulrich Weigand | 4608438 | 2015-04-21 17:27:59 +0000 | [diff] [blame] | 2795 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
| 2796 | |
Jake Ehrlich | ba874ad | 2017-11-29 00:54:20 +0000 | [diff] [blame] | 2797 | setLinkageForGV(GV, D); |
Eli Friedman | 4f85674 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 2798 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2799 | if (D->getTLSKind()) { |
Richard Smith | 1847baa | 2013-04-22 08:06:17 +0000 | [diff] [blame] | 2800 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
Richard Smith | 5a99c49 | 2015-12-01 01:10:48 +0000 | [diff] [blame] | 2801 | CXXThreadLocals.push_back(D); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 2802 | setTLSMode(GV, *D); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2803 | } |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 2804 | |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 2805 | setGVProperties(GV, D); |
| 2806 | |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 2807 | // If required by the ABI, treat declarations of static data members with |
| 2808 | // inline initializers as definitions. |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 2809 | if (getContext().isMSStaticDataMemberInlineDefinition(D)) { |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 2810 | EmitGlobalVarDefinition(D); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 2811 | } |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 2812 | |
Erich Keane | 99fdfb6 | 2017-09-26 23:42:34 +0000 | [diff] [blame] | 2813 | // Emit section information for extern variables. |
| 2814 | if (D->hasExternalStorage()) { |
| 2815 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 2816 | GV->setSection(SA->getName()); |
| 2817 | } |
| 2818 | |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 2819 | // Handle XCore specific ABI requirements. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 2820 | if (getTriple().getArch() == llvm::Triple::xcore && |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 2821 | D->getLanguageLinkage() == CLanguageLinkage && |
| 2822 | D->getType().isConstant(Context) && |
| 2823 | isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) |
| 2824 | GV->setSection(".cp.rodata"); |
Mehdi Amini | 7cb1b30 | 2017-09-05 03:58:35 +0000 | [diff] [blame] | 2825 | |
| 2826 | // Check if we a have a const declaration with an initializer, we may be |
| 2827 | // able to emit it as available_externally to expose it's value to the |
| 2828 | // optimizer. |
| 2829 | if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() && |
| 2830 | D->getType().isConstQualified() && !GV->hasInitializer() && |
| 2831 | !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) { |
| 2832 | const auto *Record = |
| 2833 | Context.getBaseElementType(D->getType())->getAsCXXRecordDecl(); |
| 2834 | bool HasMutableFields = Record && Record->hasMutableFields(); |
| 2835 | if (!HasMutableFields) { |
| 2836 | const VarDecl *InitDecl; |
| 2837 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
| 2838 | if (InitExpr) { |
| 2839 | ConstantEmitter emitter(*this); |
| 2840 | llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl); |
| 2841 | if (Init) { |
| 2842 | auto *InitType = Init->getType(); |
| 2843 | if (GV->getType()->getElementType() != InitType) { |
| 2844 | // The type of the initializer does not match the definition. |
| 2845 | // This happens when an initializer has a different type from |
| 2846 | // the type of the global (because of padding at the end of a |
| 2847 | // structure for instance). |
| 2848 | GV->setName(StringRef()); |
| 2849 | // Make a new global with the correct type, this is now guaranteed |
| 2850 | // to work. |
| 2851 | auto *NewGV = cast<llvm::GlobalVariable>( |
| 2852 | GetAddrOfGlobalVar(D, InitType, IsForDefinition)); |
| 2853 | |
| 2854 | // Erase the old global, since it is no longer used. |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2855 | GV->eraseFromParent(); |
Mehdi Amini | 7cb1b30 | 2017-09-05 03:58:35 +0000 | [diff] [blame] | 2856 | GV = NewGV; |
| 2857 | } else { |
| 2858 | GV->setInitializer(Init); |
| 2859 | GV->setConstant(true); |
| 2860 | GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 2861 | } |
| 2862 | emitter.finalize(GV); |
| 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | } |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2869 | LangAS ExpectedAS = |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2870 | D ? D->getType().getAddressSpace() |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2871 | : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); |
Benjamin Kramer | 6fbfdec | 2017-07-08 14:14:57 +0000 | [diff] [blame] | 2872 | assert(getContext().getTargetAddressSpace(ExpectedAS) == |
| 2873 | Ty->getPointerAddressSpace()); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 2874 | if (AddrSpace != ExpectedAS) |
| 2875 | return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace, |
| 2876 | ExpectedAS, Ty); |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 2877 | |
| 2878 | return GV; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2881 | llvm::Constant * |
| 2882 | CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2883 | ForDefinition_t IsForDefinition) { |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2884 | const Decl *D = GD.getDecl(); |
| 2885 | if (isa<CXXConstructorDecl>(D)) |
| 2886 | return getAddrOfCXXStructor(cast<CXXConstructorDecl>(D), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2887 | getFromCtorType(GD.getCtorType()), |
| 2888 | /*FnInfo=*/nullptr, /*FnType=*/nullptr, |
| 2889 | /*DontDefer=*/false, IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2890 | else if (isa<CXXDestructorDecl>(D)) |
| 2891 | return getAddrOfCXXStructor(cast<CXXDestructorDecl>(D), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2892 | getFromDtorType(GD.getDtorType()), |
| 2893 | /*FnInfo=*/nullptr, /*FnType=*/nullptr, |
| 2894 | /*DontDefer=*/false, IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2895 | else if (isa<CXXMethodDecl>(D)) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2896 | auto FInfo = &getTypes().arrangeCXXMethodDeclaration( |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2897 | cast<CXXMethodDecl>(D)); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2898 | auto Ty = getTypes().GetFunctionType(*FInfo); |
| 2899 | return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, |
| 2900 | IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2901 | } else if (isa<FunctionDecl>(D)) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2902 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 2903 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
| 2904 | return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, |
| 2905 | IsForDefinition); |
| 2906 | } else |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 2907 | return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2908 | IsForDefinition); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2909 | } |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2910 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2911 | llvm::GlobalVariable * |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2912 | CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2913 | llvm::Type *Ty, |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2914 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 2915 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2916 | llvm::GlobalVariable *OldGV = nullptr; |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2917 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2918 | if (GV) { |
| 2919 | // Check if the variable has the right type. |
| 2920 | if (GV->getType()->getElementType() == Ty) |
| 2921 | return GV; |
| 2922 | |
| 2923 | // Because C++ name mangling, the only way we can end up with an already |
| 2924 | // 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] | 2925 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2926 | OldGV = GV; |
| 2927 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2928 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2929 | // Create a new variable. |
| 2930 | GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2931 | Linkage, nullptr, Name); |
| 2932 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2933 | if (OldGV) { |
| 2934 | // Replace occurrences of the old variable if needed. |
| 2935 | GV->takeName(OldGV); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2936 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2937 | if (!OldGV->use_empty()) { |
| 2938 | llvm::Constant *NewPtrForOldDecl = |
| 2939 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 2940 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 2941 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2942 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2943 | OldGV->eraseFromParent(); |
| 2944 | } |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 2945 | |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 2946 | if (supportsCOMDAT() && GV->isWeakForLinker() && |
| 2947 | !GV->hasAvailableExternallyLinkage()) |
| 2948 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 2949 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 2950 | return GV; |
| 2951 | } |
| 2952 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2953 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 2954 | /// 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] | 2955 | /// then it will be created with the specified type instead of whatever the |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2956 | /// normal requested type would be. If IsForDefinition is true, it is guaranteed |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2957 | /// that an actual global with type Ty will be returned, not conversion of a |
| 2958 | /// variable with the same mangled name but some other type. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2959 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2960 | llvm::Type *Ty, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2961 | ForDefinition_t IsForDefinition) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2962 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 2963 | QualType ASTTy = D->getType(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2964 | if (!Ty) |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2965 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2967 | llvm::PointerType *PTy = |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2968 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2970 | StringRef MangledName = getMangledName(D); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2971 | return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 2975 | /// specified type and name. |
| 2976 | llvm::Constant * |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2977 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2978 | StringRef Name) { |
Rafael Espindola | 6ab4ae4 | 2018-03-21 01:30:16 +0000 | [diff] [blame] | 2979 | auto *Ret = |
| 2980 | GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr); |
| 2981 | setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts())); |
| 2982 | return Ret; |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 2985 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 2986 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 2987 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2988 | StringRef MangledName = getMangledName(D); |
| 2989 | llvm::GlobalValue *GV = GetGlobalValue(MangledName); |
| 2990 | |
| 2991 | // We already have a definition, not declaration, with the same mangled name. |
| 2992 | // Emitting of declaration is not required (and actually overwrites emitted |
| 2993 | // definition). |
| 2994 | if (GV && !GV->isDeclaration()) |
| 2995 | return; |
| 2996 | |
| 2997 | // If we have not seen a reference to this variable yet, place it into the |
| 2998 | // deferred declarations table to be emitted if needed later. |
| 2999 | if (!MustBeEmitted(D) && !GV) { |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3000 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3001 | return; |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | // The tentative definition is the only definition. |
Reid Kleckner | 953fe03 | 2015-12-05 01:52:14 +0000 | [diff] [blame] | 3005 | EmitGlobalVarDefinition(D); |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3008 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 3009 | return Context.toCharUnitsFromBits( |
| 3010 | getDataLayout().getTypeStoreSizeInBits(Ty)); |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3013 | LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { |
| 3014 | LangAS AddrSpace = LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3015 | if (LangOpts.OpenCL) { |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3016 | AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3017 | assert(AddrSpace == LangAS::opencl_global || |
| 3018 | AddrSpace == LangAS::opencl_constant || |
| 3019 | AddrSpace == LangAS::opencl_local || |
| 3020 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 3021 | return AddrSpace; |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3024 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) { |
| 3025 | if (D && D->hasAttr<CUDAConstantAttr>()) |
| 3026 | return LangAS::cuda_constant; |
| 3027 | else if (D && D->hasAttr<CUDASharedAttr>()) |
| 3028 | return LangAS::cuda_shared; |
| 3029 | else |
| 3030 | return LangAS::cuda_device; |
| 3031 | } |
| 3032 | |
| 3033 | return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3036 | template<typename SomeDecl> |
| 3037 | void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, |
| 3038 | llvm::GlobalValue *GV) { |
| 3039 | if (!getLangOpts().CPlusPlus) |
| 3040 | return; |
| 3041 | |
| 3042 | // Must have 'used' attribute, or else inline assembly can't rely on |
| 3043 | // the name existing. |
| 3044 | if (!D->template hasAttr<UsedAttr>()) |
| 3045 | return; |
| 3046 | |
| 3047 | // Must have internal linkage and an ordinary name. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3048 | if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3049 | return; |
| 3050 | |
| 3051 | // Must be in an extern "C" context. Entities declared directly within |
| 3052 | // 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] | 3053 | const SomeDecl *First = D->getFirstDecl(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 3054 | if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3055 | return; |
| 3056 | |
| 3057 | // OK, this is an internal linkage entity inside an extern "C" linkage |
| 3058 | // specification. Make a note of that so we can give it the "expected" |
| 3059 | // mangled name if nothing else is using that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 3060 | std::pair<StaticExternCMap::iterator, bool> R = |
| 3061 | StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3062 | |
| 3063 | // If we have multiple internal linkage entities with the same name |
| 3064 | // in extern "C" regions, none of them gets that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 3065 | if (!R.second) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3066 | R.first->second = nullptr; |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3069 | static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) { |
| 3070 | if (!CGM.supportsCOMDAT()) |
| 3071 | return false; |
| 3072 | |
| 3073 | if (D.hasAttr<SelectAnyAttr>()) |
| 3074 | return true; |
| 3075 | |
| 3076 | GVALinkage Linkage; |
| 3077 | if (auto *VD = dyn_cast<VarDecl>(&D)) |
| 3078 | Linkage = CGM.getContext().GetGVALinkageForVariable(VD); |
| 3079 | else |
| 3080 | Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D)); |
| 3081 | |
| 3082 | switch (Linkage) { |
| 3083 | case GVA_Internal: |
| 3084 | case GVA_AvailableExternally: |
| 3085 | case GVA_StrongExternal: |
| 3086 | return false; |
| 3087 | case GVA_DiscardableODR: |
| 3088 | case GVA_StrongODR: |
| 3089 | return true; |
| 3090 | } |
| 3091 | llvm_unreachable("No such linkage"); |
| 3092 | } |
| 3093 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3094 | void CodeGenModule::maybeSetTrivialComdat(const Decl &D, |
| 3095 | llvm::GlobalObject &GO) { |
| 3096 | if (!shouldBeInCOMDAT(*this, D)) |
| 3097 | return; |
| 3098 | GO.setComdat(TheModule.getOrInsertComdat(GO.getName())); |
| 3099 | } |
| 3100 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3101 | /// Pass IsTentative as true if you want to create a tentative definition. |
| 3102 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, |
| 3103 | bool IsTentative) { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3104 | // OpenCL global variables of sampler type are translated to function calls, |
| 3105 | // therefore no need to be translated. |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3106 | QualType ASTTy = D->getType(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3107 | if (getLangOpts().OpenCL && ASTTy->isSamplerT()) |
| 3108 | return; |
| 3109 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 3110 | // If this is OpenMP device, check if it is legal to emit this global |
| 3111 | // normally. |
| 3112 | if (LangOpts.OpenMPIsDevice && OpenMPRuntime && |
| 3113 | OpenMPRuntime->emitTargetGlobalVariable(D)) |
| 3114 | return; |
| 3115 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3116 | llvm::Constant *Init = nullptr; |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 3117 | CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 3118 | bool NeedsGlobalCtor = false; |
| 3119 | bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 3121 | const VarDecl *InitDecl; |
| 3122 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 3123 | |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3124 | Optional<ConstantEmitter> emitter; |
| 3125 | |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 3126 | // CUDA E.2.4.1 "__shared__ variables cannot have an initialization |
| 3127 | // as part of their declaration." Sema has already checked for |
| 3128 | // error cases, so we just need to set Init to UndefValue. |
| 3129 | if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice && |
| 3130 | D->hasAttr<CUDASharedAttr>()) |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 3131 | Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy)); |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 3132 | else if (!InitExpr) { |
Eli Friedman | 6859a1b | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 3133 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3134 | // implicitly initialized with { 0 }. |
| 3135 | // |
| 3136 | // Note that tentative definitions are only emitted at the end of |
| 3137 | // a translation unit, so they should never have incomplete |
| 3138 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 3139 | // never attempt to emit a tentative definition if a real one |
| 3140 | // exists. A use may still exists, however, so we still may need |
| 3141 | // to do a RAUW. |
| 3142 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | f18318c | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 3143 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3144 | } else { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3145 | initializedGlobalDecl = GlobalDecl(D); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3146 | emitter.emplace(*this); |
| 3147 | Init = emitter->tryEmitForInitializer(*InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 3148 | |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 3149 | if (!Init) { |
Anders Carlsson | ca4a545 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 3150 | QualType T = InitExpr->getType(); |
Douglas Gregor | d450f06 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 3151 | if (D->getType()->isReferenceType()) |
| 3152 | T = D->getType(); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 3153 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3154 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 3155 | Init = EmitNullConstant(T); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 3156 | NeedsGlobalCtor = true; |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 3157 | } else { |
| 3158 | ErrorUnsupported(D, "static initializer"); |
| 3159 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 3160 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 3161 | } else { |
| 3162 | // 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] | 3163 | // initializer position (just in case this entry was delayed) if we |
| 3164 | // also don't need to register a destructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3165 | if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 3166 | DelayedCXXInitPosition.erase(D); |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 3167 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3168 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3170 | llvm::Type* InitType = Init->getType(); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3171 | llvm::Constant *Entry = |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3172 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3174 | // Strip off a bitcast if we got one back. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3175 | if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 3176 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 3177 | CE->getOpcode() == llvm::Instruction::AddrSpaceCast || |
| 3178 | // All zero index gep. |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 3179 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3180 | Entry = CE->getOperand(0); |
| 3181 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3183 | // Entry is now either a Function or GlobalVariable. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3184 | auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3186 | // We have a definition after a declaration with the wrong type. |
| 3187 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 3188 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 3189 | // (which will be a definition). |
| 3190 | // |
| 3191 | // This happens if there is a prototype for a global (e.g. |
| 3192 | // "extern int x[];") and then a definition of a different type (e.g. |
| 3193 | // "int x[10];"). This also happens when an initializer has a different type |
| 3194 | // from the type of the global (this happens with unions). |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3195 | if (!GV || GV->getType()->getElementType() != InitType || |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3196 | GV->getType()->getAddressSpace() != |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3197 | getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3199 | // 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] | 3200 | Entry->setName(StringRef()); |
Daniel Dunbar | b2f4cdb | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 3201 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3202 | // 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] | 3203 | GV = cast<llvm::GlobalVariable>( |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3204 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3205 | |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3206 | // Replace all uses of the old global with the new global |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3208 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3209 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Erase the old global, since it is no longer used. |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3212 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 3213 | } |
Devang Patel | 19c2b9a | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 3214 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3215 | MaybeHandleStaticInExternC(D, GV); |
| 3216 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3217 | if (D->hasAttr<AnnotateAttr>()) |
| 3218 | AddGlobalAnnotations(D, GV); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 3219 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3220 | // Set the llvm linkage type as appropriate. |
| 3221 | llvm::GlobalValue::LinkageTypes Linkage = |
| 3222 | getLLVMLinkageVarDefinition(D, GV->isConstant()); |
| 3223 | |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 3224 | // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on |
| 3225 | // the device. [...]" |
| 3226 | // CUDA B.2.2 "The __constant__ qualifier, optionally used together with |
| 3227 | // __device__, declares a variable that: [...] |
| 3228 | // Is accessible from all the threads within the grid and from the host |
| 3229 | // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize() |
| 3230 | // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())." |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3231 | if (GV && LangOpts.CUDA) { |
| 3232 | if (LangOpts.CUDAIsDevice) { |
| 3233 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) |
| 3234 | GV->setExternallyInitialized(true); |
| 3235 | } else { |
| 3236 | // Host-side shadows of external declarations of device-side |
| 3237 | // global variables become internal definitions. These have to |
| 3238 | // be internal in order to prevent name conflicts with global |
| 3239 | // host variables with the same name in a different TUs. |
| 3240 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) { |
| 3241 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 3242 | |
| 3243 | // Shadow variables and their properties must be registered |
| 3244 | // with CUDA runtime. |
| 3245 | unsigned Flags = 0; |
| 3246 | if (!D->hasDefinition()) |
| 3247 | Flags |= CGCUDARuntime::ExternDeviceVar; |
| 3248 | if (D->hasAttr<CUDAConstantAttr>()) |
| 3249 | Flags |= CGCUDARuntime::ConstantDeviceVar; |
| 3250 | getCUDARuntime().registerDeviceVar(*GV, Flags); |
| 3251 | } else if (D->hasAttr<CUDASharedAttr>()) |
| 3252 | // __shared__ variables are odd. Shadows do get created, but |
| 3253 | // they are not registered with the CUDA runtime, so they |
| 3254 | // can't really be used to access their device-side |
| 3255 | // counterparts. It's not clear yet whether it's nvcc's bug or |
| 3256 | // a feature, but we've got to do the same for compatibility. |
| 3257 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 3258 | } |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 3259 | } |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3260 | |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 3261 | GV->setInitializer(Init); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3262 | if (emitter) emitter->finalize(GV); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 3263 | |
| 3264 | // If it is safe to mark the global 'constant', do so now. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3265 | GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && |
| 3266 | isTypeConstant(D->getType(), true)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3267 | |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 3268 | // If it is in a read-only section, mark it 'constant'. |
| 3269 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 3270 | const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()]; |
| 3271 | if ((SI.SectionFlags & ASTContext::PSF_Write) == 0) |
| 3272 | GV->setConstant(true); |
| 3273 | } |
| 3274 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3275 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3276 | |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 3277 | |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 3278 | // On Darwin, if the normal linkage of a C++ thread_local variable is |
| 3279 | // LinkOnce or Weak, we keep the normal linkage to prevent multiple |
| 3280 | // copies within a linkage unit; otherwise, the backing variable has |
| 3281 | // internal linkage and all accesses should just be calls to the |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 3282 | // Itanium-specified entry point, which has the normal linkage of the |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 3283 | // variable. This is to preserve the ability to change the implementation |
| 3284 | // behind the scenes. |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3285 | if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic && |
Manman Ren | f93fff2 | 2015-11-11 23:08:18 +0000 | [diff] [blame] | 3286 | Context.getTargetInfo().getTriple().isOSDarwin() && |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 3287 | !llvm::GlobalVariable::isLinkOnceLinkage(Linkage) && |
| 3288 | !llvm::GlobalVariable::isWeakLinkage(Linkage)) |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3289 | Linkage = llvm::GlobalValue::InternalLinkage; |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 3290 | |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 3291 | GV->setLinkage(Linkage); |
Nico Rieck | bb0554f | 2014-01-14 15:23:53 +0000 | [diff] [blame] | 3292 | if (D->hasAttr<DLLImportAttr>()) |
| 3293 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 3294 | else if (D->hasAttr<DLLExportAttr>()) |
| 3295 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 3296 | else |
| 3297 | GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 3298 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 3299 | if (Linkage == llvm::GlobalVariable::CommonLinkage) { |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 3300 | // common vars aren't constant even if declared const. |
| 3301 | GV->setConstant(false); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 3302 | // Tentative definition of global variables may be initialized with |
| 3303 | // non-zero null pointers. In this case they should have weak linkage |
| 3304 | // since common linkage must have zero initializer and must not have |
| 3305 | // explicit section therefore cannot have non-zero initial value. |
| 3306 | if (!GV->getInitializer()->isNullValue()) |
| 3307 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
| 3308 | } |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 3309 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 3310 | setNonAliasAttributes(D, GV); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 3311 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3312 | if (D->getTLSKind() && !GV->isThreadLocal()) { |
| 3313 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
Richard Smith | 5a99c49 | 2015-12-01 01:10:48 +0000 | [diff] [blame] | 3314 | CXXThreadLocals.push_back(D); |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3315 | setTLSMode(GV, *D); |
| 3316 | } |
| 3317 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3318 | maybeSetTrivialComdat(*D, *GV); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3319 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 3320 | // Emit the initializer function if necessary. |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 3321 | if (NeedsGlobalCtor || NeedsGlobalDtor) |
| 3322 | EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 3323 | |
Alexey Samsonov | 4b8de11 | 2014-08-01 21:35:28 +0000 | [diff] [blame] | 3324 | SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor); |
Kostya Serebryany | 28a26c8 | 2012-08-21 06:53:28 +0000 | [diff] [blame] | 3325 | |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 3326 | // Emit global variable debug information. |
Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 3327 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3328 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 3329 | DI->EmitGlobalVariable(GV, D); |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 3330 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 3331 | |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 3332 | static bool isVarDeclStrongDefinition(const ASTContext &Context, |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 3333 | CodeGenModule &CGM, const VarDecl *D, |
| 3334 | bool NoCommon) { |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 3335 | // Don't give variables common linkage if -fno-common was specified unless it |
| 3336 | // was overridden by a NoCommon attribute. |
| 3337 | if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) |
| 3338 | return true; |
| 3339 | |
| 3340 | // C11 6.9.2/2: |
| 3341 | // A declaration of an identifier for an object that has file scope without |
| 3342 | // an initializer, and without a storage-class specifier or with the |
| 3343 | // storage-class specifier static, constitutes a tentative definition. |
| 3344 | if (D->getInit() || D->hasExternalStorage()) |
| 3345 | return true; |
| 3346 | |
| 3347 | // A variable cannot be both common and exist in a section. |
| 3348 | if (D->hasAttr<SectionAttr>()) |
| 3349 | return true; |
| 3350 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 3351 | // A variable cannot be both common and exist in a section. |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3352 | // We don't try to determine which is the right section in the front-end. |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 3353 | // If no specialized section name is applicable, it will resort to default. |
| 3354 | if (D->hasAttr<PragmaClangBSSSectionAttr>() || |
| 3355 | D->hasAttr<PragmaClangDataSectionAttr>() || |
| 3356 | D->hasAttr<PragmaClangRodataSectionAttr>()) |
| 3357 | return true; |
| 3358 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 3359 | // Thread local vars aren't considered common linkage. |
| 3360 | if (D->getTLSKind()) |
| 3361 | return true; |
| 3362 | |
| 3363 | // Tentative definitions marked with WeakImportAttr are true definitions. |
| 3364 | if (D->hasAttr<WeakImportAttr>()) |
| 3365 | return true; |
| 3366 | |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 3367 | // A variable cannot be both common and exist in a comdat. |
| 3368 | if (shouldBeInCOMDAT(CGM, *D)) |
| 3369 | return true; |
| 3370 | |
Sanjay Patel | 2f9303a | 2016-06-30 21:02:40 +0000 | [diff] [blame] | 3371 | // Declarations with a required alignment do not have common linkage in MSVC |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 3372 | // mode. |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 3373 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | e1a0b2e | 2015-02-03 08:49:32 +0000 | [diff] [blame] | 3374 | if (D->hasAttr<AlignedAttr>()) |
| 3375 | return true; |
| 3376 | QualType VarType = D->getType(); |
| 3377 | if (Context.isAlignmentRequired(VarType)) |
| 3378 | return true; |
| 3379 | |
| 3380 | if (const auto *RT = VarType->getAs<RecordType>()) { |
| 3381 | const RecordDecl *RD = RT->getDecl(); |
| 3382 | for (const FieldDecl *FD : RD->fields()) { |
| 3383 | if (FD->isBitField()) |
| 3384 | continue; |
| 3385 | if (FD->hasAttr<AlignedAttr>()) |
| 3386 | return true; |
| 3387 | if (Context.isAlignmentRequired(FD->getType())) |
| 3388 | return true; |
| 3389 | } |
| 3390 | } |
| 3391 | } |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 3392 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 3393 | return false; |
| 3394 | } |
| 3395 | |
Hans Wennborg | 1a3a074 | 2014-05-14 19:54:53 +0000 | [diff] [blame] | 3396 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 3397 | const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) { |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 3398 | if (Linkage == GVA_Internal) |
| 3399 | return llvm::Function::InternalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3400 | |
| 3401 | if (D->hasAttr<WeakAttr>()) { |
| 3402 | if (IsConstantVariable) |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 3403 | return llvm::GlobalVariable::WeakODRLinkage; |
| 3404 | else |
| 3405 | return llvm::GlobalVariable::WeakAnyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | // We are guaranteed to have a strong definition somewhere else, |
| 3409 | // so we can use available_externally linkage. |
| 3410 | if (Linkage == GVA_AvailableExternally) |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 3411 | return llvm::GlobalValue::AvailableExternallyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3412 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3413 | // Note that Apple's kernel linker doesn't support symbol |
| 3414 | // coalescing, so we need to avoid linkonce and weak linkages there. |
| 3415 | // Normally, this means we just map to internal, but for explicit |
| 3416 | // instantiations we'll map to external. |
| 3417 | |
| 3418 | // In C++, the compiler has to emit a definition in every translation unit |
| 3419 | // that references the function. We should use linkonce_odr because |
| 3420 | // a) if all references in this translation unit are optimized away, we |
| 3421 | // don't need to codegen it. b) if the function persists, it needs to be |
| 3422 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 3423 | // definition is dependable. |
| 3424 | if (Linkage == GVA_DiscardableODR) |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 3425 | return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3426 | : llvm::Function::InternalLinkage; |
| 3427 | |
| 3428 | // An explicit instantiation of a template has weak linkage, since |
| 3429 | // explicit instantiations can occur in multiple translation units |
| 3430 | // and must all be equivalent. However, we are not allowed to |
| 3431 | // throw away these explicit instantiations. |
Justin Lebar | 27ee130 | 2016-06-30 18:41:33 +0000 | [diff] [blame] | 3432 | // |
| 3433 | // We don't currently support CUDA device code spread out across multiple TUs, |
| 3434 | // so say that CUDA templates are either external (for kernels) or internal. |
| 3435 | // This lets llvm perform aggressive inter-procedural optimizations. |
| 3436 | if (Linkage == GVA_StrongODR) { |
| 3437 | if (Context.getLangOpts().AppleKext) |
| 3438 | return llvm::Function::ExternalLinkage; |
| 3439 | if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) |
| 3440 | return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage |
| 3441 | : llvm::Function::InternalLinkage; |
| 3442 | return llvm::Function::WeakODRLinkage; |
| 3443 | } |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3444 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3445 | // C++ doesn't have tentative definitions and thus cannot have common |
| 3446 | // linkage. |
| 3447 | if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 3448 | !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D), |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 3449 | CodeGenOpts.NoCommon)) |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 3450 | return llvm::GlobalVariable::CommonLinkage; |
| 3451 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3452 | // selectany symbols are externally visible, so use weak instead of |
| 3453 | // linkonce. MSVC optimizes away references to const selectany globals, so |
| 3454 | // all definitions should be the same and ODR linkage should be used. |
| 3455 | // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx |
| 3456 | if (D->hasAttr<SelectAnyAttr>()) |
| 3457 | return llvm::GlobalVariable::WeakODRLinkage; |
| 3458 | |
| 3459 | // Otherwise, we have strong external linkage. |
| 3460 | assert(Linkage == GVA_StrongExternal); |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 3461 | return llvm::GlobalVariable::ExternalLinkage; |
| 3462 | } |
| 3463 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3464 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition( |
| 3465 | const VarDecl *VD, bool IsConstant) { |
| 3466 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 3467 | return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant); |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3470 | /// Replace the uses of a function that was declared with a non-proto type. |
| 3471 | /// We want to silently drop extra arguments from call sites |
| 3472 | static void replaceUsesOfNonProtoConstant(llvm::Constant *old, |
| 3473 | llvm::Function *newFn) { |
| 3474 | // Fast path. |
| 3475 | if (old->use_empty()) return; |
| 3476 | |
| 3477 | llvm::Type *newRetTy = newFn->getReturnType(); |
| 3478 | SmallVector<llvm::Value*, 4> newArgs; |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3479 | SmallVector<llvm::OperandBundleDef, 1> newBundles; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3480 | |
| 3481 | for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); |
| 3482 | ui != ue; ) { |
| 3483 | llvm::Value::use_iterator use = ui++; // Increment before the use is erased. |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 3484 | llvm::User *user = use->getUser(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3485 | |
| 3486 | // Recognize and replace uses of bitcasts. Most calls to |
| 3487 | // unprototyped functions will use bitcasts. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3488 | if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3489 | if (bitcast->getOpcode() == llvm::Instruction::BitCast) |
| 3490 | replaceUsesOfNonProtoConstant(bitcast, newFn); |
| 3491 | continue; |
| 3492 | } |
| 3493 | |
| 3494 | // Recognize calls to the function. |
| 3495 | llvm::CallSite callSite(user); |
| 3496 | if (!callSite) continue; |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 3497 | if (!callSite.isCallee(&*use)) continue; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3498 | |
| 3499 | // If the return types don't match exactly, then we can't |
| 3500 | // transform this call unless it's dead. |
| 3501 | if (callSite->getType() != newRetTy && !callSite->use_empty()) |
| 3502 | continue; |
| 3503 | |
| 3504 | // Get the call site's attribute list. |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3505 | SmallVector<llvm::AttributeSet, 8> newArgAttrs; |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 3506 | llvm::AttributeList oldAttrs = callSite.getAttributes(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3507 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3508 | // If the function was passed too few arguments, don't transform. |
| 3509 | unsigned newNumArgs = newFn->arg_size(); |
| 3510 | if (callSite.arg_size() < newNumArgs) continue; |
| 3511 | |
| 3512 | // If extra arguments were passed, we silently drop them. |
| 3513 | // If any of the types mismatch, we don't transform. |
| 3514 | unsigned argNo = 0; |
| 3515 | bool dontTransform = false; |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3516 | for (llvm::Argument &A : newFn->args()) { |
| 3517 | if (callSite.getArgument(argNo)->getType() != A.getType()) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3518 | dontTransform = true; |
| 3519 | break; |
| 3520 | } |
| 3521 | |
| 3522 | // Add any parameter attributes. |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 3523 | newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo)); |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3524 | argNo++; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3525 | } |
| 3526 | if (dontTransform) |
| 3527 | continue; |
| 3528 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3529 | // Okay, we can transform this. Create the new call instruction and copy |
| 3530 | // over the required information. |
| 3531 | newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo); |
| 3532 | |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3533 | // Copy over any operand bundles. |
| 3534 | callSite.getOperandBundlesAsDefs(newBundles); |
| 3535 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3536 | llvm::CallSite newCall; |
| 3537 | if (callSite.isCall()) { |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3538 | newCall = llvm::CallInst::Create(newFn, newArgs, newBundles, "", |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3539 | callSite.getInstruction()); |
| 3540 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3541 | auto *oldInvoke = cast<llvm::InvokeInst>(callSite.getInstruction()); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3542 | newCall = llvm::InvokeInst::Create(newFn, |
| 3543 | oldInvoke->getNormalDest(), |
| 3544 | oldInvoke->getUnwindDest(), |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3545 | newArgs, newBundles, "", |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3546 | callSite.getInstruction()); |
| 3547 | } |
| 3548 | newArgs.clear(); // for the next iteration |
| 3549 | |
| 3550 | if (!newCall->getType()->isVoidTy()) |
| 3551 | newCall->takeName(callSite.getInstruction()); |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 3552 | newCall.setAttributes(llvm::AttributeList::get( |
| 3553 | newFn->getContext(), oldAttrs.getFnAttributes(), |
| 3554 | oldAttrs.getRetAttributes(), newArgAttrs)); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3555 | newCall.setCallingConv(callSite.getCallingConv()); |
| 3556 | |
| 3557 | // Finally, remove the old call, replacing any uses with the new one. |
| 3558 | if (!callSite->use_empty()) |
| 3559 | callSite->replaceAllUsesWith(newCall.getInstruction()); |
| 3560 | |
| 3561 | // Copy debug location attached to CI. |
Duncan P. N. Exon Smith | 2809cc7 | 2015-03-30 20:01:41 +0000 | [diff] [blame] | 3562 | if (callSite->getDebugLoc()) |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3563 | newCall->setDebugLoc(callSite->getDebugLoc()); |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3564 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3565 | callSite->eraseFromParent(); |
| 3566 | } |
| 3567 | } |
| 3568 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 3569 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 3570 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 3571 | /// existing call uses of the old function in the module, this adjusts them to |
| 3572 | /// call the new function directly. |
| 3573 | /// |
| 3574 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 3575 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 3576 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 3577 | /// run at -O0. |
| 3578 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 3579 | llvm::Function *NewFn) { |
| 3580 | // 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] | 3581 | if (!isa<llvm::Function>(Old)) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 3583 | replaceUsesOfNonProtoConstant(Old, NewFn); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3586 | void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
David Majnemer | 60e5bdc | 2016-07-11 04:28:21 +0000 | [diff] [blame] | 3587 | auto DK = VD->isThisDeclarationADefinition(); |
| 3588 | if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) |
| 3589 | return; |
| 3590 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3591 | TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); |
| 3592 | // If we have a definition, this might be a deferred decl. If the |
| 3593 | // instantiation is explicit, make sure we emit it at the end. |
| 3594 | if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) |
| 3595 | GetAddrOfGlobalVar(VD); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3596 | |
| 3597 | EmitTopLevelDecl(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3598 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3599 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 3600 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, |
| 3601 | llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3602 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 3603 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 3604 | // Compute the function info and LLVM type. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3605 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 3606 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 3607 | |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 3608 | // Get or create the prototype for the function. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3609 | if (!GV || (GV->getType()->getElementType() != Ty)) |
| 3610 | GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, |
| 3611 | /*DontDefer=*/true, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3612 | ForDefinition)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3614 | // Already emitted. |
| 3615 | if (!GV->isDeclaration()) |
Renato Golin | c640ff6 | 2015-04-15 08:44:40 +0000 | [diff] [blame] | 3616 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 3618 | // We need to set linkage and visibility on the function before |
| 3619 | // generating code for it because various parts of IR generation |
| 3620 | // want to propagate this information down (e.g. to local static |
| 3621 | // declarations). |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3622 | auto *Fn = cast<llvm::Function>(GV); |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 3623 | setFunctionLinkage(GD, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3624 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 3625 | // FIXME: this is redundant with part of setFunctionDefinitionAttributes |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 3626 | setGVProperties(Fn, GD); |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 3627 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3628 | MaybeHandleStaticInExternC(D, Fn); |
| 3629 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 3630 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 3631 | getTargetCodeGenInfo().setCUDAKernelCallingConvention(Fn); |
| 3632 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3633 | maybeSetTrivialComdat(*D, *Fn); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3634 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 3635 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 3636 | |
Rafael Espindola | e4e7813 | 2018-03-01 00:00:02 +0000 | [diff] [blame] | 3637 | setNonAliasAttributes(GD, Fn); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 3638 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3640 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 3641 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3642 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 3643 | AddGlobalDtor(Fn, DA->getPriority()); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3644 | if (D->hasAttr<AnnotateAttr>()) |
| 3645 | AddGlobalAnnotations(D, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3648 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3649 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3650 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3651 | assert(AA && "Not an alias?"); |
| 3652 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3653 | StringRef MangledName = getMangledName(GD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | |
Hal Finkel | 0e2b975 | 2015-09-04 21:49:21 +0000 | [diff] [blame] | 3655 | if (AA->getAliasee() == MangledName) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3656 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
Hal Finkel | 0e2b975 | 2015-09-04 21:49:21 +0000 | [diff] [blame] | 3657 | return; |
| 3658 | } |
| 3659 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3660 | // If there is a definition in the module, then it wins over the alias. |
| 3661 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 3662 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 3663 | if (Entry && !Entry->isDeclaration()) |
| 3664 | return; |
| 3665 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 3666 | Aliases.push_back(GD); |
| 3667 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3668 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3669 | |
| 3670 | // Create a reference to the named value. This ensures that it is emitted |
| 3671 | // if a deferred decl. |
| 3672 | llvm::Constant *Aliasee; |
| 3673 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 3674 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 3675 | /*ForVTable=*/false); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3676 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3677 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3678 | llvm::PointerType::getUnqual(DeclTy), |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3679 | /*D=*/nullptr); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3680 | |
| 3681 | // Create the new alias itself, but don't set a name yet. |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 3682 | auto *GA = llvm::GlobalAlias::create( |
David Blaikie | 2a791d7 | 2015-09-14 18:38:22 +0000 | [diff] [blame] | 3683 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Aliasee, &getModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3685 | if (Entry) { |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 3686 | if (GA->getAliasee() == Entry) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3687 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 3688 | return; |
| 3689 | } |
| 3690 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3691 | assert(Entry->isDeclaration()); |
| 3692 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3693 | // If there is a declaration in the module, then we had an extern followed |
| 3694 | // by the alias, as in: |
| 3695 | // extern int test6(); |
| 3696 | // ... |
| 3697 | // int test6() __attribute__((alias("test7"))); |
| 3698 | // |
| 3699 | // Remove it and replace uses of it with the alias. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3700 | GA->takeName(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3702 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3703 | Entry->getType())); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3704 | Entry->eraseFromParent(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3705 | } else { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 3706 | GA->setName(MangledName); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3707 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 3709 | // Set attributes which are particular to an alias; this is a |
| 3710 | // specialization of the attributes which may be set on a global |
| 3711 | // variable/function. |
Rafael Espindola | fcfbcc6 | 2014-10-08 00:00:09 +0000 | [diff] [blame] | 3712 | if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || |
| 3713 | D->isWeakImported()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 3714 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 3715 | } |
| 3716 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3717 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
| 3718 | if (VD->getTLSKind()) |
| 3719 | setTLSMode(GA, *VD); |
| 3720 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 3721 | SetCommonAttributes(GD, GA); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3724 | void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { |
| 3725 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
| 3726 | const IFuncAttr *IFA = D->getAttr<IFuncAttr>(); |
| 3727 | assert(IFA && "Not an ifunc?"); |
| 3728 | |
| 3729 | StringRef MangledName = getMangledName(GD); |
| 3730 | |
| 3731 | if (IFA->getResolver() == MangledName) { |
| 3732 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | // Report an error if some definition overrides ifunc. |
| 3737 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 3738 | if (Entry && !Entry->isDeclaration()) { |
| 3739 | GlobalDecl OtherGD; |
| 3740 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
| 3741 | DiagnosedConflictingDefinitions.insert(GD).second) { |
| 3742 | Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name); |
| 3743 | Diags.Report(OtherGD.getDecl()->getLocation(), |
| 3744 | diag::note_previous_definition); |
| 3745 | } |
| 3746 | return; |
| 3747 | } |
| 3748 | |
| 3749 | Aliases.push_back(GD); |
| 3750 | |
| 3751 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
| 3752 | llvm::Constant *Resolver = |
| 3753 | GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD, |
| 3754 | /*ForVTable=*/false); |
| 3755 | llvm::GlobalIFunc *GIF = |
| 3756 | llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, |
| 3757 | "", Resolver, &getModule()); |
| 3758 | if (Entry) { |
| 3759 | if (GIF->getResolver() == Entry) { |
| 3760 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
| 3761 | return; |
| 3762 | } |
| 3763 | assert(Entry->isDeclaration()); |
| 3764 | |
| 3765 | // If there is a declaration in the module, then we had an extern followed |
| 3766 | // by the ifunc, as in: |
| 3767 | // extern int test(); |
| 3768 | // ... |
| 3769 | // int test() __attribute__((ifunc("resolver"))); |
| 3770 | // |
| 3771 | // Remove it and replace uses of it with the ifunc. |
| 3772 | GIF->takeName(Entry); |
| 3773 | |
| 3774 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF, |
| 3775 | Entry->getType())); |
| 3776 | Entry->eraseFromParent(); |
| 3777 | } else |
| 3778 | GIF->setName(MangledName); |
| 3779 | |
Rafael Espindola | 75c649c | 2018-03-01 00:06:55 +0000 | [diff] [blame] | 3780 | SetCommonAttributes(GD, GIF); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 3783 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 3784 | ArrayRef<llvm::Type*> Tys) { |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3785 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 3786 | Tys); |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 3787 | } |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 3788 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3789 | static llvm::StringMapEntry<llvm::GlobalVariable *> & |
| 3790 | GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map, |
| 3791 | const StringLiteral *Literal, bool TargetIsLSB, |
| 3792 | bool &IsUTF16, unsigned &StringLength) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3793 | StringRef String = Literal->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 3794 | unsigned NumBytes = String.size(); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3795 | |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 3796 | // Check for simple case. |
| 3797 | if (!Literal->containsNonAsciiOrNull()) { |
| 3798 | StringLength = NumBytes; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3799 | return *Map.insert(std::make_pair(String, nullptr)).first; |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3802 | // Otherwise, convert the UTF8 literals into a string of shorts. |
| 3803 | IsUTF16 = true; |
| 3804 | |
Justin Lebar | 9091055 | 2016-09-30 00:38:45 +0000 | [diff] [blame] | 3805 | SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls. |
| 3806 | const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); |
| 3807 | llvm::UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | |
Justin Lebar | 9091055 | 2016-09-30 00:38:45 +0000 | [diff] [blame] | 3809 | (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, |
| 3810 | ToPtr + NumBytes, llvm::strictConversion); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 3812 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3813 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 3814 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3815 | // Add an explicit null. |
| 3816 | *ToPtr = 0; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3817 | return *Map.insert(std::make_pair( |
| 3818 | StringRef(reinterpret_cast<const char *>(ToBuf.data()), |
| 3819 | (StringLength + 1) * 2), |
| 3820 | nullptr)).first; |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3823 | ConstantAddress |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 3824 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 3825 | unsigned StringLength = 0; |
| 3826 | bool isUTF16 = false; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3827 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
| 3828 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
| 3829 | getDataLayout().isLittleEndian(), isUTF16, |
| 3830 | StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3832 | if (auto *C = Entry.second) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3833 | return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3834 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3835 | llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 3836 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3837 | |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 3838 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3839 | if (!CFConstantStringClassRef) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3840 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3841 | Ty = llvm::ArrayType::get(Ty, 0); |
Rafael Espindola | dca0602 | 2018-03-20 15:48:00 +0000 | [diff] [blame] | 3842 | llvm::GlobalValue *GV = cast<llvm::GlobalValue>( |
| 3843 | CreateRuntimeVariable(Ty, "__CFConstantStringClassReference")); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3844 | |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3845 | if (getTriple().isOSBinFormatCOFF()) { |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3846 | IdentifierInfo &II = getContext().Idents.get(GV->getName()); |
| 3847 | TranslationUnitDecl *TUDecl = getContext().getTranslationUnitDecl(); |
| 3848 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3849 | |
| 3850 | const VarDecl *VD = nullptr; |
| 3851 | for (const auto &Result : DC->lookup(&II)) |
| 3852 | if ((VD = dyn_cast<VarDecl>(Result))) |
| 3853 | break; |
| 3854 | |
| 3855 | if (!VD || !VD->hasAttr<DLLExportAttr>()) { |
Rafael Espindola | dca0602 | 2018-03-20 15:48:00 +0000 | [diff] [blame] | 3856 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 3857 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3858 | } else { |
Rafael Espindola | dca0602 | 2018-03-20 15:48:00 +0000 | [diff] [blame] | 3859 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 3860 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3861 | } |
| 3862 | } |
Rafael Espindola | dca0602 | 2018-03-20 15:48:00 +0000 | [diff] [blame] | 3863 | setDSOLocal(GV); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 3864 | |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 3865 | // Decay array -> ptr |
Saleem Abdulrasool | 4fab745 | 2016-09-11 01:25:15 +0000 | [diff] [blame] | 3866 | CFConstantStringClassRef = |
| 3867 | llvm::ConstantExpr::getGetElementPtr(Ty, GV, Zeros); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3868 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 3870 | QualType CFTy = getContext().getCFConstantStringType(); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 3871 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3872 | auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 3873 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3874 | ConstantInitBuilder Builder(*this); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3875 | auto Fields = Builder.beginStruct(STy); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3876 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3877 | // Class pointer. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3878 | Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3880 | // Flags. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3881 | Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 3882 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3883 | // String pointer. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3884 | llvm::Constant *C = nullptr; |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3885 | if (isUTF16) { |
Craig Topper | c005cc0 | 2015-09-27 03:44:08 +0000 | [diff] [blame] | 3886 | auto Arr = llvm::makeArrayRef( |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3887 | reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())), |
| 3888 | Entry.first().size() / 2); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3889 | C = llvm::ConstantDataArray::get(VMContext, Arr); |
| 3890 | } else { |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3891 | C = llvm::ConstantDataArray::getString(VMContext, Entry.first()); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3892 | } |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 3893 | |
Bill Wendling | 86131f2 | 2012-01-10 08:46:39 +0000 | [diff] [blame] | 3894 | // Note: -fwritable-strings doesn't make the backing store strings of |
| 3895 | // CFStrings writable. (See <rdar://problem/10657500>) |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3896 | auto *GV = |
Rafael Espindola | e1bd71f | 2014-01-21 02:57:56 +0000 | [diff] [blame] | 3897 | new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true, |
| 3898 | llvm::GlobalValue::PrivateLinkage, C, ".str"); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3899 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 3900 | // Don't enforce the target's minimum global alignment, since the only use |
| 3901 | // of the string is via this class initializer. |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3902 | CharUnits Align = isUTF16 |
| 3903 | ? getContext().getTypeAlignInChars(getContext().ShortTy) |
| 3904 | : getContext().getTypeAlignInChars(getContext().CharTy); |
| 3905 | GV->setAlignment(Align.getQuantity()); |
| 3906 | |
| 3907 | // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. |
| 3908 | // Without it LLVM can merge the string with a non unnamed_addr one during |
| 3909 | // LTO. Doing that changes the section it ends in, which surprises ld64. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3910 | if (getTriple().isOSBinFormatMachO()) |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3911 | GV->setSection(isUTF16 ? "__TEXT,__ustring" |
| 3912 | : "__TEXT,__cstring,cstring_literals"); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3913 | |
| 3914 | // String. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3915 | llvm::Constant *Str = |
David Blaikie | d3c127e | 2015-05-07 17:27:56 +0000 | [diff] [blame] | 3916 | llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 3917 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3918 | if (isUTF16) |
| 3919 | // Cast the UTF16 string to the correct type. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3920 | Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy); |
| 3921 | Fields.add(Str); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 3922 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3923 | // String length. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3924 | auto Ty = getTypes().ConvertType(getContext().LongTy); |
| 3925 | Fields.addInt(cast<llvm::IntegerType>(Ty), StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3927 | CharUnits Alignment = getPointerAlign(); |
| 3928 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3929 | // The struct. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3930 | GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment, |
| 3931 | /*isConstant=*/false, |
| 3932 | llvm::GlobalVariable::PrivateLinkage); |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3933 | switch (getTriple().getObjectFormat()) { |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3934 | case llvm::Triple::UnknownObjectFormat: |
| 3935 | llvm_unreachable("unknown file format"); |
| 3936 | case llvm::Triple::COFF: |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3937 | case llvm::Triple::ELF: |
Dan Gohman | 839f215 | 2017-01-17 21:46:38 +0000 | [diff] [blame] | 3938 | case llvm::Triple::Wasm: |
Saleem Abdulrasool | 0295f8c | 2016-07-09 01:59:51 +0000 | [diff] [blame] | 3939 | GV->setSection("cfstring"); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 3940 | break; |
| 3941 | case llvm::Triple::MachO: |
| 3942 | GV->setSection("__DATA,__cfstring"); |
| 3943 | break; |
| 3944 | } |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3945 | Entry.second = GV; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3947 | return ConstantAddress(GV, Alignment); |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 3948 | } |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 3949 | |
Bob Haarman | c6c9b8f | 2017-09-11 22:11:57 +0000 | [diff] [blame] | 3950 | bool CodeGenModule::getExpressionLocationsEnabled() const { |
| 3951 | return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo; |
| 3952 | } |
| 3953 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3954 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
| 3955 | if (ObjCFastEnumerationStateType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 3956 | RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3957 | D->startDefinition(); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3958 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3959 | QualType FieldTypes[] = { |
| 3960 | Context.UnsignedLongTy, |
| 3961 | Context.getPointerType(Context.getObjCIdType()), |
| 3962 | Context.getPointerType(Context.UnsignedLongTy), |
| 3963 | Context.getConstantArrayType(Context.UnsignedLongTy, |
| 3964 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 3965 | }; |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3966 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3967 | for (size_t i = 0; i < 4; ++i) { |
| 3968 | FieldDecl *Field = FieldDecl::Create(Context, |
| 3969 | D, |
| 3970 | SourceLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3971 | SourceLocation(), nullptr, |
| 3972 | FieldTypes[i], /*TInfo=*/nullptr, |
| 3973 | /*BitWidth=*/nullptr, |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3974 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3975 | ICIS_NoInit); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3976 | Field->setAccess(AS_public); |
| 3977 | D->addDecl(Field); |
| 3978 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3979 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3980 | D->completeDefinition(); |
| 3981 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
| 3982 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 3984 | return ObjCFastEnumerationStateType; |
| 3985 | } |
| 3986 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 3987 | llvm::Constant * |
| 3988 | CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { |
| 3989 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3990 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 3991 | // Don't emit it as the address of the string, emit the string data itself |
| 3992 | // as an inline array. |
Chris Lattner | 00a10ca | 2012-02-06 22:47:00 +0000 | [diff] [blame] | 3993 | if (E->getCharByteWidth() == 1) { |
| 3994 | SmallString<64> Str(E->getString()); |
| 3995 | |
| 3996 | // Resize the string to the right size, which is indicated by its type. |
| 3997 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); |
| 3998 | Str.resize(CAT->getSize().getZExtValue()); |
| 3999 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
| 4000 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4001 | |
| 4002 | auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4003 | llvm::Type *ElemTy = AType->getElementType(); |
| 4004 | unsigned NumElements = AType->getNumElements(); |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 4005 | |
| 4006 | // Wide strings have either 2-byte or 4-byte elements. |
| 4007 | if (ElemTy->getPrimitiveSizeInBits() == 16) { |
| 4008 | SmallVector<uint16_t, 32> Elements; |
| 4009 | Elements.reserve(NumElements); |
| 4010 | |
| 4011 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 4012 | Elements.push_back(E->getCodeUnit(i)); |
| 4013 | Elements.resize(NumElements); |
| 4014 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4015 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4016 | |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 4017 | assert(ElemTy->getPrimitiveSizeInBits() == 32); |
| 4018 | SmallVector<uint32_t, 32> Elements; |
| 4019 | Elements.reserve(NumElements); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4020 | |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 4021 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 4022 | Elements.push_back(E->getCodeUnit(i)); |
| 4023 | Elements.resize(NumElements); |
| 4024 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4027 | static llvm::GlobalVariable * |
| 4028 | GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, |
| 4029 | CodeGenModule &CGM, StringRef GlobalName, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4030 | CharUnits Alignment) { |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4031 | // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. |
| 4032 | unsigned AddrSpace = 0; |
| 4033 | if (CGM.getLangOpts().OpenCL) |
| 4034 | AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant); |
| 4035 | |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 4036 | llvm::Module &M = CGM.getModule(); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4037 | // Create a global variable for this string |
| 4038 | auto *GV = new llvm::GlobalVariable( |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 4039 | M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName, |
| 4040 | nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4041 | GV->setAlignment(Alignment.getQuantity()); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 4042 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 4043 | if (GV->isWeakForLinker()) { |
| 4044 | assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals"); |
| 4045 | GV->setComdat(M.getOrInsertComdat(GV->getName())); |
| 4046 | } |
Rafael Espindola | 0d40f12 | 2018-03-20 20:42:55 +0000 | [diff] [blame] | 4047 | CGM.setDSOLocal(GV); |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 4048 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4049 | return GV; |
| 4050 | } |
| 4051 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4052 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 4053 | /// constant array for the given string literal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4054 | ConstantAddress |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 4055 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S, |
| 4056 | StringRef Name) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4057 | CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType()); |
James Molloy | e735b2b | 2014-05-09 16:28:56 +0000 | [diff] [blame] | 4058 | |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4059 | llvm::Constant *C = GetConstantArrayFromStringLiteral(S); |
| 4060 | llvm::GlobalVariable **Entry = nullptr; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4061 | if (!LangOpts.WritableStrings) { |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4062 | Entry = &ConstantStringMap[C]; |
| 4063 | if (auto GV = *Entry) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4064 | if (Alignment.getQuantity() > GV->getAlignment()) |
| 4065 | GV->setAlignment(Alignment.getQuantity()); |
| 4066 | return ConstantAddress(GV, Alignment); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4067 | } |
Eli Friedman | 49ddc5f | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 4068 | } |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 4069 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4070 | SmallString<256> MangledNameBuffer; |
| 4071 | StringRef GlobalVariableName; |
| 4072 | llvm::GlobalValue::LinkageTypes LT; |
David Majnemer | 3843a05 | 2014-03-23 17:47:16 +0000 | [diff] [blame] | 4073 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4074 | // Mangle the string literal if the ABI allows for it. However, we cannot |
| 4075 | // do this if we are compiling with ASan or -fwritable-strings because they |
| 4076 | // rely on strings having normal linkage. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 4077 | if (!LangOpts.WritableStrings && |
| 4078 | !LangOpts.Sanitize.has(SanitizerKind::Address) && |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4079 | getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) { |
| 4080 | llvm::raw_svector_ostream Out(MangledNameBuffer); |
| 4081 | getCXXABI().getMangleContext().mangleStringLiteral(S, Out); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4082 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4083 | LT = llvm::GlobalValue::LinkOnceODRLinkage; |
| 4084 | GlobalVariableName = MangledNameBuffer; |
| 4085 | } else { |
| 4086 | LT = llvm::GlobalValue::PrivateLinkage; |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 4087 | GlobalVariableName = Name; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4090 | auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); |
| 4091 | if (Entry) |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4092 | *Entry = GV; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 4093 | |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 4094 | SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>", |
| 4095 | QualType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4096 | return ConstantAddress(GV, Alignment); |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 4099 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 4100 | /// array for the given ObjCEncodeExpr node. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4101 | ConstantAddress |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 4102 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 4103 | std::string Str; |
| 4104 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | 4663a33 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 4105 | |
| 4106 | return GetAddrOfConstantCString(Str); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Alexey Samsonov | adc9037 | 2014-06-04 20:25:57 +0000 | [diff] [blame] | 4109 | /// GetAddrOfConstantCString - Returns a pointer to a character array containing |
| 4110 | /// the literal and a terminating '\0' character. |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4111 | /// The result has pointer to array type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4112 | ConstantAddress CodeGenModule::GetAddrOfConstantCString( |
| 4113 | const std::string &Str, const char *GlobalName) { |
Alexey Samsonov | adc9037 | 2014-06-04 20:25:57 +0000 | [diff] [blame] | 4114 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4115 | CharUnits Alignment = |
| 4116 | getContext().getAlignOfGlobalVarInChars(getContext().CharTy); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4117 | |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4118 | llvm::Constant *C = |
| 4119 | llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false); |
| 4120 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4121 | // Don't share any string literals if strings aren't constant. |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4122 | llvm::GlobalVariable **Entry = nullptr; |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4123 | if (!LangOpts.WritableStrings) { |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4124 | Entry = &ConstantStringMap[C]; |
| 4125 | if (auto GV = *Entry) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4126 | if (Alignment.getQuantity() > GV->getAlignment()) |
| 4127 | GV->setAlignment(Alignment.getQuantity()); |
| 4128 | return ConstantAddress(GV, Alignment); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4129 | } |
| 4130 | } |
| 4131 | |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 4132 | // Get the default prefix if a name wasn't specified. |
| 4133 | if (!GlobalName) |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 4134 | GlobalName = ".str"; |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 4135 | // Create a global variable for this. |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4136 | auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, |
| 4137 | GlobalName, Alignment); |
| 4138 | if (Entry) |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4139 | *Entry = GV; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4140 | return ConstantAddress(GV, Alignment); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 4141 | } |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4142 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4143 | ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4144 | const MaterializeTemporaryExpr *E, const Expr *Init) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4145 | assert((E->getStorageDuration() == SD_Static || |
| 4146 | E->getStorageDuration() == SD_Thread) && "not a global temporary"); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4147 | const auto *VD = cast<VarDecl>(E->getExtendingDecl()); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4148 | |
| 4149 | // If we're not materializing a subobject of the temporary, keep the |
| 4150 | // cv-qualifiers from the type of the MaterializeTemporaryExpr. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4151 | QualType MaterializedType = Init->getType(); |
| 4152 | if (Init == E->GetTemporaryExpr()) |
| 4153 | MaterializedType = E->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4154 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4155 | CharUnits Align = getContext().getTypeAlignInChars(MaterializedType); |
| 4156 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 4157 | if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E]) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4158 | return ConstantAddress(Slot, Align); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4159 | |
| 4160 | // FIXME: If an externally-visible declaration extends multiple temporaries, |
| 4161 | // we need to give each temporary the same name in every translation unit (and |
| 4162 | // we also need to make the temporaries externally-visible). |
| 4163 | SmallString<256> Name; |
| 4164 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 4165 | getCXXABI().getMangleContext().mangleReferenceTemporary( |
| 4166 | VD, E->getManglingNumber(), Out); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4167 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4168 | APValue *Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4169 | if (E->getStorageDuration() == SD_Static) { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4170 | // We might have a cached constant initializer for this temporary. Note |
| 4171 | // that this might have a different value from the value computed by |
| 4172 | // evaluating the initializer if the surrounding constant expression |
| 4173 | // modifies the temporary. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4174 | Value = getContext().getMaterializedTemporaryValue(E, false); |
| 4175 | if (Value && Value->isUninit()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4176 | Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4179 | // Try evaluating it now, it might have a constant initializer. |
| 4180 | Expr::EvalResult EvalResult; |
| 4181 | if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && |
| 4182 | !EvalResult.hasSideEffects()) |
| 4183 | Value = &EvalResult.Val; |
| 4184 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4185 | LangAS AddrSpace = |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 4186 | VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace(); |
| 4187 | |
| 4188 | Optional<ConstantEmitter> emitter; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4189 | llvm::Constant *InitialValue = nullptr; |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4190 | bool Constant = false; |
| 4191 | llvm::Type *Type; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4192 | if (Value) { |
| 4193 | // The temporary has a constant initializer, use it. |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 4194 | emitter.emplace(*this); |
| 4195 | InitialValue = emitter->emitForInitializer(*Value, AddrSpace, |
| 4196 | MaterializedType); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4197 | Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value); |
| 4198 | Type = InitialValue->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4199 | } else { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4200 | // No initializer, the initialization will be provided when we |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4201 | // initialize the declaration which performed lifetime extension. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4202 | Type = getTypes().ConvertTypeForMem(MaterializedType); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | // Create a global variable for this lifetime-extended temporary. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4206 | llvm::GlobalValue::LinkageTypes Linkage = |
| 4207 | getLLVMLinkageVarDefinition(VD, Constant); |
David Majnemer | c9a9c7a | 2015-02-19 19:25:17 +0000 | [diff] [blame] | 4208 | if (Linkage == llvm::GlobalVariable::ExternalLinkage) { |
| 4209 | const VarDecl *InitVD; |
| 4210 | if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) && |
| 4211 | isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) { |
| 4212 | // Temporaries defined inside a class get linkonce_odr linkage because the |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4213 | // class can be defined in multiple translation units. |
David Majnemer | c9a9c7a | 2015-02-19 19:25:17 +0000 | [diff] [blame] | 4214 | Linkage = llvm::GlobalVariable::LinkOnceODRLinkage; |
| 4215 | } else { |
| 4216 | // There is no need for this temporary to have external linkage if the |
| 4217 | // VarDecl has external linkage. |
| 4218 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 4219 | } |
| 4220 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 4221 | auto TargetAS = getContext().getTargetAddressSpace(AddrSpace); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4222 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4223 | getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 4224 | /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 4225 | if (emitter) emitter->finalize(GV); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 4226 | setGVProperties(GV, VD); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4227 | GV->setAlignment(Align.getQuantity()); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 4228 | if (supportsCOMDAT() && GV->isWeakForLinker()) |
| 4229 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4230 | if (VD->getTLSKind()) |
| 4231 | setTLSMode(GV, *VD); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 4232 | llvm::Constant *CV = GV; |
| 4233 | if (AddrSpace != LangAS::Default) |
| 4234 | CV = getTargetCodeGenInfo().performAddrSpaceCast( |
| 4235 | *this, GV, AddrSpace, LangAS::Default, |
| 4236 | Type->getPointerTo( |
| 4237 | getContext().getTargetAddressSpace(LangAS::Default))); |
| 4238 | MaterializedGlobalTemporaryMap[E] = CV; |
| 4239 | return ConstantAddress(CV, Align); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4242 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 4243 | /// properties for an implementation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4245 | ObjCImplementationDecl *D) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 4246 | for (const auto *PID : D->property_impls()) { |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4247 | // Dynamic is just for type-checking. |
| 4248 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 4249 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4250 | |
| 4251 | // Determine which methods need to be implemented, some may have |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 4252 | // been overridden. Note that ::isPropertyAccessor is not the method |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4253 | // we want, that just indicates if the decl came from a |
| 4254 | // property. What we want to know is if the method is defined in |
| 4255 | // this implementation. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4256 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 4257 | CodeGenFunction(*this).GenerateObjCGetter( |
| 4258 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4259 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4260 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 4261 | CodeGenFunction(*this).GenerateObjCSetter( |
| 4262 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4263 | } |
| 4264 | } |
| 4265 | } |
| 4266 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 4267 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4268 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 4269 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 4270 | ivar; ivar = ivar->getNextIvar()) |
| 4271 | if (ivar->getType().isDestructedType()) |
| 4272 | return true; |
| 4273 | |
| 4274 | return false; |
| 4275 | } |
| 4276 | |
Fariborz Jahanian | 800821a | 2014-11-12 22:37:43 +0000 | [diff] [blame] | 4277 | static bool AllTrivialInitializers(CodeGenModule &CGM, |
| 4278 | ObjCImplementationDecl *D) { |
| 4279 | CodeGenFunction CGF(CGM); |
| 4280 | for (ObjCImplementationDecl::init_iterator B = D->init_begin(), |
| 4281 | E = D->init_end(); B != E; ++B) { |
| 4282 | CXXCtorInitializer *CtorInitExp = *B; |
| 4283 | Expr *Init = CtorInitExp->getInit(); |
| 4284 | if (!CGF.isTrivialInitializer(Init)) |
| 4285 | return false; |
| 4286 | } |
| 4287 | return true; |
| 4288 | } |
| 4289 | |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4290 | /// EmitObjCIvarInitializations - Emit information for ivar initialization |
| 4291 | /// for an implementation. |
| 4292 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 4293 | // We might need a .cxx_destruct even if we don't have any ivar initializers. |
| 4294 | if (needsDestructMethod(D)) { |
| 4295 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
| 4296 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
| 4297 | ObjCMethodDecl *DTORMethod = |
| 4298 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4299 | cxxSelector, getContext().VoidTy, nullptr, D, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 4300 | /*isInstance=*/true, /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 4301 | /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 4302 | /*isDefined=*/false, ObjCMethodDecl::Required); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 4303 | D->addInstanceMethod(DTORMethod); |
| 4304 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 4305 | D->setHasDestructors(true); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | // If the implementation doesn't have any ivar initializers, we don't need |
| 4309 | // a .cxx_construct. |
Fariborz Jahanian | 800821a | 2014-11-12 22:37:43 +0000 | [diff] [blame] | 4310 | if (D->getNumIvarInitializers() == 0 || |
| 4311 | AllTrivialInitializers(*this, D)) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4312 | return; |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4313 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 4314 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
| 4315 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4316 | // The constructor returns 'self'. |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4317 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4318 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4319 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4320 | cxxSelector, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4321 | getContext().getObjCIdType(), |
| 4322 | nullptr, D, /*isInstance=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 4323 | /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 4324 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 4325 | /*isImplicitlyDeclared=*/true, |
| 4326 | /*isDefined=*/false, |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4327 | ObjCMethodDecl::Required); |
| 4328 | D->addInstanceMethod(CTORMethod); |
| 4329 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 4330 | D->setHasNonZeroConstructors(true); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 4333 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 4334 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | e480ce3 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 4335 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 4336 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 4337 | ErrorUnsupported(LSD, "linkage spec"); |
| 4338 | return; |
| 4339 | } |
| 4340 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4341 | EmitDeclContext(LSD); |
| 4342 | } |
| 4343 | |
| 4344 | void CodeGenModule::EmitDeclContext(const DeclContext *DC) { |
| 4345 | for (auto *I : DC->decls()) { |
| 4346 | // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope |
| 4347 | // are themselves considered "top-level", so EmitTopLevelDecl on an |
| 4348 | // ObjCImplDecl does not recursively visit them. We need to do that in |
| 4349 | // case they're nested inside another construct (LinkageSpecDecl / |
| 4350 | // ExportDecl) that does stop them from being considered "top-level". |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4351 | if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 4352 | for (auto *M : OID->methods()) |
| 4353 | EmitTopLevelDecl(M); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 4354 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4355 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4356 | EmitTopLevelDecl(I); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 4357 | } |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4360 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 4361 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 4362 | // Ignore dependent declarations. |
Richard Smith | 32b615c | 2018-02-01 00:28:36 +0000 | [diff] [blame] | 4363 | if (D->isTemplated()) |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 4364 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4366 | switch (D->getKind()) { |
Anders Carlsson | 6c0a6e4 | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 4367 | case Decl::CXXConversion: |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 4368 | case Decl::CXXMethod: |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4369 | case Decl::Function: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 4370 | EmitGlobal(cast<FunctionDecl>(D)); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4371 | // Always provide some coverage mapping |
| 4372 | // even for the functions that aren't emitted. |
| 4373 | AddDeferredUnusedCoverageMapping(D); |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 4374 | break; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4375 | |
Richard Smith | ada0888 | 2017-04-19 21:15:45 +0000 | [diff] [blame] | 4376 | case Decl::CXXDeductionGuide: |
| 4377 | // Function-like, but does not result in code emission. |
| 4378 | break; |
| 4379 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4380 | case Decl::Var: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 4381 | case Decl::Decomposition: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4382 | case Decl::VarTemplateSpecialization: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 4383 | EmitGlobal(cast<VarDecl>(D)); |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 4384 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) |
| 4385 | for (auto *B : DD->bindings()) |
| 4386 | if (auto *HD = B->getHoldingVar()) |
| 4387 | EmitGlobal(HD); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4388 | break; |
| 4389 | |
John McCall | 32f44bd | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 4390 | // Indirect fields from global anonymous structs and unions can be |
| 4391 | // ignored; only the actual variable requires IR gen support. |
| 4392 | case Decl::IndirectField: |
| 4393 | break; |
| 4394 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 4395 | // C++ Decls |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4396 | case Decl::Namespace: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4397 | EmitDeclContext(cast<NamespaceDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4398 | break; |
Richard Smith | e124612 | 2017-11-03 01:26:01 +0000 | [diff] [blame] | 4399 | case Decl::ClassTemplateSpecialization: { |
| 4400 | const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); |
| 4401 | if (DebugInfo && |
| 4402 | Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition && |
| 4403 | Spec->hasDefinition()) |
| 4404 | DebugInfo->completeTemplateDefinition(*Spec); |
| 4405 | } LLVM_FALLTHROUGH; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4406 | case Decl::CXXRecord: |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 4407 | if (DebugInfo) { |
| 4408 | if (auto *ES = D->getASTContext().getExternalSource()) |
| 4409 | if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) |
| 4410 | DebugInfo->completeUnusedClass(cast<CXXRecordDecl>(*D)); |
| 4411 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4412 | // Emit any static data members, they may be definitions. |
| 4413 | for (auto *I : cast<CXXRecordDecl>(D)->decls()) |
| 4414 | if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I)) |
| 4415 | EmitTopLevelDecl(I); |
| 4416 | break; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4417 | // No code generation needed. |
John McCall | 1e9de05 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 4418 | case Decl::UsingShadow: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 4419 | case Decl::ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4420 | case Decl::VarTemplate: |
| 4421 | case Decl::VarTemplatePartialSpecialization: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 4422 | case Decl::FunctionTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4423 | case Decl::TypeAliasTemplate: |
Douglas Gregor | 0aa91e0 | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 4424 | case Decl::Block: |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4425 | case Decl::Empty: |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4426 | break; |
David Blaikie | 3e65d36a | 2014-02-15 21:03:07 +0000 | [diff] [blame] | 4427 | case Decl::Using: // using X; [C++] |
| 4428 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 4429 | DI->EmitUsingDecl(cast<UsingDecl>(*D)); |
| 4430 | return; |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4431 | case Decl::NamespaceAlias: |
| 4432 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 4433 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); |
| 4434 | return; |
David Blaikie | 60a9fbf | 2013-04-26 05:41:06 +0000 | [diff] [blame] | 4435 | case Decl::UsingDirective: // using namespace X; [C++] |
| 4436 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 4437 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); |
| 4438 | return; |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 4439 | case Decl::CXXConstructor: |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 4440 | getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 4441 | break; |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 4442 | case Decl::CXXDestructor: |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 4443 | getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 4444 | break; |
Anders Carlsson | 8783543 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 4445 | |
| 4446 | case Decl::StaticAssert: |
| 4447 | // Nothing to do. |
| 4448 | break; |
| 4449 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 4450 | // Objective-C Decls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 4452 | // Forward declarations, no (immediate) code generation. |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 4453 | case Decl::ObjCInterface: |
Eric Christopher | f8378ca | 2012-07-19 22:22:55 +0000 | [diff] [blame] | 4454 | case Decl::ObjCCategory: |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 4455 | break; |
| 4456 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 4457 | case Decl::ObjCProtocol: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4458 | auto *Proto = cast<ObjCProtocolDecl>(D); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 4459 | if (Proto->isThisDeclarationADefinition()) |
| 4460 | ObjCRuntime->GenerateProtocol(Proto); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4461 | break; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 4462 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4463 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4464 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4465 | // Categories have properties but don't support synthesize so we |
| 4466 | // can ignore them here. |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 4467 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4468 | break; |
| 4469 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4470 | case Decl::ObjCImplementation: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4471 | auto *OMD = cast<ObjCImplementationDecl>(D); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4472 | EmitObjCPropertyImplementations(OMD); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4473 | EmitObjCIvarInitializations(OMD); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 4474 | ObjCRuntime->GenerateClass(OMD); |
Eric Christopher | 3d19de9 | 2012-04-11 05:56:05 +0000 | [diff] [blame] | 4475 | // Emit global variable debug information. |
| 4476 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 4477 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
Alexey Samsonov | 9c1b9f6 | 2012-12-03 18:28:12 +0000 | [diff] [blame] | 4478 | DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( |
| 4479 | OMD->getClassInterface()), OMD->getLocation()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4480 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4481 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4482 | case Decl::ObjCMethod: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4483 | auto *OMD = cast<ObjCMethodDecl>(D); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4484 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4485 | if (OMD->getBody()) |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4486 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 4487 | break; |
| 4488 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | case Decl::ObjCCompatibleAlias: |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 4490 | ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4491 | break; |
| 4492 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4493 | case Decl::PragmaComment: { |
| 4494 | const auto *PCD = cast<PragmaCommentDecl>(D); |
| 4495 | switch (PCD->getCommentKind()) { |
| 4496 | case PCK_Unknown: |
| 4497 | llvm_unreachable("unexpected pragma comment kind"); |
| 4498 | case PCK_Linker: |
| 4499 | AppendLinkerOptions(PCD->getArg()); |
| 4500 | break; |
| 4501 | case PCK_Lib: |
Saleem Abdulrasool | fd4db53 | 2018-02-07 01:46:46 +0000 | [diff] [blame] | 4502 | if (getTarget().getTriple().isOSBinFormatELF() && |
| 4503 | !getTarget().getTriple().isPS4()) |
| 4504 | AddELFLibDirective(PCD->getArg()); |
| 4505 | else |
| 4506 | AddDependentLib(PCD->getArg()); |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4507 | break; |
| 4508 | case PCK_Compiler: |
| 4509 | case PCK_ExeStr: |
| 4510 | case PCK_User: |
| 4511 | break; // We ignore all of these. |
| 4512 | } |
| 4513 | break; |
| 4514 | } |
| 4515 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 4516 | case Decl::PragmaDetectMismatch: { |
| 4517 | const auto *PDMD = cast<PragmaDetectMismatchDecl>(D); |
| 4518 | AddDetectMismatch(PDMD->getName(), PDMD->getValue()); |
| 4519 | break; |
| 4520 | } |
| 4521 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 4522 | case Decl::LinkageSpec: |
| 4523 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4524 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4525 | |
| 4526 | case Decl::FileScopeAsm: { |
Artem Belevich | 0488d1e | 2015-04-27 18:52:00 +0000 | [diff] [blame] | 4527 | // File-scope asm is ignored during device-side CUDA compilation. |
| 4528 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) |
| 4529 | break; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4530 | // File-scope asm is ignored during device-side OpenMP compilation. |
| 4531 | if (LangOpts.OpenMPIsDevice) |
| 4532 | break; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4533 | auto *AD = cast<FileScopeAsmDecl>(D); |
Joerg Sonnenberger | dfd511e | 2015-03-13 00:54:30 +0000 | [diff] [blame] | 4534 | getModule().appendModuleInlineAsm(AD->getAsmString()->getString()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4535 | break; |
| 4536 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4538 | case Decl::Import: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4539 | auto *Import = cast<ImportDecl>(D); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4540 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 4541 | // If we've already imported this module, we're done. |
| 4542 | if (!ImportedModules.insert(Import->getImportedModule())) |
Richard Smith | c52efa7 | 2015-08-19 02:30:28 +0000 | [diff] [blame] | 4543 | break; |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4544 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 4545 | // Emit debug information for direct imports. |
| 4546 | if (!Import->getImportedOwningModule()) { |
| 4547 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 4548 | DI->EmitImportDecl(*Import); |
| 4549 | } |
| 4550 | |
Manman Ren | 3b5dbf2 | 2016-10-14 18:55:44 +0000 | [diff] [blame] | 4551 | // Find all of the submodules and emit the module initializers. |
| 4552 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 4553 | SmallVector<clang::Module *, 16> Stack; |
| 4554 | Visited.insert(Import->getImportedModule()); |
| 4555 | Stack.push_back(Import->getImportedModule()); |
| 4556 | |
| 4557 | while (!Stack.empty()) { |
| 4558 | clang::Module *Mod = Stack.pop_back_val(); |
| 4559 | if (!EmittedModuleInitializers.insert(Mod).second) |
| 4560 | continue; |
| 4561 | |
| 4562 | for (auto *D : Context.getModuleInitializers(Mod)) |
| 4563 | EmitTopLevelDecl(D); |
| 4564 | |
| 4565 | // Visit the submodules of this module. |
| 4566 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 4567 | SubEnd = Mod->submodule_end(); |
| 4568 | Sub != SubEnd; ++Sub) { |
| 4569 | // Skip explicit children; they need to be explicitly imported to emit |
| 4570 | // the initializers. |
| 4571 | if ((*Sub)->IsExplicit) |
| 4572 | continue; |
| 4573 | |
| 4574 | if (Visited.insert(*Sub).second) |
| 4575 | Stack.push_back(*Sub); |
| 4576 | } |
| 4577 | } |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 4578 | break; |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4581 | case Decl::Export: |
| 4582 | EmitDeclContext(cast<ExportDecl>(D)); |
| 4583 | break; |
| 4584 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4585 | case Decl::OMPThreadPrivate: |
| 4586 | EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D)); |
| 4587 | break; |
| 4588 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 4589 | case Decl::OMPDeclareReduction: |
| 4590 | EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D)); |
| 4591 | break; |
| 4592 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | default: |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4594 | // Make sure we handled everything we should, every other kind is a |
| 4595 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 4596 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4597 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
Richard Smith | a634ff2 | 2014-12-01 18:59:10 +0000 | [diff] [blame] | 4598 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 4599 | } |
| 4600 | } |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4601 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4602 | void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { |
| 4603 | // Do we need to generate coverage mapping? |
| 4604 | if (!CodeGenOpts.CoverageMapping) |
| 4605 | return; |
| 4606 | switch (D->getKind()) { |
| 4607 | case Decl::CXXConversion: |
| 4608 | case Decl::CXXMethod: |
| 4609 | case Decl::Function: |
| 4610 | case Decl::ObjCMethod: |
| 4611 | case Decl::CXXConstructor: |
| 4612 | case Decl::CXXDestructor: { |
Justin Bogner | 203f092 | 2015-07-28 00:41:51 +0000 | [diff] [blame] | 4613 | if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4614 | return; |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 4615 | SourceManager &SM = getContext().getSourceManager(); |
| 4616 | if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getLocStart())) |
| 4617 | return; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4618 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
| 4619 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
| 4620 | DeferredEmptyCoverageMappingDecls[D] = true; |
| 4621 | break; |
| 4622 | } |
| 4623 | default: |
| 4624 | break; |
| 4625 | }; |
| 4626 | } |
| 4627 | |
| 4628 | void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) { |
| 4629 | // Do we need to generate coverage mapping? |
| 4630 | if (!CodeGenOpts.CoverageMapping) |
| 4631 | return; |
| 4632 | if (const auto *Fn = dyn_cast<FunctionDecl>(D)) { |
| 4633 | if (Fn->isTemplateInstantiation()) |
| 4634 | ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern()); |
| 4635 | } |
| 4636 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
| 4637 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
| 4638 | DeferredEmptyCoverageMappingDecls[D] = false; |
| 4639 | else |
| 4640 | I->second = false; |
| 4641 | } |
| 4642 | |
| 4643 | void CodeGenModule::EmitDeferredUnusedCoverageMappings() { |
Eli Friedman | 8516b7f | 2017-12-19 01:54:09 +0000 | [diff] [blame] | 4644 | // We call takeVector() here to avoid use-after-free. |
| 4645 | // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because |
| 4646 | // we deserialize function bodies to emit coverage info for them, and that |
| 4647 | // deserializes more declarations. How should we handle that case? |
| 4648 | for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) { |
Eli Friedman | dbcfade | 2017-12-13 00:14:17 +0000 | [diff] [blame] | 4649 | if (!Entry.second) |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4650 | continue; |
Eli Friedman | dbcfade | 2017-12-13 00:14:17 +0000 | [diff] [blame] | 4651 | const Decl *D = Entry.first; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 4652 | switch (D->getKind()) { |
| 4653 | case Decl::CXXConversion: |
| 4654 | case Decl::CXXMethod: |
| 4655 | case Decl::Function: |
| 4656 | case Decl::ObjCMethod: { |
| 4657 | CodeGenPGO PGO(*this); |
| 4658 | GlobalDecl GD(cast<FunctionDecl>(D)); |
| 4659 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 4660 | getFunctionLinkage(GD)); |
| 4661 | break; |
| 4662 | } |
| 4663 | case Decl::CXXConstructor: { |
| 4664 | CodeGenPGO PGO(*this); |
| 4665 | GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base); |
| 4666 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 4667 | getFunctionLinkage(GD)); |
| 4668 | break; |
| 4669 | } |
| 4670 | case Decl::CXXDestructor: { |
| 4671 | CodeGenPGO PGO(*this); |
| 4672 | GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base); |
| 4673 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 4674 | getFunctionLinkage(GD)); |
| 4675 | break; |
| 4676 | } |
| 4677 | default: |
| 4678 | break; |
| 4679 | }; |
| 4680 | } |
| 4681 | } |
| 4682 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4683 | /// Turns the given pointer into a constant. |
| 4684 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
| 4685 | const void *Ptr) { |
| 4686 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4687 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4688 | return llvm::ConstantInt::get(i64, PtrInt); |
| 4689 | } |
| 4690 | |
| 4691 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
| 4692 | llvm::NamedMDNode *&GlobalMetadata, |
| 4693 | GlobalDecl D, |
| 4694 | llvm::GlobalValue *Addr) { |
| 4695 | if (!GlobalMetadata) |
| 4696 | GlobalMetadata = |
| 4697 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
| 4698 | |
| 4699 | // TODO: should we report variant information for ctors/dtors? |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4700 | llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr), |
| 4701 | llvm::ConstantAsMetadata::get(GetPointerConstant( |
| 4702 | CGM.getLLVMContext(), D.getDecl()))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 4703 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 4706 | /// For each function which is declared within an extern "C" region and marked |
| 4707 | /// as 'used', but has internal linkage, create an alias from the unmangled |
| 4708 | /// name to the mangled name if possible. People expect to be able to refer |
| 4709 | /// to such functions with an unmangled name from inline assembly within the |
| 4710 | /// same translation unit. |
| 4711 | void CodeGenModule::EmitStaticExternCAliases() { |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 4712 | if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases()) |
Justin Lebar | cd2f6bb | 2016-01-25 22:36:37 +0000 | [diff] [blame] | 4713 | return; |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 4714 | for (auto &I : StaticExternCValues) { |
| 4715 | IdentifierInfo *Name = I.first; |
| 4716 | llvm::GlobalValue *Val = I.second; |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 4717 | if (Val && !getModule().getNamedValue(Name->getName())) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 4718 | addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 4719 | } |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 4722 | bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName, |
| 4723 | GlobalDecl &Result) const { |
| 4724 | auto Res = Manglings.find(MangledName); |
| 4725 | if (Res == Manglings.end()) |
| 4726 | return false; |
| 4727 | Result = Res->getValue(); |
| 4728 | return true; |
| 4729 | } |
| 4730 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4731 | /// Emits metadata nodes associating all the global values in the |
| 4732 | /// current module with the Decls they came from. This is useful for |
| 4733 | /// projects using IR gen as a subroutine. |
| 4734 | /// |
| 4735 | /// Since there's currently no way to associate an MDNode directly |
| 4736 | /// with an llvm::GlobalValue, we create a global named metadata |
| 4737 | /// with the name 'clang.global.decl.ptrs'. |
| 4738 | void CodeGenModule::EmitDeclMetadata() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4739 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4740 | |
Robert Lytton | 5b88f78f | 2014-07-03 09:30:29 +0000 | [diff] [blame] | 4741 | for (auto &I : MangledDeclNames) { |
| 4742 | llvm::GlobalValue *Addr = getModule().getNamedValue(I.second); |
Keno Fischer | 0e2d422 | 2015-11-05 23:18:44 +0000 | [diff] [blame] | 4743 | // Some mangled names don't necessarily have an associated GlobalValue |
| 4744 | // in this module, e.g. if we mangled it for DebugInfo. |
| 4745 | if (Addr) |
| 4746 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | /// Emits metadata nodes for all the local variables in the current |
| 4751 | /// function. |
| 4752 | void CodeGenFunction::EmitDeclMetadata() { |
| 4753 | if (LocalDeclMap.empty()) return; |
| 4754 | |
| 4755 | llvm::LLVMContext &Context = getLLVMContext(); |
| 4756 | |
| 4757 | // Find the unique metadata ID for this name. |
| 4758 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
| 4759 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4760 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4761 | |
Robert Lytton | 5b88f78f | 2014-07-03 09:30:29 +0000 | [diff] [blame] | 4762 | for (auto &I : LocalDeclMap) { |
| 4763 | const Decl *D = I.first; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4764 | llvm::Value *Addr = I.second.getPointer(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4765 | if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4766 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4767 | Alloca->setMetadata( |
| 4768 | DeclPtrKind, llvm::MDNode::get( |
| 4769 | Context, llvm::ValueAsMetadata::getConstant(DAddr))); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4770 | } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 4771 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
| 4772 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
| 4773 | } |
| 4774 | } |
| 4775 | } |
Daniel Dunbar | 900546d | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 4776 | |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 4777 | void CodeGenModule::EmitVersionIdentMetadata() { |
| 4778 | llvm::NamedMDNode *IdentMetadata = |
| 4779 | TheModule.getOrInsertNamedMetadata("llvm.ident"); |
| 4780 | std::string Version = getClangFullVersion(); |
| 4781 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 4782 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4783 | llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)}; |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 4784 | IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); |
| 4785 | } |
| 4786 | |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 4787 | void CodeGenModule::EmitTargetMetadata() { |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 4788 | // Warning, new MangledDeclNames may be appended within this loop. |
| 4789 | // We rely on MapVector insertions adding new elements to the end |
| 4790 | // of the container. |
| 4791 | // FIXME: Move this loop into the one target that needs it, and only |
| 4792 | // loop over those declarations for which we couldn't emit the target |
| 4793 | // metadata when we emitted the declaration. |
| 4794 | for (unsigned I = 0; I != MangledDeclNames.size(); ++I) { |
Richard Smith | 570706d | 2014-08-01 22:17:28 +0000 | [diff] [blame] | 4795 | auto Val = *(MangledDeclNames.begin() + I); |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 4796 | const Decl *D = Val.first.getDecl()->getMostRecentDecl(); |
| 4797 | llvm::GlobalValue *GV = GetGlobalValue(Val.second); |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 4798 | getTargetCodeGenInfo().emitTargetMD(D, GV, *this); |
| 4799 | } |
| 4800 | } |
| 4801 | |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 4802 | void CodeGenModule::EmitCoverageFile() { |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 4803 | if (getCodeGenOpts().CoverageDataFile.empty() && |
| 4804 | getCodeGenOpts().CoverageNotesFile.empty()) |
| 4805 | return; |
| 4806 | |
| 4807 | llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu"); |
| 4808 | if (!CUNode) |
| 4809 | return; |
| 4810 | |
| 4811 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
| 4812 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 4813 | auto *CoverageDataFile = |
| 4814 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile); |
| 4815 | auto *CoverageNotesFile = |
| 4816 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile); |
| 4817 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
| 4818 | llvm::MDNode *CU = CUNode->getOperand(i); |
| 4819 | llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU}; |
| 4820 | GCov->addOperand(llvm::MDNode::get(Ctx, Elts)); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 4821 | } |
| 4822 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4823 | |
Nico Weber | 17d3a2c | 2014-09-08 16:26:36 +0000 | [diff] [blame] | 4824 | llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) { |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4825 | // Sema has checked that all uuid strings are of the form |
| 4826 | // "12345678-1234-1234-1234-1234567890ab". |
| 4827 | assert(Uuid.size() == 36); |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 4828 | for (unsigned i = 0; i < 36; ++i) { |
| 4829 | if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-'); |
| 4830 | else assert(isHexDigit(Uuid[i])); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4831 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4832 | |
Nico Weber | dfa274e | 2014-09-08 16:11:15 +0000 | [diff] [blame] | 4833 | // 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] | 4834 | const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 }; |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4835 | |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 4836 | llvm::Constant *Field3[8]; |
| 4837 | for (unsigned Idx = 0; Idx < 8; ++Idx) |
| 4838 | Field3[Idx] = llvm::ConstantInt::get( |
| 4839 | Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16); |
| 4840 | |
| 4841 | llvm::Constant *Fields[4] = { |
| 4842 | llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16), |
| 4843 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16), |
| 4844 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16), |
| 4845 | llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3) |
| 4846 | }; |
| 4847 | |
| 4848 | return llvm::ConstantStruct::getAnon(Fields); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 4849 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 4850 | |
| 4851 | llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, |
| 4852 | bool ForEH) { |
| 4853 | // Return a bogus pointer if RTTI is disabled, unless it's for EH. |
| 4854 | // FIXME: should we even be calling this method if RTTI is disabled |
| 4855 | // and it's not for EH? |
| 4856 | if (!ForEH && !getLangOpts().RTTI) |
| 4857 | return llvm::Constant::getNullValue(Int8PtrTy); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4858 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 4859 | if (ForEH && Ty->isObjCObjectPointerType() && |
| 4860 | LangOpts.ObjCRuntime.isGNUFamily()) |
| 4861 | return ObjCRuntime->GetEHType(Ty); |
| 4862 | |
David Majnemer | 443250f | 2015-03-17 20:35:00 +0000 | [diff] [blame] | 4863 | return getCXXABI().getAddrOfRTTIDescriptor(Ty); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4866 | void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 4867 | // Do not emit threadprivates in simd-only mode. |
| 4868 | if (LangOpts.OpenMP && LangOpts.OpenMPSimd) |
| 4869 | return; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4870 | for (auto RefExpr : D->varlists()) { |
| 4871 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl()); |
| 4872 | bool PerformInit = |
| 4873 | VD->getAnyInitializer() && |
| 4874 | !VD->getAnyInitializer()->isConstantInitializer(getContext(), |
| 4875 | /*ForRef=*/false); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4876 | |
| 4877 | Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD)); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 4878 | if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4879 | VD, Addr, RefExpr->getLocStart(), PerformInit)) |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 4880 | CXXGlobalInits.push_back(InitFunction); |
| 4881 | } |
| 4882 | } |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 4883 | |
Peter Collingbourne | 25a80bf | 2015-09-08 23:01:30 +0000 | [diff] [blame] | 4884 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { |
| 4885 | llvm::Metadata *&InternalId = MetadataIdMap[T.getCanonicalType()]; |
| 4886 | if (InternalId) |
| 4887 | return InternalId; |
| 4888 | |
| 4889 | if (isExternallyVisible(T->getLinkage())) { |
| 4890 | std::string OutName; |
| 4891 | llvm::raw_string_ostream Out(OutName); |
| 4892 | getCXXABI().getMangleContext().mangleTypeName(T, Out); |
| 4893 | |
| 4894 | InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); |
| 4895 | } else { |
| 4896 | InternalId = llvm::MDNode::getDistinct(getLLVMContext(), |
| 4897 | llvm::ArrayRef<llvm::Metadata *>()); |
| 4898 | } |
| 4899 | |
| 4900 | return InternalId; |
| 4901 | } |
| 4902 | |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 4903 | // Generalize pointer types to a void pointer with the qualifiers of the |
| 4904 | // originally pointed-to type, e.g. 'const char *' and 'char * const *' |
| 4905 | // generalize to 'const void *' while 'char *' and 'const char **' generalize to |
| 4906 | // 'void *'. |
| 4907 | static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) { |
| 4908 | if (!Ty->isPointerType()) |
| 4909 | return Ty; |
| 4910 | |
| 4911 | return Ctx.getPointerType( |
| 4912 | QualType(Ctx.VoidTy).withCVRQualifiers( |
| 4913 | Ty->getPointeeType().getCVRQualifiers())); |
| 4914 | } |
| 4915 | |
| 4916 | // Apply type generalization to a FunctionType's return and argument types |
| 4917 | static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) { |
| 4918 | if (auto *FnType = Ty->getAs<FunctionProtoType>()) { |
| 4919 | SmallVector<QualType, 8> GeneralizedParams; |
| 4920 | for (auto &Param : FnType->param_types()) |
| 4921 | GeneralizedParams.push_back(GeneralizeType(Ctx, Param)); |
| 4922 | |
| 4923 | return Ctx.getFunctionType( |
| 4924 | GeneralizeType(Ctx, FnType->getReturnType()), |
| 4925 | GeneralizedParams, FnType->getExtProtoInfo()); |
| 4926 | } |
| 4927 | |
| 4928 | if (auto *FnType = Ty->getAs<FunctionNoProtoType>()) |
| 4929 | return Ctx.getFunctionNoProtoType( |
| 4930 | GeneralizeType(Ctx, FnType->getReturnType())); |
| 4931 | |
| 4932 | llvm_unreachable("Encountered unknown FunctionType"); |
| 4933 | } |
| 4934 | |
| 4935 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { |
| 4936 | T = GeneralizeFunctionType(getContext(), T); |
| 4937 | |
| 4938 | llvm::Metadata *&InternalId = GeneralizedMetadataIdMap[T.getCanonicalType()]; |
| 4939 | if (InternalId) |
| 4940 | return InternalId; |
| 4941 | |
| 4942 | if (isExternallyVisible(T->getLinkage())) { |
| 4943 | std::string OutName; |
| 4944 | llvm::raw_string_ostream Out(OutName); |
| 4945 | getCXXABI().getMangleContext().mangleTypeName(T, Out); |
| 4946 | Out << ".generalized"; |
| 4947 | |
| 4948 | InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); |
| 4949 | } else { |
| 4950 | InternalId = llvm::MDNode::getDistinct(getLLVMContext(), |
| 4951 | llvm::ArrayRef<llvm::Metadata *>()); |
| 4952 | } |
| 4953 | |
| 4954 | return InternalId; |
| 4955 | } |
| 4956 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4957 | /// Returns whether this module needs the "all-vtables" type identifier. |
| 4958 | bool CodeGenModule::NeedAllVtablesTypeId() const { |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4959 | // Returns true if at least one of vtable-based CFI checkers is enabled and |
| 4960 | // is not in the trapping mode. |
| 4961 | return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) && |
| 4962 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) || |
| 4963 | (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) && |
| 4964 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) || |
| 4965 | (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) && |
| 4966 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) || |
| 4967 | (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) && |
| 4968 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast))); |
| 4969 | } |
| 4970 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4971 | void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable, |
| 4972 | CharUnits Offset, |
| 4973 | const CXXRecordDecl *RD) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4974 | llvm::Metadata *MD = |
| 4975 | CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4976 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 4977 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4978 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 4979 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
| 4980 | VTable->addTypeMetadata(Offset.getQuantity(), |
| 4981 | llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4982 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4983 | if (NeedAllVtablesTypeId()) { |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4984 | llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables"); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 4985 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 4986 | } |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 4987 | } |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 4988 | |
| 4989 | // Fills in the supplied string map with the set of target features for the |
| 4990 | // passed in function. |
| 4991 | void CodeGenModule::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
| 4992 | const FunctionDecl *FD) { |
| 4993 | StringRef TargetCPU = Target.getTargetOpts().CPU; |
| 4994 | if (const auto *TD = FD->getAttr<TargetAttr>()) { |
| 4995 | // If we have a TargetAttr build up the feature map based on that. |
| 4996 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
| 4997 | |
Erich Keane | cf8807c | 2017-10-27 18:32:23 +0000 | [diff] [blame] | 4998 | ParsedAttr.Features.erase( |
| 4999 | llvm::remove_if(ParsedAttr.Features, |
| 5000 | [&](const std::string &Feat) { |
| 5001 | return !Target.isValidFeatureName( |
| 5002 | StringRef{Feat}.substr(1)); |
| 5003 | }), |
| 5004 | ParsedAttr.Features.end()); |
| 5005 | |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5006 | // Make a copy of the features as passed on the command line into the |
| 5007 | // beginning of the additional features from the function to override. |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame] | 5008 | ParsedAttr.Features.insert(ParsedAttr.Features.begin(), |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5009 | Target.getTargetOpts().FeaturesAsWritten.begin(), |
| 5010 | Target.getTargetOpts().FeaturesAsWritten.end()); |
| 5011 | |
Erich Keane | cf8807c | 2017-10-27 18:32:23 +0000 | [diff] [blame] | 5012 | if (ParsedAttr.Architecture != "" && |
| 5013 | Target.isValidCPUName(ParsedAttr.Architecture)) |
| 5014 | TargetCPU = ParsedAttr.Architecture; |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5015 | |
| 5016 | // Now populate the feature map, first with the TargetCPU which is either |
| 5017 | // the default or a new one from the target attribute string. Then we'll use |
| 5018 | // the passed in features (FeaturesAsWritten) along with the new ones from |
| 5019 | // the attribute. |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame] | 5020 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
| 5021 | ParsedAttr.Features); |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5022 | } else { |
| 5023 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
| 5024 | Target.getTargetOpts().Features); |
| 5025 | } |
| 5026 | } |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 5027 | |
| 5028 | llvm::SanitizerStatReport &CodeGenModule::getSanStats() { |
| 5029 | if (!SanStats) |
| 5030 | SanStats = llvm::make_unique<llvm::SanitizerStatReport>(&getModule()); |
| 5031 | |
| 5032 | return *SanStats; |
| 5033 | } |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5034 | llvm::Value * |
| 5035 | CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, |
| 5036 | CodeGenFunction &CGF) { |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 5037 | llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType()); |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 5038 | auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr()); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5039 | auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); |
| 5040 | return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy, |
| 5041 | "__translate_sampler_initializer"), |
| 5042 | {C}); |
| 5043 | } |