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