Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "CodeGenFunction.h" |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 23 | #include "CodeGenPGO.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 24 | #include "CodeGenTBAA.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 25 | #include "TargetInfo.h" |
Chris Lattner | 2ccb73b | 2007-06-16 00:16:26 +0000 | [diff] [blame] | 26 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/CharUnits.h" |
Chris Lattner | b8c18fa | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 5dd3474 | 2010-06-21 18:41:26 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclTemplate.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 31 | #include "clang/AST/Mangle.h" |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 32 | #include "clang/AST/RecordLayout.h" |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 33 | #include "clang/AST/RecursiveASTVisitor.h" |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 34 | #include "clang/Basic/Builtins.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 35 | #include "clang/Basic/CharInfo.h" |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 36 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 37 | #include "clang/Basic/Module.h" |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 38 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 39 | #include "clang/Basic/TargetInfo.h" |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 40 | #include "clang/Basic/Version.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "clang/Frontend/CodeGenOptions.h" |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 42 | #include "clang/Sema/SemaDiagnostic.h" |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/APSInt.h" |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 45 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 46 | #include "llvm/IR/CallingConv.h" |
| 47 | #include "llvm/IR/DataLayout.h" |
| 48 | #include "llvm/IR/Intrinsics.h" |
| 49 | #include "llvm/IR/LLVMContext.h" |
| 50 | #include "llvm/IR/Module.h" |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 51 | #include "llvm/ProfileData/InstrProfReader.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ConvertUTF.h" |
Chris Lattner | 15275e5 | 2009-11-07 09:22:46 +0000 | [diff] [blame] | 53 | #include "llvm/Support/ErrorHandling.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 54 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 55 | using namespace clang; |
| 56 | using namespace CodeGen; |
| 57 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 58 | static const char AnnotationSection[] = "llvm.metadata"; |
| 59 | |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 60 | static CGCXXABI *createCXXABI(CodeGenModule &CGM) { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 61 | switch (CGM.getTarget().getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 62 | case TargetCXXABI::GenericAArch64: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 63 | case TargetCXXABI::GenericARM: |
| 64 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 65 | case TargetCXXABI::iOS64: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 66 | case TargetCXXABI::GenericItanium: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 67 | return CreateItaniumCXXABI(CGM); |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 68 | case TargetCXXABI::Microsoft: |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 69 | return CreateMicrosoftCXXABI(CGM); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | llvm_unreachable("invalid C++ ABI kind"); |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 75 | CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, |
John McCall | 568d410 | 2013-04-16 22:48:20 +0000 | [diff] [blame] | 76 | llvm::Module &M, const llvm::DataLayout &TD, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 77 | DiagnosticsEngine &diags) |
Alexey Samsonov | d6e043b | 2013-08-12 11:48:05 +0000 | [diff] [blame] | 78 | : Context(C), LangOpts(C.getLangOpts()), CodeGenOpts(CGO), TheModule(M), |
| 79 | Diags(diags), TheDataLayout(TD), Target(C.getTargetInfo()), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 80 | ABI(createCXXABI(*this)), VMContext(M.getContext()), TBAA(nullptr), |
| 81 | TheTargetCodeGenInfo(nullptr), Types(*this), VTables(*this), |
| 82 | ObjCRuntime(nullptr), OpenCLRuntime(nullptr), OpenMPRuntime(nullptr), |
| 83 | CUDARuntime(nullptr), DebugInfo(nullptr), ARCData(nullptr), |
| 84 | NoObjCARCExceptionsMetadata(nullptr), RRData(nullptr), PGOReader(nullptr), |
| 85 | CFConstantStringClassRef(nullptr), ConstantStringClassRef(nullptr), |
| 86 | NSConstantStringType(nullptr), NSConcreteGlobalBlock(nullptr), |
| 87 | NSConcreteStackBlock(nullptr), BlockObjectAssign(nullptr), |
| 88 | BlockObjectDispose(nullptr), BlockDescriptorType(nullptr), |
| 89 | GenericBlockLiteralType(nullptr), LifetimeStartFn(nullptr), |
| 90 | LifetimeEndFn(nullptr), |
Alexey Samsonov | d6e043b | 2013-08-12 11:48:05 +0000 | [diff] [blame] | 91 | SanitizerBlacklist( |
| 92 | llvm::SpecialCaseList::createOrDie(CGO.SanitizerBlacklistFile)), |
| 93 | SanOpts(SanitizerBlacklist->isIn(M) ? SanitizerOptions::Disabled |
| 94 | : LangOpts.Sanitize) { |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 95 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 96 | // Initialize the type cache. |
| 97 | llvm::LLVMContext &LLVMContext = M.getContext(); |
| 98 | VoidTy = llvm::Type::getVoidTy(LLVMContext); |
| 99 | Int8Ty = llvm::Type::getInt8Ty(LLVMContext); |
| 100 | Int16Ty = llvm::Type::getInt16Ty(LLVMContext); |
| 101 | Int32Ty = llvm::Type::getInt32Ty(LLVMContext); |
| 102 | Int64Ty = llvm::Type::getInt64Ty(LLVMContext); |
| 103 | FloatTy = llvm::Type::getFloatTy(LLVMContext); |
| 104 | DoubleTy = llvm::Type::getDoubleTy(LLVMContext); |
| 105 | PointerWidthInBits = C.getTargetInfo().getPointerWidth(0); |
| 106 | PointerAlignInBytes = |
| 107 | C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity(); |
| 108 | IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); |
| 109 | IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits); |
| 110 | Int8PtrTy = Int8Ty->getPointerTo(0); |
| 111 | Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); |
| 112 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 113 | RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); |
| 114 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 115 | if (LangOpts.ObjC1) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 116 | createObjCRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 117 | if (LangOpts.OpenCL) |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 118 | createOpenCLRuntime(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 119 | if (LangOpts.OpenMP) |
| 120 | createOpenMPRuntime(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 121 | if (LangOpts.CUDA) |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 122 | createCUDARuntime(); |
Sanjiv Gupta | 15cb669 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 123 | |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 124 | // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0. |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 125 | if (SanOpts.Thread || |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 126 | (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)) |
| 127 | TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(), |
Alp Toker | 8286225 | 2013-12-28 21:58:40 +0000 | [diff] [blame] | 128 | getCXXABI().getMangleContext()); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 129 | |
Nick Lewycky | 207bce3 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 130 | // If debug info or coverage generation is enabled, create the CGDebugInfo |
| 131 | // object. |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 132 | if (CodeGenOpts.getDebugInfo() != CodeGenOptions::NoDebugInfo || |
Alexey Samsonov | 486e1fe | 2012-04-27 07:24:20 +0000 | [diff] [blame] | 133 | CodeGenOpts.EmitGcovArcs || |
Nick Lewycky | 207bce3 | 2011-04-21 23:44:07 +0000 | [diff] [blame] | 134 | CodeGenOpts.EmitGcovNotes) |
| 135 | DebugInfo = new CGDebugInfo(*this); |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 136 | |
| 137 | Block.GlobalUniqueCount = 0; |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 138 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 139 | if (C.getLangOpts().ObjCAutoRefCount) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 140 | ARCData = new ARCEntrypoints(); |
| 141 | RRData = new RREntrypoints(); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 142 | |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 143 | if (!CodeGenOpts.InstrProfileInput.empty()) { |
| 144 | if (llvm::error_code EC = llvm::IndexedInstrProfReader::create( |
| 145 | CodeGenOpts.InstrProfileInput, PGOReader)) { |
| 146 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 147 | "Could not read profile: %0"); |
| 148 | getDiags().Report(DiagID) << EC.message(); |
| 149 | } |
| 150 | } |
Chris Lattner | a087ff9 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | CodeGenModule::~CodeGenModule() { |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 154 | delete ObjCRuntime; |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 155 | delete OpenCLRuntime; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 156 | delete OpenMPRuntime; |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 157 | delete CUDARuntime; |
Ted Kremenek | c168e50 | 2011-10-08 05:28:26 +0000 | [diff] [blame] | 158 | delete TheTargetCodeGenInfo; |
Dan Gohman | d19ee8a | 2010-10-15 18:04:46 +0000 | [diff] [blame] | 159 | delete TBAA; |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 160 | delete DebugInfo; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 161 | delete ARCData; |
| 162 | delete RRData; |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 163 | } |
| 164 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 165 | void CodeGenModule::createObjCRuntime() { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 166 | // This is just isGNUFamily(), but we want to force implementors of |
| 167 | // new ABIs to decide how best to do this. |
| 168 | switch (LangOpts.ObjCRuntime.getKind()) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 169 | case ObjCRuntime::GNUstep: |
| 170 | case ObjCRuntime::GCC: |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 171 | case ObjCRuntime::ObjFW: |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 172 | ObjCRuntime = CreateGNUObjCRuntime(*this); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 173 | return; |
| 174 | |
| 175 | case ObjCRuntime::FragileMacOSX: |
| 176 | case ObjCRuntime::MacOSX: |
| 177 | case ObjCRuntime::iOS: |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 178 | ObjCRuntime = CreateMacObjCRuntime(*this); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | llvm_unreachable("bad runtime kind"); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 184 | void CodeGenModule::createOpenCLRuntime() { |
| 185 | OpenCLRuntime = new CGOpenCLRuntime(*this); |
| 186 | } |
| 187 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 188 | void CodeGenModule::createOpenMPRuntime() { |
| 189 | OpenMPRuntime = new CGOpenMPRuntime(*this); |
| 190 | } |
| 191 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 192 | void CodeGenModule::createCUDARuntime() { |
| 193 | CUDARuntime = CreateNVCUDARuntime(*this); |
| 194 | } |
| 195 | |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 196 | void CodeGenModule::applyReplacements() { |
| 197 | for (ReplacementsTy::iterator I = Replacements.begin(), |
| 198 | E = Replacements.end(); |
| 199 | I != E; ++I) { |
| 200 | StringRef MangledName = I->first(); |
| 201 | llvm::Constant *Replacement = I->second; |
| 202 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 203 | if (!Entry) |
| 204 | continue; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 205 | auto *OldF = cast<llvm::Function>(Entry); |
| 206 | auto *NewF = dyn_cast<llvm::Function>(Replacement); |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 207 | if (!NewF) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 208 | if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) { |
Rafael Espindola | 0fcad88 | 2014-05-16 22:20:18 +0000 | [diff] [blame] | 209 | NewF = dyn_cast<llvm::Function>(Alias->getAliasee()); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 210 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 211 | auto *CE = cast<llvm::ConstantExpr>(Replacement); |
Reid Kleckner | b78257d | 2014-02-03 18:54:51 +0000 | [diff] [blame] | 212 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 213 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
| 214 | NewF = dyn_cast<llvm::Function>(CE->getOperand(0)); |
| 215 | } |
Rafael Espindola | 0196a1d | 2013-11-12 04:53:19 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Replace old with new, but keep the old order. |
| 219 | OldF->replaceAllUsesWith(Replacement); |
| 220 | if (NewF) { |
| 221 | NewF->removeFromParent(); |
| 222 | OldF->getParent()->getFunctionList().insertAfter(OldF, NewF); |
| 223 | } |
| 224 | OldF->eraseFromParent(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 228 | void CodeGenModule::checkAliases() { |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 229 | // Check if the constructed aliases are well formed. It is really unfortunate |
| 230 | // that we have to do this in CodeGen, but we only construct mangled names |
| 231 | // and aliases during codegen. |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 232 | bool Error = false; |
Rafael Espindola | c67b815 | 2014-05-05 19:33:09 +0000 | [diff] [blame] | 233 | DiagnosticsEngine &Diags = getDiags(); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 234 | for (std::vector<GlobalDecl>::iterator I = Aliases.begin(), |
| 235 | E = Aliases.end(); I != E; ++I) { |
| 236 | const GlobalDecl &GD = *I; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 237 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 238 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
| 239 | StringRef MangledName = getMangledName(GD); |
| 240 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 241 | auto *Alias = cast<llvm::GlobalAlias>(Entry); |
Rafael Espindola | 0fcad88 | 2014-05-16 22:20:18 +0000 | [diff] [blame] | 242 | llvm::GlobalValue *GV = Alias->getAliasee(); |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 243 | if (GV->isDeclaration()) { |
Rafael Espindola | 93c7a65 | 2014-03-13 15:47:50 +0000 | [diff] [blame] | 244 | Error = true; |
Rafael Espindola | c67b815 | 2014-05-05 19:33:09 +0000 | [diff] [blame] | 245 | Diags.Report(AA->getLocation(), diag::err_alias_to_undefined); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 246 | } |
Rafael Espindola | a39fc6d | 2014-03-27 15:27:20 +0000 | [diff] [blame] | 247 | |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 248 | llvm::GlobalObject *Aliasee = Alias->getAliasee(); |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 249 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { |
| 250 | StringRef AliasSection = SA->getName(); |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 251 | if (AliasSection != Aliasee->getSection()) |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 252 | Diags.Report(SA->getLocation(), diag::warn_alias_with_section) |
| 253 | << AliasSection; |
| 254 | } |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 255 | } |
| 256 | if (!Error) |
| 257 | return; |
| 258 | |
| 259 | for (std::vector<GlobalDecl>::iterator I = Aliases.begin(), |
| 260 | E = Aliases.end(); I != E; ++I) { |
| 261 | const GlobalDecl &GD = *I; |
| 262 | StringRef MangledName = getMangledName(GD); |
| 263 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 264 | auto *Alias = cast<llvm::GlobalAlias>(Entry); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 265 | Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType())); |
| 266 | Alias->eraseFromParent(); |
| 267 | } |
| 268 | } |
| 269 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 270 | void CodeGenModule::clear() { |
| 271 | DeferredDeclsToEmit.clear(); |
| 272 | } |
| 273 | |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 274 | void CodeGenModule::Release() { |
Chris Lattner | a5ae54a | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 275 | EmitDeferred(); |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 276 | applyReplacements(); |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 277 | checkAliases(); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 278 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | fe06df4 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 279 | EmitCXXGlobalDtorFunc(); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 280 | EmitCXXThreadLocalInitFunc(); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 281 | if (ObjCRuntime) |
| 282 | if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 283 | AddGlobalCtor(ObjCInitFunction); |
Justin Bogner | f2ea775 | 2014-04-10 18:13:13 +0000 | [diff] [blame] | 284 | if (getCodeGenOpts().ProfileInstrGenerate) |
| 285 | if (llvm::Function *PGOInit = CodeGenPGO::emitInitialization(*this)) |
| 286 | AddGlobalCtor(PGOInit, 0); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 287 | if (PGOReader && PGOStats.isOutOfDate()) |
Justin Bogner | e2ef2a0 | 2014-04-15 21:22:35 +0000 | [diff] [blame] | 288 | getDiags().Report(diag::warn_profile_data_out_of_date) |
| 289 | << PGOStats.Visited << PGOStats.Missing << PGOStats.Mismatched; |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 290 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 291 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 292 | EmitGlobalAnnotations(); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 293 | EmitStaticExternCAliases(); |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 294 | emitLLVMUsed(); |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 295 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 296 | if (CodeGenOpts.Autolink && |
| 297 | (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { |
Douglas Gregor | c60437f | 2013-01-16 01:23:41 +0000 | [diff] [blame] | 298 | EmitModuleLinkOptions(); |
| 299 | } |
Manman Ren | 9691f7f | 2013-06-19 01:46:49 +0000 | [diff] [blame] | 300 | if (CodeGenOpts.DwarfVersion) |
| 301 | // We actually want the latest version when there are conflicts. |
| 302 | // We can change from Warning to Latest if such mode is supported. |
| 303 | getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version", |
| 304 | CodeGenOpts.DwarfVersion); |
Manman Ren | 4b7f23d | 2013-11-22 19:42:45 +0000 | [diff] [blame] | 305 | if (DebugInfo) |
Adrian Prantl | 2dbdd20 | 2014-05-19 23:40:06 +0000 | [diff] [blame] | 306 | // We support a single version in the linked module. The LLVM |
| 307 | // parser will drop debug info with a different version number |
| 308 | // (and warn about it, too). |
| 309 | getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version", |
Manman Ren | 61fd594 | 2013-12-02 20:10:37 +0000 | [diff] [blame] | 310 | llvm::DEBUG_METADATA_VERSION); |
Douglas Gregor | ea02f26 | 2013-01-14 18:28:43 +0000 | [diff] [blame] | 311 | |
John McCall | 0bdb1fd | 2010-09-16 06:16:50 +0000 | [diff] [blame] | 312 | SimplifyPersonality(); |
| 313 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 314 | if (getCodeGenOpts().EmitDeclMetadata) |
| 315 | EmitDeclMetadata(); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 316 | |
| 317 | if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 318 | EmitCoverageFile(); |
Devang Patel | ffa30ab | 2011-08-16 20:58:22 +0000 | [diff] [blame] | 319 | |
| 320 | if (DebugInfo) |
| 321 | DebugInfo->finalize(); |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 322 | |
| 323 | EmitVersionIdentMetadata(); |
Daniel Dunbar | 23fd462 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 326 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 327 | // Make sure that this type is translated. |
| 328 | Types.UpdateCompletedType(TD); |
Devang Patel | 945b8ae | 2011-03-23 16:29:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 331 | llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) { |
| 332 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 333 | return nullptr; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 334 | return TBAA->getTBAAInfo(QTy); |
| 335 | } |
| 336 | |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 337 | llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() { |
| 338 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 339 | return nullptr; |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 340 | return TBAA->getTBAAInfoForVTablePtr(); |
| 341 | } |
| 342 | |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 343 | llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { |
| 344 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 345 | return nullptr; |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 346 | return TBAA->getTBAAStructInfo(QTy); |
| 347 | } |
| 348 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 349 | llvm::MDNode *CodeGenModule::getTBAAStructTypeInfo(QualType QTy) { |
| 350 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 351 | return nullptr; |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 352 | return TBAA->getTBAAStructTypeInfo(QTy); |
| 353 | } |
| 354 | |
| 355 | llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy, |
| 356 | llvm::MDNode *AccessN, |
| 357 | uint64_t O) { |
| 358 | if (!TBAA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 359 | return nullptr; |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 360 | return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O); |
| 361 | } |
| 362 | |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 363 | /// Decorate the instruction with a TBAA tag. For both scalar TBAA |
| 364 | /// and struct-path aware TBAA, the tag has the same format: |
| 365 | /// base type, access type and offset. |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 366 | /// When ConvertTypeToTag is true, we create a tag based on the scalar type. |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 367 | void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst, |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 368 | llvm::MDNode *TBAAInfo, |
| 369 | bool ConvertTypeToTag) { |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 370 | if (ConvertTypeToTag && TBAA) |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 371 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, |
| 372 | TBAA->getTBAAScalarTagInfo(TBAAInfo)); |
| 373 | else |
| 374 | Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 377 | void CodeGenModule::Error(SourceLocation loc, StringRef message) { |
| 378 | unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0"); |
| 379 | getDiags().Report(Context.getFullLoc(loc), diagID) << message; |
John McCall | 7ef5cb3 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 382 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 383 | /// specified stmt yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 384 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 385 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 386 | "cannot compile this %0 yet"); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 387 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 388 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 389 | << Msg << S->getSourceRange(); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 390 | } |
Chris Lattner | 41af818 | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 392 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 393 | /// specified decl yet. |
David Blaikie | 4a9ec7b | 2013-08-19 21:02:26 +0000 | [diff] [blame] | 394 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 395 | unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 396 | "cannot compile this %0 yet"); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 397 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 398 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 399 | } |
| 400 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 401 | llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { |
| 402 | return llvm::ConstantInt::get(SizeTy, size.getQuantity()); |
| 403 | } |
| 404 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Anders Carlsson | c6a4789 | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 406 | const NamedDecl *D) const { |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 407 | // Internal definitions always have default visibility. |
Chris Lattner | 6a0f907 | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 408 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 409 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 410 | return; |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 411 | } |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 412 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 413 | // Set visibility for definitions. |
Rafael Espindola | a8fbdab | 2013-02-27 02:15:29 +0000 | [diff] [blame] | 414 | LinkageInfo LV = D->getLinkageAndVisibility(); |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 415 | if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage()) |
| 416 | GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 419 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) { |
| 420 | return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S) |
| 421 | .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel) |
| 422 | .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel) |
| 423 | .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel) |
| 424 | .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel); |
| 425 | } |
| 426 | |
| 427 | static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel( |
| 428 | CodeGenOptions::TLSModel M) { |
| 429 | switch (M) { |
| 430 | case CodeGenOptions::GeneralDynamicTLSModel: |
| 431 | return llvm::GlobalVariable::GeneralDynamicTLSModel; |
| 432 | case CodeGenOptions::LocalDynamicTLSModel: |
| 433 | return llvm::GlobalVariable::LocalDynamicTLSModel; |
| 434 | case CodeGenOptions::InitialExecTLSModel: |
| 435 | return llvm::GlobalVariable::InitialExecTLSModel; |
| 436 | case CodeGenOptions::LocalExecTLSModel: |
| 437 | return llvm::GlobalVariable::LocalExecTLSModel; |
| 438 | } |
| 439 | llvm_unreachable("Invalid TLS model!"); |
| 440 | } |
| 441 | |
| 442 | void CodeGenModule::setTLSMode(llvm::GlobalVariable *GV, |
| 443 | const VarDecl &D) const { |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 444 | assert(D.getTLSKind() && "setting TLS mode on non-TLS var!"); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 445 | |
| 446 | llvm::GlobalVariable::ThreadLocalMode TLM; |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 447 | TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel()); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 448 | |
| 449 | // Override the TLS model if it is explicitly specified. |
Aaron Ballman | c432799 | 2013-12-19 03:09:10 +0000 | [diff] [blame] | 450 | if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) { |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 451 | TLM = GetLLVMTLSModel(Attr->getModel()); |
| 452 | } |
| 453 | |
| 454 | GV->setThreadLocalMode(TLM); |
| 455 | } |
| 456 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 457 | StringRef CodeGenModule::getMangledName(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 458 | const auto *ND = cast<NamedDecl>(GD.getDecl()); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 460 | StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()]; |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 461 | if (!Str.empty()) |
| 462 | return Str; |
| 463 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 464 | if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) { |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 465 | IdentifierInfo *II = ND->getIdentifier(); |
| 466 | assert(II && "Attempt to mangle unnamed decl."); |
| 467 | |
| 468 | Str = II->getName(); |
| 469 | return Str; |
| 470 | } |
| 471 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 472 | SmallString<256> Buffer; |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 473 | llvm::raw_svector_ostream Out(Buffer); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 474 | if (const auto *D = dyn_cast<CXXConstructorDecl>(ND)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 475 | getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 476 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 477 | getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 478 | else |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 479 | getCXXABI().getMangleContext().mangleName(ND, Out); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 480 | |
| 481 | // Allocate space for the mangled name. |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 482 | Out.flush(); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 483 | size_t Length = Buffer.size(); |
| 484 | char *Name = MangledNamesAllocator.Allocate<char>(Length); |
| 485 | std::copy(Buffer.begin(), Buffer.end(), Name); |
| 486 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 487 | Str = StringRef(Name, Length); |
Anders Carlsson | 2e2f4d2 | 2010-06-22 16:05:32 +0000 | [diff] [blame] | 488 | |
| 489 | return Str; |
| 490 | } |
| 491 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 492 | void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, |
| 493 | const BlockDecl *BD) { |
| 494 | MangleContext &MangleCtx = getCXXABI().getMangleContext(); |
| 495 | const Decl *D = GD.getDecl(); |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 496 | llvm::raw_svector_ostream Out(Buffer.getBuffer()); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 497 | if (!D) |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 498 | MangleCtx.mangleGlobalBlock(BD, |
| 499 | dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 500 | else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 501 | MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 502 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 503 | MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 504 | else |
Rafael Espindola | ac00f5d | 2011-02-10 23:59:36 +0000 | [diff] [blame] | 505 | MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); |
Anders Carlsson | 635186a | 2010-06-09 02:36:32 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 508 | llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 509 | return getModule().getNamedValue(Name); |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 512 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 513 | /// main() runs. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 514 | void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority, |
| 515 | llvm::Constant *AssociatedData) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 516 | // FIXME: Type coercion of void()* types. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 517 | GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 520 | /// AddGlobalDtor - Add a function to the list that will be called |
| 521 | /// when the module is unloaded. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 522 | void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 523 | // FIXME: Type coercion of void()* types. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 524 | GlobalDtors.push_back(Structor(Priority, Dtor, 0)); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 528 | // Ctor function type is void()*. |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 529 | llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 530 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 531 | |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 532 | // Get the type of a ctor entry, { i32, void ()*, i8* }. |
| 533 | llvm::StructType *CtorStructTy = llvm::StructType::get( |
| 534 | Int32Ty, llvm::PointerType::getUnqual(CtorFTy), VoidPtrTy, NULL); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 536 | // Construct the constructor and destructor arrays. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 537 | SmallVector<llvm::Constant*, 8> Ctors; |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 538 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 539 | llvm::Constant *S[] = { |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 540 | llvm::ConstantInt::get(Int32Ty, I->Priority, false), |
| 541 | llvm::ConstantExpr::getBitCast(I->Initializer, CtorPFTy), |
| 542 | (I->AssociatedData |
| 543 | ? llvm::ConstantExpr::getBitCast(I->AssociatedData, VoidPtrTy) |
| 544 | : llvm::Constant::getNullValue(VoidPtrTy)) |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 545 | }; |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 546 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 547 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 548 | |
| 549 | if (!Ctors.empty()) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 550 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 551 | new llvm::GlobalVariable(TheModule, AT, false, |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 552 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 553 | llvm::ConstantArray::get(AT, Ctors), |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 554 | GlobalName); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 555 | } |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 556 | } |
| 557 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 558 | llvm::GlobalValue::LinkageTypes |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 559 | CodeGenModule::getFunctionLinkage(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 560 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 561 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 562 | GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 563 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame^] | 564 | if (isa<CXXDestructorDecl>(D) && |
Reid Kleckner | a12cd28 | 2013-12-11 19:21:27 +0000 | [diff] [blame] | 565 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame^] | 566 | GD.getDtorType())) { |
| 567 | // Destructor variants in the Microsoft C++ ABI are always internal or |
| 568 | // linkonce_odr thunks emitted on an as-needed basis. |
| 569 | return Linkage == GVA_Internal ? llvm::GlobalValue::InternalLinkage |
| 570 | : llvm::GlobalValue::LinkOnceODRLinkage; |
| 571 | } |
Reid Kleckner | a12cd28 | 2013-12-11 19:21:27 +0000 | [diff] [blame] | 572 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame^] | 573 | return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 574 | } |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 575 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 576 | void CodeGenModule::setFunctionDefinitionAttributes(const FunctionDecl *D, |
| 577 | llvm::Function *F) { |
| 578 | setNonAliasAttributes(D, F); |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 581 | void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | const CGFunctionInfo &Info, |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 583 | llvm::Function *F) { |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 584 | unsigned CallingConv; |
Devang Patel | 322300d | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 585 | AttributeListType AttributeList; |
Bill Wendling | f4d64cb | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 586 | ConstructAttributeList(Info, D, AttributeList, CallingConv, false); |
Bill Wendling | 3087d02 | 2012-12-07 23:17:26 +0000 | [diff] [blame] | 587 | F->setAttributes(llvm::AttributeSet::get(getLLVMContext(), AttributeList)); |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 588 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 589 | } |
| 590 | |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 591 | /// Determines whether the language options require us to model |
| 592 | /// unwind exceptions. We treat -fexceptions as mandating this |
| 593 | /// except under the fragile ObjC ABI with only ObjC exceptions |
| 594 | /// enabled. This means, for example, that C with -fexceptions |
| 595 | /// enables this. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 596 | static bool hasUnwindExceptions(const LangOptions &LangOpts) { |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 597 | // If exceptions are completely disabled, obviously this is false. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 598 | if (!LangOpts.Exceptions) return false; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 599 | |
| 600 | // If C++ exceptions are enabled, this is true. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 601 | if (LangOpts.CXXExceptions) return true; |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 602 | |
| 603 | // If ObjC exceptions are enabled, this depends on the ABI. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 604 | if (LangOpts.ObjCExceptions) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 605 | return LangOpts.ObjCRuntime.hasUnwindExceptions(); |
John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | return true; |
| 609 | } |
| 610 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 611 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 612 | llvm::Function *F) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 613 | llvm::AttrBuilder B; |
| 614 | |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 615 | if (CodeGenOpts.UnwindTables) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 616 | B.addAttribute(llvm::Attribute::UWTable); |
Rafael Espindola | c1ee12c | 2011-05-25 03:44:55 +0000 | [diff] [blame] | 617 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 618 | if (!hasUnwindExceptions(LangOpts)) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 619 | B.addAttribute(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 620 | |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 621 | if (D->hasAttr<NakedAttr>()) { |
| 622 | // Naked implies noinline: we should not be inlining such functions. |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 623 | B.addAttribute(llvm::Attribute::Naked); |
| 624 | B.addAttribute(llvm::Attribute::NoInline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 625 | } else if (D->hasAttr<OptimizeNoneAttr>()) { |
| 626 | // OptimizeNone implies noinline; we should not be inlining such functions. |
| 627 | B.addAttribute(llvm::Attribute::OptimizeNone); |
| 628 | B.addAttribute(llvm::Attribute::NoInline); |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 629 | } else if (D->hasAttr<NoDuplicateAttr>()) { |
| 630 | B.addAttribute(llvm::Attribute::NoDuplicate); |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 631 | } else if (D->hasAttr<NoInlineAttr>()) { |
| 632 | B.addAttribute(llvm::Attribute::NoInline); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 633 | } else if (D->hasAttr<AlwaysInlineAttr>() && |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 634 | !F->getAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex, |
| 635 | llvm::Attribute::NoInline)) { |
| 636 | // (noinline wins over always_inline, and we can't specify both in IR) |
| 637 | B.addAttribute(llvm::Attribute::AlwaysInline); |
Eli Friedman | c55efe4 | 2011-08-22 23:55:33 +0000 | [diff] [blame] | 638 | } |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 639 | |
Diego Novillo | 5f2b1ce | 2013-05-24 20:18:15 +0000 | [diff] [blame] | 640 | if (D->hasAttr<ColdAttr>()) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 641 | B.addAttribute(llvm::Attribute::OptimizeForSize); |
Diego Novillo | 5f2b1ce | 2013-05-24 20:18:15 +0000 | [diff] [blame] | 642 | B.addAttribute(llvm::Attribute::Cold); |
| 643 | } |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 644 | |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 645 | if (D->hasAttr<MinSizeAttr>()) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 646 | B.addAttribute(llvm::Attribute::MinSize); |
Richard Smith | b555a76 | 2012-09-28 22:46:07 +0000 | [diff] [blame] | 647 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 648 | if (D->hasAttr<OptimizeNoneAttr>()) { |
| 649 | // OptimizeNone wins over OptimizeForSize and MinSize. |
| 650 | B.removeAttribute(llvm::Attribute::OptimizeForSize); |
| 651 | B.removeAttribute(llvm::Attribute::MinSize); |
| 652 | } |
| 653 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 654 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 655 | B.addAttribute(llvm::Attribute::StackProtect); |
Josh Magee | e0fc1a8 | 2014-02-11 01:35:14 +0000 | [diff] [blame] | 656 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
| 657 | B.addAttribute(llvm::Attribute::StackProtectStrong); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 658 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 659 | B.addAttribute(llvm::Attribute::StackProtectReq); |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 660 | |
Alexey Samsonov | a1c0a2a | 2013-03-06 10:54:18 +0000 | [diff] [blame] | 661 | // Add sanitizer attributes if function is not blacklisted. |
Alexey Samsonov | d6e043b | 2013-08-12 11:48:05 +0000 | [diff] [blame] | 662 | if (!SanitizerBlacklist->isIn(*F)) { |
Alexey Samsonov | a1c0a2a | 2013-03-06 10:54:18 +0000 | [diff] [blame] | 663 | // When AddressSanitizer is enabled, set SanitizeAddress attribute |
| 664 | // unless __attribute__((no_sanitize_address)) is used. |
| 665 | if (SanOpts.Address && !D->hasAttr<NoSanitizeAddressAttr>()) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 666 | B.addAttribute(llvm::Attribute::SanitizeAddress); |
Alexey Samsonov | a1c0a2a | 2013-03-06 10:54:18 +0000 | [diff] [blame] | 667 | // Same for ThreadSanitizer and __attribute__((no_sanitize_thread)) |
| 668 | if (SanOpts.Thread && !D->hasAttr<NoSanitizeThreadAttr>()) { |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 669 | B.addAttribute(llvm::Attribute::SanitizeThread); |
Alexey Samsonov | a1c0a2a | 2013-03-06 10:54:18 +0000 | [diff] [blame] | 670 | } |
| 671 | // Same for MemorySanitizer and __attribute__((no_sanitize_memory)) |
| 672 | if (SanOpts.Memory && !D->hasAttr<NoSanitizeMemoryAttr>()) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 673 | B.addAttribute(llvm::Attribute::SanitizeMemory); |
Alexey Samsonov | a1c0a2a | 2013-03-06 10:54:18 +0000 | [diff] [blame] | 674 | } |
Alexander Potapenko | ef41fd3 | 2012-02-02 11:49:28 +0000 | [diff] [blame] | 675 | |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 676 | F->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 677 | llvm::AttributeSet::get( |
| 678 | F->getContext(), llvm::AttributeSet::FunctionIndex, B)); |
| 679 | |
| 680 | if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) |
| 681 | F->setUnnamedAddr(true); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 682 | else if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) |
Benjamin Kramer | 2960dbd | 2013-05-11 12:45:37 +0000 | [diff] [blame] | 683 | if (MD->isVirtual()) |
| 684 | F->setUnnamedAddr(true); |
| 685 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 686 | unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); |
| 687 | if (alignment) |
| 688 | F->setAlignment(alignment); |
| 689 | |
Mike Stump | 3472ae5 | 2009-10-05 22:49:20 +0000 | [diff] [blame] | 690 | // C++ ABI requires 2-byte alignment for member functions. |
Mike Stump | 916c006 | 2009-10-05 23:08:21 +0000 | [diff] [blame] | 691 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 692 | F->setAlignment(2); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | void CodeGenModule::SetCommonAttributes(const Decl *D, |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 696 | llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 697 | if (const auto *ND = dyn_cast<NamedDecl>(D)) |
Anders Carlsson | 072ef74 | 2011-01-29 19:41:00 +0000 | [diff] [blame] | 698 | setGlobalVisibility(GV, ND); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 699 | else |
| 700 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 701 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 702 | if (D->hasAttr<UsedAttr>()) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 703 | addUsedGlobal(GV); |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void CodeGenModule::setNonAliasAttributes(const Decl *D, |
Rafael Espindola | ee076a2 | 2014-05-13 18:45:53 +0000 | [diff] [blame] | 707 | llvm::GlobalObject *GO) { |
| 708 | SetCommonAttributes(D, GO); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 709 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 710 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
Rafael Espindola | ee076a2 | 2014-05-13 18:45:53 +0000 | [diff] [blame] | 711 | GO->setSection(SA->getName()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 712 | |
Rafael Espindola | ee076a2 | 2014-05-13 18:45:53 +0000 | [diff] [blame] | 713 | getTargetCodeGenInfo().SetTargetAttributes(D, GO, *this); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 716 | void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, |
| 717 | llvm::Function *F, |
| 718 | const CGFunctionInfo &FI) { |
| 719 | SetLLVMFunctionAttributes(D, FI, F); |
| 720 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 721 | |
| 722 | F->setLinkage(llvm::Function::InternalLinkage); |
| 723 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 724 | setNonAliasAttributes(D, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 725 | } |
| 726 | |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 727 | static void setLinkageAndVisibilityForGV(llvm::GlobalValue *GV, |
| 728 | const NamedDecl *ND) { |
| 729 | // Set linkage and visibility in case we never see a definition. |
| 730 | LinkageInfo LV = ND->getLinkageAndVisibility(); |
| 731 | if (LV.getLinkage() != ExternalLinkage) { |
| 732 | // Don't set internal linkage on declarations. |
| 733 | } else { |
| 734 | if (ND->hasAttr<DLLImportAttr>()) { |
| 735 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 736 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 737 | } else if (ND->hasAttr<DLLExportAttr>()) { |
| 738 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 739 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 740 | } else if (ND->hasAttr<WeakAttr>() || ND->isWeakImported()) { |
| 741 | // "extern_weak" is overloaded in LLVM; we probably should have |
| 742 | // separate linkage types for this. |
| 743 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 744 | } |
| 745 | |
| 746 | // Set visibility on a declaration only if it's explicit. |
| 747 | if (LV.isVisibilityExplicit()) |
| 748 | GV->setVisibility(CodeGenModule::GetLLVMVisibility(LV.getVisibility())); |
| 749 | } |
| 750 | } |
| 751 | |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 752 | void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, |
Eli Friedman | 895771a | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 753 | llvm::Function *F, |
| 754 | bool IsIncompleteFunction) { |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 755 | if (unsigned IID = F->getIntrinsicID()) { |
| 756 | // If this is an intrinsic function, set the function's attributes |
| 757 | // to the intrinsic's attributes. |
Bill Wendling | 311c832 | 2012-10-15 04:47:45 +0000 | [diff] [blame] | 758 | F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), |
| 759 | (llvm::Intrinsic::ID)IID)); |
Peter Collingbourne | eafa4e4 | 2011-04-06 12:29:04 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 763 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 764 | |
Eli Friedman | 895771a | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 765 | if (!IsIncompleteFunction) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 766 | SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Bob Wilson | fc6297f | 2014-04-01 01:38:16 +0000 | [diff] [blame] | 768 | // Add the Returned attribute for "this", except for iOS 5 and earlier |
| 769 | // where substantial code, including the libstdc++ dylib, was compiled with |
| 770 | // GCC and does not actually return "this". |
| 771 | if (getCXXABI().HasThisReturn(GD) && |
| 772 | !(getTarget().getTriple().isiOS() && |
| 773 | getTarget().getTriple().isOSVersionLT(6))) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 774 | assert(!F->arg_empty() && |
| 775 | F->arg_begin()->getType() |
| 776 | ->canLosslesslyBitCastTo(F->getReturnType()) && |
| 777 | "unexpected this return"); |
| 778 | F->addAttribute(1, llvm::Attribute::Returned); |
| 779 | } |
| 780 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 781 | // Only a few attributes are set on declarations; these may later be |
| 782 | // overridden by a definition. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 784 | setLinkageAndVisibilityForGV(F, FD); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 785 | |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame^] | 786 | if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(FD)) { |
| 787 | if (getCXXABI().useThunkForDtorVariant(Dtor, GD.getDtorType())) { |
| 788 | // Don't dllexport/import destructor thunks. |
| 789 | F->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 790 | } |
| 791 | } |
| 792 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 793 | if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 794 | F->setSection(SA->getName()); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 795 | |
| 796 | // A replaceable global allocation function does not act like a builtin by |
| 797 | // default, only if it is invoked by a new-expression or delete-expression. |
| 798 | if (FD->isReplaceableGlobalAllocationFunction()) |
| 799 | F->addAttribute(llvm::AttributeSet::FunctionIndex, |
| 800 | llvm::Attribute::NoBuiltin); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 803 | void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 805 | "Only globals with definition can force usage."); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 806 | LLVMUsed.push_back(GV); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 809 | void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { |
| 810 | assert(!GV->isDeclaration() && |
| 811 | "Only globals with definition can force usage."); |
| 812 | LLVMCompilerUsed.push_back(GV); |
| 813 | } |
| 814 | |
| 815 | static void emitUsed(CodeGenModule &CGM, StringRef Name, |
| 816 | std::vector<llvm::WeakVH> &List) { |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 817 | // Don't create llvm.used if there is no need. |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 818 | if (List.empty()) |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 819 | return; |
| 820 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 821 | // Convert List to what ConstantArray needs. |
Chris Lattner | 3def9ae | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 822 | SmallVector<llvm::Constant*, 8> UsedArray; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 823 | UsedArray.resize(List.size()); |
| 824 | for (unsigned i = 0, e = List.size(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | UsedArray[i] = |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 826 | llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*List[i]), |
| 827 | CGM.Int8PtrTy); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 828 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Fariborz Jahanian | 248c719 | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 830 | if (UsedArray.empty()) |
| 831 | return; |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 832 | llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 834 | auto *GV = new llvm::GlobalVariable( |
| 835 | CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, |
| 836 | llvm::ConstantArray::get(ATy, UsedArray), Name); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 837 | |
| 838 | GV->setSection("llvm.metadata"); |
| 839 | } |
| 840 | |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 841 | void CodeGenModule::emitLLVMUsed() { |
| 842 | emitUsed(*this, "llvm.used", LLVMUsed); |
| 843 | emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); |
| 844 | } |
| 845 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 846 | void CodeGenModule::AppendLinkerOptions(StringRef Opts) { |
| 847 | llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); |
| 848 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 849 | } |
| 850 | |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 851 | void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { |
| 852 | llvm::SmallString<32> Opt; |
| 853 | getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt); |
| 854 | llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
| 855 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 856 | } |
| 857 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 858 | void CodeGenModule::AddDependentLib(StringRef Lib) { |
| 859 | llvm::SmallString<24> Opt; |
| 860 | getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); |
| 861 | llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); |
| 862 | LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); |
| 863 | } |
| 864 | |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 865 | /// \brief Add link options implied by the given module, including modules |
| 866 | /// it depends on, using a postorder walk. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 867 | static void addLinkOptionsPostorder(CodeGenModule &CGM, |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 868 | Module *Mod, |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 869 | SmallVectorImpl<llvm::Value *> &Metadata, |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 870 | llvm::SmallPtrSet<Module *, 16> &Visited) { |
| 871 | // Import this module's parent. |
| 872 | if (Mod->Parent && Visited.insert(Mod->Parent)) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 873 | addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | // Import this module's dependencies. |
| 877 | for (unsigned I = Mod->Imports.size(); I > 0; --I) { |
| 878 | if (Visited.insert(Mod->Imports[I-1])) |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 879 | addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | // Add linker options to link against the libraries/frameworks |
| 883 | // described by this module. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 884 | llvm::LLVMContext &Context = CGM.getLLVMContext(); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 885 | for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 886 | // Link against a framework. Frameworks are currently Darwin only, so we |
| 887 | // don't to ask TargetCodeGenInfo for the spelling of the linker option. |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 888 | if (Mod->LinkLibraries[I-1].IsFramework) { |
| 889 | llvm::Value *Args[2] = { |
| 890 | llvm::MDString::get(Context, "-framework"), |
| 891 | llvm::MDString::get(Context, Mod->LinkLibraries[I-1].Library) |
| 892 | }; |
| 893 | |
| 894 | Metadata.push_back(llvm::MDNode::get(Context, Args)); |
| 895 | continue; |
| 896 | } |
| 897 | |
| 898 | // Link against a library. |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 899 | llvm::SmallString<24> Opt; |
| 900 | CGM.getTargetCodeGenInfo().getDependentLibraryOption( |
| 901 | Mod->LinkLibraries[I-1].Library, Opt); |
| 902 | llvm::Value *OptString = llvm::MDString::get(Context, Opt); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 903 | Metadata.push_back(llvm::MDNode::get(Context, OptString)); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | void CodeGenModule::EmitModuleLinkOptions() { |
| 908 | // Collect the set of all of the modules we want to visit to emit link |
| 909 | // options, which is essentially the imported modules and all of their |
| 910 | // non-explicit child modules. |
| 911 | llvm::SetVector<clang::Module *> LinkModules; |
| 912 | llvm::SmallPtrSet<clang::Module *, 16> Visited; |
| 913 | SmallVector<clang::Module *, 16> Stack; |
| 914 | |
| 915 | // Seed the stack with imported modules. |
| 916 | for (llvm::SetVector<clang::Module *>::iterator M = ImportedModules.begin(), |
| 917 | MEnd = ImportedModules.end(); |
| 918 | M != MEnd; ++M) { |
| 919 | if (Visited.insert(*M)) |
| 920 | Stack.push_back(*M); |
| 921 | } |
| 922 | |
| 923 | // Find all of the modules to import, making a little effort to prune |
| 924 | // non-leaf modules. |
| 925 | while (!Stack.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 926 | clang::Module *Mod = Stack.pop_back_val(); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 927 | |
| 928 | bool AnyChildren = false; |
| 929 | |
| 930 | // Visit the submodules of this module. |
| 931 | for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), |
| 932 | SubEnd = Mod->submodule_end(); |
| 933 | Sub != SubEnd; ++Sub) { |
| 934 | // Skip explicit children; they need to be explicitly imported to be |
| 935 | // linked against. |
| 936 | if ((*Sub)->IsExplicit) |
| 937 | continue; |
| 938 | |
| 939 | if (Visited.insert(*Sub)) { |
| 940 | Stack.push_back(*Sub); |
| 941 | AnyChildren = true; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | // We didn't find any children, so add this module to the list of |
| 946 | // modules to link against. |
| 947 | if (!AnyChildren) { |
| 948 | LinkModules.insert(Mod); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | // Add link options for all of the imported modules in reverse topological |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 953 | // order. We don't do anything to try to order import link flags with respect |
| 954 | // to linker options inserted by things like #pragma comment(). |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 955 | SmallVector<llvm::Value *, 16> MetadataArgs; |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 956 | Visited.clear(); |
| 957 | for (llvm::SetVector<clang::Module *>::iterator M = LinkModules.begin(), |
| 958 | MEnd = LinkModules.end(); |
| 959 | M != MEnd; ++M) { |
| 960 | if (Visited.insert(*M)) |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 961 | addLinkOptionsPostorder(*this, *M, MetadataArgs, Visited); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 962 | } |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 963 | std::reverse(MetadataArgs.begin(), MetadataArgs.end()); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 964 | LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end()); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 965 | |
Daniel Dunbar | 69e4746 | 2013-01-17 01:35:06 +0000 | [diff] [blame] | 966 | // Add the linker options metadata flag. |
| 967 | getModule().addModuleFlag(llvm::Module::AppendUnique, "Linker Options", |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 968 | llvm::MDNode::get(getLLVMContext(), |
| 969 | LinkerOptionsMetadata)); |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 972 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 973 | // Emit code for any potentially referenced deferred decls. Since a |
| 974 | // previously unused static decl may become used during the generation of code |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 975 | // for a static function, iterate until no changes are made. |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 976 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 977 | while (true) { |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 978 | if (!DeferredVTables.empty()) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 979 | EmitDeferredVTables(); |
| 980 | |
| 981 | // Emitting a v-table doesn't directly cause more v-tables to |
| 982 | // become deferred, although it can cause functions to be |
| 983 | // emitted that then need those v-tables. |
| 984 | assert(DeferredVTables.empty()); |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 985 | } |
| 986 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 987 | // Stop if we're out of both deferred v-tables and deferred declarations. |
| 988 | if (DeferredDeclsToEmit.empty()) break; |
| 989 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 990 | DeferredGlobal &G = DeferredDeclsToEmit.back(); |
| 991 | GlobalDecl D = G.GD; |
| 992 | llvm::GlobalValue *GV = G.GV; |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 993 | DeferredDeclsToEmit.pop_back(); |
| 994 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 995 | assert(GV == GetGlobalValue(getMangledName(D))); |
John McCall | c2af939 | 2010-05-27 01:45:30 +0000 | [diff] [blame] | 996 | // Check to see if we've already emitted this. This is necessary |
| 997 | // for a couple of reasons: first, decls can end up in the |
| 998 | // deferred-decls queue multiple times, and second, decls can end |
| 999 | // up with definitions in unusual ways (e.g. by an extern inline |
| 1000 | // function acquiring a strong function redefinition). Just |
| 1001 | // ignore these cases. |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1002 | if(!GV->isDeclaration()) |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1003 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1005 | // Otherwise, emit the definition and move on to the next one. |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1006 | EmitGlobalDefinition(D, GV); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1007 | } |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1008 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1009 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1010 | void CodeGenModule::EmitGlobalAnnotations() { |
| 1011 | if (Annotations.empty()) |
| 1012 | return; |
| 1013 | |
| 1014 | // Create a new global variable for the ConstantStruct in the Module. |
| 1015 | llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( |
| 1016 | Annotations[0]->getType(), Annotations.size()), Annotations); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1017 | auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false, |
| 1018 | llvm::GlobalValue::AppendingLinkage, |
| 1019 | Array, "llvm.global.annotations"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1020 | gv->setSection(AnnotationSection); |
| 1021 | } |
| 1022 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1023 | llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) { |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 1024 | llvm::Constant *&AStr = AnnotationStrings[Str]; |
| 1025 | if (AStr) |
| 1026 | return AStr; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1027 | |
| 1028 | // Not found yet, create a new global. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 1029 | llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1030 | auto *gv = |
| 1031 | new llvm::GlobalVariable(getModule(), s->getType(), true, |
| 1032 | llvm::GlobalValue::PrivateLinkage, s, ".str"); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1033 | gv->setSection(AnnotationSection); |
| 1034 | gv->setUnnamedAddr(true); |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 1035 | AStr = gv; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1036 | return gv; |
| 1037 | } |
| 1038 | |
| 1039 | llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { |
| 1040 | SourceManager &SM = getContext().getSourceManager(); |
| 1041 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 1042 | if (PLoc.isValid()) |
| 1043 | return EmitAnnotationString(PLoc.getFilename()); |
| 1044 | return EmitAnnotationString(SM.getBufferName(Loc)); |
| 1045 | } |
| 1046 | |
| 1047 | llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { |
| 1048 | SourceManager &SM = getContext().getSourceManager(); |
| 1049 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 1050 | unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : |
| 1051 | SM.getExpansionLineNumber(L); |
| 1052 | return llvm::ConstantInt::get(Int32Ty, LineNo); |
| 1053 | } |
| 1054 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1056 | const AnnotateAttr *AA, |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1057 | SourceLocation L) { |
| 1058 | // Get the globals for file name, annotation, and the line number. |
| 1059 | llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), |
| 1060 | *UnitGV = EmitAnnotationUnit(L), |
| 1061 | *LineNoCst = EmitAnnotationLineNo(L); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1062 | |
Daniel Dunbar | 346892a | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 1063 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1064 | llvm::Constant *Fields[4] = { |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1065 | llvm::ConstantExpr::getBitCast(GV, Int8PtrTy), |
| 1066 | llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), |
| 1067 | llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), |
| 1068 | LineNoCst |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1069 | }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1070 | return llvm::ConstantStruct::getAnon(Fields); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1073 | void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, |
| 1074 | llvm::GlobalValue *GV) { |
| 1075 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 1076 | // Get the struct elements for these annotations. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1077 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) |
| 1078 | Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Argyrios Kyrtzidis | c0279a9 | 2010-07-27 22:37:14 +0000 | [diff] [blame] | 1081 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 1082 | // Never defer when EmitAllDecls is specified. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1083 | if (LangOpts.EmitAllDecls) |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1084 | return false; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1085 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 1086 | return !getContext().DeclMustBeEmitted(Global); |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1089 | llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor( |
| 1090 | const CXXUuidofExpr* E) { |
| 1091 | // Sema has verified that IIDSource has a __declspec(uuid()), and that its |
| 1092 | // well-formed. |
David Majnemer | 8eaab6f | 2013-08-13 06:32:20 +0000 | [diff] [blame] | 1093 | StringRef Uuid = E->getUuidAsStringRef(Context); |
David Majnemer | cf963ce | 2013-08-09 08:35:59 +0000 | [diff] [blame] | 1094 | std::string Name = "_GUID_" + Uuid.lower(); |
| 1095 | std::replace(Name.begin(), Name.end(), '-', '_'); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Look for an existing global. |
| 1098 | if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) |
| 1099 | return GV; |
| 1100 | |
| 1101 | llvm::Constant *Init = EmitUuidofInitializer(Uuid, E->getType()); |
| 1102 | assert(Init && "failed to initialize as constant"); |
| 1103 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1104 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 1105 | getModule(), Init->getType(), |
Reid Kleckner | b9921df | 2013-09-03 21:49:32 +0000 | [diff] [blame] | 1106 | /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 1107 | return GV; |
| 1108 | } |
| 1109 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1110 | llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { |
| 1111 | const AliasAttr *AA = VD->getAttr<AliasAttr>(); |
| 1112 | assert(AA && "No alias?"); |
| 1113 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1114 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1115 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1116 | // 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] | 1117 | llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1118 | if (Entry) { |
| 1119 | unsigned AS = getContext().getTargetAddressSpace(VD->getType()); |
| 1120 | return llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS)); |
| 1121 | } |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1122 | |
| 1123 | llvm::Constant *Aliasee; |
| 1124 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 1125 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, |
| 1126 | GlobalDecl(cast<FunctionDecl>(VD)), |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 1127 | /*ForVTable=*/false); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1128 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1129 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1130 | llvm::PointerType::getUnqual(DeclTy), |
| 1131 | nullptr); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1132 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1133 | auto *F = cast<llvm::GlobalValue>(Aliasee); |
Joerg Sonnenberger | de78bba | 2012-10-16 17:45:27 +0000 | [diff] [blame] | 1134 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 1135 | WeakRefReferences.insert(F); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1136 | |
| 1137 | return Aliasee; |
| 1138 | } |
| 1139 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1140 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1141 | const auto *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1143 | // Weak references don't produce any output by themselves. |
| 1144 | if (Global->hasAttr<WeakRefAttr>()) |
| 1145 | return; |
| 1146 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1147 | // If this is an alias definition (which otherwise looks like a declaration) |
| 1148 | // emit it now. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1149 | if (Global->hasAttr<AliasAttr>()) |
Rafael Espindola | 6956d58 | 2013-10-22 14:23:09 +0000 | [diff] [blame] | 1150 | return EmitAliasDefinition(GD); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1151 | |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1152 | // If this is CUDA, be selective about which declarations we emit. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1153 | if (LangOpts.CUDA) { |
Peter Collingbourne | a9455ec | 2011-10-06 18:29:46 +0000 | [diff] [blame] | 1154 | if (CodeGenOpts.CUDAIsDevice) { |
| 1155 | if (!Global->hasAttr<CUDADeviceAttr>() && |
| 1156 | !Global->hasAttr<CUDAGlobalAttr>() && |
| 1157 | !Global->hasAttr<CUDAConstantAttr>() && |
| 1158 | !Global->hasAttr<CUDASharedAttr>()) |
| 1159 | return; |
| 1160 | } else { |
| 1161 | if (!Global->hasAttr<CUDAHostAttr>() && ( |
| 1162 | Global->hasAttr<CUDADeviceAttr>() || |
| 1163 | Global->hasAttr<CUDAConstantAttr>() || |
| 1164 | Global->hasAttr<CUDASharedAttr>())) |
| 1165 | return; |
| 1166 | } |
| 1167 | } |
| 1168 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1169 | // Ignore declarations, they will be emitted on their first use. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1170 | if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1171 | // Forward declarations are emitted lazily on first use. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1172 | if (!FD->doesThisDeclarationHaveABody()) { |
| 1173 | if (!FD->doesDeclarationForceExternallyVisibleDefinition()) |
| 1174 | return; |
| 1175 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1176 | StringRef MangledName = getMangledName(GD); |
David Majnemer | af36980 | 2014-03-29 14:19:55 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Compute the function info and LLVM type. |
| 1179 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 1180 | llvm::Type *Ty = getTypes().GetFunctionType(FI); |
| 1181 | |
| 1182 | GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false, |
| 1183 | /*DontDefer=*/false); |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 1184 | return; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1185 | } |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 1186 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1187 | const auto *VD = cast<VarDecl>(Global); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1188 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 1189 | |
Douglas Gregor | 61f6db5 | 2010-02-06 05:15:45 +0000 | [diff] [blame] | 1190 | if (VD->isThisDeclarationADefinition() != VarDecl::Definition) |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 1191 | return; |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1194 | // Defer code generation when possible if this is a static definition, inline |
| 1195 | // function etc. These we only want to emit if they are used. |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 1196 | if (!MayDeferGeneration(Global)) { |
| 1197 | // Emit the definition if it can't be deferred. |
| 1198 | EmitGlobalDefinition(GD); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1199 | return; |
| 1200 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1201 | |
| 1202 | // If we're deferring emission of a C++ variable with an |
| 1203 | // initializer, remember the order in which it appeared in the file. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1204 | if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) && |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1205 | cast<VarDecl>(Global)->hasInit()) { |
| 1206 | DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1207 | CXXGlobalInits.push_back(nullptr); |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1208 | } |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 1209 | |
| 1210 | // If the value has already been used, add it directly to the |
| 1211 | // DeferredDeclsToEmit list. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1212 | StringRef MangledName = getMangledName(GD); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1213 | if (llvm::GlobalValue *GV = GetGlobalValue(MangledName)) |
| 1214 | addDeferredDeclToEmit(GV, GD); |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 1215 | else { |
| 1216 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 1217 | // first use of the mangled name will cause it to move into |
| 1218 | // DeferredDeclsToEmit. |
| 1219 | DeferredDecls[MangledName] = GD; |
| 1220 | } |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1223 | namespace { |
| 1224 | struct FunctionIsDirectlyRecursive : |
| 1225 | public RecursiveASTVisitor<FunctionIsDirectlyRecursive> { |
| 1226 | const StringRef Name; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1227 | const Builtin::Context &BI; |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1228 | bool Result; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1229 | FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) : |
| 1230 | Name(N), BI(C), Result(false) { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1231 | } |
| 1232 | typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base; |
| 1233 | |
| 1234 | bool TraverseCallExpr(CallExpr *E) { |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1235 | const FunctionDecl *FD = E->getDirectCallee(); |
| 1236 | if (!FD) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1237 | return true; |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1238 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 1239 | if (Attr && Name == Attr->getLabel()) { |
| 1240 | Result = true; |
| 1241 | return false; |
| 1242 | } |
| 1243 | unsigned BuiltinID = FD->getBuiltinID(); |
| 1244 | if (!BuiltinID) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1245 | return true; |
Nick Lewycky | 9ed5b15 | 2012-01-18 03:41:19 +0000 | [diff] [blame] | 1246 | StringRef BuiltinName = BI.GetName(BuiltinID); |
| 1247 | if (BuiltinName.startswith("__builtin_") && |
| 1248 | Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) { |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1249 | Result = true; |
| 1250 | return false; |
| 1251 | } |
| 1252 | return true; |
| 1253 | } |
| 1254 | }; |
| 1255 | } |
| 1256 | |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1257 | // isTriviallyRecursive - Check if this function calls another |
| 1258 | // decl that, because of the asm attribute or the other decl being a builtin, |
| 1259 | // ends up pointing to itself. |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1260 | bool |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1261 | CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) { |
| 1262 | StringRef Name; |
| 1263 | if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) { |
Nick Lewycky | 72cd229 | 2012-01-18 01:50:13 +0000 | [diff] [blame] | 1264 | // asm labels are a special kind of mangling we have to support. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1265 | AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); |
| 1266 | if (!Attr) |
| 1267 | return false; |
| 1268 | Name = Attr->getLabel(); |
| 1269 | } else { |
| 1270 | Name = FD->getName(); |
| 1271 | } |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1272 | |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1273 | FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo); |
| 1274 | Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD)); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1275 | return Walker.Result; |
| 1276 | } |
| 1277 | |
| 1278 | bool |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1279 | CodeGenModule::shouldEmitFunction(GlobalDecl GD) { |
| 1280 | if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1281 | return true; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1282 | const auto *F = cast<FunctionDecl>(GD.getDecl()); |
David Majnemer | 67e541e1c | 2014-02-25 09:53:29 +0000 | [diff] [blame] | 1283 | if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>()) |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1284 | return false; |
| 1285 | // PR9614. Avoid cases where the source code is lying to us. An available |
| 1286 | // externally function should have an equivalent function somewhere else, |
| 1287 | // but a function that calls itself is clearly not equivalent to the real |
| 1288 | // implementation. |
| 1289 | // This happens in glibc's btowc and in some configure checks. |
Rafael Espindola | 4fdc175 | 2011-12-19 14:41:01 +0000 | [diff] [blame] | 1290 | return !isTriviallyRecursive(F); |
Rafael Espindola | c594135 | 2011-10-26 20:41:06 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Adrian Prantl | ffcf4ba | 2013-05-09 23:16:27 +0000 | [diff] [blame] | 1293 | /// If the type for the method's class was generated by |
| 1294 | /// CGDebugInfo::createContextChain(), the cache contains only a |
| 1295 | /// limited DIType without any declarations. Since EmitFunctionStart() |
| 1296 | /// needs to find the canonical declaration for each method, we need |
| 1297 | /// to construct the complete type prior to emitting the method. |
| 1298 | void CodeGenModule::CompleteDIClassType(const CXXMethodDecl* D) { |
| 1299 | if (!D->isInstance()) |
| 1300 | return; |
| 1301 | |
| 1302 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 1303 | if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1304 | const auto *ThisPtr = cast<PointerType>(D->getThisType(getContext())); |
Adrian Prantl | ffcf4ba | 2013-05-09 23:16:27 +0000 | [diff] [blame] | 1305 | DI->getOrCreateRecordType(ThisPtr->getPointeeType(), D->getLocation()); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1309 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1310 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1312 | PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), |
Anders Carlsson | 29295bf | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 1313 | Context.getSourceManager(), |
| 1314 | "Generating code for declaration"); |
| 1315 | |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1316 | if (isa<FunctionDecl>(D)) { |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1317 | // At -O0, don't generate IR for functions with available_externally |
| 1318 | // linkage. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 1319 | if (!shouldEmitFunction(GD)) |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1320 | return; |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 1321 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1322 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
Adrian Prantl | ffcf4ba | 2013-05-09 23:16:27 +0000 | [diff] [blame] | 1323 | CompleteDIClassType(Method); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1324 | // Make sure to emit the definition(s) before we emit the thunks. |
| 1325 | // This is necessary for the generation of certain thunks. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1326 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(Method)) |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1327 | EmitCXXConstructor(CD, GD.getCtorType()); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1328 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(Method)) |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1329 | EmitCXXDestructor(DD, GD.getDtorType()); |
| 1330 | else |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1331 | EmitGlobalFunctionDefinition(GD, GV); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1332 | |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1333 | if (Method->isVirtual()) |
| 1334 | getVTables().EmitThunks(GD); |
| 1335 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 1336 | return; |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1337 | } |
Chris Lattner | d8d760c | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 1338 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 1339 | return EmitGlobalFunctionDefinition(GD, GV); |
Douglas Gregor | a700f68 | 2010-07-13 06:02:28 +0000 | [diff] [blame] | 1340 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
Chris Lattner | d8d760c | 2010-04-13 17:57:11 +0000 | [diff] [blame] | 1343 | return EmitGlobalVarDefinition(VD); |
Chris Lattner | 7a4a29f | 2010-04-13 17:39:09 +0000 | [diff] [blame] | 1344 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1345 | llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1348 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 1349 | /// module, create and return an llvm Function with the specified type. If there |
| 1350 | /// is something in the module with the specified name, return it potentially |
| 1351 | /// bitcasted to the right type. |
| 1352 | /// |
| 1353 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 1354 | /// to set the attributes on the function when it is first created. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1355 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1356 | CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1357 | llvm::Type *Ty, |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 1358 | GlobalDecl GD, bool ForVTable, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1359 | bool DontDefer, |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1360 | llvm::AttributeSet ExtraAttrs) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 1361 | const Decl *D = GD.getDecl(); |
| 1362 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1363 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1364 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1365 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 1366 | if (WeakRefReferences.erase(Entry)) { |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 1367 | const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1368 | if (FD && !FD->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 1369 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1372 | if (Entry->getType()->getElementType() == Ty) |
| 1373 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1375 | // Make sure the result is of the correct type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1376 | return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1377 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 1379 | // This function doesn't have a complete type (for example, the return |
| 1380 | // type is an incomplete struct). Use a fake type instead, and make |
| 1381 | // sure not to try to set attributes. |
| 1382 | bool IsIncompleteFunction = false; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 1383 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1384 | llvm::FunctionType *FTy; |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 1385 | if (isa<llvm::FunctionType>(Ty)) { |
| 1386 | FTy = cast<llvm::FunctionType>(Ty); |
| 1387 | } else { |
John McCall | 9dc0db2 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 1388 | FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 1389 | IsIncompleteFunction = true; |
| 1390 | } |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1391 | |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 1392 | llvm::Function *F = llvm::Function::Create(FTy, |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 1393 | llvm::Function::ExternalLinkage, |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1394 | MangledName, &getModule()); |
| 1395 | assert(F->getName() == MangledName && "name was uniqued!"); |
Reid Kleckner | f6ce2b5 | 2013-07-22 13:07:10 +0000 | [diff] [blame] | 1396 | if (D) |
| 1397 | SetFunctionAttributes(GD, F, IsIncompleteFunction); |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1398 | if (ExtraAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex)) { |
| 1399 | llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeSet::FunctionIndex); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1400 | F->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1401 | llvm::AttributeSet::get(VMContext, |
| 1402 | llvm::AttributeSet::FunctionIndex, |
| 1403 | B)); |
| 1404 | } |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 1405 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1406 | if (!DontDefer) { |
| 1407 | // All MSVC dtors other than the base dtor are linkonce_odr and delegate to |
| 1408 | // each other bottoming out with the base dtor. Therefore we emit non-base |
| 1409 | // dtors on usage, even if there is no dtor definition in the TU. |
| 1410 | if (D && isa<CXXDestructorDecl>(D) && |
| 1411 | getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), |
| 1412 | GD.getDtorType())) |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1413 | addDeferredDeclToEmit(F, GD); |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 1414 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1415 | // This is the first use or definition of a mangled name. If there is a |
| 1416 | // deferred decl with this name, remember that we need to emit it at the end |
| 1417 | // of the file. |
| 1418 | llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); |
| 1419 | if (DDI != DeferredDecls.end()) { |
| 1420 | // Move the potentially referenced deferred decl to the |
| 1421 | // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we |
| 1422 | // don't need it anymore). |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1423 | addDeferredDeclToEmit(F, DDI->second); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1424 | DeferredDecls.erase(DDI); |
Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 1425 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1426 | // Otherwise, if this is a sized deallocation function, emit a weak |
| 1427 | // definition |
| 1428 | // for it at the end of the translation unit. |
| 1429 | } else if (D && cast<FunctionDecl>(D) |
| 1430 | ->getCorrespondingUnsizedGlobalDeallocationFunction()) { |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1431 | addDeferredDeclToEmit(F, GD); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Otherwise, there are cases we have to worry about where we're |
| 1434 | // using a declaration for which we must emit a definition but where |
| 1435 | // we might not find a top-level definition: |
| 1436 | // - member functions defined inline in their classes |
| 1437 | // - friend functions defined inline in some class |
| 1438 | // - special member functions with implicit definitions |
| 1439 | // If we ever change our AST traversal to walk into class methods, |
| 1440 | // this will be unnecessary. |
| 1441 | // |
| 1442 | // We also don't emit a definition for a function if it's going to be an |
| 1443 | // entry |
| 1444 | // in a vtable, unless it's already marked as used. |
| 1445 | } else if (getLangOpts().CPlusPlus && D) { |
| 1446 | // Look for a declaration that's lexically in a record. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1447 | const auto *FD = cast<FunctionDecl>(D); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1448 | FD = FD->getMostRecentDecl(); |
| 1449 | do { |
| 1450 | if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { |
| 1451 | if (FD->isImplicit() && !ForVTable) { |
| 1452 | assert(FD->isUsed() && |
| 1453 | "Sema didn't mark implicit function as used!"); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1454 | addDeferredDeclToEmit(F, GD.getWithDecl(FD)); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | } else if (FD->doesThisDeclarationHaveABody()) { |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1457 | addDeferredDeclToEmit(F, GD.getWithDecl(FD)); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | } |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 1460 | } |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1461 | FD = FD->getPreviousDecl(); |
| 1462 | } while (FD); |
| 1463 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1464 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 1466 | getTargetCodeGenInfo().emitTargetMD(D, F, *this); |
| 1467 | |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 1468 | // Make sure the result is of the requested type. |
| 1469 | if (!IsIncompleteFunction) { |
| 1470 | assert(F->getType()->getElementType() == Ty); |
| 1471 | return F; |
| 1472 | } |
| 1473 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1474 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
John McCall | d06fb86 | 2010-04-28 00:00:30 +0000 | [diff] [blame] | 1475 | return llvm::ConstantExpr::getBitCast(F, PTy); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1478 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 1479 | /// non-null, then this function will use the specified type if it has to |
| 1480 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1481 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1482 | llvm::Type *Ty, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1483 | bool ForVTable, |
| 1484 | bool DontDefer) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1485 | // If there was no specific requested type, just convert it now. |
| 1486 | if (!Ty) |
Anders Carlsson | 38988d7 | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 1487 | Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1489 | StringRef MangledName = getMangledName(GD); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1490 | return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1491 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1493 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 1494 | /// type and name. |
| 1495 | llvm::Constant * |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1496 | CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1497 | StringRef Name, |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1498 | llvm::AttributeSet ExtraAttrs) { |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 1499 | llvm::Constant *C = |
| 1500 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, |
| 1501 | /*DontDefer=*/false, ExtraAttrs); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1502 | if (auto *F = dyn_cast<llvm::Function>(C)) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1503 | if (F->empty()) |
| 1504 | F->setCallingConv(getRuntimeCC()); |
| 1505 | return C; |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1506 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1507 | |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1508 | /// isTypeConstant - Determine whether an object of this type can be emitted |
| 1509 | /// as a constant. |
| 1510 | /// |
| 1511 | /// If ExcludeCtor is true, the duration when the object's constructor runs |
| 1512 | /// will not be considered. The caller will need to verify that the object is |
| 1513 | /// not written to during its construction. |
| 1514 | bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) { |
| 1515 | if (!Ty.isConstant(Context) && !Ty->isReferenceType()) |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 1516 | return false; |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1517 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1518 | if (Context.getLangOpts().CPlusPlus) { |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1519 | if (const CXXRecordDecl *Record |
| 1520 | = Context.getBaseElementType(Ty)->getAsCXXRecordDecl()) |
| 1521 | return ExcludeCtor && !Record->hasMutableFields() && |
| 1522 | Record->hasTrivialDestructor(); |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 1523 | } |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1524 | |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 1525 | return true; |
| 1526 | } |
| 1527 | |
David Majnemer | 01bbd1b | 2014-04-25 17:08:41 +0000 | [diff] [blame] | 1528 | static bool isVarDeclInlineInitializedStaticDataMember(const VarDecl *VD) { |
| 1529 | if (!VD->isStaticDataMember()) |
| 1530 | return false; |
| 1531 | const VarDecl *InitDecl; |
| 1532 | const Expr *InitExpr = VD->getAnyInitializer(InitDecl); |
| 1533 | if (!InitExpr) |
| 1534 | return false; |
| 1535 | if (InitDecl->isThisDeclarationADefinition()) |
| 1536 | return false; |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1540 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 1541 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 1542 | /// is something in the module with the specified name, return it potentially |
| 1543 | /// bitcasted to the right type. |
| 1544 | /// |
| 1545 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 1546 | /// to set the attributes on the global when it is first created. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1547 | llvm::Constant * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1548 | CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1549 | llvm::PointerType *Ty, |
Rafael Espindola | b73c973 | 2014-05-22 23:33:27 +0000 | [diff] [blame] | 1550 | const VarDecl *D) { |
Daniel Dunbar | 829e988 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 1551 | // Lookup the entry, lazily creating it if necessary. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1552 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1553 | if (Entry) { |
Benjamin Kramer | fc6eb7d | 2012-08-22 15:37:55 +0000 | [diff] [blame] | 1554 | if (WeakRefReferences.erase(Entry)) { |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1555 | if (D && !D->hasAttr<WeakAttr>()) |
Anders Carlsson | af82f63 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 1556 | Entry->setLinkage(llvm::Function::ExternalLinkage); |
Rafael Espindola | 2e42fec | 2010-03-04 18:17:24 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1559 | if (Entry->getType() == Ty) |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1560 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1562 | // Make sure the result is of the correct type. |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 1563 | if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace()) |
| 1564 | return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty); |
| 1565 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1566 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 1567 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1569 | unsigned AddrSpace = GetGlobalVarAddressSpace(D, Ty->getAddressSpace()); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1570 | auto *GV = new llvm::GlobalVariable( |
| 1571 | getModule(), Ty->getElementType(), false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1572 | llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr, |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1573 | llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1575 | // This is the first use or definition of a mangled name. If there is a |
| 1576 | // deferred decl with this name, remember that we need to emit it at the end |
| 1577 | // of the file. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1578 | llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1579 | if (DDI != DeferredDecls.end()) { |
| 1580 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 1581 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
Rafael Espindola | c0ff744 | 2013-12-09 14:59:08 +0000 | [diff] [blame] | 1582 | addDeferredDeclToEmit(GV, DDI->second); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 1583 | DeferredDecls.erase(DDI); |
| 1584 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1586 | // Handle things which are present even on external declarations. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1587 | if (D) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1588 | // FIXME: This code is overly simple and should be merged with other global |
| 1589 | // handling. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1590 | GV->setConstant(isTypeConstant(D->getType(), false)); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 1591 | |
David Majnemer | f6acb76 | 2014-04-25 17:07:16 +0000 | [diff] [blame] | 1592 | setLinkageAndVisibilityForGV(GV, D); |
Eli Friedman | 4f85674 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 1593 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1594 | if (D->getTLSKind()) { |
Richard Smith | 1847baa | 2013-04-22 08:06:17 +0000 | [diff] [blame] | 1595 | if (D->getTLSKind() == VarDecl::TLS_Dynamic) |
| 1596 | CXXThreadLocals.push_back(std::make_pair(D, GV)); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 1597 | setTLSMode(GV, *D); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1598 | } |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 1599 | |
| 1600 | // If required by the ABI, treat declarations of static data members with |
| 1601 | // inline initializers as definitions. |
| 1602 | if (getCXXABI().isInlineInitializedStaticDataMemberLinkOnce() && |
David Majnemer | 01bbd1b | 2014-04-25 17:08:41 +0000 | [diff] [blame] | 1603 | isVarDeclInlineInitializedStaticDataMember(D)) |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 1604 | EmitGlobalVarDefinition(D); |
Robert Lytton | f80d688 | 2014-05-02 09:33:30 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Handle XCore specific ABI requirements. |
| 1607 | if (getTarget().getTriple().getArch() == llvm::Triple::xcore && |
| 1608 | D->getLanguageLinkage() == CLanguageLinkage && |
| 1609 | D->getType().isConstant(Context) && |
| 1610 | isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) |
| 1611 | GV->setSection(".cp.rodata"); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1612 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 1614 | if (AddrSpace != Ty->getAddressSpace()) |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 1615 | return llvm::ConstantExpr::getAddrSpaceCast(GV, Ty); |
| 1616 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 1617 | getTargetCodeGenInfo().emitTargetMD(D, GV, *this); |
| 1618 | |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 1619 | return GV; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1622 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1623 | llvm::GlobalVariable * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1624 | CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1625 | llvm::Type *Ty, |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1626 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 1627 | llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1628 | llvm::GlobalVariable *OldGV = nullptr; |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1629 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1630 | if (GV) { |
| 1631 | // Check if the variable has the right type. |
| 1632 | if (GV->getType()->getElementType() == Ty) |
| 1633 | return GV; |
| 1634 | |
| 1635 | // Because C++ name mangling, the only way we can end up with an already |
| 1636 | // 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] | 1637 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1638 | OldGV = GV; |
| 1639 | } |
| 1640 | |
| 1641 | // Create a new variable. |
| 1642 | GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1643 | Linkage, nullptr, Name); |
| 1644 | |
Anders Carlsson | da80af3 | 2011-01-29 18:20:20 +0000 | [diff] [blame] | 1645 | if (OldGV) { |
| 1646 | // Replace occurrences of the old variable if needed. |
| 1647 | GV->takeName(OldGV); |
| 1648 | |
| 1649 | if (!OldGV->use_empty()) { |
| 1650 | llvm::Constant *NewPtrForOldDecl = |
| 1651 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 1652 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 1653 | } |
| 1654 | |
| 1655 | OldGV->eraseFromParent(); |
| 1656 | } |
| 1657 | |
| 1658 | return GV; |
| 1659 | } |
| 1660 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1661 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 1662 | /// 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] | 1663 | /// then it will be created with the specified type instead of whatever the |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1664 | /// normal requested type would be. |
| 1665 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1666 | llvm::Type *Ty) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1667 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 1668 | QualType ASTTy = D->getType(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1669 | if (!Ty) |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1670 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1672 | llvm::PointerType *PTy = |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1673 | llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1675 | StringRef MangledName = getMangledName(D); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1676 | return GetOrCreateLLVMGlobal(MangledName, PTy, D); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 1680 | /// specified type and name. |
| 1681 | llvm::Constant * |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1682 | CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1683 | StringRef Name) { |
Rafael Espindola | b73c973 | 2014-05-22 23:33:27 +0000 | [diff] [blame] | 1684 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1687 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 1688 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 1689 | |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 1690 | if (MayDeferGeneration(D)) { |
| 1691 | // If we have not seen a reference to this variable yet, place it |
| 1692 | // into the deferred declarations table to be emitted if needed |
| 1693 | // later. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1694 | StringRef MangledName = getMangledName(D); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1695 | if (!GetGlobalValue(MangledName)) { |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1696 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1697 | return; |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | // The tentative definition is the only definition. |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1702 | EmitGlobalVarDefinition(D); |
| 1703 | } |
| 1704 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1705 | CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { |
Ken Dyck | 9a64869 | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 1706 | return Context.toCharUnitsFromBits( |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1707 | TheDataLayout.getTypeStoreSizeInBits(Ty)); |
Ken Dyck | 98ca794 | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 1710 | unsigned CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D, |
| 1711 | unsigned AddrSpace) { |
| 1712 | if (LangOpts.CUDA && CodeGenOpts.CUDAIsDevice) { |
| 1713 | if (D->hasAttr<CUDAConstantAttr>()) |
| 1714 | AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_constant); |
| 1715 | else if (D->hasAttr<CUDASharedAttr>()) |
| 1716 | AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_shared); |
| 1717 | else |
| 1718 | AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_device); |
| 1719 | } |
| 1720 | |
| 1721 | return AddrSpace; |
| 1722 | } |
| 1723 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 1724 | template<typename SomeDecl> |
| 1725 | void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, |
| 1726 | llvm::GlobalValue *GV) { |
| 1727 | if (!getLangOpts().CPlusPlus) |
| 1728 | return; |
| 1729 | |
| 1730 | // Must have 'used' attribute, or else inline assembly can't rely on |
| 1731 | // the name existing. |
| 1732 | if (!D->template hasAttr<UsedAttr>()) |
| 1733 | return; |
| 1734 | |
| 1735 | // Must have internal linkage and an ordinary name. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1736 | if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 1737 | return; |
| 1738 | |
| 1739 | // Must be in an extern "C" context. Entities declared directly within |
| 1740 | // 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] | 1741 | const SomeDecl *First = D->getFirstDecl(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1742 | if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 1743 | return; |
| 1744 | |
| 1745 | // OK, this is an internal linkage entity inside an extern "C" linkage |
| 1746 | // specification. Make a note of that so we can give it the "expected" |
| 1747 | // mangled name if nothing else is using that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 1748 | std::pair<StaticExternCMap::iterator, bool> R = |
| 1749 | StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 1750 | |
| 1751 | // If we have multiple internal linkage entities with the same name |
| 1752 | // in extern "C" regions, none of them gets that name. |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 1753 | if (!R.second) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1754 | R.first->second = nullptr; |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1757 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1758 | llvm::Constant *Init = nullptr; |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1759 | QualType ASTTy = D->getType(); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 1760 | CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 1761 | bool NeedsGlobalCtor = false; |
| 1762 | bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1764 | const VarDecl *InitDecl; |
| 1765 | const Expr *InitExpr = D->getAnyInitializer(InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 1766 | |
Anders Carlsson | ca4a545 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 1767 | if (!InitExpr) { |
Eli Friedman | 6859a1b | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 1768 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 1769 | // implicitly initialized with { 0 }. |
| 1770 | // |
| 1771 | // Note that tentative definitions are only emitted at the end of |
| 1772 | // a translation unit, so they should never have incomplete |
| 1773 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 1774 | // never attempt to emit a tentative definition if a real one |
| 1775 | // exists. A use may still exists, however, so we still may need |
| 1776 | // to do a RAUW. |
| 1777 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | f18318c | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 1778 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1779 | } else { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 1780 | initializedGlobalDecl = GlobalDecl(D); |
| 1781 | Init = EmitConstantInit(*InitDecl); |
Sebastian Redl | 4a7eab2 | 2012-02-25 20:51:20 +0000 | [diff] [blame] | 1782 | |
Fariborz Jahanian | 6362803 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 1783 | if (!Init) { |
Anders Carlsson | ca4a545 | 2010-01-26 17:43:42 +0000 | [diff] [blame] | 1784 | QualType T = InitExpr->getType(); |
Douglas Gregor | d450f06 | 2010-05-05 20:15:55 +0000 | [diff] [blame] | 1785 | if (D->getType()->isReferenceType()) |
| 1786 | T = D->getType(); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1787 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1788 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1789 | Init = EmitNullConstant(T); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 1790 | NeedsGlobalCtor = true; |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 1791 | } else { |
| 1792 | ErrorUnsupported(D, "static initializer"); |
| 1793 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 1794 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1795 | } else { |
| 1796 | // 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] | 1797 | // initializer position (just in case this entry was delayed) if we |
| 1798 | // also don't need to register a destructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1799 | if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 1800 | DelayedCXXInitPosition.erase(D); |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 1801 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1802 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1804 | llvm::Type* InitType = Init->getType(); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1805 | llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1807 | // Strip off a bitcast if we got one back. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1808 | if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1809 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
Matt Arsenault | 00e65b2 | 2013-11-15 02:19:52 +0000 | [diff] [blame] | 1810 | CE->getOpcode() == llvm::Instruction::AddrSpaceCast || |
| 1811 | // All zero index gep. |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1812 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1813 | Entry = CE->getOperand(0); |
| 1814 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1816 | // Entry is now either a Function or GlobalVariable. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 1817 | auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1819 | // We have a definition after a declaration with the wrong type. |
| 1820 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 1821 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 1822 | // (which will be a definition). |
| 1823 | // |
| 1824 | // This happens if there is a prototype for a global (e.g. |
| 1825 | // "extern int x[];") and then a definition of a different type (e.g. |
| 1826 | // "int x[10];"). This also happens when an initializer has a different type |
| 1827 | // from the type of the global (this happens with unions). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1828 | if (!GV || |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1829 | GV->getType()->getElementType() != InitType || |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1830 | GV->getType()->getAddressSpace() != |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 1831 | GetGlobalVarAddressSpace(D, getContext().getTargetAddressSpace(ASTTy))) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 1833 | // 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] | 1834 | Entry->setName(StringRef()); |
Daniel Dunbar | b2f4cdb | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1836 | // Make a new global with the correct type, this is now guaranteed to work. |
| 1837 | GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1838 | |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1839 | // Replace all uses of the old global with the new global |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1841 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1842 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1843 | |
| 1844 | // Erase the old global, since it is no longer used. |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1845 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 1846 | } |
Devang Patel | 19c2b9a | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 1847 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 1848 | MaybeHandleStaticInExternC(D, GV); |
| 1849 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1850 | if (D->hasAttr<AnnotateAttr>()) |
| 1851 | AddGlobalAnnotations(D, GV); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1853 | GV->setInitializer(Init); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1854 | |
| 1855 | // If it is safe to mark the global 'constant', do so now. |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1856 | GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && |
| 1857 | isTypeConstant(D->getType(), true)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1859 | GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 1860 | |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1861 | // Set the llvm linkage type as appropriate. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1862 | llvm::GlobalValue::LinkageTypes Linkage = |
| 1863 | getLLVMLinkageVarDefinition(D, GV->isConstant()); |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1864 | GV->setLinkage(Linkage); |
Nico Rieck | bb0554f | 2014-01-14 15:23:53 +0000 | [diff] [blame] | 1865 | if (D->hasAttr<DLLImportAttr>()) |
| 1866 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 1867 | else if (D->hasAttr<DLLExportAttr>()) |
| 1868 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 1869 | |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1870 | if (Linkage == llvm::GlobalVariable::CommonLinkage) |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1871 | // common vars aren't constant even if declared const. |
| 1872 | GV->setConstant(false); |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 1873 | |
Rafael Espindola | 502f65a | 2014-05-05 20:21:03 +0000 | [diff] [blame] | 1874 | setNonAliasAttributes(D, GV); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1875 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1876 | // Emit the initializer function if necessary. |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 1877 | if (NeedsGlobalCtor || NeedsGlobalDtor) |
| 1878 | EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1879 | |
Kostya Serebryany | 28a26c8 | 2012-08-21 06:53:28 +0000 | [diff] [blame] | 1880 | // If we are compiling with ASan, add metadata indicating dynamically |
| 1881 | // initialized globals. |
Will Dietz | f54319c | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 1882 | if (SanOpts.Address && NeedsGlobalCtor) { |
Kostya Serebryany | 28a26c8 | 2012-08-21 06:53:28 +0000 | [diff] [blame] | 1883 | llvm::Module &M = getModule(); |
| 1884 | |
| 1885 | llvm::NamedMDNode *DynamicInitializers = |
| 1886 | M.getOrInsertNamedMetadata("llvm.asan.dynamically_initialized_globals"); |
| 1887 | llvm::Value *GlobalToAdd[] = { GV }; |
| 1888 | llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalToAdd); |
| 1889 | DynamicInitializers->addOperand(ThisGlobal); |
| 1890 | } |
| 1891 | |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1892 | // Emit global variable debug information. |
Eric Christopher | 7cdf948 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 1893 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Douglas Gregor | b0eea8b | 2012-10-23 20:05:01 +0000 | [diff] [blame] | 1894 | if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 1895 | DI->EmitGlobalVariable(GV, D); |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1896 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1897 | |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 1898 | static bool isVarDeclStrongDefinition(const VarDecl *D, bool NoCommon) { |
| 1899 | // Don't give variables common linkage if -fno-common was specified unless it |
| 1900 | // was overridden by a NoCommon attribute. |
| 1901 | if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) |
| 1902 | return true; |
| 1903 | |
| 1904 | // C11 6.9.2/2: |
| 1905 | // A declaration of an identifier for an object that has file scope without |
| 1906 | // an initializer, and without a storage-class specifier or with the |
| 1907 | // storage-class specifier static, constitutes a tentative definition. |
| 1908 | if (D->getInit() || D->hasExternalStorage()) |
| 1909 | return true; |
| 1910 | |
| 1911 | // A variable cannot be both common and exist in a section. |
| 1912 | if (D->hasAttr<SectionAttr>()) |
| 1913 | return true; |
| 1914 | |
| 1915 | // Thread local vars aren't considered common linkage. |
| 1916 | if (D->getTLSKind()) |
| 1917 | return true; |
| 1918 | |
| 1919 | // Tentative definitions marked with WeakImportAttr are true definitions. |
| 1920 | if (D->hasAttr<WeakImportAttr>()) |
| 1921 | return true; |
| 1922 | |
| 1923 | return false; |
| 1924 | } |
| 1925 | |
Hans Wennborg | 1a3a074 | 2014-05-14 19:54:53 +0000 | [diff] [blame] | 1926 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame^] | 1927 | const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) { |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1928 | if (Linkage == GVA_Internal) |
| 1929 | return llvm::Function::InternalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1930 | |
| 1931 | if (D->hasAttr<WeakAttr>()) { |
| 1932 | if (IsConstantVariable) |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1933 | return llvm::GlobalVariable::WeakODRLinkage; |
| 1934 | else |
| 1935 | return llvm::GlobalVariable::WeakAnyLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | // We are guaranteed to have a strong definition somewhere else, |
| 1939 | // so we can use available_externally linkage. |
| 1940 | if (Linkage == GVA_AvailableExternally) |
| 1941 | return llvm::Function::AvailableExternallyLinkage; |
| 1942 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1943 | // Note that Apple's kernel linker doesn't support symbol |
| 1944 | // coalescing, so we need to avoid linkonce and weak linkages there. |
| 1945 | // Normally, this means we just map to internal, but for explicit |
| 1946 | // instantiations we'll map to external. |
| 1947 | |
| 1948 | // In C++, the compiler has to emit a definition in every translation unit |
| 1949 | // that references the function. We should use linkonce_odr because |
| 1950 | // a) if all references in this translation unit are optimized away, we |
| 1951 | // don't need to codegen it. b) if the function persists, it needs to be |
| 1952 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 1953 | // definition is dependable. |
| 1954 | if (Linkage == GVA_DiscardableODR) |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 1955 | return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1956 | : llvm::Function::InternalLinkage; |
| 1957 | |
| 1958 | // An explicit instantiation of a template has weak linkage, since |
| 1959 | // explicit instantiations can occur in multiple translation units |
| 1960 | // and must all be equivalent. However, we are not allowed to |
| 1961 | // throw away these explicit instantiations. |
| 1962 | if (Linkage == GVA_StrongODR) |
| 1963 | return !Context.getLangOpts().AppleKext ? llvm::Function::WeakODRLinkage |
| 1964 | : llvm::Function::ExternalLinkage; |
| 1965 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1966 | // If required by the ABI, give definitions of static data members with inline |
| 1967 | // initializers at least linkonce_odr linkage. |
| 1968 | if (getCXXABI().isInlineInitializedStaticDataMemberLinkOnce() && |
| 1969 | isa<VarDecl>(D) && |
| 1970 | isVarDeclInlineInitializedStaticDataMember(cast<VarDecl>(D))) |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 1971 | return llvm::GlobalValue::LinkOnceODRLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1972 | |
| 1973 | // C++ doesn't have tentative definitions and thus cannot have common |
| 1974 | // linkage. |
| 1975 | if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && |
| 1976 | !isVarDeclStrongDefinition(cast<VarDecl>(D), CodeGenOpts.NoCommon)) |
David Majnemer | 9832a3d | 2014-04-10 16:53:16 +0000 | [diff] [blame] | 1977 | return llvm::GlobalVariable::CommonLinkage; |
| 1978 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1979 | // selectany symbols are externally visible, so use weak instead of |
| 1980 | // linkonce. MSVC optimizes away references to const selectany globals, so |
| 1981 | // all definitions should be the same and ODR linkage should be used. |
| 1982 | // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx |
| 1983 | if (D->hasAttr<SelectAnyAttr>()) |
| 1984 | return llvm::GlobalVariable::WeakODRLinkage; |
| 1985 | |
| 1986 | // Otherwise, we have strong external linkage. |
| 1987 | assert(Linkage == GVA_StrongExternal); |
Fariborz Jahanian | 1518a5ec | 2010-10-27 16:21:54 +0000 | [diff] [blame] | 1988 | return llvm::GlobalVariable::ExternalLinkage; |
| 1989 | } |
| 1990 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1991 | llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition( |
| 1992 | const VarDecl *VD, bool IsConstant) { |
| 1993 | GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); |
Hans Wennborg | 275efb9 | 2014-05-28 01:52:23 +0000 | [diff] [blame^] | 1994 | return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant); |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 1997 | /// Replace the uses of a function that was declared with a non-proto type. |
| 1998 | /// We want to silently drop extra arguments from call sites |
| 1999 | static void replaceUsesOfNonProtoConstant(llvm::Constant *old, |
| 2000 | llvm::Function *newFn) { |
| 2001 | // Fast path. |
| 2002 | if (old->use_empty()) return; |
| 2003 | |
| 2004 | llvm::Type *newRetTy = newFn->getReturnType(); |
| 2005 | SmallVector<llvm::Value*, 4> newArgs; |
| 2006 | |
| 2007 | for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); |
| 2008 | ui != ue; ) { |
| 2009 | llvm::Value::use_iterator use = ui++; // Increment before the use is erased. |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 2010 | llvm::User *user = use->getUser(); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Recognize and replace uses of bitcasts. Most calls to |
| 2013 | // unprototyped functions will use bitcasts. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2014 | if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2015 | if (bitcast->getOpcode() == llvm::Instruction::BitCast) |
| 2016 | replaceUsesOfNonProtoConstant(bitcast, newFn); |
| 2017 | continue; |
| 2018 | } |
| 2019 | |
| 2020 | // Recognize calls to the function. |
| 2021 | llvm::CallSite callSite(user); |
| 2022 | if (!callSite) continue; |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 2023 | if (!callSite.isCallee(&*use)) continue; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2024 | |
| 2025 | // If the return types don't match exactly, then we can't |
| 2026 | // transform this call unless it's dead. |
| 2027 | if (callSite->getType() != newRetTy && !callSite->use_empty()) |
| 2028 | continue; |
| 2029 | |
| 2030 | // Get the call site's attribute list. |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 2031 | SmallVector<llvm::AttributeSet, 8> newAttrs; |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2032 | llvm::AttributeSet oldAttrs = callSite.getAttributes(); |
| 2033 | |
| 2034 | // Collect any return attributes from the call. |
Bill Wendling | 304f6f0 | 2013-01-21 21:57:40 +0000 | [diff] [blame] | 2035 | if (oldAttrs.hasAttributes(llvm::AttributeSet::ReturnIndex)) |
Bill Wendling | b7abb30 | 2013-01-21 22:45:00 +0000 | [diff] [blame] | 2036 | newAttrs.push_back( |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 2037 | llvm::AttributeSet::get(newFn->getContext(), |
| 2038 | oldAttrs.getRetAttributes())); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2039 | |
| 2040 | // If the function was passed too few arguments, don't transform. |
| 2041 | unsigned newNumArgs = newFn->arg_size(); |
| 2042 | if (callSite.arg_size() < newNumArgs) continue; |
| 2043 | |
| 2044 | // If extra arguments were passed, we silently drop them. |
| 2045 | // If any of the types mismatch, we don't transform. |
| 2046 | unsigned argNo = 0; |
| 2047 | bool dontTransform = false; |
| 2048 | for (llvm::Function::arg_iterator ai = newFn->arg_begin(), |
| 2049 | ae = newFn->arg_end(); ai != ae; ++ai, ++argNo) { |
| 2050 | if (callSite.getArgument(argNo)->getType() != ai->getType()) { |
| 2051 | dontTransform = true; |
| 2052 | break; |
| 2053 | } |
| 2054 | |
| 2055 | // Add any parameter attributes. |
Bill Wendling | ce2f9c5 | 2013-01-23 06:15:10 +0000 | [diff] [blame] | 2056 | if (oldAttrs.hasAttributes(argNo + 1)) |
| 2057 | newAttrs. |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 2058 | push_back(llvm:: |
| 2059 | AttributeSet::get(newFn->getContext(), |
| 2060 | oldAttrs.getParamAttributes(argNo + 1))); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2061 | } |
| 2062 | if (dontTransform) |
| 2063 | continue; |
| 2064 | |
Bill Wendling | 63ffde5 | 2013-01-18 21:26:07 +0000 | [diff] [blame] | 2065 | if (oldAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex)) |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 2066 | newAttrs.push_back(llvm::AttributeSet::get(newFn->getContext(), |
| 2067 | oldAttrs.getFnAttributes())); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2068 | |
| 2069 | // Okay, we can transform this. Create the new call instruction and copy |
| 2070 | // over the required information. |
| 2071 | newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo); |
| 2072 | |
| 2073 | llvm::CallSite newCall; |
| 2074 | if (callSite.isCall()) { |
| 2075 | newCall = llvm::CallInst::Create(newFn, newArgs, "", |
| 2076 | callSite.getInstruction()); |
| 2077 | } else { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2078 | auto *oldInvoke = cast<llvm::InvokeInst>(callSite.getInstruction()); |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2079 | newCall = llvm::InvokeInst::Create(newFn, |
| 2080 | oldInvoke->getNormalDest(), |
| 2081 | oldInvoke->getUnwindDest(), |
| 2082 | newArgs, "", |
| 2083 | callSite.getInstruction()); |
| 2084 | } |
| 2085 | newArgs.clear(); // for the next iteration |
| 2086 | |
| 2087 | if (!newCall->getType()->isVoidTy()) |
| 2088 | newCall->takeName(callSite.getInstruction()); |
| 2089 | newCall.setAttributes( |
| 2090 | llvm::AttributeSet::get(newFn->getContext(), newAttrs)); |
| 2091 | newCall.setCallingConv(callSite.getCallingConv()); |
| 2092 | |
| 2093 | // Finally, remove the old call, replacing any uses with the new one. |
| 2094 | if (!callSite->use_empty()) |
| 2095 | callSite->replaceAllUsesWith(newCall.getInstruction()); |
| 2096 | |
| 2097 | // Copy debug location attached to CI. |
| 2098 | if (!callSite->getDebugLoc().isUnknown()) |
| 2099 | newCall->setDebugLoc(callSite->getDebugLoc()); |
| 2100 | callSite->eraseFromParent(); |
| 2101 | } |
| 2102 | } |
| 2103 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 2104 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 2105 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 2106 | /// existing call uses of the old function in the module, this adjusts them to |
| 2107 | /// call the new function directly. |
| 2108 | /// |
| 2109 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 2110 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 2111 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 2112 | /// run at -O0. |
| 2113 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 2114 | llvm::Function *NewFn) { |
| 2115 | // 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] | 2116 | if (!isa<llvm::Function>(Old)) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2118 | replaceUsesOfNonProtoConstant(Old, NewFn); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 2121 | void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { |
| 2122 | TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); |
| 2123 | // If we have a definition, this might be a deferred decl. If the |
| 2124 | // instantiation is explicit, make sure we emit it at the end. |
| 2125 | if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) |
| 2126 | GetAddrOfGlobalVar(VD); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 2127 | |
| 2128 | EmitTopLevelDecl(VD); |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 2129 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2130 | |
Rafael Espindola | cd7743b | 2013-12-09 16:01:03 +0000 | [diff] [blame] | 2131 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, |
| 2132 | llvm::GlobalValue *GV) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2133 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2134 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 2135 | // Compute the function info and LLVM type. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2136 | const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); |
| 2137 | llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2138 | |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 2139 | // Get or create the prototype for the function. |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2140 | if (!GV) { |
| 2141 | llvm::Constant *C = |
| 2142 | GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer*/ true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2144 | // Strip off a bitcast if we got one back. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2145 | if (auto *CE = dyn_cast<llvm::ConstantExpr>(C)) { |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2146 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 2147 | GV = cast<llvm::GlobalValue>(CE->getOperand(0)); |
| 2148 | } else { |
| 2149 | GV = cast<llvm::GlobalValue>(C); |
| 2150 | } |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 2151 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2153 | if (!GV->isDeclaration()) { |
Argyrios Kyrtzidis | f405dd6 | 2013-11-23 18:41:35 +0000 | [diff] [blame] | 2154 | getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name); |
| 2155 | return; |
| 2156 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2158 | if (GV->getType()->getElementType() != Ty) { |
Daniel Dunbar | 99d2835 | 2009-03-09 23:53:08 +0000 | [diff] [blame] | 2159 | // If the types mismatch then we have to rewrite the definition. |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2160 | assert(GV->isDeclaration() && "Shouldn't replace non-declaration"); |
Chris Lattner | 832323e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | 5eaee56 | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 2162 | // F is the Function* for the one with the wrong type, we must make a new |
| 2163 | // Function* and update everything that used F (a declaration) with the new |
| 2164 | // Function* (which will be a definition). |
| 2165 | // |
| 2166 | // This happens if there is a prototype for a function |
| 2167 | // (e.g. "int f()") and then a definition of a different type |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2168 | // (e.g. "int f(int x)"). Move the old function aside so that it |
| 2169 | // doesn't interfere with GetAddrOfFunction. |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2170 | GV->setName(StringRef()); |
| 2171 | auto *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
John McCall | 49954ca | 2012-12-12 22:21:47 +0000 | [diff] [blame] | 2173 | // This might be an implementation of a function without a |
| 2174 | // prototype, in which case, try to do special replacement of |
| 2175 | // calls which match the new prototype. The really key thing here |
| 2176 | // is that we also potentially drop arguments from the call site |
| 2177 | // so as to make a direct call, which makes the inliner happier |
| 2178 | // and suppresses a number of optimizer warnings (!) about |
| 2179 | // dropping arguments. |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2180 | if (!GV->use_empty()) { |
| 2181 | ReplaceUsesOfNonProtoTypeWithRealFunction(GV, NewFn); |
| 2182 | GV->removeDeadConstantUsers(); |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 2183 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
Chris Lattner | 5eaee56 | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 2185 | // Replace uses of F with the Function we will endow with a body. |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2186 | if (!GV->use_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | llvm::Constant *NewPtrForOldDecl = |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2188 | llvm::ConstantExpr::getBitCast(NewFn, GV->getType()); |
| 2189 | GV->replaceAllUsesWith(NewPtrForOldDecl); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 2190 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | 5eaee56 | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 2192 | // Ok, delete the old function now, which is dead. |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2193 | GV->eraseFromParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Rafael Espindola | 489c66b | 2014-05-08 14:33:38 +0000 | [diff] [blame] | 2195 | GV = NewFn; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 2198 | // We need to set linkage and visibility on the function before |
| 2199 | // generating code for it because various parts of IR generation |
| 2200 | // want to propagate this information down (e.g. to local static |
| 2201 | // declarations). |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2202 | auto *Fn = cast<llvm::Function>(GV); |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 2203 | setFunctionLinkage(GD, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2204 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 2205 | // FIXME: this is redundant with part of setFunctionDefinitionAttributes |
Anders Carlsson | c6a4789 | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 2206 | setGlobalVisibility(Fn, D); |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 2207 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 2208 | MaybeHandleStaticInExternC(D, Fn); |
| 2209 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2210 | CodeGenFunction(*this).GenerateCode(D, Fn, FI); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 2211 | |
Rafael Espindola | e033c8c | 2014-05-08 15:26:12 +0000 | [diff] [blame] | 2212 | setFunctionDefinitionAttributes(D, Fn); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 2213 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2215 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 2216 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2217 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 2218 | AddGlobalDtor(Fn, DA->getPriority()); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2219 | if (D->hasAttr<AnnotateAttr>()) |
| 2220 | AddGlobalAnnotations(D, Fn); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 2223 | static llvm::GlobalObject &getGlobalObjectInExpr(DiagnosticsEngine &Diags, |
| 2224 | const AliasAttr *AA, |
| 2225 | llvm::Constant *C) { |
| 2226 | if (auto *GO = dyn_cast<llvm::GlobalObject>(C)) |
| 2227 | return *GO; |
| 2228 | |
| 2229 | auto *GA = dyn_cast<llvm::GlobalAlias>(C); |
| 2230 | if (GA) { |
| 2231 | if (GA->mayBeOverridden()) { |
| 2232 | Diags.Report(AA->getLocation(), diag::warn_alias_to_weak_alias) |
| 2233 | << GA->getAliasee()->getName() << GA->getName(); |
| 2234 | } |
| 2235 | |
| 2236 | return *GA->getAliasee(); |
| 2237 | } |
| 2238 | |
| 2239 | auto *CE = cast<llvm::ConstantExpr>(C); |
| 2240 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 2241 | CE->getOpcode() == llvm::Instruction::GetElementPtr || |
| 2242 | CE->getOpcode() == llvm::Instruction::AddrSpaceCast); |
| 2243 | return *cast<llvm::GlobalObject>(CE->getOperand(0)); |
| 2244 | } |
| 2245 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2246 | void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2247 | const auto *D = cast<ValueDecl>(GD.getDecl()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2248 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2249 | assert(AA && "Not an alias?"); |
| 2250 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2251 | StringRef MangledName = getMangledName(GD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2253 | // If there is a definition in the module, then it wins over the alias. |
| 2254 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 2255 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 2256 | if (Entry && !Entry->isDeclaration()) |
| 2257 | return; |
| 2258 | |
Rafael Espindola | 208b5c0 | 2013-10-22 19:26:13 +0000 | [diff] [blame] | 2259 | Aliases.push_back(GD); |
| 2260 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2261 | llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Create a reference to the named value. This ensures that it is emitted |
| 2264 | // if a deferred decl. |
| 2265 | llvm::Constant *Aliasee; |
| 2266 | if (isa<llvm::FunctionType>(DeclTy)) |
Alex Rosenberg | ba03612 | 2012-10-05 23:12:53 +0000 | [diff] [blame] | 2267 | Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 2268 | /*ForVTable=*/false); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2269 | else |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2270 | Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2271 | llvm::PointerType::getUnqual(DeclTy), |
| 2272 | nullptr); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2273 | |
| 2274 | // Create the new alias itself, but don't set a name yet. |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 2275 | auto *GA = llvm::GlobalAlias::create( |
Rafael Espindola | 6172277 | 2014-05-17 19:58:16 +0000 | [diff] [blame] | 2276 | cast<llvm::PointerType>(Aliasee->getType())->getElementType(), 0, |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 2277 | llvm::Function::ExternalLinkage, "", |
Rafael Espindola | 6172277 | 2014-05-17 19:58:16 +0000 | [diff] [blame] | 2278 | &getGlobalObjectInExpr(Diags, AA, Aliasee)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2280 | if (Entry) { |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 2281 | if (GA->getAliasee() == Entry) { |
| 2282 | Diags.Report(AA->getLocation(), diag::err_cyclic_alias); |
| 2283 | return; |
| 2284 | } |
| 2285 | |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2286 | assert(Entry->isDeclaration()); |
| 2287 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2288 | // If there is a declaration in the module, then we had an extern followed |
| 2289 | // by the alias, as in: |
| 2290 | // extern int test6(); |
| 2291 | // ... |
| 2292 | // int test6() __attribute__((alias("test7"))); |
| 2293 | // |
| 2294 | // Remove it and replace uses of it with the alias. |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2295 | GA->takeName(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2297 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2298 | Entry->getType())); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2299 | Entry->eraseFromParent(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 2300 | } else { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 2301 | GA->setName(MangledName); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2302 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 2304 | // Set attributes which are particular to an alias; this is a |
| 2305 | // specialization of the attributes which may be set on a global |
| 2306 | // variable/function. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2307 | if (D->hasAttr<DLLExportAttr>()) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2308 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 2309 | // The dllexport attribute is ignored for undefined symbols. |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2310 | if (FD->hasBody()) |
Nico Rieck | bb0554f | 2014-01-14 15:23:53 +0000 | [diff] [blame] | 2311 | GA->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 2312 | } else { |
Nico Rieck | bb0554f | 2014-01-14 15:23:53 +0000 | [diff] [blame] | 2313 | GA->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 2314 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | } else if (D->hasAttr<WeakAttr>() || |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2316 | D->hasAttr<WeakRefAttr>() || |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2317 | D->isWeakImported()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 2318 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 2319 | } |
| 2320 | |
| 2321 | SetCommonAttributes(D, GA); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 2324 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 2325 | ArrayRef<llvm::Type*> Tys) { |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 2326 | return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, |
Benjamin Kramer | 8d375ce | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 2327 | Tys); |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 2328 | } |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 2329 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2330 | static llvm::StringMapEntry<llvm::Constant*> & |
| 2331 | GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
| 2332 | const StringLiteral *Literal, |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 2333 | bool TargetIsLSB, |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2334 | bool &IsUTF16, |
| 2335 | unsigned &StringLength) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2336 | StringRef String = Literal->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2337 | unsigned NumBytes = String.size(); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2338 | |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 2339 | // Check for simple case. |
| 2340 | if (!Literal->containsNonAsciiOrNull()) { |
| 2341 | StringLength = NumBytes; |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2342 | return Map.GetOrCreateValue(String); |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 2345 | // Otherwise, convert the UTF8 literals into a string of shorts. |
| 2346 | IsUTF16 = true; |
| 2347 | |
| 2348 | SmallVector<UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls. |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 2349 | const UTF8 *FromPtr = (const UTF8 *)String.data(); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2350 | UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | |
Fariborz Jahanian | 535618b | 2010-09-07 19:57:04 +0000 | [diff] [blame] | 2352 | (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 2353 | &ToPtr, ToPtr + NumBytes, |
| 2354 | strictConversion); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 2356 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2357 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 2358 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 2359 | // Add an explicit null. |
| 2360 | *ToPtr = 0; |
| 2361 | return Map. |
| 2362 | GetOrCreateValue(StringRef(reinterpret_cast<const char *>(ToBuf.data()), |
| 2363 | (StringLength + 1) * 2)); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Fariborz Jahanian | a52b1f7 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 2366 | static llvm::StringMapEntry<llvm::Constant*> & |
| 2367 | GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
Bill Wendling | 07635cc | 2012-03-29 22:12:09 +0000 | [diff] [blame] | 2368 | const StringLiteral *Literal, |
| 2369 | unsigned &StringLength) { |
| 2370 | StringRef String = Literal->getString(); |
| 2371 | StringLength = String.size(); |
| 2372 | return Map.GetOrCreateValue(String); |
Fariborz Jahanian | a52b1f7 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2375 | llvm::Constant * |
| 2376 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 2377 | unsigned StringLength = 0; |
| 2378 | bool isUTF16 = false; |
| 2379 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2381 | getDataLayout().isLittleEndian(), |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 2382 | isUTF16, StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2384 | if (llvm::Constant *C = Entry.getValue()) |
| 2385 | return C; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2387 | llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 2388 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2389 | llvm::Value *V; |
| 2390 | |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 2391 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2392 | if (!CFConstantStringClassRef) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2393 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2394 | Ty = llvm::ArrayType::get(Ty, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | llvm::Constant *GV = CreateRuntimeVariable(Ty, |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 2396 | "__CFConstantStringClassReference"); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 2397 | // Decay array -> ptr |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2398 | V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
| 2399 | CFConstantStringClassRef = V; |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2400 | } |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2401 | else |
| 2402 | V = CFConstantStringClassRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2403 | |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 2404 | QualType CFTy = getContext().getCFConstantStringType(); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 2405 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2406 | auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 2407 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2408 | llvm::Constant *Fields[4]; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2409 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2410 | // Class pointer. |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2411 | Fields[0] = cast<llvm::ConstantExpr>(V); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2413 | // Flags. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2414 | llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 2416 | llvm::ConstantInt::get(Ty, 0x07C8); |
| 2417 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2418 | // String pointer. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2419 | llvm::Constant *C = nullptr; |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 2420 | if (isUTF16) { |
| 2421 | ArrayRef<uint16_t> Arr = |
David Greene | 0a528db | 2013-01-15 22:09:41 +0000 | [diff] [blame] | 2422 | llvm::makeArrayRef<uint16_t>(reinterpret_cast<uint16_t*>( |
| 2423 | const_cast<char *>(Entry.getKey().data())), |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 2424 | Entry.getKey().size() / 2); |
| 2425 | C = llvm::ConstantDataArray::get(VMContext, Arr); |
| 2426 | } else { |
| 2427 | C = llvm::ConstantDataArray::getString(VMContext, Entry.getKey()); |
| 2428 | } |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 2429 | |
Bill Wendling | 86131f2 | 2012-01-10 08:46:39 +0000 | [diff] [blame] | 2430 | // Note: -fwritable-strings doesn't make the backing store strings of |
| 2431 | // CFStrings writable. (See <rdar://problem/10657500>) |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2432 | auto *GV = |
Rafael Espindola | e1bd71f | 2014-01-21 02:57:56 +0000 | [diff] [blame] | 2433 | new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true, |
| 2434 | llvm::GlobalValue::PrivateLinkage, C, ".str"); |
Rafael Espindola | e79d43da | 2011-01-17 16:31:00 +0000 | [diff] [blame] | 2435 | GV->setUnnamedAddr(true); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2436 | // Don't enforce the target's minimum global alignment, since the only use |
| 2437 | // of the string is via this class initializer. |
Rafael Espindola | d19f80a | 2014-01-20 20:33:18 +0000 | [diff] [blame] | 2438 | // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. Without |
| 2439 | // it LLVM can merge the string with a non unnamed_addr one during LTO. Doing |
| 2440 | // that changes the section it ends in, which surprises ld64. |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 2441 | if (isUTF16) { |
Ken Dyck | a0f99ff | 2010-01-26 18:46:23 +0000 | [diff] [blame] | 2442 | CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); |
| 2443 | GV->setAlignment(Align.getQuantity()); |
Rafael Espindola | d19f80a | 2014-01-20 20:33:18 +0000 | [diff] [blame] | 2444 | GV->setSection("__TEXT,__ustring"); |
Daniel Dunbar | 9c8cd4c | 2011-04-12 23:30:52 +0000 | [diff] [blame] | 2445 | } else { |
| 2446 | CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); |
| 2447 | GV->setAlignment(Align.getQuantity()); |
Rafael Espindola | d19f80a | 2014-01-20 20:33:18 +0000 | [diff] [blame] | 2448 | GV->setSection("__TEXT,__cstring,cstring_literals"); |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 2449 | } |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 2450 | |
| 2451 | // String. |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2452 | Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 2453 | |
Bill Wendling | 82b87f1 | 2012-03-30 00:26:17 +0000 | [diff] [blame] | 2454 | if (isUTF16) |
| 2455 | // Cast the UTF16 string to the correct type. |
| 2456 | Fields[2] = llvm::ConstantExpr::getBitCast(Fields[2], Int8PtrTy); |
| 2457 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2458 | // String length. |
| 2459 | Ty = getTypes().ConvertType(getContext().LongTy); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 2460 | Fields[3] = llvm::ConstantInt::get(Ty, StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2462 | // The struct. |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2463 | C = llvm::ConstantStruct::get(STy, Fields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 2465 | llvm::GlobalVariable::PrivateLinkage, C, |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 2466 | "_unnamed_cfstring_"); |
Rafael Espindola | 8ff1610 | 2014-01-20 20:13:11 +0000 | [diff] [blame] | 2467 | GV->setSection("__DATA,__cfstring"); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 2468 | Entry.setValue(GV); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2469 | |
Anders Carlsson | 41b7c6b | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 2470 | return GV; |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 2471 | } |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2472 | |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 2473 | llvm::Constant * |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 2474 | CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2475 | unsigned StringLength = 0; |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2476 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Fariborz Jahanian | a52b1f7 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 2477 | GetConstantStringEntry(CFConstantStringMap, Literal, StringLength); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2478 | |
| 2479 | if (llvm::Constant *C = Entry.getValue()) |
| 2480 | return C; |
| 2481 | |
Chris Lattner | 00a10ca | 2012-02-06 22:47:00 +0000 | [diff] [blame] | 2482 | llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2483 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2484 | llvm::Value *V; |
Fariborz Jahanian | d3fa701 | 2010-04-23 22:33:39 +0000 | [diff] [blame] | 2485 | // If we don't already have it, get _NSConstantStringClassReference. |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 2486 | if (!ConstantStringClassRef) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2487 | std::string StringClass(getLangOpts().ObjCConstantStringClass); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2488 | llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 2489 | llvm::Constant *GV; |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2490 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | ccdfa39 | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 2491 | std::string str = |
| 2492 | StringClass.empty() ? "OBJC_CLASS_$_NSConstantString" |
| 2493 | : "OBJC_CLASS_$_" + StringClass; |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 2494 | GV = getObjCRuntime().GetClassGlobal(str); |
| 2495 | // Make sure the result is of the correct type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2496 | llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2497 | V = llvm::ConstantExpr::getBitCast(GV, PTy); |
| 2498 | ConstantStringClassRef = V; |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 2499 | } else { |
Fariborz Jahanian | ccdfa39 | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 2500 | std::string str = |
| 2501 | StringClass.empty() ? "_NSConstantStringClassReference" |
| 2502 | : "_" + StringClass + "ClassReference"; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2503 | llvm::Type *PTy = llvm::ArrayType::get(Ty, 0); |
Fariborz Jahanian | ccdfa39 | 2011-05-17 22:46:11 +0000 | [diff] [blame] | 2504 | GV = CreateRuntimeVariable(PTy, str); |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 2505 | // Decay array -> ptr |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2506 | V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
| 2507 | ConstantStringClassRef = V; |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 2508 | } |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2509 | } |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2510 | else |
| 2511 | V = ConstantStringClassRef; |
Douglas Gregor | abf4e0d | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 2512 | |
| 2513 | if (!NSConstantStringType) { |
| 2514 | // Construct the type for a constant NSString. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 2515 | RecordDecl *D = Context.buildImplicitRecord("__builtin_NSString"); |
Douglas Gregor | abf4e0d | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 2516 | D->startDefinition(); |
| 2517 | |
| 2518 | QualType FieldTypes[3]; |
| 2519 | |
| 2520 | // const int *isa; |
| 2521 | FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst()); |
| 2522 | // const char *str; |
| 2523 | FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst()); |
| 2524 | // unsigned int length; |
| 2525 | FieldTypes[2] = Context.UnsignedIntTy; |
| 2526 | |
| 2527 | // Create fields |
| 2528 | for (unsigned i = 0; i < 3; ++i) { |
| 2529 | FieldDecl *Field = FieldDecl::Create(Context, D, |
| 2530 | SourceLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2531 | SourceLocation(), nullptr, |
| 2532 | FieldTypes[i], /*TInfo=*/nullptr, |
| 2533 | /*BitWidth=*/nullptr, |
Douglas Gregor | abf4e0d | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 2534 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2535 | ICIS_NoInit); |
Douglas Gregor | abf4e0d | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 2536 | Field->setAccess(AS_public); |
| 2537 | D->addDecl(Field); |
| 2538 | } |
| 2539 | |
| 2540 | D->completeDefinition(); |
| 2541 | QualType NSTy = Context.getTagDeclType(D); |
| 2542 | NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy)); |
| 2543 | } |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2544 | |
Benjamin Kramer | 22d24c2 | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2545 | llvm::Constant *Fields[3]; |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Class pointer. |
Fariborz Jahanian | d1b6778 | 2013-04-16 15:25:39 +0000 | [diff] [blame] | 2548 | Fields[0] = cast<llvm::ConstantExpr>(V); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2549 | |
| 2550 | // String pointer. |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 2551 | llvm::Constant *C = |
| 2552 | llvm::ConstantDataArray::getString(VMContext, Entry.getKey()); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2553 | |
| 2554 | llvm::GlobalValue::LinkageTypes Linkage; |
| 2555 | bool isConstant; |
Fariborz Jahanian | a52b1f7 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 2556 | Linkage = llvm::GlobalValue::PrivateLinkage; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2557 | isConstant = !LangOpts.WritableStrings; |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2558 | |
| 2559 | auto *GV = new llvm::GlobalVariable(getModule(), C->getType(), isConstant, |
| 2560 | Linkage, C, ".str"); |
Rafael Espindola | de089d4 | 2011-01-17 22:11:21 +0000 | [diff] [blame] | 2561 | GV->setUnnamedAddr(true); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2562 | // Don't enforce the target's minimum global alignment, since the only use |
| 2563 | // of the string is via this class initializer. |
Fariborz Jahanian | a52b1f7 | 2011-05-13 18:13:10 +0000 | [diff] [blame] | 2564 | CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); |
| 2565 | GV->setAlignment(Align.getQuantity()); |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2566 | Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2567 | |
| 2568 | // String length. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2569 | llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2570 | Fields[2] = llvm::ConstantInt::get(Ty, StringLength); |
| 2571 | |
| 2572 | // The struct. |
Douglas Gregor | abf4e0d | 2011-08-09 15:54:21 +0000 | [diff] [blame] | 2573 | C = llvm::ConstantStruct::get(NSConstantStringType, Fields); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2574 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 2575 | llvm::GlobalVariable::PrivateLinkage, C, |
| 2576 | "_unnamed_nsstring_"); |
Rafael Espindola | 63582d6 | 2014-01-20 15:19:43 +0000 | [diff] [blame] | 2577 | const char *NSStringSection = "__OBJC,__cstring_object,regular,no_dead_strip"; |
| 2578 | const char *NSStringNonFragileABISection = |
Rafael Espindola | 8ff1610 | 2014-01-20 20:13:11 +0000 | [diff] [blame] | 2579 | "__DATA,__objc_stringobj,regular,no_dead_strip"; |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2580 | // FIXME. Fix section. |
Rafael Espindola | 8ff1610 | 2014-01-20 20:13:11 +0000 | [diff] [blame] | 2581 | GV->setSection(LangOpts.ObjCRuntime.isNonFragile() |
| 2582 | ? NSStringNonFragileABISection |
| 2583 | : NSStringSection); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2584 | Entry.setValue(GV); |
| 2585 | |
| 2586 | return GV; |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 2589 | QualType CodeGenModule::getObjCFastEnumerationStateType() { |
| 2590 | if (ObjCFastEnumerationStateType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 2591 | RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 2592 | D->startDefinition(); |
| 2593 | |
| 2594 | QualType FieldTypes[] = { |
| 2595 | Context.UnsignedLongTy, |
| 2596 | Context.getPointerType(Context.getObjCIdType()), |
| 2597 | Context.getPointerType(Context.UnsignedLongTy), |
| 2598 | Context.getConstantArrayType(Context.UnsignedLongTy, |
| 2599 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2600 | }; |
| 2601 | |
| 2602 | for (size_t i = 0; i < 4; ++i) { |
| 2603 | FieldDecl *Field = FieldDecl::Create(Context, |
| 2604 | D, |
| 2605 | SourceLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2606 | SourceLocation(), nullptr, |
| 2607 | FieldTypes[i], /*TInfo=*/nullptr, |
| 2608 | /*BitWidth=*/nullptr, |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 2609 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2610 | ICIS_NoInit); |
Douglas Gregor | 636e200 | 2011-08-09 17:23:49 +0000 | [diff] [blame] | 2611 | Field->setAccess(AS_public); |
| 2612 | D->addDecl(Field); |
| 2613 | } |
| 2614 | |
| 2615 | D->completeDefinition(); |
| 2616 | ObjCFastEnumerationStateType = Context.getTagDeclType(D); |
| 2617 | } |
| 2618 | |
| 2619 | return ObjCFastEnumerationStateType; |
| 2620 | } |
| 2621 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 2622 | llvm::Constant * |
| 2623 | CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { |
| 2624 | assert(!E->getType()->isPointerType() && "Strings are always arrays"); |
| 2625 | |
| 2626 | // Don't emit it as the address of the string, emit the string data itself |
| 2627 | // as an inline array. |
Chris Lattner | 00a10ca | 2012-02-06 22:47:00 +0000 | [diff] [blame] | 2628 | if (E->getCharByteWidth() == 1) { |
| 2629 | SmallString<64> Str(E->getString()); |
| 2630 | |
| 2631 | // Resize the string to the right size, which is indicated by its type. |
| 2632 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); |
| 2633 | Str.resize(CAT->getSize().getZExtValue()); |
| 2634 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
| 2635 | } |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2636 | |
| 2637 | auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 2638 | llvm::Type *ElemTy = AType->getElementType(); |
| 2639 | unsigned NumElements = AType->getNumElements(); |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 2640 | |
| 2641 | // Wide strings have either 2-byte or 4-byte elements. |
| 2642 | if (ElemTy->getPrimitiveSizeInBits() == 16) { |
| 2643 | SmallVector<uint16_t, 32> Elements; |
| 2644 | Elements.reserve(NumElements); |
| 2645 | |
| 2646 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 2647 | Elements.push_back(E->getCodeUnit(i)); |
| 2648 | Elements.resize(NumElements); |
| 2649 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Chris Lattner | 02cb171 | 2012-02-06 22:52:04 +0000 | [diff] [blame] | 2652 | assert(ElemTy->getPrimitiveSizeInBits() == 32); |
| 2653 | SmallVector<uint32_t, 32> Elements; |
| 2654 | Elements.reserve(NumElements); |
| 2655 | |
| 2656 | for(unsigned i = 0, e = E->getLength(); i != e; ++i) |
| 2657 | Elements.push_back(E->getCodeUnit(i)); |
| 2658 | Elements.resize(NumElements); |
| 2659 | return llvm::ConstantDataArray::get(VMContext, Elements); |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2662 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 2663 | /// constant array for the given string literal. |
| 2664 | llvm::Constant * |
| 2665 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2666 | CharUnits Align = getContext().getAlignOfGlobalVarInChars(S->getType()); |
James Molloy | e735b2b | 2014-05-09 16:28:56 +0000 | [diff] [blame] | 2667 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2668 | llvm::StringMapEntry<llvm::GlobalVariable *> *Entry = nullptr; |
| 2669 | llvm::GlobalVariable *GV = nullptr; |
| 2670 | if (!LangOpts.WritableStrings) { |
| 2671 | llvm::StringMap<llvm::GlobalVariable *> *ConstantStringMap = nullptr; |
| 2672 | switch (S->getCharByteWidth()) { |
| 2673 | case 1: |
| 2674 | ConstantStringMap = &Constant1ByteStringMap; |
| 2675 | break; |
| 2676 | case 2: |
| 2677 | ConstantStringMap = &Constant2ByteStringMap; |
| 2678 | break; |
| 2679 | case 4: |
| 2680 | ConstantStringMap = &Constant4ByteStringMap; |
| 2681 | break; |
| 2682 | default: |
| 2683 | llvm_unreachable("unhandled byte width!"); |
| 2684 | } |
| 2685 | Entry = &ConstantStringMap->GetOrCreateValue(S->getBytes()); |
| 2686 | GV = Entry->getValue(); |
Eli Friedman | 49ddc5f | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 2687 | } |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 2688 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2689 | if (!GV) { |
| 2690 | SmallString<256> MangledNameBuffer; |
| 2691 | StringRef GlobalVariableName; |
| 2692 | llvm::GlobalValue::LinkageTypes LT; |
David Majnemer | 3843a05 | 2014-03-23 17:47:16 +0000 | [diff] [blame] | 2693 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2694 | // Mangle the string literal if the ABI allows for it. However, we cannot |
| 2695 | // do this if we are compiling with ASan or -fwritable-strings because they |
| 2696 | // rely on strings having normal linkage. |
| 2697 | if (!LangOpts.WritableStrings && !SanOpts.Address && |
| 2698 | getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) { |
| 2699 | llvm::raw_svector_ostream Out(MangledNameBuffer); |
| 2700 | getCXXABI().getMangleContext().mangleStringLiteral(S, Out); |
| 2701 | Out.flush(); |
| 2702 | |
| 2703 | LT = llvm::GlobalValue::LinkOnceODRLinkage; |
| 2704 | GlobalVariableName = MangledNameBuffer; |
| 2705 | } else { |
Alp Toker | 5f072d8 | 2014-04-19 23:55:49 +0000 | [diff] [blame] | 2706 | LT = llvm::GlobalValue::PrivateLinkage; |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2707 | GlobalVariableName = ".str"; |
| 2708 | } |
| 2709 | |
| 2710 | // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. |
| 2711 | unsigned AddrSpace = 0; |
| 2712 | if (getLangOpts().OpenCL) |
| 2713 | AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant); |
| 2714 | |
| 2715 | llvm::Constant *C = GetConstantArrayFromStringLiteral(S); |
| 2716 | GV = new llvm::GlobalVariable( |
| 2717 | getModule(), C->getType(), !LangOpts.WritableStrings, LT, C, |
| 2718 | GlobalVariableName, /*InsertBefore=*/nullptr, |
| 2719 | llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
| 2720 | GV->setUnnamedAddr(true); |
| 2721 | if (Entry) |
| 2722 | Entry->setValue(GV); |
| 2723 | } |
| 2724 | |
| 2725 | if (Align.getQuantity() > GV->getAlignment()) |
| 2726 | GV->setAlignment(Align.getQuantity()); |
| 2727 | |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 2728 | return GV; |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2731 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 2732 | /// array for the given ObjCEncodeExpr node. |
| 2733 | llvm::Constant * |
| 2734 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 2735 | std::string Str; |
| 2736 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | 4663a33 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 2737 | |
| 2738 | return GetAddrOfConstantCString(Str); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 2742 | /// GenerateWritableString -- Creates storage for a string literal. |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2743 | static llvm::GlobalVariable *GenerateStringLiteral(StringRef str, |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2744 | bool constant, |
Daniel Dunbar | dfcf599 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 2745 | CodeGenModule &CGM, |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2746 | const char *GlobalName, |
| 2747 | unsigned Alignment) { |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2748 | // Create Constant for this string literal. Don't add a '\0'. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2749 | llvm::Constant *C = |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 2750 | llvm::ConstantDataArray::getString(CGM.getLLVMContext(), str, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2751 | |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2752 | // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. |
Joey Gouly | 561bba2 | 2013-11-14 18:26:10 +0000 | [diff] [blame] | 2753 | unsigned AddrSpace = 0; |
| 2754 | if (CGM.getLangOpts().OpenCL) |
| 2755 | AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant); |
| 2756 | |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2757 | // Create a global variable for this string |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2758 | auto *GV = new llvm::GlobalVariable( |
Joey Gouly | 561bba2 | 2013-11-14 18:26:10 +0000 | [diff] [blame] | 2759 | CGM.getModule(), C->getType(), constant, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2760 | llvm::GlobalValue::PrivateLinkage, C, GlobalName, nullptr, |
Joey Gouly | 561bba2 | 2013-11-14 18:26:10 +0000 | [diff] [blame] | 2761 | llvm::GlobalVariable::NotThreadLocal, AddrSpace); |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2762 | GV->setAlignment(Alignment); |
Rafael Espindola | b7f60e3 | 2011-01-10 22:34:03 +0000 | [diff] [blame] | 2763 | GV->setUnnamedAddr(true); |
| 2764 | return GV; |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2767 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 2768 | /// containing the literal. This contents are exactly that of the |
| 2769 | /// given string, i.e. it will not be null terminated automatically; |
| 2770 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 2771 | /// actually a pointer to an LLVM constant depends on |
| 2772 | /// Feature.WriteableStrings. |
| 2773 | /// |
| 2774 | /// The result has pointer to array type. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2775 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(StringRef Str, |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2776 | const char *GlobalName, |
| 2777 | unsigned Alignment) { |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2778 | // Get the default prefix if a name wasn't specified. |
| 2779 | if (!GlobalName) |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 2780 | GlobalName = ".str"; |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2781 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2782 | if (Alignment == 0) |
| 2783 | Alignment = getContext().getAlignOfGlobalVarInChars(getContext().CharTy) |
| 2784 | .getQuantity(); |
| 2785 | |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 2786 | // Don't share any string literals if strings aren't constant. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2787 | if (LangOpts.WritableStrings) |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2788 | return GenerateStringLiteral(Str, false, *this, GlobalName, Alignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2790 | llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = |
David Majnemer | 58e5bee | 2014-03-24 21:43:36 +0000 | [diff] [blame] | 2791 | Constant1ByteStringMap.GetOrCreateValue(Str); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2792 | |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2793 | if (llvm::GlobalVariable *GV = Entry.getValue()) { |
| 2794 | if (Alignment > GV->getAlignment()) { |
| 2795 | GV->setAlignment(Alignment); |
| 2796 | } |
| 2797 | return GV; |
| 2798 | } |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2799 | |
| 2800 | // Create a global variable for this. |
Chris Lattner | 00a10ca | 2012-02-06 22:47:00 +0000 | [diff] [blame] | 2801 | llvm::GlobalVariable *GV = GenerateStringLiteral(Str, true, *this, GlobalName, |
| 2802 | Alignment); |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2803 | Entry.setValue(GV); |
| 2804 | return GV; |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 2805 | } |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2806 | |
| 2807 | /// GetAddrOfConstantCString - Returns a pointer to a character |
Benjamin Kramer | 0cd19fb | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2808 | /// array containing the literal and a terminating '\0' |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2809 | /// character. The result has pointer to array type. |
Benjamin Kramer | 0cd19fb | 2011-03-05 13:45:23 +0000 | [diff] [blame] | 2810 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &Str, |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2811 | const char *GlobalName, |
| 2812 | unsigned Alignment) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2813 | StringRef StrWithNull(Str.c_str(), Str.size() + 1); |
John McCall | 9b24df4 | 2011-08-04 01:03:22 +0000 | [diff] [blame] | 2814 | return GetAddrOfConstantString(StrWithNull, GlobalName, Alignment); |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 2815 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 2816 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2817 | llvm::Constant *CodeGenModule::GetAddrOfGlobalTemporary( |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2818 | const MaterializeTemporaryExpr *E, const Expr *Init) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2819 | assert((E->getStorageDuration() == SD_Static || |
| 2820 | E->getStorageDuration() == SD_Thread) && "not a global temporary"); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2821 | const auto *VD = cast<VarDecl>(E->getExtendingDecl()); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2822 | |
| 2823 | // If we're not materializing a subobject of the temporary, keep the |
| 2824 | // cv-qualifiers from the type of the MaterializeTemporaryExpr. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2825 | QualType MaterializedType = Init->getType(); |
| 2826 | if (Init == E->GetTemporaryExpr()) |
| 2827 | MaterializedType = E->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2828 | |
| 2829 | llvm::Constant *&Slot = MaterializedGlobalTemporaryMap[E]; |
| 2830 | if (Slot) |
| 2831 | return Slot; |
| 2832 | |
| 2833 | // FIXME: If an externally-visible declaration extends multiple temporaries, |
| 2834 | // we need to give each temporary the same name in every translation unit (and |
| 2835 | // we also need to make the temporaries externally-visible). |
| 2836 | SmallString<256> Name; |
| 2837 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 2838 | getCXXABI().getMangleContext().mangleReferenceTemporary( |
| 2839 | VD, E->getManglingNumber(), Out); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2840 | Out.flush(); |
| 2841 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2842 | APValue *Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2843 | if (E->getStorageDuration() == SD_Static) { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2844 | // We might have a cached constant initializer for this temporary. Note |
| 2845 | // that this might have a different value from the value computed by |
| 2846 | // evaluating the initializer if the surrounding constant expression |
| 2847 | // modifies the temporary. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2848 | Value = getContext().getMaterializedTemporaryValue(E, false); |
| 2849 | if (Value && Value->isUninit()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2850 | Value = nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2853 | // Try evaluating it now, it might have a constant initializer. |
| 2854 | Expr::EvalResult EvalResult; |
| 2855 | if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && |
| 2856 | !EvalResult.hasSideEffects()) |
| 2857 | Value = &EvalResult.Val; |
| 2858 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2859 | llvm::Constant *InitialValue = nullptr; |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2860 | bool Constant = false; |
| 2861 | llvm::Type *Type; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2862 | if (Value) { |
| 2863 | // The temporary has a constant initializer, use it. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2864 | InitialValue = EmitConstantValue(*Value, MaterializedType, nullptr); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2865 | Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value); |
| 2866 | Type = InitialValue->getType(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2867 | } else { |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2868 | // No initializer, the initialization will be provided when we |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2869 | // initialize the declaration which performed lifetime extension. |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2870 | Type = getTypes().ConvertTypeForMem(MaterializedType); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | // Create a global variable for this lifetime-extended temporary. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2874 | llvm::GlobalValue::LinkageTypes Linkage = |
| 2875 | getLLVMLinkageVarDefinition(VD, Constant); |
David Majnemer | 846c283 | 2014-04-29 06:18:53 +0000 | [diff] [blame] | 2876 | // There is no need for this temporary to have global linkage if the global |
| 2877 | // variable has external linkage. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2878 | if (Linkage == llvm::GlobalVariable::ExternalLinkage) |
| 2879 | Linkage = llvm::GlobalVariable::PrivateLinkage; |
| 2880 | unsigned AddrSpace = GetGlobalVarAddressSpace( |
| 2881 | VD, getContext().getTargetAddressSpace(MaterializedType)); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 2882 | auto *GV = new llvm::GlobalVariable( |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 2883 | getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), |
| 2884 | /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, |
| 2885 | AddrSpace); |
David Majnemer | 846c283 | 2014-04-29 06:18:53 +0000 | [diff] [blame] | 2886 | setGlobalVisibility(GV, VD); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2887 | GV->setAlignment( |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 2888 | getContext().getTypeAlignInChars(MaterializedType).getQuantity()); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2889 | if (VD->getTLSKind()) |
| 2890 | setTLSMode(GV, *VD); |
| 2891 | Slot = GV; |
| 2892 | return GV; |
| 2893 | } |
| 2894 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2895 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 2896 | /// properties for an implementation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2898 | ObjCImplementationDecl *D) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 2899 | for (const auto *PID : D->property_impls()) { |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2900 | // Dynamic is just for type-checking. |
| 2901 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2902 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2903 | |
| 2904 | // Determine which methods need to be implemented, some may have |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2905 | // been overridden. Note that ::isPropertyAccessor is not the method |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2906 | // we want, that just indicates if the decl came from a |
| 2907 | // property. What we want to know is if the method is defined in |
| 2908 | // this implementation. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2909 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 2910 | CodeGenFunction(*this).GenerateObjCGetter( |
| 2911 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2912 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2913 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 2914 | CodeGenFunction(*this).GenerateObjCSetter( |
| 2915 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | } |
| 2919 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2920 | static bool needsDestructMethod(ObjCImplementationDecl *impl) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2921 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 2922 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2923 | ivar; ivar = ivar->getNextIvar()) |
| 2924 | if (ivar->getType().isDestructedType()) |
| 2925 | return true; |
| 2926 | |
| 2927 | return false; |
| 2928 | } |
| 2929 | |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2930 | /// EmitObjCIvarInitializations - Emit information for ivar initialization |
| 2931 | /// for an implementation. |
| 2932 | void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2933 | // We might need a .cxx_destruct even if we don't have any ivar initializers. |
| 2934 | if (needsDestructMethod(D)) { |
| 2935 | IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); |
| 2936 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
| 2937 | ObjCMethodDecl *DTORMethod = |
| 2938 | ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2939 | cxxSelector, getContext().VoidTy, nullptr, D, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2940 | /*isInstance=*/true, /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2941 | /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2942 | /*isDefined=*/false, ObjCMethodDecl::Required); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2943 | D->addInstanceMethod(DTORMethod); |
| 2944 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 2945 | D->setHasDestructors(true); |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | // If the implementation doesn't have any ivar initializers, we don't need |
| 2949 | // a .cxx_construct. |
David Chisnall | a9e5460 | 2011-03-17 14:19:08 +0000 | [diff] [blame] | 2950 | if (D->getNumIvarInitializers() == 0) |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2951 | return; |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2952 | |
John McCall | 6a4fa52 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 2953 | IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); |
| 2954 | Selector cxxSelector = getContext().Selectors.getSelector(0, &II); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2955 | // The constructor returns 'self'. |
| 2956 | ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), |
| 2957 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 2958 | D->getLocation(), |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 2959 | cxxSelector, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2960 | getContext().getObjCIdType(), |
| 2961 | nullptr, D, /*isInstance=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2962 | /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2963 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2964 | /*isImplicitlyDeclared=*/true, |
| 2965 | /*isDefined=*/false, |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2966 | ObjCMethodDecl::Required); |
| 2967 | D->addInstanceMethod(CTORMethod); |
| 2968 | CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); |
John McCall | 0d54a17 | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 2969 | D->setHasNonZeroConstructors(true); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2972 | /// EmitNamespace - Emit all declarations in a namespace. |
Anders Carlsson | 237f349 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2973 | void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2974 | for (auto *I : ND->decls()) { |
| 2975 | if (const auto *VD = dyn_cast<VarDecl>(I)) |
David Blaikie | b00f360 | 2013-09-04 21:07:37 +0000 | [diff] [blame] | 2976 | if (VD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
| 2977 | VD->getTemplateSpecializationKind() != TSK_Undeclared) |
| 2978 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2979 | EmitTopLevelDecl(I); |
David Blaikie | b00f360 | 2013-09-04 21:07:37 +0000 | [diff] [blame] | 2980 | } |
Anders Carlsson | 237f349 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2983 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 2984 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | e480ce3 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 2985 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 2986 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 2987 | ErrorUnsupported(LSD, "linkage spec"); |
| 2988 | return; |
| 2989 | } |
| 2990 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2991 | for (auto *I : LSD->decls()) { |
Fariborz Jahanian | 9dd2e08 | 2012-10-26 22:20:25 +0000 | [diff] [blame] | 2992 | // Meta-data for ObjC class includes references to implemented methods. |
| 2993 | // Generate class's method definitions first. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2994 | if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 2995 | for (auto *M : OID->methods()) |
| 2996 | EmitTopLevelDecl(M); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 2997 | } |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2998 | EmitTopLevelDecl(I); |
Fariborz Jahanian | 2d26c29 | 2012-10-26 20:22:11 +0000 | [diff] [blame] | 2999 | } |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3002 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 3003 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3004 | // Ignore dependent declarations. |
| 3005 | if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) |
| 3006 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3008 | switch (D->getKind()) { |
Anders Carlsson | 6c0a6e4 | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 3009 | case Decl::CXXConversion: |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 3010 | case Decl::CXXMethod: |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3011 | case Decl::Function: |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3012 | // Skip function templates |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3013 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 3014 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3015 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3017 | EmitGlobal(cast<FunctionDecl>(D)); |
| 3018 | break; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3019 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3020 | case Decl::Var: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3021 | // Skip variable templates |
| 3022 | if (cast<VarDecl>(D)->getDescribedVarTemplate()) |
| 3023 | return; |
| 3024 | case Decl::VarTemplateSpecialization: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 3025 | EmitGlobal(cast<VarDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3026 | break; |
| 3027 | |
John McCall | 32f44bd | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 3028 | // Indirect fields from global anonymous structs and unions can be |
| 3029 | // ignored; only the actual variable requires IR gen support. |
| 3030 | case Decl::IndirectField: |
| 3031 | break; |
| 3032 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3033 | // C++ Decls |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3034 | case Decl::Namespace: |
Anders Carlsson | 237f349 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 3035 | EmitNamespace(cast<NamespaceDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3036 | break; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3037 | // No code generation needed. |
John McCall | 1e9de05 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 3038 | case Decl::UsingShadow: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3039 | case Decl::ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3040 | case Decl::VarTemplate: |
| 3041 | case Decl::VarTemplatePartialSpecialization: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3042 | case Decl::FunctionTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3043 | case Decl::TypeAliasTemplate: |
Douglas Gregor | 0aa91e0 | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 3044 | case Decl::Block: |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 3045 | case Decl::Empty: |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3046 | break; |
David Blaikie | 3e65d36a | 2014-02-15 21:03:07 +0000 | [diff] [blame] | 3047 | case Decl::Using: // using X; [C++] |
| 3048 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 3049 | DI->EmitUsingDecl(cast<UsingDecl>(*D)); |
| 3050 | return; |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3051 | case Decl::NamespaceAlias: |
| 3052 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 3053 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); |
| 3054 | return; |
David Blaikie | 60a9fbf | 2013-04-26 05:41:06 +0000 | [diff] [blame] | 3055 | case Decl::UsingDirective: // using namespace X; [C++] |
| 3056 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
| 3057 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); |
| 3058 | return; |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3059 | case Decl::CXXConstructor: |
Anders Carlsson | 0ade971 | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 3060 | // Skip function templates |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3061 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || |
| 3062 | cast<FunctionDecl>(D)->isLateTemplateParsed()) |
Anders Carlsson | 0ade971 | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 3063 | return; |
| 3064 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 3065 | getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3066 | break; |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 3067 | case Decl::CXXDestructor: |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3068 | if (cast<FunctionDecl>(D)->isLateTemplateParsed()) |
| 3069 | return; |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 3070 | getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 3071 | break; |
Anders Carlsson | 8783543 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 3072 | |
| 3073 | case Decl::StaticAssert: |
| 3074 | // Nothing to do. |
| 3075 | break; |
| 3076 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 3077 | // Objective-C Decls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 3079 | // Forward declarations, no (immediate) code generation. |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 3080 | case Decl::ObjCInterface: |
Eric Christopher | f8378ca | 2012-07-19 22:22:55 +0000 | [diff] [blame] | 3081 | case Decl::ObjCCategory: |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 3082 | break; |
| 3083 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 3084 | case Decl::ObjCProtocol: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3085 | auto *Proto = cast<ObjCProtocolDecl>(D); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 3086 | if (Proto->isThisDeclarationADefinition()) |
| 3087 | ObjCRuntime->GenerateProtocol(Proto); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3088 | break; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3091 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3092 | // Categories have properties but don't support synthesize so we |
| 3093 | // can ignore them here. |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 3094 | ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3095 | break; |
| 3096 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3097 | case Decl::ObjCImplementation: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3098 | auto *OMD = cast<ObjCImplementationDecl>(D); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 3099 | EmitObjCPropertyImplementations(OMD); |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 3100 | EmitObjCIvarInitializations(OMD); |
Peter Collingbourne | e1d2099 | 2011-07-27 20:29:46 +0000 | [diff] [blame] | 3101 | ObjCRuntime->GenerateClass(OMD); |
Eric Christopher | 3d19de9 | 2012-04-11 05:56:05 +0000 | [diff] [blame] | 3102 | // Emit global variable debug information. |
| 3103 | if (CGDebugInfo *DI = getModuleDebugInfo()) |
Alexey Samsonov | 9c1b9f6 | 2012-12-03 18:28:12 +0000 | [diff] [blame] | 3104 | if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) |
| 3105 | DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( |
| 3106 | OMD->getClassInterface()), OMD->getLocation()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3107 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3109 | case Decl::ObjCMethod: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3110 | auto *OMD = cast<ObjCMethodDecl>(D); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3111 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 3112 | if (OMD->getBody()) |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3113 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 3114 | break; |
| 3115 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | case Decl::ObjCCompatibleAlias: |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3117 | ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3118 | break; |
| 3119 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 3120 | case Decl::LinkageSpec: |
| 3121 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3122 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3123 | |
| 3124 | case Decl::FileScopeAsm: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3125 | auto *AD = cast<FileScopeAsmDecl>(D); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3126 | StringRef AsmString = AD->getAsmString()->getString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3128 | const std::string &S = getModule().getModuleInlineAsm(); |
| 3129 | if (S.empty()) |
| 3130 | getModule().setModuleInlineAsm(AsmString); |
Joerg Sonnenberger | 8c02a24 | 2012-08-10 10:57:52 +0000 | [diff] [blame] | 3131 | else if (S.end()[-1] == '\n') |
Chris Lattner | 84037d3 | 2011-07-23 20:04:25 +0000 | [diff] [blame] | 3132 | getModule().setModuleInlineAsm(S + AsmString.str()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3133 | else |
Benjamin Kramer | b11118b | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 3134 | getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3135 | break; |
| 3136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 3138 | case Decl::Import: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3139 | auto *Import = cast<ImportDecl>(D); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 3140 | |
| 3141 | // Ignore import declarations that come from imported modules. |
| 3142 | if (clang::Module *Owner = Import->getOwningModule()) { |
| 3143 | if (getLangOpts().CurrentModule.empty() || |
| 3144 | Owner->getTopLevelModule()->Name == getLangOpts().CurrentModule) |
| 3145 | break; |
| 3146 | } |
| 3147 | |
Douglas Gregor | bc25ff4 | 2013-01-14 20:53:57 +0000 | [diff] [blame] | 3148 | ImportedModules.insert(Import->getImportedModule()); |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 3149 | break; |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | case Decl::ClassTemplateSpecialization: { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3153 | const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 3154 | if (DebugInfo && |
| 3155 | Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition) |
| 3156 | DebugInfo->completeTemplateDefinition(*Spec); |
| 3157 | } |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 3158 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | default: |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3160 | // Make sure we handled everything we should, every other kind is a |
| 3161 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 3162 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 3163 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 3164 | } |
| 3165 | } |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3166 | |
| 3167 | /// Turns the given pointer into a constant. |
| 3168 | static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, |
| 3169 | const void *Ptr) { |
| 3170 | uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3171 | llvm::Type *i64 = llvm::Type::getInt64Ty(Context); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3172 | return llvm::ConstantInt::get(i64, PtrInt); |
| 3173 | } |
| 3174 | |
| 3175 | static void EmitGlobalDeclMetadata(CodeGenModule &CGM, |
| 3176 | llvm::NamedMDNode *&GlobalMetadata, |
| 3177 | GlobalDecl D, |
| 3178 | llvm::GlobalValue *Addr) { |
| 3179 | if (!GlobalMetadata) |
| 3180 | GlobalMetadata = |
| 3181 | CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); |
| 3182 | |
| 3183 | // TODO: should we report variant information for ctors/dtors? |
| 3184 | llvm::Value *Ops[] = { |
| 3185 | Addr, |
| 3186 | GetPointerConstant(CGM.getLLVMContext(), D.getDecl()) |
| 3187 | }; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 3188 | GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3191 | /// For each function which is declared within an extern "C" region and marked |
| 3192 | /// as 'used', but has internal linkage, create an alias from the unmangled |
| 3193 | /// name to the mangled name if possible. People expect to be able to refer |
| 3194 | /// to such functions with an unmangled name from inline assembly within the |
| 3195 | /// same translation unit. |
| 3196 | void CodeGenModule::EmitStaticExternCAliases() { |
Richard Smith | f21c961 | 2013-04-13 01:28:18 +0000 | [diff] [blame] | 3197 | for (StaticExternCMap::iterator I = StaticExternCValues.begin(), |
| 3198 | E = StaticExternCValues.end(); |
| 3199 | I != E; ++I) { |
| 3200 | IdentifierInfo *Name = I->first; |
| 3201 | llvm::GlobalValue *Val = I->second; |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 3202 | if (Val && !getModule().getNamedValue(Name->getName())) |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 3203 | addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), |
| 3204 | cast<llvm::GlobalObject>(Val))); |
Richard Smith | 85465e6 | 2013-04-06 07:07:44 +0000 | [diff] [blame] | 3205 | } |
Richard Smith | df931ce | 2013-04-06 05:00:46 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3208 | /// Emits metadata nodes associating all the global values in the |
| 3209 | /// current module with the Decls they came from. This is useful for |
| 3210 | /// projects using IR gen as a subroutine. |
| 3211 | /// |
| 3212 | /// Since there's currently no way to associate an MDNode directly |
| 3213 | /// with an llvm::GlobalValue, we create a global named metadata |
| 3214 | /// with the name 'clang.global.decl.ptrs'. |
| 3215 | void CodeGenModule::EmitDeclMetadata() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3216 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3217 | |
| 3218 | // StaticLocalDeclMap |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3219 | for (llvm::DenseMap<GlobalDecl,StringRef>::iterator |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3220 | I = MangledDeclNames.begin(), E = MangledDeclNames.end(); |
| 3221 | I != E; ++I) { |
| 3222 | llvm::GlobalValue *Addr = getModule().getNamedValue(I->second); |
| 3223 | EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | /// Emits metadata nodes for all the local variables in the current |
| 3228 | /// function. |
| 3229 | void CodeGenFunction::EmitDeclMetadata() { |
| 3230 | if (LocalDeclMap.empty()) return; |
| 3231 | |
| 3232 | llvm::LLVMContext &Context = getLLVMContext(); |
| 3233 | |
| 3234 | // Find the unique metadata ID for this name. |
| 3235 | unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); |
| 3236 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3237 | llvm::NamedMDNode *GlobalMetadata = nullptr; |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3238 | |
| 3239 | for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator |
| 3240 | I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) { |
| 3241 | const Decl *D = I->first; |
| 3242 | llvm::Value *Addr = I->second; |
| 3243 | |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3244 | if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3245 | llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 3246 | Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr)); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 3247 | } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { |
John McCall | 09ae032 | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 3248 | GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); |
| 3249 | EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
Daniel Dunbar | 900546d | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 3253 | |
Rafael Espindola | 3bfa468 | 2013-10-16 19:28:50 +0000 | [diff] [blame] | 3254 | void CodeGenModule::EmitVersionIdentMetadata() { |
| 3255 | llvm::NamedMDNode *IdentMetadata = |
| 3256 | TheModule.getOrInsertNamedMetadata("llvm.ident"); |
| 3257 | std::string Version = getClangFullVersion(); |
| 3258 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
| 3259 | |
| 3260 | llvm::Value *IdentNode[] = { |
| 3261 | llvm::MDString::get(Ctx, Version) |
| 3262 | }; |
| 3263 | IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); |
| 3264 | } |
| 3265 | |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 3266 | void CodeGenModule::EmitCoverageFile() { |
| 3267 | if (!getCodeGenOpts().CoverageFile.empty()) { |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 3268 | if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) { |
| 3269 | llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); |
| 3270 | llvm::LLVMContext &Ctx = TheModule.getContext(); |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 3271 | llvm::MDString *CoverageFile = |
| 3272 | llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile); |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 3273 | for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { |
| 3274 | llvm::MDNode *CU = CUNode->getOperand(i); |
Nick Lewycky | 85c011d | 2011-05-05 00:08:20 +0000 | [diff] [blame] | 3275 | llvm::Value *node[] = { CoverageFile, CU }; |
Nick Lewycky | 480cb99 | 2011-05-04 20:46:58 +0000 | [diff] [blame] | 3276 | llvm::MDNode *N = llvm::MDNode::get(Ctx, node); |
| 3277 | GCov->addOperand(N); |
| 3278 | } |
| 3279 | } |
| 3280 | } |
| 3281 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3282 | |
| 3283 | llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid, |
| 3284 | QualType GuidType) { |
| 3285 | // Sema has checked that all uuid strings are of the form |
| 3286 | // "12345678-1234-1234-1234-1234567890ab". |
| 3287 | assert(Uuid.size() == 36); |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 3288 | for (unsigned i = 0; i < 36; ++i) { |
| 3289 | if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-'); |
| 3290 | else assert(isHexDigit(Uuid[i])); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3291 | } |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3292 | |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 3293 | const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 }; |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3294 | |
David Majnemer | bbecd09 | 2013-08-15 19:59:14 +0000 | [diff] [blame] | 3295 | llvm::Constant *Field3[8]; |
| 3296 | for (unsigned Idx = 0; Idx < 8; ++Idx) |
| 3297 | Field3[Idx] = llvm::ConstantInt::get( |
| 3298 | Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16); |
| 3299 | |
| 3300 | llvm::Constant *Fields[4] = { |
| 3301 | llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16), |
| 3302 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16), |
| 3303 | llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16), |
| 3304 | llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3) |
| 3305 | }; |
| 3306 | |
| 3307 | return llvm::ConstantStruct::getAnon(Fields); |
Nico Weber | cf4ff586 | 2012-10-11 10:13:44 +0000 | [diff] [blame] | 3308 | } |