implement a simple proof-of-concept optimization for
[oota-llvm.git] / utils / TableGen / DAGISelMatcherOpt.cpp
1 //===- 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"
15 using namespace llvm;
16
17
18 static 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
44 MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
45   OwningPtr<MatcherNode> MatcherPtr(Matcher);
46   FormRecordChildNodes(MatcherPtr);
47   return MatcherPtr.take();
48 }