MLIR: lib/IR/Block.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
10
13
14using namespace mlir;
15
16
17
18
19
21 assert(() && "Expected valid operation ordering.");
24 arg.destroy();
25}
26
28
29
30
34
35
37
38
39
41 assert(() && "already inserted into a block!");
42 assert(block->getParent() && "cannot insert before a block without a parent");
44}
45
47 assert(() && "already inserted into a block!");
48 assert(block->getParent() && "cannot insert before a block without a parent");
49 block->getParent()->getBlocks().insertAfter(block->getIterator(), this);
50}
51
52
53
55 assert(block->getParent() && "cannot insert before a block without a parent");
57}
58
59
60
64
65
67 assert(getParent() && "Block has no parent");
69}
70
71
72
73
75
76
77 auto *currOp = &op;
78 while (currOp->getBlock() != this) {
79 currOp = currOp->getParentOp();
80 if (!currOp)
81 return nullptr;
82 }
83 return currOp;
84}
85
86
87
88
93
96 arg.dropAllUses();
97 for (auto &op : *this)
98 op.dropAllDefinedValueUses();
100}
101
102
103
105
106
108
110 parentValidOpOrderPair.setInt(false);
111}
112
113
114
116
118 return false;
119
120 if (operations.empty() || llvm::hasSingleElement(operations))
121 return false;
122
124 for (auto &i : *this) {
125
126
127 if (prev && prev->orderIndex != Operation::kInvalidOrderIdx &&
128 prev->orderIndex >= i.orderIndex)
129 return true;
130 prev = &i;
131 }
132 return false;
133}
134
135
137 parentValidOpOrderPair.setInt(true);
138
139 unsigned orderIndex = 0;
140 for (auto &op : *this)
141 op.orderIndex = (orderIndex += Operation::kOrderStride);
142}
143
144
145
146
147
148
152
154 BlockArgument arg = BlockArgument::create(type, this, arguments.size(), loc);
155 arguments.push_back(arg);
156 return arg;
157}
158
159
162 assert(types.size() == locs.size() &&
163 "incorrect number of block argument locations");
164 size_t initialSize = arguments.size();
165 arguments.reserve(initialSize + types.size());
166
167 for (auto typeAndLoc : llvm::zip(types, locs))
168 addArgument(std::get<0>(typeAndLoc), std::get<1>(typeAndLoc));
169 return {arguments.data() + initialSize, arguments.data() + arguments.size()};
170}
171
173 assert(index <= arguments.size() && "invalid insertion index");
174
175 auto arg = BlockArgument::create(type, this, index, loc);
176 arguments.insert(arguments.begin() + index, arg);
177
178
181 arg.setArgNumber(index++);
182 return arg;
183}
184
185
186
189 "cannot insert arguments to blocks with predecessors");
190 return insertArgument(it->getArgNumber(), type, loc);
191}
192
194 assert(index < arguments.size());
195 arguments[index].destroy();
196 arguments.erase(arguments.begin() + index);
198 arg.setArgNumber(index++);
199}
200
202 assert(start + num <= arguments.size());
203 for (unsigned i = 0; i < num; ++i)
204 arguments[start + i].destroy();
205 arguments.erase(arguments.begin() + start, arguments.begin() + start + num);
206 for (BlockArgument arg : llvm::drop_begin(arguments, start))
207 arg.setArgNumber(start++);
208}
209
214
216 auto firstDead = llvm::find_if(arguments, shouldEraseFn);
217 if (firstDead == arguments.end())
218 return;
219
220
221
222 unsigned index = firstDead->getArgNumber();
223 firstDead->destroy();
224
225
226 for (auto it = std::next(firstDead), e = arguments.end(); it != e; ++it) {
227
228 if (shouldEraseFn(*it)) {
229 it->destroy();
230 } else {
231 it->setArgNumber(index++);
232 *firstDead++ = *it;
233 }
234 }
235 arguments.erase(firstDead, arguments.end());
236}
237
238
239
240
241
242
243
248
249
253
255
256
257
258 if (().hasTraitOpTrait::IsTerminator())
260 auto endIt = --end();
261 return {begin(), endIt};
262}
263
264
268
273
274
275
276
277
278
279
283 return nullptr;
284 auto *firstPred = *it;
285 ++it;
286 return it == pred_end() ? firstPred : nullptr;
287}
288
289
290
293 if (it == e)
294 return nullptr;
295
296
297 auto *firstPred = *it;
298 for (++it; it != e; ++it)
299 if (*it != firstPred)
300 return nullptr;
301 return firstPred;
302}
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
319
320
321 auto *newBB = new Block();
323
324
325
326 newBB->getOperations().splice(newBB->end(), getOperations(), splitBefore,
328 return newBB;
329}
330
331
332
333
334
337}
338
339
341 return I->getOperandNumber();
342}
343
344
345
346
347
349
351 if (block->empty() || llvm::hasSingleElement(*block->getParent()))
352 return;
354 if ((count = term->getNumSuccessors()))
355 base = term->getBlockOperands().data();
356}
357
362
365 if (except.contains(other)) {
366
367
368 return false;
369 }
371 while (!worklist.empty()) {
372 Block *next = worklist.pop_back_val();
373 if (next == other)
374 return true;
375
376 if (!except.insert(next).second)
377 continue;
379 }
380 return false;
381}
382
383
384
385
386
388 if ((count = blocks.size()))
389 base = blocks.data();
390}
391
394
395
396BlockRange::OwnerT BlockRange::offset_base(OwnerT object, ptrdiff_t index) {
397 if (auto *operand = llvm::dyn_cast_if_present<BlockOperand *>(object))
398 return {operand + index};
399 return {llvm::dyn_cast_if_present<Block *const *>(object) + index};
400}
401
402
403Block *BlockRange::dereference_iterator(OwnerT object, ptrdiff_t index) {
404 if (const auto *operand = llvm::dyn_cast_if_present<BlockOperand *>(object))
405 return operand[index].get();
406 return llvm::dyn_cast_if_present<Block *const *>(object)[index];
407}
static Value getBase(Value v)
Looks through known "view-like" ops to find the base memref.
This class represents an argument of a Block.
unsigned getArgNumber() const
Returns the number of this argument.
A block operand represents an operand that holds a reference to a Block, e.g.
BlockRange(ArrayRef< Block * > blocks={})
Definition Block.cpp:387
Block represents an ordered list of Operations.
void recomputeOpOrder()
Recomputes the ordering of child operations within the block.
Definition Block.cpp:136
OpListType::iterator iterator
Operation * findAncestorOpInBlock(Operation &op)
Returns 'op' if 'op' lies in this block, or otherwise finds the ancestor operation of 'op' that lies ...
Definition Block.cpp:74
ValueTypeRange< BlockArgListType > getArgumentTypes()
Return a range containing the types of the arguments for this block.
Definition Block.cpp:149
unsigned getNumSuccessors()
Definition Block.cpp:265
~Block()
Definition Block.cpp:20
iterator_range< pred_iterator > getPredecessors()
void erase()
Unlink this Block from its parent region and delete it.
Definition Block.cpp:66
BlockArgument insertArgument(args_iterator it, Type type, Location loc)
Insert one value to the position in the argument list indicated by the given iterator.
Definition Block.cpp:187
iterator_range< args_iterator > addArguments(TypeRange types, ArrayRef< Location > locs)
Add one argument to the argument list for each type specified in the list.
Definition Block.cpp:160
Block * splitBlock(iterator splitBefore)
Split the block into two blocks before the specified operation or iterator.
Definition Block.cpp:318
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition Block.cpp:27
bool isOpOrderValid()
Returns true if the ordering of the child operations is valid, false otherwise.
Definition Block.cpp:104
OpListType & getOperations()
pred_iterator pred_begin()
void dropAllDefinedValueUses()
This drops all uses of values defined in this block or in the blocks of nested regions wherever the u...
Definition Block.cpp:94
bool verifyOpOrder()
Verifies the current ordering of child operations matches the validOpOrder flag.
Definition Block.cpp:115
void invalidateOpOrder()
Invalidates the current ordering of operations.
Definition Block.cpp:107
Block * getSinglePredecessor()
If this block has exactly one predecessor, return it.
Definition Block.cpp:280
void insertAfter(Block *block)
Insert this block (which must not already be in a region) right after the specified block.
Definition Block.cpp:46
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:244
succ_iterator succ_begin()
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
Definition Block.cpp:153
void eraseArguments(unsigned start, unsigned num)
Erases 'num' arguments from the index 'start'.
Definition Block.cpp:201
bool mightHaveTerminator()
Return "true" if this block might have a terminator.
Definition Block.cpp:250
BlockArgListType getArguments()
bool isReachable(Block *other, SmallPtrSet< Block *, 16 > &&except={})
Return "true" if there is a path from this block to the given block (according to the successors rela...
Definition Block.cpp:363
Block * getUniquePredecessor()
If this block has a unique predecessor, i.e., all incoming edges originate from one block,...
Definition Block.cpp:291
void eraseArgument(unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition Block.cpp:193
Block * getSuccessor(unsigned i)
Definition Block.cpp:269
bool isEntryBlock()
Return if this block is the entry block in the parent region.
Definition Block.cpp:36
void dropAllReferences()
This drops all operand uses from operations within this block, which is an essential step in breaking...
Definition Block.cpp:89
void insertBefore(Block *block)
Insert this block (which must not already be in a region) right before the specified block.
Definition Block.cpp:40
void moveBefore(Block *block)
Unlink this block from its current region and insert it right before the specific block.
Definition Block.cpp:54
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition Block.cpp:31
BlockArgListType::iterator args_iterator
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class provides the API for ops that are known to be terminators.
Operation is the basic unit of execution within MLIR.
MutableArrayRef< BlockOperand > getBlockOperands()
unsigned getNumSuccessors()
void dropAllReferences()
This drops all operand uses from this operation, which is an essential step in breaking cyclic depend...
bool mightHaveTrait()
Returns true if the operation might have the provided trait.
Block * getBlock()
Returns the operation block that contains this operation.
Block * getSuccessor(unsigned index)
unsigned getSuccessorIndex() const
Get the successor number in the predecessor terminator.
Definition Block.cpp:340
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Operation * getParentOp()
Return the parent operation this region is attached to.
BlockListType & getBlocks()
BlockListType::iterator iterator
This class implements the successor iterators for Block.
SuccessorRange()
Definition Block.cpp:348
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class implements iteration on the types of a given range of values.
Operation * getOwner() const
Return the owner of this operand.
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref