Trace.h revision 6ba8972919b79996e7b9d646ca005d81dbebd04a
1//===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class represents a single trace of LLVM basic blocks.  A trace is a
11// single entry, multiple exit, region of code that is often hot.  Trace-based
12// optimizations treat traces almost like they are a large, strange, basic
13// block: because the trace path is assumed to be hot, optimizations for the
14// fall-through path are made at the expense of the non-fall-through paths.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_ANALYSIS_TRACE_H
19#define LLVM_ANALYSIS_TRACE_H
20
21#include <iosfwd>
22#include <vector>
23
24namespace llvm {
25  class BasicBlock;
26  class Function;
27  class Module;
28
29class Trace {
30  typedef std::vector<BasicBlock *> BasicBlockListType;
31  BasicBlockListType BasicBlocks;
32
33public:
34  /// contains - Returns true if this trace contains the given basic
35  /// block.
36  ///
37  inline bool contains (const BasicBlock *X) {
38    for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
39      if (BasicBlocks[i] == X)
40        return true;
41    return false;
42  }
43
44  /// Trace ctor - Make a new trace from a vector of basic blocks,
45  /// residing in the function which is the parent of the first
46  /// basic block in the vector.
47  ///
48  Trace (const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {
49  }
50
51  /// getEntryBasicBlock - Return the entry basic block (first block)
52  /// of the trace.
53  ///
54  BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
55
56  /// getFunction - Return this trace's parent function.
57  ///
58  Function *getFunction () const;
59
60  /// getModule - Return this Module that contains this trace's parent
61  /// function.
62  ///
63  Module *getModule () const;
64
65  /// print - Write trace to output stream.
66  ///
67  void print (std::ostream &O) const;
68
69  /// dump - Debugger convenience method; writes trace to standard error
70  /// output stream.
71  ///
72  void dump () const;
73
74  // BasicBlock iterators...
75  typedef BasicBlockListType::iterator iterator;
76  typedef BasicBlockListType::const_iterator const_iterator;
77  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
78  typedef std::reverse_iterator<iterator> reverse_iterator;
79
80  iterator                begin()       { return BasicBlocks.begin(); }
81  const_iterator          begin() const { return BasicBlocks.begin(); }
82  iterator                end  ()       { return BasicBlocks.end();   }
83  const_iterator          end  () const { return BasicBlocks.end();   }
84
85  reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
86  const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
87  reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
88  const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
89
90  unsigned                 size() const { return BasicBlocks.size(); }
91  bool                    empty() const { return BasicBlocks.empty(); }
92
93  BasicBlock *operator[] (unsigned i) const { return BasicBlocks[i]; }
94  BasicBlock *getBlock (unsigned i)   const { return BasicBlocks[i]; }
95
96  /// Returns true if B1 and B2 appear on a path from START to an exit
97  /// block => B1 appears before B2. If START is not provided, defaults
98  /// to 0, which means use getEntryBasicBlock().
99  ///
100  bool dominates (const BasicBlock *B1, const BasicBlock *B2,
101		  const BasicBlock *start = 0);
102};
103
104} // end namespace llvm
105
106#endif // TRACE_H
107