1 //===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // 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 <vector> 22 #include <cassert> 23 24 namespace llvm { 25 class BasicBlock; 26 class Function; 27 class Module; 28 class raw_ostream; 29 30 class Trace { 31 typedef std::vector<BasicBlock *> BasicBlockListType; 32 BasicBlockListType BasicBlocks; 33 34 public: 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. 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 iterator erase(iterator q) { return BasicBlocks.erase (q); } 105 iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); } 106 107 /// print - Write trace to output stream. 108 /// 109 void print(raw_ostream &O) const; 110 111 /// dump - Debugger convenience method; writes trace to standard error 112 /// output stream. 113 /// 114 void dump() const; 115 }; 116 117 } // end namespace llvm 118 119 #endif // TRACE_H 120