@@ -248,13 +248,13 @@ RewritePath RewritePath::splitCycleAtRule(unsigned ruleID) const {
248
248
// /
249
249
// / Returns true if any rewrite steps were replaced; false means the
250
250
// / rule did not appear in this path.
251
- bool RewritePath::replaceRuleWithPath ( unsigned ruleID,
252
- const RewritePath &path ) {
251
+ bool RewritePath::replaceRulesWithPaths (
252
+ llvm::function_ref< const RewritePath *( unsigned )> fn ) {
253
253
bool foundAny = false ;
254
254
255
255
for (const auto &step : Steps) {
256
256
if (step.Kind == RewriteStep::Rule &&
257
- step.getRuleID () == ruleID ) {
257
+ fn ( step.getRuleID ()) != nullptr ) {
258
258
foundAny = true ;
259
259
break ;
260
260
}
@@ -268,13 +268,13 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
268
268
for (const auto &step : Steps) {
269
269
switch (step.Kind ) {
270
270
case RewriteStep::Rule: {
271
- // All other rewrite rules remain unchanged.
272
- if (step. getRuleID () != ruleID ) {
271
+ auto *replacementPath = fn (step. getRuleID ());
272
+ if (replacementPath == nullptr ) {
273
273
newSteps.push_back (step);
274
274
break ;
275
275
}
276
276
277
- // Ok, we found a rewrite step referencing the redundant rule.
277
+ // Ok, we found a rewrite step referencing a redundant rule.
278
278
// Replace this step with the provided path. If this rewrite step has
279
279
// context, the path's own steps must be re-contextualized.
280
280
@@ -305,10 +305,10 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
305
305
306
306
// If this rewrite step is inverted, invert the entire path.
307
307
if (step.Inverse ) {
308
- for (auto newStep : llvm::reverse (path ))
308
+ for (auto newStep : llvm::reverse (*replacementPath ))
309
309
recontextualizeStep (newStep);
310
310
} else {
311
- for (auto newStep : path )
311
+ for (auto newStep : *replacementPath )
312
312
recontextualizeStep (newStep);
313
313
}
314
314
@@ -333,6 +333,17 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
333
333
return true ;
334
334
}
335
335
336
+ bool RewritePath::replaceRuleWithPath (unsigned ruleID,
337
+ const RewritePath &path) {
338
+ return replaceRulesWithPaths (
339
+ [&](unsigned otherRuleID) -> const RewritePath * {
340
+ if (ruleID == otherRuleID)
341
+ return &path;
342
+
343
+ return nullptr ;
344
+ });
345
+ }
346
+
336
347
SmallVector<unsigned , 1 >
337
348
RewritePath::getRulesInEmptyContext (const MutableTerm &term,
338
349
const RewriteSystem &system) {
0 commit comments