Chris Lattner | 36e646c | 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 | |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "isel-opt" |
Chris Lattner | 36e646c | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 15 | #include "DAGISelMatcher.h" |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 19 | #include <vector> |
Chris Lattner | 36e646c | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | f4950d0 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 22 | /// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record' |
| 23 | /// into single compound nodes like RecordChild. |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 24 | static void ContractNodes(OwningPtr<Matcher> &MatcherPtr) { |
Chris Lattner | 3ee1bc4 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 25 | // If we reached the end of the chain, we're done. |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 26 | Matcher *N = MatcherPtr.get(); |
Chris Lattner | 3ee1bc4 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 27 | if (N == 0) return; |
| 28 | |
Chris Lattner | 4236366 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 29 | // If we have a scope node, walk down all of the children. |
| 30 | if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) { |
| 31 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
| 32 | OwningPtr<Matcher> Child(Scope->takeChild(i)); |
| 33 | ContractNodes(Child); |
| 34 | Scope->resetChild(i, Child.take()); |
| 35 | } |
| 36 | return; |
| 37 | } |
Chris Lattner | 3ee1bc4 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 9feb1e3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 39 | // If we found a movechild node with a node that comes in a 'foochild' form, |
| 40 | // transform it. |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 41 | if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) { |
| 42 | Matcher *New = 0; |
| 43 | if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext())) |
| 44 | New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor()); |
Chris Lattner | 3c664b3 | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 46 | if (CheckTypeMatcher *CT= dyn_cast<CheckTypeMatcher>(MC->getNext())) |
| 47 | New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType()); |
Chris Lattner | 3c664b3 | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 48 | |
| 49 | if (New) { |
| 50 | // Insert the new node. |
Chris Lattner | ac10e4f | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 51 | New->setNext(MatcherPtr.take()); |
| 52 | MatcherPtr.reset(New); |
Chris Lattner | 3c664b3 | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 53 | // Remove the old one. |
| 54 | MC->setNext(MC->getNext()->takeNext()); |
Chris Lattner | ac10e4f | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 55 | return ContractNodes(MatcherPtr); |
Chris Lattner | 9feb1e3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 3ee1bc4 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 9feb1e3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 59 | if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) |
| 60 | if (MoveParentMatcher *MP = |
| 61 | dyn_cast<MoveParentMatcher>(MC->getNext())) { |
Chris Lattner | ac10e4f | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 62 | MatcherPtr.reset(MP->takeNext()); |
| 63 | return ContractNodes(MatcherPtr); |
Chris Lattner | 9feb1e3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | ContractNodes(N->getNextPtr()); |
Chris Lattner | 3ee1bc4 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | f4950d0 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 69 | /// SinkPatternPredicates - Pattern predicates can be checked at any level of |
| 70 | /// the matching tree. The generator dumps them at the top level of the pattern |
| 71 | /// though, which prevents factoring from being able to see past them. This |
| 72 | /// optimization sinks them as far down into the pattern as possible. |
| 73 | /// |
| 74 | /// Conceptually, we'd like to sink these predicates all the way to the last |
| 75 | /// matcher predicate in the series. However, it turns out that some |
| 76 | /// ComplexPatterns have side effects on the graph, so we really don't want to |
| 77 | /// run a the complex pattern if the pattern predicate will fail. For this |
| 78 | /// reason, we refuse to sink the pattern predicate past a ComplexPattern. |
| 79 | /// |
| 80 | static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) { |
| 81 | // Recursively scan for a PatternPredicate. |
| 82 | // If we reached the end of the chain, we're done. |
| 83 | Matcher *N = MatcherPtr.get(); |
| 84 | if (N == 0) return; |
| 85 | |
| 86 | // Walk down all members of a scope node. |
| 87 | if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) { |
| 88 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
| 89 | OwningPtr<Matcher> Child(Scope->takeChild(i)); |
| 90 | SinkPatternPredicates(Child); |
| 91 | Scope->resetChild(i, Child.take()); |
| 92 | } |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // If this node isn't a CheckPatternPredicateMatcher we keep scanning until |
| 97 | // we find one. |
| 98 | CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N); |
| 99 | if (CPPM == 0) |
| 100 | return SinkPatternPredicates(N->getNextPtr()); |
| 101 | |
| 102 | // Ok, we found one, lets try to sink it. Check if we can sink it past the |
| 103 | // next node in the chain. If not, we won't be able to change anything and |
| 104 | // might as well bail. |
| 105 | if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate()) |
| 106 | return; |
| 107 | |
| 108 | // Okay, we know we can sink it past at least one node. Unlink it from the |
| 109 | // chain and scan for the new insertion point. |
| 110 | MatcherPtr.take(); // Don't delete CPPM. |
| 111 | MatcherPtr.reset(CPPM->takeNext()); |
| 112 | |
| 113 | N = MatcherPtr.get(); |
| 114 | while (N->getNext()->isSafeToReorderWithPatternPredicate()) |
| 115 | N = N->getNext(); |
| 116 | |
| 117 | // At this point, we want to insert CPPM after N. |
| 118 | CPPM->setNext(N->takeNext()); |
| 119 | N->setNext(CPPM); |
| 120 | } |
| 121 | |
| 122 | /// FactorNodes - Turn matches like this: |
| 123 | /// Scope |
| 124 | /// OPC_CheckType i32 |
| 125 | /// ABC |
| 126 | /// OPC_CheckType i32 |
| 127 | /// XYZ |
| 128 | /// into: |
| 129 | /// OPC_CheckType i32 |
| 130 | /// Scope |
| 131 | /// ABC |
| 132 | /// XYZ |
| 133 | /// |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 134 | static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) { |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 135 | // If we reached the end of the chain, we're done. |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 136 | Matcher *N = MatcherPtr.get(); |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 137 | if (N == 0) return; |
| 138 | |
| 139 | // If this is not a push node, just scan for one. |
Chris Lattner | 4236366 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 140 | ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N); |
| 141 | if (Scope == 0) |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 142 | return FactorNodes(N->getNextPtr()); |
| 143 | |
Chris Lattner | 4236366 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 144 | // Okay, pull together the children of the scope node into a vector so we can |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 145 | // inspect it more easily. While we're at it, bucket them up by the hash |
| 146 | // code of their first predicate. |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 147 | SmallVector<Matcher*, 32> OptionsToMatch; |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 4236366 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 149 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 150 | // Factor the subexpression. |
Chris Lattner | 4236366 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 151 | OwningPtr<Matcher> Child(Scope->takeChild(i)); |
| 152 | FactorNodes(Child); |
| 153 | |
Chris Lattner | 92a2246 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 154 | if (Matcher *N = Child.take()) |
Chris Lattner | 4236366 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 155 | OptionsToMatch.push_back(N); |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 157 | |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 158 | SmallVector<Matcher*, 32> NewOptionsToMatch; |
| 159 | |
Chris Lattner | 92a2246 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 160 | // Loop over options to match, merging neighboring patterns with identical |
| 161 | // starting nodes into a shared matcher. |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 162 | for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) { |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 163 | // Find the set of matchers that start with this node. |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 164 | Matcher *Optn = OptionsToMatch[OptionIdx++]; |
| 165 | |
| 166 | if (OptionIdx == e) { |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 167 | NewOptionsToMatch.push_back(Optn); |
| 168 | continue; |
| 169 | } |
| 170 | |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 171 | // See if the next option starts with the same matcher. If the two |
| 172 | // neighbors *do* start with the same matcher, we can factor the matcher out |
| 173 | // of at least these two patterns. See what the maximal set we can merge |
| 174 | // together is. |
Chris Lattner | 92a2246 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 175 | SmallVector<Matcher*, 8> EqualMatchers; |
| 176 | EqualMatchers.push_back(Optn); |
Chris Lattner | 92a2246 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 177 | |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 178 | // Factor all of the known-equal matchers after this one into the same |
| 179 | // group. |
| 180 | while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn)) |
| 181 | EqualMatchers.push_back(OptionsToMatch[OptionIdx++]); |
| 182 | |
| 183 | // If we found a non-equal matcher, see if it is contradictory with the |
| 184 | // current node. If so, we know that the ordering relation between the |
| 185 | // current sets of nodes and this node don't matter. Look past it to see if |
| 186 | // we can merge anything else into this matching group. |
| 187 | unsigned Scan = OptionIdx; |
| 188 | while (1) { |
| 189 | while (Scan != e && Optn->isContradictory(OptionsToMatch[Scan])) |
| 190 | ++Scan; |
| 191 | |
| 192 | // Ok, we found something that isn't known to be contradictory. If it is |
| 193 | // equal, we can merge it into the set of nodes to factor, if not, we have |
| 194 | // to cease factoring. |
| 195 | if (Scan == e || !Optn->isEqual(OptionsToMatch[Scan])) break; |
| 196 | |
| 197 | // If is equal after all, add the option to EqualMatchers and remove it |
| 198 | // from OptionsToMatch. |
| 199 | EqualMatchers.push_back(OptionsToMatch[Scan]); |
| 200 | OptionsToMatch.erase(OptionsToMatch.begin()+Scan); |
| 201 | --e; |
| 202 | } |
| 203 | |
| 204 | if (Scan != e) { |
Chris Lattner | 086af32 | 2010-02-27 08:11:15 +0000 | [diff] [blame^] | 205 | DEBUG(errs() << "Couldn't merge this:\n"; |
| 206 | Optn->print(errs(), 4); |
| 207 | errs() << "into this:\n"; |
| 208 | OptionsToMatch[Scan]->print(errs(), 4); |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 209 | if (OptionIdx+1 != e) |
Chris Lattner | 086af32 | 2010-02-27 08:11:15 +0000 | [diff] [blame^] | 210 | OptionsToMatch[Scan+1]->printOne(errs()); |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 211 | if (OptionIdx+2 < e) |
Chris Lattner | 086af32 | 2010-02-27 08:11:15 +0000 | [diff] [blame^] | 212 | OptionsToMatch[Scan+2]->printOne(errs()); |
Chris Lattner | ddfd5ab | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 213 | errs() << "\n"); |
| 214 | } |
| 215 | |
| 216 | // If we only found one option starting with this matcher, no factoring is |
| 217 | // possible. |
| 218 | if (EqualMatchers.size() == 1) { |
| 219 | NewOptionsToMatch.push_back(EqualMatchers[0]); |
| 220 | continue; |
| 221 | } |
Chris Lattner | 92a2246 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 222 | |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 223 | // Factor these checks by pulling the first node off each entry and |
Chris Lattner | b0e6810 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 224 | // discarding it. Take the first one off the first entry to reuse. |
| 225 | Matcher *Shared = Optn; |
| 226 | Optn = Optn->takeNext(); |
| 227 | EqualMatchers[0] = Optn; |
| 228 | |
Chris Lattner | 92a2246 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 229 | // Remove and delete the first node from the other matchers we're factoring. |
| 230 | for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) { |
| 231 | Matcher *Tmp = EqualMatchers[i]->takeNext(); |
| 232 | delete EqualMatchers[i]; |
| 233 | EqualMatchers[i] = Tmp; |
| 234 | } |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 235 | |
Chris Lattner | b0e6810 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 236 | Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size())); |
| 237 | |
| 238 | // Recursively factor the newly created node. |
| 239 | FactorNodes(Shared->getNextPtr()); |
| 240 | |
| 241 | NewOptionsToMatch.push_back(Shared); |
Chris Lattner | cd632fb | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Reassemble a new Scope node. |
Chris Lattner | b0e6810 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 245 | assert(!NewOptionsToMatch.empty() && "where'd all our children go?"); |
| 246 | if (NewOptionsToMatch.size() == 1) |
| 247 | MatcherPtr.reset(NewOptionsToMatch[0]); |
| 248 | else { |
| 249 | Scope->setNumChildren(NewOptionsToMatch.size()); |
| 250 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) |
| 251 | Scope->resetChild(i, NewOptionsToMatch[i]); |
| 252 | } |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | 9a51517 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 255 | Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) { |
| 256 | OwningPtr<Matcher> MatcherPtr(TheMatcher); |
Chris Lattner | 9feb1e3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 257 | ContractNodes(MatcherPtr); |
Chris Lattner | f4950d0 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 258 | SinkPatternPredicates(MatcherPtr); |
Chris Lattner | 0acf2ba | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 259 | FactorNodes(MatcherPtr); |
Chris Lattner | 3ee1bc4 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 260 | return MatcherPtr.take(); |
Chris Lattner | 36e646c | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 261 | } |