blob: ca2dad306b4682f915517107bc907e42ce997932 [file] [log] [blame]
Eugene Zelenkof53a7b42017-05-05 22:30:37 +00001//===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
whitequark789164d2017-11-01 22:18:52 +000015#include "llvm-c/DebugInfo.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/None.h"
whitequark789164d2017-11-01 22:18:52 +000019#include "llvm/ADT/STLExtras.h"
Bill Wendling523bea82013-11-08 08:13:15 +000020#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/BasicBlock.h"
Bill Wendling523bea82013-11-08 08:13:15 +000024#include "llvm/IR/Constants.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000025#include "llvm/IR/DebugInfoMetadata.h"
26#include "llvm/IR/DebugLoc.h"
whitequark789164d2017-11-01 22:18:52 +000027#include "llvm/IR/DebugInfo.h"
28#include "llvm/IR/DIBuilder.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000029#include "llvm/IR/Function.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000030#include "llvm/IR/GVMaterializer.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000031#include "llvm/IR/Instruction.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000033#include "llvm/IR/LLVMContext.h"
34#include "llvm/IR/Metadata.h"
Bill Wendling523bea82013-11-08 08:13:15 +000035#include "llvm/IR/Module.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000036#include "llvm/Support/Casting.h"
37#include <algorithm>
38#include <cassert>
39#include <utility>
40
Bill Wendling523bea82013-11-08 08:13:15 +000041using namespace llvm;
42using namespace llvm::dwarf;
43
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000044DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
45 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000046 return LocalScope->getSubprogram();
47 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000048}
49
Bill Wendling523bea82013-11-08 08:13:15 +000050//===----------------------------------------------------------------------===//
51// DebugInfoFinder implementations.
52//===----------------------------------------------------------------------===//
53
54void DebugInfoFinder::reset() {
55 CUs.clear();
56 SPs.clear();
57 GVs.clear();
58 TYs.clear();
59 Scopes.clear();
60 NodesSeen.clear();
Bill Wendling523bea82013-11-08 08:13:15 +000061}
62
Bill Wendling523bea82013-11-08 08:13:15 +000063void DebugInfoFinder::processModule(const Module &M) {
Adrian Prantl5992a722016-04-08 22:43:03 +000064 for (auto *CU : M.debug_compile_units()) {
65 addCompileUnit(CU);
Adrian Prantlbceaaa92016-12-20 02:09:43 +000066 for (auto DIG : CU->getGlobalVariables()) {
67 if (!addGlobalVariable(DIG))
68 continue;
69 auto *GV = DIG->getVariable();
70 processScope(GV->getScope());
71 processType(GV->getType().resolve());
Adrian Prantl5992a722016-04-08 22:43:03 +000072 }
Adrian Prantl5992a722016-04-08 22:43:03 +000073 for (auto *ET : CU->getEnumTypes())
74 processType(ET);
75 for (auto *RT : CU->getRetainedTypes())
Adrian Prantl75819ae2016-04-15 15:57:41 +000076 if (auto *T = dyn_cast<DIType>(RT))
77 processType(T);
78 else
79 processSubprogram(cast<DISubprogram>(RT));
Adrian Prantl5992a722016-04-08 22:43:03 +000080 for (auto *Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000081 auto *Entity = Import->getEntity().resolve();
Adrian Prantl5992a722016-04-08 22:43:03 +000082 if (auto *T = dyn_cast<DIType>(Entity))
83 processType(T);
84 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000085 processSubprogram(SP);
Adrian Prantl5992a722016-04-08 22:43:03 +000086 else if (auto *NS = dyn_cast<DINamespace>(Entity))
87 processScope(NS->getScope());
88 else if (auto *M = dyn_cast<DIModule>(Entity))
89 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +000090 }
91 }
Keno Fischer30779772017-04-11 13:32:11 +000092 for (auto &F : M.functions()) {
Adrian Prantl75819ae2016-04-15 15:57:41 +000093 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
94 processSubprogram(SP);
Keno Fischer30779772017-04-11 13:32:11 +000095 // There could be subprograms from inlined functions referenced from
96 // instructions only. Walk the function to find them.
97 for (const BasicBlock &BB : F) {
98 for (const Instruction &I : BB) {
99 if (!I.getDebugLoc())
100 continue;
101 processLocation(M, I.getDebugLoc().get());
102 }
103 }
104 }
Bill Wendling523bea82013-11-08 08:13:15 +0000105}
106
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000107void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000108 if (!Loc)
109 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000110 processScope(Loc->getScope());
111 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000112}
113
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000114void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000115 if (!addType(DT))
116 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000117 processScope(DT->getScope().resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000118 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
119 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000120 processType(Ref.resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000121 return;
122 }
123 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000124 processType(DCT->getBaseType().resolve());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000125 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000126 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000127 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000128 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000129 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000130 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000131 return;
132 }
133 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000134 processType(DDT->getBaseType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000135 }
136}
137
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000138void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000139 if (!Scope)
140 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000142 processType(Ty);
143 return;
144 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000145 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000146 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000147 return;
148 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000150 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000151 return;
152 }
153 if (!addScope(Scope))
154 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000155 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000156 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000157 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000158 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000159 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
160 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000161 }
162}
163
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000164void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000165 if (!addSubprogram(SP))
166 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000167 processScope(SP->getScope().resolve());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000168 processType(SP->getType());
169 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000170 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000171 processType(TType->getType().resolve());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000172 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000173 processType(TVal->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000174 }
175 }
176}
177
Manman Ren2085ccc2013-11-17 18:42:37 +0000178void DebugInfoFinder::processDeclare(const Module &M,
179 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000180 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000181 if (!N)
182 return;
183
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000184 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000185 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000186 return;
187
David Blaikie70573dc2014-11-19 07:49:26 +0000188 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000189 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000190 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000191 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000192}
193
Manman Ren2085ccc2013-11-17 18:42:37 +0000194void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000195 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000196 if (!N)
197 return;
198
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000199 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000200 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000201 return;
202
David Blaikie70573dc2014-11-19 07:49:26 +0000203 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000204 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000205 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000206 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000207}
208
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000209bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000210 if (!DT)
211 return false;
212
David Blaikie70573dc2014-11-19 07:49:26 +0000213 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000214 return false;
215
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000216 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000217 return true;
218}
219
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000220bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000221 if (!CU)
222 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000223 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000224 return false;
225
226 CUs.push_back(CU);
227 return true;
228}
229
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000230bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
David Blaikie70573dc2014-11-19 07:49:26 +0000231 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000232 return false;
233
234 GVs.push_back(DIG);
235 return true;
236}
237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000239 if (!SP)
240 return false;
241
David Blaikie70573dc2014-11-19 07:49:26 +0000242 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000243 return false;
244
245 SPs.push_back(SP);
246 return true;
247}
248
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000249bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000250 if (!Scope)
251 return false;
252 // FIXME: Ocaml binding generates a scope with no content, we treat it
253 // as null for now.
254 if (Scope->getNumOperands() == 0)
255 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000256 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000257 return false;
258 Scopes.push_back(Scope);
259 return true;
260}
261
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000262static MDNode *stripDebugLocFromLoopID(MDNode *N) {
Daniel Sandersb96a9452017-01-28 11:22:05 +0000263 assert(N->op_begin() != N->op_end() && "Missing self reference?");
Daniel Sandersb96a9452017-01-28 11:22:05 +0000264
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000265 // if there is no debug location, we do not have to rewrite this MDNode.
266 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
267 return isa<DILocation>(Op.get());
268 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000269 return N;
270
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000271 // If there is only the debug location without any actual loop metadata, we
Daniel Sandersb96a9452017-01-28 11:22:05 +0000272 // can remove the metadata.
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000273 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
274 return !isa<DILocation>(Op.get());
275 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000276 return nullptr;
277
278 SmallVector<Metadata *, 4> Args;
279 // Reserve operand 0 for loop id self reference.
280 auto TempNode = MDNode::getTemporary(N->getContext(), None);
281 Args.push_back(TempNode.get());
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000282 // Add all non-debug location operands back.
283 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
284 if (!isa<DILocation>(*Op))
285 Args.push_back(*Op);
286 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000287
288 // Set the first operand to itself.
289 MDNode *LoopID = MDNode::get(N->getContext(), Args);
290 LoopID->replaceOperandWith(0, LoopID);
291 return LoopID;
292}
293
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000294bool llvm::stripDebugInfo(Function &F) {
295 bool Changed = false;
Adrian Prantla8b2ddb2017-10-02 18:31:29 +0000296 if (F.getMetadata(LLVMContext::MD_dbg)) {
Peter Collingbourned4bff302015-11-05 22:03:56 +0000297 Changed = true;
298 F.setSubprogram(nullptr);
299 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000300
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000301 DenseMap<MDNode*, MDNode*> LoopIDsMap;
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000302 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000303 for (auto II = BB.begin(), End = BB.end(); II != End;) {
304 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000305 if (isa<DbgInfoIntrinsic>(&I)) {
306 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000307 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000308 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000309 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000310 if (I.getDebugLoc()) {
311 Changed = true;
312 I.setDebugLoc(DebugLoc());
313 }
314 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000315
316 auto *TermInst = BB.getTerminator();
Justin Bognerb29bebe2017-08-18 21:38:03 +0000317 if (!TermInst)
318 // This is invalid IR, but we may not have run the verifier yet
319 continue;
Daniel Sandersb96a9452017-01-28 11:22:05 +0000320 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
321 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
322 if (!NewLoopID)
323 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
324 if (NewLoopID != LoopID)
325 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
326 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000327 }
328 return Changed;
329}
330
Manman Rencb14bbc2013-11-22 22:06:31 +0000331bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000332 bool Changed = false;
333
Manman Rencb14bbc2013-11-22 22:06:31 +0000334 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
335 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000336 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000337 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000338
339 // We're stripping debug info, and without them, coverage information
340 // doesn't quite make sense.
341 if (NMD->getName().startswith("llvm.dbg.") ||
342 NMD->getName() == "llvm.gcov") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000343 NMD->eraseFromParent();
344 Changed = true;
345 }
346 }
347
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000348 for (Function &F : M)
349 Changed |= stripDebugInfo(F);
350
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000351 for (auto &GV : M.globals()) {
352 SmallVector<MDNode *, 1> MDs;
353 GV.getMetadata(LLVMContext::MD_dbg, MDs);
354 if (!MDs.empty()) {
355 GV.eraseMetadata(LLVMContext::MD_dbg);
356 Changed = true;
357 }
358 }
359
Rafael Espindola468b8682015-04-01 14:44:59 +0000360 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000361 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000362
363 return Changed;
364}
Manman Ren8b4306c2013-12-02 21:29:56 +0000365
Michael Ilsemane5428042016-10-25 18:44:13 +0000366namespace {
367
368/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
369class DebugTypeInfoRemoval {
370 DenseMap<Metadata *, Metadata *> Replacements;
371
372public:
373 /// The (void)() type.
374 MDNode *EmptySubroutineType;
375
376private:
377 /// Remember what linkage name we originally had before stripping. If we end
378 /// up making two subprograms identical who originally had different linkage
379 /// names, then we need to make one of them distinct, to avoid them getting
380 /// uniqued. Maps the new node to the old linkage name.
381 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
382
383 // TODO: Remember the distinct subprogram we created for a given linkage name,
384 // so that we can continue to unique whenever possible. Map <newly created
385 // node, old linkage name> to the first (possibly distinct) mdsubprogram
386 // created for that combination. This is not strictly needed for correctness,
387 // but can cut down on the number of MDNodes and let us diff cleanly with the
388 // output of -gline-tables-only.
389
390public:
391 DebugTypeInfoRemoval(LLVMContext &C)
392 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
393 MDNode::get(C, {}))) {}
394
395 Metadata *map(Metadata *M) {
396 if (!M)
397 return nullptr;
398 auto Replacement = Replacements.find(M);
399 if (Replacement != Replacements.end())
400 return Replacement->second;
401
402 return M;
403 }
404 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
405
406 /// Recursively remap N and all its referenced children. Does a DF post-order
407 /// traversal, so as to remap bottoms up.
408 void traverseAndRemap(MDNode *N) { traverse(N); }
409
410private:
411 // Create a new DISubprogram, to replace the one given.
412 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
413 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
414 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
415 DISubprogram *Declaration = nullptr;
416 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
417 DITypeRef ContainingType(map(MDS->getContainingType()));
418 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
419 auto Variables = nullptr;
420 auto TemplateParams = nullptr;
421
422 // Make a distinct DISubprogram, for situations that warrent it.
423 auto distinctMDSubprogram = [&]() {
424 return DISubprogram::getDistinct(
425 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
426 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
427 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
428 MDS->getVirtuality(), MDS->getVirtualIndex(),
429 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
430 TemplateParams, Declaration, Variables);
431 };
432
433 if (MDS->isDistinct())
434 return distinctMDSubprogram();
435
436 auto *NewMDS = DISubprogram::get(
437 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
438 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
439 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
440 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
441 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
442 Variables);
443
444 StringRef OldLinkageName = MDS->getLinkageName();
445
446 // See if we need to make a distinct one.
447 auto OrigLinkage = NewToLinkageName.find(NewMDS);
448 if (OrigLinkage != NewToLinkageName.end()) {
449 if (OrigLinkage->second == OldLinkageName)
450 // We're good.
451 return NewMDS;
452
453 // Otherwise, need to make a distinct one.
454 // TODO: Query the map to see if we already have one.
455 return distinctMDSubprogram();
456 }
457
458 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
459 return NewMDS;
460 }
461
462 /// Create a new compile unit, to replace the one given
463 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
464 // Drop skeleton CUs.
465 if (CU->getDWOId())
466 return nullptr;
467
468 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
469 MDTuple *EnumTypes = nullptr;
470 MDTuple *RetainedTypes = nullptr;
471 MDTuple *GlobalVariables = nullptr;
472 MDTuple *ImportedEntities = nullptr;
473 return DICompileUnit::getDistinct(
474 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
475 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
476 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
477 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
Dehao Chen0944a8c2017-02-01 22:45:09 +0000478 CU->getDWOId(), CU->getSplitDebugInlining(),
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000479 CU->getDebugInfoForProfiling(), CU->getGnuPubnames());
Michael Ilsemane5428042016-10-25 18:44:13 +0000480 }
481
482 DILocation *getReplacementMDLocation(DILocation *MLD) {
483 auto *Scope = map(MLD->getScope());
484 auto *InlinedAt = map(MLD->getInlinedAt());
485 if (MLD->isDistinct())
486 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
487 MLD->getColumn(), Scope, InlinedAt);
488 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
489 Scope, InlinedAt);
490 }
491
492 /// Create a new generic MDNode, to replace the one given
493 MDNode *getReplacementMDNode(MDNode *N) {
494 SmallVector<Metadata *, 8> Ops;
495 Ops.reserve(N->getNumOperands());
496 for (auto &I : N->operands())
497 if (I)
498 Ops.push_back(map(I));
499 auto *Ret = MDNode::get(N->getContext(), Ops);
500 return Ret;
501 }
502
503 /// Attempt to re-map N to a newly created node.
504 void remap(MDNode *N) {
505 if (Replacements.count(N))
506 return;
507
508 auto doRemap = [&](MDNode *N) -> MDNode * {
509 if (!N)
510 return nullptr;
511 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
512 remap(MDSub->getUnit());
513 return getReplacementSubprogram(MDSub);
514 }
515 if (isa<DISubroutineType>(N))
516 return EmptySubroutineType;
517 if (auto *CU = dyn_cast<DICompileUnit>(N))
518 return getReplacementCU(CU);
519 if (isa<DIFile>(N))
520 return N;
521 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
522 // Remap to our referenced scope (recursively).
523 return mapNode(MDLB->getScope());
524 if (auto *MLD = dyn_cast<DILocation>(N))
525 return getReplacementMDLocation(MLD);
526
527 // Otherwise, if we see these, just drop them now. Not strictly necessary,
528 // but this speeds things up a little.
529 if (isa<DINode>(N))
530 return nullptr;
531
532 return getReplacementMDNode(N);
533 };
534 Replacements[N] = doRemap(N);
535 }
536
537 /// Do the remapping traversal.
538 void traverse(MDNode *);
539};
540
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000541} // end anonymous namespace
Michael Ilsemane5428042016-10-25 18:44:13 +0000542
543void DebugTypeInfoRemoval::traverse(MDNode *N) {
544 if (!N || Replacements.count(N))
545 return;
546
547 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
548 // parts of the graph.
549 auto prune = [](MDNode *Parent, MDNode *Child) {
550 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
551 return Child == MDS->getVariables().get();
552 return false;
553 };
554
555 SmallVector<MDNode *, 16> ToVisit;
556 DenseSet<MDNode *> Opened;
557
558 // Visit each node starting at N in post order, and map them.
559 ToVisit.push_back(N);
560 while (!ToVisit.empty()) {
561 auto *N = ToVisit.back();
562 if (!Opened.insert(N).second) {
563 // Close it.
564 remap(N);
565 ToVisit.pop_back();
566 continue;
567 }
568 for (auto &I : N->operands())
569 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
570 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
571 !isa<DICompileUnit>(MDN))
572 ToVisit.push_back(MDN);
573 }
574}
575
576bool llvm::stripNonLineTableDebugInfo(Module &M) {
577 bool Changed = false;
578
579 // First off, delete the debug intrinsics.
580 auto RemoveUses = [&](StringRef Name) {
581 if (auto *DbgVal = M.getFunction(Name)) {
582 while (!DbgVal->use_empty())
583 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
584 DbgVal->eraseFromParent();
585 Changed = true;
586 }
587 };
588 RemoveUses("llvm.dbg.declare");
589 RemoveUses("llvm.dbg.value");
590
591 // Delete non-CU debug info named metadata nodes.
592 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
593 NMI != NME;) {
594 NamedMDNode *NMD = &*NMI;
595 ++NMI;
596 // Specifically keep dbg.cu around.
597 if (NMD->getName() == "llvm.dbg.cu")
598 continue;
599 }
600
601 // Drop all dbg attachments from global variables.
602 for (auto &GV : M.globals())
603 GV.eraseMetadata(LLVMContext::MD_dbg);
604
605 DebugTypeInfoRemoval Mapper(M.getContext());
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000606 auto remap = [&](MDNode *Node) -> MDNode * {
Michael Ilsemane5428042016-10-25 18:44:13 +0000607 if (!Node)
608 return nullptr;
609 Mapper.traverseAndRemap(Node);
610 auto *NewNode = Mapper.mapNode(Node);
611 Changed |= Node != NewNode;
612 Node = NewNode;
613 return NewNode;
614 };
615
616 // Rewrite the DebugLocs to be equivalent to what
617 // -gline-tables-only would have created.
618 for (auto &F : M) {
619 if (auto *SP = F.getSubprogram()) {
620 Mapper.traverseAndRemap(SP);
621 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
622 Changed |= SP != NewSP;
623 F.setSubprogram(NewSP);
624 }
625 for (auto &BB : F) {
626 for (auto &I : BB) {
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000627 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
628 auto *Scope = DL.getScope();
629 MDNode *InlinedAt = DL.getInlinedAt();
630 Scope = remap(Scope);
631 InlinedAt = remap(InlinedAt);
632 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
633 };
Michael Ilsemane5428042016-10-25 18:44:13 +0000634
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000635 if (I.getDebugLoc() != DebugLoc())
636 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
637
638 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
639 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
640 I.getAllMetadata(MDs);
641 for (auto Attachment : MDs)
642 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
643 for (unsigned N = 0; N < T->getNumOperands(); ++N)
644 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
645 if (Loc != DebugLoc())
646 T->replaceOperandWith(N, remapDebugLoc(Loc));
Michael Ilsemane5428042016-10-25 18:44:13 +0000647 }
648 }
649 }
650
651 // Create a new llvm.dbg.cu, which is equivalent to the one
652 // -gline-tables-only would have created.
653 for (auto &NMD : M.getNamedMDList()) {
654 SmallVector<MDNode *, 8> Ops;
655 for (MDNode *Op : NMD.operands())
656 Ops.push_back(remap(Op));
657
658 if (!Changed)
659 continue;
660
661 NMD.clearOperands();
662 for (auto *Op : Ops)
663 if (Op)
664 NMD.addOperand(Op);
665 }
666 return Changed;
667}
668
Manman Renbd4daf82013-12-03 00:12:14 +0000669unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000670 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000671 M.getModuleFlag("Debug Info Version")))
672 return Val->getZExtValue();
673 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000674}
Dehao Chenf4646272017-10-02 18:13:14 +0000675
676void Instruction::applyMergedLocation(const DILocation *LocA,
677 const DILocation *LocB) {
Vedant Kumar65b0d4d2018-04-12 20:58:24 +0000678 setDebugLoc(DILocation::getMergedLocation(LocA, LocB,
679 DILocation::WithGeneratedLocation));
Dehao Chenf4646272017-10-02 18:13:14 +0000680}
whitequark789164d2017-11-01 22:18:52 +0000681
682//===----------------------------------------------------------------------===//
683// LLVM C API implementations.
684//===----------------------------------------------------------------------===//
685
686static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
687 switch (lang) {
688#define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
689case LLVMDWARFSourceLanguage##NAME: return ID;
690#include "llvm/BinaryFormat/Dwarf.def"
691#undef HANDLE_DW_LANG
692 }
693 llvm_unreachable("Unhandled Tag");
694}
695
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000696template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
697 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
698}
699
700static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
701 return static_cast<DINode::DIFlags>(Flags);
702}
703
whitequark789164d2017-11-01 22:18:52 +0000704unsigned LLVMDebugMetadataVersion() {
705 return DEBUG_METADATA_VERSION;
706}
707
708LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
709 return wrap(new DIBuilder(*unwrap(M), false));
710}
711
712LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
713 return wrap(new DIBuilder(*unwrap(M)));
714}
715
716unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
717 return getDebugMetadataVersionFromModule(*unwrap(M));
718}
719
720LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
721 return StripDebugInfo(*unwrap(M));
722}
723
724void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
725 delete unwrap(Builder);
726}
727
728void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
729 unwrap(Builder)->finalize();
730}
731
732LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
733 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
734 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
735 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
736 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
737 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
738 LLVMBool DebugInfoForProfiling) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000739 auto File = unwrapDI<DIFile>(FileRef);
whitequark789164d2017-11-01 22:18:52 +0000740
741 return wrap(unwrap(Builder)->createCompileUnit(
742 map_from_llvmDWARFsourcelanguage(Lang), File,
743 StringRef(Producer, ProducerLen), isOptimized,
744 StringRef(Flags, FlagsLen), RuntimeVer,
745 StringRef(SplitName, SplitNameLen),
746 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
747 SplitDebugInlining, DebugInfoForProfiling));
748}
749
750LLVMMetadataRef
751LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
752 size_t FilenameLen, const char *Directory,
753 size_t DirectoryLen) {
754 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
755 StringRef(Directory, DirectoryLen)));
756}
757
Robert Widmannf53050f2018-04-07 06:07:55 +0000758LLVMMetadataRef LLVMDIBuilderCreateFunction(
759 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
760 size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
761 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
762 LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
763 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
764 return wrap(unwrap(Builder)->createFunction(
765 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
766 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
767 IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
768 IsOptimized, nullptr, nullptr, nullptr));
769}
770
771
772LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
773 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
774 LLVMMetadataRef File, unsigned Line, unsigned Col) {
775 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
776 unwrapDI<DIFile>(File),
777 Line, Col));
778}
779
780LLVMMetadataRef
781LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
782 LLVMMetadataRef Scope,
783 LLVMMetadataRef File,
784 unsigned Discriminator) {
785 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
786 unwrapDI<DIFile>(File),
787 Discriminator));
788}
789
whitequark789164d2017-11-01 22:18:52 +0000790LLVMMetadataRef
791LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
792 unsigned Column, LLVMMetadataRef Scope,
793 LLVMMetadataRef InlinedAt) {
794 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
795 unwrap(InlinedAt)));
796}
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000797
798LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
799 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
800 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
801 unsigned SizeInBits, unsigned AlignInBits, LLVMMetadataRef *Elements,
802 unsigned NumElements, LLVMMetadataRef ClassTy) {
803auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
804 NumElements});
805return wrap(unwrap(Builder)->createEnumerationType(
806 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
807 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
808}
809
810LLVMMetadataRef LLVMDIBuilderCreateUnionType(
811 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
812 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
813 unsigned SizeInBits, unsigned AlignInBits, LLVMDIFlags Flags,
814 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
815 const char *UniqueId, size_t UniqueIdLen) {
816 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
817 NumElements});
818 return wrap(unwrap(Builder)->createUnionType(
819 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
820 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
821 Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
822}
823
824
825LLVMMetadataRef
826LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, unsigned Size,
827 unsigned AlignInBits, LLVMMetadataRef Ty,
828 LLVMMetadataRef *Subscripts,
829 unsigned NumSubscripts) {
830 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
831 NumSubscripts});
832 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
833 unwrapDI<DIType>(Ty), Subs));
834}
835
836LLVMMetadataRef
837LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, unsigned Size,
838 unsigned AlignInBits, LLVMMetadataRef Ty,
839 LLVMMetadataRef *Subscripts,
840 unsigned NumSubscripts) {
841 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
842 NumSubscripts});
843 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
844 unwrapDI<DIType>(Ty), Subs));
845}
846
847LLVMMetadataRef
848LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
849 size_t NameLen, unsigned SizeInBits,
850 LLVMDWARFTypeEncoding Encoding) {
851 return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
852 SizeInBits, Encoding));
853}
854
855LLVMMetadataRef LLVMDIBuilderCreatePointerType(
856 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
857 unsigned SizeInBits, unsigned AlignInBits, unsigned AddressSpace,
858 const char *Name, size_t NameLen) {
859 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
860 SizeInBits, AlignInBits,
861 AddressSpace, {Name, NameLen}));
862}
863
864LLVMMetadataRef LLVMDIBuilderCreateStructType(
865 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
866 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
867 unsigned SizeInBits, unsigned AlignInBits, LLVMDIFlags Flags,
868 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
869 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
870 const char *UniqueId, size_t UniqueIdLen) {
871 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
872 NumElements});
873 return wrap(unwrap(Builder)->createStructType(
874 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
875 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
876 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
877 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
878}
879
880LLVMMetadataRef LLVMDIBuilderCreateMemberType(
881 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
Harlan Haskinsbee4b582018-04-02 19:11:44 +0000882 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, unsigned SizeInBits,
883 unsigned AlignInBits, unsigned OffsetInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000884 LLVMMetadataRef Ty) {
885 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
886 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
887 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
888}
889
890LLVMMetadataRef
891LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
892 size_t NameLen) {
893 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
894}
895
896LLVMMetadataRef
897LLVMDIBuilderCreateStaticMemberType(
898 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
899 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
900 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
901 unsigned AlignInBits) {
902 return wrap(unwrap(Builder)->createStaticMemberType(
903 unwrapDI<DIScope>(Scope), {Name, NameLen},
904 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
905 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
906 AlignInBits));
907}
908
909LLVMMetadataRef
910LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
911 LLVMMetadataRef Type) {
912 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
913}
914
915LLVMMetadataRef
916LLVMDIBuilderCreateReplaceableCompositeType(
Harlan Haskinsbee4b582018-04-02 19:11:44 +0000917 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
918 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000919 unsigned RuntimeLang, unsigned SizeInBits, unsigned AlignInBits,
920 LLVMDIFlags Flags, const char *UniqueIdentifier,
Harlan Haskinsbee4b582018-04-02 19:11:44 +0000921 size_t UniqueIdentifierLen) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000922 return wrap(unwrap(Builder)->createReplaceableCompositeType(
923 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
924 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
925 AlignInBits, map_from_llvmDIFlags(Flags),
926 {UniqueIdentifier, UniqueIdentifierLen}));
927}
928
929LLVMMetadataRef
930LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
931 LLVMMetadataRef Type) {
932 return wrap(unwrap(Builder)->createQualifiedType(Tag,
933 unwrapDI<DIType>(Type)));
934}
935
936LLVMMetadataRef
937LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
938 LLVMMetadataRef Type) {
939 return wrap(unwrap(Builder)->createReferenceType(Tag,
940 unwrapDI<DIType>(Type)));
941}
942
943LLVMMetadataRef
944LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
945 return wrap(unwrap(Builder)->createNullPtrType());
946}
947
948LLVMMetadataRef
949LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
950 LLVMMetadataRef PointeeType,
951 LLVMMetadataRef ClassType,
952 unsigned SizeInBits,
953 unsigned AlignInBits,
954 LLVMDIFlags Flags) {
955 return wrap(unwrap(Builder)->createMemberPointerType(
956 unwrapDI<DIType>(PointeeType),
957 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
958 map_from_llvmDIFlags(Flags)));
959}
960
961LLVMMetadataRef
962LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
963 LLVMMetadataRef Type) {
964 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
965}
966
967LLVMMetadataRef
968LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
969 LLVMMetadataRef File,
970 LLVMMetadataRef *ParameterTypes,
971 unsigned NumParameterTypes,
972 LLVMDIFlags Flags) {
973 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
974 NumParameterTypes});
975 return wrap(unwrap(Builder)->createSubroutineType(
976 Elts, map_from_llvmDIFlags(Flags)));
977}
Robert Widmannf53050f2018-04-07 06:07:55 +0000978
979LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
980 return wrap(unwrap<Function>(Func)->getSubprogram());
981}
982
983void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
984 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
985}