Move implicit and paralle to a separate codegen specific section.
[oota-llvm.git] / lib / Analysis / DbgInfoPrinter.cpp
1 //===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==//
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 a pass that prints instructions, and associated debug
11 // info:
12 // 
13 //   - source/line/col information
14 //   - original variable name
15 //   - original type name
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Pass.h"
20 #include "llvm/Function.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Analysis/DebugInfo.h"
24 #include "llvm/Analysis/Passes.h"
25 #include "llvm/Analysis/ValueTracking.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31
32 static cl::opt<bool>
33 PrintDirectory("print-fullpath",
34                cl::desc("Print fullpath when printing debug info"),
35                cl::Hidden);
36
37 namespace {
38   class VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
39     raw_ostream &Out;
40     void printStopPoint(const DbgStopPointInst *DSI);
41     void printFuncStart(const DbgFuncStartInst *FS);
42     void printVariableDeclaration(const Value *V);
43   public:
44     static char ID; // Pass identification
45     PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
46
47     virtual bool runOnFunction(Function &F);
48     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49       AU.setPreservesAll();
50     }
51   };
52   char PrintDbgInfo::ID = 0;
53   static RegisterPass<PrintDbgInfo> X("print-dbginfo",
54                                      "Print debug info in human readable form");
55 }
56
57 FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
58
59 void PrintDbgInfo::printVariableDeclaration(const Value *V) {
60   std::string DisplayName, File, Directory, Type;
61   unsigned LineNo;
62
63   if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
64     return;
65
66   Out << "; ";
67   WriteAsOperand(Out, V, false, 0);
68   Out << " is variable " << DisplayName
69       << " of type " << Type << " declared at ";
70
71   if (PrintDirectory)
72     Out << Directory << "/";
73
74   Out << File << ":" << LineNo << "\n";
75 }
76
77 void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) {
78   if (PrintDirectory) {
79     std::string dir;
80     GetConstantStringInfo(DSI->getDirectory(), dir);
81     Out << dir << "/";
82   }
83
84   std::string file;
85   GetConstantStringInfo(DSI->getFileName(), file);
86   Out << file << ":" << DSI->getLine();
87
88   if (unsigned Col = DSI->getColumn())
89     Out << ":" << Col;
90 }
91
92 void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) {
93   DISubprogram Subprogram(FS->getSubprogram());
94   Out << "; fully qualified function name: " << Subprogram.getDisplayName()
95       << " return type: " << Subprogram.getReturnTypeName()
96       << " at line " << Subprogram.getLineNumber()
97       << "\n\n";
98 }
99
100 bool PrintDbgInfo::runOnFunction(Function &F) {
101   if (F.isDeclaration())
102     return false;
103
104   Out << "function " << F.getName() << "\n\n";
105
106   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
107     BasicBlock *BB = I;
108
109     if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
110       // Skip dead blocks.
111       continue;
112
113     const DbgStopPointInst *DSI = findBBStopPoint(BB);
114     Out << BB->getName();
115     Out << ":";
116
117     if (DSI) {
118       Out << "; (";
119       printStopPoint(DSI);
120       Out << ")";
121     }
122
123     Out << "\n";
124
125     // A dbgstoppoint's information is valid until we encounter a new one.
126     const DbgStopPointInst *LastDSP = DSI;
127     bool Printed = DSI != 0;
128     for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
129          i != e; ++i) {
130       if (isa<DbgInfoIntrinsic>(i)) {
131         if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
132           if (DSI->getContext() == LastDSP->getContext() &&
133               DSI->getLineValue() == LastDSP->getLineValue() &&
134               DSI->getColumnValue() == LastDSP->getColumnValue())
135             // Don't print same location twice.
136             continue;
137
138           LastDSP = cast<DbgStopPointInst>(i);
139
140           // Don't print consecutive stoppoints, use a flag to know which one we
141           // printed.
142           Printed = false;
143         } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
144           printFuncStart(FS);
145         }
146       } else {
147         if (!Printed && LastDSP) {
148           Out << "; ";
149           printStopPoint(LastDSP);
150           Out << "\n";
151           Printed = true;
152         }
153
154         Out << *i << '\n';
155         printVariableDeclaration(i);
156
157         if (const User *U = dyn_cast<User>(i)) {
158           for(unsigned i=0;i<U->getNumOperands();i++)
159             printVariableDeclaration(U->getOperand(i));
160         }
161       }
162     }
163   }
164
165   return false;
166 }