[llvm-commits] CVS: reopt/lib/TraceIO/Makefile TraceReader.cpp TraceWriter.cpp

Brian Gaeke gaeke at cs.uiuc.edu
Wed Jun 30 01:34:01 PDT 2004


Changes in directory reopt/lib/TraceIO:

Makefile added (r1.1)
TraceReader.cpp added (r1.1)
TraceWriter.cpp added (r1.1)

---
Log message:

The trace writer (from the LightWtProfiling dir) and the trace reader (from
ttftest, merged with the one from dumptrace) have been moved to this new
Trace I/O library.


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

Index: reopt/lib/TraceIO/Makefile
diff -c /dev/null reopt/lib/TraceIO/Makefile:1.1
*** /dev/null	Wed Jun 30 01:33:29 2004
--- reopt/lib/TraceIO/Makefile	Wed Jun 30 01:33:18 2004
***************
*** 0 ****
--- 1,4 ----
+ LEVEL = ../..
+ LIBRARYNAME = traceio
+ 
+ include $(LEVEL)/Makefile.common


Index: reopt/lib/TraceIO/TraceReader.cpp
diff -c /dev/null reopt/lib/TraceIO/TraceReader.cpp:1.1
*** /dev/null	Wed Jun 30 01:33:29 2004
--- reopt/lib/TraceIO/TraceReader.cpp	Wed Jun 30 01:33:18 2004
***************
*** 0 ****
--- 1,58 ----
+ 
+ #include "Support/CommandLine.h"
+ #include "Support/Debug.h"
+ #include "llvm/Analysis/Trace.h"
+ #include "llvm/Module.h"
+ #include <iterator>
+ #include <fstream>
+ using namespace llvm;
+ 
+ namespace llvm {
+ 
+ /// 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;
+   }
+   DEBUG (std::cerr << "Reading trace for function: '" << functionName << "'\n"
+                    << "Basic blocks: ");
+   std::vector<BasicBlock *> vBB;
+   do {
+     int basicBlockNum;
+     in >> basicBlockNum;
+     if (!in.eof ()) {
+       DEBUG (std::cerr << basicBlockNum << " ");
+       vBB.push_back (getBasicBlockByNum (F, basicBlockNum));
+     }
+   } while (!in.eof ());
+   DEBUG (std::cerr << "\n");
+   return new Trace (vBB);
+ }
+ 
+ }; // end namespace llvm


Index: reopt/lib/TraceIO/TraceWriter.cpp
diff -c /dev/null reopt/lib/TraceIO/TraceWriter.cpp:1.1
*** /dev/null	Wed Jun 30 01:33:29 2004
--- reopt/lib/TraceIO/TraceWriter.cpp	Wed Jun 30 01:33:18 2004
***************
*** 0 ****
--- 1,66 ----
+ //===-- LightWtProfiling/TraceWriter.cpp -------------------------*- C++ -*--=//
+ // 
+ //                     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.
+ // 
+ //===----------------------------------------------------------------------===//
+ //
+ // This file exports a method which can be used to save a trace to
+ // disk, along with the module it refers to.
+ //
+ //===----------------------------------------------------------------------===//
+ 
+ #include "llvm/Analysis/Trace.h"
+ #include "llvm/Bytecode/Writer.h"
+ #include "llvm/Function.h"
+ #include "Support/FileUtilities.h"
+ #include "Support/StringExtras.h"
+ #include <fstream>
+ using namespace llvm;
+ 
+ namespace llvm {
+ 
+ /// getBasicBlockIndex - Returns the index of BB in its parent function.
+ ///
+ static inline int getBasicBlockIndex (BasicBlock *BB) {
+   int bbNum = 0;
+   Function *F = BB->getParent ();
+   Function::iterator block = F->begin();
+   while (&*block != BB) {
+     ++block;              
+     ++bbNum;
+   }
+   return bbNum;
+ }     
+ 
+ void WriteTraceToFile (Trace *Tr) {
+   assert (Tr);
+ 
+   // Get unique filename for trace & corresponding module.
+   std::string traceFileName, bytecodeFileName;
+   Function *F = Tr->getFunction ();
+   unsigned count = 0;
+   do {
+     ++count;
+     traceFileName = F->getName () + ".trace" + utostr (count) + ".txt";
+     bytecodeFileName = F->getName () + ".trace" + utostr (count) + ".bc";
+   } while (FileOpenable(traceFileName) || FileOpenable(bytecodeFileName));
+ 
+   // Write out trace.
+   std::ofstream out (traceFileName.c_str ());
+   out << F->getName () << "\n";
+   for (Trace::iterator i = Tr->begin (), e = Tr->end (); i != e; ++i)
+     out << getBasicBlockIndex (*i) << " ";
+   out << "\n";
+   out.close ();
+ 
+   // Write out bytecode. FIXME: Should extract function F (as w/
+   // 'extract' tool) and write out only F, not its entire module.
+   std::ofstream bout (bytecodeFileName.c_str ());
+   WriteBytecodeToFile(F->getParent (), bout);
+   bout.close ();
+ }
+ 
+ } // end namespace llvm





More information about the llvm-commits mailing list