blob: 2f602be78a05ae4bcab90bf4d0027ee7e5a4c3f7 [file] [log] [blame]
Daniel Malea13ace662013-05-08 20:44:14 +00001//===--- DebugIR.cpp - Transform debug metadata to allow debugging IR -----===//
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// A Module transform pass that emits a succinct version of the IR and replaces
11// the source file metadata to allow debuggers to step through the IR.
12//
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000013// FIXME: instead of replacing debug metadata, this pass should allow for
14// additional metadata to be used to point capable debuggers to the IR file
15// without destroying the mapping to the original source file.
Daniel Malea13ace662013-05-08 20:44:14 +000016//
17//===----------------------------------------------------------------------===//
18
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000019#define DEBUG_TYPE "debug-ir"
Daniel Malea13ace662013-05-08 20:44:14 +000020
Daniel Maleaf8c243a2013-05-23 22:34:33 +000021#include "llvm/ADT/ValueMap.h"
22#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Daniel Malea13ace662013-05-08 20:44:14 +000023#include "llvm/DebugInfo.h"
24#include "llvm/DIBuilder.h"
Daniel Maleaf8c243a2013-05-23 22:34:33 +000025#include "llvm/InstVisitor.h"
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000026#include "llvm/IR/DataLayout.h"
Daniel Malea13ace662013-05-08 20:44:14 +000027#include "llvm/IR/Instruction.h"
Daniel Malea13ace662013-05-08 20:44:14 +000028#include "llvm/IR/Module.h"
Daniel Malea13ace662013-05-08 20:44:14 +000029#include "llvm/Transforms/Instrumentation.h"
Daniel Maleaf8c243a2013-05-23 22:34:33 +000030#include "llvm/Transforms/Utils/Cloning.h"
Daniel Malea13ace662013-05-08 20:44:14 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ToolOutputFile.h"
Daniel Maleaf8c243a2013-05-23 22:34:33 +000033#include "llvm/Support/FormattedStream.h"
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000034#include "llvm/Support/FileSystem.h"
35#include "llvm/Support/Path.h"
36
37#include "DebugIR.h"
38
39#include <string>
40#include <unistd.h>
41
42#define STR_HELPER(x) #x
43#define STR(x) STR_HELPER(x)
44
Daniel Malea13ace662013-05-08 20:44:14 +000045using namespace llvm;
46
47namespace {
48
Daniel Maleaf8c243a2013-05-23 22:34:33 +000049/// Builds a map of Value* to line numbers on which the Value appears in a
50/// textual representation of the IR by plugging into the AssemblyWriter by
51/// masquerading as an AssemblyAnnotationWriter.
52class ValueToLineMap : public AssemblyAnnotationWriter {
53 ValueMap<const Value *, unsigned int> Lines;
54 typedef ValueMap<const Value *, unsigned int>::const_iterator LineIter;
Daniel Malea13ace662013-05-08 20:44:14 +000055
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000056 void addEntry(const Value *V, formatted_raw_ostream &Out) {
57 Out.flush();
58 Lines.insert(std::make_pair(V, Out.getLine() + 1));
59 }
60
Daniel Maleaf8c243a2013-05-23 22:34:33 +000061public:
Daniel Malea13ace662013-05-08 20:44:14 +000062
Daniel Maleaf8c243a2013-05-23 22:34:33 +000063 /// Prints Module to a null buffer in order to build the map of Value pointers
64 /// to line numbers.
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000065 ValueToLineMap(const Module *M) {
Daniel Maleaf8c243a2013-05-23 22:34:33 +000066 raw_null_ostream ThrowAway;
67 M->print(ThrowAway, this);
68 }
69
70 // This function is called after an Instruction, GlobalValue, or GlobalAlias
71 // is printed.
72 void printInfoComment(const Value &V, formatted_raw_ostream &Out) {
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000073 addEntry(&V, Out);
74 }
75
76 void emitFunctionAnnot(const Function *F, formatted_raw_ostream &Out) {
77 addEntry(F, Out);
Daniel Maleaf8c243a2013-05-23 22:34:33 +000078 }
79
80 /// If V appears on a line in the textual IR representation, sets Line to the
81 /// line number and returns true, otherwise returns false.
82 bool getLine(const Value *V, unsigned int &Line) const {
83 LineIter i = Lines.find(V);
84 if (i != Lines.end()) {
85 Line = i->second;
86 return true;
87 }
88 return false;
89 }
90};
91
92/// Removes debug intrisncs like llvm.dbg.declare and llvm.dbg.value.
93class DebugIntrinsicsRemover : public InstVisitor<DebugIntrinsicsRemover> {
94 void remove(Instruction &I) { I.eraseFromParent(); }
95
96public:
Daniel Maleaaadaf9f2013-06-28 19:05:23 +000097 static void process(Module &M) {
98 DebugIntrinsicsRemover Remover;
99 Remover.visit(&M);
100 }
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000101 void visitDbgDeclareInst(DbgDeclareInst &I) { remove(I); }
102 void visitDbgValueInst(DbgValueInst &I) { remove(I); }
103 void visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) { remove(I); }
104};
105
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000106/// Removes debug metadata (!dbg) nodes from all instructions, and optionally
107/// metadata named "llvm.dbg.cu" if RemoveNamedInfo is true.
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000108class DebugMetadataRemover : public InstVisitor<DebugMetadataRemover> {
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000109 bool RemoveNamedInfo;
110
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000111public:
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000112 static void process(Module &M, bool RemoveNamedInfo = true) {
113 DebugMetadataRemover Remover(RemoveNamedInfo);
114 Remover.run(&M);
115 }
116
117 DebugMetadataRemover(bool RemoveNamedInfo)
118 : RemoveNamedInfo(RemoveNamedInfo) {}
119
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000120 void visitInstruction(Instruction &I) {
121 if (I.getMetadata(LLVMContext::MD_dbg))
122 I.setMetadata(LLVMContext::MD_dbg, 0);
123 }
124
125 void run(Module *M) {
126 // Remove debug metadata attached to instructions
127 visit(M);
128
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000129 if (RemoveNamedInfo) {
130 // Remove CU named metadata (and all children nodes)
131 NamedMDNode *Node = M->getNamedMetadata("llvm.dbg.cu");
132 if (Node)
133 M->eraseNamedMetadata(Node);
134 }
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000135 }
136};
137
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000138/// Updates debug metadata in a Module:
139/// - changes Filename/Directory to values provided on construction
140/// - adds/updates line number (DebugLoc) entries associated with each
141/// instruction to reflect the instruction's location in an LLVM IR file
142class DIUpdater : public InstVisitor<DIUpdater> {
143 /// Builder of debug information
144 DIBuilder Builder;
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000145
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000146 /// Helper for type attributes/sizes/etc
147 DataLayout Layout;
148
149 /// Map of Value* to line numbers
150 const ValueToLineMap LineTable;
151
152 /// Map of Value* (in original Module) to Value* (in optional cloned Module)
153 const ValueToValueMapTy *VMap;
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000154
Daniel Malea13ace662013-05-08 20:44:14 +0000155 /// Directory of debug metadata
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000156 DebugInfoFinder Finder;
157
158 /// Source filename and directory
159 StringRef Filename;
160 StringRef Directory;
161
162 // CU nodes needed when creating DI subprograms
163 MDNode *FileNode;
164 MDNode *LexicalBlockFileNode;
165 const MDNode *CUNode;
166
167 ValueMap<const Function *, MDNode *> SubprogramDescriptors;
168 DenseMap<const Type *, MDNode *> TypeDescriptors;
Daniel Malea13ace662013-05-08 20:44:14 +0000169
Daniel Malea13ace662013-05-08 20:44:14 +0000170public:
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000171 DIUpdater(Module &M, StringRef Filename = StringRef(),
172 StringRef Directory = StringRef(), const Module *DisplayM = 0,
173 const ValueToValueMapTy *VMap = 0)
174 : Builder(M), Layout(&M), LineTable(DisplayM ? DisplayM : &M), VMap(VMap),
175 Finder(), Filename(Filename), Directory(Directory), FileNode(0),
176 LexicalBlockFileNode(0), CUNode(0) {
177 Finder.processModule(M);
178 visit(&M);
179 }
180
181 ~DIUpdater() { Builder.finalize(); }
182
183 void visitModule(Module &M) {
184 if (Finder.compile_unit_count() > 1)
185 report_fatal_error("DebugIR pass supports only a signle compile unit per "
186 "Module.");
187 createCompileUnit(
188 Finder.compile_unit_count() == 1 ? *Finder.compile_unit_begin() : 0);
189 }
190
191 void visitFunction(Function &F) {
192 if (F.isDeclaration() || findDISubprogram(&F))
193 return;
194
195 StringRef MangledName = F.getName();
196 DICompositeType Sig = createFunctionSignature(&F);
197
198 // find line of function declaration
199 unsigned Line = 0;
200 if (!findLine(&F, Line)) {
201 DEBUG(dbgs() << "WARNING: No line for Function " << F.getName().str()
202 << "\n");
203 return;
204 }
205
206 Instruction *FirstInst = F.begin()->begin();
207 unsigned ScopeLine = 0;
208 if (!findLine(FirstInst, ScopeLine)) {
209 DEBUG(dbgs() << "WARNING: No line for 1st Instruction in Function "
210 << F.getName().str() << "\n");
211 return;
212 }
213
214 bool Local = F.hasInternalLinkage();
215 bool IsDefinition = !F.isDeclaration();
216 bool IsOptimized = false;
217
218 int FuncFlags = llvm::DIDescriptor::FlagPrototyped;
219 assert(CUNode && FileNode);
220 MDNode *Sub = Builder.createFunction(
221 DICompileUnit(CUNode), F.getName(), MangledName, DIFile(FileNode), Line,
222 Sig, Local, IsDefinition, ScopeLine, FuncFlags, IsOptimized, &F);
223 assert(DISubprogram(Sub).Verify());
224 DEBUG(dbgs() << "create subprogram mdnode " << Sub << ": "
225 << "\n");
226
227 SubprogramDescriptors.insert(std::make_pair(&F, Sub));
228 }
Daniel Malea13ace662013-05-08 20:44:14 +0000229
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000230 void visitInstruction(Instruction &I) {
Daniel Malea13ace662013-05-08 20:44:14 +0000231 DebugLoc Loc(I.getDebugLoc());
232
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000233 /// If a ValueToValueMap is provided, use it to get the real instruction as
234 /// the line table was generated on a clone of the module on which we are
235 /// operating.
236 Value *RealInst = 0;
237 if (VMap)
238 RealInst = VMap->lookup(&I);
239
240 if (!RealInst)
241 RealInst = &I;
242
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000243 unsigned Col = 0; // FIXME: support columns
244 unsigned Line;
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000245 if (!LineTable.getLine(RealInst, Line)) {
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000246 // Instruction has no line, it may have been removed (in the module that
247 // will be passed to the debugger) so there is nothing to do here.
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000248 DEBUG(dbgs() << "WARNING: no LineTable entry for instruction " << RealInst
249 << "\n");
250 DEBUG(RealInst->dump());
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000251 return;
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000252 }
Daniel Malea13ace662013-05-08 20:44:14 +0000253
254 DebugLoc NewLoc;
255 if (!Loc.isUnknown())
256 // I had a previous debug location: re-use the DebugLoc
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000257 NewLoc = DebugLoc::get(Line, Col, Loc.getScope(RealInst->getContext()),
258 Loc.getInlinedAt(RealInst->getContext()));
259 else if (MDNode *scope = findScope(&I))
260 NewLoc = DebugLoc::get(Line, Col, scope, 0);
261 else {
262 DEBUG(dbgs() << "WARNING: no valid scope for instruction " << &I
263 << ". no DebugLoc will be present."
264 << "\n");
Daniel Malea13ace662013-05-08 20:44:14 +0000265 return;
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000266 }
Daniel Malea13ace662013-05-08 20:44:14 +0000267
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000268 addDebugLocation(I, NewLoc);
Daniel Malea13ace662013-05-08 20:44:14 +0000269 }
270
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000271private:
Daniel Malea13ace662013-05-08 20:44:14 +0000272
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000273 void createCompileUnit(MDNode *CUToReplace) {
274 std::string Flags;
275 bool IsOptimized = false;
276 StringRef Producer;
277 unsigned RuntimeVersion(0);
278 StringRef SplitName;
279
280 if (CUToReplace) {
281 // save fields from existing CU to re-use in the new CU
282 DICompileUnit ExistingCU(CUToReplace);
283 Producer = ExistingCU.getProducer();
284 IsOptimized = ExistingCU.isOptimized();
285 Flags = ExistingCU.getFlags();
286 RuntimeVersion = ExistingCU.getRunTimeVersion();
287 SplitName = ExistingCU.getSplitDebugFilename();
288 } else {
289 Producer =
290 "LLVM Version " STR(LLVM_VERSION_MAJOR) "." STR(LLVM_VERSION_MINOR);
291 }
292
293 Builder.createCompileUnit(dwarf::DW_LANG_C99, Filename, Directory, Producer,
294 IsOptimized, Flags, RuntimeVersion);
295 CUNode = Builder.getCU();
296
297 if (CUToReplace)
298 CUToReplace->replaceAllUsesWith(const_cast<MDNode *>(CUNode));
299
300 DICompileUnit CU(CUNode);
301 FileNode = Builder.createFile(Filename, Directory);
302 LexicalBlockFileNode = Builder.createLexicalBlockFile(CU, DIFile(FileNode));
303 }
304
305 /// Returns the MDNode* that represents the DI scope to associate with I
306 MDNode *findScope(const Instruction *I) {
307 const Function *F = I->getParent()->getParent();
308 if (MDNode *ret = findDISubprogram(F))
309 return ret;
310
311 DEBUG(dbgs() << "WARNING: Using fallback lexical block file scope "
312 << LexicalBlockFileNode << " as scope for instruction " << I
313 << "\n");
314 return LexicalBlockFileNode;
315 }
316
317 /// Returns the MDNode* that is the descriptor for F
318 MDNode *findDISubprogram(const Function *F) {
319 typedef ValueMap<const Function *, MDNode *>::const_iterator FuncNodeIter;
320 FuncNodeIter i = SubprogramDescriptors.find(F);
321 if (i != SubprogramDescriptors.end())
322 return i->second;
323
324 DEBUG(dbgs() << "searching for DI scope node for Function " << F
325 << " in a list of " << Finder.subprogram_count()
326 << " subprogram nodes"
327 << "\n");
328
Daniel Malea13ace662013-05-08 20:44:14 +0000329 for (DebugInfoFinder::iterator i = Finder.subprogram_begin(),
330 e = Finder.subprogram_end();
331 i != e; ++i) {
332 DISubprogram S(*i);
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000333 if (S.getFunction() == F) {
334 DEBUG(dbgs() << "Found DISubprogram " << *i << " for function "
335 << S.getFunction() << "\n");
Daniel Malea13ace662013-05-08 20:44:14 +0000336 return *i;
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000337 }
Daniel Malea13ace662013-05-08 20:44:14 +0000338 }
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000339 DEBUG(dbgs() << "unable to find DISubprogram node for function "
340 << F->getName().str() << "\n");
Daniel Malea13ace662013-05-08 20:44:14 +0000341 return 0;
342 }
343
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000344 /// Sets Line to the line number on which V appears and returns true. If a
345 /// line location for V is not found, returns false.
346 bool findLine(const Value *V, unsigned &Line) {
347 if (LineTable.getLine(V, Line))
348 return true;
349
350 if (VMap) {
351 Value *mapped = VMap->lookup(V);
352 if (mapped && LineTable.getLine(mapped, Line))
353 return true;
354 }
355 return false;
356 }
357
358 std::string getTypeName(Type *T) {
359 std::string TypeName;
360 raw_string_ostream TypeStream(TypeName);
361 T->print(TypeStream);
362 TypeStream.flush();
363 return TypeName;
364 }
365
366 /// Returns the MDNode that represents type T if it is already created, or 0
367 /// if it is not.
368 MDNode *getType(const Type *T) {
369 typedef DenseMap<const Type *, MDNode *>::const_iterator TypeNodeIter;
370 TypeNodeIter i = TypeDescriptors.find(T);
371 if (i != TypeDescriptors.end())
372 return i->second;
373 return 0;
374 }
375
376 /// Returns a DebugInfo type from an LLVM type T.
377 DIDerivedType getOrCreateType(Type *T) {
378 MDNode *N = getType(T);
379 if (N)
380 return DIDerivedType(N);
381 else if (T->isVoidTy())
382 return DIDerivedType(0);
383 else if (T->isStructTy()) {
384 N = Builder.createStructType(
385 DIScope(LexicalBlockFileNode), T->getStructName(), DIFile(FileNode),
386 0, Layout.getTypeSizeInBits(T), Layout.getABITypeAlignment(T), 0,
387 DIType(0), DIArray(0)); // filled in later
388
389 // N is added to the map (early) so that element search below can find it,
390 // so as to avoid infinite recursion for structs that contain pointers to
391 // their own type.
392 TypeDescriptors[T] = N;
393 DICompositeType StructDescriptor(N);
394
395 SmallVector<Value *, 4> Elements;
396 for (unsigned i = 0; i < T->getStructNumElements(); ++i)
397 Elements.push_back(getOrCreateType(T->getStructElementType(i)));
398
399 // set struct elements
400 StructDescriptor.setTypeArray(Builder.getOrCreateArray(Elements));
401 } else if (T->isPointerTy()) {
402 Type *PointeeTy = T->getPointerElementType();
403 if (!(N = getType(PointeeTy)))
404 N = Builder.createPointerType(
405 getOrCreateType(PointeeTy), Layout.getPointerSizeInBits(),
406 Layout.getPrefTypeAlignment(T), getTypeName(T));
407 } else if (T->isArrayTy()) {
408 SmallVector<Value *, 1> Subrange;
409 Subrange.push_back(
410 Builder.getOrCreateSubrange(0, T->getArrayNumElements() - 1));
411
412 N = Builder.createArrayType(Layout.getTypeSizeInBits(T),
413 Layout.getPrefTypeAlignment(T),
414 getOrCreateType(T->getArrayElementType()),
415 Builder.getOrCreateArray(Subrange));
416 } else {
417 int encoding = llvm::dwarf::DW_ATE_signed;
418 if (T->isIntegerTy())
419 encoding = llvm::dwarf::DW_ATE_unsigned;
420 else if (T->isFloatingPointTy())
421 encoding = llvm::dwarf::DW_ATE_float;
422
423 N = Builder.createBasicType(getTypeName(T), T->getPrimitiveSizeInBits(),
424 0, encoding);
425 }
426 TypeDescriptors[T] = N;
427 return DIDerivedType(N);
428 }
429
430 /// Returns a DebugInfo type that represents a function signature for Func.
431 DICompositeType createFunctionSignature(const Function *Func) {
432 SmallVector<Value *, 4> Params;
433 DIDerivedType ReturnType(getOrCreateType(Func->getReturnType()));
434 Params.push_back(ReturnType);
435
436 const Function::ArgumentListType &Args(Func->getArgumentList());
437 for (Function::ArgumentListType::const_iterator i = Args.begin(),
438 e = Args.end();
439 i != e; ++i) {
440 Type *T(i->getType());
441 Params.push_back(getOrCreateType(T));
442 }
443
444 DIArray ParamArray = Builder.getOrCreateArray(Params);
445 return Builder.createSubroutineType(DIFile(FileNode), ParamArray);
446 }
447
448 /// Associates Instruction I with debug location Loc.
Daniel Malea13ace662013-05-08 20:44:14 +0000449 void addDebugLocation(Instruction &I, DebugLoc Loc) {
450 MDNode *MD = Loc.getAsMDNode(I.getContext());
451 I.setMetadata(LLVMContext::MD_dbg, MD);
452 }
453};
454
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000455/// Sets Filename/Directory from the Module identifier and returns true, or
456/// false if source information is not present.
457bool getSourceInfoFromModule(const Module &M, std::string &Directory,
458 std::string &Filename) {
459 std::string PathStr(M.getModuleIdentifier());
460 if (PathStr.length() == 0 || PathStr == "<stdin>")
461 return false;
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000462
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000463 Filename = sys::path::filename(PathStr);
464 SmallVector<char, 16> Path(PathStr.begin(), PathStr.end());
465 sys::path::remove_filename(Path);
466 Directory = StringRef(Path.data(), Path.size());
467 return true;
468}
Daniel Malea13ace662013-05-08 20:44:14 +0000469
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000470// Sets Filename/Directory from debug information in M and returns true, or
471// false if no debug information available, or cannot be parsed.
472bool getSourceInfoFromDI(const Module &M, std::string &Directory,
473 std::string &Filename) {
474 NamedMDNode *CUNode = M.getNamedMetadata("llvm.dbg.cu");
475 if (!CUNode || CUNode->getNumOperands() == 0)
476 return false;
Daniel Malea13ace662013-05-08 20:44:14 +0000477
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000478 DICompileUnit CU(CUNode->getOperand(0));
479 if (!CU.Verify())
480 return false;
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000481
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000482 Filename = CU.getFilename();
483 Directory = CU.getDirectory();
484 return true;
485}
Daniel Malea13ace662013-05-08 20:44:14 +0000486
487} // anonymous namespace
488
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000489namespace llvm {
490
491bool DebugIR::getSourceInfo(const Module &M) {
492 ParsedPath = getSourceInfoFromDI(M, Directory, Filename) ||
493 getSourceInfoFromModule(M, Directory, Filename);
494 return ParsedPath;
495}
496
497bool DebugIR::updateExtension(StringRef NewExtension) {
498 size_t dot = Filename.find_last_of(".");
499 if (dot == std::string::npos)
500 return false;
501
502 Filename.erase(dot);
503 Filename += NewExtension.str();
504 return true;
505}
506
507void DebugIR::generateFilename(OwningPtr<int> &fd) {
508 StringRef FileModel("debug-ir-%s%s%s%s.ll");
509 SmallVector<char, 16> PathVec;
510 fd.reset(new int);
511 sys::fs::unique_file(FileModel, *fd, PathVec);
512 StringRef Path(PathVec.data(), PathVec.size());
513 Filename = sys::path::filename(Path);
514 sys::path::remove_filename(PathVec);
515 Directory = StringRef(PathVec.data(), PathVec.size());
516
517 GeneratedPath = true;
518}
519
520std::string DebugIR::getPath() {
521 SmallVector<char, 16> Path;
522 sys::path::append(Path, Directory, Filename);
523 Path.resize(Filename.size() + Directory.size() + 2);
524 Path[Filename.size() + Directory.size() + 1] = '\0';
525 return std::string(Path.data());
526}
527
528void DebugIR::writeDebugBitcode(const Module *M, int *fd) {
529 OwningPtr<raw_fd_ostream> Out;
530 std::string error;
531
532 if (!fd) {
533 std::string Path = getPath();
534 Out.reset(new raw_fd_ostream(Path.c_str(), error));
535 DEBUG(dbgs() << "WRITING debug bitcode from Module " << M << " to file "
536 << Path << "\n");
537 } else {
538 DEBUG(dbgs() << "WRITING debug bitcode from Module " << M << " to fd "
539 << *fd << "\n");
540 Out.reset(new raw_fd_ostream(*fd, true));
541 }
542
543 M->print(*Out, 0);
544 Out->close();
545 sync();
546}
547
548void DebugIR::createDebugInfo(Module &M, OwningPtr<Module> &DisplayM) {
549 if (M.getFunctionList().size() == 0)
550 // no functions -- no debug info needed
551 return;
552
553 OwningPtr<ValueToValueMapTy> VMap;
554
555 if (WriteSourceToDisk && (HideDebugIntrinsics || HideDebugMetadata)) {
556 VMap.reset(new ValueToValueMapTy);
557 DisplayM.reset(CloneModule(&M, *VMap));
558
559 if (HideDebugIntrinsics)
560 DebugIntrinsicsRemover::process(*DisplayM);
561
562 if (HideDebugMetadata)
563 DebugMetadataRemover::process(*DisplayM);
564 }
565
566 DIUpdater R(M, Filename, Directory, DisplayM.get(), VMap.get());
567}
568
569bool DebugIR::isMissingPath() { return Filename.empty() || Directory.empty(); }
570
571bool DebugIR::runOnModule(Module &M) {
572 OwningPtr<int> fd;
573
574 if (isMissingPath() && !getSourceInfo(M)) {
575 if (!WriteSourceToDisk)
576 report_fatal_error("DebugIR unable to determine file name in input. "
577 "Ensure Module contains an identifier, a valid "
578 "DICompileUnit, or construct DebugIR with "
579 "non-empty Filename/Directory parameters.");
580 else
581 generateFilename(fd);
582 }
583
584 if (!GeneratedPath && WriteSourceToDisk)
585 updateExtension(".debug-ll");
586
587 // Clear line numbers. Keep debug info (if any) if we were able to read the
588 // file name from the DICompileUnit descriptor.
589 DebugMetadataRemover::process(M, !ParsedPath);
590
591 OwningPtr<Module> DisplayM;
592 createDebugInfo(M, DisplayM);
593 if (WriteSourceToDisk) {
594 Module *OutputM = DisplayM.get() ? DisplayM.get() : &M;
595 writeDebugBitcode(OutputM, fd.get());
596 }
597
598 DEBUG(M.dump());
599 return true;
600}
601
602bool DebugIR::runOnModule(Module &M, std::string &Path) {
603 bool result = runOnModule(M);
604 Path = getPath();
605 return result;
606}
607
608} // llvm namespace
609
Daniel Malea13ace662013-05-08 20:44:14 +0000610char DebugIR::ID = 0;
611INITIALIZE_PASS(DebugIR, "debug-ir", "Enable debugging IR", false, false)
Daniel Maleaf8c243a2013-05-23 22:34:33 +0000612
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000613ModulePass *llvm::createDebugIRPass(bool HideDebugIntrinsics,
614 bool HideDebugMetadata, StringRef Directory,
615 StringRef Filename) {
616 return new DebugIR(HideDebugIntrinsics, HideDebugMetadata, Directory,
617 Filename);
Daniel Malea13ace662013-05-08 20:44:14 +0000618}
Daniel Maleaaadaf9f2013-06-28 19:05:23 +0000619
620ModulePass *llvm::createDebugIRPass() { return new DebugIR(); }