[llvm-commits] CVS: reopt/tools/dumptrace/Makefile dumptrace.cpp

Brian Gaeke gaeke at cs.uiuc.edu
Tue Jun 29 16:57:01 PDT 2004


Changes in directory reopt/tools/dumptrace:

Makefile added (r1.1)
dumptrace.cpp added (r1.1)

---
Log message:

New trace dumping utility


---
Diffs of the changes:  (+188 -0)

Index: reopt/tools/dumptrace/Makefile
diff -c /dev/null reopt/tools/dumptrace/Makefile:1.1
*** /dev/null	Tue Jun 29 16:56:25 2004
--- reopt/tools/dumptrace/Makefile	Tue Jun 29 16:56:14 2004
***************
*** 0 ****
--- 1,8 ----
+ LEVEL = ../..
+ TOOLNAME = dumptrace
+ LLVMLIBS = vmcore bcreader analysis.a transformutils.a support.a
+ 
+ TOOLLINKOPTS = $(PLATFORMLIBDL)
+ 
+ include $(LEVEL)/Makefile.common
+ 


Index: reopt/tools/dumptrace/dumptrace.cpp
diff -c /dev/null reopt/tools/dumptrace/dumptrace.cpp:1.1
*** /dev/null	Tue Jun 29 16:56:25 2004
--- reopt/tools/dumptrace/dumptrace.cpp	Tue Jun 29 16:56:14 2004
***************
*** 0 ****
--- 1,180 ----
+ //===- dumptrace.cpp - Dump the BasicBlocks of a trace --------------------===//
+ // 
+ //                     The LLVM Compiler Infrastructure
+ //
+ // This file was developed by the LLVM research group and is distributed under
+ // the University of Illinois Open Source License. See LICENSE.TXT for
+ // details.
+ // 
+ //===----------------------------------------------------------------------===//
+ //
+ // A simple program for testing TraceToFunction. This reads in a trace
+ // from a file, converts it to a function, and tries to verify the
+ // function.
+ //
+ //===----------------------------------------------------------------------===//
+ 
+ #include "llvm/Analysis/Trace.h"
+ #include "llvm/Analysis/Verifier.h"
+ #include "llvm/Assembly/Writer.h"
+ #include "llvm/Bytecode/Reader.h"
+ #include "reopt/TraceToFunction.h"
+ #include "llvm/Module.h"
+ #include "Support/CommandLine.h"
+ #include "Support/FileUtilities.h"
+ #include "Support/StringExtras.h"
+ #include <iostream>
+ #include <iterator>
+ #include <fstream>
+ #include <cerrno>
+ using namespace llvm;
+ 
+ namespace {
+   cl::opt<std::string> TraceFile("trace",
+     cl::desc("Name of file containing trace"),
+     cl::value_desc("traceFileName"));
+   cl::opt<std::string> BytecodeFile("bc",
+     cl::desc("Name of file containing module"),
+     cl::value_desc("bytecodeFileName"));
+ };
+ 
+ //----------------------------------------------------------------------------//
+ 
+ namespace {
+   cl::opt<bool> Quiet("q", cl::desc("Be quiet"), cl::init(false));
+ };
+ 
+ /// getBasicBlockByNum - Returns the basic block in F whose index is
+ /// bbNum.
+ ///
+ BasicBlock *getBasicBlockByNum (Function *F, int bbNum) {
+   Function::iterator block = F->begin();
+   std::advance (block, bbNum);
+   return block;
+ }
+ 
+ /// ReadTraceFromFile - Given a module, read a trace for some function
+ /// in that module from the named file. Returns the Trace object, or a null
+ /// pointer (and sets ErrorStr) if there was an error.
+ ///
+ Trace *ReadTraceFromFile (Module *M, const std::string &filename,
+                           std::string *ErrorStr) {
+   std::ifstream in (filename.c_str ());
+   if (!in.good()) {
+     if (ErrorStr)
+       *ErrorStr = std::string("Can't open '") + filename + "': "
+                 + strerror(errno);
+     return 0;
+   }
+   char functionName[160];
+   in.getline(functionName, 160, '\n');
+   Function *F = M->getNamedFunction (functionName);
+   if (!F) {
+     if (ErrorStr)
+       *ErrorStr = std::string("Function '") + functionName
+                 + "' not found in module";
+     return 0;
+   }
+   if (!Quiet)
+     std::cerr << "Function name: '" << functionName << "'\n"
+               << "Basic blocks: ";
+   std::vector<BasicBlock *> vBB;
+   do {
+     int basicBlockNum;
+     in >> basicBlockNum;
+     if (!in.eof ()) {
+       if (!Quiet) std::cerr << basicBlockNum << " ";
+       vBB.push_back (getBasicBlockByNum (F, basicBlockNum));
+     }
+   } while (!in.eof ());
+   if (!Quiet) std::cerr << "\n";
+   return new Trace (vBB);
+ }
+ 
+ /// getBasicBlockIndex - Returns the index of BB in its parent function.
+ ///
+ int getBasicBlockIndex (BasicBlock *BB) {
+   int bbNum = 0;
+   Function *F = BB->getParent ();
+   Function::iterator block = F->begin();
+   while (&*block != BB) {
+     ++block;
+     ++bbNum;
+   }
+   return bbNum;
+ }
+ 
+ //----------------------------------------------------------------------------//
+ 
+ int main (int argc, char **argv) {
+   // Get command line arguments.
+   cl::ParseCommandLineOptions(argc, argv, " trace basic block dumper\n");
+ 
+   // Read bytecode file.
+   std::string err;
+   Module *M = ParseBytecodeFile (BytecodeFile, &err);
+   if (!M) {
+     std::cerr << "Error reading bytecode: " << err << "\n";
+     exit (1);
+   }
+ 
+   // Read trace file.
+   std::vector<BasicBlock *> vBB;
+   Trace *T = ReadTraceFromFile (M, TraceFile, &err);
+   if (!T) {
+     std::cerr << "Error reading trace: " << err << "\n";
+     exit (1);
+   }
+ 
+   // Print the trace blocks.
+   std::cout << "Trace blocks:\n";
+   for (Trace::iterator i = T->begin(), e = T->end(); i != e; ++i)
+     std::cout << **i << "\n";
+   std::cout << "\n";
+ 
+   // Print the values used which are defined outside the trace.
+   std::cout << "Values used which are defined outside the trace:\n";
+   for (Trace::iterator i = T->begin(), e = T->end(); i != e; ++i) {
+     BasicBlock *BB = *i;
+     for (BasicBlock::iterator Inst = BB->begin(), BE = BB->end(); Inst != BE;
+          ++Inst) {
+       for (User::op_iterator Op = Inst->op_begin (), OE = Inst->op_end();
+            Op != OE; ++Op) {
+         Value *UsedValue = *Op;
+         if (Instruction *DefiningInst = dyn_cast<Instruction>(UsedValue)) {
+           if (!T->contains(DefiningInst->getParent())) {
+             std::cout << "Use on trace of ";
+             WriteAsOperand (std::cout, DefiningInst, true, true, M);
+             std::cout << ", which is defined off-trace in block %"
+               << DefiningInst->getParent()->getName() << ":\n" << *Inst;
+           }
+         }
+       }
+     }
+   }
+   std::cout << "\n";
+ 
+   // Print values defined inside the trace which are used outside the trace
+   std::cout << "Values defined inside the trace which are used outside the trace:\n";
+   for (Trace::iterator i = T->begin(), e = T->end(); i != e; ++i) {
+     BasicBlock *BB = *i;
+     for (BasicBlock::iterator Inst = BB->begin(), BE = BB->end(); Inst != BE;
+          ++Inst) {
+       for (Value::use_iterator UI = Inst->use_begin(), UE = Inst->use_end();
+            UI != UE; ++UI) {
+         User *UsingEntity = *UI;
+         if (Instruction *UsingInst = dyn_cast<Instruction>(UsingEntity)) {
+           if (!T->contains(UsingInst->getParent())) {
+             std::cout << "Trace defines ";
+             WriteAsOperand(std::cout, Inst, true, true, M);
+             std::cout << ", which is used off-trace in block %"
+               << UsingInst->getParent()->getName() << " by:\n" << *UsingInst;
+           }
+         }
+       }
+     }
+   }
+   std::cout << "\n";
+ 
+   return 0;
+ }





More information about the llvm-commits mailing list