1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This class represents a single trace of LLVM basic blocks.  A trace is a
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// single entry, multiple exit, region of code that is often hot.  Trace-based
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// optimizations treat traces almost like they are a large, strange, basic
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// block: because the trace path is assumed to be hot, optimizations for the
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// fall-through path are made at the expense of the non-fall-through paths.
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_ANALYSIS_TRACE_H
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_ANALYSIS_TRACE_H
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <vector>
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cassert>
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class BasicBlock;
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class Function;
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class Module;
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class raw_ostream;
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Trace {
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<BasicBlock *> BasicBlockListType;
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlockListType BasicBlocks;
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Trace ctor - Make a new trace from a vector of basic blocks,
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// residing in the function which is the parent of the first
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// basic block in the vector.
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getEntryBasicBlock - Return the entry basic block (first block)
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// of the trace.
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// operator[]/getBlock - Return basic block N in the trace.
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock *getBlock(unsigned i)   const { return BasicBlocks[i]; }
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getFunction - Return this trace's parent function.
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Function *getFunction () const;
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getModule - Return this Module that contains this trace's parent
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// function.
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module *getModule () const;
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getBlockIndex - Return the index of the specified basic block in the
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// trace, or -1 if it is not in the trace.
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int getBlockIndex(const BasicBlock *X) const {
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (BasicBlocks[i] == X)
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return i;
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return -1;
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// contains - Returns true if this trace contains the given basic
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// block.
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool contains(const BasicBlock *X) const {
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getBlockIndex(X) != -1;
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Returns true if B1 occurs before B2 in the trace, or if it is the same
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// block as B2..  Both blocks must be in the trace.
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return B1Idx <= B2Idx;
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // BasicBlock iterators...
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef BasicBlockListType::iterator iterator;
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef BasicBlockListType::const_iterator const_iterator;
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::reverse_iterator<iterator> reverse_iterator;
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator                begin()       { return BasicBlocks.begin(); }
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_iterator          begin() const { return BasicBlocks.begin(); }
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator                end  ()       { return BasicBlocks.end();   }
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_iterator          end  () const { return BasicBlocks.end();   }
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned                 size() const { return BasicBlocks.size(); }
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool                    empty() const { return BasicBlocks.empty(); }
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator erase(iterator q)               { return BasicBlocks.erase (q); }
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// print - Write trace to output stream.
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void print(raw_ostream &O) const;
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// dump - Debugger convenience method; writes trace to standard error
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// output stream.
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void dump() const;
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // end namespace llvm
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // TRACE_H
120