1 //===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ANALYSIS_TRACE_H
19 #define LLVM_ANALYSIS_TRACE_H
31 typedef std::vector<BasicBlock *> BasicBlockListType;
32 BasicBlockListType BasicBlocks;
35 /// 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.
39 Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
41 /// getEntryBasicBlock - Return the entry basic block (first block)
44 BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
46 /// operator[]/getBlock - Return basic block N in the trace.
48 BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
49 BasicBlock *getBlock(unsigned i) const { return BasicBlocks[i]; }
51 /// getFunction - Return this trace's parent function.
53 Function *getFunction () const;
55 /// getModule - Return this Module that contains this trace's parent
58 Module *getModule () const;
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)
69 /// contains - Returns true if this trace contains the given basic
72 bool contains(const BasicBlock *X) const {
73 return getBlockIndex(X) != -1;
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.
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;
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;
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(); }
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(); }
101 unsigned size() const { return BasicBlocks.size(); }
102 bool empty() const { return BasicBlocks.empty(); }
104 iterator erase(iterator q) { return BasicBlocks.erase (q); }
105 iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
107 /// print - Write trace to output stream.
109 void print(raw_ostream &O) const;
111 /// dump - Debugger convenience method; writes trace to standard error
117 } // end namespace llvm
119 #endif // LLVM_ANALYSIS_TRACE_H