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