Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1 | //===- DAGISelEmitter.cpp - Generate an instruction selector --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits a DAG instruction selector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DAGISelEmitter.h" |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 15 | #include "DAGISelMatcher.h" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 16 | #include "Record.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Jeff Cohen | a48283b | 2005-09-25 19:04:43 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Dan Gohman | 95d1109 | 2008-07-07 21:00:17 +0000 | [diff] [blame] | 23 | #include <deque> |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 24 | #include <iostream> |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chris Lattner | 3d4ad29 | 2009-08-07 22:27:19 +0000 | [diff] [blame] | 27 | static cl::opt<bool> |
| 28 | GenDebug("gen-debug", cl::desc("Generate debug code"), cl::init(false)); |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 29 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 31 | // DAGISelEmitter Helper methods |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 32 | // |
| 33 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 34 | /// getNodeName - The top level Select_* functions have an "SDNode* N" |
| 35 | /// argument. When expanding the pattern-matching code, the intermediate |
| 36 | /// variables have type SDValue. This function provides a uniform way to |
| 37 | /// reference the underlying "SDNode *" for both cases. |
| 38 | static std::string getNodeName(const std::string &S) { |
| 39 | if (S == "N") return S; |
| 40 | return S + ".getNode()"; |
| 41 | } |
| 42 | |
| 43 | /// getNodeValue - Similar to getNodeName, except it provides a uniform |
| 44 | /// way to access the SDValue for both cases. |
| 45 | static std::string getValueName(const std::string &S) { |
| 46 | if (S == "N") return "SDValue(N, 0)"; |
| 47 | return S; |
| 48 | } |
| 49 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 50 | /// getPatternSize - Return the 'size' of this pattern. We want to match large |
| 51 | /// patterns before small ones. This is used to determine the size of a |
| 52 | /// pattern. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 53 | static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 54 | assert((EEVT::isExtIntegerInVTs(P->getExtTypes()) || |
| 55 | EEVT::isExtFloatingPointInVTs(P->getExtTypes()) || |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 56 | P->getExtTypeNum(0) == MVT::isVoid || |
| 57 | P->getExtTypeNum(0) == MVT::Flag || |
| 58 | P->getExtTypeNum(0) == MVT::iPTR || |
| 59 | P->getExtTypeNum(0) == MVT::iPTRAny) && |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 60 | "Not a valid pattern node to size!"); |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 61 | unsigned Size = 3; // The node itself. |
Evan Cheng | 657416c | 2006-02-01 06:06:31 +0000 | [diff] [blame] | 62 | // If the root node is a ConstantSDNode, increases its size. |
| 63 | // e.g. (set R32:$dst, 0). |
| 64 | if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue())) |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 65 | Size += 2; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 66 | |
| 67 | // FIXME: This is a hack to statically increase the priority of patterns |
| 68 | // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. |
| 69 | // Later we can allow complexity / cost for each pattern to be (optionally) |
| 70 | // specified. To get best possible pattern match we'll need to dynamically |
| 71 | // calculate the complexity of all patterns a dag can potentially map to. |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 72 | const ComplexPattern *AM = P->getComplexPatternInfo(CGP); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 73 | if (AM) |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 74 | Size += AM->getNumOperands() * 3; |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 75 | |
| 76 | // If this node has some predicate function that must match, it adds to the |
| 77 | // complexity of this node. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 78 | if (!P->getPredicateFns().empty()) |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 79 | ++Size; |
| 80 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 81 | // Count children in the count if they are also nodes. |
| 82 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { |
| 83 | TreePatternNode *Child = P->getChild(i); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 84 | if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 85 | Size += getPatternSize(Child, CGP); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 86 | else if (Child->isLeaf()) { |
| 87 | if (dynamic_cast<IntInit*>(Child->getLeafValue())) |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 88 | Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2). |
Chris Lattner | 05446e7 | 2010-02-16 23:13:59 +0000 | [diff] [blame] | 89 | else if (Child->getComplexPatternInfo(CGP)) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 90 | Size += getPatternSize(Child, CGP); |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 91 | else if (!Child->getPredicateFns().empty()) |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 92 | ++Size; |
Chris Lattner | 2f041d4 | 2005-10-19 04:41:05 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return Size; |
| 97 | } |
| 98 | |
| 99 | /// getResultPatternCost - Compute the number of instructions for this pattern. |
| 100 | /// This is a temporary hack. We should really include the instruction |
| 101 | /// latencies in this calculation. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 102 | static unsigned getResultPatternCost(TreePatternNode *P, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 103 | CodeGenDAGPatterns &CGP) { |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 104 | if (P->isLeaf()) return 0; |
| 105 | |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 106 | unsigned Cost = 0; |
| 107 | Record *Op = P->getOperator(); |
| 108 | if (Op->isSubClassOf("Instruction")) { |
| 109 | Cost++; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 110 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName()); |
Dan Gohman | 533297b | 2009-10-29 18:10:34 +0000 | [diff] [blame] | 111 | if (II.usesCustomInserter) |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 112 | Cost += 10; |
| 113 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 114 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 115 | Cost += getResultPatternCost(P->getChild(i), CGP); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 116 | return Cost; |
| 117 | } |
| 118 | |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 119 | /// getResultPatternCodeSize - Compute the code size of instructions for this |
| 120 | /// pattern. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 121 | static unsigned getResultPatternSize(TreePatternNode *P, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 122 | CodeGenDAGPatterns &CGP) { |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 123 | if (P->isLeaf()) return 0; |
| 124 | |
| 125 | unsigned Cost = 0; |
| 126 | Record *Op = P->getOperator(); |
| 127 | if (Op->isSubClassOf("Instruction")) { |
| 128 | Cost += Op->getValueAsInt("CodeSize"); |
| 129 | } |
| 130 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 131 | Cost += getResultPatternSize(P->getChild(i), CGP); |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 132 | return Cost; |
| 133 | } |
| 134 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 135 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 136 | // In particular, we want to match maximal patterns first and lowest cost within |
| 137 | // a particular complexity first. |
| 138 | struct PatternSortingPredicate { |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 139 | PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {} |
| 140 | CodeGenDAGPatterns &CGP; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 141 | |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 142 | typedef std::pair<unsigned, std::string> CodeLine; |
| 143 | typedef std::vector<CodeLine> CodeList; |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 144 | |
| 145 | bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair, |
| 146 | const std::pair<const PatternToMatch*, CodeList> &RHSPair) { |
| 147 | const PatternToMatch *LHS = LHSPair.first; |
| 148 | const PatternToMatch *RHS = RHSPair.first; |
| 149 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 150 | unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP); |
| 151 | unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP); |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 152 | LHSSize += LHS->getAddedComplexity(); |
| 153 | RHSSize += RHS->getAddedComplexity(); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 154 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 155 | if (LHSSize < RHSSize) return false; |
| 156 | |
| 157 | // If the patterns have equal complexity, compare generated instruction cost |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 158 | unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP); |
| 159 | unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP); |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 160 | if (LHSCost < RHSCost) return true; |
| 161 | if (LHSCost > RHSCost) return false; |
| 162 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 163 | return getResultPatternSize(LHS->getDstPattern(), CGP) < |
| 164 | getResultPatternSize(RHS->getDstPattern(), CGP); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 165 | } |
| 166 | }; |
| 167 | |
Jim Grosbach | 54f3022 | 2009-03-25 23:28:33 +0000 | [diff] [blame] | 168 | /// getRegisterValueType - Look up and return the ValueType of the specified |
| 169 | /// register. If the register is a member of multiple register classes which |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 170 | /// have different associated types, return MVT::Other. |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 171 | static MVT::SimpleValueType getRegisterValueType(Record *R, |
| 172 | const CodeGenTarget &T) { |
Jim Grosbach | 866cc60 | 2009-03-26 14:45:34 +0000 | [diff] [blame] | 173 | bool FoundRC = false; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 174 | MVT::SimpleValueType VT = MVT::Other; |
Jim Grosbach | 54f3022 | 2009-03-25 23:28:33 +0000 | [diff] [blame] | 175 | const std::vector<CodeGenRegisterClass> &RCs = T.getRegisterClasses(); |
| 176 | std::vector<CodeGenRegisterClass>::const_iterator RC; |
| 177 | std::vector<Record*>::const_iterator Element; |
| 178 | |
| 179 | for (RC = RCs.begin() ; RC != RCs.end() ; RC++) { |
| 180 | Element = find((*RC).Elements.begin(), (*RC).Elements.end(), R); |
| 181 | if (Element != (*RC).Elements.end()) { |
| 182 | if (!FoundRC) { |
Jim Grosbach | 866cc60 | 2009-03-26 14:45:34 +0000 | [diff] [blame] | 183 | FoundRC = true; |
Jim Grosbach | 54f3022 | 2009-03-25 23:28:33 +0000 | [diff] [blame] | 184 | VT = (*RC).getValueTypeNum(0); |
| 185 | } else { |
| 186 | // In multiple RC's |
| 187 | if (VT != (*RC).getValueTypeNum(0)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 188 | // Types of the RC's do not agree. Return MVT::Other. The |
Jim Grosbach | 54f3022 | 2009-03-25 23:28:33 +0000 | [diff] [blame] | 189 | // target is responsible for handling this. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 190 | return MVT::Other; |
Jim Grosbach | 54f3022 | 2009-03-25 23:28:33 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | return VT; |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Evan Cheng | f9d0318 | 2008-07-03 08:39:51 +0000 | [diff] [blame] | 198 | static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) { |
| 199 | return CGP.getSDNodeInfo(Op).getEnumName(); |
| 200 | } |
| 201 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 203 | // Node Transformation emitter implementation. |
| 204 | // |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 205 | void DAGISelEmitter::EmitNodeTransforms(raw_ostream &OS) { |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 206 | // Walk the pattern fragments, adding them to a map, which sorts them by |
| 207 | // name. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 208 | typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy; |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 209 | NXsByNameTy NXsByName; |
| 210 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 211 | for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end(); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 212 | I != E; ++I) |
| 213 | NXsByName.insert(std::make_pair(I->first->getName(), I->second)); |
| 214 | |
| 215 | OS << "\n// Node transformations.\n"; |
| 216 | |
| 217 | for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end(); |
| 218 | I != E; ++I) { |
| 219 | Record *SDNode = I->second.first; |
| 220 | std::string Code = I->second.second; |
| 221 | |
| 222 | if (Code.empty()) continue; // Empty code? Skip it. |
| 223 | |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 224 | std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName(); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 225 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 226 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 227 | OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2 |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 228 | << ") {\n"; |
| 229 | if (ClassName != "SDNode") |
| 230 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 231 | OS << Code << "\n}\n"; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | //===----------------------------------------------------------------------===// |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 236 | // Predicate emitter implementation. |
| 237 | // |
| 238 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 239 | void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) { |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 240 | OS << "\n// Predicate functions.\n"; |
| 241 | |
| 242 | // Walk the pattern fragments, adding them to a map, which sorts them by |
| 243 | // name. |
| 244 | typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy; |
| 245 | PFsByNameTy PFsByName; |
| 246 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 247 | for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 248 | I != E; ++I) |
| 249 | PFsByName.insert(std::make_pair(I->first->getName(), *I)); |
| 250 | |
| 251 | |
| 252 | for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end(); |
| 253 | I != E; ++I) { |
| 254 | Record *PatFragRecord = I->second.first;// Record that derives from PatFrag. |
| 255 | TreePattern *P = I->second.second; |
| 256 | |
| 257 | // If there is a code init for this fragment, emit the predicate code. |
| 258 | std::string Code = PatFragRecord->getValueAsCode("Predicate"); |
| 259 | if (Code.empty()) continue; |
| 260 | |
| 261 | if (P->getOnlyTree()->isLeaf()) |
| 262 | OS << "inline bool Predicate_" << PatFragRecord->getName() |
Chris Lattner | ccba15f | 2010-02-16 07:26:36 +0000 | [diff] [blame] | 263 | << "(SDNode *N) const {\n"; |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 264 | else { |
| 265 | std::string ClassName = |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 266 | CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 267 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 268 | |
| 269 | OS << "inline bool Predicate_" << PatFragRecord->getName() |
Chris Lattner | ccba15f | 2010-02-16 07:26:36 +0000 | [diff] [blame] | 270 | << "(SDNode *" << C2 << ") const {\n"; |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 271 | if (ClassName != "SDNode") |
| 272 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 273 | } |
| 274 | OS << Code << "\n}\n"; |
| 275 | } |
| 276 | |
| 277 | OS << "\n\n"; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | // PatternCodeEmitter implementation. |
| 283 | // |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 284 | class PatternCodeEmitter { |
| 285 | private: |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 286 | CodeGenDAGPatterns &CGP; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 287 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 288 | // Predicates. |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 289 | std::string PredicateCheck; |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 290 | // Pattern cost. |
| 291 | unsigned Cost; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 292 | // Instruction selector pattern. |
| 293 | TreePatternNode *Pattern; |
| 294 | // Matched instruction. |
| 295 | TreePatternNode *Instruction; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 296 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 297 | // Node to name mapping |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 298 | std::map<std::string, std::string> VariableMap; |
Evan Cheng | a58891f | 2008-02-05 22:50:29 +0000 | [diff] [blame] | 299 | // Name of the folded node which produces a flag. |
| 300 | std::pair<std::string, unsigned> FoldedFlag; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 301 | // Names of all the folded nodes which produce chains. |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 302 | std::vector<std::pair<std::string, unsigned> > FoldedChains; |
Evan Cheng | 4326ef5 | 2006-10-12 02:08:53 +0000 | [diff] [blame] | 303 | // Original input chain(s). |
| 304 | std::vector<std::pair<std::string, std::string> > OrigChains; |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 305 | std::set<std::string> Duplicates; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 306 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 307 | /// LSI - Load/Store information. |
| 308 | /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode |
| 309 | /// for each memory access. This facilitates the use of AliasAnalysis in |
| 310 | /// the backend. |
| 311 | std::vector<std::string> LSI; |
| 312 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 313 | /// GeneratedCode - This is the buffer that we emit code to. The first int |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 314 | /// indicates whether this is an exit predicate (something that should be |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 315 | /// tested, and if true, the match fails) [when 1], or normal code to emit |
| 316 | /// [when 0], or initialization code to emit [when 2]. |
| 317 | std::vector<std::pair<unsigned, std::string> > &GeneratedCode; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 318 | /// GeneratedDecl - This is the set of all SDValue declarations needed for |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 319 | /// the set of patterns for each top-level opcode. |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 320 | std::set<std::string> &GeneratedDecl; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 321 | /// TargetOpcodes - The target specific opcodes used by the resulting |
| 322 | /// instructions. |
| 323 | std::vector<std::string> &TargetOpcodes; |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 324 | std::vector<std::string> &TargetVTs; |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 325 | /// OutputIsVariadic - Records whether the instruction output pattern uses |
| 326 | /// variable_ops. This requires that the Emit function be passed an |
| 327 | /// additional argument to indicate where the input varargs operands |
| 328 | /// begin. |
| 329 | bool &OutputIsVariadic; |
| 330 | /// NumInputRootOps - Records the number of operands the root node of the |
| 331 | /// input pattern has. This information is used in the generated code to |
| 332 | /// pass to Emit functions when variable_ops processing is needed. |
| 333 | unsigned &NumInputRootOps; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 334 | |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 335 | std::string ChainName; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 336 | unsigned TmpNo; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 337 | unsigned OpcNo; |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 338 | unsigned VTNo; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 339 | |
| 340 | void emitCheck(const std::string &S) { |
| 341 | if (!S.empty()) |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 342 | GeneratedCode.push_back(std::make_pair(1, S)); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 343 | } |
| 344 | void emitCode(const std::string &S) { |
| 345 | if (!S.empty()) |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 346 | GeneratedCode.push_back(std::make_pair(0, S)); |
| 347 | } |
| 348 | void emitInit(const std::string &S) { |
| 349 | if (!S.empty()) |
| 350 | GeneratedCode.push_back(std::make_pair(2, S)); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 351 | } |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 352 | void emitDecl(const std::string &S) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 353 | assert(!S.empty() && "Invalid declaration"); |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 354 | GeneratedDecl.insert(S); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 355 | } |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 356 | void emitOpcode(const std::string &Opc) { |
| 357 | TargetOpcodes.push_back(Opc); |
| 358 | OpcNo++; |
| 359 | } |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 360 | void emitVT(const std::string &VT) { |
| 361 | TargetVTs.push_back(VT); |
| 362 | VTNo++; |
| 363 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 364 | public: |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 365 | PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck, |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 366 | TreePatternNode *pattern, TreePatternNode *instr, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 367 | std::vector<std::pair<unsigned, std::string> > &gc, |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 368 | std::set<std::string> &gd, |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 369 | std::vector<std::string> &to, |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 370 | std::vector<std::string> &tv, |
| 371 | bool &oiv, |
| 372 | unsigned &niro) |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 373 | : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr), |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 374 | GeneratedCode(gc), GeneratedDecl(gd), |
| 375 | TargetOpcodes(to), TargetVTs(tv), |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 376 | OutputIsVariadic(oiv), NumInputRootOps(niro), |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 377 | TmpNo(0), OpcNo(0), VTNo(0) {} |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 378 | |
| 379 | /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo |
| 380 | /// if the match fails. At this point, we already know that the opcode for N |
| 381 | /// matches, and the SDNode for the result has the RootName specified name. |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 382 | void EmitMatchCode(TreePatternNode *N, TreePatternNode *P, |
| 383 | const std::string &RootName, const std::string &ChainSuffix, |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 384 | bool &FoundChain); |
Chris Lattner | 39e73f7 | 2006-10-11 04:05:55 +0000 | [diff] [blame] | 385 | |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 386 | void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent, |
Christopher Lamb | 8535624 | 2008-01-31 07:27:46 +0000 | [diff] [blame] | 387 | const std::string &RootName, |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 388 | const std::string &ChainSuffix, bool &FoundChain); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 389 | |
| 390 | /// EmitResultCode - Emit the action for a pattern. Now that it has matched |
| 391 | /// we actually have to build a DAG! |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 392 | std::vector<std::string> |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 393 | EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 394 | bool InFlagDecled, bool ResNodeDecled, |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 395 | bool LikeLeaf = false, bool isRoot = false); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 397 | /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat' |
| 398 | /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 399 | /// 'Pat' may be missing types. If we find an unresolved type to add a check |
| 400 | /// for, this returns true otherwise false if Pat has all types. |
| 401 | bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other, |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 402 | const std::string &Prefix, bool isRoot = false) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 403 | // Did we find one? |
Evan Cheng | d15531b | 2006-05-19 07:24:32 +0000 | [diff] [blame] | 404 | if (Pat->getExtTypes() != Other->getExtTypes()) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 405 | // Move a type over from 'other' to 'pat'. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 406 | Pat->setTypes(Other->getExtTypes()); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 407 | // The top level node type is checked outside of the select function. |
| 408 | if (!isRoot) |
Anton Korobeynikov | c2fd919 | 2009-11-08 12:14:54 +0000 | [diff] [blame] | 409 | emitCheck(Prefix + ".getValueType() == " + |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 410 | getName(Pat->getTypeNum(0))); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 411 | return true; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 414 | unsigned OpNo = (unsigned)Pat->NodeHasProperty(SDNPHasChain, CGP); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 415 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo) |
| 416 | if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i), |
| 417 | Prefix + utostr(OpNo))) |
| 418 | return true; |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | private: |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 423 | /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 424 | /// being built. |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 425 | void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 426 | bool &ChainEmitted, bool &InFlagDecled, |
| 427 | bool &ResNodeDecled, bool isRoot = false) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 428 | const CodeGenTarget &T = CGP.getTargetInfo(); |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 429 | unsigned OpNo = (unsigned)N->NodeHasProperty(SDNPHasChain, CGP); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 430 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 431 | TreePatternNode *Child = N->getChild(i); |
| 432 | if (!Child->isLeaf()) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 433 | EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted, |
| 434 | InFlagDecled, ResNodeDecled); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 435 | } else { |
| 436 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 437 | if (!Child->getName().empty()) { |
| 438 | std::string Name = RootName + utostr(OpNo); |
| 439 | if (Duplicates.find(Name) != Duplicates.end()) |
| 440 | // A duplicate! Do not emit a copy for this node. |
| 441 | continue; |
| 442 | } |
| 443 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 444 | Record *RR = DI->getDef(); |
| 445 | if (RR->isSubClassOf("Register")) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 446 | MVT::SimpleValueType RVT = getRegisterValueType(RR, T); |
| 447 | if (RVT == MVT::Flag) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 448 | if (!InFlagDecled) { |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 449 | emitCode("SDValue InFlag = " + |
| 450 | getValueName(RootName + utostr(OpNo)) + ";"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 451 | InFlagDecled = true; |
| 452 | } else |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 453 | emitCode("InFlag = " + |
| 454 | getValueName(RootName + utostr(OpNo)) + ";"); |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 455 | } else { |
| 456 | if (!ChainEmitted) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 457 | emitCode("SDValue Chain = CurDAG->getEntryNode();"); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 458 | ChainName = "Chain"; |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 459 | ChainEmitted = true; |
| 460 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 461 | if (!InFlagDecled) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 462 | emitCode("SDValue InFlag(0, 0);"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 463 | InFlagDecled = true; |
| 464 | } |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 465 | std::string Decl = (!ResNodeDecled) ? "SDNode *" : ""; |
| 466 | emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName + |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 467 | ", " + getNodeName(RootName) + "->getDebugLoc()" + |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 468 | ", " + getQualifiedName(RR) + |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 469 | ", " + getValueName(RootName + utostr(OpNo)) + |
| 470 | ", InFlag).getNode();"); |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 471 | ResNodeDecled = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 472 | emitCode(ChainName + " = SDValue(ResNode, 0);"); |
| 473 | emitCode("InFlag = SDValue(ResNode, 1);"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 480 | if (N->NodeHasProperty(SDNPInFlag, CGP)) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 481 | if (!InFlagDecled) { |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 482 | emitCode("SDValue InFlag = " + getNodeName(RootName) + |
| 483 | "->getOperand(" + utostr(OpNo) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 484 | InFlagDecled = true; |
| 485 | } else |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 486 | abort(); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 487 | emitCode("InFlag = " + getNodeName(RootName) + |
| 488 | "->getOperand(" + utostr(OpNo) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 489 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 490 | } |
| 491 | }; |
| 492 | |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 493 | |
| 494 | /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo |
| 495 | /// if the match fails. At this point, we already know that the opcode for N |
| 496 | /// matches, and the SDNode for the result has the RootName specified name. |
| 497 | void PatternCodeEmitter::EmitMatchCode(TreePatternNode *N, TreePatternNode *P, |
| 498 | const std::string &RootName, |
| 499 | const std::string &ChainSuffix, |
| 500 | bool &FoundChain) { |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 501 | // Save loads/stores matched by a pattern. |
| 502 | if (!N->isLeaf() && N->getName().empty()) { |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 503 | if (N->NodeHasProperty(SDNPMemOperand, CGP)) |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 504 | LSI.push_back(getNodeName(RootName)); |
| 505 | } |
| 506 | |
| 507 | bool isRoot = (P == NULL); |
| 508 | // Emit instruction predicates. Each predicate is just a string for now. |
| 509 | if (isRoot) { |
| 510 | // Record input varargs info. |
| 511 | NumInputRootOps = N->getNumChildren(); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 512 | emitCheck(PredicateCheck); |
| 513 | } |
| 514 | |
| 515 | if (N->isLeaf()) { |
| 516 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
| 517 | emitCheck("cast<ConstantSDNode>(" + getNodeName(RootName) + |
| 518 | ")->getSExtValue() == INT64_C(" + |
| 519 | itostr(II->getValue()) + ")"); |
| 520 | return; |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | 05446e7 | 2010-02-16 23:13:59 +0000 | [diff] [blame] | 522 | assert(N->getComplexPatternInfo(CGP) != 0 && |
| 523 | "Cannot match this as a leaf value!"); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // If this node has a name associated with it, capture it in VariableMap. If |
| 527 | // we already saw this in the pattern, emit code to verify dagness. |
| 528 | if (!N->getName().empty()) { |
| 529 | std::string &VarMapEntry = VariableMap[N->getName()]; |
| 530 | if (VarMapEntry.empty()) { |
| 531 | VarMapEntry = RootName; |
| 532 | } else { |
| 533 | // If we get here, this is a second reference to a specific name. Since |
| 534 | // we already have checked that the first reference is valid, we don't |
| 535 | // have to recursively match it, just check that it's the same as the |
| 536 | // previously named thing. |
| 537 | emitCheck(VarMapEntry + " == " + RootName); |
| 538 | return; |
| 539 | } |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | // Emit code to load the child nodes and match their contents recursively. |
| 544 | unsigned OpNo = 0; |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 545 | bool NodeHasChain = N->NodeHasProperty(SDNPHasChain, CGP); |
| 546 | bool HasChain = N->TreeHasProperty(SDNPHasChain, CGP); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 547 | if (HasChain) { |
| 548 | if (NodeHasChain) |
| 549 | OpNo = 1; |
| 550 | if (!isRoot) { |
Evan Cheng | 014bf21 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 551 | // Check if it's profitable to fold the node. e.g. Check for multiple uses |
| 552 | // of actual result? |
| 553 | std::string ParentName(RootName.begin(), RootName.end()-1); |
Chris Lattner | 29c6270 | 2010-02-16 19:03:34 +0000 | [diff] [blame] | 554 | if (!NodeHasChain) { |
| 555 | // If this is just an interior node, check to see if it has a single |
| 556 | // use. If the node has multiple uses and the pattern has a load as |
| 557 | // an operand, then we can't fold the load. |
| 558 | emitCheck(getValueName(RootName) + ".hasOneUse()"); |
Chris Lattner | 92d3ada | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 559 | } else if (!N->isLeaf()) { // ComplexPatterns do their own legality check. |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 560 | // If the immediate use can somehow reach this node through another |
| 561 | // path, then can't fold it either or it will create a cycle. |
| 562 | // e.g. In the following diagram, XX can reach ld through YY. If |
| 563 | // ld is folded into XX, then YY is both a predecessor and a successor |
| 564 | // of XX. |
| 565 | // |
| 566 | // [ld] |
| 567 | // ^ ^ |
| 568 | // | | |
| 569 | // / \--- |
| 570 | // / [YY] |
| 571 | // | ^ |
| 572 | // [XX]-------| |
Chris Lattner | e39650a | 2010-02-16 06:10:58 +0000 | [diff] [blame] | 573 | |
| 574 | // We know we need the check if N's parent is not the root. |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 575 | bool NeedCheck = P != Pattern; |
| 576 | if (!NeedCheck) { |
Chris Lattner | 29c6270 | 2010-02-16 19:03:34 +0000 | [diff] [blame] | 577 | // If the parent is the root and the node has more than one operand, |
| 578 | // we need to check. |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 579 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator()); |
| 580 | NeedCheck = |
| 581 | P->getOperator() == CGP.get_intrinsic_void_sdnode() || |
| 582 | P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() || |
| 583 | P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() || |
| 584 | PInfo.getNumOperands() > 1 || |
| 585 | PInfo.hasProperty(SDNPHasChain) || |
| 586 | PInfo.hasProperty(SDNPInFlag) || |
| 587 | PInfo.hasProperty(SDNPOptInFlag); |
| 588 | } |
| 589 | |
| 590 | if (NeedCheck) { |
Chris Lattner | 29c6270 | 2010-02-16 19:03:34 +0000 | [diff] [blame] | 591 | emitCheck("IsProfitableToFold(" + getValueName(RootName) + |
| 592 | ", " + getNodeName(ParentName) + ", N)"); |
Evan Cheng | 014bf21 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 593 | emitCheck("IsLegalToFold(" + getValueName(RootName) + |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 594 | ", " + getNodeName(ParentName) + ", N)"); |
Chris Lattner | 29c6270 | 2010-02-16 19:03:34 +0000 | [diff] [blame] | 595 | } else { |
| 596 | // Otherwise, just verify that the node only has a single use. |
| 597 | emitCheck(getValueName(RootName) + ".hasOneUse()"); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (NodeHasChain) { |
| 603 | if (FoundChain) { |
Chris Lattner | d9c1a34 | 2010-02-17 05:35:28 +0000 | [diff] [blame] | 604 | emitCheck("IsChainCompatible(" + ChainName + ".getNode(), " + |
| 605 | getNodeName(RootName) + ")"); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 606 | OrigChains.push_back(std::make_pair(ChainName, |
| 607 | getValueName(RootName))); |
| 608 | } else |
| 609 | FoundChain = true; |
| 610 | ChainName = "Chain" + ChainSuffix; |
Chris Lattner | 92d3ada | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 611 | |
| 612 | if (!N->getComplexPatternInfo(CGP) || |
| 613 | isRoot) |
| 614 | emitInit("SDValue " + ChainName + " = " + getNodeName(RootName) + |
| 615 | "->getOperand(0);"); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 619 | // If there are node predicates for this, emit the calls. |
| 620 | for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i) |
| 621 | emitCheck(N->getPredicateFns()[i] + "(" + getNodeName(RootName) + ")"); |
| 622 | |
| 623 | // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is |
| 624 | // a constant without a predicate fn that has more that one bit set, handle |
| 625 | // this as a special case. This is usually for targets that have special |
| 626 | // handling of certain large constants (e.g. alpha with it's 8/16/32-bit |
| 627 | // handling stuff). Using these instructions is often far more efficient |
| 628 | // than materializing the constant. Unfortunately, both the instcombiner |
| 629 | // and the dag combiner can often infer that bits are dead, and thus drop |
| 630 | // them from the mask in the dag. For example, it might turn 'AND X, 255' |
| 631 | // into 'AND X, 254' if it knows the low bit is set. Emit code that checks |
| 632 | // to handle this. |
| 633 | if (!N->isLeaf() && |
| 634 | (N->getOperator()->getName() == "and" || |
| 635 | N->getOperator()->getName() == "or") && |
| 636 | N->getChild(1)->isLeaf() && |
| 637 | N->getChild(1)->getPredicateFns().empty()) { |
| 638 | if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) { |
| 639 | if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits. |
| 640 | emitInit("SDValue " + RootName + "0" + " = " + |
| 641 | getNodeName(RootName) + "->getOperand(" + utostr(0) + ");"); |
| 642 | emitInit("SDValue " + RootName + "1" + " = " + |
| 643 | getNodeName(RootName) + "->getOperand(" + utostr(1) + ");"); |
| 644 | |
| 645 | unsigned NTmp = TmpNo++; |
| 646 | emitCode("ConstantSDNode *Tmp" + utostr(NTmp) + |
| 647 | " = dyn_cast<ConstantSDNode>(" + |
| 648 | getNodeName(RootName + "1") + ");"); |
| 649 | emitCheck("Tmp" + utostr(NTmp)); |
| 650 | const char *MaskPredicate = N->getOperator()->getName() == "or" |
| 651 | ? "CheckOrMask(" : "CheckAndMask("; |
| 652 | emitCheck(MaskPredicate + getValueName(RootName + "0") + |
| 653 | ", Tmp" + utostr(NTmp) + |
| 654 | ", INT64_C(" + itostr(II->getValue()) + "))"); |
| 655 | |
| 656 | EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), |
| 657 | ChainSuffix + utostr(0), FoundChain); |
| 658 | return; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 664 | emitInit("SDValue " + getValueName(RootName + utostr(OpNo)) + " = " + |
| 665 | getNodeName(RootName) + "->getOperand(" + utostr(OpNo) + ");"); |
| 666 | |
| 667 | EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), |
| 668 | ChainSuffix + utostr(OpNo), FoundChain); |
| 669 | } |
| 670 | |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 671 | // Handle complex patterns. |
| 672 | if (const ComplexPattern *CP = N->getComplexPatternInfo(CGP)) { |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 673 | std::string Fn = CP->getSelectFunc(); |
| 674 | unsigned NumOps = CP->getNumOperands(); |
| 675 | for (unsigned i = 0; i < NumOps; ++i) { |
| 676 | emitDecl("CPTmp" + RootName + "_" + utostr(i)); |
| 677 | emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";"); |
| 678 | } |
| 679 | if (CP->hasProperty(SDNPHasChain)) { |
| 680 | emitDecl("CPInChain"); |
| 681 | emitDecl("Chain" + ChainSuffix); |
| 682 | emitCode("SDValue CPInChain;"); |
| 683 | emitCode("SDValue Chain" + ChainSuffix + ";"); |
| 684 | } |
| 685 | |
Chris Lattner | 92d3ada | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 686 | std::string Code = Fn + "(N, "; // always pass in the root. |
| 687 | Code += getValueName(RootName); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 688 | for (unsigned i = 0; i < NumOps; i++) |
| 689 | Code += ", CPTmp" + RootName + "_" + utostr(i); |
| 690 | if (CP->hasProperty(SDNPHasChain)) { |
| 691 | ChainName = "Chain" + ChainSuffix; |
Chris Lattner | e609a51 | 2010-02-17 00:31:50 +0000 | [diff] [blame] | 692 | Code += ", CPInChain, " + ChainName; |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 693 | } |
| 694 | emitCheck(Code + ")"); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | void PatternCodeEmitter::EmitChildMatchCode(TreePatternNode *Child, |
| 699 | TreePatternNode *Parent, |
| 700 | const std::string &RootName, |
| 701 | const std::string &ChainSuffix, |
| 702 | bool &FoundChain) { |
| 703 | if (!Child->isLeaf()) { |
| 704 | // If it's not a leaf, recursively match. |
| 705 | const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator()); |
| 706 | emitCheck(getNodeName(RootName) + "->getOpcode() == " + |
| 707 | CInfo.getEnumName()); |
| 708 | EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain); |
| 709 | bool HasChain = false; |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 710 | if (Child->NodeHasProperty(SDNPHasChain, CGP)) { |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 711 | HasChain = true; |
| 712 | FoldedChains.push_back(std::make_pair(getValueName(RootName), |
| 713 | CInfo.getNumResults())); |
| 714 | } |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 715 | if (Child->NodeHasProperty(SDNPOutFlag, CGP)) { |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 716 | assert(FoldedFlag.first == "" && FoldedFlag.second == 0 && |
| 717 | "Pattern folded multiple nodes which produce flags?"); |
| 718 | FoldedFlag = std::make_pair(getValueName(RootName), |
| 719 | CInfo.getNumResults() + (unsigned)HasChain); |
| 720 | } |
Chris Lattner | 5b08f77 | 2010-02-16 22:38:31 +0000 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | if (const ComplexPattern *CP = Child->getComplexPatternInfo(CGP)) { |
Chris Lattner | 92d3ada | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 725 | EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain); |
| 726 | bool HasChain = false; |
| 727 | |
| 728 | if (Child->NodeHasProperty(SDNPHasChain, CGP)) { |
| 729 | HasChain = true; |
| 730 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator()); |
| 731 | FoldedChains.push_back(std::make_pair("CPInChain", |
| 732 | PInfo.getNumResults())); |
| 733 | } |
| 734 | if (Child->NodeHasProperty(SDNPOutFlag, CGP)) { |
| 735 | assert(FoldedFlag.first == "" && FoldedFlag.second == 0 && |
| 736 | "Pattern folded multiple nodes which produce flags?"); |
| 737 | FoldedFlag = std::make_pair(getValueName(RootName), |
| 738 | CP->getNumOperands() + (unsigned)HasChain); |
| 739 | } |
Chris Lattner | 5b08f77 | 2010-02-16 22:38:31 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
| 743 | // If this child has a name associated with it, capture it in VarMap. If |
| 744 | // we already saw this in the pattern, emit code to verify dagness. |
| 745 | if (!Child->getName().empty()) { |
| 746 | std::string &VarMapEntry = VariableMap[Child->getName()]; |
| 747 | if (VarMapEntry.empty()) { |
| 748 | VarMapEntry = getValueName(RootName); |
| 749 | } else { |
| 750 | // If we get here, this is a second reference to a specific name. |
| 751 | // Since we already have checked that the first reference is valid, |
| 752 | // we don't have to recursively match it, just check that it's the |
| 753 | // same as the previously named thing. |
| 754 | emitCheck(VarMapEntry + " == " + getValueName(RootName)); |
| 755 | Duplicates.insert(getValueName(RootName)); |
| 756 | return; |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 757 | } |
Chris Lattner | 5b08f77 | 2010-02-16 22:38:31 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // Handle leaves of various types. |
| 761 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
| 762 | Record *LeafRec = DI->getDef(); |
| 763 | if (LeafRec->isSubClassOf("RegisterClass") || |
| 764 | LeafRec->isSubClassOf("PointerLikeRegClass")) { |
| 765 | // Handle register references. Nothing to do here. |
| 766 | } else if (LeafRec->isSubClassOf("Register")) { |
| 767 | // Handle register references. |
| 768 | } else if (LeafRec->getName() == "srcvalue") { |
| 769 | // Place holder for SRCVALUE nodes. Nothing to do here. |
| 770 | } else if (LeafRec->isSubClassOf("ValueType")) { |
| 771 | // Make sure this is the specified value type. |
| 772 | emitCheck("cast<VTSDNode>(" + getNodeName(RootName) + |
| 773 | ")->getVT() == MVT::" + LeafRec->getName()); |
| 774 | } else if (LeafRec->isSubClassOf("CondCode")) { |
| 775 | // Make sure this is the specified cond code. |
| 776 | emitCheck("cast<CondCodeSDNode>(" + getNodeName(RootName) + |
| 777 | ")->get() == ISD::" + LeafRec->getName()); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 778 | } else { |
| 779 | #ifndef NDEBUG |
| 780 | Child->dump(); |
Chris Lattner | 5b08f77 | 2010-02-16 22:38:31 +0000 | [diff] [blame] | 781 | errs() << " "; |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 782 | #endif |
| 783 | assert(0 && "Unknown leaf type!"); |
| 784 | } |
Chris Lattner | 5b08f77 | 2010-02-16 22:38:31 +0000 | [diff] [blame] | 785 | |
| 786 | // If there are node predicates for this, emit the calls. |
| 787 | for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i) |
| 788 | emitCheck(Child->getPredicateFns()[i] + "(" + getNodeName(RootName) + |
| 789 | ")"); |
| 790 | return; |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | 5b08f77 | 2010-02-16 22:38:31 +0000 | [diff] [blame] | 792 | |
| 793 | if (IntInit *II = dynamic_cast<IntInit*>(Child->getLeafValue())) { |
| 794 | unsigned NTmp = TmpNo++; |
| 795 | emitCode("ConstantSDNode *Tmp"+ utostr(NTmp) + |
| 796 | " = dyn_cast<ConstantSDNode>("+ |
| 797 | getNodeName(RootName) + ");"); |
| 798 | emitCheck("Tmp" + utostr(NTmp)); |
| 799 | unsigned CTmp = TmpNo++; |
| 800 | emitCode("int64_t CN"+ utostr(CTmp) + |
| 801 | " = Tmp" + utostr(NTmp) + "->getSExtValue();"); |
| 802 | emitCheck("CN" + utostr(CTmp) + " == " |
| 803 | "INT64_C(" +itostr(II->getValue()) + ")"); |
| 804 | return; |
| 805 | } |
| 806 | #ifndef NDEBUG |
| 807 | Child->dump(); |
| 808 | #endif |
| 809 | assert(0 && "Unknown leaf type!"); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /// EmitResultCode - Emit the action for a pattern. Now that it has matched |
| 813 | /// we actually have to build a DAG! |
| 814 | std::vector<std::string> |
| 815 | PatternCodeEmitter::EmitResultCode(TreePatternNode *N, |
| 816 | std::vector<Record*> DstRegs, |
| 817 | bool InFlagDecled, bool ResNodeDecled, |
| 818 | bool LikeLeaf, bool isRoot) { |
| 819 | // List of arguments of getMachineNode() or SelectNodeTo(). |
| 820 | std::vector<std::string> NodeOps; |
| 821 | // This is something selected from the pattern we matched. |
| 822 | if (!N->getName().empty()) { |
| 823 | const std::string &VarName = N->getName(); |
| 824 | std::string Val = VariableMap[VarName]; |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 825 | if (Val.empty()) { |
| 826 | errs() << "Variable '" << VarName << " referenced but not defined " |
| 827 | << "and not caught earlier!\n"; |
| 828 | abort(); |
| 829 | } |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 830 | |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 831 | unsigned ResNo = TmpNo++; |
| 832 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") { |
| 833 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
| 834 | std::string CastType; |
| 835 | std::string TmpVar = "Tmp" + utostr(ResNo); |
| 836 | switch (N->getTypeNum(0)) { |
| 837 | default: |
| 838 | errs() << "Cannot handle " << getEnumName(N->getTypeNum(0)) |
| 839 | << " type as an immediate constant. Aborting\n"; |
| 840 | abort(); |
| 841 | case MVT::i1: CastType = "bool"; break; |
| 842 | case MVT::i8: CastType = "unsigned char"; break; |
| 843 | case MVT::i16: CastType = "unsigned short"; break; |
| 844 | case MVT::i32: CastType = "unsigned"; break; |
| 845 | case MVT::i64: CastType = "uint64_t"; break; |
| 846 | } |
| 847 | emitCode("SDValue " + TmpVar + |
| 848 | " = CurDAG->getTargetConstant(((" + CastType + |
| 849 | ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " + |
| 850 | getEnumName(N->getTypeNum(0)) + ");"); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 851 | NodeOps.push_back(getValueName(TmpVar)); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 852 | } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") { |
| 853 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
| 854 | std::string TmpVar = "Tmp" + utostr(ResNo); |
| 855 | emitCode("SDValue " + TmpVar + |
| 856 | " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" + |
| 857 | Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" + |
| 858 | Val + ")->getValueType(0));"); |
Chris Lattner | 8e946be | 2010-02-21 03:22:59 +0000 | [diff] [blame] | 859 | NodeOps.push_back(getValueName(TmpVar)); |
| 860 | } else if (const ComplexPattern *CP = N->getComplexPatternInfo(CGP)) { |
| 861 | for (unsigned i = 0; i < CP->getNumOperands(); ++i) |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 862 | NodeOps.push_back(getValueName("CPTmp" + Val + "_" + utostr(i))); |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 863 | } else { |
| 864 | // This node, probably wrapped in a SDNodeXForm, behaves like a leaf |
| 865 | // node even if it isn't one. Don't select it. |
| 866 | if (!LikeLeaf) { |
| 867 | if (isRoot && N->isLeaf()) { |
| 868 | emitCode("ReplaceUses(SDValue(N, 0), " + Val + ");"); |
| 869 | emitCode("return NULL;"); |
| 870 | } |
| 871 | } |
| 872 | NodeOps.push_back(getValueName(Val)); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 873 | } |
| 874 | return NodeOps; |
| 875 | } |
| 876 | if (N->isLeaf()) { |
| 877 | // If this is an explicit register reference, handle it. |
| 878 | if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
| 879 | unsigned ResNo = TmpNo++; |
| 880 | if (DI->getDef()->isSubClassOf("Register")) { |
| 881 | emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" + |
| 882 | getQualifiedName(DI->getDef()) + ", " + |
| 883 | getEnumName(N->getTypeNum(0)) + ");"); |
| 884 | NodeOps.push_back(getValueName("Tmp" + utostr(ResNo))); |
| 885 | return NodeOps; |
| 886 | } else if (DI->getDef()->getName() == "zero_reg") { |
| 887 | emitCode("SDValue Tmp" + utostr(ResNo) + |
| 888 | " = CurDAG->getRegister(0, " + |
| 889 | getEnumName(N->getTypeNum(0)) + ");"); |
| 890 | NodeOps.push_back(getValueName("Tmp" + utostr(ResNo))); |
| 891 | return NodeOps; |
| 892 | } else if (DI->getDef()->isSubClassOf("RegisterClass")) { |
| 893 | // Handle a reference to a register class. This is used |
| 894 | // in COPY_TO_SUBREG instructions. |
| 895 | emitCode("SDValue Tmp" + utostr(ResNo) + |
| 896 | " = CurDAG->getTargetConstant(" + |
| 897 | getQualifiedName(DI->getDef()) + "RegClassID, " + |
| 898 | "MVT::i32);"); |
| 899 | NodeOps.push_back(getValueName("Tmp" + utostr(ResNo))); |
| 900 | return NodeOps; |
| 901 | } |
| 902 | } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
| 903 | unsigned ResNo = TmpNo++; |
| 904 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
| 905 | emitCode("SDValue Tmp" + utostr(ResNo) + |
| 906 | " = CurDAG->getTargetConstant(0x" + |
| 907 | utohexstr((uint64_t) II->getValue()) + |
| 908 | "ULL, " + getEnumName(N->getTypeNum(0)) + ");"); |
| 909 | NodeOps.push_back(getValueName("Tmp" + utostr(ResNo))); |
| 910 | return NodeOps; |
| 911 | } |
| 912 | |
| 913 | #ifndef NDEBUG |
| 914 | N->dump(); |
| 915 | #endif |
| 916 | assert(0 && "Unknown leaf type!"); |
| 917 | return NodeOps; |
| 918 | } |
| 919 | |
| 920 | Record *Op = N->getOperator(); |
| 921 | if (Op->isSubClassOf("Instruction")) { |
| 922 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
| 923 | CodeGenInstruction &II = CGT.getInstruction(Op->getName()); |
| 924 | const DAGInstruction &Inst = CGP.getInstruction(Op); |
| 925 | const TreePattern *InstPat = Inst.getPattern(); |
| 926 | // FIXME: Assume actual pattern comes before "implicit". |
| 927 | TreePatternNode *InstPatNode = |
| 928 | isRoot ? (InstPat ? InstPat->getTree(0) : Pattern) |
| 929 | : (InstPat ? InstPat->getTree(0) : NULL); |
| 930 | if (InstPatNode && !InstPatNode->isLeaf() && |
| 931 | InstPatNode->getOperator()->getName() == "set") { |
| 932 | InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1); |
| 933 | } |
| 934 | bool IsVariadic = isRoot && II.isVariadic; |
| 935 | // FIXME: fix how we deal with physical register operands. |
| 936 | bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0; |
| 937 | bool HasImpResults = isRoot && DstRegs.size() > 0; |
| 938 | bool NodeHasOptInFlag = isRoot && |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 939 | Pattern->TreeHasProperty(SDNPOptInFlag, CGP); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 940 | bool NodeHasInFlag = isRoot && |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 941 | Pattern->TreeHasProperty(SDNPInFlag, CGP); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 942 | bool NodeHasOutFlag = isRoot && |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 943 | Pattern->TreeHasProperty(SDNPOutFlag, CGP); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 944 | bool NodeHasChain = InstPatNode && |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 945 | InstPatNode->TreeHasProperty(SDNPHasChain, CGP); |
| 946 | bool InputHasChain = isRoot && Pattern->NodeHasProperty(SDNPHasChain, CGP); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 947 | unsigned NumResults = Inst.getNumResults(); |
| 948 | unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0; |
| 949 | |
| 950 | // Record output varargs info. |
| 951 | OutputIsVariadic = IsVariadic; |
| 952 | |
| 953 | if (NodeHasOptInFlag) { |
| 954 | emitCode("bool HasInFlag = " |
| 955 | "(N->getOperand(N->getNumOperands()-1).getValueType() == " |
| 956 | "MVT::Flag);"); |
| 957 | } |
| 958 | if (IsVariadic) |
| 959 | emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";"); |
Chris Lattner | b49985a | 2010-02-18 06:47:49 +0000 | [diff] [blame] | 960 | |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 961 | // How many results is this pattern expected to produce? |
| 962 | unsigned NumPatResults = 0; |
| 963 | for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) { |
| 964 | MVT::SimpleValueType VT = Pattern->getTypeNum(i); |
| 965 | if (VT != MVT::isVoid && VT != MVT::Flag) |
| 966 | NumPatResults++; |
| 967 | } |
| 968 | |
| 969 | if (OrigChains.size() > 0) { |
| 970 | // The original input chain is being ignored. If it is not just |
| 971 | // pointing to the op that's being folded, we should create a |
| 972 | // TokenFactor with it and the chain of the folded op as the new chain. |
| 973 | // We could potentially be doing multiple levels of folding, in that |
| 974 | // case, the TokenFactor can have more operands. |
| 975 | emitCode("SmallVector<SDValue, 8> InChains;"); |
| 976 | for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) { |
| 977 | emitCode("if (" + OrigChains[i].first + ".getNode() != " + |
| 978 | OrigChains[i].second + ".getNode()) {"); |
| 979 | emitCode(" InChains.push_back(" + OrigChains[i].first + ");"); |
| 980 | emitCode("}"); |
| 981 | } |
| 982 | emitCode("InChains.push_back(" + ChainName + ");"); |
| 983 | emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, " |
| 984 | "N->getDebugLoc(), MVT::Other, " |
| 985 | "&InChains[0], InChains.size());"); |
| 986 | if (GenDebug) { |
Chris Lattner | dcdcef2 | 2010-02-18 00:23:27 +0000 | [diff] [blame] | 987 | emitCode("CurDAG->setSubgraphColor(" + ChainName + |
| 988 | ".getNode(), \"yellow\");"); |
| 989 | emitCode("CurDAG->setSubgraphColor(" + ChainName + |
| 990 | ".getNode(), \"black\");"); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
| 994 | // Loop over all of the operands of the instruction pattern, emitting code |
| 995 | // to fill them all in. The node 'N' usually has number children equal to |
| 996 | // the number of input operands of the instruction. However, in cases |
| 997 | // where there are predicate operands for an instruction, we need to fill |
| 998 | // in the 'execute always' values. Match up the node operands to the |
| 999 | // instruction operands to do this. |
| 1000 | std::vector<std::string> AllOps; |
| 1001 | for (unsigned ChildNo = 0, InstOpNo = NumResults; |
| 1002 | InstOpNo != II.OperandList.size(); ++InstOpNo) { |
| 1003 | std::vector<std::string> Ops; |
| 1004 | |
| 1005 | // Determine what to emit for this operand. |
| 1006 | Record *OperandNode = II.OperandList[InstOpNo].Rec; |
| 1007 | if ((OperandNode->isSubClassOf("PredicateOperand") || |
| 1008 | OperandNode->isSubClassOf("OptionalDefOperand")) && |
| 1009 | !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) { |
| 1010 | // This is a predicate or optional def operand; emit the |
| 1011 | // 'default ops' operands. |
| 1012 | const DAGDefaultOperand &DefaultOp = |
| 1013 | CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec); |
| 1014 | for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) { |
| 1015 | Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs, |
| 1016 | InFlagDecled, ResNodeDecled); |
| 1017 | AllOps.insert(AllOps.end(), Ops.begin(), Ops.end()); |
| 1018 | } |
| 1019 | } else { |
| 1020 | // Otherwise this is a normal operand or a predicate operand without |
| 1021 | // 'execute always'; emit it. |
| 1022 | Ops = EmitResultCode(N->getChild(ChildNo), DstRegs, |
| 1023 | InFlagDecled, ResNodeDecled); |
| 1024 | AllOps.insert(AllOps.end(), Ops.begin(), Ops.end()); |
| 1025 | ++ChildNo; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Emit all the chain and CopyToReg stuff. |
| 1030 | bool ChainEmitted = NodeHasChain; |
| 1031 | if (NodeHasInFlag || HasImpInputs) |
| 1032 | EmitInFlagSelectCode(Pattern, "N", ChainEmitted, |
| 1033 | InFlagDecled, ResNodeDecled, true); |
| 1034 | if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) { |
| 1035 | if (!InFlagDecled) { |
| 1036 | emitCode("SDValue InFlag(0, 0);"); |
| 1037 | InFlagDecled = true; |
| 1038 | } |
| 1039 | if (NodeHasOptInFlag) { |
| 1040 | emitCode("if (HasInFlag) {"); |
| 1041 | emitCode(" InFlag = N->getOperand(N->getNumOperands()-1);"); |
| 1042 | emitCode("}"); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | unsigned ResNo = TmpNo++; |
| 1047 | |
| 1048 | unsigned OpsNo = OpcNo; |
| 1049 | std::string CodePrefix; |
| 1050 | bool ChainAssignmentNeeded = NodeHasChain && !isRoot; |
| 1051 | std::deque<std::string> After; |
| 1052 | std::string NodeName; |
| 1053 | if (!isRoot) { |
| 1054 | NodeName = "Tmp" + utostr(ResNo); |
| 1055 | CodePrefix = "SDValue " + NodeName + "("; |
| 1056 | } else { |
| 1057 | NodeName = "ResNode"; |
| 1058 | if (!ResNodeDecled) { |
| 1059 | CodePrefix = "SDNode *" + NodeName + " = "; |
| 1060 | ResNodeDecled = true; |
| 1061 | } else |
| 1062 | CodePrefix = NodeName + " = "; |
| 1063 | } |
| 1064 | |
| 1065 | std::string Code = "Opc" + utostr(OpcNo); |
| 1066 | |
| 1067 | if (!isRoot || (InputHasChain && !NodeHasChain)) |
| 1068 | // For call to "getMachineNode()". |
| 1069 | Code += ", N->getDebugLoc()"; |
| 1070 | |
| 1071 | emitOpcode(II.Namespace + "::" + II.TheDef->getName()); |
| 1072 | |
| 1073 | // Output order: results, chain, flags |
| 1074 | // Result types. |
| 1075 | if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) { |
| 1076 | Code += ", VT" + utostr(VTNo); |
| 1077 | emitVT(getEnumName(N->getTypeNum(0))); |
| 1078 | } |
| 1079 | // Add types for implicit results in physical registers, scheduler will |
| 1080 | // care of adding copyfromreg nodes. |
| 1081 | for (unsigned i = 0; i < NumDstRegs; i++) { |
| 1082 | Record *RR = DstRegs[i]; |
| 1083 | if (RR->isSubClassOf("Register")) { |
| 1084 | MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT); |
| 1085 | Code += ", " + getEnumName(RVT); |
| 1086 | } |
| 1087 | } |
| 1088 | if (NodeHasChain) |
| 1089 | Code += ", MVT::Other"; |
| 1090 | if (NodeHasOutFlag) |
| 1091 | Code += ", MVT::Flag"; |
| 1092 | |
| 1093 | // Inputs. |
| 1094 | if (IsVariadic) { |
| 1095 | for (unsigned i = 0, e = AllOps.size(); i != e; ++i) |
| 1096 | emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");"); |
| 1097 | AllOps.clear(); |
| 1098 | |
| 1099 | // Figure out whether any operands at the end of the op list are not |
| 1100 | // part of the variable section. |
| 1101 | std::string EndAdjust; |
| 1102 | if (NodeHasInFlag || HasImpInputs) |
| 1103 | EndAdjust = "-1"; // Always has one flag. |
| 1104 | else if (NodeHasOptInFlag) |
| 1105 | EndAdjust = "-(HasInFlag?1:0)"; // May have a flag. |
| 1106 | |
| 1107 | emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) + |
| 1108 | ", e = N->getNumOperands()" + EndAdjust + "; i != e; ++i) {"); |
| 1109 | |
| 1110 | emitCode(" Ops" + utostr(OpsNo) + ".push_back(N->getOperand(i));"); |
| 1111 | emitCode("}"); |
| 1112 | } |
| 1113 | |
| 1114 | // Populate MemRefs with entries for each memory accesses covered by |
| 1115 | // this pattern. |
| 1116 | if (isRoot && !LSI.empty()) { |
| 1117 | std::string MemRefs = "MemRefs" + utostr(OpsNo); |
| 1118 | emitCode("MachineSDNode::mmo_iterator " + MemRefs + " = " |
| 1119 | "MF->allocateMemRefsArray(" + utostr(LSI.size()) + ");"); |
| 1120 | for (unsigned i = 0, e = LSI.size(); i != e; ++i) |
| 1121 | emitCode(MemRefs + "[" + utostr(i) + "] = " |
| 1122 | "cast<MemSDNode>(" + LSI[i] + ")->getMemOperand();"); |
| 1123 | After.push_back("cast<MachineSDNode>(ResNode)->setMemRefs(" + |
| 1124 | MemRefs + ", " + MemRefs + " + " + utostr(LSI.size()) + |
| 1125 | ");"); |
| 1126 | } |
| 1127 | |
| 1128 | if (NodeHasChain) { |
| 1129 | if (IsVariadic) |
| 1130 | emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");"); |
| 1131 | else |
| 1132 | AllOps.push_back(ChainName); |
| 1133 | } |
| 1134 | |
| 1135 | if (IsVariadic) { |
| 1136 | if (NodeHasInFlag || HasImpInputs) |
| 1137 | emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);"); |
| 1138 | else if (NodeHasOptInFlag) { |
| 1139 | emitCode("if (HasInFlag)"); |
| 1140 | emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);"); |
| 1141 | } |
| 1142 | Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) + |
| 1143 | ".size()"; |
| 1144 | } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs) |
| 1145 | AllOps.push_back("InFlag"); |
| 1146 | |
| 1147 | unsigned NumOps = AllOps.size(); |
| 1148 | if (NumOps) { |
| 1149 | if (!NodeHasOptInFlag && NumOps < 4) { |
| 1150 | for (unsigned i = 0; i != NumOps; ++i) |
| 1151 | Code += ", " + AllOps[i]; |
| 1152 | } else { |
| 1153 | std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { "; |
| 1154 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1155 | OpsCode += AllOps[i]; |
| 1156 | if (i != NumOps-1) |
| 1157 | OpsCode += ", "; |
| 1158 | } |
| 1159 | emitCode(OpsCode + " };"); |
| 1160 | Code += ", Ops" + utostr(OpsNo) + ", "; |
| 1161 | if (NodeHasOptInFlag) { |
| 1162 | Code += "HasInFlag ? "; |
| 1163 | Code += utostr(NumOps) + " : " + utostr(NumOps-1); |
| 1164 | } else |
| 1165 | Code += utostr(NumOps); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | if (!isRoot) |
| 1170 | Code += "), 0"; |
| 1171 | |
| 1172 | std::vector<std::string> ReplaceFroms; |
| 1173 | std::vector<std::string> ReplaceTos; |
| 1174 | if (!isRoot) { |
| 1175 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 1176 | } else { |
| 1177 | |
| 1178 | if (NodeHasOutFlag) { |
| 1179 | if (!InFlagDecled) { |
| 1180 | After.push_back("SDValue InFlag(ResNode, " + |
| 1181 | utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + |
| 1182 | ");"); |
| 1183 | InFlagDecled = true; |
| 1184 | } else |
| 1185 | After.push_back("InFlag = SDValue(ResNode, " + |
| 1186 | utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + |
| 1187 | ");"); |
| 1188 | } |
| 1189 | |
| 1190 | for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) { |
| 1191 | ReplaceFroms.push_back("SDValue(" + |
| 1192 | FoldedChains[j].first + ".getNode(), " + |
| 1193 | utostr(FoldedChains[j].second) + |
| 1194 | ")"); |
| 1195 | ReplaceTos.push_back("SDValue(ResNode, " + |
| 1196 | utostr(NumResults+NumDstRegs) + ")"); |
| 1197 | } |
| 1198 | |
| 1199 | if (NodeHasOutFlag) { |
| 1200 | if (FoldedFlag.first != "") { |
| 1201 | ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".getNode(), " + |
| 1202 | utostr(FoldedFlag.second) + ")"); |
| 1203 | ReplaceTos.push_back("InFlag"); |
| 1204 | } else { |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 1205 | assert(Pattern->NodeHasProperty(SDNPOutFlag, CGP)); |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 1206 | ReplaceFroms.push_back("SDValue(N, " + |
| 1207 | utostr(NumPatResults + (unsigned)InputHasChain) |
| 1208 | + ")"); |
| 1209 | ReplaceTos.push_back("InFlag"); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if (!ReplaceFroms.empty() && InputHasChain) { |
| 1214 | ReplaceFroms.push_back("SDValue(N, " + |
| 1215 | utostr(NumPatResults) + ")"); |
| 1216 | ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " + |
| 1217 | ChainName + ".getResNo()" + ")"); |
| 1218 | ChainAssignmentNeeded |= NodeHasChain; |
| 1219 | } |
| 1220 | |
| 1221 | // User does not expect the instruction would produce a chain! |
| 1222 | if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) { |
| 1223 | ; |
| 1224 | } else if (InputHasChain && !NodeHasChain) { |
| 1225 | // One of the inner node produces a chain. |
| 1226 | assert(!NodeHasOutFlag && "Node has flag but not chain!"); |
| 1227 | ReplaceFroms.push_back("SDValue(N, " + |
| 1228 | utostr(NumPatResults) + ")"); |
| 1229 | ReplaceTos.push_back(ChainName); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | if (ChainAssignmentNeeded) { |
| 1234 | // Remember which op produces the chain. |
| 1235 | std::string ChainAssign; |
| 1236 | if (!isRoot) |
| 1237 | ChainAssign = ChainName + " = SDValue(" + NodeName + |
| 1238 | ".getNode(), " + utostr(NumResults+NumDstRegs) + ");"; |
| 1239 | else |
| 1240 | ChainAssign = ChainName + " = SDValue(" + NodeName + |
| 1241 | ", " + utostr(NumResults+NumDstRegs) + ");"; |
| 1242 | |
| 1243 | After.push_front(ChainAssign); |
| 1244 | } |
| 1245 | |
| 1246 | if (ReplaceFroms.size() == 1) { |
| 1247 | After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " + |
| 1248 | ReplaceTos[0] + ");"); |
| 1249 | } else if (!ReplaceFroms.empty()) { |
| 1250 | After.push_back("const SDValue Froms[] = {"); |
| 1251 | for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i) |
| 1252 | After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : "")); |
| 1253 | After.push_back("};"); |
| 1254 | After.push_back("const SDValue Tos[] = {"); |
| 1255 | for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i) |
| 1256 | After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : "")); |
| 1257 | After.push_back("};"); |
| 1258 | After.push_back("ReplaceUses(Froms, Tos, " + |
| 1259 | itostr(ReplaceFroms.size()) + ");"); |
| 1260 | } |
| 1261 | |
| 1262 | // We prefer to use SelectNodeTo since it avoids allocation when |
| 1263 | // possible and it avoids CSE map recalculation for the node's |
| 1264 | // users, however it's tricky to use in a non-root context. |
| 1265 | // |
| 1266 | // We also don't use SelectNodeTo if the pattern replacement is being |
| 1267 | // used to jettison a chain result, since morphing the node in place |
| 1268 | // would leave users of the chain dangling. |
| 1269 | // |
| 1270 | if (!isRoot || (InputHasChain && !NodeHasChain)) { |
| 1271 | Code = "CurDAG->getMachineNode(" + Code; |
| 1272 | } else { |
| 1273 | Code = "CurDAG->SelectNodeTo(N, " + Code; |
| 1274 | } |
| 1275 | if (isRoot) { |
| 1276 | if (After.empty()) |
| 1277 | CodePrefix = "return "; |
| 1278 | else |
| 1279 | After.push_back("return ResNode;"); |
| 1280 | } |
| 1281 | |
| 1282 | emitCode(CodePrefix + Code + ");"); |
| 1283 | |
| 1284 | if (GenDebug) { |
| 1285 | if (!isRoot) { |
| 1286 | emitCode("CurDAG->setSubgraphColor(" + |
| 1287 | NodeName +".getNode(), \"yellow\");"); |
| 1288 | emitCode("CurDAG->setSubgraphColor(" + |
| 1289 | NodeName +".getNode(), \"black\");"); |
| 1290 | } else { |
| 1291 | emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");"); |
| 1292 | emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");"); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | for (unsigned i = 0, e = After.size(); i != e; ++i) |
| 1297 | emitCode(After[i]); |
| 1298 | |
| 1299 | return NodeOps; |
| 1300 | } |
| 1301 | if (Op->isSubClassOf("SDNodeXForm")) { |
| 1302 | assert(N->getNumChildren() == 1 && "node xform should have one child!"); |
| 1303 | // PatLeaf node - the operand may or may not be a leaf node. But it should |
| 1304 | // behave like one. |
| 1305 | std::vector<std::string> Ops = |
| 1306 | EmitResultCode(N->getChild(0), DstRegs, InFlagDecled, |
| 1307 | ResNodeDecled, true); |
| 1308 | unsigned ResNo = TmpNo++; |
| 1309 | emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName() |
| 1310 | + "(" + Ops.back() + ".getNode());"); |
| 1311 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 1312 | if (isRoot) |
| 1313 | emitCode("return Tmp" + utostr(ResNo) + ".getNode();"); |
| 1314 | return NodeOps; |
| 1315 | } |
| 1316 | |
| 1317 | N->dump(); |
| 1318 | errs() << "\n"; |
| 1319 | throw std::string("Unknown node in result pattern!"); |
| 1320 | } |
| 1321 | |
| 1322 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1323 | /// EmitCodeForPattern - Given a pattern to match, emit code to the specified |
| 1324 | /// stream to match the pattern, and generate the code for the match if it |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 1325 | /// succeeds. Returns true if the pattern is not guaranteed to match. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1326 | void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1327 | std::vector<std::pair<unsigned, std::string> > &GeneratedCode, |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1328 | std::set<std::string> &GeneratedDecl, |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 1329 | std::vector<std::string> &TargetOpcodes, |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1330 | std::vector<std::string> &TargetVTs, |
| 1331 | bool &OutputIsVariadic, |
| 1332 | unsigned &NumInputRootOps) { |
| 1333 | OutputIsVariadic = false; |
| 1334 | NumInputRootOps = 0; |
| 1335 | |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 1336 | PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(), |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1337 | Pattern.getSrcPattern(), Pattern.getDstPattern(), |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 1338 | GeneratedCode, GeneratedDecl, |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1339 | TargetOpcodes, TargetVTs, |
| 1340 | OutputIsVariadic, NumInputRootOps); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 1342 | // Emit the matcher, capturing named arguments in VariableMap. |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1343 | bool FoundChain = false; |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 1344 | Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1345 | |
Chris Lattner | c87bf38 | 2010-02-14 21:11:53 +0000 | [diff] [blame] | 1346 | // TP - Get *SOME* tree pattern, we don't care which. It is only used for |
| 1347 | // diagnostics, which we know are impossible at this point. |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1348 | TreePattern &TP = *CGP.pf_begin()->second; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1349 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1350 | // At this point, we know that we structurally match the pattern, but the |
| 1351 | // types of the nodes may not match. Figure out the fewest number of type |
| 1352 | // comparisons we need to emit. For example, if there is only one integer |
| 1353 | // type supported by a target, there should be no type comparisons at all for |
| 1354 | // integer patterns! |
| 1355 | // |
| 1356 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 1357 | // remove the types, then perform type inference on the pattern as a whole. |
| 1358 | // If there are unresolved types, emit an explicit check for those types, |
| 1359 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 1360 | // types are resolved. |
| 1361 | // |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1362 | TreePatternNode *Pat = Pattern.getSrcPattern()->clone(); |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 1363 | Pat->RemoveAllTypes(); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 1364 | |
| 1365 | do { |
| 1366 | // Resolve/propagate as many types as possible. |
| 1367 | try { |
| 1368 | bool MadeChange = true; |
| 1369 | while (MadeChange) |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 1370 | MadeChange = Pat->ApplyTypeConstraints(TP, |
| 1371 | true/*Ignore reg constraints*/); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 1372 | } catch (...) { |
| 1373 | assert(0 && "Error: could not find consistent types for something we" |
| 1374 | " already decided was ok!"); |
| 1375 | abort(); |
| 1376 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 1378 | // Insert a check for an unresolved type and add it to the tree. If we find |
| 1379 | // an unresolved type to add a check for, this returns true and we iterate, |
| 1380 | // otherwise we are done. |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1381 | } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true)); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1382 | |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1383 | Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(), |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1384 | false, false, false, true); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1385 | delete Pat; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1388 | /// EraseCodeLine - Erase one code line from all of the patterns. If removing |
| 1389 | /// a line causes any of them to be empty, remove them and return true when |
| 1390 | /// done. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1391 | static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1392 | std::vector<std::pair<unsigned, std::string> > > > |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1393 | &Patterns) { |
| 1394 | bool ErasedPatterns = false; |
| 1395 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 1396 | Patterns[i].second.pop_back(); |
| 1397 | if (Patterns[i].second.empty()) { |
| 1398 | Patterns.erase(Patterns.begin()+i); |
| 1399 | --i; --e; |
| 1400 | ErasedPatterns = true; |
| 1401 | } |
| 1402 | } |
| 1403 | return ErasedPatterns; |
| 1404 | } |
| 1405 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1406 | /// EmitPatterns - Emit code for at least one pattern, but try to group common |
| 1407 | /// code together between the patterns. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1408 | void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1409 | std::vector<std::pair<unsigned, std::string> > > > |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1410 | &Patterns, unsigned Indent, |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1411 | raw_ostream &OS) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1412 | typedef std::pair<unsigned, std::string> CodeLine; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1413 | typedef std::vector<CodeLine> CodeList; |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1414 | typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1415 | |
| 1416 | if (Patterns.empty()) return; |
| 1417 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1418 | // Figure out how many patterns share the next code line. Explicitly copy |
| 1419 | // FirstCodeLine so that we don't invalidate a reference when changing |
| 1420 | // Patterns. |
| 1421 | const CodeLine FirstCodeLine = Patterns.back().second.back(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1422 | unsigned LastMatch = Patterns.size()-1; |
| 1423 | while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine) |
| 1424 | --LastMatch; |
| 1425 | |
| 1426 | // If not all patterns share this line, split the list into two pieces. The |
| 1427 | // first chunk will use this line, the second chunk won't. |
| 1428 | if (LastMatch != 0) { |
| 1429 | PatternList Shared(Patterns.begin()+LastMatch, Patterns.end()); |
| 1430 | PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch); |
| 1431 | |
| 1432 | // FIXME: Emit braces? |
| 1433 | if (Shared.size() == 1) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1434 | const PatternToMatch &Pattern = *Shared.back().first; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1435 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 1436 | Pattern.getSrcPattern()->print(OS); |
| 1437 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 1438 | Pattern.getDstPattern()->print(OS); |
| 1439 | OS << "\n"; |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1440 | unsigned AddedComplexity = Pattern.getAddedComplexity(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1441 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1442 | << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1443 | << " cost = " |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1444 | << getResultPatternCost(Pattern.getDstPattern(), CGP) |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 1445 | << " size = " |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1446 | << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n"; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1447 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1448 | if (FirstCodeLine.first != 1) { |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1449 | OS << std::string(Indent, ' ') << "{\n"; |
| 1450 | Indent += 2; |
| 1451 | } |
| 1452 | EmitPatterns(Shared, Indent, OS); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1453 | if (FirstCodeLine.first != 1) { |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1454 | Indent -= 2; |
| 1455 | OS << std::string(Indent, ' ') << "}\n"; |
| 1456 | } |
| 1457 | |
| 1458 | if (Other.size() == 1) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1459 | const PatternToMatch &Pattern = *Other.back().first; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1460 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 1461 | Pattern.getSrcPattern()->print(OS); |
| 1462 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 1463 | Pattern.getDstPattern()->print(OS); |
| 1464 | OS << "\n"; |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1465 | unsigned AddedComplexity = Pattern.getAddedComplexity(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1466 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1467 | << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1468 | << " cost = " |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1469 | << getResultPatternCost(Pattern.getDstPattern(), CGP) |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1470 | << " size = " |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1471 | << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n"; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1472 | } |
| 1473 | EmitPatterns(Other, Indent, OS); |
| 1474 | return; |
| 1475 | } |
| 1476 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1477 | // Remove this code from all of the patterns that share it. |
| 1478 | bool ErasedPatterns = EraseCodeLine(Patterns); |
| 1479 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1480 | bool isPredicate = FirstCodeLine.first == 1; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Otherwise, every pattern in the list has this line. Emit it. |
| 1483 | if (!isPredicate) { |
| 1484 | // Normal code. |
| 1485 | OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n"; |
| 1486 | } else { |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1487 | OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second; |
| 1488 | |
| 1489 | // If the next code line is another predicate, and if all of the pattern |
| 1490 | // in this group share the same next line, emit it inline now. Do this |
| 1491 | // until we run out of common predicates. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1492 | while (!ErasedPatterns && Patterns.back().second.back().first == 1) { |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1493 | // Check that all of the patterns in Patterns end with the same predicate. |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1494 | bool AllEndWithSamePredicate = true; |
| 1495 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) |
| 1496 | if (Patterns[i].second.back() != Patterns.back().second.back()) { |
| 1497 | AllEndWithSamePredicate = false; |
| 1498 | break; |
| 1499 | } |
| 1500 | // If all of the predicates aren't the same, we can't share them. |
| 1501 | if (!AllEndWithSamePredicate) break; |
| 1502 | |
| 1503 | // Otherwise we can. Emit it shared now. |
| 1504 | OS << " &&\n" << std::string(Indent+4, ' ') |
| 1505 | << Patterns.back().second.back().second; |
| 1506 | ErasedPatterns = EraseCodeLine(Patterns); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1507 | } |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1508 | |
| 1509 | OS << ") {\n"; |
| 1510 | Indent += 2; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | EmitPatterns(Patterns, Indent, OS); |
| 1514 | |
| 1515 | if (isPredicate) |
| 1516 | OS << std::string(Indent-2, ' ') << "}\n"; |
| 1517 | } |
| 1518 | |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1519 | static std::string getLegalCName(std::string OpName) { |
| 1520 | std::string::size_type pos = OpName.find("::"); |
| 1521 | if (pos != std::string::npos) |
| 1522 | OpName.replace(pos, 2, "_"); |
| 1523 | return OpName; |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1526 | void DAGISelEmitter::EmitInstructionSelector(raw_ostream &OS) { |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1527 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1528 | |
Dan Gohman | 1e0ee4b | 2008-08-20 21:45:57 +0000 | [diff] [blame] | 1529 | // Get the namespace to insert instructions into. |
| 1530 | std::string InstNS = Target.getInstNamespace(); |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 1531 | if (!InstNS.empty()) InstNS += "::"; |
| 1532 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1533 | // Group the patterns by their top-level opcodes. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1534 | std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 1535 | // All unique target node emission functions. |
| 1536 | std::map<std::string, unsigned> EmitFunctions; |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1537 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1538 | E = CGP.ptm_end(); I != E; ++I) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1539 | const PatternToMatch &Pattern = *I; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1540 | TreePatternNode *Node = Pattern.getSrcPattern(); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1541 | if (!Node->isLeaf()) { |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1542 | PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)]. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1543 | push_back(&Pattern); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1544 | } else { |
| 1545 | const ComplexPattern *CP; |
Chris Lattner | 9c5d4de | 2006-11-03 01:11:05 +0000 | [diff] [blame] | 1546 | if (dynamic_cast<IntInit*>(Node->getLeafValue())) { |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1547 | PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)]. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1548 | push_back(&Pattern); |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 1549 | } else if ((CP = Node->getComplexPatternInfo(CGP))) { |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1550 | std::vector<Record*> OpNodes = CP->getRootNodes(); |
| 1551 | for (unsigned j = 0, e = OpNodes.size(); j != e; j++) { |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1552 | PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)] |
| 1553 | .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(), |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1554 | &Pattern); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } else { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1557 | errs() << "Unrecognized opcode '"; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1558 | Node->dump(); |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1559 | errs() << "' on tree pattern '"; |
| 1560 | errs() << Pattern.getDstPattern()->getOperator()->getName() << "'!\n"; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1561 | exit(1); |
| 1562 | } |
| 1563 | } |
| 1564 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1565 | |
| 1566 | // For each opcode, there might be multiple select functions, one per |
| 1567 | // ValueType of the node (or its first operand if it doesn't produce a |
| 1568 | // non-chain result. |
| 1569 | std::map<std::string, std::vector<std::string> > OpcodeVTMap; |
| 1570 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1571 | // Emit one Select_* method for each top-level opcode. We do this instead of |
| 1572 | // emitting one giant switch statement to support compilers where this will |
| 1573 | // result in the recursive functions taking less stack space. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1574 | for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1575 | PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end(); |
| 1576 | PBOI != E; ++PBOI) { |
| 1577 | const std::string &OpName = PBOI->first; |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1578 | std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1579 | assert(!PatternsOfOp.empty() && "No patterns but map has entry?"); |
| 1580 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1581 | // Split them into groups by type. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1582 | std::map<MVT::SimpleValueType, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1583 | std::vector<const PatternToMatch*> > PatternsByType; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1584 | for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1585 | const PatternToMatch *Pat = PatternsOfOp[i]; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1586 | TreePatternNode *SrcPat = Pat->getSrcPattern(); |
Chris Lattner | 9783d62 | 2008-08-26 07:01:28 +0000 | [diff] [blame] | 1587 | PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1590 | for (std::map<MVT::SimpleValueType, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1591 | std::vector<const PatternToMatch*> >::iterator |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1592 | II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE; |
| 1593 | ++II) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1594 | MVT::SimpleValueType OpVT = II->first; |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1595 | std::vector<const PatternToMatch*> &Patterns = II->second; |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 1596 | typedef std::pair<unsigned, std::string> CodeLine; |
| 1597 | typedef std::vector<CodeLine> CodeList; |
| 1598 | typedef CodeList::iterator CodeListI; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1600 | std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1601 | std::vector<std::vector<std::string> > PatternOpcodes; |
| 1602 | std::vector<std::vector<std::string> > PatternVTs; |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1603 | std::vector<std::set<std::string> > PatternDecls; |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1604 | std::vector<bool> OutputIsVariadicFlags; |
| 1605 | std::vector<unsigned> NumInputRootOpsCounts; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1606 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 1607 | CodeList GeneratedCode; |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1608 | std::set<std::string> GeneratedDecl; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1609 | std::vector<std::string> TargetOpcodes; |
| 1610 | std::vector<std::string> TargetVTs; |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1611 | bool OutputIsVariadic; |
| 1612 | unsigned NumInputRootOps; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1613 | GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl, |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1614 | TargetOpcodes, TargetVTs, |
| 1615 | OutputIsVariadic, NumInputRootOps); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1616 | CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode)); |
| 1617 | PatternDecls.push_back(GeneratedDecl); |
| 1618 | PatternOpcodes.push_back(TargetOpcodes); |
| 1619 | PatternVTs.push_back(TargetVTs); |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1620 | OutputIsVariadicFlags.push_back(OutputIsVariadic); |
| 1621 | NumInputRootOpsCounts.push_back(NumInputRootOps); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1624 | // Factor target node emission code (emitted by EmitResultCode) into |
| 1625 | // separate functions. Uniquing and share them among all instruction |
| 1626 | // selection routines. |
| 1627 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 1628 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 1629 | std::vector<std::string> &TargetOpcodes = PatternOpcodes[i]; |
| 1630 | std::vector<std::string> &TargetVTs = PatternVTs[i]; |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1631 | std::set<std::string> Decls = PatternDecls[i]; |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1632 | bool OutputIsVariadic = OutputIsVariadicFlags[i]; |
| 1633 | unsigned NumInputRootOps = NumInputRootOpsCounts[i]; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1634 | std::vector<std::string> AddedInits; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1635 | int CodeSize = (int)GeneratedCode.size(); |
| 1636 | int LastPred = -1; |
| 1637 | for (int j = CodeSize-1; j >= 0; --j) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1638 | if (LastPred == -1 && GeneratedCode[j].first == 1) |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1639 | LastPred = j; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1640 | else if (LastPred != -1 && GeneratedCode[j].first == 2) |
| 1641 | AddedInits.push_back(GeneratedCode[j].second); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1644 | std::string CalleeCode = "(SDNode *N"; |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1645 | std::string CallerCode = "(N"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1646 | for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) { |
| 1647 | CalleeCode += ", unsigned Opc" + utostr(j); |
| 1648 | CallerCode += ", " + TargetOpcodes[j]; |
| 1649 | } |
| 1650 | for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) { |
Owen Anderson | 69110c9 | 2009-09-11 09:01:57 +0000 | [diff] [blame] | 1651 | CalleeCode += ", MVT::SimpleValueType VT" + utostr(j); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1652 | CallerCode += ", " + TargetVTs[j]; |
| 1653 | } |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1654 | for (std::set<std::string>::iterator |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1655 | I = Decls.begin(), E = Decls.end(); I != E; ++I) { |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1656 | std::string Name = *I; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1657 | CalleeCode += ", SDValue &" + Name; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1658 | CallerCode += ", " + Name; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1659 | } |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (OutputIsVariadic) { |
| 1662 | CalleeCode += ", unsigned NumInputRootOps"; |
| 1663 | CallerCode += ", " + utostr(NumInputRootOps); |
| 1664 | } |
| 1665 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1666 | CallerCode += ");"; |
Benjamin Kramer | f2a39bd | 2009-11-14 16:37:18 +0000 | [diff] [blame] | 1667 | CalleeCode += ") {\n"; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1668 | |
| 1669 | for (std::vector<std::string>::const_reverse_iterator |
| 1670 | I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I) |
| 1671 | CalleeCode += " " + *I + "\n"; |
| 1672 | |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1673 | for (int j = LastPred+1; j < CodeSize; ++j) |
| 1674 | CalleeCode += " " + GeneratedCode[j].second + "\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1675 | for (int j = LastPred+1; j < CodeSize; ++j) |
| 1676 | GeneratedCode.pop_back(); |
| 1677 | CalleeCode += "}\n"; |
| 1678 | |
| 1679 | // Uniquing the emission routines. |
| 1680 | unsigned EmitFuncNum; |
| 1681 | std::map<std::string, unsigned>::iterator EFI = |
| 1682 | EmitFunctions.find(CalleeCode); |
| 1683 | if (EFI != EmitFunctions.end()) { |
| 1684 | EmitFuncNum = EFI->second; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1685 | } else { |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1686 | EmitFuncNum = EmitFunctions.size(); |
| 1687 | EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum)); |
Benjamin Kramer | f2a39bd | 2009-11-14 16:37:18 +0000 | [diff] [blame] | 1688 | // Prevent emission routines from being inlined to reduce selection |
| 1689 | // routines stack frame sizes. |
| 1690 | OS << "DISABLE_INLINE "; |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1691 | OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1692 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1693 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1694 | // Replace the emission code within selection routines with calls to the |
| 1695 | // emission functions. |
Chris Lattner | a0cdf17 | 2010-02-13 20:06:50 +0000 | [diff] [blame] | 1696 | if (GenDebug) |
Chris Lattner | dcdcef2 | 2010-02-18 00:23:27 +0000 | [diff] [blame] | 1697 | GeneratedCode.push_back(std::make_pair(0, |
| 1698 | "CurDAG->setSubgraphColor(N, \"red\");")); |
| 1699 | CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) +CallerCode; |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 1700 | GeneratedCode.push_back(std::make_pair(3, CallerCode)); |
| 1701 | if (GenDebug) { |
| 1702 | GeneratedCode.push_back(std::make_pair(0, "if(Result) {")); |
Chris Lattner | dcdcef2 | 2010-02-18 00:23:27 +0000 | [diff] [blame] | 1703 | GeneratedCode.push_back(std::make_pair(0, |
| 1704 | " CurDAG->setSubgraphColor(Result, \"yellow\");")); |
| 1705 | GeneratedCode.push_back(std::make_pair(0, |
| 1706 | " CurDAG->setSubgraphColor(Result, \"black\");")); |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 1707 | GeneratedCode.push_back(std::make_pair(0, "}")); |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 1708 | } |
| 1709 | GeneratedCode.push_back(std::make_pair(0, "return Result;")); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1712 | // Print function. |
Chris Lattner | ab51ddd | 2006-11-14 21:32:01 +0000 | [diff] [blame] | 1713 | std::string OpVTStr; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1714 | if (OpVT == MVT::iPTR) { |
Chris Lattner | 33a4004 | 2006-11-14 22:17:10 +0000 | [diff] [blame] | 1715 | OpVTStr = "_iPTR"; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1716 | } else if (OpVT == MVT::iPTRAny) { |
Mon P Wang | e3b3a72 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 1717 | OpVTStr = "_iPTRAny"; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1718 | } else if (OpVT == MVT::isVoid) { |
Chris Lattner | 33a4004 | 2006-11-14 22:17:10 +0000 | [diff] [blame] | 1719 | // Nodes with a void result actually have a first result type of either |
| 1720 | // Other (a chain) or Flag. Since there is no one-to-one mapping from |
| 1721 | // void to this case, we handle it specially here. |
| 1722 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1723 | OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::' |
Chris Lattner | 33a4004 | 2006-11-14 22:17:10 +0000 | [diff] [blame] | 1724 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1725 | std::map<std::string, std::vector<std::string> >::iterator OpVTI = |
| 1726 | OpcodeVTMap.find(OpName); |
| 1727 | if (OpVTI == OpcodeVTMap.end()) { |
| 1728 | std::vector<std::string> VTSet; |
| 1729 | VTSet.push_back(OpVTStr); |
| 1730 | OpcodeVTMap.insert(std::make_pair(OpName, VTSet)); |
| 1731 | } else |
| 1732 | OpVTI->second.push_back(OpVTStr); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1733 | |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 1734 | // We want to emit all of the matching code now. However, we want to emit |
| 1735 | // the matches in order of minimal cost. Sort the patterns so the least |
| 1736 | // cost one is at the start. |
| 1737 | std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(), |
| 1738 | PatternSortingPredicate(CGP)); |
| 1739 | |
| 1740 | // Scan the code to see if all of the patterns are reachable and if it is |
| 1741 | // possible that the last one might not match. |
| 1742 | bool mightNotMatch = true; |
| 1743 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 1744 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 1745 | mightNotMatch = false; |
| 1746 | |
| 1747 | for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) { |
| 1748 | if (GeneratedCode[j].first == 1) { // predicate. |
| 1749 | mightNotMatch = true; |
| 1750 | break; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | // If this pattern definitely matches, and if it isn't the last one, the |
| 1755 | // patterns after it CANNOT ever match. Error out. |
| 1756 | if (mightNotMatch == false && i != CodeForPatterns.size()-1) { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1757 | errs() << "Pattern '"; |
| 1758 | CodeForPatterns[i].first->getSrcPattern()->print(errs()); |
| 1759 | errs() << "' is impossible to select!\n"; |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 1760 | exit(1); |
| 1761 | } |
| 1762 | } |
| 1763 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1764 | // Loop through and reverse all of the CodeList vectors, as we will be |
| 1765 | // accessing them from their logical front, but accessing the end of a |
| 1766 | // vector is more efficient. |
| 1767 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 1768 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 1769 | std::reverse(GeneratedCode.begin(), GeneratedCode.end()); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1770 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Next, reverse the list of patterns itself for the same reason. |
| 1773 | std::reverse(CodeForPatterns.begin(), CodeForPatterns.end()); |
| 1774 | |
Dan Gohman | 63e3e63 | 2009-01-29 01:37:18 +0000 | [diff] [blame] | 1775 | OS << "SDNode *Select_" << getLegalCName(OpName) |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1776 | << OpVTStr << "(SDNode *N) {\n"; |
Dan Gohman | 63e3e63 | 2009-01-29 01:37:18 +0000 | [diff] [blame] | 1777 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1778 | // Emit all of the patterns now, grouped together to share code. |
| 1779 | EmitPatterns(CodeForPatterns, 2, OS); |
| 1780 | |
Chris Lattner | 6490697 | 2006-09-21 18:28:27 +0000 | [diff] [blame] | 1781 | // If the last pattern has predicates (which could fail) emit code to |
| 1782 | // catch the case where nothing handles a pattern. |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1783 | if (mightNotMatch) { |
Dan Gohman | 31bd42b | 2008-09-27 23:53:14 +0000 | [diff] [blame] | 1784 | OS << "\n"; |
Chris Lattner | 409ac58 | 2010-02-17 06:28:22 +0000 | [diff] [blame] | 1785 | OS << " CannotYetSelect(N);\n"; |
Dan Gohman | 31bd42b | 2008-09-27 23:53:14 +0000 | [diff] [blame] | 1786 | OS << " return NULL;\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1787 | } |
| 1788 | OS << "}\n\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1789 | } |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1792 | OS << "// The main instruction selector code.\n" |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1793 | << "SDNode *SelectCode(SDNode *N) {\n" |
| 1794 | << " MVT::SimpleValueType NVT = N->getValueType(0).getSimpleVT().SimpleTy;\n" |
| 1795 | << " switch (N->getOpcode()) {\n" |
Dan Gohman | 28c04da | 2008-11-05 18:30:52 +0000 | [diff] [blame] | 1796 | << " default:\n" |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1797 | << " assert(!N->isMachineOpcode() && \"Node already selected!\");\n" |
Dan Gohman | 28c04da | 2008-11-05 18:30:52 +0000 | [diff] [blame] | 1798 | << " break;\n" |
| 1799 | << " case ISD::EntryToken: // These nodes remain the same.\n" |
Chris Lattner | 5216c69 | 2005-12-18 21:05:44 +0000 | [diff] [blame] | 1800 | << " case ISD::BasicBlock:\n" |
Chris Lattner | 8020a52 | 2006-01-11 19:52:27 +0000 | [diff] [blame] | 1801 | << " case ISD::Register:\n" |
Evan Cheng | 0a83ed5 | 2006-02-05 08:46:14 +0000 | [diff] [blame] | 1802 | << " case ISD::HANDLENODE:\n" |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame] | 1803 | << " case ISD::TargetConstant:\n" |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1804 | << " case ISD::TargetConstantFP:\n" |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame] | 1805 | << " case ISD::TargetConstantPool:\n" |
| 1806 | << " case ISD::TargetFrameIndex:\n" |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 1807 | << " case ISD::TargetExternalSymbol:\n" |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1808 | << " case ISD::TargetBlockAddress:\n" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1809 | << " case ISD::TargetJumpTable:\n" |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 1810 | << " case ISD::TargetGlobalTLSAddress:\n" |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1811 | << " case ISD::TargetGlobalAddress:\n" |
| 1812 | << " case ISD::TokenFactor:\n" |
| 1813 | << " case ISD::CopyFromReg:\n" |
| 1814 | << " case ISD::CopyToReg: {\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1815 | << " return NULL;\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1816 | << " }\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1817 | << " case ISD::AssertSext:\n" |
Chris Lattner | fab3728 | 2005-09-26 22:10:24 +0000 | [diff] [blame] | 1818 | << " case ISD::AssertZext: {\n" |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1819 | << " ReplaceUses(SDValue(N, 0), N->getOperand(0));\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1820 | << " return NULL;\n" |
Chris Lattner | f071bb5 | 2005-10-25 20:35:14 +0000 | [diff] [blame] | 1821 | << " }\n" |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 1822 | << " case ISD::INLINEASM: return Select_INLINEASM(N);\n" |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 1823 | << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n" |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1824 | << " case ISD::UNDEF: return Select_UNDEF(N);\n"; |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1826 | // Loop over all of the case statements, emiting a call to each method we |
| 1827 | // emitted above. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame] | 1828 | for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1829 | PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end(); |
| 1830 | PBOI != E; ++PBOI) { |
| 1831 | const std::string &OpName = PBOI->first; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1832 | // Potentially multiple versions of select for this opcode. One for each |
| 1833 | // ValueType of the node (or its first true operand if it doesn't produce a |
| 1834 | // result. |
| 1835 | std::map<std::string, std::vector<std::string> >::iterator OpVTI = |
| 1836 | OpcodeVTMap.find(OpName); |
| 1837 | std::vector<std::string> &OpVTs = OpVTI->second; |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1838 | OS << " case " << OpName << ": {\n"; |
Dale Johannesen | 3b895cf | 2009-05-12 22:32:29 +0000 | [diff] [blame] | 1839 | // If we have only one variant and it's the default, elide the |
| 1840 | // switch. Marginally faster, and makes MSVC happier. |
| 1841 | if (OpVTs.size()==1 && OpVTs[0].empty()) { |
| 1842 | OS << " return Select_" << getLegalCName(OpName) << "(N);\n"; |
| 1843 | OS << " break;\n"; |
| 1844 | OS << " }\n"; |
| 1845 | continue; |
| 1846 | } |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1847 | // Keep track of whether we see a pattern that has an iPtr result. |
| 1848 | bool HasPtrPattern = false; |
| 1849 | bool HasDefaultPattern = false; |
Chris Lattner | 717a611 | 2006-11-14 21:50:27 +0000 | [diff] [blame] | 1850 | |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1851 | OS << " switch (NVT) {\n"; |
| 1852 | for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) { |
| 1853 | std::string &VTStr = OpVTs[i]; |
| 1854 | if (VTStr.empty()) { |
| 1855 | HasDefaultPattern = true; |
| 1856 | continue; |
| 1857 | } |
Chris Lattner | 717a611 | 2006-11-14 21:50:27 +0000 | [diff] [blame] | 1858 | |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1859 | // If this is a match on iPTR: don't emit it directly, we need special |
| 1860 | // code. |
| 1861 | if (VTStr == "_iPTR") { |
| 1862 | HasPtrPattern = true; |
| 1863 | continue; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1864 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1865 | OS << " case MVT::" << VTStr.substr(1) << ":\n" |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1866 | << " return Select_" << getLegalCName(OpName) |
| 1867 | << VTStr << "(N);\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1868 | } |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1869 | OS << " default:\n"; |
| 1870 | |
| 1871 | // If there is an iPTR result version of this pattern, emit it here. |
| 1872 | if (HasPtrPattern) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1873 | OS << " if (TLI.getPointerTy() == NVT)\n"; |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1874 | OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n"; |
| 1875 | } |
| 1876 | if (HasDefaultPattern) { |
| 1877 | OS << " return Select_" << getLegalCName(OpName) << "(N);\n"; |
| 1878 | } |
| 1879 | OS << " break;\n"; |
| 1880 | OS << " }\n"; |
| 1881 | OS << " break;\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1882 | OS << " }\n"; |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1883 | } |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1885 | OS << " } // end of big switch.\n\n" |
Chris Lattner | 409ac58 | 2010-02-17 06:28:22 +0000 | [diff] [blame] | 1886 | << " CannotYetSelect(N);\n" |
Dan Gohman | 31bd42b | 2008-09-27 23:53:14 +0000 | [diff] [blame] | 1887 | << " return NULL;\n" |
| 1888 | << "}\n\n"; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 1891 | namespace { |
| 1892 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 1893 | // In particular, we want to match maximal patterns first and lowest cost within |
| 1894 | // a particular complexity first. |
| 1895 | struct PatternSortingPredicate2 { |
| 1896 | PatternSortingPredicate2(CodeGenDAGPatterns &cgp) : CGP(cgp) {} |
| 1897 | CodeGenDAGPatterns &CGP; |
| 1898 | |
| 1899 | bool operator()(const PatternToMatch *LHS, |
| 1900 | const PatternToMatch *RHS) { |
| 1901 | unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP); |
| 1902 | unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP); |
| 1903 | LHSSize += LHS->getAddedComplexity(); |
| 1904 | RHSSize += RHS->getAddedComplexity(); |
| 1905 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 1906 | if (LHSSize < RHSSize) return false; |
| 1907 | |
| 1908 | // If the patterns have equal complexity, compare generated instruction cost |
| 1909 | unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP); |
| 1910 | unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP); |
| 1911 | if (LHSCost < RHSCost) return true; |
| 1912 | if (LHSCost > RHSCost) return false; |
| 1913 | |
| 1914 | return getResultPatternSize(LHS->getDstPattern(), CGP) < |
| 1915 | getResultPatternSize(RHS->getDstPattern(), CGP); |
| 1916 | } |
| 1917 | }; |
| 1918 | } |
| 1919 | |
| 1920 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1921 | void DAGISelEmitter::run(raw_ostream &OS) { |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 1922 | EmitSourceFileHeader("DAG Instruction Selector for the " + |
| 1923 | CGP.getTargetInfo().getName() + " target", OS); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1925 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 1926 | << "// *** instruction selector class. These functions are really " |
| 1927 | << "methods.\n\n"; |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 1928 | |
Roman Levenstein | 6422e8a | 2008-05-14 10:17:11 +0000 | [diff] [blame] | 1929 | OS << "// Include standard, target-independent definitions and methods used\n" |
| 1930 | << "// by the instruction selector.\n"; |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 1931 | OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n"; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1932 | |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 1933 | EmitNodeTransforms(OS); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1934 | EmitPredicateFunctions(OS); |
| 1935 | |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 1936 | DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n"); |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1937 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1938 | I != E; ++I) { |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 1939 | DEBUG(errs() << "PATTERN: "; I->getSrcPattern()->dump()); |
| 1940 | DEBUG(errs() << "\nRESULT: "; I->getDstPattern()->dump()); |
| 1941 | DEBUG(errs() << "\n"); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1942 | } |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1943 | |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1944 | // At this point, we have full information about the 'Patterns' we need to |
| 1945 | // parse, both implicitly from instructions as well as from explicit pattern |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1946 | // definitions. Emit the resultant instruction selector. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1947 | EmitInstructionSelector(OS); |
| 1948 | |
Chris Lattner | 0ab1c5f | 2010-02-21 06:03:56 +0000 | [diff] [blame] | 1949 | #if 0 |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1950 | MatcherNode *Matcher = 0; |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 1951 | |
| 1952 | // Add all the patterns to a temporary list so we can sort them. |
| 1953 | std::vector<const PatternToMatch*> Patterns; |
| 1954 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end(); |
| 1955 | I != E; ++I) |
| 1956 | Patterns.push_back(&*I); |
| 1957 | |
| 1958 | // We want to process the matches in order of minimal cost. Sort the patterns |
| 1959 | // so the least cost one is at the start. |
| 1960 | // FIXME: Eliminate "PatternSortingPredicate" and rename. |
| 1961 | std::stable_sort(Patterns.begin(), Patterns.end(), |
| 1962 | PatternSortingPredicate2(CGP)); |
| 1963 | |
| 1964 | |
| 1965 | // Walk the patterns backwards (since we append to the front of the generated |
| 1966 | // code), building a matcher for each and adding it to the matcher for the |
| 1967 | // whole target. |
| 1968 | while (!Patterns.empty()) { |
| 1969 | const PatternToMatch &Pattern = *Patterns.back(); |
| 1970 | Patterns.pop_back(); |
| 1971 | |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1972 | MatcherNode *N = ConvertPatternToMatcher(Pattern, CGP); |
| 1973 | |
| 1974 | if (Matcher == 0) |
| 1975 | Matcher = N; |
| 1976 | else |
| 1977 | Matcher = new PushMatcherNode(N, Matcher); |
| 1978 | } |
Chris Lattner | 05446e7 | 2010-02-16 23:13:59 +0000 | [diff] [blame] | 1979 | |
| 1980 | // OptimizeMatcher(Matcher); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1981 | //Matcher->dump(); |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 1982 | EmitMatcherTable(Matcher, OS); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 1983 | delete Matcher; |
| 1984 | #endif |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1985 | } |