aboutsummaryrefslogtreecommitdiffstats
path: root/utils/TableGen/DAGISelMatcherOpt.cpp
blob: 796b815a93b5b09b190d2b93ef852445369d2b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//===- DAGISelMatcherOpt.cpp - Optimize a DAG Matcher ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the DAG Matcher optimizer.
//
//===----------------------------------------------------------------------===//

#include "DAGISelMatcher.h"
using namespace llvm;

static void ContractNodes(OwningPtr<MatcherNode> &Matcher) {
  // If we reached the end of the chain, we're done.
  MatcherNode *N = Matcher.get();
  if (N == 0) return;
  
  // If we have a push node, walk down both edges.
  if (PushMatcherNode *Push = dyn_cast<PushMatcherNode>(N))
    ContractNodes(Push->getFailurePtr());
  
  // If we found a movechild node with a node that comes in a 'foochild' form,
  // transform it.
  if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N)) {
    MatcherNode *New = 0;
    if (RecordMatcherNode *RM = dyn_cast<RecordMatcherNode>(MC->getNext()))
      New = new RecordChildMatcherNode(MC->getChildNo(), RM->getWhatFor());
    
    if (CheckTypeMatcherNode *CT= dyn_cast<CheckTypeMatcherNode>(MC->getNext()))
      New = new CheckChildTypeMatcherNode(MC->getChildNo(), CT->getType());
    
    if (New) {
      // Insert the new node.
      New->setNext(Matcher.take());
      Matcher.reset(New);
      // Remove the old one.
      MC->setNext(MC->getNext()->takeNext());
      return ContractNodes(Matcher);
    }
  }
  
  if (MoveChildMatcherNode *MC = dyn_cast<MoveChildMatcherNode>(N))
    if (MoveParentMatcherNode *MP = 
          dyn_cast<MoveParentMatcherNode>(MC->getNext())) {
      Matcher.reset(MP->takeNext());
      return ContractNodes(Matcher);
    }
  
  ContractNodes(N->getNextPtr());
}


MatcherNode *llvm::OptimizeMatcher(MatcherNode *Matcher) {
  OwningPtr<MatcherNode> MatcherPtr(Matcher);
  ContractNodes(MatcherPtr);
  return MatcherPtr.take();
}