12 #include "llvm/ADT/SmallPtrSet.h"
21 assert(representation == benefit && benefit != ImpossibleToMatchSentinel &&
22 "This pattern match benefit is too large to represent");
27 return representation;
49 :
Pattern(nullptr, RootKind::Any, generatedNames, benefit, context) {}
58 :
Pattern(interfaceID.getAsOpaquePointer(), RootKind::InterfaceID,
59 generatedNames, benefit, context) {}
68 :
Pattern(traitID.getAsOpaquePointer(), RootKind::TraitID, generatedNames,
78 : rootValue(rootValue), rootKind(rootKind), benefit(benefit),
79 contextAndHasBoundedRecursion(context, false) {
80 if (generatedNames.empty())
82 generatedOps.reserve(generatedNames.size());
83 std::transform(generatedNames.begin(), generatedNames.end(),
84 std::back_inserter(generatedOps), [context](StringRef name) {
85 return OperationName(name, context);
94 void RewritePattern::anchor() {}
110 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
111 rewriteListener->notifyOperationReplaced(from, to);
118 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
119 rewriteListener->notifyOperationReplaced(from, to);
129 "incorrect # of replacement values");
142 assert(op && newOp &&
"expected non-null op");
144 "ops have different number of results");
156 assert(op->
use_empty() &&
"expected 'op' to have no uses");
157 auto *rewriteListener = dyn_cast_if_present<Listener>(
listener);
165 if (!rewriteListener) {
171 auto eraseSingleOp = [&](
Operation *op) {
176 "expected empty regions");
181 "expected that op has no uses");
183 rewriteListener->notifyOperationErased(op);
206 for (
Block *b : llvm::post_order_ext(&r.front(), visited)) {
213 erasedBlocks.push_back(b);
215 for (
Block *b : erasedBlocks) {
233 assert(block->
use_empty() &&
"expected 'block' to have no uses");
235 for (
auto &op : llvm::make_early_inc_range(llvm::reverse(*block))) {
236 assert(op.
use_empty() &&
"expected 'op' to have no uses");
241 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
242 rewriteListener->notifyBlockErased(block);
249 if (
auto *rewriteListener = dyn_cast_if_present<Listener>(
listener))
250 rewriteListener->notifyOperationModified(op);
257 return !preservedUsers.contains(user);
263 bool *allUsesReplaced) {
264 bool allReplaced =
true;
266 bool replace = functor(operand);
269 allReplaced &= replace;
272 *allUsesReplaced = allReplaced;
277 bool *allUsesReplaced) {
278 assert(from.size() == to.size() &&
"incorrect number of replacements");
279 bool allReplaced =
true;
280 for (
auto it : llvm::zip_equal(from, to)) {
287 *allUsesReplaced = allReplaced;
294 "incorrect # of argument replacement values");
299 "expected 'source' to have no predecessors");
301 if (dest->
end() != before) {
306 "expected 'source' to have no successors");
311 assert(dest->
hasNoSuccessors() &&
"expected 'dest' to have no successors");
315 for (
auto it : llvm::zip(source->
getArguments(), argValues))
324 while (!source->
empty())
334 assert(source->
empty() &&
"expected 'source' to be empty");
361 if (before == block->
end())
366 while (before->getBlock() != newBlock)
385 while (!region.
empty())
394 anotherBlock->getIterator());
427 assert(iterator != block->
end() &&
"cannot move after end of block");
This class represents an argument of a Block.
Block represents an ordered list of Operations.
OpListType::iterator iterator
bool hasNoSuccessors()
Returns true if this blocks has no successors.
unsigned getNumArguments()
void erase()
Unlink this Block from its parent region and delete it.
Block * splitBlock(iterator splitBefore)
Split the block into two blocks before the specified operation or iterator.
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
OpListType & getOperations()
BlockArgListType getArguments()
void moveBefore(Block *block)
Unlink this block from its current region and insert it right before the specific block.
bool hasNoPredecessors()
Return true if this block has no predecessors.
bool use_empty() const
Returns true if this value has no uses.
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a saved insertion point.
RAII guard to reset the insertion point of the builder when destroyed.
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Listener * listener
The optional listener for events of this builder.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
This class represents an operand of an operation.
Operation is the basic unit of execution within MLIR.
bool use_empty()
Returns true if this operation has no uses.
void dropAllUses()
Drop all uses of results of this operation.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Block * getBlock()
Returns the operation block that contains this operation.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
void moveBefore(Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
Region * getParentRegion()
Returns the region to which the instruction belongs.
result_range getResults()
void erase()
Remove this operation from its parent block and delete it.
unsigned getNumResults()
Return the number of results held by this operation.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
bool isImpossibleToMatch() const
unsigned short getBenefit() const
If the corresponding pattern can match, return its benefit. If the.
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
Pattern(StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
Construct a pattern with a certain benefit that matches the operation with the given root name.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
BlockListType & getBlocks()
BlockListType::iterator iterator
virtual void eraseBlock(Block *block)
This method erases all operations in a block.
Block * splitBlock(Block *block, Block::iterator before)
Split the operations starting at "before" (inclusive) out of the given block into a new block,...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void moveBlockBefore(Block *block, Block *anotherBlock)
Unlink this block and insert it right before existingBlock.
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void replaceUsesWithIf(Value from, Value to, function_ref< bool(OpOperand &)> functor, bool *allUsesReplaced=nullptr)
Find uses of from and replace them with to if the functor returns true.
void replaceAllUsesExcept(Value from, Value to, Operation *exceptedUser)
Find uses of from and replace them with to except if the user is exceptedUser.
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={})
Inline the operations of block 'source' into block 'dest' before the given position.
void moveOpBefore(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
void mergeBlocks(Block *source, Block *dest, ValueRange argValues={})
Inline the operations of block 'source' into the end of block 'dest'.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
void moveOpAfter(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
virtual void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
void replaceAllOpUsesWith(Operation *from, ValueRange to)
Find uses of from and replace them with to.
This class provides an efficient unique identifier for a specific C++ type.
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Operation * getOwner() const
Return the owner of this operand.
Include the generated interface declarations.
bool mayBeGraphRegion(Region ®ion)
Return "true" if the given region may be a graph region without SSA dominance.
@ RewriterBaseListener
RewriterBase::Listener or user-derived class.
This class represents a listener that may be used to hook into various actions within an OpBuilder.
virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
Notify the listener that the specified block was inserted.
virtual void notifyOperationInserted(Operation *op, InsertPoint previous)
Notify the listener that the specified operation was inserted.
This class acts as a special tag that makes the desire to match "any" operation type explicit.
This class acts as a special tag that makes the desire to match any operation that implements a given...
This class acts as a special tag that makes the desire to match any operation that implements a given...
static constexpr auto makeIterable(RangeT &&range)
static bool classof(const OpBuilder::Listener *base)