blob: 830082ffa5ff9e7898c90f97192b4e02c43225d9 [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) {
Roman Tereshindab10b52018-04-13 21:23:11 +000064 for (auto *CU : M.debug_compile_units())
65 processCompileUnit(CU);
Keno Fischer30779772017-04-11 13:32:11 +000066 for (auto &F : M.functions()) {
Adrian Prantl75819ae2016-04-15 15:57:41 +000067 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
68 processSubprogram(SP);
Keno Fischer30779772017-04-11 13:32:11 +000069 // There could be subprograms from inlined functions referenced from
70 // instructions only. Walk the function to find them.
Roman Tereshindab10b52018-04-13 21:23:11 +000071 for (const BasicBlock &BB : F)
72 for (const Instruction &I : BB)
73 processInstruction(M, I);
Keno Fischer30779772017-04-11 13:32:11 +000074 }
Bill Wendling523bea82013-11-08 08:13:15 +000075}
76
Roman Tereshind769eb32018-04-13 21:22:24 +000077void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
78 if (!addCompileUnit(CU))
79 return;
80 for (auto DIG : CU->getGlobalVariables()) {
81 if (!addGlobalVariable(DIG))
82 continue;
83 auto *GV = DIG->getVariable();
84 processScope(GV->getScope());
85 processType(GV->getType().resolve());
86 }
87 for (auto *ET : CU->getEnumTypes())
88 processType(ET);
89 for (auto *RT : CU->getRetainedTypes())
90 if (auto *T = dyn_cast<DIType>(RT))
91 processType(T);
92 else
93 processSubprogram(cast<DISubprogram>(RT));
94 for (auto *Import : CU->getImportedEntities()) {
95 auto *Entity = Import->getEntity().resolve();
96 if (auto *T = dyn_cast<DIType>(Entity))
97 processType(T);
98 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
99 processSubprogram(SP);
100 else if (auto *NS = dyn_cast<DINamespace>(Entity))
101 processScope(NS->getScope());
102 else if (auto *M = dyn_cast<DIModule>(Entity))
103 processScope(M->getScope());
104 else
105 llvm_unreachable("unexpected imported entity type");
106 }
107}
108
Roman Tereshindab10b52018-04-13 21:23:11 +0000109void DebugInfoFinder::processInstruction(const Module &M,
110 const Instruction &I) {
111 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
112 processDeclare(M, DDI);
113 else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
114 processValue(M, DVI);
115
116 if (auto DbgLoc = I.getDebugLoc())
117 processLocation(M, DbgLoc.get());
118}
119
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000120void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000121 if (!Loc)
122 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000123 processScope(Loc->getScope());
124 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000125}
126
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000127void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000128 if (!addType(DT))
129 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000130 processScope(DT->getScope().resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000131 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
132 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000133 processType(Ref.resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000134 return;
135 }
136 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000137 processType(DCT->getBaseType().resolve());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000138 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000139 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000140 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000142 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000143 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000144 return;
145 }
146 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000147 processType(DDT->getBaseType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000148 }
149}
150
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000151void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000152 if (!Scope)
153 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000154 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000155 processType(Ty);
156 return;
157 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000158 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000159 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000160 return;
161 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000162 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000163 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000164 return;
165 }
166 if (!addScope(Scope))
167 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000168 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000169 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000170 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000171 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000172 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
173 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000174 }
175}
176
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000177void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000178 if (!addSubprogram(SP))
179 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000180 processScope(SP->getScope().resolve());
Roman Tereshind769eb32018-04-13 21:22:24 +0000181 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
182 // ValueMap containing identity mappings for all of the DICompileUnit's, not
183 // just DISubprogram's, referenced from anywhere within the Function being
184 // cloned prior to calling MapMetadata / RemapInstruction to avoid their
185 // duplication later as DICompileUnit's are also directly referenced by
186 // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
187 // Also, DICompileUnit's may reference DISubprogram's too and therefore need
188 // to be at least looked through.
189 processCompileUnit(SP->getUnit());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000190 processType(SP->getType());
191 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000192 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000193 processType(TType->getType().resolve());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000194 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000195 processType(TVal->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000196 }
197 }
198}
199
Manman Ren2085ccc2013-11-17 18:42:37 +0000200void DebugInfoFinder::processDeclare(const Module &M,
201 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000202 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000203 if (!N)
204 return;
205
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000206 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000207 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000208 return;
209
David Blaikie70573dc2014-11-19 07:49:26 +0000210 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000211 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000212 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000213 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000214}
215
Manman Ren2085ccc2013-11-17 18:42:37 +0000216void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000217 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000218 if (!N)
219 return;
220
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000221 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000222 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000223 return;
224
David Blaikie70573dc2014-11-19 07:49:26 +0000225 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000226 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000227 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000228 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000229}
230
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000231bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000232 if (!DT)
233 return false;
234
David Blaikie70573dc2014-11-19 07:49:26 +0000235 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000236 return false;
237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000239 return true;
240}
241
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000242bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000243 if (!CU)
244 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000245 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000246 return false;
247
248 CUs.push_back(CU);
249 return true;
250}
251
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000252bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
David Blaikie70573dc2014-11-19 07:49:26 +0000253 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000254 return false;
255
256 GVs.push_back(DIG);
257 return true;
258}
259
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000260bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000261 if (!SP)
262 return false;
263
David Blaikie70573dc2014-11-19 07:49:26 +0000264 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000265 return false;
266
267 SPs.push_back(SP);
268 return true;
269}
270
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000271bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000272 if (!Scope)
273 return false;
274 // FIXME: Ocaml binding generates a scope with no content, we treat it
275 // as null for now.
276 if (Scope->getNumOperands() == 0)
277 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000278 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000279 return false;
280 Scopes.push_back(Scope);
281 return true;
282}
283
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000284static MDNode *stripDebugLocFromLoopID(MDNode *N) {
Daniel Sandersb96a9452017-01-28 11:22:05 +0000285 assert(N->op_begin() != N->op_end() && "Missing self reference?");
Daniel Sandersb96a9452017-01-28 11:22:05 +0000286
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000287 // if there is no debug location, we do not have to rewrite this MDNode.
288 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
289 return isa<DILocation>(Op.get());
290 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000291 return N;
292
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000293 // If there is only the debug location without any actual loop metadata, we
Daniel Sandersb96a9452017-01-28 11:22:05 +0000294 // can remove the metadata.
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000295 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
296 return !isa<DILocation>(Op.get());
297 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000298 return nullptr;
299
300 SmallVector<Metadata *, 4> Args;
301 // Reserve operand 0 for loop id self reference.
302 auto TempNode = MDNode::getTemporary(N->getContext(), None);
303 Args.push_back(TempNode.get());
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000304 // Add all non-debug location operands back.
305 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
306 if (!isa<DILocation>(*Op))
307 Args.push_back(*Op);
308 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000309
310 // Set the first operand to itself.
311 MDNode *LoopID = MDNode::get(N->getContext(), Args);
312 LoopID->replaceOperandWith(0, LoopID);
313 return LoopID;
314}
315
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000316bool llvm::stripDebugInfo(Function &F) {
317 bool Changed = false;
Adrian Prantla8b2ddb2017-10-02 18:31:29 +0000318 if (F.getMetadata(LLVMContext::MD_dbg)) {
Peter Collingbourned4bff302015-11-05 22:03:56 +0000319 Changed = true;
320 F.setSubprogram(nullptr);
321 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000322
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000323 DenseMap<MDNode*, MDNode*> LoopIDsMap;
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000324 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000325 for (auto II = BB.begin(), End = BB.end(); II != End;) {
326 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000327 if (isa<DbgInfoIntrinsic>(&I)) {
328 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000329 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000330 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000331 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000332 if (I.getDebugLoc()) {
333 Changed = true;
334 I.setDebugLoc(DebugLoc());
335 }
336 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000337
338 auto *TermInst = BB.getTerminator();
Justin Bognerb29bebe2017-08-18 21:38:03 +0000339 if (!TermInst)
340 // This is invalid IR, but we may not have run the verifier yet
341 continue;
Daniel Sandersb96a9452017-01-28 11:22:05 +0000342 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
343 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
344 if (!NewLoopID)
345 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
346 if (NewLoopID != LoopID)
347 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
348 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000349 }
350 return Changed;
351}
352
Manman Rencb14bbc2013-11-22 22:06:31 +0000353bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000354 bool Changed = false;
355
Manman Rencb14bbc2013-11-22 22:06:31 +0000356 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
357 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000358 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000359 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000360
361 // We're stripping debug info, and without them, coverage information
362 // doesn't quite make sense.
363 if (NMD->getName().startswith("llvm.dbg.") ||
364 NMD->getName() == "llvm.gcov") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000365 NMD->eraseFromParent();
366 Changed = true;
367 }
368 }
369
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000370 for (Function &F : M)
371 Changed |= stripDebugInfo(F);
372
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000373 for (auto &GV : M.globals()) {
374 SmallVector<MDNode *, 1> MDs;
375 GV.getMetadata(LLVMContext::MD_dbg, MDs);
376 if (!MDs.empty()) {
377 GV.eraseMetadata(LLVMContext::MD_dbg);
378 Changed = true;
379 }
380 }
381
Rafael Espindola468b8682015-04-01 14:44:59 +0000382 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000383 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000384
385 return Changed;
386}
Manman Ren8b4306c2013-12-02 21:29:56 +0000387
Michael Ilsemane5428042016-10-25 18:44:13 +0000388namespace {
389
390/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
391class DebugTypeInfoRemoval {
392 DenseMap<Metadata *, Metadata *> Replacements;
393
394public:
395 /// The (void)() type.
396 MDNode *EmptySubroutineType;
397
398private:
399 /// Remember what linkage name we originally had before stripping. If we end
400 /// up making two subprograms identical who originally had different linkage
401 /// names, then we need to make one of them distinct, to avoid them getting
402 /// uniqued. Maps the new node to the old linkage name.
403 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
404
405 // TODO: Remember the distinct subprogram we created for a given linkage name,
406 // so that we can continue to unique whenever possible. Map <newly created
407 // node, old linkage name> to the first (possibly distinct) mdsubprogram
408 // created for that combination. This is not strictly needed for correctness,
409 // but can cut down on the number of MDNodes and let us diff cleanly with the
410 // output of -gline-tables-only.
411
412public:
413 DebugTypeInfoRemoval(LLVMContext &C)
414 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
415 MDNode::get(C, {}))) {}
416
417 Metadata *map(Metadata *M) {
418 if (!M)
419 return nullptr;
420 auto Replacement = Replacements.find(M);
421 if (Replacement != Replacements.end())
422 return Replacement->second;
423
424 return M;
425 }
426 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
427
428 /// Recursively remap N and all its referenced children. Does a DF post-order
429 /// traversal, so as to remap bottoms up.
430 void traverseAndRemap(MDNode *N) { traverse(N); }
431
432private:
433 // Create a new DISubprogram, to replace the one given.
434 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
435 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
436 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
437 DISubprogram *Declaration = nullptr;
438 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
439 DITypeRef ContainingType(map(MDS->getContainingType()));
440 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
441 auto Variables = nullptr;
442 auto TemplateParams = nullptr;
443
444 // Make a distinct DISubprogram, for situations that warrent it.
445 auto distinctMDSubprogram = [&]() {
446 return DISubprogram::getDistinct(
447 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
448 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
449 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
450 MDS->getVirtuality(), MDS->getVirtualIndex(),
451 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
452 TemplateParams, Declaration, Variables);
453 };
454
455 if (MDS->isDistinct())
456 return distinctMDSubprogram();
457
458 auto *NewMDS = DISubprogram::get(
459 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
460 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
461 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
462 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
463 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
464 Variables);
465
466 StringRef OldLinkageName = MDS->getLinkageName();
467
468 // See if we need to make a distinct one.
469 auto OrigLinkage = NewToLinkageName.find(NewMDS);
470 if (OrigLinkage != NewToLinkageName.end()) {
471 if (OrigLinkage->second == OldLinkageName)
472 // We're good.
473 return NewMDS;
474
475 // Otherwise, need to make a distinct one.
476 // TODO: Query the map to see if we already have one.
477 return distinctMDSubprogram();
478 }
479
480 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
481 return NewMDS;
482 }
483
484 /// Create a new compile unit, to replace the one given
485 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
486 // Drop skeleton CUs.
487 if (CU->getDWOId())
488 return nullptr;
489
490 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
491 MDTuple *EnumTypes = nullptr;
492 MDTuple *RetainedTypes = nullptr;
493 MDTuple *GlobalVariables = nullptr;
494 MDTuple *ImportedEntities = nullptr;
495 return DICompileUnit::getDistinct(
496 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
497 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
498 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
499 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
Dehao Chen0944a8c2017-02-01 22:45:09 +0000500 CU->getDWOId(), CU->getSplitDebugInlining(),
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000501 CU->getDebugInfoForProfiling(), CU->getGnuPubnames());
Michael Ilsemane5428042016-10-25 18:44:13 +0000502 }
503
504 DILocation *getReplacementMDLocation(DILocation *MLD) {
505 auto *Scope = map(MLD->getScope());
506 auto *InlinedAt = map(MLD->getInlinedAt());
507 if (MLD->isDistinct())
508 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
509 MLD->getColumn(), Scope, InlinedAt);
510 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
511 Scope, InlinedAt);
512 }
513
514 /// Create a new generic MDNode, to replace the one given
515 MDNode *getReplacementMDNode(MDNode *N) {
516 SmallVector<Metadata *, 8> Ops;
517 Ops.reserve(N->getNumOperands());
518 for (auto &I : N->operands())
519 if (I)
520 Ops.push_back(map(I));
521 auto *Ret = MDNode::get(N->getContext(), Ops);
522 return Ret;
523 }
524
525 /// Attempt to re-map N to a newly created node.
526 void remap(MDNode *N) {
527 if (Replacements.count(N))
528 return;
529
530 auto doRemap = [&](MDNode *N) -> MDNode * {
531 if (!N)
532 return nullptr;
533 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
534 remap(MDSub->getUnit());
535 return getReplacementSubprogram(MDSub);
536 }
537 if (isa<DISubroutineType>(N))
538 return EmptySubroutineType;
539 if (auto *CU = dyn_cast<DICompileUnit>(N))
540 return getReplacementCU(CU);
541 if (isa<DIFile>(N))
542 return N;
543 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
544 // Remap to our referenced scope (recursively).
545 return mapNode(MDLB->getScope());
546 if (auto *MLD = dyn_cast<DILocation>(N))
547 return getReplacementMDLocation(MLD);
548
549 // Otherwise, if we see these, just drop them now. Not strictly necessary,
550 // but this speeds things up a little.
551 if (isa<DINode>(N))
552 return nullptr;
553
554 return getReplacementMDNode(N);
555 };
556 Replacements[N] = doRemap(N);
557 }
558
559 /// Do the remapping traversal.
560 void traverse(MDNode *);
561};
562
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000563} // end anonymous namespace
Michael Ilsemane5428042016-10-25 18:44:13 +0000564
565void DebugTypeInfoRemoval::traverse(MDNode *N) {
566 if (!N || Replacements.count(N))
567 return;
568
569 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
570 // parts of the graph.
571 auto prune = [](MDNode *Parent, MDNode *Child) {
572 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
573 return Child == MDS->getVariables().get();
574 return false;
575 };
576
577 SmallVector<MDNode *, 16> ToVisit;
578 DenseSet<MDNode *> Opened;
579
580 // Visit each node starting at N in post order, and map them.
581 ToVisit.push_back(N);
582 while (!ToVisit.empty()) {
583 auto *N = ToVisit.back();
584 if (!Opened.insert(N).second) {
585 // Close it.
586 remap(N);
587 ToVisit.pop_back();
588 continue;
589 }
590 for (auto &I : N->operands())
591 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
592 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
593 !isa<DICompileUnit>(MDN))
594 ToVisit.push_back(MDN);
595 }
596}
597
598bool llvm::stripNonLineTableDebugInfo(Module &M) {
599 bool Changed = false;
600
601 // First off, delete the debug intrinsics.
602 auto RemoveUses = [&](StringRef Name) {
603 if (auto *DbgVal = M.getFunction(Name)) {
604 while (!DbgVal->use_empty())
605 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
606 DbgVal->eraseFromParent();
607 Changed = true;
608 }
609 };
610 RemoveUses("llvm.dbg.declare");
611 RemoveUses("llvm.dbg.value");
612
613 // Delete non-CU debug info named metadata nodes.
614 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
615 NMI != NME;) {
616 NamedMDNode *NMD = &*NMI;
617 ++NMI;
618 // Specifically keep dbg.cu around.
619 if (NMD->getName() == "llvm.dbg.cu")
620 continue;
621 }
622
623 // Drop all dbg attachments from global variables.
624 for (auto &GV : M.globals())
625 GV.eraseMetadata(LLVMContext::MD_dbg);
626
627 DebugTypeInfoRemoval Mapper(M.getContext());
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000628 auto remap = [&](MDNode *Node) -> MDNode * {
Michael Ilsemane5428042016-10-25 18:44:13 +0000629 if (!Node)
630 return nullptr;
631 Mapper.traverseAndRemap(Node);
632 auto *NewNode = Mapper.mapNode(Node);
633 Changed |= Node != NewNode;
634 Node = NewNode;
635 return NewNode;
636 };
637
638 // Rewrite the DebugLocs to be equivalent to what
639 // -gline-tables-only would have created.
640 for (auto &F : M) {
641 if (auto *SP = F.getSubprogram()) {
642 Mapper.traverseAndRemap(SP);
643 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
644 Changed |= SP != NewSP;
645 F.setSubprogram(NewSP);
646 }
647 for (auto &BB : F) {
648 for (auto &I : BB) {
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000649 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
650 auto *Scope = DL.getScope();
651 MDNode *InlinedAt = DL.getInlinedAt();
652 Scope = remap(Scope);
653 InlinedAt = remap(InlinedAt);
654 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
655 };
Michael Ilsemane5428042016-10-25 18:44:13 +0000656
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000657 if (I.getDebugLoc() != DebugLoc())
658 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
659
660 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
661 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
662 I.getAllMetadata(MDs);
663 for (auto Attachment : MDs)
664 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
665 for (unsigned N = 0; N < T->getNumOperands(); ++N)
666 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
667 if (Loc != DebugLoc())
668 T->replaceOperandWith(N, remapDebugLoc(Loc));
Michael Ilsemane5428042016-10-25 18:44:13 +0000669 }
670 }
671 }
672
673 // Create a new llvm.dbg.cu, which is equivalent to the one
674 // -gline-tables-only would have created.
675 for (auto &NMD : M.getNamedMDList()) {
676 SmallVector<MDNode *, 8> Ops;
677 for (MDNode *Op : NMD.operands())
678 Ops.push_back(remap(Op));
679
680 if (!Changed)
681 continue;
682
683 NMD.clearOperands();
684 for (auto *Op : Ops)
685 if (Op)
686 NMD.addOperand(Op);
687 }
688 return Changed;
689}
690
Manman Renbd4daf82013-12-03 00:12:14 +0000691unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000692 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000693 M.getModuleFlag("Debug Info Version")))
694 return Val->getZExtValue();
695 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000696}
Dehao Chenf4646272017-10-02 18:13:14 +0000697
698void Instruction::applyMergedLocation(const DILocation *LocA,
699 const DILocation *LocB) {
Vedant Kumar65b0d4d2018-04-12 20:58:24 +0000700 setDebugLoc(DILocation::getMergedLocation(LocA, LocB,
701 DILocation::WithGeneratedLocation));
Dehao Chenf4646272017-10-02 18:13:14 +0000702}
whitequark789164d2017-11-01 22:18:52 +0000703
704//===----------------------------------------------------------------------===//
705// LLVM C API implementations.
706//===----------------------------------------------------------------------===//
707
708static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
709 switch (lang) {
710#define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
711case LLVMDWARFSourceLanguage##NAME: return ID;
712#include "llvm/BinaryFormat/Dwarf.def"
713#undef HANDLE_DW_LANG
714 }
715 llvm_unreachable("Unhandled Tag");
716}
717
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000718template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
719 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
720}
721
722static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
723 return static_cast<DINode::DIFlags>(Flags);
724}
725
whitequark789164d2017-11-01 22:18:52 +0000726unsigned LLVMDebugMetadataVersion() {
727 return DEBUG_METADATA_VERSION;
728}
729
730LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
731 return wrap(new DIBuilder(*unwrap(M), false));
732}
733
734LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
735 return wrap(new DIBuilder(*unwrap(M)));
736}
737
738unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
739 return getDebugMetadataVersionFromModule(*unwrap(M));
740}
741
742LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
743 return StripDebugInfo(*unwrap(M));
744}
745
746void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
747 delete unwrap(Builder);
748}
749
750void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
751 unwrap(Builder)->finalize();
752}
753
754LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
755 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
756 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
757 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
758 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
759 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
760 LLVMBool DebugInfoForProfiling) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000761 auto File = unwrapDI<DIFile>(FileRef);
whitequark789164d2017-11-01 22:18:52 +0000762
763 return wrap(unwrap(Builder)->createCompileUnit(
764 map_from_llvmDWARFsourcelanguage(Lang), File,
765 StringRef(Producer, ProducerLen), isOptimized,
766 StringRef(Flags, FlagsLen), RuntimeVer,
767 StringRef(SplitName, SplitNameLen),
768 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
769 SplitDebugInlining, DebugInfoForProfiling));
770}
771
772LLVMMetadataRef
773LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
774 size_t FilenameLen, const char *Directory,
775 size_t DirectoryLen) {
776 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
777 StringRef(Directory, DirectoryLen)));
778}
779
Robert Widmannf53050f2018-04-07 06:07:55 +0000780LLVMMetadataRef LLVMDIBuilderCreateFunction(
781 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
782 size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
783 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
784 LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
785 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
786 return wrap(unwrap(Builder)->createFunction(
787 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
788 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
789 IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
790 IsOptimized, nullptr, nullptr, nullptr));
791}
792
793
794LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
795 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
796 LLVMMetadataRef File, unsigned Line, unsigned Col) {
797 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
798 unwrapDI<DIFile>(File),
799 Line, Col));
800}
801
802LLVMMetadataRef
803LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
804 LLVMMetadataRef Scope,
805 LLVMMetadataRef File,
806 unsigned Discriminator) {
807 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
808 unwrapDI<DIFile>(File),
809 Discriminator));
810}
811
whitequark789164d2017-11-01 22:18:52 +0000812LLVMMetadataRef
813LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
814 unsigned Column, LLVMMetadataRef Scope,
815 LLVMMetadataRef InlinedAt) {
816 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
817 unwrap(InlinedAt)));
818}
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000819
820LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
821 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
822 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
823 unsigned SizeInBits, unsigned AlignInBits, LLVMMetadataRef *Elements,
824 unsigned NumElements, LLVMMetadataRef ClassTy) {
825auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
826 NumElements});
827return wrap(unwrap(Builder)->createEnumerationType(
828 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
829 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
830}
831
832LLVMMetadataRef LLVMDIBuilderCreateUnionType(
833 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
834 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
835 unsigned SizeInBits, unsigned AlignInBits, LLVMDIFlags Flags,
836 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
837 const char *UniqueId, size_t UniqueIdLen) {
838 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
839 NumElements});
840 return wrap(unwrap(Builder)->createUnionType(
841 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
842 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
843 Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
844}
845
846
847LLVMMetadataRef
848LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, unsigned Size,
849 unsigned AlignInBits, LLVMMetadataRef Ty,
850 LLVMMetadataRef *Subscripts,
851 unsigned NumSubscripts) {
852 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
853 NumSubscripts});
854 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
855 unwrapDI<DIType>(Ty), Subs));
856}
857
858LLVMMetadataRef
859LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, unsigned Size,
860 unsigned AlignInBits, LLVMMetadataRef Ty,
861 LLVMMetadataRef *Subscripts,
862 unsigned NumSubscripts) {
863 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
864 NumSubscripts});
865 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
866 unwrapDI<DIType>(Ty), Subs));
867}
868
869LLVMMetadataRef
870LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
871 size_t NameLen, unsigned SizeInBits,
872 LLVMDWARFTypeEncoding Encoding) {
873 return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
874 SizeInBits, Encoding));
875}
876
877LLVMMetadataRef LLVMDIBuilderCreatePointerType(
878 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
879 unsigned SizeInBits, unsigned AlignInBits, unsigned AddressSpace,
880 const char *Name, size_t NameLen) {
881 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
882 SizeInBits, AlignInBits,
883 AddressSpace, {Name, NameLen}));
884}
885
886LLVMMetadataRef LLVMDIBuilderCreateStructType(
887 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
888 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
889 unsigned SizeInBits, unsigned AlignInBits, LLVMDIFlags Flags,
890 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
891 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
892 const char *UniqueId, size_t UniqueIdLen) {
893 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
894 NumElements});
895 return wrap(unwrap(Builder)->createStructType(
896 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
897 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
898 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
899 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
900}
901
902LLVMMetadataRef LLVMDIBuilderCreateMemberType(
903 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
Harlan Haskinsbee4b582018-04-02 19:11:44 +0000904 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, unsigned SizeInBits,
905 unsigned AlignInBits, unsigned OffsetInBits, LLVMDIFlags Flags,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000906 LLVMMetadataRef Ty) {
907 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
908 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
909 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
910}
911
912LLVMMetadataRef
913LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
914 size_t NameLen) {
915 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
916}
917
918LLVMMetadataRef
919LLVMDIBuilderCreateStaticMemberType(
920 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
921 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
922 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
923 unsigned AlignInBits) {
924 return wrap(unwrap(Builder)->createStaticMemberType(
925 unwrapDI<DIScope>(Scope), {Name, NameLen},
926 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
927 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
928 AlignInBits));
929}
930
931LLVMMetadataRef
932LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
933 LLVMMetadataRef Type) {
934 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
935}
936
937LLVMMetadataRef
938LLVMDIBuilderCreateReplaceableCompositeType(
Harlan Haskinsbee4b582018-04-02 19:11:44 +0000939 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
940 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000941 unsigned RuntimeLang, unsigned SizeInBits, unsigned AlignInBits,
942 LLVMDIFlags Flags, const char *UniqueIdentifier,
Harlan Haskinsbee4b582018-04-02 19:11:44 +0000943 size_t UniqueIdentifierLen) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +0000944 return wrap(unwrap(Builder)->createReplaceableCompositeType(
945 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
946 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
947 AlignInBits, map_from_llvmDIFlags(Flags),
948 {UniqueIdentifier, UniqueIdentifierLen}));
949}
950
951LLVMMetadataRef
952LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
953 LLVMMetadataRef Type) {
954 return wrap(unwrap(Builder)->createQualifiedType(Tag,
955 unwrapDI<DIType>(Type)));
956}
957
958LLVMMetadataRef
959LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
960 LLVMMetadataRef Type) {
961 return wrap(unwrap(Builder)->createReferenceType(Tag,
962 unwrapDI<DIType>(Type)));
963}
964
965LLVMMetadataRef
966LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
967 return wrap(unwrap(Builder)->createNullPtrType());
968}
969
970LLVMMetadataRef
971LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
972 LLVMMetadataRef PointeeType,
973 LLVMMetadataRef ClassType,
974 unsigned SizeInBits,
975 unsigned AlignInBits,
976 LLVMDIFlags Flags) {
977 return wrap(unwrap(Builder)->createMemberPointerType(
978 unwrapDI<DIType>(PointeeType),
979 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
980 map_from_llvmDIFlags(Flags)));
981}
982
983LLVMMetadataRef
984LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
985 LLVMMetadataRef Type) {
986 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
987}
988
989LLVMMetadataRef
990LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
991 LLVMMetadataRef File,
992 LLVMMetadataRef *ParameterTypes,
993 unsigned NumParameterTypes,
994 LLVMDIFlags Flags) {
995 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
996 NumParameterTypes});
997 return wrap(unwrap(Builder)->createSubroutineType(
998 Elts, map_from_llvmDIFlags(Flags)));
999}
Robert Widmannf53050f2018-04-07 06:07:55 +00001000
1001LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1002 return wrap(unwrap<Function>(Func)->getSubprogram());
1003}
1004
1005void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1006 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
1007}