MLIR 24.0.0git
Liveness.h
Go to the documentation of this file.
1//===- Liveness.h - Liveness analysis for MLIR ------------------*- 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 file contains an analysis for computing liveness information from a
10// given top-level operation. The current version of the analysis uses a
11// traditional algorithm to resolve detailed live-range information about all
12// values within the specified regions. It is also possible to query liveness
13// information on block level.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef MLIR_ANALYSIS_LIVENESS_H
18#define MLIR_ANALYSIS_LIVENESS_H
19
20#include <vector>
21
22#include "mlir/Support/LLVM.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/SmallPtrSet.h"
26
27namespace mlir {
28
29class Block;
31class Operation;
32class Region;
33class Value;
34
35/// Represents an analysis for computing liveness information from a
36/// given top-level operation. The analysis iterates over all associated
37/// regions that are attached to the given top-level operation. It
38/// computes liveness information for every value and block that are
39/// included in the mentioned regions. It relies on a fixpoint iteration
40/// to compute all live-in and live-out values of all included blocks.
41/// Sample usage:
42/// Liveness liveness(topLevelOp);
43/// auto &allInValues = liveness.getLiveIn(block);
44/// auto &allOutValues = liveness.getLiveOut(block);
45/// auto allOperationsInWhichValueIsLive = liveness.resolveLiveness(value);
46/// bool isDeafAfter = liveness.isDeadAfter(value, operation);
47class Liveness {
48public:
49 using OperationListT = std::vector<Operation *>;
52
53public:
54 /// Creates a new Liveness analysis that computes liveness
55 /// information for all associated regions.
57
58 /// Returns the operation this analysis was constructed from.
59 Operation *getOperation() const { return operation; }
60
61 /// Gets liveness info (if any) for the given value.
62 /// This includes all operations in which the given value is live.
63 /// Note that the operations in this list are not ordered and the current
64 /// implementation is computationally expensive (as it iterates over all
65 /// blocks in which the given value is live).
67
68 /// Gets liveness info (if any) for the block.
69 const LivenessBlockInfo *getLiveness(Block *block) const;
70
71 /// Returns a reference to a set containing live-in values (unordered).
72 const ValueSetT &getLiveIn(Block *block) const;
73
74 /// Returns a reference to a set containing live-out values (unordered).
75 const ValueSetT &getLiveOut(Block *block) const;
76
77 /// Returns true if `value` is not live after `operation`.
78 bool isDeadAfter(Value value, Operation *operation) const;
79
80 /// Dumps the liveness information in a human readable format.
81#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
82 LLVM_DUMP_METHOD void dump() const;
83#endif
84
85 /// Dumps the liveness information to the given stream.
86 void print(raw_ostream &os) const;
87
88private:
89 /// Initializes the internal mappings.
90 void build();
91
92private:
93 /// The operation this analysis was constructed from.
94 Operation *operation;
95
96 /// Maps blocks to internal liveness information.
97 BlockMapT blockMapping;
98};
99
100/// This class represents liveness information on block level.
102public:
103 /// A typedef declaration of a value set.
105
106public:
107 /// Returns the underlying block.
108 Block *getBlock() const { return block; }
109
110 /// Returns all values that are live at the beginning
111 /// of the block (unordered).
112 const ValueSetT &in() const { return inValues; }
113
114 /// Returns all values that are live at the end
115 /// of the block (unordered).
116 const ValueSetT &out() const { return outValues; }
117
118 /// Returns true if the given value is in the live-in set.
119 bool isLiveIn(Value value) const;
120
121 /// Returns true if the given value is in the live-out set.
122 bool isLiveOut(Value value) const;
123
124 /// Gets the start operation for the given value. This is the first operation
125 /// the given value is considered to be live. This could either be the start
126 /// operation of the current block (in case the value is live-in) or the
127 /// operation that defines the given value (must be referenced in this block).
128 Operation *getStartOperation(Value value) const;
129
130 /// Gets the end operation for the given value using the start operation
131 /// provided (must be referenced in this block).
132 Operation *getEndOperation(Value value, Operation *startOperation) const;
133
134 /// Get the set of values that are currently live (if any) for the current op.
135 /// This analysis takes an expansive view of "live" in that if a value is
136 /// defined by or within the operation or is fully consumed (as in last user)
137 /// by or within the operation the value is considered "live". The values in
138 /// the list are not ordered.
139 ///
140 /// This check is quite expensive as it does not cache the results of the
141 /// computation, so the currently live values have to be recomputed for each
142 /// op.
144
145private:
146 /// The underlying block.
147 Block *block = nullptr;
148
149 /// The set of all live in values.
150 ValueSetT inValues;
151
152 /// The set of all live out values.
153 ValueSetT outValues;
154
155 friend class Liveness;
156};
157
158} // namespace mlir
159
160#endif // MLIR_ANALYSIS_LIVENESS_H
Block represents an ordered list of Operations.
Definition Block.h:34
This class represents liveness information on block level.
Definition Liveness.h:101
Liveness::ValueSetT ValueSetT
A typedef declaration of a value set.
Definition Liveness.h:104
bool isLiveIn(Value value) const
Returns true if the given value is in the live-in set.
Definition Liveness.cpp:355
Operation * getStartOperation(Value value) const
Gets the start operation for the given value.
Definition Liveness.cpp:366
const ValueSetT & out() const
Returns all values that are live at the end of the block (unordered).
Definition Liveness.h:116
Block * getBlock() const
Returns the underlying block.
Definition Liveness.h:108
ValueSetT currentlyLiveValues(Operation *op) const
Get the set of values that are currently live (if any) for the current op.
Definition Liveness.cpp:398
bool isLiveOut(Value value) const
Returns true if the given value is in the live-out set.
Definition Liveness.cpp:360
const ValueSetT & in() const
Returns all values that are live at the beginning of the block (unordered).
Definition Liveness.h:112
friend class Liveness
Definition Liveness.h:155
Operation * getEndOperation(Value value, Operation *startOperation) const
Gets the end operation for the given value using the start operation provided (must be referenced in ...
Definition Liveness.cpp:377
bool isDeadAfter(Value value, Operation *operation) const
Returns true if value is not live after operation.
Definition Liveness.cpp:241
Liveness(Operation *op)
Creates a new Liveness analysis that computes liveness information for all associated regions.
Definition Liveness.cpp:158
DenseMap< Block *, LivenessBlockInfo > BlockMapT
Definition Liveness.h:50
OperationListT resolveLiveness(Value value) const
Gets liveness info (if any) for the given value.
Definition Liveness.cpp:178
const LivenessBlockInfo * getLiveness(Block *block) const
Gets liveness info (if any) for the block.
Definition Liveness.cpp:225
SmallPtrSet< Value, 16 > ValueSetT
Definition Liveness.h:51
void print(raw_ostream &os) const
Dumps the liveness information to the given stream.
Definition Liveness.cpp:262
Operation * getOperation() const
Returns the operation this analysis was constructed from.
Definition Liveness.h:59
const ValueSetT & getLiveOut(Block *block) const
Returns a reference to a set containing live-out values (unordered).
Definition Liveness.cpp:236
const ValueSetT & getLiveIn(Block *block) const
Returns a reference to a set containing live-in values (unordered).
Definition Liveness.cpp:231
std::vector< Operation * > OperationListT
Definition Liveness.h:49
LLVM_DUMP_METHOD void dump() const
Dumps the liveness information in a human readable format.
Definition Liveness.cpp:258
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Include the generated interface declarations.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123