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