Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This coordinates the per-module state used while generating code. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CodeGenModule.h" |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 14 | #include "CGBlocks.h" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 15 | #include "CGCUDARuntime.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
| 18 | #include "CGDebugInfo.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 19 | #include "CGObjCRuntime.h" |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 20 | #include "CGOpenCLRuntime.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 21 | #include "CGOpenMPRuntime.h" |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 22 | #include "CGOpenMPRuntimeNVPTX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "CodeGenFunction.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 24 | #include "CodeGenPGO.h" |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 25 | #include "ConstantEmitter.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 26 | #include "CoverageMappingGen.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 27 | #include "TargetInfo.h" |
Chris Lattner | 2ccb73b | 2007-06-16 00:16:26 +0000 | [diff] [blame] | 28 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 29 | #include "clang/AST/CharUnits.h" |
Chris Lattner | b8c18fa | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 5dd3474 | 2010-06-21 18:41:26 +0000 | [diff] [blame] | 32 | #include "clang/AST/DeclTemplate.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 33 | #include "clang/AST/Mangle.h" |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 34 | #include "clang/AST/RecordLayout.h" |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 35 | #include "clang/AST/RecursiveASTVisitor.h" |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 36 | #include "clang/AST/StmtVisitor.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" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 39 | #include "clang/Basic/CodeGenOptions.h" |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 40 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 41 | #include "clang/Basic/Module.h" |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 42 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 43 | #include "clang/Basic/TargetInfo.h" |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Version.h" |
John McCall | 5ad7407 | 2017-03-02 20:04:19 +0000 | [diff] [blame] | 45 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Richard Trieu | 5337c74 | 2018-12-06 06:12:20 +0000 | [diff] [blame] | 46 | #include "clang/Frontend/FrontendDiagnostic.h" |
Caroline Tice | 6227973 | 2018-09-21 18:34:59 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/StringSwitch.h" |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/Triple.h" |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/TargetLibraryInfo.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" |
Rong Xu | 39f6d7e | 2019-03-01 17:50:20 +0000 | [diff] [blame] | 55 | #include "llvm/IR/ProfileSummary.h" |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 56 | #include "llvm/ProfileData/InstrProfReader.h" |
Caroline Tice | 6227973 | 2018-09-21 18:34:59 +0000 | [diff] [blame] | 57 | #include "llvm/Support/CodeGen.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 58 | #include "llvm/Support/ConvertUTF.h" |
Chris Lattner | 15275e5 | 2009-11-07 09:22:46 +0000 | [diff] [blame] | 59 | #include "llvm/Support/ErrorHandling.h" |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 60 | #include "llvm/Support/MD5.h" |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 61 | #include "llvm/Support/TimeProfiler.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 62 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | using namespace CodeGen; |
| 65 | |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 66 | static llvm::cl::opt<bool> LimitedCoverage( |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 67 | "limited-coverage-experimental", llvm::cl::ZeroOrMore, llvm::cl::Hidden, |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 68 | llvm::cl::desc("Emit limited coverage mapping information (experimental)"), |
| 69 | llvm::cl::init(false)); |
| 70 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 71 | static const char AnnotationSection[] = "llvm.metadata"; |
| 72 | |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 73 | static CGCXXABI *createCXXABI(CodeGenModule &CGM) { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 74 | switch (CGM.getTarget().getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 75 | case TargetCXXABI::GenericAArch64: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 76 | case TargetCXXABI::GenericARM: |
| 77 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 78 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 79 | case TargetCXXABI::WatchOS: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 80 | case TargetCXXABI::GenericMIPS: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 81 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 82 | case TargetCXXABI::WebAssembly: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 83 | return CreateItaniumCXXABI(CGM); |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 84 | case TargetCXXABI::Microsoft: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 85 | return CreateMicrosoftCXXABI(CGM); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | llvm_unreachable("invalid C++ ABI kind"); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 91 | CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO, |
| 92 | const PreprocessorOptions &PPO, |
| 93 | const CodeGenOptions &CGO, llvm::Module &M, |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 94 | DiagnosticsEngine &diags, |
| 95 | CoverageSourceInfo *CoverageInfo) |
Adrian Prantl | e74f525 | 2015-06-30 02:26:03 +0000 | [diff] [blame] | 96 | : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO), |
| 97 | PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 98 | Target(C.getTargetInfo()), ABI(createCXXABI(*this)), |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 99 | VMContext(M.getContext()), Types(*this), VTables(*this), |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 100 | SanitizerMD(new SanitizerMetadata(*this)) { |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 101 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 102 | // Initialize the type cache. |
| 103 | llvm::LLVMContext &LLVMContext = M.getContext(); |
| 104 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
| 105 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
| 106 | Int16Ty = llvm::Type::getInt16Ty(LLVMContext); |
| 107 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 108 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
Abderrazek Zaafrani | f58a132 | 2017-12-21 19:20:01 +0000 | [diff] [blame] | 109 | HalfTy = llvm::Type::getHalfTy(LLVMContext); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 110 | FloatTy = llvm::Type::getFloatTy(LLVMContext); |
| 111 | DoubleTy = llvm::Type::getDoubleTy(LLVMContext); |
| 112 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
| 113 | PointerAlignInBytes = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 114 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
Yaxun Liu | 26f7566 | 2016-08-19 05:17:25 +0000 | [diff] [blame] | 115 | SizeSizeInBytes = |
| 116 | C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 117 | IntAlignInBytes = |
| 118 | C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 119 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
Yaxun Liu | 26f7566 | 2016-08-19 05:17:25 +0000 | [diff] [blame] | 120 | IntPtrTy = llvm::IntegerType::get(LLVMContext, |
| 121 | C.getTargetInfo().getMaxPointerWidth()); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 122 | Int8PtrTy = Int8Ty->getPointerTo(0); |
| 123 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 124 | AllocaInt8PtrTy = Int8Ty->getPointerTo( |
| 125 | M.getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 126 | ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace(); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 127 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 128 | RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); |
| 129 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 130 | if (LangOpts.ObjC) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 131 | createObjCRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 132 | if (LangOpts.OpenCL) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 133 | createOpenCLRuntime(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 134 | if (LangOpts.OpenMP) |
| 135 | createOpenMPRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 136 | if (LangOpts.CUDA) |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 137 | createCUDARuntime(); |
Sanjiv Gupta | 15cb669 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 138 | |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 139 | // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 140 | if (LangOpts.Sanitize.has(SanitizerKind::Thread) || |
Peter Collingbourne | e9f5210 | 2019-01-15 20:59:59 +0000 | [diff] [blame] | 141 | (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 142 | TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(), |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 143 | getCXXABI().getMangleContext())); |
Peter Collingbourne | e9f5210 | 2019-01-15 20:59:59 +0000 | [diff] [blame] | 144 | |
Nick Lewycky | 207bce3 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 145 | // If debug info or coverage generation is enabled, create the CGDebugInfo |
| 146 | // object. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 147 | if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo || |
| 148 | CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 149 | DebugInfo.reset(new CGDebugInfo(*this)); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 150 | |
| 151 | Block.GlobalUniqueCount = 0; |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 152 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 153 | if (C.getLangOpts().ObjC) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 154 | ObjCData.reset(new ObjCEntrypoints()); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 155 | |
Rong Xu | 9c6f153 | 2016-03-02 20:59:36 +0000 | [diff] [blame] | 156 | if (CodeGenOpts.hasProfileClangUse()) { |
| 157 | auto ReaderOrErr = llvm::IndexedInstrProfReader::create( |
Richard Smith | 8654ae5 | 2018-10-10 23:13:35 +0000 | [diff] [blame] | 158 | CodeGenOpts.ProfileInstrumentUsePath, CodeGenOpts.ProfileRemappingFile); |
Vedant Kumar | fa2d595 | 2016-05-19 03:54:54 +0000 | [diff] [blame] | 159 | if (auto E = ReaderOrErr.takeError()) { |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 160 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
Diego Novillo | 5aecacb | 2015-06-25 22:56:00 +0000 | [diff] [blame] | 161 | "Could not read profile %0: %1"); |
Vedant Kumar | fa2d595 | 2016-05-19 03:54:54 +0000 | [diff] [blame] | 162 | llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) { |
| 163 | getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath |
| 164 | << EI.message(); |
| 165 | }); |
Nico Weber | 176efac | 2015-04-06 04:16:48 +0000 | [diff] [blame] | 166 | } else |
| 167 | PGOReader = std::move(ReaderOrErr.get()); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 168 | } |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 169 | |
| 170 | // If coverage mapping generation is enabled, create the |
| 171 | // CoverageMappingModuleGen object. |
| 172 | if (CodeGenOpts.CoverageMapping) |
| 173 | CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo)); |
Chris Lattner | a087ff9 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 176 | CodeGenModule::~CodeGenModule() {} |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 177 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 178 | void CodeGenModule::createObjCRuntime() { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 179 | // This is just isGNUFamily(), but we want to force implementors of |
| 180 | // new ABIs to decide how best to do this. |
| 181 | switch (LangOpts.ObjCRuntime.getKind()) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 182 | case ObjCRuntime::GNUstep: |
| 183 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 184 | case ObjCRuntime::ObjFW: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 185 | ObjCRuntime.reset(CreateGNUObjCRuntime(*this)); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 186 | return; |
| 187 | |
| 188 | case ObjCRuntime::FragileMacOSX: |
| 189 | case ObjCRuntime::MacOSX: |
| 190 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 191 | case ObjCRuntime::WatchOS: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 192 | ObjCRuntime.reset(CreateMacObjCRuntime(*this)); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | llvm_unreachable("bad runtime kind"); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 198 | void CodeGenModule::createOpenCLRuntime() { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 199 | OpenCLRuntime.reset(new CGOpenCLRuntime(*this)); |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 202 | void CodeGenModule::createOpenMPRuntime() { |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 203 | // Select a specialized code generation class based on the target, if any. |
| 204 | // If it does not exist use the default implementation. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 205 | switch (getTriple().getArch()) { |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 206 | case llvm::Triple::nvptx: |
| 207 | case llvm::Triple::nvptx64: |
| 208 | assert(getLangOpts().OpenMPIsDevice && |
| 209 | "OpenMP NVPTX is only prepared to deal with device code."); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 210 | OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this)); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 211 | break; |
| 212 | default: |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 213 | if (LangOpts.OpenMPSimd) |
| 214 | OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this)); |
| 215 | else |
| 216 | OpenMPRuntime.reset(new CGOpenMPRuntime(*this)); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 217 | break; |
| 218 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 221 | void CodeGenModule::createCUDARuntime() { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 222 | CUDARuntime.reset(CreateNVCUDARuntime(*this)); |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 225 | void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) { |
| 226 | Replacements[Name] = C; |
| 227 | } |
| 228 | |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 229 | void CodeGenModule::applyReplacements() { |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 230 | for (auto &I : Replacements) { |
| 231 | StringRef MangledName = I.first(); |
| 232 | llvm::Constant *Replacement = I.second; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 233 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 234 | if (!Entry) |
| 235 | continue; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 236 | auto *OldF = cast<llvm::Function>(Entry); |
| 237 | auto *NewF = dyn_cast<llvm::Function>(Replacement); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 238 | if (!NewF) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 239 | if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) { |
Rafael Espindola | 0fcad88 | 2014-05-16 22:20:18 +0000 | [diff] [blame] | 240 | NewF = dyn_cast<llvm::Function>(Alias->getAliasee()); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 241 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 242 | auto *CE = cast<llvm::ConstantExpr>(Replacement); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 243 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 244 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
| 245 | NewF = dyn_cast<llvm::Function>(CE->getOperand(0)); |
| 246 | } |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Replace old with new, but keep the old order. |
| 250 | OldF->replaceAllUsesWith(Replacement); |
| 251 | if (NewF) { |
| 252 | NewF->removeFromParent(); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 253 | OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(), |
| 254 | NewF); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 255 | } |
| 256 | OldF->eraseFromParent(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 260 | void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) { |
| 261 | GlobalValReplacements.push_back(std::make_pair(GV, C)); |
| 262 | } |
| 263 | |
| 264 | void CodeGenModule::applyGlobalValReplacements() { |
| 265 | for (auto &I : GlobalValReplacements) { |
| 266 | llvm::GlobalValue *GV = I.first; |
| 267 | llvm::Constant *C = I.second; |
| 268 | |
| 269 | GV->replaceAllUsesWith(C); |
| 270 | GV->eraseFromParent(); |
| 271 | } |
| 272 | } |
| 273 | |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 274 | // This is only used in aliases that we created and we know they have a |
| 275 | // linear structure. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 276 | static const llvm::GlobalObject *getAliasedGlobal( |
| 277 | const llvm::GlobalIndirectSymbol &GIS) { |
| 278 | llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited; |
| 279 | const llvm::Constant *C = &GIS; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 280 | for (;;) { |
Rafael Espindola | e11f40a | 2014-06-04 19:03:20 +0000 | [diff] [blame] | 281 | C = C->stripPointerCasts(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 282 | if (auto *GO = dyn_cast<llvm::GlobalObject>(C)) |
| 283 | return GO; |
| 284 | // stripPointerCasts will not walk over weak aliases. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 285 | auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C); |
| 286 | if (!GIS2) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 287 | return nullptr; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 288 | if (!Visited.insert(GIS2).second) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 289 | return nullptr; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 290 | C = GIS2->getIndirectSymbol(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 294 | void CodeGenModule::checkAliases() { |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 295 | // Check if the constructed aliases are well formed. It is really unfortunate |
| 296 | // that we have to do this in CodeGen, but we only construct mangled names |
| 297 | // and aliases during codegen. |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 298 | bool Error = false; |
Rafael Espindola | c67b815 | 2014-05-05 19:33:09 +0000 | [diff] [blame] | 299 | DiagnosticsEngine &Diags = getDiags(); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 300 | for (const GlobalDecl &GD : Aliases) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 301 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 302 | SourceLocation Location; |
| 303 | bool IsIFunc = D->hasAttr<IFuncAttr>(); |
| 304 | if (const Attr *A = D->getDefiningAttr()) |
| 305 | Location = A->getLocation(); |
| 306 | else |
| 307 | llvm_unreachable("Not an alias or ifunc?"); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 308 | StringRef MangledName = getMangledName(GD); |
| 309 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 310 | auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 311 | const llvm::GlobalValue *GV = getAliasedGlobal(*Alias); |
| 312 | if (!GV) { |
| 313 | Error = true; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 314 | Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 315 | } else if (GV->isDeclaration()) { |
Rafael Espindola | 93c7a65 | 2014-03-13 15:47:50 +0000 | [diff] [blame] | 316 | Error = true; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 317 | Diags.Report(Location, diag::err_alias_to_undefined) |
| 318 | << IsIFunc << IsIFunc; |
| 319 | } else if (IsIFunc) { |
| 320 | // Check resolver function type. |
| 321 | llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>( |
| 322 | GV->getType()->getPointerElementType()); |
| 323 | assert(FTy); |
| 324 | if (!FTy->getReturnType()->isPointerTy()) |
| 325 | Diags.Report(Location, diag::err_ifunc_resolver_return); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 326 | } |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 327 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 328 | llvm::Constant *Aliasee = Alias->getIndirectSymbol(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 329 | llvm::GlobalValue *AliaseeGV; |
| 330 | if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee)) |
| 331 | AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0)); |
| 332 | else |
| 333 | AliaseeGV = cast<llvm::GlobalValue>(Aliasee); |
| 334 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 335 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 336 | StringRef AliasSection = SA->getName(); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 337 | if (AliasSection != AliaseeGV->getSection()) |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 338 | Diags.Report(SA->getLocation(), diag::warn_alias_with_section) |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 339 | << AliasSection << IsIFunc << IsIFunc; |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 340 | } |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 341 | |
| 342 | // We have to handle alias to weak aliases in here. LLVM itself disallows |
| 343 | // this since the object semantics would not match the IL one. For |
| 344 | // compatibility with gcc we implement it by just pointing the alias |
| 345 | // to its aliasee's aliasee. We also warn, since the user is probably |
| 346 | // expecting the link to be weak. |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 347 | if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) { |
Sanjoy Das | f60a0d7 | 2016-04-08 01:31:02 +0000 | [diff] [blame] | 348 | if (GA->isInterposable()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 349 | Diags.Report(Location, diag::warn_alias_to_weak_alias) |
| 350 | << GV->getName() << GA->getName() << IsIFunc; |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 351 | Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 352 | GA->getIndirectSymbol(), Alias->getType()); |
| 353 | Alias->setIndirectSymbol(Aliasee); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 356 | } |
| 357 | if (!Error) |
| 358 | return; |
| 359 | |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 360 | for (const GlobalDecl &GD : Aliases) { |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 361 | StringRef MangledName = getMangledName(GD); |
| 362 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 363 | auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 364 | Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType())); |
| 365 | Alias->eraseFromParent(); |
| 366 | } |
| 367 | } |
| 368 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 369 | void CodeGenModule::clear() { |
| 370 | DeferredDeclsToEmit.clear(); |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 371 | if (OpenMPRuntime) |
| 372 | OpenMPRuntime->clear(); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 375 | void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags, |
| 376 | StringRef MainFile) { |
| 377 | if (!hasDiagnostics()) |
| 378 | return; |
| 379 | if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) { |
| 380 | if (MainFile.empty()) |
| 381 | MainFile = "<stdin>"; |
| 382 | Diags.Report(diag::warn_profile_data_unprofiled) << MainFile; |
Vedant Kumar | 96d6ca7 | 2017-04-27 17:30:58 +0000 | [diff] [blame] | 383 | } else { |
| 384 | if (Mismatched > 0) |
| 385 | Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched; |
| 386 | |
| 387 | if (Missing > 0) |
| 388 | Diags.Report(diag::warn_profile_data_missing) << Visited << Missing; |
| 389 | } |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 392 | void CodeGenModule::Release() { |
Chris Lattner | a5ae54a | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 393 | EmitDeferred(); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 394 | EmitVTablesOpportunistically(); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 395 | applyGlobalValReplacements(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 396 | applyReplacements(); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 397 | checkAliases(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 398 | emitMultiVersionFunctions(); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 399 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | fe06df4 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 400 | EmitCXXGlobalDtorFunc(); |
Akira Hatanaka | 617e261 | 2018-04-17 18:41:52 +0000 | [diff] [blame] | 401 | registerGlobalDtorsWithAtExit(); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 402 | EmitCXXThreadLocalInitFunc(); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 403 | if (ObjCRuntime) |
| 404 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 405 | AddGlobalCtor(ObjCInitFunction); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 406 | if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice && |
| 407 | CUDARuntime) { |
Artem Belevich | c66d254 | 2018-06-27 18:32:51 +0000 | [diff] [blame] | 408 | if (llvm::Function *CudaCtorFunction = |
| 409 | CUDARuntime->makeModuleCtorFunction()) |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 410 | AddGlobalCtor(CudaCtorFunction); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 411 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 412 | if (OpenMPRuntime) { |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 413 | if (llvm::Function *OpenMPRequiresDirectiveRegFun = |
| 414 | OpenMPRuntime->emitRequiresDirectiveRegFun()) { |
| 415 | AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0); |
| 416 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 417 | if (llvm::Function *OpenMPRegistrationFunction = |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 418 | OpenMPRuntime->emitRegistrationFunction()) { |
| 419 | auto ComdatKey = OpenMPRegistrationFunction->hasComdat() ? |
| 420 | OpenMPRegistrationFunction : nullptr; |
| 421 | AddGlobalCtor(OpenMPRegistrationFunction, 0, ComdatKey); |
| 422 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 423 | OpenMPRuntime->clear(); |
| 424 | } |
Easwaran Raman | 695890c | 2015-12-17 19:14:27 +0000 | [diff] [blame] | 425 | if (PGOReader) { |
Rong Xu | 39f6d7e | 2019-03-01 17:50:20 +0000 | [diff] [blame] | 426 | getModule().setProfileSummary( |
| 427 | PGOReader->getSummary(/* UseCS */ false).getMD(VMContext), |
| 428 | llvm::ProfileSummary::PSK_Instr); |
Easwaran Raman | 695890c | 2015-12-17 19:14:27 +0000 | [diff] [blame] | 429 | if (PGOStats.hasDiagnostics()) |
| 430 | PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName); |
| 431 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 432 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 433 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 434 | EmitGlobalAnnotations(); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 435 | EmitStaticExternCAliases(); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 436 | EmitDeferredUnusedCoverageMappings(); |
| 437 | if (CoverageMapping) |
| 438 | CoverageMapping->emit(); |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 439 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 440 | CodeGenFunction(*this).EmitCfiCheckFail(); |
Evgeniy Stepanov | 1a8030e | 2017-04-07 23:00:38 +0000 | [diff] [blame] | 441 | CodeGenFunction(*this).EmitCfiCheckStub(); |
| 442 | } |
Alex Lorenz | a8fbef4 | 2017-03-23 11:14:27 +0000 | [diff] [blame] | 443 | emitAtAvailableLinkGuard(); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 444 | emitLLVMUsed(); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 445 | if (SanStats) |
| 446 | SanStats->finish(); |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 447 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 448 | if (CodeGenOpts.Autolink && |
| 449 | (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 450 | EmitModuleLinkOptions(); |
| 451 | } |
Nirav Dave | 8497ef4 | 2017-03-18 00:43:39 +0000 | [diff] [blame] | 452 | |
Ben Dunbobbin | 1d16515 | 2019-05-17 03:44:15 +0000 | [diff] [blame] | 453 | // On ELF we pass the dependent library specifiers directly to the linker |
| 454 | // without manipulating them. This is in contrast to other platforms where |
| 455 | // they are mapped to a specific linker option by the compiler. This |
| 456 | // difference is a result of the greater variety of ELF linkers and the fact |
| 457 | // that ELF linkers tend to handle libraries in a more complicated fashion |
| 458 | // than on other platforms. This forces us to defer handling the dependent |
| 459 | // libs to the linker. |
Yaxun Liu | 02afe4e | 2019-05-28 21:18:59 +0000 | [diff] [blame] | 460 | // |
| 461 | // CUDA/HIP device and host libraries are different. Currently there is no |
| 462 | // way to differentiate dependent libraries for host or device. Existing |
| 463 | // usage of #pragma comment(lib, *) is intended for host libraries on |
| 464 | // Windows. Therefore emit llvm.dependent-libraries only for host. |
| 465 | if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) { |
Ben Dunbobbin | 1d16515 | 2019-05-17 03:44:15 +0000 | [diff] [blame] | 466 | auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries"); |
| 467 | for (auto *MD : ELFDependentLibraries) |
| 468 | NMD->addOperand(MD); |
| 469 | } |
| 470 | |
Nirav Dave | 741dea0 | 2017-03-30 13:41:44 +0000 | [diff] [blame] | 471 | // Record mregparm value now so it is visible through rest of codegen. |
| 472 | if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 473 | getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters", |
| 474 | CodeGenOpts.NumRegisterParameters); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 475 | |
Reid Kleckner | 124955a | 2015-08-05 18:51:13 +0000 | [diff] [blame] | 476 | if (CodeGenOpts.DwarfVersion) { |
Manman Ren | 9691f7f | 2013-06-19 01:46:49 +0000 | [diff] [blame] | 477 | // We actually want the latest version when there are conflicts. |
| 478 | // We can change from Warning to Latest if such mode is supported. |
| 479 | getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version", |
| 480 | CodeGenOpts.DwarfVersion); |
Reid Kleckner | 124955a | 2015-08-05 18:51:13 +0000 | [diff] [blame] | 481 | } |
| 482 | if (CodeGenOpts.EmitCodeView) { |
| 483 | // Indicate that we want CodeView in the metadata. |
| 484 | getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1); |
| 485 | } |
Reid Kleckner | 7555771 | 2018-11-16 18:47:41 +0000 | [diff] [blame] | 486 | if (CodeGenOpts.CodeViewGHash) { |
| 487 | getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1); |
| 488 | } |
Adrian McCarthy | db2736d | 2018-01-09 23:49:30 +0000 | [diff] [blame] | 489 | if (CodeGenOpts.ControlFlowGuard) { |
| 490 | // We want function ID tables for Control Flow Guard. |
Hans Wennborg | a912e3e | 2018-08-10 09:49:21 +0000 | [diff] [blame] | 491 | getModule().addModuleFlag(llvm::Module::Warning, "cfguardtable", 1); |
Adrian McCarthy | db2736d | 2018-01-09 23:49:30 +0000 | [diff] [blame] | 492 | } |
Piotr Padlewski | 9d0ecf2 | 2015-09-15 21:46:50 +0000 | [diff] [blame] | 493 | if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) { |
| 494 | // We don't support LTO with 2 with different StrictVTablePointers |
| 495 | // FIXME: we could support it by stripping all the information introduced |
| 496 | // by StrictVTablePointers. |
| 497 | |
| 498 | getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1); |
| 499 | |
| 500 | llvm::Metadata *Ops[2] = { |
| 501 | llvm::MDString::get(VMContext, "StrictVTablePointers"), |
| 502 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 503 | llvm::Type::getInt32Ty(VMContext), 1))}; |
| 504 | |
| 505 | getModule().addModuleFlag(llvm::Module::Require, |
| 506 | "StrictVTablePointersRequirement", |
| 507 | llvm::MDNode::get(VMContext, Ops)); |
| 508 | } |
Manman Ren | 4b7f23d | 2013-11-22 19:42:45 +0000 | [diff] [blame] | 509 | if (DebugInfo) |
Adrian Prantl | 2dbdd20 | 2014-05-19 23:40:06 +0000 | [diff] [blame] | 510 | // We support a single version in the linked module. The LLVM |
| 511 | // parser will drop debug info with a different version number |
| 512 | // (and warn about it, too). |
| 513 | getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version", |
Manman Ren | 61fd594 | 2013-12-02 20:10:37 +0000 | [diff] [blame] | 514 | llvm::DEBUG_METADATA_VERSION); |
Douglas Gregor | ea02f26 | 2013-01-14 18:28:43 +0000 | [diff] [blame] | 515 | |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 516 | // 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] | 517 | // the correct build attributes in the ARM backend. wchar_size is also used by |
| 518 | // TargetLibraryInfo. |
Matthias Braun | bffc1bb | 2017-09-26 02:37:23 +0000 | [diff] [blame] | 519 | uint64_t WCharWidth = |
| 520 | Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity(); |
Matthias Braun | a451953 | 2017-05-20 01:29:55 +0000 | [diff] [blame] | 521 | getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth); |
| 522 | |
Justin Bogner | 9bb7f15 | 2014-06-23 20:03:28 +0000 | [diff] [blame] | 523 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 524 | if ( Arch == llvm::Triple::arm |
| 525 | || Arch == llvm::Triple::armeb |
| 526 | || Arch == llvm::Triple::thumb |
| 527 | || Arch == llvm::Triple::thumbeb) { |
Oliver Stannard | e3a4fb6 | 2014-06-20 12:43:07 +0000 | [diff] [blame] | 528 | // The minimum width of an enum in bytes |
| 529 | uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4; |
| 530 | getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth); |
| 531 | } |
| 532 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 533 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
| 534 | // Indicate that we want cross-DSO control flow integrity checks. |
| 535 | getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1); |
| 536 | } |
| 537 | |
Oren Ben Simhon | 57cc1a5 | 2018-01-09 08:53:59 +0000 | [diff] [blame] | 538 | if (CodeGenOpts.CFProtectionReturn && |
| 539 | Target.checkCFProtectionReturnSupported(getDiags())) { |
| 540 | // Indicate that we want to instrument return control flow protection. |
| 541 | getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return", |
| 542 | 1); |
| 543 | } |
| 544 | |
| 545 | if (CodeGenOpts.CFProtectionBranch && |
| 546 | Target.checkCFProtectionBranchSupported(getDiags())) { |
| 547 | // Indicate that we want to instrument branch control flow protection. |
| 548 | getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch", |
| 549 | 1); |
| 550 | } |
| 551 | |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 552 | if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) { |
Justin Lebar | d3a44f6 | 2016-04-05 18:26:20 +0000 | [diff] [blame] | 553 | // Indicate whether __nvvm_reflect should be configured to flush denormal |
| 554 | // floating point values to 0. (This corresponds to its "__CUDA_FTZ" |
| 555 | // property.) |
| 556 | getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz", |
Yaxun Liu | e1bfbc5 | 2018-07-21 02:02:22 +0000 | [diff] [blame] | 557 | CodeGenOpts.FlushDenorm ? 1 : 0); |
Justin Lebar | d3a44f6 | 2016-04-05 18:26:20 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 560 | // Emit OpenCL specific module metadata: OpenCL/SPIR version. |
| 561 | if (LangOpts.OpenCL) { |
| 562 | EmitOpenCLMetadata(); |
| 563 | // Emit SPIR version. |
Anastasia Stulova | eba9a6e | 2019-05-09 10:25:45 +0000 | [diff] [blame] | 564 | if (getTriple().isSPIR()) { |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 565 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 566 | // opencl.spir.version named metadata. |
Anastasia Stulova | f61b548 | 2019-05-30 15:18:07 +0000 | [diff] [blame] | 567 | // C++ is backwards compatible with OpenCL v2.0. |
| 568 | auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion; |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 569 | llvm::Metadata *SPIRVerElts[] = { |
| 570 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
Anastasia Stulova | f61b548 | 2019-05-30 15:18:07 +0000 | [diff] [blame] | 571 | Int32Ty, Version / 100)), |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 572 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
Anastasia Stulova | f61b548 | 2019-05-30 15:18:07 +0000 | [diff] [blame] | 573 | Int32Ty, (Version / 100 > 1) ? 0 : 2))}; |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 574 | llvm::NamedMDNode *SPIRVerMD = |
| 575 | TheModule.getOrInsertNamedMetadata("opencl.spir.version"); |
| 576 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 577 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
| 578 | } |
| 579 | } |
| 580 | |
Justin Hibbits | 90ca05e | 2014-11-18 06:17:20 +0000 | [diff] [blame] | 581 | if (uint32_t PLevel = Context.getLangOpts().PICLevel) { |
Sriraman Tallam | 70e70e6 | 2016-04-28 22:34:00 +0000 | [diff] [blame] | 582 | assert(PLevel < 3 && "Invalid PIC Level"); |
| 583 | getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel)); |
Rafael Espindola | c9d336e | 2016-06-23 15:07:32 +0000 | [diff] [blame] | 584 | if (Context.getLangOpts().PIE) |
| 585 | getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel)); |
Justin Hibbits | 90ca05e | 2014-11-18 06:17:20 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Caroline Tice | 6227973 | 2018-09-21 18:34:59 +0000 | [diff] [blame] | 588 | if (getCodeGenOpts().CodeModel.size() > 0) { |
| 589 | unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel) |
| 590 | .Case("tiny", llvm::CodeModel::Tiny) |
| 591 | .Case("small", llvm::CodeModel::Small) |
| 592 | .Case("kernel", llvm::CodeModel::Kernel) |
| 593 | .Case("medium", llvm::CodeModel::Medium) |
| 594 | .Case("large", llvm::CodeModel::Large) |
| 595 | .Default(~0u); |
| 596 | if (CM != ~0u) { |
| 597 | llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM); |
| 598 | getModule().setCodeModel(codeModel); |
| 599 | } |
| 600 | } |
| 601 | |
Sriraman Tallam | 80af005 | 2018-02-23 21:27:33 +0000 | [diff] [blame] | 602 | if (CodeGenOpts.NoPLT) |
| 603 | getModule().setRtLibUseGOT(); |
| 604 | |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 605 | SimplifyPersonality(); |
| 606 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 607 | if (getCodeGenOpts().EmitDeclMetadata) |
| 608 | EmitDeclMetadata(); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 609 | |
| 610 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 611 | EmitCoverageFile(); |
Devang Patel | ffa30ab | 2011-08-16 20:58:22 +0000 | [diff] [blame] | 612 | |
| 613 | if (DebugInfo) |
| 614 | DebugInfo->finalize(); |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 615 | |
Mikhail Maltsev | 4a4e7a3 | 2018-04-23 10:08:46 +0000 | [diff] [blame] | 616 | if (getCodeGenOpts().EmitVersionIdentMetadata) |
| 617 | EmitVersionIdentMetadata(); |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 618 | |
Scott Linder | de6beb0 | 2018-12-14 15:38:15 +0000 | [diff] [blame] | 619 | if (!getCodeGenOpts().RecordCommandLine.empty()) |
| 620 | EmitCommandLineMetadata(); |
| 621 | |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 622 | EmitTargetMetadata(); |
Daniel Dunbar | 23fd462 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 625 | void CodeGenModule::EmitOpenCLMetadata() { |
| 626 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 627 | // opencl.ocl.version named metadata node. |
Anastasia Stulova | f61b548 | 2019-05-30 15:18:07 +0000 | [diff] [blame] | 628 | // C++ is backwards compatible with OpenCL v2.0. |
| 629 | // FIXME: We might need to add CXX version at some point too? |
| 630 | auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion; |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 631 | llvm::Metadata *OCLVerElts[] = { |
| 632 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
Anastasia Stulova | f61b548 | 2019-05-30 15:18:07 +0000 | [diff] [blame] | 633 | Int32Ty, Version / 100)), |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 634 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
Anastasia Stulova | f61b548 | 2019-05-30 15:18:07 +0000 | [diff] [blame] | 635 | Int32Ty, (Version % 100) / 10))}; |
Alexey Bader | 364a116 | 2017-06-20 14:30:18 +0000 | [diff] [blame] | 636 | llvm::NamedMDNode *OCLVerMD = |
| 637 | TheModule.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 638 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 639 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 640 | } |
| 641 | |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 642 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 643 | // Make sure that this type is translated. |
| 644 | Types.UpdateCompletedType(TD); |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 645 | } |
| 646 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 647 | void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { |
| 648 | // Make sure that this type is translated. |
| 649 | Types.RefreshTypeCacheForClass(RD); |
| 650 | } |
| 651 | |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 652 | llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) { |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 653 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 654 | return nullptr; |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 655 | return TBAA->getTypeInfo(QTy); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 658 | TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) { |
Ivan A. Kosarev | 124a218 | 2018-02-20 12:33:04 +0000 | [diff] [blame] | 659 | if (!TBAA) |
| 660 | return TBAAAccessInfo(); |
| 661 | return TBAA->getAccessInfo(AccessType); |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 664 | TBAAAccessInfo |
| 665 | CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) { |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 666 | if (!TBAA) |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 667 | return TBAAAccessInfo(); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 668 | return TBAA->getVTablePtrAccessInfo(VTablePtrType); |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 671 | llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { |
| 672 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 673 | return nullptr; |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 674 | return TBAA->getTBAAStructInfo(QTy); |
| 675 | } |
| 676 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 677 | llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) { |
| 678 | if (!TBAA) |
| 679 | return nullptr; |
| 680 | return TBAA->getBaseTypeInfo(QTy); |
| 681 | } |
| 682 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 683 | llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) { |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 684 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 685 | return nullptr; |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 686 | return TBAA->getAccessTagInfo(Info); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Ivan A. Kosarev | ed141ba | 2017-10-17 09:12:13 +0000 | [diff] [blame] | 689 | TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, |
| 690 | TBAAAccessInfo TargetInfo) { |
| 691 | if (!TBAA) |
| 692 | return TBAAAccessInfo(); |
| 693 | return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo); |
| 694 | } |
| 695 | |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 696 | TBAAAccessInfo |
| 697 | CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, |
| 698 | TBAAAccessInfo InfoB) { |
| 699 | if (!TBAA) |
| 700 | return TBAAAccessInfo(); |
| 701 | return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB); |
| 702 | } |
| 703 | |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 704 | TBAAAccessInfo |
| 705 | CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, |
| 706 | TBAAAccessInfo SrcInfo) { |
| 707 | if (!TBAA) |
| 708 | return TBAAAccessInfo(); |
| 709 | return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo); |
| 710 | } |
| 711 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 712 | void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst, |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 713 | TBAAAccessInfo TBAAInfo) { |
| 714 | if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo)) |
| 715 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 718 | void CodeGenModule::DecorateInstructionWithInvariantGroup( |
| 719 | llvm::Instruction *I, const CXXRecordDecl *RD) { |
Piotr Padlewski | 34046bd | 2017-04-24 12:58:43 +0000 | [diff] [blame] | 720 | I->setMetadata(llvm::LLVMContext::MD_invariant_group, |
| 721 | llvm::MDNode::get(getLLVMContext(), {})); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 724 | void CodeGenModule::Error(SourceLocation loc, StringRef message) { |
| 725 | unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
| 726 | getDiags().Report(Context.getFullLoc(loc), diagID) << message; |
John McCall | 7ef5cb3 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 729 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 730 | /// specified stmt yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 731 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 732 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 733 | "cannot compile this %0 yet"); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 734 | std::string Msg = Type; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 735 | getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID) |
| 736 | << Msg << S->getSourceRange(); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 737 | } |
Chris Lattner | 41af818 | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 738 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 739 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 740 | /// specified decl yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 741 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 742 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 743 | "cannot compile this %0 yet"); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 744 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 745 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 746 | } |
| 747 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 748 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
| 749 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 750 | } |
| 751 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 753 | const NamedDecl *D) const { |
Rafael Espindola | 123ce97 | 2018-01-24 18:58:32 +0000 | [diff] [blame] | 754 | if (GV->hasDLLImportStorageClass()) |
| 755 | return; |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 756 | // Internal definitions always have default visibility. |
Chris Lattner | 6a0f907 | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 757 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 758 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 759 | return; |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 760 | } |
Rafael Espindola | 3f727a8 | 2018-03-14 18:14:46 +0000 | [diff] [blame] | 761 | if (!D) |
| 762 | return; |
Scott Linder | bef2663 | 2019-01-28 17:12:19 +0000 | [diff] [blame] | 763 | // Set visibility for definitions, and for declarations if requested globally |
| 764 | // or set explicitly. |
Rafael Espindola | a8fbdab | 2013-02-27 02:15:29 +0000 | [diff] [blame] | 765 | LinkageInfo LV = D->getLinkageAndVisibility(); |
Scott Linder | bef2663 | 2019-01-28 17:12:19 +0000 | [diff] [blame] | 766 | if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls || |
| 767 | !GV->isDeclarationForLinker()) |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 768 | GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 771 | static bool shouldAssumeDSOLocal(const CodeGenModule &CGM, |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 772 | llvm::GlobalValue *GV) { |
Rafael Espindola | 796d4a8 | 2018-03-02 01:29:51 +0000 | [diff] [blame] | 773 | if (GV->hasLocalLinkage()) |
| 774 | return true; |
| 775 | |
| 776 | if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()) |
| 777 | return true; |
| 778 | |
Rafael Espindola | 922f2aa | 2018-02-23 19:30:48 +0000 | [diff] [blame] | 779 | // DLLImport explicitly marks the GV as external. |
| 780 | if (GV->hasDLLImportStorageClass()) |
| 781 | return false; |
| 782 | |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 783 | const llvm::Triple &TT = CGM.getTriple(); |
Martin Storsjo | 5ff7a8e | 2018-08-29 17:26:58 +0000 | [diff] [blame] | 784 | if (TT.isWindowsGNUEnvironment()) { |
| 785 | // In MinGW, variables without DLLImport can still be automatically |
| 786 | // imported from a DLL by the linker; don't mark variables that |
| 787 | // potentially could come from another DLL as DSO local. |
| 788 | if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) && |
| 789 | !GV->isThreadLocal()) |
| 790 | return false; |
| 791 | } |
Reid Kleckner | 6bf108d | 2019-05-07 23:06:21 +0000 | [diff] [blame] | 792 | |
| 793 | // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols |
| 794 | // remain unresolved in the link, they can be resolved to zero, which is |
| 795 | // outside the current DSO. |
| 796 | if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage()) |
| 797 | return false; |
| 798 | |
Rafael Espindola | 922f2aa | 2018-02-23 19:30:48 +0000 | [diff] [blame] | 799 | // Every other GV is local on COFF. |
| 800 | // Make an exception for windows OS in the triple: Some firmware builds use |
| 801 | // *-win32-macho triples. This (accidentally?) produced windows relocations |
| 802 | // without GOT tables in older clang versions; Keep this behaviour. |
| 803 | // FIXME: even thread local variables? |
| 804 | if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) |
| 805 | return true; |
| 806 | |
| 807 | // Only handle COFF and ELF for now. |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 808 | if (!TT.isOSBinFormatELF()) |
| 809 | return false; |
| 810 | |
| 811 | // If this is not an executable, don't assume anything is local. |
| 812 | const auto &CGOpts = CGM.getCodeGenOpts(); |
| 813 | llvm::Reloc::Model RM = CGOpts.RelocationModel; |
| 814 | const auto &LOpts = CGM.getLangOpts(); |
Alexey Bataev | 6b21c4a | 2019-05-21 18:20:08 +0000 | [diff] [blame] | 815 | if (RM != llvm::Reloc::Static && !LOpts.PIE && !LOpts.OpenMPIsDevice) |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 816 | return false; |
| 817 | |
| 818 | // A definition cannot be preempted from an executable. |
| 819 | if (!GV->isDeclarationForLinker()) |
| 820 | return true; |
| 821 | |
| 822 | // Most PIC code sequences that assume that a symbol is local cannot produce a |
| 823 | // 0 if it turns out the symbol is undefined. While this is ABI and relocation |
| 824 | // depended, it seems worth it to handle it here. |
| 825 | if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage()) |
| 826 | return false; |
| 827 | |
| 828 | // PPC has no copy relocations and cannot use a plt entry as a symbol address. |
| 829 | llvm::Triple::ArchType Arch = TT.getArch(); |
| 830 | if (Arch == llvm::Triple::ppc || Arch == llvm::Triple::ppc64 || |
| 831 | Arch == llvm::Triple::ppc64le) |
| 832 | return false; |
| 833 | |
| 834 | // If we can use copy relocations we can assume it is local. |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 835 | if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV)) |
| 836 | if (!Var->isThreadLocal() && |
Rafael Espindola | 75e5736 | 2018-02-07 23:04:06 +0000 | [diff] [blame] | 837 | (RM == llvm::Reloc::Static || CGOpts.PIECopyRelocations)) |
| 838 | return true; |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 839 | |
| 840 | // If we can use a plt entry as the symbol address we can assume it |
| 841 | // is local. |
| 842 | // 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] | 843 | if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static) |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 844 | return true; |
| 845 | |
| 846 | // Otherwise don't assue it is local. |
| 847 | return false; |
| 848 | } |
| 849 | |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 850 | void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const { |
Rafael Espindola | 796d4a8 | 2018-03-02 01:29:51 +0000 | [diff] [blame] | 851 | GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV)); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 854 | void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, |
| 855 | GlobalDecl GD) const { |
| 856 | const auto *D = dyn_cast<NamedDecl>(GD.getDecl()); |
Reid Kleckner | ae9b070 | 2018-03-16 19:40:50 +0000 | [diff] [blame] | 857 | // C++ destructors have a few C++ ABI specific special cases. |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 858 | if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) { |
Reid Kleckner | ae9b070 | 2018-03-16 19:40:50 +0000 | [diff] [blame] | 859 | getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType()); |
| 860 | return; |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 861 | } |
| 862 | setDLLImportDLLExport(GV, D); |
| 863 | } |
| 864 | |
| 865 | void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, |
| 866 | const NamedDecl *D) const { |
Rafael Espindola | 3f727a8 | 2018-03-14 18:14:46 +0000 | [diff] [blame] | 867 | if (D && D->isExternallyVisible()) { |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 868 | if (D->hasAttr<DLLImportAttr>()) |
| 869 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 870 | else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker()) |
| 871 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, |
| 876 | GlobalDecl GD) const { |
| 877 | setDLLImportDLLExport(GV, GD); |
Peter Collingbourne | e08e68d | 2019-06-07 19:10:08 +0000 | [diff] [blame] | 878 | setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl())); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 881 | void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, |
| 882 | const NamedDecl *D) const { |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 883 | setDLLImportDLLExport(GV, D); |
Peter Collingbourne | e08e68d | 2019-06-07 19:10:08 +0000 | [diff] [blame] | 884 | setGVPropertiesAux(GV, D); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Peter Collingbourne | e08e68d | 2019-06-07 19:10:08 +0000 | [diff] [blame] | 887 | void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV, |
| 888 | const NamedDecl *D) const { |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 889 | setGlobalVisibility(GV, D); |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 890 | setDSOLocal(GV); |
Peter Collingbourne | e08e68d | 2019-06-07 19:10:08 +0000 | [diff] [blame] | 891 | GV->setPartition(CodeGenOpts.SymbolPartition); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 894 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) { |
| 895 | return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S) |
| 896 | .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel) |
| 897 | .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel) |
| 898 | .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel) |
| 899 | .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel); |
| 900 | } |
| 901 | |
| 902 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel( |
| 903 | CodeGenOptions::TLSModel M) { |
| 904 | switch (M) { |
| 905 | case CodeGenOptions::GeneralDynamicTLSModel: |
| 906 | return llvm::GlobalVariable::GeneralDynamicTLSModel; |
| 907 | case CodeGenOptions::LocalDynamicTLSModel: |
| 908 | return llvm::GlobalVariable::LocalDynamicTLSModel; |
| 909 | case CodeGenOptions::InitialExecTLSModel: |
| 910 | return llvm::GlobalVariable::InitialExecTLSModel; |
| 911 | case CodeGenOptions::LocalExecTLSModel: |
| 912 | return llvm::GlobalVariable::LocalExecTLSModel; |
| 913 | } |
| 914 | llvm_unreachable("Invalid TLS model!"); |
| 915 | } |
| 916 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 917 | void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const { |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 918 | assert(D.getTLSKind() && "setting TLS mode on non-TLS var!"); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 919 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 920 | llvm::GlobalValue::ThreadLocalMode TLM; |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 921 | TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel()); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 922 | |
| 923 | // Override the TLS model if it is explicitly specified. |
Aaron Ballman | c432799 | 2013-12-19 03:09:10 +0000 | [diff] [blame] | 924 | if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) { |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 925 | TLM = GetLLVMTLSModel(Attr->getModel()); |
| 926 | } |
| 927 | |
| 928 | GV->setThreadLocalMode(TLM); |
| 929 | } |
| 930 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 931 | static std::string getCPUSpecificMangling(const CodeGenModule &CGM, |
| 932 | StringRef Name) { |
| 933 | const TargetInfo &Target = CGM.getTarget(); |
| 934 | return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str(); |
| 935 | } |
| 936 | |
| 937 | static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM, |
| 938 | const CPUSpecificAttr *Attr, |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 939 | unsigned CPUIndex, |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 940 | raw_ostream &Out) { |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 941 | // cpu_specific gets the current name, dispatch gets the resolver if IFunc is |
| 942 | // supported. |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 943 | if (Attr) |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 944 | Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName()); |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 945 | else if (CGM.getTarget().supportsIFunc()) |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 946 | Out << ".resolver"; |
| 947 | } |
| 948 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 949 | static void AppendTargetMangling(const CodeGenModule &CGM, |
| 950 | const TargetAttr *Attr, raw_ostream &Out) { |
| 951 | if (Attr->isDefaultVersion()) |
| 952 | return; |
| 953 | |
| 954 | Out << '.'; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 955 | const TargetInfo &Target = CGM.getTarget(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 956 | TargetAttr::ParsedTargetAttr Info = |
| 957 | Attr->parse([&Target](StringRef LHS, StringRef RHS) { |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 958 | // Multiversioning doesn't allow "no-${feature}", so we can |
| 959 | // only have "+" prefixes here. |
| 960 | assert(LHS.startswith("+") && RHS.startswith("+") && |
| 961 | "Features should always have a prefix."); |
| 962 | return Target.multiVersionSortPriority(LHS.substr(1)) > |
| 963 | Target.multiVersionSortPriority(RHS.substr(1)); |
| 964 | }); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 965 | |
| 966 | bool IsFirst = true; |
| 967 | |
| 968 | if (!Info.Architecture.empty()) { |
| 969 | IsFirst = false; |
| 970 | Out << "arch_" << Info.Architecture; |
| 971 | } |
| 972 | |
| 973 | for (StringRef Feat : Info.Features) { |
| 974 | if (!IsFirst) |
| 975 | Out << '_'; |
| 976 | IsFirst = false; |
| 977 | Out << Feat.substr(1); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | static std::string getMangledNameImpl(const CodeGenModule &CGM, GlobalDecl GD, |
| 982 | const NamedDecl *ND, |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 983 | bool OmitMultiVersionMangling = false) { |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 984 | SmallString<256> Buffer; |
| 985 | llvm::raw_svector_ostream Out(Buffer); |
| 986 | MangleContext &MC = CGM.getCXXABI().getMangleContext(); |
| 987 | if (MC.shouldMangleDeclName(ND)) { |
| 988 | llvm::raw_svector_ostream Out(Buffer); |
| 989 | if (const auto *D = dyn_cast<CXXConstructorDecl>(ND)) |
| 990 | MC.mangleCXXCtor(D, GD.getCtorType(), Out); |
| 991 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND)) |
| 992 | MC.mangleCXXDtor(D, GD.getDtorType(), Out); |
| 993 | else |
| 994 | MC.mangleName(ND, Out); |
| 995 | } else { |
| 996 | IdentifierInfo *II = ND->getIdentifier(); |
| 997 | assert(II && "Attempt to mangle unnamed decl."); |
| 998 | const auto *FD = dyn_cast<FunctionDecl>(ND); |
| 999 | |
| 1000 | if (FD && |
| 1001 | FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) { |
| 1002 | llvm::raw_svector_ostream Out(Buffer); |
| 1003 | Out << "__regcall3__" << II->getName(); |
| 1004 | } else { |
| 1005 | Out << II->getName(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | if (const auto *FD = dyn_cast<FunctionDecl>(ND)) |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1010 | if (FD->isMultiVersion() && !OmitMultiVersionMangling) { |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 1011 | switch (FD->getMultiVersionKind()) { |
| 1012 | case MultiVersionKind::CPUDispatch: |
| 1013 | case MultiVersionKind::CPUSpecific: |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1014 | AppendCPUSpecificCPUDispatchMangling(CGM, |
| 1015 | FD->getAttr<CPUSpecificAttr>(), |
| 1016 | GD.getMultiVersionIndex(), Out); |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | case MultiVersionKind::Target: |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1019 | AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out); |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | case MultiVersionKind::None: |
| 1022 | llvm_unreachable("None multiversion type isn't valid here"); |
| 1023 | } |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 1026 | return Out.str(); |
| 1027 | } |
| 1028 | |
| 1029 | void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD, |
| 1030 | const FunctionDecl *FD) { |
| 1031 | if (!FD->isMultiVersion()) |
| 1032 | return; |
| 1033 | |
| 1034 | // Get the name of what this would be without the 'target' attribute. This |
| 1035 | // allows us to lookup the version that was emitted when this wasn't a |
| 1036 | // multiversion function. |
| 1037 | std::string NonTargetName = |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1038 | getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 1039 | GlobalDecl OtherGD; |
| 1040 | if (lookupRepresentativeDecl(NonTargetName, OtherGD)) { |
| 1041 | assert(OtherGD.getCanonicalDecl() |
| 1042 | .getDecl() |
| 1043 | ->getAsFunction() |
| 1044 | ->isMultiVersion() && |
| 1045 | "Other GD should now be a multiversioned function"); |
| 1046 | // OtherFD is the version of this function that was mangled BEFORE |
| 1047 | // becoming a MultiVersion function. It potentially needs to be updated. |
Erich Keane | 7304f0a | 2018-11-28 20:58:43 +0000 | [diff] [blame] | 1048 | const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl() |
| 1049 | .getDecl() |
| 1050 | ->getAsFunction() |
| 1051 | ->getMostRecentDecl(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 1052 | std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD); |
| 1053 | // This is so that if the initial version was already the 'default' |
| 1054 | // version, we don't try to update it. |
| 1055 | if (OtherName != NonTargetName) { |
Erich Keane | bc40c5c | 2018-01-09 01:09:12 +0000 | [diff] [blame] | 1056 | // Remove instead of erase, since others may have stored the StringRef |
| 1057 | // to this. |
| 1058 | const auto ExistingRecord = Manglings.find(NonTargetName); |
| 1059 | if (ExistingRecord != std::end(Manglings)) |
| 1060 | Manglings.remove(&(*ExistingRecord)); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 1061 | auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD)); |
| 1062 | MangledDeclNames[OtherGD.getCanonicalDecl()] = Result.first->first(); |
| 1063 | if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName)) |
| 1064 | Entry->setName(OtherName); |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1069 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
David Majnemer | d2385c0 | 2016-01-08 20:48:26 +0000 | [diff] [blame] | 1070 | GlobalDecl CanonicalGD = GD.getCanonicalDecl(); |
| 1071 | |
| 1072 | // Some ABIs don't have constructor variants. Make sure that base and |
| 1073 | // complete constructors get mangled the same. |
| 1074 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) { |
| 1075 | if (!getTarget().getCXXABI().hasConstructorVariants()) { |
| 1076 | CXXCtorType OrigCtorType = GD.getCtorType(); |
| 1077 | assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete); |
| 1078 | if (OrigCtorType == Ctor_Base) |
| 1079 | CanonicalGD = GlobalDecl(CD, Ctor_Complete); |
| 1080 | } |
| 1081 | } |
| 1082 | |
NAKAMURA Takumi | 6e4d774 | 2017-07-30 05:06:26 +0000 | [diff] [blame] | 1083 | auto FoundName = MangledDeclNames.find(CanonicalGD); |
| 1084 | if (FoundName != MangledDeclNames.end()) |
| 1085 | return FoundName->second; |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 1086 | |
Richard Smith | 6d0e97a | 2014-08-02 00:50:16 +0000 | [diff] [blame] | 1087 | // Keep the first result in the case of a mangling collision. |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 1088 | const auto *ND = cast<NamedDecl>(GD.getDecl()); |
Yaxun Liu | e739ac0 | 2019-02-27 02:02:52 +0000 | [diff] [blame] | 1089 | std::string MangledName = getMangledNameImpl(*this, GD, ND); |
| 1090 | |
Michael Liao | e40f879 | 2019-06-17 12:51:36 +0000 | [diff] [blame] | 1091 | // Adjust kernel stub mangling as we may need to be able to differentiate |
| 1092 | // them from the kernel itself (e.g., for HIP). |
Yaxun Liu | e739ac0 | 2019-02-27 02:02:52 +0000 | [diff] [blame] | 1093 | if (auto *FD = dyn_cast<FunctionDecl>(GD.getDecl())) |
Michael Liao | e40f879 | 2019-06-17 12:51:36 +0000 | [diff] [blame] | 1094 | if (!getLangOpts().CUDAIsDevice && FD->hasAttr<CUDAGlobalAttr>()) |
| 1095 | MangledName = getCUDARuntime().getDeviceStubName(MangledName); |
Yaxun Liu | e739ac0 | 2019-02-27 02:02:52 +0000 | [diff] [blame] | 1096 | |
| 1097 | auto Result = Manglings.insert(std::make_pair(MangledName, GD)); |
NAKAMURA Takumi | 6e4d774 | 2017-07-30 05:06:26 +0000 | [diff] [blame] | 1098 | return MangledDeclNames[CanonicalGD] = Result.first->first(); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 1101 | StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD, |
| 1102 | const BlockDecl *BD) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1103 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
| 1104 | const Decl *D = GD.getDecl(); |
Alp Toker | 0e64e0d | 2014-06-03 02:13:57 +0000 | [diff] [blame] | 1105 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 1106 | SmallString<256> Buffer; |
| 1107 | llvm::raw_svector_ostream Out(Buffer); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1108 | if (!D) |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 1109 | MangleCtx.mangleGlobalBlock(BD, |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 1110 | dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1111 | else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1112 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1113 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1114 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 1115 | else |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 1116 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
Alp Toker | 0e64e0d | 2014-06-03 02:13:57 +0000 | [diff] [blame] | 1117 | |
Richard Smith | 6d0e97a | 2014-08-02 00:50:16 +0000 | [diff] [blame] | 1118 | auto Result = Manglings.insert(std::make_pair(Out.str(), BD)); |
| 1119 | return Result.first->first(); |
Anders Carlsson | 635186a | 2010-06-09 02:36:32 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1122 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1123 | return getModule().getNamedValue(Name); |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1126 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 1127 | /// main() runs. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1128 | void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority, |
| 1129 | llvm::Constant *AssociatedData) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1130 | // FIXME: Type coercion of void()* types. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1131 | GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1134 | /// AddGlobalDtor - Add a function to the list that will be called |
| 1135 | /// when the module is unloaded. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1136 | void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) { |
Akira Hatanaka | 617e261 | 2018-04-17 18:41:52 +0000 | [diff] [blame] | 1137 | if (CodeGenOpts.RegisterGlobalDtorsWithAtExit) { |
| 1138 | DtorsUsingAtExit[Priority].push_back(Dtor); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1142 | // FIXME: Type coercion of void()* types. |
Craig Topper | 4b56692 | 2014-06-09 02:04:02 +0000 | [diff] [blame] | 1143 | GlobalDtors.push_back(Structor(Priority, Dtor, nullptr)); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Vassil Vassilev | 188ad3a | 2016-10-27 09:12:20 +0000 | [diff] [blame] | 1146 | void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1147 | if (Fns.empty()) return; |
| 1148 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1149 | // Ctor function type is void()*. |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1150 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
Dylan McKay | 21e7f5e | 2018-11-09 17:15:06 +0000 | [diff] [blame] | 1151 | llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy, |
| 1152 | TheModule.getDataLayout().getProgramAddressSpace()); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1153 | |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 1154 | // Get the type of a ctor entry, { i32, void ()*, i8* }. |
| 1155 | llvm::StructType *CtorStructTy = llvm::StructType::get( |
Dylan McKay | 21e7f5e | 2018-11-09 17:15:06 +0000 | [diff] [blame] | 1156 | Int32Ty, CtorPFTy, VoidPtrTy); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1157 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1158 | // Construct the constructor and destructor arrays. |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 1159 | ConstantInitBuilder builder(*this); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1160 | auto ctors = builder.beginArray(CtorStructTy); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 1161 | for (const auto &I : Fns) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1162 | auto ctor = ctors.beginStruct(CtorStructTy); |
| 1163 | ctor.addInt(Int32Ty, I.Priority); |
| 1164 | ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy)); |
| 1165 | if (I.AssociatedData) |
| 1166 | ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy)); |
| 1167 | else |
| 1168 | ctor.addNullPointer(VoidPtrTy); |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 1169 | ctor.finishAndAddTo(ctors); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1170 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1171 | |
John McCall | 18081af | 2016-11-19 20:12:25 +0000 | [diff] [blame] | 1172 | auto list = |
| 1173 | ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(), |
| 1174 | /*constant*/ false, |
| 1175 | llvm::GlobalValue::AppendingLinkage); |
| 1176 | |
| 1177 | // The LTO linker doesn't seem to like it when we set an alignment |
| 1178 | // on appending variables. Take it off as a workaround. |
| 1179 | list->setAlignment(0); |
| 1180 | |
Vassil Vassilev | 188ad3a | 2016-10-27 09:12:20 +0000 | [diff] [blame] | 1181 | Fns.clear(); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 1184 | llvm::GlobalValue::LinkageTypes |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1185 | CodeGenModule::getFunctionLinkage(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1186 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1187 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 1188 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1189 | |
Reid Kleckner | ae9b070 | 2018-03-16 19:40:50 +0000 | [diff] [blame] | 1190 | if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D)) |
| 1191 | return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType()); |
Reid Kleckner | a283e48 | 2017-12-21 19:44:23 +0000 | [diff] [blame] | 1192 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 1193 | if (isa<CXXConstructorDecl>(D) && |
| 1194 | cast<CXXConstructorDecl>(D)->isInheritingConstructor() && |
| 1195 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 1196 | // Our approach to inheriting constructors is fundamentally different from |
| 1197 | // that used by the MS ABI, so keep our inheriting constructor thunks |
| 1198 | // internal rather than trying to pick an unambiguous mangling for them. |
| 1199 | return llvm::GlobalValue::InternalLinkage; |
| 1200 | } |
| 1201 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 1202 | return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 1203 | } |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 1204 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1205 | llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1206 | llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD); |
| 1207 | if (!MDS) return nullptr; |
| 1208 | |
Benjamin Kramer | 630cf8c | 2016-11-23 11:20:27 +0000 | [diff] [blame] | 1209 | return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString())); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1212 | void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | const CGFunctionInfo &Info, |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 1214 | llvm::Function *F) { |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1215 | unsigned CallingConv; |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 1216 | llvm::AttributeList PAL; |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1217 | ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv, false); |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 1218 | F->setAttributes(PAL); |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1219 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Anastasia Stulova | e6cf6c7 | 2019-05-09 13:55:44 +0000 | [diff] [blame] | 1222 | static void removeImageAccessQualifier(std::string& TyName) { |
| 1223 | std::string ReadOnlyQual("__read_only"); |
| 1224 | std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual); |
| 1225 | if (ReadOnlyPos != std::string::npos) |
| 1226 | // "+ 1" for the space after access qualifier. |
| 1227 | TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1); |
| 1228 | else { |
| 1229 | std::string WriteOnlyQual("__write_only"); |
| 1230 | std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual); |
| 1231 | if (WriteOnlyPos != std::string::npos) |
| 1232 | TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1); |
| 1233 | else { |
| 1234 | std::string ReadWriteQual("__read_write"); |
| 1235 | std::string::size_type ReadWritePos = TyName.find(ReadWriteQual); |
| 1236 | if (ReadWritePos != std::string::npos) |
| 1237 | TyName.erase(ReadWritePos, ReadWriteQual.size() + 1); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | // Returns the address space id that should be produced to the |
| 1243 | // kernel_arg_addr_space metadata. This is always fixed to the ids |
| 1244 | // as specified in the SPIR 2.0 specification in order to differentiate |
| 1245 | // for example in clGetKernelArgInfo() implementation between the address |
| 1246 | // spaces with targets without unique mapping to the OpenCL address spaces |
| 1247 | // (basically all single AS CPUs). |
| 1248 | static unsigned ArgInfoAddressSpace(LangAS AS) { |
| 1249 | switch (AS) { |
| 1250 | case LangAS::opencl_global: return 1; |
| 1251 | case LangAS::opencl_constant: return 2; |
| 1252 | case LangAS::opencl_local: return 3; |
| 1253 | case LangAS::opencl_generic: return 4; // Not in SPIR 2.0 specs. |
| 1254 | default: |
| 1255 | return 0; // Assume private. |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn, |
| 1260 | const FunctionDecl *FD, |
| 1261 | CodeGenFunction *CGF) { |
| 1262 | assert(((FD && CGF) || (!FD && !CGF)) && |
| 1263 | "Incorrect use - FD and CGF should either be both null or not!"); |
| 1264 | // Create MDNodes that represent the kernel arg metadata. |
| 1265 | // Each MDNode is a list in the form of "key", N number of values which is |
| 1266 | // the same number of values as their are kernel arguments. |
| 1267 | |
| 1268 | const PrintingPolicy &Policy = Context.getPrintingPolicy(); |
| 1269 | |
| 1270 | // MDNode for the kernel argument address space qualifiers. |
| 1271 | SmallVector<llvm::Metadata *, 8> addressQuals; |
| 1272 | |
| 1273 | // MDNode for the kernel argument access qualifiers (images only). |
| 1274 | SmallVector<llvm::Metadata *, 8> accessQuals; |
| 1275 | |
| 1276 | // MDNode for the kernel argument type names. |
| 1277 | SmallVector<llvm::Metadata *, 8> argTypeNames; |
| 1278 | |
| 1279 | // MDNode for the kernel argument base type names. |
| 1280 | SmallVector<llvm::Metadata *, 8> argBaseTypeNames; |
| 1281 | |
| 1282 | // MDNode for the kernel argument type qualifiers. |
| 1283 | SmallVector<llvm::Metadata *, 8> argTypeQuals; |
| 1284 | |
| 1285 | // MDNode for the kernel argument names. |
| 1286 | SmallVector<llvm::Metadata *, 8> argNames; |
| 1287 | |
| 1288 | if (FD && CGF) |
| 1289 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 1290 | const ParmVarDecl *parm = FD->getParamDecl(i); |
| 1291 | QualType ty = parm->getType(); |
| 1292 | std::string typeQuals; |
| 1293 | |
| 1294 | if (ty->isPointerType()) { |
| 1295 | QualType pointeeTy = ty->getPointeeType(); |
| 1296 | |
| 1297 | // Get address qualifier. |
| 1298 | addressQuals.push_back( |
| 1299 | llvm::ConstantAsMetadata::get(CGF->Builder.getInt32( |
| 1300 | ArgInfoAddressSpace(pointeeTy.getAddressSpace())))); |
| 1301 | |
| 1302 | // Get argument type name. |
| 1303 | std::string typeName = |
| 1304 | pointeeTy.getUnqualifiedType().getAsString(Policy) + "*"; |
| 1305 | |
| 1306 | // Turn "unsigned type" to "utype" |
| 1307 | std::string::size_type pos = typeName.find("unsigned"); |
| 1308 | if (pointeeTy.isCanonical() && pos != std::string::npos) |
| 1309 | typeName.erase(pos + 1, 8); |
| 1310 | |
| 1311 | argTypeNames.push_back(llvm::MDString::get(VMContext, typeName)); |
| 1312 | |
| 1313 | std::string baseTypeName = |
| 1314 | pointeeTy.getUnqualifiedType().getCanonicalType().getAsString( |
| 1315 | Policy) + |
| 1316 | "*"; |
| 1317 | |
| 1318 | // Turn "unsigned type" to "utype" |
| 1319 | pos = baseTypeName.find("unsigned"); |
| 1320 | if (pos != std::string::npos) |
| 1321 | baseTypeName.erase(pos + 1, 8); |
| 1322 | |
| 1323 | argBaseTypeNames.push_back( |
| 1324 | llvm::MDString::get(VMContext, baseTypeName)); |
| 1325 | |
| 1326 | // Get argument type qualifiers: |
| 1327 | if (ty.isRestrictQualified()) |
| 1328 | typeQuals = "restrict"; |
| 1329 | if (pointeeTy.isConstQualified() || |
| 1330 | (pointeeTy.getAddressSpace() == LangAS::opencl_constant)) |
| 1331 | typeQuals += typeQuals.empty() ? "const" : " const"; |
| 1332 | if (pointeeTy.isVolatileQualified()) |
| 1333 | typeQuals += typeQuals.empty() ? "volatile" : " volatile"; |
| 1334 | } else { |
| 1335 | uint32_t AddrSpc = 0; |
| 1336 | bool isPipe = ty->isPipeType(); |
| 1337 | if (ty->isImageType() || isPipe) |
| 1338 | AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global); |
| 1339 | |
| 1340 | addressQuals.push_back( |
| 1341 | llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc))); |
| 1342 | |
| 1343 | // Get argument type name. |
| 1344 | std::string typeName; |
| 1345 | if (isPipe) |
| 1346 | typeName = ty.getCanonicalType() |
| 1347 | ->getAs<PipeType>() |
| 1348 | ->getElementType() |
| 1349 | .getAsString(Policy); |
| 1350 | else |
| 1351 | typeName = ty.getUnqualifiedType().getAsString(Policy); |
| 1352 | |
| 1353 | // Turn "unsigned type" to "utype" |
| 1354 | std::string::size_type pos = typeName.find("unsigned"); |
| 1355 | if (ty.isCanonical() && pos != std::string::npos) |
| 1356 | typeName.erase(pos + 1, 8); |
| 1357 | |
| 1358 | std::string baseTypeName; |
| 1359 | if (isPipe) |
| 1360 | baseTypeName = ty.getCanonicalType() |
| 1361 | ->getAs<PipeType>() |
| 1362 | ->getElementType() |
| 1363 | .getCanonicalType() |
| 1364 | .getAsString(Policy); |
| 1365 | else |
| 1366 | baseTypeName = |
| 1367 | ty.getUnqualifiedType().getCanonicalType().getAsString(Policy); |
| 1368 | |
| 1369 | // Remove access qualifiers on images |
| 1370 | // (as they are inseparable from type in clang implementation, |
| 1371 | // but OpenCL spec provides a special query to get access qualifier |
| 1372 | // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER): |
| 1373 | if (ty->isImageType()) { |
| 1374 | removeImageAccessQualifier(typeName); |
| 1375 | removeImageAccessQualifier(baseTypeName); |
| 1376 | } |
| 1377 | |
| 1378 | argTypeNames.push_back(llvm::MDString::get(VMContext, typeName)); |
| 1379 | |
| 1380 | // Turn "unsigned type" to "utype" |
| 1381 | pos = baseTypeName.find("unsigned"); |
| 1382 | if (pos != std::string::npos) |
| 1383 | baseTypeName.erase(pos + 1, 8); |
| 1384 | |
| 1385 | argBaseTypeNames.push_back( |
| 1386 | llvm::MDString::get(VMContext, baseTypeName)); |
| 1387 | |
| 1388 | if (isPipe) |
| 1389 | typeQuals = "pipe"; |
| 1390 | } |
| 1391 | |
| 1392 | argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals)); |
| 1393 | |
| 1394 | // Get image and pipe access qualifier: |
| 1395 | if (ty->isImageType() || ty->isPipeType()) { |
| 1396 | const Decl *PDecl = parm; |
| 1397 | if (auto *TD = dyn_cast<TypedefType>(ty)) |
| 1398 | PDecl = TD->getDecl(); |
| 1399 | const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>(); |
| 1400 | if (A && A->isWriteOnly()) |
| 1401 | accessQuals.push_back(llvm::MDString::get(VMContext, "write_only")); |
| 1402 | else if (A && A->isReadWrite()) |
| 1403 | accessQuals.push_back(llvm::MDString::get(VMContext, "read_write")); |
| 1404 | else |
| 1405 | accessQuals.push_back(llvm::MDString::get(VMContext, "read_only")); |
| 1406 | } else |
| 1407 | accessQuals.push_back(llvm::MDString::get(VMContext, "none")); |
| 1408 | |
| 1409 | // Get argument name. |
| 1410 | argNames.push_back(llvm::MDString::get(VMContext, parm->getName())); |
| 1411 | } |
| 1412 | |
| 1413 | Fn->setMetadata("kernel_arg_addr_space", |
| 1414 | llvm::MDNode::get(VMContext, addressQuals)); |
| 1415 | Fn->setMetadata("kernel_arg_access_qual", |
| 1416 | llvm::MDNode::get(VMContext, accessQuals)); |
| 1417 | Fn->setMetadata("kernel_arg_type", |
| 1418 | llvm::MDNode::get(VMContext, argTypeNames)); |
| 1419 | Fn->setMetadata("kernel_arg_base_type", |
| 1420 | llvm::MDNode::get(VMContext, argBaseTypeNames)); |
| 1421 | Fn->setMetadata("kernel_arg_type_qual", |
| 1422 | llvm::MDNode::get(VMContext, argTypeQuals)); |
| 1423 | if (getCodeGenOpts().EmitOpenCLArgMetadata) |
| 1424 | Fn->setMetadata("kernel_arg_name", |
| 1425 | llvm::MDNode::get(VMContext, argNames)); |
| 1426 | } |
| 1427 | |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1428 | /// Determines whether the language options require us to model |
| 1429 | /// unwind exceptions. We treat -fexceptions as mandating this |
| 1430 | /// except under the fragile ObjC ABI with only ObjC exceptions |
| 1431 | /// enabled. This means, for example, that C with -fexceptions |
| 1432 | /// enables this. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1433 | static bool hasUnwindExceptions(const LangOptions &LangOpts) { |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1434 | // If exceptions are completely disabled, obviously this is false. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1435 | if (!LangOpts.Exceptions) return false; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1436 | |
| 1437 | // If C++ exceptions are enabled, this is true. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1438 | if (LangOpts.CXXExceptions) return true; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1439 | |
| 1440 | // If ObjC exceptions are enabled, this depends on the ABI. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1441 | if (LangOpts.ObjCExceptions) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 1442 | return LangOpts.ObjCRuntime.hasUnwindExceptions(); |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 1448 | static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM, |
| 1449 | const CXXMethodDecl *MD) { |
| 1450 | // Check that the type metadata can ever actually be used by a call. |
| 1451 | if (!CGM.getCodeGenOpts().LTOUnit || |
| 1452 | !CGM.HasHiddenLTOVisibility(MD->getParent())) |
| 1453 | return false; |
| 1454 | |
| 1455 | // Only functions whose address can be taken with a member function pointer |
| 1456 | // need this sort of type metadata. |
| 1457 | return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) && |
| 1458 | !isa<CXXDestructorDecl>(MD); |
| 1459 | } |
| 1460 | |
| 1461 | std::vector<const CXXRecordDecl *> |
| 1462 | CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) { |
| 1463 | llvm::SetVector<const CXXRecordDecl *> MostBases; |
| 1464 | |
| 1465 | std::function<void (const CXXRecordDecl *)> CollectMostBases; |
| 1466 | CollectMostBases = [&](const CXXRecordDecl *RD) { |
| 1467 | if (RD->getNumBases() == 0) |
| 1468 | MostBases.insert(RD); |
| 1469 | for (const CXXBaseSpecifier &B : RD->bases()) |
| 1470 | CollectMostBases(B.getType()->getAsCXXRecordDecl()); |
| 1471 | }; |
| 1472 | CollectMostBases(RD); |
| 1473 | return MostBases.takeVector(); |
| 1474 | } |
| 1475 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1476 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 1477 | llvm::Function *F) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1478 | llvm::AttrBuilder B; |
| 1479 | |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 1480 | if (CodeGenOpts.UnwindTables) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1481 | B.addAttribute(llvm::Attribute::UWTable); |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 1482 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1483 | if (!hasUnwindExceptions(LangOpts)) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1484 | B.addAttribute(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1485 | |
Manoj Gupta | 4fbf84c | 2018-05-09 21:41:18 +0000 | [diff] [blame] | 1486 | if (!D || !D->hasAttr<NoStackProtectorAttr>()) { |
| 1487 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
| 1488 | B.addAttribute(llvm::Attribute::StackProtect); |
| 1489 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
| 1490 | B.addAttribute(llvm::Attribute::StackProtectStrong); |
| 1491 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
| 1492 | B.addAttribute(llvm::Attribute::StackProtectReq); |
| 1493 | } |
Akira Hatanaka | 200500d | 2015-10-08 19:30:57 +0000 | [diff] [blame] | 1494 | |
| 1495 | if (!D) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1496 | // If we don't have a declaration to control inlining, the function isn't |
| 1497 | // explicitly marked as alwaysinline for semantic reasons, and inlining is |
| 1498 | // disabled, mark the function as noinline. |
| 1499 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
| 1500 | CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) |
| 1501 | B.addAttribute(llvm::Attribute::NoInline); |
| 1502 | |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1503 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Akira Hatanaka | 200500d | 2015-10-08 19:30:57 +0000 | [diff] [blame] | 1504 | return; |
| 1505 | } |
| 1506 | |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1507 | // Track whether we need to add the optnone LLVM attribute, |
| 1508 | // starting with the default for this optimization level. |
| 1509 | bool ShouldAddOptNone = |
| 1510 | !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0; |
| 1511 | // We can't add optnone in the following cases, it won't pass the verifier. |
| 1512 | ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>(); |
| 1513 | ShouldAddOptNone &= !F->hasFnAttribute(llvm::Attribute::AlwaysInline); |
| 1514 | ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>(); |
| 1515 | |
| 1516 | if (ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1517 | B.addAttribute(llvm::Attribute::OptimizeNone); |
| 1518 | |
| 1519 | // OptimizeNone implies noinline; we should not be inlining such functions. |
| 1520 | B.addAttribute(llvm::Attribute::NoInline); |
| 1521 | assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && |
| 1522 | "OptimizeNone and AlwaysInline on same function!"); |
| 1523 | |
| 1524 | // We still need to handle naked functions even though optnone subsumes |
| 1525 | // much of their semantics. |
| 1526 | if (D->hasAttr<NakedAttr>()) |
| 1527 | B.addAttribute(llvm::Attribute::Naked); |
| 1528 | |
| 1529 | // OptimizeNone wins over OptimizeForSize and MinSize. |
| 1530 | F->removeFnAttr(llvm::Attribute::OptimizeForSize); |
| 1531 | F->removeFnAttr(llvm::Attribute::MinSize); |
| 1532 | } else if (D->hasAttr<NakedAttr>()) { |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 1533 | // Naked implies noinline: we should not be inlining such functions. |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1534 | B.addAttribute(llvm::Attribute::Naked); |
| 1535 | B.addAttribute(llvm::Attribute::NoInline); |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 1536 | } else if (D->hasAttr<NoDuplicateAttr>()) { |
| 1537 | B.addAttribute(llvm::Attribute::NoDuplicate); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1538 | } else if (D->hasAttr<NoInlineAttr>()) { |
| 1539 | B.addAttribute(llvm::Attribute::NoInline); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 1540 | } else if (D->hasAttr<AlwaysInlineAttr>() && |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1541 | !F->hasFnAttribute(llvm::Attribute::NoInline)) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1542 | // (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] | 1543 | B.addAttribute(llvm::Attribute::AlwaysInline); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1544 | } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) { |
| 1545 | // If we're not inlining, then force everything that isn't always_inline to |
| 1546 | // carry an explicit noinline attribute. |
| 1547 | if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline)) |
| 1548 | B.addAttribute(llvm::Attribute::NoInline); |
| 1549 | } else { |
| 1550 | // Otherwise, propagate the inline hint attribute and potentially use its |
| 1551 | // absence to mark things as noinline. |
| 1552 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
Richard Trieu | 9b36a9c | 2018-10-23 01:26:28 +0000 | [diff] [blame] | 1553 | // Search function and template pattern redeclarations for inline. |
| 1554 | auto CheckForInline = [](const FunctionDecl *FD) { |
| 1555 | auto CheckRedeclForInline = [](const FunctionDecl *Redecl) { |
| 1556 | return Redecl->isInlineSpecified(); |
| 1557 | }; |
| 1558 | if (any_of(FD->redecls(), CheckRedeclForInline)) |
| 1559 | return true; |
| 1560 | const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern(); |
| 1561 | if (!Pattern) |
| 1562 | return false; |
| 1563 | return any_of(Pattern->redecls(), CheckRedeclForInline); |
| 1564 | }; |
| 1565 | if (CheckForInline(FD)) { |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1566 | B.addAttribute(llvm::Attribute::InlineHint); |
| 1567 | } else if (CodeGenOpts.getInlining() == |
| 1568 | CodeGenOptions::OnlyHintInlining && |
| 1569 | !FD->isInlined() && |
| 1570 | !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { |
| 1571 | B.addAttribute(llvm::Attribute::NoInline); |
| 1572 | } |
| 1573 | } |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 1574 | } |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1575 | |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1576 | // Add other optimization related attributes if we are optimizing this |
| 1577 | // function. |
| 1578 | if (!D->hasAttr<OptimizeNoneAttr>()) { |
| 1579 | if (D->hasAttr<ColdAttr>()) { |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1580 | if (!ShouldAddOptNone) |
| 1581 | B.addAttribute(llvm::Attribute::OptimizeForSize); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1582 | B.addAttribute(llvm::Attribute::Cold); |
| 1583 | } |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1584 | |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1585 | if (D->hasAttr<MinSizeAttr>()) |
| 1586 | B.addAttribute(llvm::Attribute::MinSize); |
| 1587 | } |
Richard Smith | b555a76 | 2012-09-28 22:46:07 +0000 | [diff] [blame] | 1588 | |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1589 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 1590 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1591 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
| 1592 | if (alignment) |
| 1593 | F->setAlignment(alignment); |
| 1594 | |
Saleem Abdulrasool | 3fe5b7a | 2018-04-19 23:14:57 +0000 | [diff] [blame] | 1595 | if (!D->hasAttr<AlignedAttr>()) |
| 1596 | if (LangOpts.FunctionAlignment) |
| 1597 | F->setAlignment(1 << LangOpts.FunctionAlignment); |
| 1598 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 1599 | // Some C++ ABIs require 2-byte alignment for member functions, in order to |
| 1600 | // reserve a bit for differentiating between virtual and non-virtual member |
| 1601 | // functions. If the current target's C++ ABI requires this and this is a |
| 1602 | // member function, set its alignment accordingly. |
| 1603 | if (getTarget().getCXXABI().areMemberFunctionsAligned()) { |
| 1604 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 1605 | F->setAlignment(2); |
| 1606 | } |
Evgeniy Stepanov | f754309 | 2016-10-31 22:28:10 +0000 | [diff] [blame] | 1607 | |
| 1608 | // In the cross-dso CFI mode, we want !type attributes on definitions only. |
| 1609 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 1610 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 1611 | CreateFunctionTypeMetadataForIcall(FD, F); |
| 1612 | |
| 1613 | // Emit type metadata on member functions for member function pointer checks. |
| 1614 | // These are only ever necessary on definitions; we're guaranteed that the |
| 1615 | // definition will be present in the LTO unit as a result of LTO visibility. |
| 1616 | auto *MD = dyn_cast<CXXMethodDecl>(D); |
| 1617 | if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) { |
| 1618 | for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) { |
| 1619 | llvm::Metadata *Id = |
| 1620 | CreateMetadataIdentifierForType(Context.getMemberPointerType( |
| 1621 | MD->getType(), Context.getRecordType(Base).getTypePtr())); |
| 1622 | F->addTypeMetadata(0, Id); |
| 1623 | } |
| 1624 | } |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Rafael Espindola | 75c649c | 2018-03-01 00:06:55 +0000 | [diff] [blame] | 1627 | void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) { |
| 1628 | const Decl *D = GD.getDecl(); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 1629 | if (dyn_cast_or_null<NamedDecl>(D)) |
| 1630 | setGVProperties(GV, GD); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1631 | else |
| 1632 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1633 | |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1634 | if (D && D->hasAttr<UsedAttr>()) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1635 | addUsedGlobal(GV); |
Elizabeth Andrews | 6593df2 | 2018-08-22 19:05:19 +0000 | [diff] [blame] | 1636 | |
| 1637 | if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) { |
| 1638 | const auto *VD = cast<VarDecl>(D); |
Erich Keane | 85822b3 | 2018-10-25 19:13:46 +0000 | [diff] [blame] | 1639 | if (VD->getType().isConstQualified() && |
| 1640 | VD->getStorageDuration() == SD_Static) |
Elizabeth Andrews | 6593df2 | 2018-08-22 19:05:19 +0000 | [diff] [blame] | 1641 | addUsedGlobal(GV); |
| 1642 | } |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1645 | bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1646 | llvm::AttrBuilder &Attrs) { |
| 1647 | // Add target-cpu and target-features attributes to functions. If |
| 1648 | // we have a decl for the function and it has a target attribute then |
| 1649 | // parse that and add it to the feature set. |
| 1650 | StringRef TargetCPU = getTarget().getTargetOpts().CPU; |
| 1651 | std::vector<std::string> Features; |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1652 | const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl()); |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1653 | FD = FD ? FD->getMostRecentDecl() : FD; |
| 1654 | const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1655 | const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr; |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1656 | bool AddedAttr = false; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1657 | if (TD || SD) { |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1658 | llvm::StringMap<bool> FeatureMap; |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1659 | getFunctionFeatureMap(FeatureMap, GD); |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1660 | |
| 1661 | // Produce the canonical string for this set of features. |
| 1662 | for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap) |
| 1663 | Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str()); |
| 1664 | |
| 1665 | // Now add the target-cpu and target-features to the function. |
| 1666 | // While we populated the feature map above, we still need to |
| 1667 | // get and parse the target attribute so we can get the cpu for |
| 1668 | // the function. |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1669 | if (TD) { |
| 1670 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
| 1671 | if (ParsedAttr.Architecture != "" && |
| 1672 | getTarget().isValidCPUName(ParsedAttr.Architecture)) |
| 1673 | TargetCPU = ParsedAttr.Architecture; |
| 1674 | } |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1675 | } else { |
| 1676 | // Otherwise just add the existing target cpu and target features to the |
| 1677 | // function. |
| 1678 | Features = getTarget().getTargetOpts().Features; |
| 1679 | } |
| 1680 | |
| 1681 | if (TargetCPU != "") { |
| 1682 | Attrs.addAttribute("target-cpu", TargetCPU); |
| 1683 | AddedAttr = true; |
| 1684 | } |
| 1685 | if (!Features.empty()) { |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 1686 | llvm::sort(Features); |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1687 | Attrs.addAttribute("target-features", llvm::join(Features, ",")); |
| 1688 | AddedAttr = true; |
| 1689 | } |
| 1690 | |
| 1691 | return AddedAttr; |
| 1692 | } |
| 1693 | |
Rafael Espindola | 285271c | 2018-02-28 23:54:23 +0000 | [diff] [blame] | 1694 | void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, |
Rafael Espindola | ee076a2 | 2014-05-13 18:45:53 +0000 | [diff] [blame] | 1695 | llvm::GlobalObject *GO) { |
Rafael Espindola | 285271c | 2018-02-28 23:54:23 +0000 | [diff] [blame] | 1696 | const Decl *D = GD.getDecl(); |
Rafael Espindola | 75c649c | 2018-03-01 00:06:55 +0000 | [diff] [blame] | 1697 | SetCommonAttributes(GD, GO); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1698 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1699 | if (D) { |
| 1700 | if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) { |
| 1701 | if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>()) |
| 1702 | GV->addAttribute("bss-section", SA->getName()); |
| 1703 | if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>()) |
| 1704 | GV->addAttribute("data-section", SA->getName()); |
| 1705 | if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>()) |
| 1706 | GV->addAttribute("rodata-section", SA->getName()); |
| 1707 | } |
| 1708 | |
| 1709 | if (auto *F = dyn_cast<llvm::Function>(GO)) { |
| 1710 | if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>()) |
Erich Keane | 30994d2 | 2018-02-08 20:04:22 +0000 | [diff] [blame] | 1711 | if (!D->getAttr<SectionAttr>()) |
| 1712 | F->addFnAttr("implicit-section-name", SA->getName()); |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1713 | |
| 1714 | llvm::AttrBuilder Attrs; |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1715 | if (GetCPUAndFeaturesAttributes(GD, Attrs)) { |
Erich Keane | 93e5866 | 2018-02-12 17:01:41 +0000 | [diff] [blame] | 1716 | // We know that GetCPUAndFeaturesAttributes will always have the |
| 1717 | // newest set, since it has the newest possible FunctionDecl, so the |
| 1718 | // new ones should replace the old. |
| 1719 | F->removeFnAttr("target-cpu"); |
| 1720 | F->removeFnAttr("target-features"); |
| 1721 | F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs); |
| 1722 | } |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1723 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1724 | |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 1725 | if (const auto *CSA = D->getAttr<CodeSegAttr>()) |
| 1726 | GO->setSection(CSA->getName()); |
| 1727 | else if (const auto *SA = D->getAttr<SectionAttr>()) |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1728 | GO->setSection(SA->getName()); |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 1729 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1730 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1731 | getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1734 | void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD, |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 1735 | llvm::Function *F, |
| 1736 | const CGFunctionInfo &FI) { |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1737 | const Decl *D = GD.getDecl(); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1738 | SetLLVMFunctionAttributes(GD, FI, F); |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 1739 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1740 | |
| 1741 | F->setLinkage(llvm::Function::InternalLinkage); |
| 1742 | |
Rafael Espindola | 285271c | 2018-02-28 23:54:23 +0000 | [diff] [blame] | 1743 | setNonAliasAttributes(GD, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 1746 | static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) { |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1747 | // Set linkage and visibility in case we never see a definition. |
| 1748 | LinkageInfo LV = ND->getLinkageAndVisibility(); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 1749 | // Don't set internal linkage on declarations. |
| 1750 | // "extern_weak" is overloaded in LLVM; we probably should have |
| 1751 | // separate linkage types for this. |
| 1752 | if (isExternallyVisible(LV.getLinkage()) && |
| 1753 | (ND->hasAttr<WeakAttr>() || ND->isWeakImported())) |
| 1754 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 1757 | void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD, |
| 1758 | llvm::Function *F) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1759 | // Only if we are checking indirect calls. |
| 1760 | if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall)) |
| 1761 | return; |
| 1762 | |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 1763 | // Non-static class methods are handled via vtable or member function pointer |
| 1764 | // checks elsewhere. |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1765 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 1766 | return; |
| 1767 | |
| 1768 | // Additionally, if building with cross-DSO support... |
| 1769 | if (CodeGenOpts.SanitizeCfiCrossDso) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1770 | // Skip available_externally functions. They won't be codegen'ed in the |
| 1771 | // current module anyway. |
| 1772 | if (getContext().GetGVALinkageForFunction(FD) == GVA_AvailableExternally) |
| 1773 | return; |
| 1774 | } |
| 1775 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1776 | llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1777 | F->addTypeMetadata(0, MD); |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 1778 | F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType())); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Emit a hash-based bit set entry for cross-DSO calls. |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 1781 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 1782 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
| 1783 | F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 1786 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, |
| 1787 | bool IsIncompleteFunction, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1788 | bool IsThunk) { |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1789 | |
Pete Cooper | 2f1637a | 2015-05-20 17:17:45 +0000 | [diff] [blame] | 1790 | if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) { |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 1791 | // If this is an intrinsic function, set the function's attributes |
| 1792 | // to the intrinsic's attributes. |
Pete Cooper | 2f1637a | 2015-05-20 17:17:45 +0000 | [diff] [blame] | 1793 | F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID)); |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1797 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 1798 | |
Scott Linder | fb59fef | 2019-04-23 21:50:11 +0000 | [diff] [blame] | 1799 | if (!IsIncompleteFunction) |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1800 | SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Bob Wilson | fc6297f | 2014-04-01 01:38:16 +0000 | [diff] [blame] | 1802 | // Add the Returned attribute for "this", except for iOS 5 and earlier |
| 1803 | // where substantial code, including the libstdc++ dylib, was compiled with |
| 1804 | // GCC and does not actually return "this". |
David Majnemer | b9bd6fb | 2014-11-01 05:42:23 +0000 | [diff] [blame] | 1805 | if (!IsThunk && getCXXABI().HasThisReturn(GD) && |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 1806 | !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1807 | assert(!F->arg_empty() && |
| 1808 | F->arg_begin()->getType() |
| 1809 | ->canLosslesslyBitCastTo(F->getReturnType()) && |
| 1810 | "unexpected this return"); |
| 1811 | F->addAttribute(1, llvm::Attribute::Returned); |
| 1812 | } |
| 1813 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1814 | // Only a few attributes are set on declarations; these may later be |
| 1815 | // overridden by a definition. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Jake Ehrlich | ba874ad | 2017-11-29 00:54:20 +0000 | [diff] [blame] | 1817 | setLinkageForGV(F, FD); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 1818 | setGVProperties(F, FD); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1819 | |
Scott Linder | fb59fef | 2019-04-23 21:50:11 +0000 | [diff] [blame] | 1820 | // Setup target-specific attributes. |
| 1821 | if (!IsIncompleteFunction && F->isDeclaration()) |
| 1822 | getTargetCodeGenInfo().setTargetAttributes(FD, F, *this); |
| 1823 | |
Erich Keane | 7963e8b | 2018-07-18 20:04:48 +0000 | [diff] [blame] | 1824 | if (const auto *CSA = FD->getAttr<CodeSegAttr>()) |
| 1825 | F->setSection(CSA->getName()); |
| 1826 | else if (const auto *SA = FD->getAttr<SectionAttr>()) |
| 1827 | F->setSection(SA->getName()); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1828 | |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1829 | if (FD->isReplaceableGlobalAllocationFunction()) { |
| 1830 | // A replaceable global allocation function does not act like a builtin by |
| 1831 | // 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] | 1832 | F->addAttribute(llvm::AttributeList::FunctionIndex, |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 1833 | llvm::Attribute::NoBuiltin); |
Peter Collingbourne | 2c7f7e3 | 2015-09-10 02:17:40 +0000 | [diff] [blame] | 1834 | |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1835 | // A sane operator new returns a non-aliasing pointer. |
| 1836 | // FIXME: Also add NonNull attribute to the return value |
| 1837 | // for the non-nothrow forms? |
| 1838 | auto Kind = FD->getDeclName().getCXXOverloadedOperator(); |
| 1839 | if (getCodeGenOpts().AssumeSaneOperatorNew && |
| 1840 | (Kind == OO_New || Kind == OO_Array_New)) |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 1841 | F->addAttribute(llvm::AttributeList::ReturnIndex, |
Richard Smith | 351241c | 2016-04-07 21:46:12 +0000 | [diff] [blame] | 1842 | llvm::Attribute::NoAlias); |
| 1843 | } |
| 1844 | |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1845 | if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD)) |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1846 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1847 | else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1848 | if (MD->isVirtual()) |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 1849 | F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Peter Collingbourne | 0446e7c | 2016-03-14 18:41:59 +0000 | [diff] [blame] | 1850 | |
Evgeniy Stepanov | f754309 | 2016-10-31 22:28:10 +0000 | [diff] [blame] | 1851 | // Don't emit entries for function declarations in the cross-DSO mode. This |
| 1852 | // is handled with better precision by the receiving DSO. |
| 1853 | if (!CodeGenOpts.SanitizeCfiCrossDso) |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 1854 | CreateFunctionTypeMetadataForIcall(FD, F); |
Alexey Bataev | b952e63 | 2017-12-15 16:28:31 +0000 | [diff] [blame] | 1855 | |
| 1856 | if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>()) |
| 1857 | getOpenMPRuntime().emitDeclareSimdFunction(FD, F); |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 1858 | |
| 1859 | if (const auto *CB = FD->getAttr<CallbackAttr>()) { |
| 1860 | // Annotate the callback behavior as metadata: |
| 1861 | // - The callback callee (as argument number). |
| 1862 | // - The callback payloads (as argument numbers). |
| 1863 | llvm::LLVMContext &Ctx = F->getContext(); |
| 1864 | llvm::MDBuilder MDB(Ctx); |
| 1865 | |
| 1866 | // The payload indices are all but the first one in the encoding. The first |
| 1867 | // identifies the callback callee. |
| 1868 | int CalleeIdx = *CB->encoding_begin(); |
| 1869 | ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end()); |
| 1870 | F->addMetadata(llvm::LLVMContext::MD_callback, |
| 1871 | *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( |
| 1872 | CalleeIdx, PayloadIndices, |
| 1873 | /* VarArgsArePassed */ false)})); |
| 1874 | } |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1877 | void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1879 | "Only globals with definition can force usage."); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1880 | LLVMUsed.emplace_back(GV); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1883 | void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { |
| 1884 | assert(!GV->isDeclaration() && |
| 1885 | "Only globals with definition can force usage."); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1886 | LLVMCompilerUsed.emplace_back(GV); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | static void emitUsed(CodeGenModule &CGM, StringRef Name, |
Sanjoy Das | e369bd9 | 2017-05-01 17:08:00 +0000 | [diff] [blame] | 1890 | std::vector<llvm::WeakTrackingVH> &List) { |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1891 | // Don't create llvm.used if there is no need. |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1892 | if (List.empty()) |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1893 | return; |
| 1894 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1895 | // Convert List to what ConstantArray needs. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 1896 | SmallVector<llvm::Constant*, 8> UsedArray; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1897 | UsedArray.resize(List.size()); |
| 1898 | for (unsigned i = 0, e = List.size(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | UsedArray[i] = |
Justin Holewinski | f37f3d3 | 2015-02-02 21:05:49 +0000 | [diff] [blame] | 1900 | llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
| 1901 | cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 1902 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Fariborz Jahanian | 248c719 | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 1904 | if (UsedArray.empty()) |
| 1905 | return; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1906 | llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1908 | auto *GV = new llvm::GlobalVariable( |
| 1909 | CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, |
| 1910 | llvm::ConstantArray::get(ATy, UsedArray), Name); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1911 | |
| 1912 | GV->setSection("llvm.metadata"); |
| 1913 | } |
| 1914 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 1915 | void CodeGenModule::emitLLVMUsed() { |
| 1916 | emitUsed(*this, "llvm.used", LLVMUsed); |
| 1917 | emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); |
| 1918 | } |
| 1919 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1920 | void CodeGenModule::AppendLinkerOptions(StringRef Opts) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1921 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1922 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1923 | } |
| 1924 | |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1925 | void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { |
| 1926 | llvm::SmallString<32> Opt; |
| 1927 | getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1928 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1929 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 1930 | } |
| 1931 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1932 | void CodeGenModule::AddDependentLib(StringRef Lib) { |
Ben Dunbobbin | 1d16515 | 2019-05-17 03:44:15 +0000 | [diff] [blame] | 1933 | auto &C = getLLVMContext(); |
| 1934 | if (getTarget().getTriple().isOSBinFormatELF()) { |
| 1935 | ELFDependentLibraries.push_back( |
| 1936 | llvm::MDNode::get(C, llvm::MDString::get(C, Lib))); |
| 1937 | return; |
| 1938 | } |
| 1939 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1940 | llvm::SmallString<24> Opt; |
| 1941 | getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1942 | auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
Ben Dunbobbin | 1d16515 | 2019-05-17 03:44:15 +0000 | [diff] [blame] | 1943 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts)); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1946 | /// Add link options implied by the given module, including modules |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1947 | /// it depends on, using a postorder walk. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1948 | static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1949 | SmallVectorImpl<llvm::MDNode *> &Metadata, |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1950 | llvm::SmallPtrSet<Module *, 16> &Visited) { |
| 1951 | // Import this module's parent. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1952 | if (Mod->Parent && Visited.insert(Mod->Parent).second) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1953 | addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | // Import this module's dependencies. |
| 1957 | for (unsigned I = Mod->Imports.size(); I > 0; --I) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1958 | if (Visited.insert(Mod->Imports[I - 1]).second) |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1959 | addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | // Add linker options to link against the libraries/frameworks |
| 1963 | // described by this module. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1964 | llvm::LLVMContext &Context = CGM.getLLVMContext(); |
Saleem Abdulrasool | 175890e | 2019-01-05 19:27:12 +0000 | [diff] [blame] | 1965 | bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF(); |
Bruno Cardoso Lopes | a3b5f71 | 2018-04-16 19:42:32 +0000 | [diff] [blame] | 1966 | |
| 1967 | // For modules that use export_as for linking, use that module |
| 1968 | // name instead. |
| 1969 | if (Mod->UseExportAsModuleLinkName) |
| 1970 | return; |
| 1971 | |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1972 | for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1973 | // Link against a framework. Frameworks are currently Darwin only, so we |
| 1974 | // don't to ask TargetCodeGenInfo for the spelling of the linker option. |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1975 | if (Mod->LinkLibraries[I-1].IsFramework) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1976 | llvm::Metadata *Args[2] = { |
| 1977 | llvm::MDString::get(Context, "-framework"), |
| 1978 | llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)}; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1979 | |
| 1980 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
| 1981 | continue; |
| 1982 | } |
| 1983 | |
| 1984 | // Link against a library. |
Ben Dunbobbin | 1d16515 | 2019-05-17 03:44:15 +0000 | [diff] [blame] | 1985 | if (IsELF) { |
Saleem Abdulrasool | 175890e | 2019-01-05 19:27:12 +0000 | [diff] [blame] | 1986 | llvm::Metadata *Args[2] = { |
| 1987 | llvm::MDString::get(Context, "lib"), |
| 1988 | llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library), |
| 1989 | }; |
| 1990 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
| 1991 | } else { |
| 1992 | llvm::SmallString<24> Opt; |
| 1993 | CGM.getTargetCodeGenInfo().getDependentLibraryOption( |
| 1994 | Mod->LinkLibraries[I - 1].Library, Opt); |
| 1995 | auto *OptString = llvm::MDString::get(Context, Opt); |
| 1996 | Metadata.push_back(llvm::MDNode::get(Context, OptString)); |
| 1997 | } |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | void CodeGenModule::EmitModuleLinkOptions() { |
| 2002 | // Collect the set of all of the modules we want to visit to emit link |
| 2003 | // options, which is essentially the imported modules and all of their |
| 2004 | // non-explicit child modules. |
| 2005 | llvm::SetVector<clang::Module *> LinkModules; |
| 2006 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 2007 | SmallVector<clang::Module *, 16> Stack; |
| 2008 | |
| 2009 | // Seed the stack with imported modules. |
Manman Ren | 9803ee8 | 2017-01-11 18:47:38 +0000 | [diff] [blame] | 2010 | for (Module *M : ImportedModules) { |
| 2011 | // Do not add any link flags when an implementation TU of a module imports |
| 2012 | // a header of that same module. |
| 2013 | if (M->getTopLevelModuleName() == getLangOpts().CurrentModule && |
| 2014 | !getLangOpts().isCompilingModule()) |
| 2015 | continue; |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 2016 | if (Visited.insert(M).second) |
| 2017 | Stack.push_back(M); |
Manman Ren | 9803ee8 | 2017-01-11 18:47:38 +0000 | [diff] [blame] | 2018 | } |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Find all of the modules to import, making a little effort to prune |
| 2021 | // non-leaf modules. |
| 2022 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2023 | clang::Module *Mod = Stack.pop_back_val(); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2024 | |
| 2025 | bool AnyChildren = false; |
| 2026 | |
| 2027 | // Visit the submodules of this module. |
Saleem Abdulrasool | da32d7f | 2019-01-05 18:39:32 +0000 | [diff] [blame] | 2028 | for (const auto &SM : Mod->submodules()) { |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2029 | // Skip explicit children; they need to be explicitly imported to be |
| 2030 | // linked against. |
Saleem Abdulrasool | da32d7f | 2019-01-05 18:39:32 +0000 | [diff] [blame] | 2031 | if (SM->IsExplicit) |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2032 | continue; |
| 2033 | |
Saleem Abdulrasool | da32d7f | 2019-01-05 18:39:32 +0000 | [diff] [blame] | 2034 | if (Visited.insert(SM).second) { |
| 2035 | Stack.push_back(SM); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2036 | AnyChildren = true; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | // We didn't find any children, so add this module to the list of |
| 2041 | // modules to link against. |
| 2042 | if (!AnyChildren) { |
| 2043 | LinkModules.insert(Mod); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | // Add link options for all of the imported modules in reverse topological |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2048 | // order. We don't do anything to try to order import link flags with respect |
| 2049 | // to linker options inserted by things like #pragma comment(). |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 2050 | SmallVector<llvm::MDNode *, 16> MetadataArgs; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2051 | Visited.clear(); |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 2052 | for (Module *M : LinkModules) |
| 2053 | if (Visited.insert(M).second) |
| 2054 | addLinkOptionsPostorder(*this, M, MetadataArgs, Visited); |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 2055 | std::reverse(MetadataArgs.begin(), MetadataArgs.end()); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2056 | LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end()); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2057 | |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 2058 | // Add the linker options metadata flag. |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 2059 | auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options"); |
| 2060 | for (auto *MD : LinkerOptionsMetadata) |
| 2061 | NMD->addOperand(MD); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 2064 | void CodeGenModule::EmitDeferred() { |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 2065 | // Emit deferred declare target declarations. |
| 2066 | if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd) |
| 2067 | getOpenMPRuntime().emitDeferredTargetDecls(); |
| 2068 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2069 | // Emit code for any potentially referenced deferred decls. Since a |
| 2070 | // previously unused static decl may become used during the generation of code |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2071 | // for a static function, iterate until no changes are made. |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 2072 | |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2073 | if (!DeferredVTables.empty()) { |
| 2074 | EmitDeferredVTables(); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2075 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 2076 | // Emitting a vtable doesn't directly cause more vtables to |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2077 | // become deferred, although it can cause functions to be |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 2078 | // emitted that then need those vtables. |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2079 | assert(DeferredVTables.empty()); |
| 2080 | } |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 2081 | |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 2082 | // Stop if we're out of both deferred vtables and deferred declarations. |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2083 | if (DeferredDeclsToEmit.empty()) |
| 2084 | return; |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 2085 | |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2086 | // Grab the list of decls to emit. If EmitGlobalDefinition schedules more |
| 2087 | // work, it will not interfere with this. |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2088 | std::vector<GlobalDecl> CurDeclsToEmit; |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2089 | CurDeclsToEmit.swap(DeferredDeclsToEmit); |
| 2090 | |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2091 | for (GlobalDecl &D : CurDeclsToEmit) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2092 | // We should call GetAddrOfGlobal with IsForDefinition set to true in order |
| 2093 | // to get GlobalValue with exactly the type we need, not something that |
| 2094 | // might had been created for another decl with the same mangled name but |
| 2095 | // different type. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2096 | llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>( |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 2097 | GetAddrOfGlobal(D, ForDefinition)); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2098 | |
| 2099 | // In case of different address spaces, we may still get a cast, even with |
| 2100 | // IsForDefinition equal to true. Query mangled names table to get |
| 2101 | // GlobalValue. |
| 2102 | if (!GV) |
| 2103 | GV = GetGlobalValue(getMangledName(D)); |
| 2104 | |
| 2105 | // Make sure GetGlobalValue returned non-null. |
| 2106 | assert(GV); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2107 | |
John McCall | c2af939 | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 2108 | // Check to see if we've already emitted this. This is necessary |
| 2109 | // for a couple of reasons: first, decls can end up in the |
| 2110 | // deferred-decls queue multiple times, and second, decls can end |
| 2111 | // up with definitions in unusual ways (e.g. by an extern inline |
| 2112 | // function acquiring a strong function redefinition). Just |
| 2113 | // ignore these cases. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2114 | if (!GV->isDeclaration()) |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2115 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2117 | // Otherwise, emit the definition and move on to the next one. |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2118 | EmitGlobalDefinition(D, GV); |
Rafael Espindola | e5df59f | 2015-01-22 00:24:57 +0000 | [diff] [blame] | 2119 | |
| 2120 | // If we found out that we need to emit more decls, do that recursively. |
| 2121 | // This has the advantage that the decls are emitted in a DFS and related |
| 2122 | // ones are close together, which is convenient for testing. |
| 2123 | if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) { |
| 2124 | EmitDeferred(); |
| 2125 | assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty()); |
| 2126 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2127 | } |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 2128 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 2129 | |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 2130 | void CodeGenModule::EmitVTablesOpportunistically() { |
| 2131 | // Try to emit external vtables as available_externally if they have emitted |
| 2132 | // all inlined virtual functions. It runs after EmitDeferred() and therefore |
| 2133 | // is not allowed to create new references to things that need to be emitted |
| 2134 | // lazily. Note that it also uses fact that we eagerly emitting RTTI. |
| 2135 | |
Piotr Padlewski | 0554774 | 2017-06-01 09:24:36 +0000 | [diff] [blame] | 2136 | assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) |
| 2137 | && "Only emit opportunistic vtables with optimizations"); |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 2138 | |
| 2139 | for (const CXXRecordDecl *RD : OpportunisticVTables) { |
| 2140 | assert(getVTables().isVTableExternal(RD) && |
| 2141 | "This queue should only contain external vtables"); |
| 2142 | if (getCXXABI().canSpeculativelyEmitVTable(RD)) |
| 2143 | VTables.GenerateClassData(RD); |
| 2144 | } |
| 2145 | OpportunisticVTables.clear(); |
| 2146 | } |
| 2147 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2148 | void CodeGenModule::EmitGlobalAnnotations() { |
| 2149 | if (Annotations.empty()) |
| 2150 | return; |
| 2151 | |
| 2152 | // Create a new global variable for the ConstantStruct in the Module. |
| 2153 | llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( |
| 2154 | Annotations[0]->getType(), Annotations.size()), Annotations); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2155 | auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false, |
| 2156 | llvm::GlobalValue::AppendingLinkage, |
| 2157 | Array, "llvm.global.annotations"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2158 | gv->setSection(AnnotationSection); |
| 2159 | } |
| 2160 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2161 | llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) { |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 2162 | llvm::Constant *&AStr = AnnotationStrings[Str]; |
| 2163 | if (AStr) |
| 2164 | return AStr; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2165 | |
| 2166 | // Not found yet, create a new global. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 2167 | llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2168 | auto *gv = |
| 2169 | new llvm::GlobalVariable(getModule(), s->getType(), true, |
| 2170 | llvm::GlobalValue::PrivateLinkage, s, ".str"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2171 | gv->setSection(AnnotationSection); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 2172 | gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 2173 | AStr = gv; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2174 | return gv; |
| 2175 | } |
| 2176 | |
| 2177 | llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { |
| 2178 | SourceManager &SM = getContext().getSourceManager(); |
| 2179 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2180 | if (PLoc.isValid()) |
| 2181 | return EmitAnnotationString(PLoc.getFilename()); |
| 2182 | return EmitAnnotationString(SM.getBufferName(Loc)); |
| 2183 | } |
| 2184 | |
| 2185 | llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { |
| 2186 | SourceManager &SM = getContext().getSourceManager(); |
| 2187 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 2188 | unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : |
| 2189 | SM.getExpansionLineNumber(L); |
| 2190 | return llvm::ConstantInt::get(Int32Ty, LineNo); |
| 2191 | } |
| 2192 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 2194 | const AnnotateAttr *AA, |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2195 | SourceLocation L) { |
| 2196 | // Get the globals for file name, annotation, and the line number. |
| 2197 | llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), |
| 2198 | *UnitGV = EmitAnnotationUnit(L), |
| 2199 | *LineNoCst = EmitAnnotationLineNo(L); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 2200 | |
Daniel Dunbar | 346892a | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 2201 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 2202 | llvm::Constant *Fields[4] = { |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2203 | llvm::ConstantExpr::getBitCast(GV, Int8PtrTy), |
| 2204 | llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), |
| 2205 | llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), |
| 2206 | LineNoCst |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 2207 | }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2208 | return llvm::ConstantStruct::getAnon(Fields); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2211 | void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, |
| 2212 | llvm::GlobalValue *GV) { |
| 2213 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 2214 | // Get the struct elements for these annotations. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2215 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) |
| 2216 | Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2219 | bool CodeGenModule::isInSanitizerBlacklist(SanitizerMask Kind, |
| 2220 | llvm::Function *Fn, |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 2221 | SourceLocation Loc) const { |
| 2222 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
| 2223 | // Blacklist by function name. |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2224 | if (SanitizerBL.isBlacklistedFunction(Kind, Fn->getName())) |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 2225 | return true; |
| 2226 | // Blacklist by location. |
Yaron Keren | ed1fe5d | 2015-10-03 05:15:57 +0000 | [diff] [blame] | 2227 | if (Loc.isValid()) |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2228 | return SanitizerBL.isBlacklistedLocation(Kind, Loc); |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 2229 | // If location is unknown, this may be a compiler-generated function. Assume |
| 2230 | // it's located in the main file. |
| 2231 | auto &SM = Context.getSourceManager(); |
| 2232 | if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2233 | return SanitizerBL.isBlacklistedFile(Kind, MainFile->getName()); |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 2234 | } |
| 2235 | return false; |
| 2236 | } |
| 2237 | |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 2238 | bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV, |
| 2239 | SourceLocation Loc, QualType Ty, |
| 2240 | StringRef Category) const { |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2241 | // For now globals can be blacklisted only in ASan and KASan. |
Evgeniy Stepanov | c5e7f56 | 2019-07-15 20:02:23 +0000 | [diff] [blame] | 2242 | const SanitizerMask EnabledAsanMask = |
| 2243 | LangOpts.Sanitize.Mask & |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame] | 2244 | (SanitizerKind::Address | SanitizerKind::KernelAddress | |
Evgeniy Stepanov | c5e7f56 | 2019-07-15 20:02:23 +0000 | [diff] [blame] | 2245 | SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress | |
| 2246 | SanitizerKind::MemTag); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2247 | if (!EnabledAsanMask) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 2248 | return false; |
| 2249 | const auto &SanitizerBL = getContext().getSanitizerBlacklist(); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2250 | if (SanitizerBL.isBlacklistedGlobal(EnabledAsanMask, GV->getName(), Category)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 2251 | return true; |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2252 | if (SanitizerBL.isBlacklistedLocation(EnabledAsanMask, Loc, Category)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 2253 | return true; |
| 2254 | // Check global type. |
| 2255 | if (!Ty.isNull()) { |
| 2256 | // Drill down the array types: if global variable of a fixed type is |
| 2257 | // blacklisted, we also don't instrument arrays of them. |
| 2258 | while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr())) |
| 2259 | Ty = AT->getElementType(); |
| 2260 | Ty = Ty.getCanonicalType().getUnqualifiedType(); |
| 2261 | // We allow to blacklist only record types (classes, structs etc.) |
| 2262 | if (Ty->isRecordType()) { |
| 2263 | std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy()); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2264 | if (SanitizerBL.isBlacklistedType(EnabledAsanMask, TypeStr, Category)) |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 2265 | return true; |
| 2266 | } |
| 2267 | } |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 2271 | bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, |
| 2272 | StringRef Category) const { |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 2273 | const auto &XRayFilter = getContext().getXRayFilter(); |
| 2274 | using ImbueAttr = XRayFunctionFilter::ImbueAttribute; |
Dean Michael Berris | 488f7c2 | 2018-04-13 02:31:58 +0000 | [diff] [blame] | 2275 | auto Attr = ImbueAttr::NONE; |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 2276 | if (Loc.isValid()) |
| 2277 | Attr = XRayFilter.shouldImbueLocation(Loc, Category); |
| 2278 | if (Attr == ImbueAttr::NONE) |
| 2279 | Attr = XRayFilter.shouldImbueFunction(Fn->getName()); |
| 2280 | switch (Attr) { |
| 2281 | case ImbueAttr::NONE: |
| 2282 | return false; |
| 2283 | case ImbueAttr::ALWAYS: |
| 2284 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 2285 | break; |
Dean Michael Berris | 170429e | 2017-05-24 05:46:36 +0000 | [diff] [blame] | 2286 | case ImbueAttr::ALWAYS_ARG1: |
| 2287 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 2288 | Fn->addFnAttr("xray-log-args", "1"); |
| 2289 | break; |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 2290 | case ImbueAttr::NEVER: |
| 2291 | Fn->addFnAttr("function-instrument", "xray-never"); |
| 2292 | break; |
| 2293 | } |
| 2294 | return true; |
| 2295 | } |
| 2296 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2297 | bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 2298 | // Never defer when EmitAllDecls is specified. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2299 | if (LangOpts.EmitAllDecls) |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2300 | return true; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2301 | |
Elizabeth Andrews | 6593df2 | 2018-08-22 19:05:19 +0000 | [diff] [blame] | 2302 | if (CodeGenOpts.KeepStaticConsts) { |
| 2303 | const auto *VD = dyn_cast<VarDecl>(Global); |
| 2304 | if (VD && VD->getType().isConstQualified() && |
Erich Keane | 85822b3 | 2018-10-25 19:13:46 +0000 | [diff] [blame] | 2305 | VD->getStorageDuration() == SD_Static) |
Elizabeth Andrews | 6593df2 | 2018-08-22 19:05:19 +0000 | [diff] [blame] | 2306 | return true; |
| 2307 | } |
| 2308 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2309 | return getContext().DeclMustBeEmitted(Global); |
| 2310 | } |
| 2311 | |
| 2312 | bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) { |
| 2313 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) |
| 2314 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
| 2315 | // Implicit template instantiations may change linkage if they are later |
| 2316 | // explicitly instantiated, so they should not be emitted eagerly. |
| 2317 | return false; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 2318 | if (const auto *VD = dyn_cast<VarDecl>(Global)) |
| 2319 | if (Context.getInlineVariableDefinitionKind(VD) == |
| 2320 | ASTContext::InlineVariableDefinitionKind::WeakUnknown) |
| 2321 | // A definition of an inline constexpr static data member may change |
| 2322 | // linkage later if it's redeclared outside the class. |
| 2323 | return false; |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2324 | // If OpenMP is enabled and threadprivates must be generated like TLS, delay |
| 2325 | // codegen for global variables, because they may be marked as threadprivate. |
| 2326 | if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS && |
Alexey Bataev | 96edb2e | 2018-06-25 15:32:05 +0000 | [diff] [blame] | 2327 | getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) && |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2328 | !isTypeConstant(Global->getType(), false) && |
| 2329 | !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global)) |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2330 | return false; |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2331 | |
| 2332 | return true; |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2335 | ConstantAddress CodeGenModule::GetAddrOfUuidDescriptor( |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2336 | const CXXUuidofExpr* E) { |
| 2337 | // Sema has verified that IIDSource has a __declspec(uuid()), and that its |
| 2338 | // well-formed. |
David Majnemer | 2041b46 | 2016-03-28 03:19:50 +0000 | [diff] [blame] | 2339 | StringRef Uuid = E->getUuidStr(); |
David Majnemer | cf963ce | 2013-08-09 08:35:59 +0000 | [diff] [blame] | 2340 | std::string Name = "_GUID_" + Uuid.lower(); |
| 2341 | std::replace(Name.begin(), Name.end(), '-', '_'); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2342 | |
David Majnemer | 6568760 | 2016-03-27 04:46:14 +0000 | [diff] [blame] | 2343 | // The UUID descriptor should be pointer aligned. |
| 2344 | CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2345 | |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2346 | // Look for an existing global. |
| 2347 | if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2348 | return ConstantAddress(GV, Alignment); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2349 | |
Nico Weber | 17d3a2c | 2014-09-08 16:26:36 +0000 | [diff] [blame] | 2350 | llvm::Constant *Init = EmitUuidofInitializer(Uuid); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2351 | assert(Init && "failed to initialize as constant"); |
| 2352 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2353 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 2354 | getModule(), Init->getType(), |
Reid Kleckner | b9921df | 2013-09-03 21:49:32 +0000 | [diff] [blame] | 2355 | /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 2356 | if (supportsCOMDAT()) |
| 2357 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Rafael Espindola | ca08d24 | 2018-03-20 15:42:58 +0000 | [diff] [blame] | 2358 | setDSOLocal(GV); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2359 | return ConstantAddress(GV, Alignment); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2362 | ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2363 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
| 2364 | assert(AA && "No alias?"); |
| 2365 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2366 | CharUnits Alignment = getContext().getDeclAlign(VD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2367 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2368 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2369 | // 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] | 2370 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 2371 | if (Entry) { |
| 2372 | unsigned AS = getContext().getTargetAddressSpace(VD->getType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2373 | auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS)); |
| 2374 | return ConstantAddress(Ptr, Alignment); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 2375 | } |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2376 | |
| 2377 | llvm::Constant *Aliasee; |
| 2378 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 2379 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, |
| 2380 | GlobalDecl(cast<FunctionDecl>(VD)), |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 2381 | /*ForVTable=*/false); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2382 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2383 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2384 | llvm::PointerType::getUnqual(DeclTy), |
| 2385 | nullptr); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 2386 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2387 | auto *F = cast<llvm::GlobalValue>(Aliasee); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 2388 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 2389 | WeakRefReferences.insert(F); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2390 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2391 | return ConstantAddress(Aliasee, Alignment); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 2394 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2395 | const auto *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 2397 | // Weak references don't produce any output by themselves. |
| 2398 | if (Global->hasAttr<WeakRefAttr>()) |
| 2399 | return; |
| 2400 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2401 | // If this is an alias definition (which otherwise looks like a declaration) |
| 2402 | // emit it now. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2403 | if (Global->hasAttr<AliasAttr>()) |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 2404 | return EmitAliasDefinition(GD); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 2405 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 2406 | // IFunc like an alias whose value is resolved at runtime by calling resolver. |
| 2407 | if (Global->hasAttr<IFuncAttr>()) |
| 2408 | return emitIFuncDefinition(GD); |
| 2409 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2410 | // If this is a cpu_dispatch multiversion function, emit the resolver. |
| 2411 | if (Global->hasAttr<CPUDispatchAttr>()) |
| 2412 | return emitCPUDispatchDefinition(GD); |
| 2413 | |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2414 | // If this is CUDA, be selective about which declarations we emit. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2415 | if (LangOpts.CUDA) { |
Artem Belevich | f3d3db6 | 2015-03-19 18:58:18 +0000 | [diff] [blame] | 2416 | if (LangOpts.CUDAIsDevice) { |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2417 | if (!Global->hasAttr<CUDADeviceAttr>() && |
| 2418 | !Global->hasAttr<CUDAGlobalAttr>() && |
| 2419 | !Global->hasAttr<CUDAConstantAttr>() && |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 2420 | !Global->hasAttr<CUDASharedAttr>() && |
| 2421 | !(LangOpts.HIP && Global->hasAttr<HIPPinnedShadowAttr>())) |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2422 | return; |
| 2423 | } else { |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2424 | // We need to emit host-side 'shadows' for all global |
| 2425 | // device-side variables because the CUDA runtime needs their |
| 2426 | // size and host-side address in order to provide access to |
| 2427 | // their device-side incarnations. |
| 2428 | |
| 2429 | // So device-only functions are the only things we skip. |
| 2430 | if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() && |
| 2431 | Global->hasAttr<CUDADeviceAttr>()) |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2432 | return; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 2433 | |
| 2434 | assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && |
| 2435 | "Expected Variable or Function"); |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 2439 | if (LangOpts.OpenMP) { |
| 2440 | // If this is OpenMP device, check if it is legal to emit this global |
| 2441 | // normally. |
| 2442 | if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD)) |
| 2443 | return; |
| 2444 | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) { |
| 2445 | if (MustBeEmitted(Global)) |
| 2446 | EmitOMPDeclareReduction(DRD); |
| 2447 | return; |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 2448 | } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) { |
| 2449 | if (MustBeEmitted(Global)) |
| 2450 | EmitOMPDeclareMapper(DMD); |
| 2451 | return; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 2452 | } |
| 2453 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 2455 | // Ignore declarations, they will be emitted on their first use. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2456 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 2457 | // Forward declarations are emitted lazily on first use. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2458 | if (!FD->doesThisDeclarationHaveABody()) { |
| 2459 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
| 2460 | return; |
| 2461 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2462 | StringRef MangledName = getMangledName(GD); |
David Majnemer | af36980 | 2014-03-29 14:19:55 +0000 | [diff] [blame] | 2463 | |
| 2464 | // Compute the function info and LLVM type. |
| 2465 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 2466 | llvm::Type *Ty = getTypes().GetFunctionType(FI); |
| 2467 | |
| 2468 | GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false, |
| 2469 | /*DontDefer=*/false); |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 2470 | return; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2471 | } |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 2472 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2473 | const auto *VD = cast<VarDecl>(Global); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2474 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
Artem Belevich | 9953577 | 2018-12-22 01:11:09 +0000 | [diff] [blame] | 2475 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition && |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 2476 | !Context.isMSStaticDataMemberInlineDefinition(VD)) { |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2477 | if (LangOpts.OpenMP) { |
| 2478 | // Emit declaration of the must-be-emitted declare target variable. |
| 2479 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 2480 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2481 | bool UnifiedMemoryEnabled = |
| 2482 | getOpenMPRuntime().hasRequiresUnifiedSharedMemory(); |
| 2483 | if (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 2484 | !UnifiedMemoryEnabled) { |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2485 | (void)GetAddrOfGlobalVar(VD); |
| 2486 | } else { |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2487 | assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || |
| 2488 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 2489 | UnifiedMemoryEnabled)) && |
| 2490 | "Link clause or to clause with unified memory expected."); |
| 2491 | (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2492 | } |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2493 | |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2494 | return; |
| 2495 | } |
| 2496 | } |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 2497 | // If this declaration may have caused an inline variable definition to |
| 2498 | // change linkage, make sure that it's emitted. |
| 2499 | if (Context.getInlineVariableDefinitionKind(VD) == |
| 2500 | ASTContext::InlineVariableDefinitionKind::Strong) |
| 2501 | GetAddrOfGlobalVar(VD); |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 2502 | return; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 2503 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2506 | // Defer code generation to first use when possible, e.g. if this is an inline |
| 2507 | // function. If the global must always be emitted, do it eagerly if possible |
| 2508 | // to benefit from cache locality. |
| 2509 | if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) { |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 2510 | // Emit the definition if it can't be deferred. |
| 2511 | EmitGlobalDefinition(GD); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2512 | return; |
| 2513 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2514 | |
| 2515 | // If we're deferring emission of a C++ variable with an |
| 2516 | // initializer, remember the order in which it appeared in the file. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2517 | if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) && |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2518 | cast<VarDecl>(Global)->hasInit()) { |
| 2519 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2520 | CXXGlobalInits.push_back(nullptr); |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 2521 | } |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2523 | StringRef MangledName = getMangledName(GD); |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2524 | if (GetGlobalValue(MangledName) != nullptr) { |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2525 | // The value has already been used and should therefore be emitted. |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2526 | addDeferredDeclToEmit(GD); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2527 | } else if (MustBeEmitted(Global)) { |
| 2528 | // The value must be emitted, but cannot be emitted eagerly. |
| 2529 | assert(!MayBeEmittedEagerly(Global)); |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 2530 | addDeferredDeclToEmit(GD); |
Hans Wennborg | 43a0f99 | 2015-01-10 01:19:48 +0000 | [diff] [blame] | 2531 | } else { |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 2532 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 2533 | // first use of the mangled name will cause it to move into |
| 2534 | // DeferredDeclsToEmit. |
| 2535 | DeferredDecls[MangledName] = GD; |
| 2536 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2539 | // Check if T is a class type with a destructor that's not dllimport. |
| 2540 | static bool HasNonDllImportDtor(QualType T) { |
| 2541 | if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>()) |
| 2542 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 2543 | if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>()) |
| 2544 | return true; |
| 2545 | |
| 2546 | return false; |
| 2547 | } |
| 2548 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2549 | namespace { |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2550 | struct FunctionIsDirectlyRecursive |
| 2551 | : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2552 | const StringRef Name; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2553 | const Builtin::Context &BI; |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2554 | FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) |
| 2555 | : Name(N), BI(C) {} |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2556 | |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2557 | bool VisitCallExpr(const CallExpr *E) { |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2558 | const FunctionDecl *FD = E->getDirectCallee(); |
| 2559 | if (!FD) |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2560 | return false; |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2561 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 2562 | if (Attr && Name == Attr->getLabel()) |
| 2563 | return true; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2564 | unsigned BuiltinID = FD->getBuiltinID(); |
David Majnemer | 41011f6 | 2015-06-25 23:50:40 +0000 | [diff] [blame] | 2565 | if (!BuiltinID || !BI.isLibFunction(BuiltinID)) |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2566 | return false; |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 2567 | StringRef BuiltinName = BI.getName(BuiltinID); |
Nick Lewycky | 9ed5b15 | 2012-01-18 03:41:19 +0000 | [diff] [blame] | 2568 | if (BuiltinName.startswith("__builtin_") && |
| 2569 | Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) { |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2570 | return true; |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2571 | } |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2572 | return false; |
| 2573 | } |
| 2574 | |
| 2575 | bool VisitStmt(const Stmt *S) { |
| 2576 | for (const Stmt *Child : S->children()) |
| 2577 | if (Child && this->Visit(Child)) |
| 2578 | return true; |
| 2579 | return false; |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2580 | } |
| 2581 | }; |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2582 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2583 | // Make sure we're not referencing non-imported vars or functions. |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2584 | struct DLLImportFunctionVisitor |
| 2585 | : public RecursiveASTVisitor<DLLImportFunctionVisitor> { |
| 2586 | bool SafeToInline = true; |
| 2587 | |
Hans Wennborg | 1b3aee7 | 2016-09-13 22:51:42 +0000 | [diff] [blame] | 2588 | bool shouldVisitImplicitCode() const { return true; } |
| 2589 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2590 | bool VisitVarDecl(VarDecl *VD) { |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2591 | if (VD->getTLSKind()) { |
| 2592 | // A thread-local variable cannot be imported. |
| 2593 | SafeToInline = false; |
| 2594 | return SafeToInline; |
| 2595 | } |
| 2596 | |
| 2597 | // A variable definition might imply a destructor call. |
| 2598 | if (VD->isThisDeclarationADefinition()) |
| 2599 | SafeToInline = !HasNonDllImportDtor(VD->getType()); |
| 2600 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2601 | return SafeToInline; |
| 2602 | } |
| 2603 | |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2604 | bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 2605 | if (const auto *D = E->getTemporary()->getDestructor()) |
| 2606 | SafeToInline = D->hasAttr<DLLImportAttr>(); |
| 2607 | return SafeToInline; |
| 2608 | } |
| 2609 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2610 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2611 | ValueDecl *VD = E->getDecl(); |
| 2612 | if (isa<FunctionDecl>(VD)) |
| 2613 | SafeToInline = VD->hasAttr<DLLImportAttr>(); |
| 2614 | else if (VarDecl *V = dyn_cast<VarDecl>(VD)) |
| 2615 | SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>(); |
| 2616 | return SafeToInline; |
| 2617 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2618 | |
Hans Wennborg | 1b3aee7 | 2016-09-13 22:51:42 +0000 | [diff] [blame] | 2619 | bool VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 2620 | SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>(); |
| 2621 | return SafeToInline; |
| 2622 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2623 | |
Hans Wennborg | 251c204 | 2017-01-23 23:57:50 +0000 | [diff] [blame] | 2624 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2625 | CXXMethodDecl *M = E->getMethodDecl(); |
| 2626 | if (!M) { |
| 2627 | // Call through a pointer to member function. This is safe to inline. |
| 2628 | SafeToInline = true; |
| 2629 | } else { |
| 2630 | SafeToInline = M->hasAttr<DLLImportAttr>(); |
| 2631 | } |
| 2632 | return SafeToInline; |
| 2633 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2634 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2635 | bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 2636 | SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>(); |
| 2637 | return SafeToInline; |
| 2638 | } |
Hans Wennborg | cac8ce0 | 2017-02-15 23:28:10 +0000 | [diff] [blame] | 2639 | |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2640 | bool VisitCXXNewExpr(CXXNewExpr *E) { |
| 2641 | SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>(); |
| 2642 | return SafeToInline; |
| 2643 | } |
| 2644 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2645 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2646 | |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2647 | // isTriviallyRecursive - Check if this function calls another |
| 2648 | // decl that, because of the asm attribute or the other decl being a builtin, |
| 2649 | // ends up pointing to itself. |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2650 | bool |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2651 | CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) { |
| 2652 | StringRef Name; |
| 2653 | if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) { |
Nick Lewycky | 72cd229 | 2012-01-18 01:50:13 +0000 | [diff] [blame] | 2654 | // asm labels are a special kind of mangling we have to support. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2655 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 2656 | if (!Attr) |
| 2657 | return false; |
| 2658 | Name = Attr->getLabel(); |
| 2659 | } else { |
| 2660 | Name = FD->getName(); |
| 2661 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2662 | |
David Majnemer | 64b0bdf | 2015-06-30 04:41:18 +0000 | [diff] [blame] | 2663 | FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo); |
Reid Kleckner | f09c19c | 2019-01-25 19:18:40 +0000 | [diff] [blame] | 2664 | const Stmt *Body = FD->getBody(); |
| 2665 | return Body ? Walker.Visit(Body) : false; |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Hans Wennborg | 93f7547 | 2016-09-13 21:08:20 +0000 | [diff] [blame] | 2668 | bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) { |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 2669 | if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2670 | return true; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2671 | const auto *F = cast<FunctionDecl>(GD.getDecl()); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 2672 | if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>()) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2673 | return false; |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2674 | |
| 2675 | if (F->hasAttr<DLLImportAttr>()) { |
| 2676 | // Check whether it would be safe to inline this dllimport function. |
| 2677 | DLLImportFunctionVisitor Visitor; |
| 2678 | Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F)); |
| 2679 | if (!Visitor.SafeToInline) |
| 2680 | return false; |
Hans Wennborg | 93f7547 | 2016-09-13 21:08:20 +0000 | [diff] [blame] | 2681 | |
| 2682 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) { |
| 2683 | // Implicit destructor invocations aren't captured in the AST, so the |
| 2684 | // check above can't see them. Check for them manually here. |
| 2685 | for (const Decl *Member : Dtor->getParent()->decls()) |
| 2686 | if (isa<FieldDecl>(Member)) |
| 2687 | if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType())) |
| 2688 | return false; |
| 2689 | for (const CXXBaseSpecifier &B : Dtor->getParent()->bases()) |
| 2690 | if (HasNonDllImportDtor(B.getType())) |
| 2691 | return false; |
| 2692 | } |
Hans Wennborg | 6eaa832 | 2015-08-28 21:47:01 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2695 | // PR9614. Avoid cases where the source code is lying to us. An available |
| 2696 | // externally function should have an equivalent function somewhere else, |
| 2697 | // but a function that calls itself is clearly not equivalent to the real |
| 2698 | // implementation. |
| 2699 | // This happens in glibc's btowc and in some configure checks. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 2700 | return !isTriviallyRecursive(F); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Piotr Padlewski | d3b1cbd | 2017-06-01 08:04:05 +0000 | [diff] [blame] | 2703 | bool CodeGenModule::shouldOpportunisticallyEmitVTables() { |
| 2704 | return CodeGenOpts.OptimizationLevel > 0; |
| 2705 | } |
| 2706 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2707 | void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD, |
| 2708 | llvm::GlobalValue *GV) { |
| 2709 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
| 2710 | |
| 2711 | if (FD->isCPUSpecificMultiVersion()) { |
| 2712 | auto *Spec = FD->getAttr<CPUSpecificAttr>(); |
| 2713 | for (unsigned I = 0; I < Spec->cpus_size(); ++I) |
| 2714 | EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr); |
| 2715 | // Requires multiple emits. |
| 2716 | } else |
| 2717 | EmitGlobalFunctionDefinition(GD, GV); |
| 2718 | } |
| 2719 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2720 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2721 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2723 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
Anders Carlsson | 29295bf | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 2724 | Context.getSourceManager(), |
| 2725 | "Generating code for declaration"); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2726 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2727 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2728 | // At -O0, don't generate IR for functions with available_externally |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2729 | // linkage. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 2730 | if (!shouldEmitFunction(GD)) |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2731 | return; |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 2732 | |
Lubos Lunak | ab8cde4 | 2019-05-12 10:39:21 +0000 | [diff] [blame] | 2733 | llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() { |
| 2734 | std::string Name; |
| 2735 | llvm::raw_string_ostream OS(Name); |
| 2736 | FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(), |
| 2737 | /*Qualified=*/true); |
| 2738 | return Name; |
| 2739 | }); |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 2740 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2741 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2742 | // Make sure to emit the definition(s) before we emit the thunks. |
| 2743 | // This is necessary for the generation of certain thunks. |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 2744 | if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method)) |
| 2745 | ABI->emitCXXStructor(GD); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2746 | else if (FD->isMultiVersion()) |
| 2747 | EmitMultiVersionFunctionDefinition(GD, GV); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2748 | else |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2749 | EmitGlobalFunctionDefinition(GD, GV); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2750 | |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2751 | if (Method->isVirtual()) |
| 2752 | getVTables().EmitThunks(GD); |
| 2753 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 2754 | return; |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2755 | } |
Chris Lattner | d8d760c | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 2756 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2757 | if (FD->isMultiVersion()) |
| 2758 | return EmitMultiVersionFunctionDefinition(GD, GV); |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2759 | return EmitGlobalFunctionDefinition(GD, GV); |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 2760 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2761 | |
| 2762 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 2763 | return EmitGlobalVarDefinition(VD, !VD->hasDefinition()); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 2764 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2765 | llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 2768 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 2769 | llvm::Function *NewFn); |
| 2770 | |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2771 | static unsigned |
| 2772 | TargetMVPriority(const TargetInfo &TI, |
| 2773 | const CodeGenFunction::MultiVersionResolverOption &RO) { |
| 2774 | unsigned Priority = 0; |
| 2775 | for (StringRef Feat : RO.Conditions.Features) |
| 2776 | Priority = std::max(Priority, TI.multiVersionSortPriority(Feat)); |
| 2777 | |
| 2778 | if (!RO.Conditions.Architecture.empty()) |
| 2779 | Priority = std::max( |
| 2780 | Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture)); |
| 2781 | return Priority; |
| 2782 | } |
| 2783 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2784 | void CodeGenModule::emitMultiVersionFunctions() { |
| 2785 | for (GlobalDecl GD : MultiVersionFuncs) { |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2786 | SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2787 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 2788 | getContext().forEachMultiversionedFunctionVersion( |
| 2789 | FD, [this, &GD, &Options](const FunctionDecl *CurFD) { |
| 2790 | GlobalDecl CurGD{ |
| 2791 | (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)}; |
| 2792 | StringRef MangledName = getMangledName(CurGD); |
| 2793 | llvm::Constant *Func = GetGlobalValue(MangledName); |
| 2794 | if (!Func) { |
| 2795 | if (CurFD->isDefined()) { |
| 2796 | EmitGlobalFunctionDefinition(CurGD, nullptr); |
| 2797 | Func = GetGlobalValue(MangledName); |
| 2798 | } else { |
| 2799 | const CGFunctionInfo &FI = |
| 2800 | getTypes().arrangeGlobalDeclaration(GD); |
| 2801 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
| 2802 | Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false, |
| 2803 | /*DontDefer=*/false, ForDefinition); |
| 2804 | } |
| 2805 | assert(Func && "This should have just been created"); |
| 2806 | } |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2807 | |
| 2808 | const auto *TA = CurFD->getAttr<TargetAttr>(); |
| 2809 | llvm::SmallVector<StringRef, 8> Feats; |
| 2810 | TA->getAddedFeatures(Feats); |
| 2811 | |
| 2812 | Options.emplace_back(cast<llvm::Function>(Func), |
| 2813 | TA->getArchitecture(), Feats); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2814 | }); |
| 2815 | |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2816 | llvm::Function *ResolverFunc; |
| 2817 | const TargetInfo &TI = getTarget(); |
| 2818 | |
| 2819 | if (TI.supportsIFunc() || FD->isTargetMultiVersion()) |
| 2820 | ResolverFunc = cast<llvm::Function>( |
| 2821 | GetGlobalValue((getMangledName(GD) + ".resolver").str())); |
| 2822 | else |
| 2823 | ResolverFunc = cast<llvm::Function>(GetGlobalValue(getMangledName(GD))); |
| 2824 | |
Erich Keane | 0a6fde4 | 2018-01-16 19:49:52 +0000 | [diff] [blame] | 2825 | if (supportsCOMDAT()) |
| 2826 | ResolverFunc->setComdat( |
| 2827 | getModule().getOrInsertComdat(ResolverFunc->getName())); |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2828 | |
Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 2829 | llvm::stable_sort( |
| 2830 | Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS, |
| 2831 | const CodeGenFunction::MultiVersionResolverOption &RHS) { |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2832 | return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS); |
| 2833 | }); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2834 | CodeGenFunction CGF(*this); |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2835 | CGF.EmitMultiVersionResolver(ResolverFunc, Options); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2836 | } |
| 2837 | } |
| 2838 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2839 | void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) { |
| 2840 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
| 2841 | assert(FD && "Not a FunctionDecl?"); |
| 2842 | const auto *DD = FD->getAttr<CPUDispatchAttr>(); |
| 2843 | assert(DD && "Not a cpu_dispatch Function?"); |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 2844 | llvm::Type *DeclTy = getTypes().ConvertType(FD->getType()); |
Erich Keane | 9e94c18 | 2018-11-01 15:11:41 +0000 | [diff] [blame] | 2845 | |
| 2846 | if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { |
| 2847 | const CGFunctionInfo &FInfo = getTypes().arrangeCXXMethodDeclaration(CXXFD); |
| 2848 | DeclTy = getTypes().GetFunctionType(FInfo); |
| 2849 | } |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2850 | |
| 2851 | StringRef ResolverName = getMangledName(GD); |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2852 | |
| 2853 | llvm::Type *ResolverType; |
| 2854 | GlobalDecl ResolverGD; |
| 2855 | if (getTarget().supportsIFunc()) |
| 2856 | ResolverType = llvm::FunctionType::get( |
| 2857 | llvm::PointerType::get(DeclTy, |
| 2858 | Context.getTargetAddressSpace(FD->getType())), |
| 2859 | false); |
| 2860 | else { |
| 2861 | ResolverType = DeclTy; |
| 2862 | ResolverGD = GD; |
| 2863 | } |
| 2864 | |
| 2865 | auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction( |
| 2866 | ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false)); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2867 | |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2868 | SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2869 | const TargetInfo &Target = getTarget(); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2870 | unsigned Index = 0; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2871 | for (const IdentifierInfo *II : DD->cpus()) { |
| 2872 | // Get the name of the target function so we can look it up/create it. |
| 2873 | std::string MangledName = getMangledNameImpl(*this, GD, FD, true) + |
| 2874 | getCPUSpecificMangling(*this, II->getName()); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2875 | |
| 2876 | llvm::Constant *Func = GetGlobalValue(MangledName); |
| 2877 | |
Erich Keane | 248ed07 | 2018-12-07 15:31:23 +0000 | [diff] [blame] | 2878 | if (!Func) { |
| 2879 | GlobalDecl ExistingDecl = Manglings.lookup(MangledName); |
| 2880 | if (ExistingDecl.getDecl() && |
| 2881 | ExistingDecl.getDecl()->getAsFunction()->isDefined()) { |
| 2882 | EmitGlobalFunctionDefinition(ExistingDecl, nullptr); |
| 2883 | Func = GetGlobalValue(MangledName); |
| 2884 | } else { |
| 2885 | if (!ExistingDecl.getDecl()) |
| 2886 | ExistingDecl = GD.getWithMultiVersionIndex(Index); |
| 2887 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2888 | Func = GetOrCreateLLVMFunction( |
Erich Keane | 248ed07 | 2018-12-07 15:31:23 +0000 | [diff] [blame] | 2889 | MangledName, DeclTy, ExistingDecl, |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2890 | /*ForVTable=*/false, /*DontDefer=*/true, |
| 2891 | /*IsThunk=*/false, llvm::AttributeList(), ForDefinition); |
Erich Keane | 248ed07 | 2018-12-07 15:31:23 +0000 | [diff] [blame] | 2892 | } |
| 2893 | } |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2894 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2895 | llvm::SmallVector<StringRef, 32> Features; |
| 2896 | Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features); |
| 2897 | llvm::transform(Features, Features.begin(), |
| 2898 | [](StringRef Str) { return Str.substr(1); }); |
| 2899 | Features.erase(std::remove_if( |
| 2900 | Features.begin(), Features.end(), [&Target](StringRef Feat) { |
| 2901 | return !Target.validateCpuSupports(Feat); |
| 2902 | }), Features.end()); |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2903 | Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 2904 | ++Index; |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | llvm::sort( |
Fangrui Song | 1d38c13 | 2018-09-30 21:41:11 +0000 | [diff] [blame] | 2908 | Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS, |
| 2909 | const CodeGenFunction::MultiVersionResolverOption &RHS) { |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2910 | return CodeGenFunction::GetX86CpuSupportsMask(LHS.Conditions.Features) > |
| 2911 | CodeGenFunction::GetX86CpuSupportsMask(RHS.Conditions.Features); |
| 2912 | }); |
Erich Keane | 44731c5 | 2018-11-01 12:50:37 +0000 | [diff] [blame] | 2913 | |
| 2914 | // If the list contains multiple 'default' versions, such as when it contains |
| 2915 | // 'pentium' and 'generic', don't emit the call to the generic one (since we |
| 2916 | // always run on at least a 'pentium'). We do this by deleting the 'least |
| 2917 | // advanced' (read, lowest mangling letter). |
| 2918 | while (Options.size() > 1 && |
| 2919 | CodeGenFunction::GetX86CpuSupportsMask( |
| 2920 | (Options.end() - 2)->Conditions.Features) == 0) { |
| 2921 | StringRef LHSName = (Options.end() - 2)->Function->getName(); |
| 2922 | StringRef RHSName = (Options.end() - 1)->Function->getName(); |
| 2923 | if (LHSName.compare(RHSName) < 0) |
| 2924 | Options.erase(Options.end() - 2); |
| 2925 | else |
| 2926 | Options.erase(Options.end() - 1); |
| 2927 | } |
| 2928 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2929 | CodeGenFunction CGF(*this); |
Erich Keane | f353ae1 | 2018-09-13 16:58:24 +0000 | [diff] [blame] | 2930 | CGF.EmitMultiVersionResolver(ResolverFunc, Options); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2933 | /// If a dispatcher for the specified mangled name is not in the module, create |
| 2934 | /// and return an llvm Function with the specified type. |
| 2935 | llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver( |
| 2936 | GlobalDecl GD, llvm::Type *DeclTy, const FunctionDecl *FD) { |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2937 | std::string MangledName = |
| 2938 | getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true); |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2939 | |
| 2940 | // Holds the name of the resolver, in ifunc mode this is the ifunc (which has |
| 2941 | // a separate resolver). |
| 2942 | std::string ResolverName = MangledName; |
| 2943 | if (getTarget().supportsIFunc()) |
| 2944 | ResolverName += ".ifunc"; |
| 2945 | else if (FD->isTargetMultiVersion()) |
| 2946 | ResolverName += ".resolver"; |
| 2947 | |
| 2948 | // If this already exists, just return that one. |
| 2949 | if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName)) |
| 2950 | return ResolverGV; |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2951 | |
| 2952 | // Since this is the first time we've created this IFunc, make sure |
| 2953 | // that we put this multiversioned function into the list to be |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2954 | // replaced later if necessary (target multiversioning only). |
| 2955 | if (!FD->isCPUDispatchMultiVersion() && !FD->isCPUSpecificMultiVersion()) |
| 2956 | MultiVersionFuncs.push_back(GD); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2957 | |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2958 | if (getTarget().supportsIFunc()) { |
| 2959 | llvm::Type *ResolverType = llvm::FunctionType::get( |
| 2960 | llvm::PointerType::get( |
| 2961 | DeclTy, getContext().getTargetAddressSpace(FD->getType())), |
| 2962 | false); |
| 2963 | llvm::Constant *Resolver = GetOrCreateLLVMFunction( |
| 2964 | MangledName + ".resolver", ResolverType, GlobalDecl{}, |
| 2965 | /*ForVTable=*/false); |
| 2966 | llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create( |
| 2967 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Resolver, &getModule()); |
| 2968 | GIF->setName(ResolverName); |
| 2969 | SetCommonAttributes(FD, GIF); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2970 | |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2971 | return GIF; |
| 2972 | } |
| 2973 | |
| 2974 | llvm::Constant *Resolver = GetOrCreateLLVMFunction( |
| 2975 | ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false); |
| 2976 | assert(isa<llvm::GlobalValue>(Resolver) && |
| 2977 | "Resolver should be created for the first time"); |
| 2978 | SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver)); |
| 2979 | return Resolver; |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2982 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 2983 | /// module, create and return an llvm Function with the specified type. If there |
| 2984 | /// is something in the module with the specified name, return it potentially |
| 2985 | /// bitcasted to the right type. |
| 2986 | /// |
| 2987 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 2988 | /// to set the attributes on the function when it is first created. |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 2989 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction( |
| 2990 | StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable, |
| 2991 | bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs, |
| 2992 | ForDefinition_t IsForDefinition) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 2993 | const Decl *D = GD.getDecl(); |
| 2994 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2995 | // Any attempts to use a MultiVersion function should result in retrieving |
| 2996 | // the iFunc instead. Name Mangling will handle the rest of the changes. |
| 2997 | if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 2998 | // For the device mark the function as one that should be emitted. |
| 2999 | if (getLangOpts().OpenMPIsDevice && OpenMPRuntime && |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 3000 | !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() && |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 3001 | !DontDefer && !IsForDefinition) { |
Alexey Bataev | bd8ff9b | 2018-08-30 18:56:11 +0000 | [diff] [blame] | 3002 | if (const FunctionDecl *FDDef = FD->getDefinition()) { |
| 3003 | GlobalDecl GDDef; |
| 3004 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef)) |
| 3005 | GDDef = GlobalDecl(CD, GD.getCtorType()); |
| 3006 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef)) |
| 3007 | GDDef = GlobalDecl(DD, GD.getDtorType()); |
| 3008 | else |
| 3009 | GDDef = GlobalDecl(FDDef); |
| 3010 | EmitGlobal(GDDef); |
| 3011 | } |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 3012 | } |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 3013 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 3014 | if (FD->isMultiVersion()) { |
| 3015 | const auto *TA = FD->getAttr<TargetAttr>(); |
| 3016 | if (TA && TA->isDefaultVersion()) |
| 3017 | UpdateMultiVersionNames(GD, FD); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 3018 | if (!IsForDefinition) |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 3019 | return GetOrCreateMultiVersionResolver(GD, Ty, FD); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3023 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3024 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3025 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 3026 | if (WeakRefReferences.erase(Entry)) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 3027 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 3028 | if (FD && !FD->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 3029 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 3032 | // Handle dropped DLL attributes. |
Rafael Espindola | c9643d8 | 2018-03-29 16:45:18 +0000 | [diff] [blame] | 3033 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) { |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 3034 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
Rafael Espindola | c9643d8 | 2018-03-29 16:45:18 +0000 | [diff] [blame] | 3035 | setDSOLocal(Entry); |
| 3036 | } |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 3037 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3038 | // If there are two attempts to define the same mangled name, issue an |
| 3039 | // error. |
| 3040 | if (IsForDefinition && !Entry->isDeclaration()) { |
| 3041 | GlobalDecl OtherGD; |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3042 | // Check that GD is not yet in DiagnosedConflictingDefinitions is required |
| 3043 | // to make sure that we issue an error only once. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3044 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
| 3045 | (GD.getCanonicalDecl().getDecl() != |
| 3046 | OtherGD.getCanonicalDecl().getDecl()) && |
| 3047 | DiagnosedConflictingDefinitions.insert(GD).second) { |
Richard Smith | b534510 | 2018-05-30 01:52:16 +0000 | [diff] [blame] | 3048 | getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) |
| 3049 | << MangledName; |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3050 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
| 3051 | diag::note_previous_definition); |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) && |
| 3056 | (Entry->getType()->getElementType() == Ty)) { |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3057 | return Entry; |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3058 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3060 | // Make sure the result is of the correct type. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3061 | // (If function is requested for a definition, we always need to create a new |
| 3062 | // function, not just return a bitcast.) |
| 3063 | if (!IsForDefinition) |
| 3064 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3065 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 3067 | // This function doesn't have a complete type (for example, the return |
| 3068 | // type is an incomplete struct). Use a fake type instead, and make |
| 3069 | // sure not to try to set attributes. |
| 3070 | bool IsIncompleteFunction = false; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3072 | llvm::FunctionType *FTy; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 3073 | if (isa<llvm::FunctionType>(Ty)) { |
| 3074 | FTy = cast<llvm::FunctionType>(Ty); |
| 3075 | } else { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 3076 | FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 3077 | IsIncompleteFunction = true; |
| 3078 | } |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3079 | |
| 3080 | llvm::Function *F = |
| 3081 | llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, |
| 3082 | Entry ? StringRef() : MangledName, &getModule()); |
| 3083 | |
| 3084 | // If we already created a function with the same mangled name (but different |
| 3085 | // type) before, take its name and add it to the list of functions to be |
| 3086 | // replaced with F at the end of CodeGen. |
| 3087 | // |
| 3088 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 3089 | // then a definition of a different type (e.g. "int f(int x)"). |
| 3090 | if (Entry) { |
| 3091 | F->takeName(Entry); |
| 3092 | |
| 3093 | // This might be an implementation of a function without a prototype, in |
| 3094 | // which case, try to do special replacement of calls which match the new |
| 3095 | // prototype. The really key thing here is that we also potentially drop |
| 3096 | // arguments from the call site so as to make a direct call, which makes the |
| 3097 | // inliner happier and suppresses a number of optimizer warnings (!) about |
| 3098 | // dropping arguments. |
| 3099 | if (!Entry->use_empty()) { |
| 3100 | ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F); |
| 3101 | Entry->removeDeadConstantUsers(); |
| 3102 | } |
| 3103 | |
| 3104 | llvm::Constant *BC = llvm::ConstantExpr::getBitCast( |
| 3105 | F, Entry->getType()->getElementType()->getPointerTo()); |
| 3106 | addGlobalValReplacement(Entry, BC); |
| 3107 | } |
| 3108 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3109 | assert(F->getName() == MangledName && "name was uniqued!"); |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 3110 | if (D) |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 3111 | SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk); |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 3112 | if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) { |
| 3113 | llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 3114 | F->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 3115 | } |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 3116 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3117 | if (!DontDefer) { |
| 3118 | // All MSVC dtors other than the base dtor are linkonce_odr and delegate to |
| 3119 | // each other bottoming out with the base dtor. Therefore we emit non-base |
| 3120 | // dtors on usage, even if there is no dtor definition in the TU. |
| 3121 | if (D && isa<CXXDestructorDecl>(D) && |
| 3122 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
| 3123 | GD.getDtorType())) |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 3124 | addDeferredDeclToEmit(GD); |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 3125 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3126 | // This is the first use or definition of a mangled name. If there is a |
| 3127 | // deferred decl with this name, remember that we need to emit it at the end |
| 3128 | // of the file. |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 3129 | auto DDI = DeferredDecls.find(MangledName); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3130 | if (DDI != DeferredDecls.end()) { |
| 3131 | // Move the potentially referenced deferred decl to the |
| 3132 | // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we |
| 3133 | // don't need it anymore). |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 3134 | addDeferredDeclToEmit(DDI->second); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3135 | DeferredDecls.erase(DDI); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3136 | |
| 3137 | // Otherwise, there are cases we have to worry about where we're |
| 3138 | // using a declaration for which we must emit a definition but where |
| 3139 | // we might not find a top-level definition: |
| 3140 | // - member functions defined inline in their classes |
| 3141 | // - friend functions defined inline in some class |
| 3142 | // - special member functions with implicit definitions |
| 3143 | // If we ever change our AST traversal to walk into class methods, |
| 3144 | // this will be unnecessary. |
| 3145 | // |
| 3146 | // 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] | 3147 | // entry in a vtable, unless it's already marked as used. |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3148 | } else if (getLangOpts().CPlusPlus && D) { |
| 3149 | // Look for a declaration that's lexically in a record. |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 3150 | for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD; |
| 3151 | FD = FD->getPreviousDecl()) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3152 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 3153 | if (FD->doesThisDeclarationHaveABody()) { |
Sanjoy Das | 70a6051 | 2017-05-01 06:12:13 +0000 | [diff] [blame] | 3154 | addDeferredDeclToEmit(GD.getWithDecl(FD)); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3155 | break; |
| 3156 | } |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 3157 | } |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 3158 | } |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3159 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 3160 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 3162 | // Make sure the result is of the requested type. |
| 3163 | if (!IsIncompleteFunction) { |
| 3164 | assert(F->getType()->getElementType() == Ty); |
| 3165 | return F; |
| 3166 | } |
| 3167 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3168 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 3169 | return llvm::ConstantExpr::getBitCast(F, PTy); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3172 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 3173 | /// non-null, then this function will use the specified type if it has to |
| 3174 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 3175 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3176 | llvm::Type *Ty, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3177 | bool ForVTable, |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3178 | bool DontDefer, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3179 | ForDefinition_t IsForDefinition) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3180 | // If there was no specific requested type, just convert it now. |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3181 | if (!Ty) { |
| 3182 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 3183 | Ty = getTypes().ConvertType(FD->getType()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3184 | } |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3185 | |
Reid Kleckner | 2810325 | 2018-03-16 22:20:57 +0000 | [diff] [blame] | 3186 | // Devirtualized destructor calls may come through here instead of via |
| 3187 | // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead |
| 3188 | // of the complete destructor when necessary. |
| 3189 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) { |
| 3190 | if (getTarget().getCXXABI().isMicrosoft() && |
| 3191 | GD.getDtorType() == Dtor_Complete && |
| 3192 | DD->getParent()->getNumVBases() == 0) |
| 3193 | GD = GlobalDecl(DD, Dtor_Base); |
| 3194 | } |
| 3195 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3196 | StringRef MangledName = getMangledName(GD); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3197 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 3198 | /*IsThunk=*/false, llvm::AttributeList(), |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3199 | IsForDefinition); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3200 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3201 | |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3202 | static const FunctionDecl * |
| 3203 | GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { |
| 3204 | TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
| 3205 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 3206 | |
| 3207 | IdentifierInfo &CII = C.Idents.get(Name); |
| 3208 | for (const auto &Result : DC->lookup(&CII)) |
| 3209 | if (const auto FD = dyn_cast<FunctionDecl>(Result)) |
| 3210 | return FD; |
| 3211 | |
| 3212 | if (!C.getLangOpts().CPlusPlus) |
| 3213 | return nullptr; |
| 3214 | |
| 3215 | // Demangle the premangled name from getTerminateFn() |
| 3216 | IdentifierInfo &CXXII = |
Reid Kleckner | fb93154 | 2018-03-16 20:36:49 +0000 | [diff] [blame] | 3217 | (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ") |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3218 | ? C.Idents.get("terminate") |
| 3219 | : C.Idents.get(Name); |
| 3220 | |
| 3221 | for (const auto &N : {"__cxxabiv1", "std"}) { |
| 3222 | IdentifierInfo &NS = C.Idents.get(N); |
| 3223 | for (const auto &Result : DC->lookup(&NS)) { |
| 3224 | NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result); |
| 3225 | if (auto LSD = dyn_cast<LinkageSpecDecl>(Result)) |
| 3226 | for (const auto &Result : LSD->lookup(&NS)) |
| 3227 | if ((ND = dyn_cast<NamespaceDecl>(Result))) |
| 3228 | break; |
| 3229 | |
| 3230 | if (ND) |
| 3231 | for (const auto &Result : ND->lookup(&CXXII)) |
| 3232 | if (const auto *FD = dyn_cast<FunctionDecl>(Result)) |
| 3233 | return FD; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | return nullptr; |
| 3238 | } |
| 3239 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3240 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 3241 | /// type and name. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3242 | llvm::FunctionCallee |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3243 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 3244 | llvm::AttributeList ExtraAttrs, |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3245 | bool Local) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 3246 | llvm::Constant *C = |
| 3247 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3248 | /*DontDefer=*/false, /*IsThunk=*/false, |
| 3249 | ExtraAttrs); |
| 3250 | |
| 3251 | if (auto *F = dyn_cast<llvm::Function>(C)) { |
| 3252 | if (F->empty()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3253 | F->setCallingConv(getRuntimeCC()); |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3254 | |
Reid Kleckner | 1808c02 | 2019-04-29 23:05:47 +0000 | [diff] [blame] | 3255 | // In Windows Itanium environments, try to mark runtime functions |
| 3256 | // dllimport. For Mingw and MSVC, don't. We don't really know if the user |
| 3257 | // will link their standard library statically or dynamically. Marking |
| 3258 | // functions imported when they are not imported can cause linker errors |
| 3259 | // and warnings. |
| 3260 | if (!Local && getTriple().isWindowsItaniumEnvironment() && |
| 3261 | !getCodeGenOpts().LTOVisibilityPublicStd) { |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3262 | const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); |
| 3263 | if (!FD || FD->hasAttr<DLLImportAttr>()) { |
| 3264 | F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 3265 | F->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 3266 | } |
| 3267 | } |
Rafael Espindola | 3c9be62 | 2018-03-20 20:27:30 +0000 | [diff] [blame] | 3268 | setDSOLocal(F); |
Saleem Abdulrasool | 6cb0744 | 2016-12-15 06:59:05 +0000 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3272 | return {FTy, C}; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3275 | /// isTypeConstant - Determine whether an object of this type can be emitted |
| 3276 | /// as a constant. |
| 3277 | /// |
| 3278 | /// If ExcludeCtor is true, the duration when the object's constructor runs |
| 3279 | /// will not be considered. The caller will need to verify that the object is |
| 3280 | /// not written to during its construction. |
| 3281 | bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) { |
| 3282 | if (!Ty.isConstant(Context) && !Ty->isReferenceType()) |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 3283 | return false; |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3284 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3285 | if (Context.getLangOpts().CPlusPlus) { |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3286 | if (const CXXRecordDecl *Record |
| 3287 | = Context.getBaseElementType(Ty)->getAsCXXRecordDecl()) |
| 3288 | return ExcludeCtor && !Record->hasMutableFields() && |
| 3289 | Record->hasTrivialDestructor(); |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 3290 | } |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3291 | |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 3292 | return true; |
| 3293 | } |
| 3294 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3295 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 3296 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 3297 | /// is something in the module with the specified name, return it potentially |
| 3298 | /// bitcasted to the right type. |
| 3299 | /// |
| 3300 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 3301 | /// to set the attributes on the global when it is first created. |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3302 | /// |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3303 | /// If IsForDefinition is true, it is guaranteed that an actual global with |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3304 | /// type Ty will be returned, not conversion of a variable with the same |
| 3305 | /// mangled name but some other type. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3306 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3307 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3308 | llvm::PointerType *Ty, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3309 | const VarDecl *D, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3310 | ForDefinition_t IsForDefinition) { |
Daniel Dunbar | 829e988 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 3311 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3312 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 3313 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 3314 | if (WeakRefReferences.erase(Entry)) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 3315 | if (D && !D->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 3316 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Hans Wennborg | 0a20f54 | 2014-08-29 00:16:06 +0000 | [diff] [blame] | 3319 | // Handle dropped DLL attributes. |
| 3320 | if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) |
| 3321 | Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 3322 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3323 | if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D) |
| 3324 | getOpenMPRuntime().registerTargetGlobalVariable(D, Entry); |
| 3325 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3326 | if (Entry->getType() == Ty) |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3327 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3329 | // If there are two attempts to define the same mangled name, issue an |
| 3330 | // error. |
| 3331 | if (IsForDefinition && !Entry->isDeclaration()) { |
| 3332 | GlobalDecl OtherGD; |
| 3333 | const VarDecl *OtherD; |
| 3334 | |
| 3335 | // Check that D is not yet in DiagnosedConflictingDefinitions is required |
| 3336 | // to make sure that we issue an error only once. |
Artem Belevich | 2c323a0 | 2016-05-19 18:00:18 +0000 | [diff] [blame] | 3337 | if (D && lookupRepresentativeDecl(MangledName, OtherGD) && |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3338 | (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) && |
| 3339 | (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) && |
| 3340 | OtherD->hasInit() && |
| 3341 | DiagnosedConflictingDefinitions.insert(D).second) { |
Richard Smith | b534510 | 2018-05-30 01:52:16 +0000 | [diff] [blame] | 3342 | getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) |
| 3343 | << MangledName; |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3344 | getDiags().Report(OtherGD.getDecl()->getLocation(), |
| 3345 | diag::note_previous_definition); |
| 3346 | } |
| 3347 | } |
| 3348 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 3349 | // Make sure the result is of the correct type. |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 3350 | if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) |
| 3351 | return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); |
| 3352 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3353 | // (If global is requested for a definition, we always need to create a new |
| 3354 | // global, not just return a bitcast.) |
| 3355 | if (!IsForDefinition) |
| 3356 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 3357 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3359 | auto AddrSpace = GetGlobalVarAddressSpace(D); |
| 3360 | auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace); |
| 3361 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3362 | auto *GV = new llvm::GlobalVariable( |
| 3363 | getModule(), Ty->getElementType(), false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3364 | llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3365 | llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 3366 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3367 | // If we already created a global with the same mangled name (but different |
| 3368 | // type) before, take its name and remove it from its parent. |
| 3369 | if (Entry) { |
| 3370 | GV->takeName(Entry); |
| 3371 | |
| 3372 | if (!Entry->use_empty()) { |
| 3373 | llvm::Constant *NewPtrForOldDecl = |
| 3374 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
| 3375 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 3376 | } |
| 3377 | |
| 3378 | Entry->eraseFromParent(); |
| 3379 | } |
| 3380 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 3381 | // This is the first use or definition of a mangled name. If there is a |
| 3382 | // deferred decl with this name, remember that we need to emit it at the end |
| 3383 | // of the file. |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 3384 | auto DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 3385 | if (DDI != DeferredDecls.end()) { |
| 3386 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 3387 | // 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] | 3388 | addDeferredDeclToEmit(DDI->second); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 3389 | DeferredDecls.erase(DDI); |
| 3390 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 3392 | // Handle things which are present even on external declarations. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3393 | if (D) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3394 | if (LangOpts.OpenMP && !LangOpts.OpenMPSimd) |
| 3395 | getOpenMPRuntime().registerTargetGlobalVariable(D, GV); |
| 3396 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3397 | // FIXME: This code is overly simple and should be merged with other global |
| 3398 | // handling. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3399 | GV->setConstant(isTypeConstant(D->getType(), false)); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 3400 | |
Ulrich Weigand | 4608438 | 2015-04-21 17:27:59 +0000 | [diff] [blame] | 3401 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
| 3402 | |
Jake Ehrlich | ba874ad | 2017-11-29 00:54:20 +0000 | [diff] [blame] | 3403 | setLinkageForGV(GV, D); |
Eli Friedman | 4f85674 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 3404 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 3405 | if (D->getTLSKind()) { |
Richard Smith | 1847baa | 2013-04-22 08:06:17 +0000 | [diff] [blame] | 3406 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
Richard Smith | 5a99c49 | 2015-12-01 01:10:48 +0000 | [diff] [blame] | 3407 | CXXThreadLocals.push_back(D); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 3408 | setTLSMode(GV, *D); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 3409 | } |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 3410 | |
Rafael Espindola | 3dd4981 | 2018-02-23 00:22:15 +0000 | [diff] [blame] | 3411 | setGVProperties(GV, D); |
| 3412 | |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 3413 | // If required by the ABI, treat declarations of static data members with |
| 3414 | // inline initializers as definitions. |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 3415 | if (getContext().isMSStaticDataMemberInlineDefinition(D)) { |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 3416 | EmitGlobalVarDefinition(D); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 3417 | } |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 3418 | |
Erich Keane | 99fdfb6 | 2017-09-26 23:42:34 +0000 | [diff] [blame] | 3419 | // Emit section information for extern variables. |
| 3420 | if (D->hasExternalStorage()) { |
| 3421 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 3422 | GV->setSection(SA->getName()); |
| 3423 | } |
| 3424 | |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 3425 | // Handle XCore specific ABI requirements. |
Saleem Abdulrasool | 7246dcc | 2016-09-14 15:17:46 +0000 | [diff] [blame] | 3426 | if (getTriple().getArch() == llvm::Triple::xcore && |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 3427 | D->getLanguageLinkage() == CLanguageLinkage && |
| 3428 | D->getType().isConstant(Context) && |
| 3429 | isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) |
| 3430 | GV->setSection(".cp.rodata"); |
Mehdi Amini | 7cb1b30 | 2017-09-05 03:58:35 +0000 | [diff] [blame] | 3431 | |
| 3432 | // Check if we a have a const declaration with an initializer, we may be |
| 3433 | // able to emit it as available_externally to expose it's value to the |
| 3434 | // optimizer. |
| 3435 | if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() && |
| 3436 | D->getType().isConstQualified() && !GV->hasInitializer() && |
| 3437 | !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) { |
| 3438 | const auto *Record = |
| 3439 | Context.getBaseElementType(D->getType())->getAsCXXRecordDecl(); |
| 3440 | bool HasMutableFields = Record && Record->hasMutableFields(); |
| 3441 | if (!HasMutableFields) { |
| 3442 | const VarDecl *InitDecl; |
| 3443 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
| 3444 | if (InitExpr) { |
| 3445 | ConstantEmitter emitter(*this); |
| 3446 | llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl); |
| 3447 | if (Init) { |
| 3448 | auto *InitType = Init->getType(); |
| 3449 | if (GV->getType()->getElementType() != InitType) { |
| 3450 | // The type of the initializer does not match the definition. |
| 3451 | // This happens when an initializer has a different type from |
| 3452 | // the type of the global (because of padding at the end of a |
| 3453 | // structure for instance). |
| 3454 | GV->setName(StringRef()); |
| 3455 | // Make a new global with the correct type, this is now guaranteed |
| 3456 | // to work. |
| 3457 | auto *NewGV = cast<llvm::GlobalVariable>( |
| 3458 | GetAddrOfGlobalVar(D, InitType, IsForDefinition)); |
| 3459 | |
| 3460 | // Erase the old global, since it is no longer used. |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3461 | GV->eraseFromParent(); |
Mehdi Amini | 7cb1b30 | 2017-09-05 03:58:35 +0000 | [diff] [blame] | 3462 | GV = NewGV; |
| 3463 | } else { |
| 3464 | GV->setInitializer(Init); |
| 3465 | GV->setConstant(true); |
| 3466 | GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 3467 | } |
| 3468 | emitter.finalize(GV); |
| 3469 | } |
| 3470 | } |
| 3471 | } |
| 3472 | } |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3475 | LangAS ExpectedAS = |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3476 | D ? D->getType().getAddressSpace() |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3477 | : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); |
Benjamin Kramer | 6fbfdec | 2017-07-08 14:14:57 +0000 | [diff] [blame] | 3478 | assert(getContext().getTargetAddressSpace(ExpectedAS) == |
| 3479 | Ty->getPointerAddressSpace()); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3480 | if (AddrSpace != ExpectedAS) |
| 3481 | return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace, |
| 3482 | ExpectedAS, Ty); |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 3483 | |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 3484 | if (GV->isDeclaration()) |
| 3485 | getTargetCodeGenInfo().setTargetAttributes(D, GV, *this); |
| 3486 | |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 3487 | return GV; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3490 | llvm::Constant * |
| 3491 | CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3492 | ForDefinition_t IsForDefinition) { |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 3493 | const Decl *D = GD.getDecl(); |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 3494 | if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) |
| 3495 | return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr, |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3496 | /*DontDefer=*/false, IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 3497 | else if (isa<CXXMethodDecl>(D)) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3498 | auto FInfo = &getTypes().arrangeCXXMethodDeclaration( |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 3499 | cast<CXXMethodDecl>(D)); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3500 | auto Ty = getTypes().GetFunctionType(*FInfo); |
| 3501 | return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, |
| 3502 | IsForDefinition); |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 3503 | } else if (isa<FunctionDecl>(D)) { |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3504 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 3505 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
| 3506 | return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, |
| 3507 | IsForDefinition); |
| 3508 | } else |
Yaron Keren | 1c4bbc9 | 2016-12-24 15:32:39 +0000 | [diff] [blame] | 3509 | return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3510 | IsForDefinition); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3511 | } |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3512 | |
David Green | be0c5b6 | 2018-09-12 14:09:06 +0000 | [diff] [blame] | 3513 | llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable( |
| 3514 | StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, |
| 3515 | unsigned Alignment) { |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3516 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3517 | llvm::GlobalVariable *OldGV = nullptr; |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3518 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3519 | if (GV) { |
| 3520 | // Check if the variable has the right type. |
| 3521 | if (GV->getType()->getElementType() == Ty) |
| 3522 | return GV; |
| 3523 | |
| 3524 | // Because C++ name mangling, the only way we can end up with an already |
| 3525 | // 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] | 3526 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3527 | OldGV = GV; |
| 3528 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3529 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3530 | // Create a new variable. |
| 3531 | GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3532 | Linkage, nullptr, Name); |
| 3533 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3534 | if (OldGV) { |
| 3535 | // Replace occurrences of the old variable if needed. |
| 3536 | GV->takeName(OldGV); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3537 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3538 | if (!OldGV->use_empty()) { |
| 3539 | llvm::Constant *NewPtrForOldDecl = |
| 3540 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 3541 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 3542 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 3543 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3544 | OldGV->eraseFromParent(); |
| 3545 | } |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 3546 | |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 3547 | if (supportsCOMDAT() && GV->isWeakForLinker() && |
| 3548 | !GV->hasAvailableExternallyLinkage()) |
| 3549 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 3550 | |
David Green | be0c5b6 | 2018-09-12 14:09:06 +0000 | [diff] [blame] | 3551 | GV->setAlignment(Alignment); |
| 3552 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 3553 | return GV; |
| 3554 | } |
| 3555 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3556 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 3557 | /// 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] | 3558 | /// 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] | 3559 | /// normal requested type would be. If IsForDefinition is true, it is guaranteed |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3560 | /// that an actual global with type Ty will be returned, not conversion of a |
| 3561 | /// variable with the same mangled name but some other type. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3562 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3563 | llvm::Type *Ty, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3564 | ForDefinition_t IsForDefinition) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3565 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 3566 | QualType ASTTy = D->getType(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3567 | if (!Ty) |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3568 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3569 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3570 | llvm::PointerType *PTy = |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3571 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3572 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3573 | StringRef MangledName = getMangledName(D); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3574 | return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 3578 | /// specified type and name. |
| 3579 | llvm::Constant * |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3580 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3581 | StringRef Name) { |
Anastasia Stulova | 960ff08 | 2019-07-15 11:58:10 +0000 | [diff] [blame] | 3582 | auto PtrTy = |
| 3583 | getContext().getLangOpts().OpenCL |
| 3584 | ? llvm::PointerType::get( |
| 3585 | Ty, getContext().getTargetAddressSpace(LangAS::opencl_global)) |
| 3586 | : llvm::PointerType::getUnqual(Ty); |
| 3587 | auto *Ret = GetOrCreateLLVMGlobal(Name, PtrTy, nullptr); |
Rafael Espindola | 6ab4ae4 | 2018-03-21 01:30:16 +0000 | [diff] [blame] | 3588 | setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts())); |
| 3589 | return Ret; |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3592 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 3593 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 3594 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3595 | StringRef MangledName = getMangledName(D); |
| 3596 | llvm::GlobalValue *GV = GetGlobalValue(MangledName); |
| 3597 | |
| 3598 | // We already have a definition, not declaration, with the same mangled name. |
| 3599 | // Emitting of declaration is not required (and actually overwrites emitted |
| 3600 | // definition). |
| 3601 | if (GV && !GV->isDeclaration()) |
| 3602 | return; |
| 3603 | |
| 3604 | // If we have not seen a reference to this variable yet, place it into the |
| 3605 | // deferred declarations table to be emitted if needed later. |
| 3606 | if (!MustBeEmitted(D) && !GV) { |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3607 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3608 | return; |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | // The tentative definition is the only definition. |
Reid Kleckner | 953fe03 | 2015-12-05 01:52:14 +0000 | [diff] [blame] | 3612 | EmitGlobalVarDefinition(D); |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3615 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 3616 | return Context.toCharUnitsFromBits( |
| 3617 | getDataLayout().getTypeStoreSizeInBits(Ty)); |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3620 | LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { |
| 3621 | LangAS AddrSpace = LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3622 | if (LangOpts.OpenCL) { |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 3623 | AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3624 | assert(AddrSpace == LangAS::opencl_global || |
| 3625 | AddrSpace == LangAS::opencl_constant || |
| 3626 | AddrSpace == LangAS::opencl_local || |
| 3627 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 3628 | return AddrSpace; |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3631 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) { |
| 3632 | if (D && D->hasAttr<CUDAConstantAttr>()) |
| 3633 | return LangAS::cuda_constant; |
| 3634 | else if (D && D->hasAttr<CUDASharedAttr>()) |
| 3635 | return LangAS::cuda_shared; |
Yaxun Liu | a4005e1 | 2018-07-28 03:05:25 +0000 | [diff] [blame] | 3636 | else if (D && D->hasAttr<CUDADeviceAttr>()) |
| 3637 | return LangAS::cuda_device; |
| 3638 | else if (D && D->getType().isConstQualified()) |
| 3639 | return LangAS::cuda_constant; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3640 | else |
| 3641 | return LangAS::cuda_device; |
| 3642 | } |
| 3643 | |
Alexey Bataev | c568725 | 2019-03-21 19:35:27 +0000 | [diff] [blame] | 3644 | if (LangOpts.OpenMP) { |
| 3645 | LangAS AS; |
| 3646 | if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS)) |
| 3647 | return AS; |
| 3648 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3649 | return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Yaxun Liu | daceb1e | 2018-05-14 19:20:12 +0000 | [diff] [blame] | 3652 | LangAS CodeGenModule::getStringLiteralAddressSpace() const { |
| 3653 | // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. |
| 3654 | if (LangOpts.OpenCL) |
| 3655 | return LangAS::opencl_constant; |
| 3656 | if (auto AS = getTarget().getConstantAddressSpace()) |
| 3657 | return AS.getValue(); |
| 3658 | return LangAS::Default; |
| 3659 | } |
| 3660 | |
| 3661 | // In address space agnostic languages, string literals are in default address |
| 3662 | // space in AST. However, certain targets (e.g. amdgcn) request them to be |
| 3663 | // emitted in constant address space in LLVM IR. To be consistent with other |
| 3664 | // parts of AST, string literal global variables in constant address space |
| 3665 | // need to be casted to default address space before being put into address |
| 3666 | // map and referenced by other part of CodeGen. |
| 3667 | // In OpenCL, string literals are in constant address space in AST, therefore |
| 3668 | // they should not be casted to default address space. |
| 3669 | static llvm::Constant * |
| 3670 | castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, |
| 3671 | llvm::GlobalVariable *GV) { |
| 3672 | llvm::Constant *Cast = GV; |
| 3673 | if (!CGM.getLangOpts().OpenCL) { |
| 3674 | if (auto AS = CGM.getTarget().getConstantAddressSpace()) { |
| 3675 | if (AS != LangAS::Default) |
| 3676 | Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast( |
| 3677 | CGM, GV, AS.getValue(), LangAS::Default, |
| 3678 | GV->getValueType()->getPointerTo( |
| 3679 | CGM.getContext().getTargetAddressSpace(LangAS::Default))); |
| 3680 | } |
| 3681 | } |
| 3682 | return Cast; |
| 3683 | } |
| 3684 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3685 | template<typename SomeDecl> |
| 3686 | void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, |
| 3687 | llvm::GlobalValue *GV) { |
| 3688 | if (!getLangOpts().CPlusPlus) |
| 3689 | return; |
| 3690 | |
| 3691 | // Must have 'used' attribute, or else inline assembly can't rely on |
| 3692 | // the name existing. |
| 3693 | if (!D->template hasAttr<UsedAttr>()) |
| 3694 | return; |
| 3695 | |
| 3696 | // Must have internal linkage and an ordinary name. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3697 | if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3698 | return; |
| 3699 | |
| 3700 | // Must be in an extern "C" context. Entities declared directly within |
| 3701 | // 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] | 3702 | const SomeDecl *First = D->getFirstDecl(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 3703 | if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | |
| 3706 | // OK, this is an internal linkage entity inside an extern "C" linkage |
| 3707 | // specification. Make a note of that so we can give it the "expected" |
| 3708 | // mangled name if nothing else is using that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 3709 | std::pair<StaticExternCMap::iterator, bool> R = |
| 3710 | StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3711 | |
| 3712 | // If we have multiple internal linkage entities with the same name |
| 3713 | // in extern "C" regions, none of them gets that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 3714 | if (!R.second) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3715 | R.first->second = nullptr; |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3718 | static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) { |
| 3719 | if (!CGM.supportsCOMDAT()) |
| 3720 | return false; |
| 3721 | |
Konstantin Pyzhov | 15e678e | 2019-06-24 14:40:20 +0000 | [diff] [blame] | 3722 | // Do not set COMDAT attribute for CUDA/HIP stub functions to prevent |
| 3723 | // them being "merged" by the COMDAT Folding linker optimization. |
| 3724 | if (D.hasAttr<CUDAGlobalAttr>()) |
| 3725 | return false; |
| 3726 | |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3727 | if (D.hasAttr<SelectAnyAttr>()) |
| 3728 | return true; |
| 3729 | |
| 3730 | GVALinkage Linkage; |
| 3731 | if (auto *VD = dyn_cast<VarDecl>(&D)) |
| 3732 | Linkage = CGM.getContext().GetGVALinkageForVariable(VD); |
| 3733 | else |
| 3734 | Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D)); |
| 3735 | |
| 3736 | switch (Linkage) { |
| 3737 | case GVA_Internal: |
| 3738 | case GVA_AvailableExternally: |
| 3739 | case GVA_StrongExternal: |
| 3740 | return false; |
| 3741 | case GVA_DiscardableODR: |
| 3742 | case GVA_StrongODR: |
| 3743 | return true; |
| 3744 | } |
| 3745 | llvm_unreachable("No such linkage"); |
| 3746 | } |
| 3747 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3748 | void CodeGenModule::maybeSetTrivialComdat(const Decl &D, |
| 3749 | llvm::GlobalObject &GO) { |
| 3750 | if (!shouldBeInCOMDAT(*this, D)) |
| 3751 | return; |
| 3752 | GO.setComdat(TheModule.getOrInsertComdat(GO.getName())); |
| 3753 | } |
| 3754 | |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3755 | /// Pass IsTentative as true if you want to create a tentative definition. |
| 3756 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, |
| 3757 | bool IsTentative) { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3758 | // OpenCL global variables of sampler type are translated to function calls, |
| 3759 | // therefore no need to be translated. |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3760 | QualType ASTTy = D->getType(); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3761 | if (getLangOpts().OpenCL && ASTTy->isSamplerT()) |
| 3762 | return; |
| 3763 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 3764 | // If this is OpenMP device, check if it is legal to emit this global |
| 3765 | // normally. |
| 3766 | if (LangOpts.OpenMPIsDevice && OpenMPRuntime && |
| 3767 | OpenMPRuntime->emitTargetGlobalVariable(D)) |
| 3768 | return; |
| 3769 | |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 3770 | llvm::Constant *Init = nullptr; |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 3771 | CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 3772 | bool NeedsGlobalCtor = false; |
| 3773 | bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 3775 | const VarDecl *InitDecl; |
| 3776 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 3777 | |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3778 | Optional<ConstantEmitter> emitter; |
| 3779 | |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 3780 | // CUDA E.2.4.1 "__shared__ variables cannot have an initialization |
| 3781 | // as part of their declaration." Sema has already checked for |
| 3782 | // error cases, so we just need to set Init to UndefValue. |
Artem Belevich | 7b05666 | 2018-12-13 21:43:04 +0000 | [diff] [blame] | 3783 | bool IsCUDASharedVar = |
| 3784 | getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>(); |
| 3785 | // Shadows of initialized device-side global variables are also left |
| 3786 | // undefined. |
| 3787 | bool IsCUDAShadowVar = |
| 3788 | !getLangOpts().CUDAIsDevice && |
| 3789 | (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() || |
| 3790 | D->hasAttr<CUDASharedAttr>()); |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 3791 | // HIP pinned shadow of initialized host-side global variables are also |
| 3792 | // left undefined. |
| 3793 | bool IsHIPPinnedShadowVar = |
| 3794 | getLangOpts().CUDAIsDevice && D->hasAttr<HIPPinnedShadowAttr>(); |
| 3795 | if (getLangOpts().CUDA && |
| 3796 | (IsCUDASharedVar || IsCUDAShadowVar || IsHIPPinnedShadowVar)) |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 3797 | Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy)); |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 3798 | else if (!InitExpr) { |
Eli Friedman | 6859a1b | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 3799 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 3800 | // implicitly initialized with { 0 }. |
| 3801 | // |
| 3802 | // Note that tentative definitions are only emitted at the end of |
| 3803 | // a translation unit, so they should never have incomplete |
| 3804 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 3805 | // never attempt to emit a tentative definition if a real one |
| 3806 | // exists. A use may still exists, however, so we still may need |
| 3807 | // to do a RAUW. |
| 3808 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | f18318c | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 3809 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3810 | } else { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3811 | initializedGlobalDecl = GlobalDecl(D); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3812 | emitter.emplace(*this); |
| 3813 | Init = emitter->tryEmitForInitializer(*InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 3814 | |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 3815 | if (!Init) { |
Anders Carlsson | ca4a545 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 3816 | QualType T = InitExpr->getType(); |
Douglas Gregor | d450f06 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 3817 | if (D->getType()->isReferenceType()) |
| 3818 | T = D->getType(); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 3819 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3820 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 3821 | Init = EmitNullConstant(T); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 3822 | NeedsGlobalCtor = true; |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 3823 | } else { |
| 3824 | ErrorUnsupported(D, "static initializer"); |
| 3825 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 3826 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 3827 | } else { |
| 3828 | // 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] | 3829 | // initializer position (just in case this entry was delayed) if we |
| 3830 | // also don't need to register a destructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3831 | if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 3832 | DelayedCXXInitPosition.erase(D); |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 3833 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3834 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3835 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3836 | llvm::Type* InitType = Init->getType(); |
Andrey Bokhanko | 7aa88ce | 2016-01-14 10:41:16 +0000 | [diff] [blame] | 3837 | llvm::Constant *Entry = |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3838 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3840 | // Strip off a bitcast if we got one back. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3841 | if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 3842 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 3843 | CE->getOpcode() == llvm::Instruction::AddrSpaceCast || |
| 3844 | // All zero index gep. |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 3845 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3846 | Entry = CE->getOperand(0); |
| 3847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3849 | // Entry is now either a Function or GlobalVariable. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3850 | auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3852 | // We have a definition after a declaration with the wrong type. |
| 3853 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 3854 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 3855 | // (which will be a definition). |
| 3856 | // |
| 3857 | // This happens if there is a prototype for a global (e.g. |
| 3858 | // "extern int x[];") and then a definition of a different type (e.g. |
| 3859 | // "int x[10];"). This also happens when an initializer has a different type |
| 3860 | // from the type of the global (this happens with unions). |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3861 | if (!GV || GV->getType()->getElementType() != InitType || |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3862 | GV->getType()->getAddressSpace() != |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 3863 | getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 3865 | // 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] | 3866 | Entry->setName(StringRef()); |
Daniel Dunbar | b2f4cdb | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 3867 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3868 | // 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] | 3869 | GV = cast<llvm::GlobalVariable>( |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 3870 | GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 3871 | |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3872 | // Replace all uses of the old global with the new global |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3874 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3875 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 3876 | |
| 3877 | // Erase the old global, since it is no longer used. |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 3878 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 3879 | } |
Devang Patel | 19c2b9a | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 3880 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3881 | MaybeHandleStaticInExternC(D, GV); |
| 3882 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3883 | if (D->hasAttr<AnnotateAttr>()) |
| 3884 | AddGlobalAnnotations(D, GV); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 3885 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3886 | // Set the llvm linkage type as appropriate. |
| 3887 | llvm::GlobalValue::LinkageTypes Linkage = |
| 3888 | getLLVMLinkageVarDefinition(D, GV->isConstant()); |
| 3889 | |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 3890 | // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on |
| 3891 | // the device. [...]" |
| 3892 | // CUDA B.2.2 "The __constant__ qualifier, optionally used together with |
| 3893 | // __device__, declares a variable that: [...] |
| 3894 | // Is accessible from all the threads within the grid and from the host |
| 3895 | // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize() |
| 3896 | // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())." |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3897 | if (GV && LangOpts.CUDA) { |
| 3898 | if (LangOpts.CUDAIsDevice) { |
Michael Liao | 4b7a713 | 2019-05-29 17:23:27 +0000 | [diff] [blame] | 3899 | if (Linkage != llvm::GlobalValue::InternalLinkage && |
| 3900 | (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>())) |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3901 | GV->setExternallyInitialized(true); |
| 3902 | } else { |
| 3903 | // Host-side shadows of external declarations of device-side |
| 3904 | // global variables become internal definitions. These have to |
| 3905 | // be internal in order to prevent name conflicts with global |
| 3906 | // host variables with the same name in a different TUs. |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 3907 | if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || |
| 3908 | D->hasAttr<HIPPinnedShadowAttr>()) { |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3909 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 3910 | |
| 3911 | // Shadow variables and their properties must be registered |
| 3912 | // with CUDA runtime. |
| 3913 | unsigned Flags = 0; |
| 3914 | if (!D->hasDefinition()) |
| 3915 | Flags |= CGCUDARuntime::ExternDeviceVar; |
| 3916 | if (D->hasAttr<CUDAConstantAttr>()) |
| 3917 | Flags |= CGCUDARuntime::ConstantDeviceVar; |
Artem Belevich | 9953577 | 2018-12-22 01:11:09 +0000 | [diff] [blame] | 3918 | // Extern global variables will be registered in the TU where they are |
| 3919 | // defined. |
| 3920 | if (!D->hasExternalStorage()) |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 3921 | getCUDARuntime().registerDeviceVar(D, *GV, Flags); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 3922 | } else if (D->hasAttr<CUDASharedAttr>()) |
| 3923 | // __shared__ variables are odd. Shadows do get created, but |
| 3924 | // they are not registered with the CUDA runtime, so they |
| 3925 | // can't really be used to access their device-side |
| 3926 | // counterparts. It's not clear yet whether it's nvcc's bug or |
| 3927 | // a feature, but we've got to do the same for compatibility. |
| 3928 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 3929 | } |
Jingyue Wu | 284ebe2 | 2015-08-22 05:49:28 +0000 | [diff] [blame] | 3930 | } |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3931 | |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 3932 | if (!IsHIPPinnedShadowVar) |
| 3933 | GV->setInitializer(Init); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 3934 | if (emitter) emitter->finalize(GV); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 3935 | |
| 3936 | // If it is safe to mark the global 'constant', do so now. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3937 | GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && |
| 3938 | isTypeConstant(D->getType(), true)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | |
Hans Wennborg | 899ded9 | 2014-10-16 20:52:46 +0000 | [diff] [blame] | 3940 | // If it is in a read-only section, mark it 'constant'. |
| 3941 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 3942 | const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()]; |
| 3943 | if ((SI.SectionFlags & ASTContext::PSF_Write) == 0) |
| 3944 | GV->setConstant(true); |
| 3945 | } |
| 3946 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3947 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 3948 | |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 3949 | |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 3950 | // On Darwin, if the normal linkage of a C++ thread_local variable is |
| 3951 | // LinkOnce or Weak, we keep the normal linkage to prevent multiple |
| 3952 | // copies within a linkage unit; otherwise, the backing variable has |
| 3953 | // internal linkage and all accesses should just be calls to the |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 3954 | // Itanium-specified entry point, which has the normal linkage of the |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 3955 | // variable. This is to preserve the ability to change the implementation |
| 3956 | // behind the scenes. |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3957 | if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic && |
Manman Ren | f93fff2 | 2015-11-11 23:08:18 +0000 | [diff] [blame] | 3958 | Context.getTargetInfo().getTriple().isOSDarwin() && |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 3959 | !llvm::GlobalVariable::isLinkOnceLinkage(Linkage) && |
| 3960 | !llvm::GlobalVariable::isWeakLinkage(Linkage)) |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3961 | Linkage = llvm::GlobalValue::InternalLinkage; |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 3962 | |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 3963 | GV->setLinkage(Linkage); |
Nico Rieck | bb0554f | 2014-01-14 15:23:53 +0000 | [diff] [blame] | 3964 | if (D->hasAttr<DLLImportAttr>()) |
| 3965 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 3966 | else if (D->hasAttr<DLLExportAttr>()) |
| 3967 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 3968 | else |
| 3969 | GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 3970 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 3971 | if (Linkage == llvm::GlobalVariable::CommonLinkage) { |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 3972 | // common vars aren't constant even if declared const. |
| 3973 | GV->setConstant(false); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 3974 | // Tentative definition of global variables may be initialized with |
| 3975 | // non-zero null pointers. In this case they should have weak linkage |
| 3976 | // since common linkage must have zero initializer and must not have |
| 3977 | // explicit section therefore cannot have non-zero initial value. |
| 3978 | if (!GV->getInitializer()->isNullValue()) |
| 3979 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
| 3980 | } |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 3981 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 3982 | setNonAliasAttributes(D, GV); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 3983 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3984 | if (D->getTLSKind() && !GV->isThreadLocal()) { |
| 3985 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
Richard Smith | 5a99c49 | 2015-12-01 01:10:48 +0000 | [diff] [blame] | 3986 | CXXThreadLocals.push_back(D); |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 3987 | setTLSMode(GV, *D); |
| 3988 | } |
| 3989 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3990 | maybeSetTrivialComdat(*D, *GV); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 3991 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 3992 | // Emit the initializer function if necessary. |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 3993 | if (NeedsGlobalCtor || NeedsGlobalDtor) |
| 3994 | EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 3995 | |
Alexey Samsonov | 4b8de11 | 2014-08-01 21:35:28 +0000 | [diff] [blame] | 3996 | SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor); |
Kostya Serebryany | 28a26c8 | 2012-08-21 06:53:28 +0000 | [diff] [blame] | 3997 | |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 3998 | // Emit global variable debug information. |
Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 3999 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 4000 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 4001 | DI->EmitGlobalVariable(GV, D); |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 4002 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 4003 | |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 4004 | static bool isVarDeclStrongDefinition(const ASTContext &Context, |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 4005 | CodeGenModule &CGM, const VarDecl *D, |
| 4006 | bool NoCommon) { |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 4007 | // Don't give variables common linkage if -fno-common was specified unless it |
| 4008 | // was overridden by a NoCommon attribute. |
| 4009 | if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) |
| 4010 | return true; |
| 4011 | |
| 4012 | // C11 6.9.2/2: |
| 4013 | // A declaration of an identifier for an object that has file scope without |
| 4014 | // an initializer, and without a storage-class specifier or with the |
| 4015 | // storage-class specifier static, constitutes a tentative definition. |
| 4016 | if (D->getInit() || D->hasExternalStorage()) |
| 4017 | return true; |
| 4018 | |
| 4019 | // A variable cannot be both common and exist in a section. |
| 4020 | if (D->hasAttr<SectionAttr>()) |
| 4021 | return true; |
| 4022 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 4023 | // A variable cannot be both common and exist in a section. |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4024 | // 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] | 4025 | // If no specialized section name is applicable, it will resort to default. |
| 4026 | if (D->hasAttr<PragmaClangBSSSectionAttr>() || |
| 4027 | D->hasAttr<PragmaClangDataSectionAttr>() || |
| 4028 | D->hasAttr<PragmaClangRodataSectionAttr>()) |
| 4029 | return true; |
| 4030 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 4031 | // Thread local vars aren't considered common linkage. |
| 4032 | if (D->getTLSKind()) |
| 4033 | return true; |
| 4034 | |
| 4035 | // Tentative definitions marked with WeakImportAttr are true definitions. |
| 4036 | if (D->hasAttr<WeakImportAttr>()) |
| 4037 | return true; |
| 4038 | |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 4039 | // A variable cannot be both common and exist in a comdat. |
| 4040 | if (shouldBeInCOMDAT(CGM, *D)) |
| 4041 | return true; |
| 4042 | |
Sanjay Patel | 2f9303a | 2016-06-30 21:02:40 +0000 | [diff] [blame] | 4043 | // Declarations with a required alignment do not have common linkage in MSVC |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 4044 | // mode. |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 4045 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
David Majnemer | e1a0b2e | 2015-02-03 08:49:32 +0000 | [diff] [blame] | 4046 | if (D->hasAttr<AlignedAttr>()) |
| 4047 | return true; |
| 4048 | QualType VarType = D->getType(); |
| 4049 | if (Context.isAlignmentRequired(VarType)) |
| 4050 | return true; |
| 4051 | |
| 4052 | if (const auto *RT = VarType->getAs<RecordType>()) { |
| 4053 | const RecordDecl *RD = RT->getDecl(); |
| 4054 | for (const FieldDecl *FD : RD->fields()) { |
| 4055 | if (FD->isBitField()) |
| 4056 | continue; |
| 4057 | if (FD->hasAttr<AlignedAttr>()) |
| 4058 | return true; |
| 4059 | if (Context.isAlignmentRequired(FD->getType())) |
| 4060 | return true; |
| 4061 | } |
| 4062 | } |
| 4063 | } |
Shoaib Meenai | 39287e7 | 2019-01-09 20:05:16 +0000 | [diff] [blame] | 4064 | |
Erich Keane | 892e633 | 2019-02-07 15:14:11 +0000 | [diff] [blame] | 4065 | // Microsoft's link.exe doesn't support alignments greater than 32 bytes for |
| 4066 | // common symbols, so symbols with greater alignment requirements cannot be |
| 4067 | // common. |
Shoaib Meenai | 39287e7 | 2019-01-09 20:05:16 +0000 | [diff] [blame] | 4068 | // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two |
| 4069 | // alignments for common symbols via the aligncomm directive, so this |
| 4070 | // restriction only applies to MSVC environments. |
Erich Keane | 85c6224 | 2019-01-08 18:44:22 +0000 | [diff] [blame] | 4071 | if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() && |
Erich Keane | 892e633 | 2019-02-07 15:14:11 +0000 | [diff] [blame] | 4072 | Context.getTypeAlignIfKnown(D->getType()) > |
| 4073 | Context.toBits(CharUnits::fromQuantity(32))) |
Erich Keane | 85c6224 | 2019-01-08 18:44:22 +0000 | [diff] [blame] | 4074 | return true; |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 4075 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 4076 | return false; |
| 4077 | } |
| 4078 | |
Hans Wennborg | 1a3a074 | 2014-05-14 19:54:53 +0000 | [diff] [blame] | 4079 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 4080 | const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) { |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 4081 | if (Linkage == GVA_Internal) |
| 4082 | return llvm::Function::InternalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4083 | |
| 4084 | if (D->hasAttr<WeakAttr>()) { |
| 4085 | if (IsConstantVariable) |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 4086 | return llvm::GlobalVariable::WeakODRLinkage; |
| 4087 | else |
| 4088 | return llvm::GlobalVariable::WeakAnyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
Erich Keane | 7ef210d | 2018-10-22 21:20:45 +0000 | [diff] [blame] | 4091 | if (const auto *FD = D->getAsFunction()) |
| 4092 | if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally) |
| 4093 | return llvm::GlobalVariable::LinkOnceAnyLinkage; |
| 4094 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4095 | // We are guaranteed to have a strong definition somewhere else, |
| 4096 | // so we can use available_externally linkage. |
| 4097 | if (Linkage == GVA_AvailableExternally) |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 4098 | return llvm::GlobalValue::AvailableExternallyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4099 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4100 | // Note that Apple's kernel linker doesn't support symbol |
| 4101 | // coalescing, so we need to avoid linkonce and weak linkages there. |
| 4102 | // Normally, this means we just map to internal, but for explicit |
| 4103 | // instantiations we'll map to external. |
| 4104 | |
| 4105 | // In C++, the compiler has to emit a definition in every translation unit |
| 4106 | // that references the function. We should use linkonce_odr because |
| 4107 | // a) if all references in this translation unit are optimized away, we |
| 4108 | // don't need to codegen it. b) if the function persists, it needs to be |
| 4109 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 4110 | // definition is dependable. |
| 4111 | if (Linkage == GVA_DiscardableODR) |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 4112 | return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4113 | : llvm::Function::InternalLinkage; |
| 4114 | |
| 4115 | // An explicit instantiation of a template has weak linkage, since |
| 4116 | // explicit instantiations can occur in multiple translation units |
| 4117 | // and must all be equivalent. However, we are not allowed to |
| 4118 | // throw away these explicit instantiations. |
Justin Lebar | 27ee130 | 2016-06-30 18:41:33 +0000 | [diff] [blame] | 4119 | // |
| 4120 | // We don't currently support CUDA device code spread out across multiple TUs, |
| 4121 | // so say that CUDA templates are either external (for kernels) or internal. |
| 4122 | // This lets llvm perform aggressive inter-procedural optimizations. |
| 4123 | if (Linkage == GVA_StrongODR) { |
| 4124 | if (Context.getLangOpts().AppleKext) |
| 4125 | return llvm::Function::ExternalLinkage; |
| 4126 | if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice) |
| 4127 | return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage |
| 4128 | : llvm::Function::InternalLinkage; |
| 4129 | return llvm::Function::WeakODRLinkage; |
| 4130 | } |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4131 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4132 | // C++ doesn't have tentative definitions and thus cannot have common |
| 4133 | // linkage. |
| 4134 | if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 4135 | !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D), |
David Majnemer | c017d36 | 2014-08-05 00:01:13 +0000 | [diff] [blame] | 4136 | CodeGenOpts.NoCommon)) |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 4137 | return llvm::GlobalVariable::CommonLinkage; |
| 4138 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4139 | // selectany symbols are externally visible, so use weak instead of |
| 4140 | // linkonce. MSVC optimizes away references to const selectany globals, so |
| 4141 | // all definitions should be the same and ODR linkage should be used. |
| 4142 | // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx |
| 4143 | if (D->hasAttr<SelectAnyAttr>()) |
| 4144 | return llvm::GlobalVariable::WeakODRLinkage; |
| 4145 | |
| 4146 | // Otherwise, we have strong external linkage. |
| 4147 | assert(Linkage == GVA_StrongExternal); |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 4148 | return llvm::GlobalVariable::ExternalLinkage; |
| 4149 | } |
| 4150 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4151 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition( |
| 4152 | const VarDecl *VD, bool IsConstant) { |
| 4153 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame] | 4154 | return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant); |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4157 | /// Replace the uses of a function that was declared with a non-proto type. |
| 4158 | /// We want to silently drop extra arguments from call sites |
| 4159 | static void replaceUsesOfNonProtoConstant(llvm::Constant *old, |
| 4160 | llvm::Function *newFn) { |
| 4161 | // Fast path. |
| 4162 | if (old->use_empty()) return; |
| 4163 | |
| 4164 | llvm::Type *newRetTy = newFn->getReturnType(); |
| 4165 | SmallVector<llvm::Value*, 4> newArgs; |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 4166 | SmallVector<llvm::OperandBundleDef, 1> newBundles; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4167 | |
| 4168 | for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); |
| 4169 | ui != ue; ) { |
| 4170 | llvm::Value::use_iterator use = ui++; // Increment before the use is erased. |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 4171 | llvm::User *user = use->getUser(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4172 | |
| 4173 | // Recognize and replace uses of bitcasts. Most calls to |
| 4174 | // unprototyped functions will use bitcasts. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4175 | if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4176 | if (bitcast->getOpcode() == llvm::Instruction::BitCast) |
| 4177 | replaceUsesOfNonProtoConstant(bitcast, newFn); |
| 4178 | continue; |
| 4179 | } |
| 4180 | |
| 4181 | // Recognize calls to the function. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4182 | llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4183 | if (!callSite) continue; |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4184 | if (!callSite->isCallee(&*use)) |
| 4185 | continue; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4186 | |
| 4187 | // If the return types don't match exactly, then we can't |
| 4188 | // transform this call unless it's dead. |
| 4189 | if (callSite->getType() != newRetTy && !callSite->use_empty()) |
| 4190 | continue; |
| 4191 | |
| 4192 | // Get the call site's attribute list. |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 4193 | SmallVector<llvm::AttributeSet, 8> newArgAttrs; |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4194 | llvm::AttributeList oldAttrs = callSite->getAttributes(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4195 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4196 | // If the function was passed too few arguments, don't transform. |
| 4197 | unsigned newNumArgs = newFn->arg_size(); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4198 | if (callSite->arg_size() < newNumArgs) |
| 4199 | continue; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4200 | |
| 4201 | // If extra arguments were passed, we silently drop them. |
| 4202 | // If any of the types mismatch, we don't transform. |
| 4203 | unsigned argNo = 0; |
| 4204 | bool dontTransform = false; |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 4205 | for (llvm::Argument &A : newFn->args()) { |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4206 | if (callSite->getArgOperand(argNo)->getType() != A.getType()) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4207 | dontTransform = true; |
| 4208 | break; |
| 4209 | } |
| 4210 | |
| 4211 | // Add any parameter attributes. |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 4212 | newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo)); |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 4213 | argNo++; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4214 | } |
| 4215 | if (dontTransform) |
| 4216 | continue; |
| 4217 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4218 | // Okay, we can transform this. Create the new call instruction and copy |
| 4219 | // over the required information. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4220 | newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4221 | |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 4222 | // Copy over any operand bundles. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4223 | callSite->getOperandBundlesAsDefs(newBundles); |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 4224 | |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4225 | llvm::CallBase *newCall; |
| 4226 | if (dyn_cast<llvm::CallInst>(callSite)) { |
| 4227 | newCall = |
| 4228 | llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4229 | } else { |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4230 | auto *oldInvoke = cast<llvm::InvokeInst>(callSite); |
| 4231 | newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(), |
| 4232 | oldInvoke->getUnwindDest(), newArgs, |
| 4233 | newBundles, "", callSite); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4234 | } |
| 4235 | newArgs.clear(); // for the next iteration |
| 4236 | |
| 4237 | if (!newCall->getType()->isVoidTy()) |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4238 | newCall->takeName(callSite); |
| 4239 | newCall->setAttributes(llvm::AttributeList::get( |
Reid Kleckner | 7f72033 | 2017-04-13 00:58:09 +0000 | [diff] [blame] | 4240 | newFn->getContext(), oldAttrs.getFnAttributes(), |
| 4241 | oldAttrs.getRetAttributes(), newArgAttrs)); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4242 | newCall->setCallingConv(callSite->getCallingConv()); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4243 | |
| 4244 | // Finally, remove the old call, replacing any uses with the new one. |
| 4245 | if (!callSite->use_empty()) |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4246 | callSite->replaceAllUsesWith(newCall); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4247 | |
| 4248 | // Copy debug location attached to CI. |
Duncan P. N. Exon Smith | 2809cc7 | 2015-03-30 20:01:41 +0000 | [diff] [blame] | 4249 | if (callSite->getDebugLoc()) |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4250 | newCall->setDebugLoc(callSite->getDebugLoc()); |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 4251 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4252 | callSite->eraseFromParent(); |
| 4253 | } |
| 4254 | } |
| 4255 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 4256 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 4257 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 4258 | /// existing call uses of the old function in the module, this adjusts them to |
| 4259 | /// call the new function directly. |
| 4260 | /// |
| 4261 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 4262 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 4263 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 4264 | /// run at -O0. |
| 4265 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 4266 | llvm::Function *NewFn) { |
| 4267 | // 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] | 4268 | if (!isa<llvm::Function>(Old)) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4269 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 4270 | replaceUsesOfNonProtoConstant(Old, NewFn); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 4273 | void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
David Majnemer | 60e5bdc | 2016-07-11 04:28:21 +0000 | [diff] [blame] | 4274 | auto DK = VD->isThisDeclarationADefinition(); |
| 4275 | if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) |
| 4276 | return; |
| 4277 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 4278 | TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); |
| 4279 | // If we have a definition, this might be a deferred decl. If the |
| 4280 | // instantiation is explicit, make sure we emit it at the end. |
| 4281 | if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) |
| 4282 | GetAddrOfGlobalVar(VD); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4283 | |
| 4284 | EmitTopLevelDecl(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4285 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 4286 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 4287 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, |
| 4288 | llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4289 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 4290 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 4291 | // Compute the function info and LLVM type. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 4292 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 4293 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 4294 | |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 4295 | // Get or create the prototype for the function. |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 4296 | if (!GV || (GV->getType()->getElementType() != Ty)) |
| 4297 | GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, |
| 4298 | /*DontDefer=*/true, |
John McCall | d195d4c | 2016-11-30 23:25:13 +0000 | [diff] [blame] | 4299 | ForDefinition)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 4301 | // Already emitted. |
| 4302 | if (!GV->isDeclaration()) |
Renato Golin | c640ff6 | 2015-04-15 08:44:40 +0000 | [diff] [blame] | 4303 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4304 | |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 4305 | // We need to set linkage and visibility on the function before |
| 4306 | // generating code for it because various parts of IR generation |
| 4307 | // want to propagate this information down (e.g. to local static |
| 4308 | // declarations). |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4309 | auto *Fn = cast<llvm::Function>(GV); |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 4310 | setFunctionLinkage(GD, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 4311 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 4312 | // FIXME: this is redundant with part of setFunctionDefinitionAttributes |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 4313 | setGVProperties(Fn, GD); |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 4314 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 4315 | MaybeHandleStaticInExternC(D, Fn); |
| 4316 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 4317 | |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 4318 | maybeSetTrivialComdat(*D, *Fn); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 4319 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 4320 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 4321 | |
Rafael Espindola | e4e7813 | 2018-03-01 00:00:02 +0000 | [diff] [blame] | 4322 | setNonAliasAttributes(GD, Fn); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 4323 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4325 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 4326 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4327 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 4328 | AddGlobalDtor(Fn, DA->getPriority()); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 4329 | if (D->hasAttr<AnnotateAttr>()) |
| 4330 | AddGlobalAnnotations(D, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 4333 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4334 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4335 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4336 | assert(AA && "Not an alias?"); |
| 4337 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4338 | StringRef MangledName = getMangledName(GD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | |
Hal Finkel | 0e2b975 | 2015-09-04 21:49:21 +0000 | [diff] [blame] | 4340 | if (AA->getAliasee() == MangledName) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 4341 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
Hal Finkel | 0e2b975 | 2015-09-04 21:49:21 +0000 | [diff] [blame] | 4342 | return; |
| 4343 | } |
| 4344 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 4345 | // If there is a definition in the module, then it wins over the alias. |
| 4346 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 4347 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 4348 | if (Entry && !Entry->isDeclaration()) |
| 4349 | return; |
| 4350 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 4351 | Aliases.push_back(GD); |
| 4352 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4353 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4354 | |
| 4355 | // Create a reference to the named value. This ensures that it is emitted |
| 4356 | // if a deferred decl. |
| 4357 | llvm::Constant *Aliasee; |
| 4358 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 4359 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 4360 | /*ForVTable=*/false); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4361 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 4362 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4363 | llvm::PointerType::getUnqual(DeclTy), |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 4364 | /*D=*/nullptr); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4365 | |
| 4366 | // Create the new alias itself, but don't set a name yet. |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 4367 | auto *GA = llvm::GlobalAlias::create( |
David Blaikie | 2a791d7 | 2015-09-14 18:38:22 +0000 | [diff] [blame] | 4368 | DeclTy, 0, llvm::Function::ExternalLinkage, "", Aliasee, &getModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4370 | if (Entry) { |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 4371 | if (GA->getAliasee() == Entry) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 4372 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 4373 | return; |
| 4374 | } |
| 4375 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 4376 | assert(Entry->isDeclaration()); |
| 4377 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4378 | // If there is a declaration in the module, then we had an extern followed |
| 4379 | // by the alias, as in: |
| 4380 | // extern int test6(); |
| 4381 | // ... |
| 4382 | // int test6() __attribute__((alias("test7"))); |
| 4383 | // |
| 4384 | // Remove it and replace uses of it with the alias. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 4385 | GA->takeName(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4386 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4387 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4388 | Entry->getType())); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4389 | Entry->eraseFromParent(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 4390 | } else { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 4391 | GA->setName(MangledName); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4392 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 4394 | // Set attributes which are particular to an alias; this is a |
| 4395 | // specialization of the attributes which may be set on a global |
| 4396 | // variable/function. |
Rafael Espindola | fcfbcc6 | 2014-10-08 00:00:09 +0000 | [diff] [blame] | 4397 | if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || |
| 4398 | D->isWeakImported()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 4399 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 4400 | } |
| 4401 | |
David Majnemer | bb525f7c | 2014-10-15 22:38:23 +0000 | [diff] [blame] | 4402 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
| 4403 | if (VD->getTLSKind()) |
| 4404 | setTLSMode(GA, *VD); |
| 4405 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 4406 | SetCommonAttributes(GD, GA); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 4409 | void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { |
| 4410 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
| 4411 | const IFuncAttr *IFA = D->getAttr<IFuncAttr>(); |
| 4412 | assert(IFA && "Not an ifunc?"); |
| 4413 | |
| 4414 | StringRef MangledName = getMangledName(GD); |
| 4415 | |
| 4416 | if (IFA->getResolver() == MangledName) { |
| 4417 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
| 4418 | return; |
| 4419 | } |
| 4420 | |
| 4421 | // Report an error if some definition overrides ifunc. |
| 4422 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 4423 | if (Entry && !Entry->isDeclaration()) { |
| 4424 | GlobalDecl OtherGD; |
| 4425 | if (lookupRepresentativeDecl(MangledName, OtherGD) && |
| 4426 | DiagnosedConflictingDefinitions.insert(GD).second) { |
Richard Smith | b534510 | 2018-05-30 01:52:16 +0000 | [diff] [blame] | 4427 | Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name) |
| 4428 | << MangledName; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 4429 | Diags.Report(OtherGD.getDecl()->getLocation(), |
| 4430 | diag::note_previous_definition); |
| 4431 | } |
| 4432 | return; |
| 4433 | } |
| 4434 | |
| 4435 | Aliases.push_back(GD); |
| 4436 | |
| 4437 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
| 4438 | llvm::Constant *Resolver = |
| 4439 | GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD, |
| 4440 | /*ForVTable=*/false); |
| 4441 | llvm::GlobalIFunc *GIF = |
| 4442 | llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, |
| 4443 | "", Resolver, &getModule()); |
| 4444 | if (Entry) { |
| 4445 | if (GIF->getResolver() == Entry) { |
| 4446 | Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; |
| 4447 | return; |
| 4448 | } |
| 4449 | assert(Entry->isDeclaration()); |
| 4450 | |
| 4451 | // If there is a declaration in the module, then we had an extern followed |
| 4452 | // by the ifunc, as in: |
| 4453 | // extern int test(); |
| 4454 | // ... |
| 4455 | // int test() __attribute__((ifunc("resolver"))); |
| 4456 | // |
| 4457 | // Remove it and replace uses of it with the ifunc. |
| 4458 | GIF->takeName(Entry); |
| 4459 | |
| 4460 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF, |
| 4461 | Entry->getType())); |
| 4462 | Entry->eraseFromParent(); |
| 4463 | } else |
| 4464 | GIF->setName(MangledName); |
| 4465 | |
Rafael Espindola | 75c649c | 2018-03-01 00:06:55 +0000 | [diff] [blame] | 4466 | SetCommonAttributes(GD, GIF); |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 4469 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 4470 | ArrayRef<llvm::Type*> Tys) { |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 4471 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 4472 | Tys); |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 4473 | } |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 4474 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4475 | static llvm::StringMapEntry<llvm::GlobalVariable *> & |
| 4476 | GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map, |
| 4477 | const StringLiteral *Literal, bool TargetIsLSB, |
| 4478 | bool &IsUTF16, unsigned &StringLength) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4479 | StringRef String = Literal->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 4480 | unsigned NumBytes = String.size(); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 4481 | |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 4482 | // Check for simple case. |
| 4483 | if (!Literal->containsNonAsciiOrNull()) { |
| 4484 | StringLength = NumBytes; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 4485 | return *Map.insert(std::make_pair(String, nullptr)).first; |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4488 | // Otherwise, convert the UTF8 literals into a string of shorts. |
| 4489 | IsUTF16 = true; |
| 4490 | |
Justin Lebar | 9091055 | 2016-09-30 00:38:45 +0000 | [diff] [blame] | 4491 | SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls. |
| 4492 | const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); |
| 4493 | llvm::UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Justin Lebar | 9091055 | 2016-09-30 00:38:45 +0000 | [diff] [blame] | 4495 | (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, |
| 4496 | ToPtr + NumBytes, llvm::strictConversion); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 4498 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 4499 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 4500 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4501 | // Add an explicit null. |
| 4502 | *ToPtr = 0; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 4503 | return *Map.insert(std::make_pair( |
| 4504 | StringRef(reinterpret_cast<const char *>(ToBuf.data()), |
| 4505 | (StringLength + 1) * 2), |
| 4506 | nullptr)).first; |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4509 | ConstantAddress |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 4510 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 4511 | unsigned StringLength = 0; |
| 4512 | bool isUTF16 = false; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4513 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
| 4514 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
| 4515 | getDataLayout().isLittleEndian(), isUTF16, |
| 4516 | StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 4518 | if (auto *C = Entry.second) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4519 | return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4521 | llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 4522 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4523 | |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4524 | const ASTContext &Context = getContext(); |
| 4525 | const llvm::Triple &Triple = getTriple(); |
| 4526 | |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4527 | const auto CFRuntime = getLangOpts().CFRuntime; |
| 4528 | const bool IsSwiftABI = |
| 4529 | static_cast<unsigned>(CFRuntime) >= |
| 4530 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift); |
| 4531 | const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1; |
| 4532 | |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 4533 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4534 | if (!CFConstantStringClassRef) { |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4535 | const char *CFConstantStringClassName = "__CFConstantStringClassReference"; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4536 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4537 | Ty = llvm::ArrayType::get(Ty, 0); |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4538 | |
| 4539 | switch (CFRuntime) { |
| 4540 | default: break; |
| 4541 | case LangOptions::CoreFoundationABI::Swift: LLVM_FALLTHROUGH; |
Saleem Abdulrasool | 0c16a13 | 2018-10-25 17:52:13 +0000 | [diff] [blame] | 4542 | case LangOptions::CoreFoundationABI::Swift5_0: |
| 4543 | CFConstantStringClassName = |
Saleem Abdulrasool | 98ac998 | 2018-10-26 03:16:16 +0000 | [diff] [blame] | 4544 | Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN" |
| 4545 | : "$s10Foundation19_NSCFConstantStringCN"; |
Saleem Abdulrasool | 0c16a13 | 2018-10-25 17:52:13 +0000 | [diff] [blame] | 4546 | Ty = IntPtrTy; |
| 4547 | break; |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4548 | case LangOptions::CoreFoundationABI::Swift4_2: |
| 4549 | CFConstantStringClassName = |
Saleem Abdulrasool | 98ac998 | 2018-10-26 03:16:16 +0000 | [diff] [blame] | 4550 | Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN" |
| 4551 | : "$S10Foundation19_NSCFConstantStringCN"; |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4552 | Ty = IntPtrTy; |
| 4553 | break; |
| 4554 | case LangOptions::CoreFoundationABI::Swift4_1: |
| 4555 | CFConstantStringClassName = |
| 4556 | Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN" |
| 4557 | : "__T010Foundation19_NSCFConstantStringCN"; |
| 4558 | Ty = IntPtrTy; |
| 4559 | break; |
| 4560 | } |
| 4561 | |
| 4562 | llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName); |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4563 | |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4564 | if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) { |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4565 | llvm::GlobalValue *GV = nullptr; |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4566 | |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4567 | if ((GV = dyn_cast<llvm::GlobalValue>(C))) { |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4568 | IdentifierInfo &II = Context.Idents.get(GV->getName()); |
| 4569 | TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl(); |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4570 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 4571 | |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4572 | const VarDecl *VD = nullptr; |
| 4573 | for (const auto &Result : DC->lookup(&II)) |
| 4574 | if ((VD = dyn_cast<VarDecl>(Result))) |
| 4575 | break; |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4576 | |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4577 | if (Triple.isOSBinFormatELF()) { |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4578 | if (!VD) |
| 4579 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4580 | } else { |
| 4581 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4582 | if (!VD || !VD->hasAttr<DLLExportAttr>()) |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4583 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4584 | else |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4585 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4586 | } |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4587 | |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4588 | setDSOLocal(GV); |
Saleem Abdulrasool | 4976634 | 2016-06-01 04:22:24 +0000 | [diff] [blame] | 4589 | } |
| 4590 | } |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4591 | |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 4592 | // Decay array -> ptr |
Saleem Abdulrasool | 4fab745 | 2016-09-11 01:25:15 +0000 | [diff] [blame] | 4593 | CFConstantStringClassRef = |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4594 | IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty) |
| 4595 | : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4596 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4598 | QualType CFTy = Context.getCFConstantStringType(); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 4599 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4600 | auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 4601 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 4602 | ConstantInitBuilder Builder(*this); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 4603 | auto Fields = Builder.beginStruct(STy); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4604 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4605 | // Class pointer. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 4606 | Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4608 | // Flags. |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4609 | if (IsSwiftABI) { |
| 4610 | Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01); |
| 4611 | Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8); |
| 4612 | } else { |
| 4613 | Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8); |
| 4614 | } |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 4615 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4616 | // String pointer. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4617 | llvm::Constant *C = nullptr; |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4618 | if (isUTF16) { |
Craig Topper | c005cc0 | 2015-09-27 03:44:08 +0000 | [diff] [blame] | 4619 | auto Arr = llvm::makeArrayRef( |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 4620 | reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())), |
| 4621 | Entry.first().size() / 2); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4622 | C = llvm::ConstantDataArray::get(VMContext, Arr); |
| 4623 | } else { |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 4624 | C = llvm::ConstantDataArray::getString(VMContext, Entry.first()); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4625 | } |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 4626 | |
Bill Wendling | 86131f2 | 2012-01-10 08:46:39 +0000 | [diff] [blame] | 4627 | // Note: -fwritable-strings doesn't make the backing store strings of |
| 4628 | // CFStrings writable. (See <rdar://problem/10657500>) |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4629 | auto *GV = |
Rafael Espindola | e1bd71f | 2014-01-21 02:57:56 +0000 | [diff] [blame] | 4630 | new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true, |
| 4631 | llvm::GlobalValue::PrivateLinkage, C, ".str"); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 4632 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 4633 | // Don't enforce the target's minimum global alignment, since the only use |
| 4634 | // of the string is via this class initializer. |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4635 | CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy) |
| 4636 | : Context.getTypeAlignInChars(Context.CharTy); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4637 | GV->setAlignment(Align.getQuantity()); |
| 4638 | |
| 4639 | // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. |
| 4640 | // Without it LLVM can merge the string with a non unnamed_addr one during |
| 4641 | // LTO. Doing that changes the section it ends in, which surprises ld64. |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4642 | if (Triple.isOSBinFormatMachO()) |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4643 | GV->setSection(isUTF16 ? "__TEXT,__ustring" |
| 4644 | : "__TEXT,__cstring,cstring_literals"); |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4645 | // Make sure the literal ends up in .rodata to allow for safe ICF and for |
| 4646 | // the static linker to adjust permissions to read-only later on. |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4647 | else if (Triple.isOSBinFormatELF()) |
Kristina Brooks | 34e24d5 | 2018-09-25 22:27:40 +0000 | [diff] [blame] | 4648 | GV->setSection(".rodata"); |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 4649 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4650 | // String. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 4651 | llvm::Constant *Str = |
David Blaikie | d3c127e | 2015-05-07 17:27:56 +0000 | [diff] [blame] | 4652 | llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 4653 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4654 | if (isUTF16) |
| 4655 | // Cast the UTF16 string to the correct type. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 4656 | Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy); |
| 4657 | Fields.add(Str); |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 4658 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4659 | // String length. |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 4660 | llvm::IntegerType *LengthTy = |
| 4661 | llvm::IntegerType::get(getModule().getContext(), |
| 4662 | Context.getTargetInfo().getLongWidth()); |
| 4663 | if (IsSwiftABI) { |
| 4664 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || |
| 4665 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) |
| 4666 | LengthTy = Int32Ty; |
| 4667 | else |
| 4668 | LengthTy = IntPtrTy; |
| 4669 | } |
| 4670 | Fields.addInt(LengthTy, StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4671 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4672 | CharUnits Alignment = getPointerAlign(); |
| 4673 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4674 | // The struct. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 4675 | GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment, |
| 4676 | /*isConstant=*/false, |
| 4677 | llvm::GlobalVariable::PrivateLinkage); |
Akira Hatanaka | 6cb2d9d | 2019-06-14 22:06:28 +0000 | [diff] [blame] | 4678 | GV->addAttribute("objc_arc_inert"); |
Saleem Abdulrasool | d5a2788 | 2018-10-24 16:56:36 +0000 | [diff] [blame] | 4679 | switch (Triple.getObjectFormat()) { |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4680 | case llvm::Triple::UnknownObjectFormat: |
| 4681 | llvm_unreachable("unknown file format"); |
Jason Liu | a03ae73 | 2019-03-12 22:01:10 +0000 | [diff] [blame] | 4682 | case llvm::Triple::XCOFF: |
| 4683 | llvm_unreachable("XCOFF is not yet implemented"); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4684 | case llvm::Triple::COFF: |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4685 | case llvm::Triple::ELF: |
Dan Gohman | 839f215 | 2017-01-17 21:46:38 +0000 | [diff] [blame] | 4686 | case llvm::Triple::Wasm: |
Saleem Abdulrasool | 0295f8c | 2016-07-09 01:59:51 +0000 | [diff] [blame] | 4687 | GV->setSection("cfstring"); |
Saleem Abdulrasool | f7444e6 | 2016-05-30 16:23:07 +0000 | [diff] [blame] | 4688 | break; |
| 4689 | case llvm::Triple::MachO: |
| 4690 | GV->setSection("__DATA,__cfstring"); |
| 4691 | break; |
| 4692 | } |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 4693 | Entry.second = GV; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4694 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4695 | return ConstantAddress(GV, Alignment); |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 4696 | } |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 4697 | |
Bob Haarman | c6c9b8f | 2017-09-11 22:11:57 +0000 | [diff] [blame] | 4698 | bool CodeGenModule::getExpressionLocationsEnabled() const { |
| 4699 | return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo; |
| 4700 | } |
| 4701 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4702 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
| 4703 | if (ObjCFastEnumerationStateType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4704 | RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4705 | D->startDefinition(); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4707 | QualType FieldTypes[] = { |
| 4708 | Context.UnsignedLongTy, |
| 4709 | Context.getPointerType(Context.getObjCIdType()), |
| 4710 | Context.getPointerType(Context.UnsignedLongTy), |
| 4711 | Context.getConstantArrayType(Context.UnsignedLongTy, |
| 4712 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 4713 | }; |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4715 | for (size_t i = 0; i < 4; ++i) { |
| 4716 | FieldDecl *Field = FieldDecl::Create(Context, |
| 4717 | D, |
| 4718 | SourceLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4719 | SourceLocation(), nullptr, |
| 4720 | FieldTypes[i], /*TInfo=*/nullptr, |
| 4721 | /*BitWidth=*/nullptr, |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4722 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4723 | ICIS_NoInit); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4724 | Field->setAccess(AS_public); |
| 4725 | D->addDecl(Field); |
| 4726 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4727 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4728 | D->completeDefinition(); |
| 4729 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
| 4730 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4731 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 4732 | return ObjCFastEnumerationStateType; |
| 4733 | } |
| 4734 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 4735 | llvm::Constant * |
| 4736 | CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { |
| 4737 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4738 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 4739 | // Don't emit it as the address of the string, emit the string data itself |
| 4740 | // as an inline array. |
Chris Lattner | 00a10ca | 2012-02-06 22:47:00 +0000 | [diff] [blame] | 4741 | if (E->getCharByteWidth() == 1) { |
| 4742 | SmallString<64> Str(E->getString()); |
| 4743 | |
| 4744 | // Resize the string to the right size, which is indicated by its type. |
| 4745 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); |
| 4746 | Str.resize(CAT->getSize().getZExtValue()); |
| 4747 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
| 4748 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4749 | |
| 4750 | auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4751 | llvm::Type *ElemTy = AType->getElementType(); |
| 4752 | unsigned NumElements = AType->getNumElements(); |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 4753 | |
| 4754 | // Wide strings have either 2-byte or 4-byte elements. |
| 4755 | if (ElemTy->getPrimitiveSizeInBits() == 16) { |
| 4756 | SmallVector<uint16_t, 32> Elements; |
| 4757 | Elements.reserve(NumElements); |
| 4758 | |
| 4759 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 4760 | Elements.push_back(E->getCodeUnit(i)); |
| 4761 | Elements.resize(NumElements); |
| 4762 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4763 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4764 | |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 4765 | assert(ElemTy->getPrimitiveSizeInBits() == 32); |
| 4766 | SmallVector<uint32_t, 32> Elements; |
| 4767 | Elements.reserve(NumElements); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 4768 | |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 4769 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 4770 | Elements.push_back(E->getCodeUnit(i)); |
| 4771 | Elements.resize(NumElements); |
| 4772 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4775 | static llvm::GlobalVariable * |
| 4776 | GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, |
| 4777 | CodeGenModule &CGM, StringRef GlobalName, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4778 | CharUnits Alignment) { |
Yaxun Liu | daceb1e | 2018-05-14 19:20:12 +0000 | [diff] [blame] | 4779 | unsigned AddrSpace = CGM.getContext().getTargetAddressSpace( |
| 4780 | CGM.getStringLiteralAddressSpace()); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4781 | |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 4782 | llvm::Module &M = CGM.getModule(); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4783 | // Create a global variable for this string |
| 4784 | auto *GV = new llvm::GlobalVariable( |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 4785 | M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName, |
| 4786 | nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4787 | GV->setAlignment(Alignment.getQuantity()); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 4788 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 4789 | if (GV->isWeakForLinker()) { |
| 4790 | assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals"); |
| 4791 | GV->setComdat(M.getOrInsertComdat(GV->getName())); |
| 4792 | } |
Rafael Espindola | 0d40f12 | 2018-03-20 20:42:55 +0000 | [diff] [blame] | 4793 | CGM.setDSOLocal(GV); |
Rafael Espindola | d9b26d5 | 2015-01-16 20:32:35 +0000 | [diff] [blame] | 4794 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4795 | return GV; |
| 4796 | } |
| 4797 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4798 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 4799 | /// constant array for the given string literal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4800 | ConstantAddress |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 4801 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S, |
| 4802 | StringRef Name) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4803 | CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType()); |
James Molloy | e735b2b | 2014-05-09 16:28:56 +0000 | [diff] [blame] | 4804 | |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4805 | llvm::Constant *C = GetConstantArrayFromStringLiteral(S); |
| 4806 | llvm::GlobalVariable **Entry = nullptr; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4807 | if (!LangOpts.WritableStrings) { |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4808 | Entry = &ConstantStringMap[C]; |
| 4809 | if (auto GV = *Entry) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4810 | if (Alignment.getQuantity() > GV->getAlignment()) |
| 4811 | GV->setAlignment(Alignment.getQuantity()); |
Michael Liao | 0ee7bd4 | 2019-02-21 19:40:20 +0000 | [diff] [blame] | 4812 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
| 4813 | Alignment); |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4814 | } |
Eli Friedman | 49ddc5f | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 4815 | } |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 4816 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4817 | SmallString<256> MangledNameBuffer; |
| 4818 | StringRef GlobalVariableName; |
| 4819 | llvm::GlobalValue::LinkageTypes LT; |
David Majnemer | 3843a05 | 2014-03-23 17:47:16 +0000 | [diff] [blame] | 4820 | |
Reid Kleckner | 7a36896 | 2018-09-06 18:25:39 +0000 | [diff] [blame] | 4821 | // Mangle the string literal if that's how the ABI merges duplicate strings. |
| 4822 | // Don't do it if they are writable, since we don't want writes in one TU to |
| 4823 | // affect strings in another. |
| 4824 | if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) && |
| 4825 | !LangOpts.WritableStrings) { |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4826 | llvm::raw_svector_ostream Out(MangledNameBuffer); |
| 4827 | getCXXABI().getMangleContext().mangleStringLiteral(S, Out); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4828 | LT = llvm::GlobalValue::LinkOnceODRLinkage; |
| 4829 | GlobalVariableName = MangledNameBuffer; |
| 4830 | } else { |
| 4831 | LT = llvm::GlobalValue::PrivateLinkage; |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 4832 | GlobalVariableName = Name; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4835 | auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); |
| 4836 | if (Entry) |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4837 | *Entry = GV; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 4838 | |
Alexey Samsonov | a0ac3c2 | 2014-10-17 22:37:33 +0000 | [diff] [blame] | 4839 | SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>", |
| 4840 | QualType()); |
Yaxun Liu | daceb1e | 2018-05-14 19:20:12 +0000 | [diff] [blame] | 4841 | |
| 4842 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
| 4843 | Alignment); |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 4846 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 4847 | /// array for the given ObjCEncodeExpr node. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4848 | ConstantAddress |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 4849 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 4850 | std::string Str; |
| 4851 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | 4663a33 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 4852 | |
| 4853 | return GetAddrOfConstantCString(Str); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Alexey Samsonov | adc9037 | 2014-06-04 20:25:57 +0000 | [diff] [blame] | 4856 | /// GetAddrOfConstantCString - Returns a pointer to a character array containing |
| 4857 | /// the literal and a terminating '\0' character. |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4858 | /// The result has pointer to array type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4859 | ConstantAddress CodeGenModule::GetAddrOfConstantCString( |
| 4860 | const std::string &Str, const char *GlobalName) { |
Alexey Samsonov | adc9037 | 2014-06-04 20:25:57 +0000 | [diff] [blame] | 4861 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4862 | CharUnits Alignment = |
| 4863 | getContext().getAlignOfGlobalVarInChars(getContext().CharTy); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4864 | |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4865 | llvm::Constant *C = |
| 4866 | llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false); |
| 4867 | |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4868 | // Don't share any string literals if strings aren't constant. |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4869 | llvm::GlobalVariable **Entry = nullptr; |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4870 | if (!LangOpts.WritableStrings) { |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4871 | Entry = &ConstantStringMap[C]; |
| 4872 | if (auto GV = *Entry) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4873 | if (Alignment.getQuantity() > GV->getAlignment()) |
| 4874 | GV->setAlignment(Alignment.getQuantity()); |
Michael Liao | 0ee7bd4 | 2019-02-21 19:40:20 +0000 | [diff] [blame] | 4875 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
| 4876 | Alignment); |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 4880 | // Get the default prefix if a name wasn't specified. |
| 4881 | if (!GlobalName) |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 4882 | GlobalName = ".str"; |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 4883 | // Create a global variable for this. |
Alexey Samsonov | 175e52f | 2014-06-04 19:56:57 +0000 | [diff] [blame] | 4884 | auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, |
| 4885 | GlobalName, Alignment); |
| 4886 | if (Entry) |
Richard Smith | 00db2f1 | 2014-07-29 21:20:12 +0000 | [diff] [blame] | 4887 | *Entry = GV; |
Yaxun Liu | daceb1e | 2018-05-14 19:20:12 +0000 | [diff] [blame] | 4888 | |
| 4889 | return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), |
| 4890 | Alignment); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 4891 | } |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 4892 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4893 | ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4894 | const MaterializeTemporaryExpr *E, const Expr *Init) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4895 | assert((E->getStorageDuration() == SD_Static || |
| 4896 | E->getStorageDuration() == SD_Thread) && "not a global temporary"); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4897 | const auto *VD = cast<VarDecl>(E->getExtendingDecl()); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4898 | |
| 4899 | // If we're not materializing a subobject of the temporary, keep the |
| 4900 | // cv-qualifiers from the type of the MaterializeTemporaryExpr. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4901 | QualType MaterializedType = Init->getType(); |
| 4902 | if (Init == E->GetTemporaryExpr()) |
| 4903 | MaterializedType = E->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4904 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4905 | CharUnits Align = getContext().getTypeAlignInChars(MaterializedType); |
| 4906 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 4907 | if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E]) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4908 | return ConstantAddress(Slot, Align); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4909 | |
| 4910 | // FIXME: If an externally-visible declaration extends multiple temporaries, |
| 4911 | // we need to give each temporary the same name in every translation unit (and |
| 4912 | // we also need to make the temporaries externally-visible). |
| 4913 | SmallString<256> Name; |
| 4914 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 4915 | getCXXABI().getMangleContext().mangleReferenceTemporary( |
| 4916 | VD, E->getManglingNumber(), Out); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4917 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4918 | APValue *Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4919 | if (E->getStorageDuration() == SD_Static) { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4920 | // We might have a cached constant initializer for this temporary. Note |
| 4921 | // that this might have a different value from the value computed by |
| 4922 | // evaluating the initializer if the surrounding constant expression |
| 4923 | // modifies the temporary. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4924 | Value = getContext().getMaterializedTemporaryValue(E, false); |
Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 4925 | if (Value && Value->isAbsent()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4926 | Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4929 | // Try evaluating it now, it might have a constant initializer. |
| 4930 | Expr::EvalResult EvalResult; |
| 4931 | if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && |
| 4932 | !EvalResult.hasSideEffects()) |
| 4933 | Value = &EvalResult.Val; |
| 4934 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4935 | LangAS AddrSpace = |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 4936 | VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace(); |
| 4937 | |
| 4938 | Optional<ConstantEmitter> emitter; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4939 | llvm::Constant *InitialValue = nullptr; |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4940 | bool Constant = false; |
| 4941 | llvm::Type *Type; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4942 | if (Value) { |
| 4943 | // The temporary has a constant initializer, use it. |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 4944 | emitter.emplace(*this); |
| 4945 | InitialValue = emitter->emitForInitializer(*Value, AddrSpace, |
| 4946 | MaterializedType); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4947 | Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value); |
| 4948 | Type = InitialValue->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4949 | } else { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4950 | // No initializer, the initialization will be provided when we |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4951 | // initialize the declaration which performed lifetime extension. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4952 | Type = getTypes().ConvertTypeForMem(MaterializedType); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | // Create a global variable for this lifetime-extended temporary. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4956 | llvm::GlobalValue::LinkageTypes Linkage = |
| 4957 | getLLVMLinkageVarDefinition(VD, Constant); |
David Majnemer | c9a9c7a | 2015-02-19 19:25:17 +0000 | [diff] [blame] | 4958 | if (Linkage == llvm::GlobalVariable::ExternalLinkage) { |
| 4959 | const VarDecl *InitVD; |
| 4960 | if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) && |
| 4961 | isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) { |
| 4962 | // Temporaries defined inside a class get linkonce_odr linkage because the |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4963 | // class can be defined in multiple translation units. |
David Majnemer | c9a9c7a | 2015-02-19 19:25:17 +0000 | [diff] [blame] | 4964 | Linkage = llvm::GlobalVariable::LinkOnceODRLinkage; |
| 4965 | } else { |
| 4966 | // There is no need for this temporary to have external linkage if the |
| 4967 | // VarDecl has external linkage. |
| 4968 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 4969 | } |
| 4970 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 4971 | auto TargetAS = getContext().getTargetAddressSpace(AddrSpace); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 4972 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 4973 | getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 4974 | /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 4975 | if (emitter) emitter->finalize(GV); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 4976 | setGVProperties(GV, VD); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4977 | GV->setAlignment(Align.getQuantity()); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 4978 | if (supportsCOMDAT() && GV->isWeakForLinker()) |
| 4979 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4980 | if (VD->getTLSKind()) |
| 4981 | setTLSMode(GV, *VD); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 4982 | llvm::Constant *CV = GV; |
| 4983 | if (AddrSpace != LangAS::Default) |
| 4984 | CV = getTargetCodeGenInfo().performAddrSpaceCast( |
| 4985 | *this, GV, AddrSpace, LangAS::Default, |
| 4986 | Type->getPointerTo( |
| 4987 | getContext().getTargetAddressSpace(LangAS::Default))); |
| 4988 | MaterializedGlobalTemporaryMap[E] = CV; |
| 4989 | return ConstantAddress(CV, Align); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4992 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 4993 | /// properties for an implementation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4994 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4995 | ObjCImplementationDecl *D) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 4996 | for (const auto *PID : D->property_impls()) { |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 4997 | // Dynamic is just for type-checking. |
| 4998 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 4999 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 5000 | |
| 5001 | // Determine which methods need to be implemented, some may have |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 5002 | // been overridden. Note that ::isPropertyAccessor is not the method |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 5003 | // we want, that just indicates if the decl came from a |
| 5004 | // property. What we want to know is if the method is defined in |
| 5005 | // this implementation. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5006 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 5007 | CodeGenFunction(*this).GenerateObjCGetter( |
| 5008 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 5009 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5010 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 5011 | CodeGenFunction(*this).GenerateObjCSetter( |
| 5012 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 5013 | } |
| 5014 | } |
| 5015 | } |
| 5016 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 5017 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5018 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 5019 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 5020 | ivar; ivar = ivar->getNextIvar()) |
| 5021 | if (ivar->getType().isDestructedType()) |
| 5022 | return true; |
| 5023 | |
| 5024 | return false; |
| 5025 | } |
| 5026 | |
Fariborz Jahanian | 800821a | 2014-11-12 22:37:43 +0000 | [diff] [blame] | 5027 | static bool AllTrivialInitializers(CodeGenModule &CGM, |
| 5028 | ObjCImplementationDecl *D) { |
| 5029 | CodeGenFunction CGF(CGM); |
| 5030 | for (ObjCImplementationDecl::init_iterator B = D->init_begin(), |
| 5031 | E = D->init_end(); B != E; ++B) { |
| 5032 | CXXCtorInitializer *CtorInitExp = *B; |
| 5033 | Expr *Init = CtorInitExp->getInit(); |
| 5034 | if (!CGF.isTrivialInitializer(Init)) |
| 5035 | return false; |
| 5036 | } |
| 5037 | return true; |
| 5038 | } |
| 5039 | |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5040 | /// EmitObjCIvarInitializations - Emit information for ivar initialization |
| 5041 | /// for an implementation. |
| 5042 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 5043 | // We might need a .cxx_destruct even if we don't have any ivar initializers. |
| 5044 | if (needsDestructMethod(D)) { |
| 5045 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
| 5046 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
| 5047 | ObjCMethodDecl *DTORMethod = |
| 5048 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5049 | cxxSelector, getContext().VoidTy, nullptr, D, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 5050 | /*isInstance=*/true, /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 5051 | /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 5052 | /*isDefined=*/false, ObjCMethodDecl::Required); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 5053 | D->addInstanceMethod(DTORMethod); |
| 5054 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5055 | D->setHasDestructors(true); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | // If the implementation doesn't have any ivar initializers, we don't need |
| 5059 | // a .cxx_construct. |
Fariborz Jahanian | 800821a | 2014-11-12 22:37:43 +0000 | [diff] [blame] | 5060 | if (D->getNumIvarInitializers() == 0 || |
| 5061 | AllTrivialInitializers(*this, D)) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5062 | return; |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 5063 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 5064 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
| 5065 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5066 | // The constructor returns 'self'. |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 5067 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5068 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 5069 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 5070 | cxxSelector, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5071 | getContext().getObjCIdType(), |
| 5072 | nullptr, D, /*isInstance=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 5073 | /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 5074 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 5075 | /*isImplicitlyDeclared=*/true, |
| 5076 | /*isDefined=*/false, |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5077 | ObjCMethodDecl::Required); |
| 5078 | D->addInstanceMethod(CTORMethod); |
| 5079 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5080 | D->setHasNonZeroConstructors(true); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 5083 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 5084 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | e480ce3 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 5085 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 5086 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 5087 | ErrorUnsupported(LSD, "linkage spec"); |
| 5088 | return; |
| 5089 | } |
| 5090 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5091 | EmitDeclContext(LSD); |
| 5092 | } |
| 5093 | |
| 5094 | void CodeGenModule::EmitDeclContext(const DeclContext *DC) { |
| 5095 | for (auto *I : DC->decls()) { |
| 5096 | // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope |
| 5097 | // are themselves considered "top-level", so EmitTopLevelDecl on an |
| 5098 | // ObjCImplDecl does not recursively visit them. We need to do that in |
| 5099 | // case they're nested inside another construct (LinkageSpecDecl / |
| 5100 | // ExportDecl) that does stop them from being considered "top-level". |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5101 | if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 5102 | for (auto *M : OID->methods()) |
| 5103 | EmitTopLevelDecl(M); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 5104 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5105 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 5106 | EmitTopLevelDecl(I); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 5107 | } |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5110 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 5111 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 5112 | // Ignore dependent declarations. |
Richard Smith | 32b615c | 2018-02-01 00:28:36 +0000 | [diff] [blame] | 5113 | if (D->isTemplated()) |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 5114 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5115 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5116 | switch (D->getKind()) { |
Anders Carlsson | 6c0a6e4 | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 5117 | case Decl::CXXConversion: |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 5118 | case Decl::CXXMethod: |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5119 | case Decl::Function: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 5120 | EmitGlobal(cast<FunctionDecl>(D)); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 5121 | // Always provide some coverage mapping |
| 5122 | // even for the functions that aren't emitted. |
| 5123 | AddDeferredUnusedCoverageMapping(D); |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 5124 | break; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5125 | |
Richard Smith | ada0888 | 2017-04-19 21:15:45 +0000 | [diff] [blame] | 5126 | case Decl::CXXDeductionGuide: |
| 5127 | // Function-like, but does not result in code emission. |
| 5128 | break; |
| 5129 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5130 | case Decl::Var: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5131 | case Decl::Decomposition: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5132 | case Decl::VarTemplateSpecialization: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 5133 | EmitGlobal(cast<VarDecl>(D)); |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 5134 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) |
| 5135 | for (auto *B : DD->bindings()) |
| 5136 | if (auto *HD = B->getHoldingVar()) |
| 5137 | EmitGlobal(HD); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5138 | break; |
| 5139 | |
John McCall | 32f44bd | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 5140 | // Indirect fields from global anonymous structs and unions can be |
| 5141 | // ignored; only the actual variable requires IR gen support. |
| 5142 | case Decl::IndirectField: |
| 5143 | break; |
| 5144 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 5145 | // C++ Decls |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5146 | case Decl::Namespace: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5147 | EmitDeclContext(cast<NamespaceDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5148 | break; |
Richard Smith | e124612 | 2017-11-03 01:26:01 +0000 | [diff] [blame] | 5149 | case Decl::ClassTemplateSpecialization: { |
| 5150 | const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); |
| 5151 | if (DebugInfo && |
| 5152 | Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition && |
| 5153 | Spec->hasDefinition()) |
| 5154 | DebugInfo->completeTemplateDefinition(*Spec); |
| 5155 | } LLVM_FALLTHROUGH; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 5156 | case Decl::CXXRecord: |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 5157 | if (DebugInfo) { |
| 5158 | if (auto *ES = D->getASTContext().getExternalSource()) |
| 5159 | if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) |
| 5160 | DebugInfo->completeUnusedClass(cast<CXXRecordDecl>(*D)); |
| 5161 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 5162 | // Emit any static data members, they may be definitions. |
| 5163 | for (auto *I : cast<CXXRecordDecl>(D)->decls()) |
| 5164 | if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I)) |
| 5165 | EmitTopLevelDecl(I); |
| 5166 | break; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5167 | // No code generation needed. |
John McCall | 1e9de05 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 5168 | case Decl::UsingShadow: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 5169 | case Decl::ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5170 | case Decl::VarTemplate: |
Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 5171 | case Decl::Concept: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5172 | case Decl::VarTemplatePartialSpecialization: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 5173 | case Decl::FunctionTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5174 | case Decl::TypeAliasTemplate: |
Douglas Gregor | 0aa91e0 | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 5175 | case Decl::Block: |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 5176 | case Decl::Empty: |
Richard Smith | 7c7e531 | 2018-10-26 03:21:20 +0000 | [diff] [blame] | 5177 | case Decl::Binding: |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5178 | break; |
David Blaikie | 3e65d36a | 2014-02-15 21:03:07 +0000 | [diff] [blame] | 5179 | case Decl::Using: // using X; [C++] |
| 5180 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 5181 | DI->EmitUsingDecl(cast<UsingDecl>(*D)); |
| 5182 | return; |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 5183 | case Decl::NamespaceAlias: |
| 5184 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 5185 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); |
| 5186 | return; |
David Blaikie | 60a9fbf | 2013-04-26 05:41:06 +0000 | [diff] [blame] | 5187 | case Decl::UsingDirective: // using namespace X; [C++] |
| 5188 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 5189 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); |
| 5190 | return; |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 5191 | case Decl::CXXConstructor: |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 5192 | getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 5193 | break; |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 5194 | case Decl::CXXDestructor: |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 5195 | getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 5196 | break; |
Anders Carlsson | 8783543 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 5197 | |
| 5198 | case Decl::StaticAssert: |
| 5199 | // Nothing to do. |
| 5200 | break; |
| 5201 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 5202 | // Objective-C Decls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5203 | |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 5204 | // Forward declarations, no (immediate) code generation. |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 5205 | case Decl::ObjCInterface: |
Eric Christopher | f8378ca | 2012-07-19 22:22:55 +0000 | [diff] [blame] | 5206 | case Decl::ObjCCategory: |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 5207 | break; |
| 5208 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 5209 | case Decl::ObjCProtocol: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5210 | auto *Proto = cast<ObjCProtocolDecl>(D); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 5211 | if (Proto->isThisDeclarationADefinition()) |
| 5212 | ObjCRuntime->GenerateProtocol(Proto); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5213 | break; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 5214 | } |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 5215 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5216 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 5217 | // Categories have properties but don't support synthesize so we |
| 5218 | // can ignore them here. |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 5219 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5220 | break; |
| 5221 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 5222 | case Decl::ObjCImplementation: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5223 | auto *OMD = cast<ObjCImplementationDecl>(D); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 5224 | EmitObjCPropertyImplementations(OMD); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5225 | EmitObjCIvarInitializations(OMD); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 5226 | ObjCRuntime->GenerateClass(OMD); |
Eric Christopher | 3d19de9 | 2012-04-11 05:56:05 +0000 | [diff] [blame] | 5227 | // Emit global variable debug information. |
| 5228 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 5229 | if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
Alexey Samsonov | 9c1b9f6 | 2012-12-03 18:28:12 +0000 | [diff] [blame] | 5230 | DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( |
| 5231 | OMD->getClassInterface()), OMD->getLocation()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5232 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5233 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5234 | case Decl::ObjCMethod: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5235 | auto *OMD = cast<ObjCMethodDecl>(D); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5236 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5237 | if (OMD->getBody()) |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5238 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 5239 | break; |
| 5240 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | case Decl::ObjCCompatibleAlias: |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 5242 | ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5243 | break; |
| 5244 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 5245 | case Decl::PragmaComment: { |
| 5246 | const auto *PCD = cast<PragmaCommentDecl>(D); |
| 5247 | switch (PCD->getCommentKind()) { |
| 5248 | case PCK_Unknown: |
| 5249 | llvm_unreachable("unexpected pragma comment kind"); |
| 5250 | case PCK_Linker: |
| 5251 | AppendLinkerOptions(PCD->getArg()); |
| 5252 | break; |
| 5253 | case PCK_Lib: |
Saleem Abdulrasool | fd4db53 | 2018-02-07 01:46:46 +0000 | [diff] [blame] | 5254 | AddDependentLib(PCD->getArg()); |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 5255 | break; |
| 5256 | case PCK_Compiler: |
| 5257 | case PCK_ExeStr: |
| 5258 | case PCK_User: |
| 5259 | break; // We ignore all of these. |
| 5260 | } |
| 5261 | break; |
| 5262 | } |
| 5263 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 5264 | case Decl::PragmaDetectMismatch: { |
| 5265 | const auto *PDMD = cast<PragmaDetectMismatchDecl>(D); |
| 5266 | AddDetectMismatch(PDMD->getName(), PDMD->getValue()); |
| 5267 | break; |
| 5268 | } |
| 5269 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 5270 | case Decl::LinkageSpec: |
| 5271 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5272 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5273 | |
| 5274 | case Decl::FileScopeAsm: { |
Artem Belevich | 0488d1e | 2015-04-27 18:52:00 +0000 | [diff] [blame] | 5275 | // File-scope asm is ignored during device-side CUDA compilation. |
| 5276 | if (LangOpts.CUDA && LangOpts.CUDAIsDevice) |
| 5277 | break; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 5278 | // File-scope asm is ignored during device-side OpenMP compilation. |
| 5279 | if (LangOpts.OpenMPIsDevice) |
| 5280 | break; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5281 | auto *AD = cast<FileScopeAsmDecl>(D); |
Joerg Sonnenberger | dfd511e | 2015-03-13 00:54:30 +0000 | [diff] [blame] | 5282 | getModule().appendModuleInlineAsm(AD->getAsmString()->getString()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5283 | break; |
| 5284 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5285 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 5286 | case Decl::Import: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5287 | auto *Import = cast<ImportDecl>(D); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 5288 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 5289 | // If we've already imported this module, we're done. |
| 5290 | if (!ImportedModules.insert(Import->getImportedModule())) |
Richard Smith | c52efa7 | 2015-08-19 02:30:28 +0000 | [diff] [blame] | 5291 | break; |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 5292 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 5293 | // Emit debug information for direct imports. |
| 5294 | if (!Import->getImportedOwningModule()) { |
| 5295 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 5296 | DI->EmitImportDecl(*Import); |
| 5297 | } |
| 5298 | |
Manman Ren | 3b5dbf2 | 2016-10-14 18:55:44 +0000 | [diff] [blame] | 5299 | // Find all of the submodules and emit the module initializers. |
| 5300 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 5301 | SmallVector<clang::Module *, 16> Stack; |
| 5302 | Visited.insert(Import->getImportedModule()); |
| 5303 | Stack.push_back(Import->getImportedModule()); |
| 5304 | |
| 5305 | while (!Stack.empty()) { |
| 5306 | clang::Module *Mod = Stack.pop_back_val(); |
| 5307 | if (!EmittedModuleInitializers.insert(Mod).second) |
| 5308 | continue; |
| 5309 | |
| 5310 | for (auto *D : Context.getModuleInitializers(Mod)) |
| 5311 | EmitTopLevelDecl(D); |
| 5312 | |
| 5313 | // Visit the submodules of this module. |
| 5314 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 5315 | SubEnd = Mod->submodule_end(); |
| 5316 | Sub != SubEnd; ++Sub) { |
| 5317 | // Skip explicit children; they need to be explicitly imported to emit |
| 5318 | // the initializers. |
| 5319 | if ((*Sub)->IsExplicit) |
| 5320 | continue; |
| 5321 | |
| 5322 | if (Visited.insert(*Sub).second) |
| 5323 | Stack.push_back(*Sub); |
| 5324 | } |
| 5325 | } |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 5326 | break; |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5329 | case Decl::Export: |
| 5330 | EmitDeclContext(cast<ExportDecl>(D)); |
| 5331 | break; |
| 5332 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 5333 | case Decl::OMPThreadPrivate: |
| 5334 | EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D)); |
| 5335 | break; |
| 5336 | |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 5337 | case Decl::OMPAllocate: |
| 5338 | break; |
| 5339 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 5340 | case Decl::OMPDeclareReduction: |
| 5341 | EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D)); |
| 5342 | break; |
Takuto Ikuta | 8aa5370 | 2018-10-17 04:29:56 +0000 | [diff] [blame] | 5343 | |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 5344 | case Decl::OMPDeclareMapper: |
| 5345 | EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D)); |
| 5346 | break; |
| 5347 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 5348 | case Decl::OMPRequires: |
| 5349 | EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D)); |
| 5350 | break; |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 5351 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5352 | default: |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5353 | // Make sure we handled everything we should, every other kind is a |
| 5354 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 5355 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5356 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
Richard Smith | a634ff2 | 2014-12-01 18:59:10 +0000 | [diff] [blame] | 5357 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5360 | |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 5361 | void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { |
| 5362 | // Do we need to generate coverage mapping? |
| 5363 | if (!CodeGenOpts.CoverageMapping) |
| 5364 | return; |
| 5365 | switch (D->getKind()) { |
| 5366 | case Decl::CXXConversion: |
| 5367 | case Decl::CXXMethod: |
| 5368 | case Decl::Function: |
| 5369 | case Decl::ObjCMethod: |
| 5370 | case Decl::CXXConstructor: |
| 5371 | case Decl::CXXDestructor: { |
Justin Bogner | 203f092 | 2015-07-28 00:41:51 +0000 | [diff] [blame] | 5372 | if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 5373 | return; |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 5374 | SourceManager &SM = getContext().getSourceManager(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5375 | if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc())) |
Vedant Kumar | 0acf343 | 2017-09-22 18:23:04 +0000 | [diff] [blame] | 5376 | return; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 5377 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
| 5378 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
| 5379 | DeferredEmptyCoverageMappingDecls[D] = true; |
| 5380 | break; |
| 5381 | } |
| 5382 | default: |
| 5383 | break; |
| 5384 | }; |
| 5385 | } |
| 5386 | |
| 5387 | void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) { |
| 5388 | // Do we need to generate coverage mapping? |
| 5389 | if (!CodeGenOpts.CoverageMapping) |
| 5390 | return; |
| 5391 | if (const auto *Fn = dyn_cast<FunctionDecl>(D)) { |
| 5392 | if (Fn->isTemplateInstantiation()) |
| 5393 | ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern()); |
| 5394 | } |
| 5395 | auto I = DeferredEmptyCoverageMappingDecls.find(D); |
| 5396 | if (I == DeferredEmptyCoverageMappingDecls.end()) |
| 5397 | DeferredEmptyCoverageMappingDecls[D] = false; |
| 5398 | else |
| 5399 | I->second = false; |
| 5400 | } |
| 5401 | |
| 5402 | void CodeGenModule::EmitDeferredUnusedCoverageMappings() { |
Eli Friedman | 8516b7f | 2017-12-19 01:54:09 +0000 | [diff] [blame] | 5403 | // We call takeVector() here to avoid use-after-free. |
| 5404 | // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because |
| 5405 | // we deserialize function bodies to emit coverage info for them, and that |
| 5406 | // deserializes more declarations. How should we handle that case? |
| 5407 | for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) { |
Eli Friedman | dbcfade | 2017-12-13 00:14:17 +0000 | [diff] [blame] | 5408 | if (!Entry.second) |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 5409 | continue; |
Eli Friedman | dbcfade | 2017-12-13 00:14:17 +0000 | [diff] [blame] | 5410 | const Decl *D = Entry.first; |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 5411 | switch (D->getKind()) { |
| 5412 | case Decl::CXXConversion: |
| 5413 | case Decl::CXXMethod: |
| 5414 | case Decl::Function: |
| 5415 | case Decl::ObjCMethod: { |
| 5416 | CodeGenPGO PGO(*this); |
| 5417 | GlobalDecl GD(cast<FunctionDecl>(D)); |
| 5418 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 5419 | getFunctionLinkage(GD)); |
| 5420 | break; |
| 5421 | } |
| 5422 | case Decl::CXXConstructor: { |
| 5423 | CodeGenPGO PGO(*this); |
| 5424 | GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base); |
| 5425 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 5426 | getFunctionLinkage(GD)); |
| 5427 | break; |
| 5428 | } |
| 5429 | case Decl::CXXDestructor: { |
| 5430 | CodeGenPGO PGO(*this); |
| 5431 | GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base); |
| 5432 | PGO.emitEmptyCounterMapping(D, getMangledName(GD), |
| 5433 | getFunctionLinkage(GD)); |
| 5434 | break; |
| 5435 | } |
| 5436 | default: |
| 5437 | break; |
| 5438 | }; |
| 5439 | } |
| 5440 | } |
| 5441 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5442 | /// Turns the given pointer into a constant. |
| 5443 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
| 5444 | const void *Ptr) { |
| 5445 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5446 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5447 | return llvm::ConstantInt::get(i64, PtrInt); |
| 5448 | } |
| 5449 | |
| 5450 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
| 5451 | llvm::NamedMDNode *&GlobalMetadata, |
| 5452 | GlobalDecl D, |
| 5453 | llvm::GlobalValue *Addr) { |
| 5454 | if (!GlobalMetadata) |
| 5455 | GlobalMetadata = |
| 5456 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
| 5457 | |
| 5458 | // TODO: should we report variant information for ctors/dtors? |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5459 | llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr), |
| 5460 | llvm::ConstantAsMetadata::get(GetPointerConstant( |
| 5461 | CGM.getLLVMContext(), D.getDecl()))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 5462 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 5465 | /// For each function which is declared within an extern "C" region and marked |
| 5466 | /// as 'used', but has internal linkage, create an alias from the unmangled |
| 5467 | /// name to the mangled name if possible. People expect to be able to refer |
| 5468 | /// to such functions with an unmangled name from inline assembly within the |
| 5469 | /// same translation unit. |
| 5470 | void CodeGenModule::EmitStaticExternCAliases() { |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 5471 | if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases()) |
Justin Lebar | cd2f6bb | 2016-01-25 22:36:37 +0000 | [diff] [blame] | 5472 | return; |
Yaron Keren | b54db52 | 2015-06-11 12:33:25 +0000 | [diff] [blame] | 5473 | for (auto &I : StaticExternCValues) { |
| 5474 | IdentifierInfo *Name = I.first; |
| 5475 | llvm::GlobalValue *Val = I.second; |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 5476 | if (Val && !getModule().getNamedValue(Name->getName())) |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 5477 | addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 5478 | } |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 5481 | bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName, |
| 5482 | GlobalDecl &Result) const { |
| 5483 | auto Res = Manglings.find(MangledName); |
| 5484 | if (Res == Manglings.end()) |
| 5485 | return false; |
| 5486 | Result = Res->getValue(); |
| 5487 | return true; |
| 5488 | } |
| 5489 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5490 | /// Emits metadata nodes associating all the global values in the |
| 5491 | /// current module with the Decls they came from. This is useful for |
| 5492 | /// projects using IR gen as a subroutine. |
| 5493 | /// |
| 5494 | /// Since there's currently no way to associate an MDNode directly |
| 5495 | /// with an llvm::GlobalValue, we create a global named metadata |
| 5496 | /// with the name 'clang.global.decl.ptrs'. |
| 5497 | void CodeGenModule::EmitDeclMetadata() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5498 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5499 | |
Robert Lytton | 5b88f78f | 2014-07-03 09:30:29 +0000 | [diff] [blame] | 5500 | for (auto &I : MangledDeclNames) { |
| 5501 | llvm::GlobalValue *Addr = getModule().getNamedValue(I.second); |
Keno Fischer | 0e2d422 | 2015-11-05 23:18:44 +0000 | [diff] [blame] | 5502 | // Some mangled names don't necessarily have an associated GlobalValue |
| 5503 | // in this module, e.g. if we mangled it for DebugInfo. |
| 5504 | if (Addr) |
| 5505 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | /// Emits metadata nodes for all the local variables in the current |
| 5510 | /// function. |
| 5511 | void CodeGenFunction::EmitDeclMetadata() { |
| 5512 | if (LocalDeclMap.empty()) return; |
| 5513 | |
| 5514 | llvm::LLVMContext &Context = getLLVMContext(); |
| 5515 | |
| 5516 | // Find the unique metadata ID for this name. |
| 5517 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
| 5518 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5519 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5520 | |
Robert Lytton | 5b88f78f | 2014-07-03 09:30:29 +0000 | [diff] [blame] | 5521 | for (auto &I : LocalDeclMap) { |
| 5522 | const Decl *D = I.first; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5523 | llvm::Value *Addr = I.second.getPointer(); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5524 | if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5525 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5526 | Alloca->setMetadata( |
| 5527 | DeclPtrKind, llvm::MDNode::get( |
| 5528 | Context, llvm::ValueAsMetadata::getConstant(DAddr))); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 5529 | } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 5530 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
| 5531 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
| 5532 | } |
| 5533 | } |
| 5534 | } |
Daniel Dunbar | 900546d | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 5535 | |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 5536 | void CodeGenModule::EmitVersionIdentMetadata() { |
| 5537 | llvm::NamedMDNode *IdentMetadata = |
| 5538 | TheModule.getOrInsertNamedMetadata("llvm.ident"); |
| 5539 | std::string Version = getClangFullVersion(); |
| 5540 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 5541 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5542 | llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)}; |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 5543 | IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); |
| 5544 | } |
| 5545 | |
Scott Linder | de6beb0 | 2018-12-14 15:38:15 +0000 | [diff] [blame] | 5546 | void CodeGenModule::EmitCommandLineMetadata() { |
| 5547 | llvm::NamedMDNode *CommandLineMetadata = |
| 5548 | TheModule.getOrInsertNamedMetadata("llvm.commandline"); |
| 5549 | std::string CommandLine = getCodeGenOpts().RecordCommandLine; |
| 5550 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 5551 | |
| 5552 | llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)}; |
| 5553 | CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode)); |
| 5554 | } |
| 5555 | |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 5556 | void CodeGenModule::EmitTargetMetadata() { |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 5557 | // Warning, new MangledDeclNames may be appended within this loop. |
| 5558 | // We rely on MapVector insertions adding new elements to the end |
| 5559 | // of the container. |
| 5560 | // FIXME: Move this loop into the one target that needs it, and only |
| 5561 | // loop over those declarations for which we couldn't emit the target |
| 5562 | // metadata when we emitted the declaration. |
| 5563 | for (unsigned I = 0; I != MangledDeclNames.size(); ++I) { |
Richard Smith | 570706d | 2014-08-01 22:17:28 +0000 | [diff] [blame] | 5564 | auto Val = *(MangledDeclNames.begin() + I); |
Richard Smith | 1ba0a07 | 2014-08-01 20:39:36 +0000 | [diff] [blame] | 5565 | const Decl *D = Val.first.getDecl()->getMostRecentDecl(); |
| 5566 | llvm::GlobalValue *GV = GetGlobalValue(Val.second); |
Robert Lytton | 57765d5 | 2014-07-03 09:30:33 +0000 | [diff] [blame] | 5567 | getTargetCodeGenInfo().emitTargetMD(D, GV, *this); |
| 5568 | } |
| 5569 | } |
| 5570 | |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 5571 | void CodeGenModule::EmitCoverageFile() { |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 5572 | if (getCodeGenOpts().CoverageDataFile.empty() && |
| 5573 | getCodeGenOpts().CoverageNotesFile.empty()) |
| 5574 | return; |
| 5575 | |
| 5576 | llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu"); |
| 5577 | if (!CUNode) |
| 5578 | return; |
| 5579 | |
| 5580 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
| 5581 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 5582 | auto *CoverageDataFile = |
| 5583 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile); |
| 5584 | auto *CoverageNotesFile = |
| 5585 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile); |
| 5586 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
| 5587 | llvm::MDNode *CU = CUNode->getOperand(i); |
| 5588 | llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU}; |
| 5589 | GCov->addOperand(llvm::MDNode::get(Ctx, Elts)); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 5590 | } |
| 5591 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 5592 | |
Nico Weber | 17d3a2c | 2014-09-08 16:26:36 +0000 | [diff] [blame] | 5593 | llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) { |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 5594 | // Sema has checked that all uuid strings are of the form |
| 5595 | // "12345678-1234-1234-1234-1234567890ab". |
| 5596 | assert(Uuid.size() == 36); |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 5597 | for (unsigned i = 0; i < 36; ++i) { |
| 5598 | if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-'); |
| 5599 | else assert(isHexDigit(Uuid[i])); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 5600 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 5601 | |
Nico Weber | dfa274e | 2014-09-08 16:11:15 +0000 | [diff] [blame] | 5602 | // 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] | 5603 | const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 }; |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 5604 | |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 5605 | llvm::Constant *Field3[8]; |
| 5606 | for (unsigned Idx = 0; Idx < 8; ++Idx) |
| 5607 | Field3[Idx] = llvm::ConstantInt::get( |
| 5608 | Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16); |
| 5609 | |
| 5610 | llvm::Constant *Fields[4] = { |
| 5611 | llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16), |
| 5612 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16), |
| 5613 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16), |
| 5614 | llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3) |
| 5615 | }; |
| 5616 | |
| 5617 | return llvm::ConstantStruct::getAnon(Fields); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 5618 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 5619 | |
| 5620 | llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, |
| 5621 | bool ForEH) { |
| 5622 | // Return a bogus pointer if RTTI is disabled, unless it's for EH. |
| 5623 | // FIXME: should we even be calling this method if RTTI is disabled |
| 5624 | // and it's not for EH? |
Yaxun Liu | 6328f9a | 2018-06-05 15:11:02 +0000 | [diff] [blame] | 5625 | if ((!ForEH && !getLangOpts().RTTI) || getLangOpts().CUDAIsDevice) |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 5626 | return llvm::Constant::getNullValue(Int8PtrTy); |
Jake Ehrlich | c451cf2 | 2017-11-11 01:15:41 +0000 | [diff] [blame] | 5627 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 5628 | if (ForEH && Ty->isObjCObjectPointerType() && |
| 5629 | LangOpts.ObjCRuntime.isGNUFamily()) |
| 5630 | return ObjCRuntime->GetEHType(Ty); |
| 5631 | |
David Majnemer | 443250f | 2015-03-17 20:35:00 +0000 | [diff] [blame] | 5632 | return getCXXABI().getAddrOfRTTIDescriptor(Ty); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 5635 | void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5636 | // Do not emit threadprivates in simd-only mode. |
| 5637 | if (LangOpts.OpenMP && LangOpts.OpenMPSimd) |
| 5638 | return; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 5639 | for (auto RefExpr : D->varlists()) { |
| 5640 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl()); |
| 5641 | bool PerformInit = |
| 5642 | VD->getAnyInitializer() && |
| 5643 | !VD->getAnyInitializer()->isConstantInitializer(getContext(), |
| 5644 | /*ForRef=*/false); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5645 | |
| 5646 | Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD)); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 5647 | if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5648 | VD, Addr, RefExpr->getBeginLoc(), PerformInit)) |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 5649 | CXXGlobalInits.push_back(InitFunction); |
| 5650 | } |
| 5651 | } |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 5652 | |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 5653 | llvm::Metadata * |
| 5654 | CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, |
| 5655 | StringRef Suffix) { |
| 5656 | llvm::Metadata *&InternalId = Map[T.getCanonicalType()]; |
Peter Collingbourne | 25a80bf | 2015-09-08 23:01:30 +0000 | [diff] [blame] | 5657 | if (InternalId) |
| 5658 | return InternalId; |
| 5659 | |
| 5660 | if (isExternallyVisible(T->getLinkage())) { |
| 5661 | std::string OutName; |
| 5662 | llvm::raw_string_ostream Out(OutName); |
| 5663 | getCXXABI().getMangleContext().mangleTypeName(T, Out); |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 5664 | Out << Suffix; |
Peter Collingbourne | 25a80bf | 2015-09-08 23:01:30 +0000 | [diff] [blame] | 5665 | |
| 5666 | InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); |
| 5667 | } else { |
| 5668 | InternalId = llvm::MDNode::getDistinct(getLLVMContext(), |
| 5669 | llvm::ArrayRef<llvm::Metadata *>()); |
| 5670 | } |
| 5671 | |
| 5672 | return InternalId; |
| 5673 | } |
| 5674 | |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 5675 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { |
| 5676 | return CreateMetadataIdentifierImpl(T, MetadataIdMap, ""); |
| 5677 | } |
| 5678 | |
| 5679 | llvm::Metadata * |
| 5680 | CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) { |
| 5681 | return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual"); |
| 5682 | } |
| 5683 | |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 5684 | // Generalize pointer types to a void pointer with the qualifiers of the |
| 5685 | // originally pointed-to type, e.g. 'const char *' and 'char * const *' |
| 5686 | // generalize to 'const void *' while 'char *' and 'const char **' generalize to |
| 5687 | // 'void *'. |
| 5688 | static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) { |
| 5689 | if (!Ty->isPointerType()) |
| 5690 | return Ty; |
| 5691 | |
| 5692 | return Ctx.getPointerType( |
| 5693 | QualType(Ctx.VoidTy).withCVRQualifiers( |
| 5694 | Ty->getPointeeType().getCVRQualifiers())); |
| 5695 | } |
| 5696 | |
| 5697 | // Apply type generalization to a FunctionType's return and argument types |
| 5698 | static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) { |
| 5699 | if (auto *FnType = Ty->getAs<FunctionProtoType>()) { |
| 5700 | SmallVector<QualType, 8> GeneralizedParams; |
| 5701 | for (auto &Param : FnType->param_types()) |
| 5702 | GeneralizedParams.push_back(GeneralizeType(Ctx, Param)); |
| 5703 | |
| 5704 | return Ctx.getFunctionType( |
| 5705 | GeneralizeType(Ctx, FnType->getReturnType()), |
| 5706 | GeneralizedParams, FnType->getExtProtoInfo()); |
| 5707 | } |
| 5708 | |
| 5709 | if (auto *FnType = Ty->getAs<FunctionNoProtoType>()) |
| 5710 | return Ctx.getFunctionNoProtoType( |
| 5711 | GeneralizeType(Ctx, FnType->getReturnType())); |
| 5712 | |
| 5713 | llvm_unreachable("Encountered unknown FunctionType"); |
| 5714 | } |
| 5715 | |
| 5716 | llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { |
Peter Collingbourne | e44acad | 2018-06-26 02:15:47 +0000 | [diff] [blame] | 5717 | return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T), |
| 5718 | GeneralizedMetadataIdMap, ".generalized"); |
Vlad Tsyrklevich | 634c601 | 2017-10-31 22:39:44 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 5721 | /// Returns whether this module needs the "all-vtables" type identifier. |
| 5722 | bool CodeGenModule::NeedAllVtablesTypeId() const { |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 5723 | // Returns true if at least one of vtable-based CFI checkers is enabled and |
| 5724 | // is not in the trapping mode. |
| 5725 | return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) && |
| 5726 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) || |
| 5727 | (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) && |
| 5728 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) || |
| 5729 | (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) && |
| 5730 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) || |
| 5731 | (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) && |
| 5732 | !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast))); |
| 5733 | } |
| 5734 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 5735 | void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable, |
| 5736 | CharUnits Offset, |
| 5737 | const CXXRecordDecl *RD) { |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 5738 | llvm::Metadata *MD = |
| 5739 | CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 5740 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 5741 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 5742 | if (CodeGenOpts.SanitizeCfiCrossDso) |
| 5743 | if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) |
| 5744 | VTable->addTypeMetadata(Offset.getQuantity(), |
| 5745 | llvm::ConstantAsMetadata::get(CrossDsoTypeId)); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 5746 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 5747 | if (NeedAllVtablesTypeId()) { |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 5748 | llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables"); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 5749 | VTable->addTypeMetadata(Offset.getQuantity(), MD); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 5750 | } |
Peter Collingbourne | 86d34a7 | 2015-06-17 19:08:05 +0000 | [diff] [blame] | 5751 | } |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5752 | |
Gabor Buella | 1a83d06 | 2018-06-07 08:48:36 +0000 | [diff] [blame] | 5753 | TargetAttr::ParsedTargetAttr CodeGenModule::filterFunctionTargetAttrs(const TargetAttr *TD) { |
| 5754 | assert(TD != nullptr); |
| 5755 | TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse(); |
| 5756 | |
| 5757 | ParsedAttr.Features.erase( |
| 5758 | llvm::remove_if(ParsedAttr.Features, |
| 5759 | [&](const std::string &Feat) { |
| 5760 | return !Target.isValidFeatureName( |
| 5761 | StringRef{Feat}.substr(1)); |
| 5762 | }), |
| 5763 | ParsedAttr.Features.end()); |
| 5764 | return ParsedAttr; |
| 5765 | } |
| 5766 | |
| 5767 | |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5768 | // Fills in the supplied string map with the set of target features for the |
| 5769 | // passed in function. |
| 5770 | void CodeGenModule::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 5771 | GlobalDecl GD) { |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5772 | StringRef TargetCPU = Target.getTargetOpts().CPU; |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 5773 | const FunctionDecl *FD = GD.getDecl()->getAsFunction(); |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5774 | if (const auto *TD = FD->getAttr<TargetAttr>()) { |
Gabor Buella | 1a83d06 | 2018-06-07 08:48:36 +0000 | [diff] [blame] | 5775 | TargetAttr::ParsedTargetAttr ParsedAttr = filterFunctionTargetAttrs(TD); |
Erich Keane | cf8807c | 2017-10-27 18:32:23 +0000 | [diff] [blame] | 5776 | |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5777 | // Make a copy of the features as passed on the command line into the |
| 5778 | // beginning of the additional features from the function to override. |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame] | 5779 | ParsedAttr.Features.insert(ParsedAttr.Features.begin(), |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5780 | Target.getTargetOpts().FeaturesAsWritten.begin(), |
| 5781 | Target.getTargetOpts().FeaturesAsWritten.end()); |
| 5782 | |
Erich Keane | cf8807c | 2017-10-27 18:32:23 +0000 | [diff] [blame] | 5783 | if (ParsedAttr.Architecture != "" && |
| 5784 | Target.isValidCPUName(ParsedAttr.Architecture)) |
| 5785 | TargetCPU = ParsedAttr.Architecture; |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5786 | |
| 5787 | // Now populate the feature map, first with the TargetCPU which is either |
| 5788 | // the default or a new one from the target attribute string. Then we'll use |
| 5789 | // the passed in features (FeaturesAsWritten) along with the new ones from |
| 5790 | // the attribute. |
Erich Keane | b0d4423 | 2017-07-18 20:41:02 +0000 | [diff] [blame] | 5791 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
| 5792 | ParsedAttr.Features); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 5793 | } else if (const auto *SD = FD->getAttr<CPUSpecificAttr>()) { |
| 5794 | llvm::SmallVector<StringRef, 32> FeaturesTmp; |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 5795 | Target.getCPUSpecificCPUDispatchFeatures( |
| 5796 | SD->getCPUName(GD.getMultiVersionIndex())->getName(), FeaturesTmp); |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 5797 | std::vector<std::string> Features(FeaturesTmp.begin(), FeaturesTmp.end()); |
| 5798 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, Features); |
Eric Christopher | 2b90a64 | 2015-11-11 23:05:08 +0000 | [diff] [blame] | 5799 | } else { |
| 5800 | Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, |
| 5801 | Target.getTargetOpts().Features); |
| 5802 | } |
| 5803 | } |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 5804 | |
| 5805 | llvm::SanitizerStatReport &CodeGenModule::getSanStats() { |
| 5806 | if (!SanStats) |
| 5807 | SanStats = llvm::make_unique<llvm::SanitizerStatReport>(&getModule()); |
| 5808 | |
| 5809 | return *SanStats; |
| 5810 | } |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5811 | llvm::Value * |
| 5812 | CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, |
| 5813 | CodeGenFunction &CGF) { |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 5814 | llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType()); |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 5815 | auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr()); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 5816 | auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); |
| 5817 | return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy, |
| 5818 | "__translate_sampler_initializer"), |
| 5819 | {C}); |
| 5820 | } |