Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions llvm/utils/TableGen/DAGISelMatcherOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,34 +191,10 @@ static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
return nullptr;
}

/// FactorNodes - Turn matches like this:
/// Scope
/// OPC_CheckType i32
/// ABC
/// OPC_CheckType i32
/// XYZ
/// into:
/// OPC_CheckType i32
/// Scope
/// ABC
/// XYZ
///
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
// Look for a push node. Iterates instead of recurses to reduce stack usage.
ScopeMatcher *Scope = nullptr;
std::unique_ptr<Matcher> *RebindableMatcherPtr = &InputMatcherPtr;
while (!Scope) {
// If we reached the end of the chain, we're done.
Matcher *N = RebindableMatcherPtr->get();
if (!N)
return;

// If this is not a push node, just scan for one.
Scope = dyn_cast<ScopeMatcher>(N);
if (!Scope)
RebindableMatcherPtr = &(N->getNextPtr());
}
std::unique_ptr<Matcher> &MatcherPtr = *RebindableMatcherPtr;
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr);

static void FactorScope(std::unique_ptr<Matcher> &MatcherPtr) {
ScopeMatcher *Scope = cast<ScopeMatcher>(MatcherPtr.get());

// Okay, pull together the children of the scope node into a vector so we can
// inspect it more easily.
Expand Down Expand Up @@ -353,7 +329,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
Shared->setNext(new ScopeMatcher(std::move(EqualMatchers)));

// Recursively factor the newly created node.
FactorNodes(Shared->getNextPtr());
FactorScope(Shared->getNextPtr());
}

// Put the new Matcher where we started in OptionsToMatch.
Expand Down Expand Up @@ -470,7 +446,7 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
for (auto &M : Cases) {
if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M.second)) {
std::unique_ptr<Matcher> Scope(SM);
FactorNodes(Scope);
FactorScope(Scope);
M.second = Scope.release();
assert(M.second && "null matcher");
}
Expand All @@ -492,6 +468,31 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
Scope->resetChild(i, OptionsToMatch[i]);
}

/// FactorNodes - Turn matches like this:
/// Scope
/// OPC_CheckType i32
/// ABC
/// OPC_CheckType i32
/// XYZ
/// into:
/// OPC_CheckType i32
/// Scope
/// ABC
/// XYZ
///
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this is referencing unique_ptr at all. This could be done in terms of Matcher* or Matcher&

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change the contents of the unique_ptr by calling unique_ptr::reset in several places. It replaces the ScopeMatcher with a SwitchOpcodeMatcher/SwitchType matcher by changing what the owner of ScopeMatcher is pointing to. This requires being able to modify the unique_ptr inside the owner.

// Look for a scope matcher. Iterates instead of recurses to reduce stack
// usage.
std::unique_ptr<Matcher> *MatcherPtr = &InputMatcherPtr;
do {
if (isa<ScopeMatcher>(*MatcherPtr))
return FactorScope(*MatcherPtr);

// If this is not a scope matcher, go to the next node.
MatcherPtr = &(MatcherPtr->get()->getNextPtr());
} while (MatcherPtr->get());
}

void llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
const CodeGenDAGPatterns &CGP) {
ContractNodes(MatcherPtr, CGP);
Expand Down