CodeGenModule.h 42 KB

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