1193323Sed//===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This class represents a single trace of LLVM basic blocks.  A trace is a
11193323Sed// single entry, multiple exit, region of code that is often hot.  Trace-based
12193323Sed// optimizations treat traces almost like they are a large, strange, basic
13193323Sed// block: because the trace path is assumed to be hot, optimizations for the
14193323Sed// fall-through path are made at the expense of the non-fall-through paths.
15193323Sed//
16193323Sed//===----------------------------------------------------------------------===//
17193323Sed
18193323Sed#ifndef LLVM_ANALYSIS_TRACE_H
19193323Sed#define LLVM_ANALYSIS_TRACE_H
20193323Sed
21252723Sdim#include <cassert>
22193323Sed#include <vector>
23193323Sed
24193323Sednamespace llvm {
25193323Sed  class BasicBlock;
26193323Sed  class Function;
27193323Sed  class Module;
28198090Srdivacky  class raw_ostream;
29193323Sed
30193323Sedclass Trace {
31193323Sed  typedef std::vector<BasicBlock *> BasicBlockListType;
32193323Sed  BasicBlockListType BasicBlocks;
33193323Sed
34193323Sedpublic:
35193323Sed  /// Trace ctor - Make a new trace from a vector of basic blocks,
36193323Sed  /// residing in the function which is the parent of the first
37193323Sed  /// basic block in the vector.
38193323Sed  ///
39193323Sed  Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
40193323Sed
41193323Sed  /// getEntryBasicBlock - Return the entry basic block (first block)
42193323Sed  /// of the trace.
43193323Sed  ///
44193323Sed  BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
45193323Sed
46193323Sed  /// operator[]/getBlock - Return basic block N in the trace.
47193323Sed  ///
48193323Sed  BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
49193323Sed  BasicBlock *getBlock(unsigned i)   const { return BasicBlocks[i]; }
50193323Sed
51193323Sed  /// getFunction - Return this trace's parent function.
52193323Sed  ///
53193323Sed  Function *getFunction () const;
54193323Sed
55193323Sed  /// getModule - Return this Module that contains this trace's parent
56193323Sed  /// function.
57193323Sed  ///
58193323Sed  Module *getModule () const;
59193323Sed
60193323Sed  /// getBlockIndex - Return the index of the specified basic block in the
61193323Sed  /// trace, or -1 if it is not in the trace.
62193323Sed  int getBlockIndex(const BasicBlock *X) const {
63193323Sed    for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
64193323Sed      if (BasicBlocks[i] == X)
65193323Sed        return i;
66193323Sed    return -1;
67193323Sed  }
68193323Sed
69193323Sed  /// contains - Returns true if this trace contains the given basic
70193323Sed  /// block.
71193323Sed  ///
72193323Sed  bool contains(const BasicBlock *X) const {
73193323Sed    return getBlockIndex(X) != -1;
74193323Sed  }
75193323Sed
76193323Sed  /// Returns true if B1 occurs before B2 in the trace, or if it is the same
77193323Sed  /// block as B2..  Both blocks must be in the trace.
78193323Sed  ///
79193323Sed  bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
80193323Sed    int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
81193323Sed    assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
82193323Sed    return B1Idx <= B2Idx;
83193323Sed  }
84193323Sed
85193323Sed  // BasicBlock iterators...
86193323Sed  typedef BasicBlockListType::iterator iterator;
87193323Sed  typedef BasicBlockListType::const_iterator const_iterator;
88193323Sed  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
89193323Sed  typedef std::reverse_iterator<iterator> reverse_iterator;
90193323Sed
91193323Sed  iterator                begin()       { return BasicBlocks.begin(); }
92193323Sed  const_iterator          begin() const { return BasicBlocks.begin(); }
93193323Sed  iterator                end  ()       { return BasicBlocks.end();   }
94193323Sed  const_iterator          end  () const { return BasicBlocks.end();   }
95193323Sed
96193323Sed  reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
97193323Sed  const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
98193323Sed  reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
99193323Sed  const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
100193323Sed
101193323Sed  unsigned                 size() const { return BasicBlocks.size(); }
102193323Sed  bool                    empty() const { return BasicBlocks.empty(); }
103193323Sed
104193323Sed  iterator erase(iterator q)               { return BasicBlocks.erase (q); }
105193323Sed  iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
106193323Sed
107193323Sed  /// print - Write trace to output stream.
108193323Sed  ///
109198090Srdivacky  void print(raw_ostream &O) const;
110193323Sed
111193323Sed  /// dump - Debugger convenience method; writes trace to standard error
112193323Sed  /// output stream.
113193323Sed  ///
114198090Srdivacky  void dump() const;
115193323Sed};
116193323Sed
117193323Sed} // end namespace llvm
118193323Sed
119252723Sdim#endif // LLVM_ANALYSIS_TRACE_H
120