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