DIBuilder.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
  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 file implements the DIBuilder.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/DIBuilder.h"
  13. #include "llvm/IR/IRBuilder.h"
  14. #include "LLVMContextImpl.h"
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/BinaryFormat/Dwarf.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/DebugInfo.h"
  20. #include "llvm/IR/IntrinsicInst.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/Support/Debug.h"
  23. using namespace llvm;
  24. using namespace llvm::dwarf;
  25. static cl::opt<bool>
  26. UseDbgAddr("use-dbg-addr",
  27. llvm::cl::desc("Use llvm.dbg.addr for all local variables"),
  28. cl::init(false), cl::Hidden);
  29. DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes, DICompileUnit *CU)
  30. : M(m), VMContext(M.getContext()), CUNode(CU),
  31. DeclareFn(nullptr), ValueFn(nullptr), LabelFn(nullptr),
  32. AllowUnresolvedNodes(AllowUnresolvedNodes) {}
  33. void DIBuilder::trackIfUnresolved(MDNode *N) {
  34. if (!N)
  35. return;
  36. if (N->isResolved())
  37. return;
  38. assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes");
  39. UnresolvedNodes.emplace_back(N);
  40. }
  41. void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
  42. MDTuple *Temp = SP->getRetainedNodes().get();
  43. if (!Temp || !Temp->isTemporary())
  44. return;
  45. SmallVector<Metadata *, 16> RetainedNodes;
  46. auto PV = PreservedVariables.find(SP);
  47. if (PV != PreservedVariables.end())
  48. RetainedNodes.append(PV->second.begin(), PV->second.end());
  49. auto PL = PreservedLabels.find(SP);
  50. if (PL != PreservedLabels.end())
  51. RetainedNodes.append(PL->second.begin(), PL->second.end());
  52. DINodeArray Node = getOrCreateArray(RetainedNodes);
  53. TempMDTuple(Temp)->replaceAllUsesWith(Node.get());
  54. }
  55. void DIBuilder::finalize() {
  56. if (!CUNode) {
  57. assert(!AllowUnresolvedNodes &&
  58. "creating type nodes without a CU is not supported");
  59. return;
  60. }
  61. CUNode->replaceEnumTypes(MDTuple::get(VMContext, AllEnumTypes));
  62. SmallVector<Metadata *, 16> RetainValues;
  63. // Declarations and definitions of the same type may be retained. Some
  64. // clients RAUW these pairs, leaving duplicates in the retained types
  65. // list. Use a set to remove the duplicates while we transform the
  66. // TrackingVHs back into Values.
  67. SmallPtrSet<Metadata *, 16> RetainSet;
  68. for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
  69. if (RetainSet.insert(AllRetainTypes[I]).second)
  70. RetainValues.push_back(AllRetainTypes[I]);
  71. if (!RetainValues.empty())
  72. CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));
  73. DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
  74. for (auto *SP : SPs)
  75. finalizeSubprogram(SP);
  76. for (auto *N : RetainValues)
  77. if (auto *SP = dyn_cast<DISubprogram>(N))
  78. finalizeSubprogram(SP);
  79. if (!AllGVs.empty())
  80. CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
  81. if (!AllImportedModules.empty())
  82. CUNode->replaceImportedEntities(MDTuple::get(
  83. VMContext, SmallVector<Metadata *, 16>(AllImportedModules.begin(),
  84. AllImportedModules.end())));
  85. for (const auto &I : AllMacrosPerParent) {
  86. // DIMacroNode's with nullptr parent are DICompileUnit direct children.
  87. if (!I.first) {
  88. CUNode->replaceMacros(MDTuple::get(VMContext, I.second.getArrayRef()));
  89. continue;
  90. }
  91. // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
  92. auto *TMF = cast<DIMacroFile>(I.first);
  93. auto *MF = DIMacroFile::get(VMContext, dwarf::DW_MACINFO_start_file,
  94. TMF->getLine(), TMF->getFile(),
  95. getOrCreateMacroArray(I.second.getArrayRef()));
  96. replaceTemporary(llvm::TempDIMacroNode(TMF), MF);
  97. }
  98. // Now that all temp nodes have been replaced or deleted, resolve remaining
  99. // cycles.
  100. for (const auto &N : UnresolvedNodes)
  101. if (N && !N->isResolved())
  102. N->resolveCycles();
  103. UnresolvedNodes.clear();
  104. // Can't handle unresolved nodes anymore.
  105. AllowUnresolvedNodes = false;
  106. }
  107. /// If N is compile unit return NULL otherwise return N.
  108. static DIScope *getNonCompileUnitScope(DIScope *N) {
  109. if (!N || isa<DICompileUnit>(N))
  110. return nullptr;
  111. return cast<DIScope>(N);
  112. }
  113. DICompileUnit *DIBuilder::createCompileUnit(
  114. unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized,
  115. StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
  116. DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId,
  117. bool SplitDebugInlining, bool DebugInfoForProfiling,
  118. DICompileUnit::DebugNameTableKind NameTableKind, bool RangesBaseAddress) {
  119. assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
  120. (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
  121. "Invalid Language tag");
  122. assert(!CUNode && "Can only make one compile unit per DIBuilder instance");
  123. CUNode = DICompileUnit::getDistinct(
  124. VMContext, Lang, File, Producer, isOptimized, Flags, RunTimeVer,
  125. SplitName, Kind, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId,
  126. SplitDebugInlining, DebugInfoForProfiling, NameTableKind,
  127. RangesBaseAddress);
  128. // Create a named metadata so that it is easier to find cu in a module.
  129. NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
  130. NMD->addOperand(CUNode);
  131. trackIfUnresolved(CUNode);
  132. return CUNode;
  133. }
  134. static DIImportedEntity *
  135. createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
  136. Metadata *NS, DIFile *File, unsigned Line, StringRef Name,
  137. SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
  138. if (Line)
  139. assert(File && "Source location has line number but no file");
  140. unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
  141. auto *M = DIImportedEntity::get(C, Tag, Context, cast_or_null<DINode>(NS),
  142. File, Line, Name);
  143. if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
  144. // A new Imported Entity was just added to the context.
  145. // Add it to the Imported Modules list.
  146. AllImportedModules.emplace_back(M);
  147. return M;
  148. }
  149. DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
  150. DINamespace *NS, DIFile *File,
  151. unsigned Line) {
  152. return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
  153. Context, NS, File, Line, StringRef(),
  154. AllImportedModules);
  155. }
  156. DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
  157. DIImportedEntity *NS,
  158. DIFile *File, unsigned Line) {
  159. return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
  160. Context, NS, File, Line, StringRef(),
  161. AllImportedModules);
  162. }
  163. DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
  164. DIFile *File, unsigned Line) {
  165. return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
  166. Context, M, File, Line, StringRef(),
  167. AllImportedModules);
  168. }
  169. DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
  170. DINode *Decl,
  171. DIFile *File,
  172. unsigned Line,
  173. StringRef Name) {
  174. // Make sure to use the unique identifier based metadata reference for
  175. // types that have one.
  176. return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
  177. Context, Decl, File, Line, Name,
  178. AllImportedModules);
  179. }
  180. DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
  181. Optional<DIFile::ChecksumInfo<StringRef>> CS,
  182. Optional<StringRef> Source) {
  183. return DIFile::get(VMContext, Filename, Directory, CS, Source);
  184. }
  185. DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
  186. unsigned MacroType, StringRef Name,
  187. StringRef Value) {
  188. assert(!Name.empty() && "Unable to create macro without name");
  189. assert((MacroType == dwarf::DW_MACINFO_undef ||
  190. MacroType == dwarf::DW_MACINFO_define) &&
  191. "Unexpected macro type");
  192. auto *M = DIMacro::get(VMContext, MacroType, LineNumber, Name, Value);
  193. AllMacrosPerParent[Parent].insert(M);
  194. return M;
  195. }
  196. DIMacroFile *DIBuilder::createTempMacroFile(DIMacroFile *Parent,
  197. unsigned LineNumber, DIFile *File) {
  198. auto *MF = DIMacroFile::getTemporary(VMContext, dwarf::DW_MACINFO_start_file,
  199. LineNumber, File, DIMacroNodeArray())
  200. .release();
  201. AllMacrosPerParent[Parent].insert(MF);
  202. // Add the new temporary DIMacroFile to the macro per parent map as a parent.
  203. // This is needed to assure DIMacroFile with no children to have an entry in
  204. // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
  205. AllMacrosPerParent.insert({MF, {}});
  206. return MF;
  207. }
  208. DIEnumerator *DIBuilder::createEnumerator(StringRef Name, int64_t Val,
  209. bool IsUnsigned) {
  210. assert(!Name.empty() && "Unable to create enumerator without name");
  211. return DIEnumerator::get(VMContext, Val, IsUnsigned, Name);
  212. }
  213. DIBasicType *DIBuilder::createUnspecifiedType(StringRef Name) {
  214. assert(!Name.empty() && "Unable to create type without name");
  215. return DIBasicType::get(VMContext, dwarf::DW_TAG_unspecified_type, Name);
  216. }
  217. DIBasicType *DIBuilder::createNullPtrType() {
  218. return createUnspecifiedType("decltype(nullptr)");
  219. }
  220. DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
  221. unsigned Encoding,
  222. DINode::DIFlags Flags) {
  223. assert(!Name.empty() && "Unable to create type without name");
  224. return DIBasicType::get(VMContext, dwarf::DW_TAG_base_type, Name, SizeInBits,
  225. 0, Encoding, Flags);
  226. }
  227. DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
  228. return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
  229. 0, 0, None, DINode::FlagZero);
  230. }
  231. DIDerivedType *DIBuilder::createPointerType(
  232. DIType *PointeeTy,
  233. uint64_t SizeInBits,
  234. uint32_t AlignInBits,
  235. Optional<unsigned> DWARFAddressSpace,
  236. StringRef Name) {
  237. // FIXME: Why is there a name here?
  238. return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
  239. nullptr, 0, nullptr, PointeeTy, SizeInBits,
  240. AlignInBits, 0, DWARFAddressSpace,
  241. DINode::FlagZero);
  242. }
  243. DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
  244. DIType *Base,
  245. uint64_t SizeInBits,
  246. uint32_t AlignInBits,
  247. DINode::DIFlags Flags) {
  248. return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
  249. nullptr, 0, nullptr, PointeeTy, SizeInBits,
  250. AlignInBits, 0, None, Flags, Base);
  251. }
  252. DIDerivedType *DIBuilder::createReferenceType(
  253. unsigned Tag, DIType *RTy,
  254. uint64_t SizeInBits,
  255. uint32_t AlignInBits,
  256. Optional<unsigned> DWARFAddressSpace) {
  257. assert(RTy && "Unable to create reference type");
  258. return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
  259. SizeInBits, AlignInBits, 0, DWARFAddressSpace,
  260. DINode::FlagZero);
  261. }
  262. DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
  263. DIFile *File, unsigned LineNo,
  264. DIScope *Context) {
  265. return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
  266. LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
  267. 0, None, DINode::FlagZero);
  268. }
  269. DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
  270. assert(Ty && "Invalid type!");
  271. assert(FriendTy && "Invalid friend type!");
  272. return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
  273. FriendTy, 0, 0, 0, None, DINode::FlagZero);
  274. }
  275. DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
  276. uint64_t BaseOffset,
  277. uint32_t VBPtrOffset,
  278. DINode::DIFlags Flags) {
  279. assert(Ty && "Unable to create inheritance");
  280. Metadata *ExtraData = ConstantAsMetadata::get(
  281. ConstantInt::get(IntegerType::get(VMContext, 32), VBPtrOffset));
  282. return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
  283. 0, Ty, BaseTy, 0, 0, BaseOffset, None,
  284. Flags, ExtraData);
  285. }
  286. DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
  287. DIFile *File, unsigned LineNumber,
  288. uint64_t SizeInBits,
  289. uint32_t AlignInBits,
  290. uint64_t OffsetInBits,
  291. DINode::DIFlags Flags, DIType *Ty) {
  292. return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
  293. LineNumber, getNonCompileUnitScope(Scope), Ty,
  294. SizeInBits, AlignInBits, OffsetInBits, None, Flags);
  295. }
  296. static ConstantAsMetadata *getConstantOrNull(Constant *C) {
  297. if (C)
  298. return ConstantAsMetadata::get(C);
  299. return nullptr;
  300. }
  301. DIDerivedType *DIBuilder::createVariantMemberType(
  302. DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
  303. uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
  304. Constant *Discriminant, DINode::DIFlags Flags, DIType *Ty) {
  305. return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
  306. LineNumber, getNonCompileUnitScope(Scope), Ty,
  307. SizeInBits, AlignInBits, OffsetInBits, None, Flags,
  308. getConstantOrNull(Discriminant));
  309. }
  310. DIDerivedType *DIBuilder::createBitFieldMemberType(
  311. DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
  312. uint64_t SizeInBits, uint64_t OffsetInBits, uint64_t StorageOffsetInBits,
  313. DINode::DIFlags Flags, DIType *Ty) {
  314. Flags |= DINode::FlagBitField;
  315. return DIDerivedType::get(
  316. VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
  317. getNonCompileUnitScope(Scope), Ty, SizeInBits, /* AlignInBits */ 0,
  318. OffsetInBits, None, Flags,
  319. ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64),
  320. StorageOffsetInBits)));
  321. }
  322. DIDerivedType *
  323. DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
  324. unsigned LineNumber, DIType *Ty,
  325. DINode::DIFlags Flags, llvm::Constant *Val,
  326. uint32_t AlignInBits) {
  327. Flags |= DINode::FlagStaticMember;
  328. return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
  329. LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
  330. AlignInBits, 0, None, Flags,
  331. getConstantOrNull(Val));
  332. }
  333. DIDerivedType *
  334. DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
  335. uint64_t SizeInBits, uint32_t AlignInBits,
  336. uint64_t OffsetInBits, DINode::DIFlags Flags,
  337. DIType *Ty, MDNode *PropertyNode) {
  338. return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
  339. LineNumber, getNonCompileUnitScope(File), Ty,
  340. SizeInBits, AlignInBits, OffsetInBits, None, Flags,
  341. PropertyNode);
  342. }
  343. DIObjCProperty *
  344. DIBuilder::createObjCProperty(StringRef Name, DIFile *File, unsigned LineNumber,
  345. StringRef GetterName, StringRef SetterName,
  346. unsigned PropertyAttributes, DIType *Ty) {
  347. return DIObjCProperty::get(VMContext, Name, File, LineNumber, GetterName,
  348. SetterName, PropertyAttributes, Ty);
  349. }
  350. DITemplateTypeParameter *
  351. DIBuilder::createTemplateTypeParameter(DIScope *Context, StringRef Name,
  352. DIType *Ty) {
  353. assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
  354. return DITemplateTypeParameter::get(VMContext, Name, Ty);
  355. }
  356. static DITemplateValueParameter *
  357. createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag,
  358. DIScope *Context, StringRef Name, DIType *Ty,
  359. Metadata *MD) {
  360. assert((!Context || isa<DICompileUnit>(Context)) && "Expected compile unit");
  361. return DITemplateValueParameter::get(VMContext, Tag, Name, Ty, MD);
  362. }
  363. DITemplateValueParameter *
  364. DIBuilder::createTemplateValueParameter(DIScope *Context, StringRef Name,
  365. DIType *Ty, Constant *Val) {
  366. return createTemplateValueParameterHelper(
  367. VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty,
  368. getConstantOrNull(Val));
  369. }
  370. DITemplateValueParameter *
  371. DIBuilder::createTemplateTemplateParameter(DIScope *Context, StringRef Name,
  372. DIType *Ty, StringRef Val) {
  373. return createTemplateValueParameterHelper(
  374. VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
  375. MDString::get(VMContext, Val));
  376. }
  377. DITemplateValueParameter *
  378. DIBuilder::createTemplateParameterPack(DIScope *Context, StringRef Name,
  379. DIType *Ty, DINodeArray Val) {
  380. return createTemplateValueParameterHelper(
  381. VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
  382. Val.get());
  383. }
  384. DICompositeType *DIBuilder::createClassType(
  385. DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
  386. uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
  387. DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
  388. DIType *VTableHolder, MDNode *TemplateParams, StringRef UniqueIdentifier) {
  389. assert((!Context || isa<DIScope>(Context)) &&
  390. "createClassType should be called with a valid Context");
  391. auto *R = DICompositeType::get(
  392. VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
  393. getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits,
  394. OffsetInBits, Flags, Elements, 0, VTableHolder,
  395. cast_or_null<MDTuple>(TemplateParams), UniqueIdentifier);
  396. trackIfUnresolved(R);
  397. return R;
  398. }
  399. DICompositeType *DIBuilder::createStructType(
  400. DIScope *Context, StringRef Name, DIFile *File, unsigned LineNumber,
  401. uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
  402. DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
  403. DIType *VTableHolder, StringRef UniqueIdentifier) {
  404. auto *R = DICompositeType::get(
  405. VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
  406. getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
  407. Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier);
  408. trackIfUnresolved(R);
  409. return R;
  410. }
  411. DICompositeType *DIBuilder::createUnionType(
  412. DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
  413. uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
  414. DINodeArray Elements, unsigned RunTimeLang, StringRef UniqueIdentifier) {
  415. auto *R = DICompositeType::get(
  416. VMContext, dwarf::DW_TAG_union_type, Name, File, LineNumber,
  417. getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
  418. Elements, RunTimeLang, nullptr, nullptr, UniqueIdentifier);
  419. trackIfUnresolved(R);
  420. return R;
  421. }
  422. DICompositeType *DIBuilder::createVariantPart(
  423. DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
  424. uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
  425. DIDerivedType *Discriminator, DINodeArray Elements, StringRef UniqueIdentifier) {
  426. auto *R = DICompositeType::get(
  427. VMContext, dwarf::DW_TAG_variant_part, Name, File, LineNumber,
  428. getNonCompileUnitScope(Scope), nullptr, SizeInBits, AlignInBits, 0, Flags,
  429. Elements, 0, nullptr, nullptr, UniqueIdentifier, Discriminator);
  430. trackIfUnresolved(R);
  431. return R;
  432. }
  433. DISubroutineType *DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes,
  434. DINode::DIFlags Flags,
  435. unsigned CC) {
  436. return DISubroutineType::get(VMContext, Flags, CC, ParameterTypes);
  437. }
  438. DICompositeType *DIBuilder::createEnumerationType(
  439. DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
  440. uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
  441. DIType *UnderlyingType, StringRef UniqueIdentifier, bool IsScoped) {
  442. auto *CTy = DICompositeType::get(
  443. VMContext, dwarf::DW_TAG_enumeration_type, Name, File, LineNumber,
  444. getNonCompileUnitScope(Scope), UnderlyingType, SizeInBits, AlignInBits, 0,
  445. IsScoped ? DINode::FlagEnumClass : DINode::FlagZero, Elements, 0, nullptr,
  446. nullptr, UniqueIdentifier);
  447. AllEnumTypes.push_back(CTy);
  448. trackIfUnresolved(CTy);
  449. return CTy;
  450. }
  451. DICompositeType *DIBuilder::createArrayType(uint64_t Size,
  452. uint32_t AlignInBits, DIType *Ty,
  453. DINodeArray Subscripts) {
  454. auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
  455. nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
  456. DINode::FlagZero, Subscripts, 0, nullptr);
  457. trackIfUnresolved(R);
  458. return R;
  459. }
  460. DICompositeType *DIBuilder::createVectorType(uint64_t Size,
  461. uint32_t AlignInBits, DIType *Ty,
  462. DINodeArray Subscripts) {
  463. auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
  464. nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
  465. DINode::FlagVector, Subscripts, 0, nullptr);
  466. trackIfUnresolved(R);
  467. return R;
  468. }
  469. DISubprogram *DIBuilder::createArtificialSubprogram(DISubprogram *SP) {
  470. auto NewSP = SP->cloneWithFlags(SP->getFlags() | DINode::FlagArtificial);
  471. return MDNode::replaceWithDistinct(std::move(NewSP));
  472. }
  473. static DIType *createTypeWithFlags(const DIType *Ty,
  474. DINode::DIFlags FlagsToSet) {
  475. auto NewTy = Ty->cloneWithFlags(Ty->getFlags() | FlagsToSet);
  476. return MDNode::replaceWithUniqued(std::move(NewTy));
  477. }
  478. DIType *DIBuilder::createArtificialType(DIType *Ty) {
  479. // FIXME: Restrict this to the nodes where it's valid.
  480. if (Ty->isArtificial())
  481. return Ty;
  482. return createTypeWithFlags(Ty, DINode::FlagArtificial);
  483. }
  484. DIType *DIBuilder::createObjectPointerType(DIType *Ty) {
  485. // FIXME: Restrict this to the nodes where it's valid.
  486. if (Ty->isObjectPointer())
  487. return Ty;
  488. DINode::DIFlags Flags = DINode::FlagObjectPointer | DINode::FlagArtificial;
  489. return createTypeWithFlags(Ty, Flags);
  490. }
  491. void DIBuilder::retainType(DIScope *T) {
  492. assert(T && "Expected non-null type");
  493. assert((isa<DIType>(T) || (isa<DISubprogram>(T) &&
  494. cast<DISubprogram>(T)->isDefinition() == false)) &&
  495. "Expected type or subprogram declaration");
  496. AllRetainTypes.emplace_back(T);
  497. }
  498. DIBasicType *DIBuilder::createUnspecifiedParameter() { return nullptr; }
  499. DICompositeType *
  500. DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIScope *Scope,
  501. DIFile *F, unsigned Line, unsigned RuntimeLang,
  502. uint64_t SizeInBits, uint32_t AlignInBits,
  503. StringRef UniqueIdentifier) {
  504. // FIXME: Define in terms of createReplaceableForwardDecl() by calling
  505. // replaceWithUniqued().
  506. auto *RetTy = DICompositeType::get(
  507. VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
  508. SizeInBits, AlignInBits, 0, DINode::FlagFwdDecl, nullptr, RuntimeLang,
  509. nullptr, nullptr, UniqueIdentifier);
  510. trackIfUnresolved(RetTy);
  511. return RetTy;
  512. }
  513. DICompositeType *DIBuilder::createReplaceableCompositeType(
  514. unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
  515. unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
  516. DINode::DIFlags Flags, StringRef UniqueIdentifier) {
  517. auto *RetTy =
  518. DICompositeType::getTemporary(
  519. VMContext, Tag, Name, F, Line, getNonCompileUnitScope(Scope), nullptr,
  520. SizeInBits, AlignInBits, 0, Flags, nullptr, RuntimeLang, nullptr,
  521. nullptr, UniqueIdentifier)
  522. .release();
  523. trackIfUnresolved(RetTy);
  524. return RetTy;
  525. }
  526. DINodeArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) {
  527. return MDTuple::get(VMContext, Elements);
  528. }
  529. DIMacroNodeArray
  530. DIBuilder::getOrCreateMacroArray(ArrayRef<Metadata *> Elements) {
  531. return MDTuple::get(VMContext, Elements);
  532. }
  533. DITypeRefArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) {
  534. SmallVector<llvm::Metadata *, 16> Elts;
  535. for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
  536. if (Elements[i] && isa<MDNode>(Elements[i]))
  537. Elts.push_back(cast<DIType>(Elements[i]));
  538. else
  539. Elts.push_back(Elements[i]);
  540. }
  541. return DITypeRefArray(MDNode::get(VMContext, Elts));
  542. }
  543. DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
  544. return DISubrange::get(VMContext, Count, Lo);
  545. }
  546. DISubrange *DIBuilder::getOrCreateSubrange(int64_t Lo, Metadata *CountNode) {
  547. return DISubrange::get(VMContext, CountNode, Lo);
  548. }
  549. static void checkGlobalVariableScope(DIScope *Context) {
  550. #ifndef NDEBUG
  551. if (auto *CT =
  552. dyn_cast_or_null<DICompositeType>(getNonCompileUnitScope(Context)))
  553. assert(CT->getIdentifier().empty() &&
  554. "Context of a global variable should not be a type with identifier");
  555. #endif
  556. }
  557. DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
  558. DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
  559. unsigned LineNumber, DIType *Ty, bool isLocalToUnit, DIExpression *Expr,
  560. MDNode *Decl, MDTuple *templateParams, uint32_t AlignInBits) {
  561. checkGlobalVariableScope(Context);
  562. auto *GV = DIGlobalVariable::getDistinct(
  563. VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
  564. LineNumber, Ty, isLocalToUnit, true, cast_or_null<DIDerivedType>(Decl),
  565. templateParams, AlignInBits);
  566. if (!Expr)
  567. Expr = createExpression();
  568. auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr);
  569. AllGVs.push_back(N);
  570. return N;
  571. }
  572. DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
  573. DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
  574. unsigned LineNumber, DIType *Ty, bool isLocalToUnit, MDNode *Decl,
  575. MDTuple *templateParams, uint32_t AlignInBits) {
  576. checkGlobalVariableScope(Context);
  577. return DIGlobalVariable::getTemporary(
  578. VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
  579. LineNumber, Ty, isLocalToUnit, false,
  580. cast_or_null<DIDerivedType>(Decl), templateParams, AlignInBits)
  581. .release();
  582. }
  583. static DILocalVariable *createLocalVariable(
  584. LLVMContext &VMContext,
  585. DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> &PreservedVariables,
  586. DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
  587. unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
  588. uint32_t AlignInBits) {
  589. // FIXME: Why getNonCompileUnitScope()?
  590. // FIXME: Why is "!Context" okay here?
  591. // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
  592. // the only valid scopes)?
  593. DIScope *Context = getNonCompileUnitScope(Scope);
  594. auto *Node =
  595. DILocalVariable::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
  596. File, LineNo, Ty, ArgNo, Flags, AlignInBits);
  597. if (AlwaysPreserve) {
  598. // The optimizer may remove local variables. If there is an interest
  599. // to preserve variable info in such situation then stash it in a
  600. // named mdnode.
  601. DISubprogram *Fn = getDISubprogram(Scope);
  602. assert(Fn && "Missing subprogram for local variable");
  603. PreservedVariables[Fn].emplace_back(Node);
  604. }
  605. return Node;
  606. }
  607. DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
  608. DIFile *File, unsigned LineNo,
  609. DIType *Ty, bool AlwaysPreserve,
  610. DINode::DIFlags Flags,
  611. uint32_t AlignInBits) {
  612. return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
  613. /* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
  614. Flags, AlignInBits);
  615. }
  616. DILocalVariable *DIBuilder::createParameterVariable(
  617. DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
  618. unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags) {
  619. assert(ArgNo && "Expected non-zero argument number for parameter");
  620. return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
  621. File, LineNo, Ty, AlwaysPreserve, Flags,
  622. /* AlignInBits */0);
  623. }
  624. DILabel *DIBuilder::createLabel(
  625. DIScope *Scope, StringRef Name, DIFile *File,
  626. unsigned LineNo, bool AlwaysPreserve) {
  627. DIScope *Context = getNonCompileUnitScope(Scope);
  628. auto *Node =
  629. DILabel::get(VMContext, cast_or_null<DILocalScope>(Context), Name,
  630. File, LineNo);
  631. if (AlwaysPreserve) {
  632. /// The optimizer may remove labels. If there is an interest
  633. /// to preserve label info in such situation then append it to
  634. /// the list of retained nodes of the DISubprogram.
  635. DISubprogram *Fn = getDISubprogram(Scope);
  636. assert(Fn && "Missing subprogram for label");
  637. PreservedLabels[Fn].emplace_back(Node);
  638. }
  639. return Node;
  640. }
  641. DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
  642. return DIExpression::get(VMContext, Addr);
  643. }
  644. DIExpression *DIBuilder::createExpression(ArrayRef<int64_t> Signed) {
  645. // TODO: Remove the callers of this signed version and delete.
  646. SmallVector<uint64_t, 8> Addr(Signed.begin(), Signed.end());
  647. return createExpression(Addr);
  648. }
  649. template <class... Ts>
  650. static DISubprogram *getSubprogram(bool IsDistinct, Ts &&... Args) {
  651. if (IsDistinct)
  652. return DISubprogram::getDistinct(std::forward<Ts>(Args)...);
  653. return DISubprogram::get(std::forward<Ts>(Args)...);
  654. }
  655. DISubprogram *DIBuilder::createFunction(
  656. DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
  657. unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
  658. DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
  659. DITemplateParameterArray TParams, DISubprogram *Decl,
  660. DITypeArray ThrownTypes) {
  661. bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
  662. auto *Node = getSubprogram(
  663. /*IsDistinct=*/IsDefinition, VMContext, getNonCompileUnitScope(Context),
  664. Name, LinkageName, File, LineNo, Ty, ScopeLine, nullptr, 0, 0, Flags,
  665. SPFlags, IsDefinition ? CUNode : nullptr, TParams, Decl,
  666. MDTuple::getTemporary(VMContext, None).release(), ThrownTypes);
  667. if (IsDefinition)
  668. AllSubprograms.push_back(Node);
  669. trackIfUnresolved(Node);
  670. return Node;
  671. }
  672. DISubprogram *DIBuilder::createTempFunctionFwdDecl(
  673. DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
  674. unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
  675. DINode::DIFlags Flags, DISubprogram::DISPFlags SPFlags,
  676. DITemplateParameterArray TParams, DISubprogram *Decl,
  677. DITypeArray ThrownTypes) {
  678. bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
  679. return DISubprogram::getTemporary(VMContext, getNonCompileUnitScope(Context),
  680. Name, LinkageName, File, LineNo, Ty,
  681. ScopeLine, nullptr, 0, 0, Flags, SPFlags,
  682. IsDefinition ? CUNode : nullptr, TParams,
  683. Decl, nullptr, ThrownTypes)
  684. .release();
  685. }
  686. DISubprogram *DIBuilder::createMethod(
  687. DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
  688. unsigned LineNo, DISubroutineType *Ty, unsigned VIndex, int ThisAdjustment,
  689. DIType *VTableHolder, DINode::DIFlags Flags,
  690. DISubprogram::DISPFlags SPFlags, DITemplateParameterArray TParams,
  691. DITypeArray ThrownTypes) {
  692. assert(getNonCompileUnitScope(Context) &&
  693. "Methods should have both a Context and a context that isn't "
  694. "the compile unit.");
  695. // FIXME: Do we want to use different scope/lines?
  696. bool IsDefinition = SPFlags & DISubprogram::SPFlagDefinition;
  697. auto *SP = getSubprogram(
  698. /*IsDistinct=*/IsDefinition, VMContext, cast<DIScope>(Context), Name,
  699. LinkageName, F, LineNo, Ty, LineNo, VTableHolder, VIndex, ThisAdjustment,
  700. Flags, SPFlags, IsDefinition ? CUNode : nullptr, TParams, nullptr,
  701. nullptr, ThrownTypes);
  702. if (IsDefinition)
  703. AllSubprograms.push_back(SP);
  704. trackIfUnresolved(SP);
  705. return SP;
  706. }
  707. DICommonBlock *DIBuilder::createCommonBlock(
  708. DIScope *Scope, DIGlobalVariable *Decl, StringRef Name, DIFile *File,
  709. unsigned LineNo) {
  710. return DICommonBlock::get(
  711. VMContext, Scope, Decl, Name, File, LineNo);
  712. }
  713. DINamespace *DIBuilder::createNameSpace(DIScope *Scope, StringRef Name,
  714. bool ExportSymbols) {
  715. // It is okay to *not* make anonymous top-level namespaces distinct, because
  716. // all nodes that have an anonymous namespace as their parent scope are
  717. // guaranteed to be unique and/or are linked to their containing
  718. // DICompileUnit. This decision is an explicit tradeoff of link time versus
  719. // memory usage versus code simplicity and may get revisited in the future.
  720. return DINamespace::get(VMContext, getNonCompileUnitScope(Scope), Name,
  721. ExportSymbols);
  722. }
  723. DIModule *DIBuilder::createModule(DIScope *Scope, StringRef Name,
  724. StringRef ConfigurationMacros,
  725. StringRef IncludePath,
  726. StringRef ISysRoot) {
  727. return DIModule::get(VMContext, getNonCompileUnitScope(Scope), Name,
  728. ConfigurationMacros, IncludePath, ISysRoot);
  729. }
  730. DILexicalBlockFile *DIBuilder::createLexicalBlockFile(DIScope *Scope,
  731. DIFile *File,
  732. unsigned Discriminator) {
  733. return DILexicalBlockFile::get(VMContext, Scope, File, Discriminator);
  734. }
  735. DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
  736. unsigned Line, unsigned Col) {
  737. // Make these distinct, to avoid merging two lexical blocks on the same
  738. // file/line/column.
  739. return DILexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
  740. File, Line, Col);
  741. }
  742. Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
  743. DIExpression *Expr, const DILocation *DL,
  744. Instruction *InsertBefore) {
  745. return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
  746. InsertBefore);
  747. }
  748. Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
  749. DIExpression *Expr, const DILocation *DL,
  750. BasicBlock *InsertAtEnd) {
  751. // If this block already has a terminator then insert this intrinsic before
  752. // the terminator. Otherwise, put it at the end of the block.
  753. Instruction *InsertBefore = InsertAtEnd->getTerminator();
  754. return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
  755. }
  756. Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
  757. Instruction *InsertBefore) {
  758. return insertLabel(
  759. LabelInfo, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
  760. InsertBefore);
  761. }
  762. Instruction *DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
  763. BasicBlock *InsertAtEnd) {
  764. return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr);
  765. }
  766. Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
  767. DILocalVariable *VarInfo,
  768. DIExpression *Expr,
  769. const DILocation *DL,
  770. Instruction *InsertBefore) {
  771. return insertDbgValueIntrinsic(
  772. V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
  773. InsertBefore);
  774. }
  775. Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V,
  776. DILocalVariable *VarInfo,
  777. DIExpression *Expr,
  778. const DILocation *DL,
  779. BasicBlock *InsertAtEnd) {
  780. return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr);
  781. }
  782. /// Return an IRBuilder for inserting dbg.declare and dbg.value intrinsics. This
  783. /// abstracts over the various ways to specify an insert position.
  784. static IRBuilder<> getIRBForDbgInsertion(const DILocation *DL,
  785. BasicBlock *InsertBB,
  786. Instruction *InsertBefore) {
  787. IRBuilder<> B(DL->getContext());
  788. if (InsertBefore)
  789. B.SetInsertPoint(InsertBefore);
  790. else if (InsertBB)
  791. B.SetInsertPoint(InsertBB);
  792. B.SetCurrentDebugLocation(DL);
  793. return B;
  794. }
  795. static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
  796. assert(V && "no value passed to dbg intrinsic");
  797. return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
  798. }
  799. static Function *getDeclareIntrin(Module &M) {
  800. return Intrinsic::getDeclaration(&M, UseDbgAddr ? Intrinsic::dbg_addr
  801. : Intrinsic::dbg_declare);
  802. }
  803. Instruction *DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
  804. DIExpression *Expr, const DILocation *DL,
  805. BasicBlock *InsertBB, Instruction *InsertBefore) {
  806. assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
  807. assert(DL && "Expected debug loc");
  808. assert(DL->getScope()->getSubprogram() ==
  809. VarInfo->getScope()->getSubprogram() &&
  810. "Expected matching subprograms");
  811. if (!DeclareFn)
  812. DeclareFn = getDeclareIntrin(M);
  813. trackIfUnresolved(VarInfo);
  814. trackIfUnresolved(Expr);
  815. Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
  816. MetadataAsValue::get(VMContext, VarInfo),
  817. MetadataAsValue::get(VMContext, Expr)};
  818. IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
  819. return B.CreateCall(DeclareFn, Args);
  820. }
  821. Instruction *DIBuilder::insertDbgValueIntrinsic(
  822. Value *V, DILocalVariable *VarInfo, DIExpression *Expr,
  823. const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
  824. assert(V && "no value passed to dbg.value");
  825. assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.value");
  826. assert(DL && "Expected debug loc");
  827. assert(DL->getScope()->getSubprogram() ==
  828. VarInfo->getScope()->getSubprogram() &&
  829. "Expected matching subprograms");
  830. if (!ValueFn)
  831. ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
  832. trackIfUnresolved(VarInfo);
  833. trackIfUnresolved(Expr);
  834. Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V),
  835. MetadataAsValue::get(VMContext, VarInfo),
  836. MetadataAsValue::get(VMContext, Expr)};
  837. IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
  838. return B.CreateCall(ValueFn, Args);
  839. }
  840. Instruction *DIBuilder::insertLabel(
  841. DILabel *LabelInfo, const DILocation *DL,
  842. BasicBlock *InsertBB, Instruction *InsertBefore) {
  843. assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
  844. assert(DL && "Expected debug loc");
  845. assert(DL->getScope()->getSubprogram() ==
  846. LabelInfo->getScope()->getSubprogram() &&
  847. "Expected matching subprograms");
  848. if (!LabelFn)
  849. LabelFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_label);
  850. trackIfUnresolved(LabelInfo);
  851. Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)};
  852. IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore);
  853. return B.CreateCall(LabelFn, Args);
  854. }
  855. void DIBuilder::replaceVTableHolder(DICompositeType *&T,
  856. DIType *VTableHolder) {
  857. {
  858. TypedTrackingMDRef<DICompositeType> N(T);
  859. N->replaceVTableHolder(VTableHolder);
  860. T = N.get();
  861. }
  862. // If this didn't create a self-reference, just return.
  863. if (T != VTableHolder)
  864. return;
  865. // Look for unresolved operands. T will drop RAUW support, orphaning any
  866. // cycles underneath it.
  867. if (T->isResolved())
  868. for (const MDOperand &O : T->operands())
  869. if (auto *N = dyn_cast_or_null<MDNode>(O))
  870. trackIfUnresolved(N);
  871. }
  872. void DIBuilder::replaceArrays(DICompositeType *&T, DINodeArray Elements,
  873. DINodeArray TParams) {
  874. {
  875. TypedTrackingMDRef<DICompositeType> N(T);
  876. if (Elements)
  877. N->replaceElements(Elements);
  878. if (TParams)
  879. N->replaceTemplateParams(DITemplateParameterArray(TParams));
  880. T = N.get();
  881. }
  882. // If T isn't resolved, there's no problem.
  883. if (!T->isResolved())
  884. return;
  885. // If T is resolved, it may be due to a self-reference cycle. Track the
  886. // arrays explicitly if they're unresolved, or else the cycles will be
  887. // orphaned.
  888. if (Elements)
  889. trackIfUnresolved(Elements.get());
  890. if (TParams)
  891. trackIfUnresolved(TParams.get());
  892. }