Trace.h revision 3f25328fbff583894772e45bb088e995b371190f
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file was developed by the LLVM research group and is distributed under
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the University of Illinois Open Source License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class represents a single trace of LLVM basic blocks.  A trace is a
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// single entry, multiple exit, region of code that is often hot.  Trace-based
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// optimizations treat traces almost like they are a large, strange, basic
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// block: because the trace path is assumed to be hot, optimizations for the
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// fall-through path are made at the expense of the non-fall-through paths.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef LLVM_ANALYSIS_TRACE_H
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_ANALYSIS_TRACE_H
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <iosfwd>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <cassert>
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class BasicBlock;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Function;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Module;
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Trace {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<BasicBlock *> BasicBlockListType;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BasicBlockListType BasicBlocks;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Trace ctor - Make a new trace from a vector of basic blocks,
36  /// residing in the function which is the parent of the first
37  /// basic block in the vector.
38  ///
39  Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
40
41  /// getEntryBasicBlock - Return the entry basic block (first block)
42  /// of the trace.
43  ///
44  BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
45
46  /// operator[]/getBlock - Return basic block N in the trace.
47  ///
48  BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
49  BasicBlock *getBlock(unsigned i)   const { return BasicBlocks[i]; }
50
51  /// getFunction - Return this trace's parent function.
52  ///
53  Function *getFunction () const;
54
55  /// getModule - Return this Module that contains this trace's parent
56  /// function.
57  ///
58  Module *getModule () const;
59
60  /// getBlockIndex - Return the index of the specified basic block in the
61  /// trace, or -1 if it is not in the trace.
62  int getBlockIndex(const BasicBlock *X) const {
63    for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
64      if (BasicBlocks[i] == X)
65        return i;
66    return -1;
67  }
68
69  /// contains - Returns true if this trace contains the given basic
70  /// block.
71  ///
72  bool contains(const BasicBlock *X) const {
73    return getBlockIndex(X) != -1;
74  }
75
76  /// Returns true if B1 occurs before B2 in the trace, or if it is the same
77  /// block as B2..  Both blocks must be in the trace.
78  ///
79  bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
80    int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
81    assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
82    return B1Idx <= B2Idx;
83  }
84
85  // BasicBlock iterators...
86  typedef BasicBlockListType::iterator iterator;
87  typedef BasicBlockListType::const_iterator const_iterator;
88  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
89  typedef std::reverse_iterator<iterator> reverse_iterator;
90
91  iterator                begin()       { return BasicBlocks.begin(); }
92  const_iterator          begin() const { return BasicBlocks.begin(); }
93  iterator                end  ()       { return BasicBlocks.end();   }
94  const_iterator          end  () const { return BasicBlocks.end();   }
95
96  reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
97  const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
98  reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
99  const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
100
101  unsigned                 size() const { return BasicBlocks.size(); }
102  bool                    empty() const { return BasicBlocks.empty(); }
103
104  /// print - Write trace to output stream.
105  ///
106  void print (std::ostream &O) const;
107
108  /// dump - Debugger convenience method; writes trace to standard error
109  /// output stream.
110  ///
111  void dump () const;
112};
113
114} // end namespace llvm
115
116#endif // TRACE_H
117