CodeGenModule.h 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is the internal per-translation-unit state used for llvm translation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H
  14. #include "CGVTables.h"
  15. #include "CodeGenTypeCache.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/DeclOpenMP.h"
  22. #include "clang/AST/GlobalDecl.h"
  23. #include "clang/AST/Mangle.h"
  24. #include "clang/Basic/ABI.h"
  25. #include "clang/Basic/LangOptions.h"
  26. #include "clang/Basic/Module.h"
  27. #include "clang/Basic/SanitizerBlacklist.h"
  28. #include "clang/Basic/XRayLists.h"
  29. #include "llvm/ADT/DenseMap.h"
  30. #include "llvm/ADT/SetVector.h"
  31. #include "llvm/ADT/SmallPtrSet.h"
  32. #include "llvm/ADT/StringMap.h"
  33. #include "llvm/IR/Module.h"
  34. #include "llvm/IR/ValueHandle.h"
  35. #include "llvm/Transforms/Utils/SanitizerStats.h"
  36. namespace llvm {
  37. class Module;
  38. class Constant;
  39. class ConstantInt;
  40. class Function;
  41. class GlobalValue;
  42. class DataLayout;
  43. class FunctionType;
  44. class LLVMContext;
  45. class IndexedInstrProfReader;
  46. }
  47. namespace clang {
  48. class ASTContext;
  49. class AtomicType;
  50. class FunctionDecl;
  51. class IdentifierInfo;
  52. class ObjCMethodDecl;
  53. class ObjCImplementationDecl;
  54. class ObjCCategoryImplDecl;
  55. class ObjCProtocolDecl;
  56. class ObjCEncodeExpr;
  57. class BlockExpr;
  58. class CharUnits;
  59. class Decl;
  60. class Expr;
  61. class Stmt;
  62. class InitListExpr;
  63. class StringLiteral;
  64. class NamedDecl;
  65. class ValueDecl;
  66. class VarDecl;
  67. class LangOptions;
  68. class CodeGenOptions;
  69. class HeaderSearchOptions;
  70. class PreprocessorOptions;
  71. class DiagnosticsEngine;
  72. class AnnotateAttr;
  73. class CXXDestructorDecl;
  74. class Module;
  75. class CoverageSourceInfo;
  76. namespace CodeGen {
  77. class CallArgList;
  78. class CodeGenFunction;
  79. class CodeGenTBAA;
  80. class CGCXXABI;
  81. class CGDebugInfo;
  82. class CGObjCRuntime;
  83. class CGOpenCLRuntime;
  84. class CGOpenMPRuntime;
  85. class CGCUDARuntime;
  86. class BlockFieldFlags;
  87. class FunctionArgList;
  88. class CoverageMappingModuleGen;
  89. class TargetCodeGenInfo;
  90. enum ForDefinition_t : bool {
  91. NotForDefinition = false,
  92. ForDefinition = true
  93. };
  94. struct OrderGlobalInits {
  95. unsigned int priority;
  96. unsigned int lex_order;
  97. OrderGlobalInits(unsigned int p, unsigned int l)
  98. : priority(p), lex_order(l) {}
  99. bool operator==(const OrderGlobalInits &RHS) const {
  100. return priority == RHS.priority && lex_order == RHS.lex_order;
  101. }
  102. bool operator<(const OrderGlobalInits &RHS) const {
  103. return std::tie(priority, lex_order) <
  104. std::tie(RHS.priority, RHS.lex_order);
  105. }
  106. };
  107. struct ObjCEntrypoints {
  108. ObjCEntrypoints() { memset(this, 0, sizeof(*this)); }
  109. /// void objc_alloc(id);
  110. llvm::FunctionCallee objc_alloc;
  111. /// void objc_allocWithZone(id);
  112. llvm::FunctionCallee objc_allocWithZone;
  113. /// void objc_alloc_init(id);
  114. llvm::FunctionCallee objc_alloc_init;
  115. /// void objc_autoreleasePoolPop(void*);
  116. llvm::FunctionCallee objc_autoreleasePoolPop;
  117. /// void objc_autoreleasePoolPop(void*);
  118. /// Note this method is used when we are using exception handling
  119. llvm::FunctionCallee objc_autoreleasePoolPopInvoke;
  120. /// void *objc_autoreleasePoolPush(void);
  121. llvm::Function *objc_autoreleasePoolPush;
  122. /// id objc_autorelease(id);
  123. llvm::Function *objc_autorelease;
  124. /// id objc_autorelease(id);
  125. /// Note this is the runtime method not the intrinsic.
  126. llvm::FunctionCallee objc_autoreleaseRuntimeFunction;
  127. /// id objc_autoreleaseReturnValue(id);
  128. llvm::Function *objc_autoreleaseReturnValue;
  129. /// void objc_copyWeak(id *dest, id *src);
  130. llvm::Function *objc_copyWeak;
  131. /// void objc_destroyWeak(id*);
  132. llvm::Function *objc_destroyWeak;
  133. /// id objc_initWeak(id*, id);
  134. llvm::Function *objc_initWeak;
  135. /// id objc_loadWeak(id*);
  136. llvm::Function *objc_loadWeak;
  137. /// id objc_loadWeakRetained(id*);
  138. llvm::Function *objc_loadWeakRetained;
  139. /// void objc_moveWeak(id *dest, id *src);
  140. llvm::Function *objc_moveWeak;
  141. /// id objc_retain(id);
  142. llvm::Function *objc_retain;
  143. /// id objc_retain(id);
  144. /// Note this is the runtime method not the intrinsic.
  145. llvm::FunctionCallee objc_retainRuntimeFunction;
  146. /// id objc_retainAutorelease(id);
  147. llvm::Function *objc_retainAutorelease;
  148. /// id objc_retainAutoreleaseReturnValue(id);
  149. llvm::Function *objc_retainAutoreleaseReturnValue;
  150. /// id objc_retainAutoreleasedReturnValue(id);
  151. llvm::Function *objc_retainAutoreleasedReturnValue;
  152. /// id objc_retainBlock(id);
  153. llvm::Function *objc_retainBlock;
  154. /// void objc_release(id);
  155. llvm::Function *objc_release;
  156. /// void objc_release(id);
  157. /// Note this is the runtime method not the intrinsic.
  158. llvm::FunctionCallee objc_releaseRuntimeFunction;
  159. /// void objc_storeStrong(id*, id);
  160. llvm::Function *objc_storeStrong;
  161. /// id objc_storeWeak(id*, id);
  162. llvm::Function *objc_storeWeak;
  163. /// id objc_unsafeClaimAutoreleasedReturnValue(id);
  164. llvm::Function *objc_unsafeClaimAutoreleasedReturnValue;
  165. /// A void(void) inline asm to use to mark that the return value of
  166. /// a call will be immediately retain.
  167. llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
  168. /// void clang.arc.use(...);
  169. llvm::Function *clang_arc_use;
  170. };
  171. /// This class records statistics on instrumentation based profiling.
  172. class InstrProfStats {
  173. uint32_t VisitedInMainFile;
  174. uint32_t MissingInMainFile;
  175. uint32_t Visited;
  176. uint32_t Missing;
  177. uint32_t Mismatched;
  178. public:
  179. InstrProfStats()
  180. : VisitedInMainFile(0), MissingInMainFile(0), Visited(0), Missing(0),
  181. Mismatched(0) {}
  182. /// Record that we've visited a function and whether or not that function was
  183. /// in the main source file.
  184. void addVisited(bool MainFile) {
  185. if (MainFile)
  186. ++VisitedInMainFile;
  187. ++Visited;
  188. }
  189. /// Record that a function we've visited has no profile data.
  190. void addMissing(bool MainFile) {
  191. if (MainFile)
  192. ++MissingInMainFile;
  193. ++Missing;
  194. }
  195. /// Record that a function we've visited has mismatched profile data.
  196. void addMismatched(bool MainFile) { ++Mismatched; }
  197. /// Whether or not the stats we've gathered indicate any potential problems.
  198. bool hasDiagnostics() { return Missing || Mismatched; }
  199. /// Report potential problems we've found to \c Diags.
  200. void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile);
  201. };
  202. /// A pair of helper functions for a __block variable.
  203. class BlockByrefHelpers : public llvm::FoldingSetNode {
  204. // MSVC requires this type to be complete in order to process this
  205. // header.
  206. public:
  207. llvm::Constant *CopyHelper;
  208. llvm::Constant *DisposeHelper;
  209. /// The alignment of the field. This is important because
  210. /// different offsets to the field within the byref struct need to
  211. /// have different helper functions.
  212. CharUnits Alignment;
  213. BlockByrefHelpers(CharUnits alignment)
  214. : CopyHelper(nullptr), DisposeHelper(nullptr), Alignment(alignment) {}
  215. BlockByrefHelpers(const BlockByrefHelpers &) = default;
  216. virtual ~BlockByrefHelpers();
  217. void Profile(llvm::FoldingSetNodeID &id) const {
  218. id.AddInteger(Alignment.getQuantity());
  219. profileImpl(id);
  220. }
  221. virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
  222. virtual bool needsCopy() const { return true; }
  223. virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src) = 0;
  224. virtual bool needsDispose() const { return true; }
  225. virtual void emitDispose(CodeGenFunction &CGF, Address field) = 0;
  226. };
  227. /// This class organizes the cross-function state that is used while generating
  228. /// LLVM code.
  229. class CodeGenModule : public CodeGenTypeCache {
  230. CodeGenModule(const CodeGenModule &) = delete;
  231. void operator=(const CodeGenModule &) = delete;
  232. public:
  233. struct Structor {
  234. Structor() : Priority(0), Initializer(nullptr), AssociatedData(nullptr) {}
  235. Structor(int Priority, llvm::Constant *Initializer,
  236. llvm::Constant *AssociatedData)
  237. : Priority(Priority), Initializer(Initializer),
  238. AssociatedData(AssociatedData) {}
  239. int Priority;
  240. llvm::Constant *Initializer;
  241. llvm::Constant *AssociatedData;
  242. };
  243. typedef std::vector<Structor> CtorList;
  244. private:
  245. ASTContext &Context;
  246. const LangOptions &LangOpts;
  247. const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
  248. const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
  249. const CodeGenOptions &CodeGenOpts;
  250. llvm::Module &TheModule;
  251. DiagnosticsEngine &Diags;
  252. const TargetInfo &Target;
  253. std::unique_ptr<CGCXXABI> ABI;
  254. llvm::LLVMContext &VMContext;
  255. std::unique_ptr<CodeGenTBAA> TBAA;
  256. mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
  257. // This should not be moved earlier, since its initialization depends on some
  258. // of the previous reference members being already initialized and also checks
  259. // if TheTargetCodeGenInfo is NULL
  260. CodeGenTypes Types;
  261. /// Holds information about C++ vtables.
  262. CodeGenVTables VTables;
  263. std::unique_ptr<CGObjCRuntime> ObjCRuntime;
  264. std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
  265. std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
  266. std::unique_ptr<CGCUDARuntime> CUDARuntime;
  267. std::unique_ptr<CGDebugInfo> DebugInfo;
  268. std::unique_ptr<ObjCEntrypoints> ObjCData;
  269. llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
  270. std::unique_ptr<llvm::IndexedInstrProfReader> PGOReader;
  271. InstrProfStats PGOStats;
  272. std::unique_ptr<llvm::SanitizerStatReport> SanStats;
  273. // A set of references that have only been seen via a weakref so far. This is
  274. // used to remove the weak of the reference if we ever see a direct reference
  275. // or a definition.
  276. llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
  277. /// This contains all the decls which have definitions but/ which are deferred
  278. /// for emission and therefore should only be output if they are actually
  279. /// used. If a decl is in this, then it is known to have not been referenced
  280. /// yet.
  281. std::map<StringRef, GlobalDecl> DeferredDecls;
  282. /// This is a list of deferred decls which we have seen that *are* actually
  283. /// referenced. These get code generated when the module is done.
  284. std::vector<GlobalDecl> DeferredDeclsToEmit;
  285. void addDeferredDeclToEmit(GlobalDecl GD) {
  286. DeferredDeclsToEmit.emplace_back(GD);
  287. }
  288. /// List of alias we have emitted. Used to make sure that what they point to
  289. /// is defined once we get to the end of the of the translation unit.
  290. std::vector<GlobalDecl> Aliases;
  291. /// List of multiversion functions that have to be emitted. Used to make sure
  292. /// we properly emit the iFunc.
  293. std::vector<GlobalDecl> MultiVersionFuncs;
  294. typedef llvm::StringMap<llvm::TrackingVH<llvm::Constant> > ReplacementsTy;
  295. ReplacementsTy Replacements;
  296. /// List of global values to be replaced with something else. Used when we
  297. /// want to replace a GlobalValue but can't identify it by its mangled name
  298. /// anymore (because the name is already taken).
  299. llvm::SmallVector<std::pair<llvm::GlobalValue *, llvm::Constant *>, 8>
  300. GlobalValReplacements;
  301. /// Set of global decls for which we already diagnosed mangled name conflict.
  302. /// Required to not issue a warning (on a mangling conflict) multiple times
  303. /// for the same decl.
  304. llvm::DenseSet<GlobalDecl> DiagnosedConflictingDefinitions;
  305. /// A queue of (optional) vtables to consider emitting.
  306. std::vector<const CXXRecordDecl*> DeferredVTables;
  307. /// A queue of (optional) vtables that may be emitted opportunistically.
  308. std::vector<const CXXRecordDecl *> OpportunisticVTables;
  309. /// List of global values which are required to be present in the object file;
  310. /// bitcast to i8*. This is used for forcing visibility of symbols which may
  311. /// otherwise be optimized out.
  312. std::vector<llvm::WeakTrackingVH> LLVMUsed;
  313. std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
  314. /// Store the list of global constructors and their respective priorities to
  315. /// be emitted when the translation unit is complete.
  316. CtorList GlobalCtors;
  317. /// Store the list of global destructors and their respective priorities to be
  318. /// emitted when the translation unit is complete.
  319. CtorList GlobalDtors;
  320. /// An ordered map of canonical GlobalDecls to their mangled names.
  321. llvm::MapVector<GlobalDecl, StringRef> MangledDeclNames;
  322. llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> Manglings;
  323. // An ordered map of canonical GlobalDecls paired with the cpu-index for
  324. // cpu-specific name manglings.
  325. llvm::MapVector<std::pair<GlobalDecl, unsigned>, StringRef>
  326. CPUSpecificMangledDeclNames;
  327. llvm::StringMap<std::pair<GlobalDecl, unsigned>, llvm::BumpPtrAllocator>
  328. CPUSpecificManglings;
  329. /// Global annotations.
  330. std::vector<llvm::Constant*> Annotations;
  331. /// Map used to get unique annotation strings.
  332. llvm::StringMap<llvm::Constant*> AnnotationStrings;
  333. llvm::StringMap<llvm::GlobalVariable *> CFConstantStringMap;
  334. llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;
  335. llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;
  336. llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;
  337. llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;
  338. llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
  339. llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
  340. /// Map used to get unique type descriptor constants for sanitizers.
  341. llvm::DenseMap<QualType, llvm::Constant *> TypeDescriptorMap;
  342. /// Map used to track internal linkage functions declared within
  343. /// extern "C" regions.
  344. typedef llvm::MapVector<IdentifierInfo *,
  345. llvm::GlobalValue *> StaticExternCMap;
  346. StaticExternCMap StaticExternCValues;
  347. /// thread_local variables defined or used in this TU.
  348. std::vector<const VarDecl *> CXXThreadLocals;
  349. /// thread_local variables with initializers that need to run
  350. /// before any thread_local variable in this TU is odr-used.
  351. std::vector<llvm::Function *> CXXThreadLocalInits;
  352. std::vector<const VarDecl *> CXXThreadLocalInitVars;
  353. /// Global variables with initializers that need to run before main.
  354. std::vector<llvm::Function *> CXXGlobalInits;
  355. /// When a C++ decl with an initializer is deferred, null is
  356. /// appended to CXXGlobalInits, and the index of that null is placed
  357. /// here so that the initializer will be performed in the correct
  358. /// order. Once the decl is emitted, the index is replaced with ~0U to ensure
  359. /// that we don't re-emit the initializer.
  360. llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
  361. typedef std::pair<OrderGlobalInits, llvm::Function*> GlobalInitData;
  362. struct GlobalInitPriorityCmp {
  363. bool operator()(const GlobalInitData &LHS,
  364. const GlobalInitData &RHS) const {
  365. return LHS.first.priority < RHS.first.priority;
  366. }
  367. };
  368. /// Global variables with initializers whose order of initialization is set by
  369. /// init_priority attribute.
  370. SmallVector<GlobalInitData, 8> PrioritizedCXXGlobalInits;
  371. /// Global destructor functions and arguments that need to run on termination.
  372. std::vector<
  373. std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH, llvm::Constant *>>
  374. CXXGlobalDtors;
  375. /// The complete set of modules that has been imported.
  376. llvm::SetVector<clang::Module *> ImportedModules;
  377. /// The set of modules for which the module initializers
  378. /// have been emitted.
  379. llvm::SmallPtrSet<clang::Module *, 16> EmittedModuleInitializers;
  380. /// A vector of metadata strings for linker options.
  381. SmallVector<llvm::MDNode *, 16> LinkerOptionsMetadata;
  382. /// A vector of metadata strings for dependent libraries for ELF.
  383. SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
  384. /// @name Cache for Objective-C runtime types
  385. /// @{
  386. /// Cached reference to the class for constant strings. This value has type
  387. /// int * but is actually an Obj-C class pointer.
  388. llvm::WeakTrackingVH CFConstantStringClassRef;
  389. /// The type used to describe the state of a fast enumeration in
  390. /// Objective-C's for..in loop.
  391. QualType ObjCFastEnumerationStateType;
  392. /// @}
  393. /// Lazily create the Objective-C runtime
  394. void createObjCRuntime();
  395. void createOpenCLRuntime();
  396. void createOpenMPRuntime();
  397. void createCUDARuntime();
  398. bool isTriviallyRecursive(const FunctionDecl *F);
  399. bool shouldEmitFunction(GlobalDecl GD);
  400. bool shouldOpportunisticallyEmitVTables();
  401. /// Map used to be sure we don't emit the same CompoundLiteral twice.
  402. llvm::DenseMap<const CompoundLiteralExpr *, llvm::GlobalVariable *>
  403. EmittedCompoundLiterals;
  404. /// Map of the global blocks we've emitted, so that we don't have to re-emit
  405. /// them if the constexpr evaluator gets aggressive.
  406. llvm::DenseMap<const BlockExpr *, llvm::Constant *> EmittedGlobalBlocks;
  407. /// @name Cache for Blocks Runtime Globals
  408. /// @{
  409. llvm::Constant *NSConcreteGlobalBlock = nullptr;
  410. llvm::Constant *NSConcreteStackBlock = nullptr;
  411. llvm::FunctionCallee BlockObjectAssign = nullptr;
  412. llvm::FunctionCallee BlockObjectDispose = nullptr;
  413. llvm::Type *BlockDescriptorType = nullptr;
  414. llvm::Type *GenericBlockLiteralType = nullptr;
  415. struct {
  416. int GlobalUniqueCount;
  417. } Block;
  418. /// void @llvm.lifetime.start(i64 %size, i8* nocapture <ptr>)
  419. llvm::Function *LifetimeStartFn = nullptr;
  420. /// void @llvm.lifetime.end(i64 %size, i8* nocapture <ptr>)
  421. llvm::Function *LifetimeEndFn = nullptr;
  422. GlobalDecl initializedGlobalDecl;
  423. std::unique_ptr<SanitizerMetadata> SanitizerMD;
  424. /// @}
  425. llvm::MapVector<const Decl *, bool> DeferredEmptyCoverageMappingDecls;
  426. std::unique_ptr<CoverageMappingModuleGen> CoverageMapping;
  427. /// Mapping from canonical types to their metadata identifiers. We need to
  428. /// maintain this mapping because identifiers may be formed from distinct
  429. /// MDNodes.
  430. typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap;
  431. MetadataTypeMap MetadataIdMap;
  432. MetadataTypeMap VirtualMetadataIdMap;
  433. MetadataTypeMap GeneralizedMetadataIdMap;
  434. public:
  435. CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
  436. const PreprocessorOptions &ppopts,
  437. const CodeGenOptions &CodeGenOpts, llvm::Module &M,
  438. DiagnosticsEngine &Diags,
  439. CoverageSourceInfo *CoverageInfo = nullptr);
  440. ~CodeGenModule();
  441. void clear();
  442. /// Finalize LLVM code generation.
  443. void Release();
  444. /// Return true if we should emit location information for expressions.
  445. bool getExpressionLocationsEnabled() const;
  446. /// Return a reference to the configured Objective-C runtime.
  447. CGObjCRuntime &getObjCRuntime() {
  448. if (!ObjCRuntime) createObjCRuntime();
  449. return *ObjCRuntime;
  450. }
  451. /// Return true iff an Objective-C runtime has been configured.
  452. bool hasObjCRuntime() { return !!ObjCRuntime; }
  453. /// Return a reference to the configured OpenCL runtime.
  454. CGOpenCLRuntime &getOpenCLRuntime() {
  455. assert(OpenCLRuntime != nullptr);
  456. return *OpenCLRuntime;
  457. }
  458. /// Return a reference to the configured OpenMP runtime.
  459. CGOpenMPRuntime &getOpenMPRuntime() {
  460. assert(OpenMPRuntime != nullptr);
  461. return *OpenMPRuntime;
  462. }
  463. /// Return a reference to the configured CUDA runtime.
  464. CGCUDARuntime &getCUDARuntime() {
  465. assert(CUDARuntime != nullptr);
  466. return *CUDARuntime;
  467. }
  468. ObjCEntrypoints &getObjCEntrypoints() const {
  469. assert(ObjCData != nullptr);
  470. return *ObjCData;
  471. }
  472. // Version checking function, used to implement ObjC's @available:
  473. // i32 @__isOSVersionAtLeast(i32, i32, i32)
  474. llvm::FunctionCallee IsOSVersionAtLeastFn = nullptr;
  475. InstrProfStats &getPGOStats() { return PGOStats; }
  476. llvm::IndexedInstrProfReader *getPGOReader() const { return PGOReader.get(); }
  477. CoverageMappingModuleGen *getCoverageMapping() const {
  478. return CoverageMapping.get();
  479. }
  480. llvm::Constant *getStaticLocalDeclAddress(const VarDecl *D) {
  481. return StaticLocalDeclMap[D];
  482. }
  483. void setStaticLocalDeclAddress(const VarDecl *D,
  484. llvm::Constant *C) {
  485. StaticLocalDeclMap[D] = C;
  486. }
  487. llvm::Constant *
  488. getOrCreateStaticVarDecl(const VarDecl &D,
  489. llvm::GlobalValue::LinkageTypes Linkage);
  490. llvm::GlobalVariable *getStaticLocalDeclGuardAddress(const VarDecl *D) {
  491. return StaticLocalDeclGuardMap[D];
  492. }
  493. void setStaticLocalDeclGuardAddress(const VarDecl *D,
  494. llvm::GlobalVariable *C) {
  495. StaticLocalDeclGuardMap[D] = C;
  496. }
  497. bool lookupRepresentativeDecl(StringRef MangledName,
  498. GlobalDecl &Result) const;
  499. llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
  500. return AtomicSetterHelperFnMap[Ty];
  501. }
  502. void setAtomicSetterHelperFnMap(QualType Ty,
  503. llvm::Constant *Fn) {
  504. AtomicSetterHelperFnMap[Ty] = Fn;
  505. }
  506. llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
  507. return AtomicGetterHelperFnMap[Ty];
  508. }
  509. void setAtomicGetterHelperFnMap(QualType Ty,
  510. llvm::Constant *Fn) {
  511. AtomicGetterHelperFnMap[Ty] = Fn;
  512. }
  513. llvm::Constant *getTypeDescriptorFromMap(QualType Ty) {
  514. return TypeDescriptorMap[Ty];
  515. }
  516. void setTypeDescriptorInMap(QualType Ty, llvm::Constant *C) {
  517. TypeDescriptorMap[Ty] = C;
  518. }
  519. CGDebugInfo *getModuleDebugInfo() { return DebugInfo.get(); }
  520. llvm::MDNode *getNoObjCARCExceptionsMetadata() {
  521. if (!NoObjCARCExceptionsMetadata)
  522. NoObjCARCExceptionsMetadata = llvm::MDNode::get(getLLVMContext(), None);
  523. return NoObjCARCExceptionsMetadata;
  524. }
  525. ASTContext &getContext() const { return Context; }
  526. const LangOptions &getLangOpts() const { return LangOpts; }
  527. const HeaderSearchOptions &getHeaderSearchOpts()
  528. const { return HeaderSearchOpts; }
  529. const PreprocessorOptions &getPreprocessorOpts()
  530. const { return PreprocessorOpts; }
  531. const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
  532. llvm::Module &getModule() const { return TheModule; }
  533. DiagnosticsEngine &getDiags() const { return Diags; }
  534. const llvm::DataLayout &getDataLayout() const {
  535. return TheModule.getDataLayout();
  536. }
  537. const TargetInfo &getTarget() const { return Target; }
  538. const llvm::Triple &getTriple() const { return Target.getTriple(); }
  539. bool supportsCOMDAT() const;
  540. void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
  541. CGCXXABI &getCXXABI() const { return *ABI; }
  542. llvm::LLVMContext &getLLVMContext() { return VMContext; }
  543. bool shouldUseTBAA() const { return TBAA != nullptr; }
  544. const TargetCodeGenInfo &getTargetCodeGenInfo();
  545. CodeGenTypes &getTypes() { return Types; }
  546. CodeGenVTables &getVTables() { return VTables; }
  547. ItaniumVTableContext &getItaniumVTableContext() {
  548. return VTables.getItaniumVTableContext();
  549. }
  550. MicrosoftVTableContext &getMicrosoftVTableContext() {
  551. return VTables.getMicrosoftVTableContext();
  552. }
  553. CtorList &getGlobalCtors() { return GlobalCtors; }
  554. CtorList &getGlobalDtors() { return GlobalDtors; }
  555. /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
  556. /// the given type.
  557. llvm::MDNode *getTBAATypeInfo(QualType QTy);
  558. /// getTBAAAccessInfo - Get TBAA information that describes an access to
  559. /// an object of the given type.
  560. TBAAAccessInfo getTBAAAccessInfo(QualType AccessType);
  561. /// getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an
  562. /// access to a virtual table pointer.
  563. TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType);
  564. llvm::MDNode *getTBAAStructInfo(QualType QTy);
  565. /// getTBAABaseTypeInfo - Get metadata that describes the given base access
  566. /// type. Return null if the type is not suitable for use in TBAA access tags.
  567. llvm::MDNode *getTBAABaseTypeInfo(QualType QTy);
  568. /// getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
  569. llvm::MDNode *getTBAAAccessTagInfo(TBAAAccessInfo Info);
  570. /// mergeTBAAInfoForCast - Get merged TBAA information for the purposes of
  571. /// type casts.
  572. TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
  573. TBAAAccessInfo TargetInfo);
  574. /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
  575. /// purposes of conditional operator.
  576. TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
  577. TBAAAccessInfo InfoB);
  578. /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
  579. /// purposes of memory transfer calls.
  580. TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
  581. TBAAAccessInfo SrcInfo);
  582. /// getTBAAInfoForSubobject - Get TBAA information for an access with a given
  583. /// base lvalue.
  584. TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
  585. if (Base.getTBAAInfo().isMayAlias())
  586. return TBAAAccessInfo::getMayAliasInfo();
  587. return getTBAAAccessInfo(AccessType);
  588. }
  589. bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
  590. bool isPaddedAtomicType(QualType type);
  591. bool isPaddedAtomicType(const AtomicType *type);
  592. /// DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
  593. void DecorateInstructionWithTBAA(llvm::Instruction *Inst,
  594. TBAAAccessInfo TBAAInfo);
  595. /// Adds !invariant.barrier !tag to instruction
  596. void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
  597. const CXXRecordDecl *RD);
  598. /// Emit the given number of characters as a value of type size_t.
  599. llvm::ConstantInt *getSize(CharUnits numChars);
  600. /// Set the visibility for the given LLVM GlobalValue.
  601. void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
  602. void setDSOLocal(llvm::GlobalValue *GV) const;
  603. void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
  604. void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
  605. /// Set visibility, dllimport/dllexport and dso_local.
  606. /// This must be called after dllimport/dllexport is set.
  607. void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const;
  608. void setGVProperties(llvm::GlobalValue *GV, const NamedDecl *D) const;
  609. void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const;
  610. /// Set the TLS mode for the given LLVM GlobalValue for the thread-local
  611. /// variable declaration D.
  612. void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const;
  613. static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
  614. switch (V) {
  615. case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
  616. case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
  617. case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
  618. }
  619. llvm_unreachable("unknown visibility!");
  620. }
  621. llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
  622. ForDefinition_t IsForDefinition
  623. = NotForDefinition);
  624. /// Will return a global variable of the given type. If a variable with a
  625. /// different type already exists then a new variable with the right type
  626. /// will be created and all uses of the old variable will be replaced with a
  627. /// bitcast to the new variable.
  628. llvm::GlobalVariable *
  629. CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
  630. llvm::GlobalValue::LinkageTypes Linkage,
  631. unsigned Alignment);
  632. llvm::Function *
  633. CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name,
  634. const CGFunctionInfo &FI,
  635. SourceLocation Loc = SourceLocation(),
  636. bool TLS = false);
  637. /// Return the AST address space of the underlying global variable for D, as
  638. /// determined by its declaration. Normally this is the same as the address
  639. /// space of D's type, but in CUDA, address spaces are associated with
  640. /// declarations, not types. If D is nullptr, return the default address
  641. /// space for global variable.
  642. ///
  643. /// For languages without explicit address spaces, if D has default address
  644. /// space, target-specific global or constant address space may be returned.
  645. LangAS GetGlobalVarAddressSpace(const VarDecl *D);
  646. /// Return the llvm::Constant for the address of the given global variable.
  647. /// If Ty is non-null and if the global doesn't exist, then it will be created
  648. /// with the specified type instead of whatever the normal requested type
  649. /// would be. If IsForDefinition is true, it is guaranteed that an actual
  650. /// global with type Ty will be returned, not conversion of a variable with
  651. /// the same mangled name but some other type.
  652. llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
  653. llvm::Type *Ty = nullptr,
  654. ForDefinition_t IsForDefinition
  655. = NotForDefinition);
  656. /// Return the AST address space of string literal, which is used to emit
  657. /// the string literal as global variable in LLVM IR.
  658. /// Note: This is not necessarily the address space of the string literal
  659. /// in AST. For address space agnostic language, e.g. C++, string literal
  660. /// in AST is always in default address space.
  661. LangAS getStringLiteralAddressSpace() const;
  662. /// Return the address of the given function. If Ty is non-null, then this
  663. /// function will use the specified type if it has to create it.
  664. llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
  665. bool ForVTable = false,
  666. bool DontDefer = false,
  667. ForDefinition_t IsForDefinition
  668. = NotForDefinition);
  669. /// Get the address of the RTTI descriptor for the given type.
  670. llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
  671. /// Get the address of a uuid descriptor .
  672. ConstantAddress GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
  673. /// Get the address of the thunk for the given global decl.
  674. llvm::Constant *GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
  675. GlobalDecl GD);
  676. /// Get a reference to the target of VD.
  677. ConstantAddress GetWeakRefReference(const ValueDecl *VD);
  678. /// Returns the assumed alignment of an opaque pointer to the given class.
  679. CharUnits getClassPointerAlignment(const CXXRecordDecl *CD);
  680. /// Returns the assumed alignment of a virtual base of a class.
  681. CharUnits getVBaseAlignment(CharUnits DerivedAlign,
  682. const CXXRecordDecl *Derived,
  683. const CXXRecordDecl *VBase);
  684. /// Given a class pointer with an actual known alignment, and the
  685. /// expected alignment of an object at a dynamic offset w.r.t that
  686. /// pointer, return the alignment to assume at the offset.
  687. CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign,
  688. const CXXRecordDecl *Class,
  689. CharUnits ExpectedTargetAlign);
  690. CharUnits
  691. computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass,
  692. CastExpr::path_const_iterator Start,
  693. CastExpr::path_const_iterator End);
  694. /// Returns the offset from a derived class to a class. Returns null if the
  695. /// offset is 0.
  696. llvm::Constant *
  697. GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
  698. CastExpr::path_const_iterator PathBegin,
  699. CastExpr::path_const_iterator PathEnd);
  700. llvm::FoldingSet<BlockByrefHelpers> ByrefHelpersCache;
  701. /// Fetches the global unique block count.
  702. int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
  703. /// Fetches the type of a generic block descriptor.
  704. llvm::Type *getBlockDescriptorType();
  705. /// The type of a generic block literal.
  706. llvm::Type *getGenericBlockLiteralType();
  707. /// Gets the address of a block which requires no captures.
  708. llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name);
  709. /// Returns the address of a block which requires no caputres, or null if
  710. /// we've yet to emit the block for BE.
  711. llvm::Constant *getAddrOfGlobalBlockIfEmitted(const BlockExpr *BE) {
  712. return EmittedGlobalBlocks.lookup(BE);
  713. }
  714. /// Notes that BE's global block is available via Addr. Asserts that BE
  715. /// isn't already emitted.
  716. void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr);
  717. /// Return a pointer to a constant CFString object for the given string.
  718. ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal);
  719. /// Return a pointer to a constant NSString object for the given string. Or a
  720. /// user defined String object as defined via
  721. /// -fconstant-string-class=class_name option.
  722. ConstantAddress GetAddrOfConstantString(const StringLiteral *Literal);
  723. /// Return a constant array for the given string.
  724. llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
  725. /// Return a pointer to a constant array for the given string literal.
  726. ConstantAddress
  727. GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
  728. StringRef Name = ".str");
  729. /// Return a pointer to a constant array for the given ObjCEncodeExpr node.
  730. ConstantAddress
  731. GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
  732. /// Returns a pointer to a character array containing the literal and a
  733. /// terminating '\0' character. The result has pointer to array type.
  734. ///
  735. /// \param GlobalName If provided, the name to use for the global (if one is
  736. /// created).
  737. ConstantAddress
  738. GetAddrOfConstantCString(const std::string &Str,
  739. const char *GlobalName = nullptr);
  740. /// Returns a pointer to a constant global variable for the given file-scope
  741. /// compound literal expression.
  742. ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
  743. /// If it's been emitted already, returns the GlobalVariable corresponding to
  744. /// a compound literal. Otherwise, returns null.
  745. llvm::GlobalVariable *
  746. getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E);
  747. /// Notes that CLE's GlobalVariable is GV. Asserts that CLE isn't already
  748. /// emitted.
  749. void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE,
  750. llvm::GlobalVariable *GV);
  751. /// Returns a pointer to a global variable representing a temporary
  752. /// with static or thread storage duration.
  753. ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E,
  754. const Expr *Inner);
  755. /// Retrieve the record type that describes the state of an
  756. /// Objective-C fast enumeration loop (for..in).
  757. QualType getObjCFastEnumerationStateType();
  758. // Produce code for this constructor/destructor. This method doesn't try
  759. // to apply any ABI rules about which other constructors/destructors
  760. // are needed or if they are alias to each other.
  761. llvm::Function *codegenCXXStructor(GlobalDecl GD);
  762. /// Return the address of the constructor/destructor of the given type.
  763. llvm::Constant *
  764. getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
  765. llvm::FunctionType *FnType = nullptr,
  766. bool DontDefer = false,
  767. ForDefinition_t IsForDefinition = NotForDefinition) {
  768. return cast<llvm::Constant>(getAddrAndTypeOfCXXStructor(GD, FnInfo, FnType,
  769. DontDefer,
  770. IsForDefinition)
  771. .getCallee());
  772. }
  773. llvm::FunctionCallee getAddrAndTypeOfCXXStructor(
  774. GlobalDecl GD, const CGFunctionInfo *FnInfo = nullptr,
  775. llvm::FunctionType *FnType = nullptr, bool DontDefer = false,
  776. ForDefinition_t IsForDefinition = NotForDefinition);
  777. /// Given a builtin id for a function like "__builtin_fabsf", return a
  778. /// Function* for "fabsf".
  779. llvm::Constant *getBuiltinLibFunction(const FunctionDecl *FD,
  780. unsigned BuiltinID);
  781. llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys = None);
  782. /// Emit code for a single top level declaration.
  783. void EmitTopLevelDecl(Decl *D);
  784. /// Stored a deferred empty coverage mapping for an unused
  785. /// and thus uninstrumented top level declaration.
  786. void AddDeferredUnusedCoverageMapping(Decl *D);
  787. /// Remove the deferred empty coverage mapping as this
  788. /// declaration is actually instrumented.
  789. void ClearUnusedCoverageMapping(const Decl *D);
  790. /// Emit all the deferred coverage mappings
  791. /// for the uninstrumented functions.
  792. void EmitDeferredUnusedCoverageMappings();
  793. /// Tell the consumer that this variable has been instantiated.
  794. void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
  795. /// If the declaration has internal linkage but is inside an
  796. /// extern "C" linkage specification, prepare to emit an alias for it
  797. /// to the expected name.
  798. template<typename SomeDecl>
  799. void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
  800. /// Add a global to a list to be added to the llvm.used metadata.
  801. void addUsedGlobal(llvm::GlobalValue *GV);
  802. /// Add a global to a list to be added to the llvm.compiler.used metadata.
  803. void addCompilerUsedGlobal(llvm::GlobalValue *GV);
  804. /// Add a destructor and object to add to the C++ global destructor function.
  805. void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object) {
  806. CXXGlobalDtors.emplace_back(DtorFn.getFunctionType(), DtorFn.getCallee(),
  807. Object);
  808. }
  809. /// Create or return a runtime function declaration with the specified type
  810. /// and name.
  811. llvm::FunctionCallee
  812. CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
  813. llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
  814. bool Local = false);
  815. /// Create a new runtime global variable with the specified type and name.
  816. llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
  817. StringRef Name);
  818. ///@name Custom Blocks Runtime Interfaces
  819. ///@{
  820. llvm::Constant *getNSConcreteGlobalBlock();
  821. llvm::Constant *getNSConcreteStackBlock();
  822. llvm::FunctionCallee getBlockObjectAssign();
  823. llvm::FunctionCallee getBlockObjectDispose();
  824. ///@}
  825. llvm::Function *getLLVMLifetimeStartFn();
  826. llvm::Function *getLLVMLifetimeEndFn();
  827. // Make sure that this type is translated.
  828. void UpdateCompletedType(const TagDecl *TD);
  829. llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
  830. /// Emit type info if type of an expression is a variably modified
  831. /// type. Also emit proper debug info for cast types.
  832. void EmitExplicitCastExprType(const ExplicitCastExpr *E,
  833. CodeGenFunction *CGF = nullptr);
  834. /// Return the result of value-initializing the given type, i.e. a null
  835. /// expression of the given type. This is usually, but not always, an LLVM
  836. /// null constant.
  837. llvm::Constant *EmitNullConstant(QualType T);
  838. /// Return a null constant appropriate for zero-initializing a base class with
  839. /// the given type. This is usually, but not always, an LLVM null constant.
  840. llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
  841. /// Emit a general error that something can't be done.
  842. void Error(SourceLocation loc, StringRef error);
  843. /// Print out an error that codegen doesn't support the specified stmt yet.
  844. void ErrorUnsupported(const Stmt *S, const char *Type);
  845. /// Print out an error that codegen doesn't support the specified decl yet.
  846. void ErrorUnsupported(const Decl *D, const char *Type);
  847. /// Set the attributes on the LLVM function for the given decl and function
  848. /// info. This applies attributes necessary for handling the ABI as well as
  849. /// user specified attributes like section.
  850. void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F,
  851. const CGFunctionInfo &FI);
  852. /// Set the LLVM function attributes (sext, zext, etc).
  853. void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info,
  854. llvm::Function *F);
  855. /// Set the LLVM function attributes which only apply to a function
  856. /// definition.
  857. void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
  858. /// Return true iff the given type uses 'sret' when used as a return type.
  859. bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
  860. /// Return true iff the given type uses an argument slot when 'sret' is used
  861. /// as a return type.
  862. bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
  863. /// Return true iff the given type uses 'fpret' when used as a return type.
  864. bool ReturnTypeUsesFPRet(QualType ResultType);
  865. /// Return true iff the given type uses 'fp2ret' when used as a return type.
  866. bool ReturnTypeUsesFP2Ret(QualType ResultType);
  867. /// Get the LLVM attributes and calling convention to use for a particular
  868. /// function type.
  869. ///
  870. /// \param Name - The function name.
  871. /// \param Info - The function type information.
  872. /// \param CalleeInfo - The callee information these attributes are being
  873. /// constructed for. If valid, the attributes applied to this decl may
  874. /// contribute to the function attributes and calling convention.
  875. /// \param Attrs [out] - On return, the attribute list to use.
  876. /// \param CallingConv [out] - On return, the LLVM calling convention to use.
  877. void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info,
  878. CGCalleeInfo CalleeInfo,
  879. llvm::AttributeList &Attrs, unsigned &CallingConv,
  880. bool AttrOnCallSite);
  881. /// Adds attributes to F according to our CodeGenOptions and LangOptions, as
  882. /// though we had emitted it ourselves. We remove any attributes on F that
  883. /// conflict with the attributes we add here.
  884. ///
  885. /// This is useful for adding attrs to bitcode modules that you want to link
  886. /// with but don't control, such as CUDA's libdevice. When linking with such
  887. /// a bitcode library, you might want to set e.g. its functions'
  888. /// "unsafe-fp-math" attribute to match the attr of the functions you're
  889. /// codegen'ing. Otherwise, LLVM will interpret the bitcode module's lack of
  890. /// unsafe-fp-math attrs as tantamount to unsafe-fp-math=false, and then LLVM
  891. /// will propagate unsafe-fp-math=false up to every transitive caller of a
  892. /// function in the bitcode library!
  893. ///
  894. /// With the exception of fast-math attrs, this will only make the attributes
  895. /// on the function more conservative. But it's unsafe to call this on a
  896. /// function which relies on particular fast-math attributes for correctness.
  897. /// It's up to you to ensure that this is safe.
  898. void AddDefaultFnAttrs(llvm::Function &F);
  899. /// Parses the target attributes passed in, and returns only the ones that are
  900. /// valid feature names.
  901. TargetAttr::ParsedTargetAttr filterFunctionTargetAttrs(const TargetAttr *TD);
  902. // Fills in the supplied string map with the set of target features for the
  903. // passed in function.
  904. void getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap, GlobalDecl GD);
  905. StringRef getMangledName(GlobalDecl GD);
  906. StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD);
  907. void EmitTentativeDefinition(const VarDecl *D);
  908. void EmitVTable(CXXRecordDecl *Class);
  909. void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
  910. /// Appends Opts to the "llvm.linker.options" metadata value.
  911. void AppendLinkerOptions(StringRef Opts);
  912. /// Appends a detect mismatch command to the linker options.
  913. void AddDetectMismatch(StringRef Name, StringRef Value);
  914. /// Appends a dependent lib to the appropriate metadata value.
  915. void AddDependentLib(StringRef Lib);
  916. llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
  917. void setFunctionLinkage(GlobalDecl GD, llvm::Function *F) {
  918. F->setLinkage(getFunctionLinkage(GD));
  919. }
  920. /// Return the appropriate linkage for the vtable, VTT, and type information
  921. /// of the given class.
  922. llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
  923. /// Return the store size, in character units, of the given LLVM type.
  924. CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
  925. /// Returns LLVM linkage for a declarator.
  926. llvm::GlobalValue::LinkageTypes
  927. getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage,
  928. bool IsConstantVariable);
  929. /// Returns LLVM linkage for a declarator.
  930. llvm::GlobalValue::LinkageTypes
  931. getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant);
  932. /// Emit all the global annotations.
  933. void EmitGlobalAnnotations();
  934. /// Emit an annotation string.
  935. llvm::Constant *EmitAnnotationString(StringRef Str);
  936. /// Emit the annotation's translation unit.
  937. llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
  938. /// Emit the annotation line number.
  939. llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
  940. /// Generate the llvm::ConstantStruct which contains the annotation
  941. /// information for a given GlobalValue. The annotation struct is
  942. /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
  943. /// GlobalValue being annotated. The second field is the constant string
  944. /// created from the AnnotateAttr's annotation. The third field is a constant
  945. /// string containing the name of the translation unit. The fourth field is
  946. /// the line number in the file of the annotated value declaration.
  947. llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
  948. const AnnotateAttr *AA,
  949. SourceLocation L);
  950. /// Add global annotations that are set on D, for the global GV. Those
  951. /// annotations are emitted during finalization of the LLVM code.
  952. void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
  953. bool isInSanitizerBlacklist(SanitizerMask Kind, llvm::Function *Fn,
  954. SourceLocation Loc) const;
  955. bool isInSanitizerBlacklist(llvm::GlobalVariable *GV, SourceLocation Loc,
  956. QualType Ty,
  957. StringRef Category = StringRef()) const;
  958. /// Imbue XRay attributes to a function, applying the always/never attribute
  959. /// lists in the process. Returns true if we did imbue attributes this way,
  960. /// false otherwise.
  961. bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
  962. StringRef Category = StringRef()) const;
  963. SanitizerMetadata *getSanitizerMetadata() {
  964. return SanitizerMD.get();
  965. }
  966. void addDeferredVTable(const CXXRecordDecl *RD) {
  967. DeferredVTables.push_back(RD);
  968. }
  969. /// Emit code for a single global function or var decl. Forward declarations
  970. /// are emitted lazily.
  971. void EmitGlobal(GlobalDecl D);
  972. bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
  973. llvm::GlobalValue *GetGlobalValue(StringRef Ref);
  974. /// Set attributes which are common to any form of a global definition (alias,
  975. /// Objective-C method, function, global variable).
  976. ///
  977. /// NOTE: This should only be called for definitions.
  978. void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV);
  979. void addReplacement(StringRef Name, llvm::Constant *C);
  980. void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C);
  981. /// Emit a code for threadprivate directive.
  982. /// \param D Threadprivate declaration.
  983. void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
  984. /// Emit a code for declare reduction construct.
  985. void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
  986. CodeGenFunction *CGF = nullptr);
  987. /// Emit a code for declare mapper construct.
  988. void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
  989. CodeGenFunction *CGF = nullptr);
  990. /// Emit a code for requires directive.
  991. /// \param D Requires declaration
  992. void EmitOMPRequiresDecl(const OMPRequiresDecl *D);
  993. /// Returns whether the given record has hidden LTO visibility and therefore
  994. /// may participate in (single-module) CFI and whole-program vtable
  995. /// optimization.
  996. bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
  997. /// Emit type metadata for the given vtable using the given layout.
  998. void EmitVTableTypeMetadata(llvm::GlobalVariable *VTable,
  999. const VTableLayout &VTLayout);
  1000. /// Generate a cross-DSO type identifier for MD.
  1001. llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
  1002. /// Create a metadata identifier for the given type. This may either be an
  1003. /// MDString (for external identifiers) or a distinct unnamed MDNode (for
  1004. /// internal identifiers).
  1005. llvm::Metadata *CreateMetadataIdentifierForType(QualType T);
  1006. /// Create a metadata identifier that is intended to be used to check virtual
  1007. /// calls via a member function pointer.
  1008. llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T);
  1009. /// Create a metadata identifier for the generalization of the given type.
  1010. /// This may either be an MDString (for external identifiers) or a distinct
  1011. /// unnamed MDNode (for internal identifiers).
  1012. llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
  1013. /// Create and attach type metadata to the given function.
  1014. void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
  1015. llvm::Function *F);
  1016. /// Returns whether this module needs the "all-vtables" type identifier.
  1017. bool NeedAllVtablesTypeId() const;
  1018. /// Create and attach type metadata for the given vtable.
  1019. void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset,
  1020. const CXXRecordDecl *RD);
  1021. /// Return a vector of most-base classes for RD. This is used to implement
  1022. /// control flow integrity checks for member function pointers.
  1023. ///
  1024. /// A most-base class of a class C is defined as a recursive base class of C,
  1025. /// including C itself, that does not have any bases.
  1026. std::vector<const CXXRecordDecl *>
  1027. getMostBaseClasses(const CXXRecordDecl *RD);
  1028. /// Get the declaration of std::terminate for the platform.
  1029. llvm::FunctionCallee getTerminateFn();
  1030. llvm::SanitizerStatReport &getSanStats();
  1031. llvm::Value *
  1032. createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF);
  1033. /// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
  1034. /// information in the program executable. The argument information stored
  1035. /// includes the argument name, its type, the address and access qualifiers
  1036. /// used. This helper can be used to generate metadata for source code kernel
  1037. /// function as well as generated implicitly kernels. If a kernel is generated
  1038. /// implicitly null value has to be passed to the last two parameters,
  1039. /// otherwise all parameters must have valid non-null values.
  1040. /// \param FN is a pointer to IR function being generated.
  1041. /// \param FD is a pointer to function declaration if any.
  1042. /// \param CGF is a pointer to CodeGenFunction that generates this function.
  1043. void GenOpenCLArgMetadata(llvm::Function *FN,
  1044. const FunctionDecl *FD = nullptr,
  1045. CodeGenFunction *CGF = nullptr);
  1046. /// Get target specific null pointer.
  1047. /// \param T is the LLVM type of the null pointer.
  1048. /// \param QT is the clang QualType of the null pointer.
  1049. llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
  1050. private:
  1051. llvm::Constant *GetOrCreateLLVMFunction(
  1052. StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
  1053. bool DontDefer = false, bool IsThunk = false,
  1054. llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
  1055. ForDefinition_t IsForDefinition = NotForDefinition);
  1056. llvm::Constant *GetOrCreateMultiVersionResolver(GlobalDecl GD,
  1057. llvm::Type *DeclTy,
  1058. const FunctionDecl *FD);
  1059. void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD);
  1060. llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
  1061. llvm::PointerType *PTy,
  1062. const VarDecl *D,
  1063. ForDefinition_t IsForDefinition
  1064. = NotForDefinition);
  1065. bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
  1066. llvm::AttrBuilder &AttrBuilder);
  1067. void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
  1068. /// Set function attributes for a function declaration.
  1069. void SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
  1070. bool IsIncompleteFunction, bool IsThunk);
  1071. void EmitGlobalDefinition(GlobalDecl D, llvm::GlobalValue *GV = nullptr);
  1072. void EmitGlobalFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
  1073. void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
  1074. void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
  1075. void EmitAliasDefinition(GlobalDecl GD);
  1076. void emitIFuncDefinition(GlobalDecl GD);
  1077. void emitCPUDispatchDefinition(GlobalDecl GD);
  1078. void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
  1079. void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
  1080. // C++ related functions.
  1081. void EmitDeclContext(const DeclContext *DC);
  1082. void EmitLinkageSpec(const LinkageSpecDecl *D);
  1083. /// Emit the function that initializes C++ thread_local variables.
  1084. void EmitCXXThreadLocalInitFunc();
  1085. /// Emit the function that initializes C++ globals.
  1086. void EmitCXXGlobalInitFunc();
  1087. /// Emit the function that destroys C++ globals.
  1088. void EmitCXXGlobalDtorFunc();
  1089. /// Emit the function that initializes the specified global (if PerformInit is
  1090. /// true) and registers its destructor.
  1091. void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
  1092. llvm::GlobalVariable *Addr,
  1093. bool PerformInit);
  1094. void EmitPointerToInitFunc(const VarDecl *VD, llvm::GlobalVariable *Addr,
  1095. llvm::Function *InitFunc, InitSegAttr *ISA);
  1096. // FIXME: Hardcoding priority here is gross.
  1097. void AddGlobalCtor(llvm::Function *Ctor, int Priority = 65535,
  1098. llvm::Constant *AssociatedData = nullptr);
  1099. void AddGlobalDtor(llvm::Function *Dtor, int Priority = 65535);
  1100. /// EmitCtorList - Generates a global array of functions and priorities using
  1101. /// the given list and name. This array will have appending linkage and is
  1102. /// suitable for use as a LLVM constructor or destructor array. Clears Fns.
  1103. void EmitCtorList(CtorList &Fns, const char *GlobalName);
  1104. /// Emit any needed decls for which code generation was deferred.
  1105. void EmitDeferred();
  1106. /// Try to emit external vtables as available_externally if they have emitted
  1107. /// all inlined virtual functions. It runs after EmitDeferred() and therefore
  1108. /// is not allowed to create new references to things that need to be emitted
  1109. /// lazily.
  1110. void EmitVTablesOpportunistically();
  1111. /// Call replaceAllUsesWith on all pairs in Replacements.
  1112. void applyReplacements();
  1113. /// Call replaceAllUsesWith on all pairs in GlobalValReplacements.
  1114. void applyGlobalValReplacements();
  1115. void checkAliases();
  1116. std::map<int, llvm::TinyPtrVector<llvm::Function *>> DtorsUsingAtExit;
  1117. /// Register functions annotated with __attribute__((destructor)) using
  1118. /// __cxa_atexit, if it is available, or atexit otherwise.
  1119. void registerGlobalDtorsWithAtExit();
  1120. void emitMultiVersionFunctions();
  1121. /// Emit any vtables which we deferred and still have a use for.
  1122. void EmitDeferredVTables();
  1123. /// Emit a dummy function that reference a CoreFoundation symbol when
  1124. /// @available is used on Darwin.
  1125. void emitAtAvailableLinkGuard();
  1126. /// Emit the llvm.used and llvm.compiler.used metadata.
  1127. void emitLLVMUsed();
  1128. /// Emit the link options introduced by imported modules.
  1129. void EmitModuleLinkOptions();
  1130. /// Emit aliases for internal-linkage declarations inside "C" language
  1131. /// linkage specifications, giving them the "expected" name where possible.
  1132. void EmitStaticExternCAliases();
  1133. void EmitDeclMetadata();
  1134. /// Emit the Clang version as llvm.ident metadata.
  1135. void EmitVersionIdentMetadata();
  1136. /// Emit the Clang commandline as llvm.commandline metadata.
  1137. void EmitCommandLineMetadata();
  1138. /// Emits target specific Metadata for global declarations.
  1139. void EmitTargetMetadata();
  1140. /// Emits OpenCL specific Metadata e.g. OpenCL version.
  1141. void EmitOpenCLMetadata();
  1142. /// Emit the llvm.gcov metadata used to tell LLVM where to emit the .gcno and
  1143. /// .gcda files in a way that persists in .bc files.
  1144. void EmitCoverageFile();
  1145. /// Emits the initializer for a uuidof string.
  1146. llvm::Constant *EmitUuidofInitializer(StringRef uuidstr);
  1147. /// Determine whether the definition must be emitted; if this returns \c
  1148. /// false, the definition can be emitted lazily if it's used.
  1149. bool MustBeEmitted(const ValueDecl *D);
  1150. /// Determine whether the definition can be emitted eagerly, or should be
  1151. /// delayed until the end of the translation unit. This is relevant for
  1152. /// definitions whose linkage can change, e.g. implicit function instantions
  1153. /// which may later be explicitly instantiated.
  1154. bool MayBeEmittedEagerly(const ValueDecl *D);
  1155. /// Check whether we can use a "simpler", more core exceptions personality
  1156. /// function.
  1157. void SimplifyPersonality();
  1158. /// Helper function for ConstructAttributeList and AddDefaultFnAttrs.
  1159. /// Constructs an AttrList for a function with the given properties.
  1160. void ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
  1161. bool AttrOnCallSite,
  1162. llvm::AttrBuilder &FuncAttrs);
  1163. llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
  1164. StringRef Suffix);
  1165. };
  1166. } // end namespace CodeGen
  1167. } // end namespace clang
  1168. #endif // LLVM_CLANG_LIB_CODEGEN_CODEGENMODULE_H