CodeGenModule.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is the internal per-translation-unit state used for llvm translation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
  15. #include "CGVTables.h"
  16. #include "CodeGenTypes.h"
  17. #include "SanitizerMetadata.h"
  18. #include "clang/AST/Attr.h"
  19. #include "clang/AST/DeclCXX.h"
  20. #include "clang/AST/DeclObjC.h"
  21. #include "clang/AST/GlobalDecl.h"
  22. #include "clang/AST/Mangle.h"
  23. #include "clang/Basic/ABI.h"
  24. #include "clang/Basic/LangOptions.h"
  25. #include "clang/Basic/Module.h"
  26. #include "clang/Basic/SanitizerBlacklist.h"
  27. #include "llvm/ADT/DenseMap.h"
  28. #include "llvm/ADT/SetVector.h"
  29. #include "llvm/ADT/SmallPtrSet.h"
  30. #include "llvm/ADT/StringMap.h"
  31. #include "llvm/IR/CallingConv.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/IR/ValueHandle.h"
  34. namespace llvm {
  35. class Module;
  36. class Constant;
  37. class ConstantInt;
  38. class Function;
  39. class GlobalValue;
  40. class DataLayout;
  41. class FunctionType;
  42. class LLVMContext;
  43. class IndexedInstrProfReader;
  44. }
  45. namespace clang {
  46. class TargetCodeGenInfo;
  47. class ASTContext;
  48. class AtomicType;
  49. class FunctionDecl;
  50. class IdentifierInfo;
  51. class ObjCMethodDecl;
  52. class ObjCImplementationDecl;
  53. class ObjCCategoryImplDecl;
  54. class ObjCProtocolDecl;
  55. class ObjCEncodeExpr;
  56. class BlockExpr;
  57. class CharUnits;
  58. class Decl;
  59. class Expr;
  60. class Stmt;
  61. class InitListExpr;
  62. class StringLiteral;
  63. class NamedDecl;
  64. class ValueDecl;
  65. class VarDecl;
  66. class LangOptions;
  67. class CodeGenOptions;
  68. class DiagnosticsEngine;
  69. class AnnotateAttr;
  70. class CXXDestructorDecl;
  71. class Module;
  72. class CoverageSourceInfo;
  73. namespace CodeGen {
  74. class CallArgList;
  75. class CodeGenFunction;
  76. class CodeGenTBAA;
  77. class CGCXXABI;
  78. class CGDebugInfo;
  79. class CGObjCRuntime;
  80. class CGOpenCLRuntime;
  81. class CGOpenMPRuntime;
  82. class CGCUDARuntime;
  83. class BlockFieldFlags;
  84. class FunctionArgList;
  85. class CoverageMappingModuleGen;
  86. struct OrderGlobalInits {
  87. unsigned int priority;
  88. unsigned int lex_order;
  89. OrderGlobalInits(unsigned int p, unsigned int l)
  90. : priority(p), lex_order(l) {}
  91. bool operator==(const OrderGlobalInits &RHS) const {
  92. return priority == RHS.priority && lex_order == RHS.lex_order;
  93. }
  94. bool operator<(const OrderGlobalInits &RHS) const {
  95. return std::tie(priority, lex_order) <
  96. std::tie(RHS.priority, RHS.lex_order);
  97. }
  98. };
  99. struct CodeGenTypeCache {
  100. /// void
  101. llvm::Type *VoidTy;
  102. /// i8, i16, i32, and i64
  103. llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
  104. /// float, double
  105. llvm::Type *FloatTy, *DoubleTy;
  106. /// int
  107. llvm::IntegerType *IntTy;
  108. /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
  109. union {
  110. llvm::IntegerType *IntPtrTy;
  111. llvm::IntegerType *SizeTy;
  112. llvm::IntegerType *PtrDiffTy;
  113. };
  114. /// void* in address space 0
  115. union {
  116. llvm::PointerType *VoidPtrTy;
  117. llvm::PointerType *Int8PtrTy;
  118. };
  119. /// void** in address space 0
  120. union {
  121. llvm::PointerType *VoidPtrPtrTy;
  122. llvm::PointerType *Int8PtrPtrTy;
  123. };
  124. /// The width of a pointer into the generic address space.
  125. unsigned char PointerWidthInBits;
  126. /// The size and alignment of a pointer into the generic address
  127. /// space.
  128. union {
  129. unsigned char PointerAlignInBytes;
  130. unsigned char PointerSizeInBytes;
  131. unsigned char SizeSizeInBytes; // sizeof(size_t)
  132. };
  133. llvm::CallingConv::ID RuntimeCC;
  134. llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
  135. };
  136. struct RREntrypoints {
  137. RREntrypoints() { memset(this, 0, sizeof(*this)); }
  138. /// void objc_autoreleasePoolPop(void*);
  139. llvm::Constant *objc_autoreleasePoolPop;
  140. /// void *objc_autoreleasePoolPush(void);
  141. llvm::Constant *objc_autoreleasePoolPush;
  142. };
  143. struct ARCEntrypoints {
  144. ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
  145. /// id objc_autorelease(id);
  146. llvm::Constant *objc_autorelease;
  147. /// id objc_autoreleaseReturnValue(id);
  148. llvm::Constant *objc_autoreleaseReturnValue;
  149. /// void objc_copyWeak(id *dest, id *src);
  150. llvm::Constant *objc_copyWeak;
  151. /// void objc_destroyWeak(id*);
  152. llvm::Constant *objc_destroyWeak;
  153. /// id objc_initWeak(id*, id);
  154. llvm::Constant *objc_initWeak;
  155. /// id objc_loadWeak(id*);
  156. llvm::Constant *objc_loadWeak;
  157. /// id objc_loadWeakRetained(id*);
  158. llvm::Constant *objc_loadWeakRetained;
  159. /// void objc_moveWeak(id *dest, id *src);
  160. llvm::Constant *objc_moveWeak;
  161. /// id objc_retain(id);
  162. llvm::Constant *objc_retain;
  163. /// id objc_retainAutorelease(id);
  164. llvm::Constant *objc_retainAutorelease;
  165. /// id objc_retainAutoreleaseReturnValue(id);
  166. llvm::Constant *objc_retainAutoreleaseReturnValue;
  167. /// id objc_retainAutoreleasedReturnValue(id);
  168. llvm::Constant *objc_retainAutoreleasedReturnValue;
  169. /// id objc_retainBlock(id);
  170. llvm::Constant *objc_retainBlock;
  171. /// void objc_release(id);
  172. llvm::Constant *objc_release;
  173. /// id objc_storeStrong(id*, id);
  174. llvm::Constant *objc_storeStrong;
  175. /// id objc_storeWeak(id*, id);
  176. llvm::Constant *objc_storeWeak;
  177. /// A void(void) inline asm to use to mark that the return value of
  178. /// a call will be immediately retain.
  179. llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
  180. /// void clang.arc.use(...);
  181. llvm::Constant *clang_arc_use;
  182. };
  183. /// This class records statistics on instrumentation based profiling.
  184. class InstrProfStats {
  185. uint32_t VisitedInMainFile;
  186. uint32_t MissingInMainFile;
  187. uint32_t Visited;
  188. uint32_t Missing;
  189. uint32_t Mismatched;
  190. public:
  191. InstrProfStats()
  192. : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
  193. Mismatched(0) {}
  194. /// Record that we've visited a function and whether or not that function was
  195. /// in the main source file.
  196. void addVisited(bool MainFile) {
  197. if (MainFile)
  198. ++VisitedInMainFile;
  199. ++Visited;
  200. }
  201. /// Record that a function we've visited has no profile data.
  202. void addMissing(bool MainFile) {
  203. if (MainFile)
  204. ++MissingInMainFile;
  205. ++Missing;
  206. }
  207. /// Record that a function we've visited has mismatched profile data.
  208. void addMismatched(bool MainFile) { ++Mismatched; }
  209. /// Whether or not the stats we've gathered indicate any potential problems.
  210. bool hasDiagnostics() { return Missing || Mismatched; }
  211. /// Report potential problems we've found to \c Diags.
  212. void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
  213. };
  214. /// This class organizes the cross-function state that is used while generating
  215. /// LLVM code.
  216. class CodeGenModule : public CodeGenTypeCache {
  217. CodeGenModule(const CodeGenModule &) LLVM_DELETED_FUNCTION;
  218. void operator=(const CodeGenModule &) LLVM_DELETED_FUNCTION;
  219. struct Structor {
  220. Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
  221. Structor(int Priority, llvm::Constant *Initializer,
  222. llvm::Constant *AssociatedData)
  223. : Priority(Priority), Initializer(Initializer),
  224. AssociatedData(AssociatedData) {}
  225. int Priority;
  226. llvm::Constant *Initializer;
  227. llvm::Constant *AssociatedData;
  228. };
  229. typedef std::vector<Structor> CtorList;
  230. ASTContext &Context;
  231. const LangOptions &LangOpts;
  232. const CodeGenOptions &CodeGenOpts;
  233. llvm::Module &TheModule;
  234. DiagnosticsEngine &Diags;
  235. const llvm::DataLayout &TheDataLayout;
  236. const TargetInfo &Target;
  237. std::unique_ptr<CGCXXABI> ABI;
  238. llvm::LLVMContext &VMContext;
  239. CodeGenTBAA *TBAA;
  240. mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
  241. // This should not be moved earlier, since its initialization depends on some
  242. // of the previous reference members being already initialized and also checks
  243. // if TheTargetCodeGenInfo is NULL
  244. CodeGenTypes Types;
  245. /// Holds information about C++ vtables.
  246. CodeGenVTables VTables;
  247. CGObjCRuntime* ObjCRuntime;
  248. CGOpenCLRuntime* OpenCLRuntime;
  249. CGOpenMPRuntime* OpenMPRuntime;
  250. CGCUDARuntime* CUDARuntime;
  251. CGDebugInfo* DebugInfo;
  252. ARCEntrypoints *ARCData;
  253. llvm::MDNode *NoObjCARCExceptionsMetadata;
  254. RREntrypoints *RRData;
  255. std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
  256. InstrProfStats PGOStats;
  257. // A set of references that have only been seen via a weakref so far. This is
  258. // used to remove the weak of the reference if we ever see a direct reference
  259. // or a definition.
  260. llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
  261. /// This contains all the decls which have definitions but/ which are deferred
  262. /// for emission and therefore should only be output if they are actually
  263. /// used. If a decl is in this, then it is known to have not been referenced
  264. /// yet.
  265. std::map<StringRef, GlobalDecl> DeferredDecls;
  266. /// This is a list of deferred decls which we have seen that *are* actually
  267. /// referenced. These get code generated when the module is done.
  268. struct DeferredGlobal {
  269. DeferredGlobal(llvm::GlobalValue *GV, GlobalDecl GD) : GV(GV), GD(GD) {}
  270. llvm::AssertingVH<llvm::GlobalValue> GV;
  271. GlobalDecl GD;
  272. };
  273. std::vector<DeferredGlobal> DeferredDeclsToEmit;
  274. void addDeferredDeclToEmit(llvm::GlobalValue *GV, GlobalDecl GD) {
  275. DeferredDeclsToEmit.push_back(DeferredGlobal(GV, GD));
  276. }
  277. /// List of alias we have emitted. Used to make sure that what they point to
  278. /// is defined once we get to the end of the of the translation unit.
  279. std::vector<GlobalDecl> Aliases;
  280. typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
  281. ReplacementsTy Replacements;
  282. /// A queue of (optional) vtables to consider emitting.
  283. std::vector<const CXXRecordDecl*> DeferredVTables;
  284. /// List of global values which are required to be present in the object file;
  285. /// bitcast to i8*. This is used for forcing visibility of symbols which may
  286. /// otherwise be optimized out.
  287. std::vector<llvm::WeakVH> LLVMUsed;
  288. std::vector<llvm::WeakVH> LLVMCompilerUsed;
  289. /// Store the list of global constructors and their respective priorities to
  290. /// be emitted when the translation unit is complete.
  291. CtorList GlobalCtors;
  292. /// Store the list of global destructors and their respective priorities to be
  293. /// emitted when the translation unit is complete.
  294. CtorList GlobalDtors;
  295. /// An ordered map of canonical GlobalDecls to their mangled names.
  296. llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
  297. llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
  298. /// Global annotations.
  299. std::vector<llvm::Constant*> Annotations;
  300. /// Map used to get unique annotation strings.
  301. llvm::StringMap<llvm::Constant*> AnnotationStrings;
  302. llvm::StringMap<llvm::Constant*> CFConstantStringMap;
  303. llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
  304. llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
  305. llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
  306. llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
  307. llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
  308. llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
  309. /// Map used to get unique type descriptor constants for sanitizers.
  310. llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
  311. /// Map used to track internal linkage functions declared within
  312. /// extern "C" regions.
  313. typedef llvm::MapVector<IdentifierInfo *,
  314. llvm::GlobalValue *> StaticExternCMap;
  315. StaticExternCMap StaticExternCValues;
  316. /// \brief thread_local variables defined or used in this TU.
  317. std::vector<std::pair<const VarDecl *, llvm::GlobalVariable *> >
  318. CXXThreadLocals;
  319. /// \brief thread_local variables with initializers that need to run
  320. /// before any thread_local variable in this TU is odr-used.
  321. std::vector<llvm::Function *> CXXThreadLocalInits;
  322. std::vector<llvm::GlobalVariable *> CXXThreadLocalInitVars;
  323. /// Global variables with initializers that need to run before main.
  324. std::vector<llvm::Function *> CXXGlobalInits;
  325. /// When a C++ decl with an initializer is deferred, null is
  326. /// appended to CXXGlobalInits, and the index of that null is placed
  327. /// here so that the initializer will be performed in the correct
  328. /// order.
  329. llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
  330. typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
  331. struct GlobalInitPriorityCmp {
  332. bool operator()(const GlobalInitData &LHS,
  333. const GlobalInitData &RHS) const {
  334. return LHS.first.priority < RHS.first.priority;
  335. }
  336. };
  337. /// Global variables with initializers whose order of initialization is set by
  338. /// init_priority attribute.
  339. SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
  340. /// Global destructor functions and arguments that need to run on termination.
  341. std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
  342. /// \brief The complete set of modules that has been imported.
  343. llvm::SetVector<clang::Module *> ImportedModules;
  344. /// \brief A vector of metadata strings.
  345. SmallVector<llvm::Value *, 16> LinkerOptionsMetadata;
  346. /// @name Cache for Objective-C runtime types
  347. /// @{
  348. /// Cached reference to the class for constant strings. This value has type
  349. /// int * but is actually an Obj-C class pointer.
  350. llvm::WeakVH CFConstantStringClassRef;
  351. /// Cached reference to the class for constant strings. This value has type
  352. /// int * but is actually an Obj-C class pointer.
  353. llvm::WeakVH ConstantStringClassRef;
  354. /// \brief The LLVM type corresponding to NSConstantString.
  355. llvm::StructType *NSConstantStringType;
  356. /// \brief The type used to describe the state of a fast enumeration in
  357. /// Objective-C's for..in loop.
  358. QualType ObjCFastEnumerationStateType;
  359. /// @}
  360. /// Lazily create the Objective-C runtime
  361. void createObjCRuntime();
  362. void createOpenCLRuntime();
  363. void createOpenMPRuntime();
  364. void createCUDARuntime();
  365. bool isTriviallyRecursive(const FunctionDecl *F);
  366. bool shouldEmitFunction(GlobalDecl GD);
  367. /// @name Cache for Blocks Runtime Globals
  368. /// @{
  369. llvm::Constant *NSConcreteGlobalBlock;
  370. llvm::Constant *NSConcreteStackBlock;
  371. llvm::Constant *BlockObjectAssign;
  372. llvm::Constant *BlockObjectDispose;
  373. llvm::Type *BlockDescriptorType;
  374. llvm::Type *GenericBlockLiteralType;
  375. struct {
  376. int GlobalUniqueCount;
  377. } Block;
  378. /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
  379. llvm::Constant *LifetimeStartFn;
  380. /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
  381. llvm::Constant *LifetimeEndFn;
  382. GlobalDecl initializedGlobalDecl;
  383. std::unique_ptr<SanitizerMetadata> SanitizerMD;
  384. /// @}
  385. llvm::DenseMap<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
  386. std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
  387. public:
  388. CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
  389. llvm::Module &M, const llvm::DataLayout &TD,
  390. DiagnosticsEngine &Diags,
  391. CoverageSourceInfo *CoverageInfo = nullptr);
  392. ~CodeGenModule();
  393. void clear();
  394. /// Finalize LLVM code generation.
  395. void Release();
  396. /// Return a reference to the configured Objective-C runtime.
  397. CGObjCRuntime &getObjCRuntime() {
  398. if (!ObjCRuntime) createObjCRuntime();
  399. return *ObjCRuntime;
  400. }
  401. /// Return true iff an Objective-C runtime has been configured.
  402. bool hasObjCRuntime() { return !!ObjCRuntime; }
  403. /// Return a reference to the configured OpenCL runtime.
  404. CGOpenCLRuntime &getOpenCLRuntime() {
  405. assert(OpenCLRuntime != nullptr);
  406. return *OpenCLRuntime;
  407. }
  408. /// Return a reference to the configured OpenMP runtime.
  409. CGOpenMPRuntime &getOpenMPRuntime() {
  410. assert(OpenMPRuntime != nullptr);
  411. return *OpenMPRuntime;
  412. }
  413. /// Return a reference to the configured CUDA runtime.
  414. CGCUDARuntime &getCUDARuntime() {
  415. assert(CUDARuntime != nullptr);
  416. return *CUDARuntime;
  417. }
  418. ARCEntrypoints &getARCEntrypoints() const {
  419. assert(getLangOpts().ObjCAutoRefCount && ARCData != nullptr);
  420. return *ARCData;
  421. }
  422. RREntrypoints &getRREntrypoints() const {
  423. assert(RRData != nullptr);
  424. return *RRData;
  425. }
  426. InstrProfStats &getPGOStats() { return PGOStats; }
  427. llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
  428. CoverageMappingModuleGen *getCoverageMapping() const {
  429. return CoverageMapping.get();
  430. }
  431. llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
  432. return StaticLocalDeclMap[D];
  433. }
  434. void setStaticLocalDeclAddress(const VarDecl *D,
  435. llvm::Constant *C) {
  436. StaticLocalDeclMap[D] = C;
  437. }
  438. llvm::Constant *
  439. getOrCreateStaticVarDecl(const VarDecl &D,
  440. llvm::GlobalValue::LinkageTypes Linkage);
  441. llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
  442. return StaticLocalDeclGuardMap[D];
  443. }
  444. void setStaticLocalDeclGuardAddress(const VarDecl *D,
  445. llvm::GlobalVariable *C) {
  446. StaticLocalDeclGuardMap[D] = C;
  447. }
  448. bool lookupRepresentativeDecl(StringRef MangledName,
  449. GlobalDecl &Result) const;
  450. llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
  451. return AtomicSetterHelperFnMap[Ty];
  452. }
  453. void setAtomicSetterHelperFnMap(QualType Ty,
  454. llvm::Constant *Fn) {
  455. AtomicSetterHelperFnMap[Ty] = Fn;
  456. }
  457. llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
  458. return AtomicGetterHelperFnMap[Ty];
  459. }
  460. void setAtomicGetterHelperFnMap(QualType Ty,
  461. llvm::Constant *Fn) {
  462. AtomicGetterHelperFnMap[Ty] = Fn;
  463. }
  464. llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
  465. return TypeDescriptorMap[Ty];
  466. }
  467. void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
  468. TypeDescriptorMap[Ty] = C;
  469. }
  470. CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
  471. llvm::MDNode *getNoObjCARCExceptionsMetadata() {
  472. if (!NoObjCARCExceptionsMetadata)
  473. NoObjCARCExceptionsMetadata =
  474. llvm::MDNode::get(getLLVMContext(),
  475. SmallVector<llvm::Value*,1>());
  476. return NoObjCARCExceptionsMetadata;
  477. }
  478. ASTContext &getContext() const { return Context; }
  479. const LangOptions &getLangOpts() const { return LangOpts; }
  480. const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
  481. llvm::Module &getModule() const { return TheModule; }
  482. DiagnosticsEngine &getDiags() const { return Diags; }
  483. const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
  484. const TargetInfo &getTarget() const { return Target; }
  485. const llvm::Triple &getTriple() const;
  486. bool supportsCOMDAT() const;
  487. CGCXXABI &getCXXABI() const { return *ABI; }
  488. llvm::LLVMContext &getLLVMContext() { return VMContext; }
  489. bool shouldUseTBAA() const { return TBAA != nullptr; }
  490. const TargetCodeGenInfo &getTargetCodeGenInfo();
  491. CodeGenTypes &getTypes() { return Types; }
  492. CodeGenVTables &getVTables() { return VTables; }
  493. ItaniumVTableContext &getItaniumVTableContext() {
  494. return VTables.getItaniumVTableContext();
  495. }
  496. MicrosoftVTableContext &getMicrosoftVTableContext() {
  497. return VTables.getMicrosoftVTableContext();
  498. }
  499. llvm::MDNode *getTBAAInfo(QualType QTy);
  500. llvm::MDNode *getTBAAInfoForVTablePtr();
  501. llvm::MDNode *getTBAAStructInfo(QualType QTy);
  502. /// Return the MDNode in the type DAG for the given struct type.
  503. llvm::MDNode *getTBAAStructTypeInfo(QualType QTy);
  504. /// Return the path-aware tag for given base type, access node and offset.
  505. llvm::MDNode *getTBAAStructTagInfo(QualType BaseTy, llvm::MDNode *AccessN,
  506. uint64_t O);
  507. bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
  508. bool isPaddedAtomicType(QualType type);
  509. bool isPaddedAtomicType(const AtomicType *type);
  510. /// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
  511. /// is the same as the type. For struct-path aware TBAA, the tag
  512. /// is different from the type: base type, access type and offset.
  513. /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
  514. void DecorateInstruction(llvm::Instruction *Inst,
  515. llvm::MDNode *TBAAInfo,
  516. bool ConvertTypeToTag = true);
  517. /// Emit the given number of characters as a value of type size_t.
  518. llvm::ConstantInt *getSize(CharUnits numChars);
  519. /// Set the visibility for the given LLVM GlobalValue.
  520. void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
  521. /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
  522. /// variable declaration D.
  523. void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
  524. static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
  525. switch (V) {
  526. case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
  527. case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
  528. case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
  529. }
  530. llvm_unreachable("unknown visibility!");
  531. }
  532. llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
  533. if (isa<CXXConstructorDecl>(GD.getDecl()))
  534. return getAddrOfCXXStructor(cast<CXXConstructorDecl>(GD.getDecl()),
  535. getFromCtorType(GD.getCtorType()));
  536. else if (isa<CXXDestructorDecl>(GD.getDecl()))
  537. return getAddrOfCXXStructor(cast<CXXDestructorDecl>(GD.getDecl()),
  538. getFromDtorType(GD.getDtorType()));
  539. else if (isa<FunctionDecl>(GD.getDecl()))
  540. return GetAddrOfFunction(GD);
  541. else
  542. return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
  543. }
  544. /// Will return a global variable of the given type. If a variable with a
  545. /// different type already exists then a new variable with the right type
  546. /// will be created and all uses of the old variable will be replaced with a
  547. /// bitcast to the new variable.
  548. llvm::GlobalVariable *
  549. CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
  550. llvm::GlobalValue::LinkageTypes Linkage);
  551. llvm::Function *
  552. CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name,
  553. SourceLocation Loc = SourceLocation(),
  554. bool TLS = false);
  555. /// Return the address space of the underlying global variable for D, as
  556. /// determined by its declaration. Normally this is the same as the address
  557. /// space of D's type, but in CUDA, address spaces are associated with
  558. /// declarations, not types.
  559. unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace);
  560. /// Return the llvm::Constant for the address of the given global variable.
  561. /// If Ty is non-null and if the global doesn't exist, then it will be greated
  562. /// with the specified type instead of whatever the normal requested type
  563. /// would be.
  564. llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
  565. llvm::Type *Ty = nullptr);
  566. /// Return the address of the given function. If Ty is non-null, then this
  567. /// function will use the specified type if it has to create it.
  568. llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = 0,
  569. bool ForVTable = false,
  570. bool DontDefer = false);
  571. /// Get the address of the RTTI descriptor for the given type.
  572. llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
  573. /// Get the address of a uuid descriptor .
  574. llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
  575. /// Get the address of the thunk for the given global decl.
  576. llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
  577. /// Get a reference to the target of VD.
  578. llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
  579. /// Returns the offset from a derived class to a class. Returns null if the
  580. /// offset is 0.
  581. llvm::Constant *
  582. GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
  583. CastExpr::path_const_iterator PathBegin,
  584. CastExpr::path_const_iterator PathEnd);
  585. /// A pair of helper functions for a __block variable.
  586. class ByrefHelpers : public llvm::FoldingSetNode {
  587. public:
  588. llvm::Constant *CopyHelper;
  589. llvm::Constant *DisposeHelper;
  590. /// The alignment of the field. This is important because
  591. /// different offsets to the field within the byref struct need to
  592. /// have different helper functions.
  593. CharUnits Alignment;
  594. ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
  595. virtual ~ByrefHelpers();
  596. void Profile(llvm::FoldingSetNodeID &id) const {
  597. id.AddInteger(Alignment.getQuantity());
  598. profileImpl(id);
  599. }
  600. virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
  601. virtual bool needsCopy() const { return true; }
  602. virtual void emitCopy(CodeGenFunction &CGF,
  603. llvm::Value *dest, llvm::Value *src) = 0;
  604. virtual bool needsDispose() const { return true; }
  605. virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
  606. };
  607. llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
  608. /// Fetches the global unique block count.
  609. int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
  610. /// Fetches the type of a generic block descriptor.
  611. llvm::Type *getBlockDescriptorType();
  612. /// The type of a generic block literal.
  613. llvm::Type *getGenericBlockLiteralType();
  614. /// Gets the address of a block which requires no captures.
  615. llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
  616. /// Return a pointer to a constant CFString object for the given string.
  617. llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
  618. /// Return a pointer to a constant NSString object for the given string. Or a
  619. /// user defined String object as defined via
  620. /// -fconstant-string-class=class_name option.
  621. llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
  622. /// Return a constant array for the given string.
  623. llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
  624. /// Return a pointer to a constant array for the given string literal.
  625. llvm::GlobalVariable *
  626. GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
  627. StringRef Name = ".str");
  628. /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
  629. llvm::GlobalVariable *
  630. GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
  631. /// Returns a pointer to a character array containing the literal and a
  632. /// terminating '\0' character. The result has pointer to array type.
  633. ///
  634. /// \param GlobalName If provided, the name to use for the global (if one is
  635. /// created).
  636. llvm::GlobalVariable *
  637. GetAddrOfConstantCString(const std::string &Str,
  638. const char *GlobalName = nullptr,
  639. unsigned Alignment = 0);
  640. /// Returns a pointer to a constant global variable for the given file-scope
  641. /// compound literal expression.
  642. llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
  643. /// \brief Returns a pointer to a global variable representing a temporary
  644. /// with static or thread storage duration.
  645. llvm::Constant *GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
  646. const Expr *Inner);
  647. /// \brief Retrieve the record type that describes the state of an
  648. /// Objective-C fast enumeration loop (for..in).
  649. QualType getObjCFastEnumerationStateType();
  650. // Produce code for this constructor/destructor. This method doesn't try
  651. // to apply any ABI rules about which other constructors/destructors
  652. // are needed or if they are alias to each other.
  653. llvm::Function *codegenCXXStructor(const CXXMethodDecl *MD,
  654. StructorType Type);
  655. /// Return the address of the constructor/destructor of the given type.
  656. llvm::GlobalValue *
  657. getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type,
  658. const CGFunctionInfo *FnInfo = nullptr,
  659. llvm::FunctionType *FnType = nullptr,
  660. bool DontDefer = false);
  661. /// Given a builtin id for a function like "__builtin_fabsf", return a
  662. /// Function* for "fabsf".
  663. llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
  664. unsigned BuiltinID);
  665. llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
  666. /// Emit code for a single top level declaration.
  667. void EmitTopLevelDecl(Decl *D);
  668. /// \brief Stored a deferred empty coverage mapping for an unused
  669. /// and thus uninstrumented top level declaration.
  670. void AddDeferredUnusedCoverageMapping(Decl *D);
  671. /// \brief Remove the deferred empty coverage mapping as this
  672. /// declaration is actually instrumented.
  673. void ClearUnusedCoverageMapping(const Decl *D);
  674. /// \brief Emit all the deferred coverage mappings
  675. /// for the uninstrumented functions.
  676. void EmitDeferredUnusedCoverageMappings();
  677. /// Tell the consumer that this variable has been instantiated.
  678. void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
  679. /// \brief If the declaration has internal linkage but is inside an
  680. /// extern "C" linkage specification, prepare to emit an alias for it
  681. /// to the expected name.
  682. template<typename SomeDecl>
  683. void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
  684. /// Add a global to a list to be added to the llvm.used metadata.
  685. void addUsedGlobal(llvm::GlobalValue *GV);
  686. /// Add a global to a list to be added to the llvm.compiler.used metadata.
  687. void addCompilerUsedGlobal(llvm::GlobalValue *GV);
  688. /// Add a destructor and object to add to the C++ global destructor function.
  689. void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
  690. CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
  691. }
  692. /// Create a new runtime function with the specified type and name.
  693. llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
  694. StringRef Name,
  695. llvm::AttributeSet ExtraAttrs =
  696. llvm::AttributeSet());
  697. /// Create a new runtime global variable with the specified type and name.
  698. llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
  699. StringRef Name);
  700. ///@name Custom Blocks Runtime Interfaces
  701. ///@{
  702. llvm::Constant *getNSConcreteGlobalBlock();
  703. llvm::Constant *getNSConcreteStackBlock();
  704. llvm::Constant *getBlockObjectAssign();
  705. llvm::Constant *getBlockObjectDispose();
  706. ///@}
  707. llvm::Constant *getLLVMLifetimeStartFn();
  708. llvm::Constant *getLLVMLifetimeEndFn();
  709. // Make sure that this type is translated.
  710. void UpdateCompletedType(const TagDecl *TD);
  711. llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
  712. /// Try to emit the initializer for the given declaration as a constant;
  713. /// returns 0 if the expression cannot be emitted as a constant.
  714. llvm::Constant *EmitConstantInit(const VarDecl &D,
  715. CodeGenFunction *CGF = nullptr);
  716. /// Try to emit the given expression as a constant; returns 0 if the
  717. /// expression cannot be emitted as a constant.
  718. llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
  719. CodeGenFunction *CGF = nullptr);
  720. /// Emit the given constant value as a constant, in the type's scalar
  721. /// representation.
  722. llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
  723. CodeGenFunction *CGF = nullptr);
  724. /// Emit the given constant value as a constant, in the type's memory
  725. /// representation.
  726. llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
  727. QualType DestType,
  728. CodeGenFunction *CGF = nullptr);
  729. /// Return the result of value-initializing the given type, i.e. a null
  730. /// expression of the given type. This is usually, but not always, an LLVM
  731. /// null constant.
  732. llvm::Constant *EmitNullConstant(QualType T);
  733. /// Return a null constant appropriate for zero-initializing a base class with
  734. /// the given type. This is usually, but not always, an LLVM null constant.
  735. llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
  736. /// Emit a general error that something can't be done.
  737. void Error(SourceLocation loc, StringRef error);
  738. /// Print out an error that codegen doesn't support the specified stmt yet.
  739. void ErrorUnsupported(const Stmt *S, const char *Type);
  740. /// Print out an error that codegen doesn't support the specified decl yet.
  741. void ErrorUnsupported(const Decl *D, const char *Type);
  742. /// Set the attributes on the LLVM function for the given decl and function
  743. /// info. This applies attributes necessary for handling the ABI as well as
  744. /// user specified attributes like section.
  745. void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
  746. const CGFunctionInfo &FI);
  747. /// Set the LLVM function attributes (sext, zext, etc).
  748. void SetLLVMFunctionAttributes(const Decl *D,
  749. const CGFunctionInfo &Info,
  750. llvm::Function *F);
  751. /// Set the LLVM function attributes which only apply to a function
  752. /// definition.
  753. void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
  754. /// Return true iff the given type uses 'sret' when used as a return type.
  755. bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
  756. /// Return true iff the given type uses an argument slot when 'sret' is used
  757. /// as a return type.
  758. bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
  759. /// Return true iff the given type uses 'fpret' when used as a return type.
  760. bool ReturnTypeUsesFPRet(QualType ResultType);
  761. /// Return true iff the given type uses 'fp2ret' when used as a return type.
  762. bool ReturnTypeUsesFP2Ret(QualType ResultType);
  763. /// Get the LLVM attributes and calling convention to use for a particular
  764. /// function type.
  765. ///
  766. /// \param Info - The function type information.
  767. /// \param TargetDecl - The decl these attributes are being constructed
  768. /// for. If supplied the attributes applied to this decl may contribute to the
  769. /// function attributes and calling convention.
  770. /// \param PAL [out] - On return, the attribute list to use.
  771. /// \param CallingConv [out] - On return, the LLVM calling convention to use.
  772. void ConstructAttributeList(const CGFunctionInfo &Info,
  773. const Decl *TargetDecl,
  774. AttributeListType &PAL,
  775. unsigned &CallingConv,
  776. bool AttrOnCallSite);
  777. StringRef getMangledName(GlobalDecl GD);
  778. StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
  779. void EmitTentativeDefinition(const VarDecl *D);
  780. void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
  781. /// Emit the RTTI descriptors for the builtin types.
  782. void EmitFundamentalRTTIDescriptors();
  783. /// \brief Appends Opts to the "Linker Options" metadata value.
  784. void AppendLinkerOptions(StringRef Opts);
  785. /// \brief Appends a detect mismatch command to the linker options.
  786. void AddDetectMismatch(StringRef Name, StringRef Value);
  787. /// \brief Appends a dependent lib to the "Linker Options" metadata value.
  788. void AddDependentLib(StringRef Lib);
  789. llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
  790. void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
  791. F->setLinkage(getFunctionLinkage(GD));
  792. }
  793. /// Return the appropriate linkage for the vtable, VTT, and type information
  794. /// of the given class.
  795. llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
  796. /// Return the store size, in character units, of the given LLVM type.
  797. CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
  798. /// Returns LLVM linkage for a declarator.
  799. llvm::GlobalValue::LinkageTypes
  800. getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
  801. bool IsConstantVariable);
  802. /// Returns LLVM linkage for a declarator.
  803. llvm::GlobalValue::LinkageTypes
  804. getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
  805. /// Emit all the global annotations.
  806. void EmitGlobalAnnotations();
  807. /// Emit an annotation string.
  808. llvm::Constant *EmitAnnotationString(StringRef Str);
  809. /// Emit the annotation's translation unit.
  810. llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
  811. /// Emit the annotation line number.
  812. llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
  813. /// Generate the llvm::ConstantStruct which contains the annotation
  814. /// information for a given GlobalValue. The annotation struct is
  815. /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
  816. /// GlobalValue being annotated. The second field is the constant string
  817. /// created from the AnnotateAttr's annotation. The third field is a constant
  818. /// string containing the name of the translation unit. The fourth field is
  819. /// the line number in the file of the annotated value declaration.
  820. llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
  821. const AnnotateAttr *AA,
  822. SourceLocation L);
  823. /// Add global annotations that are set on D, for the global GV. Those
  824. /// annotations are emitted during finalization of the LLVM code.
  825. void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
  826. bool isInSanitizerBlacklist(llvm::Function *Fn, SourceLocation Loc) const;
  827. SanitizerMetadata *getSanitizerMetadata() {
  828. return SanitizerMD.get();
  829. }
  830. void addDeferredVTable(const CXXRecordDecl *RD) {
  831. DeferredVTables.push_back(RD);
  832. }
  833. /// Emit code for a singal global function or var decl. Forward declarations
  834. /// are emitted lazily.
  835. void EmitGlobal(GlobalDecl D);
  836. bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target,
  837. bool InEveryTU);
  838. bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
  839. /// Set attributes for a global definition.
  840. void setFunctionDefinitionAttributes(const FunctionDecl *D,
  841. llvm::Function *F);
  842. llvm::GlobalValue *GetGlobalValue(StringRef Ref);
  843. /// Set attributes which are common to any form of a global definition (alias,
  844. /// Objective-C method, function, global variable).
  845. ///
  846. /// NOTE: This should only be called for definitions.
  847. void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
  848. /// Set attributes which must be preserved by an alias. This includes common
  849. /// attributes (i.e. it includes a call to SetCommonAttributes).
  850. ///
  851. /// NOTE: This should only be called for definitions.
  852. void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV);
  853. void addReplacement(StringRef Name, llvm::Constant *C);
  854. private:
  855. llvm::Constant *
  856. GetOrCreateLLVMFunction(StringRef MangledName, llvm::Type *Ty, GlobalDecl D,
  857. bool ForVTable, bool DontDefer = false,
  858. llvm::AttributeSet ExtraAttrs = llvm::AttributeSet());
  859. llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
  860. llvm::PointerType *PTy,
  861. const VarDecl *D);
  862. void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);
  863. /// Set function attributes for a function declaration.
  864. void SetFunctionAttributes(GlobalDecl GD,
  865. llvm::Function *F,
  866. bool IsIncompleteFunction);
  867. void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
  868. void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
  869. void EmitGlobalVarDefinition(const VarDecl *D);
  870. void EmitAliasDefinition(GlobalDecl GD);
  871. void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
  872. void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
  873. // C++ related functions.
  874. void EmitNamespace(const NamespaceDecl *D);
  875. void EmitLinkageSpec(const LinkageSpecDecl *D);
  876. void CompleteDIClassType(const CXXMethodDecl* D);
  877. /// \brief Emit the function that initializes C++ thread_local variables.
  878. void EmitCXXThreadLocalInitFunc();
  879. /// Emit the function that initializes C++ globals.
  880. void EmitCXXGlobalInitFunc();
  881. /// Emit the function that destroys C++ globals.
  882. void EmitCXXGlobalDtorFunc();
  883. /// Emit the function that initializes the specified global (if PerformInit is
  884. /// true) and registers its destructor.
  885. void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
  886. llvm::GlobalVariable *Addr,
  887. bool PerformInit);
  888. void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
  889. llvm::Function *InitFunc, InitSegAttr *ISA);
  890. // FIXME: Hardcoding priority here is gross.
  891. void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
  892. llvm::Constant *AssociatedData = 0);
  893. void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
  894. /// Generates a global array of functions and priorities using the given list
  895. /// and name. This array will have appending linkage and is suitable for use
  896. /// as a LLVM constructor or destructor array.
  897. void EmitCtorList(const CtorList &Fns, const char *GlobalName);
  898. /// Emit the RTTI descriptors for the given type.
  899. void EmitFundamentalRTTIDescriptor(QualType Type);
  900. /// Emit any needed decls for which code generation was deferred.
  901. void EmitDeferred();
  902. /// Call replaceAllUsesWith on all pairs in Replacements.
  903. void applyReplacements();
  904. void checkAliases();
  905. /// Emit any vtables which we deferred and still have a use for.
  906. void EmitDeferredVTables();
  907. /// Emit the llvm.used and llvm.compiler.used metadata.
  908. void emitLLVMUsed();
  909. /// \brief Emit the link options introduced by imported modules.
  910. void EmitModuleLinkOptions();
  911. /// \brief Emit aliases for internal-linkage declarations inside "C" language
  912. /// linkage specifications, giving them the "expected" name where possible.
  913. void EmitStaticExternCAliases();
  914. void EmitDeclMetadata();
  915. /// \brief Emit the Clang version as llvm.ident metadata.
  916. void EmitVersionIdentMetadata();
  917. /// Emits target specific Metadata for global declarations.
  918. void EmitTargetMetadata();
  919. /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
  920. /// .gcda files in a way that persists in .bc files.
  921. void EmitCoverageFile();
  922. /// Emits the initializer for a uuidof string.
  923. llvm::Constant *EmitUuidofInitializer(StringRef uuidstr);
  924. /// Determine if the given decl can be emitted lazily; this is only relevant
  925. /// for definitions. The given decl must be either a function or var decl.
  926. bool MayDeferGeneration(const ValueDecl *D);
  927. /// Check whether we can use a "simpler", more core exceptions personality
  928. /// function.
  929. void SimplifyPersonality();
  930. };
  931. } // end namespace CodeGen
  932. } // end namespace clang
  933. #endif