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