LLVM  10.0.0svn
Trace.h
Go to the documentation of this file.
1 //===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class represents a single trace of LLVM basic blocks. A trace is a
10 // single entry, multiple exit, region of code that is often hot. Trace-based
11 // optimizations treat traces almost like they are a large, strange, basic
12 // block: because the trace path is assumed to be hot, optimizations for the
13 // fall-through path are made at the expense of the non-fall-through paths.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_ANALYSIS_TRACE_H
18 #define LLVM_ANALYSIS_TRACE_H
19 
20 #include <cassert>
21 #include <vector>
22 
23 namespace llvm {
24 
25 class BasicBlock;
26 class Function;
27 class Module;
28 class raw_ostream;
29 
30 class Trace {
31  using BasicBlockListType = std::vector<BasicBlock *>;
32 
33  BasicBlockListType BasicBlocks;
34 
35 public:
36  /// Trace ctor - Make a new trace from a vector of basic blocks,
37  /// residing in the function which is the parent of the first
38  /// basic block in the vector.
39  Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
40 
41  /// getEntryBasicBlock - Return the entry basic block (first block)
42  /// of the trace.
43  BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
44 
45  /// operator[]/getBlock - Return basic block N in the trace.
46  BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
47  BasicBlock *getBlock(unsigned i) const { return BasicBlocks[i]; }
48 
49  /// getFunction - Return this trace's parent function.
50  Function *getFunction () const;
51 
52  /// getModule - Return this Module that contains this trace's parent
53  /// function.
54  Module *getModule () const;
55 
56  /// getBlockIndex - Return the index of the specified basic block in the
57  /// trace, or -1 if it is not in the trace.
58  int getBlockIndex(const BasicBlock *X) const {
59  for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
60  if (BasicBlocks[i] == X)
61  return i;
62  return -1;
63  }
64 
65  /// contains - Returns true if this trace contains the given basic
66  /// block.
67  bool contains(const BasicBlock *X) const {
68  return getBlockIndex(X) != -1;
69  }
70 
71  /// Returns true if B1 occurs before B2 in the trace, or if it is the same
72  /// block as B2.. Both blocks must be in the trace.
73  bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
74  int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
75  assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
76  return B1Idx <= B2Idx;
77  }
78 
79  // BasicBlock iterators...
82  using reverse_iterator = std::reverse_iterator<iterator>;
83  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
84 
85  iterator begin() { return BasicBlocks.begin(); }
86  const_iterator begin() const { return BasicBlocks.begin(); }
87  iterator end () { return BasicBlocks.end(); }
88  const_iterator end () const { return BasicBlocks.end(); }
89 
90  reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
91  const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
92  reverse_iterator rend () { return BasicBlocks.rend(); }
93  const_reverse_iterator rend () const { return BasicBlocks.rend(); }
94 
95  unsigned size() const { return BasicBlocks.size(); }
96  bool empty() const { return BasicBlocks.empty(); }
97 
98  iterator erase(iterator q) { return BasicBlocks.erase (q); }
99  iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
100 
101  /// print - Write trace to output stream.
102  void print(raw_ostream &O) const;
103 
104  /// dump - Debugger convenience method; writes trace to standard error
105  /// output stream.
106  void dump() const;
107 };
108 
109 } // end namespace llvm
110 
111 #endif // LLVM_ANALYSIS_TRACE_H
BasicBlock * getEntryBasicBlock() const
getEntryBasicBlock - Return the entry basic block (first block) of the trace.
Definition: Trace.h:43
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:320
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
iterator erase(iterator q)
Definition: Trace.h:98
int getBlockIndex(const BasicBlock *X) const
getBlockIndex - Return the index of the specified basic block in the trace, or -1 if it is not in the...
Definition: Trace.h:58
BasicBlockListType::iterator iterator
Definition: Trace.h:80
iterator begin()
Definition: Trace.h:85
bool contains(const BasicBlock *X) const
contains - Returns true if this trace contains the given basic block.
Definition: Trace.h:67
iterator erase(iterator q1, iterator q2)
Definition: Trace.h:99
bool empty() const
Definition: Trace.h:96
Function * getFunction() const
getFunction - Return this trace&#39;s parent function.
Definition: Trace.cpp:27
unsigned size() const
Definition: Trace.h:95
iterator end()
Definition: Trace.h:87
void dump() const
dump - Debugger convenience method; writes trace to standard error output stream. ...
Definition: Trace.cpp:50
const_reverse_iterator rend() const
Definition: Trace.h:93
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
const_iterator begin() const
Definition: Trace.h:86
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Trace.h:83
bool dominates(const BasicBlock *B1, const BasicBlock *B2) const
Returns true if B1 occurs before B2 in the trace, or if it is the same block as B2.
Definition: Trace.h:73
std::reverse_iterator< iterator > reverse_iterator
Definition: Trace.h:82
reverse_iterator rbegin()
Definition: Trace.h:90
reverse_iterator rend()
Definition: Trace.h:92
const_reverse_iterator rbegin() const
Definition: Trace.h:91
typename SuperClass::iterator iterator
Definition: SmallVector.h:319
Trace(const std::vector< BasicBlock *> &vBB)
Trace ctor - Make a new trace from a vector of basic blocks, residing in the function which is the pa...
Definition: Trace.h:39
Module * getModule() const
getModule - Return this Module that contains this trace&#39;s parent function.
Definition: Trace.cpp:31
void print(raw_ostream &O) const
print - Write trace to output stream.
Definition: Trace.cpp:36
const_iterator end() const
Definition: Trace.h:88
BasicBlock * getBlock(unsigned i) const
Definition: Trace.h:47
BasicBlockListType::const_iterator const_iterator
Definition: Trace.h:81
BasicBlock * operator[](unsigned i) const
operator[]/getBlock - Return basic block N in the trace.
Definition: Trace.h:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45