blob: 796b815a93b5b09b190d2b93ef852445369d2b3c [file] [log] [blame]
Chris Lattner91c6a822010-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 Lattner22c48b32010-02-24 19:52:48 +000017static void ContractNodes(OwningPtr<MatcherNode> &Matcher) {
Chris Lattner19b5a752010-02-24 07:31:45 +000018 // 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))
Chris Lattner22c48b32010-02-24 19:52:48 +000024 ContractNodes(Push->getFailurePtr());
Chris Lattner19b5a752010-02-24 07:31:45 +000025
Chris Lattner22c48b32010-02-24 19:52:48 +000026 // If we found a movechild node with a node that comes in a 'foochild' form,
27 // transform it.
Chris Lattner19b5a752010-02-24 07:31:45 +000028 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
Chris Lattner23cfda72010-02-24 20:15:25 +000029 MatcherNode *New = 0;
30 if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext()))
31 New = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
32
33 if (CheckTypeMatcherNode *CT= dyn_cast<CheckTypeMatcherNode>(MC->getNext()))
34 New = new CheckChildTypeMatcherNode(MC->getChildNo(), CT->getType());
35
36 if (New) {
37 // Insert the new node.
Chris Lattner22c48b32010-02-24 19:52:48 +000038 New->setNext(Matcher.take());
39 Matcher.reset(New);
Chris Lattner23cfda72010-02-24 20:15:25 +000040 // Remove the old one.
41 MC->setNext(MC->getNext()->takeNext());
Chris Lattner22c48b32010-02-24 19:52:48 +000042 return ContractNodes(Matcher);
43 }
Chris Lattner19b5a752010-02-24 07:31:45 +000044 }
Chris Lattner22c48b32010-02-24 19:52:48 +000045
46 if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N))
47 if (MoveParentMatcherNode *MP =
48 dyn_cast<MoveParentMatcherNode>(MC->getNext())) {
49 Matcher.reset(MP->takeNext());
50 return ContractNodes(Matcher);
51 }
52
53 ContractNodes(N->getNextPtr());
Chris Lattner19b5a752010-02-24 07:31:45 +000054}
55
56
57MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
58 OwningPtr<MatcherNode> MatcherPtr(Matcher);
Chris Lattner22c48b32010-02-24 19:52:48 +000059 ContractNodes(MatcherPtr);
Chris Lattner19b5a752010-02-24 07:31:45 +000060 return MatcherPtr.take();
Chris Lattner91c6a822010-02-24 07:06:50 +000061}