Skip to content

Commit

Permalink
[TableGen][GlobalISel] Refactor optimizeRules related bit to allow co…
Browse files Browse the repository at this point in the history
…de reuse

In theory, reapplying optimizeRules on each group matchers should give
us a second nesting level on the matching table. In practice, we need
more work to make that happen because all the predicates are actually
not directly available through the predicate matchers list.

NFC.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321025 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Quentin Colombet committed Dec 18, 2017
1 parent b198292 commit ebf7d45
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions utils/TableGen/GlobalISelEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ class Matcher {
public:
virtual ~Matcher() = default;
virtual void emit(MatchTable &Table) = 0;
virtual std::unique_ptr<PredicateMatcher> forgetFirstCondition() = 0;
};

class GroupMatcher : public Matcher {
Expand All @@ -595,6 +596,15 @@ class GroupMatcher : public Matcher {
Rules.clear();
}
void emit(MatchTable &Table) override;

std::unique_ptr<PredicateMatcher> forgetFirstCondition() override {
// We shouldn't need to mess up with groups, since we
// should have merged everything shareable upfront.
// If we start to look into reordering predicates,
// we may want to reconsider this.
assert(0 && "Groups should be formed maximal for now");
llvm_unreachable("No need for this for now");
}
};

/// Generates code to check that a match rule matches.
Expand Down Expand Up @@ -749,7 +759,7 @@ class RuleMatcher : public Matcher {
/// matcher.
unsigned countRendererFns() const;

std::unique_ptr<PredicateMatcher> forgetFirstCondition();
std::unique_ptr<PredicateMatcher> forgetFirstCondition() override;

// FIXME: Remove this as soon as possible
InstructionMatcher &insnmatchers_front() const { return *Matchers.front(); }
Expand Down Expand Up @@ -2615,7 +2625,7 @@ class GlobalISelEmitter {
/// # predicate C
/// \endverbatim
std::vector<Matcher *> optimizeRules(
std::vector<RuleMatcher> &Rules,
const std::vector<Matcher *> &Rules,
std::vector<std::unique_ptr<GroupMatcher>> &StorageGroupMatcher);
};

Expand Down Expand Up @@ -3552,15 +3562,15 @@ void GlobalISelEmitter::emitImmPredicates(
}

std::vector<Matcher *> GlobalISelEmitter::optimizeRules(
std::vector<RuleMatcher> &Rules,
const std::vector<Matcher *> &Rules,
std::vector<std::unique_ptr<GroupMatcher>> &StorageGroupMatcher) {
std::vector<Matcher *> OptRules;
// Start with a stupid grouping for now.
std::unique_ptr<GroupMatcher> CurrentGroup = make_unique<GroupMatcher>();
assert(CurrentGroup->conditions_empty());
unsigned NbGroup = 0;
for (RuleMatcher &Rule : Rules) {
std::unique_ptr<PredicateMatcher> Predicate = Rule.forgetFirstCondition();
for (Matcher *Rule : Rules) {
std::unique_ptr<PredicateMatcher> Predicate = Rule->forgetFirstCondition();
if (!CurrentGroup->conditions_empty() &&
!CurrentGroup->lastConditionMatches(*Predicate)) {
// Start a new group.
Expand All @@ -3572,7 +3582,7 @@ std::vector<Matcher *> GlobalISelEmitter::optimizeRules(
}
if (CurrentGroup->conditions_empty())
CurrentGroup->addCondition(std::move(Predicate));
CurrentGroup->addRule(Rule);
CurrentGroup->addRule(*Rule);
}
if (!CurrentGroup->conditions_empty()) {
++NbGroup;
Expand Down Expand Up @@ -3823,12 +3833,13 @@ void GlobalISelEmitter::run(raw_ostream &OS) {
});
std::vector<std::unique_ptr<GroupMatcher>> StorageGroupMatcher;

std::vector<Matcher *> OptRules;
if (OptimizeMatchTable)
OptRules = optimizeRules(Rules, StorageGroupMatcher);
else
for (Matcher &Rule : Rules)
OptRules.push_back(&Rule);
std::vector<Matcher *> InputRules;
for (Matcher &Rule : Rules)
InputRules.push_back(&Rule);

std::vector<Matcher *> OptRules =
OptimizeMatchTable ? optimizeRules(InputRules, StorageGroupMatcher)
: InputRules;

MatchTable Table(0);
for (Matcher *Rule : OptRules) {
Expand Down

0 comments on commit ebf7d45

Please sign in to comment.