2010-02-24 15:06:50 +08:00
|
|
|
//===- DAGISelMatcherOpt.cpp - Optimize a DAG Matcher ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the DAG Matcher optimizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DAGISelMatcher.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-02-25 09:56:48 +08:00
|
|
|
static void ContractNodes(OwningPtr<MatcherNode> &MatcherPtr) {
|
2010-02-24 15:31:45 +08:00
|
|
|
// If we reached the end of the chain, we're done.
|
2010-02-25 09:56:48 +08:00
|
|
|
MatcherNode *N = MatcherPtr.get();
|
2010-02-24 15:31:45 +08:00
|
|
|
if (N == 0) return;
|
|
|
|
|
2010-02-25 09:56:48 +08:00
|
|
|
// If we have a scope node, walk down both edges.
|
|
|
|
if (ScopeMatcherNode *Push = dyn_cast<ScopeMatcherNode>(N))
|
|
|
|
ContractNodes(Push->getCheckPtr());
|
2010-02-24 15:31:45 +08:00
|
|
|
|
2010-02-25 03:52:48 +08:00
|
|
|
// If we found a movechild node with a node that comes in a 'foochild' form,
|
|
|
|
// transform it.
|
2010-02-24 15:31:45 +08:00
|
|
|
if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
|
2010-02-25 04:15:25 +08:00
|
|
|
MatcherNode *New = 0;
|
|
|
|
if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext()))
|
|
|
|
New = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
|
|
|
|
|
|
|
|
if (CheckTypeMatcherNode *CT= dyn_cast<CheckTypeMatcherNode>(MC->getNext()))
|
|
|
|
New = new CheckChildTypeMatcherNode(MC->getChildNo(), CT->getType());
|
|
|
|
|
|
|
|
if (New) {
|
|
|
|
// Insert the new node.
|
2010-02-25 09:56:48 +08:00
|
|
|
New->setNext(MatcherPtr.take());
|
|
|
|
MatcherPtr.reset(New);
|
2010-02-25 04:15:25 +08:00
|
|
|
// Remove the old one.
|
|
|
|
MC->setNext(MC->getNext()->takeNext());
|
2010-02-25 09:56:48 +08:00
|
|
|
return ContractNodes(MatcherPtr);
|
2010-02-25 03:52:48 +08:00
|
|
|
}
|
2010-02-24 15:31:45 +08:00
|
|
|
}
|
2010-02-25 03:52:48 +08:00
|
|
|
|
|
|
|
if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N))
|
|
|
|
if (MoveParentMatcherNode *MP =
|
|
|
|
dyn_cast<MoveParentMatcherNode>(MC->getNext())) {
|
2010-02-25 09:56:48 +08:00
|
|
|
MatcherPtr.reset(MP->takeNext());
|
|
|
|
return ContractNodes(MatcherPtr);
|
2010-02-25 03:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ContractNodes(N->getNextPtr());
|
2010-02-24 15:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
|
|
|
|
OwningPtr<MatcherNode> MatcherPtr(Matcher);
|
2010-02-25 03:52:48 +08:00
|
|
|
ContractNodes(MatcherPtr);
|
2010-02-24 15:31:45 +08:00
|
|
|
return MatcherPtr.take();
|
2010-02-24 15:06:50 +08:00
|
|
|
}
|