blob: d3658202a660e7b183565d99d004e10718c81724 [file] [log] [blame]
Chris Lattner36e646c2010-02-24 07:06:50 +00001//===- DAGISelMatcherOpt.cpp - Optimize a DAG Matcher ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DAG Matcher optimizer.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelMatcher.h"
15using namespace llvm;
16
Chris Lattner3ee1bc42010-02-24 07:31:45 +000017
18static void FormRecordChildNodes(OwningPtr<MatcherNode> &Matcher) {
19 // If we reached the end of the chain, we're done.
20 MatcherNode *N = Matcher.get();
21 if (N == 0) return;
22
23 // If we have a push node, walk down both edges.
24 if (PushMatcherNode *Push = dyn_cast<PushMatcherNode>(N))
25 FormRecordChildNodes(Push->getFailurePtr());
26
27 // If we found a movechild node, check to see if our pattern matches.
28 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
29 if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext()))
30 if (MoveParentMatcherNode *MP =
31 dyn_cast<MoveParentMatcherNode>(RM->getNext())) {
32 MatcherNode *New
33 = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
34 New->setNext(MP->takeNext());
35 Matcher.reset(New);
36 return FormRecordChildNodes(Matcher);
37 }
38 }
39
40 FormRecordChildNodes(N->getNextPtr());
41}
42
43
44MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
45 OwningPtr<MatcherNode> MatcherPtr(Matcher);
46 FormRecordChildNodes(MatcherPtr);
47 return MatcherPtr.take();
Chris Lattner36e646c2010-02-24 07:06:50 +000048}