Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1 | //===- DAGISelMatcherGen.cpp - Matcher generator --------------------------===// |
| 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 | #include "DAGISelMatcher.h" |
| 11 | #include "CodeGenDAGPatterns.h" |
Jim Grosbach | 4a6d735 | 2011-03-11 02:19:02 +0000 | [diff] [blame] | 12 | #include "CodeGenRegisters.h" |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 15 | #include "llvm/TableGen/Error.h" |
| 16 | #include "llvm/TableGen/Record.h" |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 17 | #include <utility> |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Chris Lattner | c94fa4c | 2010-02-19 00:27:40 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 21 | /// getRegisterValueType - Look up and return the ValueType of the specified |
| 22 | /// register. If the register is a member of multiple register classes which |
| 23 | /// have different associated types, return MVT::Other. |
| 24 | static MVT::SimpleValueType getRegisterValueType(Record *R, |
| 25 | const CodeGenTarget &T) { |
| 26 | bool FoundRC = false; |
| 27 | MVT::SimpleValueType VT = MVT::Other; |
Jakob Stoklund Olesen | ae1920b | 2011-06-15 04:50:36 +0000 | [diff] [blame] | 28 | const CodeGenRegister *Reg = T.getRegBank().getReg(R); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 29 | |
David Blaikie | 7a16f34 | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 30 | for (const auto &RC : T.getRegBank().getRegClasses()) { |
| 31 | if (!RC.contains(Reg)) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 32 | continue; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 34 | if (!FoundRC) { |
| 35 | FoundRC = true; |
Simon Pilgrim | 46fd9f6 | 2018-08-16 15:29:24 +0000 | [diff] [blame] | 36 | const ValueTypeByHwMode &VVT = RC.getValueTypeNum(0); |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 37 | if (VVT.isSimple()) |
| 38 | VT = VVT.getSimple().SimpleTy; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 39 | continue; |
Chris Lattner | c94fa4c | 2010-02-19 00:27:40 +0000 | [diff] [blame] | 40 | } |
Chris Lattner | 67ea6a7 | 2010-03-01 22:49:06 +0000 | [diff] [blame] | 41 | |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 42 | #ifndef NDEBUG |
Simon Pilgrim | 46fd9f6 | 2018-08-16 15:29:24 +0000 | [diff] [blame] | 43 | // If this occurs in multiple register classes, they all have to agree. |
| 44 | const ValueTypeByHwMode &T = RC.getValueTypeNum(0); |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 45 | assert((!T.isSimple() || T.getSimple().SimpleTy == VT) && |
| 46 | "ValueType mismatch between register classes for this register"); |
| 47 | #endif |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 48 | } |
| 49 | return VT; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | namespace { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 54 | class MatcherGen { |
| 55 | const PatternToMatch &Pattern; |
| 56 | const CodeGenDAGPatterns &CGP; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 57 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 58 | /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts |
| 59 | /// out with all of the types removed. This allows us to insert type checks |
| 60 | /// as we scan the tree. |
Florian Hahn | 0b596f0 | 2018-05-30 21:00:18 +0000 | [diff] [blame] | 61 | TreePatternNodePtr PatWithNoTypes; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 62 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 63 | /// VariableMap - A map from variable names ('$dst') to the recorded operand |
| 64 | /// number that they were captured as. These are biased by 1 to make |
| 65 | /// insertion easier. |
| 66 | StringMap<unsigned> VariableMap; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 67 | |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 68 | /// This maintains the recorded operand number that OPC_CheckComplexPattern |
| 69 | /// drops each sub-operand into. We don't want to insert these into |
| 70 | /// VariableMap because that leads to identity checking if they are |
| 71 | /// encountered multiple times. Biased by 1 like VariableMap for |
| 72 | /// consistency. |
| 73 | StringMap<unsigned> NamedComplexPatternOperands; |
| 74 | |
Chris Lattner | c94fa4c | 2010-02-19 00:27:40 +0000 | [diff] [blame] | 75 | /// NextRecordedOperandNo - As we emit opcodes to record matched values in |
| 76 | /// the RecordedNodes array, this keeps track of which slot will be next to |
| 77 | /// record into. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 78 | unsigned NextRecordedOperandNo; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 80 | /// MatchedChainNodes - This maintains the position in the recorded nodes |
| 81 | /// array of all of the recorded input nodes that have chains. |
| 82 | SmallVector<unsigned, 2> MatchedChainNodes; |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 84 | /// MatchedComplexPatterns - This maintains a list of all of the |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 85 | /// ComplexPatterns that we need to check. The second element of each pair |
| 86 | /// is the recorded operand number of the input node. |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 87 | SmallVector<std::pair<const TreePatternNode*, |
| 88 | unsigned>, 2> MatchedComplexPatterns; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 90 | /// PhysRegInputs - List list has an entry for each explicitly specified |
| 91 | /// physreg input to the pattern. The first elt is the Register node, the |
| 92 | /// second is the recorded slot number the input pattern match saved it in. |
| 93 | SmallVector<std::pair<Record*, unsigned>, 2> PhysRegInputs; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 95 | /// Matcher - This is the top level of the generated matcher, the result. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 96 | Matcher *TheMatcher; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 98 | /// CurPredicate - As we emit matcher nodes, this points to the latest check |
Chris Lattner | bd8227f | 2010-02-18 02:53:41 +0000 | [diff] [blame] | 99 | /// which should have future checks stuck into its Next position. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 100 | Matcher *CurPredicate; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 101 | public: |
| 102 | MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 103 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 104 | bool EmitMatcherCode(unsigned Variant); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 105 | void EmitResultCode(); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 106 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 107 | Matcher *GetMatcher() const { return TheMatcher; } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 108 | private: |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 109 | void AddMatcher(Matcher *NewNode); |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 110 | void InferPossibleTypes(unsigned ForceMode); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 111 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 112 | // Matcher Generation. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 113 | void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes, |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 114 | unsigned ForceMode); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 115 | void EmitLeafMatchCode(const TreePatternNode *N); |
| 116 | void EmitOperatorMatchCode(const TreePatternNode *N, |
| 117 | TreePatternNode *NodeNoTypes, |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 118 | unsigned ForceMode); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 119 | |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 120 | /// If this is the first time a node with unique identifier Name has been |
| 121 | /// seen, record it. Otherwise, emit a check to make sure this is the same |
| 122 | /// node. Returns true if this is the first encounter. |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 123 | bool recordUniqueNode(ArrayRef<std::string> Names); |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 124 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 125 | // Result Code Generation. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 126 | unsigned getNamedArgumentSlot(StringRef Name) { |
| 127 | unsigned VarMapEntry = VariableMap[Name]; |
| 128 | assert(VarMapEntry != 0 && |
| 129 | "Variable referenced but not defined and not caught earlier!"); |
| 130 | return VarMapEntry-1; |
| 131 | } |
| 132 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 133 | void EmitResultOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 134 | SmallVectorImpl<unsigned> &ResultOps); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 135 | void EmitResultOfNamedOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 136 | SmallVectorImpl<unsigned> &ResultOps); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 137 | void EmitResultLeafAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 138 | SmallVectorImpl<unsigned> &ResultOps); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 139 | void EmitResultInstructionAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 140 | SmallVectorImpl<unsigned> &ResultOps); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 141 | void EmitResultSDNodeXFormAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 142 | SmallVectorImpl<unsigned> &ResultOps); |
| 143 | }; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 144 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 145 | } // end anon namespace. |
| 146 | |
| 147 | MatcherGen::MatcherGen(const PatternToMatch &pattern, |
| 148 | const CodeGenDAGPatterns &cgp) |
Chris Lattner | 853b919 | 2010-02-19 00:33:13 +0000 | [diff] [blame] | 149 | : Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0), |
Craig Topper | 095734c | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 150 | TheMatcher(nullptr), CurPredicate(nullptr) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 151 | // We need to produce the matcher tree for the patterns source pattern. To do |
| 152 | // this we need to match the structure as well as the types. To do the type |
| 153 | // matching, we want to figure out the fewest number of type checks we need to |
| 154 | // emit. For example, if there is only one integer type supported by a |
| 155 | // target, there should be no type comparisons at all for integer patterns! |
| 156 | // |
| 157 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 158 | // remove the types, then perform type inference on the pattern as a whole. |
| 159 | // If there are unresolved types, emit an explicit check for those types, |
| 160 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 161 | // types are resolved. |
| 162 | // |
| 163 | PatWithNoTypes = Pattern.getSrcPattern()->clone(); |
| 164 | PatWithNoTypes->RemoveAllTypes(); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 165 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 166 | // If there are types that are manifestly known, infer them. |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 167 | InferPossibleTypes(Pattern.ForceMode); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /// InferPossibleTypes - As we emit the pattern, we end up generating type |
| 171 | /// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we |
| 172 | /// want to propagate implied types as far throughout the tree as possible so |
| 173 | /// that we avoid doing redundant type checks. This does the type propagation. |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 174 | void MatcherGen::InferPossibleTypes(unsigned ForceMode) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 175 | // TP - Get *SOME* tree pattern, we don't care which. It is only used for |
| 176 | // diagnostics, which we know are impossible at this point. |
| 177 | TreePattern &TP = *CGP.pf_begin()->second; |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 178 | TP.getInfer().CodeGen = true; |
| 179 | TP.getInfer().ForceMode = ForceMode; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 180 | |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 181 | bool MadeChange = true; |
| 182 | while (MadeChange) |
| 183 | MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP, |
| 184 | true/*Ignore reg constraints*/); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 188 | /// AddMatcher - Add a matcher node to the current graph we're building. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 189 | void MatcherGen::AddMatcher(Matcher *NewNode) { |
Craig Topper | 095734c | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 190 | if (CurPredicate) |
Chris Lattner | bd8227f | 2010-02-18 02:53:41 +0000 | [diff] [blame] | 191 | CurPredicate->setNext(NewNode); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 192 | else |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 193 | TheMatcher = NewNode; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 194 | CurPredicate = NewNode; |
| 195 | } |
| 196 | |
| 197 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 198 | //===----------------------------------------------------------------------===// |
| 199 | // Pattern Match Generation |
| 200 | //===----------------------------------------------------------------------===// |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 201 | |
| 202 | /// EmitLeafMatchCode - Generate matching code for leaf nodes. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 203 | void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) { |
| 204 | assert(N->isLeaf() && "Not a leaf?"); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 205 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 206 | // Direct match against an integer constant. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 207 | if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) { |
Chris Lattner | bd8965a | 2010-03-01 07:27:07 +0000 | [diff] [blame] | 208 | // If this is the root of the dag we're matching, we emit a redundant opcode |
| 209 | // check to ensure that this gets folded into the normal top-level |
| 210 | // OpcodeSwitch. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 211 | if (N == Pattern.getSrcPattern()) { |
Chris Lattner | bd8965a | 2010-03-01 07:27:07 +0000 | [diff] [blame] | 212 | const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm")); |
| 213 | AddMatcher(new CheckOpcodeMatcher(NI)); |
| 214 | } |
| 215 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 216 | return AddMatcher(new CheckIntegerMatcher(II->getValue())); |
Chris Lattner | bd8965a | 2010-03-01 07:27:07 +0000 | [diff] [blame] | 217 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 218 | |
Jakob Stoklund Olesen | 8e3cb3e | 2013-03-24 19:37:00 +0000 | [diff] [blame] | 219 | // An UnsetInit represents a named node without any constraints. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 220 | if (isa<UnsetInit>(N->getLeafValue())) { |
| 221 | assert(N->hasName() && "Unnamed ? leaf"); |
Jakob Stoklund Olesen | 8e3cb3e | 2013-03-24 19:37:00 +0000 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 225 | DefInit *DI = dyn_cast<DefInit>(N->getLeafValue()); |
Craig Topper | 095734c | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 226 | if (!DI) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 227 | errs() << "Unknown leaf kind: " << *N << "\n"; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 228 | abort(); |
| 229 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 230 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 231 | Record *LeafRec = DI->getDef(); |
Jakob Stoklund Olesen | f0a804d | 2013-03-23 20:35:01 +0000 | [diff] [blame] | 232 | |
| 233 | // A ValueType leaf node can represent a register when named, or itself when |
| 234 | // unnamed. |
| 235 | if (LeafRec->isSubClassOf("ValueType")) { |
| 236 | // A named ValueType leaf always matches: (add i32:$a, i32:$b). |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 237 | if (N->hasName()) |
Jakob Stoklund Olesen | f0a804d | 2013-03-23 20:35:01 +0000 | [diff] [blame] | 238 | return; |
| 239 | // An unnamed ValueType as in (sext_inreg GPR:$foo, i8). |
| 240 | return AddMatcher(new CheckValueTypeMatcher(LeafRec->getName())); |
| 241 | } |
| 242 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 243 | if (// Handle register references. Nothing to do here, they always match. |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 244 | LeafRec->isSubClassOf("RegisterClass") || |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 245 | LeafRec->isSubClassOf("RegisterOperand") || |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 246 | LeafRec->isSubClassOf("PointerLikeRegClass") || |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 247 | LeafRec->isSubClassOf("SubRegIndex") || |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 248 | // Place holder for SRCVALUE nodes. Nothing to do here. |
| 249 | LeafRec->getName() == "srcvalue") |
| 250 | return; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 251 | |
| 252 | // If we have a physreg reference like (mul gpr:$src, EAX) then we need to |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 253 | // record the register |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 254 | if (LeafRec->isSubClassOf("Register")) { |
Matthias Braun | 0c517c8 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 255 | AddMatcher(new RecordMatcher("physreg input "+LeafRec->getName().str(), |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 256 | NextRecordedOperandNo)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 257 | PhysRegInputs.push_back(std::make_pair(LeafRec, NextRecordedOperandNo++)); |
| 258 | return; |
| 259 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 260 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 261 | if (LeafRec->isSubClassOf("CondCode")) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 262 | return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName())); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 263 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 264 | if (LeafRec->isSubClassOf("ComplexPattern")) { |
Chris Lattner | c2676b2 | 2010-02-17 00:11:30 +0000 | [diff] [blame] | 265 | // We can't model ComplexPattern uses that don't have their name taken yet. |
| 266 | // The OPC_CheckComplexPattern operation implicitly records the results. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 267 | if (N->getName().empty()) { |
James Y Knight | aeda490 | 2015-05-11 22:17:13 +0000 | [diff] [blame] | 268 | std::string S; |
| 269 | raw_string_ostream OS(S); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 270 | OS << "We expect complex pattern uses to have names: " << *N; |
James Y Knight | aeda490 | 2015-05-11 22:17:13 +0000 | [diff] [blame] | 271 | PrintFatalError(OS.str()); |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | 1f2ed5f | 2010-02-22 22:18:05 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 274 | // Remember this ComplexPattern so that we can emit it after all the other |
| 275 | // structural matches are done. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 276 | unsigned InputOperand = VariableMap[N->getName()] - 1; |
| 277 | MatchedComplexPatterns.push_back(std::make_pair(N, InputOperand)); |
Chris Lattner | 8dc4f2b | 2010-02-17 06:08:25 +0000 | [diff] [blame] | 278 | return; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 279 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 280 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 281 | errs() << "Unknown leaf kind: " << *N << "\n"; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 282 | abort(); |
| 283 | } |
| 284 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 285 | void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N, |
| 286 | TreePatternNode *NodeNoTypes, |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 287 | unsigned ForceMode) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 288 | assert(!N->isLeaf() && "Not an operator?"); |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 289 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 290 | if (N->getOperator()->isSubClassOf("ComplexPattern")) { |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 291 | // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is |
| 292 | // "MY_PAT:op1:op2". We should already have validated that the uses are |
| 293 | // consistent. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 294 | std::string PatternName = N->getOperator()->getName(); |
| 295 | for (unsigned i = 0; i < N->getNumChildren(); ++i) { |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 296 | PatternName += ":"; |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 297 | PatternName += N->getChild(i)->getName(); |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (recordUniqueNode(PatternName)) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 301 | auto NodeAndOpNum = std::make_pair(N, NextRecordedOperandNo - 1); |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 302 | MatchedComplexPatterns.push_back(NodeAndOpNum); |
| 303 | } |
| 304 | |
| 305 | return; |
| 306 | } |
| 307 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 308 | const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator()); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 309 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 310 | // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is |
Lama Saba | b7014e3 | 2017-07-30 20:12:17 +0000 | [diff] [blame] | 311 | // a constant without a predicate fn that has more than one bit set, handle |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 312 | // this as a special case. This is usually for targets that have special |
| 313 | // handling of certain large constants (e.g. alpha with it's 8/16/32-bit |
| 314 | // handling stuff). Using these instructions is often far more efficient |
| 315 | // than materializing the constant. Unfortunately, both the instcombiner |
| 316 | // and the dag combiner can often infer that bits are dead, and thus drop |
| 317 | // them from the mask in the dag. For example, it might turn 'AND X, 255' |
| 318 | // into 'AND X, 254' if it knows the low bit is set. Emit code that checks |
| 319 | // to handle this. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 320 | if ((N->getOperator()->getName() == "and" || |
| 321 | N->getOperator()->getName() == "or") && |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 322 | N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateCalls().empty() && |
| 323 | N->getPredicateCalls().empty()) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 324 | if (IntInit *II = dyn_cast<IntInit>(N->getChild(1)->getLeafValue())) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 325 | if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits. |
Chris Lattner | e9eeda8 | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 326 | // If this is at the root of the pattern, we emit a redundant |
| 327 | // CheckOpcode so that the following checks get factored properly under |
| 328 | // a single opcode check. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 329 | if (N == Pattern.getSrcPattern()) |
Chris Lattner | e9eeda8 | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 330 | AddMatcher(new CheckOpcodeMatcher(CInfo)); |
| 331 | |
| 332 | // Emit the CheckAndImm/CheckOrImm node. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 333 | if (N->getOperator()->getName() == "and") |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 334 | AddMatcher(new CheckAndImmMatcher(II->getValue())); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 335 | else |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 336 | AddMatcher(new CheckOrImmMatcher(II->getValue())); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 337 | |
| 338 | // Match the LHS of the AND as appropriate. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 339 | AddMatcher(new MoveChildMatcher(0)); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 340 | EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0), ForceMode); |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 341 | AddMatcher(new MoveParentMatcher()); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | } |
| 345 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 346 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 347 | // Check that the current opcode lines up. |
Chris Lattner | a230f96 | 2010-02-27 21:48:43 +0000 | [diff] [blame] | 348 | AddMatcher(new CheckOpcodeMatcher(CInfo)); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 350 | // If this node has memory references (i.e. is a load or store), tell the |
| 351 | // interpreter to capture them in the memref array. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 352 | if (N->NodeHasProperty(SDNPMemOperand, CGP)) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 353 | AddMatcher(new RecordMemRefMatcher()); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 354 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 355 | // If this node has a chain, then the chain is operand #0 is the SDNode, and |
| 356 | // the child numbers of the node are all offset by one. |
| 357 | unsigned OpNo = 0; |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 358 | if (N->NodeHasProperty(SDNPHasChain, CGP)) { |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 359 | // Record the node and remember it in our chained nodes list. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 360 | AddMatcher(new RecordMatcher("'" + N->getOperator()->getName().str() + |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 361 | "' chained node", |
| 362 | NextRecordedOperandNo)); |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 363 | // Remember all of the input chains our pattern will match. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 364 | MatchedChainNodes.push_back(NextRecordedOperandNo++); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 2f7ecde | 2010-02-17 01:34:15 +0000 | [diff] [blame] | 366 | // Don't look at the input chain when matching the tree pattern to the |
| 367 | // SDNode. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 368 | OpNo = 1; |
| 369 | |
Chris Lattner | 6bc1b51 | 2010-02-16 19:19:58 +0000 | [diff] [blame] | 370 | // If this node is not the root and the subtree underneath it produces a |
| 371 | // chain, then the result of matching the node is also produce a chain. |
| 372 | // Beyond that, this means that we're also folding (at least) the root node |
| 373 | // into the node that produce the chain (for example, matching |
| 374 | // "(add reg, (load ptr))" as a add_with_memory on X86). This is |
| 375 | // problematic, if the 'reg' node also uses the load (say, its chain). |
| 376 | // Graphically: |
| 377 | // |
| 378 | // [LD] |
| 379 | // ^ ^ |
| 380 | // | \ DAG's like cheese. |
| 381 | // / | |
| 382 | // / [YY] |
| 383 | // | ^ |
| 384 | // [XX]--/ |
| 385 | // |
| 386 | // It would be invalid to fold XX and LD. In this case, folding the two |
| 387 | // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG' |
| 388 | // To prevent this, we emit a dynamic check for legality before allowing |
| 389 | // this to be folded. |
| 390 | // |
| 391 | const TreePatternNode *Root = Pattern.getSrcPattern(); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 392 | if (N != Root) { // Not the root of the pattern. |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 393 | // If there is a node between the root and this node, then we definitely |
| 394 | // need to emit the check. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 395 | bool NeedCheck = !Root->hasChild(N); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 396 | |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 397 | // If it *is* an immediate child of the root, we can still need a check if |
| 398 | // the root SDNode has multiple inputs. For us, this means that it is an |
| 399 | // intrinsic, has multiple operands, or has other inputs like chain or |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 400 | // glue). |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 401 | if (!NeedCheck) { |
| 402 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator()); |
| 403 | NeedCheck = |
| 404 | Root->getOperator() == CGP.get_intrinsic_void_sdnode() || |
| 405 | Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() || |
| 406 | Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() || |
| 407 | PInfo.getNumOperands() > 1 || |
| 408 | PInfo.hasProperty(SDNPHasChain) || |
Chris Lattner | 036609b | 2010-12-23 18:28:41 +0000 | [diff] [blame] | 409 | PInfo.hasProperty(SDNPInGlue) || |
| 410 | PInfo.hasProperty(SDNPOptInGlue); |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 411 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 412 | |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 413 | if (NeedCheck) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 414 | AddMatcher(new CheckFoldableChainNodeMatcher()); |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 415 | } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 418 | // If this node has an output glue and isn't the root, remember it. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 419 | if (N->NodeHasProperty(SDNPOutGlue, CGP) && |
| 420 | N != Pattern.getSrcPattern()) { |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 421 | // TODO: This redundantly records nodes with both glues and chains. |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 423 | // Record the node and remember it in our chained nodes list. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 424 | AddMatcher(new RecordMatcher("'" + N->getOperator()->getName().str() + |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 425 | "' glue output node", |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 426 | NextRecordedOperandNo)); |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 427 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 429 | // If this node is known to have an input glue or if it *might* have an input |
| 430 | // glue, capture it as the glue input of the pattern. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 431 | if (N->NodeHasProperty(SDNPOptInGlue, CGP) || |
| 432 | N->NodeHasProperty(SDNPInGlue, CGP)) |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 433 | AddMatcher(new CaptureGlueInputMatcher()); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 434 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 435 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 436 | // Get the code suitable for matching this child. Move to the child, check |
| 437 | // it then move back to the parent. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 438 | AddMatcher(new MoveChildMatcher(OpNo)); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 439 | EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i), ForceMode); |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 440 | AddMatcher(new MoveParentMatcher()); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 444 | bool MatcherGen::recordUniqueNode(ArrayRef<std::string> Names) { |
| 445 | unsigned Entry = 0; |
| 446 | for (const std::string &Name : Names) { |
| 447 | unsigned &VarMapEntry = VariableMap[Name]; |
| 448 | if (!Entry) |
| 449 | Entry = VarMapEntry; |
| 450 | assert(Entry == VarMapEntry); |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 453 | bool NewRecord = false; |
| 454 | if (Entry == 0) { |
| 455 | // If it is a named node, we must emit a 'Record' opcode. |
| 456 | std::string WhatFor; |
| 457 | for (const std::string &Name : Names) { |
| 458 | if (!WhatFor.empty()) |
| 459 | WhatFor += ','; |
| 460 | WhatFor += "$" + Name; |
| 461 | } |
| 462 | AddMatcher(new RecordMatcher(WhatFor, NextRecordedOperandNo)); |
| 463 | Entry = ++NextRecordedOperandNo; |
| 464 | NewRecord = true; |
| 465 | } else { |
| 466 | // If we get here, this is a second reference to a specific name. Since |
| 467 | // we already have checked that the first reference is valid, we don't |
| 468 | // have to recursively match it, just check that it's the same as the |
| 469 | // previously named thing. |
| 470 | AddMatcher(new CheckSameMatcher(Entry-1)); |
| 471 | } |
| 472 | |
| 473 | for (const std::string &Name : Names) |
| 474 | VariableMap[Name] = Entry; |
| 475 | |
| 476 | return NewRecord; |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 478 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 479 | void MatcherGen::EmitMatchCode(const TreePatternNode *N, |
| 480 | TreePatternNode *NodeNoTypes, |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 481 | unsigned ForceMode) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 482 | // If N and NodeNoTypes don't agree on a type, then this is a case where we |
Craig Topper | 858594e | 2014-01-25 17:34:23 +0000 | [diff] [blame] | 483 | // need to do a type check. Emit the check, apply the type to NodeNoTypes and |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 484 | // reinfer any correlated types. |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 485 | SmallVector<unsigned, 2> ResultsToTypeCheck; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 486 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 487 | for (unsigned i = 0, e = NodeNoTypes->getNumTypes(); i != e; ++i) { |
| 488 | if (NodeNoTypes->getExtType(i) == N->getExtType(i)) continue; |
| 489 | NodeNoTypes->setType(i, N->getExtType(i)); |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 490 | InferPossibleTypes(ForceMode); |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 491 | ResultsToTypeCheck.push_back(i); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 492 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 493 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 494 | // If this node has a name associated with it, capture it in VariableMap. If |
| 495 | // we already saw this in the pattern, emit code to verify dagness. |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 496 | SmallVector<std::string, 4> Names; |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 497 | if (!N->getName().empty()) |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 498 | Names.push_back(N->getName()); |
| 499 | |
| 500 | for (const ScopedName &Name : N->getNamesAsPredicateArg()) { |
| 501 | Names.push_back(("pred:" + Twine(Name.getScope()) + ":" + Name.getIdentifier()).str()); |
| 502 | } |
| 503 | |
| 504 | if (!Names.empty()) { |
| 505 | if (!recordUniqueNode(Names)) |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 506 | return; |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 507 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 508 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 509 | if (N->isLeaf()) |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 510 | EmitLeafMatchCode(N); |
| 511 | else |
Krzysztof Parzyszek | db81564 | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 512 | EmitOperatorMatchCode(N, NodeNoTypes, ForceMode); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 514 | // If there are node predicates for this node, generate their checks. |
Nicolai Haehnle | 98272e4 | 2018-11-30 14:15:13 +0000 | [diff] [blame] | 515 | for (unsigned i = 0, e = N->getPredicateCalls().size(); i != e; ++i) { |
| 516 | const TreePredicateCall &Pred = N->getPredicateCalls()[i]; |
| 517 | SmallVector<unsigned, 4> Operands; |
| 518 | if (Pred.Fn.usesOperands()) { |
| 519 | TreePattern *TP = Pred.Fn.getOrigPatFragRecord(); |
| 520 | for (unsigned i = 0; i < TP->getNumArgs(); ++i) { |
| 521 | std::string Name = |
| 522 | ("pred:" + Twine(Pred.Scope) + ":" + TP->getArgName(i)).str(); |
| 523 | Operands.push_back(getNamedArgumentSlot(Name)); |
| 524 | } |
| 525 | } |
| 526 | AddMatcher(new CheckPredicateMatcher(Pred.Fn, Operands)); |
| 527 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 529 | for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i) |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 530 | AddMatcher(new CheckTypeMatcher(N->getSimpleType(ResultsToTypeCheck[i]), |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 531 | ResultsToTypeCheck[i])); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 534 | /// EmitMatcherCode - Generate the code that matches the predicate of this |
| 535 | /// pattern for the specified Variant. If the variant is invalid this returns |
| 536 | /// true and does not generate code, if it is valid, it returns false. |
| 537 | bool MatcherGen::EmitMatcherCode(unsigned Variant) { |
| 538 | // If the root of the pattern is a ComplexPattern and if it is specified to |
| 539 | // match some number of root opcodes, these are considered to be our variants. |
| 540 | // Depending on which variant we're generating code for, emit the root opcode |
| 541 | // check. |
| 542 | if (const ComplexPattern *CP = |
| 543 | Pattern.getSrcPattern()->getComplexPatternInfo(CGP)) { |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 544 | const std::vector<Record*> &OpNodes = CP->getRootNodes(); |
Chris Lattner | 405f125 | 2010-03-01 22:29:19 +0000 | [diff] [blame] | 545 | assert(!OpNodes.empty() &&"Complex Pattern must specify what it can match"); |
| 546 | if (Variant >= OpNodes.size()) return true; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 405f125 | 2010-03-01 22:29:19 +0000 | [diff] [blame] | 548 | AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant]))); |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 549 | } else { |
| 550 | if (Variant != 0) return true; |
| 551 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 9752fb1 | 2010-03-04 01:25:36 +0000 | [diff] [blame] | 553 | // Emit the matcher for the pattern structure and types. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 554 | EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes.get(), |
Florian Hahn | 0b596f0 | 2018-05-30 21:00:18 +0000 | [diff] [blame] | 555 | Pattern.ForceMode); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 556 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 557 | // If the pattern has a predicate on it (e.g. only enabled when a subtarget |
| 558 | // feature is around, do the check). |
| 559 | if (!Pattern.getPredicateCheck().empty()) |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 560 | AddMatcher(new CheckPatternPredicateMatcher(Pattern.getPredicateCheck())); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 562 | // Now that we've completed the structural type match, emit any ComplexPattern |
| 563 | // checks (e.g. addrmode matches). We emit this after the structural match |
| 564 | // because they are generally more expensive to evaluate and more difficult to |
| 565 | // factor. |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 566 | for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) { |
Florian Hahn | 0b596f0 | 2018-05-30 21:00:18 +0000 | [diff] [blame] | 567 | auto N = MatchedComplexPatterns[i].first; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 569 | // Remember where the results of this match get stuck. |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 570 | if (N->isLeaf()) { |
| 571 | NamedComplexPatternOperands[N->getName()] = NextRecordedOperandNo + 1; |
| 572 | } else { |
| 573 | unsigned CurOp = NextRecordedOperandNo; |
| 574 | for (unsigned i = 0; i < N->getNumChildren(); ++i) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 575 | NamedComplexPatternOperands[N->getChild(i)->getName()] = CurOp + 1; |
| 576 | CurOp += N->getChild(i)->getNumMIResults(CGP); |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 579 | |
| 580 | // Get the slot we recorded the value in from the name on the node. |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 581 | unsigned RecNodeEntry = MatchedComplexPatterns[i].second; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 582 | |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 583 | const ComplexPattern &CP = *N->getComplexPatternInfo(CGP); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 585 | // Emit a CheckComplexPat operation, which does the match (aborting if it |
| 586 | // fails) and pushes the matched operands onto the recorded nodes list. |
| 587 | AddMatcher(new CheckComplexPatMatcher(CP, RecNodeEntry, |
| 588 | N->getName(), NextRecordedOperandNo)); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 590 | // Record the right number of operands. |
| 591 | NextRecordedOperandNo += CP.getNumOperands(); |
| 592 | if (CP.hasProperty(SDNPHasChain)) { |
| 593 | // If the complex pattern has a chain, then we need to keep track of the |
| 594 | // fact that we just recorded a chain input. The chain input will be |
| 595 | // matched as the last operand of the predicate if it was successful. |
| 596 | ++NextRecordedOperandNo; // Chained node operand. |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 598 | // It is the last operand recorded. |
| 599 | assert(NextRecordedOperandNo > 1 && |
| 600 | "Should have recorded input/result chains at least!"); |
| 601 | MatchedChainNodes.push_back(NextRecordedOperandNo-1); |
| 602 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 604 | // TODO: Complex patterns can't have output glues, if they did, we'd want |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 605 | // to record them. |
| 606 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 607 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 608 | return false; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 612 | //===----------------------------------------------------------------------===// |
| 613 | // Node Result Generation |
| 614 | //===----------------------------------------------------------------------===// |
| 615 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 616 | void MatcherGen::EmitResultOfNamedOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 617 | SmallVectorImpl<unsigned> &ResultOps){ |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 618 | assert(!N->getName().empty() && "Operand not named!"); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 619 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 620 | if (unsigned SlotNo = NamedComplexPatternOperands[N->getName()]) { |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 621 | // Complex operands have already been completely selected, just find the |
| 622 | // right slot ant add the arguments directly. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 623 | for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i) |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 624 | ResultOps.push_back(SlotNo - 1 + i); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 629 | unsigned SlotNo = getNamedArgumentSlot(N->getName()); |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 631 | // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target |
| 632 | // version of the immediate so that it doesn't get selected due to some other |
| 633 | // node use. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 634 | if (!N->isLeaf()) { |
| 635 | StringRef OperatorName = N->getOperator()->getName(); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 636 | if (OperatorName == "imm" || OperatorName == "fpimm") { |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 637 | AddMatcher(new EmitConvertToTargetMatcher(SlotNo)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 638 | ResultOps.push_back(NextRecordedOperandNo++); |
| 639 | return; |
| 640 | } |
| 641 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 642 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 643 | for (unsigned i = 0; i < N->getNumMIResults(CGP); ++i) |
Tim Northover | ee8d5c3 | 2014-05-20 11:52:46 +0000 | [diff] [blame] | 644 | ResultOps.push_back(SlotNo + i); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 647 | void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 648 | SmallVectorImpl<unsigned> &ResultOps) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 649 | assert(N->isLeaf() && "Must be a leaf"); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 650 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 651 | if (IntInit *II = dyn_cast<IntInit>(N->getLeafValue())) { |
| 652 | AddMatcher(new EmitIntegerMatcher(II->getValue(), N->getSimpleType(0))); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 653 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 654 | return; |
| 655 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 657 | // If this is an explicit register reference, handle it. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 658 | if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) { |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 659 | Record *Def = DI->getDef(); |
| 660 | if (Def->isSubClassOf("Register")) { |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 661 | const CodeGenRegister *Reg = |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 662 | CGP.getTargetInfo().getRegBank().getReg(Def); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 663 | AddMatcher(new EmitRegisterMatcher(Reg, N->getSimpleType(0))); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 664 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 667 | |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 668 | if (Def->getName() == "zero_reg") { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 669 | AddMatcher(new EmitRegisterMatcher(nullptr, N->getSimpleType(0))); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 670 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 674 | // Handle a reference to a register class. This is used |
| 675 | // in COPY_TO_SUBREG instructions. |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 676 | if (Def->isSubClassOf("RegisterOperand")) |
| 677 | Def = Def->getValueAsDef("RegClass"); |
| 678 | if (Def->isSubClassOf("RegisterClass")) { |
| 679 | std::string Value = getQualifiedName(Def) + "RegClassID"; |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 680 | AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 681 | ResultOps.push_back(NextRecordedOperandNo++); |
| 682 | return; |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 683 | } |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 684 | |
| 685 | // Handle a subregister index. This is used for INSERT_SUBREG etc. |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 686 | if (Def->isSubClassOf("SubRegIndex")) { |
| 687 | std::string Value = getQualifiedName(Def); |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 688 | AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32)); |
| 689 | ResultOps.push_back(NextRecordedOperandNo++); |
| 690 | return; |
| 691 | } |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 692 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 693 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 694 | errs() << "unhandled leaf node: \n"; |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 695 | N->dump(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 698 | static bool |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 699 | mayInstNodeLoadOrStore(const TreePatternNode *N, |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 700 | const CodeGenDAGPatterns &CGP) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 701 | Record *Op = N->getOperator(); |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 702 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
| 703 | CodeGenInstruction &II = CGT.getInstruction(Op); |
| 704 | return II.mayLoad || II.mayStore; |
| 705 | } |
| 706 | |
| 707 | static unsigned |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 708 | numNodesThatMayLoadOrStore(const TreePatternNode *N, |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 709 | const CodeGenDAGPatterns &CGP) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 710 | if (N->isLeaf()) |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 711 | return 0; |
| 712 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 713 | Record *OpRec = N->getOperator(); |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 714 | if (!OpRec->isSubClassOf("Instruction")) |
| 715 | return 0; |
| 716 | |
| 717 | unsigned Count = 0; |
| 718 | if (mayInstNodeLoadOrStore(N, CGP)) |
| 719 | ++Count; |
| 720 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 721 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 722 | Count += numNodesThatMayLoadOrStore(N->getChild(i), CGP); |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 723 | |
| 724 | return Count; |
| 725 | } |
| 726 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 727 | void MatcherGen:: |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 728 | EmitResultInstructionAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 729 | SmallVectorImpl<unsigned> &OutputOps) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 730 | Record *Op = N->getOperator(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 731 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
Chris Lattner | f30187a | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 732 | CodeGenInstruction &II = CGT.getInstruction(Op); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 733 | const DAGInstruction &Inst = CGP.getInstruction(Op); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 734 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 735 | bool isRoot = N == Pattern.getDstPattern(); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 737 | // TreeHasOutGlue - True if this tree has glue. |
| 738 | bool TreeHasInGlue = false, TreeHasOutGlue = false; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 739 | if (isRoot) { |
| 740 | const TreePatternNode *SrcPat = Pattern.getSrcPattern(); |
Chris Lattner | 036609b | 2010-12-23 18:28:41 +0000 | [diff] [blame] | 741 | TreeHasInGlue = SrcPat->TreeHasProperty(SDNPOptInGlue, CGP) || |
| 742 | SrcPat->TreeHasProperty(SDNPInGlue, CGP); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 744 | // FIXME2: this is checking the entire pattern, not just the node in |
| 745 | // question, doing this just for the root seems like a total hack. |
Chris Lattner | 036609b | 2010-12-23 18:28:41 +0000 | [diff] [blame] | 746 | TreeHasOutGlue = SrcPat->TreeHasProperty(SDNPOutGlue, CGP); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // NumResults - This is the number of results produced by the instruction in |
| 750 | // the "outs" list. |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 751 | unsigned NumResults = Inst.getNumResults(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 752 | |
Matt Arsenault | 2220408 | 2014-11-02 23:46:51 +0000 | [diff] [blame] | 753 | // Number of operands we know the output instruction must have. If it is |
| 754 | // variadic, we could have more operands. |
| 755 | unsigned NumFixedOperands = II.Operands.size(); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 756 | |
Matt Arsenault | 2220408 | 2014-11-02 23:46:51 +0000 | [diff] [blame] | 757 | SmallVector<unsigned, 8> InstOps; |
| 758 | |
| 759 | // Loop over all of the fixed operands of the instruction pattern, emitting |
| 760 | // code to fill them all in. The node 'N' usually has number children equal to |
| 761 | // the number of input operands of the instruction. However, in cases where |
| 762 | // there are predicate operands for an instruction, we need to fill in the |
| 763 | // 'execute always' values. Match up the node operands to the instruction |
| 764 | // operands to do this. |
| 765 | unsigned ChildNo = 0; |
| 766 | for (unsigned InstOpNo = NumResults, e = NumFixedOperands; |
| 767 | InstOpNo != e; ++InstOpNo) { |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 768 | // Determine what to emit for this operand. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 769 | Record *OperandNode = II.Operands[InstOpNo].Rec; |
Tom Stellard | 6d3d765 | 2012-09-06 14:15:52 +0000 | [diff] [blame] | 770 | if (OperandNode->isSubClassOf("OperandWithDefaultOps") && |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 771 | !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) { |
| 772 | // This is a predicate or optional def operand; emit the |
| 773 | // 'default ops' operands. |
Eric Christopher | ae321ed | 2010-08-10 23:46:20 +0000 | [diff] [blame] | 774 | const DAGDefaultOperand &DefaultOp |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 775 | = CGP.getDefaultOperand(OperandNode); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 776 | for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 777 | EmitResultOperand(DefaultOp.DefaultOps[i].get(), InstOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 778 | continue; |
| 779 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 780 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 781 | // Otherwise this is a normal operand or a predicate operand without |
| 782 | // 'execute always'; emit it. |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 783 | |
Ulrich Weigand | ec8d1a5 | 2013-03-19 19:51:09 +0000 | [diff] [blame] | 784 | // For operands with multiple sub-operands we may need to emit |
| 785 | // multiple child patterns to cover them all. However, ComplexPattern |
| 786 | // children may themselves emit multiple MI operands. |
| 787 | unsigned NumSubOps = 1; |
| 788 | if (OperandNode->isSubClassOf("Operand")) { |
| 789 | DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo"); |
| 790 | if (unsigned NumArgs = MIOpInfo->getNumArgs()) |
| 791 | NumSubOps = NumArgs; |
| 792 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 793 | |
Ulrich Weigand | ec8d1a5 | 2013-03-19 19:51:09 +0000 | [diff] [blame] | 794 | unsigned FinalNumOps = InstOps.size() + NumSubOps; |
| 795 | while (InstOps.size() < FinalNumOps) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 796 | const TreePatternNode *Child = N->getChild(ChildNo); |
Ulrich Weigand | ec8d1a5 | 2013-03-19 19:51:09 +0000 | [diff] [blame] | 797 | unsigned BeforeAddingNumOps = InstOps.size(); |
| 798 | EmitResultOperand(Child, InstOps); |
| 799 | assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands"); |
| 800 | |
| 801 | // If the operand is an instruction and it produced multiple results, just |
| 802 | // take the first one. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 803 | if (!Child->isLeaf() && Child->getOperator()->isSubClassOf("Instruction")) |
Ulrich Weigand | ec8d1a5 | 2013-03-19 19:51:09 +0000 | [diff] [blame] | 804 | InstOps.resize(BeforeAddingNumOps+1); |
| 805 | |
| 806 | ++ChildNo; |
| 807 | } |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 808 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 809 | |
Matt Arsenault | 2220408 | 2014-11-02 23:46:51 +0000 | [diff] [blame] | 810 | // If this is a variadic output instruction (i.e. REG_SEQUENCE), we can't |
| 811 | // expand suboperands, use default operands, or other features determined from |
| 812 | // the CodeGenInstruction after the fixed operands, which were handled |
| 813 | // above. Emit the remaining instructions implicitly added by the use for |
| 814 | // variable_ops. |
| 815 | if (II.Operands.isVariadic) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 816 | for (unsigned I = ChildNo, E = N->getNumChildren(); I < E; ++I) |
| 817 | EmitResultOperand(N->getChild(I), InstOps); |
Matt Arsenault | 2220408 | 2014-11-02 23:46:51 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 820 | // If this node has input glue or explicitly specified input physregs, we |
| 821 | // need to add chained and glued copyfromreg nodes and materialize the glue |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 822 | // input. |
| 823 | if (isRoot && !PhysRegInputs.empty()) { |
| 824 | // Emit all of the CopyToReg nodes for the input physical registers. These |
| 825 | // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src). |
| 826 | for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 827 | AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second, |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 828 | PhysRegInputs[i].first)); |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 829 | // Even if the node has no other glue inputs, the resultant node must be |
| 830 | // glued to the CopyFromReg nodes we just generated. |
| 831 | TreeHasInGlue = true; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 832 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 834 | // Result order: node results, chain, glue |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 836 | // Determine the result types. |
| 837 | SmallVector<MVT::SimpleValueType, 4> ResultVTs; |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 838 | for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) |
| 839 | ResultVTs.push_back(N->getSimpleType(i)); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 840 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 841 | // If this is the root instruction of a pattern that has physical registers in |
| 842 | // its result pattern, add output VTs for them. For example, X86 has: |
| 843 | // (set AL, (mul ...)) |
| 844 | // This also handles implicit results like: |
| 845 | // (implicit EFLAGS) |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 846 | if (isRoot && !Pattern.getDstRegs().empty()) { |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 847 | // If the root came from an implicit def in the instruction handling stuff, |
| 848 | // don't re-add it. |
Craig Topper | 095734c | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 849 | Record *HandledReg = nullptr; |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 850 | if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other) |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 851 | HandledReg = II.ImplicitDefs[0]; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 852 | |
Simon Pilgrim | 5aac6c6 | 2017-06-19 13:24:12 +0000 | [diff] [blame] | 853 | for (Record *Reg : Pattern.getDstRegs()) { |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 854 | if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue; |
| 855 | ResultVTs.push_back(getRegisterValueType(Reg, CGT)); |
| 856 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 857 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 1e50631 | 2010-03-19 05:34:15 +0000 | [diff] [blame] | 859 | // If this is the root of the pattern and the pattern we're matching includes |
| 860 | // a node that is variadic, mark the generated node as variadic so that it |
| 861 | // gets the excess operands from the input DAG. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 862 | int NumFixedArityOperands = -1; |
Chris Lattner | 1e50631 | 2010-03-19 05:34:15 +0000 | [diff] [blame] | 863 | if (isRoot && |
Matt Arsenault | 2220408 | 2014-11-02 23:46:51 +0000 | [diff] [blame] | 864 | Pattern.getSrcPattern()->NodeHasProperty(SDNPVariadic, CGP)) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 865 | NumFixedArityOperands = Pattern.getSrcPattern()->getNumChildren(); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 866 | |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 867 | // If this is the root node and multiple matched nodes in the input pattern |
| 868 | // have MemRefs in them, have the interpreter collect them and plop them onto |
| 869 | // this node. If there is just one node with MemRefs, leave them on that node |
| 870 | // even if it is not the root. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 871 | // |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 872 | // FIXME3: This is actively incorrect for result patterns with multiple |
| 873 | // memory-referencing instructions. |
| 874 | bool PatternHasMemOperands = |
| 875 | Pattern.getSrcPattern()->TreeHasProperty(SDNPMemOperand, CGP); |
| 876 | |
| 877 | bool NodeHasMemRefs = false; |
| 878 | if (PatternHasMemOperands) { |
| 879 | unsigned NumNodesThatLoadOrStore = |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 880 | numNodesThatMayLoadOrStore(Pattern.getDstPattern(), CGP); |
Cameron Zwarich | 3a2d255 | 2011-05-19 21:13:30 +0000 | [diff] [blame] | 881 | bool NodeIsUniqueLoadOrStore = mayInstNodeLoadOrStore(N, CGP) && |
| 882 | NumNodesThatLoadOrStore == 1; |
| 883 | NodeHasMemRefs = |
| 884 | NodeIsUniqueLoadOrStore || (isRoot && (mayInstNodeLoadOrStore(N, CGP) || |
| 885 | NumNodesThatLoadOrStore != 1)); |
| 886 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 887 | |
Ulrich Weigand | 3a90426 | 2018-07-13 13:18:00 +0000 | [diff] [blame] | 888 | // Determine whether we need to attach a chain to this node. |
| 889 | bool NodeHasChain = false; |
| 890 | if (Pattern.getSrcPattern()->TreeHasProperty(SDNPHasChain, CGP)) { |
| 891 | // For some instructions, we were able to infer from the pattern whether |
| 892 | // they should have a chain. Otherwise, attach the chain to the root. |
| 893 | // |
| 894 | // FIXME2: This is extremely dubious for several reasons, not the least of |
| 895 | // which it gives special status to instructions with patterns that Pat<> |
| 896 | // nodes can't duplicate. |
| 897 | if (II.hasChain_Inferred) |
| 898 | NodeHasChain = II.hasChain; |
| 899 | else |
| 900 | NodeHasChain = isRoot; |
| 901 | // Instructions which load and store from memory should have a chain, |
| 902 | // regardless of whether they happen to have a pattern saying so. |
| 903 | if (II.hasCtrlDep || II.mayLoad || II.mayStore || II.canFoldAsLoad || |
| 904 | II.hasSideEffects) |
| 905 | NodeHasChain = true; |
| 906 | } |
| 907 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 908 | assert((!ResultVTs.empty() || TreeHasOutGlue || NodeHasChain) && |
Chris Lattner | 0be6fe7 | 2010-03-27 19:15:02 +0000 | [diff] [blame] | 909 | "Node has no result"); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 910 | |
Craig Topper | 77eddb7 | 2017-07-07 06:22:35 +0000 | [diff] [blame] | 911 | AddMatcher(new EmitNodeMatcher(II.Namespace.str()+"::"+II.TheDef->getName().str(), |
Craig Topper | 8f74ea3 | 2014-01-21 07:20:05 +0000 | [diff] [blame] | 912 | ResultVTs, InstOps, |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 913 | NodeHasChain, TreeHasInGlue, TreeHasOutGlue, |
Chris Lattner | 6281cda | 2010-02-28 02:41:25 +0000 | [diff] [blame] | 914 | NodeHasMemRefs, NumFixedArityOperands, |
| 915 | NextRecordedOperandNo)); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 917 | // The non-chain and non-glue results of the newly emitted node get recorded. |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 918 | for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) { |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 919 | if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Glue) break; |
Chris Lattner | 77f2e27 | 2010-02-21 06:03:07 +0000 | [diff] [blame] | 920 | OutputOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 921 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | void MatcherGen:: |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 925 | EmitResultSDNodeXFormAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 926 | SmallVectorImpl<unsigned> &ResultOps) { |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 927 | assert(N->getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?"); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 928 | |
| 929 | // Emit the operand. |
| 930 | SmallVector<unsigned, 8> InputOps; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 932 | // FIXME2: Could easily generalize this to support multiple inputs and outputs |
| 933 | // to the SDNodeXForm. For now we just support one input and one output like |
| 934 | // the old instruction selector. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 935 | assert(N->getNumChildren() == 1); |
| 936 | EmitResultOperand(N->getChild(0), InputOps); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 937 | |
| 938 | // The input currently must have produced exactly one result. |
| 939 | assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm"); |
| 940 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 941 | AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N->getOperator())); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 942 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 945 | void MatcherGen::EmitResultOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 946 | SmallVectorImpl<unsigned> &ResultOps) { |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 947 | // This is something selected from the pattern we matched. |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 948 | if (!N->getName().empty()) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 949 | return EmitResultOfNamedOperand(N, ResultOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 950 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 951 | if (N->isLeaf()) |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 952 | return EmitResultLeafAsOperand(N, ResultOps); |
| 953 | |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 954 | Record *OpRec = N->getOperator(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 955 | if (OpRec->isSubClassOf("Instruction")) |
| 956 | return EmitResultInstructionAsOperand(N, ResultOps); |
| 957 | if (OpRec->isSubClassOf("SDNodeXForm")) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 958 | return EmitResultSDNodeXFormAsOperand(N, ResultOps); |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 959 | errs() << "Unknown result node to emit code for: " << *N << '\n'; |
Joerg Sonnenberger | 61131ab | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 960 | PrintFatalError("Unknown node in result pattern!"); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | void MatcherGen::EmitResultCode() { |
Chris Lattner | 01bcd94 | 2010-03-01 22:46:42 +0000 | [diff] [blame] | 964 | // Patterns that match nodes with (potentially multiple) chain inputs have to |
| 965 | // merge them together into a token factor. This informs the generated code |
| 966 | // what all the chained nodes are. |
| 967 | if (!MatchedChainNodes.empty()) |
Craig Topper | 8f74ea3 | 2014-01-21 07:20:05 +0000 | [diff] [blame] | 968 | AddMatcher(new EmitMergeInputChainsMatcher(MatchedChainNodes)); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 969 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 970 | // Codegen the root of the result pattern, capturing the resulting values. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 971 | SmallVector<unsigned, 8> Ops; |
Florian Hahn | 74dff3b | 2018-06-14 20:32:58 +0000 | [diff] [blame] | 972 | EmitResultOperand(Pattern.getDstPattern(), Ops); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 974 | // At this point, we have however many values the result pattern produces. |
| 975 | // However, the input pattern might not need all of these. If there are |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 976 | // excess values at the end (such as implicit defs of condition codes etc) |
Chris Lattner | 8950bca | 2010-12-23 17:03:20 +0000 | [diff] [blame] | 977 | // just lop them off. This doesn't need to worry about glue or chains, just |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 978 | // explicit results. |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 979 | // |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 980 | unsigned NumSrcResults = Pattern.getSrcPattern()->getNumTypes(); |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 981 | |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 982 | // If the pattern also has (implicit) results, count them as well. |
| 983 | if (!Pattern.getDstRegs().empty()) { |
| 984 | // If the root came from an implicit def in the instruction handling stuff, |
| 985 | // don't re-add it. |
Craig Topper | 095734c | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 986 | Record *HandledReg = nullptr; |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 987 | const TreePatternNode *DstPat = Pattern.getDstPattern(); |
| 988 | if (!DstPat->isLeaf() &&DstPat->getOperator()->isSubClassOf("Instruction")){ |
| 989 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
| 990 | CodeGenInstruction &II = CGT.getInstruction(DstPat->getOperator()); |
| 991 | |
| 992 | if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other) |
| 993 | HandledReg = II.ImplicitDefs[0]; |
| 994 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 995 | |
Simon Pilgrim | 5aac6c6 | 2017-06-19 13:24:12 +0000 | [diff] [blame] | 996 | for (Record *Reg : Pattern.getDstRegs()) { |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 997 | if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue; |
| 998 | ++NumSrcResults; |
| 999 | } |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 1002 | assert(Ops.size() >= NumSrcResults && "Didn't provide enough results"); |
Craig Topper | 0f562fe | 2018-12-05 00:47:59 +0000 | [diff] [blame] | 1003 | SmallVector<unsigned, 8> Results(Ops); |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 1004 | |
Craig Topper | 0f562fe | 2018-12-05 00:47:59 +0000 | [diff] [blame] | 1005 | // Apply result permutation. |
| 1006 | for (unsigned ResNo = 0; ResNo < Pattern.getDstPattern()->getNumResults(); |
| 1007 | ++ResNo) { |
| 1008 | Results[ResNo] = Ops[Pattern.getDstPattern()->getResultIndex(ResNo)]; |
| 1009 | } |
| 1010 | |
| 1011 | Results.resize(NumSrcResults); |
| 1012 | AddMatcher(new CompleteMatchMatcher(Results, Pattern)); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 1016 | /// ConvertPatternToMatcher - Create the matcher for the specified pattern with |
| 1017 | /// the specified variant. If the variant number is invalid, this returns null. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 1018 | Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern, |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 1019 | unsigned Variant, |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 1020 | const CodeGenDAGPatterns &CGP) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1021 | MatcherGen Gen(Pattern, CGP); |
| 1022 | |
| 1023 | // Generate the code for the matcher. |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 1024 | if (Gen.EmitMatcherCode(Variant)) |
Craig Topper | 095734c | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1025 | return nullptr; |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 1027 | // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence. |
| 1028 | // FIXME2: Split result code out to another table, and make the matcher end |
| 1029 | // with an "Emit <index>" command. This allows result generation stuff to be |
| 1030 | // shared and factored? |
Jim Grosbach | fbadcd0 | 2010-12-21 16:16:00 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1032 | // If the match succeeds, then we generate Pattern. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 1033 | Gen.EmitResultCode(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Unconditional match. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 1036 | return Gen.GetMatcher(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1037 | } |