blob: b74144ebfdc801288743d1fe27696391d04eca2e [file] [log] [blame]
Chris Lattnerfe718932008-01-06 01:10:31 +00001//===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
Chris Lattner6cefb772008-01-05 22:25:12 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfe718932008-01-06 01:10:31 +000010// This file implements the CodeGenDAGPatterns class, which is used to read and
Chris Lattner6cefb772008-01-05 22:25:12 +000011// represent the patterns present in a .td file for instructions.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner93c7e412008-01-05 23:37:52 +000015#include "CodeGenDAGPatterns.h"
Chris Lattner6cefb772008-01-05 22:25:12 +000016#include "Record.h"
17#include "llvm/ADT/StringExtras.h"
Chris Lattner2cacec52010-03-15 06:00:16 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattner6cefb772008-01-05 22:25:12 +000019#include "llvm/Support/Debug.h"
Chris Lattner6cefb772008-01-05 22:25:12 +000020#include <set>
Chuck Rose III9a79de32008-01-15 21:43:17 +000021#include <algorithm>
Chris Lattner6cefb772008-01-05 22:25:12 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
Chris Lattner2cacec52010-03-15 06:00:16 +000025// EEVT::TypeSet Implementation
26//===----------------------------------------------------------------------===//
Chris Lattner6cefb772008-01-05 22:25:12 +000027
Owen Anderson825b72b2009-08-11 20:47:22 +000028static inline bool isInteger(MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +000029 return EVT(VT).isInteger();
Duncan Sands83ec4b62008-06-06 12:08:01 +000030}
Owen Anderson825b72b2009-08-11 20:47:22 +000031static inline bool isFloatingPoint(MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +000032 return EVT(VT).isFloatingPoint();
Duncan Sands83ec4b62008-06-06 12:08:01 +000033}
Owen Anderson825b72b2009-08-11 20:47:22 +000034static inline bool isVector(MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +000035 return EVT(VT).isVector();
Duncan Sands83ec4b62008-06-06 12:08:01 +000036}
Chris Lattner774ce292010-03-19 17:41:26 +000037static inline bool isScalar(MVT::SimpleValueType VT) {
38 return !EVT(VT).isVector();
39}
Duncan Sands83ec4b62008-06-06 12:08:01 +000040
Chris Lattner2cacec52010-03-15 06:00:16 +000041EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) {
42 if (VT == MVT::iAny)
43 EnforceInteger(TP);
44 else if (VT == MVT::fAny)
45 EnforceFloatingPoint(TP);
46 else if (VT == MVT::vAny)
47 EnforceVector(TP);
48 else {
49 assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR ||
50 VT == MVT::iPTRAny) && "Not a concrete type!");
51 TypeVec.push_back(VT);
52 }
Chris Lattner6cefb772008-01-05 22:25:12 +000053}
54
Chris Lattner2cacec52010-03-15 06:00:16 +000055
56EEVT::TypeSet::TypeSet(const std::vector<MVT::SimpleValueType> &VTList) {
57 assert(!VTList.empty() && "empty list?");
58 TypeVec.append(VTList.begin(), VTList.end());
Jim Grosbachfbadcd02010-12-21 16:16:00 +000059
Chris Lattner2cacec52010-03-15 06:00:16 +000060 if (!VTList.empty())
61 assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny &&
62 VTList[0] != MVT::fAny);
Jim Grosbachfbadcd02010-12-21 16:16:00 +000063
Chris Lattner0d7952e2010-03-27 20:32:26 +000064 // Verify no duplicates.
Chris Lattner2cacec52010-03-15 06:00:16 +000065 array_pod_sort(TypeVec.begin(), TypeVec.end());
Chris Lattner0d7952e2010-03-27 20:32:26 +000066 assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end());
Chris Lattner6cefb772008-01-05 22:25:12 +000067}
68
Chris Lattner5a9b8fb2010-03-19 04:54:36 +000069/// FillWithPossibleTypes - Set to all legal types and return true, only valid
70/// on completely unknown type sets.
Chris Lattner774ce292010-03-19 17:41:26 +000071bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP,
72 bool (*Pred)(MVT::SimpleValueType),
73 const char *PredicateName) {
Chris Lattner5a9b8fb2010-03-19 04:54:36 +000074 assert(isCompletelyUnknown());
Jim Grosbachfbadcd02010-12-21 16:16:00 +000075 const std::vector<MVT::SimpleValueType> &LegalTypes =
Chris Lattner774ce292010-03-19 17:41:26 +000076 TP.getDAGPatterns().getTargetInfo().getLegalValueTypes();
Jim Grosbachfbadcd02010-12-21 16:16:00 +000077
Chris Lattner774ce292010-03-19 17:41:26 +000078 for (unsigned i = 0, e = LegalTypes.size(); i != e; ++i)
79 if (Pred == 0 || Pred(LegalTypes[i]))
80 TypeVec.push_back(LegalTypes[i]);
81
82 // If we have nothing that matches the predicate, bail out.
83 if (TypeVec.empty())
84 TP.error("Type inference contradiction found, no " +
Jim Grosbachfbadcd02010-12-21 16:16:00 +000085 std::string(PredicateName) + " types found");
Chris Lattner774ce292010-03-19 17:41:26 +000086 // No need to sort with one element.
87 if (TypeVec.size() == 1) return true;
88
89 // Remove duplicates.
90 array_pod_sort(TypeVec.begin(), TypeVec.end());
91 TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end());
Jim Grosbachfbadcd02010-12-21 16:16:00 +000092
Chris Lattner5a9b8fb2010-03-19 04:54:36 +000093 return true;
94}
Chris Lattner2cacec52010-03-15 06:00:16 +000095
96/// hasIntegerTypes - Return true if this TypeSet contains iAny or an
97/// integer value type.
98bool EEVT::TypeSet::hasIntegerTypes() const {
99 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
100 if (isInteger(TypeVec[i]))
101 return true;
102 return false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000103}
Chris Lattner2cacec52010-03-15 06:00:16 +0000104
105/// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
106/// a floating point value type.
107bool EEVT::TypeSet::hasFloatingPointTypes() const {
108 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
109 if (isFloatingPoint(TypeVec[i]))
110 return true;
111 return false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000112}
Chris Lattner2cacec52010-03-15 06:00:16 +0000113
114/// hasVectorTypes - Return true if this TypeSet contains a vAny or a vector
115/// value type.
116bool EEVT::TypeSet::hasVectorTypes() const {
117 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
118 if (isVector(TypeVec[i]))
119 return true;
120 return false;
Chris Lattner6cefb772008-01-05 22:25:12 +0000121}
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000122
Chris Lattner2cacec52010-03-15 06:00:16 +0000123
124std::string EEVT::TypeSet::getName() const {
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000125 if (TypeVec.empty()) return "<empty>";
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000126
Chris Lattner2cacec52010-03-15 06:00:16 +0000127 std::string Result;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000128
Chris Lattner2cacec52010-03-15 06:00:16 +0000129 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) {
130 std::string VTName = llvm::getEnumName(TypeVec[i]);
131 // Strip off MVT:: prefix if present.
132 if (VTName.substr(0,5) == "MVT::")
133 VTName = VTName.substr(5);
134 if (i) Result += ':';
135 Result += VTName;
136 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000137
Chris Lattner2cacec52010-03-15 06:00:16 +0000138 if (TypeVec.size() == 1)
139 return Result;
140 return "{" + Result + "}";
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000141}
Chris Lattner2cacec52010-03-15 06:00:16 +0000142
143/// MergeInTypeInfo - This merges in type information from the specified
144/// argument. If 'this' changes, it returns true. If the two types are
145/// contradictory (e.g. merge f32 into i32) then this throws an exception.
146bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){
147 if (InVT.isCompletelyUnknown() || *this == InVT)
148 return false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000149
Chris Lattner2cacec52010-03-15 06:00:16 +0000150 if (isCompletelyUnknown()) {
151 *this = InVT;
152 return true;
153 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000154
Chris Lattner2cacec52010-03-15 06:00:16 +0000155 assert(TypeVec.size() >= 1 && InVT.TypeVec.size() >= 1 && "No unknowns");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000156
Chris Lattner2cacec52010-03-15 06:00:16 +0000157 // Handle the abstract cases, seeing if we can resolve them better.
158 switch (TypeVec[0]) {
159 default: break;
160 case MVT::iPTR:
161 case MVT::iPTRAny:
162 if (InVT.hasIntegerTypes()) {
163 EEVT::TypeSet InCopy(InVT);
164 InCopy.EnforceInteger(TP);
165 InCopy.EnforceScalar(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000166
Chris Lattner2cacec52010-03-15 06:00:16 +0000167 if (InCopy.isConcrete()) {
168 // If the RHS has one integer type, upgrade iPTR to i32.
169 TypeVec[0] = InVT.TypeVec[0];
170 return true;
171 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000172
Chris Lattner2cacec52010-03-15 06:00:16 +0000173 // If the input has multiple scalar integers, this doesn't add any info.
174 if (!InCopy.isCompletelyUnknown())
175 return false;
176 }
177 break;
178 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000179
Chris Lattner2cacec52010-03-15 06:00:16 +0000180 // If the input constraint is iAny/iPTR and this is an integer type list,
181 // remove non-integer types from the list.
182 if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) &&
183 hasIntegerTypes()) {
184 bool MadeChange = EnforceInteger(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000185
Chris Lattner2cacec52010-03-15 06:00:16 +0000186 // If we're merging in iPTR/iPTRAny and the node currently has a list of
187 // multiple different integer types, replace them with a single iPTR.
188 if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) &&
189 TypeVec.size() != 1) {
190 TypeVec.resize(1);
191 TypeVec[0] = InVT.TypeVec[0];
192 MadeChange = true;
193 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000194
Chris Lattner2cacec52010-03-15 06:00:16 +0000195 return MadeChange;
196 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000197
Chris Lattner2cacec52010-03-15 06:00:16 +0000198 // If this is a type list and the RHS is a typelist as well, eliminate entries
199 // from this list that aren't in the other one.
200 bool MadeChange = false;
201 TypeSet InputSet(*this);
202
203 for (unsigned i = 0; i != TypeVec.size(); ++i) {
204 bool InInVT = false;
205 for (unsigned j = 0, e = InVT.TypeVec.size(); j != e; ++j)
206 if (TypeVec[i] == InVT.TypeVec[j]) {
207 InInVT = true;
208 break;
209 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000210
Chris Lattner2cacec52010-03-15 06:00:16 +0000211 if (InInVT) continue;
212 TypeVec.erase(TypeVec.begin()+i--);
213 MadeChange = true;
214 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000215
Chris Lattner2cacec52010-03-15 06:00:16 +0000216 // If we removed all of our types, we have a type contradiction.
217 if (!TypeVec.empty())
218 return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000219
Chris Lattner2cacec52010-03-15 06:00:16 +0000220 // FIXME: Really want an SMLoc here!
221 TP.error("Type inference contradiction found, merging '" +
222 InVT.getName() + "' into '" + InputSet.getName() + "'");
223 return true; // unreachable
224}
225
226/// EnforceInteger - Remove all non-integer types from this set.
227bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) {
Chris Lattner2cacec52010-03-15 06:00:16 +0000228 // If we know nothing, then get the full set.
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000229 if (TypeVec.empty())
Chris Lattner774ce292010-03-19 17:41:26 +0000230 return FillWithPossibleTypes(TP, isInteger, "integer");
Chris Lattner2cacec52010-03-15 06:00:16 +0000231 if (!hasFloatingPointTypes())
Chris Lattner774ce292010-03-19 17:41:26 +0000232 return false;
233
234 TypeSet InputSet(*this);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000235
Chris Lattner2cacec52010-03-15 06:00:16 +0000236 // Filter out all the fp types.
237 for (unsigned i = 0; i != TypeVec.size(); ++i)
Chris Lattner774ce292010-03-19 17:41:26 +0000238 if (!isInteger(TypeVec[i]))
Chris Lattner2cacec52010-03-15 06:00:16 +0000239 TypeVec.erase(TypeVec.begin()+i--);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000240
Chris Lattner2cacec52010-03-15 06:00:16 +0000241 if (TypeVec.empty())
242 TP.error("Type inference contradiction found, '" +
243 InputSet.getName() + "' needs to be integer");
Chris Lattner774ce292010-03-19 17:41:26 +0000244 return true;
Chris Lattner2cacec52010-03-15 06:00:16 +0000245}
246
247/// EnforceFloatingPoint - Remove all integer types from this set.
248bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) {
Chris Lattner2cacec52010-03-15 06:00:16 +0000249 // If we know nothing, then get the full set.
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000250 if (TypeVec.empty())
Chris Lattner774ce292010-03-19 17:41:26 +0000251 return FillWithPossibleTypes(TP, isFloatingPoint, "floating point");
252
Chris Lattner2cacec52010-03-15 06:00:16 +0000253 if (!hasIntegerTypes())
Chris Lattner774ce292010-03-19 17:41:26 +0000254 return false;
255
256 TypeSet InputSet(*this);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000257
Chris Lattner2cacec52010-03-15 06:00:16 +0000258 // Filter out all the fp types.
259 for (unsigned i = 0; i != TypeVec.size(); ++i)
Chris Lattner774ce292010-03-19 17:41:26 +0000260 if (!isFloatingPoint(TypeVec[i]))
Chris Lattner2cacec52010-03-15 06:00:16 +0000261 TypeVec.erase(TypeVec.begin()+i--);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000262
Chris Lattner2cacec52010-03-15 06:00:16 +0000263 if (TypeVec.empty())
264 TP.error("Type inference contradiction found, '" +
265 InputSet.getName() + "' needs to be floating point");
Chris Lattner774ce292010-03-19 17:41:26 +0000266 return true;
Chris Lattner2cacec52010-03-15 06:00:16 +0000267}
268
269/// EnforceScalar - Remove all vector types from this.
270bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) {
Chris Lattner2cacec52010-03-15 06:00:16 +0000271 // If we know nothing, then get the full set.
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000272 if (TypeVec.empty())
Chris Lattner774ce292010-03-19 17:41:26 +0000273 return FillWithPossibleTypes(TP, isScalar, "scalar");
274
Chris Lattner2cacec52010-03-15 06:00:16 +0000275 if (!hasVectorTypes())
Chris Lattner774ce292010-03-19 17:41:26 +0000276 return false;
277
278 TypeSet InputSet(*this);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000279
Chris Lattner2cacec52010-03-15 06:00:16 +0000280 // Filter out all the vector types.
281 for (unsigned i = 0; i != TypeVec.size(); ++i)
Chris Lattner774ce292010-03-19 17:41:26 +0000282 if (!isScalar(TypeVec[i]))
Chris Lattner2cacec52010-03-15 06:00:16 +0000283 TypeVec.erase(TypeVec.begin()+i--);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000284
Chris Lattner2cacec52010-03-15 06:00:16 +0000285 if (TypeVec.empty())
286 TP.error("Type inference contradiction found, '" +
287 InputSet.getName() + "' needs to be scalar");
Chris Lattner774ce292010-03-19 17:41:26 +0000288 return true;
Chris Lattner2cacec52010-03-15 06:00:16 +0000289}
290
291/// EnforceVector - Remove all vector types from this.
292bool EEVT::TypeSet::EnforceVector(TreePattern &TP) {
Chris Lattner774ce292010-03-19 17:41:26 +0000293 // If we know nothing, then get the full set.
294 if (TypeVec.empty())
295 return FillWithPossibleTypes(TP, isVector, "vector");
296
Chris Lattner2cacec52010-03-15 06:00:16 +0000297 TypeSet InputSet(*this);
298 bool MadeChange = false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000299
Chris Lattner2cacec52010-03-15 06:00:16 +0000300 // Filter out all the scalar types.
301 for (unsigned i = 0; i != TypeVec.size(); ++i)
Chris Lattner774ce292010-03-19 17:41:26 +0000302 if (!isVector(TypeVec[i])) {
Chris Lattner2cacec52010-03-15 06:00:16 +0000303 TypeVec.erase(TypeVec.begin()+i--);
Chris Lattner774ce292010-03-19 17:41:26 +0000304 MadeChange = true;
305 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000306
Chris Lattner2cacec52010-03-15 06:00:16 +0000307 if (TypeVec.empty())
308 TP.error("Type inference contradiction found, '" +
309 InputSet.getName() + "' needs to be a vector");
310 return MadeChange;
311}
312
313
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000314
Chris Lattner2cacec52010-03-15 06:00:16 +0000315/// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
316/// this an other based on this information.
317bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) {
318 // Both operands must be integer or FP, but we don't care which.
319 bool MadeChange = false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000320
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000321 if (isCompletelyUnknown())
322 MadeChange = FillWithPossibleTypes(TP);
323
324 if (Other.isCompletelyUnknown())
325 MadeChange = Other.FillWithPossibleTypes(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000326
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000327 // If one side is known to be integer or known to be FP but the other side has
328 // no information, get at least the type integrality info in there.
329 if (!hasFloatingPointTypes())
330 MadeChange |= Other.EnforceInteger(TP);
331 else if (!hasIntegerTypes())
332 MadeChange |= Other.EnforceFloatingPoint(TP);
333 if (!Other.hasFloatingPointTypes())
334 MadeChange |= EnforceInteger(TP);
335 else if (!Other.hasIntegerTypes())
336 MadeChange |= EnforceFloatingPoint(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000337
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000338 assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() &&
339 "Should have a type list now");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000340
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000341 // If one contains vectors but the other doesn't pull vectors out.
342 if (!hasVectorTypes())
343 MadeChange |= Other.EnforceScalar(TP);
344 if (!hasVectorTypes())
345 MadeChange |= EnforceScalar(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000346
David Greene9d7f0112011-02-01 19:12:32 +0000347 if (TypeVec.size() == 1 && Other.TypeVec.size() == 1) {
348 // If we are down to concrete types, this code does not currently
349 // handle nodes which have multiple types, where some types are
350 // integer, and some are fp. Assert that this is not the case.
351 assert(!(hasIntegerTypes() && hasFloatingPointTypes()) &&
352 !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) &&
353 "SDTCisOpSmallerThanOp does not handle mixed int/fp types!");
354
355 // Otherwise, if these are both vector types, either this vector
356 // must have a larger bitsize than the other, or this element type
357 // must be larger than the other.
358 EVT Type(TypeVec[0]);
359 EVT OtherType(Other.TypeVec[0]);
360
361 if (hasVectorTypes() && Other.hasVectorTypes()) {
362 if (Type.getSizeInBits() >= OtherType.getSizeInBits())
363 if (Type.getVectorElementType().getSizeInBits()
364 >= OtherType.getVectorElementType().getSizeInBits())
365 TP.error("Type inference contradiction found, '" +
366 getName() + "' element type not smaller than '" +
367 Other.getName() +"'!");
368 }
369 else
370 // For scalar types, the bitsize of this type must be larger
371 // than that of the other.
372 if (Type.getSizeInBits() >= OtherType.getSizeInBits())
373 TP.error("Type inference contradiction found, '" +
374 getName() + "' is not smaller than '" +
375 Other.getName() +"'!");
376
377 }
378
379
380 // Handle int and fp as disjoint sets. This won't work for patterns
381 // that have mixed fp/int types but those are likely rare and would
382 // not have been accepted by this code previously.
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000383
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000384 // Okay, find the smallest type from the current set and remove it from the
385 // largest set.
David Greenec83e2032011-02-04 17:01:53 +0000386 MVT::SimpleValueType SmallestInt = MVT::LAST_VALUETYPE;
David Greene9d7f0112011-02-01 19:12:32 +0000387 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
388 if (isInteger(TypeVec[i])) {
389 SmallestInt = TypeVec[i];
390 break;
391 }
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000392 for (unsigned i = 1, e = TypeVec.size(); i != e; ++i)
David Greene9d7f0112011-02-01 19:12:32 +0000393 if (isInteger(TypeVec[i]) && TypeVec[i] < SmallestInt)
394 SmallestInt = TypeVec[i];
395
David Greenec83e2032011-02-04 17:01:53 +0000396 MVT::SimpleValueType SmallestFP = MVT::LAST_VALUETYPE;
David Greene9d7f0112011-02-01 19:12:32 +0000397 for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
398 if (isFloatingPoint(TypeVec[i])) {
399 SmallestFP = TypeVec[i];
400 break;
401 }
402 for (unsigned i = 1, e = TypeVec.size(); i != e; ++i)
403 if (isFloatingPoint(TypeVec[i]) && TypeVec[i] < SmallestFP)
404 SmallestFP = TypeVec[i];
405
406 int OtherIntSize = 0;
407 int OtherFPSize = 0;
408 for (SmallVector<MVT::SimpleValueType, 2>::iterator TVI =
409 Other.TypeVec.begin();
410 TVI != Other.TypeVec.end();
411 /* NULL */) {
412 if (isInteger(*TVI)) {
413 ++OtherIntSize;
414 if (*TVI == SmallestInt) {
415 TVI = Other.TypeVec.erase(TVI);
416 --OtherIntSize;
417 MadeChange = true;
418 continue;
419 }
420 }
421 else if (isFloatingPoint(*TVI)) {
422 ++OtherFPSize;
423 if (*TVI == SmallestFP) {
424 TVI = Other.TypeVec.erase(TVI);
425 --OtherFPSize;
426 MadeChange = true;
427 continue;
428 }
429 }
430 ++TVI;
431 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000432
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000433 // If this is the only type in the large set, the constraint can never be
434 // satisfied.
David Greene9d7f0112011-02-01 19:12:32 +0000435 if ((Other.hasIntegerTypes() && OtherIntSize == 0)
436 || (Other.hasFloatingPointTypes() && OtherFPSize == 0))
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000437 TP.error("Type inference contradiction found, '" +
438 Other.getName() + "' has nothing larger than '" + getName() +"'!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000439
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000440 // Okay, find the largest type in the Other set and remove it from the
441 // current set.
David Greenec83e2032011-02-04 17:01:53 +0000442 MVT::SimpleValueType LargestInt = MVT::Other;
David Greene9d7f0112011-02-01 19:12:32 +0000443 for (unsigned i = 0, e = Other.TypeVec.size(); i != e; ++i)
444 if (isInteger(Other.TypeVec[i])) {
445 LargestInt = Other.TypeVec[i];
446 break;
447 }
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000448 for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i)
David Greene9d7f0112011-02-01 19:12:32 +0000449 if (isInteger(Other.TypeVec[i]) && Other.TypeVec[i] > LargestInt)
450 LargestInt = Other.TypeVec[i];
451
David Greenec83e2032011-02-04 17:01:53 +0000452 MVT::SimpleValueType LargestFP = MVT::Other;
David Greene9d7f0112011-02-01 19:12:32 +0000453 for (unsigned i = 0, e = Other.TypeVec.size(); i != e; ++i)
454 if (isFloatingPoint(Other.TypeVec[i])) {
455 LargestFP = Other.TypeVec[i];
456 break;
457 }
458 for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i)
459 if (isFloatingPoint(Other.TypeVec[i]) && Other.TypeVec[i] > LargestFP)
460 LargestFP = Other.TypeVec[i];
461
462 int IntSize = 0;
463 int FPSize = 0;
464 for (SmallVector<MVT::SimpleValueType, 2>::iterator TVI =
465 TypeVec.begin();
466 TVI != TypeVec.end();
467 /* NULL */) {
468 if (isInteger(*TVI)) {
469 ++IntSize;
470 if (*TVI == LargestInt) {
471 TVI = TypeVec.erase(TVI);
472 --IntSize;
473 MadeChange = true;
474 continue;
475 }
476 }
477 else if (isFloatingPoint(*TVI)) {
478 ++FPSize;
479 if (*TVI == LargestFP) {
480 TVI = TypeVec.erase(TVI);
481 --FPSize;
482 MadeChange = true;
483 continue;
484 }
485 }
486 ++TVI;
487 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000488
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000489 // If this is the only type in the small set, the constraint can never be
490 // satisfied.
David Greene9d7f0112011-02-01 19:12:32 +0000491 if ((hasIntegerTypes() && IntSize == 0)
492 || (hasFloatingPointTypes() && FPSize == 0))
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000493 TP.error("Type inference contradiction found, '" +
494 getName() + "' has nothing smaller than '" + Other.getName()+"'!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000495
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000496 return MadeChange;
Chris Lattner2cacec52010-03-15 06:00:16 +0000497}
498
499/// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
Chris Lattner66fb9d22010-03-24 00:01:16 +0000500/// whose element is specified by VTOperand.
501bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand,
Chris Lattner2cacec52010-03-15 06:00:16 +0000502 TreePattern &TP) {
Chris Lattner66fb9d22010-03-24 00:01:16 +0000503 // "This" must be a vector and "VTOperand" must be a scalar.
Chris Lattner2cacec52010-03-15 06:00:16 +0000504 bool MadeChange = false;
Chris Lattner66fb9d22010-03-24 00:01:16 +0000505 MadeChange |= EnforceVector(TP);
506 MadeChange |= VTOperand.EnforceScalar(TP);
507
508 // If we know the vector type, it forces the scalar to agree.
509 if (isConcrete()) {
510 EVT IVT = getConcrete();
511 IVT = IVT.getVectorElementType();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000512 return MadeChange |
Chris Lattner66fb9d22010-03-24 00:01:16 +0000513 VTOperand.MergeInTypeInfo(IVT.getSimpleVT().SimpleTy, TP);
514 }
515
516 // If the scalar type is known, filter out vector types whose element types
517 // disagree.
518 if (!VTOperand.isConcrete())
519 return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000520
Chris Lattner66fb9d22010-03-24 00:01:16 +0000521 MVT::SimpleValueType VT = VTOperand.getConcrete();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000522
Chris Lattner66fb9d22010-03-24 00:01:16 +0000523 TypeSet InputSet(*this);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000524
Chris Lattner66fb9d22010-03-24 00:01:16 +0000525 // Filter out all the types which don't have the right element type.
526 for (unsigned i = 0; i != TypeVec.size(); ++i) {
527 assert(isVector(TypeVec[i]) && "EnforceVector didn't work");
528 if (EVT(TypeVec[i]).getVectorElementType().getSimpleVT().SimpleTy != VT) {
Chris Lattner2cacec52010-03-15 06:00:16 +0000529 TypeVec.erase(TypeVec.begin()+i--);
530 MadeChange = true;
531 }
Chris Lattner66fb9d22010-03-24 00:01:16 +0000532 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000533
Chris Lattner2cacec52010-03-15 06:00:16 +0000534 if (TypeVec.empty()) // FIXME: Really want an SMLoc here!
535 TP.error("Type inference contradiction found, forcing '" +
536 InputSet.getName() + "' to have a vector element");
537 return MadeChange;
538}
539
David Greene60322692011-01-24 20:53:18 +0000540/// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to be a
541/// vector type specified by VTOperand.
542bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand,
543 TreePattern &TP) {
544 // "This" must be a vector and "VTOperand" must be a vector.
545 bool MadeChange = false;
546 MadeChange |= EnforceVector(TP);
547 MadeChange |= VTOperand.EnforceVector(TP);
548
549 // "This" must be larger than "VTOperand."
550 MadeChange |= VTOperand.EnforceSmallerThan(*this, TP);
551
552 // If we know the vector type, it forces the scalar types to agree.
553 if (isConcrete()) {
554 EVT IVT = getConcrete();
555 IVT = IVT.getVectorElementType();
556
557 EEVT::TypeSet EltTypeSet(IVT.getSimpleVT().SimpleTy, TP);
558 MadeChange |= VTOperand.EnforceVectorEltTypeIs(EltTypeSet, TP);
559 } else if (VTOperand.isConcrete()) {
560 EVT IVT = VTOperand.getConcrete();
561 IVT = IVT.getVectorElementType();
562
563 EEVT::TypeSet EltTypeSet(IVT.getSimpleVT().SimpleTy, TP);
564 MadeChange |= EnforceVectorEltTypeIs(EltTypeSet, TP);
565 }
566
567 return MadeChange;
568}
569
Chris Lattner2cacec52010-03-15 06:00:16 +0000570//===----------------------------------------------------------------------===//
571// Helpers for working with extended types.
Chris Lattner6cefb772008-01-05 22:25:12 +0000572
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000573bool RecordPtrCmp::operator()(const Record *LHS, const Record *RHS) const {
574 return LHS->getID() < RHS->getID();
575}
Scott Michel327d0652008-03-05 17:49:05 +0000576
577/// Dependent variable map for CodeGenDAGPattern variant generation
578typedef std::map<std::string, int> DepVarMap;
579
580/// Const iterator shorthand for DepVarMap
581typedef DepVarMap::const_iterator DepVarMap_citer;
582
Chris Lattner54379062011-04-17 21:38:24 +0000583static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
Scott Michel327d0652008-03-05 17:49:05 +0000584 if (N->isLeaf()) {
Chris Lattner54379062011-04-17 21:38:24 +0000585 if (dynamic_cast<DefInit*>(N->getLeafValue()) != NULL)
Scott Michel327d0652008-03-05 17:49:05 +0000586 DepMap[N->getName()]++;
Scott Michel327d0652008-03-05 17:49:05 +0000587 } else {
588 for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
589 FindDepVarsOf(N->getChild(i), DepMap);
590 }
591}
Chris Lattner54379062011-04-17 21:38:24 +0000592
593/// Find dependent variables within child patterns
594static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
Scott Michel327d0652008-03-05 17:49:05 +0000595 DepVarMap depcounts;
596 FindDepVarsOf(N, depcounts);
597 for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) {
Chris Lattner54379062011-04-17 21:38:24 +0000598 if (i->second > 1) // std::pair<std::string, int>
Scott Michel327d0652008-03-05 17:49:05 +0000599 DepVars.insert(i->first);
Scott Michel327d0652008-03-05 17:49:05 +0000600 }
601}
602
Daniel Dunbar6aa526b2010-10-08 02:07:22 +0000603#ifndef NDEBUG
Chris Lattner54379062011-04-17 21:38:24 +0000604/// Dump the dependent variable set:
605static void DumpDepVars(MultipleUseVarSet &DepVars) {
Scott Michel327d0652008-03-05 17:49:05 +0000606 if (DepVars.empty()) {
Chris Lattner569f1212009-08-23 04:44:11 +0000607 DEBUG(errs() << "<empty set>");
Scott Michel327d0652008-03-05 17:49:05 +0000608 } else {
Chris Lattner569f1212009-08-23 04:44:11 +0000609 DEBUG(errs() << "[ ");
Jim Grosbachbb168242010-10-08 18:13:57 +0000610 for (MultipleUseVarSet::const_iterator i = DepVars.begin(),
611 e = DepVars.end(); i != e; ++i) {
Chris Lattner569f1212009-08-23 04:44:11 +0000612 DEBUG(errs() << (*i) << " ");
Scott Michel327d0652008-03-05 17:49:05 +0000613 }
Chris Lattner569f1212009-08-23 04:44:11 +0000614 DEBUG(errs() << "]");
Scott Michel327d0652008-03-05 17:49:05 +0000615 }
616}
Daniel Dunbar6aa526b2010-10-08 02:07:22 +0000617#endif
618
Chris Lattner54379062011-04-17 21:38:24 +0000619
620//===----------------------------------------------------------------------===//
621// TreePredicateFn Implementation
622//===----------------------------------------------------------------------===//
623
624std::string TreePredicateFn::getPredCode() const {
625 return PatFragRec->getRecord()->getValueAsCode("PredicateCode");
626}
627
628
629/// isAlwaysTrue - Return true if this is a noop predicate.
630bool TreePredicateFn::isAlwaysTrue() const {
631 return getPredCode().empty();
632}
633
634/// Return the name to use in the generated code to reference this, this is
635/// "Predicate_foo" if from a pattern fragment "foo".
636std::string TreePredicateFn::getFnName() const {
637 return "Predicate_" + PatFragRec->getRecord()->getName();
638}
639
640/// getCodeToRunOnSDNode - Return the code for the function body that
641/// evaluates this predicate. The argument is expected to be in "Node",
642/// not N. This handles casting and conversion to a concrete node type as
643/// appropriate.
644std::string TreePredicateFn::getCodeToRunOnSDNode() const {
645 std::string ClassName;
646 if (PatFragRec->getOnlyTree()->isLeaf())
647 ClassName = "SDNode";
648 else {
649 Record *Op = PatFragRec->getOnlyTree()->getOperator();
650 ClassName = PatFragRec->getDAGPatterns().getSDNodeInfo(Op).getSDClassName();
651 }
652 std::string Result;
653 if (ClassName == "SDNode")
654 Result = " SDNode *N = Node;\n";
655 else
656 Result = " " + ClassName + "*N = cast<" + ClassName + ">(Node);\n";
657
658 return Result + getPredCode();
Scott Michel327d0652008-03-05 17:49:05 +0000659}
660
Chris Lattner6cefb772008-01-05 22:25:12 +0000661//===----------------------------------------------------------------------===//
Dan Gohman22bb3112008-08-22 00:20:26 +0000662// PatternToMatch implementation
663//
664
Chris Lattner48e86db2010-03-29 01:40:38 +0000665
666/// getPatternSize - Return the 'size' of this pattern. We want to match large
667/// patterns before small ones. This is used to determine the size of a
668/// pattern.
669static unsigned getPatternSize(const TreePatternNode *P,
670 const CodeGenDAGPatterns &CGP) {
671 unsigned Size = 3; // The node itself.
672 // If the root node is a ConstantSDNode, increases its size.
673 // e.g. (set R32:$dst, 0).
674 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
675 Size += 2;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000676
Chris Lattner48e86db2010-03-29 01:40:38 +0000677 // FIXME: This is a hack to statically increase the priority of patterns
678 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
679 // Later we can allow complexity / cost for each pattern to be (optionally)
680 // specified. To get best possible pattern match we'll need to dynamically
681 // calculate the complexity of all patterns a dag can potentially map to.
682 const ComplexPattern *AM = P->getComplexPatternInfo(CGP);
683 if (AM)
684 Size += AM->getNumOperands() * 3;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000685
Chris Lattner48e86db2010-03-29 01:40:38 +0000686 // If this node has some predicate function that must match, it adds to the
687 // complexity of this node.
688 if (!P->getPredicateFns().empty())
689 ++Size;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000690
Chris Lattner48e86db2010-03-29 01:40:38 +0000691 // Count children in the count if they are also nodes.
692 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
693 TreePatternNode *Child = P->getChild(i);
694 if (!Child->isLeaf() && Child->getNumTypes() &&
695 Child->getType(0) != MVT::Other)
696 Size += getPatternSize(Child, CGP);
697 else if (Child->isLeaf()) {
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000698 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Chris Lattner48e86db2010-03-29 01:40:38 +0000699 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
700 else if (Child->getComplexPatternInfo(CGP))
701 Size += getPatternSize(Child, CGP);
702 else if (!Child->getPredicateFns().empty())
703 ++Size;
704 }
705 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000706
Chris Lattner48e86db2010-03-29 01:40:38 +0000707 return Size;
708}
709
710/// Compute the complexity metric for the input pattern. This roughly
711/// corresponds to the number of nodes that are covered.
712unsigned PatternToMatch::
713getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
714 return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
715}
716
717
Dan Gohman22bb3112008-08-22 00:20:26 +0000718/// getPredicateCheck - Return a single string containing all of this
719/// pattern's predicates concatenated with "&&" operators.
720///
721std::string PatternToMatch::getPredicateCheck() const {
722 std::string PredicateCheck;
723 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
724 if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
725 Record *Def = Pred->getDef();
726 if (!Def->isSubClassOf("Predicate")) {
727#ifndef NDEBUG
728 Def->dump();
729#endif
730 assert(0 && "Unknown predicate type!");
731 }
732 if (!PredicateCheck.empty())
733 PredicateCheck += " && ";
734 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
735 }
736 }
737
738 return PredicateCheck;
739}
740
741//===----------------------------------------------------------------------===//
Chris Lattner6cefb772008-01-05 22:25:12 +0000742// SDTypeConstraint implementation
743//
744
745SDTypeConstraint::SDTypeConstraint(Record *R) {
746 OperandNo = R->getValueAsInt("OperandNum");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000747
Chris Lattner6cefb772008-01-05 22:25:12 +0000748 if (R->isSubClassOf("SDTCisVT")) {
749 ConstraintType = SDTCisVT;
750 x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT"));
Chris Lattnerc8122612010-03-28 06:04:39 +0000751 if (x.SDTCisVT_Info.VT == MVT::isVoid)
752 throw TGError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000753
Chris Lattner6cefb772008-01-05 22:25:12 +0000754 } else if (R->isSubClassOf("SDTCisPtrTy")) {
755 ConstraintType = SDTCisPtrTy;
756 } else if (R->isSubClassOf("SDTCisInt")) {
757 ConstraintType = SDTCisInt;
758 } else if (R->isSubClassOf("SDTCisFP")) {
759 ConstraintType = SDTCisFP;
Bob Wilson36e3e662009-08-12 22:30:59 +0000760 } else if (R->isSubClassOf("SDTCisVec")) {
761 ConstraintType = SDTCisVec;
Chris Lattner6cefb772008-01-05 22:25:12 +0000762 } else if (R->isSubClassOf("SDTCisSameAs")) {
763 ConstraintType = SDTCisSameAs;
764 x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum");
765 } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) {
766 ConstraintType = SDTCisVTSmallerThanOp;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000767 x.SDTCisVTSmallerThanOp_Info.OtherOperandNum =
Chris Lattner6cefb772008-01-05 22:25:12 +0000768 R->getValueAsInt("OtherOperandNum");
769 } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) {
770 ConstraintType = SDTCisOpSmallerThanOp;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000771 x.SDTCisOpSmallerThanOp_Info.BigOperandNum =
Chris Lattner6cefb772008-01-05 22:25:12 +0000772 R->getValueAsInt("BigOperandNum");
Nate Begemanb5af3342008-02-09 01:37:05 +0000773 } else if (R->isSubClassOf("SDTCisEltOfVec")) {
774 ConstraintType = SDTCisEltOfVec;
Chris Lattner2cacec52010-03-15 06:00:16 +0000775 x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum");
David Greene60322692011-01-24 20:53:18 +0000776 } else if (R->isSubClassOf("SDTCisSubVecOfVec")) {
777 ConstraintType = SDTCisSubVecOfVec;
778 x.SDTCisSubVecOfVec_Info.OtherOperandNum =
779 R->getValueAsInt("OtherOpNum");
Chris Lattner6cefb772008-01-05 22:25:12 +0000780 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000781 errs() << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n";
Chris Lattner6cefb772008-01-05 22:25:12 +0000782 exit(1);
783 }
784}
785
786/// getOperandNum - Return the node corresponding to operand #OpNo in tree
Chris Lattner2e68a022010-03-19 21:56:21 +0000787/// N, and the result number in ResNo.
788static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
789 const SDNodeInfo &NodeInfo,
790 unsigned &ResNo) {
791 unsigned NumResults = NodeInfo.getNumResults();
792 if (OpNo < NumResults) {
793 ResNo = OpNo;
794 return N;
795 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000796
Chris Lattner2e68a022010-03-19 21:56:21 +0000797 OpNo -= NumResults;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000798
Chris Lattner2e68a022010-03-19 21:56:21 +0000799 if (OpNo >= N->getNumChildren()) {
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000800 errs() << "Invalid operand number in type constraint "
Chris Lattner2e68a022010-03-19 21:56:21 +0000801 << (OpNo+NumResults) << " ";
Chris Lattner6cefb772008-01-05 22:25:12 +0000802 N->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +0000803 errs() << '\n';
Chris Lattner6cefb772008-01-05 22:25:12 +0000804 exit(1);
805 }
806
Chris Lattner2e68a022010-03-19 21:56:21 +0000807 return N->getChild(OpNo);
Chris Lattner6cefb772008-01-05 22:25:12 +0000808}
809
810/// ApplyTypeConstraint - Given a node in a pattern, apply this type
811/// constraint to the nodes operands. This returns true if it makes a
812/// change, false otherwise. If a type contradiction is found, throw an
813/// exception.
814bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
815 const SDNodeInfo &NodeInfo,
816 TreePattern &TP) const {
Chris Lattner2e68a022010-03-19 21:56:21 +0000817 unsigned ResNo = 0; // The result number being referenced.
818 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000819
Chris Lattner6cefb772008-01-05 22:25:12 +0000820 switch (ConstraintType) {
821 default: assert(0 && "Unknown constraint type!");
822 case SDTCisVT:
823 // Operand must be a particular type.
Chris Lattnerd7349192010-03-19 21:37:09 +0000824 return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000825 case SDTCisPtrTy:
Chris Lattner6cefb772008-01-05 22:25:12 +0000826 // Operand must be same as target pointer type.
Chris Lattnerd7349192010-03-19 21:37:09 +0000827 return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000828 case SDTCisInt:
829 // Require it to be one of the legal integer VTs.
Chris Lattnerd7349192010-03-19 21:37:09 +0000830 return NodeToApply->getExtType(ResNo).EnforceInteger(TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000831 case SDTCisFP:
832 // Require it to be one of the legal fp VTs.
Chris Lattnerd7349192010-03-19 21:37:09 +0000833 return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000834 case SDTCisVec:
835 // Require it to be one of the legal vector VTs.
Chris Lattnerd7349192010-03-19 21:37:09 +0000836 return NodeToApply->getExtType(ResNo).EnforceVector(TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000837 case SDTCisSameAs: {
Chris Lattner2e68a022010-03-19 21:56:21 +0000838 unsigned OResNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000839 TreePatternNode *OtherNode =
Chris Lattner2e68a022010-03-19 21:56:21 +0000840 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo);
Chris Lattnerd7349192010-03-19 21:37:09 +0000841 return NodeToApply->UpdateNodeType(OResNo, OtherNode->getExtType(ResNo),TP)|
842 OtherNode->UpdateNodeType(ResNo,NodeToApply->getExtType(OResNo),TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000843 }
844 case SDTCisVTSmallerThanOp: {
845 // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must
846 // have an integer type that is smaller than the VT.
847 if (!NodeToApply->isLeaf() ||
848 !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) ||
849 !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()
850 ->isSubClassOf("ValueType"))
851 TP.error(N->getOperator()->getName() + " expects a VT operand!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 MVT::SimpleValueType VT =
Chris Lattner6cefb772008-01-05 22:25:12 +0000853 getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef());
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000854
Chris Lattnercc878302010-03-24 00:06:46 +0000855 EEVT::TypeSet TypeListTmp(VT, TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000856
Chris Lattner2e68a022010-03-19 21:56:21 +0000857 unsigned OResNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000858 TreePatternNode *OtherNode =
Chris Lattner2e68a022010-03-19 21:56:21 +0000859 getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo,
860 OResNo);
Chris Lattner2cacec52010-03-15 06:00:16 +0000861
Chris Lattnercc878302010-03-24 00:06:46 +0000862 return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000863 }
864 case SDTCisOpSmallerThanOp: {
Chris Lattner2e68a022010-03-19 21:56:21 +0000865 unsigned BResNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000866 TreePatternNode *BigOperand =
Chris Lattner2e68a022010-03-19 21:56:21 +0000867 getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo,
868 BResNo);
Chris Lattnerd7349192010-03-19 21:37:09 +0000869 return NodeToApply->getExtType(ResNo).
870 EnforceSmallerThan(BigOperand->getExtType(BResNo), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000871 }
Nate Begemanb5af3342008-02-09 01:37:05 +0000872 case SDTCisEltOfVec: {
Chris Lattner2e68a022010-03-19 21:56:21 +0000873 unsigned VResNo = 0;
Chris Lattner2cacec52010-03-15 06:00:16 +0000874 TreePatternNode *VecOperand =
Chris Lattner2e68a022010-03-19 21:56:21 +0000875 getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo,
876 VResNo);
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000877
Chris Lattner66fb9d22010-03-24 00:01:16 +0000878 // Filter vector types out of VecOperand that don't have the right element
879 // type.
880 return VecOperand->getExtType(VResNo).
881 EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP);
Nate Begemanb5af3342008-02-09 01:37:05 +0000882 }
David Greene60322692011-01-24 20:53:18 +0000883 case SDTCisSubVecOfVec: {
884 unsigned VResNo = 0;
885 TreePatternNode *BigVecOperand =
886 getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo,
887 VResNo);
888
889 // Filter vector types out of BigVecOperand that don't have the
890 // right subvector type.
891 return BigVecOperand->getExtType(VResNo).
892 EnforceVectorSubVectorTypeIs(NodeToApply->getExtType(ResNo), TP);
893 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000894 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000895 return false;
896}
897
898//===----------------------------------------------------------------------===//
899// SDNodeInfo implementation
900//
901SDNodeInfo::SDNodeInfo(Record *R) : Def(R) {
902 EnumName = R->getValueAsString("Opcode");
903 SDClassName = R->getValueAsString("SDClass");
904 Record *TypeProfile = R->getValueAsDef("TypeProfile");
905 NumResults = TypeProfile->getValueAsInt("NumResults");
906 NumOperands = TypeProfile->getValueAsInt("NumOperands");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000907
Chris Lattner6cefb772008-01-05 22:25:12 +0000908 // Parse the properties.
909 Properties = 0;
910 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
911 for (unsigned i = 0, e = PropList.size(); i != e; ++i) {
912 if (PropList[i]->getName() == "SDNPCommutative") {
913 Properties |= 1 << SDNPCommutative;
914 } else if (PropList[i]->getName() == "SDNPAssociative") {
915 Properties |= 1 << SDNPAssociative;
916 } else if (PropList[i]->getName() == "SDNPHasChain") {
917 Properties |= 1 << SDNPHasChain;
Chris Lattner036609b2010-12-23 18:28:41 +0000918 } else if (PropList[i]->getName() == "SDNPOutGlue") {
919 Properties |= 1 << SDNPOutGlue;
920 } else if (PropList[i]->getName() == "SDNPInGlue") {
921 Properties |= 1 << SDNPInGlue;
922 } else if (PropList[i]->getName() == "SDNPOptInGlue") {
923 Properties |= 1 << SDNPOptInGlue;
Chris Lattnerc8478d82008-01-06 06:44:58 +0000924 } else if (PropList[i]->getName() == "SDNPMayStore") {
925 Properties |= 1 << SDNPMayStore;
Chris Lattner710e9952008-01-10 04:38:57 +0000926 } else if (PropList[i]->getName() == "SDNPMayLoad") {
927 Properties |= 1 << SDNPMayLoad;
Chris Lattnerbc0b9f72008-01-10 05:39:30 +0000928 } else if (PropList[i]->getName() == "SDNPSideEffect") {
929 Properties |= 1 << SDNPSideEffect;
Mon P Wang28873102008-06-25 08:15:39 +0000930 } else if (PropList[i]->getName() == "SDNPMemOperand") {
931 Properties |= 1 << SDNPMemOperand;
Chris Lattnere8cabf32010-03-19 05:07:09 +0000932 } else if (PropList[i]->getName() == "SDNPVariadic") {
933 Properties |= 1 << SDNPVariadic;
Chris Lattner6cefb772008-01-05 22:25:12 +0000934 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000935 errs() << "Unknown SD Node property '" << PropList[i]->getName()
936 << "' on node '" << R->getName() << "'!\n";
Chris Lattner6cefb772008-01-05 22:25:12 +0000937 exit(1);
938 }
939 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000940
941
Chris Lattner6cefb772008-01-05 22:25:12 +0000942 // Parse the type constraints.
943 std::vector<Record*> ConstraintList =
944 TypeProfile->getValueAsListOfDefs("Constraints");
945 TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end());
946}
947
Chris Lattner22579812010-02-28 00:22:30 +0000948/// getKnownType - If the type constraints on this node imply a fixed type
949/// (e.g. all stores return void, etc), then return it as an
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000950/// MVT::SimpleValueType. Otherwise, return EEVT::Other.
Chris Lattner084df622010-03-24 00:41:19 +0000951MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
Chris Lattner22579812010-02-28 00:22:30 +0000952 unsigned NumResults = getNumResults();
953 assert(NumResults <= 1 &&
954 "We only work with nodes with zero or one result so far!");
Chris Lattner084df622010-03-24 00:41:19 +0000955 assert(ResNo == 0 && "Only handles single result nodes so far");
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000956
Chris Lattner22579812010-02-28 00:22:30 +0000957 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) {
958 // Make sure that this applies to the correct node result.
959 if (TypeConstraints[i].OperandNo >= NumResults) // FIXME: need value #
960 continue;
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000961
Chris Lattner22579812010-02-28 00:22:30 +0000962 switch (TypeConstraints[i].ConstraintType) {
963 default: break;
964 case SDTypeConstraint::SDTCisVT:
965 return TypeConstraints[i].x.SDTCisVT_Info.VT;
966 case SDTypeConstraint::SDTCisPtrTy:
967 return MVT::iPTR;
968 }
969 }
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000970 return MVT::Other;
Chris Lattner22579812010-02-28 00:22:30 +0000971}
972
Chris Lattner6cefb772008-01-05 22:25:12 +0000973//===----------------------------------------------------------------------===//
974// TreePatternNode implementation
975//
976
977TreePatternNode::~TreePatternNode() {
978#if 0 // FIXME: implement refcounted tree nodes!
979 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
980 delete getChild(i);
981#endif
982}
983
Chris Lattnerd7349192010-03-19 21:37:09 +0000984static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
985 if (Operator->getName() == "set" ||
Chris Lattner310adf12010-03-27 02:53:27 +0000986 Operator->getName() == "implicit")
Chris Lattnerd7349192010-03-19 21:37:09 +0000987 return 0; // All return nothing.
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000988
Chris Lattner93dc92e2010-03-22 20:56:36 +0000989 if (Operator->isSubClassOf("Intrinsic"))
990 return CDP.getIntrinsic(Operator).IS.RetVTs.size();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000991
Chris Lattnerd7349192010-03-19 21:37:09 +0000992 if (Operator->isSubClassOf("SDNode"))
993 return CDP.getSDNodeInfo(Operator).getNumResults();
Jim Grosbachfbadcd02010-12-21 16:16:00 +0000994
Chris Lattnerd7349192010-03-19 21:37:09 +0000995 if (Operator->isSubClassOf("PatFrag")) {
996 // If we've already parsed this pattern fragment, get it. Otherwise, handle
997 // the forward reference case where one pattern fragment references another
998 // before it is processed.
999 if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator))
1000 return PFRec->getOnlyTree()->getNumTypes();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001001
Chris Lattnerd7349192010-03-19 21:37:09 +00001002 // Get the result tree.
1003 DagInit *Tree = Operator->getValueAsDag("Fragment");
1004 Record *Op = 0;
1005 if (Tree && dynamic_cast<DefInit*>(Tree->getOperator()))
1006 Op = dynamic_cast<DefInit*>(Tree->getOperator())->getDef();
1007 assert(Op && "Invalid Fragment");
1008 return GetNumNodeResults(Op, CDP);
1009 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001010
Chris Lattnerd7349192010-03-19 21:37:09 +00001011 if (Operator->isSubClassOf("Instruction")) {
1012 CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator);
Chris Lattner0be6fe72010-03-27 19:15:02 +00001013
1014 // FIXME: Should allow access to all the results here.
Chris Lattnerc240bb02010-11-01 04:03:32 +00001015 unsigned NumDefsToAdd = InstInfo.Operands.NumDefs ? 1 : 0;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001016
Chris Lattner9414ae52010-03-27 20:09:24 +00001017 // Add on one implicit def if it has a resolvable type.
1018 if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other)
1019 ++NumDefsToAdd;
Chris Lattner0be6fe72010-03-27 19:15:02 +00001020 return NumDefsToAdd;
Chris Lattnerd7349192010-03-19 21:37:09 +00001021 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001022
Chris Lattnerd7349192010-03-19 21:37:09 +00001023 if (Operator->isSubClassOf("SDNodeXForm"))
1024 return 1; // FIXME: Generalize SDNodeXForm
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001025
Chris Lattnerd7349192010-03-19 21:37:09 +00001026 Operator->dump();
1027 errs() << "Unhandled node in GetNumNodeResults\n";
1028 exit(1);
1029}
1030
1031void TreePatternNode::print(raw_ostream &OS) const {
1032 if (isLeaf())
1033 OS << *getLeafValue();
1034 else
1035 OS << '(' << getOperator()->getName();
1036
1037 for (unsigned i = 0, e = Types.size(); i != e; ++i)
1038 OS << ':' << getExtType(i).getName();
Chris Lattner6cefb772008-01-05 22:25:12 +00001039
1040 if (!isLeaf()) {
1041 if (getNumChildren() != 0) {
1042 OS << " ";
1043 getChild(0)->print(OS);
1044 for (unsigned i = 1, e = getNumChildren(); i != e; ++i) {
1045 OS << ", ";
1046 getChild(i)->print(OS);
1047 }
1048 }
1049 OS << ")";
1050 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001051
Dan Gohman0540e172008-10-15 06:17:21 +00001052 for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i)
Chris Lattner54379062011-04-17 21:38:24 +00001053 OS << "<<P:" << PredicateFns[i].getFnName() << ">>";
Chris Lattner6cefb772008-01-05 22:25:12 +00001054 if (TransformFn)
1055 OS << "<<X:" << TransformFn->getName() << ">>";
1056 if (!getName().empty())
1057 OS << ":$" << getName();
1058
1059}
1060void TreePatternNode::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001061 print(errs());
Chris Lattner6cefb772008-01-05 22:25:12 +00001062}
1063
Scott Michel327d0652008-03-05 17:49:05 +00001064/// isIsomorphicTo - Return true if this node is recursively
1065/// isomorphic to the specified node. For this comparison, the node's
1066/// entire state is considered. The assigned name is ignored, since
1067/// nodes with differing names are considered isomorphic. However, if
1068/// the assigned name is present in the dependent variable set, then
1069/// the assigned name is considered significant and the node is
1070/// isomorphic if the names match.
1071bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N,
1072 const MultipleUseVarSet &DepVars) const {
Chris Lattner6cefb772008-01-05 22:25:12 +00001073 if (N == this) return true;
Chris Lattnerd7349192010-03-19 21:37:09 +00001074 if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() ||
Dan Gohman0540e172008-10-15 06:17:21 +00001075 getPredicateFns() != N->getPredicateFns() ||
Chris Lattner6cefb772008-01-05 22:25:12 +00001076 getTransformFn() != N->getTransformFn())
1077 return false;
1078
1079 if (isLeaf()) {
Scott Michel327d0652008-03-05 17:49:05 +00001080 if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) {
1081 if (DefInit *NDI = dynamic_cast<DefInit*>(N->getLeafValue())) {
Chris Lattner71a2cb22008-03-20 01:22:40 +00001082 return ((DI->getDef() == NDI->getDef())
1083 && (DepVars.find(getName()) == DepVars.end()
1084 || getName() == N->getName()));
Scott Michel327d0652008-03-05 17:49:05 +00001085 }
1086 }
Chris Lattner6cefb772008-01-05 22:25:12 +00001087 return getLeafValue() == N->getLeafValue();
1088 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001089
Chris Lattner6cefb772008-01-05 22:25:12 +00001090 if (N->getOperator() != getOperator() ||
1091 N->getNumChildren() != getNumChildren()) return false;
1092 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
Scott Michel327d0652008-03-05 17:49:05 +00001093 if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars))
Chris Lattner6cefb772008-01-05 22:25:12 +00001094 return false;
1095 return true;
1096}
1097
1098/// clone - Make a copy of this tree and all of its children.
1099///
1100TreePatternNode *TreePatternNode::clone() const {
1101 TreePatternNode *New;
1102 if (isLeaf()) {
Chris Lattnerd7349192010-03-19 21:37:09 +00001103 New = new TreePatternNode(getLeafValue(), getNumTypes());
Chris Lattner6cefb772008-01-05 22:25:12 +00001104 } else {
1105 std::vector<TreePatternNode*> CChildren;
1106 CChildren.reserve(Children.size());
1107 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1108 CChildren.push_back(getChild(i)->clone());
Chris Lattnerd7349192010-03-19 21:37:09 +00001109 New = new TreePatternNode(getOperator(), CChildren, getNumTypes());
Chris Lattner6cefb772008-01-05 22:25:12 +00001110 }
1111 New->setName(getName());
Chris Lattnerd7349192010-03-19 21:37:09 +00001112 New->Types = Types;
Dan Gohman0540e172008-10-15 06:17:21 +00001113 New->setPredicateFns(getPredicateFns());
Chris Lattner6cefb772008-01-05 22:25:12 +00001114 New->setTransformFn(getTransformFn());
1115 return New;
1116}
1117
Chris Lattner47661322010-02-14 22:22:58 +00001118/// RemoveAllTypes - Recursively strip all the types of this tree.
1119void TreePatternNode::RemoveAllTypes() {
Chris Lattnerd7349192010-03-19 21:37:09 +00001120 for (unsigned i = 0, e = Types.size(); i != e; ++i)
1121 Types[i] = EEVT::TypeSet(); // Reset to unknown type.
Chris Lattner47661322010-02-14 22:22:58 +00001122 if (isLeaf()) return;
1123 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1124 getChild(i)->RemoveAllTypes();
1125}
1126
1127
Chris Lattner6cefb772008-01-05 22:25:12 +00001128/// SubstituteFormalArguments - Replace the formal arguments in this tree
1129/// with actual values specified by ArgMap.
1130void TreePatternNode::
1131SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) {
1132 if (isLeaf()) return;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001133
Chris Lattner6cefb772008-01-05 22:25:12 +00001134 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1135 TreePatternNode *Child = getChild(i);
1136 if (Child->isLeaf()) {
1137 Init *Val = Child->getLeafValue();
1138 if (dynamic_cast<DefInit*>(Val) &&
1139 static_cast<DefInit*>(Val)->getDef()->getName() == "node") {
1140 // We found a use of a formal argument, replace it with its value.
Dan Gohman0540e172008-10-15 06:17:21 +00001141 TreePatternNode *NewChild = ArgMap[Child->getName()];
1142 assert(NewChild && "Couldn't find formal argument!");
1143 assert((Child->getPredicateFns().empty() ||
1144 NewChild->getPredicateFns() == Child->getPredicateFns()) &&
1145 "Non-empty child predicate clobbered!");
1146 setChild(i, NewChild);
Chris Lattner6cefb772008-01-05 22:25:12 +00001147 }
1148 } else {
1149 getChild(i)->SubstituteFormalArguments(ArgMap);
1150 }
1151 }
1152}
1153
1154
1155/// InlinePatternFragments - If this pattern refers to any pattern
1156/// fragments, inline them into place, giving us a pattern without any
1157/// PatFrag references.
1158TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) {
1159 if (isLeaf()) return this; // nothing to do.
1160 Record *Op = getOperator();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001161
Chris Lattner6cefb772008-01-05 22:25:12 +00001162 if (!Op->isSubClassOf("PatFrag")) {
1163 // Just recursively inline children nodes.
Dan Gohman0540e172008-10-15 06:17:21 +00001164 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1165 TreePatternNode *Child = getChild(i);
1166 TreePatternNode *NewChild = Child->InlinePatternFragments(TP);
1167
1168 assert((Child->getPredicateFns().empty() ||
1169 NewChild->getPredicateFns() == Child->getPredicateFns()) &&
1170 "Non-empty child predicate clobbered!");
1171
1172 setChild(i, NewChild);
1173 }
Chris Lattner6cefb772008-01-05 22:25:12 +00001174 return this;
1175 }
1176
1177 // Otherwise, we found a reference to a fragment. First, look up its
1178 // TreePattern record.
1179 TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001180
Chris Lattner6cefb772008-01-05 22:25:12 +00001181 // Verify that we are passing the right number of operands.
1182 if (Frag->getNumArgs() != Children.size())
1183 TP.error("'" + Op->getName() + "' fragment requires " +
1184 utostr(Frag->getNumArgs()) + " operands!");
1185
1186 TreePatternNode *FragTree = Frag->getOnlyTree()->clone();
1187
Chris Lattner54379062011-04-17 21:38:24 +00001188 TreePredicateFn PredFn(Frag);
1189 if (!PredFn.isAlwaysTrue())
1190 FragTree->addPredicateFn(PredFn);
Dan Gohman0540e172008-10-15 06:17:21 +00001191
Chris Lattner6cefb772008-01-05 22:25:12 +00001192 // Resolve formal arguments to their actual value.
1193 if (Frag->getNumArgs()) {
1194 // Compute the map of formal to actual arguments.
1195 std::map<std::string, TreePatternNode*> ArgMap;
1196 for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i)
1197 ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001198
Chris Lattner6cefb772008-01-05 22:25:12 +00001199 FragTree->SubstituteFormalArguments(ArgMap);
1200 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001201
Chris Lattner6cefb772008-01-05 22:25:12 +00001202 FragTree->setName(getName());
Chris Lattnerd7349192010-03-19 21:37:09 +00001203 for (unsigned i = 0, e = Types.size(); i != e; ++i)
1204 FragTree->UpdateNodeType(i, getExtType(i), TP);
Dan Gohman0540e172008-10-15 06:17:21 +00001205
1206 // Transfer in the old predicates.
1207 for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i)
1208 FragTree->addPredicateFn(getPredicateFns()[i]);
1209
Chris Lattner6cefb772008-01-05 22:25:12 +00001210 // Get a new copy of this fragment to stitch into here.
1211 //delete this; // FIXME: implement refcounting!
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001212
Chris Lattner2ca698d2008-06-30 03:02:03 +00001213 // The fragment we inlined could have recursive inlining that is needed. See
1214 // if there are any pattern fragments in it and inline them as needed.
1215 return FragTree->InlinePatternFragments(TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001216}
1217
1218/// getImplicitType - Check to see if the specified record has an implicit
Nick Lewyckyfc4c2552009-06-17 04:23:52 +00001219/// type which should be applied to it. This will infer the type of register
Chris Lattner6cefb772008-01-05 22:25:12 +00001220/// references from the register file information, for example.
1221///
Chris Lattnerd7349192010-03-19 21:37:09 +00001222static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo,
1223 bool NotRegisters, TreePattern &TP) {
Chris Lattner2cacec52010-03-15 06:00:16 +00001224 // Check to see if this is a register or a register class.
Chris Lattner6cefb772008-01-05 22:25:12 +00001225 if (R->isSubClassOf("RegisterClass")) {
Chris Lattner640a3f52010-03-23 23:50:31 +00001226 assert(ResNo == 0 && "Regclass ref only has one result!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001227 if (NotRegisters)
Chris Lattner2cacec52010-03-15 06:00:16 +00001228 return EEVT::TypeSet(); // Unknown.
1229 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1230 return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes());
Chris Lattner640a3f52010-03-23 23:50:31 +00001231 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001232
Chris Lattner640a3f52010-03-23 23:50:31 +00001233 if (R->isSubClassOf("PatFrag")) {
1234 assert(ResNo == 0 && "FIXME: PatFrag with multiple results?");
Chris Lattner6cefb772008-01-05 22:25:12 +00001235 // Pattern fragment types will be resolved when they are inlined.
Chris Lattner2cacec52010-03-15 06:00:16 +00001236 return EEVT::TypeSet(); // Unknown.
Chris Lattner640a3f52010-03-23 23:50:31 +00001237 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001238
Chris Lattner640a3f52010-03-23 23:50:31 +00001239 if (R->isSubClassOf("Register")) {
1240 assert(ResNo == 0 && "Registers only produce one result!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001241 if (NotRegisters)
Chris Lattner2cacec52010-03-15 06:00:16 +00001242 return EEVT::TypeSet(); // Unknown.
Chris Lattner6cefb772008-01-05 22:25:12 +00001243 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
Chris Lattner2cacec52010-03-15 06:00:16 +00001244 return EEVT::TypeSet(T.getRegisterVTs(R));
Chris Lattner640a3f52010-03-23 23:50:31 +00001245 }
Jakob Stoklund Olesen73ea7bf2010-05-24 14:48:12 +00001246
1247 if (R->isSubClassOf("SubRegIndex")) {
1248 assert(ResNo == 0 && "SubRegisterIndices only produce one result!");
1249 return EEVT::TypeSet();
1250 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001251
Chris Lattner640a3f52010-03-23 23:50:31 +00001252 if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) {
1253 assert(ResNo == 0 && "This node only has one result!");
Chris Lattner6cefb772008-01-05 22:25:12 +00001254 // Using a VTSDNode or CondCodeSDNode.
Chris Lattner2cacec52010-03-15 06:00:16 +00001255 return EEVT::TypeSet(MVT::Other, TP);
Chris Lattner640a3f52010-03-23 23:50:31 +00001256 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001257
Chris Lattner640a3f52010-03-23 23:50:31 +00001258 if (R->isSubClassOf("ComplexPattern")) {
1259 assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001260 if (NotRegisters)
Chris Lattner2cacec52010-03-15 06:00:16 +00001261 return EEVT::TypeSet(); // Unknown.
1262 return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(),
1263 TP);
Chris Lattner640a3f52010-03-23 23:50:31 +00001264 }
1265 if (R->isSubClassOf("PointerLikeRegClass")) {
1266 assert(ResNo == 0 && "Regclass can only have one result!");
Chris Lattner2cacec52010-03-15 06:00:16 +00001267 return EEVT::TypeSet(MVT::iPTR, TP);
Chris Lattner640a3f52010-03-23 23:50:31 +00001268 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001269
Chris Lattner640a3f52010-03-23 23:50:31 +00001270 if (R->getName() == "node" || R->getName() == "srcvalue" ||
1271 R->getName() == "zero_reg") {
Chris Lattner6cefb772008-01-05 22:25:12 +00001272 // Placeholder.
Chris Lattner2cacec52010-03-15 06:00:16 +00001273 return EEVT::TypeSet(); // Unknown.
Chris Lattner6cefb772008-01-05 22:25:12 +00001274 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001275
Chris Lattner6cefb772008-01-05 22:25:12 +00001276 TP.error("Unknown node flavor used in pattern: " + R->getName());
Chris Lattner2cacec52010-03-15 06:00:16 +00001277 return EEVT::TypeSet(MVT::Other, TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001278}
1279
Chris Lattnere67bde52008-01-06 05:36:50 +00001280
1281/// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
1282/// CodeGenIntrinsic information for it, otherwise return a null pointer.
1283const CodeGenIntrinsic *TreePatternNode::
1284getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
1285 if (getOperator() != CDP.get_intrinsic_void_sdnode() &&
1286 getOperator() != CDP.get_intrinsic_w_chain_sdnode() &&
1287 getOperator() != CDP.get_intrinsic_wo_chain_sdnode())
1288 return 0;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001289
1290 unsigned IID =
Chris Lattnere67bde52008-01-06 05:36:50 +00001291 dynamic_cast<IntInit*>(getChild(0)->getLeafValue())->getValue();
1292 return &CDP.getIntrinsicInfo(IID);
1293}
1294
Chris Lattner47661322010-02-14 22:22:58 +00001295/// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
1296/// return the ComplexPattern information, otherwise return null.
1297const ComplexPattern *
1298TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
1299 if (!isLeaf()) return 0;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001300
Chris Lattner47661322010-02-14 22:22:58 +00001301 DefInit *DI = dynamic_cast<DefInit*>(getLeafValue());
1302 if (DI && DI->getDef()->isSubClassOf("ComplexPattern"))
1303 return &CGP.getComplexPattern(DI->getDef());
1304 return 0;
1305}
1306
1307/// NodeHasProperty - Return true if this node has the specified property.
1308bool TreePatternNode::NodeHasProperty(SDNP Property,
Chris Lattner751d5aa2010-02-14 22:33:49 +00001309 const CodeGenDAGPatterns &CGP) const {
Chris Lattner47661322010-02-14 22:22:58 +00001310 if (isLeaf()) {
1311 if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
1312 return CP->hasProperty(Property);
1313 return false;
1314 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001315
Chris Lattner47661322010-02-14 22:22:58 +00001316 Record *Operator = getOperator();
1317 if (!Operator->isSubClassOf("SDNode")) return false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001318
Chris Lattner47661322010-02-14 22:22:58 +00001319 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
1320}
1321
1322
1323
1324
1325/// TreeHasProperty - Return true if any node in this tree has the specified
1326/// property.
1327bool TreePatternNode::TreeHasProperty(SDNP Property,
Chris Lattner751d5aa2010-02-14 22:33:49 +00001328 const CodeGenDAGPatterns &CGP) const {
Chris Lattner47661322010-02-14 22:22:58 +00001329 if (NodeHasProperty(Property, CGP))
1330 return true;
1331 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1332 if (getChild(i)->TreeHasProperty(Property, CGP))
1333 return true;
1334 return false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001335}
Chris Lattner47661322010-02-14 22:22:58 +00001336
Evan Cheng6bd95672008-06-16 20:29:38 +00001337/// isCommutativeIntrinsic - Return true if the node corresponds to a
1338/// commutative intrinsic.
1339bool
1340TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const {
1341 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP))
1342 return Int->isCommutative;
1343 return false;
1344}
1345
Chris Lattnere67bde52008-01-06 05:36:50 +00001346
Bob Wilson6c01ca92009-01-05 17:23:09 +00001347/// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +00001348/// this node and its children in the tree. This returns true if it makes a
1349/// change, false otherwise. If a type contradiction is found, throw an
1350/// exception.
1351bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
Chris Lattnerfe718932008-01-06 01:10:31 +00001352 CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
Chris Lattner6cefb772008-01-05 22:25:12 +00001353 if (isLeaf()) {
1354 if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) {
1355 // If it's a regclass or something else known, include the type.
Chris Lattnerd7349192010-03-19 21:37:09 +00001356 bool MadeChange = false;
1357 for (unsigned i = 0, e = Types.size(); i != e; ++i)
1358 MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
1359 NotRegisters, TP), TP);
1360 return MadeChange;
Chris Lattner523f6a52010-02-14 21:10:15 +00001361 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001362
Chris Lattner523f6a52010-02-14 21:10:15 +00001363 if (IntInit *II = dynamic_cast<IntInit*>(getLeafValue())) {
Chris Lattnerd7349192010-03-19 21:37:09 +00001364 assert(Types.size() == 1 && "Invalid IntInit");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001365
Chris Lattnerd7349192010-03-19 21:37:09 +00001366 // Int inits are always integers. :)
1367 bool MadeChange = Types[0].EnforceInteger(TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001368
Chris Lattnerd7349192010-03-19 21:37:09 +00001369 if (!Types[0].isConcrete())
Chris Lattner2cacec52010-03-15 06:00:16 +00001370 return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001371
Chris Lattnerd7349192010-03-19 21:37:09 +00001372 MVT::SimpleValueType VT = getType(0);
Chris Lattner2cacec52010-03-15 06:00:16 +00001373 if (VT == MVT::iPTR || VT == MVT::iPTRAny)
1374 return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001375
Chris Lattner2cacec52010-03-15 06:00:16 +00001376 unsigned Size = EVT(VT).getSizeInBits();
1377 // Make sure that the value is representable for this type.
1378 if (Size >= 32) return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001379
Chris Lattner2cacec52010-03-15 06:00:16 +00001380 int Val = (II->getValue() << (32-Size)) >> (32-Size);
1381 if (Val == II->getValue()) return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001382
Chris Lattner2cacec52010-03-15 06:00:16 +00001383 // If sign-extended doesn't fit, does it fit as unsigned?
1384 unsigned ValueMask;
1385 unsigned UnsignedVal;
1386 ValueMask = unsigned(~uint32_t(0UL) >> (32-Size));
1387 UnsignedVal = unsigned(II->getValue());
Scott Michel0123b7d2008-02-15 23:05:48 +00001388
Chris Lattner2cacec52010-03-15 06:00:16 +00001389 if ((ValueMask & UnsignedVal) == UnsignedVal)
1390 return MadeChange;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001391
Chris Lattner2cacec52010-03-15 06:00:16 +00001392 TP.error("Integer value '" + itostr(II->getValue())+
Chris Lattnerd7349192010-03-19 21:37:09 +00001393 "' is out of range for type '" + getEnumName(getType(0)) + "'!");
Chris Lattner6cefb772008-01-05 22:25:12 +00001394 return MadeChange;
1395 }
1396 return false;
1397 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001398
Chris Lattner6cefb772008-01-05 22:25:12 +00001399 // special handling for set, which isn't really an SDNode.
1400 if (getOperator()->getName() == "set") {
Chris Lattnerd7349192010-03-19 21:37:09 +00001401 assert(getNumTypes() == 0 && "Set doesn't produce a value");
1402 assert(getNumChildren() >= 2 && "Missing RHS of a set?");
Chris Lattner6cefb772008-01-05 22:25:12 +00001403 unsigned NC = getNumChildren();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001404
Chris Lattnerd7349192010-03-19 21:37:09 +00001405 TreePatternNode *SetVal = getChild(NC-1);
1406 bool MadeChange = SetVal->ApplyTypeConstraints(TP, NotRegisters);
1407
Chris Lattner6cefb772008-01-05 22:25:12 +00001408 for (unsigned i = 0; i < NC-1; ++i) {
Chris Lattnerd7349192010-03-19 21:37:09 +00001409 TreePatternNode *Child = getChild(i);
1410 MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001411
Chris Lattner6cefb772008-01-05 22:25:12 +00001412 // Types of operands must match.
Chris Lattnerd7349192010-03-19 21:37:09 +00001413 MadeChange |= Child->UpdateNodeType(0, SetVal->getExtType(i), TP);
1414 MadeChange |= SetVal->UpdateNodeType(i, Child->getExtType(0), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001415 }
1416 return MadeChange;
Chris Lattner6eb30122010-02-23 05:51:07 +00001417 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001418
Chris Lattner310adf12010-03-27 02:53:27 +00001419 if (getOperator()->getName() == "implicit") {
Chris Lattnerd7349192010-03-19 21:37:09 +00001420 assert(getNumTypes() == 0 && "Node doesn't produce a value");
1421
Chris Lattner6cefb772008-01-05 22:25:12 +00001422 bool MadeChange = false;
1423 for (unsigned i = 0; i < getNumChildren(); ++i)
1424 MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
Chris Lattner6cefb772008-01-05 22:25:12 +00001425 return MadeChange;
Chris Lattner6eb30122010-02-23 05:51:07 +00001426 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001427
Chris Lattner6eb30122010-02-23 05:51:07 +00001428 if (getOperator()->getName() == "COPY_TO_REGCLASS") {
Dan Gohmanf8c73942009-04-13 15:38:05 +00001429 bool MadeChange = false;
1430 MadeChange |= getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
1431 MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001432
Chris Lattnerd7349192010-03-19 21:37:09 +00001433 assert(getChild(0)->getNumTypes() == 1 &&
1434 getChild(1)->getNumTypes() == 1 && "Unhandled case");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001435
Chris Lattner2cacec52010-03-15 06:00:16 +00001436 // child #1 of COPY_TO_REGCLASS should be a register class. We don't care
1437 // what type it gets, so if it didn't get a concrete type just give it the
1438 // first viable type from the reg class.
Chris Lattnerd7349192010-03-19 21:37:09 +00001439 if (!getChild(1)->hasTypeSet(0) &&
1440 !getChild(1)->getExtType(0).isCompletelyUnknown()) {
1441 MVT::SimpleValueType RCVT = getChild(1)->getExtType(0).getTypeList()[0];
1442 MadeChange |= getChild(1)->UpdateNodeType(0, RCVT, TP);
Chris Lattner2cacec52010-03-15 06:00:16 +00001443 }
Dan Gohmanf8c73942009-04-13 15:38:05 +00001444 return MadeChange;
Chris Lattner6eb30122010-02-23 05:51:07 +00001445 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001446
Chris Lattner6eb30122010-02-23 05:51:07 +00001447 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001448 bool MadeChange = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001449
Chris Lattner6cefb772008-01-05 22:25:12 +00001450 // Apply the result type to the node.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +00001451 unsigned NumRetVTs = Int->IS.RetVTs.size();
1452 unsigned NumParamVTs = Int->IS.ParamVTs.size();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001453
Bill Wendlingcdcc3e62008-11-13 09:08:33 +00001454 for (unsigned i = 0, e = NumRetVTs; i != e; ++i)
Chris Lattnerd7349192010-03-19 21:37:09 +00001455 MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +00001456
Chris Lattnerd7349192010-03-19 21:37:09 +00001457 if (getNumChildren() != NumParamVTs + 1)
Chris Lattnere67bde52008-01-06 05:36:50 +00001458 TP.error("Intrinsic '" + Int->Name + "' expects " +
Chris Lattnerd7349192010-03-19 21:37:09 +00001459 utostr(NumParamVTs) + " operands, not " +
Bill Wendlingcdcc3e62008-11-13 09:08:33 +00001460 utostr(getNumChildren() - 1) + " operands!");
Chris Lattner6cefb772008-01-05 22:25:12 +00001461
1462 // Apply type info to the intrinsic ID.
Chris Lattnerd7349192010-03-19 21:37:09 +00001463 MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001464
Chris Lattnerd7349192010-03-19 21:37:09 +00001465 for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) {
1466 MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001467
Chris Lattnerd7349192010-03-19 21:37:09 +00001468 MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i];
1469 assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case");
1470 MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001471 }
1472 return MadeChange;
Chris Lattner6eb30122010-02-23 05:51:07 +00001473 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001474
Chris Lattner6eb30122010-02-23 05:51:07 +00001475 if (getOperator()->isSubClassOf("SDNode")) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001476 const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001477
Chris Lattner2a22cdc2010-03-28 08:48:47 +00001478 // Check that the number of operands is sane. Negative operands -> varargs.
1479 if (NI.getNumOperands() >= 0 &&
1480 getNumChildren() != (unsigned)NI.getNumOperands())
1481 TP.error(getOperator()->getName() + " node requires exactly " +
1482 itostr(NI.getNumOperands()) + " operands!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001483
Chris Lattner6cefb772008-01-05 22:25:12 +00001484 bool MadeChange = NI.ApplyTypeConstraints(this, TP);
1485 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1486 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
Chris Lattnerd7349192010-03-19 21:37:09 +00001487 return MadeChange;
Chris Lattner6eb30122010-02-23 05:51:07 +00001488 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001489
Chris Lattner6eb30122010-02-23 05:51:07 +00001490 if (getOperator()->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001491 const DAGInstruction &Inst = CDP.getInstruction(getOperator());
Chris Lattner6cefb772008-01-05 22:25:12 +00001492 CodeGenInstruction &InstInfo =
Chris Lattnerf30187a2010-03-19 00:07:20 +00001493 CDP.getTargetInfo().getInstruction(getOperator());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001494
Chris Lattner0be6fe72010-03-27 19:15:02 +00001495 bool MadeChange = false;
1496
1497 // Apply the result types to the node, these come from the things in the
1498 // (outs) list of the instruction.
1499 // FIXME: Cap at one result so far.
Chris Lattnerc240bb02010-11-01 04:03:32 +00001500 unsigned NumResultsToAdd = InstInfo.Operands.NumDefs ? 1 : 0;
Chris Lattner0be6fe72010-03-27 19:15:02 +00001501 for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo) {
1502 Record *ResultNode = Inst.getResult(ResNo);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001503
Chris Lattnera938ac62009-07-29 20:43:05 +00001504 if (ResultNode->isSubClassOf("PointerLikeRegClass")) {
Chris Lattner0be6fe72010-03-27 19:15:02 +00001505 MadeChange |= UpdateNodeType(ResNo, MVT::iPTR, TP);
Christopher Lamb5b415372008-03-11 09:33:47 +00001506 } else if (ResultNode->getName() == "unknown") {
Chris Lattner2cacec52010-03-15 06:00:16 +00001507 // Nothing to do.
Chris Lattner6cefb772008-01-05 22:25:12 +00001508 } else {
1509 assert(ResultNode->isSubClassOf("RegisterClass") &&
1510 "Operands should be register classes!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001511 const CodeGenRegisterClass &RC =
Chris Lattner6cefb772008-01-05 22:25:12 +00001512 CDP.getTargetInfo().getRegisterClass(ResultNode);
Chris Lattner0be6fe72010-03-27 19:15:02 +00001513 MadeChange |= UpdateNodeType(ResNo, RC.getValueTypes(), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001514 }
Chris Lattner0be6fe72010-03-27 19:15:02 +00001515 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001516
Chris Lattner0be6fe72010-03-27 19:15:02 +00001517 // If the instruction has implicit defs, we apply the first one as a result.
1518 // FIXME: This sucks, it should apply all implicit defs.
1519 if (!InstInfo.ImplicitDefs.empty()) {
1520 unsigned ResNo = NumResultsToAdd;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001521
Chris Lattner9414ae52010-03-27 20:09:24 +00001522 // FIXME: Generalize to multiple possible types and multiple possible
1523 // ImplicitDefs.
1524 MVT::SimpleValueType VT =
1525 InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001526
Chris Lattner9414ae52010-03-27 20:09:24 +00001527 if (VT != MVT::Other)
1528 MadeChange |= UpdateNodeType(ResNo, VT, TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001529 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001530
Chris Lattner2cacec52010-03-15 06:00:16 +00001531 // If this is an INSERT_SUBREG, constrain the source and destination VTs to
1532 // be the same.
1533 if (getOperator()->getName() == "INSERT_SUBREG") {
Chris Lattnerd7349192010-03-19 21:37:09 +00001534 assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled");
1535 MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP);
1536 MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP);
Chris Lattner2cacec52010-03-15 06:00:16 +00001537 }
Chris Lattner6cefb772008-01-05 22:25:12 +00001538
1539 unsigned ChildNo = 0;
1540 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
1541 Record *OperandNode = Inst.getOperand(i);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001542
Chris Lattner6cefb772008-01-05 22:25:12 +00001543 // If the instruction expects a predicate or optional def operand, we
1544 // codegen this by setting the operand to it's default value if it has a
1545 // non-empty DefaultOps field.
1546 if ((OperandNode->isSubClassOf("PredicateOperand") ||
1547 OperandNode->isSubClassOf("OptionalDefOperand")) &&
1548 !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1549 continue;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001550
Chris Lattner6cefb772008-01-05 22:25:12 +00001551 // Verify that we didn't run out of provided operands.
1552 if (ChildNo >= getNumChildren())
1553 TP.error("Instruction '" + getOperator()->getName() +
1554 "' expects more operands than were provided.");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001555
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 MVT::SimpleValueType VT;
Chris Lattner6cefb772008-01-05 22:25:12 +00001557 TreePatternNode *Child = getChild(ChildNo++);
Chris Lattner0be6fe72010-03-27 19:15:02 +00001558 unsigned ChildResNo = 0; // Instructions always use res #0 of their op.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001559
Chris Lattner6cefb772008-01-05 22:25:12 +00001560 if (OperandNode->isSubClassOf("RegisterClass")) {
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001561 const CodeGenRegisterClass &RC =
Chris Lattner6cefb772008-01-05 22:25:12 +00001562 CDP.getTargetInfo().getRegisterClass(OperandNode);
Chris Lattner0be6fe72010-03-27 19:15:02 +00001563 MadeChange |= Child->UpdateNodeType(ChildResNo, RC.getValueTypes(), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +00001564 } else if (OperandNode->isSubClassOf("Operand")) {
1565 VT = getValueType(OperandNode->getValueAsDef("Type"));
Chris Lattner0be6fe72010-03-27 19:15:02 +00001566 MadeChange |= Child->UpdateNodeType(ChildResNo, VT, TP);
Chris Lattnera938ac62009-07-29 20:43:05 +00001567 } else if (OperandNode->isSubClassOf("PointerLikeRegClass")) {
Chris Lattner0be6fe72010-03-27 19:15:02 +00001568 MadeChange |= Child->UpdateNodeType(ChildResNo, MVT::iPTR, TP);
Christopher Lamb5b415372008-03-11 09:33:47 +00001569 } else if (OperandNode->getName() == "unknown") {
Chris Lattner2cacec52010-03-15 06:00:16 +00001570 // Nothing to do.
Chris Lattner6cefb772008-01-05 22:25:12 +00001571 } else {
1572 assert(0 && "Unknown operand type!");
1573 abort();
1574 }
1575 MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters);
1576 }
Christopher Lamb5b415372008-03-11 09:33:47 +00001577
Christopher Lamb02f69372008-03-10 04:16:09 +00001578 if (ChildNo != getNumChildren())
Chris Lattner6cefb772008-01-05 22:25:12 +00001579 TP.error("Instruction '" + getOperator()->getName() +
1580 "' was provided too many operands!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001581
Chris Lattner6cefb772008-01-05 22:25:12 +00001582 return MadeChange;
Chris Lattner6cefb772008-01-05 22:25:12 +00001583 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001584
Chris Lattner6eb30122010-02-23 05:51:07 +00001585 assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001586
Chris Lattner6eb30122010-02-23 05:51:07 +00001587 // Node transforms always take one operand.
1588 if (getNumChildren() != 1)
1589 TP.error("Node transform '" + getOperator()->getName() +
1590 "' requires one operand!");
1591
Chris Lattner2cacec52010-03-15 06:00:16 +00001592 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
1593
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001594
Chris Lattner6eb30122010-02-23 05:51:07 +00001595 // If either the output or input of the xform does not have exact
1596 // type info. We assume they must be the same. Otherwise, it is perfectly
1597 // legal to transform from one type to a completely different type.
Chris Lattner2cacec52010-03-15 06:00:16 +00001598#if 0
Chris Lattner6eb30122010-02-23 05:51:07 +00001599 if (!hasTypeSet() || !getChild(0)->hasTypeSet()) {
Chris Lattner2cacec52010-03-15 06:00:16 +00001600 bool MadeChange = UpdateNodeType(getChild(0)->getExtType(), TP);
1601 MadeChange |= getChild(0)->UpdateNodeType(getExtType(), TP);
Chris Lattner6eb30122010-02-23 05:51:07 +00001602 return MadeChange;
1603 }
Chris Lattner2cacec52010-03-15 06:00:16 +00001604#endif
1605 return MadeChange;
Chris Lattner6cefb772008-01-05 22:25:12 +00001606}
1607
1608/// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
1609/// RHS of a commutative operation, not the on LHS.
1610static bool OnlyOnRHSOfCommutative(TreePatternNode *N) {
1611 if (!N->isLeaf() && N->getOperator()->getName() == "imm")
1612 return true;
1613 if (N->isLeaf() && dynamic_cast<IntInit*>(N->getLeafValue()))
1614 return true;
1615 return false;
1616}
1617
1618
1619/// canPatternMatch - If it is impossible for this pattern to match on this
1620/// target, fill in Reason and return false. Otherwise, return true. This is
Jim Grosbachda4231f2009-03-26 16:17:51 +00001621/// used as a sanity check for .td files (to prevent people from writing stuff
Chris Lattner6cefb772008-01-05 22:25:12 +00001622/// that can never possibly work), and to prevent the pattern permuter from
1623/// generating stuff that is useless.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001624bool TreePatternNode::canPatternMatch(std::string &Reason,
Dan Gohmanee4fa192008-04-03 00:02:49 +00001625 const CodeGenDAGPatterns &CDP) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001626 if (isLeaf()) return true;
1627
1628 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1629 if (!getChild(i)->canPatternMatch(Reason, CDP))
1630 return false;
1631
1632 // If this is an intrinsic, handle cases that would make it not match. For
1633 // example, if an operand is required to be an immediate.
1634 if (getOperator()->isSubClassOf("Intrinsic")) {
1635 // TODO:
1636 return true;
1637 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001638
Chris Lattner6cefb772008-01-05 22:25:12 +00001639 // If this node is a commutative operator, check that the LHS isn't an
1640 // immediate.
1641 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator());
Evan Cheng6bd95672008-06-16 20:29:38 +00001642 bool isCommIntrinsic = isCommutativeIntrinsic(CDP);
1643 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001644 // Scan all of the operands of the node and make sure that only the last one
1645 // is a constant node, unless the RHS also is.
1646 if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) {
Evan Cheng6bd95672008-06-16 20:29:38 +00001647 bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id.
1648 for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +00001649 if (OnlyOnRHSOfCommutative(getChild(i))) {
1650 Reason="Immediate value must be on the RHS of commutative operators!";
1651 return false;
1652 }
1653 }
1654 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001655
Chris Lattner6cefb772008-01-05 22:25:12 +00001656 return true;
1657}
1658
1659//===----------------------------------------------------------------------===//
1660// TreePattern implementation
1661//
1662
1663TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +00001664 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){
Chris Lattner2cacec52010-03-15 06:00:16 +00001665 isInputPattern = isInput;
1666 for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i)
Chris Lattnerc2173052010-03-28 06:50:34 +00001667 Trees.push_back(ParseTreePattern(RawPat->getElement(i), ""));
Chris Lattner6cefb772008-01-05 22:25:12 +00001668}
1669
1670TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +00001671 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){
Chris Lattner6cefb772008-01-05 22:25:12 +00001672 isInputPattern = isInput;
Chris Lattnerc2173052010-03-28 06:50:34 +00001673 Trees.push_back(ParseTreePattern(Pat, ""));
Chris Lattner6cefb772008-01-05 22:25:12 +00001674}
1675
1676TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +00001677 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){
Chris Lattner6cefb772008-01-05 22:25:12 +00001678 isInputPattern = isInput;
1679 Trees.push_back(Pat);
1680}
1681
Chris Lattner6cefb772008-01-05 22:25:12 +00001682void TreePattern::error(const std::string &Msg) const {
1683 dump();
Chris Lattnera14b1de2009-03-13 16:25:21 +00001684 throw TGError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg);
Chris Lattner6cefb772008-01-05 22:25:12 +00001685}
1686
Chris Lattner2cacec52010-03-15 06:00:16 +00001687void TreePattern::ComputeNamedNodes() {
1688 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
1689 ComputeNamedNodes(Trees[i]);
1690}
1691
1692void TreePattern::ComputeNamedNodes(TreePatternNode *N) {
1693 if (!N->getName().empty())
1694 NamedNodes[N->getName()].push_back(N);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001695
Chris Lattner2cacec52010-03-15 06:00:16 +00001696 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
1697 ComputeNamedNodes(N->getChild(i));
1698}
1699
Chris Lattnerd7349192010-03-19 21:37:09 +00001700
Chris Lattnerc2173052010-03-28 06:50:34 +00001701TreePatternNode *TreePattern::ParseTreePattern(Init *TheInit, StringRef OpName){
1702 if (DefInit *DI = dynamic_cast<DefInit*>(TheInit)) {
1703 Record *R = DI->getDef();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001704
Chris Lattnerc2173052010-03-28 06:50:34 +00001705 // Direct reference to a leaf DagNode or PatFrag? Turn it into a
1706 // TreePatternNode if its own. For example:
1707 /// (foo GPR, imm) -> (foo GPR, (imm))
1708 if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag"))
1709 return ParseTreePattern(new DagInit(DI, "",
1710 std::vector<std::pair<Init*, std::string> >()),
1711 OpName);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001712
Chris Lattnerc2173052010-03-28 06:50:34 +00001713 // Input argument?
1714 TreePatternNode *Res = new TreePatternNode(DI, 1);
Chris Lattner2a22cdc2010-03-28 08:48:47 +00001715 if (R->getName() == "node" && !OpName.empty()) {
Chris Lattnerc2173052010-03-28 06:50:34 +00001716 if (OpName.empty())
1717 error("'node' argument requires a name to match with operand list");
1718 Args.push_back(OpName);
1719 }
1720
1721 Res->setName(OpName);
1722 return Res;
1723 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001724
Chris Lattnerc2173052010-03-28 06:50:34 +00001725 if (IntInit *II = dynamic_cast<IntInit*>(TheInit)) {
1726 if (!OpName.empty())
1727 error("Constant int argument should not have a name!");
1728 return new TreePatternNode(II, 1);
1729 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001730
Chris Lattnerc2173052010-03-28 06:50:34 +00001731 if (BitsInit *BI = dynamic_cast<BitsInit*>(TheInit)) {
1732 // Turn this into an IntInit.
1733 Init *II = BI->convertInitializerTo(new IntRecTy());
1734 if (II == 0 || !dynamic_cast<IntInit*>(II))
1735 error("Bits value must be constants!");
Chris Lattnerb775b1e2010-03-28 06:57:56 +00001736 return ParseTreePattern(II, OpName);
Chris Lattnerc2173052010-03-28 06:50:34 +00001737 }
1738
1739 DagInit *Dag = dynamic_cast<DagInit*>(TheInit);
1740 if (!Dag) {
1741 TheInit->dump();
1742 error("Pattern has unexpected init kind!");
1743 }
Chris Lattner6cefb772008-01-05 22:25:12 +00001744 DefInit *OpDef = dynamic_cast<DefInit*>(Dag->getOperator());
1745 if (!OpDef) error("Pattern has unexpected operator type!");
1746 Record *Operator = OpDef->getDef();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001747
Chris Lattner6cefb772008-01-05 22:25:12 +00001748 if (Operator->isSubClassOf("ValueType")) {
1749 // If the operator is a ValueType, then this must be "type cast" of a leaf
1750 // node.
1751 if (Dag->getNumArgs() != 1)
1752 error("Type cast only takes one operand!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001753
Chris Lattnerc2173052010-03-28 06:50:34 +00001754 TreePatternNode *New = ParseTreePattern(Dag->getArg(0), Dag->getArgName(0));
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001755
Chris Lattner6cefb772008-01-05 22:25:12 +00001756 // Apply the type cast.
Chris Lattnerd7349192010-03-19 21:37:09 +00001757 assert(New->getNumTypes() == 1 && "FIXME: Unhandled");
1758 New->UpdateNodeType(0, getValueType(Operator), *this);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001759
Chris Lattnerc2173052010-03-28 06:50:34 +00001760 if (!OpName.empty())
1761 error("ValueType cast should not have a name!");
Chris Lattner6cefb772008-01-05 22:25:12 +00001762 return New;
1763 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001764
Chris Lattner6cefb772008-01-05 22:25:12 +00001765 // Verify that this is something that makes sense for an operator.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001766 if (!Operator->isSubClassOf("PatFrag") &&
Nate Begeman7cee8172009-03-19 05:21:56 +00001767 !Operator->isSubClassOf("SDNode") &&
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001768 !Operator->isSubClassOf("Instruction") &&
Chris Lattner6cefb772008-01-05 22:25:12 +00001769 !Operator->isSubClassOf("SDNodeXForm") &&
1770 !Operator->isSubClassOf("Intrinsic") &&
1771 Operator->getName() != "set" &&
Chris Lattner310adf12010-03-27 02:53:27 +00001772 Operator->getName() != "implicit")
Chris Lattner6cefb772008-01-05 22:25:12 +00001773 error("Unrecognized node '" + Operator->getName() + "'!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001774
Chris Lattner6cefb772008-01-05 22:25:12 +00001775 // Check to see if this is something that is illegal in an input pattern.
Chris Lattnerb775b1e2010-03-28 06:57:56 +00001776 if (isInputPattern) {
1777 if (Operator->isSubClassOf("Instruction") ||
1778 Operator->isSubClassOf("SDNodeXForm"))
1779 error("Cannot use '" + Operator->getName() + "' in an input pattern!");
1780 } else {
1781 if (Operator->isSubClassOf("Intrinsic"))
1782 error("Cannot use '" + Operator->getName() + "' in an output pattern!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001783
Chris Lattnerb775b1e2010-03-28 06:57:56 +00001784 if (Operator->isSubClassOf("SDNode") &&
1785 Operator->getName() != "imm" &&
1786 Operator->getName() != "fpimm" &&
1787 Operator->getName() != "tglobaltlsaddr" &&
1788 Operator->getName() != "tconstpool" &&
1789 Operator->getName() != "tjumptable" &&
1790 Operator->getName() != "tframeindex" &&
1791 Operator->getName() != "texternalsym" &&
1792 Operator->getName() != "tblockaddress" &&
1793 Operator->getName() != "tglobaladdr" &&
1794 Operator->getName() != "bb" &&
1795 Operator->getName() != "vt")
1796 error("Cannot use '" + Operator->getName() + "' in an output pattern!");
1797 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001798
Chris Lattner6cefb772008-01-05 22:25:12 +00001799 std::vector<TreePatternNode*> Children;
Chris Lattnerc2173052010-03-28 06:50:34 +00001800
1801 // Parse all the operands.
1802 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i)
1803 Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgName(i)));
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001804
Chris Lattner6cefb772008-01-05 22:25:12 +00001805 // If the operator is an intrinsic, then this is just syntactic sugar for for
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001806 // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and
Chris Lattner6cefb772008-01-05 22:25:12 +00001807 // convert the intrinsic name to a number.
1808 if (Operator->isSubClassOf("Intrinsic")) {
1809 const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator);
1810 unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1;
1811
1812 // If this intrinsic returns void, it must have side-effects and thus a
1813 // chain.
Chris Lattnerc2173052010-03-28 06:50:34 +00001814 if (Int.IS.RetVTs.empty())
Chris Lattner6cefb772008-01-05 22:25:12 +00001815 Operator = getDAGPatterns().get_intrinsic_void_sdnode();
Chris Lattnerc2173052010-03-28 06:50:34 +00001816 else if (Int.ModRef != CodeGenIntrinsic::NoMem)
Chris Lattner6cefb772008-01-05 22:25:12 +00001817 // Has side-effects, requires chain.
1818 Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode();
Chris Lattnerc2173052010-03-28 06:50:34 +00001819 else // Otherwise, no chain.
Chris Lattner6cefb772008-01-05 22:25:12 +00001820 Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001821
Chris Lattnerd7349192010-03-19 21:37:09 +00001822 TreePatternNode *IIDNode = new TreePatternNode(new IntInit(IID), 1);
Chris Lattner6cefb772008-01-05 22:25:12 +00001823 Children.insert(Children.begin(), IIDNode);
1824 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001825
Chris Lattnerd7349192010-03-19 21:37:09 +00001826 unsigned NumResults = GetNumNodeResults(Operator, CDP);
1827 TreePatternNode *Result = new TreePatternNode(Operator, Children, NumResults);
Chris Lattnerc2173052010-03-28 06:50:34 +00001828 Result->setName(OpName);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001829
Chris Lattnerc2173052010-03-28 06:50:34 +00001830 if (!Dag->getName().empty()) {
1831 assert(Result->getName().empty());
1832 Result->setName(Dag->getName());
1833 }
Nate Begeman7cee8172009-03-19 05:21:56 +00001834 return Result;
Chris Lattner6cefb772008-01-05 22:25:12 +00001835}
1836
Chris Lattner7a0eb912010-03-28 08:38:32 +00001837/// SimplifyTree - See if we can simplify this tree to eliminate something that
1838/// will never match in favor of something obvious that will. This is here
1839/// strictly as a convenience to target authors because it allows them to write
1840/// more type generic things and have useless type casts fold away.
1841///
1842/// This returns true if any change is made.
1843static bool SimplifyTree(TreePatternNode *&N) {
1844 if (N->isLeaf())
1845 return false;
1846
1847 // If we have a bitconvert with a resolved type and if the source and
1848 // destination types are the same, then the bitconvert is useless, remove it.
1849 if (N->getOperator()->getName() == "bitconvert" &&
Chris Lattner7a0eb912010-03-28 08:38:32 +00001850 N->getExtType(0).isConcrete() &&
1851 N->getExtType(0) == N->getChild(0)->getExtType(0) &&
1852 N->getName().empty()) {
1853 N = N->getChild(0);
1854 SimplifyTree(N);
1855 return true;
1856 }
1857
1858 // Walk all children.
1859 bool MadeChange = false;
1860 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
1861 TreePatternNode *Child = N->getChild(i);
1862 MadeChange |= SimplifyTree(Child);
1863 N->setChild(i, Child);
1864 }
1865 return MadeChange;
1866}
1867
1868
1869
Chris Lattner6cefb772008-01-05 22:25:12 +00001870/// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +00001871/// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +00001872/// otherwise. Throw an exception if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +00001873bool TreePattern::
1874InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) {
1875 if (NamedNodes.empty())
1876 ComputeNamedNodes();
1877
Chris Lattner6cefb772008-01-05 22:25:12 +00001878 bool MadeChange = true;
1879 while (MadeChange) {
1880 MadeChange = false;
Chris Lattner7a0eb912010-03-28 08:38:32 +00001881 for (unsigned i = 0, e = Trees.size(); i != e; ++i) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001882 MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false);
Chris Lattner7a0eb912010-03-28 08:38:32 +00001883 MadeChange |= SimplifyTree(Trees[i]);
1884 }
Chris Lattner2cacec52010-03-15 06:00:16 +00001885
1886 // If there are constraints on our named nodes, apply them.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001887 for (StringMap<SmallVector<TreePatternNode*,1> >::iterator
Chris Lattner2cacec52010-03-15 06:00:16 +00001888 I = NamedNodes.begin(), E = NamedNodes.end(); I != E; ++I) {
1889 SmallVectorImpl<TreePatternNode*> &Nodes = I->second;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001890
Chris Lattner2cacec52010-03-15 06:00:16 +00001891 // If we have input named node types, propagate their types to the named
1892 // values here.
1893 if (InNamedTypes) {
1894 // FIXME: Should be error?
1895 assert(InNamedTypes->count(I->getKey()) &&
1896 "Named node in output pattern but not input pattern?");
1897
1898 const SmallVectorImpl<TreePatternNode*> &InNodes =
1899 InNamedTypes->find(I->getKey())->second;
1900
1901 // The input types should be fully resolved by now.
1902 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
1903 // If this node is a register class, and it is the root of the pattern
1904 // then we're mapping something onto an input register. We allow
1905 // changing the type of the input register in this case. This allows
1906 // us to match things like:
1907 // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
1908 if (Nodes[i] == Trees[0] && Nodes[i]->isLeaf()) {
1909 DefInit *DI = dynamic_cast<DefInit*>(Nodes[i]->getLeafValue());
1910 if (DI && DI->getDef()->isSubClassOf("RegisterClass"))
1911 continue;
1912 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001913
Daniel Dunbar32f6a8b2010-03-21 01:38:21 +00001914 assert(Nodes[i]->getNumTypes() == 1 &&
Chris Lattnerd7349192010-03-19 21:37:09 +00001915 InNodes[0]->getNumTypes() == 1 &&
1916 "FIXME: cannot name multiple result nodes yet");
1917 MadeChange |= Nodes[i]->UpdateNodeType(0, InNodes[0]->getExtType(0),
1918 *this);
Chris Lattner2cacec52010-03-15 06:00:16 +00001919 }
1920 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001921
Chris Lattner2cacec52010-03-15 06:00:16 +00001922 // If there are multiple nodes with the same name, they must all have the
1923 // same type.
1924 if (I->second.size() > 1) {
1925 for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) {
Chris Lattnerd7349192010-03-19 21:37:09 +00001926 TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1];
Daniel Dunbar32f6a8b2010-03-21 01:38:21 +00001927 assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&
Chris Lattnerd7349192010-03-19 21:37:09 +00001928 "FIXME: cannot name multiple result nodes yet");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001929
Chris Lattnerd7349192010-03-19 21:37:09 +00001930 MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this);
1931 MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this);
Chris Lattner2cacec52010-03-15 06:00:16 +00001932 }
1933 }
1934 }
Chris Lattner6cefb772008-01-05 22:25:12 +00001935 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001936
Chris Lattner6cefb772008-01-05 22:25:12 +00001937 bool HasUnresolvedTypes = false;
1938 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
1939 HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType();
1940 return !HasUnresolvedTypes;
1941}
1942
Daniel Dunbar1a551802009-07-03 00:10:29 +00001943void TreePattern::print(raw_ostream &OS) const {
Chris Lattner6cefb772008-01-05 22:25:12 +00001944 OS << getRecord()->getName();
1945 if (!Args.empty()) {
1946 OS << "(" << Args[0];
1947 for (unsigned i = 1, e = Args.size(); i != e; ++i)
1948 OS << ", " << Args[i];
1949 OS << ")";
1950 }
1951 OS << ": ";
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001952
Chris Lattner6cefb772008-01-05 22:25:12 +00001953 if (Trees.size() > 1)
1954 OS << "[\n";
1955 for (unsigned i = 0, e = Trees.size(); i != e; ++i) {
1956 OS << "\t";
1957 Trees[i]->print(OS);
1958 OS << "\n";
1959 }
1960
1961 if (Trees.size() > 1)
1962 OS << "]\n";
1963}
1964
Daniel Dunbar1a551802009-07-03 00:10:29 +00001965void TreePattern::dump() const { print(errs()); }
Chris Lattner6cefb772008-01-05 22:25:12 +00001966
1967//===----------------------------------------------------------------------===//
Chris Lattnerfe718932008-01-06 01:10:31 +00001968// CodeGenDAGPatterns implementation
Chris Lattner6cefb772008-01-05 22:25:12 +00001969//
1970
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001971CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) :
Chris Lattner67db8832010-12-13 00:23:57 +00001972 Records(R), Target(R) {
1973
Dale Johannesen49de9822009-02-05 01:49:45 +00001974 Intrinsics = LoadIntrinsics(Records, false);
1975 TgtIntrinsics = LoadIntrinsics(Records, true);
Chris Lattner6cefb772008-01-05 22:25:12 +00001976 ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +00001977 ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +00001978 ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +00001979 ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +00001980 ParseDefaultOperands();
1981 ParseInstructions();
1982 ParsePatterns();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00001983
Chris Lattner6cefb772008-01-05 22:25:12 +00001984 // Generate variants. For example, commutative patterns can match
1985 // multiple ways. Add them to PatternsToMatch as well.
1986 GenerateVariants();
Dan Gohmanee4fa192008-04-03 00:02:49 +00001987
1988 // Infer instruction flags. For example, we can detect loads,
1989 // stores, and side effects in many cases by examining an
1990 // instruction's pattern.
1991 InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +00001992}
1993
Chris Lattnerfe718932008-01-06 01:10:31 +00001994CodeGenDAGPatterns::~CodeGenDAGPatterns() {
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +00001995 for (pf_iterator I = PatternFragments.begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001996 E = PatternFragments.end(); I != E; ++I)
1997 delete I->second;
1998}
1999
2000
Chris Lattnerfe718932008-01-06 01:10:31 +00002001Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const {
Chris Lattner6cefb772008-01-05 22:25:12 +00002002 Record *N = Records.getDef(Name);
2003 if (!N || !N->isSubClassOf("SDNode")) {
Daniel Dunbar1a551802009-07-03 00:10:29 +00002004 errs() << "Error getting SDNode '" << Name << "'!\n";
Chris Lattner6cefb772008-01-05 22:25:12 +00002005 exit(1);
2006 }
2007 return N;
2008}
2009
2010// Parse all of the SDNode definitions for the target, populating SDNodes.
Chris Lattnerfe718932008-01-06 01:10:31 +00002011void CodeGenDAGPatterns::ParseNodeInfo() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002012 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode");
2013 while (!Nodes.empty()) {
2014 SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back()));
2015 Nodes.pop_back();
2016 }
2017
Jim Grosbachda4231f2009-03-26 16:17:51 +00002018 // Get the builtin intrinsic nodes.
Chris Lattner6cefb772008-01-05 22:25:12 +00002019 intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void");
2020 intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain");
2021 intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain");
2022}
2023
2024/// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
2025/// map, and emit them to the file as functions.
Chris Lattnerfe718932008-01-06 01:10:31 +00002026void CodeGenDAGPatterns::ParseNodeTransforms() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002027 std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm");
2028 while (!Xforms.empty()) {
2029 Record *XFormNode = Xforms.back();
2030 Record *SDNode = XFormNode->getValueAsDef("Opcode");
2031 std::string Code = XFormNode->getValueAsCode("XFormFunction");
Chris Lattner443e3f92008-01-05 22:54:53 +00002032 SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code)));
Chris Lattner6cefb772008-01-05 22:25:12 +00002033
2034 Xforms.pop_back();
2035 }
2036}
2037
Chris Lattnerfe718932008-01-06 01:10:31 +00002038void CodeGenDAGPatterns::ParseComplexPatterns() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002039 std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern");
2040 while (!AMs.empty()) {
2041 ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back()));
2042 AMs.pop_back();
2043 }
2044}
2045
2046
2047/// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
2048/// file, building up the PatternFragments map. After we've collected them all,
2049/// inline fragments together as necessary, so that there are no references left
2050/// inside a pattern fragment to a pattern fragment.
2051///
Chris Lattnerfe718932008-01-06 01:10:31 +00002052void CodeGenDAGPatterns::ParsePatternFragments() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002053 std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002054
Chris Lattnerdc32f982008-01-05 22:43:57 +00002055 // First step, parse all of the fragments.
Chris Lattner6cefb772008-01-05 22:25:12 +00002056 for (unsigned i = 0, e = Fragments.size(); i != e; ++i) {
2057 DagInit *Tree = Fragments[i]->getValueAsDag("Fragment");
2058 TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this);
2059 PatternFragments[Fragments[i]] = P;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002060
Chris Lattnerdc32f982008-01-05 22:43:57 +00002061 // Validate the argument list, converting it to set, to discard duplicates.
Chris Lattner6cefb772008-01-05 22:25:12 +00002062 std::vector<std::string> &Args = P->getArgList();
Chris Lattnerdc32f982008-01-05 22:43:57 +00002063 std::set<std::string> OperandsSet(Args.begin(), Args.end());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002064
Chris Lattnerdc32f982008-01-05 22:43:57 +00002065 if (OperandsSet.count(""))
Chris Lattner6cefb772008-01-05 22:25:12 +00002066 P->error("Cannot have unnamed 'node' values in pattern fragment!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002067
Chris Lattner6cefb772008-01-05 22:25:12 +00002068 // Parse the operands list.
2069 DagInit *OpsList = Fragments[i]->getValueAsDag("Operands");
2070 DefInit *OpsOp = dynamic_cast<DefInit*>(OpsList->getOperator());
2071 // Special cases: ops == outs == ins. Different names are used to
Jim Grosbachda4231f2009-03-26 16:17:51 +00002072 // improve readability.
Chris Lattner6cefb772008-01-05 22:25:12 +00002073 if (!OpsOp ||
2074 (OpsOp->getDef()->getName() != "ops" &&
2075 OpsOp->getDef()->getName() != "outs" &&
2076 OpsOp->getDef()->getName() != "ins"))
2077 P->error("Operands list should start with '(ops ... '!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002078
2079 // Copy over the arguments.
Chris Lattner6cefb772008-01-05 22:25:12 +00002080 Args.clear();
2081 for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) {
2082 if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) ||
2083 static_cast<DefInit*>(OpsList->getArg(j))->
2084 getDef()->getName() != "node")
2085 P->error("Operands list should all be 'node' values.");
2086 if (OpsList->getArgName(j).empty())
2087 P->error("Operands list should have names for each operand!");
Chris Lattnerdc32f982008-01-05 22:43:57 +00002088 if (!OperandsSet.count(OpsList->getArgName(j)))
Chris Lattner6cefb772008-01-05 22:25:12 +00002089 P->error("'" + OpsList->getArgName(j) +
2090 "' does not occur in pattern or was multiply specified!");
Chris Lattnerdc32f982008-01-05 22:43:57 +00002091 OperandsSet.erase(OpsList->getArgName(j));
Chris Lattner6cefb772008-01-05 22:25:12 +00002092 Args.push_back(OpsList->getArgName(j));
2093 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002094
Chris Lattnerdc32f982008-01-05 22:43:57 +00002095 if (!OperandsSet.empty())
Chris Lattner6cefb772008-01-05 22:25:12 +00002096 P->error("Operands list does not contain an entry for operand '" +
Chris Lattnerdc32f982008-01-05 22:43:57 +00002097 *OperandsSet.begin() + "'!");
Chris Lattner6cefb772008-01-05 22:25:12 +00002098
Chris Lattnerdc32f982008-01-05 22:43:57 +00002099 // If there is a code init for this fragment, keep track of the fact that
2100 // this fragment uses it.
Chris Lattner54379062011-04-17 21:38:24 +00002101 TreePredicateFn PredFn(P);
2102 if (!PredFn.isAlwaysTrue())
2103 P->getOnlyTree()->addPredicateFn(PredFn);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002104
Chris Lattner6cefb772008-01-05 22:25:12 +00002105 // If there is a node transformation corresponding to this, keep track of
2106 // it.
2107 Record *Transform = Fragments[i]->getValueAsDef("OperandTransform");
2108 if (!getSDNodeTransform(Transform).second.empty()) // not noop xform?
2109 P->getOnlyTree()->setTransformFn(Transform);
2110 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002111
Chris Lattner6cefb772008-01-05 22:25:12 +00002112 // Now that we've parsed all of the tree fragments, do a closure on them so
2113 // that there are not references to PatFrags left inside of them.
Chris Lattner2ca698d2008-06-30 03:02:03 +00002114 for (unsigned i = 0, e = Fragments.size(); i != e; ++i) {
2115 TreePattern *ThePat = PatternFragments[Fragments[i]];
Chris Lattner6cefb772008-01-05 22:25:12 +00002116 ThePat->InlinePatternFragments();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002117
Chris Lattner6cefb772008-01-05 22:25:12 +00002118 // Infer as many types as possible. Don't worry about it if we don't infer
2119 // all of them, some may depend on the inputs of the pattern.
2120 try {
2121 ThePat->InferAllTypes();
2122 } catch (...) {
2123 // If this pattern fragment is not supported by this target (no types can
2124 // satisfy its constraints), just ignore it. If the bogus pattern is
2125 // actually used by instructions, the type consistency error will be
2126 // reported there.
2127 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002128
Chris Lattner6cefb772008-01-05 22:25:12 +00002129 // If debugging, print out the pattern fragment result.
2130 DEBUG(ThePat->dump());
2131 }
2132}
2133
Chris Lattnerfe718932008-01-06 01:10:31 +00002134void CodeGenDAGPatterns::ParseDefaultOperands() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002135 std::vector<Record*> DefaultOps[2];
2136 DefaultOps[0] = Records.getAllDerivedDefinitions("PredicateOperand");
2137 DefaultOps[1] = Records.getAllDerivedDefinitions("OptionalDefOperand");
2138
2139 // Find some SDNode.
2140 assert(!SDNodes.empty() && "No SDNodes parsed?");
2141 Init *SomeSDNode = new DefInit(SDNodes.begin()->first);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002142
Chris Lattner6cefb772008-01-05 22:25:12 +00002143 for (unsigned iter = 0; iter != 2; ++iter) {
2144 for (unsigned i = 0, e = DefaultOps[iter].size(); i != e; ++i) {
2145 DagInit *DefaultInfo = DefaultOps[iter][i]->getValueAsDag("DefaultOps");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002146
Chris Lattner6cefb772008-01-05 22:25:12 +00002147 // Clone the DefaultInfo dag node, changing the operator from 'ops' to
2148 // SomeSDnode so that we can parse this.
2149 std::vector<std::pair<Init*, std::string> > Ops;
2150 for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op)
2151 Ops.push_back(std::make_pair(DefaultInfo->getArg(op),
2152 DefaultInfo->getArgName(op)));
Nate Begeman7cee8172009-03-19 05:21:56 +00002153 DagInit *DI = new DagInit(SomeSDNode, "", Ops);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002154
Chris Lattner6cefb772008-01-05 22:25:12 +00002155 // Create a TreePattern to parse this.
2156 TreePattern P(DefaultOps[iter][i], DI, false, *this);
2157 assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!");
2158
2159 // Copy the operands over into a DAGDefaultOperand.
2160 DAGDefaultOperand DefaultOpInfo;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002161
Chris Lattner6cefb772008-01-05 22:25:12 +00002162 TreePatternNode *T = P.getTree(0);
2163 for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) {
2164 TreePatternNode *TPN = T->getChild(op);
2165 while (TPN->ApplyTypeConstraints(P, false))
2166 /* Resolve all types */;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002167
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00002168 if (TPN->ContainsUnresolvedType()) {
Chris Lattner6cefb772008-01-05 22:25:12 +00002169 if (iter == 0)
2170 throw "Value #" + utostr(i) + " of PredicateOperand '" +
Chris Lattner53d09bd2010-02-23 05:59:10 +00002171 DefaultOps[iter][i]->getName() +"' doesn't have a concrete type!";
Chris Lattner6cefb772008-01-05 22:25:12 +00002172 else
2173 throw "Value #" + utostr(i) + " of OptionalDefOperand '" +
Chris Lattner53d09bd2010-02-23 05:59:10 +00002174 DefaultOps[iter][i]->getName() +"' doesn't have a concrete type!";
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00002175 }
Chris Lattner6cefb772008-01-05 22:25:12 +00002176 DefaultOpInfo.DefaultOps.push_back(TPN);
2177 }
2178
2179 // Insert it into the DefaultOperands map so we can find it later.
2180 DefaultOperands[DefaultOps[iter][i]] = DefaultOpInfo;
2181 }
2182 }
2183}
2184
2185/// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
2186/// instruction input. Return true if this is a real use.
2187static bool HandleUse(TreePattern *I, TreePatternNode *Pat,
Chris Lattneracfb70f2010-04-20 06:30:25 +00002188 std::map<std::string, TreePatternNode*> &InstInputs) {
Chris Lattner6cefb772008-01-05 22:25:12 +00002189 // No name -> not interesting.
2190 if (Pat->getName().empty()) {
2191 if (Pat->isLeaf()) {
2192 DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue());
2193 if (DI && DI->getDef()->isSubClassOf("RegisterClass"))
2194 I->error("Input " + DI->getDef()->getName() + " must be named!");
Chris Lattner6cefb772008-01-05 22:25:12 +00002195 }
2196 return false;
2197 }
2198
2199 Record *Rec;
2200 if (Pat->isLeaf()) {
2201 DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue());
2202 if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!");
2203 Rec = DI->getDef();
2204 } else {
Chris Lattner6cefb772008-01-05 22:25:12 +00002205 Rec = Pat->getOperator();
2206 }
2207
2208 // SRCVALUE nodes are ignored.
2209 if (Rec->getName() == "srcvalue")
2210 return false;
2211
2212 TreePatternNode *&Slot = InstInputs[Pat->getName()];
2213 if (!Slot) {
2214 Slot = Pat;
Chris Lattner53d09bd2010-02-23 05:59:10 +00002215 return true;
Chris Lattner6cefb772008-01-05 22:25:12 +00002216 }
Chris Lattner53d09bd2010-02-23 05:59:10 +00002217 Record *SlotRec;
2218 if (Slot->isLeaf()) {
2219 SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef();
2220 } else {
2221 assert(Slot->getNumChildren() == 0 && "can't be a use with children!");
2222 SlotRec = Slot->getOperator();
2223 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002224
Chris Lattner53d09bd2010-02-23 05:59:10 +00002225 // Ensure that the inputs agree if we've already seen this input.
2226 if (Rec != SlotRec)
2227 I->error("All $" + Pat->getName() + " inputs must agree with each other");
Chris Lattnerd7349192010-03-19 21:37:09 +00002228 if (Slot->getExtTypes() != Pat->getExtTypes())
Chris Lattner53d09bd2010-02-23 05:59:10 +00002229 I->error("All $" + Pat->getName() + " inputs must agree with each other");
Chris Lattner6cefb772008-01-05 22:25:12 +00002230 return true;
2231}
2232
2233/// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is
2234/// part of "I", the instruction), computing the set of inputs and outputs of
2235/// the pattern. Report errors if we see anything naughty.
Chris Lattnerfe718932008-01-06 01:10:31 +00002236void CodeGenDAGPatterns::
Chris Lattner6cefb772008-01-05 22:25:12 +00002237FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
2238 std::map<std::string, TreePatternNode*> &InstInputs,
2239 std::map<std::string, TreePatternNode*>&InstResults,
Chris Lattner6cefb772008-01-05 22:25:12 +00002240 std::vector<Record*> &InstImpResults) {
2241 if (Pat->isLeaf()) {
Chris Lattneracfb70f2010-04-20 06:30:25 +00002242 bool isUse = HandleUse(I, Pat, InstInputs);
Chris Lattner6cefb772008-01-05 22:25:12 +00002243 if (!isUse && Pat->getTransformFn())
2244 I->error("Cannot specify a transform function for a non-input value!");
2245 return;
Chris Lattner84aa60b2010-02-17 06:53:36 +00002246 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002247
Chris Lattner84aa60b2010-02-17 06:53:36 +00002248 if (Pat->getOperator()->getName() == "implicit") {
Chris Lattner6cefb772008-01-05 22:25:12 +00002249 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
2250 TreePatternNode *Dest = Pat->getChild(i);
2251 if (!Dest->isLeaf())
2252 I->error("implicitly defined value should be a register!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002253
Chris Lattner6cefb772008-01-05 22:25:12 +00002254 DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue());
2255 if (!Val || !Val->getDef()->isSubClassOf("Register"))
2256 I->error("implicitly defined value should be a register!");
2257 InstImpResults.push_back(Val->getDef());
2258 }
2259 return;
Chris Lattner84aa60b2010-02-17 06:53:36 +00002260 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002261
Chris Lattner84aa60b2010-02-17 06:53:36 +00002262 if (Pat->getOperator()->getName() != "set") {
Chris Lattner6cefb772008-01-05 22:25:12 +00002263 // If this is not a set, verify that the children nodes are not void typed,
2264 // and recurse.
2265 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
Chris Lattnerd7349192010-03-19 21:37:09 +00002266 if (Pat->getChild(i)->getNumTypes() == 0)
Chris Lattner6cefb772008-01-05 22:25:12 +00002267 I->error("Cannot have void nodes inside of patterns!");
2268 FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults,
Chris Lattneracfb70f2010-04-20 06:30:25 +00002269 InstImpResults);
Chris Lattner6cefb772008-01-05 22:25:12 +00002270 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002271
Chris Lattner6cefb772008-01-05 22:25:12 +00002272 // If this is a non-leaf node with no children, treat it basically as if
2273 // it were a leaf. This handles nodes like (imm).
Chris Lattneracfb70f2010-04-20 06:30:25 +00002274 bool isUse = HandleUse(I, Pat, InstInputs);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002275
Chris Lattner6cefb772008-01-05 22:25:12 +00002276 if (!isUse && Pat->getTransformFn())
2277 I->error("Cannot specify a transform function for a non-input value!");
2278 return;
Chris Lattner84aa60b2010-02-17 06:53:36 +00002279 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002280
Chris Lattner6cefb772008-01-05 22:25:12 +00002281 // Otherwise, this is a set, validate and collect instruction results.
2282 if (Pat->getNumChildren() == 0)
2283 I->error("set requires operands!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002284
Chris Lattner6cefb772008-01-05 22:25:12 +00002285 if (Pat->getTransformFn())
2286 I->error("Cannot specify a transform function on a set node!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002287
Chris Lattner6cefb772008-01-05 22:25:12 +00002288 // Check the set destinations.
2289 unsigned NumDests = Pat->getNumChildren()-1;
2290 for (unsigned i = 0; i != NumDests; ++i) {
2291 TreePatternNode *Dest = Pat->getChild(i);
2292 if (!Dest->isLeaf())
2293 I->error("set destination should be a register!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002294
Chris Lattner6cefb772008-01-05 22:25:12 +00002295 DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue());
2296 if (!Val)
2297 I->error("set destination should be a register!");
2298
2299 if (Val->getDef()->isSubClassOf("RegisterClass") ||
Chris Lattnera938ac62009-07-29 20:43:05 +00002300 Val->getDef()->isSubClassOf("PointerLikeRegClass")) {
Chris Lattner6cefb772008-01-05 22:25:12 +00002301 if (Dest->getName().empty())
2302 I->error("set destination must have a name!");
2303 if (InstResults.count(Dest->getName()))
2304 I->error("cannot set '" + Dest->getName() +"' multiple times");
2305 InstResults[Dest->getName()] = Dest;
2306 } else if (Val->getDef()->isSubClassOf("Register")) {
2307 InstImpResults.push_back(Val->getDef());
2308 } else {
2309 I->error("set destination should be a register!");
2310 }
2311 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002312
Chris Lattner6cefb772008-01-05 22:25:12 +00002313 // Verify and collect info from the computation.
2314 FindPatternInputsAndOutputs(I, Pat->getChild(NumDests),
Chris Lattneracfb70f2010-04-20 06:30:25 +00002315 InstInputs, InstResults, InstImpResults);
Chris Lattner6cefb772008-01-05 22:25:12 +00002316}
2317
Dan Gohmanee4fa192008-04-03 00:02:49 +00002318//===----------------------------------------------------------------------===//
2319// Instruction Analysis
2320//===----------------------------------------------------------------------===//
2321
2322class InstAnalyzer {
2323 const CodeGenDAGPatterns &CDP;
2324 bool &mayStore;
2325 bool &mayLoad;
Evan Cheng0f040a22011-03-15 05:09:26 +00002326 bool &IsBitcast;
Dan Gohmanee4fa192008-04-03 00:02:49 +00002327 bool &HasSideEffects;
Chris Lattner1e506312010-03-19 05:34:15 +00002328 bool &IsVariadic;
Dan Gohmanee4fa192008-04-03 00:02:49 +00002329public:
2330 InstAnalyzer(const CodeGenDAGPatterns &cdp,
Evan Cheng0f040a22011-03-15 05:09:26 +00002331 bool &maystore, bool &mayload, bool &isbc, bool &hse, bool &isv)
2332 : CDP(cdp), mayStore(maystore), mayLoad(mayload), IsBitcast(isbc),
2333 HasSideEffects(hse), IsVariadic(isv) {
Dan Gohmanee4fa192008-04-03 00:02:49 +00002334 }
2335
2336 /// Analyze - Analyze the specified instruction, returning true if the
2337 /// instruction had a pattern.
2338 bool Analyze(Record *InstRecord) {
2339 const TreePattern *Pattern = CDP.getInstruction(InstRecord).getPattern();
2340 if (Pattern == 0) {
2341 HasSideEffects = 1;
2342 return false; // No pattern.
2343 }
2344
2345 // FIXME: Assume only the first tree is the pattern. The others are clobber
2346 // nodes.
2347 AnalyzeNode(Pattern->getTree(0));
2348 return true;
2349 }
2350
2351private:
Evan Cheng0f040a22011-03-15 05:09:26 +00002352 bool IsNodeBitcast(const TreePatternNode *N) const {
2353 if (HasSideEffects || mayLoad || mayStore || IsVariadic)
2354 return false;
2355
2356 if (N->getNumChildren() != 2)
2357 return false;
2358
2359 const TreePatternNode *N0 = N->getChild(0);
2360 if (!N0->isLeaf() || !dynamic_cast<DefInit*>(N0->getLeafValue()))
2361 return false;
2362
2363 const TreePatternNode *N1 = N->getChild(1);
2364 if (N1->isLeaf())
2365 return false;
2366 if (N1->getNumChildren() != 1 || !N1->getChild(0)->isLeaf())
2367 return false;
2368
2369 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N1->getOperator());
2370 if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1)
2371 return false;
2372 return OpInfo.getEnumName() == "ISD::BITCAST";
2373 }
2374
Dan Gohmanee4fa192008-04-03 00:02:49 +00002375 void AnalyzeNode(const TreePatternNode *N) {
2376 if (N->isLeaf()) {
2377 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
2378 Record *LeafRec = DI->getDef();
2379 // Handle ComplexPattern leaves.
2380 if (LeafRec->isSubClassOf("ComplexPattern")) {
2381 const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
2382 if (CP.hasProperty(SDNPMayStore)) mayStore = true;
2383 if (CP.hasProperty(SDNPMayLoad)) mayLoad = true;
2384 if (CP.hasProperty(SDNPSideEffect)) HasSideEffects = true;
2385 }
2386 }
2387 return;
2388 }
2389
2390 // Analyze children.
2391 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2392 AnalyzeNode(N->getChild(i));
2393
2394 // Ignore set nodes, which are not SDNodes.
Evan Cheng0f040a22011-03-15 05:09:26 +00002395 if (N->getOperator()->getName() == "set") {
2396 IsBitcast = IsNodeBitcast(N);
Dan Gohmanee4fa192008-04-03 00:02:49 +00002397 return;
Evan Cheng0f040a22011-03-15 05:09:26 +00002398 }
Dan Gohmanee4fa192008-04-03 00:02:49 +00002399
2400 // Get information about the SDNode for the operator.
2401 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator());
2402
2403 // Notice properties of the node.
2404 if (OpInfo.hasProperty(SDNPMayStore)) mayStore = true;
2405 if (OpInfo.hasProperty(SDNPMayLoad)) mayLoad = true;
2406 if (OpInfo.hasProperty(SDNPSideEffect)) HasSideEffects = true;
Chris Lattner1e506312010-03-19 05:34:15 +00002407 if (OpInfo.hasProperty(SDNPVariadic)) IsVariadic = true;
Dan Gohmanee4fa192008-04-03 00:02:49 +00002408
2409 if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
2410 // If this is an intrinsic, analyze it.
2411 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadArgMem)
2412 mayLoad = true;// These may load memory.
2413
Dan Gohman7365c092010-08-05 23:36:21 +00002414 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteArgMem)
Dan Gohmanee4fa192008-04-03 00:02:49 +00002415 mayStore = true;// Intrinsics that can write to memory are 'mayStore'.
2416
Dan Gohman7365c092010-08-05 23:36:21 +00002417 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem)
Dan Gohmanee4fa192008-04-03 00:02:49 +00002418 // WriteMem intrinsics can have other strange effects.
2419 HasSideEffects = true;
2420 }
2421 }
2422
2423};
2424
2425static void InferFromPattern(const CodeGenInstruction &Inst,
2426 bool &MayStore, bool &MayLoad,
Evan Cheng0f040a22011-03-15 05:09:26 +00002427 bool &IsBitcast,
Chris Lattner1e506312010-03-19 05:34:15 +00002428 bool &HasSideEffects, bool &IsVariadic,
Dan Gohmanee4fa192008-04-03 00:02:49 +00002429 const CodeGenDAGPatterns &CDP) {
Evan Cheng0f040a22011-03-15 05:09:26 +00002430 MayStore = MayLoad = IsBitcast = HasSideEffects = IsVariadic = false;
Dan Gohmanee4fa192008-04-03 00:02:49 +00002431
2432 bool HadPattern =
Evan Cheng0f040a22011-03-15 05:09:26 +00002433 InstAnalyzer(CDP, MayStore, MayLoad, IsBitcast, HasSideEffects, IsVariadic)
Chris Lattner1e506312010-03-19 05:34:15 +00002434 .Analyze(Inst.TheDef);
Dan Gohmanee4fa192008-04-03 00:02:49 +00002435
2436 // InstAnalyzer only correctly analyzes mayStore/mayLoad so far.
2437 if (Inst.mayStore) { // If the .td file explicitly sets mayStore, use it.
2438 // If we decided that this is a store from the pattern, then the .td file
2439 // entry is redundant.
2440 if (MayStore)
2441 fprintf(stderr,
2442 "Warning: mayStore flag explicitly set on instruction '%s'"
2443 " but flag already inferred from pattern.\n",
2444 Inst.TheDef->getName().c_str());
2445 MayStore = true;
2446 }
2447
2448 if (Inst.mayLoad) { // If the .td file explicitly sets mayLoad, use it.
2449 // If we decided that this is a load from the pattern, then the .td file
2450 // entry is redundant.
2451 if (MayLoad)
2452 fprintf(stderr,
2453 "Warning: mayLoad flag explicitly set on instruction '%s'"
2454 " but flag already inferred from pattern.\n",
2455 Inst.TheDef->getName().c_str());
2456 MayLoad = true;
2457 }
2458
2459 if (Inst.neverHasSideEffects) {
2460 if (HadPattern)
2461 fprintf(stderr, "Warning: neverHasSideEffects set on instruction '%s' "
2462 "which already has a pattern\n", Inst.TheDef->getName().c_str());
2463 HasSideEffects = false;
2464 }
2465
2466 if (Inst.hasSideEffects) {
2467 if (HasSideEffects)
2468 fprintf(stderr, "Warning: hasSideEffects set on instruction '%s' "
2469 "which already inferred this.\n", Inst.TheDef->getName().c_str());
2470 HasSideEffects = true;
2471 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002472
Chris Lattnerc240bb02010-11-01 04:03:32 +00002473 if (Inst.Operands.isVariadic)
Chris Lattner1e506312010-03-19 05:34:15 +00002474 IsVariadic = true; // Can warn if we want.
Dan Gohmanee4fa192008-04-03 00:02:49 +00002475}
2476
Chris Lattner6cefb772008-01-05 22:25:12 +00002477/// ParseInstructions - Parse all of the instructions, inlining and resolving
2478/// any fragments involved. This populates the Instructions list with fully
2479/// resolved instructions.
Chris Lattnerfe718932008-01-06 01:10:31 +00002480void CodeGenDAGPatterns::ParseInstructions() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002481 std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002482
Chris Lattner6cefb772008-01-05 22:25:12 +00002483 for (unsigned i = 0, e = Instrs.size(); i != e; ++i) {
2484 ListInit *LI = 0;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002485
Chris Lattner6cefb772008-01-05 22:25:12 +00002486 if (dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern")))
2487 LI = Instrs[i]->getValueAsListInit("Pattern");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002488
Chris Lattner6cefb772008-01-05 22:25:12 +00002489 // If there is no pattern, only collect minimal information about the
2490 // instruction for its operand list. We have to assume that there is one
2491 // result, as we have no detailed info.
2492 if (!LI || LI->getSize() == 0) {
2493 std::vector<Record*> Results;
2494 std::vector<Record*> Operands;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002495
Chris Lattnerf30187a2010-03-19 00:07:20 +00002496 CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]);
Chris Lattner6cefb772008-01-05 22:25:12 +00002497
Chris Lattnerc240bb02010-11-01 04:03:32 +00002498 if (InstInfo.Operands.size() != 0) {
2499 if (InstInfo.Operands.NumDefs == 0) {
Chris Lattner6cefb772008-01-05 22:25:12 +00002500 // These produce no results
Chris Lattnerc240bb02010-11-01 04:03:32 +00002501 for (unsigned j = 0, e = InstInfo.Operands.size(); j < e; ++j)
2502 Operands.push_back(InstInfo.Operands[j].Rec);
Chris Lattner6cefb772008-01-05 22:25:12 +00002503 } else {
2504 // Assume the first operand is the result.
Chris Lattnerc240bb02010-11-01 04:03:32 +00002505 Results.push_back(InstInfo.Operands[0].Rec);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002506
Chris Lattner6cefb772008-01-05 22:25:12 +00002507 // The rest are inputs.
Chris Lattnerc240bb02010-11-01 04:03:32 +00002508 for (unsigned j = 1, e = InstInfo.Operands.size(); j < e; ++j)
2509 Operands.push_back(InstInfo.Operands[j].Rec);
Chris Lattner6cefb772008-01-05 22:25:12 +00002510 }
2511 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002512
Chris Lattner6cefb772008-01-05 22:25:12 +00002513 // Create and insert the instruction.
2514 std::vector<Record*> ImpResults;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002515 Instructions.insert(std::make_pair(Instrs[i],
Chris Lattner62bcec82010-04-20 06:28:43 +00002516 DAGInstruction(0, Results, Operands, ImpResults)));
Chris Lattner6cefb772008-01-05 22:25:12 +00002517 continue; // no pattern.
2518 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002519
Chris Lattner6cefb772008-01-05 22:25:12 +00002520 // Parse the instruction.
2521 TreePattern *I = new TreePattern(Instrs[i], LI, true, *this);
2522 // Inline pattern fragments into it.
2523 I->InlinePatternFragments();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002524
Chris Lattner6cefb772008-01-05 22:25:12 +00002525 // Infer as many types as possible. If we cannot infer all of them, we can
2526 // never do anything with this instruction pattern: report it to the user.
2527 if (!I->InferAllTypes())
2528 I->error("Could not infer all types in pattern!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002529
2530 // InstInputs - Keep track of all of the inputs of the instruction, along
Chris Lattner6cefb772008-01-05 22:25:12 +00002531 // with the record they are declared as.
2532 std::map<std::string, TreePatternNode*> InstInputs;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002533
Chris Lattner6cefb772008-01-05 22:25:12 +00002534 // InstResults - Keep track of all the virtual registers that are 'set'
2535 // in the instruction, including what reg class they are.
2536 std::map<std::string, TreePatternNode*> InstResults;
2537
Chris Lattner6cefb772008-01-05 22:25:12 +00002538 std::vector<Record*> InstImpResults;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002539
Chris Lattner6cefb772008-01-05 22:25:12 +00002540 // Verify that the top-level forms in the instruction are of void type, and
2541 // fill in the InstResults map.
2542 for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) {
2543 TreePatternNode *Pat = I->getTree(j);
Chris Lattnerd7349192010-03-19 21:37:09 +00002544 if (Pat->getNumTypes() != 0)
Chris Lattner6cefb772008-01-05 22:25:12 +00002545 I->error("Top-level forms in instruction pattern should have"
2546 " void types");
2547
2548 // Find inputs and outputs, and verify the structure of the uses/defs.
2549 FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults,
Chris Lattneracfb70f2010-04-20 06:30:25 +00002550 InstImpResults);
Chris Lattner6cefb772008-01-05 22:25:12 +00002551 }
2552
2553 // Now that we have inputs and outputs of the pattern, inspect the operands
2554 // list for the instruction. This determines the order that operands are
2555 // added to the machine instruction the node corresponds to.
2556 unsigned NumResults = InstResults.size();
2557
2558 // Parse the operands list from the (ops) list, validating it.
2559 assert(I->getArgList().empty() && "Args list should still be empty here!");
Chris Lattnerf30187a2010-03-19 00:07:20 +00002560 CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]);
Chris Lattner6cefb772008-01-05 22:25:12 +00002561
2562 // Check that all of the results occur first in the list.
2563 std::vector<Record*> Results;
Chris Lattnerd7349192010-03-19 21:37:09 +00002564 TreePatternNode *Res0Node = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +00002565 for (unsigned i = 0; i != NumResults; ++i) {
Chris Lattnerc240bb02010-11-01 04:03:32 +00002566 if (i == CGI.Operands.size())
Chris Lattner6cefb772008-01-05 22:25:12 +00002567 I->error("'" + InstResults.begin()->first +
2568 "' set but does not appear in operand list!");
Chris Lattnerc240bb02010-11-01 04:03:32 +00002569 const std::string &OpName = CGI.Operands[i].Name;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002570
Chris Lattner6cefb772008-01-05 22:25:12 +00002571 // Check that it exists in InstResults.
2572 TreePatternNode *RNode = InstResults[OpName];
2573 if (RNode == 0)
2574 I->error("Operand $" + OpName + " does not exist in operand list!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002575
Chris Lattner6cefb772008-01-05 22:25:12 +00002576 if (i == 0)
2577 Res0Node = RNode;
2578 Record *R = dynamic_cast<DefInit*>(RNode->getLeafValue())->getDef();
2579 if (R == 0)
2580 I->error("Operand $" + OpName + " should be a set destination: all "
2581 "outputs must occur before inputs in operand list!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002582
Chris Lattnerc240bb02010-11-01 04:03:32 +00002583 if (CGI.Operands[i].Rec != R)
Chris Lattner6cefb772008-01-05 22:25:12 +00002584 I->error("Operand $" + OpName + " class mismatch!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002585
Chris Lattner6cefb772008-01-05 22:25:12 +00002586 // Remember the return type.
Chris Lattnerc240bb02010-11-01 04:03:32 +00002587 Results.push_back(CGI.Operands[i].Rec);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002588
Chris Lattner6cefb772008-01-05 22:25:12 +00002589 // Okay, this one checks out.
2590 InstResults.erase(OpName);
2591 }
2592
2593 // Loop over the inputs next. Make a copy of InstInputs so we can destroy
2594 // the copy while we're checking the inputs.
2595 std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs);
2596
2597 std::vector<TreePatternNode*> ResultNodeOperands;
2598 std::vector<Record*> Operands;
Chris Lattnerc240bb02010-11-01 04:03:32 +00002599 for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
2600 CGIOperandList::OperandInfo &Op = CGI.Operands[i];
Chris Lattner6cefb772008-01-05 22:25:12 +00002601 const std::string &OpName = Op.Name;
2602 if (OpName.empty())
2603 I->error("Operand #" + utostr(i) + " in operands list has no name!");
2604
2605 if (!InstInputsCheck.count(OpName)) {
2606 // If this is an predicate operand or optional def operand with an
2607 // DefaultOps set filled in, we can ignore this. When we codegen it,
2608 // we will do so as always executed.
2609 if (Op.Rec->isSubClassOf("PredicateOperand") ||
2610 Op.Rec->isSubClassOf("OptionalDefOperand")) {
2611 // Does it have a non-empty DefaultOps field? If so, ignore this
2612 // operand.
2613 if (!getDefaultOperand(Op.Rec).DefaultOps.empty())
2614 continue;
2615 }
2616 I->error("Operand $" + OpName +
2617 " does not appear in the instruction pattern");
2618 }
2619 TreePatternNode *InVal = InstInputsCheck[OpName];
2620 InstInputsCheck.erase(OpName); // It occurred, remove from map.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002621
Chris Lattner6cefb772008-01-05 22:25:12 +00002622 if (InVal->isLeaf() &&
2623 dynamic_cast<DefInit*>(InVal->getLeafValue())) {
2624 Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef();
2625 if (Op.Rec != InRec && !InRec->isSubClassOf("ComplexPattern"))
2626 I->error("Operand $" + OpName + "'s register class disagrees"
2627 " between the operand and pattern");
2628 }
2629 Operands.push_back(Op.Rec);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002630
Chris Lattner6cefb772008-01-05 22:25:12 +00002631 // Construct the result for the dest-pattern operand list.
2632 TreePatternNode *OpNode = InVal->clone();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002633
Chris Lattner6cefb772008-01-05 22:25:12 +00002634 // No predicate is useful on the result.
Dan Gohman0540e172008-10-15 06:17:21 +00002635 OpNode->clearPredicateFns();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002636
Chris Lattner6cefb772008-01-05 22:25:12 +00002637 // Promote the xform function to be an explicit node if set.
2638 if (Record *Xform = OpNode->getTransformFn()) {
2639 OpNode->setTransformFn(0);
2640 std::vector<TreePatternNode*> Children;
2641 Children.push_back(OpNode);
Chris Lattnerd7349192010-03-19 21:37:09 +00002642 OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes());
Chris Lattner6cefb772008-01-05 22:25:12 +00002643 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002644
Chris Lattner6cefb772008-01-05 22:25:12 +00002645 ResultNodeOperands.push_back(OpNode);
2646 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002647
Chris Lattner6cefb772008-01-05 22:25:12 +00002648 if (!InstInputsCheck.empty())
2649 I->error("Input operand $" + InstInputsCheck.begin()->first +
2650 " occurs in pattern but not in operands list!");
2651
2652 TreePatternNode *ResultPattern =
Chris Lattnerd7349192010-03-19 21:37:09 +00002653 new TreePatternNode(I->getRecord(), ResultNodeOperands,
2654 GetNumNodeResults(I->getRecord(), *this));
Chris Lattner6cefb772008-01-05 22:25:12 +00002655 // Copy fully inferred output node type to instruction result pattern.
Chris Lattnerd7349192010-03-19 21:37:09 +00002656 for (unsigned i = 0; i != NumResults; ++i)
2657 ResultPattern->setType(i, Res0Node->getExtType(i));
Chris Lattner6cefb772008-01-05 22:25:12 +00002658
2659 // Create and insert the instruction.
Chris Lattneracfb70f2010-04-20 06:30:25 +00002660 // FIXME: InstImpResults should not be part of DAGInstruction.
Chris Lattner62bcec82010-04-20 06:28:43 +00002661 DAGInstruction TheInst(I, Results, Operands, InstImpResults);
Chris Lattner6cefb772008-01-05 22:25:12 +00002662 Instructions.insert(std::make_pair(I->getRecord(), TheInst));
2663
2664 // Use a temporary tree pattern to infer all types and make sure that the
2665 // constructed result is correct. This depends on the instruction already
2666 // being inserted into the Instructions map.
2667 TreePattern Temp(I->getRecord(), ResultPattern, false, *this);
Chris Lattner2cacec52010-03-15 06:00:16 +00002668 Temp.InferAllTypes(&I->getNamedNodesMap());
Chris Lattner6cefb772008-01-05 22:25:12 +00002669
2670 DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second;
2671 TheInsertedInst.setResultPattern(Temp.getOnlyTree());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002672
Chris Lattner6cefb772008-01-05 22:25:12 +00002673 DEBUG(I->dump());
2674 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002675
Chris Lattner6cefb772008-01-05 22:25:12 +00002676 // If we can, convert the instructions to be patterns that are matched!
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +00002677 for (std::map<Record*, DAGInstruction, RecordPtrCmp>::iterator II =
2678 Instructions.begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00002679 E = Instructions.end(); II != E; ++II) {
2680 DAGInstruction &TheInst = II->second;
Chris Lattnerf1ab4f12008-01-06 01:52:22 +00002681 const TreePattern *I = TheInst.getPattern();
Chris Lattner6cefb772008-01-05 22:25:12 +00002682 if (I == 0) continue; // No pattern.
2683
2684 // FIXME: Assume only the first tree is the pattern. The others are clobber
2685 // nodes.
2686 TreePatternNode *Pattern = I->getTree(0);
2687 TreePatternNode *SrcPattern;
2688 if (Pattern->getOperator()->getName() == "set") {
2689 SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone();
2690 } else{
2691 // Not a set (store or something?)
2692 SrcPattern = Pattern;
2693 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002694
Chris Lattner6cefb772008-01-05 22:25:12 +00002695 Record *Instr = II->first;
Chris Lattner25b6f912010-02-23 06:16:51 +00002696 AddPatternToMatch(I,
Jim Grosbach997759a2010-12-07 23:05:49 +00002697 PatternToMatch(Instr,
2698 Instr->getValueAsListInit("Predicates"),
Chris Lattner967d54a2010-02-23 06:35:45 +00002699 SrcPattern,
2700 TheInst.getResultPattern(),
Chris Lattner25b6f912010-02-23 06:16:51 +00002701 TheInst.getImpResults(),
Chris Lattner117ccb72010-03-01 22:09:11 +00002702 Instr->getValueAsInt("AddedComplexity"),
2703 Instr->getID()));
Chris Lattner6cefb772008-01-05 22:25:12 +00002704 }
2705}
2706
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002707
2708typedef std::pair<const TreePatternNode*, unsigned> NameRecord;
2709
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002710static void FindNames(const TreePatternNode *P,
Chris Lattnera27234e2010-02-23 07:22:28 +00002711 std::map<std::string, NameRecord> &Names,
2712 const TreePattern *PatternTop) {
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002713 if (!P->getName().empty()) {
2714 NameRecord &Rec = Names[P->getName()];
2715 // If this is the first instance of the name, remember the node.
2716 if (Rec.second++ == 0)
2717 Rec.first = P;
Chris Lattnerd7349192010-03-19 21:37:09 +00002718 else if (Rec.first->getExtTypes() != P->getExtTypes())
Chris Lattnera27234e2010-02-23 07:22:28 +00002719 PatternTop->error("repetition of value: $" + P->getName() +
2720 " where different uses have different types!");
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002721 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002722
Chris Lattner967d54a2010-02-23 06:35:45 +00002723 if (!P->isLeaf()) {
2724 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattnera27234e2010-02-23 07:22:28 +00002725 FindNames(P->getChild(i), Names, PatternTop);
Chris Lattner967d54a2010-02-23 06:35:45 +00002726 }
2727}
2728
Chris Lattner25b6f912010-02-23 06:16:51 +00002729void CodeGenDAGPatterns::AddPatternToMatch(const TreePattern *Pattern,
2730 const PatternToMatch &PTM) {
Chris Lattner967d54a2010-02-23 06:35:45 +00002731 // Do some sanity checking on the pattern we're about to match.
Chris Lattner25b6f912010-02-23 06:16:51 +00002732 std::string Reason;
2733 if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this))
Chris Lattner967d54a2010-02-23 06:35:45 +00002734 Pattern->error("Pattern can never match: " + Reason);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002735
Chris Lattner405f1252010-03-01 22:29:19 +00002736 // If the source pattern's root is a complex pattern, that complex pattern
2737 // must specify the nodes it can potentially match.
2738 if (const ComplexPattern *CP =
2739 PTM.getSrcPattern()->getComplexPatternInfo(*this))
2740 if (CP->getRootNodes().empty())
2741 Pattern->error("ComplexPattern at root must specify list of opcodes it"
2742 " could match");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002743
2744
Chris Lattner967d54a2010-02-23 06:35:45 +00002745 // Find all of the named values in the input and output, ensure they have the
2746 // same type.
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002747 std::map<std::string, NameRecord> SrcNames, DstNames;
Chris Lattnera27234e2010-02-23 07:22:28 +00002748 FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
2749 FindNames(PTM.getDstPattern(), DstNames, Pattern);
Chris Lattner967d54a2010-02-23 06:35:45 +00002750
2751 // Scan all of the named values in the destination pattern, rejecting them if
2752 // they don't exist in the input pattern.
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002753 for (std::map<std::string, NameRecord>::iterator
Chris Lattnerba1cff42010-02-23 07:50:58 +00002754 I = DstNames.begin(), E = DstNames.end(); I != E; ++I) {
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002755 if (SrcNames[I->first].first == 0)
Chris Lattner967d54a2010-02-23 06:35:45 +00002756 Pattern->error("Pattern has input without matching name in output: $" +
2757 I->first);
Chris Lattnerba1cff42010-02-23 07:50:58 +00002758 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002759
Chris Lattner4ac7a0c2010-02-23 06:55:24 +00002760 // Scan all of the named values in the source pattern, rejecting them if the
2761 // name isn't used in the dest, and isn't used to tie two values together.
2762 for (std::map<std::string, NameRecord>::iterator
2763 I = SrcNames.begin(), E = SrcNames.end(); I != E; ++I)
2764 if (DstNames[I->first].first == 0 && SrcNames[I->first].second == 1)
2765 Pattern->error("Pattern has dead named input: $" + I->first);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002766
Chris Lattner25b6f912010-02-23 06:16:51 +00002767 PatternsToMatch.push_back(PTM);
2768}
2769
2770
Dan Gohmanee4fa192008-04-03 00:02:49 +00002771
2772void CodeGenDAGPatterns::InferInstructionFlags() {
Chris Lattnerf6502782010-03-19 00:34:35 +00002773 const std::vector<const CodeGenInstruction*> &Instructions =
2774 Target.getInstructionsByEnumValue();
Chris Lattnerb61e09d2010-03-19 00:18:23 +00002775 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
2776 CodeGenInstruction &InstInfo =
2777 const_cast<CodeGenInstruction &>(*Instructions[i]);
Dan Gohmanee4fa192008-04-03 00:02:49 +00002778 // Determine properties of the instruction from its pattern.
Evan Cheng0f040a22011-03-15 05:09:26 +00002779 bool MayStore, MayLoad, IsBitcast, HasSideEffects, IsVariadic;
2780 InferFromPattern(InstInfo, MayStore, MayLoad, IsBitcast,
2781 HasSideEffects, IsVariadic, *this);
Dan Gohmanee4fa192008-04-03 00:02:49 +00002782 InstInfo.mayStore = MayStore;
2783 InstInfo.mayLoad = MayLoad;
Evan Cheng0f040a22011-03-15 05:09:26 +00002784 InstInfo.isBitcast = IsBitcast;
Dan Gohmanee4fa192008-04-03 00:02:49 +00002785 InstInfo.hasSideEffects = HasSideEffects;
Chris Lattnerc240bb02010-11-01 04:03:32 +00002786 InstInfo.Operands.isVariadic = IsVariadic;
Dan Gohmanee4fa192008-04-03 00:02:49 +00002787 }
2788}
2789
Chris Lattner2cacec52010-03-15 06:00:16 +00002790/// Given a pattern result with an unresolved type, see if we can find one
2791/// instruction with an unresolved result type. Force this result type to an
2792/// arbitrary element if it's possible types to converge results.
2793static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) {
2794 if (N->isLeaf())
2795 return false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002796
Chris Lattner2cacec52010-03-15 06:00:16 +00002797 // Analyze children.
2798 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2799 if (ForceArbitraryInstResultType(N->getChild(i), TP))
2800 return true;
2801
2802 if (!N->getOperator()->isSubClassOf("Instruction"))
2803 return false;
2804
2805 // If this type is already concrete or completely unknown we can't do
2806 // anything.
Chris Lattnerd7349192010-03-19 21:37:09 +00002807 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) {
2808 if (N->getExtType(i).isCompletelyUnknown() || N->getExtType(i).isConcrete())
2809 continue;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002810
Chris Lattnerd7349192010-03-19 21:37:09 +00002811 // Otherwise, force its type to the first possibility (an arbitrary choice).
2812 if (N->getExtType(i).MergeInTypeInfo(N->getExtType(i).getTypeList()[0], TP))
2813 return true;
2814 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002815
Chris Lattnerd7349192010-03-19 21:37:09 +00002816 return false;
Chris Lattner2cacec52010-03-15 06:00:16 +00002817}
2818
Chris Lattnerfe718932008-01-06 01:10:31 +00002819void CodeGenDAGPatterns::ParsePatterns() {
Chris Lattner6cefb772008-01-05 22:25:12 +00002820 std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
2821
2822 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
Chris Lattnerd7349192010-03-19 21:37:09 +00002823 Record *CurPattern = Patterns[i];
2824 DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
Chris Lattner310adf12010-03-27 02:53:27 +00002825 TreePattern *Pattern = new TreePattern(CurPattern, Tree, true, *this);
Chris Lattner6cefb772008-01-05 22:25:12 +00002826
2827 // Inline pattern fragments into it.
2828 Pattern->InlinePatternFragments();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002829
Chris Lattnerd7349192010-03-19 21:37:09 +00002830 ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs");
Chris Lattner6cefb772008-01-05 22:25:12 +00002831 if (LI->getSize() == 0) continue; // no pattern.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002832
Chris Lattner6cefb772008-01-05 22:25:12 +00002833 // Parse the instruction.
Chris Lattnerd7349192010-03-19 21:37:09 +00002834 TreePattern *Result = new TreePattern(CurPattern, LI, false, *this);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002835
Chris Lattner6cefb772008-01-05 22:25:12 +00002836 // Inline pattern fragments into it.
2837 Result->InlinePatternFragments();
2838
2839 if (Result->getNumTrees() != 1)
2840 Result->error("Cannot handle instructions producing instructions "
2841 "with temporaries yet!");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002842
Chris Lattner6cefb772008-01-05 22:25:12 +00002843 bool IterateInference;
2844 bool InferredAllPatternTypes, InferredAllResultTypes;
2845 do {
2846 // Infer as many types as possible. If we cannot infer all of them, we
2847 // can never do anything with this pattern: report it to the user.
Chris Lattner2cacec52010-03-15 06:00:16 +00002848 InferredAllPatternTypes =
2849 Pattern->InferAllTypes(&Pattern->getNamedNodesMap());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002850
Chris Lattner6cefb772008-01-05 22:25:12 +00002851 // Infer as many types as possible. If we cannot infer all of them, we
2852 // can never do anything with this pattern: report it to the user.
Chris Lattner2cacec52010-03-15 06:00:16 +00002853 InferredAllResultTypes =
2854 Result->InferAllTypes(&Pattern->getNamedNodesMap());
Chris Lattner6cefb772008-01-05 22:25:12 +00002855
Chris Lattner6c6ba362010-03-18 23:15:10 +00002856 IterateInference = false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002857
Chris Lattner6cefb772008-01-05 22:25:12 +00002858 // Apply the type of the result to the source pattern. This helps us
2859 // resolve cases where the input type is known to be a pointer type (which
2860 // is considered resolved), but the result knows it needs to be 32- or
2861 // 64-bits. Infer the other way for good measure.
Chris Lattnerd7349192010-03-19 21:37:09 +00002862 for (unsigned i = 0, e = std::min(Result->getTree(0)->getNumTypes(),
2863 Pattern->getTree(0)->getNumTypes());
2864 i != e; ++i) {
Chris Lattner6c6ba362010-03-18 23:15:10 +00002865 IterateInference = Pattern->getTree(0)->
Chris Lattnerd7349192010-03-19 21:37:09 +00002866 UpdateNodeType(i, Result->getTree(0)->getExtType(i), *Result);
Chris Lattner6c6ba362010-03-18 23:15:10 +00002867 IterateInference |= Result->getTree(0)->
Chris Lattnerd7349192010-03-19 21:37:09 +00002868 UpdateNodeType(i, Pattern->getTree(0)->getExtType(i), *Result);
Chris Lattner6c6ba362010-03-18 23:15:10 +00002869 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002870
Chris Lattner2cacec52010-03-15 06:00:16 +00002871 // If our iteration has converged and the input pattern's types are fully
2872 // resolved but the result pattern is not fully resolved, we may have a
2873 // situation where we have two instructions in the result pattern and
2874 // the instructions require a common register class, but don't care about
2875 // what actual MVT is used. This is actually a bug in our modelling:
2876 // output patterns should have register classes, not MVTs.
2877 //
2878 // In any case, to handle this, we just go through and disambiguate some
2879 // arbitrary types to the result pattern's nodes.
2880 if (!IterateInference && InferredAllPatternTypes &&
2881 !InferredAllResultTypes)
2882 IterateInference = ForceArbitraryInstResultType(Result->getTree(0),
2883 *Result);
Chris Lattner6cefb772008-01-05 22:25:12 +00002884 } while (IterateInference);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002885
Chris Lattner6cefb772008-01-05 22:25:12 +00002886 // Verify that we inferred enough types that we can do something with the
2887 // pattern and result. If these fire the user has to add type casts.
2888 if (!InferredAllPatternTypes)
2889 Pattern->error("Could not infer all types in pattern!");
Chris Lattner2cacec52010-03-15 06:00:16 +00002890 if (!InferredAllResultTypes) {
2891 Pattern->dump();
Chris Lattner6cefb772008-01-05 22:25:12 +00002892 Result->error("Could not infer all types in pattern result!");
Chris Lattner2cacec52010-03-15 06:00:16 +00002893 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002894
Chris Lattner6cefb772008-01-05 22:25:12 +00002895 // Validate that the input pattern is correct.
2896 std::map<std::string, TreePatternNode*> InstInputs;
2897 std::map<std::string, TreePatternNode*> InstResults;
Chris Lattner6cefb772008-01-05 22:25:12 +00002898 std::vector<Record*> InstImpResults;
2899 for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j)
2900 FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j),
2901 InstInputs, InstResults,
Chris Lattneracfb70f2010-04-20 06:30:25 +00002902 InstImpResults);
Chris Lattner6cefb772008-01-05 22:25:12 +00002903
2904 // Promote the xform function to be an explicit node if set.
2905 TreePatternNode *DstPattern = Result->getOnlyTree();
2906 std::vector<TreePatternNode*> ResultNodeOperands;
2907 for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) {
2908 TreePatternNode *OpNode = DstPattern->getChild(ii);
2909 if (Record *Xform = OpNode->getTransformFn()) {
2910 OpNode->setTransformFn(0);
2911 std::vector<TreePatternNode*> Children;
2912 Children.push_back(OpNode);
Chris Lattnerd7349192010-03-19 21:37:09 +00002913 OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes());
Chris Lattner6cefb772008-01-05 22:25:12 +00002914 }
2915 ResultNodeOperands.push_back(OpNode);
2916 }
2917 DstPattern = Result->getOnlyTree();
2918 if (!DstPattern->isLeaf())
2919 DstPattern = new TreePatternNode(DstPattern->getOperator(),
Chris Lattnerd7349192010-03-19 21:37:09 +00002920 ResultNodeOperands,
2921 DstPattern->getNumTypes());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002922
Chris Lattnerd7349192010-03-19 21:37:09 +00002923 for (unsigned i = 0, e = Result->getOnlyTree()->getNumTypes(); i != e; ++i)
2924 DstPattern->setType(i, Result->getOnlyTree()->getExtType(i));
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002925
Chris Lattner6cefb772008-01-05 22:25:12 +00002926 TreePattern Temp(Result->getRecord(), DstPattern, false, *this);
2927 Temp.InferAllTypes();
2928
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002929
Chris Lattner25b6f912010-02-23 06:16:51 +00002930 AddPatternToMatch(Pattern,
Jim Grosbach997759a2010-12-07 23:05:49 +00002931 PatternToMatch(CurPattern,
2932 CurPattern->getValueAsListInit("Predicates"),
Chris Lattnerd7349192010-03-19 21:37:09 +00002933 Pattern->getTree(0),
2934 Temp.getOnlyTree(), InstImpResults,
2935 CurPattern->getValueAsInt("AddedComplexity"),
2936 CurPattern->getID()));
Chris Lattner6cefb772008-01-05 22:25:12 +00002937 }
2938}
2939
2940/// CombineChildVariants - Given a bunch of permutations of each child of the
2941/// 'operator' node, put them together in all possible ways.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002942static void CombineChildVariants(TreePatternNode *Orig,
Chris Lattner6cefb772008-01-05 22:25:12 +00002943 const std::vector<std::vector<TreePatternNode*> > &ChildVariants,
2944 std::vector<TreePatternNode*> &OutVariants,
Scott Michel327d0652008-03-05 17:49:05 +00002945 CodeGenDAGPatterns &CDP,
2946 const MultipleUseVarSet &DepVars) {
Chris Lattner6cefb772008-01-05 22:25:12 +00002947 // Make sure that each operand has at least one variant to choose from.
2948 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
2949 if (ChildVariants[i].empty())
2950 return;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002951
Chris Lattner6cefb772008-01-05 22:25:12 +00002952 // The end result is an all-pairs construction of the resultant pattern.
2953 std::vector<unsigned> Idxs;
2954 Idxs.resize(ChildVariants.size());
Scott Michel327d0652008-03-05 17:49:05 +00002955 bool NotDone;
2956 do {
2957#ifndef NDEBUG
Chris Lattneraaf54862010-02-27 06:51:44 +00002958 DEBUG(if (!Idxs.empty()) {
2959 errs() << Orig->getOperator()->getName() << ": Idxs = [ ";
2960 for (unsigned i = 0; i < Idxs.size(); ++i) {
2961 errs() << Idxs[i] << " ";
2962 }
2963 errs() << "]\n";
2964 });
Scott Michel327d0652008-03-05 17:49:05 +00002965#endif
Chris Lattner6cefb772008-01-05 22:25:12 +00002966 // Create the variant and add it to the output list.
2967 std::vector<TreePatternNode*> NewChildren;
2968 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
2969 NewChildren.push_back(ChildVariants[i][Idxs[i]]);
Chris Lattnerd7349192010-03-19 21:37:09 +00002970 TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren,
2971 Orig->getNumTypes());
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002972
Chris Lattner6cefb772008-01-05 22:25:12 +00002973 // Copy over properties.
2974 R->setName(Orig->getName());
Dan Gohman0540e172008-10-15 06:17:21 +00002975 R->setPredicateFns(Orig->getPredicateFns());
Chris Lattner6cefb772008-01-05 22:25:12 +00002976 R->setTransformFn(Orig->getTransformFn());
Chris Lattnerd7349192010-03-19 21:37:09 +00002977 for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i)
2978 R->setType(i, Orig->getExtType(i));
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002979
Scott Michel327d0652008-03-05 17:49:05 +00002980 // If this pattern cannot match, do not include it as a variant.
Chris Lattner6cefb772008-01-05 22:25:12 +00002981 std::string ErrString;
2982 if (!R->canPatternMatch(ErrString, CDP)) {
2983 delete R;
2984 } else {
2985 bool AlreadyExists = false;
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002986
Chris Lattner6cefb772008-01-05 22:25:12 +00002987 // Scan to see if this pattern has already been emitted. We can get
2988 // duplication due to things like commuting:
2989 // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
2990 // which are the same pattern. Ignore the dups.
2991 for (unsigned i = 0, e = OutVariants.size(); i != e; ++i)
Scott Michel327d0652008-03-05 17:49:05 +00002992 if (R->isIsomorphicTo(OutVariants[i], DepVars)) {
Chris Lattner6cefb772008-01-05 22:25:12 +00002993 AlreadyExists = true;
2994 break;
2995 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00002996
Chris Lattner6cefb772008-01-05 22:25:12 +00002997 if (AlreadyExists)
2998 delete R;
2999 else
3000 OutVariants.push_back(R);
3001 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003002
Scott Michel327d0652008-03-05 17:49:05 +00003003 // Increment indices to the next permutation by incrementing the
3004 // indicies from last index backward, e.g., generate the sequence
3005 // [0, 0], [0, 1], [1, 0], [1, 1].
3006 int IdxsIdx;
3007 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
3008 if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size())
3009 Idxs[IdxsIdx] = 0;
3010 else
Chris Lattner6cefb772008-01-05 22:25:12 +00003011 break;
Chris Lattner6cefb772008-01-05 22:25:12 +00003012 }
Scott Michel327d0652008-03-05 17:49:05 +00003013 NotDone = (IdxsIdx >= 0);
3014 } while (NotDone);
Chris Lattner6cefb772008-01-05 22:25:12 +00003015}
3016
3017/// CombineChildVariants - A helper function for binary operators.
3018///
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003019static void CombineChildVariants(TreePatternNode *Orig,
Chris Lattner6cefb772008-01-05 22:25:12 +00003020 const std::vector<TreePatternNode*> &LHS,
3021 const std::vector<TreePatternNode*> &RHS,
3022 std::vector<TreePatternNode*> &OutVariants,
Scott Michel327d0652008-03-05 17:49:05 +00003023 CodeGenDAGPatterns &CDP,
3024 const MultipleUseVarSet &DepVars) {
Chris Lattner6cefb772008-01-05 22:25:12 +00003025 std::vector<std::vector<TreePatternNode*> > ChildVariants;
3026 ChildVariants.push_back(LHS);
3027 ChildVariants.push_back(RHS);
Scott Michel327d0652008-03-05 17:49:05 +00003028 CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003029}
Chris Lattner6cefb772008-01-05 22:25:12 +00003030
3031
3032static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N,
3033 std::vector<TreePatternNode *> &Children) {
3034 assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!");
3035 Record *Operator = N->getOperator();
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003036
Chris Lattner6cefb772008-01-05 22:25:12 +00003037 // Only permit raw nodes.
Dan Gohman0540e172008-10-15 06:17:21 +00003038 if (!N->getName().empty() || !N->getPredicateFns().empty() ||
Chris Lattner6cefb772008-01-05 22:25:12 +00003039 N->getTransformFn()) {
3040 Children.push_back(N);
3041 return;
3042 }
3043
3044 if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
3045 Children.push_back(N->getChild(0));
3046 else
3047 GatherChildrenOfAssociativeOpcode(N->getChild(0), Children);
3048
3049 if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
3050 Children.push_back(N->getChild(1));
3051 else
3052 GatherChildrenOfAssociativeOpcode(N->getChild(1), Children);
3053}
3054
3055/// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
3056/// the (potentially recursive) pattern by using algebraic laws.
3057///
3058static void GenerateVariantsOf(TreePatternNode *N,
3059 std::vector<TreePatternNode*> &OutVariants,
Scott Michel327d0652008-03-05 17:49:05 +00003060 CodeGenDAGPatterns &CDP,
3061 const MultipleUseVarSet &DepVars) {
Chris Lattner6cefb772008-01-05 22:25:12 +00003062 // We cannot permute leaves.
3063 if (N->isLeaf()) {
3064 OutVariants.push_back(N);
3065 return;
3066 }
3067
3068 // Look up interesting info about the node.
3069 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator());
3070
Jim Grosbachda4231f2009-03-26 16:17:51 +00003071 // If this node is associative, re-associate.
Chris Lattner6cefb772008-01-05 22:25:12 +00003072 if (NodeInfo.hasProperty(SDNPAssociative)) {
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003073 // Re-associate by pulling together all of the linked operators
Chris Lattner6cefb772008-01-05 22:25:12 +00003074 std::vector<TreePatternNode*> MaximalChildren;
3075 GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
3076
3077 // Only handle child sizes of 3. Otherwise we'll end up trying too many
3078 // permutations.
3079 if (MaximalChildren.size() == 3) {
3080 // Find the variants of all of our maximal children.
3081 std::vector<TreePatternNode*> AVariants, BVariants, CVariants;
Scott Michel327d0652008-03-05 17:49:05 +00003082 GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars);
3083 GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars);
3084 GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars);
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003085
Chris Lattner6cefb772008-01-05 22:25:12 +00003086 // There are only two ways we can permute the tree:
3087 // (A op B) op C and A op (B op C)
3088 // Within these forms, we can also permute A/B/C.
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003089
Chris Lattner6cefb772008-01-05 22:25:12 +00003090 // Generate legal pair permutations of A/B/C.
3091 std::vector<TreePatternNode*> ABVariants;
3092 std::vector<TreePatternNode*> BAVariants;
3093 std::vector<TreePatternNode*> ACVariants;
3094 std::vector<TreePatternNode*> CAVariants;
3095 std::vector<TreePatternNode*> BCVariants;
3096 std::vector<TreePatternNode*> CBVariants;
Scott Michel327d0652008-03-05 17:49:05 +00003097 CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars);
3098 CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars);
3099 CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars);
3100 CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars);
3101 CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars);
3102 CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003103
3104 // Combine those into the result: (x op x) op x
Scott Michel327d0652008-03-05 17:49:05 +00003105 CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars);
3106 CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars);
3107 CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars);
3108 CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars);
3109 CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars);
3110 CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003111
3112 // Combine those into the result: x op (x op x)
Scott Michel327d0652008-03-05 17:49:05 +00003113 CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars);
3114 CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars);
3115 CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars);
3116 CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars);
3117 CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars);
3118 CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003119 return;
3120 }
3121 }
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003122
Chris Lattner6cefb772008-01-05 22:25:12 +00003123 // Compute permutations of all children.
3124 std::vector<std::vector<TreePatternNode*> > ChildVariants;
3125 ChildVariants.resize(N->getNumChildren());
3126 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
Scott Michel327d0652008-03-05 17:49:05 +00003127 GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003128
3129 // Build all permutations based on how the children were formed.
Scott Michel327d0652008-03-05 17:49:05 +00003130 CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003131
3132 // If this node is commutative, consider the commuted order.
Evan Cheng6bd95672008-06-16 20:29:38 +00003133 bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP);
3134 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
3135 assert((N->getNumChildren()==2 || isCommIntrinsic) &&
3136 "Commutative but doesn't have 2 children!");
Chris Lattner6cefb772008-01-05 22:25:12 +00003137 // Don't count children which are actually register references.
3138 unsigned NC = 0;
3139 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
3140 TreePatternNode *Child = N->getChild(i);
3141 if (Child->isLeaf())
3142 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
3143 Record *RR = DI->getDef();
3144 if (RR->isSubClassOf("Register"))
3145 continue;
3146 }
3147 NC++;
3148 }
3149 // Consider the commuted order.
Evan Cheng6bd95672008-06-16 20:29:38 +00003150 if (isCommIntrinsic) {
3151 // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd
3152 // operands are the commutative operands, and there might be more operands
3153 // after those.
3154 assert(NC >= 3 &&
3155 "Commutative intrinsic should have at least 3 childrean!");
3156 std::vector<std::vector<TreePatternNode*> > Variants;
3157 Variants.push_back(ChildVariants[0]); // Intrinsic id.
3158 Variants.push_back(ChildVariants[2]);
3159 Variants.push_back(ChildVariants[1]);
3160 for (unsigned i = 3; i != NC; ++i)
3161 Variants.push_back(ChildVariants[i]);
3162 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
3163 } else if (NC == 2)
Chris Lattner6cefb772008-01-05 22:25:12 +00003164 CombineChildVariants(N, ChildVariants[1], ChildVariants[0],
Scott Michel327d0652008-03-05 17:49:05 +00003165 OutVariants, CDP, DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003166 }
3167}
3168
3169
3170// GenerateVariants - Generate variants. For example, commutative patterns can
3171// match multiple ways. Add them to PatternsToMatch as well.
Chris Lattnerfe718932008-01-06 01:10:31 +00003172void CodeGenDAGPatterns::GenerateVariants() {
Chris Lattner569f1212009-08-23 04:44:11 +00003173 DEBUG(errs() << "Generating instruction variants.\n");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003174
Chris Lattner6cefb772008-01-05 22:25:12 +00003175 // Loop over all of the patterns we've collected, checking to see if we can
3176 // generate variants of the instruction, through the exploitation of
Jim Grosbachda4231f2009-03-26 16:17:51 +00003177 // identities. This permits the target to provide aggressive matching without
Chris Lattner6cefb772008-01-05 22:25:12 +00003178 // the .td file having to contain tons of variants of instructions.
3179 //
3180 // Note that this loop adds new patterns to the PatternsToMatch list, but we
3181 // intentionally do not reconsider these. Any variants of added patterns have
3182 // already been added.
3183 //
3184 for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) {
Scott Michel327d0652008-03-05 17:49:05 +00003185 MultipleUseVarSet DepVars;
Chris Lattner6cefb772008-01-05 22:25:12 +00003186 std::vector<TreePatternNode*> Variants;
Scott Michel327d0652008-03-05 17:49:05 +00003187 FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars);
Chris Lattner569f1212009-08-23 04:44:11 +00003188 DEBUG(errs() << "Dependent/multiply used variables: ");
Scott Michel327d0652008-03-05 17:49:05 +00003189 DEBUG(DumpDepVars(DepVars));
Chris Lattner569f1212009-08-23 04:44:11 +00003190 DEBUG(errs() << "\n");
Jim Grosbachbb168242010-10-08 18:13:57 +00003191 GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this,
3192 DepVars);
Chris Lattner6cefb772008-01-05 22:25:12 +00003193
3194 assert(!Variants.empty() && "Must create at least original variant!");
3195 Variants.erase(Variants.begin()); // Remove the original pattern.
3196
3197 if (Variants.empty()) // No variants for this pattern.
3198 continue;
3199
Chris Lattner569f1212009-08-23 04:44:11 +00003200 DEBUG(errs() << "FOUND VARIANTS OF: ";
3201 PatternsToMatch[i].getSrcPattern()->dump();
3202 errs() << "\n");
Chris Lattner6cefb772008-01-05 22:25:12 +00003203
3204 for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
3205 TreePatternNode *Variant = Variants[v];
3206
Chris Lattner569f1212009-08-23 04:44:11 +00003207 DEBUG(errs() << " VAR#" << v << ": ";
3208 Variant->dump();
3209 errs() << "\n");
Jim Grosbachfbadcd02010-12-21 16:16:00 +00003210
Chris Lattner6cefb772008-01-05 22:25:12 +00003211 // Scan to see if an instruction or explicit pattern already matches this.
3212 bool AlreadyExists = false;
3213 for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
Evan Chengc0ad80f2009-06-26 05:59:16 +00003214 // Skip if the top level predicates do not match.
3215 if (PatternsToMatch[i].getPredicates() !=
3216 PatternsToMatch[p].getPredicates())
3217 continue;
Chris Lattner6cefb772008-01-05 22:25:12 +00003218 // Check to see if this variant already exists.
Jim Grosbachbb168242010-10-08 18:13:57 +00003219 if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
3220 DepVars)) {
Chris Lattner569f1212009-08-23 04:44:11 +00003221 DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n");
Chris Lattner6cefb772008-01-05 22:25:12 +00003222 AlreadyExists = true;
3223 break;
3224 }
3225 }
3226 // If we already have it, ignore the variant.
3227 if (AlreadyExists) continue;
3228
3229 // Otherwise, add it to the list of patterns we have.
3230 PatternsToMatch.
Jim Grosbach997759a2010-12-07 23:05:49 +00003231 push_back(PatternToMatch(PatternsToMatch[i].getSrcRecord(),
3232 PatternsToMatch[i].getPredicates(),
Chris Lattner6cefb772008-01-05 22:25:12 +00003233 Variant, PatternsToMatch[i].getDstPattern(),
3234 PatternsToMatch[i].getDstRegs(),
Chris Lattner117ccb72010-03-01 22:09:11 +00003235 PatternsToMatch[i].getAddedComplexity(),
3236 Record::getNewUID()));
Chris Lattner6cefb772008-01-05 22:25:12 +00003237 }
3238
Chris Lattner569f1212009-08-23 04:44:11 +00003239 DEBUG(errs() << "\n");
Chris Lattner6cefb772008-01-05 22:25:12 +00003240 }
3241}
3242