Chris Lattner | 91c6a82 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 17 | static void ContractNodes(OwningPtr<MatcherNode> &Matcher) { |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 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)) |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 24 | ContractNodes(Push->getFailurePtr()); |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 26 | // If we found a movechild node with a node that comes in a 'foochild' form, |
| 27 | // transform it. |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 28 | if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) { |
Chris Lattner | 23cfda7 | 2010-02-24 20:15:25 +0000 | [diff] [blame^] | 29 | 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 Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 38 | New->setNext(Matcher.take()); |
| 39 | Matcher.reset(New); |
Chris Lattner | 23cfda7 | 2010-02-24 20:15:25 +0000 | [diff] [blame^] | 40 | // Remove the old one. |
| 41 | MC->setNext(MC->getNext()->takeNext()); |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 42 | return ContractNodes(Matcher); |
| 43 | } |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 45 | |
| 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 Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) { |
| 58 | OwningPtr<MatcherNode> MatcherPtr(Matcher); |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 59 | ContractNodes(MatcherPtr); |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 60 | return MatcherPtr.take(); |
Chris Lattner | 91c6a82 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 61 | } |