1//===- Value.cpp - MLIR Value Classes -------------------------------------===//
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#include "mlir/IR/Value.h"
10#include "mlir/IR/Block.h"
11#include "mlir/IR/Operation.h"
12
13using namespace mlir;
14using namespace mlir::detail;
15
16/// If this value is the result of an Operation, return the operation that
17/// defines it.
18Operation *Value::getDefiningOp() const {
19 if (auto result = llvm::dyn_cast<OpResult>(Val: *this))
20 return result.getOwner();
21 return nullptr;
22}
23
24Location Value::getLoc() const {
25 if (auto *op = getDefiningOp())
26 return op->getLoc();
27
28 return llvm::cast<BlockArgument>(Val: *this).getLoc();
29}
30
31void Value::setLoc(Location loc) {
32 if (auto *op = getDefiningOp())
33 return op->setLoc(loc);
34
35 return llvm::cast<BlockArgument>(Val&: *this).setLoc(loc);
36}
37
38/// Return the Region in which this Value is defined.
39Region *Value::getParentRegion() {
40 if (auto *op = getDefiningOp())
41 return op->getParentRegion();
42 return llvm::cast<BlockArgument>(Val&: *this).getOwner()->getParent();
43}
44
45/// Return the Block in which this Value is defined.
46Block *Value::getParentBlock() {
47 if (Operation *op = getDefiningOp())
48 return op->getBlock();
49 return llvm::cast<BlockArgument>(Val&: *this).getOwner();
50}
51
52unsigned Value::getNumUses() const {
53 return (unsigned)std::distance(first: use_begin(), last: use_end());
54}
55
56bool Value::hasNUses(unsigned n) const {
57 return hasNItems(Begin: use_begin(), End: use_end(), N: n);
58}
59
60bool Value::hasNUsesOrMore(unsigned n) const {
61 return hasNItemsOrMore(Begin: use_begin(), End: use_end(), N: n);
62}
63
64//===----------------------------------------------------------------------===//
65// Value::UseLists
66//===----------------------------------------------------------------------===//
67
68/// Replace all uses of 'this' value with the new value, updating anything in
69/// the IR that uses 'this' to use the other value instead except if the user is
70/// listed in 'exceptions' .
71void Value::replaceAllUsesExcept(
72 Value newValue, const SmallPtrSetImpl<Operation *> &exceptions) {
73 for (OpOperand &use : llvm::make_early_inc_range(Range: getUses())) {
74 if (exceptions.count(Ptr: use.getOwner()) == 0)
75 use.set(newValue);
76 }
77}
78
79/// Replace all uses of 'this' value with 'newValue', updating anything in the
80/// IR that uses 'this' to use the other value instead except if the user is
81/// 'exceptedUser'.
82void Value::replaceAllUsesExcept(Value newValue, Operation *exceptedUser) {
83 for (OpOperand &use : llvm::make_early_inc_range(Range: getUses())) {
84 if (use.getOwner() != exceptedUser)
85 use.set(newValue);
86 }
87}
88
89/// Replace all uses of 'this' value with 'newValue' if the given callback
90/// returns true. Returns whether any uses were replaced.
91bool Value::replaceUsesWithIf(Value newValue,
92 function_ref<bool(OpOperand &)> shouldReplace) {
93 bool replaced = false;
94 for (OpOperand &use : llvm::make_early_inc_range(Range: getUses())) {
95 if (shouldReplace(use)) {
96 use.set(newValue);
97 replaced = true;
98 }
99 }
100 return replaced;
101}
102
103/// Returns true if the value is used outside of the given block.
104bool Value::isUsedOutsideOfBlock(Block *block) const {
105 return llvm::any_of(Range: getUsers(), P: [block](Operation *user) {
106 return user->getBlock() != block;
107 });
108}
109
110/// Shuffles the use-list order according to the provided indices.
111void Value::shuffleUseList(ArrayRef<unsigned> indices) {
112 getImpl()->shuffleUseList(indices);
113}
114
115//===----------------------------------------------------------------------===//
116// OpResult
117//===----------------------------------------------------------------------===//
118
119/// Returns the parent operation of this trailing result.
120Operation *OpResultImpl::getOwner() const {
121 // We need to do some arithmetic to get the operation pointer. Results are
122 // stored in reverse order before the operation, so move the trailing owner up
123 // to the start of the array. A rough diagram of the memory layout is:
124 //
125 // | Out-of-Line results | Inline results | Operation |
126 //
127 // Given that the results are reverse order we use the result number to know
128 // how far to jump to get to the operation. So if we are currently the 0th
129 // result, the layout would be:
130 //
131 // | Inline result 0 | Operation
132 //
133 // ^-- To get the base address of the operation, we add the result count + 1.
134 if (const auto *result = dyn_cast<InlineOpResult>(Val: this)) {
135 result += result->getResultNumber() + 1;
136 return reinterpret_cast<Operation *>(const_cast<InlineOpResult *>(result));
137 }
138
139 // Out-of-line results are stored in an array just before the inline results.
140 const OutOfLineOpResult *outOfLineIt = (const OutOfLineOpResult *)(this);
141 outOfLineIt += (outOfLineIt->outOfLineIndex + 1);
142
143 // Move the owner past the inline results to get to the operation.
144 const auto *inlineIt = reinterpret_cast<const InlineOpResult *>(outOfLineIt);
145 inlineIt += getMaxInlineResults();
146 return reinterpret_cast<Operation *>(const_cast<InlineOpResult *>(inlineIt));
147}
148
149OpResultImpl *OpResultImpl::getNextResultAtOffset(intptr_t offset) {
150 if (offset == 0)
151 return this;
152 // We need to do some arithmetic to get the next result given that results are
153 // in reverse order, and that we need to account for the different types of
154 // results. As a reminder, the rough diagram of the memory layout is:
155 //
156 // | Out-of-Line results | Inline results | Operation |
157 //
158 // So an example operation with two results would look something like:
159 //
160 // | Inline result 1 | Inline result 0 | Operation |
161 //
162
163 // Handle the case where this result is an inline result.
164 OpResultImpl *result = this;
165 if (auto *inlineResult = dyn_cast<InlineOpResult>(Val: this)) {
166 // Check to see how many results there are after this one before the start
167 // of the out-of-line results. If the desired offset is less than the number
168 // remaining, we can directly use the offset from the current result
169 // pointer. The following diagrams highlight the two situations.
170 //
171 // | Out-of-Line results | Inline results | Operation |
172 // ^- Say we are here.
173 // ^- If our destination is here, we can use the
174 // offset directly.
175 //
176 intptr_t leftBeforeTrailing =
177 getMaxInlineResults() - inlineResult->getResultNumber() - 1;
178 if (leftBeforeTrailing >= offset)
179 return inlineResult - offset;
180
181 // Otherwise, adjust the current result pointer to the end (start in memory)
182 // of the inline result array.
183 //
184 // | Out-of-Line results | Inline results | Operation |
185 // ^- Say we are here.
186 // ^- If our destination is here, we need to first jump to
187 // the end (start in memory) of the inline result array.
188 //
189 result = inlineResult - leftBeforeTrailing;
190 offset -= leftBeforeTrailing;
191 }
192
193 // If we land here, the current result is an out-of-line result and we can
194 // offset directly.
195 return reinterpret_cast<OutOfLineOpResult *>(result) - offset;
196}
197
198/// Given a number of operation results, returns the number that need to be
199/// stored inline.
200unsigned OpResult::getNumInline(unsigned numResults) {
201 return std::min(a: numResults, b: OpResultImpl::getMaxInlineResults());
202}
203
204/// Given a number of operation results, returns the number that need to be
205/// stored as trailing.
206unsigned OpResult::getNumTrailing(unsigned numResults) {
207 // If we can pack all of the results, there is no need for additional storage.
208 unsigned maxInline = OpResultImpl::getMaxInlineResults();
209 return numResults <= maxInline ? 0 : numResults - maxInline;
210}
211
212//===----------------------------------------------------------------------===//
213// BlockOperand
214//===----------------------------------------------------------------------===//
215
216/// Provide the use list that is attached to the given block.
217IRObjectWithUseList<BlockOperand> *BlockOperand::getUseList(Block *value) {
218 return value;
219}
220
221/// Return which operand this is in the operand list.
222unsigned BlockOperand::getOperandNumber() const {
223 return this - &getOwner()->getBlockOperands()[0];
224}
225
226//===----------------------------------------------------------------------===//
227// OpOperand
228//===----------------------------------------------------------------------===//
229
230/// Return which operand this is in the operand list.
231unsigned OpOperand::getOperandNumber() const {
232 return this - &getOwner()->getOpOperands()[0];
233}
234