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" |
| 12 | #include "Record.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" |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 15 | #include <utility> |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Chris Lattner | c94fa4c | 2010-02-19 00:27:40 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 19 | /// getRegisterValueType - Look up and return the ValueType of the specified |
| 20 | /// register. If the register is a member of multiple register classes which |
| 21 | /// have different associated types, return MVT::Other. |
| 22 | static MVT::SimpleValueType getRegisterValueType(Record *R, |
| 23 | const CodeGenTarget &T) { |
| 24 | bool FoundRC = false; |
| 25 | MVT::SimpleValueType VT = MVT::Other; |
| 26 | const std::vector<CodeGenRegisterClass> &RCs = T.getRegisterClasses(); |
| 27 | std::vector<Record*>::const_iterator Element; |
| 28 | |
| 29 | for (unsigned rc = 0, e = RCs.size(); rc != e; ++rc) { |
| 30 | const CodeGenRegisterClass &RC = RCs[rc]; |
| 31 | if (!std::count(RC.Elements.begin(), RC.Elements.end(), R)) |
| 32 | continue; |
| 33 | |
| 34 | if (!FoundRC) { |
| 35 | FoundRC = true; |
| 36 | VT = RC.getValueTypeNum(0); |
| 37 | continue; |
Chris Lattner | c94fa4c | 2010-02-19 00:27:40 +0000 | [diff] [blame] | 38 | } |
Chris Lattner | 67ea6a7 | 2010-03-01 22:49:06 +0000 | [diff] [blame] | 39 | |
| 40 | // If this occurs in multiple register classes, they all have to agree. |
| 41 | assert(VT == RC.getValueTypeNum(0)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 42 | } |
| 43 | return VT; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | namespace { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 48 | class MatcherGen { |
| 49 | const PatternToMatch &Pattern; |
| 50 | const CodeGenDAGPatterns &CGP; |
| 51 | |
| 52 | /// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts |
| 53 | /// out with all of the types removed. This allows us to insert type checks |
| 54 | /// as we scan the tree. |
| 55 | TreePatternNode *PatWithNoTypes; |
| 56 | |
| 57 | /// VariableMap - A map from variable names ('$dst') to the recorded operand |
| 58 | /// number that they were captured as. These are biased by 1 to make |
| 59 | /// insertion easier. |
| 60 | StringMap<unsigned> VariableMap; |
Chris Lattner | c94fa4c | 2010-02-19 00:27:40 +0000 | [diff] [blame] | 61 | |
| 62 | /// NextRecordedOperandNo - As we emit opcodes to record matched values in |
| 63 | /// the RecordedNodes array, this keeps track of which slot will be next to |
| 64 | /// record into. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 65 | unsigned NextRecordedOperandNo; |
| 66 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 67 | /// MatchedChainNodes - This maintains the position in the recorded nodes |
| 68 | /// array of all of the recorded input nodes that have chains. |
| 69 | SmallVector<unsigned, 2> MatchedChainNodes; |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 70 | |
| 71 | /// MatchedFlagResultNodes - This maintains the position in the recorded |
| 72 | /// nodes array of all of the recorded input nodes that have flag results. |
| 73 | SmallVector<unsigned, 2> MatchedFlagResultNodes; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 75 | /// MatchedComplexPatterns - This maintains a list of all of the |
| 76 | /// ComplexPatterns that we need to check. The patterns are known to have |
| 77 | /// names which were recorded. The second element of each pair is the first |
| 78 | /// slot number that the OPC_CheckComplexPat opcode drops the matched |
| 79 | /// results into. |
| 80 | SmallVector<std::pair<const TreePatternNode*, |
| 81 | unsigned>, 2> MatchedComplexPatterns; |
| 82 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 83 | /// PhysRegInputs - List list has an entry for each explicitly specified |
| 84 | /// physreg input to the pattern. The first elt is the Register node, the |
| 85 | /// second is the recorded slot number the input pattern match saved it in. |
| 86 | SmallVector<std::pair<Record*, unsigned>, 2> PhysRegInputs; |
| 87 | |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 88 | /// Matcher - This is the top level of the generated matcher, the result. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 89 | Matcher *TheMatcher; |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 90 | |
| 91 | /// CurPredicate - As we emit matcher nodes, this points to the latest check |
Chris Lattner | bd8227f | 2010-02-18 02:53:41 +0000 | [diff] [blame] | 92 | /// which should have future checks stuck into its Next position. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 93 | Matcher *CurPredicate; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 94 | public: |
| 95 | MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp); |
| 96 | |
| 97 | ~MatcherGen() { |
| 98 | delete PatWithNoTypes; |
| 99 | } |
| 100 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 101 | bool EmitMatcherCode(unsigned Variant); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 102 | void EmitResultCode(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 103 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 104 | Matcher *GetMatcher() const { return TheMatcher; } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 105 | private: |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 106 | void AddMatcher(Matcher *NewNode); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 107 | void InferPossibleTypes(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 108 | |
| 109 | // Matcher Generation. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 110 | void EmitMatchCode(const TreePatternNode *N, TreePatternNode *NodeNoTypes); |
| 111 | void EmitLeafMatchCode(const TreePatternNode *N); |
| 112 | void EmitOperatorMatchCode(const TreePatternNode *N, |
| 113 | TreePatternNode *NodeNoTypes); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 114 | |
| 115 | // Result Code Generation. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 116 | unsigned getNamedArgumentSlot(StringRef Name) { |
| 117 | unsigned VarMapEntry = VariableMap[Name]; |
| 118 | assert(VarMapEntry != 0 && |
| 119 | "Variable referenced but not defined and not caught earlier!"); |
| 120 | return VarMapEntry-1; |
| 121 | } |
| 122 | |
| 123 | /// GetInstPatternNode - Get the pattern for an instruction. |
| 124 | const TreePatternNode *GetInstPatternNode(const DAGInstruction &Ins, |
| 125 | const TreePatternNode *N); |
| 126 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 127 | void EmitResultOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 128 | SmallVectorImpl<unsigned> &ResultOps); |
| 129 | void EmitResultOfNamedOperand(const TreePatternNode *N, |
| 130 | SmallVectorImpl<unsigned> &ResultOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 131 | void EmitResultLeafAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 132 | SmallVectorImpl<unsigned> &ResultOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 133 | void EmitResultInstructionAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 134 | SmallVectorImpl<unsigned> &ResultOps); |
| 135 | void EmitResultSDNodeXFormAsOperand(const TreePatternNode *N, |
| 136 | SmallVectorImpl<unsigned> &ResultOps); |
| 137 | }; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 138 | |
| 139 | } // end anon namespace. |
| 140 | |
| 141 | MatcherGen::MatcherGen(const PatternToMatch &pattern, |
| 142 | const CodeGenDAGPatterns &cgp) |
Chris Lattner | 853b919 | 2010-02-19 00:33:13 +0000 | [diff] [blame] | 143 | : Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0), |
Chris Lattner | 01bcd94 | 2010-03-01 22:46:42 +0000 | [diff] [blame] | 144 | TheMatcher(0), CurPredicate(0) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 145 | // We need to produce the matcher tree for the patterns source pattern. To do |
| 146 | // this we need to match the structure as well as the types. To do the type |
| 147 | // matching, we want to figure out the fewest number of type checks we need to |
| 148 | // emit. For example, if there is only one integer type supported by a |
| 149 | // target, there should be no type comparisons at all for integer patterns! |
| 150 | // |
| 151 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 152 | // remove the types, then perform type inference on the pattern as a whole. |
| 153 | // If there are unresolved types, emit an explicit check for those types, |
| 154 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 155 | // types are resolved. |
| 156 | // |
| 157 | PatWithNoTypes = Pattern.getSrcPattern()->clone(); |
| 158 | PatWithNoTypes->RemoveAllTypes(); |
| 159 | |
| 160 | // If there are types that are manifestly known, infer them. |
| 161 | InferPossibleTypes(); |
| 162 | } |
| 163 | |
| 164 | /// InferPossibleTypes - As we emit the pattern, we end up generating type |
| 165 | /// checks and applying them to the 'PatWithNoTypes' tree. As we do this, we |
| 166 | /// want to propagate implied types as far throughout the tree as possible so |
| 167 | /// that we avoid doing redundant type checks. This does the type propagation. |
| 168 | void MatcherGen::InferPossibleTypes() { |
| 169 | // TP - Get *SOME* tree pattern, we don't care which. It is only used for |
| 170 | // diagnostics, which we know are impossible at this point. |
| 171 | TreePattern &TP = *CGP.pf_begin()->second; |
| 172 | |
| 173 | try { |
| 174 | bool MadeChange = true; |
| 175 | while (MadeChange) |
| 176 | MadeChange = PatWithNoTypes->ApplyTypeConstraints(TP, |
| 177 | true/*Ignore reg constraints*/); |
| 178 | } catch (...) { |
| 179 | errs() << "Type constraint application shouldn't fail!"; |
| 180 | abort(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 185 | /// AddMatcher - Add a matcher node to the current graph we're building. |
| 186 | void MatcherGen::AddMatcher(Matcher *NewNode) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 187 | if (CurPredicate != 0) |
Chris Lattner | bd8227f | 2010-02-18 02:53:41 +0000 | [diff] [blame] | 188 | CurPredicate->setNext(NewNode); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 189 | else |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 190 | TheMatcher = NewNode; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 191 | CurPredicate = NewNode; |
| 192 | } |
| 193 | |
| 194 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
| 196 | // Pattern Match Generation |
| 197 | //===----------------------------------------------------------------------===// |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 198 | |
| 199 | /// EmitLeafMatchCode - Generate matching code for leaf nodes. |
| 200 | void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) { |
| 201 | assert(N->isLeaf() && "Not a leaf?"); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 202 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 203 | // Direct match against an integer constant. |
Chris Lattner | bd8965a | 2010-03-01 07:27:07 +0000 | [diff] [blame] | 204 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
| 205 | // If this is the root of the dag we're matching, we emit a redundant opcode |
| 206 | // check to ensure that this gets folded into the normal top-level |
| 207 | // OpcodeSwitch. |
| 208 | if (N == Pattern.getSrcPattern()) { |
| 209 | const SDNodeInfo &NI = CGP.getSDNodeInfo(CGP.getSDNodeNamed("imm")); |
| 210 | AddMatcher(new CheckOpcodeMatcher(NI)); |
| 211 | } |
| 212 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 213 | return AddMatcher(new CheckIntegerMatcher(II->getValue())); |
Chris Lattner | bd8965a | 2010-03-01 07:27:07 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 215 | |
| 216 | DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue()); |
| 217 | if (DI == 0) { |
| 218 | errs() << "Unknown leaf kind: " << *DI << "\n"; |
| 219 | abort(); |
| 220 | } |
| 221 | |
| 222 | Record *LeafRec = DI->getDef(); |
| 223 | if (// Handle register references. Nothing to do here, they always match. |
| 224 | LeafRec->isSubClassOf("RegisterClass") || |
| 225 | LeafRec->isSubClassOf("PointerLikeRegClass") || |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 226 | LeafRec->isSubClassOf("SubRegIndex") || |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 227 | // Place holder for SRCVALUE nodes. Nothing to do here. |
| 228 | LeafRec->getName() == "srcvalue") |
| 229 | return; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 230 | |
| 231 | // If we have a physreg reference like (mul gpr:$src, EAX) then we need to |
| 232 | // record the register |
| 233 | if (LeafRec->isSubClassOf("Register")) { |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 234 | AddMatcher(new RecordMatcher("physreg input "+LeafRec->getName(), |
| 235 | NextRecordedOperandNo)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 236 | PhysRegInputs.push_back(std::make_pair(LeafRec, NextRecordedOperandNo++)); |
| 237 | return; |
| 238 | } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 239 | |
| 240 | if (LeafRec->isSubClassOf("ValueType")) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 241 | return AddMatcher(new CheckValueTypeMatcher(LeafRec->getName())); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 242 | |
| 243 | if (LeafRec->isSubClassOf("CondCode")) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 244 | return AddMatcher(new CheckCondCodeMatcher(LeafRec->getName())); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 245 | |
| 246 | if (LeafRec->isSubClassOf("ComplexPattern")) { |
Chris Lattner | c2676b2 | 2010-02-17 00:11:30 +0000 | [diff] [blame] | 247 | // We can't model ComplexPattern uses that don't have their name taken yet. |
| 248 | // The OPC_CheckComplexPattern operation implicitly records the results. |
| 249 | if (N->getName().empty()) { |
Chris Lattner | 53a2f60 | 2010-02-16 23:16:25 +0000 | [diff] [blame] | 250 | errs() << "We expect complex pattern uses to have names: " << *N << "\n"; |
| 251 | exit(1); |
| 252 | } |
Chris Lattner | 1f2ed5f | 2010-02-22 22:18:05 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 254 | // Remember this ComplexPattern so that we can emit it after all the other |
| 255 | // structural matches are done. |
| 256 | MatchedComplexPatterns.push_back(std::make_pair(N, 0)); |
Chris Lattner | 8dc4f2b | 2010-02-17 06:08:25 +0000 | [diff] [blame] | 257 | return; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | errs() << "Unknown leaf kind: " << *N << "\n"; |
| 261 | abort(); |
| 262 | } |
| 263 | |
| 264 | void MatcherGen::EmitOperatorMatchCode(const TreePatternNode *N, |
| 265 | TreePatternNode *NodeNoTypes) { |
| 266 | assert(!N->isLeaf() && "Not an operator?"); |
| 267 | const SDNodeInfo &CInfo = CGP.getSDNodeInfo(N->getOperator()); |
| 268 | |
| 269 | // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is |
| 270 | // a constant without a predicate fn that has more that one bit set, handle |
| 271 | // this as a special case. This is usually for targets that have special |
| 272 | // handling of certain large constants (e.g. alpha with it's 8/16/32-bit |
| 273 | // handling stuff). Using these instructions is often far more efficient |
| 274 | // than materializing the constant. Unfortunately, both the instcombiner |
| 275 | // and the dag combiner can often infer that bits are dead, and thus drop |
| 276 | // them from the mask in the dag. For example, it might turn 'AND X, 255' |
| 277 | // into 'AND X, 254' if it knows the low bit is set. Emit code that checks |
| 278 | // to handle this. |
| 279 | if ((N->getOperator()->getName() == "and" || |
| 280 | N->getOperator()->getName() == "or") && |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 281 | N->getChild(1)->isLeaf() && N->getChild(1)->getPredicateFns().empty() && |
| 282 | N->getPredicateFns().empty()) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 283 | if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) { |
| 284 | if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits. |
Chris Lattner | e9eeda8 | 2010-03-01 02:15:34 +0000 | [diff] [blame] | 285 | // If this is at the root of the pattern, we emit a redundant |
| 286 | // CheckOpcode so that the following checks get factored properly under |
| 287 | // a single opcode check. |
| 288 | if (N == Pattern.getSrcPattern()) |
| 289 | AddMatcher(new CheckOpcodeMatcher(CInfo)); |
| 290 | |
| 291 | // Emit the CheckAndImm/CheckOrImm node. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 292 | if (N->getOperator()->getName() == "and") |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 293 | AddMatcher(new CheckAndImmMatcher(II->getValue())); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 294 | else |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 295 | AddMatcher(new CheckOrImmMatcher(II->getValue())); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 296 | |
| 297 | // Match the LHS of the AND as appropriate. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 298 | AddMatcher(new MoveChildMatcher(0)); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 299 | EmitMatchCode(N->getChild(0), NodeNoTypes->getChild(0)); |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 300 | AddMatcher(new MoveParentMatcher()); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Check that the current opcode lines up. |
Chris Lattner | a230f96 | 2010-02-27 21:48:43 +0000 | [diff] [blame] | 307 | AddMatcher(new CheckOpcodeMatcher(CInfo)); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 309 | // If this node has memory references (i.e. is a load or store), tell the |
| 310 | // interpreter to capture them in the memref array. |
| 311 | if (N->NodeHasProperty(SDNPMemOperand, CGP)) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 312 | AddMatcher(new RecordMemRefMatcher()); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 313 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 314 | // If this node has a chain, then the chain is operand #0 is the SDNode, and |
| 315 | // the child numbers of the node are all offset by one. |
| 316 | unsigned OpNo = 0; |
Chris Lattner | 6bc1b51 | 2010-02-16 19:19:58 +0000 | [diff] [blame] | 317 | if (N->NodeHasProperty(SDNPHasChain, CGP)) { |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 318 | // Record the node and remember it in our chained nodes list. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 319 | AddMatcher(new RecordMatcher("'" + N->getOperator()->getName() + |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 320 | "' chained node", |
| 321 | NextRecordedOperandNo)); |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 322 | // Remember all of the input chains our pattern will match. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 323 | MatchedChainNodes.push_back(NextRecordedOperandNo++); |
Chris Lattner | 785d16f | 2010-02-17 02:16:19 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 2f7ecde | 2010-02-17 01:34:15 +0000 | [diff] [blame] | 325 | // Don't look at the input chain when matching the tree pattern to the |
| 326 | // SDNode. |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 327 | OpNo = 1; |
| 328 | |
Chris Lattner | 6bc1b51 | 2010-02-16 19:19:58 +0000 | [diff] [blame] | 329 | // If this node is not the root and the subtree underneath it produces a |
| 330 | // chain, then the result of matching the node is also produce a chain. |
| 331 | // Beyond that, this means that we're also folding (at least) the root node |
| 332 | // into the node that produce the chain (for example, matching |
| 333 | // "(add reg, (load ptr))" as a add_with_memory on X86). This is |
| 334 | // problematic, if the 'reg' node also uses the load (say, its chain). |
| 335 | // Graphically: |
| 336 | // |
| 337 | // [LD] |
| 338 | // ^ ^ |
| 339 | // | \ DAG's like cheese. |
| 340 | // / | |
| 341 | // / [YY] |
| 342 | // | ^ |
| 343 | // [XX]--/ |
| 344 | // |
| 345 | // It would be invalid to fold XX and LD. In this case, folding the two |
| 346 | // nodes together would induce a cycle in the DAG, making it a 'cyclic DAG' |
| 347 | // To prevent this, we emit a dynamic check for legality before allowing |
| 348 | // this to be folded. |
| 349 | // |
| 350 | const TreePatternNode *Root = Pattern.getSrcPattern(); |
| 351 | if (N != Root) { // Not the root of the pattern. |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 352 | // If there is a node between the root and this node, then we definitely |
| 353 | // need to emit the check. |
| 354 | bool NeedCheck = !Root->hasChild(N); |
| 355 | |
| 356 | // If it *is* an immediate child of the root, we can still need a check if |
| 357 | // the root SDNode has multiple inputs. For us, this means that it is an |
| 358 | // intrinsic, has multiple operands, or has other inputs like chain or |
| 359 | // flag). |
| 360 | if (!NeedCheck) { |
| 361 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Root->getOperator()); |
| 362 | NeedCheck = |
| 363 | Root->getOperator() == CGP.get_intrinsic_void_sdnode() || |
| 364 | Root->getOperator() == CGP.get_intrinsic_w_chain_sdnode() || |
| 365 | Root->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() || |
| 366 | PInfo.getNumOperands() > 1 || |
| 367 | PInfo.hasProperty(SDNPHasChain) || |
| 368 | PInfo.hasProperty(SDNPInFlag) || |
| 369 | PInfo.hasProperty(SDNPOptInFlag); |
| 370 | } |
| 371 | |
| 372 | if (NeedCheck) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 373 | AddMatcher(new CheckFoldableChainNodeMatcher()); |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 374 | } |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 376 | |
| 377 | // If this node has an output flag and isn't the root, remember it. |
| 378 | if (N->NodeHasProperty(SDNPOutFlag, CGP) && |
| 379 | N != Pattern.getSrcPattern()) { |
| 380 | // TODO: This redundantly records nodes with both flags and chains. |
| 381 | |
| 382 | // Record the node and remember it in our chained nodes list. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 383 | AddMatcher(new RecordMatcher("'" + N->getOperator()->getName() + |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 384 | "' flag output node", |
| 385 | NextRecordedOperandNo)); |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 386 | // Remember all of the nodes with output flags our pattern will match. |
| 387 | MatchedFlagResultNodes.push_back(NextRecordedOperandNo++); |
| 388 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 389 | |
| 390 | // If this node is known to have an input flag or if it *might* have an input |
| 391 | // flag, capture it as the flag input of the pattern. |
| 392 | if (N->NodeHasProperty(SDNPOptInFlag, CGP) || |
| 393 | N->NodeHasProperty(SDNPInFlag, CGP)) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 394 | AddMatcher(new CaptureFlagInputMatcher()); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 395 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 396 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 397 | // Get the code suitable for matching this child. Move to the child, check |
| 398 | // it then move back to the parent. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 399 | AddMatcher(new MoveChildMatcher(OpNo)); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 400 | EmitMatchCode(N->getChild(i), NodeNoTypes->getChild(i)); |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 401 | AddMatcher(new MoveParentMatcher()); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | |
| 406 | void MatcherGen::EmitMatchCode(const TreePatternNode *N, |
| 407 | TreePatternNode *NodeNoTypes) { |
| 408 | // If N and NodeNoTypes don't agree on a type, then this is a case where we |
| 409 | // need to do a type check. Emit the check, apply the tyep to NodeNoTypes and |
| 410 | // reinfer any correlated types. |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 411 | SmallVector<unsigned, 2> ResultsToTypeCheck; |
| 412 | |
| 413 | for (unsigned i = 0, e = NodeNoTypes->getNumTypes(); i != e; ++i) { |
| 414 | if (NodeNoTypes->getExtType(i) == N->getExtType(i)) continue; |
| 415 | NodeNoTypes->setType(i, N->getExtType(i)); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 416 | InferPossibleTypes(); |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 417 | ResultsToTypeCheck.push_back(i); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 420 | // If this node has a name associated with it, capture it in VariableMap. If |
| 421 | // we already saw this in the pattern, emit code to verify dagness. |
| 422 | if (!N->getName().empty()) { |
| 423 | unsigned &VarMapEntry = VariableMap[N->getName()]; |
| 424 | if (VarMapEntry == 0) { |
Chris Lattner | 20df242 | 2010-02-22 23:33:44 +0000 | [diff] [blame] | 425 | // If it is a named node, we must emit a 'Record' opcode. |
Chris Lattner | 0cebe61 | 2010-03-01 02:24:17 +0000 | [diff] [blame] | 426 | AddMatcher(new RecordMatcher("$" + N->getName(), NextRecordedOperandNo)); |
Chris Lattner | 20df242 | 2010-02-22 23:33:44 +0000 | [diff] [blame] | 427 | VarMapEntry = ++NextRecordedOperandNo; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 428 | } else { |
| 429 | // If we get here, this is a second reference to a specific name. Since |
| 430 | // we already have checked that the first reference is valid, we don't |
| 431 | // have to recursively match it, just check that it's the same as the |
| 432 | // previously named thing. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 433 | AddMatcher(new CheckSameMatcher(VarMapEntry-1)); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 438 | if (N->isLeaf()) |
| 439 | EmitLeafMatchCode(N); |
| 440 | else |
| 441 | EmitOperatorMatchCode(N, NodeNoTypes); |
Chris Lattner | 60c0e37 | 2010-03-01 07:54:59 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 6fd326b | 2010-03-07 07:20:49 +0000 | [diff] [blame] | 443 | // If there are node predicates for this node, generate their checks. |
| 444 | for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i) |
| 445 | AddMatcher(new CheckPredicateMatcher(N->getPredicateFns()[i])); |
| 446 | |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 447 | for (unsigned i = 0, e = ResultsToTypeCheck.size(); i != e; ++i) |
| 448 | AddMatcher(new CheckTypeMatcher(N->getType(ResultsToTypeCheck[i]), |
| 449 | ResultsToTypeCheck[i])); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 452 | /// EmitMatcherCode - Generate the code that matches the predicate of this |
| 453 | /// pattern for the specified Variant. If the variant is invalid this returns |
| 454 | /// true and does not generate code, if it is valid, it returns false. |
| 455 | bool MatcherGen::EmitMatcherCode(unsigned Variant) { |
| 456 | // If the root of the pattern is a ComplexPattern and if it is specified to |
| 457 | // match some number of root opcodes, these are considered to be our variants. |
| 458 | // Depending on which variant we're generating code for, emit the root opcode |
| 459 | // check. |
| 460 | if (const ComplexPattern *CP = |
| 461 | Pattern.getSrcPattern()->getComplexPatternInfo(CGP)) { |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 462 | const std::vector<Record*> &OpNodes = CP->getRootNodes(); |
Chris Lattner | 405f125 | 2010-03-01 22:29:19 +0000 | [diff] [blame] | 463 | assert(!OpNodes.empty() &&"Complex Pattern must specify what it can match"); |
| 464 | if (Variant >= OpNodes.size()) return true; |
| 465 | |
| 466 | AddMatcher(new CheckOpcodeMatcher(CGP.getSDNodeInfo(OpNodes[Variant]))); |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 467 | } else { |
| 468 | if (Variant != 0) return true; |
| 469 | } |
| 470 | |
Chris Lattner | 9752fb1 | 2010-03-04 01:25:36 +0000 | [diff] [blame] | 471 | // Emit the matcher for the pattern structure and types. |
| 472 | EmitMatchCode(Pattern.getSrcPattern(), PatWithNoTypes); |
| 473 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 474 | // If the pattern has a predicate on it (e.g. only enabled when a subtarget |
| 475 | // feature is around, do the check). |
| 476 | if (!Pattern.getPredicateCheck().empty()) |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 477 | AddMatcher(new CheckPatternPredicateMatcher(Pattern.getPredicateCheck())); |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 478 | |
| 479 | // Now that we've completed the structural type match, emit any ComplexPattern |
| 480 | // checks (e.g. addrmode matches). We emit this after the structural match |
| 481 | // because they are generally more expensive to evaluate and more difficult to |
| 482 | // factor. |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 483 | for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) { |
| 484 | const TreePatternNode *N = MatchedComplexPatterns[i].first; |
| 485 | |
| 486 | // Remember where the results of this match get stuck. |
| 487 | MatchedComplexPatterns[i].second = NextRecordedOperandNo; |
| 488 | |
| 489 | // Get the slot we recorded the value in from the name on the node. |
| 490 | unsigned RecNodeEntry = VariableMap[N->getName()]; |
| 491 | assert(!N->getName().empty() && RecNodeEntry && |
| 492 | "Complex pattern should have a name and slot"); |
| 493 | --RecNodeEntry; // Entries in VariableMap are biased. |
| 494 | |
| 495 | const ComplexPattern &CP = |
| 496 | CGP.getComplexPattern(((DefInit*)N->getLeafValue())->getDef()); |
| 497 | |
| 498 | // Emit a CheckComplexPat operation, which does the match (aborting if it |
| 499 | // fails) and pushes the matched operands onto the recorded nodes list. |
| 500 | AddMatcher(new CheckComplexPatMatcher(CP, RecNodeEntry, |
| 501 | N->getName(), NextRecordedOperandNo)); |
| 502 | |
| 503 | // Record the right number of operands. |
| 504 | NextRecordedOperandNo += CP.getNumOperands(); |
| 505 | if (CP.hasProperty(SDNPHasChain)) { |
| 506 | // If the complex pattern has a chain, then we need to keep track of the |
| 507 | // fact that we just recorded a chain input. The chain input will be |
| 508 | // matched as the last operand of the predicate if it was successful. |
| 509 | ++NextRecordedOperandNo; // Chained node operand. |
| 510 | |
| 511 | // It is the last operand recorded. |
| 512 | assert(NextRecordedOperandNo > 1 && |
| 513 | "Should have recorded input/result chains at least!"); |
| 514 | MatchedChainNodes.push_back(NextRecordedOperandNo-1); |
| 515 | } |
| 516 | |
| 517 | // TODO: Complex patterns can't have output flags, if they did, we'd want |
| 518 | // to record them. |
| 519 | } |
| 520 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 521 | return false; |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 525 | //===----------------------------------------------------------------------===// |
| 526 | // Node Result Generation |
| 527 | //===----------------------------------------------------------------------===// |
| 528 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 529 | void MatcherGen::EmitResultOfNamedOperand(const TreePatternNode *N, |
| 530 | SmallVectorImpl<unsigned> &ResultOps){ |
| 531 | assert(!N->getName().empty() && "Operand not named!"); |
| 532 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 533 | // A reference to a complex pattern gets all of the results of the complex |
| 534 | // pattern's match. |
| 535 | if (const ComplexPattern *CP = N->getComplexPatternInfo(CGP)) { |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 536 | unsigned SlotNo = 0; |
| 537 | for (unsigned i = 0, e = MatchedComplexPatterns.size(); i != e; ++i) |
| 538 | if (MatchedComplexPatterns[i].first->getName() == N->getName()) { |
| 539 | SlotNo = MatchedComplexPatterns[i].second; |
| 540 | break; |
| 541 | } |
| 542 | assert(SlotNo != 0 && "Didn't get a slot number assigned?"); |
| 543 | |
Chris Lattner | 20df242 | 2010-02-22 23:33:44 +0000 | [diff] [blame] | 544 | // The first slot entry is the node itself, the subsequent entries are the |
| 545 | // matched values. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 546 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 547 | ResultOps.push_back(SlotNo+i); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
Chris Lattner | 57bf8a4 | 2010-03-04 01:23:08 +0000 | [diff] [blame] | 551 | unsigned SlotNo = getNamedArgumentSlot(N->getName()); |
| 552 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 553 | // If this is an 'imm' or 'fpimm' node, make sure to convert it to the target |
| 554 | // version of the immediate so that it doesn't get selected due to some other |
| 555 | // node use. |
| 556 | if (!N->isLeaf()) { |
| 557 | StringRef OperatorName = N->getOperator()->getName(); |
| 558 | if (OperatorName == "imm" || OperatorName == "fpimm") { |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 559 | AddMatcher(new EmitConvertToTargetMatcher(SlotNo)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 560 | ResultOps.push_back(NextRecordedOperandNo++); |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | ResultOps.push_back(SlotNo); |
| 566 | } |
| 567 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 568 | void MatcherGen::EmitResultLeafAsOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 569 | SmallVectorImpl<unsigned> &ResultOps) { |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 570 | assert(N->isLeaf() && "Must be a leaf"); |
| 571 | |
| 572 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 573 | AddMatcher(new EmitIntegerMatcher(II->getValue(), N->getType(0))); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 574 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | |
| 578 | // If this is an explicit register reference, handle it. |
| 579 | if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
| 580 | if (DI->getDef()->isSubClassOf("Register")) { |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 581 | AddMatcher(new EmitRegisterMatcher(DI->getDef(), N->getType(0))); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 582 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
| 586 | if (DI->getDef()->getName() == "zero_reg") { |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 587 | AddMatcher(new EmitRegisterMatcher(0, N->getType(0))); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 588 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 592 | // Handle a reference to a register class. This is used |
| 593 | // in COPY_TO_SUBREG instructions. |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 594 | if (DI->getDef()->isSubClassOf("RegisterClass")) { |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 595 | std::string Value = getQualifiedName(DI->getDef()) + "RegClassID"; |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 596 | AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 597 | ResultOps.push_back(NextRecordedOperandNo++); |
| 598 | return; |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 599 | } |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 600 | |
| 601 | // Handle a subregister index. This is used for INSERT_SUBREG etc. |
| 602 | if (DI->getDef()->isSubClassOf("SubRegIndex")) { |
| 603 | std::string Value = getQualifiedName(DI->getDef()); |
| 604 | AddMatcher(new EmitStringIntegerMatcher(Value, MVT::i32)); |
| 605 | ResultOps.push_back(NextRecordedOperandNo++); |
| 606 | return; |
| 607 | } |
Chris Lattner | 845c042 | 2010-02-18 22:03:03 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 610 | errs() << "unhandled leaf node: \n"; |
| 611 | N->dump(); |
| 612 | } |
| 613 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 614 | /// GetInstPatternNode - Get the pattern for an instruction. |
| 615 | /// |
| 616 | const TreePatternNode *MatcherGen:: |
| 617 | GetInstPatternNode(const DAGInstruction &Inst, const TreePatternNode *N) { |
| 618 | const TreePattern *InstPat = Inst.getPattern(); |
| 619 | |
| 620 | // FIXME2?: Assume actual pattern comes before "implicit". |
| 621 | TreePatternNode *InstPatNode; |
| 622 | if (InstPat) |
| 623 | InstPatNode = InstPat->getTree(0); |
| 624 | else if (/*isRoot*/ N == Pattern.getDstPattern()) |
| 625 | InstPatNode = Pattern.getSrcPattern(); |
| 626 | else |
| 627 | return 0; |
| 628 | |
| 629 | if (InstPatNode && !InstPatNode->isLeaf() && |
| 630 | InstPatNode->getOperator()->getName() == "set") |
| 631 | InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1); |
| 632 | |
| 633 | return InstPatNode; |
| 634 | } |
| 635 | |
| 636 | void MatcherGen:: |
| 637 | EmitResultInstructionAsOperand(const TreePatternNode *N, |
| 638 | SmallVectorImpl<unsigned> &OutputOps) { |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 639 | Record *Op = N->getOperator(); |
| 640 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
Chris Lattner | f30187a | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 641 | CodeGenInstruction &II = CGT.getInstruction(Op); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 642 | const DAGInstruction &Inst = CGP.getInstruction(Op); |
| 643 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 644 | // If we can, get the pattern for the instruction we're generating. We derive |
| 645 | // a variety of information from this pattern, such as whether it has a chain. |
| 646 | // |
| 647 | // FIXME2: This is extremely dubious for several reasons, not the least of |
| 648 | // which it gives special status to instructions with patterns that Pat<> |
| 649 | // nodes can't duplicate. |
| 650 | const TreePatternNode *InstPatNode = GetInstPatternNode(Inst, N); |
| 651 | |
| 652 | // NodeHasChain - Whether the instruction node we're creating takes chains. |
| 653 | bool NodeHasChain = InstPatNode && |
| 654 | InstPatNode->TreeHasProperty(SDNPHasChain, CGP); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 656 | bool isRoot = N == Pattern.getDstPattern(); |
| 657 | |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 658 | // TreeHasOutFlag - True if this tree has a flag. |
| 659 | bool TreeHasInFlag = false, TreeHasOutFlag = false; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 660 | if (isRoot) { |
| 661 | const TreePatternNode *SrcPat = Pattern.getSrcPattern(); |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 662 | TreeHasInFlag = SrcPat->TreeHasProperty(SDNPOptInFlag, CGP) || |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 663 | SrcPat->TreeHasProperty(SDNPInFlag, CGP); |
| 664 | |
| 665 | // FIXME2: this is checking the entire pattern, not just the node in |
| 666 | // question, doing this just for the root seems like a total hack. |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 667 | TreeHasOutFlag = SrcPat->TreeHasProperty(SDNPOutFlag, CGP); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // NumResults - This is the number of results produced by the instruction in |
| 671 | // the "outs" list. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 672 | unsigned NumResults = Inst.getNumResults(); |
| 673 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 674 | // Loop over all of the operands of the instruction pattern, emitting code |
| 675 | // to fill them all in. The node 'N' usually has number children equal to |
| 676 | // the number of input operands of the instruction. However, in cases |
| 677 | // where there are predicate operands for an instruction, we need to fill |
| 678 | // in the 'execute always' values. Match up the node operands to the |
| 679 | // instruction operands to do this. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 680 | SmallVector<unsigned, 8> InstOps; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 681 | for (unsigned ChildNo = 0, InstOpNo = NumResults, e = II.Operands.size(); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 682 | InstOpNo != e; ++InstOpNo) { |
| 683 | |
| 684 | // Determine what to emit for this operand. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 685 | Record *OperandNode = II.Operands[InstOpNo].Rec; |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 686 | if ((OperandNode->isSubClassOf("PredicateOperand") || |
| 687 | OperandNode->isSubClassOf("OptionalDefOperand")) && |
| 688 | !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) { |
| 689 | // This is a predicate or optional def operand; emit the |
| 690 | // 'default ops' operands. |
Eric Christopher | ae321ed | 2010-08-10 23:46:20 +0000 | [diff] [blame] | 691 | const DAGDefaultOperand &DefaultOp |
| 692 | = CGP.getDefaultOperand(OperandNode); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 693 | for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 694 | EmitResultOperand(DefaultOp.DefaultOps[i], InstOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 695 | continue; |
| 696 | } |
| 697 | |
Chris Lattner | 0be6fe7 | 2010-03-27 19:15:02 +0000 | [diff] [blame] | 698 | const TreePatternNode *Child = N->getChild(ChildNo); |
| 699 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 700 | // Otherwise this is a normal operand or a predicate operand without |
| 701 | // 'execute always'; emit it. |
Chris Lattner | 0be6fe7 | 2010-03-27 19:15:02 +0000 | [diff] [blame] | 702 | unsigned BeforeAddingNumOps = InstOps.size(); |
| 703 | EmitResultOperand(Child, InstOps); |
| 704 | assert(InstOps.size() > BeforeAddingNumOps && "Didn't add any operands"); |
| 705 | |
| 706 | // If the operand is an instruction and it produced multiple results, just |
| 707 | // take the first one. |
| 708 | if (!Child->isLeaf() && Child->getOperator()->isSubClassOf("Instruction")) |
| 709 | InstOps.resize(BeforeAddingNumOps+1); |
| 710 | |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 711 | ++ChildNo; |
| 712 | } |
| 713 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 714 | // If this node has an input flag or explicitly specified input physregs, we |
| 715 | // need to add chained and flagged copyfromreg nodes and materialize the flag |
| 716 | // input. |
| 717 | if (isRoot && !PhysRegInputs.empty()) { |
| 718 | // Emit all of the CopyToReg nodes for the input physical registers. These |
| 719 | // occur in patterns like (mul:i8 AL:i8, GR8:i8:$src). |
| 720 | for (unsigned i = 0, e = PhysRegInputs.size(); i != e; ++i) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 721 | AddMatcher(new EmitCopyToRegMatcher(PhysRegInputs[i].second, |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 722 | PhysRegInputs[i].first)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 723 | // Even if the node has no other flag inputs, the resultant node must be |
| 724 | // flagged to the CopyFromReg nodes we just generated. |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 725 | TreeHasInFlag = true; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // Result order: node results, chain, flags |
| 729 | |
| 730 | // Determine the result types. |
| 731 | SmallVector<MVT::SimpleValueType, 4> ResultVTs; |
Chris Lattner | 0be6fe7 | 2010-03-27 19:15:02 +0000 | [diff] [blame] | 732 | for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) |
| 733 | ResultVTs.push_back(N->getType(i)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 734 | |
| 735 | // If this is the root instruction of a pattern that has physical registers in |
| 736 | // its result pattern, add output VTs for them. For example, X86 has: |
| 737 | // (set AL, (mul ...)) |
| 738 | // This also handles implicit results like: |
| 739 | // (implicit EFLAGS) |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 740 | if (isRoot && !Pattern.getDstRegs().empty()) { |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 741 | // If the root came from an implicit def in the instruction handling stuff, |
| 742 | // don't re-add it. |
| 743 | Record *HandledReg = 0; |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 744 | if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other) |
Chris Lattner | 9287953 | 2010-03-18 23:57:40 +0000 | [diff] [blame] | 745 | HandledReg = II.ImplicitDefs[0]; |
| 746 | |
| 747 | for (unsigned i = 0; i != Pattern.getDstRegs().size(); ++i) { |
| 748 | Record *Reg = Pattern.getDstRegs()[i]; |
| 749 | if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue; |
| 750 | ResultVTs.push_back(getRegisterValueType(Reg, CGT)); |
| 751 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 752 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 1e50631 | 2010-03-19 05:34:15 +0000 | [diff] [blame] | 754 | // If this is the root of the pattern and the pattern we're matching includes |
| 755 | // a node that is variadic, mark the generated node as variadic so that it |
| 756 | // gets the excess operands from the input DAG. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 757 | int NumFixedArityOperands = -1; |
Chris Lattner | 1e50631 | 2010-03-19 05:34:15 +0000 | [diff] [blame] | 758 | if (isRoot && |
| 759 | (Pattern.getSrcPattern()->NodeHasProperty(SDNPVariadic, CGP))) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 760 | NumFixedArityOperands = Pattern.getSrcPattern()->getNumChildren(); |
| 761 | |
| 762 | // If this is the root node and any of the nodes matched nodes in the input |
| 763 | // pattern have MemRefs in them, have the interpreter collect them and plop |
| 764 | // them onto this node. |
| 765 | // |
| 766 | // FIXME3: This is actively incorrect for result patterns where the root of |
| 767 | // the pattern is not the memory reference and is also incorrect when the |
| 768 | // result pattern has multiple memory-referencing instructions. For example, |
| 769 | // in the X86 backend, this pattern causes the memrefs to get attached to the |
| 770 | // CVTSS2SDrr instead of the MOVSSrm: |
| 771 | // |
| 772 | // def : Pat<(extloadf32 addr:$src), |
| 773 | // (CVTSS2SDrr (MOVSSrm addr:$src))>; |
| 774 | // |
| 775 | bool NodeHasMemRefs = |
| 776 | isRoot && Pattern.getSrcPattern()->TreeHasProperty(SDNPMemOperand, CGP); |
| 777 | |
Chris Lattner | 0be6fe7 | 2010-03-27 19:15:02 +0000 | [diff] [blame] | 778 | assert((!ResultVTs.empty() || TreeHasOutFlag || NodeHasChain) && |
| 779 | "Node has no result"); |
| 780 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 781 | AddMatcher(new EmitNodeMatcher(II.Namespace+"::"+II.TheDef->getName(), |
Chris Lattner | e86097a | 2010-02-28 02:31:26 +0000 | [diff] [blame] | 782 | ResultVTs.data(), ResultVTs.size(), |
| 783 | InstOps.data(), InstOps.size(), |
Chris Lattner | ff7fb60 | 2010-02-28 21:53:42 +0000 | [diff] [blame] | 784 | NodeHasChain, TreeHasInFlag, TreeHasOutFlag, |
Chris Lattner | 6281cda | 2010-02-28 02:41:25 +0000 | [diff] [blame] | 785 | NodeHasMemRefs, NumFixedArityOperands, |
| 786 | NextRecordedOperandNo)); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 788 | // The non-chain and non-flag results of the newly emitted node get recorded. |
| 789 | for (unsigned i = 0, e = ResultVTs.size(); i != e; ++i) { |
| 790 | if (ResultVTs[i] == MVT::Other || ResultVTs[i] == MVT::Flag) break; |
Chris Lattner | 77f2e27 | 2010-02-21 06:03:07 +0000 | [diff] [blame] | 791 | OutputOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 792 | } |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | void MatcherGen:: |
| 796 | EmitResultSDNodeXFormAsOperand(const TreePatternNode *N, |
| 797 | SmallVectorImpl<unsigned> &ResultOps) { |
| 798 | assert(N->getOperator()->isSubClassOf("SDNodeXForm") && "Not SDNodeXForm?"); |
| 799 | |
| 800 | // Emit the operand. |
| 801 | SmallVector<unsigned, 8> InputOps; |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 802 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 803 | // FIXME2: Could easily generalize this to support multiple inputs and outputs |
| 804 | // to the SDNodeXForm. For now we just support one input and one output like |
| 805 | // the old instruction selector. |
| 806 | assert(N->getNumChildren() == 1); |
| 807 | EmitResultOperand(N->getChild(0), InputOps); |
| 808 | |
| 809 | // The input currently must have produced exactly one result. |
| 810 | assert(InputOps.size() == 1 && "Unexpected input to SDNodeXForm"); |
| 811 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 812 | AddMatcher(new EmitNodeXFormMatcher(InputOps[0], N->getOperator())); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 813 | ResultOps.push_back(NextRecordedOperandNo++); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | void MatcherGen::EmitResultOperand(const TreePatternNode *N, |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 817 | SmallVectorImpl<unsigned> &ResultOps) { |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 818 | // This is something selected from the pattern we matched. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 819 | if (!N->getName().empty()) |
| 820 | return EmitResultOfNamedOperand(N, ResultOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 821 | |
| 822 | if (N->isLeaf()) |
| 823 | return EmitResultLeafAsOperand(N, ResultOps); |
| 824 | |
| 825 | Record *OpRec = N->getOperator(); |
| 826 | if (OpRec->isSubClassOf("Instruction")) |
| 827 | return EmitResultInstructionAsOperand(N, ResultOps); |
| 828 | if (OpRec->isSubClassOf("SDNodeXForm")) |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 829 | return EmitResultSDNodeXFormAsOperand(N, ResultOps); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 830 | errs() << "Unknown result node to emit code for: " << *N << '\n'; |
| 831 | throw std::string("Unknown node in result pattern!"); |
| 832 | } |
| 833 | |
| 834 | void MatcherGen::EmitResultCode() { |
Chris Lattner | 01bcd94 | 2010-03-01 22:46:42 +0000 | [diff] [blame] | 835 | // Patterns that match nodes with (potentially multiple) chain inputs have to |
| 836 | // merge them together into a token factor. This informs the generated code |
| 837 | // what all the chained nodes are. |
| 838 | if (!MatchedChainNodes.empty()) |
| 839 | AddMatcher(new EmitMergeInputChainsMatcher |
| 840 | (MatchedChainNodes.data(), MatchedChainNodes.size())); |
| 841 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 842 | // Codegen the root of the result pattern, capturing the resulting values. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 843 | SmallVector<unsigned, 8> Ops; |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 844 | EmitResultOperand(Pattern.getDstPattern(), Ops); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 846 | // At this point, we have however many values the result pattern produces. |
| 847 | // 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] | 848 | // excess values at the end (such as implicit defs of condition codes etc) |
| 849 | // just lop them off. This doesn't need to worry about flags or chains, just |
| 850 | // explicit results. |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 851 | // |
Chris Lattner | b4a52b0 | 2010-03-27 20:45:15 +0000 | [diff] [blame] | 852 | unsigned NumSrcResults = Pattern.getSrcPattern()->getNumTypes(); |
| 853 | |
| 854 | // If the pattern also has (implicit) results, count them as well. |
| 855 | if (!Pattern.getDstRegs().empty()) { |
| 856 | // If the root came from an implicit def in the instruction handling stuff, |
| 857 | // don't re-add it. |
| 858 | Record *HandledReg = 0; |
| 859 | const TreePatternNode *DstPat = Pattern.getDstPattern(); |
| 860 | if (!DstPat->isLeaf() &&DstPat->getOperator()->isSubClassOf("Instruction")){ |
| 861 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
| 862 | CodeGenInstruction &II = CGT.getInstruction(DstPat->getOperator()); |
| 863 | |
| 864 | if (II.HasOneImplicitDefWithKnownVT(CGT) != MVT::Other) |
| 865 | HandledReg = II.ImplicitDefs[0]; |
| 866 | } |
| 867 | |
| 868 | for (unsigned i = 0; i != Pattern.getDstRegs().size(); ++i) { |
| 869 | Record *Reg = Pattern.getDstRegs()[i]; |
| 870 | if (!Reg->isSubClassOf("Register") || Reg == HandledReg) continue; |
| 871 | ++NumSrcResults; |
| 872 | } |
| 873 | } |
| 874 | |
Chris Lattner | 565a6f9 | 2010-02-21 23:54:05 +0000 | [diff] [blame] | 875 | assert(Ops.size() >= NumSrcResults && "Didn't provide enough results"); |
| 876 | Ops.resize(NumSrcResults); |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 877 | |
| 878 | // If the matched pattern covers nodes which define a flag result, emit a node |
| 879 | // that tells the matcher about them so that it can update their results. |
| 880 | if (!MatchedFlagResultNodes.empty()) |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 881 | AddMatcher(new MarkFlagResultsMatcher(MatchedFlagResultNodes.data(), |
Chris Lattner | 4d0c931 | 2010-03-01 01:54:19 +0000 | [diff] [blame] | 882 | MatchedFlagResultNodes.size())); |
Chris Lattner | 02f7358 | 2010-02-24 05:33:42 +0000 | [diff] [blame] | 883 | |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 884 | AddMatcher(new CompleteMatchMatcher(Ops.data(), Ops.size(), Pattern)); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 888 | /// ConvertPatternToMatcher - Create the matcher for the specified pattern with |
| 889 | /// the specified variant. If the variant number is invalid, this returns null. |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 890 | Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern, |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 891 | unsigned Variant, |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 892 | const CodeGenDAGPatterns &CGP) { |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 893 | MatcherGen Gen(Pattern, CGP); |
| 894 | |
| 895 | // Generate the code for the matcher. |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 896 | if (Gen.EmitMatcherCode(Variant)) |
| 897 | return 0; |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 898 | |
| 899 | // FIXME2: Kill extra MoveParent commands at the end of the matcher sequence. |
| 900 | // FIXME2: Split result code out to another table, and make the matcher end |
| 901 | // with an "Emit <index>" command. This allows result generation stuff to be |
| 902 | // shared and factored? |
| 903 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 904 | // If the match succeeds, then we generate Pattern. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 905 | Gen.EmitResultCode(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 906 | |
| 907 | // Unconditional match. |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 908 | return Gen.GetMatcher(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 909 | } |