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
32 using BasicBlockListType = std::vector<BasicBlock *>;
34 BasicBlockListType BasicBlocks;
37 /// Trace ctor - Make a new trace from a vector of basic blocks,
38 /// residing in the function which is the parent of the first
39 /// basic block in the vector.
40 Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
42 /// 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.
47 BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
48 BasicBlock *getBlock(unsigned i) const { return BasicBlocks[i]; }
50 /// getFunction - Return this trace's parent function.
51 Function *getFunction () const;
53 /// getModule - Return this Module that contains this trace's parent
55 Module *getModule () const;
57 /// getBlockIndex - Return the index of the specified basic block in the
58 /// trace, or -1 if it is not in the trace.
59 int getBlockIndex(const BasicBlock *X) const {
60 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
61 if (BasicBlocks[i] == X)
66 /// contains - Returns true if this trace contains the given basic
68 bool contains(const BasicBlock *X) const {
69 return getBlockIndex(X) != -1;
72 /// Returns true if B1 occurs before B2 in the trace, or if it is the same
73 /// block as B2.. Both blocks must be in the trace.
74 bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
75 int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
76 assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
77 return B1Idx <= B2Idx;
80 // BasicBlock iterators...
81 using iterator = BasicBlockListType::iterator;
82 using const_iterator = BasicBlockListType::const_iterator;
83 using reverse_iterator = std::reverse_iterator<iterator>;
84 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
86 iterator begin() { return BasicBlocks.begin(); }
87 const_iterator begin() const { return BasicBlocks.begin(); }
88 iterator end () { return BasicBlocks.end(); }
89 const_iterator end () const { return BasicBlocks.end(); }
91 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
92 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
93 reverse_iterator rend () { return BasicBlocks.rend(); }
94 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
96 unsigned size() const { return BasicBlocks.size(); }
97 bool empty() const { return BasicBlocks.empty(); }
99 iterator erase(iterator q) { return BasicBlocks.erase (q); }
100 iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
102 /// print - Write trace to output stream.
103 void print(raw_ostream &O) const;
105 /// dump - Debugger convenience method; writes trace to standard error
110 } // end namespace llvm
112 #endif // LLVM_ANALYSIS_TRACE_H