1f22ef01cSRoman Divacky //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements the CodeGenDAGPatterns class, which is used to read and
11f22ef01cSRoman Divacky // represent the patterns present in a .td file for instructions.
12f22ef01cSRoman Divacky //
13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
14f22ef01cSRoman Divacky 
15f22ef01cSRoman Divacky #include "CodeGenDAGPatterns.h"
16f22ef01cSRoman Divacky #include "Record.h"
17f22ef01cSRoman Divacky #include "llvm/ADT/StringExtras.h"
18f22ef01cSRoman Divacky #include "llvm/ADT/STLExtras.h"
19f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
20f22ef01cSRoman Divacky #include <set>
21f22ef01cSRoman Divacky #include <algorithm>
22f22ef01cSRoman Divacky using namespace llvm;
23f22ef01cSRoman Divacky 
24f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
25f22ef01cSRoman Divacky //  EEVT::TypeSet Implementation
26f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
27f22ef01cSRoman Divacky 
28f22ef01cSRoman Divacky static inline bool isInteger(MVT::SimpleValueType VT) {
29f22ef01cSRoman Divacky   return EVT(VT).isInteger();
30f22ef01cSRoman Divacky }
31f22ef01cSRoman Divacky static inline bool isFloatingPoint(MVT::SimpleValueType VT) {
32f22ef01cSRoman Divacky   return EVT(VT).isFloatingPoint();
33f22ef01cSRoman Divacky }
34f22ef01cSRoman Divacky static inline bool isVector(MVT::SimpleValueType VT) {
35f22ef01cSRoman Divacky   return EVT(VT).isVector();
36f22ef01cSRoman Divacky }
37f22ef01cSRoman Divacky static inline bool isScalar(MVT::SimpleValueType VT) {
38f22ef01cSRoman Divacky   return !EVT(VT).isVector();
39f22ef01cSRoman Divacky }
40f22ef01cSRoman Divacky 
41f22ef01cSRoman Divacky EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) {
42f22ef01cSRoman Divacky   if (VT == MVT::iAny)
43f22ef01cSRoman Divacky     EnforceInteger(TP);
44f22ef01cSRoman Divacky   else if (VT == MVT::fAny)
45f22ef01cSRoman Divacky     EnforceFloatingPoint(TP);
46f22ef01cSRoman Divacky   else if (VT == MVT::vAny)
47f22ef01cSRoman Divacky     EnforceVector(TP);
48f22ef01cSRoman Divacky   else {
49f22ef01cSRoman Divacky     assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR ||
50f22ef01cSRoman Divacky             VT == MVT::iPTRAny) && "Not a concrete type!");
51f22ef01cSRoman Divacky     TypeVec.push_back(VT);
52f22ef01cSRoman Divacky   }
53f22ef01cSRoman Divacky }
54f22ef01cSRoman Divacky 
55f22ef01cSRoman Divacky 
56f22ef01cSRoman Divacky EEVT::TypeSet::TypeSet(const std::vector<MVT::SimpleValueType> &VTList) {
57f22ef01cSRoman Divacky   assert(!VTList.empty() && "empty list?");
58f22ef01cSRoman Divacky   TypeVec.append(VTList.begin(), VTList.end());
59f22ef01cSRoman Divacky 
60f22ef01cSRoman Divacky   if (!VTList.empty())
61f22ef01cSRoman Divacky     assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny &&
62f22ef01cSRoman Divacky            VTList[0] != MVT::fAny);
63f22ef01cSRoman Divacky 
64f22ef01cSRoman Divacky   // Verify no duplicates.
65f22ef01cSRoman Divacky   array_pod_sort(TypeVec.begin(), TypeVec.end());
66f22ef01cSRoman Divacky   assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end());
67f22ef01cSRoman Divacky }
68f22ef01cSRoman Divacky 
69f22ef01cSRoman Divacky /// FillWithPossibleTypes - Set to all legal types and return true, only valid
70f22ef01cSRoman Divacky /// on completely unknown type sets.
71f22ef01cSRoman Divacky bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP,
72f22ef01cSRoman Divacky                                           bool (*Pred)(MVT::SimpleValueType),
73f22ef01cSRoman Divacky                                           const char *PredicateName) {
74f22ef01cSRoman Divacky   assert(isCompletelyUnknown());
75f22ef01cSRoman Divacky   const std::vector<MVT::SimpleValueType> &LegalTypes =
76f22ef01cSRoman Divacky     TP.getDAGPatterns().getTargetInfo().getLegalValueTypes();
77f22ef01cSRoman Divacky 
78f22ef01cSRoman Divacky   for (unsigned i = 0, e = LegalTypes.size(); i != e; ++i)
79f22ef01cSRoman Divacky     if (Pred == 0 || Pred(LegalTypes[i]))
80f22ef01cSRoman Divacky       TypeVec.push_back(LegalTypes[i]);
81f22ef01cSRoman Divacky 
82f22ef01cSRoman Divacky   // If we have nothing that matches the predicate, bail out.
83f22ef01cSRoman Divacky   if (TypeVec.empty())
84f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, no " +
85f22ef01cSRoman Divacky              std::string(PredicateName) + " types found");
86f22ef01cSRoman Divacky   // No need to sort with one element.
87f22ef01cSRoman Divacky   if (TypeVec.size() == 1) return true;
88f22ef01cSRoman Divacky 
89f22ef01cSRoman Divacky   // Remove duplicates.
90f22ef01cSRoman Divacky   array_pod_sort(TypeVec.begin(), TypeVec.end());
91f22ef01cSRoman Divacky   TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end());
92f22ef01cSRoman Divacky 
93f22ef01cSRoman Divacky   return true;
94f22ef01cSRoman Divacky }
95f22ef01cSRoman Divacky 
96f22ef01cSRoman Divacky /// hasIntegerTypes - Return true if this TypeSet contains iAny or an
97f22ef01cSRoman Divacky /// integer value type.
98f22ef01cSRoman Divacky bool EEVT::TypeSet::hasIntegerTypes() const {
99f22ef01cSRoman Divacky   for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
100f22ef01cSRoman Divacky     if (isInteger(TypeVec[i]))
101f22ef01cSRoman Divacky       return true;
102f22ef01cSRoman Divacky   return false;
103f22ef01cSRoman Divacky }
104f22ef01cSRoman Divacky 
105f22ef01cSRoman Divacky /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
106f22ef01cSRoman Divacky /// a floating point value type.
107f22ef01cSRoman Divacky bool EEVT::TypeSet::hasFloatingPointTypes() const {
108f22ef01cSRoman Divacky   for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
109f22ef01cSRoman Divacky     if (isFloatingPoint(TypeVec[i]))
110f22ef01cSRoman Divacky       return true;
111f22ef01cSRoman Divacky   return false;
112f22ef01cSRoman Divacky }
113f22ef01cSRoman Divacky 
114f22ef01cSRoman Divacky /// hasVectorTypes - Return true if this TypeSet contains a vAny or a vector
115f22ef01cSRoman Divacky /// value type.
116f22ef01cSRoman Divacky bool EEVT::TypeSet::hasVectorTypes() const {
117f22ef01cSRoman Divacky   for (unsigned i = 0, e = TypeVec.size(); i != e; ++i)
118f22ef01cSRoman Divacky     if (isVector(TypeVec[i]))
119f22ef01cSRoman Divacky       return true;
120f22ef01cSRoman Divacky   return false;
121f22ef01cSRoman Divacky }
122f22ef01cSRoman Divacky 
123f22ef01cSRoman Divacky 
124f22ef01cSRoman Divacky std::string EEVT::TypeSet::getName() const {
125f22ef01cSRoman Divacky   if (TypeVec.empty()) return "<empty>";
126f22ef01cSRoman Divacky 
127f22ef01cSRoman Divacky   std::string Result;
128f22ef01cSRoman Divacky 
129f22ef01cSRoman Divacky   for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) {
130f22ef01cSRoman Divacky     std::string VTName = llvm::getEnumName(TypeVec[i]);
131f22ef01cSRoman Divacky     // Strip off MVT:: prefix if present.
132f22ef01cSRoman Divacky     if (VTName.substr(0,5) == "MVT::")
133f22ef01cSRoman Divacky       VTName = VTName.substr(5);
134f22ef01cSRoman Divacky     if (i) Result += ':';
135f22ef01cSRoman Divacky     Result += VTName;
136f22ef01cSRoman Divacky   }
137f22ef01cSRoman Divacky 
138f22ef01cSRoman Divacky   if (TypeVec.size() == 1)
139f22ef01cSRoman Divacky     return Result;
140f22ef01cSRoman Divacky   return "{" + Result + "}";
141f22ef01cSRoman Divacky }
142f22ef01cSRoman Divacky 
143f22ef01cSRoman Divacky /// MergeInTypeInfo - This merges in type information from the specified
144f22ef01cSRoman Divacky /// argument.  If 'this' changes, it returns true.  If the two types are
145f22ef01cSRoman Divacky /// contradictory (e.g. merge f32 into i32) then this throws an exception.
146f22ef01cSRoman Divacky bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){
147f22ef01cSRoman Divacky   if (InVT.isCompletelyUnknown() || *this == InVT)
148f22ef01cSRoman Divacky     return false;
149f22ef01cSRoman Divacky 
150f22ef01cSRoman Divacky   if (isCompletelyUnknown()) {
151f22ef01cSRoman Divacky     *this = InVT;
152f22ef01cSRoman Divacky     return true;
153f22ef01cSRoman Divacky   }
154f22ef01cSRoman Divacky 
155f22ef01cSRoman Divacky   assert(TypeVec.size() >= 1 && InVT.TypeVec.size() >= 1 && "No unknowns");
156f22ef01cSRoman Divacky 
157f22ef01cSRoman Divacky   // Handle the abstract cases, seeing if we can resolve them better.
158f22ef01cSRoman Divacky   switch (TypeVec[0]) {
159f22ef01cSRoman Divacky   default: break;
160f22ef01cSRoman Divacky   case MVT::iPTR:
161f22ef01cSRoman Divacky   case MVT::iPTRAny:
162f22ef01cSRoman Divacky     if (InVT.hasIntegerTypes()) {
163f22ef01cSRoman Divacky       EEVT::TypeSet InCopy(InVT);
164f22ef01cSRoman Divacky       InCopy.EnforceInteger(TP);
165f22ef01cSRoman Divacky       InCopy.EnforceScalar(TP);
166f22ef01cSRoman Divacky 
167f22ef01cSRoman Divacky       if (InCopy.isConcrete()) {
168f22ef01cSRoman Divacky         // If the RHS has one integer type, upgrade iPTR to i32.
169f22ef01cSRoman Divacky         TypeVec[0] = InVT.TypeVec[0];
170f22ef01cSRoman Divacky         return true;
171f22ef01cSRoman Divacky       }
172f22ef01cSRoman Divacky 
173f22ef01cSRoman Divacky       // If the input has multiple scalar integers, this doesn't add any info.
174f22ef01cSRoman Divacky       if (!InCopy.isCompletelyUnknown())
175f22ef01cSRoman Divacky         return false;
176f22ef01cSRoman Divacky     }
177f22ef01cSRoman Divacky     break;
178f22ef01cSRoman Divacky   }
179f22ef01cSRoman Divacky 
180f22ef01cSRoman Divacky   // If the input constraint is iAny/iPTR and this is an integer type list,
181f22ef01cSRoman Divacky   // remove non-integer types from the list.
182f22ef01cSRoman Divacky   if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) &&
183f22ef01cSRoman Divacky       hasIntegerTypes()) {
184f22ef01cSRoman Divacky     bool MadeChange = EnforceInteger(TP);
185f22ef01cSRoman Divacky 
186f22ef01cSRoman Divacky     // If we're merging in iPTR/iPTRAny and the node currently has a list of
187f22ef01cSRoman Divacky     // multiple different integer types, replace them with a single iPTR.
188f22ef01cSRoman Divacky     if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) &&
189f22ef01cSRoman Divacky         TypeVec.size() != 1) {
190f22ef01cSRoman Divacky       TypeVec.resize(1);
191f22ef01cSRoman Divacky       TypeVec[0] = InVT.TypeVec[0];
192f22ef01cSRoman Divacky       MadeChange = true;
193f22ef01cSRoman Divacky     }
194f22ef01cSRoman Divacky 
195f22ef01cSRoman Divacky     return MadeChange;
196f22ef01cSRoman Divacky   }
197f22ef01cSRoman Divacky 
198f22ef01cSRoman Divacky   // If this is a type list and the RHS is a typelist as well, eliminate entries
199f22ef01cSRoman Divacky   // from this list that aren't in the other one.
200f22ef01cSRoman Divacky   bool MadeChange = false;
201f22ef01cSRoman Divacky   TypeSet InputSet(*this);
202f22ef01cSRoman Divacky 
203f22ef01cSRoman Divacky   for (unsigned i = 0; i != TypeVec.size(); ++i) {
204f22ef01cSRoman Divacky     bool InInVT = false;
205f22ef01cSRoman Divacky     for (unsigned j = 0, e = InVT.TypeVec.size(); j != e; ++j)
206f22ef01cSRoman Divacky       if (TypeVec[i] == InVT.TypeVec[j]) {
207f22ef01cSRoman Divacky         InInVT = true;
208f22ef01cSRoman Divacky         break;
209f22ef01cSRoman Divacky       }
210f22ef01cSRoman Divacky 
211f22ef01cSRoman Divacky     if (InInVT) continue;
212f22ef01cSRoman Divacky     TypeVec.erase(TypeVec.begin()+i--);
213f22ef01cSRoman Divacky     MadeChange = true;
214f22ef01cSRoman Divacky   }
215f22ef01cSRoman Divacky 
216f22ef01cSRoman Divacky   // If we removed all of our types, we have a type contradiction.
217f22ef01cSRoman Divacky   if (!TypeVec.empty())
218f22ef01cSRoman Divacky     return MadeChange;
219f22ef01cSRoman Divacky 
220f22ef01cSRoman Divacky   // FIXME: Really want an SMLoc here!
221f22ef01cSRoman Divacky   TP.error("Type inference contradiction found, merging '" +
222f22ef01cSRoman Divacky            InVT.getName() + "' into '" + InputSet.getName() + "'");
223f22ef01cSRoman Divacky   return true; // unreachable
224f22ef01cSRoman Divacky }
225f22ef01cSRoman Divacky 
226f22ef01cSRoman Divacky /// EnforceInteger - Remove all non-integer types from this set.
227f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) {
228f22ef01cSRoman Divacky   // If we know nothing, then get the full set.
229f22ef01cSRoman Divacky   if (TypeVec.empty())
230f22ef01cSRoman Divacky     return FillWithPossibleTypes(TP, isInteger, "integer");
231f22ef01cSRoman Divacky   if (!hasFloatingPointTypes())
232f22ef01cSRoman Divacky     return false;
233f22ef01cSRoman Divacky 
234f22ef01cSRoman Divacky   TypeSet InputSet(*this);
235f22ef01cSRoman Divacky 
236f22ef01cSRoman Divacky   // Filter out all the fp types.
237f22ef01cSRoman Divacky   for (unsigned i = 0; i != TypeVec.size(); ++i)
238f22ef01cSRoman Divacky     if (!isInteger(TypeVec[i]))
239f22ef01cSRoman Divacky       TypeVec.erase(TypeVec.begin()+i--);
240f22ef01cSRoman Divacky 
241f22ef01cSRoman Divacky   if (TypeVec.empty())
242f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, '" +
243f22ef01cSRoman Divacky              InputSet.getName() + "' needs to be integer");
244f22ef01cSRoman Divacky   return true;
245f22ef01cSRoman Divacky }
246f22ef01cSRoman Divacky 
247f22ef01cSRoman Divacky /// EnforceFloatingPoint - Remove all integer types from this set.
248f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) {
249f22ef01cSRoman Divacky   // If we know nothing, then get the full set.
250f22ef01cSRoman Divacky   if (TypeVec.empty())
251f22ef01cSRoman Divacky     return FillWithPossibleTypes(TP, isFloatingPoint, "floating point");
252f22ef01cSRoman Divacky 
253f22ef01cSRoman Divacky   if (!hasIntegerTypes())
254f22ef01cSRoman Divacky     return false;
255f22ef01cSRoman Divacky 
256f22ef01cSRoman Divacky   TypeSet InputSet(*this);
257f22ef01cSRoman Divacky 
258f22ef01cSRoman Divacky   // Filter out all the fp types.
259f22ef01cSRoman Divacky   for (unsigned i = 0; i != TypeVec.size(); ++i)
260f22ef01cSRoman Divacky     if (!isFloatingPoint(TypeVec[i]))
261f22ef01cSRoman Divacky       TypeVec.erase(TypeVec.begin()+i--);
262f22ef01cSRoman Divacky 
263f22ef01cSRoman Divacky   if (TypeVec.empty())
264f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, '" +
265f22ef01cSRoman Divacky              InputSet.getName() + "' needs to be floating point");
266f22ef01cSRoman Divacky   return true;
267f22ef01cSRoman Divacky }
268f22ef01cSRoman Divacky 
269f22ef01cSRoman Divacky /// EnforceScalar - Remove all vector types from this.
270f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) {
271f22ef01cSRoman Divacky   // If we know nothing, then get the full set.
272f22ef01cSRoman Divacky   if (TypeVec.empty())
273f22ef01cSRoman Divacky     return FillWithPossibleTypes(TP, isScalar, "scalar");
274f22ef01cSRoman Divacky 
275f22ef01cSRoman Divacky   if (!hasVectorTypes())
276f22ef01cSRoman Divacky     return false;
277f22ef01cSRoman Divacky 
278f22ef01cSRoman Divacky   TypeSet InputSet(*this);
279f22ef01cSRoman Divacky 
280f22ef01cSRoman Divacky   // Filter out all the vector types.
281f22ef01cSRoman Divacky   for (unsigned i = 0; i != TypeVec.size(); ++i)
282f22ef01cSRoman Divacky     if (!isScalar(TypeVec[i]))
283f22ef01cSRoman Divacky       TypeVec.erase(TypeVec.begin()+i--);
284f22ef01cSRoman Divacky 
285f22ef01cSRoman Divacky   if (TypeVec.empty())
286f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, '" +
287f22ef01cSRoman Divacky              InputSet.getName() + "' needs to be scalar");
288f22ef01cSRoman Divacky   return true;
289f22ef01cSRoman Divacky }
290f22ef01cSRoman Divacky 
291f22ef01cSRoman Divacky /// EnforceVector - Remove all vector types from this.
292f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceVector(TreePattern &TP) {
293f22ef01cSRoman Divacky   // If we know nothing, then get the full set.
294f22ef01cSRoman Divacky   if (TypeVec.empty())
295f22ef01cSRoman Divacky     return FillWithPossibleTypes(TP, isVector, "vector");
296f22ef01cSRoman Divacky 
297f22ef01cSRoman Divacky   TypeSet InputSet(*this);
298f22ef01cSRoman Divacky   bool MadeChange = false;
299f22ef01cSRoman Divacky 
300f22ef01cSRoman Divacky   // Filter out all the scalar types.
301f22ef01cSRoman Divacky   for (unsigned i = 0; i != TypeVec.size(); ++i)
302f22ef01cSRoman Divacky     if (!isVector(TypeVec[i])) {
303f22ef01cSRoman Divacky       TypeVec.erase(TypeVec.begin()+i--);
304f22ef01cSRoman Divacky       MadeChange = true;
305f22ef01cSRoman Divacky     }
306f22ef01cSRoman Divacky 
307f22ef01cSRoman Divacky   if (TypeVec.empty())
308f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, '" +
309f22ef01cSRoman Divacky              InputSet.getName() + "' needs to be a vector");
310f22ef01cSRoman Divacky   return MadeChange;
311f22ef01cSRoman Divacky }
312f22ef01cSRoman Divacky 
313f22ef01cSRoman Divacky 
314f22ef01cSRoman Divacky 
315f22ef01cSRoman Divacky /// EnforceSmallerThan - 'this' must be a smaller VT than Other.  Update
316f22ef01cSRoman Divacky /// this an other based on this information.
317f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) {
318f22ef01cSRoman Divacky   // Both operands must be integer or FP, but we don't care which.
319f22ef01cSRoman Divacky   bool MadeChange = false;
320f22ef01cSRoman Divacky 
321f22ef01cSRoman Divacky   if (isCompletelyUnknown())
322f22ef01cSRoman Divacky     MadeChange = FillWithPossibleTypes(TP);
323f22ef01cSRoman Divacky 
324f22ef01cSRoman Divacky   if (Other.isCompletelyUnknown())
325f22ef01cSRoman Divacky     MadeChange = Other.FillWithPossibleTypes(TP);
326f22ef01cSRoman Divacky 
327f22ef01cSRoman Divacky   // If one side is known to be integer or known to be FP but the other side has
328f22ef01cSRoman Divacky   // no information, get at least the type integrality info in there.
329f22ef01cSRoman Divacky   if (!hasFloatingPointTypes())
330f22ef01cSRoman Divacky     MadeChange |= Other.EnforceInteger(TP);
331f22ef01cSRoman Divacky   else if (!hasIntegerTypes())
332f22ef01cSRoman Divacky     MadeChange |= Other.EnforceFloatingPoint(TP);
333f22ef01cSRoman Divacky   if (!Other.hasFloatingPointTypes())
334f22ef01cSRoman Divacky     MadeChange |= EnforceInteger(TP);
335f22ef01cSRoman Divacky   else if (!Other.hasIntegerTypes())
336f22ef01cSRoman Divacky     MadeChange |= EnforceFloatingPoint(TP);
337f22ef01cSRoman Divacky 
338f22ef01cSRoman Divacky   assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() &&
339f22ef01cSRoman Divacky          "Should have a type list now");
340f22ef01cSRoman Divacky 
341f22ef01cSRoman Divacky   // If one contains vectors but the other doesn't pull vectors out.
342f22ef01cSRoman Divacky   if (!hasVectorTypes())
343f22ef01cSRoman Divacky     MadeChange |= Other.EnforceScalar(TP);
344f22ef01cSRoman Divacky   if (!hasVectorTypes())
345f22ef01cSRoman Divacky     MadeChange |= EnforceScalar(TP);
346f22ef01cSRoman Divacky 
347f22ef01cSRoman Divacky   // This code does not currently handle nodes which have multiple types,
348f22ef01cSRoman Divacky   // where some types are integer, and some are fp.  Assert that this is not
349f22ef01cSRoman Divacky   // the case.
350f22ef01cSRoman Divacky   assert(!(hasIntegerTypes() && hasFloatingPointTypes()) &&
351f22ef01cSRoman Divacky          !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) &&
352f22ef01cSRoman Divacky          "SDTCisOpSmallerThanOp does not handle mixed int/fp types!");
353f22ef01cSRoman Divacky 
354f22ef01cSRoman Divacky   // Okay, find the smallest type from the current set and remove it from the
355f22ef01cSRoman Divacky   // largest set.
356f22ef01cSRoman Divacky   MVT::SimpleValueType Smallest = TypeVec[0];
357f22ef01cSRoman Divacky   for (unsigned i = 1, e = TypeVec.size(); i != e; ++i)
358f22ef01cSRoman Divacky     if (TypeVec[i] < Smallest)
359f22ef01cSRoman Divacky       Smallest = TypeVec[i];
360f22ef01cSRoman Divacky 
361f22ef01cSRoman Divacky   // If this is the only type in the large set, the constraint can never be
362f22ef01cSRoman Divacky   // satisfied.
363f22ef01cSRoman Divacky   if (Other.TypeVec.size() == 1 && Other.TypeVec[0] == Smallest)
364f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, '" +
365f22ef01cSRoman Divacky              Other.getName() + "' has nothing larger than '" + getName() +"'!");
366f22ef01cSRoman Divacky 
367f22ef01cSRoman Divacky   SmallVector<MVT::SimpleValueType, 2>::iterator TVI =
368f22ef01cSRoman Divacky     std::find(Other.TypeVec.begin(), Other.TypeVec.end(), Smallest);
369f22ef01cSRoman Divacky   if (TVI != Other.TypeVec.end()) {
370f22ef01cSRoman Divacky     Other.TypeVec.erase(TVI);
371f22ef01cSRoman Divacky     MadeChange = true;
372f22ef01cSRoman Divacky   }
373f22ef01cSRoman Divacky 
374f22ef01cSRoman Divacky   // Okay, find the largest type in the Other set and remove it from the
375f22ef01cSRoman Divacky   // current set.
376f22ef01cSRoman Divacky   MVT::SimpleValueType Largest = Other.TypeVec[0];
377f22ef01cSRoman Divacky   for (unsigned i = 1, e = Other.TypeVec.size(); i != e; ++i)
378f22ef01cSRoman Divacky     if (Other.TypeVec[i] > Largest)
379f22ef01cSRoman Divacky       Largest = Other.TypeVec[i];
380f22ef01cSRoman Divacky 
381f22ef01cSRoman Divacky   // If this is the only type in the small set, the constraint can never be
382f22ef01cSRoman Divacky   // satisfied.
383f22ef01cSRoman Divacky   if (TypeVec.size() == 1 && TypeVec[0] == Largest)
384f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, '" +
385f22ef01cSRoman Divacky              getName() + "' has nothing smaller than '" + Other.getName()+"'!");
386f22ef01cSRoman Divacky 
387f22ef01cSRoman Divacky   TVI = std::find(TypeVec.begin(), TypeVec.end(), Largest);
388f22ef01cSRoman Divacky   if (TVI != TypeVec.end()) {
389f22ef01cSRoman Divacky     TypeVec.erase(TVI);
390f22ef01cSRoman Divacky     MadeChange = true;
391f22ef01cSRoman Divacky   }
392f22ef01cSRoman Divacky 
393f22ef01cSRoman Divacky   return MadeChange;
394f22ef01cSRoman Divacky }
395f22ef01cSRoman Divacky 
396f22ef01cSRoman Divacky /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
397f22ef01cSRoman Divacky /// whose element is specified by VTOperand.
398f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand,
399f22ef01cSRoman Divacky                                            TreePattern &TP) {
400f22ef01cSRoman Divacky   // "This" must be a vector and "VTOperand" must be a scalar.
401f22ef01cSRoman Divacky   bool MadeChange = false;
402f22ef01cSRoman Divacky   MadeChange |= EnforceVector(TP);
403f22ef01cSRoman Divacky   MadeChange |= VTOperand.EnforceScalar(TP);
404f22ef01cSRoman Divacky 
405f22ef01cSRoman Divacky   // If we know the vector type, it forces the scalar to agree.
406f22ef01cSRoman Divacky   if (isConcrete()) {
407f22ef01cSRoman Divacky     EVT IVT = getConcrete();
408f22ef01cSRoman Divacky     IVT = IVT.getVectorElementType();
409f22ef01cSRoman Divacky     return MadeChange |
410f22ef01cSRoman Divacky       VTOperand.MergeInTypeInfo(IVT.getSimpleVT().SimpleTy, TP);
411f22ef01cSRoman Divacky   }
412f22ef01cSRoman Divacky 
413f22ef01cSRoman Divacky   // If the scalar type is known, filter out vector types whose element types
414f22ef01cSRoman Divacky   // disagree.
415f22ef01cSRoman Divacky   if (!VTOperand.isConcrete())
416f22ef01cSRoman Divacky     return MadeChange;
417f22ef01cSRoman Divacky 
418f22ef01cSRoman Divacky   MVT::SimpleValueType VT = VTOperand.getConcrete();
419f22ef01cSRoman Divacky 
420f22ef01cSRoman Divacky   TypeSet InputSet(*this);
421f22ef01cSRoman Divacky 
422f22ef01cSRoman Divacky   // Filter out all the types which don't have the right element type.
423f22ef01cSRoman Divacky   for (unsigned i = 0; i != TypeVec.size(); ++i) {
424f22ef01cSRoman Divacky     assert(isVector(TypeVec[i]) && "EnforceVector didn't work");
425f22ef01cSRoman Divacky     if (EVT(TypeVec[i]).getVectorElementType().getSimpleVT().SimpleTy != VT) {
426f22ef01cSRoman Divacky       TypeVec.erase(TypeVec.begin()+i--);
427f22ef01cSRoman Divacky       MadeChange = true;
428f22ef01cSRoman Divacky     }
429f22ef01cSRoman Divacky   }
430f22ef01cSRoman Divacky 
431f22ef01cSRoman Divacky   if (TypeVec.empty())  // FIXME: Really want an SMLoc here!
432f22ef01cSRoman Divacky     TP.error("Type inference contradiction found, forcing '" +
433f22ef01cSRoman Divacky              InputSet.getName() + "' to have a vector element");
434f22ef01cSRoman Divacky   return MadeChange;
435f22ef01cSRoman Divacky }
436f22ef01cSRoman Divacky 
437f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
438f22ef01cSRoman Divacky // Helpers for working with extended types.
439f22ef01cSRoman Divacky 
440f22ef01cSRoman Divacky bool RecordPtrCmp::operator()(const Record *LHS, const Record *RHS) const {
441f22ef01cSRoman Divacky   return LHS->getID() < RHS->getID();
442f22ef01cSRoman Divacky }
443f22ef01cSRoman Divacky 
444f22ef01cSRoman Divacky /// Dependent variable map for CodeGenDAGPattern variant generation
445f22ef01cSRoman Divacky typedef std::map<std::string, int> DepVarMap;
446f22ef01cSRoman Divacky 
447f22ef01cSRoman Divacky /// Const iterator shorthand for DepVarMap
448f22ef01cSRoman Divacky typedef DepVarMap::const_iterator DepVarMap_citer;
449f22ef01cSRoman Divacky 
450f22ef01cSRoman Divacky namespace {
451f22ef01cSRoman Divacky void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
452f22ef01cSRoman Divacky   if (N->isLeaf()) {
453f22ef01cSRoman Divacky     if (dynamic_cast<DefInit*>(N->getLeafValue()) != NULL) {
454f22ef01cSRoman Divacky       DepMap[N->getName()]++;
455f22ef01cSRoman Divacky     }
456f22ef01cSRoman Divacky   } else {
457f22ef01cSRoman Divacky     for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
458f22ef01cSRoman Divacky       FindDepVarsOf(N->getChild(i), DepMap);
459f22ef01cSRoman Divacky   }
460f22ef01cSRoman Divacky }
461f22ef01cSRoman Divacky 
462f22ef01cSRoman Divacky //! Find dependent variables within child patterns
463f22ef01cSRoman Divacky /*!
464f22ef01cSRoman Divacky  */
465f22ef01cSRoman Divacky void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
466f22ef01cSRoman Divacky   DepVarMap depcounts;
467f22ef01cSRoman Divacky   FindDepVarsOf(N, depcounts);
468f22ef01cSRoman Divacky   for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) {
469f22ef01cSRoman Divacky     if (i->second > 1) {            // std::pair<std::string, int>
470f22ef01cSRoman Divacky       DepVars.insert(i->first);
471f22ef01cSRoman Divacky     }
472f22ef01cSRoman Divacky   }
473f22ef01cSRoman Divacky }
474f22ef01cSRoman Divacky 
475f22ef01cSRoman Divacky //! Dump the dependent variable set:
476f22ef01cSRoman Divacky void DumpDepVars(MultipleUseVarSet &DepVars) {
477f22ef01cSRoman Divacky   if (DepVars.empty()) {
478f22ef01cSRoman Divacky     DEBUG(errs() << "<empty set>");
479f22ef01cSRoman Divacky   } else {
480f22ef01cSRoman Divacky     DEBUG(errs() << "[ ");
481f22ef01cSRoman Divacky     for (MultipleUseVarSet::const_iterator i = DepVars.begin(), e = DepVars.end();
482f22ef01cSRoman Divacky          i != e; ++i) {
483f22ef01cSRoman Divacky       DEBUG(errs() << (*i) << " ");
484f22ef01cSRoman Divacky     }
485f22ef01cSRoman Divacky     DEBUG(errs() << "]");
486f22ef01cSRoman Divacky   }
487f22ef01cSRoman Divacky }
488f22ef01cSRoman Divacky }
489f22ef01cSRoman Divacky 
490f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
491f22ef01cSRoman Divacky // PatternToMatch implementation
492f22ef01cSRoman Divacky //
493f22ef01cSRoman Divacky 
494f22ef01cSRoman Divacky 
495f22ef01cSRoman Divacky /// getPatternSize - Return the 'size' of this pattern.  We want to match large
496f22ef01cSRoman Divacky /// patterns before small ones.  This is used to determine the size of a
497f22ef01cSRoman Divacky /// pattern.
498f22ef01cSRoman Divacky static unsigned getPatternSize(const TreePatternNode *P,
499f22ef01cSRoman Divacky                                const CodeGenDAGPatterns &CGP) {
500f22ef01cSRoman Divacky   unsigned Size = 3;  // The node itself.
501f22ef01cSRoman Divacky   // If the root node is a ConstantSDNode, increases its size.
502f22ef01cSRoman Divacky   // e.g. (set R32:$dst, 0).
503f22ef01cSRoman Divacky   if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
504f22ef01cSRoman Divacky     Size += 2;
505f22ef01cSRoman Divacky 
506f22ef01cSRoman Divacky   // FIXME: This is a hack to statically increase the priority of patterns
507f22ef01cSRoman Divacky   // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
508f22ef01cSRoman Divacky   // Later we can allow complexity / cost for each pattern to be (optionally)
509f22ef01cSRoman Divacky   // specified. To get best possible pattern match we'll need to dynamically
510f22ef01cSRoman Divacky   // calculate the complexity of all patterns a dag can potentially map to.
511f22ef01cSRoman Divacky   const ComplexPattern *AM = P->getComplexPatternInfo(CGP);
512f22ef01cSRoman Divacky   if (AM)
513f22ef01cSRoman Divacky     Size += AM->getNumOperands() * 3;
514f22ef01cSRoman Divacky 
515f22ef01cSRoman Divacky   // If this node has some predicate function that must match, it adds to the
516f22ef01cSRoman Divacky   // complexity of this node.
517f22ef01cSRoman Divacky   if (!P->getPredicateFns().empty())
518f22ef01cSRoman Divacky     ++Size;
519f22ef01cSRoman Divacky 
520f22ef01cSRoman Divacky   // Count children in the count if they are also nodes.
521f22ef01cSRoman Divacky   for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
522f22ef01cSRoman Divacky     TreePatternNode *Child = P->getChild(i);
523f22ef01cSRoman Divacky     if (!Child->isLeaf() && Child->getNumTypes() &&
524f22ef01cSRoman Divacky         Child->getType(0) != MVT::Other)
525f22ef01cSRoman Divacky       Size += getPatternSize(Child, CGP);
526f22ef01cSRoman Divacky     else if (Child->isLeaf()) {
527f22ef01cSRoman Divacky       if (dynamic_cast<IntInit*>(Child->getLeafValue()))
528f22ef01cSRoman Divacky         Size += 5;  // Matches a ConstantSDNode (+3) and a specific value (+2).
529f22ef01cSRoman Divacky       else if (Child->getComplexPatternInfo(CGP))
530f22ef01cSRoman Divacky         Size += getPatternSize(Child, CGP);
531f22ef01cSRoman Divacky       else if (!Child->getPredicateFns().empty())
532f22ef01cSRoman Divacky         ++Size;
533f22ef01cSRoman Divacky     }
534f22ef01cSRoman Divacky   }
535f22ef01cSRoman Divacky 
536f22ef01cSRoman Divacky   return Size;
537f22ef01cSRoman Divacky }
538f22ef01cSRoman Divacky 
539f22ef01cSRoman Divacky /// Compute the complexity metric for the input pattern.  This roughly
540f22ef01cSRoman Divacky /// corresponds to the number of nodes that are covered.
541f22ef01cSRoman Divacky unsigned PatternToMatch::
542f22ef01cSRoman Divacky getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
543f22ef01cSRoman Divacky   return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
544f22ef01cSRoman Divacky }
545f22ef01cSRoman Divacky 
546f22ef01cSRoman Divacky 
547f22ef01cSRoman Divacky /// getPredicateCheck - Return a single string containing all of this
548f22ef01cSRoman Divacky /// pattern's predicates concatenated with "&&" operators.
549f22ef01cSRoman Divacky ///
550f22ef01cSRoman Divacky std::string PatternToMatch::getPredicateCheck() const {
551f22ef01cSRoman Divacky   std::string PredicateCheck;
552f22ef01cSRoman Divacky   for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
553f22ef01cSRoman Divacky     if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
554f22ef01cSRoman Divacky       Record *Def = Pred->getDef();
555f22ef01cSRoman Divacky       if (!Def->isSubClassOf("Predicate")) {
556f22ef01cSRoman Divacky #ifndef NDEBUG
557f22ef01cSRoman Divacky         Def->dump();
558f22ef01cSRoman Divacky #endif
559f22ef01cSRoman Divacky         assert(0 && "Unknown predicate type!");
560f22ef01cSRoman Divacky       }
561f22ef01cSRoman Divacky       if (!PredicateCheck.empty())
562f22ef01cSRoman Divacky         PredicateCheck += " && ";
563f22ef01cSRoman Divacky       PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
564f22ef01cSRoman Divacky     }
565f22ef01cSRoman Divacky   }
566f22ef01cSRoman Divacky 
567f22ef01cSRoman Divacky   return PredicateCheck;
568f22ef01cSRoman Divacky }
569f22ef01cSRoman Divacky 
570f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
571f22ef01cSRoman Divacky // SDTypeConstraint implementation
572f22ef01cSRoman Divacky //
573f22ef01cSRoman Divacky 
574f22ef01cSRoman Divacky SDTypeConstraint::SDTypeConstraint(Record *R) {
575f22ef01cSRoman Divacky   OperandNo = R->getValueAsInt("OperandNum");
576f22ef01cSRoman Divacky 
577f22ef01cSRoman Divacky   if (R->isSubClassOf("SDTCisVT")) {
578f22ef01cSRoman Divacky     ConstraintType = SDTCisVT;
579f22ef01cSRoman Divacky     x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT"));
580f22ef01cSRoman Divacky     if (x.SDTCisVT_Info.VT == MVT::isVoid)
581f22ef01cSRoman Divacky       throw TGError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT");
582f22ef01cSRoman Divacky 
583f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisPtrTy")) {
584f22ef01cSRoman Divacky     ConstraintType = SDTCisPtrTy;
585f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisInt")) {
586f22ef01cSRoman Divacky     ConstraintType = SDTCisInt;
587f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisFP")) {
588f22ef01cSRoman Divacky     ConstraintType = SDTCisFP;
589f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisVec")) {
590f22ef01cSRoman Divacky     ConstraintType = SDTCisVec;
591f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisSameAs")) {
592f22ef01cSRoman Divacky     ConstraintType = SDTCisSameAs;
593f22ef01cSRoman Divacky     x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum");
594f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) {
595f22ef01cSRoman Divacky     ConstraintType = SDTCisVTSmallerThanOp;
596f22ef01cSRoman Divacky     x.SDTCisVTSmallerThanOp_Info.OtherOperandNum =
597f22ef01cSRoman Divacky       R->getValueAsInt("OtherOperandNum");
598f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) {
599f22ef01cSRoman Divacky     ConstraintType = SDTCisOpSmallerThanOp;
600f22ef01cSRoman Divacky     x.SDTCisOpSmallerThanOp_Info.BigOperandNum =
601f22ef01cSRoman Divacky       R->getValueAsInt("BigOperandNum");
602f22ef01cSRoman Divacky   } else if (R->isSubClassOf("SDTCisEltOfVec")) {
603f22ef01cSRoman Divacky     ConstraintType = SDTCisEltOfVec;
604f22ef01cSRoman Divacky     x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum");
605f22ef01cSRoman Divacky   } else {
606f22ef01cSRoman Divacky     errs() << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n";
607f22ef01cSRoman Divacky     exit(1);
608f22ef01cSRoman Divacky   }
609f22ef01cSRoman Divacky }
610f22ef01cSRoman Divacky 
611f22ef01cSRoman Divacky /// getOperandNum - Return the node corresponding to operand #OpNo in tree
612f22ef01cSRoman Divacky /// N, and the result number in ResNo.
613f22ef01cSRoman Divacky static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
614f22ef01cSRoman Divacky                                       const SDNodeInfo &NodeInfo,
615f22ef01cSRoman Divacky                                       unsigned &ResNo) {
616f22ef01cSRoman Divacky   unsigned NumResults = NodeInfo.getNumResults();
617f22ef01cSRoman Divacky   if (OpNo < NumResults) {
618f22ef01cSRoman Divacky     ResNo = OpNo;
619f22ef01cSRoman Divacky     return N;
620f22ef01cSRoman Divacky   }
621f22ef01cSRoman Divacky 
622f22ef01cSRoman Divacky   OpNo -= NumResults;
623f22ef01cSRoman Divacky 
624f22ef01cSRoman Divacky   if (OpNo >= N->getNumChildren()) {
625f22ef01cSRoman Divacky     errs() << "Invalid operand number in type constraint "
626f22ef01cSRoman Divacky            << (OpNo+NumResults) << " ";
627f22ef01cSRoman Divacky     N->dump();
628f22ef01cSRoman Divacky     errs() << '\n';
629f22ef01cSRoman Divacky     exit(1);
630f22ef01cSRoman Divacky   }
631f22ef01cSRoman Divacky 
632f22ef01cSRoman Divacky   return N->getChild(OpNo);
633f22ef01cSRoman Divacky }
634f22ef01cSRoman Divacky 
635f22ef01cSRoman Divacky /// ApplyTypeConstraint - Given a node in a pattern, apply this type
636f22ef01cSRoman Divacky /// constraint to the nodes operands.  This returns true if it makes a
637f22ef01cSRoman Divacky /// change, false otherwise.  If a type contradiction is found, throw an
638f22ef01cSRoman Divacky /// exception.
639f22ef01cSRoman Divacky bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
640f22ef01cSRoman Divacky                                            const SDNodeInfo &NodeInfo,
641f22ef01cSRoman Divacky                                            TreePattern &TP) const {
642f22ef01cSRoman Divacky   unsigned ResNo = 0; // The result number being referenced.
643f22ef01cSRoman Divacky   TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo);
644f22ef01cSRoman Divacky 
645f22ef01cSRoman Divacky   switch (ConstraintType) {
646f22ef01cSRoman Divacky   default: assert(0 && "Unknown constraint type!");
647f22ef01cSRoman Divacky   case SDTCisVT:
648f22ef01cSRoman Divacky     // Operand must be a particular type.
649f22ef01cSRoman Divacky     return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP);
650f22ef01cSRoman Divacky   case SDTCisPtrTy:
651f22ef01cSRoman Divacky     // Operand must be same as target pointer type.
652f22ef01cSRoman Divacky     return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP);
653f22ef01cSRoman Divacky   case SDTCisInt:
654f22ef01cSRoman Divacky     // Require it to be one of the legal integer VTs.
655f22ef01cSRoman Divacky     return NodeToApply->getExtType(ResNo).EnforceInteger(TP);
656f22ef01cSRoman Divacky   case SDTCisFP:
657f22ef01cSRoman Divacky     // Require it to be one of the legal fp VTs.
658f22ef01cSRoman Divacky     return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP);
659f22ef01cSRoman Divacky   case SDTCisVec:
660f22ef01cSRoman Divacky     // Require it to be one of the legal vector VTs.
661f22ef01cSRoman Divacky     return NodeToApply->getExtType(ResNo).EnforceVector(TP);
662f22ef01cSRoman Divacky   case SDTCisSameAs: {
663f22ef01cSRoman Divacky     unsigned OResNo = 0;
664f22ef01cSRoman Divacky     TreePatternNode *OtherNode =
665f22ef01cSRoman Divacky       getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo);
666f22ef01cSRoman Divacky     return NodeToApply->UpdateNodeType(OResNo, OtherNode->getExtType(ResNo),TP)|
667f22ef01cSRoman Divacky            OtherNode->UpdateNodeType(ResNo,NodeToApply->getExtType(OResNo),TP);
668f22ef01cSRoman Divacky   }
669f22ef01cSRoman Divacky   case SDTCisVTSmallerThanOp: {
670f22ef01cSRoman Divacky     // The NodeToApply must be a leaf node that is a VT.  OtherOperandNum must
671f22ef01cSRoman Divacky     // have an integer type that is smaller than the VT.
672f22ef01cSRoman Divacky     if (!NodeToApply->isLeaf() ||
673f22ef01cSRoman Divacky         !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) ||
674f22ef01cSRoman Divacky         !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()
675f22ef01cSRoman Divacky                ->isSubClassOf("ValueType"))
676f22ef01cSRoman Divacky       TP.error(N->getOperator()->getName() + " expects a VT operand!");
677f22ef01cSRoman Divacky     MVT::SimpleValueType VT =
678f22ef01cSRoman Divacky      getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef());
679f22ef01cSRoman Divacky 
680f22ef01cSRoman Divacky     EEVT::TypeSet TypeListTmp(VT, TP);
681f22ef01cSRoman Divacky 
682f22ef01cSRoman Divacky     unsigned OResNo = 0;
683f22ef01cSRoman Divacky     TreePatternNode *OtherNode =
684f22ef01cSRoman Divacky       getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo,
685f22ef01cSRoman Divacky                     OResNo);
686f22ef01cSRoman Divacky 
687f22ef01cSRoman Divacky     return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP);
688f22ef01cSRoman Divacky   }
689f22ef01cSRoman Divacky   case SDTCisOpSmallerThanOp: {
690f22ef01cSRoman Divacky     unsigned BResNo = 0;
691f22ef01cSRoman Divacky     TreePatternNode *BigOperand =
692f22ef01cSRoman Divacky       getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo,
693f22ef01cSRoman Divacky                     BResNo);
694f22ef01cSRoman Divacky     return NodeToApply->getExtType(ResNo).
695f22ef01cSRoman Divacky                   EnforceSmallerThan(BigOperand->getExtType(BResNo), TP);
696f22ef01cSRoman Divacky   }
697f22ef01cSRoman Divacky   case SDTCisEltOfVec: {
698f22ef01cSRoman Divacky     unsigned VResNo = 0;
699f22ef01cSRoman Divacky     TreePatternNode *VecOperand =
700f22ef01cSRoman Divacky       getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo,
701f22ef01cSRoman Divacky                     VResNo);
702f22ef01cSRoman Divacky 
703f22ef01cSRoman Divacky     // Filter vector types out of VecOperand that don't have the right element
704f22ef01cSRoman Divacky     // type.
705f22ef01cSRoman Divacky     return VecOperand->getExtType(VResNo).
706f22ef01cSRoman Divacky       EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP);
707f22ef01cSRoman Divacky   }
708f22ef01cSRoman Divacky   }
709f22ef01cSRoman Divacky   return false;
710f22ef01cSRoman Divacky }
711f22ef01cSRoman Divacky 
712f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
713f22ef01cSRoman Divacky // SDNodeInfo implementation
714f22ef01cSRoman Divacky //
715f22ef01cSRoman Divacky SDNodeInfo::SDNodeInfo(Record *R) : Def(R) {
716f22ef01cSRoman Divacky   EnumName    = R->getValueAsString("Opcode");
717f22ef01cSRoman Divacky   SDClassName = R->getValueAsString("SDClass");
718f22ef01cSRoman Divacky   Record *TypeProfile = R->getValueAsDef("TypeProfile");
719f22ef01cSRoman Divacky   NumResults = TypeProfile->getValueAsInt("NumResults");
720f22ef01cSRoman Divacky   NumOperands = TypeProfile->getValueAsInt("NumOperands");
721f22ef01cSRoman Divacky 
722f22ef01cSRoman Divacky   // Parse the properties.
723f22ef01cSRoman Divacky   Properties = 0;
724f22ef01cSRoman Divacky   std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
725f22ef01cSRoman Divacky   for (unsigned i = 0, e = PropList.size(); i != e; ++i) {
726f22ef01cSRoman Divacky     if (PropList[i]->getName() == "SDNPCommutative") {
727f22ef01cSRoman Divacky       Properties |= 1 << SDNPCommutative;
728f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPAssociative") {
729f22ef01cSRoman Divacky       Properties |= 1 << SDNPAssociative;
730f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPHasChain") {
731f22ef01cSRoman Divacky       Properties |= 1 << SDNPHasChain;
732f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPOutFlag") {
733f22ef01cSRoman Divacky       Properties |= 1 << SDNPOutFlag;
734f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPInFlag") {
735f22ef01cSRoman Divacky       Properties |= 1 << SDNPInFlag;
736f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPOptInFlag") {
737f22ef01cSRoman Divacky       Properties |= 1 << SDNPOptInFlag;
738f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPMayStore") {
739f22ef01cSRoman Divacky       Properties |= 1 << SDNPMayStore;
740f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPMayLoad") {
741f22ef01cSRoman Divacky       Properties |= 1 << SDNPMayLoad;
742f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPSideEffect") {
743f22ef01cSRoman Divacky       Properties |= 1 << SDNPSideEffect;
744f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPMemOperand") {
745f22ef01cSRoman Divacky       Properties |= 1 << SDNPMemOperand;
746f22ef01cSRoman Divacky     } else if (PropList[i]->getName() == "SDNPVariadic") {
747f22ef01cSRoman Divacky       Properties |= 1 << SDNPVariadic;
748f22ef01cSRoman Divacky     } else {
749f22ef01cSRoman Divacky       errs() << "Unknown SD Node property '" << PropList[i]->getName()
750f22ef01cSRoman Divacky              << "' on node '" << R->getName() << "'!\n";
751f22ef01cSRoman Divacky       exit(1);
752f22ef01cSRoman Divacky     }
753f22ef01cSRoman Divacky   }
754f22ef01cSRoman Divacky 
755f22ef01cSRoman Divacky 
756f22ef01cSRoman Divacky   // Parse the type constraints.
757f22ef01cSRoman Divacky   std::vector<Record*> ConstraintList =
758f22ef01cSRoman Divacky     TypeProfile->getValueAsListOfDefs("Constraints");
759f22ef01cSRoman Divacky   TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end());
760f22ef01cSRoman Divacky }
761f22ef01cSRoman Divacky 
762f22ef01cSRoman Divacky /// getKnownType - If the type constraints on this node imply a fixed type
763f22ef01cSRoman Divacky /// (e.g. all stores return void, etc), then return it as an
764f22ef01cSRoman Divacky /// MVT::SimpleValueType.  Otherwise, return EEVT::Other.
765f22ef01cSRoman Divacky MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
766f22ef01cSRoman Divacky   unsigned NumResults = getNumResults();
767f22ef01cSRoman Divacky   assert(NumResults <= 1 &&
768f22ef01cSRoman Divacky          "We only work with nodes with zero or one result so far!");
769f22ef01cSRoman Divacky   assert(ResNo == 0 && "Only handles single result nodes so far");
770f22ef01cSRoman Divacky 
771f22ef01cSRoman Divacky   for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) {
772f22ef01cSRoman Divacky     // Make sure that this applies to the correct node result.
773f22ef01cSRoman Divacky     if (TypeConstraints[i].OperandNo >= NumResults)  // FIXME: need value #
774f22ef01cSRoman Divacky       continue;
775f22ef01cSRoman Divacky 
776f22ef01cSRoman Divacky     switch (TypeConstraints[i].ConstraintType) {
777f22ef01cSRoman Divacky     default: break;
778f22ef01cSRoman Divacky     case SDTypeConstraint::SDTCisVT:
779f22ef01cSRoman Divacky       return TypeConstraints[i].x.SDTCisVT_Info.VT;
780f22ef01cSRoman Divacky     case SDTypeConstraint::SDTCisPtrTy:
781f22ef01cSRoman Divacky       return MVT::iPTR;
782f22ef01cSRoman Divacky     }
783f22ef01cSRoman Divacky   }
784f22ef01cSRoman Divacky   return MVT::Other;
785f22ef01cSRoman Divacky }
786f22ef01cSRoman Divacky 
787f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
788f22ef01cSRoman Divacky // TreePatternNode implementation
789f22ef01cSRoman Divacky //
790f22ef01cSRoman Divacky 
791f22ef01cSRoman Divacky TreePatternNode::~TreePatternNode() {
792f22ef01cSRoman Divacky #if 0 // FIXME: implement refcounted tree nodes!
793f22ef01cSRoman Divacky   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
794f22ef01cSRoman Divacky     delete getChild(i);
795f22ef01cSRoman Divacky #endif
796f22ef01cSRoman Divacky }
797f22ef01cSRoman Divacky 
798f22ef01cSRoman Divacky static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
799f22ef01cSRoman Divacky   if (Operator->getName() == "set" ||
800f22ef01cSRoman Divacky       Operator->getName() == "implicit")
801f22ef01cSRoman Divacky     return 0;  // All return nothing.
802f22ef01cSRoman Divacky 
803f22ef01cSRoman Divacky   if (Operator->isSubClassOf("Intrinsic"))
804f22ef01cSRoman Divacky     return CDP.getIntrinsic(Operator).IS.RetVTs.size();
805f22ef01cSRoman Divacky 
806f22ef01cSRoman Divacky   if (Operator->isSubClassOf("SDNode"))
807f22ef01cSRoman Divacky     return CDP.getSDNodeInfo(Operator).getNumResults();
808f22ef01cSRoman Divacky 
809f22ef01cSRoman Divacky   if (Operator->isSubClassOf("PatFrag")) {
810f22ef01cSRoman Divacky     // If we've already parsed this pattern fragment, get it.  Otherwise, handle
811f22ef01cSRoman Divacky     // the forward reference case where one pattern fragment references another
812f22ef01cSRoman Divacky     // before it is processed.
813f22ef01cSRoman Divacky     if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator))
814f22ef01cSRoman Divacky       return PFRec->getOnlyTree()->getNumTypes();
815f22ef01cSRoman Divacky 
816f22ef01cSRoman Divacky     // Get the result tree.
817f22ef01cSRoman Divacky     DagInit *Tree = Operator->getValueAsDag("Fragment");
818f22ef01cSRoman Divacky     Record *Op = 0;
819f22ef01cSRoman Divacky     if (Tree && dynamic_cast<DefInit*>(Tree->getOperator()))
820f22ef01cSRoman Divacky       Op = dynamic_cast<DefInit*>(Tree->getOperator())->getDef();
821f22ef01cSRoman Divacky     assert(Op && "Invalid Fragment");
822f22ef01cSRoman Divacky     return GetNumNodeResults(Op, CDP);
823f22ef01cSRoman Divacky   }
824f22ef01cSRoman Divacky 
825f22ef01cSRoman Divacky   if (Operator->isSubClassOf("Instruction")) {
826f22ef01cSRoman Divacky     CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator);
827f22ef01cSRoman Divacky 
828f22ef01cSRoman Divacky     // FIXME: Should allow access to all the results here.
829f22ef01cSRoman Divacky     unsigned NumDefsToAdd = InstInfo.NumDefs ? 1 : 0;
830f22ef01cSRoman Divacky 
831f22ef01cSRoman Divacky     // Add on one implicit def if it has a resolvable type.
832f22ef01cSRoman Divacky     if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other)
833f22ef01cSRoman Divacky       ++NumDefsToAdd;
834f22ef01cSRoman Divacky     return NumDefsToAdd;
835f22ef01cSRoman Divacky   }
836f22ef01cSRoman Divacky 
837f22ef01cSRoman Divacky   if (Operator->isSubClassOf("SDNodeXForm"))
838f22ef01cSRoman Divacky     return 1;  // FIXME: Generalize SDNodeXForm
839f22ef01cSRoman Divacky 
840f22ef01cSRoman Divacky   Operator->dump();
841f22ef01cSRoman Divacky   errs() << "Unhandled node in GetNumNodeResults\n";
842f22ef01cSRoman Divacky   exit(1);
843f22ef01cSRoman Divacky }
844f22ef01cSRoman Divacky 
845f22ef01cSRoman Divacky void TreePatternNode::print(raw_ostream &OS) const {
846f22ef01cSRoman Divacky   if (isLeaf())
847f22ef01cSRoman Divacky     OS << *getLeafValue();
848f22ef01cSRoman Divacky   else
849f22ef01cSRoman Divacky     OS << '(' << getOperator()->getName();
850f22ef01cSRoman Divacky 
851f22ef01cSRoman Divacky   for (unsigned i = 0, e = Types.size(); i != e; ++i)
852f22ef01cSRoman Divacky     OS << ':' << getExtType(i).getName();
853f22ef01cSRoman Divacky 
854f22ef01cSRoman Divacky   if (!isLeaf()) {
855f22ef01cSRoman Divacky     if (getNumChildren() != 0) {
856f22ef01cSRoman Divacky       OS << " ";
857f22ef01cSRoman Divacky       getChild(0)->print(OS);
858f22ef01cSRoman Divacky       for (unsigned i = 1, e = getNumChildren(); i != e; ++i) {
859f22ef01cSRoman Divacky         OS << ", ";
860f22ef01cSRoman Divacky         getChild(i)->print(OS);
861f22ef01cSRoman Divacky       }
862f22ef01cSRoman Divacky     }
863f22ef01cSRoman Divacky     OS << ")";
864f22ef01cSRoman Divacky   }
865f22ef01cSRoman Divacky 
866f22ef01cSRoman Divacky   for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i)
867f22ef01cSRoman Divacky     OS << "<<P:" << PredicateFns[i] << ">>";
868f22ef01cSRoman Divacky   if (TransformFn)
869f22ef01cSRoman Divacky     OS << "<<X:" << TransformFn->getName() << ">>";
870f22ef01cSRoman Divacky   if (!getName().empty())
871f22ef01cSRoman Divacky     OS << ":$" << getName();
872f22ef01cSRoman Divacky 
873f22ef01cSRoman Divacky }
874f22ef01cSRoman Divacky void TreePatternNode::dump() const {
875f22ef01cSRoman Divacky   print(errs());
876f22ef01cSRoman Divacky }
877f22ef01cSRoman Divacky 
878f22ef01cSRoman Divacky /// isIsomorphicTo - Return true if this node is recursively
879f22ef01cSRoman Divacky /// isomorphic to the specified node.  For this comparison, the node's
880f22ef01cSRoman Divacky /// entire state is considered. The assigned name is ignored, since
881f22ef01cSRoman Divacky /// nodes with differing names are considered isomorphic. However, if
882f22ef01cSRoman Divacky /// the assigned name is present in the dependent variable set, then
883f22ef01cSRoman Divacky /// the assigned name is considered significant and the node is
884f22ef01cSRoman Divacky /// isomorphic if the names match.
885f22ef01cSRoman Divacky bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N,
886f22ef01cSRoman Divacky                                      const MultipleUseVarSet &DepVars) const {
887f22ef01cSRoman Divacky   if (N == this) return true;
888f22ef01cSRoman Divacky   if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() ||
889f22ef01cSRoman Divacky       getPredicateFns() != N->getPredicateFns() ||
890f22ef01cSRoman Divacky       getTransformFn() != N->getTransformFn())
891f22ef01cSRoman Divacky     return false;
892f22ef01cSRoman Divacky 
893f22ef01cSRoman Divacky   if (isLeaf()) {
894f22ef01cSRoman Divacky     if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) {
895f22ef01cSRoman Divacky       if (DefInit *NDI = dynamic_cast<DefInit*>(N->getLeafValue())) {
896f22ef01cSRoman Divacky         return ((DI->getDef() == NDI->getDef())
897f22ef01cSRoman Divacky                 && (DepVars.find(getName()) == DepVars.end()
898f22ef01cSRoman Divacky                     || getName() == N->getName()));
899f22ef01cSRoman Divacky       }
900f22ef01cSRoman Divacky     }
901f22ef01cSRoman Divacky     return getLeafValue() == N->getLeafValue();
902f22ef01cSRoman Divacky   }
903f22ef01cSRoman Divacky 
904f22ef01cSRoman Divacky   if (N->getOperator() != getOperator() ||
905f22ef01cSRoman Divacky       N->getNumChildren() != getNumChildren()) return false;
906f22ef01cSRoman Divacky   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
907f22ef01cSRoman Divacky     if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars))
908f22ef01cSRoman Divacky       return false;
909f22ef01cSRoman Divacky   return true;
910f22ef01cSRoman Divacky }
911f22ef01cSRoman Divacky 
912f22ef01cSRoman Divacky /// clone - Make a copy of this tree and all of its children.
913f22ef01cSRoman Divacky ///
914f22ef01cSRoman Divacky TreePatternNode *TreePatternNode::clone() const {
915f22ef01cSRoman Divacky   TreePatternNode *New;
916f22ef01cSRoman Divacky   if (isLeaf()) {
917f22ef01cSRoman Divacky     New = new TreePatternNode(getLeafValue(), getNumTypes());
918f22ef01cSRoman Divacky   } else {
919f22ef01cSRoman Divacky     std::vector<TreePatternNode*> CChildren;
920f22ef01cSRoman Divacky     CChildren.reserve(Children.size());
921f22ef01cSRoman Divacky     for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
922f22ef01cSRoman Divacky       CChildren.push_back(getChild(i)->clone());
923f22ef01cSRoman Divacky     New = new TreePatternNode(getOperator(), CChildren, getNumTypes());
924f22ef01cSRoman Divacky   }
925f22ef01cSRoman Divacky   New->setName(getName());
926f22ef01cSRoman Divacky   New->Types = Types;
927f22ef01cSRoman Divacky   New->setPredicateFns(getPredicateFns());
928f22ef01cSRoman Divacky   New->setTransformFn(getTransformFn());
929f22ef01cSRoman Divacky   return New;
930f22ef01cSRoman Divacky }
931f22ef01cSRoman Divacky 
932f22ef01cSRoman Divacky /// RemoveAllTypes - Recursively strip all the types of this tree.
933f22ef01cSRoman Divacky void TreePatternNode::RemoveAllTypes() {
934f22ef01cSRoman Divacky   for (unsigned i = 0, e = Types.size(); i != e; ++i)
935f22ef01cSRoman Divacky     Types[i] = EEVT::TypeSet();  // Reset to unknown type.
936f22ef01cSRoman Divacky   if (isLeaf()) return;
937f22ef01cSRoman Divacky   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
938f22ef01cSRoman Divacky     getChild(i)->RemoveAllTypes();
939f22ef01cSRoman Divacky }
940f22ef01cSRoman Divacky 
941f22ef01cSRoman Divacky 
942f22ef01cSRoman Divacky /// SubstituteFormalArguments - Replace the formal arguments in this tree
943f22ef01cSRoman Divacky /// with actual values specified by ArgMap.
944f22ef01cSRoman Divacky void TreePatternNode::
945f22ef01cSRoman Divacky SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) {
946f22ef01cSRoman Divacky   if (isLeaf()) return;
947f22ef01cSRoman Divacky 
948f22ef01cSRoman Divacky   for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
949f22ef01cSRoman Divacky     TreePatternNode *Child = getChild(i);
950f22ef01cSRoman Divacky     if (Child->isLeaf()) {
951f22ef01cSRoman Divacky       Init *Val = Child->getLeafValue();
952f22ef01cSRoman Divacky       if (dynamic_cast<DefInit*>(Val) &&
953f22ef01cSRoman Divacky           static_cast<DefInit*>(Val)->getDef()->getName() == "node") {
954f22ef01cSRoman Divacky         // We found a use of a formal argument, replace it with its value.
955f22ef01cSRoman Divacky         TreePatternNode *NewChild = ArgMap[Child->getName()];
956f22ef01cSRoman Divacky         assert(NewChild && "Couldn't find formal argument!");
957f22ef01cSRoman Divacky         assert((Child->getPredicateFns().empty() ||
958f22ef01cSRoman Divacky                 NewChild->getPredicateFns() == Child->getPredicateFns()) &&
959f22ef01cSRoman Divacky                "Non-empty child predicate clobbered!");
960f22ef01cSRoman Divacky         setChild(i, NewChild);
961f22ef01cSRoman Divacky       }
962f22ef01cSRoman Divacky     } else {
963f22ef01cSRoman Divacky       getChild(i)->SubstituteFormalArguments(ArgMap);
964f22ef01cSRoman Divacky     }
965f22ef01cSRoman Divacky   }
966f22ef01cSRoman Divacky }
967f22ef01cSRoman Divacky 
968f22ef01cSRoman Divacky 
969f22ef01cSRoman Divacky /// InlinePatternFragments - If this pattern refers to any pattern
970f22ef01cSRoman Divacky /// fragments, inline them into place, giving us a pattern without any
971f22ef01cSRoman Divacky /// PatFrag references.
972f22ef01cSRoman Divacky TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) {
973f22ef01cSRoman Divacky   if (isLeaf()) return this;  // nothing to do.
974f22ef01cSRoman Divacky   Record *Op = getOperator();
975f22ef01cSRoman Divacky 
976f22ef01cSRoman Divacky   if (!Op->isSubClassOf("PatFrag")) {
977f22ef01cSRoman Divacky     // Just recursively inline children nodes.
978f22ef01cSRoman Divacky     for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
979f22ef01cSRoman Divacky       TreePatternNode *Child = getChild(i);
980f22ef01cSRoman Divacky       TreePatternNode *NewChild = Child->InlinePatternFragments(TP);
981f22ef01cSRoman Divacky 
982f22ef01cSRoman Divacky       assert((Child->getPredicateFns().empty() ||
983f22ef01cSRoman Divacky               NewChild->getPredicateFns() == Child->getPredicateFns()) &&
984f22ef01cSRoman Divacky              "Non-empty child predicate clobbered!");
985f22ef01cSRoman Divacky 
986f22ef01cSRoman Divacky       setChild(i, NewChild);
987f22ef01cSRoman Divacky     }
988f22ef01cSRoman Divacky     return this;
989f22ef01cSRoman Divacky   }
990f22ef01cSRoman Divacky 
991f22ef01cSRoman Divacky   // Otherwise, we found a reference to a fragment.  First, look up its
992f22ef01cSRoman Divacky   // TreePattern record.
993f22ef01cSRoman Divacky   TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op);
994f22ef01cSRoman Divacky 
995f22ef01cSRoman Divacky   // Verify that we are passing the right number of operands.
996f22ef01cSRoman Divacky   if (Frag->getNumArgs() != Children.size())
997f22ef01cSRoman Divacky     TP.error("'" + Op->getName() + "' fragment requires " +
998f22ef01cSRoman Divacky              utostr(Frag->getNumArgs()) + " operands!");
999f22ef01cSRoman Divacky 
1000f22ef01cSRoman Divacky   TreePatternNode *FragTree = Frag->getOnlyTree()->clone();
1001f22ef01cSRoman Divacky 
1002f22ef01cSRoman Divacky   std::string Code = Op->getValueAsCode("Predicate");
1003f22ef01cSRoman Divacky   if (!Code.empty())
1004f22ef01cSRoman Divacky     FragTree->addPredicateFn("Predicate_"+Op->getName());
1005f22ef01cSRoman Divacky 
1006f22ef01cSRoman Divacky   // Resolve formal arguments to their actual value.
1007f22ef01cSRoman Divacky   if (Frag->getNumArgs()) {
1008f22ef01cSRoman Divacky     // Compute the map of formal to actual arguments.
1009f22ef01cSRoman Divacky     std::map<std::string, TreePatternNode*> ArgMap;
1010f22ef01cSRoman Divacky     for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i)
1011f22ef01cSRoman Divacky       ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP);
1012f22ef01cSRoman Divacky 
1013f22ef01cSRoman Divacky     FragTree->SubstituteFormalArguments(ArgMap);
1014f22ef01cSRoman Divacky   }
1015f22ef01cSRoman Divacky 
1016f22ef01cSRoman Divacky   FragTree->setName(getName());
1017f22ef01cSRoman Divacky   for (unsigned i = 0, e = Types.size(); i != e; ++i)
1018f22ef01cSRoman Divacky     FragTree->UpdateNodeType(i, getExtType(i), TP);
1019f22ef01cSRoman Divacky 
1020f22ef01cSRoman Divacky   // Transfer in the old predicates.
1021f22ef01cSRoman Divacky   for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i)
1022f22ef01cSRoman Divacky     FragTree->addPredicateFn(getPredicateFns()[i]);
1023f22ef01cSRoman Divacky 
1024f22ef01cSRoman Divacky   // Get a new copy of this fragment to stitch into here.
1025f22ef01cSRoman Divacky   //delete this;    // FIXME: implement refcounting!
1026f22ef01cSRoman Divacky 
1027f22ef01cSRoman Divacky   // The fragment we inlined could have recursive inlining that is needed.  See
1028f22ef01cSRoman Divacky   // if there are any pattern fragments in it and inline them as needed.
1029f22ef01cSRoman Divacky   return FragTree->InlinePatternFragments(TP);
1030f22ef01cSRoman Divacky }
1031f22ef01cSRoman Divacky 
1032f22ef01cSRoman Divacky /// getImplicitType - Check to see if the specified record has an implicit
1033f22ef01cSRoman Divacky /// type which should be applied to it.  This will infer the type of register
1034f22ef01cSRoman Divacky /// references from the register file information, for example.
1035f22ef01cSRoman Divacky ///
1036f22ef01cSRoman Divacky static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo,
1037f22ef01cSRoman Divacky                                      bool NotRegisters, TreePattern &TP) {
1038f22ef01cSRoman Divacky   // Check to see if this is a register or a register class.
1039f22ef01cSRoman Divacky   if (R->isSubClassOf("RegisterClass")) {
1040f22ef01cSRoman Divacky     assert(ResNo == 0 && "Regclass ref only has one result!");
1041f22ef01cSRoman Divacky     if (NotRegisters)
1042f22ef01cSRoman Divacky       return EEVT::TypeSet(); // Unknown.
1043f22ef01cSRoman Divacky     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1044f22ef01cSRoman Divacky     return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes());
1045f22ef01cSRoman Divacky   }
1046f22ef01cSRoman Divacky 
1047f22ef01cSRoman Divacky   if (R->isSubClassOf("PatFrag")) {
1048f22ef01cSRoman Divacky     assert(ResNo == 0 && "FIXME: PatFrag with multiple results?");
1049f22ef01cSRoman Divacky     // Pattern fragment types will be resolved when they are inlined.
1050f22ef01cSRoman Divacky     return EEVT::TypeSet(); // Unknown.
1051f22ef01cSRoman Divacky   }
1052f22ef01cSRoman Divacky 
1053f22ef01cSRoman Divacky   if (R->isSubClassOf("Register")) {
1054f22ef01cSRoman Divacky     assert(ResNo == 0 && "Registers only produce one result!");
1055f22ef01cSRoman Divacky     if (NotRegisters)
1056f22ef01cSRoman Divacky       return EEVT::TypeSet(); // Unknown.
1057f22ef01cSRoman Divacky     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1058f22ef01cSRoman Divacky     return EEVT::TypeSet(T.getRegisterVTs(R));
1059f22ef01cSRoman Divacky   }
1060f22ef01cSRoman Divacky 
1061f22ef01cSRoman Divacky   if (R->isSubClassOf("SubRegIndex")) {
1062f22ef01cSRoman Divacky     assert(ResNo == 0 && "SubRegisterIndices only produce one result!");
1063f22ef01cSRoman Divacky     return EEVT::TypeSet();
1064f22ef01cSRoman Divacky   }
1065f22ef01cSRoman Divacky 
1066f22ef01cSRoman Divacky   if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) {
1067f22ef01cSRoman Divacky     assert(ResNo == 0 && "This node only has one result!");
1068f22ef01cSRoman Divacky     // Using a VTSDNode or CondCodeSDNode.
1069f22ef01cSRoman Divacky     return EEVT::TypeSet(MVT::Other, TP);
1070f22ef01cSRoman Divacky   }
1071f22ef01cSRoman Divacky 
1072f22ef01cSRoman Divacky   if (R->isSubClassOf("ComplexPattern")) {
1073f22ef01cSRoman Divacky     assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
1074f22ef01cSRoman Divacky     if (NotRegisters)
1075f22ef01cSRoman Divacky       return EEVT::TypeSet(); // Unknown.
1076f22ef01cSRoman Divacky    return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(),
1077f22ef01cSRoman Divacky                          TP);
1078f22ef01cSRoman Divacky   }
1079f22ef01cSRoman Divacky   if (R->isSubClassOf("PointerLikeRegClass")) {
1080f22ef01cSRoman Divacky     assert(ResNo == 0 && "Regclass can only have one result!");
1081f22ef01cSRoman Divacky     return EEVT::TypeSet(MVT::iPTR, TP);
1082f22ef01cSRoman Divacky   }
1083f22ef01cSRoman Divacky 
1084f22ef01cSRoman Divacky   if (R->getName() == "node" || R->getName() == "srcvalue" ||
1085f22ef01cSRoman Divacky       R->getName() == "zero_reg") {
1086f22ef01cSRoman Divacky     // Placeholder.
1087f22ef01cSRoman Divacky     return EEVT::TypeSet(); // Unknown.
1088f22ef01cSRoman Divacky   }
1089f22ef01cSRoman Divacky 
1090f22ef01cSRoman Divacky   TP.error("Unknown node flavor used in pattern: " + R->getName());
1091f22ef01cSRoman Divacky   return EEVT::TypeSet(MVT::Other, TP);
1092f22ef01cSRoman Divacky }
1093f22ef01cSRoman Divacky 
1094f22ef01cSRoman Divacky 
1095f22ef01cSRoman Divacky /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
1096f22ef01cSRoman Divacky /// CodeGenIntrinsic information for it, otherwise return a null pointer.
1097f22ef01cSRoman Divacky const CodeGenIntrinsic *TreePatternNode::
1098f22ef01cSRoman Divacky getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
1099f22ef01cSRoman Divacky   if (getOperator() != CDP.get_intrinsic_void_sdnode() &&
1100f22ef01cSRoman Divacky       getOperator() != CDP.get_intrinsic_w_chain_sdnode() &&
1101f22ef01cSRoman Divacky       getOperator() != CDP.get_intrinsic_wo_chain_sdnode())
1102f22ef01cSRoman Divacky     return 0;
1103f22ef01cSRoman Divacky 
1104f22ef01cSRoman Divacky   unsigned IID =
1105f22ef01cSRoman Divacky     dynamic_cast<IntInit*>(getChild(0)->getLeafValue())->getValue();
1106f22ef01cSRoman Divacky   return &CDP.getIntrinsicInfo(IID);
1107f22ef01cSRoman Divacky }
1108f22ef01cSRoman Divacky 
1109f22ef01cSRoman Divacky /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
1110f22ef01cSRoman Divacky /// return the ComplexPattern information, otherwise return null.
1111f22ef01cSRoman Divacky const ComplexPattern *
1112f22ef01cSRoman Divacky TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
1113f22ef01cSRoman Divacky   if (!isLeaf()) return 0;
1114f22ef01cSRoman Divacky 
1115f22ef01cSRoman Divacky   DefInit *DI = dynamic_cast<DefInit*>(getLeafValue());
1116f22ef01cSRoman Divacky   if (DI && DI->getDef()->isSubClassOf("ComplexPattern"))
1117f22ef01cSRoman Divacky     return &CGP.getComplexPattern(DI->getDef());
1118f22ef01cSRoman Divacky   return 0;
1119f22ef01cSRoman Divacky }
1120f22ef01cSRoman Divacky 
1121f22ef01cSRoman Divacky /// NodeHasProperty - Return true if this node has the specified property.
1122f22ef01cSRoman Divacky bool TreePatternNode::NodeHasProperty(SDNP Property,
1123f22ef01cSRoman Divacky                                       const CodeGenDAGPatterns &CGP) const {
1124f22ef01cSRoman Divacky   if (isLeaf()) {
1125f22ef01cSRoman Divacky     if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
1126f22ef01cSRoman Divacky       return CP->hasProperty(Property);
1127f22ef01cSRoman Divacky     return false;
1128f22ef01cSRoman Divacky   }
1129f22ef01cSRoman Divacky 
1130f22ef01cSRoman Divacky   Record *Operator = getOperator();
1131f22ef01cSRoman Divacky   if (!Operator->isSubClassOf("SDNode")) return false;
1132f22ef01cSRoman Divacky 
1133f22ef01cSRoman Divacky   return CGP.getSDNodeInfo(Operator).hasProperty(Property);
1134f22ef01cSRoman Divacky }
1135f22ef01cSRoman Divacky 
1136f22ef01cSRoman Divacky 
1137f22ef01cSRoman Divacky 
1138f22ef01cSRoman Divacky 
1139f22ef01cSRoman Divacky /// TreeHasProperty - Return true if any node in this tree has the specified
1140f22ef01cSRoman Divacky /// property.
1141f22ef01cSRoman Divacky bool TreePatternNode::TreeHasProperty(SDNP Property,
1142f22ef01cSRoman Divacky                                       const CodeGenDAGPatterns &CGP) const {
1143f22ef01cSRoman Divacky   if (NodeHasProperty(Property, CGP))
1144f22ef01cSRoman Divacky     return true;
1145f22ef01cSRoman Divacky   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1146f22ef01cSRoman Divacky     if (getChild(i)->TreeHasProperty(Property, CGP))
1147f22ef01cSRoman Divacky       return true;
1148f22ef01cSRoman Divacky   return false;
1149f22ef01cSRoman Divacky }
1150f22ef01cSRoman Divacky 
1151f22ef01cSRoman Divacky /// isCommutativeIntrinsic - Return true if the node corresponds to a
1152f22ef01cSRoman Divacky /// commutative intrinsic.
1153f22ef01cSRoman Divacky bool
1154f22ef01cSRoman Divacky TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const {
1155f22ef01cSRoman Divacky   if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP))
1156f22ef01cSRoman Divacky     return Int->isCommutative;
1157f22ef01cSRoman Divacky   return false;
1158f22ef01cSRoman Divacky }
1159f22ef01cSRoman Divacky 
1160f22ef01cSRoman Divacky 
1161f22ef01cSRoman Divacky /// ApplyTypeConstraints - Apply all of the type constraints relevant to
1162f22ef01cSRoman Divacky /// this node and its children in the tree.  This returns true if it makes a
1163f22ef01cSRoman Divacky /// change, false otherwise.  If a type contradiction is found, throw an
1164f22ef01cSRoman Divacky /// exception.
1165f22ef01cSRoman Divacky bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
1166f22ef01cSRoman Divacky   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
1167f22ef01cSRoman Divacky   if (isLeaf()) {
1168f22ef01cSRoman Divacky     if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) {
1169f22ef01cSRoman Divacky       // If it's a regclass or something else known, include the type.
1170f22ef01cSRoman Divacky       bool MadeChange = false;
1171f22ef01cSRoman Divacky       for (unsigned i = 0, e = Types.size(); i != e; ++i)
1172f22ef01cSRoman Divacky         MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
1173f22ef01cSRoman Divacky                                                         NotRegisters, TP), TP);
1174f22ef01cSRoman Divacky       return MadeChange;
1175f22ef01cSRoman Divacky     }
1176f22ef01cSRoman Divacky 
1177f22ef01cSRoman Divacky     if (IntInit *II = dynamic_cast<IntInit*>(getLeafValue())) {
1178f22ef01cSRoman Divacky       assert(Types.size() == 1 && "Invalid IntInit");
1179f22ef01cSRoman Divacky 
1180f22ef01cSRoman Divacky       // Int inits are always integers. :)
1181f22ef01cSRoman Divacky       bool MadeChange = Types[0].EnforceInteger(TP);
1182f22ef01cSRoman Divacky 
1183f22ef01cSRoman Divacky       if (!Types[0].isConcrete())
1184f22ef01cSRoman Divacky         return MadeChange;
1185f22ef01cSRoman Divacky 
1186f22ef01cSRoman Divacky       MVT::SimpleValueType VT = getType(0);
1187f22ef01cSRoman Divacky       if (VT == MVT::iPTR || VT == MVT::iPTRAny)
1188f22ef01cSRoman Divacky         return MadeChange;
1189f22ef01cSRoman Divacky 
1190f22ef01cSRoman Divacky       unsigned Size = EVT(VT).getSizeInBits();
1191f22ef01cSRoman Divacky       // Make sure that the value is representable for this type.
1192f22ef01cSRoman Divacky       if (Size >= 32) return MadeChange;
1193f22ef01cSRoman Divacky 
1194f22ef01cSRoman Divacky       int Val = (II->getValue() << (32-Size)) >> (32-Size);
1195f22ef01cSRoman Divacky       if (Val == II->getValue()) return MadeChange;
1196f22ef01cSRoman Divacky 
1197f22ef01cSRoman Divacky       // If sign-extended doesn't fit, does it fit as unsigned?
1198f22ef01cSRoman Divacky       unsigned ValueMask;
1199f22ef01cSRoman Divacky       unsigned UnsignedVal;
1200f22ef01cSRoman Divacky       ValueMask = unsigned(~uint32_t(0UL) >> (32-Size));
1201f22ef01cSRoman Divacky       UnsignedVal = unsigned(II->getValue());
1202f22ef01cSRoman Divacky 
1203f22ef01cSRoman Divacky       if ((ValueMask & UnsignedVal) == UnsignedVal)
1204f22ef01cSRoman Divacky         return MadeChange;
1205f22ef01cSRoman Divacky 
1206f22ef01cSRoman Divacky       TP.error("Integer value '" + itostr(II->getValue())+
1207f22ef01cSRoman Divacky                "' is out of range for type '" + getEnumName(getType(0)) + "'!");
1208f22ef01cSRoman Divacky       return MadeChange;
1209f22ef01cSRoman Divacky     }
1210f22ef01cSRoman Divacky     return false;
1211f22ef01cSRoman Divacky   }
1212f22ef01cSRoman Divacky 
1213f22ef01cSRoman Divacky   // special handling for set, which isn't really an SDNode.
1214f22ef01cSRoman Divacky   if (getOperator()->getName() == "set") {
1215f22ef01cSRoman Divacky     assert(getNumTypes() == 0 && "Set doesn't produce a value");
1216f22ef01cSRoman Divacky     assert(getNumChildren() >= 2 && "Missing RHS of a set?");
1217f22ef01cSRoman Divacky     unsigned NC = getNumChildren();
1218f22ef01cSRoman Divacky 
1219f22ef01cSRoman Divacky     TreePatternNode *SetVal = getChild(NC-1);
1220f22ef01cSRoman Divacky     bool MadeChange = SetVal->ApplyTypeConstraints(TP, NotRegisters);
1221f22ef01cSRoman Divacky 
1222f22ef01cSRoman Divacky     for (unsigned i = 0; i < NC-1; ++i) {
1223f22ef01cSRoman Divacky       TreePatternNode *Child = getChild(i);
1224f22ef01cSRoman Divacky       MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters);
1225f22ef01cSRoman Divacky 
1226f22ef01cSRoman Divacky       // Types of operands must match.
1227f22ef01cSRoman Divacky       MadeChange |= Child->UpdateNodeType(0, SetVal->getExtType(i), TP);
1228f22ef01cSRoman Divacky       MadeChange |= SetVal->UpdateNodeType(i, Child->getExtType(0), TP);
1229f22ef01cSRoman Divacky     }
1230f22ef01cSRoman Divacky     return MadeChange;
1231f22ef01cSRoman Divacky   }
1232f22ef01cSRoman Divacky 
1233f22ef01cSRoman Divacky   if (getOperator()->getName() == "implicit") {
1234f22ef01cSRoman Divacky     assert(getNumTypes() == 0 && "Node doesn't produce a value");
1235f22ef01cSRoman Divacky 
1236f22ef01cSRoman Divacky     bool MadeChange = false;
1237f22ef01cSRoman Divacky     for (unsigned i = 0; i < getNumChildren(); ++i)
1238f22ef01cSRoman Divacky       MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1239f22ef01cSRoman Divacky     return MadeChange;
1240f22ef01cSRoman Divacky   }
1241f22ef01cSRoman Divacky 
1242f22ef01cSRoman Divacky   if (getOperator()->getName() == "COPY_TO_REGCLASS") {
1243f22ef01cSRoman Divacky     bool MadeChange = false;
1244f22ef01cSRoman Divacky     MadeChange |= getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
1245f22ef01cSRoman Divacky     MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters);
1246f22ef01cSRoman Divacky 
1247f22ef01cSRoman Divacky     assert(getChild(0)->getNumTypes() == 1 &&
1248f22ef01cSRoman Divacky            getChild(1)->getNumTypes() == 1 && "Unhandled case");
1249f22ef01cSRoman Divacky 
1250f22ef01cSRoman Divacky     // child #1 of COPY_TO_REGCLASS should be a register class.  We don't care
1251f22ef01cSRoman Divacky     // what type it gets, so if it didn't get a concrete type just give it the
1252f22ef01cSRoman Divacky     // first viable type from the reg class.
1253f22ef01cSRoman Divacky     if (!getChild(1)->hasTypeSet(0) &&
1254f22ef01cSRoman Divacky         !getChild(1)->getExtType(0).isCompletelyUnknown()) {
1255f22ef01cSRoman Divacky       MVT::SimpleValueType RCVT = getChild(1)->getExtType(0).getTypeList()[0];
1256f22ef01cSRoman Divacky       MadeChange |= getChild(1)->UpdateNodeType(0, RCVT, TP);
1257f22ef01cSRoman Divacky     }
1258f22ef01cSRoman Divacky     return MadeChange;
1259f22ef01cSRoman Divacky   }
1260f22ef01cSRoman Divacky 
1261f22ef01cSRoman Divacky   if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) {
1262f22ef01cSRoman Divacky     bool MadeChange = false;
1263f22ef01cSRoman Divacky 
1264f22ef01cSRoman Divacky     // Apply the result type to the node.
1265f22ef01cSRoman Divacky     unsigned NumRetVTs = Int->IS.RetVTs.size();
1266f22ef01cSRoman Divacky     unsigned NumParamVTs = Int->IS.ParamVTs.size();
1267f22ef01cSRoman Divacky 
1268f22ef01cSRoman Divacky     for (unsigned i = 0, e = NumRetVTs; i != e; ++i)
1269f22ef01cSRoman Divacky       MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP);
1270f22ef01cSRoman Divacky 
1271f22ef01cSRoman Divacky     if (getNumChildren() != NumParamVTs + 1)
1272f22ef01cSRoman Divacky       TP.error("Intrinsic '" + Int->Name + "' expects " +
1273f22ef01cSRoman Divacky                utostr(NumParamVTs) + " operands, not " +
1274f22ef01cSRoman Divacky                utostr(getNumChildren() - 1) + " operands!");
1275f22ef01cSRoman Divacky 
1276f22ef01cSRoman Divacky     // Apply type info to the intrinsic ID.
1277f22ef01cSRoman Divacky     MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP);
1278f22ef01cSRoman Divacky 
1279f22ef01cSRoman Divacky     for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) {
1280f22ef01cSRoman Divacky       MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters);
1281f22ef01cSRoman Divacky 
1282f22ef01cSRoman Divacky       MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i];
1283f22ef01cSRoman Divacky       assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case");
1284f22ef01cSRoman Divacky       MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP);
1285f22ef01cSRoman Divacky     }
1286f22ef01cSRoman Divacky     return MadeChange;
1287f22ef01cSRoman Divacky   }
1288f22ef01cSRoman Divacky 
1289f22ef01cSRoman Divacky   if (getOperator()->isSubClassOf("SDNode")) {
1290f22ef01cSRoman Divacky     const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator());
1291f22ef01cSRoman Divacky 
1292f22ef01cSRoman Divacky     // Check that the number of operands is sane.  Negative operands -> varargs.
1293f22ef01cSRoman Divacky     if (NI.getNumOperands() >= 0 &&
1294f22ef01cSRoman Divacky         getNumChildren() != (unsigned)NI.getNumOperands())
1295f22ef01cSRoman Divacky       TP.error(getOperator()->getName() + " node requires exactly " +
1296f22ef01cSRoman Divacky                itostr(NI.getNumOperands()) + " operands!");
1297f22ef01cSRoman Divacky 
1298f22ef01cSRoman Divacky     bool MadeChange = NI.ApplyTypeConstraints(this, TP);
1299f22ef01cSRoman Divacky     for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1300f22ef01cSRoman Divacky       MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1301f22ef01cSRoman Divacky     return MadeChange;
1302f22ef01cSRoman Divacky   }
1303f22ef01cSRoman Divacky 
1304f22ef01cSRoman Divacky   if (getOperator()->isSubClassOf("Instruction")) {
1305f22ef01cSRoman Divacky     const DAGInstruction &Inst = CDP.getInstruction(getOperator());
1306f22ef01cSRoman Divacky     CodeGenInstruction &InstInfo =
1307f22ef01cSRoman Divacky       CDP.getTargetInfo().getInstruction(getOperator());
1308f22ef01cSRoman Divacky 
1309f22ef01cSRoman Divacky     bool MadeChange = false;
1310f22ef01cSRoman Divacky 
1311f22ef01cSRoman Divacky     // Apply the result types to the node, these come from the things in the
1312f22ef01cSRoman Divacky     // (outs) list of the instruction.
1313f22ef01cSRoman Divacky     // FIXME: Cap at one result so far.
1314f22ef01cSRoman Divacky     unsigned NumResultsToAdd = InstInfo.NumDefs ? 1 : 0;
1315f22ef01cSRoman Divacky     for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo) {
1316f22ef01cSRoman Divacky       Record *ResultNode = Inst.getResult(ResNo);
1317f22ef01cSRoman Divacky 
1318f22ef01cSRoman Divacky       if (ResultNode->isSubClassOf("PointerLikeRegClass")) {
1319f22ef01cSRoman Divacky         MadeChange |= UpdateNodeType(ResNo, MVT::iPTR, TP);
1320f22ef01cSRoman Divacky       } else if (ResultNode->getName() == "unknown") {
1321f22ef01cSRoman Divacky         // Nothing to do.
1322f22ef01cSRoman Divacky       } else {
1323f22ef01cSRoman Divacky         assert(ResultNode->isSubClassOf("RegisterClass") &&
1324f22ef01cSRoman Divacky                "Operands should be register classes!");
1325f22ef01cSRoman Divacky         const CodeGenRegisterClass &RC =
1326f22ef01cSRoman Divacky           CDP.getTargetInfo().getRegisterClass(ResultNode);
1327f22ef01cSRoman Divacky         MadeChange |= UpdateNodeType(ResNo, RC.getValueTypes(), TP);
1328f22ef01cSRoman Divacky       }
1329f22ef01cSRoman Divacky     }
1330f22ef01cSRoman Divacky 
1331f22ef01cSRoman Divacky     // If the instruction has implicit defs, we apply the first one as a result.
1332f22ef01cSRoman Divacky     // FIXME: This sucks, it should apply all implicit defs.
1333f22ef01cSRoman Divacky     if (!InstInfo.ImplicitDefs.empty()) {
1334f22ef01cSRoman Divacky       unsigned ResNo = NumResultsToAdd;
1335f22ef01cSRoman Divacky 
1336f22ef01cSRoman Divacky       // FIXME: Generalize to multiple possible types and multiple possible
1337f22ef01cSRoman Divacky       // ImplicitDefs.
1338f22ef01cSRoman Divacky       MVT::SimpleValueType VT =
1339f22ef01cSRoman Divacky         InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo());
1340f22ef01cSRoman Divacky 
1341f22ef01cSRoman Divacky       if (VT != MVT::Other)
1342f22ef01cSRoman Divacky         MadeChange |= UpdateNodeType(ResNo, VT, TP);
1343f22ef01cSRoman Divacky     }
1344f22ef01cSRoman Divacky 
1345f22ef01cSRoman Divacky     // If this is an INSERT_SUBREG, constrain the source and destination VTs to
1346f22ef01cSRoman Divacky     // be the same.
1347f22ef01cSRoman Divacky     if (getOperator()->getName() == "INSERT_SUBREG") {
1348f22ef01cSRoman Divacky       assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled");
1349f22ef01cSRoman Divacky       MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP);
1350f22ef01cSRoman Divacky       MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP);
1351f22ef01cSRoman Divacky     }
1352f22ef01cSRoman Divacky 
1353f22ef01cSRoman Divacky     unsigned ChildNo = 0;
1354f22ef01cSRoman Divacky     for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
1355f22ef01cSRoman Divacky       Record *OperandNode = Inst.getOperand(i);
1356f22ef01cSRoman Divacky 
1357f22ef01cSRoman Divacky       // If the instruction expects a predicate or optional def operand, we
1358f22ef01cSRoman Divacky       // codegen this by setting the operand to it's default value if it has a
1359f22ef01cSRoman Divacky       // non-empty DefaultOps field.
1360f22ef01cSRoman Divacky       if ((OperandNode->isSubClassOf("PredicateOperand") ||
1361f22ef01cSRoman Divacky            OperandNode->isSubClassOf("OptionalDefOperand")) &&
1362f22ef01cSRoman Divacky           !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1363f22ef01cSRoman Divacky         continue;
1364f22ef01cSRoman Divacky 
1365f22ef01cSRoman Divacky       // Verify that we didn't run out of provided operands.
1366f22ef01cSRoman Divacky       if (ChildNo >= getNumChildren())
1367f22ef01cSRoman Divacky         TP.error("Instruction '" + getOperator()->getName() +
1368f22ef01cSRoman Divacky                  "' expects more operands than were provided.");
1369f22ef01cSRoman Divacky 
1370f22ef01cSRoman Divacky       MVT::SimpleValueType VT;
1371f22ef01cSRoman Divacky       TreePatternNode *Child = getChild(ChildNo++);
1372f22ef01cSRoman Divacky       unsigned ChildResNo = 0;  // Instructions always use res #0 of their op.
1373f22ef01cSRoman Divacky 
1374f22ef01cSRoman Divacky       if (OperandNode->isSubClassOf("RegisterClass")) {
1375f22ef01cSRoman Divacky         const CodeGenRegisterClass &RC =
1376f22ef01cSRoman Divacky           CDP.getTargetInfo().getRegisterClass(OperandNode);
1377f22ef01cSRoman Divacky         MadeChange |= Child->UpdateNodeType(ChildResNo, RC.getValueTypes(), TP);
1378f22ef01cSRoman Divacky       } else if (OperandNode->isSubClassOf("Operand")) {
1379f22ef01cSRoman Divacky         VT = getValueType(OperandNode->getValueAsDef("Type"));
1380f22ef01cSRoman Divacky         MadeChange |= Child->UpdateNodeType(ChildResNo, VT, TP);
1381f22ef01cSRoman Divacky       } else if (OperandNode->isSubClassOf("PointerLikeRegClass")) {
1382f22ef01cSRoman Divacky         MadeChange |= Child->UpdateNodeType(ChildResNo, MVT::iPTR, TP);
1383f22ef01cSRoman Divacky       } else if (OperandNode->getName() == "unknown") {
1384f22ef01cSRoman Divacky         // Nothing to do.
1385f22ef01cSRoman Divacky       } else {
1386f22ef01cSRoman Divacky         assert(0 && "Unknown operand type!");
1387f22ef01cSRoman Divacky         abort();
1388f22ef01cSRoman Divacky       }
1389f22ef01cSRoman Divacky       MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters);
1390f22ef01cSRoman Divacky     }
1391f22ef01cSRoman Divacky 
1392f22ef01cSRoman Divacky     if (ChildNo != getNumChildren())
1393f22ef01cSRoman Divacky       TP.error("Instruction '" + getOperator()->getName() +
1394f22ef01cSRoman Divacky                "' was provided too many operands!");
1395f22ef01cSRoman Divacky 
1396f22ef01cSRoman Divacky     return MadeChange;
1397f22ef01cSRoman Divacky   }
1398f22ef01cSRoman Divacky 
1399f22ef01cSRoman Divacky   assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!");
1400f22ef01cSRoman Divacky 
1401f22ef01cSRoman Divacky   // Node transforms always take one operand.
1402f22ef01cSRoman Divacky   if (getNumChildren() != 1)
1403f22ef01cSRoman Divacky     TP.error("Node transform '" + getOperator()->getName() +
1404f22ef01cSRoman Divacky              "' requires one operand!");
1405f22ef01cSRoman Divacky 
1406f22ef01cSRoman Divacky   bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
1407f22ef01cSRoman Divacky 
1408f22ef01cSRoman Divacky 
1409f22ef01cSRoman Divacky   // If either the output or input of the xform does not have exact
1410f22ef01cSRoman Divacky   // type info. We assume they must be the same. Otherwise, it is perfectly
1411f22ef01cSRoman Divacky   // legal to transform from one type to a completely different type.
1412f22ef01cSRoman Divacky #if 0
1413f22ef01cSRoman Divacky   if (!hasTypeSet() || !getChild(0)->hasTypeSet()) {
1414f22ef01cSRoman Divacky     bool MadeChange = UpdateNodeType(getChild(0)->getExtType(), TP);
1415f22ef01cSRoman Divacky     MadeChange |= getChild(0)->UpdateNodeType(getExtType(), TP);
1416f22ef01cSRoman Divacky     return MadeChange;
1417f22ef01cSRoman Divacky   }
1418f22ef01cSRoman Divacky #endif
1419f22ef01cSRoman Divacky   return MadeChange;
1420f22ef01cSRoman Divacky }
1421f22ef01cSRoman Divacky 
1422f22ef01cSRoman Divacky /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
1423f22ef01cSRoman Divacky /// RHS of a commutative operation, not the on LHS.
1424f22ef01cSRoman Divacky static bool OnlyOnRHSOfCommutative(TreePatternNode *N) {
1425f22ef01cSRoman Divacky   if (!N->isLeaf() && N->getOperator()->getName() == "imm")
1426f22ef01cSRoman Divacky     return true;
1427f22ef01cSRoman Divacky   if (N->isLeaf() && dynamic_cast<IntInit*>(N->getLeafValue()))
1428f22ef01cSRoman Divacky     return true;
1429f22ef01cSRoman Divacky   return false;
1430f22ef01cSRoman Divacky }
1431f22ef01cSRoman Divacky 
1432f22ef01cSRoman Divacky 
1433f22ef01cSRoman Divacky /// canPatternMatch - If it is impossible for this pattern to match on this
1434f22ef01cSRoman Divacky /// target, fill in Reason and return false.  Otherwise, return true.  This is
1435f22ef01cSRoman Divacky /// used as a sanity check for .td files (to prevent people from writing stuff
1436f22ef01cSRoman Divacky /// that can never possibly work), and to prevent the pattern permuter from
1437f22ef01cSRoman Divacky /// generating stuff that is useless.
1438f22ef01cSRoman Divacky bool TreePatternNode::canPatternMatch(std::string &Reason,
1439f22ef01cSRoman Divacky                                       const CodeGenDAGPatterns &CDP) {
1440f22ef01cSRoman Divacky   if (isLeaf()) return true;
1441f22ef01cSRoman Divacky 
1442f22ef01cSRoman Divacky   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1443f22ef01cSRoman Divacky     if (!getChild(i)->canPatternMatch(Reason, CDP))
1444f22ef01cSRoman Divacky       return false;
1445f22ef01cSRoman Divacky 
1446f22ef01cSRoman Divacky   // If this is an intrinsic, handle cases that would make it not match.  For
1447f22ef01cSRoman Divacky   // example, if an operand is required to be an immediate.
1448f22ef01cSRoman Divacky   if (getOperator()->isSubClassOf("Intrinsic")) {
1449f22ef01cSRoman Divacky     // TODO:
1450f22ef01cSRoman Divacky     return true;
1451f22ef01cSRoman Divacky   }
1452f22ef01cSRoman Divacky 
1453f22ef01cSRoman Divacky   // If this node is a commutative operator, check that the LHS isn't an
1454f22ef01cSRoman Divacky   // immediate.
1455f22ef01cSRoman Divacky   const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator());
1456f22ef01cSRoman Divacky   bool isCommIntrinsic = isCommutativeIntrinsic(CDP);
1457f22ef01cSRoman Divacky   if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
1458f22ef01cSRoman Divacky     // Scan all of the operands of the node and make sure that only the last one
1459f22ef01cSRoman Divacky     // is a constant node, unless the RHS also is.
1460f22ef01cSRoman Divacky     if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) {
1461f22ef01cSRoman Divacky       bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id.
1462f22ef01cSRoman Divacky       for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i)
1463f22ef01cSRoman Divacky         if (OnlyOnRHSOfCommutative(getChild(i))) {
1464f22ef01cSRoman Divacky           Reason="Immediate value must be on the RHS of commutative operators!";
1465f22ef01cSRoman Divacky           return false;
1466f22ef01cSRoman Divacky         }
1467f22ef01cSRoman Divacky     }
1468f22ef01cSRoman Divacky   }
1469f22ef01cSRoman Divacky 
1470f22ef01cSRoman Divacky   return true;
1471f22ef01cSRoman Divacky }
1472f22ef01cSRoman Divacky 
1473f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
1474f22ef01cSRoman Divacky // TreePattern implementation
1475f22ef01cSRoman Divacky //
1476f22ef01cSRoman Divacky 
1477f22ef01cSRoman Divacky TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
1478f22ef01cSRoman Divacky                          CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){
1479f22ef01cSRoman Divacky   isInputPattern = isInput;
1480f22ef01cSRoman Divacky   for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i)
1481f22ef01cSRoman Divacky     Trees.push_back(ParseTreePattern(RawPat->getElement(i), ""));
1482f22ef01cSRoman Divacky }
1483f22ef01cSRoman Divacky 
1484f22ef01cSRoman Divacky TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
1485f22ef01cSRoman Divacky                          CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){
1486f22ef01cSRoman Divacky   isInputPattern = isInput;
1487f22ef01cSRoman Divacky   Trees.push_back(ParseTreePattern(Pat, ""));
1488f22ef01cSRoman Divacky }
1489f22ef01cSRoman Divacky 
1490f22ef01cSRoman Divacky TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
1491f22ef01cSRoman Divacky                          CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){
1492f22ef01cSRoman Divacky   isInputPattern = isInput;
1493f22ef01cSRoman Divacky   Trees.push_back(Pat);
1494f22ef01cSRoman Divacky }
1495f22ef01cSRoman Divacky 
1496f22ef01cSRoman Divacky void TreePattern::error(const std::string &Msg) const {
1497f22ef01cSRoman Divacky   dump();
1498f22ef01cSRoman Divacky   throw TGError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg);
1499f22ef01cSRoman Divacky }
1500f22ef01cSRoman Divacky 
1501f22ef01cSRoman Divacky void TreePattern::ComputeNamedNodes() {
1502f22ef01cSRoman Divacky   for (unsigned i = 0, e = Trees.size(); i != e; ++i)
1503f22ef01cSRoman Divacky     ComputeNamedNodes(Trees[i]);
1504f22ef01cSRoman Divacky }
1505f22ef01cSRoman Divacky 
1506f22ef01cSRoman Divacky void TreePattern::ComputeNamedNodes(TreePatternNode *N) {
1507f22ef01cSRoman Divacky   if (!N->getName().empty())
1508f22ef01cSRoman Divacky     NamedNodes[N->getName()].push_back(N);
1509f22ef01cSRoman Divacky 
1510f22ef01cSRoman Divacky   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
1511f22ef01cSRoman Divacky     ComputeNamedNodes(N->getChild(i));
1512f22ef01cSRoman Divacky }
1513f22ef01cSRoman Divacky 
1514f22ef01cSRoman Divacky 
1515f22ef01cSRoman Divacky TreePatternNode *TreePattern::ParseTreePattern(Init *TheInit, StringRef OpName){
1516f22ef01cSRoman Divacky   if (DefInit *DI = dynamic_cast<DefInit*>(TheInit)) {
1517f22ef01cSRoman Divacky     Record *R = DI->getDef();
1518f22ef01cSRoman Divacky 
1519f22ef01cSRoman Divacky     // Direct reference to a leaf DagNode or PatFrag?  Turn it into a
1520f22ef01cSRoman Divacky     // TreePatternNode if its own.  For example:
1521f22ef01cSRoman Divacky     ///   (foo GPR, imm) -> (foo GPR, (imm))
1522f22ef01cSRoman Divacky     if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag"))
1523f22ef01cSRoman Divacky       return ParseTreePattern(new DagInit(DI, "",
1524f22ef01cSRoman Divacky                           std::vector<std::pair<Init*, std::string> >()),
1525f22ef01cSRoman Divacky                               OpName);
1526f22ef01cSRoman Divacky 
1527f22ef01cSRoman Divacky     // Input argument?
1528f22ef01cSRoman Divacky     TreePatternNode *Res = new TreePatternNode(DI, 1);
1529f22ef01cSRoman Divacky     if (R->getName() == "node" && !OpName.empty()) {
1530f22ef01cSRoman Divacky       if (OpName.empty())
1531f22ef01cSRoman Divacky         error("'node' argument requires a name to match with operand list");
1532f22ef01cSRoman Divacky       Args.push_back(OpName);
1533f22ef01cSRoman Divacky     }
1534f22ef01cSRoman Divacky 
1535f22ef01cSRoman Divacky     Res->setName(OpName);
1536f22ef01cSRoman Divacky     return Res;
1537f22ef01cSRoman Divacky   }
1538f22ef01cSRoman Divacky 
1539f22ef01cSRoman Divacky   if (IntInit *II = dynamic_cast<IntInit*>(TheInit)) {
1540f22ef01cSRoman Divacky     if (!OpName.empty())
1541f22ef01cSRoman Divacky       error("Constant int argument should not have a name!");
1542f22ef01cSRoman Divacky     return new TreePatternNode(II, 1);
1543f22ef01cSRoman Divacky   }
1544f22ef01cSRoman Divacky 
1545f22ef01cSRoman Divacky   if (BitsInit *BI = dynamic_cast<BitsInit*>(TheInit)) {
1546f22ef01cSRoman Divacky     // Turn this into an IntInit.
1547f22ef01cSRoman Divacky     Init *II = BI->convertInitializerTo(new IntRecTy());
1548f22ef01cSRoman Divacky     if (II == 0 || !dynamic_cast<IntInit*>(II))
1549f22ef01cSRoman Divacky       error("Bits value must be constants!");
1550f22ef01cSRoman Divacky     return ParseTreePattern(II, OpName);
1551f22ef01cSRoman Divacky   }
1552f22ef01cSRoman Divacky 
1553f22ef01cSRoman Divacky   DagInit *Dag = dynamic_cast<DagInit*>(TheInit);
1554f22ef01cSRoman Divacky   if (!Dag) {
1555f22ef01cSRoman Divacky     TheInit->dump();
1556f22ef01cSRoman Divacky     error("Pattern has unexpected init kind!");
1557f22ef01cSRoman Divacky   }
1558f22ef01cSRoman Divacky   DefInit *OpDef = dynamic_cast<DefInit*>(Dag->getOperator());
1559f22ef01cSRoman Divacky   if (!OpDef) error("Pattern has unexpected operator type!");
1560f22ef01cSRoman Divacky   Record *Operator = OpDef->getDef();
1561f22ef01cSRoman Divacky 
1562f22ef01cSRoman Divacky   if (Operator->isSubClassOf("ValueType")) {
1563f22ef01cSRoman Divacky     // If the operator is a ValueType, then this must be "type cast" of a leaf
1564f22ef01cSRoman Divacky     // node.
1565f22ef01cSRoman Divacky     if (Dag->getNumArgs() != 1)
1566f22ef01cSRoman Divacky       error("Type cast only takes one operand!");
1567f22ef01cSRoman Divacky 
1568f22ef01cSRoman Divacky     TreePatternNode *New = ParseTreePattern(Dag->getArg(0), Dag->getArgName(0));
1569f22ef01cSRoman Divacky 
1570f22ef01cSRoman Divacky     // Apply the type cast.
1571f22ef01cSRoman Divacky     assert(New->getNumTypes() == 1 && "FIXME: Unhandled");
1572f22ef01cSRoman Divacky     New->UpdateNodeType(0, getValueType(Operator), *this);
1573f22ef01cSRoman Divacky 
1574f22ef01cSRoman Divacky     if (!OpName.empty())
1575f22ef01cSRoman Divacky       error("ValueType cast should not have a name!");
1576f22ef01cSRoman Divacky     return New;
1577f22ef01cSRoman Divacky   }
1578f22ef01cSRoman Divacky 
1579f22ef01cSRoman Divacky   // Verify that this is something that makes sense for an operator.
1580f22ef01cSRoman Divacky   if (!Operator->isSubClassOf("PatFrag") &&
1581f22ef01cSRoman Divacky       !Operator->isSubClassOf("SDNode") &&
1582f22ef01cSRoman Divacky       !Operator->isSubClassOf("Instruction") &&
1583f22ef01cSRoman Divacky       !Operator->isSubClassOf("SDNodeXForm") &&
1584f22ef01cSRoman Divacky       !Operator->isSubClassOf("Intrinsic") &&
1585f22ef01cSRoman Divacky       Operator->getName() != "set" &&
1586f22ef01cSRoman Divacky       Operator->getName() != "implicit")
1587f22ef01cSRoman Divacky     error("Unrecognized node '" + Operator->getName() + "'!");
1588f22ef01cSRoman Divacky 
1589f22ef01cSRoman Divacky   //  Check to see if this is something that is illegal in an input pattern.
1590f22ef01cSRoman Divacky   if (isInputPattern) {
1591f22ef01cSRoman Divacky     if (Operator->isSubClassOf("Instruction") ||
1592f22ef01cSRoman Divacky         Operator->isSubClassOf("SDNodeXForm"))
1593f22ef01cSRoman Divacky       error("Cannot use '" + Operator->getName() + "' in an input pattern!");
1594f22ef01cSRoman Divacky   } else {
1595f22ef01cSRoman Divacky     if (Operator->isSubClassOf("Intrinsic"))
1596f22ef01cSRoman Divacky       error("Cannot use '" + Operator->getName() + "' in an output pattern!");
1597f22ef01cSRoman Divacky 
1598f22ef01cSRoman Divacky     if (Operator->isSubClassOf("SDNode") &&
1599f22ef01cSRoman Divacky         Operator->getName() != "imm" &&
1600f22ef01cSRoman Divacky         Operator->getName() != "fpimm" &&
1601f22ef01cSRoman Divacky         Operator->getName() != "tglobaltlsaddr" &&
1602f22ef01cSRoman Divacky         Operator->getName() != "tconstpool" &&
1603f22ef01cSRoman Divacky         Operator->getName() != "tjumptable" &&
1604f22ef01cSRoman Divacky         Operator->getName() != "tframeindex" &&
1605f22ef01cSRoman Divacky         Operator->getName() != "texternalsym" &&
1606f22ef01cSRoman Divacky         Operator->getName() != "tblockaddress" &&
1607f22ef01cSRoman Divacky         Operator->getName() != "tglobaladdr" &&
1608f22ef01cSRoman Divacky         Operator->getName() != "bb" &&
1609f22ef01cSRoman Divacky         Operator->getName() != "vt")
1610f22ef01cSRoman Divacky       error("Cannot use '" + Operator->getName() + "' in an output pattern!");
1611f22ef01cSRoman Divacky   }
1612f22ef01cSRoman Divacky 
1613f22ef01cSRoman Divacky   std::vector<TreePatternNode*> Children;
1614f22ef01cSRoman Divacky 
1615f22ef01cSRoman Divacky   // Parse all the operands.
1616f22ef01cSRoman Divacky   for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i)
1617f22ef01cSRoman Divacky     Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgName(i)));
1618f22ef01cSRoman Divacky 
1619f22ef01cSRoman Divacky   // If the operator is an intrinsic, then this is just syntactic sugar for for
1620f22ef01cSRoman Divacky   // (intrinsic_* <number>, ..children..).  Pick the right intrinsic node, and
1621f22ef01cSRoman Divacky   // convert the intrinsic name to a number.
1622f22ef01cSRoman Divacky   if (Operator->isSubClassOf("Intrinsic")) {
1623f22ef01cSRoman Divacky     const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator);
1624f22ef01cSRoman Divacky     unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1;
1625f22ef01cSRoman Divacky 
1626f22ef01cSRoman Divacky     // If this intrinsic returns void, it must have side-effects and thus a
1627f22ef01cSRoman Divacky     // chain.
1628f22ef01cSRoman Divacky     if (Int.IS.RetVTs.empty())
1629f22ef01cSRoman Divacky       Operator = getDAGPatterns().get_intrinsic_void_sdnode();
1630f22ef01cSRoman Divacky     else if (Int.ModRef != CodeGenIntrinsic::NoMem)
1631f22ef01cSRoman Divacky       // Has side-effects, requires chain.
1632f22ef01cSRoman Divacky       Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode();
1633f22ef01cSRoman Divacky     else // Otherwise, no chain.
1634f22ef01cSRoman Divacky       Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode();
1635f22ef01cSRoman Divacky 
1636f22ef01cSRoman Divacky     TreePatternNode *IIDNode = new TreePatternNode(new IntInit(IID), 1);
1637f22ef01cSRoman Divacky     Children.insert(Children.begin(), IIDNode);
1638f22ef01cSRoman Divacky   }
1639f22ef01cSRoman Divacky 
1640f22ef01cSRoman Divacky   unsigned NumResults = GetNumNodeResults(Operator, CDP);
1641f22ef01cSRoman Divacky   TreePatternNode *Result = new TreePatternNode(Operator, Children, NumResults);
1642f22ef01cSRoman Divacky   Result->setName(OpName);
1643f22ef01cSRoman Divacky 
1644f22ef01cSRoman Divacky   if (!Dag->getName().empty()) {
1645f22ef01cSRoman Divacky     assert(Result->getName().empty());
1646f22ef01cSRoman Divacky     Result->setName(Dag->getName());
1647f22ef01cSRoman Divacky   }
1648f22ef01cSRoman Divacky   return Result;
1649f22ef01cSRoman Divacky }
1650f22ef01cSRoman Divacky 
1651f22ef01cSRoman Divacky /// SimplifyTree - See if we can simplify this tree to eliminate something that
1652f22ef01cSRoman Divacky /// will never match in favor of something obvious that will.  This is here
1653f22ef01cSRoman Divacky /// strictly as a convenience to target authors because it allows them to write
1654f22ef01cSRoman Divacky /// more type generic things and have useless type casts fold away.
1655f22ef01cSRoman Divacky ///
1656f22ef01cSRoman Divacky /// This returns true if any change is made.
1657f22ef01cSRoman Divacky static bool SimplifyTree(TreePatternNode *&N) {
1658f22ef01cSRoman Divacky   if (N->isLeaf())
1659f22ef01cSRoman Divacky     return false;
1660f22ef01cSRoman Divacky 
1661f22ef01cSRoman Divacky   // If we have a bitconvert with a resolved type and if the source and
1662f22ef01cSRoman Divacky   // destination types are the same, then the bitconvert is useless, remove it.
1663f22ef01cSRoman Divacky   if (N->getOperator()->getName() == "bitconvert" &&
1664f22ef01cSRoman Divacky       N->getExtType(0).isConcrete() &&
1665f22ef01cSRoman Divacky       N->getExtType(0) == N->getChild(0)->getExtType(0) &&
1666f22ef01cSRoman Divacky       N->getName().empty()) {
1667f22ef01cSRoman Divacky     N = N->getChild(0);
1668f22ef01cSRoman Divacky     SimplifyTree(N);
1669f22ef01cSRoman Divacky     return true;
1670f22ef01cSRoman Divacky   }
1671f22ef01cSRoman Divacky 
1672f22ef01cSRoman Divacky   // Walk all children.
1673f22ef01cSRoman Divacky   bool MadeChange = false;
1674f22ef01cSRoman Divacky   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
1675f22ef01cSRoman Divacky     TreePatternNode *Child = N->getChild(i);
1676f22ef01cSRoman Divacky     MadeChange |= SimplifyTree(Child);
1677f22ef01cSRoman Divacky     N->setChild(i, Child);
1678f22ef01cSRoman Divacky   }
1679f22ef01cSRoman Divacky   return MadeChange;
1680f22ef01cSRoman Divacky }
1681f22ef01cSRoman Divacky 
1682f22ef01cSRoman Divacky 
1683f22ef01cSRoman Divacky 
1684f22ef01cSRoman Divacky /// InferAllTypes - Infer/propagate as many types throughout the expression
1685f22ef01cSRoman Divacky /// patterns as possible.  Return true if all types are inferred, false
1686f22ef01cSRoman Divacky /// otherwise.  Throw an exception if a type contradiction is found.
1687f22ef01cSRoman Divacky bool TreePattern::
1688f22ef01cSRoman Divacky InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) {
1689f22ef01cSRoman Divacky   if (NamedNodes.empty())
1690f22ef01cSRoman Divacky     ComputeNamedNodes();
1691f22ef01cSRoman Divacky 
1692f22ef01cSRoman Divacky   bool MadeChange = true;
1693f22ef01cSRoman Divacky   while (MadeChange) {
1694f22ef01cSRoman Divacky     MadeChange = false;
1695f22ef01cSRoman Divacky     for (unsigned i = 0, e = Trees.size(); i != e; ++i) {
1696f22ef01cSRoman Divacky       MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false);
1697f22ef01cSRoman Divacky       MadeChange |= SimplifyTree(Trees[i]);
1698f22ef01cSRoman Divacky     }
1699f22ef01cSRoman Divacky 
1700f22ef01cSRoman Divacky     // If there are constraints on our named nodes, apply them.
1701f22ef01cSRoman Divacky     for (StringMap<SmallVector<TreePatternNode*,1> >::iterator
1702f22ef01cSRoman Divacky          I = NamedNodes.begin(), E = NamedNodes.end(); I != E; ++I) {
1703f22ef01cSRoman Divacky       SmallVectorImpl<TreePatternNode*> &Nodes = I->second;
1704f22ef01cSRoman Divacky 
1705f22ef01cSRoman Divacky       // If we have input named node types, propagate their types to the named
1706f22ef01cSRoman Divacky       // values here.
1707f22ef01cSRoman Divacky       if (InNamedTypes) {
1708f22ef01cSRoman Divacky         // FIXME: Should be error?
1709f22ef01cSRoman Divacky         assert(InNamedTypes->count(I->getKey()) &&
1710f22ef01cSRoman Divacky                "Named node in output pattern but not input pattern?");
1711f22ef01cSRoman Divacky 
1712f22ef01cSRoman Divacky         const SmallVectorImpl<TreePatternNode*> &InNodes =
1713f22ef01cSRoman Divacky           InNamedTypes->find(I->getKey())->second;
1714f22ef01cSRoman Divacky 
1715f22ef01cSRoman Divacky         // The input types should be fully resolved by now.
1716f22ef01cSRoman Divacky         for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
1717f22ef01cSRoman Divacky           // If this node is a register class, and it is the root of the pattern
1718f22ef01cSRoman Divacky           // then we're mapping something onto an input register.  We allow
1719f22ef01cSRoman Divacky           // changing the type of the input register in this case.  This allows
1720f22ef01cSRoman Divacky           // us to match things like:
1721f22ef01cSRoman Divacky           //  def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
1722f22ef01cSRoman Divacky           if (Nodes[i] == Trees[0] && Nodes[i]->isLeaf()) {
1723f22ef01cSRoman Divacky             DefInit *DI = dynamic_cast<DefInit*>(Nodes[i]->getLeafValue());
1724f22ef01cSRoman Divacky             if (DI && DI->getDef()->isSubClassOf("RegisterClass"))
1725f22ef01cSRoman Divacky               continue;
1726f22ef01cSRoman Divacky           }
1727f22ef01cSRoman Divacky 
1728f22ef01cSRoman Divacky           assert(Nodes[i]->getNumTypes() == 1 &&
1729f22ef01cSRoman Divacky                  InNodes[0]->getNumTypes() == 1 &&
1730f22ef01cSRoman Divacky                  "FIXME: cannot name multiple result nodes yet");
1731f22ef01cSRoman Divacky           MadeChange |= Nodes[i]->UpdateNodeType(0, InNodes[0]->getExtType(0),
1732f22ef01cSRoman Divacky                                                  *this);
1733f22ef01cSRoman Divacky         }
1734f22ef01cSRoman Divacky       }
1735f22ef01cSRoman Divacky 
1736f22ef01cSRoman Divacky       // If there are multiple nodes with the same name, they must all have the
1737f22ef01cSRoman Divacky       // same type.
1738f22ef01cSRoman Divacky       if (I->second.size() > 1) {
1739f22ef01cSRoman Divacky         for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) {
1740f22ef01cSRoman Divacky           TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1];
1741f22ef01cSRoman Divacky           assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&
1742f22ef01cSRoman Divacky                  "FIXME: cannot name multiple result nodes yet");
1743f22ef01cSRoman Divacky 
1744f22ef01cSRoman Divacky           MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this);
1745f22ef01cSRoman Divacky           MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this);
1746f22ef01cSRoman Divacky         }
1747f22ef01cSRoman Divacky       }
1748f22ef01cSRoman Divacky     }
1749f22ef01cSRoman Divacky   }
1750f22ef01cSRoman Divacky 
1751f22ef01cSRoman Divacky   bool HasUnresolvedTypes = false;
1752f22ef01cSRoman Divacky   for (unsigned i = 0, e = Trees.size(); i != e; ++i)
1753f22ef01cSRoman Divacky     HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType();
1754f22ef01cSRoman Divacky   return !HasUnresolvedTypes;
1755f22ef01cSRoman Divacky }
1756f22ef01cSRoman Divacky 
1757f22ef01cSRoman Divacky void TreePattern::print(raw_ostream &OS) const {
1758f22ef01cSRoman Divacky   OS << getRecord()->getName();
1759f22ef01cSRoman Divacky   if (!Args.empty()) {
1760f22ef01cSRoman Divacky     OS << "(" << Args[0];
1761f22ef01cSRoman Divacky     for (unsigned i = 1, e = Args.size(); i != e; ++i)
1762f22ef01cSRoman Divacky       OS << ", " << Args[i];
1763f22ef01cSRoman Divacky     OS << ")";
1764f22ef01cSRoman Divacky   }
1765f22ef01cSRoman Divacky   OS << ": ";
1766f22ef01cSRoman Divacky 
1767f22ef01cSRoman Divacky   if (Trees.size() > 1)
1768f22ef01cSRoman Divacky     OS << "[\n";
1769f22ef01cSRoman Divacky   for (unsigned i = 0, e = Trees.size(); i != e; ++i) {
1770f22ef01cSRoman Divacky     OS << "\t";
1771f22ef01cSRoman Divacky     Trees[i]->print(OS);
1772f22ef01cSRoman Divacky     OS << "\n";
1773f22ef01cSRoman Divacky   }
1774f22ef01cSRoman Divacky 
1775f22ef01cSRoman Divacky   if (Trees.size() > 1)
1776f22ef01cSRoman Divacky     OS << "]\n";
1777f22ef01cSRoman Divacky }
1778f22ef01cSRoman Divacky 
1779f22ef01cSRoman Divacky void TreePattern::dump() const { print(errs()); }
1780f22ef01cSRoman Divacky 
1781f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
1782f22ef01cSRoman Divacky // CodeGenDAGPatterns implementation
1783f22ef01cSRoman Divacky //
1784f22ef01cSRoman Divacky 
1785f22ef01cSRoman Divacky CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : Records(R) {
1786f22ef01cSRoman Divacky   Intrinsics = LoadIntrinsics(Records, false);
1787f22ef01cSRoman Divacky   TgtIntrinsics = LoadIntrinsics(Records, true);
1788f22ef01cSRoman Divacky   ParseNodeInfo();
1789f22ef01cSRoman Divacky   ParseNodeTransforms();
1790f22ef01cSRoman Divacky   ParseComplexPatterns();
1791f22ef01cSRoman Divacky   ParsePatternFragments();
1792f22ef01cSRoman Divacky   ParseDefaultOperands();
1793f22ef01cSRoman Divacky   ParseInstructions();
1794f22ef01cSRoman Divacky   ParsePatterns();
1795f22ef01cSRoman Divacky 
1796f22ef01cSRoman Divacky   // Generate variants.  For example, commutative patterns can match
1797f22ef01cSRoman Divacky   // multiple ways.  Add them to PatternsToMatch as well.
1798f22ef01cSRoman Divacky   GenerateVariants();
1799f22ef01cSRoman Divacky 
1800f22ef01cSRoman Divacky   // Infer instruction flags.  For example, we can detect loads,
1801f22ef01cSRoman Divacky   // stores, and side effects in many cases by examining an
1802f22ef01cSRoman Divacky   // instruction's pattern.
1803f22ef01cSRoman Divacky   InferInstructionFlags();
1804f22ef01cSRoman Divacky }
1805f22ef01cSRoman Divacky 
1806f22ef01cSRoman Divacky CodeGenDAGPatterns::~CodeGenDAGPatterns() {
1807f22ef01cSRoman Divacky   for (pf_iterator I = PatternFragments.begin(),
1808f22ef01cSRoman Divacky        E = PatternFragments.end(); I != E; ++I)
1809f22ef01cSRoman Divacky     delete I->second;
1810f22ef01cSRoman Divacky }
1811f22ef01cSRoman Divacky 
1812f22ef01cSRoman Divacky 
1813f22ef01cSRoman Divacky Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const {
1814f22ef01cSRoman Divacky   Record *N = Records.getDef(Name);
1815f22ef01cSRoman Divacky   if (!N || !N->isSubClassOf("SDNode")) {
1816f22ef01cSRoman Divacky     errs() << "Error getting SDNode '" << Name << "'!\n";
1817f22ef01cSRoman Divacky     exit(1);
1818f22ef01cSRoman Divacky   }
1819f22ef01cSRoman Divacky   return N;
1820f22ef01cSRoman Divacky }
1821f22ef01cSRoman Divacky 
1822f22ef01cSRoman Divacky // Parse all of the SDNode definitions for the target, populating SDNodes.
1823f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseNodeInfo() {
1824f22ef01cSRoman Divacky   std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode");
1825f22ef01cSRoman Divacky   while (!Nodes.empty()) {
1826f22ef01cSRoman Divacky     SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back()));
1827f22ef01cSRoman Divacky     Nodes.pop_back();
1828f22ef01cSRoman Divacky   }
1829f22ef01cSRoman Divacky 
1830f22ef01cSRoman Divacky   // Get the builtin intrinsic nodes.
1831f22ef01cSRoman Divacky   intrinsic_void_sdnode     = getSDNodeNamed("intrinsic_void");
1832f22ef01cSRoman Divacky   intrinsic_w_chain_sdnode  = getSDNodeNamed("intrinsic_w_chain");
1833f22ef01cSRoman Divacky   intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain");
1834f22ef01cSRoman Divacky }
1835f22ef01cSRoman Divacky 
1836f22ef01cSRoman Divacky /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
1837f22ef01cSRoman Divacky /// map, and emit them to the file as functions.
1838f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseNodeTransforms() {
1839f22ef01cSRoman Divacky   std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm");
1840f22ef01cSRoman Divacky   while (!Xforms.empty()) {
1841f22ef01cSRoman Divacky     Record *XFormNode = Xforms.back();
1842f22ef01cSRoman Divacky     Record *SDNode = XFormNode->getValueAsDef("Opcode");
1843f22ef01cSRoman Divacky     std::string Code = XFormNode->getValueAsCode("XFormFunction");
1844f22ef01cSRoman Divacky     SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code)));
1845f22ef01cSRoman Divacky 
1846f22ef01cSRoman Divacky     Xforms.pop_back();
1847f22ef01cSRoman Divacky   }
1848f22ef01cSRoman Divacky }
1849f22ef01cSRoman Divacky 
1850f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseComplexPatterns() {
1851f22ef01cSRoman Divacky   std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern");
1852f22ef01cSRoman Divacky   while (!AMs.empty()) {
1853f22ef01cSRoman Divacky     ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back()));
1854f22ef01cSRoman Divacky     AMs.pop_back();
1855f22ef01cSRoman Divacky   }
1856f22ef01cSRoman Divacky }
1857f22ef01cSRoman Divacky 
1858f22ef01cSRoman Divacky 
1859f22ef01cSRoman Divacky /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
1860f22ef01cSRoman Divacky /// file, building up the PatternFragments map.  After we've collected them all,
1861f22ef01cSRoman Divacky /// inline fragments together as necessary, so that there are no references left
1862f22ef01cSRoman Divacky /// inside a pattern fragment to a pattern fragment.
1863f22ef01cSRoman Divacky ///
1864f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParsePatternFragments() {
1865f22ef01cSRoman Divacky   std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag");
1866f22ef01cSRoman Divacky 
1867f22ef01cSRoman Divacky   // First step, parse all of the fragments.
1868f22ef01cSRoman Divacky   for (unsigned i = 0, e = Fragments.size(); i != e; ++i) {
1869f22ef01cSRoman Divacky     DagInit *Tree = Fragments[i]->getValueAsDag("Fragment");
1870f22ef01cSRoman Divacky     TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this);
1871f22ef01cSRoman Divacky     PatternFragments[Fragments[i]] = P;
1872f22ef01cSRoman Divacky 
1873f22ef01cSRoman Divacky     // Validate the argument list, converting it to set, to discard duplicates.
1874f22ef01cSRoman Divacky     std::vector<std::string> &Args = P->getArgList();
1875f22ef01cSRoman Divacky     std::set<std::string> OperandsSet(Args.begin(), Args.end());
1876f22ef01cSRoman Divacky 
1877f22ef01cSRoman Divacky     if (OperandsSet.count(""))
1878f22ef01cSRoman Divacky       P->error("Cannot have unnamed 'node' values in pattern fragment!");
1879f22ef01cSRoman Divacky 
1880f22ef01cSRoman Divacky     // Parse the operands list.
1881f22ef01cSRoman Divacky     DagInit *OpsList = Fragments[i]->getValueAsDag("Operands");
1882f22ef01cSRoman Divacky     DefInit *OpsOp = dynamic_cast<DefInit*>(OpsList->getOperator());
1883f22ef01cSRoman Divacky     // Special cases: ops == outs == ins. Different names are used to
1884f22ef01cSRoman Divacky     // improve readability.
1885f22ef01cSRoman Divacky     if (!OpsOp ||
1886f22ef01cSRoman Divacky         (OpsOp->getDef()->getName() != "ops" &&
1887f22ef01cSRoman Divacky          OpsOp->getDef()->getName() != "outs" &&
1888f22ef01cSRoman Divacky          OpsOp->getDef()->getName() != "ins"))
1889f22ef01cSRoman Divacky       P->error("Operands list should start with '(ops ... '!");
1890f22ef01cSRoman Divacky 
1891f22ef01cSRoman Divacky     // Copy over the arguments.
1892f22ef01cSRoman Divacky     Args.clear();
1893f22ef01cSRoman Divacky     for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) {
1894f22ef01cSRoman Divacky       if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) ||
1895f22ef01cSRoman Divacky           static_cast<DefInit*>(OpsList->getArg(j))->
1896f22ef01cSRoman Divacky           getDef()->getName() != "node")
1897f22ef01cSRoman Divacky         P->error("Operands list should all be 'node' values.");
1898f22ef01cSRoman Divacky       if (OpsList->getArgName(j).empty())
1899f22ef01cSRoman Divacky         P->error("Operands list should have names for each operand!");
1900f22ef01cSRoman Divacky       if (!OperandsSet.count(OpsList->getArgName(j)))
1901f22ef01cSRoman Divacky         P->error("'" + OpsList->getArgName(j) +
1902f22ef01cSRoman Divacky                  "' does not occur in pattern or was multiply specified!");
1903f22ef01cSRoman Divacky       OperandsSet.erase(OpsList->getArgName(j));
1904f22ef01cSRoman Divacky       Args.push_back(OpsList->getArgName(j));
1905f22ef01cSRoman Divacky     }
1906f22ef01cSRoman Divacky 
1907f22ef01cSRoman Divacky     if (!OperandsSet.empty())
1908f22ef01cSRoman Divacky       P->error("Operands list does not contain an entry for operand '" +
1909f22ef01cSRoman Divacky                *OperandsSet.begin() + "'!");
1910f22ef01cSRoman Divacky 
1911f22ef01cSRoman Divacky     // If there is a code init for this fragment, keep track of the fact that
1912f22ef01cSRoman Divacky     // this fragment uses it.
1913f22ef01cSRoman Divacky     std::string Code = Fragments[i]->getValueAsCode("Predicate");
1914f22ef01cSRoman Divacky     if (!Code.empty())
1915f22ef01cSRoman Divacky       P->getOnlyTree()->addPredicateFn("Predicate_"+Fragments[i]->getName());
1916f22ef01cSRoman Divacky 
1917f22ef01cSRoman Divacky     // If there is a node transformation corresponding to this, keep track of
1918f22ef01cSRoman Divacky     // it.
1919f22ef01cSRoman Divacky     Record *Transform = Fragments[i]->getValueAsDef("OperandTransform");
1920f22ef01cSRoman Divacky     if (!getSDNodeTransform(Transform).second.empty())    // not noop xform?
1921f22ef01cSRoman Divacky       P->getOnlyTree()->setTransformFn(Transform);
1922f22ef01cSRoman Divacky   }
1923f22ef01cSRoman Divacky 
1924f22ef01cSRoman Divacky   // Now that we've parsed all of the tree fragments, do a closure on them so
1925f22ef01cSRoman Divacky   // that there are not references to PatFrags left inside of them.
1926f22ef01cSRoman Divacky   for (unsigned i = 0, e = Fragments.size(); i != e; ++i) {
1927f22ef01cSRoman Divacky     TreePattern *ThePat = PatternFragments[Fragments[i]];
1928f22ef01cSRoman Divacky     ThePat->InlinePatternFragments();
1929f22ef01cSRoman Divacky 
1930f22ef01cSRoman Divacky     // Infer as many types as possible.  Don't worry about it if we don't infer
1931f22ef01cSRoman Divacky     // all of them, some may depend on the inputs of the pattern.
1932f22ef01cSRoman Divacky     try {
1933f22ef01cSRoman Divacky       ThePat->InferAllTypes();
1934f22ef01cSRoman Divacky     } catch (...) {
1935f22ef01cSRoman Divacky       // If this pattern fragment is not supported by this target (no types can
1936f22ef01cSRoman Divacky       // satisfy its constraints), just ignore it.  If the bogus pattern is
1937f22ef01cSRoman Divacky       // actually used by instructions, the type consistency error will be
1938f22ef01cSRoman Divacky       // reported there.
1939f22ef01cSRoman Divacky     }
1940f22ef01cSRoman Divacky 
1941f22ef01cSRoman Divacky     // If debugging, print out the pattern fragment result.
1942f22ef01cSRoman Divacky     DEBUG(ThePat->dump());
1943f22ef01cSRoman Divacky   }
1944f22ef01cSRoman Divacky }
1945f22ef01cSRoman Divacky 
1946f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseDefaultOperands() {
1947f22ef01cSRoman Divacky   std::vector<Record*> DefaultOps[2];
1948f22ef01cSRoman Divacky   DefaultOps[0] = Records.getAllDerivedDefinitions("PredicateOperand");
1949f22ef01cSRoman Divacky   DefaultOps[1] = Records.getAllDerivedDefinitions("OptionalDefOperand");
1950f22ef01cSRoman Divacky 
1951f22ef01cSRoman Divacky   // Find some SDNode.
1952f22ef01cSRoman Divacky   assert(!SDNodes.empty() && "No SDNodes parsed?");
1953f22ef01cSRoman Divacky   Init *SomeSDNode = new DefInit(SDNodes.begin()->first);
1954f22ef01cSRoman Divacky 
1955f22ef01cSRoman Divacky   for (unsigned iter = 0; iter != 2; ++iter) {
1956f22ef01cSRoman Divacky     for (unsigned i = 0, e = DefaultOps[iter].size(); i != e; ++i) {
1957f22ef01cSRoman Divacky       DagInit *DefaultInfo = DefaultOps[iter][i]->getValueAsDag("DefaultOps");
1958f22ef01cSRoman Divacky 
1959f22ef01cSRoman Divacky       // Clone the DefaultInfo dag node, changing the operator from 'ops' to
1960f22ef01cSRoman Divacky       // SomeSDnode so that we can parse this.
1961f22ef01cSRoman Divacky       std::vector<std::pair<Init*, std::string> > Ops;
1962f22ef01cSRoman Divacky       for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op)
1963f22ef01cSRoman Divacky         Ops.push_back(std::make_pair(DefaultInfo->getArg(op),
1964f22ef01cSRoman Divacky                                      DefaultInfo->getArgName(op)));
1965f22ef01cSRoman Divacky       DagInit *DI = new DagInit(SomeSDNode, "", Ops);
1966f22ef01cSRoman Divacky 
1967f22ef01cSRoman Divacky       // Create a TreePattern to parse this.
1968f22ef01cSRoman Divacky       TreePattern P(DefaultOps[iter][i], DI, false, *this);
1969f22ef01cSRoman Divacky       assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!");
1970f22ef01cSRoman Divacky 
1971f22ef01cSRoman Divacky       // Copy the operands over into a DAGDefaultOperand.
1972f22ef01cSRoman Divacky       DAGDefaultOperand DefaultOpInfo;
1973f22ef01cSRoman Divacky 
1974f22ef01cSRoman Divacky       TreePatternNode *T = P.getTree(0);
1975f22ef01cSRoman Divacky       for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) {
1976f22ef01cSRoman Divacky         TreePatternNode *TPN = T->getChild(op);
1977f22ef01cSRoman Divacky         while (TPN->ApplyTypeConstraints(P, false))
1978f22ef01cSRoman Divacky           /* Resolve all types */;
1979f22ef01cSRoman Divacky 
1980f22ef01cSRoman Divacky         if (TPN->ContainsUnresolvedType()) {
1981f22ef01cSRoman Divacky           if (iter == 0)
1982f22ef01cSRoman Divacky             throw "Value #" + utostr(i) + " of PredicateOperand '" +
1983f22ef01cSRoman Divacky               DefaultOps[iter][i]->getName() +"' doesn't have a concrete type!";
1984f22ef01cSRoman Divacky           else
1985f22ef01cSRoman Divacky             throw "Value #" + utostr(i) + " of OptionalDefOperand '" +
1986f22ef01cSRoman Divacky               DefaultOps[iter][i]->getName() +"' doesn't have a concrete type!";
1987f22ef01cSRoman Divacky         }
1988f22ef01cSRoman Divacky         DefaultOpInfo.DefaultOps.push_back(TPN);
1989f22ef01cSRoman Divacky       }
1990f22ef01cSRoman Divacky 
1991f22ef01cSRoman Divacky       // Insert it into the DefaultOperands map so we can find it later.
1992f22ef01cSRoman Divacky       DefaultOperands[DefaultOps[iter][i]] = DefaultOpInfo;
1993f22ef01cSRoman Divacky     }
1994f22ef01cSRoman Divacky   }
1995f22ef01cSRoman Divacky }
1996f22ef01cSRoman Divacky 
1997f22ef01cSRoman Divacky /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
1998f22ef01cSRoman Divacky /// instruction input.  Return true if this is a real use.
1999f22ef01cSRoman Divacky static bool HandleUse(TreePattern *I, TreePatternNode *Pat,
2000f22ef01cSRoman Divacky                       std::map<std::string, TreePatternNode*> &InstInputs) {
2001f22ef01cSRoman Divacky   // No name -> not interesting.
2002f22ef01cSRoman Divacky   if (Pat->getName().empty()) {
2003f22ef01cSRoman Divacky     if (Pat->isLeaf()) {
2004f22ef01cSRoman Divacky       DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue());
2005f22ef01cSRoman Divacky       if (DI && DI->getDef()->isSubClassOf("RegisterClass"))
2006f22ef01cSRoman Divacky         I->error("Input " + DI->getDef()->getName() + " must be named!");
2007f22ef01cSRoman Divacky     }
2008f22ef01cSRoman Divacky     return false;
2009f22ef01cSRoman Divacky   }
2010f22ef01cSRoman Divacky 
2011f22ef01cSRoman Divacky   Record *Rec;
2012f22ef01cSRoman Divacky   if (Pat->isLeaf()) {
2013f22ef01cSRoman Divacky     DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue());
2014f22ef01cSRoman Divacky     if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!");
2015f22ef01cSRoman Divacky     Rec = DI->getDef();
2016f22ef01cSRoman Divacky   } else {
2017f22ef01cSRoman Divacky     Rec = Pat->getOperator();
2018f22ef01cSRoman Divacky   }
2019f22ef01cSRoman Divacky 
2020f22ef01cSRoman Divacky   // SRCVALUE nodes are ignored.
2021f22ef01cSRoman Divacky   if (Rec->getName() == "srcvalue")
2022f22ef01cSRoman Divacky     return false;
2023f22ef01cSRoman Divacky 
2024f22ef01cSRoman Divacky   TreePatternNode *&Slot = InstInputs[Pat->getName()];
2025f22ef01cSRoman Divacky   if (!Slot) {
2026f22ef01cSRoman Divacky     Slot = Pat;
2027f22ef01cSRoman Divacky     return true;
2028f22ef01cSRoman Divacky   }
2029f22ef01cSRoman Divacky   Record *SlotRec;
2030f22ef01cSRoman Divacky   if (Slot->isLeaf()) {
2031f22ef01cSRoman Divacky     SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef();
2032f22ef01cSRoman Divacky   } else {
2033f22ef01cSRoman Divacky     assert(Slot->getNumChildren() == 0 && "can't be a use with children!");
2034f22ef01cSRoman Divacky     SlotRec = Slot->getOperator();
2035f22ef01cSRoman Divacky   }
2036f22ef01cSRoman Divacky 
2037f22ef01cSRoman Divacky   // Ensure that the inputs agree if we've already seen this input.
2038f22ef01cSRoman Divacky   if (Rec != SlotRec)
2039f22ef01cSRoman Divacky     I->error("All $" + Pat->getName() + " inputs must agree with each other");
2040f22ef01cSRoman Divacky   if (Slot->getExtTypes() != Pat->getExtTypes())
2041f22ef01cSRoman Divacky     I->error("All $" + Pat->getName() + " inputs must agree with each other");
2042f22ef01cSRoman Divacky   return true;
2043f22ef01cSRoman Divacky }
2044f22ef01cSRoman Divacky 
2045f22ef01cSRoman Divacky /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is
2046f22ef01cSRoman Divacky /// part of "I", the instruction), computing the set of inputs and outputs of
2047f22ef01cSRoman Divacky /// the pattern.  Report errors if we see anything naughty.
2048f22ef01cSRoman Divacky void CodeGenDAGPatterns::
2049f22ef01cSRoman Divacky FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
2050f22ef01cSRoman Divacky                             std::map<std::string, TreePatternNode*> &InstInputs,
2051f22ef01cSRoman Divacky                             std::map<std::string, TreePatternNode*>&InstResults,
2052f22ef01cSRoman Divacky                             std::vector<Record*> &InstImpResults) {
2053f22ef01cSRoman Divacky   if (Pat->isLeaf()) {
2054f22ef01cSRoman Divacky     bool isUse = HandleUse(I, Pat, InstInputs);
2055f22ef01cSRoman Divacky     if (!isUse && Pat->getTransformFn())
2056f22ef01cSRoman Divacky       I->error("Cannot specify a transform function for a non-input value!");
2057f22ef01cSRoman Divacky     return;
2058f22ef01cSRoman Divacky   }
2059f22ef01cSRoman Divacky 
2060f22ef01cSRoman Divacky   if (Pat->getOperator()->getName() == "implicit") {
2061f22ef01cSRoman Divacky     for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
2062f22ef01cSRoman Divacky       TreePatternNode *Dest = Pat->getChild(i);
2063f22ef01cSRoman Divacky       if (!Dest->isLeaf())
2064f22ef01cSRoman Divacky         I->error("implicitly defined value should be a register!");
2065f22ef01cSRoman Divacky 
2066f22ef01cSRoman Divacky       DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue());
2067f22ef01cSRoman Divacky       if (!Val || !Val->getDef()->isSubClassOf("Register"))
2068f22ef01cSRoman Divacky         I->error("implicitly defined value should be a register!");
2069f22ef01cSRoman Divacky       InstImpResults.push_back(Val->getDef());
2070f22ef01cSRoman Divacky     }
2071f22ef01cSRoman Divacky     return;
2072f22ef01cSRoman Divacky   }
2073f22ef01cSRoman Divacky 
2074f22ef01cSRoman Divacky   if (Pat->getOperator()->getName() != "set") {
2075f22ef01cSRoman Divacky     // If this is not a set, verify that the children nodes are not void typed,
2076f22ef01cSRoman Divacky     // and recurse.
2077f22ef01cSRoman Divacky     for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
2078f22ef01cSRoman Divacky       if (Pat->getChild(i)->getNumTypes() == 0)
2079f22ef01cSRoman Divacky         I->error("Cannot have void nodes inside of patterns!");
2080f22ef01cSRoman Divacky       FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults,
2081f22ef01cSRoman Divacky                                   InstImpResults);
2082f22ef01cSRoman Divacky     }
2083f22ef01cSRoman Divacky 
2084f22ef01cSRoman Divacky     // If this is a non-leaf node with no children, treat it basically as if
2085f22ef01cSRoman Divacky     // it were a leaf.  This handles nodes like (imm).
2086f22ef01cSRoman Divacky     bool isUse = HandleUse(I, Pat, InstInputs);
2087f22ef01cSRoman Divacky 
2088f22ef01cSRoman Divacky     if (!isUse && Pat->getTransformFn())
2089f22ef01cSRoman Divacky       I->error("Cannot specify a transform function for a non-input value!");
2090f22ef01cSRoman Divacky     return;
2091f22ef01cSRoman Divacky   }
2092f22ef01cSRoman Divacky 
2093f22ef01cSRoman Divacky   // Otherwise, this is a set, validate and collect instruction results.
2094f22ef01cSRoman Divacky   if (Pat->getNumChildren() == 0)
2095f22ef01cSRoman Divacky     I->error("set requires operands!");
2096f22ef01cSRoman Divacky 
2097f22ef01cSRoman Divacky   if (Pat->getTransformFn())
2098f22ef01cSRoman Divacky     I->error("Cannot specify a transform function on a set node!");
2099f22ef01cSRoman Divacky 
2100f22ef01cSRoman Divacky   // Check the set destinations.
2101f22ef01cSRoman Divacky   unsigned NumDests = Pat->getNumChildren()-1;
2102f22ef01cSRoman Divacky   for (unsigned i = 0; i != NumDests; ++i) {
2103f22ef01cSRoman Divacky     TreePatternNode *Dest = Pat->getChild(i);
2104f22ef01cSRoman Divacky     if (!Dest->isLeaf())
2105f22ef01cSRoman Divacky       I->error("set destination should be a register!");
2106f22ef01cSRoman Divacky 
2107f22ef01cSRoman Divacky     DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue());
2108f22ef01cSRoman Divacky     if (!Val)
2109f22ef01cSRoman Divacky       I->error("set destination should be a register!");
2110f22ef01cSRoman Divacky 
2111f22ef01cSRoman Divacky     if (Val->getDef()->isSubClassOf("RegisterClass") ||
2112f22ef01cSRoman Divacky         Val->getDef()->isSubClassOf("PointerLikeRegClass")) {
2113f22ef01cSRoman Divacky       if (Dest->getName().empty())
2114f22ef01cSRoman Divacky         I->error("set destination must have a name!");
2115f22ef01cSRoman Divacky       if (InstResults.count(Dest->getName()))
2116f22ef01cSRoman Divacky         I->error("cannot set '" + Dest->getName() +"' multiple times");
2117f22ef01cSRoman Divacky       InstResults[Dest->getName()] = Dest;
2118f22ef01cSRoman Divacky     } else if (Val->getDef()->isSubClassOf("Register")) {
2119f22ef01cSRoman Divacky       InstImpResults.push_back(Val->getDef());
2120f22ef01cSRoman Divacky     } else {
2121f22ef01cSRoman Divacky       I->error("set destination should be a register!");
2122f22ef01cSRoman Divacky     }
2123f22ef01cSRoman Divacky   }
2124f22ef01cSRoman Divacky 
2125f22ef01cSRoman Divacky   // Verify and collect info from the computation.
2126f22ef01cSRoman Divacky   FindPatternInputsAndOutputs(I, Pat->getChild(NumDests),
2127f22ef01cSRoman Divacky                               InstInputs, InstResults, InstImpResults);
2128f22ef01cSRoman Divacky }
2129f22ef01cSRoman Divacky 
2130f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
2131f22ef01cSRoman Divacky // Instruction Analysis
2132f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
2133f22ef01cSRoman Divacky 
2134f22ef01cSRoman Divacky class InstAnalyzer {
2135f22ef01cSRoman Divacky   const CodeGenDAGPatterns &CDP;
2136f22ef01cSRoman Divacky   bool &mayStore;
2137f22ef01cSRoman Divacky   bool &mayLoad;
2138f22ef01cSRoman Divacky   bool &HasSideEffects;
2139f22ef01cSRoman Divacky   bool &IsVariadic;
2140f22ef01cSRoman Divacky public:
2141f22ef01cSRoman Divacky   InstAnalyzer(const CodeGenDAGPatterns &cdp,
2142f22ef01cSRoman Divacky                bool &maystore, bool &mayload, bool &hse, bool &isv)
2143f22ef01cSRoman Divacky     : CDP(cdp), mayStore(maystore), mayLoad(mayload), HasSideEffects(hse),
2144f22ef01cSRoman Divacky       IsVariadic(isv) {
2145f22ef01cSRoman Divacky   }
2146f22ef01cSRoman Divacky 
2147f22ef01cSRoman Divacky   /// Analyze - Analyze the specified instruction, returning true if the
2148f22ef01cSRoman Divacky   /// instruction had a pattern.
2149f22ef01cSRoman Divacky   bool Analyze(Record *InstRecord) {
2150f22ef01cSRoman Divacky     const TreePattern *Pattern = CDP.getInstruction(InstRecord).getPattern();
2151f22ef01cSRoman Divacky     if (Pattern == 0) {
2152f22ef01cSRoman Divacky       HasSideEffects = 1;
2153f22ef01cSRoman Divacky       return false;  // No pattern.
2154f22ef01cSRoman Divacky     }
2155f22ef01cSRoman Divacky 
2156f22ef01cSRoman Divacky     // FIXME: Assume only the first tree is the pattern. The others are clobber
2157f22ef01cSRoman Divacky     // nodes.
2158f22ef01cSRoman Divacky     AnalyzeNode(Pattern->getTree(0));
2159f22ef01cSRoman Divacky     return true;
2160f22ef01cSRoman Divacky   }
2161f22ef01cSRoman Divacky 
2162f22ef01cSRoman Divacky private:
2163f22ef01cSRoman Divacky   void AnalyzeNode(const TreePatternNode *N) {
2164f22ef01cSRoman Divacky     if (N->isLeaf()) {
2165f22ef01cSRoman Divacky       if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
2166f22ef01cSRoman Divacky         Record *LeafRec = DI->getDef();
2167f22ef01cSRoman Divacky         // Handle ComplexPattern leaves.
2168f22ef01cSRoman Divacky         if (LeafRec->isSubClassOf("ComplexPattern")) {
2169f22ef01cSRoman Divacky           const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
2170f22ef01cSRoman Divacky           if (CP.hasProperty(SDNPMayStore)) mayStore = true;
2171f22ef01cSRoman Divacky           if (CP.hasProperty(SDNPMayLoad)) mayLoad = true;
2172f22ef01cSRoman Divacky           if (CP.hasProperty(SDNPSideEffect)) HasSideEffects = true;
2173f22ef01cSRoman Divacky         }
2174f22ef01cSRoman Divacky       }
2175f22ef01cSRoman Divacky       return;
2176f22ef01cSRoman Divacky     }
2177f22ef01cSRoman Divacky 
2178f22ef01cSRoman Divacky     // Analyze children.
2179f22ef01cSRoman Divacky     for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2180f22ef01cSRoman Divacky       AnalyzeNode(N->getChild(i));
2181f22ef01cSRoman Divacky 
2182f22ef01cSRoman Divacky     // Ignore set nodes, which are not SDNodes.
2183f22ef01cSRoman Divacky     if (N->getOperator()->getName() == "set")
2184f22ef01cSRoman Divacky       return;
2185f22ef01cSRoman Divacky 
2186f22ef01cSRoman Divacky     // Get information about the SDNode for the operator.
2187f22ef01cSRoman Divacky     const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator());
2188f22ef01cSRoman Divacky 
2189f22ef01cSRoman Divacky     // Notice properties of the node.
2190f22ef01cSRoman Divacky     if (OpInfo.hasProperty(SDNPMayStore)) mayStore = true;
2191f22ef01cSRoman Divacky     if (OpInfo.hasProperty(SDNPMayLoad)) mayLoad = true;
2192f22ef01cSRoman Divacky     if (OpInfo.hasProperty(SDNPSideEffect)) HasSideEffects = true;
2193f22ef01cSRoman Divacky     if (OpInfo.hasProperty(SDNPVariadic)) IsVariadic = true;
2194f22ef01cSRoman Divacky 
2195f22ef01cSRoman Divacky     if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
2196f22ef01cSRoman Divacky       // If this is an intrinsic, analyze it.
2197f22ef01cSRoman Divacky       if (IntInfo->ModRef >= CodeGenIntrinsic::ReadArgMem)
2198f22ef01cSRoman Divacky         mayLoad = true;// These may load memory.
2199f22ef01cSRoman Divacky 
2200e580952dSDimitry Andric       if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteArgMem)
2201f22ef01cSRoman Divacky         mayStore = true;// Intrinsics that can write to memory are 'mayStore'.
2202f22ef01cSRoman Divacky 
2203e580952dSDimitry Andric       if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem)
2204f22ef01cSRoman Divacky         // WriteMem intrinsics can have other strange effects.
2205f22ef01cSRoman Divacky         HasSideEffects = true;
2206f22ef01cSRoman Divacky     }
2207f22ef01cSRoman Divacky   }
2208f22ef01cSRoman Divacky 
2209f22ef01cSRoman Divacky };
2210f22ef01cSRoman Divacky 
2211f22ef01cSRoman Divacky static void InferFromPattern(const CodeGenInstruction &Inst,
2212f22ef01cSRoman Divacky                              bool &MayStore, bool &MayLoad,
2213f22ef01cSRoman Divacky                              bool &HasSideEffects, bool &IsVariadic,
2214f22ef01cSRoman Divacky                              const CodeGenDAGPatterns &CDP) {
2215f22ef01cSRoman Divacky   MayStore = MayLoad = HasSideEffects = IsVariadic = false;
2216f22ef01cSRoman Divacky 
2217f22ef01cSRoman Divacky   bool HadPattern =
2218f22ef01cSRoman Divacky     InstAnalyzer(CDP, MayStore, MayLoad, HasSideEffects, IsVariadic)
2219f22ef01cSRoman Divacky     .Analyze(Inst.TheDef);
2220f22ef01cSRoman Divacky 
2221f22ef01cSRoman Divacky   // InstAnalyzer only correctly analyzes mayStore/mayLoad so far.
2222f22ef01cSRoman Divacky   if (Inst.mayStore) {  // If the .td file explicitly sets mayStore, use it.
2223f22ef01cSRoman Divacky     // If we decided that this is a store from the pattern, then the .td file
2224f22ef01cSRoman Divacky     // entry is redundant.
2225f22ef01cSRoman Divacky     if (MayStore)
2226f22ef01cSRoman Divacky       fprintf(stderr,
2227f22ef01cSRoman Divacky               "Warning: mayStore flag explicitly set on instruction '%s'"
2228f22ef01cSRoman Divacky               " but flag already inferred from pattern.\n",
2229f22ef01cSRoman Divacky               Inst.TheDef->getName().c_str());
2230f22ef01cSRoman Divacky     MayStore = true;
2231f22ef01cSRoman Divacky   }
2232f22ef01cSRoman Divacky 
2233f22ef01cSRoman Divacky   if (Inst.mayLoad) {  // If the .td file explicitly sets mayLoad, use it.
2234f22ef01cSRoman Divacky     // If we decided that this is a load from the pattern, then the .td file
2235f22ef01cSRoman Divacky     // entry is redundant.
2236f22ef01cSRoman Divacky     if (MayLoad)
2237f22ef01cSRoman Divacky       fprintf(stderr,
2238f22ef01cSRoman Divacky               "Warning: mayLoad flag explicitly set on instruction '%s'"
2239f22ef01cSRoman Divacky               " but flag already inferred from pattern.\n",
2240f22ef01cSRoman Divacky               Inst.TheDef->getName().c_str());
2241f22ef01cSRoman Divacky     MayLoad = true;
2242f22ef01cSRoman Divacky   }
2243f22ef01cSRoman Divacky 
2244f22ef01cSRoman Divacky   if (Inst.neverHasSideEffects) {
2245f22ef01cSRoman Divacky     if (HadPattern)
2246f22ef01cSRoman Divacky       fprintf(stderr, "Warning: neverHasSideEffects set on instruction '%s' "
2247f22ef01cSRoman Divacky               "which already has a pattern\n", Inst.TheDef->getName().c_str());
2248f22ef01cSRoman Divacky     HasSideEffects = false;
2249f22ef01cSRoman Divacky   }
2250f22ef01cSRoman Divacky 
2251f22ef01cSRoman Divacky   if (Inst.hasSideEffects) {
2252f22ef01cSRoman Divacky     if (HasSideEffects)
2253f22ef01cSRoman Divacky       fprintf(stderr, "Warning: hasSideEffects set on instruction '%s' "
2254f22ef01cSRoman Divacky               "which already inferred this.\n", Inst.TheDef->getName().c_str());
2255f22ef01cSRoman Divacky     HasSideEffects = true;
2256f22ef01cSRoman Divacky   }
2257f22ef01cSRoman Divacky 
2258f22ef01cSRoman Divacky   if (Inst.isVariadic)
2259f22ef01cSRoman Divacky     IsVariadic = true;  // Can warn if we want.
2260f22ef01cSRoman Divacky }
2261f22ef01cSRoman Divacky 
2262f22ef01cSRoman Divacky /// ParseInstructions - Parse all of the instructions, inlining and resolving
2263f22ef01cSRoman Divacky /// any fragments involved.  This populates the Instructions list with fully
2264f22ef01cSRoman Divacky /// resolved instructions.
2265f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseInstructions() {
2266f22ef01cSRoman Divacky   std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction");
2267f22ef01cSRoman Divacky 
2268f22ef01cSRoman Divacky   for (unsigned i = 0, e = Instrs.size(); i != e; ++i) {
2269f22ef01cSRoman Divacky     ListInit *LI = 0;
2270f22ef01cSRoman Divacky 
2271f22ef01cSRoman Divacky     if (dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern")))
2272f22ef01cSRoman Divacky       LI = Instrs[i]->getValueAsListInit("Pattern");
2273f22ef01cSRoman Divacky 
2274f22ef01cSRoman Divacky     // If there is no pattern, only collect minimal information about the
2275f22ef01cSRoman Divacky     // instruction for its operand list.  We have to assume that there is one
2276f22ef01cSRoman Divacky     // result, as we have no detailed info.
2277f22ef01cSRoman Divacky     if (!LI || LI->getSize() == 0) {
2278f22ef01cSRoman Divacky       std::vector<Record*> Results;
2279f22ef01cSRoman Divacky       std::vector<Record*> Operands;
2280f22ef01cSRoman Divacky 
2281f22ef01cSRoman Divacky       CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]);
2282f22ef01cSRoman Divacky 
2283f22ef01cSRoman Divacky       if (InstInfo.OperandList.size() != 0) {
2284f22ef01cSRoman Divacky         if (InstInfo.NumDefs == 0) {
2285f22ef01cSRoman Divacky           // These produce no results
2286f22ef01cSRoman Divacky           for (unsigned j = 0, e = InstInfo.OperandList.size(); j < e; ++j)
2287f22ef01cSRoman Divacky             Operands.push_back(InstInfo.OperandList[j].Rec);
2288f22ef01cSRoman Divacky         } else {
2289f22ef01cSRoman Divacky           // Assume the first operand is the result.
2290f22ef01cSRoman Divacky           Results.push_back(InstInfo.OperandList[0].Rec);
2291f22ef01cSRoman Divacky 
2292f22ef01cSRoman Divacky           // The rest are inputs.
2293f22ef01cSRoman Divacky           for (unsigned j = 1, e = InstInfo.OperandList.size(); j < e; ++j)
2294f22ef01cSRoman Divacky             Operands.push_back(InstInfo.OperandList[j].Rec);
2295f22ef01cSRoman Divacky         }
2296f22ef01cSRoman Divacky       }
2297f22ef01cSRoman Divacky 
2298f22ef01cSRoman Divacky       // Create and insert the instruction.
2299f22ef01cSRoman Divacky       std::vector<Record*> ImpResults;
2300f22ef01cSRoman Divacky       Instructions.insert(std::make_pair(Instrs[i],
2301f22ef01cSRoman Divacky                           DAGInstruction(0, Results, Operands, ImpResults)));
2302f22ef01cSRoman Divacky       continue;  // no pattern.
2303f22ef01cSRoman Divacky     }
2304f22ef01cSRoman Divacky 
2305f22ef01cSRoman Divacky     // Parse the instruction.
2306f22ef01cSRoman Divacky     TreePattern *I = new TreePattern(Instrs[i], LI, true, *this);
2307f22ef01cSRoman Divacky     // Inline pattern fragments into it.
2308f22ef01cSRoman Divacky     I->InlinePatternFragments();
2309f22ef01cSRoman Divacky 
2310f22ef01cSRoman Divacky     // Infer as many types as possible.  If we cannot infer all of them, we can
2311f22ef01cSRoman Divacky     // never do anything with this instruction pattern: report it to the user.
2312f22ef01cSRoman Divacky     if (!I->InferAllTypes())
2313f22ef01cSRoman Divacky       I->error("Could not infer all types in pattern!");
2314f22ef01cSRoman Divacky 
2315f22ef01cSRoman Divacky     // InstInputs - Keep track of all of the inputs of the instruction, along
2316f22ef01cSRoman Divacky     // with the record they are declared as.
2317f22ef01cSRoman Divacky     std::map<std::string, TreePatternNode*> InstInputs;
2318f22ef01cSRoman Divacky 
2319f22ef01cSRoman Divacky     // InstResults - Keep track of all the virtual registers that are 'set'
2320f22ef01cSRoman Divacky     // in the instruction, including what reg class they are.
2321f22ef01cSRoman Divacky     std::map<std::string, TreePatternNode*> InstResults;
2322f22ef01cSRoman Divacky 
2323f22ef01cSRoman Divacky     std::vector<Record*> InstImpResults;
2324f22ef01cSRoman Divacky 
2325f22ef01cSRoman Divacky     // Verify that the top-level forms in the instruction are of void type, and
2326f22ef01cSRoman Divacky     // fill in the InstResults map.
2327f22ef01cSRoman Divacky     for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) {
2328f22ef01cSRoman Divacky       TreePatternNode *Pat = I->getTree(j);
2329f22ef01cSRoman Divacky       if (Pat->getNumTypes() != 0)
2330f22ef01cSRoman Divacky         I->error("Top-level forms in instruction pattern should have"
2331f22ef01cSRoman Divacky                  " void types");
2332f22ef01cSRoman Divacky 
2333f22ef01cSRoman Divacky       // Find inputs and outputs, and verify the structure of the uses/defs.
2334f22ef01cSRoman Divacky       FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults,
2335f22ef01cSRoman Divacky                                   InstImpResults);
2336f22ef01cSRoman Divacky     }
2337f22ef01cSRoman Divacky 
2338f22ef01cSRoman Divacky     // Now that we have inputs and outputs of the pattern, inspect the operands
2339f22ef01cSRoman Divacky     // list for the instruction.  This determines the order that operands are
2340f22ef01cSRoman Divacky     // added to the machine instruction the node corresponds to.
2341f22ef01cSRoman Divacky     unsigned NumResults = InstResults.size();
2342f22ef01cSRoman Divacky 
2343f22ef01cSRoman Divacky     // Parse the operands list from the (ops) list, validating it.
2344f22ef01cSRoman Divacky     assert(I->getArgList().empty() && "Args list should still be empty here!");
2345f22ef01cSRoman Divacky     CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]);
2346f22ef01cSRoman Divacky 
2347f22ef01cSRoman Divacky     // Check that all of the results occur first in the list.
2348f22ef01cSRoman Divacky     std::vector<Record*> Results;
2349f22ef01cSRoman Divacky     TreePatternNode *Res0Node = 0;
2350f22ef01cSRoman Divacky     for (unsigned i = 0; i != NumResults; ++i) {
2351f22ef01cSRoman Divacky       if (i == CGI.OperandList.size())
2352f22ef01cSRoman Divacky         I->error("'" + InstResults.begin()->first +
2353f22ef01cSRoman Divacky                  "' set but does not appear in operand list!");
2354f22ef01cSRoman Divacky       const std::string &OpName = CGI.OperandList[i].Name;
2355f22ef01cSRoman Divacky 
2356f22ef01cSRoman Divacky       // Check that it exists in InstResults.
2357f22ef01cSRoman Divacky       TreePatternNode *RNode = InstResults[OpName];
2358f22ef01cSRoman Divacky       if (RNode == 0)
2359f22ef01cSRoman Divacky         I->error("Operand $" + OpName + " does not exist in operand list!");
2360f22ef01cSRoman Divacky 
2361f22ef01cSRoman Divacky       if (i == 0)
2362f22ef01cSRoman Divacky         Res0Node = RNode;
2363f22ef01cSRoman Divacky       Record *R = dynamic_cast<DefInit*>(RNode->getLeafValue())->getDef();
2364f22ef01cSRoman Divacky       if (R == 0)
2365f22ef01cSRoman Divacky         I->error("Operand $" + OpName + " should be a set destination: all "
2366f22ef01cSRoman Divacky                  "outputs must occur before inputs in operand list!");
2367f22ef01cSRoman Divacky 
2368f22ef01cSRoman Divacky       if (CGI.OperandList[i].Rec != R)
2369f22ef01cSRoman Divacky         I->error("Operand $" + OpName + " class mismatch!");
2370f22ef01cSRoman Divacky 
2371f22ef01cSRoman Divacky       // Remember the return type.
2372f22ef01cSRoman Divacky       Results.push_back(CGI.OperandList[i].Rec);
2373f22ef01cSRoman Divacky 
2374f22ef01cSRoman Divacky       // Okay, this one checks out.
2375f22ef01cSRoman Divacky       InstResults.erase(OpName);
2376f22ef01cSRoman Divacky     }
2377f22ef01cSRoman Divacky 
2378f22ef01cSRoman Divacky     // Loop over the inputs next.  Make a copy of InstInputs so we can destroy
2379f22ef01cSRoman Divacky     // the copy while we're checking the inputs.
2380f22ef01cSRoman Divacky     std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs);
2381f22ef01cSRoman Divacky 
2382f22ef01cSRoman Divacky     std::vector<TreePatternNode*> ResultNodeOperands;
2383f22ef01cSRoman Divacky     std::vector<Record*> Operands;
2384f22ef01cSRoman Divacky     for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) {
2385f22ef01cSRoman Divacky       CodeGenInstruction::OperandInfo &Op = CGI.OperandList[i];
2386f22ef01cSRoman Divacky       const std::string &OpName = Op.Name;
2387f22ef01cSRoman Divacky       if (OpName.empty())
2388f22ef01cSRoman Divacky         I->error("Operand #" + utostr(i) + " in operands list has no name!");
2389f22ef01cSRoman Divacky 
2390f22ef01cSRoman Divacky       if (!InstInputsCheck.count(OpName)) {
2391f22ef01cSRoman Divacky         // If this is an predicate operand or optional def operand with an
2392f22ef01cSRoman Divacky         // DefaultOps set filled in, we can ignore this.  When we codegen it,
2393f22ef01cSRoman Divacky         // we will do so as always executed.
2394f22ef01cSRoman Divacky         if (Op.Rec->isSubClassOf("PredicateOperand") ||
2395f22ef01cSRoman Divacky             Op.Rec->isSubClassOf("OptionalDefOperand")) {
2396f22ef01cSRoman Divacky           // Does it have a non-empty DefaultOps field?  If so, ignore this
2397f22ef01cSRoman Divacky           // operand.
2398f22ef01cSRoman Divacky           if (!getDefaultOperand(Op.Rec).DefaultOps.empty())
2399f22ef01cSRoman Divacky             continue;
2400f22ef01cSRoman Divacky         }
2401f22ef01cSRoman Divacky         I->error("Operand $" + OpName +
2402f22ef01cSRoman Divacky                  " does not appear in the instruction pattern");
2403f22ef01cSRoman Divacky       }
2404f22ef01cSRoman Divacky       TreePatternNode *InVal = InstInputsCheck[OpName];
2405f22ef01cSRoman Divacky       InstInputsCheck.erase(OpName);   // It occurred, remove from map.
2406f22ef01cSRoman Divacky 
2407f22ef01cSRoman Divacky       if (InVal->isLeaf() &&
2408f22ef01cSRoman Divacky           dynamic_cast<DefInit*>(InVal->getLeafValue())) {
2409f22ef01cSRoman Divacky         Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef();
2410f22ef01cSRoman Divacky         if (Op.Rec != InRec && !InRec->isSubClassOf("ComplexPattern"))
2411f22ef01cSRoman Divacky           I->error("Operand $" + OpName + "'s register class disagrees"
2412f22ef01cSRoman Divacky                    " between the operand and pattern");
2413f22ef01cSRoman Divacky       }
2414f22ef01cSRoman Divacky       Operands.push_back(Op.Rec);
2415f22ef01cSRoman Divacky 
2416f22ef01cSRoman Divacky       // Construct the result for the dest-pattern operand list.
2417f22ef01cSRoman Divacky       TreePatternNode *OpNode = InVal->clone();
2418f22ef01cSRoman Divacky 
2419f22ef01cSRoman Divacky       // No predicate is useful on the result.
2420f22ef01cSRoman Divacky       OpNode->clearPredicateFns();
2421f22ef01cSRoman Divacky 
2422f22ef01cSRoman Divacky       // Promote the xform function to be an explicit node if set.
2423f22ef01cSRoman Divacky       if (Record *Xform = OpNode->getTransformFn()) {
2424f22ef01cSRoman Divacky         OpNode->setTransformFn(0);
2425f22ef01cSRoman Divacky         std::vector<TreePatternNode*> Children;
2426f22ef01cSRoman Divacky         Children.push_back(OpNode);
2427f22ef01cSRoman Divacky         OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes());
2428f22ef01cSRoman Divacky       }
2429f22ef01cSRoman Divacky 
2430f22ef01cSRoman Divacky       ResultNodeOperands.push_back(OpNode);
2431f22ef01cSRoman Divacky     }
2432f22ef01cSRoman Divacky 
2433f22ef01cSRoman Divacky     if (!InstInputsCheck.empty())
2434f22ef01cSRoman Divacky       I->error("Input operand $" + InstInputsCheck.begin()->first +
2435f22ef01cSRoman Divacky                " occurs in pattern but not in operands list!");
2436f22ef01cSRoman Divacky 
2437f22ef01cSRoman Divacky     TreePatternNode *ResultPattern =
2438f22ef01cSRoman Divacky       new TreePatternNode(I->getRecord(), ResultNodeOperands,
2439f22ef01cSRoman Divacky                           GetNumNodeResults(I->getRecord(), *this));
2440f22ef01cSRoman Divacky     // Copy fully inferred output node type to instruction result pattern.
2441f22ef01cSRoman Divacky     for (unsigned i = 0; i != NumResults; ++i)
2442f22ef01cSRoman Divacky       ResultPattern->setType(i, Res0Node->getExtType(i));
2443f22ef01cSRoman Divacky 
2444f22ef01cSRoman Divacky     // Create and insert the instruction.
2445f22ef01cSRoman Divacky     // FIXME: InstImpResults should not be part of DAGInstruction.
2446f22ef01cSRoman Divacky     DAGInstruction TheInst(I, Results, Operands, InstImpResults);
2447f22ef01cSRoman Divacky     Instructions.insert(std::make_pair(I->getRecord(), TheInst));
2448f22ef01cSRoman Divacky 
2449f22ef01cSRoman Divacky     // Use a temporary tree pattern to infer all types and make sure that the
2450f22ef01cSRoman Divacky     // constructed result is correct.  This depends on the instruction already
2451f22ef01cSRoman Divacky     // being inserted into the Instructions map.
2452f22ef01cSRoman Divacky     TreePattern Temp(I->getRecord(), ResultPattern, false, *this);
2453f22ef01cSRoman Divacky     Temp.InferAllTypes(&I->getNamedNodesMap());
2454f22ef01cSRoman Divacky 
2455f22ef01cSRoman Divacky     DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second;
2456f22ef01cSRoman Divacky     TheInsertedInst.setResultPattern(Temp.getOnlyTree());
2457f22ef01cSRoman Divacky 
2458f22ef01cSRoman Divacky     DEBUG(I->dump());
2459f22ef01cSRoman Divacky   }
2460f22ef01cSRoman Divacky 
2461f22ef01cSRoman Divacky   // If we can, convert the instructions to be patterns that are matched!
2462f22ef01cSRoman Divacky   for (std::map<Record*, DAGInstruction, RecordPtrCmp>::iterator II =
2463f22ef01cSRoman Divacky         Instructions.begin(),
2464f22ef01cSRoman Divacky        E = Instructions.end(); II != E; ++II) {
2465f22ef01cSRoman Divacky     DAGInstruction &TheInst = II->second;
2466f22ef01cSRoman Divacky     const TreePattern *I = TheInst.getPattern();
2467f22ef01cSRoman Divacky     if (I == 0) continue;  // No pattern.
2468f22ef01cSRoman Divacky 
2469f22ef01cSRoman Divacky     // FIXME: Assume only the first tree is the pattern. The others are clobber
2470f22ef01cSRoman Divacky     // nodes.
2471f22ef01cSRoman Divacky     TreePatternNode *Pattern = I->getTree(0);
2472f22ef01cSRoman Divacky     TreePatternNode *SrcPattern;
2473f22ef01cSRoman Divacky     if (Pattern->getOperator()->getName() == "set") {
2474f22ef01cSRoman Divacky       SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone();
2475f22ef01cSRoman Divacky     } else{
2476f22ef01cSRoman Divacky       // Not a set (store or something?)
2477f22ef01cSRoman Divacky       SrcPattern = Pattern;
2478f22ef01cSRoman Divacky     }
2479f22ef01cSRoman Divacky 
2480f22ef01cSRoman Divacky     Record *Instr = II->first;
2481f22ef01cSRoman Divacky     AddPatternToMatch(I,
2482f22ef01cSRoman Divacky                       PatternToMatch(Instr->getValueAsListInit("Predicates"),
2483f22ef01cSRoman Divacky                                      SrcPattern,
2484f22ef01cSRoman Divacky                                      TheInst.getResultPattern(),
2485f22ef01cSRoman Divacky                                      TheInst.getImpResults(),
2486f22ef01cSRoman Divacky                                      Instr->getValueAsInt("AddedComplexity"),
2487f22ef01cSRoman Divacky                                      Instr->getID()));
2488f22ef01cSRoman Divacky   }
2489f22ef01cSRoman Divacky }
2490f22ef01cSRoman Divacky 
2491f22ef01cSRoman Divacky 
2492f22ef01cSRoman Divacky typedef std::pair<const TreePatternNode*, unsigned> NameRecord;
2493f22ef01cSRoman Divacky 
2494f22ef01cSRoman Divacky static void FindNames(const TreePatternNode *P,
2495f22ef01cSRoman Divacky                       std::map<std::string, NameRecord> &Names,
2496f22ef01cSRoman Divacky                       const TreePattern *PatternTop) {
2497f22ef01cSRoman Divacky   if (!P->getName().empty()) {
2498f22ef01cSRoman Divacky     NameRecord &Rec = Names[P->getName()];
2499f22ef01cSRoman Divacky     // If this is the first instance of the name, remember the node.
2500f22ef01cSRoman Divacky     if (Rec.second++ == 0)
2501f22ef01cSRoman Divacky       Rec.first = P;
2502f22ef01cSRoman Divacky     else if (Rec.first->getExtTypes() != P->getExtTypes())
2503f22ef01cSRoman Divacky       PatternTop->error("repetition of value: $" + P->getName() +
2504f22ef01cSRoman Divacky                         " where different uses have different types!");
2505f22ef01cSRoman Divacky   }
2506f22ef01cSRoman Divacky 
2507f22ef01cSRoman Divacky   if (!P->isLeaf()) {
2508f22ef01cSRoman Divacky     for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
2509f22ef01cSRoman Divacky       FindNames(P->getChild(i), Names, PatternTop);
2510f22ef01cSRoman Divacky   }
2511f22ef01cSRoman Divacky }
2512f22ef01cSRoman Divacky 
2513f22ef01cSRoman Divacky void CodeGenDAGPatterns::AddPatternToMatch(const TreePattern *Pattern,
2514f22ef01cSRoman Divacky                                            const PatternToMatch &PTM) {
2515f22ef01cSRoman Divacky   // Do some sanity checking on the pattern we're about to match.
2516f22ef01cSRoman Divacky   std::string Reason;
2517f22ef01cSRoman Divacky   if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this))
2518f22ef01cSRoman Divacky     Pattern->error("Pattern can never match: " + Reason);
2519f22ef01cSRoman Divacky 
2520f22ef01cSRoman Divacky   // If the source pattern's root is a complex pattern, that complex pattern
2521f22ef01cSRoman Divacky   // must specify the nodes it can potentially match.
2522f22ef01cSRoman Divacky   if (const ComplexPattern *CP =
2523f22ef01cSRoman Divacky         PTM.getSrcPattern()->getComplexPatternInfo(*this))
2524f22ef01cSRoman Divacky     if (CP->getRootNodes().empty())
2525f22ef01cSRoman Divacky       Pattern->error("ComplexPattern at root must specify list of opcodes it"
2526f22ef01cSRoman Divacky                      " could match");
2527f22ef01cSRoman Divacky 
2528f22ef01cSRoman Divacky 
2529f22ef01cSRoman Divacky   // Find all of the named values in the input and output, ensure they have the
2530f22ef01cSRoman Divacky   // same type.
2531f22ef01cSRoman Divacky   std::map<std::string, NameRecord> SrcNames, DstNames;
2532f22ef01cSRoman Divacky   FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
2533f22ef01cSRoman Divacky   FindNames(PTM.getDstPattern(), DstNames, Pattern);
2534f22ef01cSRoman Divacky 
2535f22ef01cSRoman Divacky   // Scan all of the named values in the destination pattern, rejecting them if
2536f22ef01cSRoman Divacky   // they don't exist in the input pattern.
2537f22ef01cSRoman Divacky   for (std::map<std::string, NameRecord>::iterator
2538f22ef01cSRoman Divacky        I = DstNames.begin(), E = DstNames.end(); I != E; ++I) {
2539f22ef01cSRoman Divacky     if (SrcNames[I->first].first == 0)
2540f22ef01cSRoman Divacky       Pattern->error("Pattern has input without matching name in output: $" +
2541f22ef01cSRoman Divacky                      I->first);
2542f22ef01cSRoman Divacky   }
2543f22ef01cSRoman Divacky 
2544f22ef01cSRoman Divacky   // Scan all of the named values in the source pattern, rejecting them if the
2545f22ef01cSRoman Divacky   // name isn't used in the dest, and isn't used to tie two values together.
2546f22ef01cSRoman Divacky   for (std::map<std::string, NameRecord>::iterator
2547f22ef01cSRoman Divacky        I = SrcNames.begin(), E = SrcNames.end(); I != E; ++I)
2548f22ef01cSRoman Divacky     if (DstNames[I->first].first == 0 && SrcNames[I->first].second == 1)
2549f22ef01cSRoman Divacky       Pattern->error("Pattern has dead named input: $" + I->first);
2550f22ef01cSRoman Divacky 
2551f22ef01cSRoman Divacky   PatternsToMatch.push_back(PTM);
2552f22ef01cSRoman Divacky }
2553f22ef01cSRoman Divacky 
2554f22ef01cSRoman Divacky 
2555f22ef01cSRoman Divacky 
2556f22ef01cSRoman Divacky void CodeGenDAGPatterns::InferInstructionFlags() {
2557f22ef01cSRoman Divacky   const std::vector<const CodeGenInstruction*> &Instructions =
2558f22ef01cSRoman Divacky     Target.getInstructionsByEnumValue();
2559f22ef01cSRoman Divacky   for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
2560f22ef01cSRoman Divacky     CodeGenInstruction &InstInfo =
2561f22ef01cSRoman Divacky       const_cast<CodeGenInstruction &>(*Instructions[i]);
2562f22ef01cSRoman Divacky     // Determine properties of the instruction from its pattern.
2563f22ef01cSRoman Divacky     bool MayStore, MayLoad, HasSideEffects, IsVariadic;
2564f22ef01cSRoman Divacky     InferFromPattern(InstInfo, MayStore, MayLoad, HasSideEffects, IsVariadic,
2565f22ef01cSRoman Divacky                      *this);
2566f22ef01cSRoman Divacky     InstInfo.mayStore = MayStore;
2567f22ef01cSRoman Divacky     InstInfo.mayLoad = MayLoad;
2568f22ef01cSRoman Divacky     InstInfo.hasSideEffects = HasSideEffects;
2569f22ef01cSRoman Divacky     InstInfo.isVariadic = IsVariadic;
2570f22ef01cSRoman Divacky   }
2571f22ef01cSRoman Divacky }
2572f22ef01cSRoman Divacky 
2573f22ef01cSRoman Divacky /// Given a pattern result with an unresolved type, see if we can find one
2574f22ef01cSRoman Divacky /// instruction with an unresolved result type.  Force this result type to an
2575f22ef01cSRoman Divacky /// arbitrary element if it's possible types to converge results.
2576f22ef01cSRoman Divacky static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) {
2577f22ef01cSRoman Divacky   if (N->isLeaf())
2578f22ef01cSRoman Divacky     return false;
2579f22ef01cSRoman Divacky 
2580f22ef01cSRoman Divacky   // Analyze children.
2581f22ef01cSRoman Divacky   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2582f22ef01cSRoman Divacky     if (ForceArbitraryInstResultType(N->getChild(i), TP))
2583f22ef01cSRoman Divacky       return true;
2584f22ef01cSRoman Divacky 
2585f22ef01cSRoman Divacky   if (!N->getOperator()->isSubClassOf("Instruction"))
2586f22ef01cSRoman Divacky     return false;
2587f22ef01cSRoman Divacky 
2588f22ef01cSRoman Divacky   // If this type is already concrete or completely unknown we can't do
2589f22ef01cSRoman Divacky   // anything.
2590f22ef01cSRoman Divacky   for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) {
2591f22ef01cSRoman Divacky     if (N->getExtType(i).isCompletelyUnknown() || N->getExtType(i).isConcrete())
2592f22ef01cSRoman Divacky       continue;
2593f22ef01cSRoman Divacky 
2594f22ef01cSRoman Divacky     // Otherwise, force its type to the first possibility (an arbitrary choice).
2595f22ef01cSRoman Divacky     if (N->getExtType(i).MergeInTypeInfo(N->getExtType(i).getTypeList()[0], TP))
2596f22ef01cSRoman Divacky       return true;
2597f22ef01cSRoman Divacky   }
2598f22ef01cSRoman Divacky 
2599f22ef01cSRoman Divacky   return false;
2600f22ef01cSRoman Divacky }
2601f22ef01cSRoman Divacky 
2602f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParsePatterns() {
2603f22ef01cSRoman Divacky   std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
2604f22ef01cSRoman Divacky 
2605f22ef01cSRoman Divacky   for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
2606f22ef01cSRoman Divacky     Record *CurPattern = Patterns[i];
2607f22ef01cSRoman Divacky     DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
2608f22ef01cSRoman Divacky     TreePattern *Pattern = new TreePattern(CurPattern, Tree, true, *this);
2609f22ef01cSRoman Divacky 
2610f22ef01cSRoman Divacky     // Inline pattern fragments into it.
2611f22ef01cSRoman Divacky     Pattern->InlinePatternFragments();
2612f22ef01cSRoman Divacky 
2613f22ef01cSRoman Divacky     ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs");
2614f22ef01cSRoman Divacky     if (LI->getSize() == 0) continue;  // no pattern.
2615f22ef01cSRoman Divacky 
2616f22ef01cSRoman Divacky     // Parse the instruction.
2617f22ef01cSRoman Divacky     TreePattern *Result = new TreePattern(CurPattern, LI, false, *this);
2618f22ef01cSRoman Divacky 
2619f22ef01cSRoman Divacky     // Inline pattern fragments into it.
2620f22ef01cSRoman Divacky     Result->InlinePatternFragments();
2621f22ef01cSRoman Divacky 
2622f22ef01cSRoman Divacky     if (Result->getNumTrees() != 1)
2623f22ef01cSRoman Divacky       Result->error("Cannot handle instructions producing instructions "
2624f22ef01cSRoman Divacky                     "with temporaries yet!");
2625f22ef01cSRoman Divacky 
2626f22ef01cSRoman Divacky     bool IterateInference;
2627f22ef01cSRoman Divacky     bool InferredAllPatternTypes, InferredAllResultTypes;
2628f22ef01cSRoman Divacky     do {
2629f22ef01cSRoman Divacky       // Infer as many types as possible.  If we cannot infer all of them, we
2630f22ef01cSRoman Divacky       // can never do anything with this pattern: report it to the user.
2631f22ef01cSRoman Divacky       InferredAllPatternTypes =
2632f22ef01cSRoman Divacky         Pattern->InferAllTypes(&Pattern->getNamedNodesMap());
2633f22ef01cSRoman Divacky 
2634f22ef01cSRoman Divacky       // Infer as many types as possible.  If we cannot infer all of them, we
2635f22ef01cSRoman Divacky       // can never do anything with this pattern: report it to the user.
2636f22ef01cSRoman Divacky       InferredAllResultTypes =
2637f22ef01cSRoman Divacky         Result->InferAllTypes(&Pattern->getNamedNodesMap());
2638f22ef01cSRoman Divacky 
2639f22ef01cSRoman Divacky       IterateInference = false;
2640f22ef01cSRoman Divacky 
2641f22ef01cSRoman Divacky       // Apply the type of the result to the source pattern.  This helps us
2642f22ef01cSRoman Divacky       // resolve cases where the input type is known to be a pointer type (which
2643f22ef01cSRoman Divacky       // is considered resolved), but the result knows it needs to be 32- or
2644f22ef01cSRoman Divacky       // 64-bits.  Infer the other way for good measure.
2645f22ef01cSRoman Divacky       for (unsigned i = 0, e = std::min(Result->getTree(0)->getNumTypes(),
2646f22ef01cSRoman Divacky                                         Pattern->getTree(0)->getNumTypes());
2647f22ef01cSRoman Divacky            i != e; ++i) {
2648f22ef01cSRoman Divacky         IterateInference = Pattern->getTree(0)->
2649f22ef01cSRoman Divacky           UpdateNodeType(i, Result->getTree(0)->getExtType(i), *Result);
2650f22ef01cSRoman Divacky         IterateInference |= Result->getTree(0)->
2651f22ef01cSRoman Divacky           UpdateNodeType(i, Pattern->getTree(0)->getExtType(i), *Result);
2652f22ef01cSRoman Divacky       }
2653f22ef01cSRoman Divacky 
2654f22ef01cSRoman Divacky       // If our iteration has converged and the input pattern's types are fully
2655f22ef01cSRoman Divacky       // resolved but the result pattern is not fully resolved, we may have a
2656f22ef01cSRoman Divacky       // situation where we have two instructions in the result pattern and
2657f22ef01cSRoman Divacky       // the instructions require a common register class, but don't care about
2658f22ef01cSRoman Divacky       // what actual MVT is used.  This is actually a bug in our modelling:
2659f22ef01cSRoman Divacky       // output patterns should have register classes, not MVTs.
2660f22ef01cSRoman Divacky       //
2661f22ef01cSRoman Divacky       // In any case, to handle this, we just go through and disambiguate some
2662f22ef01cSRoman Divacky       // arbitrary types to the result pattern's nodes.
2663f22ef01cSRoman Divacky       if (!IterateInference && InferredAllPatternTypes &&
2664f22ef01cSRoman Divacky           !InferredAllResultTypes)
2665f22ef01cSRoman Divacky         IterateInference = ForceArbitraryInstResultType(Result->getTree(0),
2666f22ef01cSRoman Divacky                                                         *Result);
2667f22ef01cSRoman Divacky     } while (IterateInference);
2668f22ef01cSRoman Divacky 
2669f22ef01cSRoman Divacky     // Verify that we inferred enough types that we can do something with the
2670f22ef01cSRoman Divacky     // pattern and result.  If these fire the user has to add type casts.
2671f22ef01cSRoman Divacky     if (!InferredAllPatternTypes)
2672f22ef01cSRoman Divacky       Pattern->error("Could not infer all types in pattern!");
2673f22ef01cSRoman Divacky     if (!InferredAllResultTypes) {
2674f22ef01cSRoman Divacky       Pattern->dump();
2675f22ef01cSRoman Divacky       Result->error("Could not infer all types in pattern result!");
2676f22ef01cSRoman Divacky     }
2677f22ef01cSRoman Divacky 
2678f22ef01cSRoman Divacky     // Validate that the input pattern is correct.
2679f22ef01cSRoman Divacky     std::map<std::string, TreePatternNode*> InstInputs;
2680f22ef01cSRoman Divacky     std::map<std::string, TreePatternNode*> InstResults;
2681f22ef01cSRoman Divacky     std::vector<Record*> InstImpResults;
2682f22ef01cSRoman Divacky     for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j)
2683f22ef01cSRoman Divacky       FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j),
2684f22ef01cSRoman Divacky                                   InstInputs, InstResults,
2685f22ef01cSRoman Divacky                                   InstImpResults);
2686f22ef01cSRoman Divacky 
2687f22ef01cSRoman Divacky     // Promote the xform function to be an explicit node if set.
2688f22ef01cSRoman Divacky     TreePatternNode *DstPattern = Result->getOnlyTree();
2689f22ef01cSRoman Divacky     std::vector<TreePatternNode*> ResultNodeOperands;
2690f22ef01cSRoman Divacky     for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) {
2691f22ef01cSRoman Divacky       TreePatternNode *OpNode = DstPattern->getChild(ii);
2692f22ef01cSRoman Divacky       if (Record *Xform = OpNode->getTransformFn()) {
2693f22ef01cSRoman Divacky         OpNode->setTransformFn(0);
2694f22ef01cSRoman Divacky         std::vector<TreePatternNode*> Children;
2695f22ef01cSRoman Divacky         Children.push_back(OpNode);
2696f22ef01cSRoman Divacky         OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes());
2697f22ef01cSRoman Divacky       }
2698f22ef01cSRoman Divacky       ResultNodeOperands.push_back(OpNode);
2699f22ef01cSRoman Divacky     }
2700f22ef01cSRoman Divacky     DstPattern = Result->getOnlyTree();
2701f22ef01cSRoman Divacky     if (!DstPattern->isLeaf())
2702f22ef01cSRoman Divacky       DstPattern = new TreePatternNode(DstPattern->getOperator(),
2703f22ef01cSRoman Divacky                                        ResultNodeOperands,
2704f22ef01cSRoman Divacky                                        DstPattern->getNumTypes());
2705f22ef01cSRoman Divacky 
2706f22ef01cSRoman Divacky     for (unsigned i = 0, e = Result->getOnlyTree()->getNumTypes(); i != e; ++i)
2707f22ef01cSRoman Divacky       DstPattern->setType(i, Result->getOnlyTree()->getExtType(i));
2708f22ef01cSRoman Divacky 
2709f22ef01cSRoman Divacky     TreePattern Temp(Result->getRecord(), DstPattern, false, *this);
2710f22ef01cSRoman Divacky     Temp.InferAllTypes();
2711f22ef01cSRoman Divacky 
2712f22ef01cSRoman Divacky 
2713f22ef01cSRoman Divacky     AddPatternToMatch(Pattern,
2714f22ef01cSRoman Divacky                     PatternToMatch(CurPattern->getValueAsListInit("Predicates"),
2715f22ef01cSRoman Divacky                                    Pattern->getTree(0),
2716f22ef01cSRoman Divacky                                    Temp.getOnlyTree(), InstImpResults,
2717f22ef01cSRoman Divacky                                    CurPattern->getValueAsInt("AddedComplexity"),
2718f22ef01cSRoman Divacky                                    CurPattern->getID()));
2719f22ef01cSRoman Divacky   }
2720f22ef01cSRoman Divacky }
2721f22ef01cSRoman Divacky 
2722f22ef01cSRoman Divacky /// CombineChildVariants - Given a bunch of permutations of each child of the
2723f22ef01cSRoman Divacky /// 'operator' node, put them together in all possible ways.
2724f22ef01cSRoman Divacky static void CombineChildVariants(TreePatternNode *Orig,
2725f22ef01cSRoman Divacky                const std::vector<std::vector<TreePatternNode*> > &ChildVariants,
2726f22ef01cSRoman Divacky                                  std::vector<TreePatternNode*> &OutVariants,
2727f22ef01cSRoman Divacky                                  CodeGenDAGPatterns &CDP,
2728f22ef01cSRoman Divacky                                  const MultipleUseVarSet &DepVars) {
2729f22ef01cSRoman Divacky   // Make sure that each operand has at least one variant to choose from.
2730f22ef01cSRoman Divacky   for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
2731f22ef01cSRoman Divacky     if (ChildVariants[i].empty())
2732f22ef01cSRoman Divacky       return;
2733f22ef01cSRoman Divacky 
2734f22ef01cSRoman Divacky   // The end result is an all-pairs construction of the resultant pattern.
2735f22ef01cSRoman Divacky   std::vector<unsigned> Idxs;
2736f22ef01cSRoman Divacky   Idxs.resize(ChildVariants.size());
2737f22ef01cSRoman Divacky   bool NotDone;
2738f22ef01cSRoman Divacky   do {
2739f22ef01cSRoman Divacky #ifndef NDEBUG
2740f22ef01cSRoman Divacky     DEBUG(if (!Idxs.empty()) {
2741f22ef01cSRoman Divacky             errs() << Orig->getOperator()->getName() << ": Idxs = [ ";
2742f22ef01cSRoman Divacky               for (unsigned i = 0; i < Idxs.size(); ++i) {
2743f22ef01cSRoman Divacky                 errs() << Idxs[i] << " ";
2744f22ef01cSRoman Divacky             }
2745f22ef01cSRoman Divacky             errs() << "]\n";
2746f22ef01cSRoman Divacky           });
2747f22ef01cSRoman Divacky #endif
2748f22ef01cSRoman Divacky     // Create the variant and add it to the output list.
2749f22ef01cSRoman Divacky     std::vector<TreePatternNode*> NewChildren;
2750f22ef01cSRoman Divacky     for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
2751f22ef01cSRoman Divacky       NewChildren.push_back(ChildVariants[i][Idxs[i]]);
2752f22ef01cSRoman Divacky     TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren,
2753f22ef01cSRoman Divacky                                              Orig->getNumTypes());
2754f22ef01cSRoman Divacky 
2755f22ef01cSRoman Divacky     // Copy over properties.
2756f22ef01cSRoman Divacky     R->setName(Orig->getName());
2757f22ef01cSRoman Divacky     R->setPredicateFns(Orig->getPredicateFns());
2758f22ef01cSRoman Divacky     R->setTransformFn(Orig->getTransformFn());
2759f22ef01cSRoman Divacky     for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i)
2760f22ef01cSRoman Divacky       R->setType(i, Orig->getExtType(i));
2761f22ef01cSRoman Divacky 
2762f22ef01cSRoman Divacky     // If this pattern cannot match, do not include it as a variant.
2763f22ef01cSRoman Divacky     std::string ErrString;
2764f22ef01cSRoman Divacky     if (!R->canPatternMatch(ErrString, CDP)) {
2765f22ef01cSRoman Divacky       delete R;
2766f22ef01cSRoman Divacky     } else {
2767f22ef01cSRoman Divacky       bool AlreadyExists = false;
2768f22ef01cSRoman Divacky 
2769f22ef01cSRoman Divacky       // Scan to see if this pattern has already been emitted.  We can get
2770f22ef01cSRoman Divacky       // duplication due to things like commuting:
2771f22ef01cSRoman Divacky       //   (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
2772f22ef01cSRoman Divacky       // which are the same pattern.  Ignore the dups.
2773f22ef01cSRoman Divacky       for (unsigned i = 0, e = OutVariants.size(); i != e; ++i)
2774f22ef01cSRoman Divacky         if (R->isIsomorphicTo(OutVariants[i], DepVars)) {
2775f22ef01cSRoman Divacky           AlreadyExists = true;
2776f22ef01cSRoman Divacky           break;
2777f22ef01cSRoman Divacky         }
2778f22ef01cSRoman Divacky 
2779f22ef01cSRoman Divacky       if (AlreadyExists)
2780f22ef01cSRoman Divacky         delete R;
2781f22ef01cSRoman Divacky       else
2782f22ef01cSRoman Divacky         OutVariants.push_back(R);
2783f22ef01cSRoman Divacky     }
2784f22ef01cSRoman Divacky 
2785f22ef01cSRoman Divacky     // Increment indices to the next permutation by incrementing the
2786f22ef01cSRoman Divacky     // indicies from last index backward, e.g., generate the sequence
2787f22ef01cSRoman Divacky     // [0, 0], [0, 1], [1, 0], [1, 1].
2788f22ef01cSRoman Divacky     int IdxsIdx;
2789f22ef01cSRoman Divacky     for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
2790f22ef01cSRoman Divacky       if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size())
2791f22ef01cSRoman Divacky         Idxs[IdxsIdx] = 0;
2792f22ef01cSRoman Divacky       else
2793f22ef01cSRoman Divacky         break;
2794f22ef01cSRoman Divacky     }
2795f22ef01cSRoman Divacky     NotDone = (IdxsIdx >= 0);
2796f22ef01cSRoman Divacky   } while (NotDone);
2797f22ef01cSRoman Divacky }
2798f22ef01cSRoman Divacky 
2799f22ef01cSRoman Divacky /// CombineChildVariants - A helper function for binary operators.
2800f22ef01cSRoman Divacky ///
2801f22ef01cSRoman Divacky static void CombineChildVariants(TreePatternNode *Orig,
2802f22ef01cSRoman Divacky                                  const std::vector<TreePatternNode*> &LHS,
2803f22ef01cSRoman Divacky                                  const std::vector<TreePatternNode*> &RHS,
2804f22ef01cSRoman Divacky                                  std::vector<TreePatternNode*> &OutVariants,
2805f22ef01cSRoman Divacky                                  CodeGenDAGPatterns &CDP,
2806f22ef01cSRoman Divacky                                  const MultipleUseVarSet &DepVars) {
2807f22ef01cSRoman Divacky   std::vector<std::vector<TreePatternNode*> > ChildVariants;
2808f22ef01cSRoman Divacky   ChildVariants.push_back(LHS);
2809f22ef01cSRoman Divacky   ChildVariants.push_back(RHS);
2810f22ef01cSRoman Divacky   CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars);
2811f22ef01cSRoman Divacky }
2812f22ef01cSRoman Divacky 
2813f22ef01cSRoman Divacky 
2814f22ef01cSRoman Divacky static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N,
2815f22ef01cSRoman Divacky                                      std::vector<TreePatternNode *> &Children) {
2816f22ef01cSRoman Divacky   assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!");
2817f22ef01cSRoman Divacky   Record *Operator = N->getOperator();
2818f22ef01cSRoman Divacky 
2819f22ef01cSRoman Divacky   // Only permit raw nodes.
2820f22ef01cSRoman Divacky   if (!N->getName().empty() || !N->getPredicateFns().empty() ||
2821f22ef01cSRoman Divacky       N->getTransformFn()) {
2822f22ef01cSRoman Divacky     Children.push_back(N);
2823f22ef01cSRoman Divacky     return;
2824f22ef01cSRoman Divacky   }
2825f22ef01cSRoman Divacky 
2826f22ef01cSRoman Divacky   if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
2827f22ef01cSRoman Divacky     Children.push_back(N->getChild(0));
2828f22ef01cSRoman Divacky   else
2829f22ef01cSRoman Divacky     GatherChildrenOfAssociativeOpcode(N->getChild(0), Children);
2830f22ef01cSRoman Divacky 
2831f22ef01cSRoman Divacky   if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
2832f22ef01cSRoman Divacky     Children.push_back(N->getChild(1));
2833f22ef01cSRoman Divacky   else
2834f22ef01cSRoman Divacky     GatherChildrenOfAssociativeOpcode(N->getChild(1), Children);
2835f22ef01cSRoman Divacky }
2836f22ef01cSRoman Divacky 
2837f22ef01cSRoman Divacky /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
2838f22ef01cSRoman Divacky /// the (potentially recursive) pattern by using algebraic laws.
2839f22ef01cSRoman Divacky ///
2840f22ef01cSRoman Divacky static void GenerateVariantsOf(TreePatternNode *N,
2841f22ef01cSRoman Divacky                                std::vector<TreePatternNode*> &OutVariants,
2842f22ef01cSRoman Divacky                                CodeGenDAGPatterns &CDP,
2843f22ef01cSRoman Divacky                                const MultipleUseVarSet &DepVars) {
2844f22ef01cSRoman Divacky   // We cannot permute leaves.
2845f22ef01cSRoman Divacky   if (N->isLeaf()) {
2846f22ef01cSRoman Divacky     OutVariants.push_back(N);
2847f22ef01cSRoman Divacky     return;
2848f22ef01cSRoman Divacky   }
2849f22ef01cSRoman Divacky 
2850f22ef01cSRoman Divacky   // Look up interesting info about the node.
2851f22ef01cSRoman Divacky   const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator());
2852f22ef01cSRoman Divacky 
2853f22ef01cSRoman Divacky   // If this node is associative, re-associate.
2854f22ef01cSRoman Divacky   if (NodeInfo.hasProperty(SDNPAssociative)) {
2855f22ef01cSRoman Divacky     // Re-associate by pulling together all of the linked operators
2856f22ef01cSRoman Divacky     std::vector<TreePatternNode*> MaximalChildren;
2857f22ef01cSRoman Divacky     GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
2858f22ef01cSRoman Divacky 
2859f22ef01cSRoman Divacky     // Only handle child sizes of 3.  Otherwise we'll end up trying too many
2860f22ef01cSRoman Divacky     // permutations.
2861f22ef01cSRoman Divacky     if (MaximalChildren.size() == 3) {
2862f22ef01cSRoman Divacky       // Find the variants of all of our maximal children.
2863f22ef01cSRoman Divacky       std::vector<TreePatternNode*> AVariants, BVariants, CVariants;
2864f22ef01cSRoman Divacky       GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars);
2865f22ef01cSRoman Divacky       GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars);
2866f22ef01cSRoman Divacky       GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars);
2867f22ef01cSRoman Divacky 
2868f22ef01cSRoman Divacky       // There are only two ways we can permute the tree:
2869f22ef01cSRoman Divacky       //   (A op B) op C    and    A op (B op C)
2870f22ef01cSRoman Divacky       // Within these forms, we can also permute A/B/C.
2871f22ef01cSRoman Divacky 
2872f22ef01cSRoman Divacky       // Generate legal pair permutations of A/B/C.
2873f22ef01cSRoman Divacky       std::vector<TreePatternNode*> ABVariants;
2874f22ef01cSRoman Divacky       std::vector<TreePatternNode*> BAVariants;
2875f22ef01cSRoman Divacky       std::vector<TreePatternNode*> ACVariants;
2876f22ef01cSRoman Divacky       std::vector<TreePatternNode*> CAVariants;
2877f22ef01cSRoman Divacky       std::vector<TreePatternNode*> BCVariants;
2878f22ef01cSRoman Divacky       std::vector<TreePatternNode*> CBVariants;
2879f22ef01cSRoman Divacky       CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars);
2880f22ef01cSRoman Divacky       CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars);
2881f22ef01cSRoman Divacky       CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars);
2882f22ef01cSRoman Divacky       CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars);
2883f22ef01cSRoman Divacky       CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars);
2884f22ef01cSRoman Divacky       CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars);
2885f22ef01cSRoman Divacky 
2886f22ef01cSRoman Divacky       // Combine those into the result: (x op x) op x
2887f22ef01cSRoman Divacky       CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars);
2888f22ef01cSRoman Divacky       CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars);
2889f22ef01cSRoman Divacky       CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars);
2890f22ef01cSRoman Divacky       CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars);
2891f22ef01cSRoman Divacky       CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars);
2892f22ef01cSRoman Divacky       CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars);
2893f22ef01cSRoman Divacky 
2894f22ef01cSRoman Divacky       // Combine those into the result: x op (x op x)
2895f22ef01cSRoman Divacky       CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars);
2896f22ef01cSRoman Divacky       CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars);
2897f22ef01cSRoman Divacky       CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars);
2898f22ef01cSRoman Divacky       CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars);
2899f22ef01cSRoman Divacky       CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars);
2900f22ef01cSRoman Divacky       CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars);
2901f22ef01cSRoman Divacky       return;
2902f22ef01cSRoman Divacky     }
2903f22ef01cSRoman Divacky   }
2904f22ef01cSRoman Divacky 
2905f22ef01cSRoman Divacky   // Compute permutations of all children.
2906f22ef01cSRoman Divacky   std::vector<std::vector<TreePatternNode*> > ChildVariants;
2907f22ef01cSRoman Divacky   ChildVariants.resize(N->getNumChildren());
2908f22ef01cSRoman Divacky   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2909f22ef01cSRoman Divacky     GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars);
2910f22ef01cSRoman Divacky 
2911f22ef01cSRoman Divacky   // Build all permutations based on how the children were formed.
2912f22ef01cSRoman Divacky   CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars);
2913f22ef01cSRoman Divacky 
2914f22ef01cSRoman Divacky   // If this node is commutative, consider the commuted order.
2915f22ef01cSRoman Divacky   bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP);
2916f22ef01cSRoman Divacky   if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
2917f22ef01cSRoman Divacky     assert((N->getNumChildren()==2 || isCommIntrinsic) &&
2918f22ef01cSRoman Divacky            "Commutative but doesn't have 2 children!");
2919f22ef01cSRoman Divacky     // Don't count children which are actually register references.
2920f22ef01cSRoman Divacky     unsigned NC = 0;
2921f22ef01cSRoman Divacky     for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
2922f22ef01cSRoman Divacky       TreePatternNode *Child = N->getChild(i);
2923f22ef01cSRoman Divacky       if (Child->isLeaf())
2924f22ef01cSRoman Divacky         if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
2925f22ef01cSRoman Divacky           Record *RR = DI->getDef();
2926f22ef01cSRoman Divacky           if (RR->isSubClassOf("Register"))
2927f22ef01cSRoman Divacky             continue;
2928f22ef01cSRoman Divacky         }
2929f22ef01cSRoman Divacky       NC++;
2930f22ef01cSRoman Divacky     }
2931f22ef01cSRoman Divacky     // Consider the commuted order.
2932f22ef01cSRoman Divacky     if (isCommIntrinsic) {
2933f22ef01cSRoman Divacky       // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd
2934f22ef01cSRoman Divacky       // operands are the commutative operands, and there might be more operands
2935f22ef01cSRoman Divacky       // after those.
2936f22ef01cSRoman Divacky       assert(NC >= 3 &&
2937f22ef01cSRoman Divacky              "Commutative intrinsic should have at least 3 childrean!");
2938f22ef01cSRoman Divacky       std::vector<std::vector<TreePatternNode*> > Variants;
2939f22ef01cSRoman Divacky       Variants.push_back(ChildVariants[0]); // Intrinsic id.
2940f22ef01cSRoman Divacky       Variants.push_back(ChildVariants[2]);
2941f22ef01cSRoman Divacky       Variants.push_back(ChildVariants[1]);
2942f22ef01cSRoman Divacky       for (unsigned i = 3; i != NC; ++i)
2943f22ef01cSRoman Divacky         Variants.push_back(ChildVariants[i]);
2944f22ef01cSRoman Divacky       CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
2945f22ef01cSRoman Divacky     } else if (NC == 2)
2946f22ef01cSRoman Divacky       CombineChildVariants(N, ChildVariants[1], ChildVariants[0],
2947f22ef01cSRoman Divacky                            OutVariants, CDP, DepVars);
2948f22ef01cSRoman Divacky   }
2949f22ef01cSRoman Divacky }
2950f22ef01cSRoman Divacky 
2951f22ef01cSRoman Divacky 
2952f22ef01cSRoman Divacky // GenerateVariants - Generate variants.  For example, commutative patterns can
2953f22ef01cSRoman Divacky // match multiple ways.  Add them to PatternsToMatch as well.
2954f22ef01cSRoman Divacky void CodeGenDAGPatterns::GenerateVariants() {
2955f22ef01cSRoman Divacky   DEBUG(errs() << "Generating instruction variants.\n");
2956f22ef01cSRoman Divacky 
2957f22ef01cSRoman Divacky   // Loop over all of the patterns we've collected, checking to see if we can
2958f22ef01cSRoman Divacky   // generate variants of the instruction, through the exploitation of
2959f22ef01cSRoman Divacky   // identities.  This permits the target to provide aggressive matching without
2960f22ef01cSRoman Divacky   // the .td file having to contain tons of variants of instructions.
2961f22ef01cSRoman Divacky   //
2962f22ef01cSRoman Divacky   // Note that this loop adds new patterns to the PatternsToMatch list, but we
2963f22ef01cSRoman Divacky   // intentionally do not reconsider these.  Any variants of added patterns have
2964f22ef01cSRoman Divacky   // already been added.
2965f22ef01cSRoman Divacky   //
2966f22ef01cSRoman Divacky   for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) {
2967f22ef01cSRoman Divacky     MultipleUseVarSet             DepVars;
2968f22ef01cSRoman Divacky     std::vector<TreePatternNode*> Variants;
2969f22ef01cSRoman Divacky     FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars);
2970f22ef01cSRoman Divacky     DEBUG(errs() << "Dependent/multiply used variables: ");
2971f22ef01cSRoman Divacky     DEBUG(DumpDepVars(DepVars));
2972f22ef01cSRoman Divacky     DEBUG(errs() << "\n");
2973f22ef01cSRoman Divacky     GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this, DepVars);
2974f22ef01cSRoman Divacky 
2975f22ef01cSRoman Divacky     assert(!Variants.empty() && "Must create at least original variant!");
2976f22ef01cSRoman Divacky     Variants.erase(Variants.begin());  // Remove the original pattern.
2977f22ef01cSRoman Divacky 
2978f22ef01cSRoman Divacky     if (Variants.empty())  // No variants for this pattern.
2979f22ef01cSRoman Divacky       continue;
2980f22ef01cSRoman Divacky 
2981f22ef01cSRoman Divacky     DEBUG(errs() << "FOUND VARIANTS OF: ";
2982f22ef01cSRoman Divacky           PatternsToMatch[i].getSrcPattern()->dump();
2983f22ef01cSRoman Divacky           errs() << "\n");
2984f22ef01cSRoman Divacky 
2985f22ef01cSRoman Divacky     for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
2986f22ef01cSRoman Divacky       TreePatternNode *Variant = Variants[v];
2987f22ef01cSRoman Divacky 
2988f22ef01cSRoman Divacky       DEBUG(errs() << "  VAR#" << v <<  ": ";
2989f22ef01cSRoman Divacky             Variant->dump();
2990f22ef01cSRoman Divacky             errs() << "\n");
2991f22ef01cSRoman Divacky 
2992f22ef01cSRoman Divacky       // Scan to see if an instruction or explicit pattern already matches this.
2993f22ef01cSRoman Divacky       bool AlreadyExists = false;
2994f22ef01cSRoman Divacky       for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
2995f22ef01cSRoman Divacky         // Skip if the top level predicates do not match.
2996f22ef01cSRoman Divacky         if (PatternsToMatch[i].getPredicates() !=
2997f22ef01cSRoman Divacky             PatternsToMatch[p].getPredicates())
2998f22ef01cSRoman Divacky           continue;
2999f22ef01cSRoman Divacky         // Check to see if this variant already exists.
3000f22ef01cSRoman Divacky         if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(), DepVars)) {
3001f22ef01cSRoman Divacky           DEBUG(errs() << "  *** ALREADY EXISTS, ignoring variant.\n");
3002f22ef01cSRoman Divacky           AlreadyExists = true;
3003f22ef01cSRoman Divacky           break;
3004f22ef01cSRoman Divacky         }
3005f22ef01cSRoman Divacky       }
3006f22ef01cSRoman Divacky       // If we already have it, ignore the variant.
3007f22ef01cSRoman Divacky       if (AlreadyExists) continue;
3008f22ef01cSRoman Divacky 
3009f22ef01cSRoman Divacky       // Otherwise, add it to the list of patterns we have.
3010f22ef01cSRoman Divacky       PatternsToMatch.
3011f22ef01cSRoman Divacky         push_back(PatternToMatch(PatternsToMatch[i].getPredicates(),
3012f22ef01cSRoman Divacky                                  Variant, PatternsToMatch[i].getDstPattern(),
3013f22ef01cSRoman Divacky                                  PatternsToMatch[i].getDstRegs(),
3014f22ef01cSRoman Divacky                                  PatternsToMatch[i].getAddedComplexity(),
3015f22ef01cSRoman Divacky                                  Record::getNewUID()));
3016f22ef01cSRoman Divacky     }
3017f22ef01cSRoman Divacky 
3018f22ef01cSRoman Divacky     DEBUG(errs() << "\n");
3019f22ef01cSRoman Divacky   }
3020f22ef01cSRoman Divacky }
3021f22ef01cSRoman Divacky 
3022