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