DebugInfo.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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());
  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();
  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());
  117. if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
  118. for (DIType *Ref : ST->getTypeArray())
  119. processType(Ref);
  120. return;
  121. }
  122. if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
  123. processType(DCT->getBaseType());
  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());
  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());
  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());
  178. } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
  179. processType(TVal->getType());
  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());
  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());
  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. DIType *ContainingType =
  377. cast_or_null<DIType>(map(MDS->getContainingType()));
  378. auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
  379. auto Variables = nullptr;
  380. auto TemplateParams = nullptr;
  381. // Make a distinct DISubprogram, for situations that warrent it.
  382. auto distinctMDSubprogram = [&]() {
  383. return DISubprogram::getDistinct(
  384. MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
  385. FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(),
  386. ContainingType, MDS->getVirtualIndex(), MDS->getThisAdjustment(),
  387. MDS->getFlags(), MDS->getSPFlags(), Unit, TemplateParams, Declaration,
  388. Variables);
  389. };
  390. if (MDS->isDistinct())
  391. return distinctMDSubprogram();
  392. auto *NewMDS = DISubprogram::get(
  393. MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
  394. FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), ContainingType,
  395. MDS->getVirtualIndex(), MDS->getThisAdjustment(), MDS->getFlags(),
  396. MDS->getSPFlags(), Unit, TemplateParams, Declaration, Variables);
  397. StringRef OldLinkageName = MDS->getLinkageName();
  398. // See if we need to make a distinct one.
  399. auto OrigLinkage = NewToLinkageName.find(NewMDS);
  400. if (OrigLinkage != NewToLinkageName.end()) {
  401. if (OrigLinkage->second == OldLinkageName)
  402. // We're good.
  403. return NewMDS;
  404. // Otherwise, need to make a distinct one.
  405. // TODO: Query the map to see if we already have one.
  406. return distinctMDSubprogram();
  407. }
  408. NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
  409. return NewMDS;
  410. }
  411. /// Create a new compile unit, to replace the one given
  412. DICompileUnit *getReplacementCU(DICompileUnit *CU) {
  413. // Drop skeleton CUs.
  414. if (CU->getDWOId())
  415. return nullptr;
  416. auto *File = cast_or_null<DIFile>(map(CU->getFile()));
  417. MDTuple *EnumTypes = nullptr;
  418. MDTuple *RetainedTypes = nullptr;
  419. MDTuple *GlobalVariables = nullptr;
  420. MDTuple *ImportedEntities = nullptr;
  421. return DICompileUnit::getDistinct(
  422. CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
  423. CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
  424. CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
  425. RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
  426. CU->getDWOId(), CU->getSplitDebugInlining(),
  427. CU->getDebugInfoForProfiling(), CU->getNameTableKind(),
  428. CU->getRangesBaseAddress());
  429. }
  430. DILocation *getReplacementMDLocation(DILocation *MLD) {
  431. auto *Scope = map(MLD->getScope());
  432. auto *InlinedAt = map(MLD->getInlinedAt());
  433. if (MLD->isDistinct())
  434. return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
  435. MLD->getColumn(), Scope, InlinedAt);
  436. return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
  437. Scope, InlinedAt);
  438. }
  439. /// Create a new generic MDNode, to replace the one given
  440. MDNode *getReplacementMDNode(MDNode *N) {
  441. SmallVector<Metadata *, 8> Ops;
  442. Ops.reserve(N->getNumOperands());
  443. for (auto &I : N->operands())
  444. if (I)
  445. Ops.push_back(map(I));
  446. auto *Ret = MDNode::get(N->getContext(), Ops);
  447. return Ret;
  448. }
  449. /// Attempt to re-map N to a newly created node.
  450. void remap(MDNode *N) {
  451. if (Replacements.count(N))
  452. return;
  453. auto doRemap = [&](MDNode *N) -> MDNode * {
  454. if (!N)
  455. return nullptr;
  456. if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
  457. remap(MDSub->getUnit());
  458. return getReplacementSubprogram(MDSub);
  459. }
  460. if (isa<DISubroutineType>(N))
  461. return EmptySubroutineType;
  462. if (auto *CU = dyn_cast<DICompileUnit>(N))
  463. return getReplacementCU(CU);
  464. if (isa<DIFile>(N))
  465. return N;
  466. if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
  467. // Remap to our referenced scope (recursively).
  468. return mapNode(MDLB->getScope());
  469. if (auto *MLD = dyn_cast<DILocation>(N))
  470. return getReplacementMDLocation(MLD);
  471. // Otherwise, if we see these, just drop them now. Not strictly necessary,
  472. // but this speeds things up a little.
  473. if (isa<DINode>(N))
  474. return nullptr;
  475. return getReplacementMDNode(N);
  476. };
  477. Replacements[N] = doRemap(N);
  478. }
  479. /// Do the remapping traversal.
  480. void traverse(MDNode *);
  481. };
  482. } // end anonymous namespace
  483. void DebugTypeInfoRemoval::traverse(MDNode *N) {
  484. if (!N || Replacements.count(N))
  485. return;
  486. // To avoid cycles, as well as for efficiency sake, we will sometimes prune
  487. // parts of the graph.
  488. auto prune = [](MDNode *Parent, MDNode *Child) {
  489. if (auto *MDS = dyn_cast<DISubprogram>(Parent))
  490. return Child == MDS->getRetainedNodes().get();
  491. return false;
  492. };
  493. SmallVector<MDNode *, 16> ToVisit;
  494. DenseSet<MDNode *> Opened;
  495. // Visit each node starting at N in post order, and map them.
  496. ToVisit.push_back(N);
  497. while (!ToVisit.empty()) {
  498. auto *N = ToVisit.back();
  499. if (!Opened.insert(N).second) {
  500. // Close it.
  501. remap(N);
  502. ToVisit.pop_back();
  503. continue;
  504. }
  505. for (auto &I : N->operands())
  506. if (auto *MDN = dyn_cast_or_null<MDNode>(I))
  507. if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
  508. !isa<DICompileUnit>(MDN))
  509. ToVisit.push_back(MDN);
  510. }
  511. }
  512. bool llvm::stripNonLineTableDebugInfo(Module &M) {
  513. bool Changed = false;
  514. // First off, delete the debug intrinsics.
  515. auto RemoveUses = [&](StringRef Name) {
  516. if (auto *DbgVal = M.getFunction(Name)) {
  517. while (!DbgVal->use_empty())
  518. cast<Instruction>(DbgVal->user_back())->eraseFromParent();
  519. DbgVal->eraseFromParent();
  520. Changed = true;
  521. }
  522. };
  523. RemoveUses("llvm.dbg.declare");
  524. RemoveUses("llvm.dbg.value");
  525. // Delete non-CU debug info named metadata nodes.
  526. for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
  527. NMI != NME;) {
  528. NamedMDNode *NMD = &*NMI;
  529. ++NMI;
  530. // Specifically keep dbg.cu around.
  531. if (NMD->getName() == "llvm.dbg.cu")
  532. continue;
  533. }
  534. // Drop all dbg attachments from global variables.
  535. for (auto &GV : M.globals())
  536. GV.eraseMetadata(LLVMContext::MD_dbg);
  537. DebugTypeInfoRemoval Mapper(M.getContext());
  538. auto remap = [&](MDNode *Node) -> MDNode * {
  539. if (!Node)
  540. return nullptr;
  541. Mapper.traverseAndRemap(Node);
  542. auto *NewNode = Mapper.mapNode(Node);
  543. Changed |= Node != NewNode;
  544. Node = NewNode;
  545. return NewNode;
  546. };
  547. // Rewrite the DebugLocs to be equivalent to what
  548. // -gline-tables-only would have created.
  549. for (auto &F : M) {
  550. if (auto *SP = F.getSubprogram()) {
  551. Mapper.traverseAndRemap(SP);
  552. auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
  553. Changed |= SP != NewSP;
  554. F.setSubprogram(NewSP);
  555. }
  556. for (auto &BB : F) {
  557. for (auto &I : BB) {
  558. auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
  559. auto *Scope = DL.getScope();
  560. MDNode *InlinedAt = DL.getInlinedAt();
  561. Scope = remap(Scope);
  562. InlinedAt = remap(InlinedAt);
  563. return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
  564. };
  565. if (I.getDebugLoc() != DebugLoc())
  566. I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
  567. // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
  568. SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
  569. I.getAllMetadata(MDs);
  570. for (auto Attachment : MDs)
  571. if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
  572. for (unsigned N = 0; N < T->getNumOperands(); ++N)
  573. if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
  574. if (Loc != DebugLoc())
  575. T->replaceOperandWith(N, remapDebugLoc(Loc));
  576. }
  577. }
  578. }
  579. // Create a new llvm.dbg.cu, which is equivalent to the one
  580. // -gline-tables-only would have created.
  581. for (auto &NMD : M.getNamedMDList()) {
  582. SmallVector<MDNode *, 8> Ops;
  583. for (MDNode *Op : NMD.operands())
  584. Ops.push_back(remap(Op));
  585. if (!Changed)
  586. continue;
  587. NMD.clearOperands();
  588. for (auto *Op : Ops)
  589. if (Op)
  590. NMD.addOperand(Op);
  591. }
  592. return Changed;
  593. }
  594. unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
  595. if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
  596. M.getModuleFlag("Debug Info Version")))
  597. return Val->getZExtValue();
  598. return 0;
  599. }
  600. void Instruction::applyMergedLocation(const DILocation *LocA,
  601. const DILocation *LocB) {
  602. setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
  603. }
  604. //===----------------------------------------------------------------------===//
  605. // LLVM C API implementations.
  606. //===----------------------------------------------------------------------===//
  607. static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
  608. switch (lang) {
  609. #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
  610. case LLVMDWARFSourceLanguage##NAME: \
  611. return ID;
  612. #include "llvm/BinaryFormat/Dwarf.def"
  613. #undef HANDLE_DW_LANG
  614. }
  615. llvm_unreachable("Unhandled Tag");
  616. }
  617. template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
  618. return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
  619. }
  620. static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
  621. return static_cast<DINode::DIFlags>(Flags);
  622. }
  623. static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
  624. return static_cast<LLVMDIFlags>(Flags);
  625. }
  626. static DISubprogram::DISPFlags
  627. pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) {
  628. return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized);
  629. }
  630. unsigned LLVMDebugMetadataVersion() {
  631. return DEBUG_METADATA_VERSION;
  632. }
  633. LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
  634. return wrap(new DIBuilder(*unwrap(M), false));
  635. }
  636. LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
  637. return wrap(new DIBuilder(*unwrap(M)));
  638. }
  639. unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
  640. return getDebugMetadataVersionFromModule(*unwrap(M));
  641. }
  642. LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
  643. return StripDebugInfo(*unwrap(M));
  644. }
  645. void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
  646. delete unwrap(Builder);
  647. }
  648. void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
  649. unwrap(Builder)->finalize();
  650. }
  651. LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
  652. LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
  653. LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
  654. LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
  655. unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
  656. LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
  657. LLVMBool DebugInfoForProfiling) {
  658. auto File = unwrapDI<DIFile>(FileRef);
  659. return wrap(unwrap(Builder)->createCompileUnit(
  660. map_from_llvmDWARFsourcelanguage(Lang), File,
  661. StringRef(Producer, ProducerLen), isOptimized,
  662. StringRef(Flags, FlagsLen), RuntimeVer,
  663. StringRef(SplitName, SplitNameLen),
  664. static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
  665. SplitDebugInlining, DebugInfoForProfiling));
  666. }
  667. LLVMMetadataRef
  668. LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
  669. size_t FilenameLen, const char *Directory,
  670. size_t DirectoryLen) {
  671. return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
  672. StringRef(Directory, DirectoryLen)));
  673. }
  674. LLVMMetadataRef
  675. LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
  676. const char *Name, size_t NameLen,
  677. const char *ConfigMacros, size_t ConfigMacrosLen,
  678. const char *IncludePath, size_t IncludePathLen,
  679. const char *ISysRoot, size_t ISysRootLen) {
  680. return wrap(unwrap(Builder)->createModule(
  681. unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
  682. StringRef(ConfigMacros, ConfigMacrosLen),
  683. StringRef(IncludePath, IncludePathLen),
  684. StringRef(ISysRoot, ISysRootLen)));
  685. }
  686. LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
  687. LLVMMetadataRef ParentScope,
  688. const char *Name, size_t NameLen,
  689. LLVMBool ExportSymbols) {
  690. return wrap(unwrap(Builder)->createNameSpace(
  691. unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
  692. }
  693. LLVMMetadataRef LLVMDIBuilderCreateFunction(
  694. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  695. size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
  696. LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
  697. LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
  698. unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
  699. return wrap(unwrap(Builder)->createFunction(
  700. unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
  701. unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine,
  702. map_from_llvmDIFlags(Flags),
  703. pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr,
  704. nullptr, nullptr));
  705. }
  706. LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
  707. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
  708. LLVMMetadataRef File, unsigned Line, unsigned Col) {
  709. return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
  710. unwrapDI<DIFile>(File),
  711. Line, Col));
  712. }
  713. LLVMMetadataRef
  714. LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
  715. LLVMMetadataRef Scope,
  716. LLVMMetadataRef File,
  717. unsigned Discriminator) {
  718. return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
  719. unwrapDI<DIFile>(File),
  720. Discriminator));
  721. }
  722. LLVMMetadataRef
  723. LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
  724. LLVMMetadataRef Scope,
  725. LLVMMetadataRef NS,
  726. LLVMMetadataRef File,
  727. unsigned Line) {
  728. return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
  729. unwrapDI<DINamespace>(NS),
  730. unwrapDI<DIFile>(File),
  731. Line));
  732. }
  733. LLVMMetadataRef
  734. LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
  735. LLVMMetadataRef Scope,
  736. LLVMMetadataRef ImportedEntity,
  737. LLVMMetadataRef File,
  738. unsigned Line) {
  739. return wrap(unwrap(Builder)->createImportedModule(
  740. unwrapDI<DIScope>(Scope),
  741. unwrapDI<DIImportedEntity>(ImportedEntity),
  742. unwrapDI<DIFile>(File), Line));
  743. }
  744. LLVMMetadataRef
  745. LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
  746. LLVMMetadataRef Scope,
  747. LLVMMetadataRef M,
  748. LLVMMetadataRef File,
  749. unsigned Line) {
  750. return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
  751. unwrapDI<DIModule>(M),
  752. unwrapDI<DIFile>(File),
  753. Line));
  754. }
  755. LLVMMetadataRef
  756. LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
  757. LLVMMetadataRef Scope,
  758. LLVMMetadataRef Decl,
  759. LLVMMetadataRef File,
  760. unsigned Line,
  761. const char *Name, size_t NameLen) {
  762. return wrap(unwrap(Builder)->createImportedDeclaration(
  763. unwrapDI<DIScope>(Scope),
  764. unwrapDI<DINode>(Decl),
  765. unwrapDI<DIFile>(File), Line, {Name, NameLen}));
  766. }
  767. LLVMMetadataRef
  768. LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
  769. unsigned Column, LLVMMetadataRef Scope,
  770. LLVMMetadataRef InlinedAt) {
  771. return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
  772. unwrap(InlinedAt)));
  773. }
  774. unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
  775. return unwrapDI<DILocation>(Location)->getLine();
  776. }
  777. unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
  778. return unwrapDI<DILocation>(Location)->getColumn();
  779. }
  780. LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
  781. return wrap(unwrapDI<DILocation>(Location)->getScope());
  782. }
  783. LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) {
  784. return wrap(unwrapDI<DILocation>(Location)->getInlinedAt());
  785. }
  786. LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) {
  787. return wrap(unwrapDI<DIScope>(Scope)->getFile());
  788. }
  789. const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) {
  790. auto Dir = unwrapDI<DIFile>(File)->getDirectory();
  791. *Len = Dir.size();
  792. return Dir.data();
  793. }
  794. const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) {
  795. auto Name = unwrapDI<DIFile>(File)->getFilename();
  796. *Len = Name.size();
  797. return Name.data();
  798. }
  799. const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) {
  800. if (auto Src = unwrapDI<DIFile>(File)->getSource()) {
  801. *Len = Src->size();
  802. return Src->data();
  803. }
  804. *Len = 0;
  805. return "";
  806. }
  807. LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
  808. const char *Name, size_t NameLen,
  809. int64_t Value,
  810. LLVMBool IsUnsigned) {
  811. return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value,
  812. IsUnsigned != 0));
  813. }
  814. LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
  815. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  816. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  817. uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
  818. unsigned NumElements, LLVMMetadataRef ClassTy) {
  819. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  820. NumElements});
  821. return wrap(unwrap(Builder)->createEnumerationType(
  822. unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
  823. LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
  824. }
  825. LLVMMetadataRef LLVMDIBuilderCreateUnionType(
  826. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  827. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  828. uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
  829. LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
  830. const char *UniqueId, size_t UniqueIdLen) {
  831. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  832. NumElements});
  833. return wrap(unwrap(Builder)->createUnionType(
  834. unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
  835. LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
  836. Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
  837. }
  838. LLVMMetadataRef
  839. LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
  840. uint32_t AlignInBits, LLVMMetadataRef Ty,
  841. LLVMMetadataRef *Subscripts,
  842. unsigned NumSubscripts) {
  843. auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
  844. NumSubscripts});
  845. return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
  846. unwrapDI<DIType>(Ty), Subs));
  847. }
  848. LLVMMetadataRef
  849. LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
  850. uint32_t AlignInBits, LLVMMetadataRef Ty,
  851. LLVMMetadataRef *Subscripts,
  852. unsigned NumSubscripts) {
  853. auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
  854. NumSubscripts});
  855. return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
  856. unwrapDI<DIType>(Ty), Subs));
  857. }
  858. LLVMMetadataRef
  859. LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
  860. size_t NameLen, uint64_t SizeInBits,
  861. LLVMDWARFTypeEncoding Encoding,
  862. LLVMDIFlags Flags) {
  863. return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
  864. SizeInBits, Encoding,
  865. map_from_llvmDIFlags(Flags)));
  866. }
  867. LLVMMetadataRef LLVMDIBuilderCreatePointerType(
  868. LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
  869. uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
  870. const char *Name, size_t NameLen) {
  871. return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
  872. SizeInBits, AlignInBits,
  873. AddressSpace, {Name, NameLen}));
  874. }
  875. LLVMMetadataRef LLVMDIBuilderCreateStructType(
  876. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  877. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  878. uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
  879. LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
  880. unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
  881. const char *UniqueId, size_t UniqueIdLen) {
  882. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  883. NumElements});
  884. return wrap(unwrap(Builder)->createStructType(
  885. unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
  886. LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
  887. unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
  888. unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
  889. }
  890. LLVMMetadataRef LLVMDIBuilderCreateMemberType(
  891. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  892. size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
  893. uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
  894. LLVMMetadataRef Ty) {
  895. return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
  896. {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
  897. OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
  898. }
  899. LLVMMetadataRef
  900. LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
  901. size_t NameLen) {
  902. return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
  903. }
  904. LLVMMetadataRef
  905. LLVMDIBuilderCreateStaticMemberType(
  906. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  907. size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
  908. LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
  909. uint32_t AlignInBits) {
  910. return wrap(unwrap(Builder)->createStaticMemberType(
  911. unwrapDI<DIScope>(Scope), {Name, NameLen},
  912. unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
  913. map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
  914. AlignInBits));
  915. }
  916. LLVMMetadataRef
  917. LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
  918. const char *Name, size_t NameLen,
  919. LLVMMetadataRef File, unsigned LineNo,
  920. uint64_t SizeInBits, uint32_t AlignInBits,
  921. uint64_t OffsetInBits, LLVMDIFlags Flags,
  922. LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
  923. return wrap(unwrap(Builder)->createObjCIVar(
  924. {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
  925. SizeInBits, AlignInBits, OffsetInBits,
  926. map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
  927. unwrapDI<MDNode>(PropertyNode)));
  928. }
  929. LLVMMetadataRef
  930. LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
  931. const char *Name, size_t NameLen,
  932. LLVMMetadataRef File, unsigned LineNo,
  933. const char *GetterName, size_t GetterNameLen,
  934. const char *SetterName, size_t SetterNameLen,
  935. unsigned PropertyAttributes,
  936. LLVMMetadataRef Ty) {
  937. return wrap(unwrap(Builder)->createObjCProperty(
  938. {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
  939. {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
  940. PropertyAttributes, unwrapDI<DIType>(Ty)));
  941. }
  942. LLVMMetadataRef
  943. LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
  944. LLVMMetadataRef Type) {
  945. return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
  946. }
  947. LLVMMetadataRef
  948. LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
  949. const char *Name, size_t NameLen,
  950. LLVMMetadataRef File, unsigned LineNo,
  951. LLVMMetadataRef Scope) {
  952. return wrap(unwrap(Builder)->createTypedef(
  953. unwrapDI<DIType>(Type), {Name, NameLen},
  954. unwrapDI<DIFile>(File), LineNo,
  955. unwrapDI<DIScope>(Scope)));
  956. }
  957. LLVMMetadataRef
  958. LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
  959. LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
  960. uint64_t BaseOffset, uint32_t VBPtrOffset,
  961. LLVMDIFlags Flags) {
  962. return wrap(unwrap(Builder)->createInheritance(
  963. unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
  964. BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
  965. }
  966. LLVMMetadataRef
  967. LLVMDIBuilderCreateForwardDecl(
  968. LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
  969. size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
  970. unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
  971. const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
  972. return wrap(unwrap(Builder)->createForwardDecl(
  973. Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
  974. unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
  975. AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
  976. }
  977. LLVMMetadataRef
  978. LLVMDIBuilderCreateReplaceableCompositeType(
  979. LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
  980. size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
  981. unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
  982. LLVMDIFlags Flags, const char *UniqueIdentifier,
  983. size_t UniqueIdentifierLen) {
  984. return wrap(unwrap(Builder)->createReplaceableCompositeType(
  985. Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
  986. unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
  987. AlignInBits, map_from_llvmDIFlags(Flags),
  988. {UniqueIdentifier, UniqueIdentifierLen}));
  989. }
  990. LLVMMetadataRef
  991. LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
  992. LLVMMetadataRef Type) {
  993. return wrap(unwrap(Builder)->createQualifiedType(Tag,
  994. unwrapDI<DIType>(Type)));
  995. }
  996. LLVMMetadataRef
  997. LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
  998. LLVMMetadataRef Type) {
  999. return wrap(unwrap(Builder)->createReferenceType(Tag,
  1000. unwrapDI<DIType>(Type)));
  1001. }
  1002. LLVMMetadataRef
  1003. LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
  1004. return wrap(unwrap(Builder)->createNullPtrType());
  1005. }
  1006. LLVMMetadataRef
  1007. LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
  1008. LLVMMetadataRef PointeeType,
  1009. LLVMMetadataRef ClassType,
  1010. uint64_t SizeInBits,
  1011. uint32_t AlignInBits,
  1012. LLVMDIFlags Flags) {
  1013. return wrap(unwrap(Builder)->createMemberPointerType(
  1014. unwrapDI<DIType>(PointeeType),
  1015. unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
  1016. map_from_llvmDIFlags(Flags)));
  1017. }
  1018. LLVMMetadataRef
  1019. LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
  1020. LLVMMetadataRef Scope,
  1021. const char *Name, size_t NameLen,
  1022. LLVMMetadataRef File, unsigned LineNumber,
  1023. uint64_t SizeInBits,
  1024. uint64_t OffsetInBits,
  1025. uint64_t StorageOffsetInBits,
  1026. LLVMDIFlags Flags, LLVMMetadataRef Type) {
  1027. return wrap(unwrap(Builder)->createBitFieldMemberType(
  1028. unwrapDI<DIScope>(Scope), {Name, NameLen},
  1029. unwrapDI<DIFile>(File), LineNumber,
  1030. SizeInBits, OffsetInBits, StorageOffsetInBits,
  1031. map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
  1032. }
  1033. LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
  1034. LLVMMetadataRef Scope, const char *Name, size_t NameLen,
  1035. LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
  1036. uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
  1037. LLVMMetadataRef DerivedFrom,
  1038. LLVMMetadataRef *Elements, unsigned NumElements,
  1039. LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
  1040. const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
  1041. auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
  1042. NumElements});
  1043. return wrap(unwrap(Builder)->createClassType(
  1044. unwrapDI<DIScope>(Scope), {Name, NameLen},
  1045. unwrapDI<DIFile>(File), LineNumber,
  1046. SizeInBits, AlignInBits, OffsetInBits,
  1047. map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
  1048. Elts, unwrapDI<DIType>(VTableHolder),
  1049. unwrapDI<MDNode>(TemplateParamsNode),
  1050. {UniqueIdentifier, UniqueIdentifierLen}));
  1051. }
  1052. LLVMMetadataRef
  1053. LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
  1054. LLVMMetadataRef Type) {
  1055. return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
  1056. }
  1057. const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
  1058. StringRef Str = unwrap<DIType>(DType)->getName();
  1059. *Length = Str.size();
  1060. return Str.data();
  1061. }
  1062. uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
  1063. return unwrapDI<DIType>(DType)->getSizeInBits();
  1064. }
  1065. uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
  1066. return unwrapDI<DIType>(DType)->getOffsetInBits();
  1067. }
  1068. uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
  1069. return unwrapDI<DIType>(DType)->getAlignInBits();
  1070. }
  1071. unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
  1072. return unwrapDI<DIType>(DType)->getLine();
  1073. }
  1074. LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
  1075. return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
  1076. }
  1077. LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
  1078. LLVMMetadataRef *Types,
  1079. size_t Length) {
  1080. return wrap(
  1081. unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
  1082. }
  1083. LLVMMetadataRef
  1084. LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
  1085. LLVMMetadataRef File,
  1086. LLVMMetadataRef *ParameterTypes,
  1087. unsigned NumParameterTypes,
  1088. LLVMDIFlags Flags) {
  1089. auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
  1090. NumParameterTypes});
  1091. return wrap(unwrap(Builder)->createSubroutineType(
  1092. Elts, map_from_llvmDIFlags(Flags)));
  1093. }
  1094. LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
  1095. int64_t *Addr, size_t Length) {
  1096. return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
  1097. Length)));
  1098. }
  1099. LLVMMetadataRef
  1100. LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
  1101. int64_t Value) {
  1102. return wrap(unwrap(Builder)->createConstantValueExpression(Value));
  1103. }
  1104. LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
  1105. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1106. size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
  1107. unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
  1108. LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
  1109. return wrap(unwrap(Builder)->createGlobalVariableExpression(
  1110. unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
  1111. unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
  1112. unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl),
  1113. nullptr, AlignInBits));
  1114. }
  1115. LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) {
  1116. return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getVariable());
  1117. }
  1118. LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression(
  1119. LLVMMetadataRef GVE) {
  1120. return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getExpression());
  1121. }
  1122. LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) {
  1123. return wrap(unwrapDI<DIVariable>(Var)->getFile());
  1124. }
  1125. LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) {
  1126. return wrap(unwrapDI<DIVariable>(Var)->getScope());
  1127. }
  1128. unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) {
  1129. return unwrapDI<DIVariable>(Var)->getLine();
  1130. }
  1131. LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
  1132. size_t Count) {
  1133. return wrap(
  1134. MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
  1135. }
  1136. void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
  1137. MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
  1138. }
  1139. void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
  1140. LLVMMetadataRef Replacement) {
  1141. auto *Node = unwrapDI<MDNode>(TargetMetadata);
  1142. Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
  1143. MDNode::deleteTemporary(Node);
  1144. }
  1145. LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
  1146. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1147. size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
  1148. unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
  1149. LLVMMetadataRef Decl, uint32_t AlignInBits) {
  1150. return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
  1151. unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
  1152. unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
  1153. unwrapDI<MDNode>(Decl), nullptr, AlignInBits));
  1154. }
  1155. LLVMValueRef
  1156. LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage,
  1157. LLVMMetadataRef VarInfo, LLVMMetadataRef Expr,
  1158. LLVMMetadataRef DL, LLVMValueRef Instr) {
  1159. return wrap(unwrap(Builder)->insertDeclare(
  1160. unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
  1161. unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
  1162. unwrap<Instruction>(Instr)));
  1163. }
  1164. LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
  1165. LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
  1166. LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
  1167. return wrap(unwrap(Builder)->insertDeclare(
  1168. unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
  1169. unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
  1170. unwrap(Block)));
  1171. }
  1172. LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
  1173. LLVMValueRef Val,
  1174. LLVMMetadataRef VarInfo,
  1175. LLVMMetadataRef Expr,
  1176. LLVMMetadataRef DebugLoc,
  1177. LLVMValueRef Instr) {
  1178. return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
  1179. unwrap(Val), unwrap<DILocalVariable>(VarInfo),
  1180. unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
  1181. unwrap<Instruction>(Instr)));
  1182. }
  1183. LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
  1184. LLVMValueRef Val,
  1185. LLVMMetadataRef VarInfo,
  1186. LLVMMetadataRef Expr,
  1187. LLVMMetadataRef DebugLoc,
  1188. LLVMBasicBlockRef Block) {
  1189. return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
  1190. unwrap(Val), unwrap<DILocalVariable>(VarInfo),
  1191. unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
  1192. unwrap(Block)));
  1193. }
  1194. LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
  1195. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1196. size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
  1197. LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
  1198. return wrap(unwrap(Builder)->createAutoVariable(
  1199. unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
  1200. LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
  1201. map_from_llvmDIFlags(Flags), AlignInBits));
  1202. }
  1203. LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
  1204. LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
  1205. size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
  1206. LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
  1207. return wrap(unwrap(Builder)->createParameterVariable(
  1208. unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
  1209. LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
  1210. map_from_llvmDIFlags(Flags)));
  1211. }
  1212. LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
  1213. int64_t Lo, int64_t Count) {
  1214. return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
  1215. }
  1216. LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
  1217. LLVMMetadataRef *Data,
  1218. size_t Length) {
  1219. Metadata **DataValue = unwrap(Data);
  1220. return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
  1221. }
  1222. LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
  1223. return wrap(unwrap<Function>(Func)->getSubprogram());
  1224. }
  1225. void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
  1226. unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
  1227. }
  1228. unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) {
  1229. return unwrapDI<DISubprogram>(Subprogram)->getLine();
  1230. }
  1231. LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) {
  1232. return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode());
  1233. }
  1234. void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) {
  1235. if (Loc)
  1236. unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc)));
  1237. else
  1238. unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc());
  1239. }
  1240. LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
  1241. switch(unwrap(Metadata)->getMetadataID()) {
  1242. #define HANDLE_METADATA_LEAF(CLASS) \
  1243. case Metadata::CLASS##Kind: \
  1244. return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
  1245. #include "llvm/IR/Metadata.def"
  1246. default:
  1247. return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;
  1248. }
  1249. }