Skip to content

Commit 86561b8

Browse files
committed
RequirementMachine: Generalize RewritePath::replaceRuleWithPath() to replaceRulesWithPaths()
1 parent f810d3f commit 86561b8

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ RewritePath RewritePath::splitCycleAtRule(unsigned ruleID) const {
248248
///
249249
/// Returns true if any rewrite steps were replaced; false means the
250250
/// 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) {
253253
bool foundAny = false;
254254

255255
for (const auto &step : Steps) {
256256
if (step.Kind == RewriteStep::Rule &&
257-
step.getRuleID() == ruleID) {
257+
fn(step.getRuleID()) != nullptr) {
258258
foundAny = true;
259259
break;
260260
}
@@ -268,13 +268,13 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
268268
for (const auto &step : Steps) {
269269
switch (step.Kind) {
270270
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) {
273273
newSteps.push_back(step);
274274
break;
275275
}
276276

277-
// Ok, we found a rewrite step referencing the redundant rule.
277+
// Ok, we found a rewrite step referencing a redundant rule.
278278
// Replace this step with the provided path. If this rewrite step has
279279
// context, the path's own steps must be re-contextualized.
280280

@@ -305,10 +305,10 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
305305

306306
// If this rewrite step is inverted, invert the entire path.
307307
if (step.Inverse) {
308-
for (auto newStep : llvm::reverse(path))
308+
for (auto newStep : llvm::reverse(*replacementPath))
309309
recontextualizeStep(newStep);
310310
} else {
311-
for (auto newStep : path)
311+
for (auto newStep : *replacementPath)
312312
recontextualizeStep(newStep);
313313
}
314314

@@ -333,6 +333,17 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
333333
return true;
334334
}
335335

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+
336347
SmallVector<unsigned, 1>
337348
RewritePath::getRulesInEmptyContext(const MutableTerm &term,
338349
const RewriteSystem &system) {

lib/AST/RequirementMachine/RewriteLoop.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ class RewritePath {
407407

408408
RewritePath splitCycleAtRule(unsigned ruleID) const;
409409

410+
bool replaceRulesWithPaths(llvm::function_ref<const RewritePath *(unsigned)> fn);
411+
410412
bool replaceRuleWithPath(unsigned ruleID, const RewritePath &path);
411413

412414
SmallVector<unsigned, 1> getRulesInEmptyContext(const MutableTerm &term,

0 commit comments

Comments
 (0)