Skip to content

Commit 5067bfe

Browse files
committed
RequirementMachine: Re-organize some methods in RewriteSystem.cpp
1 parent 7d2b22a commit 5067bfe

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,75 @@ void RewriteSystem::initialize(
139139
addRule(rule.first, rule.second);
140140
}
141141

142+
/// Reduce a term by applying all rewrite rules until fixed point.
143+
///
144+
/// If \p path is non-null, records the series of rewrite steps taken.
145+
bool RewriteSystem::simplify(MutableTerm &term, RewritePath *path) const {
146+
bool changed = false;
147+
148+
MutableTerm original;
149+
RewritePath subpath;
150+
151+
bool debug = false;
152+
if (Debug.contains(DebugFlags::Simplify)) {
153+
original = term;
154+
debug = true;
155+
}
156+
157+
while (true) {
158+
bool tryAgain = false;
159+
160+
auto from = term.begin();
161+
auto end = term.end();
162+
while (from < end) {
163+
auto ruleID = Trie.find(from, end);
164+
if (ruleID) {
165+
const auto &rule = getRule(*ruleID);
166+
if (!rule.isSimplified()) {
167+
auto to = from + rule.getLHS().size();
168+
assert(std::equal(from, to, rule.getLHS().begin()));
169+
170+
unsigned startOffset = (unsigned)(from - term.begin());
171+
unsigned endOffset = term.size() - rule.getLHS().size() - startOffset;
172+
173+
term.rewriteSubTerm(from, to, rule.getRHS());
174+
175+
if (path || debug) {
176+
subpath.add(RewriteStep::forRewriteRule(startOffset, endOffset, *ruleID,
177+
/*inverse=*/false));
178+
}
179+
180+
changed = true;
181+
tryAgain = true;
182+
break;
183+
}
184+
}
185+
186+
++from;
187+
}
188+
189+
if (!tryAgain)
190+
break;
191+
}
192+
193+
if (debug) {
194+
if (changed) {
195+
llvm::dbgs() << "= Simplified " << original << " to " << term << " via ";
196+
subpath.dump(llvm::dbgs(), original, *this);
197+
llvm::dbgs() << "\n";
198+
} else {
199+
llvm::dbgs() << "= Irreducible term: " << term << "\n";
200+
}
201+
}
202+
203+
if (path != nullptr) {
204+
assert(changed != subpath.empty());
205+
path->append(subpath);
206+
}
207+
208+
return changed;
209+
}
210+
142211
/// Simplify terms appearing in the substitutions of the last symbol of \p term,
143212
/// which must be a superclass or concrete type symbol.
144213
void RewriteSystem::simplifySubstitutions(MutableTerm &term,
@@ -337,75 +406,6 @@ bool RewriteSystem::addRule(MutableTerm lhs, MutableTerm rhs,
337406
return true;
338407
}
339408

340-
/// Reduce a term by applying all rewrite rules until fixed point.
341-
///
342-
/// If \p path is non-null, records the series of rewrite steps taken.
343-
bool RewriteSystem::simplify(MutableTerm &term, RewritePath *path) const {
344-
bool changed = false;
345-
346-
MutableTerm original;
347-
RewritePath subpath;
348-
349-
bool debug = false;
350-
if (Debug.contains(DebugFlags::Simplify)) {
351-
original = term;
352-
debug = true;
353-
}
354-
355-
while (true) {
356-
bool tryAgain = false;
357-
358-
auto from = term.begin();
359-
auto end = term.end();
360-
while (from < end) {
361-
auto ruleID = Trie.find(from, end);
362-
if (ruleID) {
363-
const auto &rule = getRule(*ruleID);
364-
if (!rule.isSimplified()) {
365-
auto to = from + rule.getLHS().size();
366-
assert(std::equal(from, to, rule.getLHS().begin()));
367-
368-
unsigned startOffset = (unsigned)(from - term.begin());
369-
unsigned endOffset = term.size() - rule.getLHS().size() - startOffset;
370-
371-
term.rewriteSubTerm(from, to, rule.getRHS());
372-
373-
if (path || debug) {
374-
subpath.add(RewriteStep::forRewriteRule(startOffset, endOffset, *ruleID,
375-
/*inverse=*/false));
376-
}
377-
378-
changed = true;
379-
tryAgain = true;
380-
break;
381-
}
382-
}
383-
384-
++from;
385-
}
386-
387-
if (!tryAgain)
388-
break;
389-
}
390-
391-
if (debug) {
392-
if (changed) {
393-
llvm::dbgs() << "= Simplified " << original << " to " << term << " via ";
394-
subpath.dump(llvm::dbgs(), original, *this);
395-
llvm::dbgs() << "\n";
396-
} else {
397-
llvm::dbgs() << "= Irreducible term: " << term << "\n";
398-
}
399-
}
400-
401-
if (path != nullptr) {
402-
assert(changed != subpath.empty());
403-
path->append(subpath);
404-
}
405-
406-
return changed;
407-
}
408-
409409
/// Delete any rules whose left hand sides can be reduced by other rules,
410410
/// and reduce the right hand sides of all remaining rules as much as
411411
/// possible.

0 commit comments

Comments
 (0)