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