LLVM API Documentation

Trace.h
Go to the documentation of this file.
00001 //===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class represents a single trace of LLVM basic blocks.  A trace is a
00011 // single entry, multiple exit, region of code that is often hot.  Trace-based
00012 // optimizations treat traces almost like they are a large, strange, basic
00013 // block: because the trace path is assumed to be hot, optimizations for the
00014 // fall-through path are made at the expense of the non-fall-through paths.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_ANALYSIS_TRACE_H
00019 #define LLVM_ANALYSIS_TRACE_H
00020 
00021 #include <cassert>
00022 #include <vector>
00023 
00024 namespace llvm {
00025   class BasicBlock;
00026   class Function;
00027   class Module;
00028   class raw_ostream;
00029 
00030 class Trace {
00031   typedef std::vector<BasicBlock *> BasicBlockListType;
00032   BasicBlockListType BasicBlocks;
00033 
00034 public:
00035   /// Trace ctor - Make a new trace from a vector of basic blocks,
00036   /// residing in the function which is the parent of the first
00037   /// basic block in the vector.
00038   ///
00039   Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
00040 
00041   /// getEntryBasicBlock - Return the entry basic block (first block)
00042   /// of the trace.
00043   ///
00044   BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
00045 
00046   /// operator[]/getBlock - Return basic block N in the trace.
00047   ///
00048   BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
00049   BasicBlock *getBlock(unsigned i)   const { return BasicBlocks[i]; }
00050 
00051   /// getFunction - Return this trace's parent function.
00052   ///
00053   Function *getFunction () const;
00054 
00055   /// getModule - Return this Module that contains this trace's parent
00056   /// function.
00057   ///
00058   Module *getModule () const;
00059 
00060   /// getBlockIndex - Return the index of the specified basic block in the
00061   /// trace, or -1 if it is not in the trace.
00062   int getBlockIndex(const BasicBlock *X) const {
00063     for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
00064       if (BasicBlocks[i] == X)
00065         return i;
00066     return -1;
00067   }
00068 
00069   /// contains - Returns true if this trace contains the given basic
00070   /// block.
00071   ///
00072   bool contains(const BasicBlock *X) const {
00073     return getBlockIndex(X) != -1;
00074   }
00075 
00076   /// Returns true if B1 occurs before B2 in the trace, or if it is the same
00077   /// block as B2..  Both blocks must be in the trace.
00078   ///
00079   bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
00080     int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
00081     assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
00082     return B1Idx <= B2Idx;
00083   }
00084 
00085   // BasicBlock iterators...
00086   typedef BasicBlockListType::iterator iterator;
00087   typedef BasicBlockListType::const_iterator const_iterator;
00088   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00089   typedef std::reverse_iterator<iterator> reverse_iterator;
00090 
00091   iterator                begin()       { return BasicBlocks.begin(); }
00092   const_iterator          begin() const { return BasicBlocks.begin(); }
00093   iterator                end  ()       { return BasicBlocks.end();   }
00094   const_iterator          end  () const { return BasicBlocks.end();   }
00095 
00096   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
00097   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
00098   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
00099   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
00100 
00101   unsigned                 size() const { return BasicBlocks.size(); }
00102   bool                    empty() const { return BasicBlocks.empty(); }
00103 
00104   iterator erase(iterator q)               { return BasicBlocks.erase (q); }
00105   iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
00106 
00107   /// print - Write trace to output stream.
00108   ///
00109   void print(raw_ostream &O) const;
00110 
00111   /// dump - Debugger convenience method; writes trace to standard error
00112   /// output stream.
00113   ///
00114   void dump() const;
00115 };
00116 
00117 } // end namespace llvm
00118 
00119 #endif // LLVM_ANALYSIS_TRACE_H