16ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//===- Trace.cpp - Implementation of Trace class --------------------------===//
26ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//
36ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//                     The LLVM Compiler Infrastructure
46ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
76ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//
86ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//===----------------------------------------------------------------------===//
96ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//
106ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner// This class represents a single trace of LLVM basic blocks.  A trace is a
116ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner// single entry, multiple exit, region of code that is often hot.  Trace-based
126ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner// optimizations treat traces almost like they are a large, strange, basic
136ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner// block: because the trace path is assumed to be hot, optimizations for the
146ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner// fall-through path are made at the expense of the non-fall-through paths.
156ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//
166ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner//===----------------------------------------------------------------------===//
176ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner
186ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner#include "llvm/Analysis/Trace.h"
196ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner#include "llvm/Function.h"
206ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner#include "llvm/Assembly/Writer.h"
21c48e3d1870dbb8f5fdecc1615fb80d62a1c3d943David Greene#include "llvm/Support/Debug.h"
22bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner#include "llvm/Support/raw_ostream.h"
236ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattnerusing namespace llvm;
246ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner
256ba8972919b79996e7b9d646ca005d81dbebd04aChris LattnerFunction *Trace::getFunction() const {
266ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner  return getEntryBasicBlock()->getParent();
276ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner}
286ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner
296ba8972919b79996e7b9d646ca005d81dbebd04aChris LattnerModule *Trace::getModule() const {
306ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner  return getFunction()->getParent();
316ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner}
326ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner
336ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner/// print - Write trace to output stream.
346ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner///
35bdff548e4dd577a72094d57b282de4e765643b96Chris Lattnervoid Trace::print(raw_ostream &O) const {
36bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner  Function *F = getFunction();
37a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer  O << "; Trace from function " << F->getName() << ", blocks:\n";
386f81b510217bd87f265cca054c5d9885250d8525Bill Wendling  for (const_iterator i = begin(), e = end(); i != e; ++i) {
396ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner    O << "; ";
405c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling    WriteAsOperand(O, *i, true, getModule());
416ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner    O << "\n";
426ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner  }
436ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner  O << "; Trace parent function: \n" << *F;
446ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner}
456ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner
466ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner/// dump - Debugger convenience method; writes trace to standard error
476ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner/// output stream.
486ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner///
496f81b510217bd87f265cca054c5d9885250d8525Bill Wendlingvoid Trace::dump() const {
50c48e3d1870dbb8f5fdecc1615fb80d62a1c3d943David Greene  print(dbgs());
516ba8972919b79996e7b9d646ca005d81dbebd04aChris Lattner}
52