Move all of the header files which are involved in modelling the LLVM IR
[oota-llvm.git] / lib / Analysis / DbgInfoPrinter.cpp
index 6346a90a68c7650374cd9d75b15715627e768b36..f674e0cc3eeaf3e869c0dd2027c463d73b668054 100644 (file)
@@ -1,4 +1,4 @@
-//===- DbgInfoPrinter.cpp - Print debug info in a human readable form -----==//
+//===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==//
 //
 //                     The LLVM Compiler Infrastructure
 //
 //
 // This file implements a pass that prints instructions, and associated debug
 // info:
+// 
 //   - source/line/col information
 //   - original variable name
 //   - original type name
 //
 //===----------------------------------------------------------------------===//
-#include "llvm/Pass.h"
-#include "llvm/Function.h"
-#include "llvm/Module.h"
-#include "llvm/Value.h"
-#include "llvm/IntrinsicInst.h"
-#include "llvm/Assembly/Writer.h"
-#include "llvm/Analysis/DebugInfo.h"
+
 #include "llvm/Analysis/Passes.h"
-#include "llvm/Analysis/ValueTracking.h"
+#include "llvm/Assembly/Writer.h"
+#include "llvm/DebugInfo.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Pass.h"
 #include "llvm/Support/CFG.h"
-#include "llvm/Support/Compiler.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/raw_ostream.h"
+
 using namespace llvm;
 
 static cl::opt<bool>
-PrintDirectory("print-fullpath", cl::desc("Print fullpath when printing debug info"), cl::Hidden);
+PrintDirectory("print-fullpath",
+               cl::desc("Print fullpath when printing debug info"),
+               cl::Hidden);
 
 namespace {
-  struct VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
-    private:
-      raw_ostream &Out;
-      void printStopPoint(const DbgStopPointInst *DSI);
-      void printFuncStart(const DbgFuncStartInst *FS);
-      void printVariableDeclaration(const Value *V);
+  class PrintDbgInfo : public FunctionPass {
+    raw_ostream &Out;
+    void printVariableDeclaration(const Value *V);
   public:
-      static char ID; // Pass identification
-      PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
+    static char ID; // Pass identification
+    PrintDbgInfo() : FunctionPass(ID), Out(errs()) {
+      initializePrintDbgInfoPass(*PassRegistry::getPassRegistry());
+    }
 
-      virtual bool runOnFunction(Function &F);
-      virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-        AU.setPreservesAll();
-      }
+    virtual bool runOnFunction(Function &F);
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesAll();
+    }
   };
   char PrintDbgInfo::ID = 0;
-  static RegisterPass<PrintDbgInfo> X("print-dbginfo",
-                                     "Print debug info in human readable form");
 }
 
+INITIALIZE_PASS(PrintDbgInfo, "print-dbginfo",
+                "Print debug info in human readable form", false, false)
+
 FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
 
-void PrintDbgInfo::printVariableDeclaration(const Value *V) {
-  std::string DisplayName, File, Directory, Type;
-  unsigned LineNo;
-  if (getLocationInfo(V, DisplayName, Type, LineNo, File, Directory)) {
-    Out << "; ";
-    WriteAsOperand(Out, V, false, 0);
-    Out << " is variable " << DisplayName
-      << " of type " << Type << " declared at ";
-    if (PrintDirectory) {
-      Out << Directory << "/";
-    }
-    Out << File << ":" << LineNo << "\n";
+/// Find the debug info descriptor corresponding to this global variable.
+static Value *findDbgGlobalDeclare(GlobalVariable *V) {
+  const Module *M = V->getParent();
+  NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
+  if (!NMD)
+    return 0;
+
+  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
+    DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
+    if (!DIG.isGlobalVariable())
+      continue;
+    if (DIGlobalVariable(DIG).getGlobal() == V)
+      return DIG;
   }
+  return 0;
 }
 
-void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI)
-{
-  if (PrintDirectory) {
-    const char *Dir = GetConstantStringInfo(DSI->getDirectory());
-    Out << (Dir ? Dir : "") << "/";
+/// Find the debug info descriptor corresponding to this function.
+static Value *findDbgSubprogramDeclare(Function *V) {
+  const Module *M = V->getParent();
+  NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp");
+  if (!NMD)
+    return 0;
+
+  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
+    DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
+    if (!DIG.isSubprogram())
+      continue;
+    if (DISubprogram(DIG).getFunction() == V)
+      return DIG;
   }
+  return 0;
+}
 
-  const char *FN = GetConstantStringInfo(DSI->getFileName());
-  Out << (FN ? FN : "") << ":" << DSI->getLine();
+/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
+/// It looks through pointer casts too.
+static const DbgDeclareInst *findDbgDeclare(const Value *V) {
+  V = V->stripPointerCasts();
 
-  if (unsigned Col = DSI->getColumn())
-    Out << ":" << Col;
+  if (!isa<Instruction>(V) && !isa<Argument>(V))
+    return 0;
+
+  const Function *F = NULL;
+  if (const Instruction *I = dyn_cast<Instruction>(V))
+    F = I->getParent()->getParent();
+  else if (const Argument *A = dyn_cast<Argument>(V))
+    F = A->getParent();
+
+  for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
+    for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
+         BI != BE; ++BI)
+      if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
+        if (DDI->getAddress() == V)
+          return DDI;
+
+  return 0;
 }
 
-void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS)
-{
-  DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
-  Out << ";fully qualified function name: " << Subprogram.getDisplayName()
-    << " return type: " << Subprogram.getType().getName()
-    << " at line " << Subprogram.getLineNumber()
-    << "\n\n";
+static bool getLocationInfo(const Value *V, std::string &DisplayName,
+                            std::string &Type, unsigned &LineNo,
+                            std::string &File, std::string &Dir) {
+  DICompileUnit Unit;
+  DIType TypeD;
+
+  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
+    Value *DIGV = findDbgGlobalDeclare(GV);
+    if (!DIGV) return false;
+    DIGlobalVariable Var(cast<MDNode>(DIGV));
+
+    StringRef D = Var.getDisplayName();
+    if (!D.empty())
+      DisplayName = D;
+    LineNo = Var.getLineNumber();
+    Unit = Var.getCompileUnit();
+    TypeD = Var.getType();
+  } else if (Function *F = dyn_cast<Function>(const_cast<Value*>(V))){
+    Value *DIF = findDbgSubprogramDeclare(F);
+    if (!DIF) return false;
+    DISubprogram Var(cast<MDNode>(DIF));
+
+    StringRef D = Var.getDisplayName();
+    if (!D.empty())
+      DisplayName = D;
+    LineNo = Var.getLineNumber();
+    Unit = Var.getCompileUnit();
+    TypeD = Var.getType();
+  } else {
+    const DbgDeclareInst *DDI = findDbgDeclare(V);
+    if (!DDI) return false;
+    DIVariable Var(cast<MDNode>(DDI->getVariable()));
+
+    StringRef D = Var.getName();
+    if (!D.empty())
+      DisplayName = D;
+    LineNo = Var.getLineNumber();
+    Unit = Var.getCompileUnit();
+    TypeD = Var.getType();
+  }
+
+  StringRef T = TypeD.getName();
+  if (!T.empty())
+    Type = T;
+  StringRef F = Unit.getFilename();
+  if (!F.empty())
+    File = F;
+  StringRef D = Unit.getDirectory();
+  if (!D.empty())
+    Dir = D;
+  return true;
+}
+
+void PrintDbgInfo::printVariableDeclaration(const Value *V) {
+  std::string DisplayName, File, Directory, Type;
+  unsigned LineNo = 0;
+
+  if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
+    return;
+
+  Out << "; ";
+  WriteAsOperand(Out, V, false, 0);
+  if (isa<Function>(V)) 
+    Out << " is function " << DisplayName
+        << " of type " << Type << " declared at ";
+  else
+    Out << " is variable " << DisplayName
+        << " of type " << Type << " declared at ";
+
+  if (PrintDirectory)
+    Out << Directory << "/";
+
+  Out << File << ":" << LineNo << "\n";
 }
 
-bool PrintDbgInfo::runOnFunction(Function &F)
-{
+bool PrintDbgInfo::runOnFunction(Function &F) {
   if (F.isDeclaration())
     return false;
+
   Out << "function " << F.getName() << "\n\n";
+
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
     BasicBlock *BB = I;
+
     if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
       // Skip dead blocks.
       continue;
-    const DbgStopPointInst *DSI = findBBStopPoint(BB);
+
     Out << BB->getName();
     Out << ":";
-    if (DSI) {
-      Out << "; (";
-      printStopPoint(DSI);
-      Out << ")";
-    }
+
     Out << "\n";
-    // A dbgstoppoint's information is valid until we encounter a new one.
-    const DbgStopPointInst *LastDSP = DSI;
-    bool printed = DSI != 0;
-    for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; ++i) {
-      if (isa<DbgInfoIntrinsic>(i)) {
-        if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
-
-          if (DSI->getContext() == LastDSP->getContext() &&
-              DSI->getLineValue() == LastDSP->getLineValue() &&
-              DSI->getColumnValue() == LastDSP->getColumnValue()) {
-            // Don't print same location twice.
-            continue;
-          }
-          LastDSP = cast<DbgStopPointInst>(i);
-          // Don't print consecutive stoppoints, use a flag
-          // to know which one we printed.
-          printed = false;
-
-        } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
-          printFuncStart(FS);
-        }
-      } else {
-        if (!printed && LastDSP) {
-          Out << "; ";
-          printStopPoint(LastDSP);
-          Out << "\n";
-          printed = true;
-        }
-        Out << *i;
+
+    for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
+         i != e; ++i) {
+
         printVariableDeclaration(i);
+
         if (const User *U = dyn_cast<User>(i)) {
-          for(unsigned i=0;i<U->getNumOperands();i++) {
+          for(unsigned i=0;i<U->getNumOperands();i++)
             printVariableDeclaration(U->getOperand(i));
-          }
         }
-      }
     }
   }
   return false;