LLVM 23.0.0git
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
21#include <cassert>
22#include <vector>
23
24namespace llvm {
25
26class BasicBlock;
27class Function;
28class Module;
29class raw_ostream;
30
31class Trace {
32 using BasicBlockListType = std::vector<BasicBlock *>;
33
34 BasicBlockListType BasicBlocks;
35
36public:
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) {}
41
42 /// getEntryBasicBlock - Return the entry basic block (first block)
43 /// of the trace.
44 BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
45
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]; }
49
50 /// getFunction - Return this trace's parent function.
52
53 /// getModule - Return this Module that contains this trace's parent
54 /// function.
55 LLVM_ABI Module *getModule() const;
56
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)
62 return i;
63 return -1;
64 }
65
66 /// contains - Returns true if this trace contains the given basic
67 /// block.
68 bool contains(const BasicBlock *X) const {
69 return getBlockIndex(X) != -1;
70 }
71
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;
78 }
79
80 // BasicBlock iterators...
83 using reverse_iterator = std::reverse_iterator<iterator>;
84 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
85
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(); }
90
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(); }
95
96 unsigned size() const { return BasicBlocks.size(); }
97 bool empty() const { return BasicBlocks.empty(); }
98
99 iterator erase(iterator q) { return BasicBlocks.erase (q); }
100 iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
101
102 /// print - Write trace to output stream.
103 LLVM_ABI void print(raw_ostream &O) const;
104
105 /// dump - Debugger convenience method; writes trace to standard error
106 /// output stream.
107 LLVM_ABI void dump() const;
108};
109
110} // end namespace llvm
111
112#endif // LLVM_ANALYSIS_TRACE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
#define LLVM_ABI
Definition Compiler.h:213
LLVM Basic Block Representation.
Definition BasicBlock.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
std::reverse_iterator< iterator > reverse_iterator
Definition Trace.h:83
BasicBlock * operator[](unsigned i) const
operator[]/getBlock - Return basic block N in the trace.
Definition Trace.h:47
LLVM_ABI void dump() const
dump - Debugger convenience method; writes trace to standard error output stream.
Definition Trace.cpp:50
bool empty() const
Definition Trace.h:97
BasicBlockListType::const_iterator const_iterator
Definition Trace.h:82
iterator begin()
Definition Trace.h:86
reverse_iterator rend()
Definition Trace.h:93
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:74
iterator erase(iterator q1, iterator q2)
Definition Trace.h:100
const_iterator end() const
Definition Trace.h:89
const_iterator begin() const
Definition Trace.h:87
const_reverse_iterator rbegin() const
Definition Trace.h:92
unsigned size() const
Definition Trace.h:96
const_reverse_iterator rend() const
Definition Trace.h:94
BasicBlock * getBlock(unsigned i) const
Definition Trace.h:48
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Trace.h:84
BasicBlock * getEntryBasicBlock() const
getEntryBasicBlock - Return the entry basic block (first block) of the trace.
Definition Trace.h:44
LLVM_ABI Function * getFunction() const
getFunction - Return this trace's parent function.
Definition Trace.cpp:27
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:59
reverse_iterator rbegin()
Definition Trace.h:91
bool contains(const BasicBlock *X) const
contains - Returns true if this trace contains the given basic block.
Definition Trace.h:68
iterator end()
Definition Trace.h:88
iterator erase(iterator q)
Definition Trace.h:99
LLVM_ABI void print(raw_ostream &O) const
print - Write trace to output stream.
Definition Trace.cpp:36
BasicBlockListType::iterator iterator
Definition Trace.h:81
LLVM_ABI Module * getModule() const
getModule - Return this Module that contains this trace's parent function.
Definition Trace.cpp:31
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:40
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.