@@ -497,42 +497,19 @@ class RewriterBase : public OpBuilder {
497497 Region::iterator before);
498498 void inlineRegionBefore (Region ®ion, Block *before);
499499
500- // / This method replaces the uses of the results of `op` with the values in
501- // / `newValues` when the provided `functor` returns true for a specific use.
502- // / The number of values in `newValues` is required to match the number of
503- // / results of `op`. `allUsesReplaced`, if non-null, is set to true if all of
504- // / the uses of `op` were replaced. Note that in some rewriters, the given
505- // / 'functor' may be stored beyond the lifetime of the rewrite being applied.
506- // / As such, the function should not capture by reference and instead use
507- // / value capture as necessary.
508- virtual void
509- replaceOpWithIf (Operation *op, ValueRange newValues, bool *allUsesReplaced,
510- llvm::unique_function<bool (OpOperand &) const > functor);
511- void replaceOpWithIf (Operation *op, ValueRange newValues,
512- llvm::unique_function<bool (OpOperand &) const > functor) {
513- replaceOpWithIf (op, newValues, /* allUsesReplaced=*/ nullptr ,
514- std::move (functor));
515- }
516-
517- // / This method replaces the uses of the results of `op` with the values in
518- // / `newValues` when a use is nested within the given `block`. The number of
519- // / values in `newValues` is required to match the number of results of `op`.
520- // / If all uses of this operation are replaced, the operation is erased.
521- void replaceOpWithinBlock (Operation *op, ValueRange newValues, Block *block,
522- bool *allUsesReplaced = nullptr );
523-
524- // / This method replaces the results of the operation with the specified list
525- // / of values. The number of provided values must match the number of results
526- // / of the operation. The replaced op is erased.
500+ // / Replace the results of the given (original) operation with the specified
501+ // / list of values (replacements). The result types of the given op and the
502+ // / replacements must match. The original op is erased.
527503 virtual void replaceOp (Operation *op, ValueRange newValues);
528504
529- // / This method replaces the results of the operation with the specified
530- // / new op (replacement). The number of results of the two operations must
531- // / match. The replaced op is erased.
505+ // / Replace the results of the given (original) operation with the specified
506+ // / new op (replacement). The result types of the two ops must match. The
507+ // / original op is erased.
532508 virtual void replaceOp (Operation *op, Operation *newOp);
533509
534- // / Replaces the result op with a new op that is created without verification.
535- // / The result values of the two ops must be the same types.
510+ // / Replace the results of the given (original) op with a new op that is
511+ // / created without verification (replacement). The result values of the two
512+ // / ops must match. The original op is erased.
536513 template <typename OpTy, typename ... Args>
537514 OpTy replaceOpWithNewOp (Operation *op, Args &&...args) {
538515 auto newOp = create<OpTy>(op->getLoc (), std::forward<Args>(args)...);
@@ -634,9 +611,8 @@ class RewriterBase : public OpBuilder {
634611 finalizeOpModification (root);
635612 }
636613
637- // / Find uses of `from` and replace them with `to`. It also marks every
638- // / modified uses and notifies the rewriter that an in-place operation
639- // / modification is about to happen.
614+ // / Find uses of `from` and replace them with `to`. Also notify the listener
615+ // / about every in-place op modification (for every use that was replaced).
640616 void replaceAllUsesWith (Value from, Value to) {
641617 return replaceAllUsesWith (from.getImpl (), to);
642618 }
@@ -652,30 +628,51 @@ class RewriterBase : public OpBuilder {
652628 for (auto it : llvm::zip (from, to))
653629 replaceAllUsesWith (std::get<0 >(it), std::get<1 >(it));
654630 }
631+ void replaceAllUsesWith (Operation *from, ValueRange to) {
632+ replaceAllUsesWith (from->getResults (), to);
633+ }
655634
656635 // / Find uses of `from` and replace them with `to` if the `functor` returns
657- // / true. It also marks every modified uses and notifies the rewriter that an
658- // / in-place operation modification is about to happen.
636+ // / true. Also notify the listener about every in-place op modification (for
637+ // / every use that was replaced). The optional `allUsesReplaced` flag is set
638+ // / to "true" if all uses were replaced.
659639 void replaceUsesWithIf (Value from, Value to,
660- function_ref<bool (OpOperand &)> functor);
640+ function_ref<bool (OpOperand &)> functor,
641+ bool *allUsesReplaced = nullptr);
661642 void replaceUsesWithIf (ValueRange from, ValueRange to,
662- function_ref<bool (OpOperand &)> functor) {
663- assert (from.size () == to.size () && " incorrect number of replacements" );
664- for (auto it : llvm::zip (from, to))
665- replaceUsesWithIf (std::get<0 >(it), std::get<1 >(it), functor);
643+ function_ref<bool (OpOperand &)> functor,
644+ bool *allUsesReplaced = nullptr);
645+ void replaceUsesWithIf (Operation *from, ValueRange to,
646+ function_ref<bool (OpOperand &)> functor,
647+ bool *allUsesReplaced = nullptr) {
648+ replaceUsesWithIf (from->getResults (), to, functor, allUsesReplaced);
649+ }
650+
651+ // / Find uses of `from` within `block` and replace them with `to`. Also notify
652+ // / the listener about every in-place op modification (for every use that was
653+ // / replaced). The optional `allUsesReplaced` flag is set to "true" if all
654+ // / uses were replaced.
655+ void replaceUsesWithinBlock (Operation *op, ValueRange newValues, Block *block,
656+ bool *allUsesReplaced = nullptr ) {
657+ replaceUsesWithIf (
658+ op, newValues,
659+ [block](OpOperand &use) {
660+ return block->getParentOp ()->isProperAncestor (use.getOwner ());
661+ },
662+ allUsesReplaced);
666663 }
667664
668665 // / Find uses of `from` and replace them with `to` except if the user is
669- // / `exceptedUser`. It also marks every modified uses and notifies the
670- // / rewriter that an in-place operation modification is about to happen .
666+ // / `exceptedUser`. Also notify the listener about every in-place op
667+ // / modification (for every use that was replaced) .
671668 void replaceAllUsesExcept (Value from, Value to, Operation *exceptedUser) {
672669 return replaceUsesWithIf (from, to, [&](OpOperand &use) {
673670 Operation *user = use.getOwner ();
674671 return user != exceptedUser;
675672 });
676673 }
677674
678- // / Used to notify the rewriter that the IR failed to be rewritten because of
675+ // / Used to notify the listener that the IR failed to be rewritten because of
679676 // / a match failure, and provide a callback to populate a diagnostic with the
680677 // / reason why the failure occurred. This method allows for derived rewriters
681678 // / to optionally hook into the reason why a rewrite failed, and display it to
0 commit comments