blob: d4796e91bf37898cb08d6dbd2d187388b57c04af [file] [log] [blame]
Chris Lattner36e646c2010-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"
Chris Lattnercd632fb2010-02-25 07:45:24 +000015#include "llvm/ADT/DenseMap.h"
16#include <vector>
Chris Lattner36e646c2010-02-24 07:06:50 +000017using namespace llvm;
18
Chris Lattner9a515172010-02-25 02:04:40 +000019static void ContractNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattner3ee1bc42010-02-24 07:31:45 +000020 // If we reached the end of the chain, we're done.
Chris Lattner9a515172010-02-25 02:04:40 +000021 Matcher *N = MatcherPtr.get();
Chris Lattner3ee1bc42010-02-24 07:31:45 +000022 if (N == 0) return;
23
Chris Lattner42363662010-02-25 19:00:39 +000024 // If we have a scope node, walk down all of the children.
25 if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
26 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
27 OwningPtr<Matcher> Child(Scope->takeChild(i));
28 ContractNodes(Child);
29 Scope->resetChild(i, Child.take());
30 }
31 return;
32 }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000033
Chris Lattner9feb1e32010-02-24 19:52:48 +000034 // If we found a movechild node with a node that comes in a 'foochild' form,
35 // transform it.
Chris Lattner9a515172010-02-25 02:04:40 +000036 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
37 Matcher *New = 0;
38 if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
39 New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor());
Chris Lattner3c664b32010-02-24 20:15:25 +000040
Chris Lattner9a515172010-02-25 02:04:40 +000041 if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext()))
42 New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
Chris Lattner3c664b32010-02-24 20:15:25 +000043
44 if (New) {
45 // Insert the new node.
Chris Lattnerac10e4f2010-02-25 01:56:48 +000046 New->setNext(MatcherPtr.take());
47 MatcherPtr.reset(New);
Chris Lattner3c664b32010-02-24 20:15:25 +000048 // Remove the old one.
49 MC->setNext(MC->getNext()->takeNext());
Chris Lattnerac10e4f2010-02-25 01:56:48 +000050 return ContractNodes(MatcherPtr);
Chris Lattner9feb1e32010-02-24 19:52:48 +000051 }
Chris Lattner3ee1bc42010-02-24 07:31:45 +000052 }
Chris Lattner9feb1e32010-02-24 19:52:48 +000053
Chris Lattner9a515172010-02-25 02:04:40 +000054 if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
55 if (MoveParentMatcher *MP =
56 dyn_cast<MoveParentMatcher>(MC->getNext())) {
Chris Lattnerac10e4f2010-02-25 01:56:48 +000057 MatcherPtr.reset(MP->takeNext());
58 return ContractNodes(MatcherPtr);
Chris Lattner9feb1e32010-02-24 19:52:48 +000059 }
60
61 ContractNodes(N->getNextPtr());
Chris Lattner3ee1bc42010-02-24 07:31:45 +000062}
63
Chris Lattner9a515172010-02-25 02:04:40 +000064static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
Chris Lattner0acf2ba2010-02-25 01:57:41 +000065 // If we reached the end of the chain, we're done.
Chris Lattner9a515172010-02-25 02:04:40 +000066 Matcher *N = MatcherPtr.get();
Chris Lattner0acf2ba2010-02-25 01:57:41 +000067 if (N == 0) return;
68
69 // If this is not a push node, just scan for one.
Chris Lattner42363662010-02-25 19:00:39 +000070 ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
71 if (Scope == 0)
Chris Lattner0acf2ba2010-02-25 01:57:41 +000072 return FactorNodes(N->getNextPtr());
73
Chris Lattner42363662010-02-25 19:00:39 +000074 // Okay, pull together the children of the scope node into a vector so we can
Chris Lattnercd632fb2010-02-25 07:45:24 +000075 // inspect it more easily. While we're at it, bucket them up by the hash
76 // code of their first predicate.
Chris Lattner9a515172010-02-25 02:04:40 +000077 SmallVector<Matcher*, 32> OptionsToMatch;
Chris Lattnercd632fb2010-02-25 07:45:24 +000078 typedef DenseMap<unsigned, std::vector<Matcher*> > HashTableTy;
79 HashTableTy MatchersByHash;
Chris Lattner0acf2ba2010-02-25 01:57:41 +000080
Chris Lattner42363662010-02-25 19:00:39 +000081 for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
Chris Lattnercd632fb2010-02-25 07:45:24 +000082 // Factor the subexpression.
Chris Lattner42363662010-02-25 19:00:39 +000083 OwningPtr<Matcher> Child(Scope->takeChild(i));
84 FactorNodes(Child);
85
Chris Lattnerb0e68102010-02-26 07:36:37 +000086 if (Matcher *N = Child.take()) {
Chris Lattner42363662010-02-25 19:00:39 +000087 OptionsToMatch.push_back(N);
88 MatchersByHash[N->getHash()].push_back(N);
Chris Lattnercd632fb2010-02-25 07:45:24 +000089 }
90 }
Chris Lattner0acf2ba2010-02-25 01:57:41 +000091
Chris Lattnercd632fb2010-02-25 07:45:24 +000092 SmallVector<Matcher*, 32> NewOptionsToMatch;
93
94 // Now that we have bucketed up things by hash code, iterate over sets of
95 // matchers that all start with the same node. We would like to iterate over
96 // the hash table, but it isn't in deterministic order, emulate this by going
97 // about this slightly backwards. After each set of nodes is processed, we
98 // remove them from MatchersByHash.
99 for (unsigned i = 0, e = OptionsToMatch.size();
100 i != e && !MatchersByHash.empty(); ++i) {
101 // Find the set of matchers that start with this node.
102 Matcher *Optn = OptionsToMatch[i];
103
104 // Find all nodes that hash to the same value. If there is no entry in the
105 // hash table, then we must have previously processed a node equal to this
106 // one.
107 HashTableTy::iterator DMI = MatchersByHash.find(Optn->getHash());
Chris Lattnerb0e68102010-02-26 07:36:37 +0000108 if (DMI == MatchersByHash.end()) {
109 delete Optn;
110 continue;
111 }
Chris Lattnercd632fb2010-02-25 07:45:24 +0000112
113 std::vector<Matcher*> &HashMembers = DMI->second;
114 assert(!HashMembers.empty() && "Should be removed if empty");
115
116 // Check to see if this node is in HashMembers, if not it was equal to a
117 // previous node and removed.
118 std::vector<Matcher*>::iterator MemberSlot =
119 std::find(HashMembers.begin(), HashMembers.end(), Optn);
Chris Lattnerb0e68102010-02-26 07:36:37 +0000120 if (MemberSlot == HashMembers.end()) {
121 delete Optn;
122 continue;
123 }
Chris Lattnercd632fb2010-02-25 07:45:24 +0000124
125 // If the node *does* exist in HashMembers, then we've confirmed that it
126 // hasn't been processed as equal to a previous node. Process it now, start
127 // by removing it from the list of hash-equal nodes.
128 HashMembers.erase(MemberSlot);
129
130 // Scan all of the hash members looking for ones that are equal, removing
131 // them from HashMembers, adding them to EqualMatchers.
132 SmallVector<Matcher*, 8> EqualMatchers;
133
134 // Scan the vector backwards so we're generally removing from the end to
135 // avoid pointless data copying.
136 for (unsigned i = HashMembers.size(); i != 0; --i) {
137 if (!HashMembers[i-1]->isEqual(Optn)) continue;
138
139 EqualMatchers.push_back(HashMembers[i-1]);
140 HashMembers.erase(HashMembers.begin()+i-1);
141 }
142 EqualMatchers.push_back(Optn);
143
144 // Reverse the vector so that we preserve the match ordering.
145 std::reverse(EqualMatchers.begin(), EqualMatchers.end());
146
147 // If HashMembers is empty at this point, then we've gotten all nodes with
148 // the same hash, nuke the entry in the hash table.
149 if (HashMembers.empty())
150 MatchersByHash.erase(Optn->getHash());
151
152 // Okay, we have the list of all matchers that start with the same node as
153 // Optn. If there is more than one in the set, we want to factor them.
154 if (EqualMatchers.size() == 1) {
155 NewOptionsToMatch.push_back(Optn);
156 continue;
157 }
158
159 // Factor these checks by pulling the first node off each entry and
Chris Lattnerb0e68102010-02-26 07:36:37 +0000160 // discarding it. Take the first one off the first entry to reuse.
161 Matcher *Shared = Optn;
162 Optn = Optn->takeNext();
163 EqualMatchers[0] = Optn;
164
165 // Skip the first node. Leave the first node around though, we'll delete it
166 // on subsequent iterations over OptionsToMatch.
167 for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i)
168 EqualMatchers[i] = EqualMatchers[i]->takeNext();
Chris Lattnercd632fb2010-02-25 07:45:24 +0000169
Chris Lattnerb0e68102010-02-26 07:36:37 +0000170 Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size()));
171
172 // Recursively factor the newly created node.
173 FactorNodes(Shared->getNextPtr());
174
175 NewOptionsToMatch.push_back(Shared);
Chris Lattnercd632fb2010-02-25 07:45:24 +0000176 }
177
178 // Reassemble a new Scope node.
Chris Lattnerb0e68102010-02-26 07:36:37 +0000179 assert(!NewOptionsToMatch.empty() && "where'd all our children go?");
180 if (NewOptionsToMatch.size() == 1)
181 MatcherPtr.reset(NewOptionsToMatch[0]);
182 else {
183 Scope->setNumChildren(NewOptionsToMatch.size());
184 for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
185 Scope->resetChild(i, NewOptionsToMatch[i]);
186 }
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000187}
188
Chris Lattner9a515172010-02-25 02:04:40 +0000189Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) {
190 OwningPtr<Matcher> MatcherPtr(TheMatcher);
Chris Lattner9feb1e32010-02-24 19:52:48 +0000191 ContractNodes(MatcherPtr);
Chris Lattner0acf2ba2010-02-25 01:57:41 +0000192 FactorNodes(MatcherPtr);
Chris Lattner3ee1bc42010-02-24 07:31:45 +0000193 return MatcherPtr.take();
Chris Lattner36e646c2010-02-24 07:06:50 +0000194}