Chris Lattner | e732743 | 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" |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 15 | #include "CodeGenDAGPatterns.h" |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSet.h" |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | e732743 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chandler Carruth | 97acce2 | 2014-04-22 03:06:00 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "isel-opt" |
| 22 | |
Chris Lattner | d9e1e83 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 23 | /// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record' |
| 24 | /// into single compound nodes like RecordChild. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 25 | static void ContractNodes(std::unique_ptr<Matcher> &MatcherPtr, |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 26 | const CodeGenDAGPatterns &CGP) { |
Chris Lattner | ab41756 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 27 | // If we reached the end of the chain, we're done. |
Chris Lattner | 2c3f649 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 28 | Matcher *N = MatcherPtr.get(); |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 29 | if (!N) return; |
Chris Lattner | ab41756 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 30 | |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 31 | // If we have a scope node, walk down all of the children. |
| 32 | if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) { |
| 33 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 34 | std::unique_ptr<Matcher> Child(Scope->takeChild(i)); |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 35 | ContractNodes(Child, CGP); |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 36 | Scope->resetChild(i, Child.release()); |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 37 | } |
| 38 | return; |
| 39 | } |
Chris Lattner | ab41756 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 6b79232 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 41 | // If we found a movechild node with a node that comes in a 'foochild' form, |
| 42 | // transform it. |
Chris Lattner | 2c3f649 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 43 | if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) { |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 44 | Matcher *New = nullptr; |
Chris Lattner | 2c3f649 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 45 | if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext())) |
Chris Lattner | f57437a | 2010-03-16 00:35:11 +0000 | [diff] [blame] | 46 | if (MC->getChildNo() < 8) // Only have RecordChild0...7 |
| 47 | New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(), |
| 48 | RM->getResultNo()); |
Craig Topper | 7ca1d18 | 2014-02-05 05:44:28 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 6c2d178 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 50 | if (CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(MC->getNext())) |
| 51 | if (MC->getChildNo() < 8 && // Only have CheckChildType0...7 |
| 52 | CT->getResNo() == 0) // CheckChildType checks res #0 |
Chris Lattner | f57437a | 2010-03-16 00:35:11 +0000 | [diff] [blame] | 53 | New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType()); |
Craig Topper | a1bbc32 | 2013-10-05 05:38:16 +0000 | [diff] [blame] | 54 | |
| 55 | if (CheckSameMatcher *CS = dyn_cast<CheckSameMatcher>(MC->getNext())) |
| 56 | if (MC->getChildNo() < 4) // Only have CheckChildSame0...3 |
| 57 | New = new CheckChildSameMatcher(MC->getChildNo(), CS->getMatchNumber()); |
| 58 | |
Craig Topper | 7ca1d18 | 2014-02-05 05:44:28 +0000 | [diff] [blame] | 59 | if (CheckIntegerMatcher *CS = dyn_cast<CheckIntegerMatcher>(MC->getNext())) |
| 60 | if (MC->getChildNo() < 5) // Only have CheckChildInteger0...4 |
| 61 | New = new CheckChildIntegerMatcher(MC->getChildNo(), CS->getValue()); |
| 62 | |
Chris Lattner | 0c95baa | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 63 | if (New) { |
| 64 | // Insert the new node. |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 65 | New->setNext(MatcherPtr.release()); |
Chris Lattner | ac55f9d | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 66 | MatcherPtr.reset(New); |
Chris Lattner | 0c95baa | 2010-02-24 20:15:25 +0000 | [diff] [blame] | 67 | // Remove the old one. |
| 68 | MC->setNext(MC->getNext()->takeNext()); |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 69 | return ContractNodes(MatcherPtr, CGP); |
Chris Lattner | 6b79232 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | ab41756 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 6b79232 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 72 | |
Chris Lattner | c3f80e0 | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 73 | // Zap movechild -> moveparent. |
Chris Lattner | 2c3f649 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 74 | if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) |
| 75 | if (MoveParentMatcher *MP = |
| 76 | dyn_cast<MoveParentMatcher>(MC->getNext())) { |
Chris Lattner | ac55f9d | 2010-02-25 01:56:48 +0000 | [diff] [blame] | 77 | MatcherPtr.reset(MP->takeNext()); |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 78 | return ContractNodes(MatcherPtr, CGP); |
Chris Lattner | 6b79232 | 2010-02-24 19:52:48 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | 560169d | 2010-02-28 23:00:47 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 81 | // Turn EmitNode->CompleteMatch into MorphNodeTo if we can. |
Chris Lattner | c3f80e0 | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 82 | if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N)) |
Chris Lattner | abb1c79 | 2010-02-28 02:41:25 +0000 | [diff] [blame] | 83 | if (CompleteMatchMatcher *CM = |
| 84 | dyn_cast<CompleteMatchMatcher>(EN->getNext())) { |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 85 | // We can only use MorphNodeTo if the result values match up. |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 86 | unsigned RootResultFirst = EN->getFirstResultSlot(); |
| 87 | bool ResultsMatch = true; |
| 88 | for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i) |
| 89 | if (CM->getResult(i) != RootResultFirst+i) |
| 90 | ResultsMatch = false; |
| 91 | |
Chris Lattner | f7f9e8c | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 92 | // If the selected node defines a subset of the glue/chain results, we |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 93 | // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 94 | // matched pattern has a chain but the root node doesn't. |
| 95 | const PatternToMatch &Pattern = CM->getPattern(); |
| 96 | |
| 97 | if (!EN->hasChain() && |
| 98 | Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP)) |
| 99 | ResultsMatch = false; |
| 100 | |
Chris Lattner | f7f9e8c | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 101 | // If the matched node has glue and the output root doesn't, we can't |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 102 | // use MorphNodeTo. |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 103 | // |
Chris Lattner | f7f9e8c | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 104 | // NOTE: Strictly speaking, we don't have to check for glue here |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 105 | // because the code in the pattern generator doesn't handle it right. We |
| 106 | // do it anyway for thoroughness. |
Chris Lattner | a838264 | 2010-02-28 21:53:42 +0000 | [diff] [blame] | 107 | if (!EN->hasOutFlag() && |
Chris Lattner | 2a0a3b4 | 2010-12-23 18:28:41 +0000 | [diff] [blame] | 108 | Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP)) |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 109 | ResultsMatch = false; |
| 110 | |
| 111 | |
| 112 | // If the root result node defines more results than the source root node |
Chris Lattner | f7f9e8c | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 113 | // *and* has a chain or glue input, then we can't match it because it |
| 114 | // would end up replacing the extra result with the chain/glue. |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 115 | #if 0 |
Chris Lattner | f7f9e8c | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 116 | if ((EN->hasGlue() || EN->hasChain()) && |
| 117 | EN->getNumNonChainGlueVTs() > ... need to get no results reliably ...) |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 118 | ResultMatch = false; |
| 119 | #endif |
| 120 | |
| 121 | if (ResultsMatch) { |
| 122 | const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList(); |
| 123 | const SmallVectorImpl<unsigned> &Operands = EN->getOperandList(); |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 124 | MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(), |
Craig Topper | a2c6019 | 2014-01-21 07:20:05 +0000 | [diff] [blame] | 125 | VTs, Operands, |
Chris Lattner | a838264 | 2010-02-28 21:53:42 +0000 | [diff] [blame] | 126 | EN->hasChain(), EN->hasInFlag(), |
| 127 | EN->hasOutFlag(), |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 128 | EN->hasMemRefs(), |
| 129 | EN->getNumFixedArityOperands(), |
| 130 | Pattern)); |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
Chris Lattner | 1e634e3 | 2010-03-01 22:29:19 +0000 | [diff] [blame] | 134 | // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode |
Chris Lattner | 9d67dca | 2010-02-28 20:55:18 +0000 | [diff] [blame] | 135 | // variants. |
Chris Lattner | c3f80e0 | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 138 | ContractNodes(N->getNextPtr(), CGP); |
Chris Lattner | 90b1b9d | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | // If we have a CheckType/CheckChildType/Record node followed by a |
| 142 | // CheckOpcode, invert the two nodes. We prefer to do structural checks |
| 143 | // before type checks, as this opens opportunities for factoring on targets |
| 144 | // like X86 where many operations are valid on multiple types. |
| 145 | if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) || |
| 146 | isa<RecordMatcher>(N)) && |
Chris Lattner | 053a28a | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 147 | isa<CheckOpcodeMatcher>(N->getNext())) { |
Chris Lattner | 90b1b9d | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 148 | // Unlink the two nodes from the list. |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 149 | Matcher *CheckType = MatcherPtr.release(); |
Chris Lattner | 90b1b9d | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 150 | Matcher *CheckOpcode = CheckType->takeNext(); |
| 151 | Matcher *Tail = CheckOpcode->takeNext(); |
| 152 | |
| 153 | // Relink them. |
| 154 | MatcherPtr.reset(CheckOpcode); |
| 155 | CheckOpcode->setNext(CheckType); |
| 156 | CheckType->setNext(Tail); |
| 157 | return ContractNodes(MatcherPtr, CGP); |
| 158 | } |
Chris Lattner | ab41756 | 2010-02-24 07:31:45 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 161 | /// FindNodeWithKind - Scan a series of matchers looking for a matcher with a |
| 162 | /// specified kind. Return null if we didn't find one otherwise return the |
| 163 | /// matcher. |
| 164 | static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) { |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 165 | for (; M; M = M->getNext()) |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 166 | if (M->getKind() == Kind) |
| 167 | return M; |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 168 | return nullptr; |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Chris Lattner | d9e1e83 | 2010-02-27 06:22:57 +0000 | [diff] [blame] | 172 | /// FactorNodes - Turn matches like this: |
| 173 | /// Scope |
| 174 | /// OPC_CheckType i32 |
| 175 | /// ABC |
| 176 | /// OPC_CheckType i32 |
| 177 | /// XYZ |
| 178 | /// into: |
| 179 | /// OPC_CheckType i32 |
| 180 | /// Scope |
| 181 | /// ABC |
| 182 | /// XYZ |
| 183 | /// |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 184 | static void FactorNodes(std::unique_ptr<Matcher> &MatcherPtr) { |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 185 | // If we reached the end of the chain, we're done. |
Chris Lattner | 2c3f649 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 186 | Matcher *N = MatcherPtr.get(); |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 187 | if (!N) return; |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 188 | |
| 189 | // If this is not a push node, just scan for one. |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 190 | ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N); |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 191 | if (!Scope) |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 192 | return FactorNodes(N->getNextPtr()); |
| 193 | |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 194 | // Okay, pull together the children of the scope node into a vector so we can |
Craig Topper | d34bf35 | 2016-05-05 06:19:25 +0000 | [diff] [blame] | 195 | // inspect it more easily. |
Chris Lattner | 2c3f649 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 196 | SmallVector<Matcher*, 32> OptionsToMatch; |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 197 | |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 198 | for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) { |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 199 | // Factor the subexpression. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 200 | std::unique_ptr<Matcher> Child(Scope->takeChild(i)); |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 201 | FactorNodes(Child); |
| 202 | |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 203 | if (Matcher *N = Child.release()) |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 204 | OptionsToMatch.push_back(N); |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 207 | SmallVector<Matcher*, 32> NewOptionsToMatch; |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 4f9a671 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 209 | // Loop over options to match, merging neighboring patterns with identical |
| 210 | // starting nodes into a shared matcher. |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 211 | for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) { |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 212 | // Find the set of matchers that start with this node. |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 213 | Matcher *Optn = OptionsToMatch[OptionIdx++]; |
| 214 | |
| 215 | if (OptionIdx == e) { |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 216 | NewOptionsToMatch.push_back(Optn); |
| 217 | continue; |
| 218 | } |
| 219 | |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 220 | // See if the next option starts with the same matcher. If the two |
| 221 | // neighbors *do* start with the same matcher, we can factor the matcher out |
| 222 | // of at least these two patterns. See what the maximal set we can merge |
| 223 | // together is. |
Chris Lattner | 4f9a671 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 224 | SmallVector<Matcher*, 8> EqualMatchers; |
| 225 | EqualMatchers.push_back(Optn); |
Chris Lattner | 4f9a671 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 226 | |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 227 | // Factor all of the known-equal matchers after this one into the same |
| 228 | // group. |
| 229 | while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn)) |
| 230 | EqualMatchers.push_back(OptionsToMatch[OptionIdx++]); |
| 231 | |
| 232 | // If we found a non-equal matcher, see if it is contradictory with the |
| 233 | // current node. If so, we know that the ordering relation between the |
| 234 | // current sets of nodes and this node don't matter. Look past it to see if |
| 235 | // we can merge anything else into this matching group. |
| 236 | unsigned Scan = OptionIdx; |
| 237 | while (1) { |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 238 | // If we ran out of stuff to scan, we're done. |
| 239 | if (Scan == e) break; |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 240 | |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 241 | Matcher *ScanMatcher = OptionsToMatch[Scan]; |
| 242 | |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 243 | // If we found an entry that matches out matcher, merge it into the set to |
| 244 | // handle. |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 245 | if (Optn->isEqual(ScanMatcher)) { |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 246 | // If is equal after all, add the option to EqualMatchers and remove it |
| 247 | // from OptionsToMatch. |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 248 | EqualMatchers.push_back(ScanMatcher); |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 249 | OptionsToMatch.erase(OptionsToMatch.begin()+Scan); |
| 250 | --e; |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | // If the option we're checking for contradicts the start of the list, |
| 255 | // skip over it. |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 256 | if (Optn->isContradictory(ScanMatcher)) { |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 257 | ++Scan; |
| 258 | continue; |
| 259 | } |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 260 | |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 261 | // If we're scanning for a simple node, see if it occurs later in the |
| 262 | // sequence. If so, and if we can move it up, it might be contradictory |
| 263 | // or the same as what we're looking for. If so, reorder it. |
| 264 | if (Optn->isSimplePredicateOrRecordNode()) { |
| 265 | Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind()); |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 266 | if (M2 && M2 != ScanMatcher && |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 267 | M2->canMoveBefore(ScanMatcher) && |
| 268 | (M2->isEqual(Optn) || M2->isContradictory(Optn))) { |
| 269 | Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2); |
| 270 | M2->setNext(MatcherWithoutM2); |
| 271 | OptionsToMatch[Scan] = M2; |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 272 | continue; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Otherwise, we don't know how to handle this entry, we have to bail. |
| 277 | break; |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Chris Lattner | 278606b | 2010-02-27 21:48:43 +0000 | [diff] [blame] | 280 | if (Scan != e && |
| 281 | // Don't print it's obvious nothing extra could be merged anyway. |
| 282 | Scan+1 != e) { |
Chris Lattner | c95d58d | 2010-03-07 07:21:24 +0000 | [diff] [blame] | 283 | DEBUG(errs() << "Couldn't merge this:\n"; |
Chris Lattner | 2586c86 | 2010-02-27 08:11:15 +0000 | [diff] [blame] | 284 | Optn->print(errs(), 4); |
| 285 | errs() << "into this:\n"; |
| 286 | OptionsToMatch[Scan]->print(errs(), 4); |
Chris Lattner | 21a7bf3 | 2010-02-27 08:13:23 +0000 | [diff] [blame] | 287 | if (Scan+1 != e) |
Chris Lattner | 2586c86 | 2010-02-27 08:11:15 +0000 | [diff] [blame] | 288 | OptionsToMatch[Scan+1]->printOne(errs()); |
Chris Lattner | 21a7bf3 | 2010-02-27 08:13:23 +0000 | [diff] [blame] | 289 | if (Scan+2 < e) |
Chris Lattner | 2586c86 | 2010-02-27 08:11:15 +0000 | [diff] [blame] | 290 | OptionsToMatch[Scan+2]->printOne(errs()); |
Chris Lattner | c95d58d | 2010-03-07 07:21:24 +0000 | [diff] [blame] | 291 | errs() << "\n"); |
Chris Lattner | c577b81 | 2010-02-27 07:49:13 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // If we only found one option starting with this matcher, no factoring is |
| 295 | // possible. |
| 296 | if (EqualMatchers.size() == 1) { |
| 297 | NewOptionsToMatch.push_back(EqualMatchers[0]); |
| 298 | continue; |
| 299 | } |
Chris Lattner | 4f9a671 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 301 | // Factor these checks by pulling the first node off each entry and |
Chris Lattner | be5b634 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 302 | // discarding it. Take the first one off the first entry to reuse. |
| 303 | Matcher *Shared = Optn; |
| 304 | Optn = Optn->takeNext(); |
| 305 | EqualMatchers[0] = Optn; |
| 306 | |
Chris Lattner | 4f9a671 | 2010-02-26 08:08:41 +0000 | [diff] [blame] | 307 | // Remove and delete the first node from the other matchers we're factoring. |
| 308 | for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) { |
| 309 | Matcher *Tmp = EqualMatchers[i]->takeNext(); |
| 310 | delete EqualMatchers[i]; |
| 311 | EqualMatchers[i] = Tmp; |
| 312 | } |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 313 | |
Craig Topper | a2c6019 | 2014-01-21 07:20:05 +0000 | [diff] [blame] | 314 | Shared->setNext(new ScopeMatcher(EqualMatchers)); |
Chris Lattner | be5b634 | 2010-02-26 07:36:37 +0000 | [diff] [blame] | 315 | |
| 316 | // Recursively factor the newly created node. |
| 317 | FactorNodes(Shared->getNextPtr()); |
| 318 | |
| 319 | NewOptionsToMatch.push_back(Shared); |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 321 | |
| 322 | // If we're down to a single pattern to match, then we don't need this scope |
| 323 | // anymore. |
| 324 | if (NewOptionsToMatch.size() == 1) { |
| 325 | MatcherPtr.reset(NewOptionsToMatch[0]); |
| 326 | return; |
| 327 | } |
| 328 | |
Chris Lattner | 664ac98 | 2010-03-01 22:04:33 +0000 | [diff] [blame] | 329 | if (NewOptionsToMatch.empty()) { |
David Blaikie | b61064e | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 330 | MatcherPtr.reset(); |
Chris Lattner | 664ac98 | 2010-03-01 22:04:33 +0000 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 334 | // If our factoring failed (didn't achieve anything) see if we can simplify in |
| 335 | // other ways. |
| 336 | |
| 337 | // Check to see if all of the leading entries are now opcode checks. If so, |
| 338 | // we can convert this Scope to be a OpcodeSwitch instead. |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 339 | bool AllOpcodeChecks = true, AllTypeChecks = true; |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 340 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) { |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 341 | // Check to see if this breaks a series of CheckOpcodeMatchers. |
| 342 | if (AllOpcodeChecks && |
| 343 | !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) { |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 344 | #if 0 |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 345 | if (i > 3) { |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 346 | errs() << "FAILING OPC #" << i << "\n"; |
| 347 | NewOptionsToMatch[i]->dump(); |
| 348 | } |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 349 | #endif |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 350 | AllOpcodeChecks = false; |
| 351 | } |
| 352 | |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 353 | // Check to see if this breaks a series of CheckTypeMatcher's. |
| 354 | if (AllTypeChecks) { |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 355 | CheckTypeMatcher *CTM = |
| 356 | cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i], |
| 357 | Matcher::CheckType)); |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 358 | if (!CTM || |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 359 | // iPTR checks could alias any other case without us knowing, don't |
| 360 | // bother with them. |
| 361 | CTM->getType() == MVT::iPTR || |
Chris Lattner | 6c2d178 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 362 | // SwitchType only works for result #0. |
| 363 | CTM->getResNo() != 0 || |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 364 | // If the CheckType isn't at the start of the list, see if we can move |
| 365 | // it there. |
| 366 | !CTM->canMoveBefore(NewOptionsToMatch[i])) { |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 367 | #if 0 |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 368 | if (i > 3 && AllTypeChecks) { |
| 369 | errs() << "FAILING TYPE #" << i << "\n"; |
| 370 | NewOptionsToMatch[i]->dump(); |
| 371 | } |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 372 | #endif |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 373 | AllTypeChecks = false; |
| 374 | } |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot. |
| 379 | if (AllOpcodeChecks) { |
| 380 | StringSet<> Opcodes; |
| 381 | SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases; |
| 382 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) { |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 383 | CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]); |
David Blaikie | 0356975 | 2014-11-19 02:56:00 +0000 | [diff] [blame] | 384 | assert(Opcodes.insert(COM->getOpcode().getEnumName()).second && |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 385 | "Duplicate opcodes not factored?"); |
| 386 | Cases.push_back(std::make_pair(&COM->getOpcode(), COM->getNext())); |
| 387 | } |
| 388 | |
Craig Topper | a2c6019 | 2014-01-21 07:20:05 +0000 | [diff] [blame] | 389 | MatcherPtr.reset(new SwitchOpcodeMatcher(Cases)); |
Chris Lattner | f4d1775 | 2010-03-01 06:59:22 +0000 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 393 | // If all the options are CheckType's, we can form the SwitchType, woot. |
| 394 | if (AllTypeChecks) { |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 395 | DenseMap<unsigned, unsigned> TypeEntry; |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 396 | SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases; |
| 397 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) { |
Chris Lattner | a160389 | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 398 | CheckTypeMatcher *CTM = |
| 399 | cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i], |
| 400 | Matcher::CheckType)); |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 401 | Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM); |
| 402 | MVT::SimpleValueType CTMTy = CTM->getType(); |
| 403 | delete CTM; |
| 404 | |
| 405 | unsigned &Entry = TypeEntry[CTMTy]; |
| 406 | if (Entry != 0) { |
| 407 | // If we have unfactored duplicate types, then we should factor them. |
| 408 | Matcher *PrevMatcher = Cases[Entry-1].second; |
| 409 | if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) { |
| 410 | SM->setNumChildren(SM->getNumChildren()+1); |
| 411 | SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM); |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM }; |
Craig Topper | 9475352 | 2016-05-05 06:19:27 +0000 | [diff] [blame] | 416 | std::unique_ptr<Matcher> Case(new ScopeMatcher(Entries)); |
| 417 | FactorNodes(Case); |
| 418 | Cases[Entry-1].second = Case.release(); |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 419 | continue; |
| 420 | } |
| 421 | |
| 422 | Entry = Cases.size()+1; |
| 423 | Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM)); |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 426 | if (Cases.size() != 1) { |
Craig Topper | a2c6019 | 2014-01-21 07:20:05 +0000 | [diff] [blame] | 427 | MatcherPtr.reset(new SwitchTypeMatcher(Cases)); |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 428 | } else { |
| 429 | // If we factored and ended up with one case, create it now. |
Chris Lattner | 6c2d178 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 430 | MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0)); |
Chris Lattner | b9071a2 | 2010-03-07 07:01:28 +0000 | [diff] [blame] | 431 | MatcherPtr->setNext(Cases[0].second); |
| 432 | } |
Chris Lattner | 3e1ffd0 | 2010-03-03 06:28:15 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
Chris Lattner | 62702da0 | 2010-02-25 07:45:24 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 00f2e4b | 2010-03-01 22:19:47 +0000 | [diff] [blame] | 437 | // Reassemble the Scope node with the adjusted children. |
| 438 | Scope->setNumChildren(NewOptionsToMatch.size()); |
| 439 | for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) |
| 440 | Scope->resetChild(i, NewOptionsToMatch[i]); |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Craig Topper | 574f6d4 | 2014-12-15 00:40:07 +0000 | [diff] [blame] | 443 | void |
| 444 | llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr, |
| 445 | const CodeGenDAGPatterns &CGP) { |
Chris Lattner | 102a8a0 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 446 | ContractNodes(MatcherPtr, CGP); |
Chris Lattner | c36ab92 | 2010-02-25 01:57:41 +0000 | [diff] [blame] | 447 | FactorNodes(MatcherPtr); |
Chris Lattner | e732743 | 2010-02-24 07:06:50 +0000 | [diff] [blame] | 448 | } |