blob: 0062f1bbf581df18d9b92b73c0caf0a8fb62cab7 [file] [log] [blame]
Bill Wendling523bea82013-11-08 08:13:15 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
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
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "LLVMContextImpl.h"
Bill Wendling523bea82013-11-08 08:13:15 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000022#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000027#include "llvm/IR/GVMaterializer.h"
Bill Wendling523bea82013-11-08 08:13:15 +000028#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000029#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34using namespace llvm::dwarf;
35
36//===----------------------------------------------------------------------===//
37// DIDescriptor
38//===----------------------------------------------------------------------===//
39
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000040static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000041 if (!DbgNode || Elt >= DbgNode->getNumOperands())
42 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000043 return DbgNode->getOperand(Elt);
44}
45
46static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
47 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
48}
49
Bill Wendling523bea82013-11-08 08:13:15 +000050DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
51 MDNode *Field = getNodeField(DbgNode, Elt);
52 return DIDescriptor(Field);
53}
54
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +000055/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +000056unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
57 DIType Ty = getType().resolve(Map);
58 // Follow derived types until we reach a type that
59 // reports back a size.
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000060 while (isa<MDDerivedType>(Ty) && !Ty.getSizeInBits()) {
61 DIDerivedType DT = cast<MDDerivedType>(Ty);
Adrian Prantlb1416832014-08-01 22:11:58 +000062 Ty = DT.getTypeDerivedFrom().resolve(Map);
63 }
64 assert(Ty.getSizeInBits() && "type with size 0");
65 return Ty.getSizeInBits();
66}
67
Bill Wendling523bea82013-11-08 08:13:15 +000068//===----------------------------------------------------------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +000069// Simple Descriptor Constructors and other Methods
70//===----------------------------------------------------------------------===//
71
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +000072void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +000073 assert(DbgNode && "Trying to replace an unverified type!");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +000074 assert(DbgNode->isTemporary() && "Expected temporary node");
75 TempMDNode Temp(get());
Bill Wendling523bea82013-11-08 08:13:15 +000076
77 // Since we use a TrackingVH for the node, its easy for clients to manufacture
78 // legitimate situations where they want to replaceAllUsesWith() on something
79 // which, due to uniquing, has merged with the source. We shield clients from
80 // this detail by allowing a value to be replaced with replaceAllUsesWith()
81 // itself.
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +000082 if (Temp.get() == D.get()) {
83 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
84 return;
Bill Wendling523bea82013-11-08 08:13:15 +000085 }
David Blaikied3f094a2014-05-06 03:41:57 +000086
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +000087 Temp->replaceAllUsesWith(D.get());
88 DbgNode = D.get();
Bill Wendling523bea82013-11-08 08:13:15 +000089}
90
Frederic Riss36acf0f2014-09-15 07:50:36 +000091void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +000092 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +000093 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +000094 assert(DbgNode->isTemporary() && "Expected temporary node");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +000095 TempMDNode Node(get());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000096 Node->replaceAllUsesWith(D);
Bill Wendling523bea82013-11-08 08:13:15 +000097}
98
Duncan P. N. Exon Smith3b960c92015-03-31 01:47:55 +000099#ifndef NDEBUG
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000100/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000101static bool isTypeRef(const Metadata *MD) {
102 if (!MD)
103 return true;
104 if (auto *S = dyn_cast<MDString>(MD))
105 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000106 return isa<MDType>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000107}
108
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000109/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000110static bool isScopeRef(const Metadata *MD) {
111 if (!MD)
112 return true;
113 if (auto *S = dyn_cast<MDString>(MD))
114 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000115 return isa<MDScope>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000116}
117
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000118/// \brief Check if a value can be a DescriptorRef.
119static bool isDescriptorRef(const Metadata *MD) {
120 if (!MD)
121 return true;
122 if (auto *S = dyn_cast<MDString>(MD))
123 return !S->getString().empty();
124 return isa<MDNode>(MD);
125}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000126#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000127
Duncan P. N. Exon Smith930f3882015-04-06 18:02:43 +0000128DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
Bill Wendling523bea82013-11-08 08:13:15 +0000129
Bill Wendling523bea82013-11-08 08:13:15 +0000130bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
131 assert(CurFn && "Invalid function");
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000132 DISubprogram SP = dyn_cast<MDSubprogram>(getContext());
133 if (!SP)
Bill Wendling523bea82013-11-08 08:13:15 +0000134 return false;
135 // This variable is not inlined function argument if its scope
136 // does not describe current function.
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000137 return !SP.describes(CurFn);
Bill Wendling523bea82013-11-08 08:13:15 +0000138}
139
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000140Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000141 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000142 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
143 return dyn_cast<Function>(C->getValue());
144 return nullptr;
145}
146
Bill Wendling523bea82013-11-08 08:13:15 +0000147bool DISubprogram::describes(const Function *F) {
148 assert(F && "Invalid function");
149 if (F == getFunction())
150 return true;
151 StringRef Name = getLinkageName();
152 if (Name.empty())
153 Name = getName();
154 if (F->getName() == Name)
155 return true;
156 return false;
157}
158
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000159GlobalVariable *DIGlobalVariable::getGlobal() const {
160 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000161}
162
Bill Wendling523bea82013-11-08 08:13:15 +0000163DIScopeRef DIScope::getContext() const {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000164 if (DIType T = dyn_cast<MDType>(*this))
165 return T.getContext();
Bill Wendling523bea82013-11-08 08:13:15 +0000166
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000167 if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
168 return DIScopeRef(SP.getContext());
Bill Wendling523bea82013-11-08 08:13:15 +0000169
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000170 if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(*this))
171 return DIScopeRef(LB.getContext());
Bill Wendling523bea82013-11-08 08:13:15 +0000172
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000173 if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
174 return DIScopeRef(NS.getContext());
Bill Wendling523bea82013-11-08 08:13:15 +0000175
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000176 assert((isa<MDFile>(*this) || isa<MDCompileUnit>(*this)) &&
177 "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000178 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000179}
180
Bill Wendling523bea82013-11-08 08:13:15 +0000181StringRef DIScope::getName() const {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000182 if (DIType T = dyn_cast<MDType>(*this))
183 return T.getName();
184 if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
185 return SP.getName();
186 if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
187 return NS.getName();
188 assert((isa<MDLexicalBlockBase>(*this) || isa<MDFile>(*this) ||
189 isa<MDCompileUnit>(*this)) &&
Bill Wendling523bea82013-11-08 08:13:15 +0000190 "Unhandled type of scope.");
191 return StringRef();
192}
193
194StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000195 if (auto *N = get())
Duncan P. N. Exon Smith897030c2015-04-06 16:43:40 +0000196 if (auto *F = N->getFile())
197 return F->getFilename();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000198 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000199}
200
201StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000202 if (auto *N = get())
Duncan P. N. Exon Smith897030c2015-04-06 16:43:40 +0000203 if (auto *F = N->getFile())
204 return F->getDirectory();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000205 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000206}
207
208void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000209 get()->replaceSubprograms(MDSubprogramArray(Subprograms));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000210}
211
212void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +0000213 get()->replaceGlobalVariables(MDGlobalVariableArray(GlobalVariables));
Bill Wendling523bea82013-11-08 08:13:15 +0000214}
215
Diego Novillof5041ce2014-03-03 20:06:11 +0000216DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000217 DILexicalBlockFile NewScope) {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000218 assert(NewScope && "Expected valid scope");
219
220 const auto *Old = cast<MDLocation>(DbgNode);
221 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
222 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000223}
224
Diego Novillof5041ce2014-03-03 20:06:11 +0000225unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
226 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
227 return ++Ctx.pImpl->DiscriminatorTable[Key];
228}
229
Bill Wendling523bea82013-11-08 08:13:15 +0000230DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
231 LLVMContext &VMContext) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000232 return cast<MDLocalVariable>(DV)
233 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000234}
235
Bill Wendling523bea82013-11-08 08:13:15 +0000236DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000237 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000238}
239
Bill Wendling523bea82013-11-08 08:13:15 +0000240DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +0000241 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
242 return LocalScope->getSubprogram();
243 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000244}
245
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000246DISubprogram llvm::getDISubprogram(const Function *F) {
247 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000248 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000249 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000250 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +0000251 });
252 if (Inst == BB.end())
253 continue;
254 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000255 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +0000256 DISubprogram Subprogram = getDISubprogram(Scope);
257 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000258 }
259
260 return DISubprogram();
261}
262
Bill Wendling523bea82013-11-08 08:13:15 +0000263DICompositeType llvm::getDICompositeType(DIType T) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000264 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
265 return C;
Bill Wendling523bea82013-11-08 08:13:15 +0000266
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000267 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000268 // This function is currently used by dragonegg and dragonegg does
269 // not generate identifier for types, so using an empty map to resolve
270 // DerivedFrom should be fine.
271 DITypeIdentifierMap EmptyMap;
272 return getDICompositeType(
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000273 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000274 }
275
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000276 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000277}
278
Bill Wendling523bea82013-11-08 08:13:15 +0000279DITypeIdentifierMap
280llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
281 DITypeIdentifierMap Map;
282 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000283 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000284 DIArray Retain = CU.getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000285 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
286 if (!isa<MDCompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +0000287 continue;
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000288 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
Bill Wendling523bea82013-11-08 08:13:15 +0000289 if (MDString *TypeId = Ty.getIdentifier()) {
290 // Definition has priority over declaration.
291 // Try to insert (TypeId, Ty) to Map.
292 std::pair<DITypeIdentifierMap::iterator, bool> P =
293 Map.insert(std::make_pair(TypeId, Ty));
294 // If TypeId already exists in Map and this is a definition, replace
295 // whatever we had (declaration or definition) with the definition.
296 if (!P.second && !Ty.isForwardDecl())
297 P.first->second = Ty;
298 }
299 }
300 }
301 return Map;
302}
303
304//===----------------------------------------------------------------------===//
305// DebugInfoFinder implementations.
306//===----------------------------------------------------------------------===//
307
308void DebugInfoFinder::reset() {
309 CUs.clear();
310 SPs.clear();
311 GVs.clear();
312 TYs.clear();
313 Scopes.clear();
314 NodesSeen.clear();
315 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000316 TypeMapInitialized = false;
317}
318
Manman Renb46e5502013-11-17 19:35:03 +0000319void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000320 if (!TypeMapInitialized)
321 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
322 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
323 TypeMapInitialized = true;
324 }
Bill Wendling523bea82013-11-08 08:13:15 +0000325}
326
Bill Wendling523bea82013-11-08 08:13:15 +0000327void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000328 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000329 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000330 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000331 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000332 addCompileUnit(CU);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000333 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
Bill Wendling523bea82013-11-08 08:13:15 +0000334 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000335 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000336 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000337 }
338 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000339 for (auto *SP : CU->getSubprograms())
340 processSubprogram(SP);
341 for (auto *ET : CU->getEnumTypes())
342 processType(ET);
343 for (auto *RT : CU->getRetainedTypes())
344 processType(RT);
345 for (DIImportedEntity Import : CU->getImportedEntities()) {
Adrian Prantld09ba232014-04-01 03:41:04 +0000346 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000347 if (auto *T = dyn_cast<MDType>(Entity))
348 processType(T);
349 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
350 processSubprogram(SP);
351 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
352 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000353 }
354 }
355 }
356}
357
Manman Ren2085ccc2013-11-17 18:42:37 +0000358void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000359 if (!Loc)
360 return;
Manman Renb46e5502013-11-17 19:35:03 +0000361 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000362 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000363 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000364}
365
Bill Wendling523bea82013-11-08 08:13:15 +0000366void DebugInfoFinder::processType(DIType DT) {
367 if (!addType(DT))
368 return;
369 processScope(DT.getContext().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000370 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000371 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000372 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000373 for (MDTypeRef Ref : ST->getTypeArray())
374 processType(Ref.resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000375 return;
376 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000377 for (Metadata *D : DCT->getElements()->operands()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000378 if (DIType T = dyn_cast<MDType>(D))
379 processType(T);
380 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
381 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000382 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000383 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000384 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
385 }
386}
387
388void DebugInfoFinder::processScope(DIScope Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000389 if (!Scope)
390 return;
391 if (DIType Ty = dyn_cast<MDType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000392 processType(Ty);
393 return;
394 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000395 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
396 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000397 return;
398 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000399 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
400 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000401 return;
402 }
403 if (!addScope(Scope))
404 return;
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000405 if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000406 processScope(LB.getContext());
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000407 } else if (DINameSpace NS = dyn_cast<MDNamespace>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000408 processScope(NS.getContext());
409 }
410}
411
Bill Wendling523bea82013-11-08 08:13:15 +0000412void DebugInfoFinder::processSubprogram(DISubprogram SP) {
413 if (!addSubprogram(SP))
414 return;
415 processScope(SP.getContext().resolve(TypeIdentifierMap));
416 processType(SP.getType());
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000417 for (auto *Element : SP.getTemplateParams()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000418 if (DITemplateTypeParameter TType =
419 dyn_cast<MDTemplateTypeParameter>(Element)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000420 processType(TType.getType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000421 } else if (DITemplateValueParameter TVal =
422 dyn_cast<MDTemplateValueParameter>(Element)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000423 processType(TVal.getType().resolve(TypeIdentifierMap));
424 }
425 }
426}
427
Manman Ren2085ccc2013-11-17 18:42:37 +0000428void DebugInfoFinder::processDeclare(const Module &M,
429 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000430 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
431 if (!N)
432 return;
Manman Renb46e5502013-11-17 19:35:03 +0000433 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000434
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000435 DIVariable DV = dyn_cast<MDLocalVariable>(N);
436 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000437 return;
438
David Blaikie70573dc2014-11-19 07:49:26 +0000439 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000440 return;
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000441 processScope(DV.getContext());
442 processType(DV.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000443}
444
Manman Ren2085ccc2013-11-17 18:42:37 +0000445void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000446 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
447 if (!N)
448 return;
Manman Renb46e5502013-11-17 19:35:03 +0000449 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000450
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000451 DIVariable DV = dyn_cast<MDLocalVariable>(N);
452 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000453 return;
454
David Blaikie70573dc2014-11-19 07:49:26 +0000455 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000456 return;
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000457 processScope(DV.getContext());
458 processType(DV.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000459}
460
Bill Wendling523bea82013-11-08 08:13:15 +0000461bool DebugInfoFinder::addType(DIType DT) {
462 if (!DT)
463 return false;
464
David Blaikie70573dc2014-11-19 07:49:26 +0000465 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000466 return false;
467
468 TYs.push_back(DT);
469 return true;
470}
471
Bill Wendling523bea82013-11-08 08:13:15 +0000472bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
473 if (!CU)
474 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000475 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000476 return false;
477
478 CUs.push_back(CU);
479 return true;
480}
481
Bill Wendling523bea82013-11-08 08:13:15 +0000482bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
483 if (!DIG)
484 return false;
485
David Blaikie70573dc2014-11-19 07:49:26 +0000486 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000487 return false;
488
489 GVs.push_back(DIG);
490 return true;
491}
492
Bill Wendling523bea82013-11-08 08:13:15 +0000493bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
494 if (!SP)
495 return false;
496
David Blaikie70573dc2014-11-19 07:49:26 +0000497 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000498 return false;
499
500 SPs.push_back(SP);
501 return true;
502}
503
504bool DebugInfoFinder::addScope(DIScope Scope) {
505 if (!Scope)
506 return false;
507 // FIXME: Ocaml binding generates a scope with no content, we treat it
508 // as null for now.
509 if (Scope->getNumOperands() == 0)
510 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000511 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000512 return false;
513 Scopes.push_back(Scope);
514 return true;
515}
516
517//===----------------------------------------------------------------------===//
518// DIDescriptor: dump routines for all descriptors.
519//===----------------------------------------------------------------------===//
520
Bill Wendling523bea82013-11-08 08:13:15 +0000521void DIDescriptor::dump() const {
522 print(dbgs());
523 dbgs() << '\n';
524}
525
Bill Wendling523bea82013-11-08 08:13:15 +0000526void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000527 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000528 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000529 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000530}
531
532static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
533 const LLVMContext &Ctx) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000534 if (!DL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000535 return;
536
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000537 DIScope Scope = cast<MDScope>(DL.getScope());
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000538 // Omit the directory, because it's likely to be long and uninteresting.
539 CommentOS << Scope.getFilename();
540 CommentOS << ':' << DL.getLine();
541 if (DL.getCol() != 0)
542 CommentOS << ':' << DL.getCol();
543
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000544 DebugLoc InlinedAtDL = DL.getInlinedAt();
545 if (!InlinedAtDL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000546 return;
547
548 CommentOS << " @[ ";
549 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
550 CommentOS << " ]";
Bill Wendling523bea82013-11-08 08:13:15 +0000551}
552
553void DIVariable::printExtendedName(raw_ostream &OS) const {
554 const LLVMContext &Ctx = DbgNode->getContext();
555 StringRef Res = getName();
556 if (!Res.empty())
557 OS << Res << "," << getLineNumber();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000558 if (auto *InlinedAt = get()->getInlinedAt()) {
559 if (DebugLoc InlinedAtDL = InlinedAt) {
Bill Wendling523bea82013-11-08 08:13:15 +0000560 OS << " @[";
561 printDebugLoc(InlinedAtDL, OS, Ctx);
562 OS << "]";
563 }
564 }
565}
566
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000567template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
568 assert(isDescriptorRef(V) &&
569 "DIDescriptorRef should be a MDString or MDNode");
570}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000571template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000572 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
573}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000574template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000575 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
576}
577
Bill Wendling523bea82013-11-08 08:13:15 +0000578template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000579DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
580 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
581}
582template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000583DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000584 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000585}
Bill Wendling523bea82013-11-08 08:13:15 +0000586template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000587 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000588}
Manman Rencb14bbc2013-11-22 22:06:31 +0000589
Duncan P. N. Exon Smith5bf3cdc2015-04-06 22:27:37 +0000590template <>
591DIDescriptor
592DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
593 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
594}
595template <>
596DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
597 return MDScopeRef(Val).resolve(Map);
598}
599template <>
600DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
601 return MDTypeRef(Val).resolve(Map);
602}
603
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000604bool llvm::stripDebugInfo(Function &F) {
605 bool Changed = false;
606 for (BasicBlock &BB : F) {
607 for (Instruction &I : BB) {
608 if (I.getDebugLoc()) {
609 Changed = true;
610 I.setDebugLoc(DebugLoc());
611 }
612 }
613 }
614 return Changed;
615}
616
Manman Rencb14bbc2013-11-22 22:06:31 +0000617bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000618 bool Changed = false;
619
620 // Remove all of the calls to the debugger intrinsics, and remove them from
621 // the module.
622 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
623 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000624 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000625 CI->eraseFromParent();
626 }
627 Declare->eraseFromParent();
628 Changed = true;
629 }
630
631 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
632 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000633 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000634 CI->eraseFromParent();
635 }
636 DbgVal->eraseFromParent();
637 Changed = true;
638 }
639
640 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
641 NME = M.named_metadata_end(); NMI != NME;) {
642 NamedMDNode *NMD = NMI;
643 ++NMI;
644 if (NMD->getName().startswith("llvm.dbg.")) {
645 NMD->eraseFromParent();
646 Changed = true;
647 }
648 }
649
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000650 for (Function &F : M)
651 Changed |= stripDebugInfo(F);
652
Rafael Espindola468b8682015-04-01 14:44:59 +0000653 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000654 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000655
656 return Changed;
657}
Manman Ren8b4306c2013-12-02 21:29:56 +0000658
Manman Renbd4daf82013-12-03 00:12:14 +0000659unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000660 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000661 M.getModuleFlag("Debug Info Version")))
662 return Val->getZExtValue();
663 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000664}
David Blaikie6876b3b2014-07-01 20:05:26 +0000665
David Blaikiea8c35092014-07-02 18:30:05 +0000666llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
667llvm::makeSubprogramMap(const Module &M) {
668 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000669
670 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
671 if (!CU_Nodes)
672 return R;
673
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000674 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000675 DICompileUnit CUNode = cast<MDCompileUnit>(N);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000676 for (DISubprogram SP : CUNode->getSubprograms()) {
David Blaikie6876b3b2014-07-01 20:05:26 +0000677 if (Function *F = SP.getFunction())
678 R.insert(std::make_pair(F, SP));
679 }
680 }
681 return R;
682}