DebugInfo.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
  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 helper classes used to build and interpret debug
  10. // information in LLVM IR form.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm-c/DebugInfo.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/None.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/BasicBlock.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/DebugInfoMetadata.h"
  24. #include "llvm/IR/DebugLoc.h"
  25. #include "llvm/IR/DebugInfo.h"
  26. #include "llvm/IR/DIBuilder.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/IR/GVMaterializer.h"
  29. #include "llvm/IR/Instruction.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/LLVMContext.h"
  32. #include "llvm/IR/Metadata.h"
  33. #include "llvm/IR/Module.h"
  34. #include "llvm/Support/Casting.h"
  35. #include <algorithm>
  36. #include <cassert>
  37. #include <utility>
  38. using namespace llvm;
  39. using namespace llvm::dwarf;
  40. DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
  41. if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
  42. return LocalScope->getSubprogram();
  43. return nullptr;
  44. }
  45. //===----------------------------------------------------------------------===//
  46. // DebugInfoFinder implementations.
  47. //===----------------------------------------------------------------------===//
  48. void DebugInfoFinder::reset() {
  49. CUs.clear();
  50. SPs.clear();
  51. GVs.clear();
  52. TYs.clear();
  53. Scopes.clear();
  54. NodesSeen.clear();
  55. }
  56. void DebugInfoFinder::processModule(const Module &M) {
  57. for (auto *CU : M.debug_compile_units())
  58. processCompileUnit(CU);
  59. for (auto &F : M.functions()) {
  60. if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
  61. processSubprogram(SP);
  62. // There could be subprograms from inlined functions referenced from
  63. // instructions only. Walk the function to find them.
  64. for (const BasicBlock &BB : F)
  65. for (const Instruction &I : BB)
  66. processInstruction(M, I);
  67. }
  68. }
  69. void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
  70. if (!addCompileUnit(CU))
  71. return;
  72. for (auto DIG : CU->getGlobalVariables()) {
  73. if (!addGlobalVariable(DIG))
  74. continue;
  75. auto *GV = DIG->getVariable();
  76. processScope(GV->getScope());
  77. processType(GV->getType().resolve());
  78. }
  79. for (auto *ET : CU->getEnumTypes())
  80. processType(ET);
  81. for (auto *RT : CU->getRetainedTypes())
  82. if (auto *T = dyn_cast<DIType>(RT))
  83. processType(T);
  84. else
  85. processSubprogram(cast<DISubprogram>(RT));
  86. for (auto *Import : CU->getImportedEntities()) {
  87. auto *Entity = Import->getEntity().resolve();
  88. if (auto *T = dyn_cast<DIType>(Entity))
  89. processType(T);
  90. else if (auto *SP = dyn_cast<DISubprogram>(Entity))
  91. processSubprogram(SP);
  92. else if (auto *NS = dyn_cast<DINamespace>(Entity))
  93. processScope(NS->getScope());
  94. else if (auto *M = dyn_cast<DIModule>(Entity))
  95. processScope(M->getScope());
  96. }
  97. }
  98. void DebugInfoFinder::processInstruction(const Module &M,
  99. const Instruction &I) {
  100. if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
  101. processDeclare(M, DDI);
  102. else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
  103. processValue(M, DVI);
  104. if (auto DbgLoc = I.getDebugLoc())
  105. processLocation(M, DbgLoc.get());
  106. }
  107. void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
  108. if (!Loc)
  109. return;
  110. processScope(Loc->getScope());
  111. processLocation(M, Loc->getInlinedAt());
  112. }
  113. void DebugInfoFinder::processType(DIType *DT) {
  114. if (!addType(DT))
  115. return;
  116. processScope(DT->getScope().resolve());
  117. if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
  118. for (DITypeRef Ref : ST->getTypeArray())
  119. processType(Ref.resolve());
  120. return;
  121. }
  122. if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
  123. processType(DCT->getBaseType().resolve());
  124. for (Metadata *D : DCT->getElements()) {
  125. if (auto *T = dyn_cast<DIType>(D))
  126. processType(T);
  127. else if (auto *SP = dyn_cast<DISubprogram>(D))
  128. processSubprogram(SP);
  129. }
  130. return;
  131. }
  132. if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
  133. processType(DDT->getBaseType().resolve());
  134. }
  135. }
  136. void DebugInfoFinder::processScope(DIScope *Scope) {
  137. if (!Scope)
  138. return;
  139. if (auto *Ty = dyn_cast<DIType>(Scope)) {
  140. processType(Ty);
  141. return;
  142. }
  143. if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
  144. addCompileUnit(CU);
  145. return;
  146. }
  147. if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
  148. processSubprogram(SP);
  149. return;
  150. }
  151. if (!addScope(Scope))
  152. return;
  153. if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
  154. processScope(LB->getScope());
  155. } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
  156. processScope(NS->getScope());
  157. } else if (auto *M = dyn_cast<DIModule>(Scope)) {
  158. processScope(M->getScope());
  159. }
  160. }
  161. void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
  162. if (!addSubprogram(SP))
  163. return;
  164. processScope(SP->getScope().resolve());
  165. // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
  166. // ValueMap containing identity mappings for all of the DICompileUnit's, not
  167. // just DISubprogram's, referenced from anywhere within the Function being
  168. // cloned prior to calling MapMetadata / RemapInstruction to avoid their
  169. // duplication later as DICompileUnit's are also directly referenced by
  170. // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
  171. // Also, DICompileUnit's may reference DISubprogram's too and therefore need
  172. // to be at least looked through.
  173. processCompileUnit(SP->getUnit());
  174. processType(SP->getType());
  175. for (auto *Element : SP->getTemplateParams()) {
  176. if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
  177. processType(TType->getType().resolve());
  178. } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
  179. processType(TVal->getType().resolve());
  180. }
  181. }
  182. }
  183. void DebugInfoFinder::processDeclare(const Module &M,
  184. const DbgDeclareInst *DDI) {
  185. auto *N = dyn_cast<MDNode>(DDI->getVariable());
  186. if (!N)
  187. return;
  188. auto *DV = dyn_cast<DILocalVariable>(N);
  189. if (!DV)
  190. return;
  191. if (!NodesSeen.insert(DV).second)
  192. return;
  193. processScope(DV->getScope());
  194. processType(DV->getType().resolve());
  195. }
  196. void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
  197. auto *N = dyn_cast<MDNode>(DVI->getVariable());
  198. if (!N)
  199. return;
  200. auto *DV = dyn_cast<DILocalVariable>(N);
  201. if (!DV)
  202. return;
  203. if (!NodesSeen.insert(DV).second)
  204. return;
  205. processScope(DV->getScope());
  206. processType(DV->getType().resolve());
  207. }
  208. bool DebugInfoFinder::addType(DIType *DT) {
  209. if (!DT)
  210. return false;
  211. if (!NodesSeen.insert(DT).second)
  212. return false;
  213. TYs.push_back(const_cast<DIType *>(DT));
  214. return true;
  215. }
  216. bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
  217. if (!CU)
  218. return false;
  219. if (!NodesSeen.insert(CU).second)
  220. return false;
  221. CUs.push_back(CU);
  222. return true;
  223. }
  224. bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
  225. if (!NodesSeen.insert(DIG).second)
  226. return false;
  227. GVs.push_back(DIG);
  228. return true;
  229. }
  230. bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
  231. if (!SP)
  232. return false;
  233. if (!NodesSeen.insert(SP).second)
  234. return false;
  235. SPs.push_back(SP);
  236. return true;
  237. }
  238. bool DebugInfoFinder::addScope(DIScope *Scope) {
  239. if (!Scope)
  240. return false;
  241. // FIXME: Ocaml binding generates a scope with no content, we treat it
  242. // as null for now.
  243. if (Scope->getNumOperands() == 0)
  244. return false;
  245. if (!NodesSeen.insert(Scope).second)
  246. return false;
  247. Scopes.push_back(Scope);
  248. return true;
  249. }
  250. static MDNode *stripDebugLocFromLoopID(MDNode *N) {
  251. assert(!empty(N->operands()) && "Missing self reference?");
  252. // if there is no debug location, we do not have to rewrite this MDNode.
  253. if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
  254. return isa<DILocation>(Op.get());
  255. }))
  256. return N;
  257. // If there is only the debug location without any actual loop metadata, we
  258. // can remove the metadata.
  259. if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
  260. return !isa<DILocation>(Op.get());
  261. }))
  262. return nullptr;
  263. SmallVector<Metadata *, 4> Args;
  264. // Reserve operand 0 for loop id self reference.
  265. auto TempNode = MDNode::getTemporary(N->getContext(), None);
  266. Args.push_back(TempNode.get());
  267. // Add all non-debug location operands back.
  268. for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
  269. if (!isa<DILocation>(*Op))
  270. Args.push_back(*Op);
  271. }
  272. // Set the first operand to itself.
  273. MDNode *LoopID = MDNode::get(N->getContext(), Args);
  274. LoopID->replaceOperandWith(0, LoopID);
  275. return LoopID;
  276. }
  277. bool llvm::stripDebugInfo(Function &F) {
  278. bool Changed = false;
  279. if (F.hasMetadata(LLVMContext::MD_dbg)) {
  280. Changed = true;
  281. F.setSubprogram(nullptr);
  282. }
  283. DenseMap<MDNode*, MDNode*> LoopIDsMap;
  284. for (BasicBlock &BB : F) {
  285. for (auto II = BB.begin(), End = BB.end(); II != End;) {
  286. Instruction &I = *II++; // We may delete the instruction, increment now.
  287. if (isa<DbgInfoIntrinsic>(&I)) {
  288. I.eraseFromParent();
  289. Changed = true;
  290. continue;
  291. }
  292. if (I.getDebugLoc()) {
  293. Changed = true;
  294. I.setDebugLoc(DebugLoc());
  295. }
  296. }
  297. auto *TermInst = BB.getTerminator();
  298. if (!TermInst)
  299. // This is invalid IR, but we may not have run the verifier yet
  300. continue;
  301. if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
  302. auto *NewLoopID = LoopIDsMap.lookup(LoopID);
  303. if (!NewLoopID)
  304. NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
  305. if (NewLoopID != LoopID)
  306. TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
  307. }
  308. }
  309. return Changed;
  310. }
  311. bool llvm::StripDebugInfo(Module &M) {
  312. bool Changed = false;
  313. for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
  314. NME = M.named_metadata_end(); NMI != NME;) {
  315. NamedMDNode *NMD = &*NMI;
  316. ++NMI;
  317. // We're stripping debug info, and without them, coverage information
  318. // doesn't quite make sense.
  319. if (NMD->getName().startswith("llvm.dbg.") ||
  320. NMD->getName() == "llvm.gcov") {
  321. NMD->eraseFromParent();
  322. Changed = true;
  323. }
  324. }
  325. for (Function &F : M)
  326. Changed |= stripDebugInfo(F);
  327. for (auto &GV : M.globals()) {
  328. Changed |= GV.eraseMetadata(LLVMContext::MD_dbg);
  329. }
  330. if (GVMaterializer *Materializer = M.getMaterializer())
  331. Materializer->setStripDebugInfo();
  332. return Changed;
  333. }
  334. namespace {
  335. /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
  336. class DebugTypeInfoRemoval {
  337. DenseMap<Metadata *, Metadata *> Replacements;
  338. public:
  339. /// The (void)() type.
  340. MDNode *EmptySubroutineType;
  341. private:
  342. /// Remember what linkage name we originally had before stripping. If we end
  343. /// up making two subprograms identical who originally had different linkage
  344. /// names, then we need to make one of them distinct, to avoid them getting
  345. /// uniqued. Maps the new node to the old linkage name.
  346. DenseMap<DISubprogram *, StringRef> NewToLinkageName;
  347. // TODO: Remember the distinct subprogram we created for a given linkage name,
  348. // so that we can continue to unique whenever possible. Map <newly created
  349. // node, old linkage name> to the first (possibly distinct) mdsubprogram
  350. // created for that combination. This is not strictly needed for correctness,
  351. // but can cut down on the number of MDNodes and let us diff cleanly with the
  352. // output of -gline-tables-only.
  353. public:
  354. DebugTypeInfoRemoval(LLVMContext &C)
  355. : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
  356. MDNode::get(C, {}))) {}
  357. Metadata *map(Metadata *M) {
  358. if (!M)
  359. return nullptr;
  360. auto Replacement = Replacements.find(M);
  361. if (Replacement != Replacements.end())
  362. return Replacement->second;
  363. return M;
  364. }
  365. MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
  366. /// Recursively remap N and all its referenced children. Does a DF post-order
  367. /// traversal, so as to remap bottoms up.
  368. void traverseAndRemap(MDNode *N) { traverse(N); }
  369. private:
  370. // Create a new DISubprogram, to replace the one given.
  371. DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
  372. auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
  373. StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
  374. DISubprogram *Declaration = nullptr;
  375. auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
  376. DITypeRef ContainingType(map(MDS->getContainingType()));
  377. auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
  378. auto Variables = nullptr;
  379. auto TemplateParams = nullptr;
  380. // Make a distinct DISubprogram, for situations that warrent it.
  381. auto distinctMDSubprogram = [&]() {
  382. return DISubprogram::getDistinct(
  383. MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
  384. FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(),
  385. ContainingType, MDS->getVirtualIndex(), MDS->getThisAdjustment(),
  386. MDS->getFlags(), MDS->getSPFlags(), Unit, TemplateParams, Declaration,
  387. Variables);
  388. };
  389. if (MDS->isDistinct())
  390. return distinctMDSubprogram();
  391. auto *NewMDS = DISubprogram::get(
  392. MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
  393. FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), ContainingType,
  394. MDS->getVirtualIndex(), MDS->getThisAdjustment(), MDS->getFlags(),
  395. MDS->getSPFlags(), Unit, TemplateParams, Declaration, Variables);
  396. StringRef OldLinkageName = MDS->getLinkageName();
  397. // See if we need to make a distinct one.
  398. auto OrigLinkage = NewToLinkageName.find(NewMDS);
  399. if (OrigLinkage != NewToLinkageName.end()) {
  400. if (OrigLinkage->second == OldLinkageName)
  401. // We're good.
  402. return NewMDS;
  403. // Otherwise, need to make a distinct one.
  404. // TODO: Query the map to see if we already have one.
  405. return distinctMDSubprogram();
  406. }
  407. NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
  408. return NewMDS;
  409. }
  410. /// Create a new compile unit, to replace the one given
  411. DICompileUnit *getReplacementCU(DICompileUnit *CU) {
  412. // Drop skeleton CUs.
  413. if (CU->getDWOId())
  414. return nullptr;
  415. auto *File = cast_or_null<DIFile>(map(CU->getFile()));
  416. MDTuple *EnumTypes = nullptr;
  417. MDTuple *RetainedTypes = nullptr;
  418. MDTuple *GlobalVariables = nullptr;
  419. MDTuple *ImportedEntities = nullptr;
  420. return DICompileUnit::getDistinct(
  421. CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
  422. CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
  423. CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
  424. RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
  425. CU->getDWOId(), CU->getSplitDebugInlining(),
  426. CU->getDebugInfoForProfiling(), CU->getNameTableKind(),
  427. CU->getRangesBaseAddress());
  428. }
  429. DILocation *getReplacementMDLocation(DILocation *MLD) {
  430. auto *Scope = map(MLD->getScope());
  431. auto *InlinedAt = map(MLD->getInlinedAt());
  432. if (MLD->isDistinct())
  433. return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
  434. MLD->getColumn(), Scope, InlinedAt);
  435. return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
  436. Scope, InlinedAt);
  437. }
  438. /// Create a new generic MDNode, to replace the one given
  439. MDNode *getReplacementMDNode(MDNode *N) {
  440. SmallVector<Metadata *, 8> Ops;
  441. Ops.reserve(N->getNumOperands());
  442. for (auto &I : N->operands())
  443. if (I)
  444. Ops.push_back(map(I));
  445. auto *Ret = MDNode::get(N->getContext(), Ops);
  446. return Ret;
  447. }
  448. /// Attempt to re-map N to a newly created node.
  449. void remap(MDNode *N) {
  450. if (Replacements.count(N))
  451. return;
  452. auto doRemap = [&](MDNode *N) -> MDNode * {
  453. if (!N)
  454. return nullptr;
  455. if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
  456. remap(MDSub->getUnit());
  457. return getReplacementSubprogram(MDSub);
  458. }
  459. if (isa<DISubroutineType>(N))
  460. return EmptySubroutineType;
  461. if (auto *CU = dyn_cast<DICompileUnit>(N))
  462. return getReplacementCU(CU);
  463. if (isa<DIFile>(N))
  464. return N;
  465. if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
  466. // Remap to our referenced scope (recursively).
  467. return mapNode(MDLB->getScope());
  468. if (auto *MLD = dyn_cast<DILocation>(N))
  469. return getReplacementMDLocation(MLD);
  470. // Otherwise, if we see these, just drop them now. Not strictly necessary,
  471. // but this speeds things up a little.
  472. if (isa<DINode>(N))
  473. return nullptr;
  474. return getReplacementMDNode(N);
  475. };
  476. Replacements[N] = doRemap(N);
  477. }
  478. /// Do the remapping traversal.
  479. void traverse(MDNode *);
  480. };
  481. } // end anonymous namespace
  482. void DebugTypeInfoRemoval::traverse(MDNode *N) {
  483. if (!N || Replacements.count(N))
  484. return;
  485. // To avoid cycles, as well as for efficiency sake, we will sometimes prune
  486. // parts of the graph.
  487. auto prune = [](MDNode *Parent, MDNode *Child) {
  488. if (auto *MDS = dyn_cast<DISubprogram>(Parent))
  489. return Child == MDS->getRetainedNodes().get();
  490. return false;
  491. };
  492. SmallVector<MDNode *, 16> ToVisit;
  493. DenseSet<MDNode *> Opened;
  494. // Visit each node starting at N in post order, and map them.
  495. ToVisit.push_back(N);
  496. while (!ToVisit.empty()) {
  497. auto *N = ToVisit.back();
  498. if (!Opened.insert(N).second) {
  499. // Close it.
  500. remap(N);
  501. ToVisit.pop_back();
  502. continue;
  503. }
  504. for (auto &I : N->operands())
  505. if (auto *MDN = dyn_cast_or_null<MDNode>(I))
  506. if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
  507. !isa<DICompileUnit>(MDN))
  508. ToVisit.push_back(MDN);
  509. }
  510. }
  511. bool llvm::stripNonLineTableDebugInfo(Module &M) {
  512. bool Changed = false;
  513. // First off, delete the debug intrinsics.
  514. auto RemoveUses = [&](StringRef Name) {
  515. if (auto *DbgVal = M.getFunction(Name)) {
  516. while (!DbgVal->use_empty())
  517. cast<Instruction>(DbgVal->user_back())->eraseFromParent();
  518. DbgVal->eraseFromParent();
  519. Changed = true;
  520. }
  521. };
  522. RemoveUses("llvm.dbg.declare");
  523. RemoveUses("llvm.dbg.value");
  524. // Delete non-CU debug info named metadata nodes.
  525. for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
  526. NMI != NME;) {
  527. NamedMDNode *NMD = &*NMI;
  528. ++NMI;
  529. // Specifically keep dbg.cu around.
  530. if (NMD->getName() == "llvm.dbg.cu")
  531. continue;
  532. }
  533. // Drop all dbg attachments from global variables.
  534. for (auto &GV : M.globals())
  535. GV.eraseMetadata(LLVMContext::MD_dbg);
  536. DebugTypeInfoRemoval Mapper(M.getContext());
  537. auto remap = [&](MDNode *Node) -> MDNode * {
  538. if (!Node)
  539. return nullptr;
  540. Mapper.traverseAndRemap(Node);
  541. auto *NewNode = Mapper.mapNode(Node);
  542. Changed |= Node != NewNode;
  543. Node = NewNode;
  544. return NewNode;
  545. };
  546. // Rewrite the DebugLocs to be equivalent to what
  547. // -gline-tables-only would have created.
  548. for (auto &F : M) {
  549. if (auto *SP = F.getSubprogram()) {
  550. Mapper.traverseAndRemap(SP);
  551. auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
  552. Changed |= SP != NewSP;
  553. F.setSubprogram(NewSP);
  554. }
  555. for (auto &BB : F) {
  556. for (auto &I : BB) {
  557. auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
  558. auto *Scope = DL.getScope();
  559. MDNode *InlinedAt = DL.getInlinedAt();
  560. Scope = remap(Scope);
  561. InlinedAt = remap(InlinedAt);
  562. return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
  563. };
  564. if (I.getDebugLoc() != DebugLoc())
  565. I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
  566. // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
  567. SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
  568. I.getAllMetadata(MDs);
  569. for (auto Attachment : MDs)
  570. if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
  571. for (unsigned N = 0; N < T->getNumOperands(); ++N)
  572. if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
  573. if (Loc != DebugLoc())
  574. T->replaceOperandWith(N, remapDebugLoc(Loc));
  575. }
  576. }
  577. }
  578. // Create a new llvm.dbg.cu, which is equivalent to the one
  579. // -gline-tables-only would have created.
  580. for (auto &NMD : M.getNamedMDList()) {
  581. SmallVector<MDNode *, 8> Ops;
  582. for (MDNode *Op : NMD.operands())
  583. Ops.push_back(remap(Op));
  584. if (!Changed)
  585. continue;
  586. NMD.clearOperands();
  587. for (auto *Op : Ops)
  588. if (Op)
  589. NMD.addOperand(Op);
  590. }
  591. return Changed;
  592. }
  593. unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
  594. if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
  595. M.getModuleFlag("Debug Info Version")))
  596. return Val->getZExtValue();
  597. return 0;
  598. }
  599. void Instruction::applyMergedLocation(const DILocation *LocA,
  600. const DILocation *LocB) {
  601. setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
  602. }
  603. //===----------------------------------------------------------------------===//
  604. // LLVM C API implementations.
  605. //===----------------------------------------------------------------------===//
  606. static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
  607. switch (lang) {
  608. #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
  609. case LLVMDWARFSourceLanguage##NAME: \
  610. return ID;
  611. #include "llvm/BinaryFormat/Dwarf.def"
  612. #undef HANDLE_DW_LANG
  613. }
  614. llvm_unreachable("Unhandled Tag");
  615. }
  616. template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
  617. return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
  618. }
  619. static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
  620. return static_cast<DINode::DIFlags>(Flags);
  621. }
  622. static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
  623. return static_cast<LLVMDIFlags>(Flags);
  624. }
  625. static DISubprogram::DISPFlags
  626. pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) {
  627. return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized);
  628. }
  629. unsigned LLVMDebugMetadataVersion() {
  630. return DEBUG_METADATA_VERSION;
  631. }
  632. LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
  633. return wrap(new DIBuilder(*unwrap(M), false));
  634. }
  635. LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
  636. return wrap(new DIBuilder(*unwrap(M)));
  637. }
  638. unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
  639. return getDebugMetadataVersionFromModule(*unwrap(M));
  640. }
  641. LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
  642. return StripDebugInfo(*unwrap(M));
  643. }
  644. void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
  645. delete unwrap(Builder);
  646. }
  647. void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
  648. unwrap(Builder)->finalize();
  649. }
  650. LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
  651. LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
  652. LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
  653. LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
  654. unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
  655. LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
  656. LLVMBool DebugInfoForProfiling) {
  657. auto File = unwrapDI<DIFile>(FileRef);
  658. return wrap(unwrap(Builder)->createCompileUnit(
  659. map_from_llvmDWARFsourcelanguage(Lang), File,
  660. StringRef(Producer, ProducerLen), isOptimized,
  661. StringRef(Flags, FlagsLen), RuntimeVer,
  662. StringRef(SplitName, SplitNameLen),
  663. static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
  664. SplitDebugInlining, DebugInfoForProfiling));
  665. }
  666. LLVMMetadataRef
  667. LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
  668. size_t FilenameLen, const char *Directory,
  669. size_t DirectoryLen) {
  670. return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
  671. StringRef(Directory, DirectoryLen)));
  672. }
  673. LLVMMetadataRef
  674. LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
  675. const char *Name, size_t NameLen,
  676. const char *ConfigMacros, size_t ConfigMacrosLen,
  677. const char *IncludePath, size_t IncludePathLen,
  678. const char *ISysRoot, size_t ISysRootLen) {
  679. return wrap(unwrap(Builder)->createModule(
  680. unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
  681. StringRef(ConfigMacros, ConfigMacrosLen),
  682. StringRef(IncludePath, IncludePathLen),
  683. StringRef(ISysRoot, ISysRootLen)));
  684. }
  685. LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
  686. LLVMMetadataRef ParentScope,
  687. const char *Name, size_t NameLen,
  688. LLVMBool ExportSymbols) {
  689. return wrap(unwrap(Builder)->createNameSpace(
  690. unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
  691. }
  692. LLVMMetadataRef LLVMDIBuilderCreateFunction(
  693. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  694. size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
  695. LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
  696. LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
  697. unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
  698. return wrap(unwrap(Builder)->createFunction(
  699. unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
  700. unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine,
  701. map_from_llvmDIFlags(Flags),
  702. pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr,
  703. nullptr, nullptr));
  704. }
  705. LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
  706. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
  707. LLVMMetadataRef File, unsigned Line, unsigned Col) {
  708. return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
  709. unwrapDI<DIFile>(File),
  710. Line, Col));
  711. }
  712. LLVMMetadataRef
  713. LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
  714. LLVMMetadataRef Scope,
  715. LLVMMetadataRef File,
  716. unsigned Discriminator) {
  717. return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
  718. unwrapDI<DIFile>(File),
  719. Discriminator));
  720. }
  721. LLVMMetadataRef
  722. LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
  723. LLVMMetadataRef Scope,
  724. LLVMMetadataRef NS,
  725. LLVMMetadataRef File,
  726. unsigned Line) {
  727. return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
  728. unwrapDI<DINamespace>(NS),
  729. unwrapDI<DIFile>(File),
  730. Line));
  731. }
  732. LLVMMetadataRef
  733. LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
  734. LLVMMetadataRef Scope,
  735. LLVMMetadataRef ImportedEntity,
  736. LLVMMetadataRef File,
  737. unsigned Line) {
  738. return wrap(unwrap(Builder)->createImportedModule(
  739. unwrapDI<DIScope>(Scope),
  740. unwrapDI<DIImportedEntity>(ImportedEntity),
  741. unwrapDI<DIFile>(File), Line));
  742. }
  743. LLVMMetadataRef
  744. LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
  745. LLVMMetadataRef Scope,
  746. LLVMMetadataRef M,
  747. LLVMMetadataRef File,
  748. unsigned Line) {
  749. return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
  750. unwrapDI<DIModule>(M),
  751. unwrapDI<DIFile>(File),
  752. Line));
  753. }
  754. LLVMMetadataRef
  755. LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
  756. LLVMMetadataRef Scope,
  757. LLVMMetadataRef Decl,
  758. LLVMMetadataRef File,
  759. unsigned Line,
  760. const char *Name, size_t NameLen) {
  761. return wrap(unwrap(Builder)->createImportedDeclaration(
  762. unwrapDI<DIScope>(Scope),
  763. unwrapDI<DINode>(Decl),
  764. unwrapDI<DIFile>(File), Line, {Name, NameLen}));
  765. }
  766. LLVMMetadataRef
  767. LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
  768. unsigned Column, LLVMMetadataRef Scope,
  769. LLVMMetadataRef InlinedAt) {
  770. return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
  771. unwrap(InlinedAt)));
  772. }
  773. unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
  774. return unwrapDI<DILocation>(Location)->getLine();
  775. }
  776. unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
  777. return unwrapDI<DILocation>(Location)->getColumn();
  778. }
  779. LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
  780. return wrap(unwrapDI<DILocation>(Location)->getScope());
  781. }
  782. LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
  783. const char *Name, size_t NameLen,
  784. int64_t Value,
  785. LLVMBool IsUnsigned) {
  786. return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value,
  787. IsUnsigned != 0));
  788. }
  789. LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
  790. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  791. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  792. uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
  793. unsigned NumElements, LLVMMetadataRef ClassTy) {
  794. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  795. NumElements});
  796. return wrap(unwrap(Builder)->createEnumerationType(
  797. unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
  798. LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
  799. }
  800. LLVMMetadataRef LLVMDIBuilderCreateUnionType(
  801. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  802. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  803. uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
  804. LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
  805. const char *UniqueId, size_t UniqueIdLen) {
  806. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  807. NumElements});
  808. return wrap(unwrap(Builder)->createUnionType(
  809. unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
  810. LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
  811. Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
  812. }
  813. LLVMMetadataRef
  814. LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
  815. uint32_t AlignInBits, LLVMMetadataRef Ty,
  816. LLVMMetadataRef *Subscripts,
  817. unsigned NumSubscripts) {
  818. auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
  819. NumSubscripts});
  820. return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
  821. unwrapDI<DIType>(Ty), Subs));
  822. }
  823. LLVMMetadataRef
  824. LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
  825. uint32_t AlignInBits, LLVMMetadataRef Ty,
  826. LLVMMetadataRef *Subscripts,
  827. unsigned NumSubscripts) {
  828. auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
  829. NumSubscripts});
  830. return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
  831. unwrapDI<DIType>(Ty), Subs));
  832. }
  833. LLVMMetadataRef
  834. LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
  835. size_t NameLen, uint64_t SizeInBits,
  836. LLVMDWARFTypeEncoding Encoding,
  837. LLVMDIFlags Flags) {
  838. return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
  839. SizeInBits, Encoding,
  840. map_from_llvmDIFlags(Flags)));
  841. }
  842. LLVMMetadataRef LLVMDIBuilderCreatePointerType(
  843. LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
  844. uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
  845. const char *Name, size_t NameLen) {
  846. return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
  847. SizeInBits, AlignInBits,
  848. AddressSpace, {Name, NameLen}));
  849. }
  850. LLVMMetadataRef LLVMDIBuilderCreateStructType(
  851. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  852. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  853. uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
  854. LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
  855. unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
  856. const char *UniqueId, size_t UniqueIdLen) {
  857. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  858. NumElements});
  859. return wrap(unwrap(Builder)->createStructType(
  860. unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
  861. LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
  862. unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
  863. unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
  864. }
  865. LLVMMetadataRef LLVMDIBuilderCreateMemberType(
  866. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  867. size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
  868. uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
  869. LLVMMetadataRef Ty) {
  870. return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
  871. {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
  872. OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
  873. }
  874. LLVMMetadataRef
  875. LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
  876. size_t NameLen) {
  877. return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
  878. }
  879. LLVMMetadataRef
  880. LLVMDIBuilderCreateStaticMemberType(
  881. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  882. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  883. LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
  884. uint32_t AlignInBits) {
  885. return wrap(unwrap(Builder)->createStaticMemberType(
  886. unwrapDI<DIScope>(Scope), {Name, NameLen},
  887. unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
  888. map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
  889. AlignInBits));
  890. }
  891. LLVMMetadataRef
  892. LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
  893. const char *Name, size_t NameLen,
  894. LLVMMetadataRef File, unsigned LineNo,
  895. uint64_t SizeInBits, uint32_t AlignInBits,
  896. uint64_t OffsetInBits, LLVMDIFlags Flags,
  897. LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
  898. return wrap(unwrap(Builder)->createObjCIVar(
  899. {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
  900. SizeInBits, AlignInBits, OffsetInBits,
  901. map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
  902. unwrapDI<MDNode>(PropertyNode)));
  903. }
  904. LLVMMetadataRef
  905. LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
  906. const char *Name, size_t NameLen,
  907. LLVMMetadataRef File, unsigned LineNo,
  908. const char *GetterName, size_t GetterNameLen,
  909. const char *SetterName, size_t SetterNameLen,
  910. unsigned PropertyAttributes,
  911. LLVMMetadataRef Ty) {
  912. return wrap(unwrap(Builder)->createObjCProperty(
  913. {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
  914. {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
  915. PropertyAttributes, unwrapDI<DIType>(Ty)));
  916. }
  917. LLVMMetadataRef
  918. LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
  919. LLVMMetadataRef Type) {
  920. return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
  921. }
  922. LLVMMetadataRef
  923. LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
  924. const char *Name, size_t NameLen,
  925. LLVMMetadataRef File, unsigned LineNo,
  926. LLVMMetadataRef Scope) {
  927. return wrap(unwrap(Builder)->createTypedef(
  928. unwrapDI<DIType>(Type), {Name, NameLen},
  929. unwrapDI<DIFile>(File), LineNo,
  930. unwrapDI<DIScope>(Scope)));
  931. }
  932. LLVMMetadataRef
  933. LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
  934. LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
  935. uint64_t BaseOffset, uint32_t VBPtrOffset,
  936. LLVMDIFlags Flags) {
  937. return wrap(unwrap(Builder)->createInheritance(
  938. unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
  939. BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
  940. }
  941. LLVMMetadataRef
  942. LLVMDIBuilderCreateForwardDecl(
  943. LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
  944. size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
  945. unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
  946. const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
  947. return wrap(unwrap(Builder)->createForwardDecl(
  948. Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
  949. unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
  950. AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
  951. }
  952. LLVMMetadataRef
  953. LLVMDIBuilderCreateReplaceableCompositeType(
  954. LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
  955. size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
  956. unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
  957. LLVMDIFlags Flags, const char *UniqueIdentifier,
  958. size_t UniqueIdentifierLen) {
  959. return wrap(unwrap(Builder)->createReplaceableCompositeType(
  960. Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
  961. unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
  962. AlignInBits, map_from_llvmDIFlags(Flags),
  963. {UniqueIdentifier, UniqueIdentifierLen}));
  964. }
  965. LLVMMetadataRef
  966. LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
  967. LLVMMetadataRef Type) {
  968. return wrap(unwrap(Builder)->createQualifiedType(Tag,
  969. unwrapDI<DIType>(Type)));
  970. }
  971. LLVMMetadataRef
  972. LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
  973. LLVMMetadataRef Type) {
  974. return wrap(unwrap(Builder)->createReferenceType(Tag,
  975. unwrapDI<DIType>(Type)));
  976. }
  977. LLVMMetadataRef
  978. LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
  979. return wrap(unwrap(Builder)->createNullPtrType());
  980. }
  981. LLVMMetadataRef
  982. LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
  983. LLVMMetadataRef PointeeType,
  984. LLVMMetadataRef ClassType,
  985. uint64_t SizeInBits,
  986. uint32_t AlignInBits,
  987. LLVMDIFlags Flags) {
  988. return wrap(unwrap(Builder)->createMemberPointerType(
  989. unwrapDI<DIType>(PointeeType),
  990. unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
  991. map_from_llvmDIFlags(Flags)));
  992. }
  993. LLVMMetadataRef
  994. LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
  995. LLVMMetadataRef Scope,
  996. const char *Name, size_t NameLen,
  997. LLVMMetadataRef File, unsigned LineNumber,
  998. uint64_t SizeInBits,
  999. uint64_t OffsetInBits,
  1000. uint64_t StorageOffsetInBits,
  1001. LLVMDIFlags Flags, LLVMMetadataRef Type) {
  1002. return wrap(unwrap(Builder)->createBitFieldMemberType(
  1003. unwrapDI<DIScope>(Scope), {Name, NameLen},
  1004. unwrapDI<DIFile>(File), LineNumber,
  1005. SizeInBits, OffsetInBits, StorageOffsetInBits,
  1006. map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
  1007. }
  1008. LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
  1009. LLVMMetadataRef Scope, const char *Name, size_t NameLen,
  1010. LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
  1011. uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
  1012. LLVMMetadataRef DerivedFrom,
  1013. LLVMMetadataRef *Elements, unsigned NumElements,
  1014. LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
  1015. const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
  1016. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  1017. NumElements});
  1018. return wrap(unwrap(Builder)->createClassType(
  1019. unwrapDI<DIScope>(Scope), {Name, NameLen},
  1020. unwrapDI<DIFile>(File), LineNumber,
  1021. SizeInBits, AlignInBits, OffsetInBits,
  1022. map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
  1023. Elts, unwrapDI<DIType>(VTableHolder),
  1024. unwrapDI<MDNode>(TemplateParamsNode),
  1025. {UniqueIdentifier, UniqueIdentifierLen}));
  1026. }
  1027. LLVMMetadataRef
  1028. LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
  1029. LLVMMetadataRef Type) {
  1030. return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
  1031. }
  1032. const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
  1033. StringRef Str = unwrap<DIType>(DType)->getName();
  1034. *Length = Str.size();
  1035. return Str.data();
  1036. }
  1037. uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
  1038. return unwrapDI<DIType>(DType)->getSizeInBits();
  1039. }
  1040. uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
  1041. return unwrapDI<DIType>(DType)->getOffsetInBits();
  1042. }
  1043. uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
  1044. return unwrapDI<DIType>(DType)->getAlignInBits();
  1045. }
  1046. unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
  1047. return unwrapDI<DIType>(DType)->getLine();
  1048. }
  1049. LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
  1050. return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
  1051. }
  1052. LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
  1053. LLVMMetadataRef *Types,
  1054. size_t Length) {
  1055. return wrap(
  1056. unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
  1057. }
  1058. LLVMMetadataRef
  1059. LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
  1060. LLVMMetadataRef File,
  1061. LLVMMetadataRef *ParameterTypes,
  1062. unsigned NumParameterTypes,
  1063. LLVMDIFlags Flags) {
  1064. auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
  1065. NumParameterTypes});
  1066. return wrap(unwrap(Builder)->createSubroutineType(
  1067. Elts, map_from_llvmDIFlags(Flags)));
  1068. }
  1069. LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
  1070. int64_t *Addr, size_t Length) {
  1071. return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
  1072. Length)));
  1073. }
  1074. LLVMMetadataRef
  1075. LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
  1076. int64_t Value) {
  1077. return wrap(unwrap(Builder)->createConstantValueExpression(Value));
  1078. }
  1079. LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
  1080. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1081. size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
  1082. unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
  1083. LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
  1084. return wrap(unwrap(Builder)->createGlobalVariableExpression(
  1085. unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
  1086. unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
  1087. unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl),
  1088. nullptr, AlignInBits));
  1089. }
  1090. LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
  1091. size_t Count) {
  1092. return wrap(
  1093. MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
  1094. }
  1095. void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
  1096. MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
  1097. }
  1098. void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
  1099. LLVMMetadataRef Replacement) {
  1100. auto *Node = unwrapDI<MDNode>(TargetMetadata);
  1101. Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
  1102. MDNode::deleteTemporary(Node);
  1103. }
  1104. LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
  1105. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1106. size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
  1107. unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
  1108. LLVMMetadataRef Decl, uint32_t AlignInBits) {
  1109. return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
  1110. unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
  1111. unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
  1112. unwrapDI<MDNode>(Decl), nullptr, AlignInBits));
  1113. }
  1114. LLVMValueRef
  1115. LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage,
  1116. LLVMMetadataRef VarInfo, LLVMMetadataRef Expr,
  1117. LLVMMetadataRef DL, LLVMValueRef Instr) {
  1118. return wrap(unwrap(Builder)->insertDeclare(
  1119. unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
  1120. unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
  1121. unwrap<Instruction>(Instr)));
  1122. }
  1123. LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
  1124. LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
  1125. LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
  1126. return wrap(unwrap(Builder)->insertDeclare(
  1127. unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
  1128. unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
  1129. unwrap(Block)));
  1130. }
  1131. LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
  1132. LLVMValueRef Val,
  1133. LLVMMetadataRef VarInfo,
  1134. LLVMMetadataRef Expr,
  1135. LLVMMetadataRef DebugLoc,
  1136. LLVMValueRef Instr) {
  1137. return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
  1138. unwrap(Val), unwrap<DILocalVariable>(VarInfo),
  1139. unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
  1140. unwrap<Instruction>(Instr)));
  1141. }
  1142. LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
  1143. LLVMValueRef Val,
  1144. LLVMMetadataRef VarInfo,
  1145. LLVMMetadataRef Expr,
  1146. LLVMMetadataRef DebugLoc,
  1147. LLVMBasicBlockRef Block) {
  1148. return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
  1149. unwrap(Val), unwrap<DILocalVariable>(VarInfo),
  1150. unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
  1151. unwrap(Block)));
  1152. }
  1153. LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
  1154. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1155. size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
  1156. LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
  1157. return wrap(unwrap(Builder)->createAutoVariable(
  1158. unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
  1159. LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
  1160. map_from_llvmDIFlags(Flags), AlignInBits));
  1161. }
  1162. LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
  1163. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1164. size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
  1165. LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
  1166. return wrap(unwrap(Builder)->createParameterVariable(
  1167. unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
  1168. LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
  1169. map_from_llvmDIFlags(Flags)));
  1170. }
  1171. LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
  1172. int64_t Lo, int64_t Count) {
  1173. return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
  1174. }
  1175. LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
  1176. LLVMMetadataRef *Data,
  1177. size_t Length) {
  1178. Metadata **DataValue = unwrap(Data);
  1179. return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
  1180. }
  1181. LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
  1182. return wrap(unwrap<Function>(Func)->getSubprogram());
  1183. }
  1184. void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
  1185. unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
  1186. }
  1187. LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
  1188. switch(unwrap(Metadata)->getMetadataID()) {
  1189. #define HANDLE_METADATA_LEAF(CLASS) \
  1190. case Metadata::CLASS##Kind: \
  1191. return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
  1192. #include "llvm/IR/Metadata.def"
  1193. default:
  1194. return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;
  1195. }
  1196. }