MLIR  20.0.0git
Block.cpp
Go to the documentation of this file.
1 //===- Block.cpp - MLIR Block Class ---------------------------------------===//
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/Block.h"
10 
11 #include "mlir/IR/Builders.h"
12 #include "mlir/IR/Operation.h"
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 
16 using namespace mlir;
17 
18 //===----------------------------------------------------------------------===//
19 // Block
20 //===----------------------------------------------------------------------===//
21 
23  assert(!verifyOpOrder() && "Expected valid operation ordering.");
24  clear();
25  for (BlockArgument arg : arguments)
26  arg.destroy();
27 }
28 
29 Region *Block::getParent() const { return parentValidOpOrderPair.getPointer(); }
30 
31 /// Returns the closest surrounding operation that contains this block or
32 /// nullptr if this block is unlinked.
34  return getParent() ? getParent()->getParentOp() : nullptr;
35 }
36 
37 /// Return if this block is the entry block in the parent region.
38 bool Block::isEntryBlock() { return this == &getParent()->front(); }
39 
40 /// Insert this block (which must not already be in a region) right before the
41 /// specified block.
42 void Block::insertBefore(Block *block) {
43  assert(!getParent() && "already inserted into a block!");
44  assert(block->getParent() && "cannot insert before a block without a parent");
45  block->getParent()->getBlocks().insert(block->getIterator(), this);
46 }
47 
48 void Block::insertAfter(Block *block) {
49  assert(!getParent() && "already inserted into a block!");
50  assert(block->getParent() && "cannot insert before a block without a parent");
51  block->getParent()->getBlocks().insertAfter(block->getIterator(), this);
52 }
53 
54 /// Unlink this block from its current region and insert it right before the
55 /// specific block.
56 void Block::moveBefore(Block *block) {
57  assert(block->getParent() && "cannot insert before a block without a parent");
58  moveBefore(block->getParent(), block->getIterator());
59 }
60 
61 /// Unlink this block from its current region and insert it right before the
62 /// block that the given iterator points to in the region region.
63 void Block::moveBefore(Region *region, llvm::iplist<Block>::iterator iterator) {
64  region->getBlocks().splice(iterator, getParent()->getBlocks(), getIterator());
65 }
66 
67 /// Unlink this Block from its parent Region and delete it.
68 void Block::erase() {
69  assert(getParent() && "Block has no parent");
70  getParent()->getBlocks().erase(this);
71 }
72 
73 /// Returns 'op' if 'op' lies in this block, or otherwise finds the
74 /// ancestor operation of 'op' that lies in this block. Returns nullptr if
75 /// the latter fails.
77  // Traverse up the operation hierarchy starting from the owner of operand to
78  // find the ancestor operation that resides in the block of 'forOp'.
79  auto *currOp = &op;
80  while (currOp->getBlock() != this) {
81  currOp = currOp->getParentOp();
82  if (!currOp)
83  return nullptr;
84  }
85  return currOp;
86 }
87 
88 /// This drops all operand uses from operations within this block, which is
89 /// an essential step in breaking cyclic dependences between references when
90 /// they are to be deleted.
92  for (Operation &i : *this)
94 }
95 
97  for (auto arg : getArguments())
98  arg.dropAllUses();
99  for (auto &op : *this)
100  op.dropAllDefinedValueUses();
101  dropAllUses();
102 }
103 
104 /// Returns true if the ordering of the child operations is valid, false
105 /// otherwise.
106 bool Block::isOpOrderValid() { return parentValidOpOrderPair.getInt(); }
107 
108 /// Invalidates the current ordering of operations.
110  // Validate the current ordering.
111  assert(!verifyOpOrder());
112  parentValidOpOrderPair.setInt(false);
113 }
114 
115 /// Verifies the current ordering of child operations. Returns false if the
116 /// order is valid, true otherwise.
118  // The order is already known to be invalid.
119  if (!isOpOrderValid())
120  return false;
121  // The order is valid if there are less than 2 operations.
122  if (operations.empty() || std::next(operations.begin()) == operations.end())
123  return false;
124 
125  Operation *prev = nullptr;
126  for (auto &i : *this) {
127  // The previous operation must have a smaller order index than the next as
128  // it appears earlier in the list.
129  if (prev && prev->orderIndex != Operation::kInvalidOrderIdx &&
130  prev->orderIndex >= i.orderIndex)
131  return true;
132  prev = &i;
133  }
134  return false;
135 }
136 
137 /// Recomputes the ordering of child operations within the block.
139  parentValidOpOrderPair.setInt(true);
140 
141  unsigned orderIndex = 0;
142  for (auto &op : *this)
143  op.orderIndex = (orderIndex += Operation::kOrderStride);
144 }
145 
146 //===----------------------------------------------------------------------===//
147 // Argument list management.
148 //===----------------------------------------------------------------------===//
149 
150 /// Return a range containing the types of the arguments for this block.
152  return ValueTypeRange<BlockArgListType>(getArguments());
153 }
154 
156  BlockArgument arg = BlockArgument::create(type, this, arguments.size(), loc);
157  arguments.push_back(arg);
158  return arg;
159 }
160 
161 /// Add one argument to the argument list for each type specified in the list.
164  assert(types.size() == locs.size() &&
165  "incorrect number of block argument locations");
166  size_t initialSize = arguments.size();
167  arguments.reserve(initialSize + types.size());
168 
169  for (auto typeAndLoc : llvm::zip(types, locs))
170  addArgument(std::get<0>(typeAndLoc), std::get<1>(typeAndLoc));
171  return {arguments.data() + initialSize, arguments.data() + arguments.size()};
172 }
173 
174 BlockArgument Block::insertArgument(unsigned index, Type type, Location loc) {
175  assert(index <= arguments.size() && "invalid insertion index");
176 
177  auto arg = BlockArgument::create(type, this, index, loc);
178  arguments.insert(arguments.begin() + index, arg);
179  // Update the cached position for all the arguments after the newly inserted
180  // one.
181  ++index;
182  for (BlockArgument arg : llvm::drop_begin(arguments, index))
183  arg.setArgNumber(index++);
184  return arg;
185 }
186 
187 /// Insert one value to the given position of the argument list. The existing
188 /// arguments are shifted. The block is expected not to have predecessors.
190  assert(getPredecessors().empty() &&
191  "cannot insert arguments to blocks with predecessors");
192  return insertArgument(it->getArgNumber(), type, loc);
193 }
194 
195 void Block::eraseArgument(unsigned index) {
196  assert(index < arguments.size());
197  arguments[index].destroy();
198  arguments.erase(arguments.begin() + index);
199  for (BlockArgument arg : llvm::drop_begin(arguments, index))
200  arg.setArgNumber(index++);
201 }
202 
203 void Block::eraseArguments(unsigned start, unsigned num) {
204  assert(start + num <= arguments.size());
205  for (unsigned i = 0; i < num; ++i)
206  arguments[start + i].destroy();
207  arguments.erase(arguments.begin() + start, arguments.begin() + start + num);
208  for (BlockArgument arg : llvm::drop_begin(arguments, start))
209  arg.setArgNumber(start++);
210 }
211 
212 void Block::eraseArguments(const BitVector &eraseIndices) {
214  [&](BlockArgument arg) { return eraseIndices.test(arg.getArgNumber()); });
215 }
216 
218  auto firstDead = llvm::find_if(arguments, shouldEraseFn);
219  if (firstDead == arguments.end())
220  return;
221 
222  // Destroy the first dead argument, this avoids reapplying the predicate to
223  // it.
224  unsigned index = firstDead->getArgNumber();
225  firstDead->destroy();
226 
227  // Iterate the remaining arguments to remove any that are now dead.
228  for (auto it = std::next(firstDead), e = arguments.end(); it != e; ++it) {
229  // Destroy dead arguments, and shift those that are still live.
230  if (shouldEraseFn(*it)) {
231  it->destroy();
232  } else {
233  it->setArgNumber(index++);
234  *firstDead++ = *it;
235  }
236  }
237  arguments.erase(firstDead, arguments.end());
238 }
239 
240 //===----------------------------------------------------------------------===//
241 // Terminator management
242 //===----------------------------------------------------------------------===//
243 
244 /// Get the terminator operation of this block. This function asserts that
245 /// the block might have a valid terminator operation.
247  assert(mightHaveTerminator());
248  return &back();
249 }
250 
251 /// Check whether this block might have a terminator.
254 }
255 
256 // Indexed successor access.
258  return empty() ? 0 : back().getNumSuccessors();
259 }
260 
262  assert(i < getNumSuccessors());
263  return getTerminator()->getSuccessor(i);
264 }
265 
266 /// If this block has exactly one predecessor, return it. Otherwise, return
267 /// null.
268 ///
269 /// Note that multiple edges from a single block (e.g. if you have a cond
270 /// branch with the same block as the true/false destinations) is not
271 /// considered to be a single predecessor.
273  auto it = pred_begin();
274  if (it == pred_end())
275  return nullptr;
276  auto *firstPred = *it;
277  ++it;
278  return it == pred_end() ? firstPred : nullptr;
279 }
280 
281 /// If this block has a unique predecessor, i.e., all incoming edges originate
282 /// from one block, return it. Otherwise, return null.
284  auto it = pred_begin(), e = pred_end();
285  if (it == e)
286  return nullptr;
287 
288  // Check for any conflicting predecessors.
289  auto *firstPred = *it;
290  for (++it; it != e; ++it)
291  if (*it != firstPred)
292  return nullptr;
293  return firstPred;
294 }
295 
296 //===----------------------------------------------------------------------===//
297 // Other
298 //===----------------------------------------------------------------------===//
299 
300 /// Split the block into two blocks before the specified operation or
301 /// iterator.
302 ///
303 /// Note that all operations BEFORE the specified iterator stay as part of
304 /// the original basic block, and the rest of the operations in the original
305 /// block are moved to the new block, including the old terminator. The
306 /// original block is left without a terminator.
307 ///
308 /// The newly formed Block is returned, and the specified iterator is
309 /// invalidated.
311  // Start by creating a new basic block, and insert it immediate after this
312  // one in the containing region.
313  auto *newBB = new Block();
314  getParent()->getBlocks().insert(std::next(Region::iterator(this)), newBB);
315 
316  // Move all of the operations from the split point to the end of the region
317  // into the new block.
318  newBB->getOperations().splice(newBB->end(), getOperations(), splitBefore,
319  end());
320  return newBB;
321 }
322 
323 //===----------------------------------------------------------------------===//
324 // Predecessors
325 //===----------------------------------------------------------------------===//
326 
327 Block *PredecessorIterator::unwrap(BlockOperand &value) {
328  return value.getOwner()->getBlock();
329 }
330 
331 /// Get the successor number in the predecessor terminator.
333  return I->getOperandNumber();
334 }
335 
336 //===----------------------------------------------------------------------===//
337 // Successors
338 //===----------------------------------------------------------------------===//
339 
341 
343  if (block->empty() || llvm::hasSingleElement(*block->getParent()))
344  return;
345  Operation *term = &block->back();
346  if ((count = term->getNumSuccessors()))
347  base = term->getBlockOperands().data();
348 }
349 
351  if ((count = term->getNumSuccessors()))
352  base = term->getBlockOperands().data();
353 }
354 
356  assert(getParent() == other->getParent() && "expected same region");
357  if (except.contains(other)) {
358  // Fast path: If `other` is in the `except` set, there can be no path from
359  // "this" to `other` (that does not pass through an excluded block).
360  return false;
361  }
363  while (!worklist.empty()) {
364  Block *next = worklist.pop_back_val();
365  if (next == other)
366  return true;
367  // Note: `except` keeps track of already visited blocks.
368  if (!except.insert(next).second)
369  continue;
370  worklist.append(next->succ_begin(), next->succ_end());
371  }
372  return false;
373 }
374 
375 //===----------------------------------------------------------------------===//
376 // BlockRange
377 //===----------------------------------------------------------------------===//
378 
380  if ((count = blocks.size()))
381  base = blocks.data();
382 }
383 
385  : BlockRange(successors.begin().getBase(), successors.size()) {}
386 
387 /// See `llvm::detail::indexed_accessor_range_base` for details.
388 BlockRange::OwnerT BlockRange::offset_base(OwnerT object, ptrdiff_t index) {
389  if (auto *operand = llvm::dyn_cast_if_present<BlockOperand *>(object))
390  return {operand + index};
391  return {llvm::dyn_cast_if_present<Block *const *>(object) + index};
392 }
393 
394 /// See `llvm::detail::indexed_accessor_range_base` for details.
395 Block *BlockRange::dereference_iterator(OwnerT object, ptrdiff_t index) {
396  if (const auto *operand = llvm::dyn_cast_if_present<BlockOperand *>(object))
397  return operand[index].get();
398  return llvm::dyn_cast_if_present<Block *const *>(object)[index];
399 }
static Value getBase(Value v)
Looks through known "view-like" ops to find the base memref.
This class represents an argument of a Block.
Definition: Value.h:319
unsigned getArgNumber() const
Returns the number of this argument.
Definition: Value.h:331
A block operand represents an operand that holds a reference to a Block, e.g.
Definition: BlockSupport.h:30
This class provides an abstraction over the different types of ranges over Blocks.
Definition: BlockSupport.h:106
BlockRange(ArrayRef< Block * > blocks=std::nullopt)
Definition: Block.cpp:379
Block represents an ordered list of Operations.
Definition: Block.h:33
void recomputeOpOrder()
Recomputes the ordering of child operations within the block.
Definition: Block.cpp:138
OpListType::iterator iterator
Definition: Block.h:140
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:76
ValueTypeRange< BlockArgListType > getArgumentTypes()
Return a range containing the types of the arguments for this block.
Definition: Block.cpp:151
unsigned getNumSuccessors()
Definition: Block.cpp:257
bool empty()
Definition: Block.h:148
Operation & back()
Definition: Block.h:152
void erase()
Unlink this Block from its parent region and delete it.
Definition: Block.cpp:68
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:189
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:162
Block * splitBlock(iterator splitBefore)
Split the block into two blocks before the specified operation or iterator.
Definition: Block.cpp:310
Block()=default
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition: Block.cpp:29
bool isOpOrderValid()
Returns true if the ordering of the child operations is valid, false otherwise.
Definition: Block.cpp:106
succ_iterator succ_end()
Definition: Block.h:266
pred_iterator pred_begin()
Definition: Block.h:233
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:96
bool verifyOpOrder()
Verifies the current ordering of child operations matches the validOpOrder flag.
Definition: Block.cpp:117
void invalidateOpOrder()
Invalidates the current ordering of operations.
Definition: Block.cpp:109
Block * getSinglePredecessor()
If this block has exactly one predecessor, return it.
Definition: Block.cpp:272
void insertAfter(Block *block)
Insert this block (which must not already be in a region) right after the specified block.
Definition: Block.cpp:48
Operation * getTerminator()
Get the terminator operation of this block.
Definition: Block.cpp:246
succ_iterator succ_begin()
Definition: Block.h:265
iterator_range< pred_iterator > getPredecessors()
Definition: Block.h:237
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
Definition: Block.cpp:155
void clear()
Definition: Block.h:38
void eraseArguments(unsigned start, unsigned num)
Erases 'num' arguments from the index 'start'.
Definition: Block.cpp:203
OpListType & getOperations()
Definition: Block.h:137
bool mightHaveTerminator()
Check whether this block might have a terminator.
Definition: Block.cpp:252
BlockArgListType getArguments()
Definition: Block.h:87
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:355
iterator end()
Definition: Block.h:144
Block * getUniquePredecessor()
If this block has a unique predecessor, i.e., all incoming edges originate from one block,...
Definition: Block.cpp:283
void eraseArgument(unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition: Block.cpp:195
Block * getSuccessor(unsigned i)
Definition: Block.cpp:261
bool isEntryBlock()
Return if this block is the entry block in the parent region.
Definition: Block.cpp:38
void dropAllReferences()
This drops all operand uses from operations within this block, which is an essential step in breaking...
Definition: Block.cpp:91
void insertBefore(Block *block)
Insert this block (which must not already be in a region) right before the specified block.
Definition: Block.cpp:42
pred_iterator pred_end()
Definition: Block.h:236
void moveBefore(Block *block)
Unlink this block from its current region and insert it right before the specific block.
Definition: Block.cpp:56
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition: Block.cpp:33
BlockArgListType::iterator args_iterator
Definition: Block.h:92
void dropAllUses()
Drop all uses of this object from their respective owners.
Definition: UseDefLists.h:202
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This class provides the API for ops that are known to be terminators.
Definition: OpDefinition.h:764
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Block * getSuccessor(unsigned index)
Definition: Operation.h:709
unsigned getNumSuccessors()
Definition: Operation.h:707
void dropAllReferences()
This drops all operand uses from this operation, which is an essential step in breaking cyclic depend...
Definition: Operation.cpp:584
bool mightHaveTrait()
Returns true if the operation might have the provided trait.
Definition: Operation.h:758
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition: Operation.h:234
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:213
MutableArrayRef< BlockOperand > getBlockOperands()
Definition: Operation.h:696
unsigned getSuccessorIndex() const
Get the successor number in the predecessor terminator.
Definition: Block.cpp:332
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition: Region.h:200
BlockListType & getBlocks()
Definition: Region.h:45
Block & front()
Definition: Region.h:65
BlockListType::iterator iterator
Definition: Region.h:52
This class implements the successor iterators for Block.
Definition: BlockSupport.h:73
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class implements iteration on the types of a given range of values.
Definition: TypeRange.h:131
Operation * getOwner() const
Return the owner of this operand.
Definition: UseDefLists.h:38
Include the generated interface declarations.