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 | |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "isel-opt" |
Chris Lattner | 91c6a82 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 15 | #include "DAGISelMatcher.h" |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 16 | #include "CodeGenDAGPatterns.h" |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSet.h" |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 21 | #include <vector> |
Chris Lattner | 91c6a82 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | d323fd4 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 24 | /// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record' |
| 25 | /// into single compound nodes like RecordChild. |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 26 | static void ContractNodes(OwningPtr<Matcher> &MatcherPtr, |
| 27 | const CodeGenDAGPatterns &CGP) { |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 28 | // If we reached the end of the chain, we're done. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 29 | Matcher *N = MatcherPtr.get(); |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 30 | if (N == 0) return; |
| 31 | |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 32 | // If we have a scope node, walk down all of the children. |
| 33 | if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) { |
| 34 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
| 35 | OwningPtr<Matcher> Child(Scope->takeChild(i)); |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 36 | ContractNodes(Child, CGP); |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 37 | Scope->resetChild(i, Child.take()); |
| 38 | } |
| 39 | return; |
| 40 | } |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 42 | // If we found a movechild node with a node that comes in a 'foochild' form, |
| 43 | // transform it. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 44 | if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) { |
| 45 | Matcher *New = 0; |
| 46 | if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext())) |
Chris Lattner | 5f19c43 | 2010-03-16 00:35:11 +0000 | [diff] [blame] | 47 | if (MC->getChildNo() < 8) // Only have RecordChild0...7 |
| 48 | New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(), |
| 49 | RM->getResultNo()); |
Chris Lattner | 23cfda7 | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 51 | if (CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(MC->getNext())) |
| 52 | if (MC->getChildNo() < 8 && // Only have CheckChildType0...7 |
| 53 | CT->getResNo() == 0) // CheckChildType checks res #0 |
Chris Lattner | 5f19c43 | 2010-03-16 00:35:11 +0000 | [diff] [blame] | 54 | New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType()); |
Chris Lattner | 23cfda7 | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 55 | |
| 56 | if (New) { |
| 57 | // Insert the new node. |
Chris Lattner | 60df53e | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 58 | New->setNext(MatcherPtr.take()); |
| 59 | MatcherPtr.reset(New); |
Chris Lattner | 23cfda7 | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 60 | // Remove the old one. |
| 61 | MC->setNext(MC->getNext()->takeNext()); |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 62 | return ContractNodes(MatcherPtr, CGP); |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 65 | |
Chris Lattner | e86097a | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 66 | // Zap movechild -> moveparent. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 67 | if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) |
| 68 | if (MoveParentMatcher *MP = |
| 69 | dyn_cast<MoveParentMatcher>(MC->getNext())) { |
Chris Lattner | 60df53e | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 70 | MatcherPtr.reset(MP->takeNext()); |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 71 | return ContractNodes(MatcherPtr, CGP); |
Chris Lattner | 22c48b3 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | ccd23cc | 2010-02-28 23:00:47 +0000 | [diff] [blame] | 73 | |
Chris Lattner | c99b5a2 | 2010-03-01 02:33:14 +0000 | [diff] [blame] | 74 | // Turn EmitNode->MarkFlagResults->CompleteMatch into |
| 75 | // MarkFlagResults->EmitNode->CompleteMatch when we can to encourage |
| 76 | // MorphNodeTo formation. This is safe because MarkFlagResults never refers |
| 77 | // to the root of the pattern. |
| 78 | if (isa<EmitNodeMatcher>(N) && isa<MarkFlagResultsMatcher>(N->getNext()) && |
| 79 | isa<CompleteMatchMatcher>(N->getNext()->getNext())) { |
| 80 | // Unlink the two nodes from the list. |
| 81 | Matcher *EmitNode = MatcherPtr.take(); |
| 82 | Matcher *MFR = EmitNode->takeNext(); |
| 83 | Matcher *Tail = MFR->takeNext(); |
| 84 | |
| 85 | // Relink them. |
| 86 | MatcherPtr.reset(MFR); |
| 87 | MFR->setNext(EmitNode); |
| 88 | EmitNode->setNext(Tail); |
| 89 | return ContractNodes(MatcherPtr, CGP); |
| 90 | } |
Chris Lattner | ccd23cc | 2010-02-28 23:00:47 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 92 | // Turn EmitNode->CompleteMatch into MorphNodeTo if we can. |
Chris Lattner | e86097a | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 93 | if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N)) |
Chris Lattner | 6281cda | 2010-02-28 02:41:25 +0000 | [diff] [blame] | 94 | if (CompleteMatchMatcher *CM = |
| 95 | dyn_cast<CompleteMatchMatcher>(EN->getNext())) { |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 96 | // We can only use MorphNodeTo if the result values match up. |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 97 | unsigned RootResultFirst = EN->getFirstResultSlot(); |
| 98 | bool ResultsMatch = true; |
| 99 | for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i) |
| 100 | if (CM->getResult(i) != RootResultFirst+i) |
| 101 | ResultsMatch = false; |
| 102 | |
| 103 | // If the selected node defines a subset of the flag/chain results, we |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 104 | // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 105 | // matched pattern has a chain but the root node doesn't. |
| 106 | const PatternToMatch &Pattern = CM->getPattern(); |
| 107 | |
| 108 | if (!EN->hasChain() && |
| 109 | Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP)) |
| 110 | ResultsMatch = false; |
| 111 | |
| 112 | // If the matched node has a flag and the output root doesn't, we can't |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 113 | // use MorphNodeTo. |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 114 | // |
| 115 | // NOTE: Strictly speaking, we don't have to check for the flag here |
| 116 | // because the code in the pattern generator doesn't handle it right. We |
| 117 | // do it anyway for thoroughness. |
Chris Lattner | ff7fb60 | 2010-02-28 21:53:42 +0000 | [diff] [blame] | 118 | if (!EN->hasOutFlag() && |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 119 | Pattern.getSrcPattern()->NodeHasProperty(SDNPOutFlag, CGP)) |
| 120 | ResultsMatch = false; |
| 121 | |
| 122 | |
| 123 | // If the root result node defines more results than the source root node |
| 124 | // *and* has a chain or flag input, then we can't match it because it |
| 125 | // would end up replacing the extra result with the chain/flag. |
| 126 | #if 0 |
| 127 | if ((EN->hasFlag() || EN->hasChain()) && |
| 128 | EN->getNumNonChainFlagVTs() > ... need to get no results reliably ...) |
| 129 | ResultMatch = false; |
| 130 | #endif |
| 131 | |
| 132 | if (ResultsMatch) { |
| 133 | const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList(); |
| 134 | const SmallVectorImpl<unsigned> &Operands = EN->getOperandList(); |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 135 | MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(), |
Chris Lattner | ff7fb60 | 2010-02-28 21:53:42 +0000 | [diff] [blame] | 136 | VTs.data(), VTs.size(), |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 137 | Operands.data(),Operands.size(), |
Chris Lattner | ff7fb60 | 2010-02-28 21:53:42 +0000 | [diff] [blame] | 138 | EN->hasChain(), EN->hasInFlag(), |
| 139 | EN->hasOutFlag(), |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 140 | EN->hasMemRefs(), |
| 141 | EN->getNumFixedArityOperands(), |
| 142 | Pattern)); |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
Chris Lattner | 405f125 | 2010-03-01 22:29:19 +0000 | [diff] [blame] | 146 | // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode |
Chris Lattner | 9a21500 | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 147 | // variants. |
Chris Lattner | e86097a | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 150 | ContractNodes(N->getNextPtr(), CGP); |
Chris Lattner | e9eeda8 | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | // If we have a CheckType/CheckChildType/Record node followed by a |
| 154 | // CheckOpcode, invert the two nodes. We prefer to do structural checks |
| 155 | // before type checks, as this opens opportunities for factoring on targets |
| 156 | // like X86 where many operations are valid on multiple types. |
| 157 | if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) || |
| 158 | isa<RecordMatcher>(N)) && |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 159 | isa<CheckOpcodeMatcher>(N->getNext())) { |
Chris Lattner | e9eeda8 | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 160 | // Unlink the two nodes from the list. |
| 161 | Matcher *CheckType = MatcherPtr.take(); |
| 162 | Matcher *CheckOpcode = CheckType->takeNext(); |
| 163 | Matcher *Tail = CheckOpcode->takeNext(); |
| 164 | |
| 165 | // Relink them. |
| 166 | MatcherPtr.reset(CheckOpcode); |
| 167 | CheckOpcode->setNext(CheckType); |
| 168 | CheckType->setNext(Tail); |
| 169 | return ContractNodes(MatcherPtr, CGP); |
| 170 | } |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | d323fd4 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 173 | /// SinkPatternPredicates - Pattern predicates can be checked at any level of |
| 174 | /// the matching tree. The generator dumps them at the top level of the pattern |
| 175 | /// though, which prevents factoring from being able to see past them. This |
| 176 | /// optimization sinks them as far down into the pattern as possible. |
| 177 | /// |
| 178 | /// Conceptually, we'd like to sink these predicates all the way to the last |
| 179 | /// matcher predicate in the series. However, it turns out that some |
| 180 | /// ComplexPatterns have side effects on the graph, so we really don't want to |
| 181 | /// run a the complex pattern if the pattern predicate will fail. For this |
| 182 | /// reason, we refuse to sink the pattern predicate past a ComplexPattern. |
| 183 | /// |
| 184 | static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) { |
| 185 | // Recursively scan for a PatternPredicate. |
| 186 | // If we reached the end of the chain, we're done. |
| 187 | Matcher *N = MatcherPtr.get(); |
| 188 | if (N == 0) return; |
| 189 | |
| 190 | // Walk down all members of a scope node. |
| 191 | if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) { |
| 192 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
| 193 | OwningPtr<Matcher> Child(Scope->takeChild(i)); |
| 194 | SinkPatternPredicates(Child); |
| 195 | Scope->resetChild(i, Child.take()); |
| 196 | } |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | // If this node isn't a CheckPatternPredicateMatcher we keep scanning until |
| 201 | // we find one. |
| 202 | CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N); |
| 203 | if (CPPM == 0) |
| 204 | return SinkPatternPredicates(N->getNextPtr()); |
| 205 | |
| 206 | // Ok, we found one, lets try to sink it. Check if we can sink it past the |
| 207 | // next node in the chain. If not, we won't be able to change anything and |
| 208 | // might as well bail. |
| 209 | if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate()) |
| 210 | return; |
| 211 | |
| 212 | // Okay, we know we can sink it past at least one node. Unlink it from the |
| 213 | // chain and scan for the new insertion point. |
| 214 | MatcherPtr.take(); // Don't delete CPPM. |
| 215 | MatcherPtr.reset(CPPM->takeNext()); |
| 216 | |
| 217 | N = MatcherPtr.get(); |
| 218 | while (N->getNext()->isSafeToReorderWithPatternPredicate()) |
| 219 | N = N->getNext(); |
| 220 | |
| 221 | // At this point, we want to insert CPPM after N. |
| 222 | CPPM->setNext(N->takeNext()); |
| 223 | N->setNext(CPPM); |
| 224 | } |
| 225 | |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 226 | /// FindNodeWithKind - Scan a series of matchers looking for a matcher with a |
| 227 | /// specified kind. Return null if we didn't find one otherwise return the |
| 228 | /// matcher. |
| 229 | static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) { |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 230 | for (; M; M = M->getNext()) |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 231 | if (M->getKind() == Kind) |
| 232 | return M; |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | |
Chris Lattner | d323fd4 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 237 | /// FactorNodes - Turn matches like this: |
| 238 | /// Scope |
| 239 | /// OPC_CheckType i32 |
| 240 | /// ABC |
| 241 | /// OPC_CheckType i32 |
| 242 | /// XYZ |
| 243 | /// into: |
| 244 | /// OPC_CheckType i32 |
| 245 | /// Scope |
| 246 | /// ABC |
| 247 | /// XYZ |
| 248 | /// |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 249 | static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) { |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 250 | // If we reached the end of the chain, we're done. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 251 | Matcher *N = MatcherPtr.get(); |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 252 | if (N == 0) return; |
| 253 | |
| 254 | // If this is not a push node, just scan for one. |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 255 | ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N); |
| 256 | if (Scope == 0) |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 257 | return FactorNodes(N->getNextPtr()); |
| 258 | |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 259 | // Okay, pull together the children of the scope node into a vector so we can |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 260 | // inspect it more easily. While we're at it, bucket them up by the hash |
| 261 | // code of their first predicate. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 262 | SmallVector<Matcher*, 32> OptionsToMatch; |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 263 | |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 264 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 265 | // Factor the subexpression. |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 266 | OwningPtr<Matcher> Child(Scope->takeChild(i)); |
| 267 | FactorNodes(Child); |
| 268 | |
Chris Lattner | d6f0602 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 269 | if (Matcher *N = Child.take()) |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 270 | OptionsToMatch.push_back(N); |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 273 | SmallVector<Matcher*, 32> NewOptionsToMatch; |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 274 | |
Chris Lattner | d6f0602 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 275 | // Loop over options to match, merging neighboring patterns with identical |
| 276 | // starting nodes into a shared matcher. |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 277 | for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) { |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 278 | // Find the set of matchers that start with this node. |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 279 | Matcher *Optn = OptionsToMatch[OptionIdx++]; |
| 280 | |
| 281 | if (OptionIdx == e) { |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 282 | NewOptionsToMatch.push_back(Optn); |
| 283 | continue; |
| 284 | } |
| 285 | |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 286 | // See if the next option starts with the same matcher. If the two |
| 287 | // neighbors *do* start with the same matcher, we can factor the matcher out |
| 288 | // of at least these two patterns. See what the maximal set we can merge |
| 289 | // together is. |
Chris Lattner | d6f0602 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 290 | SmallVector<Matcher*, 8> EqualMatchers; |
| 291 | EqualMatchers.push_back(Optn); |
Chris Lattner | d6f0602 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 293 | // Factor all of the known-equal matchers after this one into the same |
| 294 | // group. |
| 295 | while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn)) |
| 296 | EqualMatchers.push_back(OptionsToMatch[OptionIdx++]); |
| 297 | |
| 298 | // If we found a non-equal matcher, see if it is contradictory with the |
| 299 | // current node. If so, we know that the ordering relation between the |
| 300 | // current sets of nodes and this node don't matter. Look past it to see if |
| 301 | // we can merge anything else into this matching group. |
| 302 | unsigned Scan = OptionIdx; |
| 303 | while (1) { |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 304 | // If we ran out of stuff to scan, we're done. |
| 305 | if (Scan == e) break; |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 307 | Matcher *ScanMatcher = OptionsToMatch[Scan]; |
| 308 | |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 309 | // If we found an entry that matches out matcher, merge it into the set to |
| 310 | // handle. |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 311 | if (Optn->isEqual(ScanMatcher)) { |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 312 | // If is equal after all, add the option to EqualMatchers and remove it |
| 313 | // from OptionsToMatch. |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 314 | EqualMatchers.push_back(ScanMatcher); |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 315 | OptionsToMatch.erase(OptionsToMatch.begin()+Scan); |
| 316 | --e; |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | // If the option we're checking for contradicts the start of the list, |
| 321 | // skip over it. |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 322 | if (Optn->isContradictory(ScanMatcher)) { |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 323 | ++Scan; |
| 324 | continue; |
| 325 | } |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 327 | // If we're scanning for a simple node, see if it occurs later in the |
| 328 | // sequence. If so, and if we can move it up, it might be contradictory |
| 329 | // or the same as what we're looking for. If so, reorder it. |
| 330 | if (Optn->isSimplePredicateOrRecordNode()) { |
| 331 | Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind()); |
| 332 | if (M2 != 0 && M2 != ScanMatcher && |
| 333 | M2->canMoveBefore(ScanMatcher) && |
| 334 | (M2->isEqual(Optn) || M2->isContradictory(Optn))) { |
| 335 | Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2); |
| 336 | M2->setNext(MatcherWithoutM2); |
| 337 | OptionsToMatch[Scan] = M2; |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 338 | continue; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Otherwise, we don't know how to handle this entry, we have to bail. |
| 343 | break; |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | a230f96 | 2010-02-27 21:48:43 +0000 | [diff] [blame] | 346 | if (Scan != e && |
| 347 | // Don't print it's obvious nothing extra could be merged anyway. |
| 348 | Scan+1 != e) { |
Chris Lattner | 81d6d52 | 2010-03-07 07:21:24 +0000 | [diff] [blame] | 349 | DEBUG(errs() << "Couldn't merge this:\n"; |
Chris Lattner | 2478962 | 2010-02-27 08:11:15 +0000 | [diff] [blame] | 350 | Optn->print(errs(), 4); |
| 351 | errs() << "into this:\n"; |
| 352 | OptionsToMatch[Scan]->print(errs(), 4); |
Chris Lattner | db8b622 | 2010-02-27 08:13:23 +0000 | [diff] [blame] | 353 | if (Scan+1 != e) |
Chris Lattner | 2478962 | 2010-02-27 08:11:15 +0000 | [diff] [blame] | 354 | OptionsToMatch[Scan+1]->printOne(errs()); |
Chris Lattner | db8b622 | 2010-02-27 08:13:23 +0000 | [diff] [blame] | 355 | if (Scan+2 < e) |
Chris Lattner | 2478962 | 2010-02-27 08:11:15 +0000 | [diff] [blame] | 356 | OptionsToMatch[Scan+2]->printOne(errs()); |
Chris Lattner | 81d6d52 | 2010-03-07 07:21:24 +0000 | [diff] [blame] | 357 | errs() << "\n"); |
Chris Lattner | 82781b9 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // If we only found one option starting with this matcher, no factoring is |
| 361 | // possible. |
| 362 | if (EqualMatchers.size() == 1) { |
| 363 | NewOptionsToMatch.push_back(EqualMatchers[0]); |
| 364 | continue; |
| 365 | } |
Chris Lattner | d6f0602 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 367 | // Factor these checks by pulling the first node off each entry and |
Chris Lattner | d4397b9 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 368 | // discarding it. Take the first one off the first entry to reuse. |
| 369 | Matcher *Shared = Optn; |
| 370 | Optn = Optn->takeNext(); |
| 371 | EqualMatchers[0] = Optn; |
| 372 | |
Chris Lattner | d6f0602 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 373 | // Remove and delete the first node from the other matchers we're factoring. |
| 374 | for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) { |
| 375 | Matcher *Tmp = EqualMatchers[i]->takeNext(); |
| 376 | delete EqualMatchers[i]; |
| 377 | EqualMatchers[i] = Tmp; |
| 378 | } |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 379 | |
Chris Lattner | d4397b9 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 380 | Shared->setNext(new ScopeMatcher(&EqualMatchers[0], EqualMatchers.size())); |
| 381 | |
| 382 | // Recursively factor the newly created node. |
| 383 | FactorNodes(Shared->getNextPtr()); |
| 384 | |
| 385 | NewOptionsToMatch.push_back(Shared); |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 386 | } |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 387 | |
| 388 | // If we're down to a single pattern to match, then we don't need this scope |
| 389 | // anymore. |
| 390 | if (NewOptionsToMatch.size() == 1) { |
| 391 | MatcherPtr.reset(NewOptionsToMatch[0]); |
| 392 | return; |
| 393 | } |
| 394 | |
Chris Lattner | f94bc54 | 2010-03-01 22:04:33 +0000 | [diff] [blame] | 395 | if (NewOptionsToMatch.empty()) { |
| 396 | MatcherPtr.reset(0); |
| 397 | return; |
| 398 | } |
| 399 | |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 400 | // If our factoring failed (didn't achieve anything) see if we can simplify in |
| 401 | // other ways. |
| 402 | |
| 403 | // Check to see if all of the leading entries are now opcode checks. If so, |
| 404 | // we can convert this Scope to be a OpcodeSwitch instead. |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 405 | bool AllOpcodeChecks = true, AllTypeChecks = true; |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 406 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) { |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 407 | // Check to see if this breaks a series of CheckOpcodeMatchers. |
| 408 | if (AllOpcodeChecks && |
| 409 | !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) { |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 410 | #if 0 |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 411 | if (i > 3) { |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 412 | errs() << "FAILING OPC #" << i << "\n"; |
| 413 | NewOptionsToMatch[i]->dump(); |
| 414 | } |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 415 | #endif |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 416 | AllOpcodeChecks = false; |
| 417 | } |
| 418 | |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 419 | // Check to see if this breaks a series of CheckTypeMatcher's. |
| 420 | if (AllTypeChecks) { |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 421 | CheckTypeMatcher *CTM = |
| 422 | cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i], |
| 423 | Matcher::CheckType)); |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 424 | if (CTM == 0 || |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 425 | // iPTR checks could alias any other case without us knowing, don't |
| 426 | // bother with them. |
| 427 | CTM->getType() == MVT::iPTR || |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 428 | // SwitchType only works for result #0. |
| 429 | CTM->getResNo() != 0 || |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 430 | // If the CheckType isn't at the start of the list, see if we can move |
| 431 | // it there. |
| 432 | !CTM->canMoveBefore(NewOptionsToMatch[i])) { |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 433 | #if 0 |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 434 | if (i > 3 && AllTypeChecks) { |
| 435 | errs() << "FAILING TYPE #" << i << "\n"; |
| 436 | NewOptionsToMatch[i]->dump(); |
| 437 | } |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 438 | #endif |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 439 | AllTypeChecks = false; |
| 440 | } |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 441 | } |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot. |
| 445 | if (AllOpcodeChecks) { |
| 446 | StringSet<> Opcodes; |
| 447 | SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases; |
| 448 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) { |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 449 | CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]); |
Chris Lattner | eb66921 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 450 | assert(Opcodes.insert(COM->getOpcode().getEnumName()) && |
| 451 | "Duplicate opcodes not factored?"); |
| 452 | Cases.push_back(std::make_pair(&COM->getOpcode(), COM->getNext())); |
| 453 | } |
| 454 | |
| 455 | MatcherPtr.reset(new SwitchOpcodeMatcher(&Cases[0], Cases.size())); |
| 456 | return; |
| 457 | } |
| 458 | |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 459 | // If all the options are CheckType's, we can form the SwitchType, woot. |
| 460 | if (AllTypeChecks) { |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 461 | DenseMap<unsigned, unsigned> TypeEntry; |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 462 | SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases; |
| 463 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) { |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 464 | CheckTypeMatcher *CTM = |
| 465 | cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i], |
| 466 | Matcher::CheckType)); |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 467 | Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM); |
| 468 | MVT::SimpleValueType CTMTy = CTM->getType(); |
| 469 | delete CTM; |
| 470 | |
| 471 | unsigned &Entry = TypeEntry[CTMTy]; |
| 472 | if (Entry != 0) { |
| 473 | // If we have unfactored duplicate types, then we should factor them. |
| 474 | Matcher *PrevMatcher = Cases[Entry-1].second; |
| 475 | if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) { |
| 476 | SM->setNumChildren(SM->getNumChildren()+1); |
| 477 | SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM); |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM }; |
| 482 | Cases[Entry-1].second = new ScopeMatcher(Entries, 2); |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | Entry = Cases.size()+1; |
| 487 | Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM)); |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 490 | if (Cases.size() != 1) { |
| 491 | MatcherPtr.reset(new SwitchTypeMatcher(&Cases[0], Cases.size())); |
| 492 | } else { |
| 493 | // If we factored and ended up with one case, create it now. |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 494 | MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0)); |
Chris Lattner | 9cdd965 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 495 | MatcherPtr->setNext(Cases[0].second); |
| 496 | } |
Chris Lattner | cfe2eab | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
Chris Lattner | 04db82d | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 7c720fc | 2010-03-01 22:19:47 +0000 | [diff] [blame] | 501 | // Reassemble the Scope node with the adjusted children. |
| 502 | Scope->setNumChildren(NewOptionsToMatch.size()); |
| 503 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) |
| 504 | Scope->resetChild(i, NewOptionsToMatch[i]); |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 507 | Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher, |
| 508 | const CodeGenDAGPatterns &CGP) { |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 509 | OwningPtr<Matcher> MatcherPtr(TheMatcher); |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 510 | ContractNodes(MatcherPtr, CGP); |
Chris Lattner | d323fd4 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 511 | SinkPatternPredicates(MatcherPtr); |
Chris Lattner | 0615840 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 512 | FactorNodes(MatcherPtr); |
Chris Lattner | 19b5a75 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 513 | return MatcherPtr.take(); |
Chris Lattner | 91c6a82 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 514 | } |