408bd63e33140bb678ac155c64166f13636a9440
[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 static void ContractNodes(OwningPtr<MatcherNode> &Matcher) {
18   // If we reached the end of the chain, we're done.
19   MatcherNode *N = Matcher.get();
20   if (N == 0) return;
21   
22   // If we have a push node, walk down both edges.
23   if (PushMatcherNode *Push = dyn_cast<PushMatcherNode>(N))
24     ContractNodes(Push->getFailurePtr());
25   
26   // If we found a movechild node with a node that comes in a 'foochild' form,
27   // transform it.
28   if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
29     if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext())) {
30       MatcherNode *New
31         = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
32       New->setNext(Matcher.take());
33       Matcher.reset(New);
34       MC->setNext(RM->takeNext());
35       return ContractNodes(Matcher);
36     }
37   }
38   
39   if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N))
40     if (MoveParentMatcherNode *MP = 
41           dyn_cast<MoveParentMatcherNode>(MC->getNext())) {
42       Matcher.reset(MP->takeNext());
43       return ContractNodes(Matcher);
44     }
45   
46   ContractNodes(N->getNextPtr());
47 }
48
49
50 MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
51   OwningPtr<MatcherNode> MatcherPtr(Matcher);
52   ContractNodes(MatcherPtr);
53   return MatcherPtr.take();
54 }