1*9880d681SAndroid Build Coastguard Worker //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the CodeGenDAGPatterns class, which is used to read and
11*9880d681SAndroid Build Coastguard Worker // represent the patterns present in a .td file for instructions.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "CodeGenDAGPatterns.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallString.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Twine.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/TableGen/Error.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/TableGen/Record.h"
24*9880d681SAndroid Build Coastguard Worker #include <algorithm>
25*9880d681SAndroid Build Coastguard Worker #include <cstdio>
26*9880d681SAndroid Build Coastguard Worker #include <set>
27*9880d681SAndroid Build Coastguard Worker using namespace llvm;
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "dag-patterns"
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
32*9880d681SAndroid Build Coastguard Worker // EEVT::TypeSet Implementation
33*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
34*9880d681SAndroid Build Coastguard Worker
isInteger(MVT::SimpleValueType VT)35*9880d681SAndroid Build Coastguard Worker static inline bool isInteger(MVT::SimpleValueType VT) {
36*9880d681SAndroid Build Coastguard Worker return MVT(VT).isInteger();
37*9880d681SAndroid Build Coastguard Worker }
isFloatingPoint(MVT::SimpleValueType VT)38*9880d681SAndroid Build Coastguard Worker static inline bool isFloatingPoint(MVT::SimpleValueType VT) {
39*9880d681SAndroid Build Coastguard Worker return MVT(VT).isFloatingPoint();
40*9880d681SAndroid Build Coastguard Worker }
isVector(MVT::SimpleValueType VT)41*9880d681SAndroid Build Coastguard Worker static inline bool isVector(MVT::SimpleValueType VT) {
42*9880d681SAndroid Build Coastguard Worker return MVT(VT).isVector();
43*9880d681SAndroid Build Coastguard Worker }
isScalar(MVT::SimpleValueType VT)44*9880d681SAndroid Build Coastguard Worker static inline bool isScalar(MVT::SimpleValueType VT) {
45*9880d681SAndroid Build Coastguard Worker return !MVT(VT).isVector();
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker
TypeSet(MVT::SimpleValueType VT,TreePattern & TP)48*9880d681SAndroid Build Coastguard Worker EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) {
49*9880d681SAndroid Build Coastguard Worker if (VT == MVT::iAny)
50*9880d681SAndroid Build Coastguard Worker EnforceInteger(TP);
51*9880d681SAndroid Build Coastguard Worker else if (VT == MVT::fAny)
52*9880d681SAndroid Build Coastguard Worker EnforceFloatingPoint(TP);
53*9880d681SAndroid Build Coastguard Worker else if (VT == MVT::vAny)
54*9880d681SAndroid Build Coastguard Worker EnforceVector(TP);
55*9880d681SAndroid Build Coastguard Worker else {
56*9880d681SAndroid Build Coastguard Worker assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR ||
57*9880d681SAndroid Build Coastguard Worker VT == MVT::iPTRAny || VT == MVT::Any) && "Not a concrete type!");
58*9880d681SAndroid Build Coastguard Worker TypeVec.push_back(VT);
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker
TypeSet(ArrayRef<MVT::SimpleValueType> VTList)63*9880d681SAndroid Build Coastguard Worker EEVT::TypeSet::TypeSet(ArrayRef<MVT::SimpleValueType> VTList) {
64*9880d681SAndroid Build Coastguard Worker assert(!VTList.empty() && "empty list?");
65*9880d681SAndroid Build Coastguard Worker TypeVec.append(VTList.begin(), VTList.end());
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker if (!VTList.empty())
68*9880d681SAndroid Build Coastguard Worker assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny &&
69*9880d681SAndroid Build Coastguard Worker VTList[0] != MVT::fAny);
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker // Verify no duplicates.
72*9880d681SAndroid Build Coastguard Worker array_pod_sort(TypeVec.begin(), TypeVec.end());
73*9880d681SAndroid Build Coastguard Worker assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end());
74*9880d681SAndroid Build Coastguard Worker }
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker /// FillWithPossibleTypes - Set to all legal types and return true, only valid
77*9880d681SAndroid Build Coastguard Worker /// on completely unknown type sets.
FillWithPossibleTypes(TreePattern & TP,bool (* Pred)(MVT::SimpleValueType),const char * PredicateName)78*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP,
79*9880d681SAndroid Build Coastguard Worker bool (*Pred)(MVT::SimpleValueType),
80*9880d681SAndroid Build Coastguard Worker const char *PredicateName) {
81*9880d681SAndroid Build Coastguard Worker assert(isCompletelyUnknown());
82*9880d681SAndroid Build Coastguard Worker ArrayRef<MVT::SimpleValueType> LegalTypes =
83*9880d681SAndroid Build Coastguard Worker TP.getDAGPatterns().getTargetInfo().getLegalValueTypes();
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
86*9880d681SAndroid Build Coastguard Worker return false;
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker for (MVT::SimpleValueType VT : LegalTypes)
89*9880d681SAndroid Build Coastguard Worker if (!Pred || Pred(VT))
90*9880d681SAndroid Build Coastguard Worker TypeVec.push_back(VT);
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker // If we have nothing that matches the predicate, bail out.
93*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) {
94*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, no " +
95*9880d681SAndroid Build Coastguard Worker std::string(PredicateName) + " types found");
96*9880d681SAndroid Build Coastguard Worker return false;
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker // No need to sort with one element.
99*9880d681SAndroid Build Coastguard Worker if (TypeVec.size() == 1) return true;
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker // Remove duplicates.
102*9880d681SAndroid Build Coastguard Worker array_pod_sort(TypeVec.begin(), TypeVec.end());
103*9880d681SAndroid Build Coastguard Worker TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end());
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker return true;
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker /// hasIntegerTypes - Return true if this TypeSet contains iAny or an
109*9880d681SAndroid Build Coastguard Worker /// integer value type.
hasIntegerTypes() const110*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::hasIntegerTypes() const {
111*9880d681SAndroid Build Coastguard Worker return std::any_of(TypeVec.begin(), TypeVec.end(), isInteger);
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
115*9880d681SAndroid Build Coastguard Worker /// a floating point value type.
hasFloatingPointTypes() const116*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::hasFloatingPointTypes() const {
117*9880d681SAndroid Build Coastguard Worker return std::any_of(TypeVec.begin(), TypeVec.end(), isFloatingPoint);
118*9880d681SAndroid Build Coastguard Worker }
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker /// hasScalarTypes - Return true if this TypeSet contains a scalar value type.
hasScalarTypes() const121*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::hasScalarTypes() const {
122*9880d681SAndroid Build Coastguard Worker return std::any_of(TypeVec.begin(), TypeVec.end(), isScalar);
123*9880d681SAndroid Build Coastguard Worker }
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker /// hasVectorTypes - Return true if this TypeSet contains a vAny or a vector
126*9880d681SAndroid Build Coastguard Worker /// value type.
hasVectorTypes() const127*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::hasVectorTypes() const {
128*9880d681SAndroid Build Coastguard Worker return std::any_of(TypeVec.begin(), TypeVec.end(), isVector);
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker
getName() const132*9880d681SAndroid Build Coastguard Worker std::string EEVT::TypeSet::getName() const {
133*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) return "<empty>";
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker std::string Result;
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) {
138*9880d681SAndroid Build Coastguard Worker std::string VTName = llvm::getEnumName(TypeVec[i]);
139*9880d681SAndroid Build Coastguard Worker // Strip off MVT:: prefix if present.
140*9880d681SAndroid Build Coastguard Worker if (VTName.substr(0,5) == "MVT::")
141*9880d681SAndroid Build Coastguard Worker VTName = VTName.substr(5);
142*9880d681SAndroid Build Coastguard Worker if (i) Result += ':';
143*9880d681SAndroid Build Coastguard Worker Result += VTName;
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker if (TypeVec.size() == 1)
147*9880d681SAndroid Build Coastguard Worker return Result;
148*9880d681SAndroid Build Coastguard Worker return "{" + Result + "}";
149*9880d681SAndroid Build Coastguard Worker }
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker /// MergeInTypeInfo - This merges in type information from the specified
152*9880d681SAndroid Build Coastguard Worker /// argument. If 'this' changes, it returns true. If the two types are
153*9880d681SAndroid Build Coastguard Worker /// contradictory (e.g. merge f32 into i32) then this flags an error.
MergeInTypeInfo(const EEVT::TypeSet & InVT,TreePattern & TP)154*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){
155*9880d681SAndroid Build Coastguard Worker if (InVT.isCompletelyUnknown() || *this == InVT || TP.hasError())
156*9880d681SAndroid Build Coastguard Worker return false;
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker if (isCompletelyUnknown()) {
159*9880d681SAndroid Build Coastguard Worker *this = InVT;
160*9880d681SAndroid Build Coastguard Worker return true;
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker assert(!TypeVec.empty() && !InVT.TypeVec.empty() && "No unknowns");
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker // Handle the abstract cases, seeing if we can resolve them better.
166*9880d681SAndroid Build Coastguard Worker switch (TypeVec[0]) {
167*9880d681SAndroid Build Coastguard Worker default: break;
168*9880d681SAndroid Build Coastguard Worker case MVT::iPTR:
169*9880d681SAndroid Build Coastguard Worker case MVT::iPTRAny:
170*9880d681SAndroid Build Coastguard Worker if (InVT.hasIntegerTypes()) {
171*9880d681SAndroid Build Coastguard Worker EEVT::TypeSet InCopy(InVT);
172*9880d681SAndroid Build Coastguard Worker InCopy.EnforceInteger(TP);
173*9880d681SAndroid Build Coastguard Worker InCopy.EnforceScalar(TP);
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker if (InCopy.isConcrete()) {
176*9880d681SAndroid Build Coastguard Worker // If the RHS has one integer type, upgrade iPTR to i32.
177*9880d681SAndroid Build Coastguard Worker TypeVec[0] = InVT.TypeVec[0];
178*9880d681SAndroid Build Coastguard Worker return true;
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker // If the input has multiple scalar integers, this doesn't add any info.
182*9880d681SAndroid Build Coastguard Worker if (!InCopy.isCompletelyUnknown())
183*9880d681SAndroid Build Coastguard Worker return false;
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker break;
186*9880d681SAndroid Build Coastguard Worker }
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker // If the input constraint is iAny/iPTR and this is an integer type list,
189*9880d681SAndroid Build Coastguard Worker // remove non-integer types from the list.
190*9880d681SAndroid Build Coastguard Worker if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) &&
191*9880d681SAndroid Build Coastguard Worker hasIntegerTypes()) {
192*9880d681SAndroid Build Coastguard Worker bool MadeChange = EnforceInteger(TP);
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // If we're merging in iPTR/iPTRAny and the node currently has a list of
195*9880d681SAndroid Build Coastguard Worker // multiple different integer types, replace them with a single iPTR.
196*9880d681SAndroid Build Coastguard Worker if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) &&
197*9880d681SAndroid Build Coastguard Worker TypeVec.size() != 1) {
198*9880d681SAndroid Build Coastguard Worker TypeVec.assign(1, InVT.TypeVec[0]);
199*9880d681SAndroid Build Coastguard Worker MadeChange = true;
200*9880d681SAndroid Build Coastguard Worker }
201*9880d681SAndroid Build Coastguard Worker
202*9880d681SAndroid Build Coastguard Worker return MadeChange;
203*9880d681SAndroid Build Coastguard Worker }
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker // If this is a type list and the RHS is a typelist as well, eliminate entries
206*9880d681SAndroid Build Coastguard Worker // from this list that aren't in the other one.
207*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker TypeVec.clear();
210*9880d681SAndroid Build Coastguard Worker std::set_intersection(InputSet.TypeVec.begin(), InputSet.TypeVec.end(),
211*9880d681SAndroid Build Coastguard Worker InVT.TypeVec.begin(), InVT.TypeVec.end(),
212*9880d681SAndroid Build Coastguard Worker std::back_inserter(TypeVec));
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker // If the intersection is the same size as the original set then we're done.
215*9880d681SAndroid Build Coastguard Worker if (TypeVec.size() == InputSet.TypeVec.size())
216*9880d681SAndroid Build Coastguard Worker return false;
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker // If we removed all of our types, we have a type contradiction.
219*9880d681SAndroid Build Coastguard Worker if (!TypeVec.empty())
220*9880d681SAndroid Build Coastguard Worker return true;
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker // FIXME: Really want an SMLoc here!
223*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, merging '" +
224*9880d681SAndroid Build Coastguard Worker InVT.getName() + "' into '" + InputSet.getName() + "'");
225*9880d681SAndroid Build Coastguard Worker return false;
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker /// EnforceInteger - Remove all non-integer types from this set.
EnforceInteger(TreePattern & TP)229*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) {
230*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
231*9880d681SAndroid Build Coastguard Worker return false;
232*9880d681SAndroid Build Coastguard Worker // If we know nothing, then get the full set.
233*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty())
234*9880d681SAndroid Build Coastguard Worker return FillWithPossibleTypes(TP, isInteger, "integer");
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker if (!hasFloatingPointTypes())
237*9880d681SAndroid Build Coastguard Worker return false;
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker // Filter out all the fp types.
242*9880d681SAndroid Build Coastguard Worker TypeVec.erase(std::remove_if(TypeVec.begin(), TypeVec.end(),
243*9880d681SAndroid Build Coastguard Worker std::not1(std::ptr_fun(isInteger))),
244*9880d681SAndroid Build Coastguard Worker TypeVec.end());
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) {
247*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, '" +
248*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' needs to be integer");
249*9880d681SAndroid Build Coastguard Worker return false;
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker return true;
252*9880d681SAndroid Build Coastguard Worker }
253*9880d681SAndroid Build Coastguard Worker
254*9880d681SAndroid Build Coastguard Worker /// EnforceFloatingPoint - Remove all integer types from this set.
EnforceFloatingPoint(TreePattern & TP)255*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) {
256*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
257*9880d681SAndroid Build Coastguard Worker return false;
258*9880d681SAndroid Build Coastguard Worker // If we know nothing, then get the full set.
259*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty())
260*9880d681SAndroid Build Coastguard Worker return FillWithPossibleTypes(TP, isFloatingPoint, "floating point");
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker if (!hasIntegerTypes())
263*9880d681SAndroid Build Coastguard Worker return false;
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker // Filter out all the integer types.
268*9880d681SAndroid Build Coastguard Worker TypeVec.erase(std::remove_if(TypeVec.begin(), TypeVec.end(),
269*9880d681SAndroid Build Coastguard Worker std::not1(std::ptr_fun(isFloatingPoint))),
270*9880d681SAndroid Build Coastguard Worker TypeVec.end());
271*9880d681SAndroid Build Coastguard Worker
272*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) {
273*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, '" +
274*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' needs to be floating point");
275*9880d681SAndroid Build Coastguard Worker return false;
276*9880d681SAndroid Build Coastguard Worker }
277*9880d681SAndroid Build Coastguard Worker return true;
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker /// EnforceScalar - Remove all vector types from this.
EnforceScalar(TreePattern & TP)281*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) {
282*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
283*9880d681SAndroid Build Coastguard Worker return false;
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker // If we know nothing, then get the full set.
286*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty())
287*9880d681SAndroid Build Coastguard Worker return FillWithPossibleTypes(TP, isScalar, "scalar");
288*9880d681SAndroid Build Coastguard Worker
289*9880d681SAndroid Build Coastguard Worker if (!hasVectorTypes())
290*9880d681SAndroid Build Coastguard Worker return false;
291*9880d681SAndroid Build Coastguard Worker
292*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker // Filter out all the vector types.
295*9880d681SAndroid Build Coastguard Worker TypeVec.erase(std::remove_if(TypeVec.begin(), TypeVec.end(),
296*9880d681SAndroid Build Coastguard Worker std::not1(std::ptr_fun(isScalar))),
297*9880d681SAndroid Build Coastguard Worker TypeVec.end());
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) {
300*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, '" +
301*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' needs to be scalar");
302*9880d681SAndroid Build Coastguard Worker return false;
303*9880d681SAndroid Build Coastguard Worker }
304*9880d681SAndroid Build Coastguard Worker return true;
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker /// EnforceVector - Remove all vector types from this.
EnforceVector(TreePattern & TP)308*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceVector(TreePattern &TP) {
309*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
310*9880d681SAndroid Build Coastguard Worker return false;
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker // If we know nothing, then get the full set.
313*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty())
314*9880d681SAndroid Build Coastguard Worker return FillWithPossibleTypes(TP, isVector, "vector");
315*9880d681SAndroid Build Coastguard Worker
316*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
317*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard Worker // Filter out all the scalar types.
320*9880d681SAndroid Build Coastguard Worker TypeVec.erase(std::remove_if(TypeVec.begin(), TypeVec.end(),
321*9880d681SAndroid Build Coastguard Worker std::not1(std::ptr_fun(isVector))),
322*9880d681SAndroid Build Coastguard Worker TypeVec.end());
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) {
325*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, '" +
326*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' needs to be a vector");
327*9880d681SAndroid Build Coastguard Worker return false;
328*9880d681SAndroid Build Coastguard Worker }
329*9880d681SAndroid Build Coastguard Worker return MadeChange;
330*9880d681SAndroid Build Coastguard Worker }
331*9880d681SAndroid Build Coastguard Worker
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard Worker /// EnforceSmallerThan - 'this' must be a smaller VT than Other. For vectors
335*9880d681SAndroid Build Coastguard Worker /// this should be based on the element type. Update this and other based on
336*9880d681SAndroid Build Coastguard Worker /// this information.
EnforceSmallerThan(EEVT::TypeSet & Other,TreePattern & TP)337*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) {
338*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
339*9880d681SAndroid Build Coastguard Worker return false;
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker // Both operands must be integer or FP, but we don't care which.
342*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard Worker if (isCompletelyUnknown())
345*9880d681SAndroid Build Coastguard Worker MadeChange = FillWithPossibleTypes(TP);
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard Worker if (Other.isCompletelyUnknown())
348*9880d681SAndroid Build Coastguard Worker MadeChange = Other.FillWithPossibleTypes(TP);
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker // If one side is known to be integer or known to be FP but the other side has
351*9880d681SAndroid Build Coastguard Worker // no information, get at least the type integrality info in there.
352*9880d681SAndroid Build Coastguard Worker if (!hasFloatingPointTypes())
353*9880d681SAndroid Build Coastguard Worker MadeChange |= Other.EnforceInteger(TP);
354*9880d681SAndroid Build Coastguard Worker else if (!hasIntegerTypes())
355*9880d681SAndroid Build Coastguard Worker MadeChange |= Other.EnforceFloatingPoint(TP);
356*9880d681SAndroid Build Coastguard Worker if (!Other.hasFloatingPointTypes())
357*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceInteger(TP);
358*9880d681SAndroid Build Coastguard Worker else if (!Other.hasIntegerTypes())
359*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceFloatingPoint(TP);
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() &&
362*9880d681SAndroid Build Coastguard Worker "Should have a type list now");
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker // If one contains vectors but the other doesn't pull vectors out.
365*9880d681SAndroid Build Coastguard Worker if (!hasVectorTypes())
366*9880d681SAndroid Build Coastguard Worker MadeChange |= Other.EnforceScalar(TP);
367*9880d681SAndroid Build Coastguard Worker else if (!hasScalarTypes())
368*9880d681SAndroid Build Coastguard Worker MadeChange |= Other.EnforceVector(TP);
369*9880d681SAndroid Build Coastguard Worker if (!Other.hasVectorTypes())
370*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceScalar(TP);
371*9880d681SAndroid Build Coastguard Worker else if (!Other.hasScalarTypes())
372*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVector(TP);
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker // This code does not currently handle nodes which have multiple types,
375*9880d681SAndroid Build Coastguard Worker // where some types are integer, and some are fp. Assert that this is not
376*9880d681SAndroid Build Coastguard Worker // the case.
377*9880d681SAndroid Build Coastguard Worker assert(!(hasIntegerTypes() && hasFloatingPointTypes()) &&
378*9880d681SAndroid Build Coastguard Worker !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) &&
379*9880d681SAndroid Build Coastguard Worker "SDTCisOpSmallerThanOp does not handle mixed int/fp types!");
380*9880d681SAndroid Build Coastguard Worker
381*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
382*9880d681SAndroid Build Coastguard Worker return false;
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker // Okay, find the smallest type from current set and remove anything the
385*9880d681SAndroid Build Coastguard Worker // same or smaller from the other set. We need to ensure that the scalar
386*9880d681SAndroid Build Coastguard Worker // type size is smaller than the scalar size of the smallest type. For
387*9880d681SAndroid Build Coastguard Worker // vectors, we also need to make sure that the total size is no larger than
388*9880d681SAndroid Build Coastguard Worker // the size of the smallest type.
389*9880d681SAndroid Build Coastguard Worker {
390*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(Other);
391*9880d681SAndroid Build Coastguard Worker MVT Smallest = *std::min_element(TypeVec.begin(), TypeVec.end(),
392*9880d681SAndroid Build Coastguard Worker [](MVT A, MVT B) {
393*9880d681SAndroid Build Coastguard Worker return A.getScalarSizeInBits() < B.getScalarSizeInBits() ||
394*9880d681SAndroid Build Coastguard Worker (A.getScalarSizeInBits() == B.getScalarSizeInBits() &&
395*9880d681SAndroid Build Coastguard Worker A.getSizeInBits() < B.getSizeInBits());
396*9880d681SAndroid Build Coastguard Worker });
397*9880d681SAndroid Build Coastguard Worker
398*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(Other.TypeVec.begin(), Other.TypeVec.end(),
399*9880d681SAndroid Build Coastguard Worker [Smallest](MVT OtherVT) {
400*9880d681SAndroid Build Coastguard Worker // Don't compare vector and non-vector types.
401*9880d681SAndroid Build Coastguard Worker if (OtherVT.isVector() != Smallest.isVector())
402*9880d681SAndroid Build Coastguard Worker return false;
403*9880d681SAndroid Build Coastguard Worker // The getSizeInBits() check here is only needed for vectors, but is
404*9880d681SAndroid Build Coastguard Worker // a subset of the scalar check for scalars so no need to qualify.
405*9880d681SAndroid Build Coastguard Worker return OtherVT.getScalarSizeInBits() <= Smallest.getScalarSizeInBits()||
406*9880d681SAndroid Build Coastguard Worker OtherVT.getSizeInBits() < Smallest.getSizeInBits();
407*9880d681SAndroid Build Coastguard Worker });
408*9880d681SAndroid Build Coastguard Worker MadeChange |= I != Other.TypeVec.end(); // If we're about to remove types.
409*9880d681SAndroid Build Coastguard Worker Other.TypeVec.erase(I, Other.TypeVec.end());
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker if (Other.TypeVec.empty()) {
412*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, '" + InputSet.getName() +
413*9880d681SAndroid Build Coastguard Worker "' has nothing larger than '" + getName() +"'!");
414*9880d681SAndroid Build Coastguard Worker return false;
415*9880d681SAndroid Build Coastguard Worker }
416*9880d681SAndroid Build Coastguard Worker }
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard Worker // Okay, find the largest type from the other set and remove anything the
419*9880d681SAndroid Build Coastguard Worker // same or smaller from the current set. We need to ensure that the scalar
420*9880d681SAndroid Build Coastguard Worker // type size is larger than the scalar size of the largest type. For
421*9880d681SAndroid Build Coastguard Worker // vectors, we also need to make sure that the total size is no smaller than
422*9880d681SAndroid Build Coastguard Worker // the size of the largest type.
423*9880d681SAndroid Build Coastguard Worker {
424*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
425*9880d681SAndroid Build Coastguard Worker MVT Largest = *std::max_element(Other.TypeVec.begin(), Other.TypeVec.end(),
426*9880d681SAndroid Build Coastguard Worker [](MVT A, MVT B) {
427*9880d681SAndroid Build Coastguard Worker return A.getScalarSizeInBits() < B.getScalarSizeInBits() ||
428*9880d681SAndroid Build Coastguard Worker (A.getScalarSizeInBits() == B.getScalarSizeInBits() &&
429*9880d681SAndroid Build Coastguard Worker A.getSizeInBits() < B.getSizeInBits());
430*9880d681SAndroid Build Coastguard Worker });
431*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
432*9880d681SAndroid Build Coastguard Worker [Largest](MVT OtherVT) {
433*9880d681SAndroid Build Coastguard Worker // Don't compare vector and non-vector types.
434*9880d681SAndroid Build Coastguard Worker if (OtherVT.isVector() != Largest.isVector())
435*9880d681SAndroid Build Coastguard Worker return false;
436*9880d681SAndroid Build Coastguard Worker return OtherVT.getScalarSizeInBits() >= Largest.getScalarSizeInBits() ||
437*9880d681SAndroid Build Coastguard Worker OtherVT.getSizeInBits() > Largest.getSizeInBits();
438*9880d681SAndroid Build Coastguard Worker });
439*9880d681SAndroid Build Coastguard Worker MadeChange |= I != TypeVec.end(); // If we're about to remove types.
440*9880d681SAndroid Build Coastguard Worker TypeVec.erase(I, TypeVec.end());
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) {
443*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, '" + InputSet.getName() +
444*9880d681SAndroid Build Coastguard Worker "' has nothing smaller than '" + Other.getName() +"'!");
445*9880d681SAndroid Build Coastguard Worker return false;
446*9880d681SAndroid Build Coastguard Worker }
447*9880d681SAndroid Build Coastguard Worker }
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard Worker return MadeChange;
450*9880d681SAndroid Build Coastguard Worker }
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard Worker /// EnforceVectorEltTypeIs - 'this' is now constrained to be a vector type
453*9880d681SAndroid Build Coastguard Worker /// whose element is specified by VTOperand.
EnforceVectorEltTypeIs(MVT::SimpleValueType VT,TreePattern & TP)454*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceVectorEltTypeIs(MVT::SimpleValueType VT,
455*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
456*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVector(TP);
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
461*9880d681SAndroid Build Coastguard Worker
462*9880d681SAndroid Build Coastguard Worker // Filter out all the types which don't have the right element type.
463*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
464*9880d681SAndroid Build Coastguard Worker [VT](MVT VVT) {
465*9880d681SAndroid Build Coastguard Worker return VVT.getVectorElementType().SimpleTy != VT;
466*9880d681SAndroid Build Coastguard Worker });
467*9880d681SAndroid Build Coastguard Worker MadeChange |= I != TypeVec.end();
468*9880d681SAndroid Build Coastguard Worker TypeVec.erase(I, TypeVec.end());
469*9880d681SAndroid Build Coastguard Worker
470*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) { // FIXME: Really want an SMLoc here!
471*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
472*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have a vector element of type " +
473*9880d681SAndroid Build Coastguard Worker getEnumName(VT));
474*9880d681SAndroid Build Coastguard Worker return false;
475*9880d681SAndroid Build Coastguard Worker }
476*9880d681SAndroid Build Coastguard Worker
477*9880d681SAndroid Build Coastguard Worker return MadeChange;
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker
480*9880d681SAndroid Build Coastguard Worker /// EnforceVectorEltTypeIs - 'this' is now constrained to be a vector type
481*9880d681SAndroid Build Coastguard Worker /// whose element is specified by VTOperand.
EnforceVectorEltTypeIs(EEVT::TypeSet & VTOperand,TreePattern & TP)482*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand,
483*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
484*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
485*9880d681SAndroid Build Coastguard Worker return false;
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker // "This" must be a vector and "VTOperand" must be a scalar.
488*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
489*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVector(TP);
490*9880d681SAndroid Build Coastguard Worker MadeChange |= VTOperand.EnforceScalar(TP);
491*9880d681SAndroid Build Coastguard Worker
492*9880d681SAndroid Build Coastguard Worker // If we know the vector type, it forces the scalar to agree.
493*9880d681SAndroid Build Coastguard Worker if (isConcrete()) {
494*9880d681SAndroid Build Coastguard Worker MVT IVT = getConcrete();
495*9880d681SAndroid Build Coastguard Worker IVT = IVT.getVectorElementType();
496*9880d681SAndroid Build Coastguard Worker return MadeChange || VTOperand.MergeInTypeInfo(IVT.SimpleTy, TP);
497*9880d681SAndroid Build Coastguard Worker }
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker // If the scalar type is known, filter out vector types whose element types
500*9880d681SAndroid Build Coastguard Worker // disagree.
501*9880d681SAndroid Build Coastguard Worker if (!VTOperand.isConcrete())
502*9880d681SAndroid Build Coastguard Worker return MadeChange;
503*9880d681SAndroid Build Coastguard Worker
504*9880d681SAndroid Build Coastguard Worker MVT::SimpleValueType VT = VTOperand.getConcrete();
505*9880d681SAndroid Build Coastguard Worker
506*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVectorEltTypeIs(VT, TP);
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker return MadeChange;
509*9880d681SAndroid Build Coastguard Worker }
510*9880d681SAndroid Build Coastguard Worker
511*9880d681SAndroid Build Coastguard Worker /// EnforceVectorSubVectorTypeIs - 'this' is now constrained to be a
512*9880d681SAndroid Build Coastguard Worker /// vector type specified by VTOperand.
EnforceVectorSubVectorTypeIs(EEVT::TypeSet & VTOperand,TreePattern & TP)513*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand,
514*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
515*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
516*9880d681SAndroid Build Coastguard Worker return false;
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker // "This" must be a vector and "VTOperand" must be a vector.
519*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
520*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVector(TP);
521*9880d681SAndroid Build Coastguard Worker MadeChange |= VTOperand.EnforceVector(TP);
522*9880d681SAndroid Build Coastguard Worker
523*9880d681SAndroid Build Coastguard Worker // If one side is known to be integer or known to be FP but the other side has
524*9880d681SAndroid Build Coastguard Worker // no information, get at least the type integrality info in there.
525*9880d681SAndroid Build Coastguard Worker if (!hasFloatingPointTypes())
526*9880d681SAndroid Build Coastguard Worker MadeChange |= VTOperand.EnforceInteger(TP);
527*9880d681SAndroid Build Coastguard Worker else if (!hasIntegerTypes())
528*9880d681SAndroid Build Coastguard Worker MadeChange |= VTOperand.EnforceFloatingPoint(TP);
529*9880d681SAndroid Build Coastguard Worker if (!VTOperand.hasFloatingPointTypes())
530*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceInteger(TP);
531*9880d681SAndroid Build Coastguard Worker else if (!VTOperand.hasIntegerTypes())
532*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceFloatingPoint(TP);
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker assert(!isCompletelyUnknown() && !VTOperand.isCompletelyUnknown() &&
535*9880d681SAndroid Build Coastguard Worker "Should have a type list now");
536*9880d681SAndroid Build Coastguard Worker
537*9880d681SAndroid Build Coastguard Worker // If we know the vector type, it forces the scalar types to agree.
538*9880d681SAndroid Build Coastguard Worker // Also force one vector to have more elements than the other.
539*9880d681SAndroid Build Coastguard Worker if (isConcrete()) {
540*9880d681SAndroid Build Coastguard Worker MVT IVT = getConcrete();
541*9880d681SAndroid Build Coastguard Worker unsigned NumElems = IVT.getVectorNumElements();
542*9880d681SAndroid Build Coastguard Worker IVT = IVT.getVectorElementType();
543*9880d681SAndroid Build Coastguard Worker
544*9880d681SAndroid Build Coastguard Worker EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP);
545*9880d681SAndroid Build Coastguard Worker MadeChange |= VTOperand.EnforceVectorEltTypeIs(EltTypeSet, TP);
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard Worker // Only keep types that have less elements than VTOperand.
548*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(VTOperand);
549*9880d681SAndroid Build Coastguard Worker
550*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(VTOperand.TypeVec.begin(), VTOperand.TypeVec.end(),
551*9880d681SAndroid Build Coastguard Worker [NumElems](MVT VVT) {
552*9880d681SAndroid Build Coastguard Worker return VVT.getVectorNumElements() >= NumElems;
553*9880d681SAndroid Build Coastguard Worker });
554*9880d681SAndroid Build Coastguard Worker MadeChange |= I != VTOperand.TypeVec.end();
555*9880d681SAndroid Build Coastguard Worker VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
556*9880d681SAndroid Build Coastguard Worker
557*9880d681SAndroid Build Coastguard Worker if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here!
558*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
559*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have less vector elements than '" +
560*9880d681SAndroid Build Coastguard Worker getName() + "'");
561*9880d681SAndroid Build Coastguard Worker return false;
562*9880d681SAndroid Build Coastguard Worker }
563*9880d681SAndroid Build Coastguard Worker } else if (VTOperand.isConcrete()) {
564*9880d681SAndroid Build Coastguard Worker MVT IVT = VTOperand.getConcrete();
565*9880d681SAndroid Build Coastguard Worker unsigned NumElems = IVT.getVectorNumElements();
566*9880d681SAndroid Build Coastguard Worker IVT = IVT.getVectorElementType();
567*9880d681SAndroid Build Coastguard Worker
568*9880d681SAndroid Build Coastguard Worker EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP);
569*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVectorEltTypeIs(EltTypeSet, TP);
570*9880d681SAndroid Build Coastguard Worker
571*9880d681SAndroid Build Coastguard Worker // Only keep types that have more elements than 'this'.
572*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
575*9880d681SAndroid Build Coastguard Worker [NumElems](MVT VVT) {
576*9880d681SAndroid Build Coastguard Worker return VVT.getVectorNumElements() <= NumElems;
577*9880d681SAndroid Build Coastguard Worker });
578*9880d681SAndroid Build Coastguard Worker MadeChange |= I != TypeVec.end();
579*9880d681SAndroid Build Coastguard Worker TypeVec.erase(I, TypeVec.end());
580*9880d681SAndroid Build Coastguard Worker
581*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) { // FIXME: Really want an SMLoc here!
582*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
583*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have more vector elements than '" +
584*9880d681SAndroid Build Coastguard Worker VTOperand.getName() + "'");
585*9880d681SAndroid Build Coastguard Worker return false;
586*9880d681SAndroid Build Coastguard Worker }
587*9880d681SAndroid Build Coastguard Worker }
588*9880d681SAndroid Build Coastguard Worker
589*9880d681SAndroid Build Coastguard Worker return MadeChange;
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker
592*9880d681SAndroid Build Coastguard Worker /// EnforceVectorSameNumElts - 'this' is now constrained to
593*9880d681SAndroid Build Coastguard Worker /// be a vector with same num elements as VTOperand.
EnforceVectorSameNumElts(EEVT::TypeSet & VTOperand,TreePattern & TP)594*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceVectorSameNumElts(EEVT::TypeSet &VTOperand,
595*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
596*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
597*9880d681SAndroid Build Coastguard Worker return false;
598*9880d681SAndroid Build Coastguard Worker
599*9880d681SAndroid Build Coastguard Worker // "This" must be a vector and "VTOperand" must be a vector.
600*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
601*9880d681SAndroid Build Coastguard Worker MadeChange |= EnforceVector(TP);
602*9880d681SAndroid Build Coastguard Worker MadeChange |= VTOperand.EnforceVector(TP);
603*9880d681SAndroid Build Coastguard Worker
604*9880d681SAndroid Build Coastguard Worker // If we know one of the vector types, it forces the other type to agree.
605*9880d681SAndroid Build Coastguard Worker if (isConcrete()) {
606*9880d681SAndroid Build Coastguard Worker MVT IVT = getConcrete();
607*9880d681SAndroid Build Coastguard Worker unsigned NumElems = IVT.getVectorNumElements();
608*9880d681SAndroid Build Coastguard Worker
609*9880d681SAndroid Build Coastguard Worker // Only keep types that have same elements as 'this'.
610*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(VTOperand);
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(VTOperand.TypeVec.begin(), VTOperand.TypeVec.end(),
613*9880d681SAndroid Build Coastguard Worker [NumElems](MVT VVT) {
614*9880d681SAndroid Build Coastguard Worker return VVT.getVectorNumElements() != NumElems;
615*9880d681SAndroid Build Coastguard Worker });
616*9880d681SAndroid Build Coastguard Worker MadeChange |= I != VTOperand.TypeVec.end();
617*9880d681SAndroid Build Coastguard Worker VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Worker if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here!
620*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
621*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have same number elements as '" +
622*9880d681SAndroid Build Coastguard Worker getName() + "'");
623*9880d681SAndroid Build Coastguard Worker return false;
624*9880d681SAndroid Build Coastguard Worker }
625*9880d681SAndroid Build Coastguard Worker } else if (VTOperand.isConcrete()) {
626*9880d681SAndroid Build Coastguard Worker MVT IVT = VTOperand.getConcrete();
627*9880d681SAndroid Build Coastguard Worker unsigned NumElems = IVT.getVectorNumElements();
628*9880d681SAndroid Build Coastguard Worker
629*9880d681SAndroid Build Coastguard Worker // Only keep types that have same elements as VTOperand.
630*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
631*9880d681SAndroid Build Coastguard Worker
632*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
633*9880d681SAndroid Build Coastguard Worker [NumElems](MVT VVT) {
634*9880d681SAndroid Build Coastguard Worker return VVT.getVectorNumElements() != NumElems;
635*9880d681SAndroid Build Coastguard Worker });
636*9880d681SAndroid Build Coastguard Worker MadeChange |= I != TypeVec.end();
637*9880d681SAndroid Build Coastguard Worker TypeVec.erase(I, TypeVec.end());
638*9880d681SAndroid Build Coastguard Worker
639*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) { // FIXME: Really want an SMLoc here!
640*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
641*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have same number elements than '" +
642*9880d681SAndroid Build Coastguard Worker VTOperand.getName() + "'");
643*9880d681SAndroid Build Coastguard Worker return false;
644*9880d681SAndroid Build Coastguard Worker }
645*9880d681SAndroid Build Coastguard Worker }
646*9880d681SAndroid Build Coastguard Worker
647*9880d681SAndroid Build Coastguard Worker return MadeChange;
648*9880d681SAndroid Build Coastguard Worker }
649*9880d681SAndroid Build Coastguard Worker
650*9880d681SAndroid Build Coastguard Worker /// EnforceSameSize - 'this' is now constrained to be same size as VTOperand.
EnforceSameSize(EEVT::TypeSet & VTOperand,TreePattern & TP)651*9880d681SAndroid Build Coastguard Worker bool EEVT::TypeSet::EnforceSameSize(EEVT::TypeSet &VTOperand,
652*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
653*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
654*9880d681SAndroid Build Coastguard Worker return false;
655*9880d681SAndroid Build Coastguard Worker
656*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker // If we know one of the types, it forces the other type agree.
659*9880d681SAndroid Build Coastguard Worker if (isConcrete()) {
660*9880d681SAndroid Build Coastguard Worker MVT IVT = getConcrete();
661*9880d681SAndroid Build Coastguard Worker unsigned Size = IVT.getSizeInBits();
662*9880d681SAndroid Build Coastguard Worker
663*9880d681SAndroid Build Coastguard Worker // Only keep types that have the same size as 'this'.
664*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(VTOperand);
665*9880d681SAndroid Build Coastguard Worker
666*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(VTOperand.TypeVec.begin(), VTOperand.TypeVec.end(),
667*9880d681SAndroid Build Coastguard Worker [&](MVT VT) {
668*9880d681SAndroid Build Coastguard Worker return VT.getSizeInBits() != Size;
669*9880d681SAndroid Build Coastguard Worker });
670*9880d681SAndroid Build Coastguard Worker MadeChange |= I != VTOperand.TypeVec.end();
671*9880d681SAndroid Build Coastguard Worker VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
672*9880d681SAndroid Build Coastguard Worker
673*9880d681SAndroid Build Coastguard Worker if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here!
674*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
675*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have same size as '" +
676*9880d681SAndroid Build Coastguard Worker getName() + "'");
677*9880d681SAndroid Build Coastguard Worker return false;
678*9880d681SAndroid Build Coastguard Worker }
679*9880d681SAndroid Build Coastguard Worker } else if (VTOperand.isConcrete()) {
680*9880d681SAndroid Build Coastguard Worker MVT IVT = VTOperand.getConcrete();
681*9880d681SAndroid Build Coastguard Worker unsigned Size = IVT.getSizeInBits();
682*9880d681SAndroid Build Coastguard Worker
683*9880d681SAndroid Build Coastguard Worker // Only keep types that have the same size as VTOperand.
684*9880d681SAndroid Build Coastguard Worker TypeSet InputSet(*this);
685*9880d681SAndroid Build Coastguard Worker
686*9880d681SAndroid Build Coastguard Worker auto I = std::remove_if(TypeVec.begin(), TypeVec.end(),
687*9880d681SAndroid Build Coastguard Worker [&](MVT VT) {
688*9880d681SAndroid Build Coastguard Worker return VT.getSizeInBits() != Size;
689*9880d681SAndroid Build Coastguard Worker });
690*9880d681SAndroid Build Coastguard Worker MadeChange |= I != TypeVec.end();
691*9880d681SAndroid Build Coastguard Worker TypeVec.erase(I, TypeVec.end());
692*9880d681SAndroid Build Coastguard Worker
693*9880d681SAndroid Build Coastguard Worker if (TypeVec.empty()) { // FIXME: Really want an SMLoc here!
694*9880d681SAndroid Build Coastguard Worker TP.error("Type inference contradiction found, forcing '" +
695*9880d681SAndroid Build Coastguard Worker InputSet.getName() + "' to have same size as '" +
696*9880d681SAndroid Build Coastguard Worker VTOperand.getName() + "'");
697*9880d681SAndroid Build Coastguard Worker return false;
698*9880d681SAndroid Build Coastguard Worker }
699*9880d681SAndroid Build Coastguard Worker }
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard Worker return MadeChange;
702*9880d681SAndroid Build Coastguard Worker }
703*9880d681SAndroid Build Coastguard Worker
704*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
705*9880d681SAndroid Build Coastguard Worker // Helpers for working with extended types.
706*9880d681SAndroid Build Coastguard Worker
707*9880d681SAndroid Build Coastguard Worker /// Dependent variable map for CodeGenDAGPattern variant generation
708*9880d681SAndroid Build Coastguard Worker typedef std::map<std::string, int> DepVarMap;
709*9880d681SAndroid Build Coastguard Worker
FindDepVarsOf(TreePatternNode * N,DepVarMap & DepMap)710*9880d681SAndroid Build Coastguard Worker static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
711*9880d681SAndroid Build Coastguard Worker if (N->isLeaf()) {
712*9880d681SAndroid Build Coastguard Worker if (isa<DefInit>(N->getLeafValue()))
713*9880d681SAndroid Build Coastguard Worker DepMap[N->getName()]++;
714*9880d681SAndroid Build Coastguard Worker } else {
715*9880d681SAndroid Build Coastguard Worker for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
716*9880d681SAndroid Build Coastguard Worker FindDepVarsOf(N->getChild(i), DepMap);
717*9880d681SAndroid Build Coastguard Worker }
718*9880d681SAndroid Build Coastguard Worker }
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard Worker /// Find dependent variables within child patterns
FindDepVars(TreePatternNode * N,MultipleUseVarSet & DepVars)721*9880d681SAndroid Build Coastguard Worker static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
722*9880d681SAndroid Build Coastguard Worker DepVarMap depcounts;
723*9880d681SAndroid Build Coastguard Worker FindDepVarsOf(N, depcounts);
724*9880d681SAndroid Build Coastguard Worker for (const std::pair<std::string, int> &Pair : depcounts) {
725*9880d681SAndroid Build Coastguard Worker if (Pair.second > 1)
726*9880d681SAndroid Build Coastguard Worker DepVars.insert(Pair.first);
727*9880d681SAndroid Build Coastguard Worker }
728*9880d681SAndroid Build Coastguard Worker }
729*9880d681SAndroid Build Coastguard Worker
730*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
731*9880d681SAndroid Build Coastguard Worker /// Dump the dependent variable set:
DumpDepVars(MultipleUseVarSet & DepVars)732*9880d681SAndroid Build Coastguard Worker static void DumpDepVars(MultipleUseVarSet &DepVars) {
733*9880d681SAndroid Build Coastguard Worker if (DepVars.empty()) {
734*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "<empty set>");
735*9880d681SAndroid Build Coastguard Worker } else {
736*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "[ ");
737*9880d681SAndroid Build Coastguard Worker for (const std::string &DepVar : DepVars) {
738*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << DepVar << " ");
739*9880d681SAndroid Build Coastguard Worker }
740*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "]");
741*9880d681SAndroid Build Coastguard Worker }
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker #endif
744*9880d681SAndroid Build Coastguard Worker
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
747*9880d681SAndroid Build Coastguard Worker // TreePredicateFn Implementation
748*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
749*9880d681SAndroid Build Coastguard Worker
750*9880d681SAndroid Build Coastguard Worker /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
TreePredicateFn(TreePattern * N)751*9880d681SAndroid Build Coastguard Worker TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) {
752*9880d681SAndroid Build Coastguard Worker assert((getPredCode().empty() || getImmCode().empty()) &&
753*9880d681SAndroid Build Coastguard Worker ".td file corrupt: can't have a node predicate *and* an imm predicate");
754*9880d681SAndroid Build Coastguard Worker }
755*9880d681SAndroid Build Coastguard Worker
getPredCode() const756*9880d681SAndroid Build Coastguard Worker std::string TreePredicateFn::getPredCode() const {
757*9880d681SAndroid Build Coastguard Worker return PatFragRec->getRecord()->getValueAsString("PredicateCode");
758*9880d681SAndroid Build Coastguard Worker }
759*9880d681SAndroid Build Coastguard Worker
getImmCode() const760*9880d681SAndroid Build Coastguard Worker std::string TreePredicateFn::getImmCode() const {
761*9880d681SAndroid Build Coastguard Worker return PatFragRec->getRecord()->getValueAsString("ImmediateCode");
762*9880d681SAndroid Build Coastguard Worker }
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard Worker
765*9880d681SAndroid Build Coastguard Worker /// isAlwaysTrue - Return true if this is a noop predicate.
isAlwaysTrue() const766*9880d681SAndroid Build Coastguard Worker bool TreePredicateFn::isAlwaysTrue() const {
767*9880d681SAndroid Build Coastguard Worker return getPredCode().empty() && getImmCode().empty();
768*9880d681SAndroid Build Coastguard Worker }
769*9880d681SAndroid Build Coastguard Worker
770*9880d681SAndroid Build Coastguard Worker /// Return the name to use in the generated code to reference this, this is
771*9880d681SAndroid Build Coastguard Worker /// "Predicate_foo" if from a pattern fragment "foo".
getFnName() const772*9880d681SAndroid Build Coastguard Worker std::string TreePredicateFn::getFnName() const {
773*9880d681SAndroid Build Coastguard Worker return "Predicate_" + PatFragRec->getRecord()->getName();
774*9880d681SAndroid Build Coastguard Worker }
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard Worker /// getCodeToRunOnSDNode - Return the code for the function body that
777*9880d681SAndroid Build Coastguard Worker /// evaluates this predicate. The argument is expected to be in "Node",
778*9880d681SAndroid Build Coastguard Worker /// not N. This handles casting and conversion to a concrete node type as
779*9880d681SAndroid Build Coastguard Worker /// appropriate.
getCodeToRunOnSDNode() const780*9880d681SAndroid Build Coastguard Worker std::string TreePredicateFn::getCodeToRunOnSDNode() const {
781*9880d681SAndroid Build Coastguard Worker // Handle immediate predicates first.
782*9880d681SAndroid Build Coastguard Worker std::string ImmCode = getImmCode();
783*9880d681SAndroid Build Coastguard Worker if (!ImmCode.empty()) {
784*9880d681SAndroid Build Coastguard Worker std::string Result =
785*9880d681SAndroid Build Coastguard Worker " int64_t Imm = cast<ConstantSDNode>(Node)->getSExtValue();\n";
786*9880d681SAndroid Build Coastguard Worker return Result + ImmCode;
787*9880d681SAndroid Build Coastguard Worker }
788*9880d681SAndroid Build Coastguard Worker
789*9880d681SAndroid Build Coastguard Worker // Handle arbitrary node predicates.
790*9880d681SAndroid Build Coastguard Worker assert(!getPredCode().empty() && "Don't have any predicate code!");
791*9880d681SAndroid Build Coastguard Worker std::string ClassName;
792*9880d681SAndroid Build Coastguard Worker if (PatFragRec->getOnlyTree()->isLeaf())
793*9880d681SAndroid Build Coastguard Worker ClassName = "SDNode";
794*9880d681SAndroid Build Coastguard Worker else {
795*9880d681SAndroid Build Coastguard Worker Record *Op = PatFragRec->getOnlyTree()->getOperator();
796*9880d681SAndroid Build Coastguard Worker ClassName = PatFragRec->getDAGPatterns().getSDNodeInfo(Op).getSDClassName();
797*9880d681SAndroid Build Coastguard Worker }
798*9880d681SAndroid Build Coastguard Worker std::string Result;
799*9880d681SAndroid Build Coastguard Worker if (ClassName == "SDNode")
800*9880d681SAndroid Build Coastguard Worker Result = " SDNode *N = Node;\n";
801*9880d681SAndroid Build Coastguard Worker else
802*9880d681SAndroid Build Coastguard Worker Result = " auto *N = cast<" + ClassName + ">(Node);\n";
803*9880d681SAndroid Build Coastguard Worker
804*9880d681SAndroid Build Coastguard Worker return Result + getPredCode();
805*9880d681SAndroid Build Coastguard Worker }
806*9880d681SAndroid Build Coastguard Worker
807*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
808*9880d681SAndroid Build Coastguard Worker // PatternToMatch implementation
809*9880d681SAndroid Build Coastguard Worker //
810*9880d681SAndroid Build Coastguard Worker
811*9880d681SAndroid Build Coastguard Worker
812*9880d681SAndroid Build Coastguard Worker /// getPatternSize - Return the 'size' of this pattern. We want to match large
813*9880d681SAndroid Build Coastguard Worker /// patterns before small ones. This is used to determine the size of a
814*9880d681SAndroid Build Coastguard Worker /// pattern.
getPatternSize(const TreePatternNode * P,const CodeGenDAGPatterns & CGP)815*9880d681SAndroid Build Coastguard Worker static unsigned getPatternSize(const TreePatternNode *P,
816*9880d681SAndroid Build Coastguard Worker const CodeGenDAGPatterns &CGP) {
817*9880d681SAndroid Build Coastguard Worker unsigned Size = 3; // The node itself.
818*9880d681SAndroid Build Coastguard Worker // If the root node is a ConstantSDNode, increases its size.
819*9880d681SAndroid Build Coastguard Worker // e.g. (set R32:$dst, 0).
820*9880d681SAndroid Build Coastguard Worker if (P->isLeaf() && isa<IntInit>(P->getLeafValue()))
821*9880d681SAndroid Build Coastguard Worker Size += 2;
822*9880d681SAndroid Build Coastguard Worker
823*9880d681SAndroid Build Coastguard Worker // FIXME: This is a hack to statically increase the priority of patterns
824*9880d681SAndroid Build Coastguard Worker // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
825*9880d681SAndroid Build Coastguard Worker // Later we can allow complexity / cost for each pattern to be (optionally)
826*9880d681SAndroid Build Coastguard Worker // specified. To get best possible pattern match we'll need to dynamically
827*9880d681SAndroid Build Coastguard Worker // calculate the complexity of all patterns a dag can potentially map to.
828*9880d681SAndroid Build Coastguard Worker const ComplexPattern *AM = P->getComplexPatternInfo(CGP);
829*9880d681SAndroid Build Coastguard Worker if (AM) {
830*9880d681SAndroid Build Coastguard Worker Size += AM->getNumOperands() * 3;
831*9880d681SAndroid Build Coastguard Worker
832*9880d681SAndroid Build Coastguard Worker // We don't want to count any children twice, so return early.
833*9880d681SAndroid Build Coastguard Worker return Size;
834*9880d681SAndroid Build Coastguard Worker }
835*9880d681SAndroid Build Coastguard Worker
836*9880d681SAndroid Build Coastguard Worker // If this node has some predicate function that must match, it adds to the
837*9880d681SAndroid Build Coastguard Worker // complexity of this node.
838*9880d681SAndroid Build Coastguard Worker if (!P->getPredicateFns().empty())
839*9880d681SAndroid Build Coastguard Worker ++Size;
840*9880d681SAndroid Build Coastguard Worker
841*9880d681SAndroid Build Coastguard Worker // Count children in the count if they are also nodes.
842*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
843*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = P->getChild(i);
844*9880d681SAndroid Build Coastguard Worker if (!Child->isLeaf() && Child->getNumTypes() &&
845*9880d681SAndroid Build Coastguard Worker Child->getType(0) != MVT::Other)
846*9880d681SAndroid Build Coastguard Worker Size += getPatternSize(Child, CGP);
847*9880d681SAndroid Build Coastguard Worker else if (Child->isLeaf()) {
848*9880d681SAndroid Build Coastguard Worker if (isa<IntInit>(Child->getLeafValue()))
849*9880d681SAndroid Build Coastguard Worker Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
850*9880d681SAndroid Build Coastguard Worker else if (Child->getComplexPatternInfo(CGP))
851*9880d681SAndroid Build Coastguard Worker Size += getPatternSize(Child, CGP);
852*9880d681SAndroid Build Coastguard Worker else if (!Child->getPredicateFns().empty())
853*9880d681SAndroid Build Coastguard Worker ++Size;
854*9880d681SAndroid Build Coastguard Worker }
855*9880d681SAndroid Build Coastguard Worker }
856*9880d681SAndroid Build Coastguard Worker
857*9880d681SAndroid Build Coastguard Worker return Size;
858*9880d681SAndroid Build Coastguard Worker }
859*9880d681SAndroid Build Coastguard Worker
860*9880d681SAndroid Build Coastguard Worker /// Compute the complexity metric for the input pattern. This roughly
861*9880d681SAndroid Build Coastguard Worker /// corresponds to the number of nodes that are covered.
862*9880d681SAndroid Build Coastguard Worker int PatternToMatch::
getPatternComplexity(const CodeGenDAGPatterns & CGP) const863*9880d681SAndroid Build Coastguard Worker getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
864*9880d681SAndroid Build Coastguard Worker return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
865*9880d681SAndroid Build Coastguard Worker }
866*9880d681SAndroid Build Coastguard Worker
867*9880d681SAndroid Build Coastguard Worker
868*9880d681SAndroid Build Coastguard Worker /// getPredicateCheck - Return a single string containing all of this
869*9880d681SAndroid Build Coastguard Worker /// pattern's predicates concatenated with "&&" operators.
870*9880d681SAndroid Build Coastguard Worker ///
getPredicateCheck() const871*9880d681SAndroid Build Coastguard Worker std::string PatternToMatch::getPredicateCheck() const {
872*9880d681SAndroid Build Coastguard Worker SmallVector<Record *, 4> PredicateRecs;
873*9880d681SAndroid Build Coastguard Worker for (Init *I : Predicates->getValues()) {
874*9880d681SAndroid Build Coastguard Worker if (DefInit *Pred = dyn_cast<DefInit>(I)) {
875*9880d681SAndroid Build Coastguard Worker Record *Def = Pred->getDef();
876*9880d681SAndroid Build Coastguard Worker if (!Def->isSubClassOf("Predicate")) {
877*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
878*9880d681SAndroid Build Coastguard Worker Def->dump();
879*9880d681SAndroid Build Coastguard Worker #endif
880*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unknown predicate type!");
881*9880d681SAndroid Build Coastguard Worker }
882*9880d681SAndroid Build Coastguard Worker PredicateRecs.push_back(Def);
883*9880d681SAndroid Build Coastguard Worker }
884*9880d681SAndroid Build Coastguard Worker }
885*9880d681SAndroid Build Coastguard Worker // Sort so that different orders get canonicalized to the same string.
886*9880d681SAndroid Build Coastguard Worker std::sort(PredicateRecs.begin(), PredicateRecs.end(), LessRecord());
887*9880d681SAndroid Build Coastguard Worker
888*9880d681SAndroid Build Coastguard Worker SmallString<128> PredicateCheck;
889*9880d681SAndroid Build Coastguard Worker for (Record *Pred : PredicateRecs) {
890*9880d681SAndroid Build Coastguard Worker if (!PredicateCheck.empty())
891*9880d681SAndroid Build Coastguard Worker PredicateCheck += " && ";
892*9880d681SAndroid Build Coastguard Worker PredicateCheck += "(" + Pred->getValueAsString("CondString") + ")";
893*9880d681SAndroid Build Coastguard Worker }
894*9880d681SAndroid Build Coastguard Worker
895*9880d681SAndroid Build Coastguard Worker return PredicateCheck.str();
896*9880d681SAndroid Build Coastguard Worker }
897*9880d681SAndroid Build Coastguard Worker
898*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
899*9880d681SAndroid Build Coastguard Worker // SDTypeConstraint implementation
900*9880d681SAndroid Build Coastguard Worker //
901*9880d681SAndroid Build Coastguard Worker
SDTypeConstraint(Record * R)902*9880d681SAndroid Build Coastguard Worker SDTypeConstraint::SDTypeConstraint(Record *R) {
903*9880d681SAndroid Build Coastguard Worker OperandNo = R->getValueAsInt("OperandNum");
904*9880d681SAndroid Build Coastguard Worker
905*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("SDTCisVT")) {
906*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisVT;
907*9880d681SAndroid Build Coastguard Worker x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT"));
908*9880d681SAndroid Build Coastguard Worker if (x.SDTCisVT_Info.VT == MVT::isVoid)
909*9880d681SAndroid Build Coastguard Worker PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT");
910*9880d681SAndroid Build Coastguard Worker
911*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisPtrTy")) {
912*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisPtrTy;
913*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisInt")) {
914*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisInt;
915*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisFP")) {
916*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisFP;
917*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisVec")) {
918*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisVec;
919*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisSameAs")) {
920*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisSameAs;
921*9880d681SAndroid Build Coastguard Worker x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum");
922*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) {
923*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisVTSmallerThanOp;
924*9880d681SAndroid Build Coastguard Worker x.SDTCisVTSmallerThanOp_Info.OtherOperandNum =
925*9880d681SAndroid Build Coastguard Worker R->getValueAsInt("OtherOperandNum");
926*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) {
927*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisOpSmallerThanOp;
928*9880d681SAndroid Build Coastguard Worker x.SDTCisOpSmallerThanOp_Info.BigOperandNum =
929*9880d681SAndroid Build Coastguard Worker R->getValueAsInt("BigOperandNum");
930*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisEltOfVec")) {
931*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisEltOfVec;
932*9880d681SAndroid Build Coastguard Worker x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum");
933*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisSubVecOfVec")) {
934*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisSubVecOfVec;
935*9880d681SAndroid Build Coastguard Worker x.SDTCisSubVecOfVec_Info.OtherOperandNum =
936*9880d681SAndroid Build Coastguard Worker R->getValueAsInt("OtherOpNum");
937*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCVecEltisVT")) {
938*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCVecEltisVT;
939*9880d681SAndroid Build Coastguard Worker x.SDTCVecEltisVT_Info.VT = getValueType(R->getValueAsDef("VT"));
940*9880d681SAndroid Build Coastguard Worker if (MVT(x.SDTCVecEltisVT_Info.VT).isVector())
941*9880d681SAndroid Build Coastguard Worker PrintFatalError(R->getLoc(), "Cannot use vector type as SDTCVecEltisVT");
942*9880d681SAndroid Build Coastguard Worker if (!MVT(x.SDTCVecEltisVT_Info.VT).isInteger() &&
943*9880d681SAndroid Build Coastguard Worker !MVT(x.SDTCVecEltisVT_Info.VT).isFloatingPoint())
944*9880d681SAndroid Build Coastguard Worker PrintFatalError(R->getLoc(), "Must use integer or floating point type "
945*9880d681SAndroid Build Coastguard Worker "as SDTCVecEltisVT");
946*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisSameNumEltsAs")) {
947*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisSameNumEltsAs;
948*9880d681SAndroid Build Coastguard Worker x.SDTCisSameNumEltsAs_Info.OtherOperandNum =
949*9880d681SAndroid Build Coastguard Worker R->getValueAsInt("OtherOperandNum");
950*9880d681SAndroid Build Coastguard Worker } else if (R->isSubClassOf("SDTCisSameSizeAs")) {
951*9880d681SAndroid Build Coastguard Worker ConstraintType = SDTCisSameSizeAs;
952*9880d681SAndroid Build Coastguard Worker x.SDTCisSameSizeAs_Info.OtherOperandNum =
953*9880d681SAndroid Build Coastguard Worker R->getValueAsInt("OtherOperandNum");
954*9880d681SAndroid Build Coastguard Worker } else {
955*9880d681SAndroid Build Coastguard Worker PrintFatalError("Unrecognized SDTypeConstraint '" + R->getName() + "'!\n");
956*9880d681SAndroid Build Coastguard Worker }
957*9880d681SAndroid Build Coastguard Worker }
958*9880d681SAndroid Build Coastguard Worker
959*9880d681SAndroid Build Coastguard Worker /// getOperandNum - Return the node corresponding to operand #OpNo in tree
960*9880d681SAndroid Build Coastguard Worker /// N, and the result number in ResNo.
getOperandNum(unsigned OpNo,TreePatternNode * N,const SDNodeInfo & NodeInfo,unsigned & ResNo)961*9880d681SAndroid Build Coastguard Worker static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
962*9880d681SAndroid Build Coastguard Worker const SDNodeInfo &NodeInfo,
963*9880d681SAndroid Build Coastguard Worker unsigned &ResNo) {
964*9880d681SAndroid Build Coastguard Worker unsigned NumResults = NodeInfo.getNumResults();
965*9880d681SAndroid Build Coastguard Worker if (OpNo < NumResults) {
966*9880d681SAndroid Build Coastguard Worker ResNo = OpNo;
967*9880d681SAndroid Build Coastguard Worker return N;
968*9880d681SAndroid Build Coastguard Worker }
969*9880d681SAndroid Build Coastguard Worker
970*9880d681SAndroid Build Coastguard Worker OpNo -= NumResults;
971*9880d681SAndroid Build Coastguard Worker
972*9880d681SAndroid Build Coastguard Worker if (OpNo >= N->getNumChildren()) {
973*9880d681SAndroid Build Coastguard Worker std::string S;
974*9880d681SAndroid Build Coastguard Worker raw_string_ostream OS(S);
975*9880d681SAndroid Build Coastguard Worker OS << "Invalid operand number in type constraint "
976*9880d681SAndroid Build Coastguard Worker << (OpNo+NumResults) << " ";
977*9880d681SAndroid Build Coastguard Worker N->print(OS);
978*9880d681SAndroid Build Coastguard Worker PrintFatalError(OS.str());
979*9880d681SAndroid Build Coastguard Worker }
980*9880d681SAndroid Build Coastguard Worker
981*9880d681SAndroid Build Coastguard Worker return N->getChild(OpNo);
982*9880d681SAndroid Build Coastguard Worker }
983*9880d681SAndroid Build Coastguard Worker
984*9880d681SAndroid Build Coastguard Worker /// ApplyTypeConstraint - Given a node in a pattern, apply this type
985*9880d681SAndroid Build Coastguard Worker /// constraint to the nodes operands. This returns true if it makes a
986*9880d681SAndroid Build Coastguard Worker /// change, false otherwise. If a type contradiction is found, flag an error.
ApplyTypeConstraint(TreePatternNode * N,const SDNodeInfo & NodeInfo,TreePattern & TP) const987*9880d681SAndroid Build Coastguard Worker bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
988*9880d681SAndroid Build Coastguard Worker const SDNodeInfo &NodeInfo,
989*9880d681SAndroid Build Coastguard Worker TreePattern &TP) const {
990*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
991*9880d681SAndroid Build Coastguard Worker return false;
992*9880d681SAndroid Build Coastguard Worker
993*9880d681SAndroid Build Coastguard Worker unsigned ResNo = 0; // The result number being referenced.
994*9880d681SAndroid Build Coastguard Worker TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo);
995*9880d681SAndroid Build Coastguard Worker
996*9880d681SAndroid Build Coastguard Worker switch (ConstraintType) {
997*9880d681SAndroid Build Coastguard Worker case SDTCisVT:
998*9880d681SAndroid Build Coastguard Worker // Operand must be a particular type.
999*9880d681SAndroid Build Coastguard Worker return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP);
1000*9880d681SAndroid Build Coastguard Worker case SDTCisPtrTy:
1001*9880d681SAndroid Build Coastguard Worker // Operand must be same as target pointer type.
1002*9880d681SAndroid Build Coastguard Worker return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP);
1003*9880d681SAndroid Build Coastguard Worker case SDTCisInt:
1004*9880d681SAndroid Build Coastguard Worker // Require it to be one of the legal integer VTs.
1005*9880d681SAndroid Build Coastguard Worker return NodeToApply->getExtType(ResNo).EnforceInteger(TP);
1006*9880d681SAndroid Build Coastguard Worker case SDTCisFP:
1007*9880d681SAndroid Build Coastguard Worker // Require it to be one of the legal fp VTs.
1008*9880d681SAndroid Build Coastguard Worker return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP);
1009*9880d681SAndroid Build Coastguard Worker case SDTCisVec:
1010*9880d681SAndroid Build Coastguard Worker // Require it to be one of the legal vector VTs.
1011*9880d681SAndroid Build Coastguard Worker return NodeToApply->getExtType(ResNo).EnforceVector(TP);
1012*9880d681SAndroid Build Coastguard Worker case SDTCisSameAs: {
1013*9880d681SAndroid Build Coastguard Worker unsigned OResNo = 0;
1014*9880d681SAndroid Build Coastguard Worker TreePatternNode *OtherNode =
1015*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo);
1016*9880d681SAndroid Build Coastguard Worker return NodeToApply->UpdateNodeType(ResNo, OtherNode->getExtType(OResNo),TP)|
1017*9880d681SAndroid Build Coastguard Worker OtherNode->UpdateNodeType(OResNo,NodeToApply->getExtType(ResNo),TP);
1018*9880d681SAndroid Build Coastguard Worker }
1019*9880d681SAndroid Build Coastguard Worker case SDTCisVTSmallerThanOp: {
1020*9880d681SAndroid Build Coastguard Worker // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must
1021*9880d681SAndroid Build Coastguard Worker // have an integer type that is smaller than the VT.
1022*9880d681SAndroid Build Coastguard Worker if (!NodeToApply->isLeaf() ||
1023*9880d681SAndroid Build Coastguard Worker !isa<DefInit>(NodeToApply->getLeafValue()) ||
1024*9880d681SAndroid Build Coastguard Worker !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()
1025*9880d681SAndroid Build Coastguard Worker ->isSubClassOf("ValueType")) {
1026*9880d681SAndroid Build Coastguard Worker TP.error(N->getOperator()->getName() + " expects a VT operand!");
1027*9880d681SAndroid Build Coastguard Worker return false;
1028*9880d681SAndroid Build Coastguard Worker }
1029*9880d681SAndroid Build Coastguard Worker MVT::SimpleValueType VT =
1030*9880d681SAndroid Build Coastguard Worker getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef());
1031*9880d681SAndroid Build Coastguard Worker
1032*9880d681SAndroid Build Coastguard Worker EEVT::TypeSet TypeListTmp(VT, TP);
1033*9880d681SAndroid Build Coastguard Worker
1034*9880d681SAndroid Build Coastguard Worker unsigned OResNo = 0;
1035*9880d681SAndroid Build Coastguard Worker TreePatternNode *OtherNode =
1036*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo,
1037*9880d681SAndroid Build Coastguard Worker OResNo);
1038*9880d681SAndroid Build Coastguard Worker
1039*9880d681SAndroid Build Coastguard Worker return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP);
1040*9880d681SAndroid Build Coastguard Worker }
1041*9880d681SAndroid Build Coastguard Worker case SDTCisOpSmallerThanOp: {
1042*9880d681SAndroid Build Coastguard Worker unsigned BResNo = 0;
1043*9880d681SAndroid Build Coastguard Worker TreePatternNode *BigOperand =
1044*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo,
1045*9880d681SAndroid Build Coastguard Worker BResNo);
1046*9880d681SAndroid Build Coastguard Worker return NodeToApply->getExtType(ResNo).
1047*9880d681SAndroid Build Coastguard Worker EnforceSmallerThan(BigOperand->getExtType(BResNo), TP);
1048*9880d681SAndroid Build Coastguard Worker }
1049*9880d681SAndroid Build Coastguard Worker case SDTCisEltOfVec: {
1050*9880d681SAndroid Build Coastguard Worker unsigned VResNo = 0;
1051*9880d681SAndroid Build Coastguard Worker TreePatternNode *VecOperand =
1052*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo,
1053*9880d681SAndroid Build Coastguard Worker VResNo);
1054*9880d681SAndroid Build Coastguard Worker
1055*9880d681SAndroid Build Coastguard Worker // Filter vector types out of VecOperand that don't have the right element
1056*9880d681SAndroid Build Coastguard Worker // type.
1057*9880d681SAndroid Build Coastguard Worker return VecOperand->getExtType(VResNo).
1058*9880d681SAndroid Build Coastguard Worker EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP);
1059*9880d681SAndroid Build Coastguard Worker }
1060*9880d681SAndroid Build Coastguard Worker case SDTCisSubVecOfVec: {
1061*9880d681SAndroid Build Coastguard Worker unsigned VResNo = 0;
1062*9880d681SAndroid Build Coastguard Worker TreePatternNode *BigVecOperand =
1063*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo,
1064*9880d681SAndroid Build Coastguard Worker VResNo);
1065*9880d681SAndroid Build Coastguard Worker
1066*9880d681SAndroid Build Coastguard Worker // Filter vector types out of BigVecOperand that don't have the
1067*9880d681SAndroid Build Coastguard Worker // right subvector type.
1068*9880d681SAndroid Build Coastguard Worker return BigVecOperand->getExtType(VResNo).
1069*9880d681SAndroid Build Coastguard Worker EnforceVectorSubVectorTypeIs(NodeToApply->getExtType(ResNo), TP);
1070*9880d681SAndroid Build Coastguard Worker }
1071*9880d681SAndroid Build Coastguard Worker case SDTCVecEltisVT: {
1072*9880d681SAndroid Build Coastguard Worker return NodeToApply->getExtType(ResNo).
1073*9880d681SAndroid Build Coastguard Worker EnforceVectorEltTypeIs(x.SDTCVecEltisVT_Info.VT, TP);
1074*9880d681SAndroid Build Coastguard Worker }
1075*9880d681SAndroid Build Coastguard Worker case SDTCisSameNumEltsAs: {
1076*9880d681SAndroid Build Coastguard Worker unsigned OResNo = 0;
1077*9880d681SAndroid Build Coastguard Worker TreePatternNode *OtherNode =
1078*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum,
1079*9880d681SAndroid Build Coastguard Worker N, NodeInfo, OResNo);
1080*9880d681SAndroid Build Coastguard Worker return OtherNode->getExtType(OResNo).
1081*9880d681SAndroid Build Coastguard Worker EnforceVectorSameNumElts(NodeToApply->getExtType(ResNo), TP);
1082*9880d681SAndroid Build Coastguard Worker }
1083*9880d681SAndroid Build Coastguard Worker case SDTCisSameSizeAs: {
1084*9880d681SAndroid Build Coastguard Worker unsigned OResNo = 0;
1085*9880d681SAndroid Build Coastguard Worker TreePatternNode *OtherNode =
1086*9880d681SAndroid Build Coastguard Worker getOperandNum(x.SDTCisSameSizeAs_Info.OtherOperandNum,
1087*9880d681SAndroid Build Coastguard Worker N, NodeInfo, OResNo);
1088*9880d681SAndroid Build Coastguard Worker return OtherNode->getExtType(OResNo).
1089*9880d681SAndroid Build Coastguard Worker EnforceSameSize(NodeToApply->getExtType(ResNo), TP);
1090*9880d681SAndroid Build Coastguard Worker }
1091*9880d681SAndroid Build Coastguard Worker }
1092*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid ConstraintType!");
1093*9880d681SAndroid Build Coastguard Worker }
1094*9880d681SAndroid Build Coastguard Worker
1095*9880d681SAndroid Build Coastguard Worker // Update the node type to match an instruction operand or result as specified
1096*9880d681SAndroid Build Coastguard Worker // in the ins or outs lists on the instruction definition. Return true if the
1097*9880d681SAndroid Build Coastguard Worker // type was actually changed.
UpdateNodeTypeFromInst(unsigned ResNo,Record * Operand,TreePattern & TP)1098*9880d681SAndroid Build Coastguard Worker bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
1099*9880d681SAndroid Build Coastguard Worker Record *Operand,
1100*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
1101*9880d681SAndroid Build Coastguard Worker // The 'unknown' operand indicates that types should be inferred from the
1102*9880d681SAndroid Build Coastguard Worker // context.
1103*9880d681SAndroid Build Coastguard Worker if (Operand->isSubClassOf("unknown_class"))
1104*9880d681SAndroid Build Coastguard Worker return false;
1105*9880d681SAndroid Build Coastguard Worker
1106*9880d681SAndroid Build Coastguard Worker // The Operand class specifies a type directly.
1107*9880d681SAndroid Build Coastguard Worker if (Operand->isSubClassOf("Operand"))
1108*9880d681SAndroid Build Coastguard Worker return UpdateNodeType(ResNo, getValueType(Operand->getValueAsDef("Type")),
1109*9880d681SAndroid Build Coastguard Worker TP);
1110*9880d681SAndroid Build Coastguard Worker
1111*9880d681SAndroid Build Coastguard Worker // PointerLikeRegClass has a type that is determined at runtime.
1112*9880d681SAndroid Build Coastguard Worker if (Operand->isSubClassOf("PointerLikeRegClass"))
1113*9880d681SAndroid Build Coastguard Worker return UpdateNodeType(ResNo, MVT::iPTR, TP);
1114*9880d681SAndroid Build Coastguard Worker
1115*9880d681SAndroid Build Coastguard Worker // Both RegisterClass and RegisterOperand operands derive their types from a
1116*9880d681SAndroid Build Coastguard Worker // register class def.
1117*9880d681SAndroid Build Coastguard Worker Record *RC = nullptr;
1118*9880d681SAndroid Build Coastguard Worker if (Operand->isSubClassOf("RegisterClass"))
1119*9880d681SAndroid Build Coastguard Worker RC = Operand;
1120*9880d681SAndroid Build Coastguard Worker else if (Operand->isSubClassOf("RegisterOperand"))
1121*9880d681SAndroid Build Coastguard Worker RC = Operand->getValueAsDef("RegClass");
1122*9880d681SAndroid Build Coastguard Worker
1123*9880d681SAndroid Build Coastguard Worker assert(RC && "Unknown operand type");
1124*9880d681SAndroid Build Coastguard Worker CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo();
1125*9880d681SAndroid Build Coastguard Worker return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP);
1126*9880d681SAndroid Build Coastguard Worker }
1127*9880d681SAndroid Build Coastguard Worker
1128*9880d681SAndroid Build Coastguard Worker
1129*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
1130*9880d681SAndroid Build Coastguard Worker // SDNodeInfo implementation
1131*9880d681SAndroid Build Coastguard Worker //
SDNodeInfo(Record * R)1132*9880d681SAndroid Build Coastguard Worker SDNodeInfo::SDNodeInfo(Record *R) : Def(R) {
1133*9880d681SAndroid Build Coastguard Worker EnumName = R->getValueAsString("Opcode");
1134*9880d681SAndroid Build Coastguard Worker SDClassName = R->getValueAsString("SDClass");
1135*9880d681SAndroid Build Coastguard Worker Record *TypeProfile = R->getValueAsDef("TypeProfile");
1136*9880d681SAndroid Build Coastguard Worker NumResults = TypeProfile->getValueAsInt("NumResults");
1137*9880d681SAndroid Build Coastguard Worker NumOperands = TypeProfile->getValueAsInt("NumOperands");
1138*9880d681SAndroid Build Coastguard Worker
1139*9880d681SAndroid Build Coastguard Worker // Parse the properties.
1140*9880d681SAndroid Build Coastguard Worker Properties = 0;
1141*9880d681SAndroid Build Coastguard Worker for (Record *Property : R->getValueAsListOfDefs("Properties")) {
1142*9880d681SAndroid Build Coastguard Worker if (Property->getName() == "SDNPCommutative") {
1143*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPCommutative;
1144*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPAssociative") {
1145*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPAssociative;
1146*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPHasChain") {
1147*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPHasChain;
1148*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPOutGlue") {
1149*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPOutGlue;
1150*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPInGlue") {
1151*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPInGlue;
1152*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPOptInGlue") {
1153*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPOptInGlue;
1154*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPMayStore") {
1155*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPMayStore;
1156*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPMayLoad") {
1157*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPMayLoad;
1158*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPSideEffect") {
1159*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPSideEffect;
1160*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPMemOperand") {
1161*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPMemOperand;
1162*9880d681SAndroid Build Coastguard Worker } else if (Property->getName() == "SDNPVariadic") {
1163*9880d681SAndroid Build Coastguard Worker Properties |= 1 << SDNPVariadic;
1164*9880d681SAndroid Build Coastguard Worker } else {
1165*9880d681SAndroid Build Coastguard Worker PrintFatalError("Unknown SD Node property '" +
1166*9880d681SAndroid Build Coastguard Worker Property->getName() + "' on node '" +
1167*9880d681SAndroid Build Coastguard Worker R->getName() + "'!");
1168*9880d681SAndroid Build Coastguard Worker }
1169*9880d681SAndroid Build Coastguard Worker }
1170*9880d681SAndroid Build Coastguard Worker
1171*9880d681SAndroid Build Coastguard Worker
1172*9880d681SAndroid Build Coastguard Worker // Parse the type constraints.
1173*9880d681SAndroid Build Coastguard Worker std::vector<Record*> ConstraintList =
1174*9880d681SAndroid Build Coastguard Worker TypeProfile->getValueAsListOfDefs("Constraints");
1175*9880d681SAndroid Build Coastguard Worker TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end());
1176*9880d681SAndroid Build Coastguard Worker }
1177*9880d681SAndroid Build Coastguard Worker
1178*9880d681SAndroid Build Coastguard Worker /// getKnownType - If the type constraints on this node imply a fixed type
1179*9880d681SAndroid Build Coastguard Worker /// (e.g. all stores return void, etc), then return it as an
1180*9880d681SAndroid Build Coastguard Worker /// MVT::SimpleValueType. Otherwise, return EEVT::Other.
getKnownType(unsigned ResNo) const1181*9880d681SAndroid Build Coastguard Worker MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
1182*9880d681SAndroid Build Coastguard Worker unsigned NumResults = getNumResults();
1183*9880d681SAndroid Build Coastguard Worker assert(NumResults <= 1 &&
1184*9880d681SAndroid Build Coastguard Worker "We only work with nodes with zero or one result so far!");
1185*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "Only handles single result nodes so far");
1186*9880d681SAndroid Build Coastguard Worker
1187*9880d681SAndroid Build Coastguard Worker for (const SDTypeConstraint &Constraint : TypeConstraints) {
1188*9880d681SAndroid Build Coastguard Worker // Make sure that this applies to the correct node result.
1189*9880d681SAndroid Build Coastguard Worker if (Constraint.OperandNo >= NumResults) // FIXME: need value #
1190*9880d681SAndroid Build Coastguard Worker continue;
1191*9880d681SAndroid Build Coastguard Worker
1192*9880d681SAndroid Build Coastguard Worker switch (Constraint.ConstraintType) {
1193*9880d681SAndroid Build Coastguard Worker default: break;
1194*9880d681SAndroid Build Coastguard Worker case SDTypeConstraint::SDTCisVT:
1195*9880d681SAndroid Build Coastguard Worker return Constraint.x.SDTCisVT_Info.VT;
1196*9880d681SAndroid Build Coastguard Worker case SDTypeConstraint::SDTCisPtrTy:
1197*9880d681SAndroid Build Coastguard Worker return MVT::iPTR;
1198*9880d681SAndroid Build Coastguard Worker }
1199*9880d681SAndroid Build Coastguard Worker }
1200*9880d681SAndroid Build Coastguard Worker return MVT::Other;
1201*9880d681SAndroid Build Coastguard Worker }
1202*9880d681SAndroid Build Coastguard Worker
1203*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
1204*9880d681SAndroid Build Coastguard Worker // TreePatternNode implementation
1205*9880d681SAndroid Build Coastguard Worker //
1206*9880d681SAndroid Build Coastguard Worker
~TreePatternNode()1207*9880d681SAndroid Build Coastguard Worker TreePatternNode::~TreePatternNode() {
1208*9880d681SAndroid Build Coastguard Worker #if 0 // FIXME: implement refcounted tree nodes!
1209*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1210*9880d681SAndroid Build Coastguard Worker delete getChild(i);
1211*9880d681SAndroid Build Coastguard Worker #endif
1212*9880d681SAndroid Build Coastguard Worker }
1213*9880d681SAndroid Build Coastguard Worker
GetNumNodeResults(Record * Operator,CodeGenDAGPatterns & CDP)1214*9880d681SAndroid Build Coastguard Worker static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
1215*9880d681SAndroid Build Coastguard Worker if (Operator->getName() == "set" ||
1216*9880d681SAndroid Build Coastguard Worker Operator->getName() == "implicit")
1217*9880d681SAndroid Build Coastguard Worker return 0; // All return nothing.
1218*9880d681SAndroid Build Coastguard Worker
1219*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("Intrinsic"))
1220*9880d681SAndroid Build Coastguard Worker return CDP.getIntrinsic(Operator).IS.RetVTs.size();
1221*9880d681SAndroid Build Coastguard Worker
1222*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("SDNode"))
1223*9880d681SAndroid Build Coastguard Worker return CDP.getSDNodeInfo(Operator).getNumResults();
1224*9880d681SAndroid Build Coastguard Worker
1225*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("PatFrag")) {
1226*9880d681SAndroid Build Coastguard Worker // If we've already parsed this pattern fragment, get it. Otherwise, handle
1227*9880d681SAndroid Build Coastguard Worker // the forward reference case where one pattern fragment references another
1228*9880d681SAndroid Build Coastguard Worker // before it is processed.
1229*9880d681SAndroid Build Coastguard Worker if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator))
1230*9880d681SAndroid Build Coastguard Worker return PFRec->getOnlyTree()->getNumTypes();
1231*9880d681SAndroid Build Coastguard Worker
1232*9880d681SAndroid Build Coastguard Worker // Get the result tree.
1233*9880d681SAndroid Build Coastguard Worker DagInit *Tree = Operator->getValueAsDag("Fragment");
1234*9880d681SAndroid Build Coastguard Worker Record *Op = nullptr;
1235*9880d681SAndroid Build Coastguard Worker if (Tree)
1236*9880d681SAndroid Build Coastguard Worker if (DefInit *DI = dyn_cast<DefInit>(Tree->getOperator()))
1237*9880d681SAndroid Build Coastguard Worker Op = DI->getDef();
1238*9880d681SAndroid Build Coastguard Worker assert(Op && "Invalid Fragment");
1239*9880d681SAndroid Build Coastguard Worker return GetNumNodeResults(Op, CDP);
1240*9880d681SAndroid Build Coastguard Worker }
1241*9880d681SAndroid Build Coastguard Worker
1242*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("Instruction")) {
1243*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator);
1244*9880d681SAndroid Build Coastguard Worker
1245*9880d681SAndroid Build Coastguard Worker unsigned NumDefsToAdd = InstInfo.Operands.NumDefs;
1246*9880d681SAndroid Build Coastguard Worker
1247*9880d681SAndroid Build Coastguard Worker // Subtract any defaulted outputs.
1248*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != InstInfo.Operands.NumDefs; ++i) {
1249*9880d681SAndroid Build Coastguard Worker Record *OperandNode = InstInfo.Operands[i].Rec;
1250*9880d681SAndroid Build Coastguard Worker
1251*9880d681SAndroid Build Coastguard Worker if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
1252*9880d681SAndroid Build Coastguard Worker !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1253*9880d681SAndroid Build Coastguard Worker --NumDefsToAdd;
1254*9880d681SAndroid Build Coastguard Worker }
1255*9880d681SAndroid Build Coastguard Worker
1256*9880d681SAndroid Build Coastguard Worker // Add on one implicit def if it has a resolvable type.
1257*9880d681SAndroid Build Coastguard Worker if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other)
1258*9880d681SAndroid Build Coastguard Worker ++NumDefsToAdd;
1259*9880d681SAndroid Build Coastguard Worker return NumDefsToAdd;
1260*9880d681SAndroid Build Coastguard Worker }
1261*9880d681SAndroid Build Coastguard Worker
1262*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("SDNodeXForm"))
1263*9880d681SAndroid Build Coastguard Worker return 1; // FIXME: Generalize SDNodeXForm
1264*9880d681SAndroid Build Coastguard Worker
1265*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("ValueType"))
1266*9880d681SAndroid Build Coastguard Worker return 1; // A type-cast of one result.
1267*9880d681SAndroid Build Coastguard Worker
1268*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("ComplexPattern"))
1269*9880d681SAndroid Build Coastguard Worker return 1;
1270*9880d681SAndroid Build Coastguard Worker
1271*9880d681SAndroid Build Coastguard Worker Operator->dump();
1272*9880d681SAndroid Build Coastguard Worker PrintFatalError("Unhandled node in GetNumNodeResults");
1273*9880d681SAndroid Build Coastguard Worker }
1274*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const1275*9880d681SAndroid Build Coastguard Worker void TreePatternNode::print(raw_ostream &OS) const {
1276*9880d681SAndroid Build Coastguard Worker if (isLeaf())
1277*9880d681SAndroid Build Coastguard Worker OS << *getLeafValue();
1278*9880d681SAndroid Build Coastguard Worker else
1279*9880d681SAndroid Build Coastguard Worker OS << '(' << getOperator()->getName();
1280*9880d681SAndroid Build Coastguard Worker
1281*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Types.size(); i != e; ++i)
1282*9880d681SAndroid Build Coastguard Worker OS << ':' << getExtType(i).getName();
1283*9880d681SAndroid Build Coastguard Worker
1284*9880d681SAndroid Build Coastguard Worker if (!isLeaf()) {
1285*9880d681SAndroid Build Coastguard Worker if (getNumChildren() != 0) {
1286*9880d681SAndroid Build Coastguard Worker OS << " ";
1287*9880d681SAndroid Build Coastguard Worker getChild(0)->print(OS);
1288*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = getNumChildren(); i != e; ++i) {
1289*9880d681SAndroid Build Coastguard Worker OS << ", ";
1290*9880d681SAndroid Build Coastguard Worker getChild(i)->print(OS);
1291*9880d681SAndroid Build Coastguard Worker }
1292*9880d681SAndroid Build Coastguard Worker }
1293*9880d681SAndroid Build Coastguard Worker OS << ")";
1294*9880d681SAndroid Build Coastguard Worker }
1295*9880d681SAndroid Build Coastguard Worker
1296*9880d681SAndroid Build Coastguard Worker for (const TreePredicateFn &Pred : PredicateFns)
1297*9880d681SAndroid Build Coastguard Worker OS << "<<P:" << Pred.getFnName() << ">>";
1298*9880d681SAndroid Build Coastguard Worker if (TransformFn)
1299*9880d681SAndroid Build Coastguard Worker OS << "<<X:" << TransformFn->getName() << ">>";
1300*9880d681SAndroid Build Coastguard Worker if (!getName().empty())
1301*9880d681SAndroid Build Coastguard Worker OS << ":$" << getName();
1302*9880d681SAndroid Build Coastguard Worker
1303*9880d681SAndroid Build Coastguard Worker }
dump() const1304*9880d681SAndroid Build Coastguard Worker void TreePatternNode::dump() const {
1305*9880d681SAndroid Build Coastguard Worker print(errs());
1306*9880d681SAndroid Build Coastguard Worker }
1307*9880d681SAndroid Build Coastguard Worker
1308*9880d681SAndroid Build Coastguard Worker /// isIsomorphicTo - Return true if this node is recursively
1309*9880d681SAndroid Build Coastguard Worker /// isomorphic to the specified node. For this comparison, the node's
1310*9880d681SAndroid Build Coastguard Worker /// entire state is considered. The assigned name is ignored, since
1311*9880d681SAndroid Build Coastguard Worker /// nodes with differing names are considered isomorphic. However, if
1312*9880d681SAndroid Build Coastguard Worker /// the assigned name is present in the dependent variable set, then
1313*9880d681SAndroid Build Coastguard Worker /// the assigned name is considered significant and the node is
1314*9880d681SAndroid Build Coastguard Worker /// isomorphic if the names match.
isIsomorphicTo(const TreePatternNode * N,const MultipleUseVarSet & DepVars) const1315*9880d681SAndroid Build Coastguard Worker bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N,
1316*9880d681SAndroid Build Coastguard Worker const MultipleUseVarSet &DepVars) const {
1317*9880d681SAndroid Build Coastguard Worker if (N == this) return true;
1318*9880d681SAndroid Build Coastguard Worker if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() ||
1319*9880d681SAndroid Build Coastguard Worker getPredicateFns() != N->getPredicateFns() ||
1320*9880d681SAndroid Build Coastguard Worker getTransformFn() != N->getTransformFn())
1321*9880d681SAndroid Build Coastguard Worker return false;
1322*9880d681SAndroid Build Coastguard Worker
1323*9880d681SAndroid Build Coastguard Worker if (isLeaf()) {
1324*9880d681SAndroid Build Coastguard Worker if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
1325*9880d681SAndroid Build Coastguard Worker if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) {
1326*9880d681SAndroid Build Coastguard Worker return ((DI->getDef() == NDI->getDef())
1327*9880d681SAndroid Build Coastguard Worker && (DepVars.find(getName()) == DepVars.end()
1328*9880d681SAndroid Build Coastguard Worker || getName() == N->getName()));
1329*9880d681SAndroid Build Coastguard Worker }
1330*9880d681SAndroid Build Coastguard Worker }
1331*9880d681SAndroid Build Coastguard Worker return getLeafValue() == N->getLeafValue();
1332*9880d681SAndroid Build Coastguard Worker }
1333*9880d681SAndroid Build Coastguard Worker
1334*9880d681SAndroid Build Coastguard Worker if (N->getOperator() != getOperator() ||
1335*9880d681SAndroid Build Coastguard Worker N->getNumChildren() != getNumChildren()) return false;
1336*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1337*9880d681SAndroid Build Coastguard Worker if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars))
1338*9880d681SAndroid Build Coastguard Worker return false;
1339*9880d681SAndroid Build Coastguard Worker return true;
1340*9880d681SAndroid Build Coastguard Worker }
1341*9880d681SAndroid Build Coastguard Worker
1342*9880d681SAndroid Build Coastguard Worker /// clone - Make a copy of this tree and all of its children.
1343*9880d681SAndroid Build Coastguard Worker ///
clone() const1344*9880d681SAndroid Build Coastguard Worker TreePatternNode *TreePatternNode::clone() const {
1345*9880d681SAndroid Build Coastguard Worker TreePatternNode *New;
1346*9880d681SAndroid Build Coastguard Worker if (isLeaf()) {
1347*9880d681SAndroid Build Coastguard Worker New = new TreePatternNode(getLeafValue(), getNumTypes());
1348*9880d681SAndroid Build Coastguard Worker } else {
1349*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> CChildren;
1350*9880d681SAndroid Build Coastguard Worker CChildren.reserve(Children.size());
1351*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1352*9880d681SAndroid Build Coastguard Worker CChildren.push_back(getChild(i)->clone());
1353*9880d681SAndroid Build Coastguard Worker New = new TreePatternNode(getOperator(), CChildren, getNumTypes());
1354*9880d681SAndroid Build Coastguard Worker }
1355*9880d681SAndroid Build Coastguard Worker New->setName(getName());
1356*9880d681SAndroid Build Coastguard Worker New->Types = Types;
1357*9880d681SAndroid Build Coastguard Worker New->setPredicateFns(getPredicateFns());
1358*9880d681SAndroid Build Coastguard Worker New->setTransformFn(getTransformFn());
1359*9880d681SAndroid Build Coastguard Worker return New;
1360*9880d681SAndroid Build Coastguard Worker }
1361*9880d681SAndroid Build Coastguard Worker
1362*9880d681SAndroid Build Coastguard Worker /// RemoveAllTypes - Recursively strip all the types of this tree.
RemoveAllTypes()1363*9880d681SAndroid Build Coastguard Worker void TreePatternNode::RemoveAllTypes() {
1364*9880d681SAndroid Build Coastguard Worker // Reset to unknown type.
1365*9880d681SAndroid Build Coastguard Worker std::fill(Types.begin(), Types.end(), EEVT::TypeSet());
1366*9880d681SAndroid Build Coastguard Worker if (isLeaf()) return;
1367*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1368*9880d681SAndroid Build Coastguard Worker getChild(i)->RemoveAllTypes();
1369*9880d681SAndroid Build Coastguard Worker }
1370*9880d681SAndroid Build Coastguard Worker
1371*9880d681SAndroid Build Coastguard Worker
1372*9880d681SAndroid Build Coastguard Worker /// SubstituteFormalArguments - Replace the formal arguments in this tree
1373*9880d681SAndroid Build Coastguard Worker /// with actual values specified by ArgMap.
1374*9880d681SAndroid Build Coastguard Worker void TreePatternNode::
SubstituteFormalArguments(std::map<std::string,TreePatternNode * > & ArgMap)1375*9880d681SAndroid Build Coastguard Worker SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) {
1376*9880d681SAndroid Build Coastguard Worker if (isLeaf()) return;
1377*9880d681SAndroid Build Coastguard Worker
1378*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1379*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = getChild(i);
1380*9880d681SAndroid Build Coastguard Worker if (Child->isLeaf()) {
1381*9880d681SAndroid Build Coastguard Worker Init *Val = Child->getLeafValue();
1382*9880d681SAndroid Build Coastguard Worker // Note that, when substituting into an output pattern, Val might be an
1383*9880d681SAndroid Build Coastguard Worker // UnsetInit.
1384*9880d681SAndroid Build Coastguard Worker if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) &&
1385*9880d681SAndroid Build Coastguard Worker cast<DefInit>(Val)->getDef()->getName() == "node")) {
1386*9880d681SAndroid Build Coastguard Worker // We found a use of a formal argument, replace it with its value.
1387*9880d681SAndroid Build Coastguard Worker TreePatternNode *NewChild = ArgMap[Child->getName()];
1388*9880d681SAndroid Build Coastguard Worker assert(NewChild && "Couldn't find formal argument!");
1389*9880d681SAndroid Build Coastguard Worker assert((Child->getPredicateFns().empty() ||
1390*9880d681SAndroid Build Coastguard Worker NewChild->getPredicateFns() == Child->getPredicateFns()) &&
1391*9880d681SAndroid Build Coastguard Worker "Non-empty child predicate clobbered!");
1392*9880d681SAndroid Build Coastguard Worker setChild(i, NewChild);
1393*9880d681SAndroid Build Coastguard Worker }
1394*9880d681SAndroid Build Coastguard Worker } else {
1395*9880d681SAndroid Build Coastguard Worker getChild(i)->SubstituteFormalArguments(ArgMap);
1396*9880d681SAndroid Build Coastguard Worker }
1397*9880d681SAndroid Build Coastguard Worker }
1398*9880d681SAndroid Build Coastguard Worker }
1399*9880d681SAndroid Build Coastguard Worker
1400*9880d681SAndroid Build Coastguard Worker
1401*9880d681SAndroid Build Coastguard Worker /// InlinePatternFragments - If this pattern refers to any pattern
1402*9880d681SAndroid Build Coastguard Worker /// fragments, inline them into place, giving us a pattern without any
1403*9880d681SAndroid Build Coastguard Worker /// PatFrag references.
InlinePatternFragments(TreePattern & TP)1404*9880d681SAndroid Build Coastguard Worker TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) {
1405*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
1406*9880d681SAndroid Build Coastguard Worker return nullptr;
1407*9880d681SAndroid Build Coastguard Worker
1408*9880d681SAndroid Build Coastguard Worker if (isLeaf())
1409*9880d681SAndroid Build Coastguard Worker return this; // nothing to do.
1410*9880d681SAndroid Build Coastguard Worker Record *Op = getOperator();
1411*9880d681SAndroid Build Coastguard Worker
1412*9880d681SAndroid Build Coastguard Worker if (!Op->isSubClassOf("PatFrag")) {
1413*9880d681SAndroid Build Coastguard Worker // Just recursively inline children nodes.
1414*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1415*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = getChild(i);
1416*9880d681SAndroid Build Coastguard Worker TreePatternNode *NewChild = Child->InlinePatternFragments(TP);
1417*9880d681SAndroid Build Coastguard Worker
1418*9880d681SAndroid Build Coastguard Worker assert((Child->getPredicateFns().empty() ||
1419*9880d681SAndroid Build Coastguard Worker NewChild->getPredicateFns() == Child->getPredicateFns()) &&
1420*9880d681SAndroid Build Coastguard Worker "Non-empty child predicate clobbered!");
1421*9880d681SAndroid Build Coastguard Worker
1422*9880d681SAndroid Build Coastguard Worker setChild(i, NewChild);
1423*9880d681SAndroid Build Coastguard Worker }
1424*9880d681SAndroid Build Coastguard Worker return this;
1425*9880d681SAndroid Build Coastguard Worker }
1426*9880d681SAndroid Build Coastguard Worker
1427*9880d681SAndroid Build Coastguard Worker // Otherwise, we found a reference to a fragment. First, look up its
1428*9880d681SAndroid Build Coastguard Worker // TreePattern record.
1429*9880d681SAndroid Build Coastguard Worker TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op);
1430*9880d681SAndroid Build Coastguard Worker
1431*9880d681SAndroid Build Coastguard Worker // Verify that we are passing the right number of operands.
1432*9880d681SAndroid Build Coastguard Worker if (Frag->getNumArgs() != Children.size()) {
1433*9880d681SAndroid Build Coastguard Worker TP.error("'" + Op->getName() + "' fragment requires " +
1434*9880d681SAndroid Build Coastguard Worker utostr(Frag->getNumArgs()) + " operands!");
1435*9880d681SAndroid Build Coastguard Worker return nullptr;
1436*9880d681SAndroid Build Coastguard Worker }
1437*9880d681SAndroid Build Coastguard Worker
1438*9880d681SAndroid Build Coastguard Worker TreePatternNode *FragTree = Frag->getOnlyTree()->clone();
1439*9880d681SAndroid Build Coastguard Worker
1440*9880d681SAndroid Build Coastguard Worker TreePredicateFn PredFn(Frag);
1441*9880d681SAndroid Build Coastguard Worker if (!PredFn.isAlwaysTrue())
1442*9880d681SAndroid Build Coastguard Worker FragTree->addPredicateFn(PredFn);
1443*9880d681SAndroid Build Coastguard Worker
1444*9880d681SAndroid Build Coastguard Worker // Resolve formal arguments to their actual value.
1445*9880d681SAndroid Build Coastguard Worker if (Frag->getNumArgs()) {
1446*9880d681SAndroid Build Coastguard Worker // Compute the map of formal to actual arguments.
1447*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> ArgMap;
1448*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i)
1449*9880d681SAndroid Build Coastguard Worker ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP);
1450*9880d681SAndroid Build Coastguard Worker
1451*9880d681SAndroid Build Coastguard Worker FragTree->SubstituteFormalArguments(ArgMap);
1452*9880d681SAndroid Build Coastguard Worker }
1453*9880d681SAndroid Build Coastguard Worker
1454*9880d681SAndroid Build Coastguard Worker FragTree->setName(getName());
1455*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Types.size(); i != e; ++i)
1456*9880d681SAndroid Build Coastguard Worker FragTree->UpdateNodeType(i, getExtType(i), TP);
1457*9880d681SAndroid Build Coastguard Worker
1458*9880d681SAndroid Build Coastguard Worker // Transfer in the old predicates.
1459*9880d681SAndroid Build Coastguard Worker for (const TreePredicateFn &Pred : getPredicateFns())
1460*9880d681SAndroid Build Coastguard Worker FragTree->addPredicateFn(Pred);
1461*9880d681SAndroid Build Coastguard Worker
1462*9880d681SAndroid Build Coastguard Worker // Get a new copy of this fragment to stitch into here.
1463*9880d681SAndroid Build Coastguard Worker //delete this; // FIXME: implement refcounting!
1464*9880d681SAndroid Build Coastguard Worker
1465*9880d681SAndroid Build Coastguard Worker // The fragment we inlined could have recursive inlining that is needed. See
1466*9880d681SAndroid Build Coastguard Worker // if there are any pattern fragments in it and inline them as needed.
1467*9880d681SAndroid Build Coastguard Worker return FragTree->InlinePatternFragments(TP);
1468*9880d681SAndroid Build Coastguard Worker }
1469*9880d681SAndroid Build Coastguard Worker
1470*9880d681SAndroid Build Coastguard Worker /// getImplicitType - Check to see if the specified record has an implicit
1471*9880d681SAndroid Build Coastguard Worker /// type which should be applied to it. This will infer the type of register
1472*9880d681SAndroid Build Coastguard Worker /// references from the register file information, for example.
1473*9880d681SAndroid Build Coastguard Worker ///
1474*9880d681SAndroid Build Coastguard Worker /// When Unnamed is set, return the type of a DAG operand with no name, such as
1475*9880d681SAndroid Build Coastguard Worker /// the F8RC register class argument in:
1476*9880d681SAndroid Build Coastguard Worker ///
1477*9880d681SAndroid Build Coastguard Worker /// (COPY_TO_REGCLASS GPR:$src, F8RC)
1478*9880d681SAndroid Build Coastguard Worker ///
1479*9880d681SAndroid Build Coastguard Worker /// When Unnamed is false, return the type of a named DAG operand such as the
1480*9880d681SAndroid Build Coastguard Worker /// GPR:$src operand above.
1481*9880d681SAndroid Build Coastguard Worker ///
getImplicitType(Record * R,unsigned ResNo,bool NotRegisters,bool Unnamed,TreePattern & TP)1482*9880d681SAndroid Build Coastguard Worker static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo,
1483*9880d681SAndroid Build Coastguard Worker bool NotRegisters,
1484*9880d681SAndroid Build Coastguard Worker bool Unnamed,
1485*9880d681SAndroid Build Coastguard Worker TreePattern &TP) {
1486*9880d681SAndroid Build Coastguard Worker // Check to see if this is a register operand.
1487*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("RegisterOperand")) {
1488*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "Regoperand ref only has one result!");
1489*9880d681SAndroid Build Coastguard Worker if (NotRegisters)
1490*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1491*9880d681SAndroid Build Coastguard Worker Record *RegClass = R->getValueAsDef("RegClass");
1492*9880d681SAndroid Build Coastguard Worker const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1493*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(T.getRegisterClass(RegClass).getValueTypes());
1494*9880d681SAndroid Build Coastguard Worker }
1495*9880d681SAndroid Build Coastguard Worker
1496*9880d681SAndroid Build Coastguard Worker // Check to see if this is a register or a register class.
1497*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("RegisterClass")) {
1498*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "Regclass ref only has one result!");
1499*9880d681SAndroid Build Coastguard Worker // An unnamed register class represents itself as an i32 immediate, for
1500*9880d681SAndroid Build Coastguard Worker // example on a COPY_TO_REGCLASS instruction.
1501*9880d681SAndroid Build Coastguard Worker if (Unnamed)
1502*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(MVT::i32, TP);
1503*9880d681SAndroid Build Coastguard Worker
1504*9880d681SAndroid Build Coastguard Worker // In a named operand, the register class provides the possible set of
1505*9880d681SAndroid Build Coastguard Worker // types.
1506*9880d681SAndroid Build Coastguard Worker if (NotRegisters)
1507*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1508*9880d681SAndroid Build Coastguard Worker const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1509*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes());
1510*9880d681SAndroid Build Coastguard Worker }
1511*9880d681SAndroid Build Coastguard Worker
1512*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("PatFrag")) {
1513*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "FIXME: PatFrag with multiple results?");
1514*9880d681SAndroid Build Coastguard Worker // Pattern fragment types will be resolved when they are inlined.
1515*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1516*9880d681SAndroid Build Coastguard Worker }
1517*9880d681SAndroid Build Coastguard Worker
1518*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("Register")) {
1519*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "Registers only produce one result!");
1520*9880d681SAndroid Build Coastguard Worker if (NotRegisters)
1521*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1522*9880d681SAndroid Build Coastguard Worker const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1523*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(T.getRegisterVTs(R));
1524*9880d681SAndroid Build Coastguard Worker }
1525*9880d681SAndroid Build Coastguard Worker
1526*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("SubRegIndex")) {
1527*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "SubRegisterIndices only produce one result!");
1528*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(MVT::i32, TP);
1529*9880d681SAndroid Build Coastguard Worker }
1530*9880d681SAndroid Build Coastguard Worker
1531*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("ValueType")) {
1532*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "This node only has one result!");
1533*9880d681SAndroid Build Coastguard Worker // An unnamed VTSDNode represents itself as an MVT::Other immediate.
1534*9880d681SAndroid Build Coastguard Worker //
1535*9880d681SAndroid Build Coastguard Worker // (sext_inreg GPR:$src, i16)
1536*9880d681SAndroid Build Coastguard Worker // ~~~
1537*9880d681SAndroid Build Coastguard Worker if (Unnamed)
1538*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(MVT::Other, TP);
1539*9880d681SAndroid Build Coastguard Worker // With a name, the ValueType simply provides the type of the named
1540*9880d681SAndroid Build Coastguard Worker // variable.
1541*9880d681SAndroid Build Coastguard Worker //
1542*9880d681SAndroid Build Coastguard Worker // (sext_inreg i32:$src, i16)
1543*9880d681SAndroid Build Coastguard Worker // ~~~~~~~~
1544*9880d681SAndroid Build Coastguard Worker if (NotRegisters)
1545*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1546*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(getValueType(R), TP);
1547*9880d681SAndroid Build Coastguard Worker }
1548*9880d681SAndroid Build Coastguard Worker
1549*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("CondCode")) {
1550*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "This node only has one result!");
1551*9880d681SAndroid Build Coastguard Worker // Using a CondCodeSDNode.
1552*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(MVT::Other, TP);
1553*9880d681SAndroid Build Coastguard Worker }
1554*9880d681SAndroid Build Coastguard Worker
1555*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("ComplexPattern")) {
1556*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
1557*9880d681SAndroid Build Coastguard Worker if (NotRegisters)
1558*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1559*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(),
1560*9880d681SAndroid Build Coastguard Worker TP);
1561*9880d681SAndroid Build Coastguard Worker }
1562*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("PointerLikeRegClass")) {
1563*9880d681SAndroid Build Coastguard Worker assert(ResNo == 0 && "Regclass can only have one result!");
1564*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(MVT::iPTR, TP);
1565*9880d681SAndroid Build Coastguard Worker }
1566*9880d681SAndroid Build Coastguard Worker
1567*9880d681SAndroid Build Coastguard Worker if (R->getName() == "node" || R->getName() == "srcvalue" ||
1568*9880d681SAndroid Build Coastguard Worker R->getName() == "zero_reg") {
1569*9880d681SAndroid Build Coastguard Worker // Placeholder.
1570*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(); // Unknown.
1571*9880d681SAndroid Build Coastguard Worker }
1572*9880d681SAndroid Build Coastguard Worker
1573*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("Operand"))
1574*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(getValueType(R->getValueAsDef("Type")));
1575*9880d681SAndroid Build Coastguard Worker
1576*9880d681SAndroid Build Coastguard Worker TP.error("Unknown node flavor used in pattern: " + R->getName());
1577*9880d681SAndroid Build Coastguard Worker return EEVT::TypeSet(MVT::Other, TP);
1578*9880d681SAndroid Build Coastguard Worker }
1579*9880d681SAndroid Build Coastguard Worker
1580*9880d681SAndroid Build Coastguard Worker
1581*9880d681SAndroid Build Coastguard Worker /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
1582*9880d681SAndroid Build Coastguard Worker /// CodeGenIntrinsic information for it, otherwise return a null pointer.
1583*9880d681SAndroid Build Coastguard Worker const CodeGenIntrinsic *TreePatternNode::
getIntrinsicInfo(const CodeGenDAGPatterns & CDP) const1584*9880d681SAndroid Build Coastguard Worker getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
1585*9880d681SAndroid Build Coastguard Worker if (getOperator() != CDP.get_intrinsic_void_sdnode() &&
1586*9880d681SAndroid Build Coastguard Worker getOperator() != CDP.get_intrinsic_w_chain_sdnode() &&
1587*9880d681SAndroid Build Coastguard Worker getOperator() != CDP.get_intrinsic_wo_chain_sdnode())
1588*9880d681SAndroid Build Coastguard Worker return nullptr;
1589*9880d681SAndroid Build Coastguard Worker
1590*9880d681SAndroid Build Coastguard Worker unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue();
1591*9880d681SAndroid Build Coastguard Worker return &CDP.getIntrinsicInfo(IID);
1592*9880d681SAndroid Build Coastguard Worker }
1593*9880d681SAndroid Build Coastguard Worker
1594*9880d681SAndroid Build Coastguard Worker /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
1595*9880d681SAndroid Build Coastguard Worker /// return the ComplexPattern information, otherwise return null.
1596*9880d681SAndroid Build Coastguard Worker const ComplexPattern *
getComplexPatternInfo(const CodeGenDAGPatterns & CGP) const1597*9880d681SAndroid Build Coastguard Worker TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
1598*9880d681SAndroid Build Coastguard Worker Record *Rec;
1599*9880d681SAndroid Build Coastguard Worker if (isLeaf()) {
1600*9880d681SAndroid Build Coastguard Worker DefInit *DI = dyn_cast<DefInit>(getLeafValue());
1601*9880d681SAndroid Build Coastguard Worker if (!DI)
1602*9880d681SAndroid Build Coastguard Worker return nullptr;
1603*9880d681SAndroid Build Coastguard Worker Rec = DI->getDef();
1604*9880d681SAndroid Build Coastguard Worker } else
1605*9880d681SAndroid Build Coastguard Worker Rec = getOperator();
1606*9880d681SAndroid Build Coastguard Worker
1607*9880d681SAndroid Build Coastguard Worker if (!Rec->isSubClassOf("ComplexPattern"))
1608*9880d681SAndroid Build Coastguard Worker return nullptr;
1609*9880d681SAndroid Build Coastguard Worker return &CGP.getComplexPattern(Rec);
1610*9880d681SAndroid Build Coastguard Worker }
1611*9880d681SAndroid Build Coastguard Worker
getNumMIResults(const CodeGenDAGPatterns & CGP) const1612*9880d681SAndroid Build Coastguard Worker unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
1613*9880d681SAndroid Build Coastguard Worker // A ComplexPattern specifically declares how many results it fills in.
1614*9880d681SAndroid Build Coastguard Worker if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
1615*9880d681SAndroid Build Coastguard Worker return CP->getNumOperands();
1616*9880d681SAndroid Build Coastguard Worker
1617*9880d681SAndroid Build Coastguard Worker // If MIOperandInfo is specified, that gives the count.
1618*9880d681SAndroid Build Coastguard Worker if (isLeaf()) {
1619*9880d681SAndroid Build Coastguard Worker DefInit *DI = dyn_cast<DefInit>(getLeafValue());
1620*9880d681SAndroid Build Coastguard Worker if (DI && DI->getDef()->isSubClassOf("Operand")) {
1621*9880d681SAndroid Build Coastguard Worker DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
1622*9880d681SAndroid Build Coastguard Worker if (MIOps->getNumArgs())
1623*9880d681SAndroid Build Coastguard Worker return MIOps->getNumArgs();
1624*9880d681SAndroid Build Coastguard Worker }
1625*9880d681SAndroid Build Coastguard Worker }
1626*9880d681SAndroid Build Coastguard Worker
1627*9880d681SAndroid Build Coastguard Worker // Otherwise there is just one result.
1628*9880d681SAndroid Build Coastguard Worker return 1;
1629*9880d681SAndroid Build Coastguard Worker }
1630*9880d681SAndroid Build Coastguard Worker
1631*9880d681SAndroid Build Coastguard Worker /// NodeHasProperty - Return true if this node has the specified property.
NodeHasProperty(SDNP Property,const CodeGenDAGPatterns & CGP) const1632*9880d681SAndroid Build Coastguard Worker bool TreePatternNode::NodeHasProperty(SDNP Property,
1633*9880d681SAndroid Build Coastguard Worker const CodeGenDAGPatterns &CGP) const {
1634*9880d681SAndroid Build Coastguard Worker if (isLeaf()) {
1635*9880d681SAndroid Build Coastguard Worker if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
1636*9880d681SAndroid Build Coastguard Worker return CP->hasProperty(Property);
1637*9880d681SAndroid Build Coastguard Worker return false;
1638*9880d681SAndroid Build Coastguard Worker }
1639*9880d681SAndroid Build Coastguard Worker
1640*9880d681SAndroid Build Coastguard Worker Record *Operator = getOperator();
1641*9880d681SAndroid Build Coastguard Worker if (!Operator->isSubClassOf("SDNode")) return false;
1642*9880d681SAndroid Build Coastguard Worker
1643*9880d681SAndroid Build Coastguard Worker return CGP.getSDNodeInfo(Operator).hasProperty(Property);
1644*9880d681SAndroid Build Coastguard Worker }
1645*9880d681SAndroid Build Coastguard Worker
1646*9880d681SAndroid Build Coastguard Worker
1647*9880d681SAndroid Build Coastguard Worker
1648*9880d681SAndroid Build Coastguard Worker
1649*9880d681SAndroid Build Coastguard Worker /// TreeHasProperty - Return true if any node in this tree has the specified
1650*9880d681SAndroid Build Coastguard Worker /// property.
TreeHasProperty(SDNP Property,const CodeGenDAGPatterns & CGP) const1651*9880d681SAndroid Build Coastguard Worker bool TreePatternNode::TreeHasProperty(SDNP Property,
1652*9880d681SAndroid Build Coastguard Worker const CodeGenDAGPatterns &CGP) const {
1653*9880d681SAndroid Build Coastguard Worker if (NodeHasProperty(Property, CGP))
1654*9880d681SAndroid Build Coastguard Worker return true;
1655*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1656*9880d681SAndroid Build Coastguard Worker if (getChild(i)->TreeHasProperty(Property, CGP))
1657*9880d681SAndroid Build Coastguard Worker return true;
1658*9880d681SAndroid Build Coastguard Worker return false;
1659*9880d681SAndroid Build Coastguard Worker }
1660*9880d681SAndroid Build Coastguard Worker
1661*9880d681SAndroid Build Coastguard Worker /// isCommutativeIntrinsic - Return true if the node corresponds to a
1662*9880d681SAndroid Build Coastguard Worker /// commutative intrinsic.
1663*9880d681SAndroid Build Coastguard Worker bool
isCommutativeIntrinsic(const CodeGenDAGPatterns & CDP) const1664*9880d681SAndroid Build Coastguard Worker TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const {
1665*9880d681SAndroid Build Coastguard Worker if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP))
1666*9880d681SAndroid Build Coastguard Worker return Int->isCommutative;
1667*9880d681SAndroid Build Coastguard Worker return false;
1668*9880d681SAndroid Build Coastguard Worker }
1669*9880d681SAndroid Build Coastguard Worker
isOperandClass(const TreePatternNode * N,StringRef Class)1670*9880d681SAndroid Build Coastguard Worker static bool isOperandClass(const TreePatternNode *N, StringRef Class) {
1671*9880d681SAndroid Build Coastguard Worker if (!N->isLeaf())
1672*9880d681SAndroid Build Coastguard Worker return N->getOperator()->isSubClassOf(Class);
1673*9880d681SAndroid Build Coastguard Worker
1674*9880d681SAndroid Build Coastguard Worker DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
1675*9880d681SAndroid Build Coastguard Worker if (DI && DI->getDef()->isSubClassOf(Class))
1676*9880d681SAndroid Build Coastguard Worker return true;
1677*9880d681SAndroid Build Coastguard Worker
1678*9880d681SAndroid Build Coastguard Worker return false;
1679*9880d681SAndroid Build Coastguard Worker }
1680*9880d681SAndroid Build Coastguard Worker
emitTooManyOperandsError(TreePattern & TP,StringRef InstName,unsigned Expected,unsigned Actual)1681*9880d681SAndroid Build Coastguard Worker static void emitTooManyOperandsError(TreePattern &TP,
1682*9880d681SAndroid Build Coastguard Worker StringRef InstName,
1683*9880d681SAndroid Build Coastguard Worker unsigned Expected,
1684*9880d681SAndroid Build Coastguard Worker unsigned Actual) {
1685*9880d681SAndroid Build Coastguard Worker TP.error("Instruction '" + InstName + "' was provided " + Twine(Actual) +
1686*9880d681SAndroid Build Coastguard Worker " operands but expected only " + Twine(Expected) + "!");
1687*9880d681SAndroid Build Coastguard Worker }
1688*9880d681SAndroid Build Coastguard Worker
emitTooFewOperandsError(TreePattern & TP,StringRef InstName,unsigned Actual)1689*9880d681SAndroid Build Coastguard Worker static void emitTooFewOperandsError(TreePattern &TP,
1690*9880d681SAndroid Build Coastguard Worker StringRef InstName,
1691*9880d681SAndroid Build Coastguard Worker unsigned Actual) {
1692*9880d681SAndroid Build Coastguard Worker TP.error("Instruction '" + InstName +
1693*9880d681SAndroid Build Coastguard Worker "' expects more than the provided " + Twine(Actual) + " operands!");
1694*9880d681SAndroid Build Coastguard Worker }
1695*9880d681SAndroid Build Coastguard Worker
1696*9880d681SAndroid Build Coastguard Worker /// ApplyTypeConstraints - Apply all of the type constraints relevant to
1697*9880d681SAndroid Build Coastguard Worker /// this node and its children in the tree. This returns true if it makes a
1698*9880d681SAndroid Build Coastguard Worker /// change, false otherwise. If a type contradiction is found, flag an error.
ApplyTypeConstraints(TreePattern & TP,bool NotRegisters)1699*9880d681SAndroid Build Coastguard Worker bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
1700*9880d681SAndroid Build Coastguard Worker if (TP.hasError())
1701*9880d681SAndroid Build Coastguard Worker return false;
1702*9880d681SAndroid Build Coastguard Worker
1703*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
1704*9880d681SAndroid Build Coastguard Worker if (isLeaf()) {
1705*9880d681SAndroid Build Coastguard Worker if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
1706*9880d681SAndroid Build Coastguard Worker // If it's a regclass or something else known, include the type.
1707*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
1708*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Types.size(); i != e; ++i)
1709*9880d681SAndroid Build Coastguard Worker MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
1710*9880d681SAndroid Build Coastguard Worker NotRegisters,
1711*9880d681SAndroid Build Coastguard Worker !hasName(), TP), TP);
1712*9880d681SAndroid Build Coastguard Worker return MadeChange;
1713*9880d681SAndroid Build Coastguard Worker }
1714*9880d681SAndroid Build Coastguard Worker
1715*9880d681SAndroid Build Coastguard Worker if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
1716*9880d681SAndroid Build Coastguard Worker assert(Types.size() == 1 && "Invalid IntInit");
1717*9880d681SAndroid Build Coastguard Worker
1718*9880d681SAndroid Build Coastguard Worker // Int inits are always integers. :)
1719*9880d681SAndroid Build Coastguard Worker bool MadeChange = Types[0].EnforceInteger(TP);
1720*9880d681SAndroid Build Coastguard Worker
1721*9880d681SAndroid Build Coastguard Worker if (!Types[0].isConcrete())
1722*9880d681SAndroid Build Coastguard Worker return MadeChange;
1723*9880d681SAndroid Build Coastguard Worker
1724*9880d681SAndroid Build Coastguard Worker MVT::SimpleValueType VT = getType(0);
1725*9880d681SAndroid Build Coastguard Worker if (VT == MVT::iPTR || VT == MVT::iPTRAny)
1726*9880d681SAndroid Build Coastguard Worker return MadeChange;
1727*9880d681SAndroid Build Coastguard Worker
1728*9880d681SAndroid Build Coastguard Worker unsigned Size = MVT(VT).getSizeInBits();
1729*9880d681SAndroid Build Coastguard Worker // Make sure that the value is representable for this type.
1730*9880d681SAndroid Build Coastguard Worker if (Size >= 32) return MadeChange;
1731*9880d681SAndroid Build Coastguard Worker
1732*9880d681SAndroid Build Coastguard Worker // Check that the value doesn't use more bits than we have. It must either
1733*9880d681SAndroid Build Coastguard Worker // be a sign- or zero-extended equivalent of the original.
1734*9880d681SAndroid Build Coastguard Worker int64_t SignBitAndAbove = II->getValue() >> (Size - 1);
1735*9880d681SAndroid Build Coastguard Worker if (SignBitAndAbove == -1 || SignBitAndAbove == 0 || SignBitAndAbove == 1)
1736*9880d681SAndroid Build Coastguard Worker return MadeChange;
1737*9880d681SAndroid Build Coastguard Worker
1738*9880d681SAndroid Build Coastguard Worker TP.error("Integer value '" + itostr(II->getValue()) +
1739*9880d681SAndroid Build Coastguard Worker "' is out of range for type '" + getEnumName(getType(0)) + "'!");
1740*9880d681SAndroid Build Coastguard Worker return false;
1741*9880d681SAndroid Build Coastguard Worker }
1742*9880d681SAndroid Build Coastguard Worker return false;
1743*9880d681SAndroid Build Coastguard Worker }
1744*9880d681SAndroid Build Coastguard Worker
1745*9880d681SAndroid Build Coastguard Worker // special handling for set, which isn't really an SDNode.
1746*9880d681SAndroid Build Coastguard Worker if (getOperator()->getName() == "set") {
1747*9880d681SAndroid Build Coastguard Worker assert(getNumTypes() == 0 && "Set doesn't produce a value");
1748*9880d681SAndroid Build Coastguard Worker assert(getNumChildren() >= 2 && "Missing RHS of a set?");
1749*9880d681SAndroid Build Coastguard Worker unsigned NC = getNumChildren();
1750*9880d681SAndroid Build Coastguard Worker
1751*9880d681SAndroid Build Coastguard Worker TreePatternNode *SetVal = getChild(NC-1);
1752*9880d681SAndroid Build Coastguard Worker bool MadeChange = SetVal->ApplyTypeConstraints(TP, NotRegisters);
1753*9880d681SAndroid Build Coastguard Worker
1754*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NC-1; ++i) {
1755*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = getChild(i);
1756*9880d681SAndroid Build Coastguard Worker MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters);
1757*9880d681SAndroid Build Coastguard Worker
1758*9880d681SAndroid Build Coastguard Worker // Types of operands must match.
1759*9880d681SAndroid Build Coastguard Worker MadeChange |= Child->UpdateNodeType(0, SetVal->getExtType(i), TP);
1760*9880d681SAndroid Build Coastguard Worker MadeChange |= SetVal->UpdateNodeType(i, Child->getExtType(0), TP);
1761*9880d681SAndroid Build Coastguard Worker }
1762*9880d681SAndroid Build Coastguard Worker return MadeChange;
1763*9880d681SAndroid Build Coastguard Worker }
1764*9880d681SAndroid Build Coastguard Worker
1765*9880d681SAndroid Build Coastguard Worker if (getOperator()->getName() == "implicit") {
1766*9880d681SAndroid Build Coastguard Worker assert(getNumTypes() == 0 && "Node doesn't produce a value");
1767*9880d681SAndroid Build Coastguard Worker
1768*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
1769*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < getNumChildren(); ++i)
1770*9880d681SAndroid Build Coastguard Worker MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1771*9880d681SAndroid Build Coastguard Worker return MadeChange;
1772*9880d681SAndroid Build Coastguard Worker }
1773*9880d681SAndroid Build Coastguard Worker
1774*9880d681SAndroid Build Coastguard Worker if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) {
1775*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
1776*9880d681SAndroid Build Coastguard Worker
1777*9880d681SAndroid Build Coastguard Worker // Apply the result type to the node.
1778*9880d681SAndroid Build Coastguard Worker unsigned NumRetVTs = Int->IS.RetVTs.size();
1779*9880d681SAndroid Build Coastguard Worker unsigned NumParamVTs = Int->IS.ParamVTs.size();
1780*9880d681SAndroid Build Coastguard Worker
1781*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = NumRetVTs; i != e; ++i)
1782*9880d681SAndroid Build Coastguard Worker MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP);
1783*9880d681SAndroid Build Coastguard Worker
1784*9880d681SAndroid Build Coastguard Worker if (getNumChildren() != NumParamVTs + 1) {
1785*9880d681SAndroid Build Coastguard Worker TP.error("Intrinsic '" + Int->Name + "' expects " +
1786*9880d681SAndroid Build Coastguard Worker utostr(NumParamVTs) + " operands, not " +
1787*9880d681SAndroid Build Coastguard Worker utostr(getNumChildren() - 1) + " operands!");
1788*9880d681SAndroid Build Coastguard Worker return false;
1789*9880d681SAndroid Build Coastguard Worker }
1790*9880d681SAndroid Build Coastguard Worker
1791*9880d681SAndroid Build Coastguard Worker // Apply type info to the intrinsic ID.
1792*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP);
1793*9880d681SAndroid Build Coastguard Worker
1794*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) {
1795*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters);
1796*9880d681SAndroid Build Coastguard Worker
1797*9880d681SAndroid Build Coastguard Worker MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i];
1798*9880d681SAndroid Build Coastguard Worker assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case");
1799*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP);
1800*9880d681SAndroid Build Coastguard Worker }
1801*9880d681SAndroid Build Coastguard Worker return MadeChange;
1802*9880d681SAndroid Build Coastguard Worker }
1803*9880d681SAndroid Build Coastguard Worker
1804*9880d681SAndroid Build Coastguard Worker if (getOperator()->isSubClassOf("SDNode")) {
1805*9880d681SAndroid Build Coastguard Worker const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator());
1806*9880d681SAndroid Build Coastguard Worker
1807*9880d681SAndroid Build Coastguard Worker // Check that the number of operands is sane. Negative operands -> varargs.
1808*9880d681SAndroid Build Coastguard Worker if (NI.getNumOperands() >= 0 &&
1809*9880d681SAndroid Build Coastguard Worker getNumChildren() != (unsigned)NI.getNumOperands()) {
1810*9880d681SAndroid Build Coastguard Worker TP.error(getOperator()->getName() + " node requires exactly " +
1811*9880d681SAndroid Build Coastguard Worker itostr(NI.getNumOperands()) + " operands!");
1812*9880d681SAndroid Build Coastguard Worker return false;
1813*9880d681SAndroid Build Coastguard Worker }
1814*9880d681SAndroid Build Coastguard Worker
1815*9880d681SAndroid Build Coastguard Worker bool MadeChange = NI.ApplyTypeConstraints(this, TP);
1816*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1817*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1818*9880d681SAndroid Build Coastguard Worker return MadeChange;
1819*9880d681SAndroid Build Coastguard Worker }
1820*9880d681SAndroid Build Coastguard Worker
1821*9880d681SAndroid Build Coastguard Worker if (getOperator()->isSubClassOf("Instruction")) {
1822*9880d681SAndroid Build Coastguard Worker const DAGInstruction &Inst = CDP.getInstruction(getOperator());
1823*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &InstInfo =
1824*9880d681SAndroid Build Coastguard Worker CDP.getTargetInfo().getInstruction(getOperator());
1825*9880d681SAndroid Build Coastguard Worker
1826*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
1827*9880d681SAndroid Build Coastguard Worker
1828*9880d681SAndroid Build Coastguard Worker // Apply the result types to the node, these come from the things in the
1829*9880d681SAndroid Build Coastguard Worker // (outs) list of the instruction.
1830*9880d681SAndroid Build Coastguard Worker unsigned NumResultsToAdd = std::min(InstInfo.Operands.NumDefs,
1831*9880d681SAndroid Build Coastguard Worker Inst.getNumResults());
1832*9880d681SAndroid Build Coastguard Worker for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo)
1833*9880d681SAndroid Build Coastguard Worker MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP);
1834*9880d681SAndroid Build Coastguard Worker
1835*9880d681SAndroid Build Coastguard Worker // If the instruction has implicit defs, we apply the first one as a result.
1836*9880d681SAndroid Build Coastguard Worker // FIXME: This sucks, it should apply all implicit defs.
1837*9880d681SAndroid Build Coastguard Worker if (!InstInfo.ImplicitDefs.empty()) {
1838*9880d681SAndroid Build Coastguard Worker unsigned ResNo = NumResultsToAdd;
1839*9880d681SAndroid Build Coastguard Worker
1840*9880d681SAndroid Build Coastguard Worker // FIXME: Generalize to multiple possible types and multiple possible
1841*9880d681SAndroid Build Coastguard Worker // ImplicitDefs.
1842*9880d681SAndroid Build Coastguard Worker MVT::SimpleValueType VT =
1843*9880d681SAndroid Build Coastguard Worker InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo());
1844*9880d681SAndroid Build Coastguard Worker
1845*9880d681SAndroid Build Coastguard Worker if (VT != MVT::Other)
1846*9880d681SAndroid Build Coastguard Worker MadeChange |= UpdateNodeType(ResNo, VT, TP);
1847*9880d681SAndroid Build Coastguard Worker }
1848*9880d681SAndroid Build Coastguard Worker
1849*9880d681SAndroid Build Coastguard Worker // If this is an INSERT_SUBREG, constrain the source and destination VTs to
1850*9880d681SAndroid Build Coastguard Worker // be the same.
1851*9880d681SAndroid Build Coastguard Worker if (getOperator()->getName() == "INSERT_SUBREG") {
1852*9880d681SAndroid Build Coastguard Worker assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled");
1853*9880d681SAndroid Build Coastguard Worker MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP);
1854*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP);
1855*9880d681SAndroid Build Coastguard Worker } else if (getOperator()->getName() == "REG_SEQUENCE") {
1856*9880d681SAndroid Build Coastguard Worker // We need to do extra, custom typechecking for REG_SEQUENCE since it is
1857*9880d681SAndroid Build Coastguard Worker // variadic.
1858*9880d681SAndroid Build Coastguard Worker
1859*9880d681SAndroid Build Coastguard Worker unsigned NChild = getNumChildren();
1860*9880d681SAndroid Build Coastguard Worker if (NChild < 3) {
1861*9880d681SAndroid Build Coastguard Worker TP.error("REG_SEQUENCE requires at least 3 operands!");
1862*9880d681SAndroid Build Coastguard Worker return false;
1863*9880d681SAndroid Build Coastguard Worker }
1864*9880d681SAndroid Build Coastguard Worker
1865*9880d681SAndroid Build Coastguard Worker if (NChild % 2 == 0) {
1866*9880d681SAndroid Build Coastguard Worker TP.error("REG_SEQUENCE requires an odd number of operands!");
1867*9880d681SAndroid Build Coastguard Worker return false;
1868*9880d681SAndroid Build Coastguard Worker }
1869*9880d681SAndroid Build Coastguard Worker
1870*9880d681SAndroid Build Coastguard Worker if (!isOperandClass(getChild(0), "RegisterClass")) {
1871*9880d681SAndroid Build Coastguard Worker TP.error("REG_SEQUENCE requires a RegisterClass for first operand!");
1872*9880d681SAndroid Build Coastguard Worker return false;
1873*9880d681SAndroid Build Coastguard Worker }
1874*9880d681SAndroid Build Coastguard Worker
1875*9880d681SAndroid Build Coastguard Worker for (unsigned I = 1; I < NChild; I += 2) {
1876*9880d681SAndroid Build Coastguard Worker TreePatternNode *SubIdxChild = getChild(I + 1);
1877*9880d681SAndroid Build Coastguard Worker if (!isOperandClass(SubIdxChild, "SubRegIndex")) {
1878*9880d681SAndroid Build Coastguard Worker TP.error("REG_SEQUENCE requires a SubRegIndex for operand " +
1879*9880d681SAndroid Build Coastguard Worker itostr(I + 1) + "!");
1880*9880d681SAndroid Build Coastguard Worker return false;
1881*9880d681SAndroid Build Coastguard Worker }
1882*9880d681SAndroid Build Coastguard Worker }
1883*9880d681SAndroid Build Coastguard Worker }
1884*9880d681SAndroid Build Coastguard Worker
1885*9880d681SAndroid Build Coastguard Worker unsigned ChildNo = 0;
1886*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
1887*9880d681SAndroid Build Coastguard Worker Record *OperandNode = Inst.getOperand(i);
1888*9880d681SAndroid Build Coastguard Worker
1889*9880d681SAndroid Build Coastguard Worker // If the instruction expects a predicate or optional def operand, we
1890*9880d681SAndroid Build Coastguard Worker // codegen this by setting the operand to it's default value if it has a
1891*9880d681SAndroid Build Coastguard Worker // non-empty DefaultOps field.
1892*9880d681SAndroid Build Coastguard Worker if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
1893*9880d681SAndroid Build Coastguard Worker !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1894*9880d681SAndroid Build Coastguard Worker continue;
1895*9880d681SAndroid Build Coastguard Worker
1896*9880d681SAndroid Build Coastguard Worker // Verify that we didn't run out of provided operands.
1897*9880d681SAndroid Build Coastguard Worker if (ChildNo >= getNumChildren()) {
1898*9880d681SAndroid Build Coastguard Worker emitTooFewOperandsError(TP, getOperator()->getName(), getNumChildren());
1899*9880d681SAndroid Build Coastguard Worker return false;
1900*9880d681SAndroid Build Coastguard Worker }
1901*9880d681SAndroid Build Coastguard Worker
1902*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = getChild(ChildNo++);
1903*9880d681SAndroid Build Coastguard Worker unsigned ChildResNo = 0; // Instructions always use res #0 of their op.
1904*9880d681SAndroid Build Coastguard Worker
1905*9880d681SAndroid Build Coastguard Worker // If the operand has sub-operands, they may be provided by distinct
1906*9880d681SAndroid Build Coastguard Worker // child patterns, so attempt to match each sub-operand separately.
1907*9880d681SAndroid Build Coastguard Worker if (OperandNode->isSubClassOf("Operand")) {
1908*9880d681SAndroid Build Coastguard Worker DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
1909*9880d681SAndroid Build Coastguard Worker if (unsigned NumArgs = MIOpInfo->getNumArgs()) {
1910*9880d681SAndroid Build Coastguard Worker // But don't do that if the whole operand is being provided by
1911*9880d681SAndroid Build Coastguard Worker // a single ComplexPattern-related Operand.
1912*9880d681SAndroid Build Coastguard Worker
1913*9880d681SAndroid Build Coastguard Worker if (Child->getNumMIResults(CDP) < NumArgs) {
1914*9880d681SAndroid Build Coastguard Worker // Match first sub-operand against the child we already have.
1915*9880d681SAndroid Build Coastguard Worker Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
1916*9880d681SAndroid Build Coastguard Worker MadeChange |=
1917*9880d681SAndroid Build Coastguard Worker Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
1918*9880d681SAndroid Build Coastguard Worker
1919*9880d681SAndroid Build Coastguard Worker // And the remaining sub-operands against subsequent children.
1920*9880d681SAndroid Build Coastguard Worker for (unsigned Arg = 1; Arg < NumArgs; ++Arg) {
1921*9880d681SAndroid Build Coastguard Worker if (ChildNo >= getNumChildren()) {
1922*9880d681SAndroid Build Coastguard Worker emitTooFewOperandsError(TP, getOperator()->getName(),
1923*9880d681SAndroid Build Coastguard Worker getNumChildren());
1924*9880d681SAndroid Build Coastguard Worker return false;
1925*9880d681SAndroid Build Coastguard Worker }
1926*9880d681SAndroid Build Coastguard Worker Child = getChild(ChildNo++);
1927*9880d681SAndroid Build Coastguard Worker
1928*9880d681SAndroid Build Coastguard Worker SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef();
1929*9880d681SAndroid Build Coastguard Worker MadeChange |=
1930*9880d681SAndroid Build Coastguard Worker Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
1931*9880d681SAndroid Build Coastguard Worker }
1932*9880d681SAndroid Build Coastguard Worker continue;
1933*9880d681SAndroid Build Coastguard Worker }
1934*9880d681SAndroid Build Coastguard Worker }
1935*9880d681SAndroid Build Coastguard Worker }
1936*9880d681SAndroid Build Coastguard Worker
1937*9880d681SAndroid Build Coastguard Worker // If we didn't match by pieces above, attempt to match the whole
1938*9880d681SAndroid Build Coastguard Worker // operand now.
1939*9880d681SAndroid Build Coastguard Worker MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP);
1940*9880d681SAndroid Build Coastguard Worker }
1941*9880d681SAndroid Build Coastguard Worker
1942*9880d681SAndroid Build Coastguard Worker if (!InstInfo.Operands.isVariadic && ChildNo != getNumChildren()) {
1943*9880d681SAndroid Build Coastguard Worker emitTooManyOperandsError(TP, getOperator()->getName(),
1944*9880d681SAndroid Build Coastguard Worker ChildNo, getNumChildren());
1945*9880d681SAndroid Build Coastguard Worker return false;
1946*9880d681SAndroid Build Coastguard Worker }
1947*9880d681SAndroid Build Coastguard Worker
1948*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1949*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1950*9880d681SAndroid Build Coastguard Worker return MadeChange;
1951*9880d681SAndroid Build Coastguard Worker }
1952*9880d681SAndroid Build Coastguard Worker
1953*9880d681SAndroid Build Coastguard Worker if (getOperator()->isSubClassOf("ComplexPattern")) {
1954*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
1955*9880d681SAndroid Build Coastguard Worker
1956*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < getNumChildren(); ++i)
1957*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
1958*9880d681SAndroid Build Coastguard Worker
1959*9880d681SAndroid Build Coastguard Worker return MadeChange;
1960*9880d681SAndroid Build Coastguard Worker }
1961*9880d681SAndroid Build Coastguard Worker
1962*9880d681SAndroid Build Coastguard Worker assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!");
1963*9880d681SAndroid Build Coastguard Worker
1964*9880d681SAndroid Build Coastguard Worker // Node transforms always take one operand.
1965*9880d681SAndroid Build Coastguard Worker if (getNumChildren() != 1) {
1966*9880d681SAndroid Build Coastguard Worker TP.error("Node transform '" + getOperator()->getName() +
1967*9880d681SAndroid Build Coastguard Worker "' requires one operand!");
1968*9880d681SAndroid Build Coastguard Worker return false;
1969*9880d681SAndroid Build Coastguard Worker }
1970*9880d681SAndroid Build Coastguard Worker
1971*9880d681SAndroid Build Coastguard Worker bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
1972*9880d681SAndroid Build Coastguard Worker
1973*9880d681SAndroid Build Coastguard Worker
1974*9880d681SAndroid Build Coastguard Worker // If either the output or input of the xform does not have exact
1975*9880d681SAndroid Build Coastguard Worker // type info. We assume they must be the same. Otherwise, it is perfectly
1976*9880d681SAndroid Build Coastguard Worker // legal to transform from one type to a completely different type.
1977*9880d681SAndroid Build Coastguard Worker #if 0
1978*9880d681SAndroid Build Coastguard Worker if (!hasTypeSet() || !getChild(0)->hasTypeSet()) {
1979*9880d681SAndroid Build Coastguard Worker bool MadeChange = UpdateNodeType(getChild(0)->getExtType(), TP);
1980*9880d681SAndroid Build Coastguard Worker MadeChange |= getChild(0)->UpdateNodeType(getExtType(), TP);
1981*9880d681SAndroid Build Coastguard Worker return MadeChange;
1982*9880d681SAndroid Build Coastguard Worker }
1983*9880d681SAndroid Build Coastguard Worker #endif
1984*9880d681SAndroid Build Coastguard Worker return MadeChange;
1985*9880d681SAndroid Build Coastguard Worker }
1986*9880d681SAndroid Build Coastguard Worker
1987*9880d681SAndroid Build Coastguard Worker /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
1988*9880d681SAndroid Build Coastguard Worker /// RHS of a commutative operation, not the on LHS.
OnlyOnRHSOfCommutative(TreePatternNode * N)1989*9880d681SAndroid Build Coastguard Worker static bool OnlyOnRHSOfCommutative(TreePatternNode *N) {
1990*9880d681SAndroid Build Coastguard Worker if (!N->isLeaf() && N->getOperator()->getName() == "imm")
1991*9880d681SAndroid Build Coastguard Worker return true;
1992*9880d681SAndroid Build Coastguard Worker if (N->isLeaf() && isa<IntInit>(N->getLeafValue()))
1993*9880d681SAndroid Build Coastguard Worker return true;
1994*9880d681SAndroid Build Coastguard Worker return false;
1995*9880d681SAndroid Build Coastguard Worker }
1996*9880d681SAndroid Build Coastguard Worker
1997*9880d681SAndroid Build Coastguard Worker
1998*9880d681SAndroid Build Coastguard Worker /// canPatternMatch - If it is impossible for this pattern to match on this
1999*9880d681SAndroid Build Coastguard Worker /// target, fill in Reason and return false. Otherwise, return true. This is
2000*9880d681SAndroid Build Coastguard Worker /// used as a sanity check for .td files (to prevent people from writing stuff
2001*9880d681SAndroid Build Coastguard Worker /// that can never possibly work), and to prevent the pattern permuter from
2002*9880d681SAndroid Build Coastguard Worker /// generating stuff that is useless.
canPatternMatch(std::string & Reason,const CodeGenDAGPatterns & CDP)2003*9880d681SAndroid Build Coastguard Worker bool TreePatternNode::canPatternMatch(std::string &Reason,
2004*9880d681SAndroid Build Coastguard Worker const CodeGenDAGPatterns &CDP) {
2005*9880d681SAndroid Build Coastguard Worker if (isLeaf()) return true;
2006*9880d681SAndroid Build Coastguard Worker
2007*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2008*9880d681SAndroid Build Coastguard Worker if (!getChild(i)->canPatternMatch(Reason, CDP))
2009*9880d681SAndroid Build Coastguard Worker return false;
2010*9880d681SAndroid Build Coastguard Worker
2011*9880d681SAndroid Build Coastguard Worker // If this is an intrinsic, handle cases that would make it not match. For
2012*9880d681SAndroid Build Coastguard Worker // example, if an operand is required to be an immediate.
2013*9880d681SAndroid Build Coastguard Worker if (getOperator()->isSubClassOf("Intrinsic")) {
2014*9880d681SAndroid Build Coastguard Worker // TODO:
2015*9880d681SAndroid Build Coastguard Worker return true;
2016*9880d681SAndroid Build Coastguard Worker }
2017*9880d681SAndroid Build Coastguard Worker
2018*9880d681SAndroid Build Coastguard Worker if (getOperator()->isSubClassOf("ComplexPattern"))
2019*9880d681SAndroid Build Coastguard Worker return true;
2020*9880d681SAndroid Build Coastguard Worker
2021*9880d681SAndroid Build Coastguard Worker // If this node is a commutative operator, check that the LHS isn't an
2022*9880d681SAndroid Build Coastguard Worker // immediate.
2023*9880d681SAndroid Build Coastguard Worker const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator());
2024*9880d681SAndroid Build Coastguard Worker bool isCommIntrinsic = isCommutativeIntrinsic(CDP);
2025*9880d681SAndroid Build Coastguard Worker if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
2026*9880d681SAndroid Build Coastguard Worker // Scan all of the operands of the node and make sure that only the last one
2027*9880d681SAndroid Build Coastguard Worker // is a constant node, unless the RHS also is.
2028*9880d681SAndroid Build Coastguard Worker if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) {
2029*9880d681SAndroid Build Coastguard Worker bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id.
2030*9880d681SAndroid Build Coastguard Worker for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i)
2031*9880d681SAndroid Build Coastguard Worker if (OnlyOnRHSOfCommutative(getChild(i))) {
2032*9880d681SAndroid Build Coastguard Worker Reason="Immediate value must be on the RHS of commutative operators!";
2033*9880d681SAndroid Build Coastguard Worker return false;
2034*9880d681SAndroid Build Coastguard Worker }
2035*9880d681SAndroid Build Coastguard Worker }
2036*9880d681SAndroid Build Coastguard Worker }
2037*9880d681SAndroid Build Coastguard Worker
2038*9880d681SAndroid Build Coastguard Worker return true;
2039*9880d681SAndroid Build Coastguard Worker }
2040*9880d681SAndroid Build Coastguard Worker
2041*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
2042*9880d681SAndroid Build Coastguard Worker // TreePattern implementation
2043*9880d681SAndroid Build Coastguard Worker //
2044*9880d681SAndroid Build Coastguard Worker
TreePattern(Record * TheRec,ListInit * RawPat,bool isInput,CodeGenDAGPatterns & cdp)2045*9880d681SAndroid Build Coastguard Worker TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
2046*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2047*9880d681SAndroid Build Coastguard Worker isInputPattern(isInput), HasError(false) {
2048*9880d681SAndroid Build Coastguard Worker for (Init *I : RawPat->getValues())
2049*9880d681SAndroid Build Coastguard Worker Trees.push_back(ParseTreePattern(I, ""));
2050*9880d681SAndroid Build Coastguard Worker }
2051*9880d681SAndroid Build Coastguard Worker
TreePattern(Record * TheRec,DagInit * Pat,bool isInput,CodeGenDAGPatterns & cdp)2052*9880d681SAndroid Build Coastguard Worker TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
2053*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2054*9880d681SAndroid Build Coastguard Worker isInputPattern(isInput), HasError(false) {
2055*9880d681SAndroid Build Coastguard Worker Trees.push_back(ParseTreePattern(Pat, ""));
2056*9880d681SAndroid Build Coastguard Worker }
2057*9880d681SAndroid Build Coastguard Worker
TreePattern(Record * TheRec,TreePatternNode * Pat,bool isInput,CodeGenDAGPatterns & cdp)2058*9880d681SAndroid Build Coastguard Worker TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
2059*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2060*9880d681SAndroid Build Coastguard Worker isInputPattern(isInput), HasError(false) {
2061*9880d681SAndroid Build Coastguard Worker Trees.push_back(Pat);
2062*9880d681SAndroid Build Coastguard Worker }
2063*9880d681SAndroid Build Coastguard Worker
error(const Twine & Msg)2064*9880d681SAndroid Build Coastguard Worker void TreePattern::error(const Twine &Msg) {
2065*9880d681SAndroid Build Coastguard Worker if (HasError)
2066*9880d681SAndroid Build Coastguard Worker return;
2067*9880d681SAndroid Build Coastguard Worker dump();
2068*9880d681SAndroid Build Coastguard Worker PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg);
2069*9880d681SAndroid Build Coastguard Worker HasError = true;
2070*9880d681SAndroid Build Coastguard Worker }
2071*9880d681SAndroid Build Coastguard Worker
ComputeNamedNodes()2072*9880d681SAndroid Build Coastguard Worker void TreePattern::ComputeNamedNodes() {
2073*9880d681SAndroid Build Coastguard Worker for (TreePatternNode *Tree : Trees)
2074*9880d681SAndroid Build Coastguard Worker ComputeNamedNodes(Tree);
2075*9880d681SAndroid Build Coastguard Worker }
2076*9880d681SAndroid Build Coastguard Worker
ComputeNamedNodes(TreePatternNode * N)2077*9880d681SAndroid Build Coastguard Worker void TreePattern::ComputeNamedNodes(TreePatternNode *N) {
2078*9880d681SAndroid Build Coastguard Worker if (!N->getName().empty())
2079*9880d681SAndroid Build Coastguard Worker NamedNodes[N->getName()].push_back(N);
2080*9880d681SAndroid Build Coastguard Worker
2081*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2082*9880d681SAndroid Build Coastguard Worker ComputeNamedNodes(N->getChild(i));
2083*9880d681SAndroid Build Coastguard Worker }
2084*9880d681SAndroid Build Coastguard Worker
2085*9880d681SAndroid Build Coastguard Worker
ParseTreePattern(Init * TheInit,StringRef OpName)2086*9880d681SAndroid Build Coastguard Worker TreePatternNode *TreePattern::ParseTreePattern(Init *TheInit, StringRef OpName){
2087*9880d681SAndroid Build Coastguard Worker if (DefInit *DI = dyn_cast<DefInit>(TheInit)) {
2088*9880d681SAndroid Build Coastguard Worker Record *R = DI->getDef();
2089*9880d681SAndroid Build Coastguard Worker
2090*9880d681SAndroid Build Coastguard Worker // Direct reference to a leaf DagNode or PatFrag? Turn it into a
2091*9880d681SAndroid Build Coastguard Worker // TreePatternNode of its own. For example:
2092*9880d681SAndroid Build Coastguard Worker /// (foo GPR, imm) -> (foo GPR, (imm))
2093*9880d681SAndroid Build Coastguard Worker if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag"))
2094*9880d681SAndroid Build Coastguard Worker return ParseTreePattern(
2095*9880d681SAndroid Build Coastguard Worker DagInit::get(DI, "",
2096*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<Init*, std::string> >()),
2097*9880d681SAndroid Build Coastguard Worker OpName);
2098*9880d681SAndroid Build Coastguard Worker
2099*9880d681SAndroid Build Coastguard Worker // Input argument?
2100*9880d681SAndroid Build Coastguard Worker TreePatternNode *Res = new TreePatternNode(DI, 1);
2101*9880d681SAndroid Build Coastguard Worker if (R->getName() == "node" && !OpName.empty()) {
2102*9880d681SAndroid Build Coastguard Worker if (OpName.empty())
2103*9880d681SAndroid Build Coastguard Worker error("'node' argument requires a name to match with operand list");
2104*9880d681SAndroid Build Coastguard Worker Args.push_back(OpName);
2105*9880d681SAndroid Build Coastguard Worker }
2106*9880d681SAndroid Build Coastguard Worker
2107*9880d681SAndroid Build Coastguard Worker Res->setName(OpName);
2108*9880d681SAndroid Build Coastguard Worker return Res;
2109*9880d681SAndroid Build Coastguard Worker }
2110*9880d681SAndroid Build Coastguard Worker
2111*9880d681SAndroid Build Coastguard Worker // ?:$name or just $name.
2112*9880d681SAndroid Build Coastguard Worker if (isa<UnsetInit>(TheInit)) {
2113*9880d681SAndroid Build Coastguard Worker if (OpName.empty())
2114*9880d681SAndroid Build Coastguard Worker error("'?' argument requires a name to match with operand list");
2115*9880d681SAndroid Build Coastguard Worker TreePatternNode *Res = new TreePatternNode(TheInit, 1);
2116*9880d681SAndroid Build Coastguard Worker Args.push_back(OpName);
2117*9880d681SAndroid Build Coastguard Worker Res->setName(OpName);
2118*9880d681SAndroid Build Coastguard Worker return Res;
2119*9880d681SAndroid Build Coastguard Worker }
2120*9880d681SAndroid Build Coastguard Worker
2121*9880d681SAndroid Build Coastguard Worker if (IntInit *II = dyn_cast<IntInit>(TheInit)) {
2122*9880d681SAndroid Build Coastguard Worker if (!OpName.empty())
2123*9880d681SAndroid Build Coastguard Worker error("Constant int argument should not have a name!");
2124*9880d681SAndroid Build Coastguard Worker return new TreePatternNode(II, 1);
2125*9880d681SAndroid Build Coastguard Worker }
2126*9880d681SAndroid Build Coastguard Worker
2127*9880d681SAndroid Build Coastguard Worker if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) {
2128*9880d681SAndroid Build Coastguard Worker // Turn this into an IntInit.
2129*9880d681SAndroid Build Coastguard Worker Init *II = BI->convertInitializerTo(IntRecTy::get());
2130*9880d681SAndroid Build Coastguard Worker if (!II || !isa<IntInit>(II))
2131*9880d681SAndroid Build Coastguard Worker error("Bits value must be constants!");
2132*9880d681SAndroid Build Coastguard Worker return ParseTreePattern(II, OpName);
2133*9880d681SAndroid Build Coastguard Worker }
2134*9880d681SAndroid Build Coastguard Worker
2135*9880d681SAndroid Build Coastguard Worker DagInit *Dag = dyn_cast<DagInit>(TheInit);
2136*9880d681SAndroid Build Coastguard Worker if (!Dag) {
2137*9880d681SAndroid Build Coastguard Worker TheInit->dump();
2138*9880d681SAndroid Build Coastguard Worker error("Pattern has unexpected init kind!");
2139*9880d681SAndroid Build Coastguard Worker }
2140*9880d681SAndroid Build Coastguard Worker DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
2141*9880d681SAndroid Build Coastguard Worker if (!OpDef) error("Pattern has unexpected operator type!");
2142*9880d681SAndroid Build Coastguard Worker Record *Operator = OpDef->getDef();
2143*9880d681SAndroid Build Coastguard Worker
2144*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("ValueType")) {
2145*9880d681SAndroid Build Coastguard Worker // If the operator is a ValueType, then this must be "type cast" of a leaf
2146*9880d681SAndroid Build Coastguard Worker // node.
2147*9880d681SAndroid Build Coastguard Worker if (Dag->getNumArgs() != 1)
2148*9880d681SAndroid Build Coastguard Worker error("Type cast only takes one operand!");
2149*9880d681SAndroid Build Coastguard Worker
2150*9880d681SAndroid Build Coastguard Worker TreePatternNode *New = ParseTreePattern(Dag->getArg(0), Dag->getArgName(0));
2151*9880d681SAndroid Build Coastguard Worker
2152*9880d681SAndroid Build Coastguard Worker // Apply the type cast.
2153*9880d681SAndroid Build Coastguard Worker assert(New->getNumTypes() == 1 && "FIXME: Unhandled");
2154*9880d681SAndroid Build Coastguard Worker New->UpdateNodeType(0, getValueType(Operator), *this);
2155*9880d681SAndroid Build Coastguard Worker
2156*9880d681SAndroid Build Coastguard Worker if (!OpName.empty())
2157*9880d681SAndroid Build Coastguard Worker error("ValueType cast should not have a name!");
2158*9880d681SAndroid Build Coastguard Worker return New;
2159*9880d681SAndroid Build Coastguard Worker }
2160*9880d681SAndroid Build Coastguard Worker
2161*9880d681SAndroid Build Coastguard Worker // Verify that this is something that makes sense for an operator.
2162*9880d681SAndroid Build Coastguard Worker if (!Operator->isSubClassOf("PatFrag") &&
2163*9880d681SAndroid Build Coastguard Worker !Operator->isSubClassOf("SDNode") &&
2164*9880d681SAndroid Build Coastguard Worker !Operator->isSubClassOf("Instruction") &&
2165*9880d681SAndroid Build Coastguard Worker !Operator->isSubClassOf("SDNodeXForm") &&
2166*9880d681SAndroid Build Coastguard Worker !Operator->isSubClassOf("Intrinsic") &&
2167*9880d681SAndroid Build Coastguard Worker !Operator->isSubClassOf("ComplexPattern") &&
2168*9880d681SAndroid Build Coastguard Worker Operator->getName() != "set" &&
2169*9880d681SAndroid Build Coastguard Worker Operator->getName() != "implicit")
2170*9880d681SAndroid Build Coastguard Worker error("Unrecognized node '" + Operator->getName() + "'!");
2171*9880d681SAndroid Build Coastguard Worker
2172*9880d681SAndroid Build Coastguard Worker // Check to see if this is something that is illegal in an input pattern.
2173*9880d681SAndroid Build Coastguard Worker if (isInputPattern) {
2174*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("Instruction") ||
2175*9880d681SAndroid Build Coastguard Worker Operator->isSubClassOf("SDNodeXForm"))
2176*9880d681SAndroid Build Coastguard Worker error("Cannot use '" + Operator->getName() + "' in an input pattern!");
2177*9880d681SAndroid Build Coastguard Worker } else {
2178*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("Intrinsic"))
2179*9880d681SAndroid Build Coastguard Worker error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2180*9880d681SAndroid Build Coastguard Worker
2181*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("SDNode") &&
2182*9880d681SAndroid Build Coastguard Worker Operator->getName() != "imm" &&
2183*9880d681SAndroid Build Coastguard Worker Operator->getName() != "fpimm" &&
2184*9880d681SAndroid Build Coastguard Worker Operator->getName() != "tglobaltlsaddr" &&
2185*9880d681SAndroid Build Coastguard Worker Operator->getName() != "tconstpool" &&
2186*9880d681SAndroid Build Coastguard Worker Operator->getName() != "tjumptable" &&
2187*9880d681SAndroid Build Coastguard Worker Operator->getName() != "tframeindex" &&
2188*9880d681SAndroid Build Coastguard Worker Operator->getName() != "texternalsym" &&
2189*9880d681SAndroid Build Coastguard Worker Operator->getName() != "tblockaddress" &&
2190*9880d681SAndroid Build Coastguard Worker Operator->getName() != "tglobaladdr" &&
2191*9880d681SAndroid Build Coastguard Worker Operator->getName() != "bb" &&
2192*9880d681SAndroid Build Coastguard Worker Operator->getName() != "vt" &&
2193*9880d681SAndroid Build Coastguard Worker Operator->getName() != "mcsym")
2194*9880d681SAndroid Build Coastguard Worker error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2195*9880d681SAndroid Build Coastguard Worker }
2196*9880d681SAndroid Build Coastguard Worker
2197*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> Children;
2198*9880d681SAndroid Build Coastguard Worker
2199*9880d681SAndroid Build Coastguard Worker // Parse all the operands.
2200*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i)
2201*9880d681SAndroid Build Coastguard Worker Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgName(i)));
2202*9880d681SAndroid Build Coastguard Worker
2203*9880d681SAndroid Build Coastguard Worker // If the operator is an intrinsic, then this is just syntactic sugar for for
2204*9880d681SAndroid Build Coastguard Worker // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and
2205*9880d681SAndroid Build Coastguard Worker // convert the intrinsic name to a number.
2206*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("Intrinsic")) {
2207*9880d681SAndroid Build Coastguard Worker const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator);
2208*9880d681SAndroid Build Coastguard Worker unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1;
2209*9880d681SAndroid Build Coastguard Worker
2210*9880d681SAndroid Build Coastguard Worker // If this intrinsic returns void, it must have side-effects and thus a
2211*9880d681SAndroid Build Coastguard Worker // chain.
2212*9880d681SAndroid Build Coastguard Worker if (Int.IS.RetVTs.empty())
2213*9880d681SAndroid Build Coastguard Worker Operator = getDAGPatterns().get_intrinsic_void_sdnode();
2214*9880d681SAndroid Build Coastguard Worker else if (Int.ModRef != CodeGenIntrinsic::NoMem)
2215*9880d681SAndroid Build Coastguard Worker // Has side-effects, requires chain.
2216*9880d681SAndroid Build Coastguard Worker Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode();
2217*9880d681SAndroid Build Coastguard Worker else // Otherwise, no chain.
2218*9880d681SAndroid Build Coastguard Worker Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode();
2219*9880d681SAndroid Build Coastguard Worker
2220*9880d681SAndroid Build Coastguard Worker TreePatternNode *IIDNode = new TreePatternNode(IntInit::get(IID), 1);
2221*9880d681SAndroid Build Coastguard Worker Children.insert(Children.begin(), IIDNode);
2222*9880d681SAndroid Build Coastguard Worker }
2223*9880d681SAndroid Build Coastguard Worker
2224*9880d681SAndroid Build Coastguard Worker if (Operator->isSubClassOf("ComplexPattern")) {
2225*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < Children.size(); ++i) {
2226*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = Children[i];
2227*9880d681SAndroid Build Coastguard Worker
2228*9880d681SAndroid Build Coastguard Worker if (Child->getName().empty())
2229*9880d681SAndroid Build Coastguard Worker error("All arguments to a ComplexPattern must be named");
2230*9880d681SAndroid Build Coastguard Worker
2231*9880d681SAndroid Build Coastguard Worker // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
2232*9880d681SAndroid Build Coastguard Worker // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
2233*9880d681SAndroid Build Coastguard Worker // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
2234*9880d681SAndroid Build Coastguard Worker auto OperandId = std::make_pair(Operator, i);
2235*9880d681SAndroid Build Coastguard Worker auto PrevOp = ComplexPatternOperands.find(Child->getName());
2236*9880d681SAndroid Build Coastguard Worker if (PrevOp != ComplexPatternOperands.end()) {
2237*9880d681SAndroid Build Coastguard Worker if (PrevOp->getValue() != OperandId)
2238*9880d681SAndroid Build Coastguard Worker error("All ComplexPattern operands must appear consistently: "
2239*9880d681SAndroid Build Coastguard Worker "in the same order in just one ComplexPattern instance.");
2240*9880d681SAndroid Build Coastguard Worker } else
2241*9880d681SAndroid Build Coastguard Worker ComplexPatternOperands[Child->getName()] = OperandId;
2242*9880d681SAndroid Build Coastguard Worker }
2243*9880d681SAndroid Build Coastguard Worker }
2244*9880d681SAndroid Build Coastguard Worker
2245*9880d681SAndroid Build Coastguard Worker unsigned NumResults = GetNumNodeResults(Operator, CDP);
2246*9880d681SAndroid Build Coastguard Worker TreePatternNode *Result = new TreePatternNode(Operator, Children, NumResults);
2247*9880d681SAndroid Build Coastguard Worker Result->setName(OpName);
2248*9880d681SAndroid Build Coastguard Worker
2249*9880d681SAndroid Build Coastguard Worker if (!Dag->getName().empty()) {
2250*9880d681SAndroid Build Coastguard Worker assert(Result->getName().empty());
2251*9880d681SAndroid Build Coastguard Worker Result->setName(Dag->getName());
2252*9880d681SAndroid Build Coastguard Worker }
2253*9880d681SAndroid Build Coastguard Worker return Result;
2254*9880d681SAndroid Build Coastguard Worker }
2255*9880d681SAndroid Build Coastguard Worker
2256*9880d681SAndroid Build Coastguard Worker /// SimplifyTree - See if we can simplify this tree to eliminate something that
2257*9880d681SAndroid Build Coastguard Worker /// will never match in favor of something obvious that will. This is here
2258*9880d681SAndroid Build Coastguard Worker /// strictly as a convenience to target authors because it allows them to write
2259*9880d681SAndroid Build Coastguard Worker /// more type generic things and have useless type casts fold away.
2260*9880d681SAndroid Build Coastguard Worker ///
2261*9880d681SAndroid Build Coastguard Worker /// This returns true if any change is made.
SimplifyTree(TreePatternNode * & N)2262*9880d681SAndroid Build Coastguard Worker static bool SimplifyTree(TreePatternNode *&N) {
2263*9880d681SAndroid Build Coastguard Worker if (N->isLeaf())
2264*9880d681SAndroid Build Coastguard Worker return false;
2265*9880d681SAndroid Build Coastguard Worker
2266*9880d681SAndroid Build Coastguard Worker // If we have a bitconvert with a resolved type and if the source and
2267*9880d681SAndroid Build Coastguard Worker // destination types are the same, then the bitconvert is useless, remove it.
2268*9880d681SAndroid Build Coastguard Worker if (N->getOperator()->getName() == "bitconvert" &&
2269*9880d681SAndroid Build Coastguard Worker N->getExtType(0).isConcrete() &&
2270*9880d681SAndroid Build Coastguard Worker N->getExtType(0) == N->getChild(0)->getExtType(0) &&
2271*9880d681SAndroid Build Coastguard Worker N->getName().empty()) {
2272*9880d681SAndroid Build Coastguard Worker N = N->getChild(0);
2273*9880d681SAndroid Build Coastguard Worker SimplifyTree(N);
2274*9880d681SAndroid Build Coastguard Worker return true;
2275*9880d681SAndroid Build Coastguard Worker }
2276*9880d681SAndroid Build Coastguard Worker
2277*9880d681SAndroid Build Coastguard Worker // Walk all children.
2278*9880d681SAndroid Build Coastguard Worker bool MadeChange = false;
2279*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
2280*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = N->getChild(i);
2281*9880d681SAndroid Build Coastguard Worker MadeChange |= SimplifyTree(Child);
2282*9880d681SAndroid Build Coastguard Worker N->setChild(i, Child);
2283*9880d681SAndroid Build Coastguard Worker }
2284*9880d681SAndroid Build Coastguard Worker return MadeChange;
2285*9880d681SAndroid Build Coastguard Worker }
2286*9880d681SAndroid Build Coastguard Worker
2287*9880d681SAndroid Build Coastguard Worker
2288*9880d681SAndroid Build Coastguard Worker
2289*9880d681SAndroid Build Coastguard Worker /// InferAllTypes - Infer/propagate as many types throughout the expression
2290*9880d681SAndroid Build Coastguard Worker /// patterns as possible. Return true if all types are inferred, false
2291*9880d681SAndroid Build Coastguard Worker /// otherwise. Flags an error if a type contradiction is found.
2292*9880d681SAndroid Build Coastguard Worker bool TreePattern::
InferAllTypes(const StringMap<SmallVector<TreePatternNode *,1>> * InNamedTypes)2293*9880d681SAndroid Build Coastguard Worker InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) {
2294*9880d681SAndroid Build Coastguard Worker if (NamedNodes.empty())
2295*9880d681SAndroid Build Coastguard Worker ComputeNamedNodes();
2296*9880d681SAndroid Build Coastguard Worker
2297*9880d681SAndroid Build Coastguard Worker bool MadeChange = true;
2298*9880d681SAndroid Build Coastguard Worker while (MadeChange) {
2299*9880d681SAndroid Build Coastguard Worker MadeChange = false;
2300*9880d681SAndroid Build Coastguard Worker for (TreePatternNode *Tree : Trees) {
2301*9880d681SAndroid Build Coastguard Worker MadeChange |= Tree->ApplyTypeConstraints(*this, false);
2302*9880d681SAndroid Build Coastguard Worker MadeChange |= SimplifyTree(Tree);
2303*9880d681SAndroid Build Coastguard Worker }
2304*9880d681SAndroid Build Coastguard Worker
2305*9880d681SAndroid Build Coastguard Worker // If there are constraints on our named nodes, apply them.
2306*9880d681SAndroid Build Coastguard Worker for (auto &Entry : NamedNodes) {
2307*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<TreePatternNode*> &Nodes = Entry.second;
2308*9880d681SAndroid Build Coastguard Worker
2309*9880d681SAndroid Build Coastguard Worker // If we have input named node types, propagate their types to the named
2310*9880d681SAndroid Build Coastguard Worker // values here.
2311*9880d681SAndroid Build Coastguard Worker if (InNamedTypes) {
2312*9880d681SAndroid Build Coastguard Worker if (!InNamedTypes->count(Entry.getKey())) {
2313*9880d681SAndroid Build Coastguard Worker error("Node '" + std::string(Entry.getKey()) +
2314*9880d681SAndroid Build Coastguard Worker "' in output pattern but not input pattern");
2315*9880d681SAndroid Build Coastguard Worker return true;
2316*9880d681SAndroid Build Coastguard Worker }
2317*9880d681SAndroid Build Coastguard Worker
2318*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<TreePatternNode*> &InNodes =
2319*9880d681SAndroid Build Coastguard Worker InNamedTypes->find(Entry.getKey())->second;
2320*9880d681SAndroid Build Coastguard Worker
2321*9880d681SAndroid Build Coastguard Worker // The input types should be fully resolved by now.
2322*9880d681SAndroid Build Coastguard Worker for (TreePatternNode *Node : Nodes) {
2323*9880d681SAndroid Build Coastguard Worker // If this node is a register class, and it is the root of the pattern
2324*9880d681SAndroid Build Coastguard Worker // then we're mapping something onto an input register. We allow
2325*9880d681SAndroid Build Coastguard Worker // changing the type of the input register in this case. This allows
2326*9880d681SAndroid Build Coastguard Worker // us to match things like:
2327*9880d681SAndroid Build Coastguard Worker // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
2328*9880d681SAndroid Build Coastguard Worker if (Node == Trees[0] && Node->isLeaf()) {
2329*9880d681SAndroid Build Coastguard Worker DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
2330*9880d681SAndroid Build Coastguard Worker if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
2331*9880d681SAndroid Build Coastguard Worker DI->getDef()->isSubClassOf("RegisterOperand")))
2332*9880d681SAndroid Build Coastguard Worker continue;
2333*9880d681SAndroid Build Coastguard Worker }
2334*9880d681SAndroid Build Coastguard Worker
2335*9880d681SAndroid Build Coastguard Worker assert(Node->getNumTypes() == 1 &&
2336*9880d681SAndroid Build Coastguard Worker InNodes[0]->getNumTypes() == 1 &&
2337*9880d681SAndroid Build Coastguard Worker "FIXME: cannot name multiple result nodes yet");
2338*9880d681SAndroid Build Coastguard Worker MadeChange |= Node->UpdateNodeType(0, InNodes[0]->getExtType(0),
2339*9880d681SAndroid Build Coastguard Worker *this);
2340*9880d681SAndroid Build Coastguard Worker }
2341*9880d681SAndroid Build Coastguard Worker }
2342*9880d681SAndroid Build Coastguard Worker
2343*9880d681SAndroid Build Coastguard Worker // If there are multiple nodes with the same name, they must all have the
2344*9880d681SAndroid Build Coastguard Worker // same type.
2345*9880d681SAndroid Build Coastguard Worker if (Entry.second.size() > 1) {
2346*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) {
2347*9880d681SAndroid Build Coastguard Worker TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1];
2348*9880d681SAndroid Build Coastguard Worker assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&
2349*9880d681SAndroid Build Coastguard Worker "FIXME: cannot name multiple result nodes yet");
2350*9880d681SAndroid Build Coastguard Worker
2351*9880d681SAndroid Build Coastguard Worker MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this);
2352*9880d681SAndroid Build Coastguard Worker MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this);
2353*9880d681SAndroid Build Coastguard Worker }
2354*9880d681SAndroid Build Coastguard Worker }
2355*9880d681SAndroid Build Coastguard Worker }
2356*9880d681SAndroid Build Coastguard Worker }
2357*9880d681SAndroid Build Coastguard Worker
2358*9880d681SAndroid Build Coastguard Worker bool HasUnresolvedTypes = false;
2359*9880d681SAndroid Build Coastguard Worker for (const TreePatternNode *Tree : Trees)
2360*9880d681SAndroid Build Coastguard Worker HasUnresolvedTypes |= Tree->ContainsUnresolvedType();
2361*9880d681SAndroid Build Coastguard Worker return !HasUnresolvedTypes;
2362*9880d681SAndroid Build Coastguard Worker }
2363*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const2364*9880d681SAndroid Build Coastguard Worker void TreePattern::print(raw_ostream &OS) const {
2365*9880d681SAndroid Build Coastguard Worker OS << getRecord()->getName();
2366*9880d681SAndroid Build Coastguard Worker if (!Args.empty()) {
2367*9880d681SAndroid Build Coastguard Worker OS << "(" << Args[0];
2368*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Args.size(); i != e; ++i)
2369*9880d681SAndroid Build Coastguard Worker OS << ", " << Args[i];
2370*9880d681SAndroid Build Coastguard Worker OS << ")";
2371*9880d681SAndroid Build Coastguard Worker }
2372*9880d681SAndroid Build Coastguard Worker OS << ": ";
2373*9880d681SAndroid Build Coastguard Worker
2374*9880d681SAndroid Build Coastguard Worker if (Trees.size() > 1)
2375*9880d681SAndroid Build Coastguard Worker OS << "[\n";
2376*9880d681SAndroid Build Coastguard Worker for (const TreePatternNode *Tree : Trees) {
2377*9880d681SAndroid Build Coastguard Worker OS << "\t";
2378*9880d681SAndroid Build Coastguard Worker Tree->print(OS);
2379*9880d681SAndroid Build Coastguard Worker OS << "\n";
2380*9880d681SAndroid Build Coastguard Worker }
2381*9880d681SAndroid Build Coastguard Worker
2382*9880d681SAndroid Build Coastguard Worker if (Trees.size() > 1)
2383*9880d681SAndroid Build Coastguard Worker OS << "]\n";
2384*9880d681SAndroid Build Coastguard Worker }
2385*9880d681SAndroid Build Coastguard Worker
dump() const2386*9880d681SAndroid Build Coastguard Worker void TreePattern::dump() const { print(errs()); }
2387*9880d681SAndroid Build Coastguard Worker
2388*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
2389*9880d681SAndroid Build Coastguard Worker // CodeGenDAGPatterns implementation
2390*9880d681SAndroid Build Coastguard Worker //
2391*9880d681SAndroid Build Coastguard Worker
CodeGenDAGPatterns(RecordKeeper & R)2392*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) :
2393*9880d681SAndroid Build Coastguard Worker Records(R), Target(R) {
2394*9880d681SAndroid Build Coastguard Worker
2395*9880d681SAndroid Build Coastguard Worker Intrinsics = LoadIntrinsics(Records, false);
2396*9880d681SAndroid Build Coastguard Worker TgtIntrinsics = LoadIntrinsics(Records, true);
2397*9880d681SAndroid Build Coastguard Worker ParseNodeInfo();
2398*9880d681SAndroid Build Coastguard Worker ParseNodeTransforms();
2399*9880d681SAndroid Build Coastguard Worker ParseComplexPatterns();
2400*9880d681SAndroid Build Coastguard Worker ParsePatternFragments();
2401*9880d681SAndroid Build Coastguard Worker ParseDefaultOperands();
2402*9880d681SAndroid Build Coastguard Worker ParseInstructions();
2403*9880d681SAndroid Build Coastguard Worker ParsePatternFragments(/*OutFrags*/true);
2404*9880d681SAndroid Build Coastguard Worker ParsePatterns();
2405*9880d681SAndroid Build Coastguard Worker
2406*9880d681SAndroid Build Coastguard Worker // Generate variants. For example, commutative patterns can match
2407*9880d681SAndroid Build Coastguard Worker // multiple ways. Add them to PatternsToMatch as well.
2408*9880d681SAndroid Build Coastguard Worker GenerateVariants();
2409*9880d681SAndroid Build Coastguard Worker
2410*9880d681SAndroid Build Coastguard Worker // Infer instruction flags. For example, we can detect loads,
2411*9880d681SAndroid Build Coastguard Worker // stores, and side effects in many cases by examining an
2412*9880d681SAndroid Build Coastguard Worker // instruction's pattern.
2413*9880d681SAndroid Build Coastguard Worker InferInstructionFlags();
2414*9880d681SAndroid Build Coastguard Worker
2415*9880d681SAndroid Build Coastguard Worker // Verify that instruction flags match the patterns.
2416*9880d681SAndroid Build Coastguard Worker VerifyInstructionFlags();
2417*9880d681SAndroid Build Coastguard Worker }
2418*9880d681SAndroid Build Coastguard Worker
getSDNodeNamed(const std::string & Name) const2419*9880d681SAndroid Build Coastguard Worker Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const {
2420*9880d681SAndroid Build Coastguard Worker Record *N = Records.getDef(Name);
2421*9880d681SAndroid Build Coastguard Worker if (!N || !N->isSubClassOf("SDNode"))
2422*9880d681SAndroid Build Coastguard Worker PrintFatalError("Error getting SDNode '" + Name + "'!");
2423*9880d681SAndroid Build Coastguard Worker
2424*9880d681SAndroid Build Coastguard Worker return N;
2425*9880d681SAndroid Build Coastguard Worker }
2426*9880d681SAndroid Build Coastguard Worker
2427*9880d681SAndroid Build Coastguard Worker // Parse all of the SDNode definitions for the target, populating SDNodes.
ParseNodeInfo()2428*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParseNodeInfo() {
2429*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode");
2430*9880d681SAndroid Build Coastguard Worker while (!Nodes.empty()) {
2431*9880d681SAndroid Build Coastguard Worker SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back()));
2432*9880d681SAndroid Build Coastguard Worker Nodes.pop_back();
2433*9880d681SAndroid Build Coastguard Worker }
2434*9880d681SAndroid Build Coastguard Worker
2435*9880d681SAndroid Build Coastguard Worker // Get the builtin intrinsic nodes.
2436*9880d681SAndroid Build Coastguard Worker intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void");
2437*9880d681SAndroid Build Coastguard Worker intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain");
2438*9880d681SAndroid Build Coastguard Worker intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain");
2439*9880d681SAndroid Build Coastguard Worker }
2440*9880d681SAndroid Build Coastguard Worker
2441*9880d681SAndroid Build Coastguard Worker /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
2442*9880d681SAndroid Build Coastguard Worker /// map, and emit them to the file as functions.
ParseNodeTransforms()2443*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParseNodeTransforms() {
2444*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm");
2445*9880d681SAndroid Build Coastguard Worker while (!Xforms.empty()) {
2446*9880d681SAndroid Build Coastguard Worker Record *XFormNode = Xforms.back();
2447*9880d681SAndroid Build Coastguard Worker Record *SDNode = XFormNode->getValueAsDef("Opcode");
2448*9880d681SAndroid Build Coastguard Worker std::string Code = XFormNode->getValueAsString("XFormFunction");
2449*9880d681SAndroid Build Coastguard Worker SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code)));
2450*9880d681SAndroid Build Coastguard Worker
2451*9880d681SAndroid Build Coastguard Worker Xforms.pop_back();
2452*9880d681SAndroid Build Coastguard Worker }
2453*9880d681SAndroid Build Coastguard Worker }
2454*9880d681SAndroid Build Coastguard Worker
ParseComplexPatterns()2455*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParseComplexPatterns() {
2456*9880d681SAndroid Build Coastguard Worker std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern");
2457*9880d681SAndroid Build Coastguard Worker while (!AMs.empty()) {
2458*9880d681SAndroid Build Coastguard Worker ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back()));
2459*9880d681SAndroid Build Coastguard Worker AMs.pop_back();
2460*9880d681SAndroid Build Coastguard Worker }
2461*9880d681SAndroid Build Coastguard Worker }
2462*9880d681SAndroid Build Coastguard Worker
2463*9880d681SAndroid Build Coastguard Worker
2464*9880d681SAndroid Build Coastguard Worker /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
2465*9880d681SAndroid Build Coastguard Worker /// file, building up the PatternFragments map. After we've collected them all,
2466*9880d681SAndroid Build Coastguard Worker /// inline fragments together as necessary, so that there are no references left
2467*9880d681SAndroid Build Coastguard Worker /// inside a pattern fragment to a pattern fragment.
2468*9880d681SAndroid Build Coastguard Worker ///
ParsePatternFragments(bool OutFrags)2469*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
2470*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag");
2471*9880d681SAndroid Build Coastguard Worker
2472*9880d681SAndroid Build Coastguard Worker // First step, parse all of the fragments.
2473*9880d681SAndroid Build Coastguard Worker for (Record *Frag : Fragments) {
2474*9880d681SAndroid Build Coastguard Worker if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
2475*9880d681SAndroid Build Coastguard Worker continue;
2476*9880d681SAndroid Build Coastguard Worker
2477*9880d681SAndroid Build Coastguard Worker DagInit *Tree = Frag->getValueAsDag("Fragment");
2478*9880d681SAndroid Build Coastguard Worker TreePattern *P =
2479*9880d681SAndroid Build Coastguard Worker (PatternFragments[Frag] = llvm::make_unique<TreePattern>(
2480*9880d681SAndroid Build Coastguard Worker Frag, Tree, !Frag->isSubClassOf("OutPatFrag"),
2481*9880d681SAndroid Build Coastguard Worker *this)).get();
2482*9880d681SAndroid Build Coastguard Worker
2483*9880d681SAndroid Build Coastguard Worker // Validate the argument list, converting it to set, to discard duplicates.
2484*9880d681SAndroid Build Coastguard Worker std::vector<std::string> &Args = P->getArgList();
2485*9880d681SAndroid Build Coastguard Worker std::set<std::string> OperandsSet(Args.begin(), Args.end());
2486*9880d681SAndroid Build Coastguard Worker
2487*9880d681SAndroid Build Coastguard Worker if (OperandsSet.count(""))
2488*9880d681SAndroid Build Coastguard Worker P->error("Cannot have unnamed 'node' values in pattern fragment!");
2489*9880d681SAndroid Build Coastguard Worker
2490*9880d681SAndroid Build Coastguard Worker // Parse the operands list.
2491*9880d681SAndroid Build Coastguard Worker DagInit *OpsList = Frag->getValueAsDag("Operands");
2492*9880d681SAndroid Build Coastguard Worker DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator());
2493*9880d681SAndroid Build Coastguard Worker // Special cases: ops == outs == ins. Different names are used to
2494*9880d681SAndroid Build Coastguard Worker // improve readability.
2495*9880d681SAndroid Build Coastguard Worker if (!OpsOp ||
2496*9880d681SAndroid Build Coastguard Worker (OpsOp->getDef()->getName() != "ops" &&
2497*9880d681SAndroid Build Coastguard Worker OpsOp->getDef()->getName() != "outs" &&
2498*9880d681SAndroid Build Coastguard Worker OpsOp->getDef()->getName() != "ins"))
2499*9880d681SAndroid Build Coastguard Worker P->error("Operands list should start with '(ops ... '!");
2500*9880d681SAndroid Build Coastguard Worker
2501*9880d681SAndroid Build Coastguard Worker // Copy over the arguments.
2502*9880d681SAndroid Build Coastguard Worker Args.clear();
2503*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) {
2504*9880d681SAndroid Build Coastguard Worker if (!isa<DefInit>(OpsList->getArg(j)) ||
2505*9880d681SAndroid Build Coastguard Worker cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node")
2506*9880d681SAndroid Build Coastguard Worker P->error("Operands list should all be 'node' values.");
2507*9880d681SAndroid Build Coastguard Worker if (OpsList->getArgName(j).empty())
2508*9880d681SAndroid Build Coastguard Worker P->error("Operands list should have names for each operand!");
2509*9880d681SAndroid Build Coastguard Worker if (!OperandsSet.count(OpsList->getArgName(j)))
2510*9880d681SAndroid Build Coastguard Worker P->error("'" + OpsList->getArgName(j) +
2511*9880d681SAndroid Build Coastguard Worker "' does not occur in pattern or was multiply specified!");
2512*9880d681SAndroid Build Coastguard Worker OperandsSet.erase(OpsList->getArgName(j));
2513*9880d681SAndroid Build Coastguard Worker Args.push_back(OpsList->getArgName(j));
2514*9880d681SAndroid Build Coastguard Worker }
2515*9880d681SAndroid Build Coastguard Worker
2516*9880d681SAndroid Build Coastguard Worker if (!OperandsSet.empty())
2517*9880d681SAndroid Build Coastguard Worker P->error("Operands list does not contain an entry for operand '" +
2518*9880d681SAndroid Build Coastguard Worker *OperandsSet.begin() + "'!");
2519*9880d681SAndroid Build Coastguard Worker
2520*9880d681SAndroid Build Coastguard Worker // If there is a code init for this fragment, keep track of the fact that
2521*9880d681SAndroid Build Coastguard Worker // this fragment uses it.
2522*9880d681SAndroid Build Coastguard Worker TreePredicateFn PredFn(P);
2523*9880d681SAndroid Build Coastguard Worker if (!PredFn.isAlwaysTrue())
2524*9880d681SAndroid Build Coastguard Worker P->getOnlyTree()->addPredicateFn(PredFn);
2525*9880d681SAndroid Build Coastguard Worker
2526*9880d681SAndroid Build Coastguard Worker // If there is a node transformation corresponding to this, keep track of
2527*9880d681SAndroid Build Coastguard Worker // it.
2528*9880d681SAndroid Build Coastguard Worker Record *Transform = Frag->getValueAsDef("OperandTransform");
2529*9880d681SAndroid Build Coastguard Worker if (!getSDNodeTransform(Transform).second.empty()) // not noop xform?
2530*9880d681SAndroid Build Coastguard Worker P->getOnlyTree()->setTransformFn(Transform);
2531*9880d681SAndroid Build Coastguard Worker }
2532*9880d681SAndroid Build Coastguard Worker
2533*9880d681SAndroid Build Coastguard Worker // Now that we've parsed all of the tree fragments, do a closure on them so
2534*9880d681SAndroid Build Coastguard Worker // that there are not references to PatFrags left inside of them.
2535*9880d681SAndroid Build Coastguard Worker for (Record *Frag : Fragments) {
2536*9880d681SAndroid Build Coastguard Worker if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
2537*9880d681SAndroid Build Coastguard Worker continue;
2538*9880d681SAndroid Build Coastguard Worker
2539*9880d681SAndroid Build Coastguard Worker TreePattern &ThePat = *PatternFragments[Frag];
2540*9880d681SAndroid Build Coastguard Worker ThePat.InlinePatternFragments();
2541*9880d681SAndroid Build Coastguard Worker
2542*9880d681SAndroid Build Coastguard Worker // Infer as many types as possible. Don't worry about it if we don't infer
2543*9880d681SAndroid Build Coastguard Worker // all of them, some may depend on the inputs of the pattern.
2544*9880d681SAndroid Build Coastguard Worker ThePat.InferAllTypes();
2545*9880d681SAndroid Build Coastguard Worker ThePat.resetError();
2546*9880d681SAndroid Build Coastguard Worker
2547*9880d681SAndroid Build Coastguard Worker // If debugging, print out the pattern fragment result.
2548*9880d681SAndroid Build Coastguard Worker DEBUG(ThePat.dump());
2549*9880d681SAndroid Build Coastguard Worker }
2550*9880d681SAndroid Build Coastguard Worker }
2551*9880d681SAndroid Build Coastguard Worker
ParseDefaultOperands()2552*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParseDefaultOperands() {
2553*9880d681SAndroid Build Coastguard Worker std::vector<Record*> DefaultOps;
2554*9880d681SAndroid Build Coastguard Worker DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps");
2555*9880d681SAndroid Build Coastguard Worker
2556*9880d681SAndroid Build Coastguard Worker // Find some SDNode.
2557*9880d681SAndroid Build Coastguard Worker assert(!SDNodes.empty() && "No SDNodes parsed?");
2558*9880d681SAndroid Build Coastguard Worker Init *SomeSDNode = DefInit::get(SDNodes.begin()->first);
2559*9880d681SAndroid Build Coastguard Worker
2560*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) {
2561*9880d681SAndroid Build Coastguard Worker DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps");
2562*9880d681SAndroid Build Coastguard Worker
2563*9880d681SAndroid Build Coastguard Worker // Clone the DefaultInfo dag node, changing the operator from 'ops' to
2564*9880d681SAndroid Build Coastguard Worker // SomeSDnode so that we can parse this.
2565*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<Init*, std::string> > Ops;
2566*9880d681SAndroid Build Coastguard Worker for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op)
2567*9880d681SAndroid Build Coastguard Worker Ops.push_back(std::make_pair(DefaultInfo->getArg(op),
2568*9880d681SAndroid Build Coastguard Worker DefaultInfo->getArgName(op)));
2569*9880d681SAndroid Build Coastguard Worker DagInit *DI = DagInit::get(SomeSDNode, "", Ops);
2570*9880d681SAndroid Build Coastguard Worker
2571*9880d681SAndroid Build Coastguard Worker // Create a TreePattern to parse this.
2572*9880d681SAndroid Build Coastguard Worker TreePattern P(DefaultOps[i], DI, false, *this);
2573*9880d681SAndroid Build Coastguard Worker assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!");
2574*9880d681SAndroid Build Coastguard Worker
2575*9880d681SAndroid Build Coastguard Worker // Copy the operands over into a DAGDefaultOperand.
2576*9880d681SAndroid Build Coastguard Worker DAGDefaultOperand DefaultOpInfo;
2577*9880d681SAndroid Build Coastguard Worker
2578*9880d681SAndroid Build Coastguard Worker TreePatternNode *T = P.getTree(0);
2579*9880d681SAndroid Build Coastguard Worker for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) {
2580*9880d681SAndroid Build Coastguard Worker TreePatternNode *TPN = T->getChild(op);
2581*9880d681SAndroid Build Coastguard Worker while (TPN->ApplyTypeConstraints(P, false))
2582*9880d681SAndroid Build Coastguard Worker /* Resolve all types */;
2583*9880d681SAndroid Build Coastguard Worker
2584*9880d681SAndroid Build Coastguard Worker if (TPN->ContainsUnresolvedType()) {
2585*9880d681SAndroid Build Coastguard Worker PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" +
2586*9880d681SAndroid Build Coastguard Worker DefaultOps[i]->getName() +
2587*9880d681SAndroid Build Coastguard Worker "' doesn't have a concrete type!");
2588*9880d681SAndroid Build Coastguard Worker }
2589*9880d681SAndroid Build Coastguard Worker DefaultOpInfo.DefaultOps.push_back(TPN);
2590*9880d681SAndroid Build Coastguard Worker }
2591*9880d681SAndroid Build Coastguard Worker
2592*9880d681SAndroid Build Coastguard Worker // Insert it into the DefaultOperands map so we can find it later.
2593*9880d681SAndroid Build Coastguard Worker DefaultOperands[DefaultOps[i]] = DefaultOpInfo;
2594*9880d681SAndroid Build Coastguard Worker }
2595*9880d681SAndroid Build Coastguard Worker }
2596*9880d681SAndroid Build Coastguard Worker
2597*9880d681SAndroid Build Coastguard Worker /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
2598*9880d681SAndroid Build Coastguard Worker /// instruction input. Return true if this is a real use.
HandleUse(TreePattern * I,TreePatternNode * Pat,std::map<std::string,TreePatternNode * > & InstInputs)2599*9880d681SAndroid Build Coastguard Worker static bool HandleUse(TreePattern *I, TreePatternNode *Pat,
2600*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> &InstInputs) {
2601*9880d681SAndroid Build Coastguard Worker // No name -> not interesting.
2602*9880d681SAndroid Build Coastguard Worker if (Pat->getName().empty()) {
2603*9880d681SAndroid Build Coastguard Worker if (Pat->isLeaf()) {
2604*9880d681SAndroid Build Coastguard Worker DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
2605*9880d681SAndroid Build Coastguard Worker if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
2606*9880d681SAndroid Build Coastguard Worker DI->getDef()->isSubClassOf("RegisterOperand")))
2607*9880d681SAndroid Build Coastguard Worker I->error("Input " + DI->getDef()->getName() + " must be named!");
2608*9880d681SAndroid Build Coastguard Worker }
2609*9880d681SAndroid Build Coastguard Worker return false;
2610*9880d681SAndroid Build Coastguard Worker }
2611*9880d681SAndroid Build Coastguard Worker
2612*9880d681SAndroid Build Coastguard Worker Record *Rec;
2613*9880d681SAndroid Build Coastguard Worker if (Pat->isLeaf()) {
2614*9880d681SAndroid Build Coastguard Worker DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
2615*9880d681SAndroid Build Coastguard Worker if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!");
2616*9880d681SAndroid Build Coastguard Worker Rec = DI->getDef();
2617*9880d681SAndroid Build Coastguard Worker } else {
2618*9880d681SAndroid Build Coastguard Worker Rec = Pat->getOperator();
2619*9880d681SAndroid Build Coastguard Worker }
2620*9880d681SAndroid Build Coastguard Worker
2621*9880d681SAndroid Build Coastguard Worker // SRCVALUE nodes are ignored.
2622*9880d681SAndroid Build Coastguard Worker if (Rec->getName() == "srcvalue")
2623*9880d681SAndroid Build Coastguard Worker return false;
2624*9880d681SAndroid Build Coastguard Worker
2625*9880d681SAndroid Build Coastguard Worker TreePatternNode *&Slot = InstInputs[Pat->getName()];
2626*9880d681SAndroid Build Coastguard Worker if (!Slot) {
2627*9880d681SAndroid Build Coastguard Worker Slot = Pat;
2628*9880d681SAndroid Build Coastguard Worker return true;
2629*9880d681SAndroid Build Coastguard Worker }
2630*9880d681SAndroid Build Coastguard Worker Record *SlotRec;
2631*9880d681SAndroid Build Coastguard Worker if (Slot->isLeaf()) {
2632*9880d681SAndroid Build Coastguard Worker SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef();
2633*9880d681SAndroid Build Coastguard Worker } else {
2634*9880d681SAndroid Build Coastguard Worker assert(Slot->getNumChildren() == 0 && "can't be a use with children!");
2635*9880d681SAndroid Build Coastguard Worker SlotRec = Slot->getOperator();
2636*9880d681SAndroid Build Coastguard Worker }
2637*9880d681SAndroid Build Coastguard Worker
2638*9880d681SAndroid Build Coastguard Worker // Ensure that the inputs agree if we've already seen this input.
2639*9880d681SAndroid Build Coastguard Worker if (Rec != SlotRec)
2640*9880d681SAndroid Build Coastguard Worker I->error("All $" + Pat->getName() + " inputs must agree with each other");
2641*9880d681SAndroid Build Coastguard Worker if (Slot->getExtTypes() != Pat->getExtTypes())
2642*9880d681SAndroid Build Coastguard Worker I->error("All $" + Pat->getName() + " inputs must agree with each other");
2643*9880d681SAndroid Build Coastguard Worker return true;
2644*9880d681SAndroid Build Coastguard Worker }
2645*9880d681SAndroid Build Coastguard Worker
2646*9880d681SAndroid Build Coastguard Worker /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is
2647*9880d681SAndroid Build Coastguard Worker /// part of "I", the instruction), computing the set of inputs and outputs of
2648*9880d681SAndroid Build Coastguard Worker /// the pattern. Report errors if we see anything naughty.
2649*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::
FindPatternInputsAndOutputs(TreePattern * I,TreePatternNode * Pat,std::map<std::string,TreePatternNode * > & InstInputs,std::map<std::string,TreePatternNode * > & InstResults,std::vector<Record * > & InstImpResults)2650*9880d681SAndroid Build Coastguard Worker FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
2651*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> &InstInputs,
2652*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*>&InstResults,
2653*9880d681SAndroid Build Coastguard Worker std::vector<Record*> &InstImpResults) {
2654*9880d681SAndroid Build Coastguard Worker if (Pat->isLeaf()) {
2655*9880d681SAndroid Build Coastguard Worker bool isUse = HandleUse(I, Pat, InstInputs);
2656*9880d681SAndroid Build Coastguard Worker if (!isUse && Pat->getTransformFn())
2657*9880d681SAndroid Build Coastguard Worker I->error("Cannot specify a transform function for a non-input value!");
2658*9880d681SAndroid Build Coastguard Worker return;
2659*9880d681SAndroid Build Coastguard Worker }
2660*9880d681SAndroid Build Coastguard Worker
2661*9880d681SAndroid Build Coastguard Worker if (Pat->getOperator()->getName() == "implicit") {
2662*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
2663*9880d681SAndroid Build Coastguard Worker TreePatternNode *Dest = Pat->getChild(i);
2664*9880d681SAndroid Build Coastguard Worker if (!Dest->isLeaf())
2665*9880d681SAndroid Build Coastguard Worker I->error("implicitly defined value should be a register!");
2666*9880d681SAndroid Build Coastguard Worker
2667*9880d681SAndroid Build Coastguard Worker DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
2668*9880d681SAndroid Build Coastguard Worker if (!Val || !Val->getDef()->isSubClassOf("Register"))
2669*9880d681SAndroid Build Coastguard Worker I->error("implicitly defined value should be a register!");
2670*9880d681SAndroid Build Coastguard Worker InstImpResults.push_back(Val->getDef());
2671*9880d681SAndroid Build Coastguard Worker }
2672*9880d681SAndroid Build Coastguard Worker return;
2673*9880d681SAndroid Build Coastguard Worker }
2674*9880d681SAndroid Build Coastguard Worker
2675*9880d681SAndroid Build Coastguard Worker if (Pat->getOperator()->getName() != "set") {
2676*9880d681SAndroid Build Coastguard Worker // If this is not a set, verify that the children nodes are not void typed,
2677*9880d681SAndroid Build Coastguard Worker // and recurse.
2678*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
2679*9880d681SAndroid Build Coastguard Worker if (Pat->getChild(i)->getNumTypes() == 0)
2680*9880d681SAndroid Build Coastguard Worker I->error("Cannot have void nodes inside of patterns!");
2681*9880d681SAndroid Build Coastguard Worker FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults,
2682*9880d681SAndroid Build Coastguard Worker InstImpResults);
2683*9880d681SAndroid Build Coastguard Worker }
2684*9880d681SAndroid Build Coastguard Worker
2685*9880d681SAndroid Build Coastguard Worker // If this is a non-leaf node with no children, treat it basically as if
2686*9880d681SAndroid Build Coastguard Worker // it were a leaf. This handles nodes like (imm).
2687*9880d681SAndroid Build Coastguard Worker bool isUse = HandleUse(I, Pat, InstInputs);
2688*9880d681SAndroid Build Coastguard Worker
2689*9880d681SAndroid Build Coastguard Worker if (!isUse && Pat->getTransformFn())
2690*9880d681SAndroid Build Coastguard Worker I->error("Cannot specify a transform function for a non-input value!");
2691*9880d681SAndroid Build Coastguard Worker return;
2692*9880d681SAndroid Build Coastguard Worker }
2693*9880d681SAndroid Build Coastguard Worker
2694*9880d681SAndroid Build Coastguard Worker // Otherwise, this is a set, validate and collect instruction results.
2695*9880d681SAndroid Build Coastguard Worker if (Pat->getNumChildren() == 0)
2696*9880d681SAndroid Build Coastguard Worker I->error("set requires operands!");
2697*9880d681SAndroid Build Coastguard Worker
2698*9880d681SAndroid Build Coastguard Worker if (Pat->getTransformFn())
2699*9880d681SAndroid Build Coastguard Worker I->error("Cannot specify a transform function on a set node!");
2700*9880d681SAndroid Build Coastguard Worker
2701*9880d681SAndroid Build Coastguard Worker // Check the set destinations.
2702*9880d681SAndroid Build Coastguard Worker unsigned NumDests = Pat->getNumChildren()-1;
2703*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumDests; ++i) {
2704*9880d681SAndroid Build Coastguard Worker TreePatternNode *Dest = Pat->getChild(i);
2705*9880d681SAndroid Build Coastguard Worker if (!Dest->isLeaf())
2706*9880d681SAndroid Build Coastguard Worker I->error("set destination should be a register!");
2707*9880d681SAndroid Build Coastguard Worker
2708*9880d681SAndroid Build Coastguard Worker DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
2709*9880d681SAndroid Build Coastguard Worker if (!Val) {
2710*9880d681SAndroid Build Coastguard Worker I->error("set destination should be a register!");
2711*9880d681SAndroid Build Coastguard Worker continue;
2712*9880d681SAndroid Build Coastguard Worker }
2713*9880d681SAndroid Build Coastguard Worker
2714*9880d681SAndroid Build Coastguard Worker if (Val->getDef()->isSubClassOf("RegisterClass") ||
2715*9880d681SAndroid Build Coastguard Worker Val->getDef()->isSubClassOf("ValueType") ||
2716*9880d681SAndroid Build Coastguard Worker Val->getDef()->isSubClassOf("RegisterOperand") ||
2717*9880d681SAndroid Build Coastguard Worker Val->getDef()->isSubClassOf("PointerLikeRegClass")) {
2718*9880d681SAndroid Build Coastguard Worker if (Dest->getName().empty())
2719*9880d681SAndroid Build Coastguard Worker I->error("set destination must have a name!");
2720*9880d681SAndroid Build Coastguard Worker if (InstResults.count(Dest->getName()))
2721*9880d681SAndroid Build Coastguard Worker I->error("cannot set '" + Dest->getName() +"' multiple times");
2722*9880d681SAndroid Build Coastguard Worker InstResults[Dest->getName()] = Dest;
2723*9880d681SAndroid Build Coastguard Worker } else if (Val->getDef()->isSubClassOf("Register")) {
2724*9880d681SAndroid Build Coastguard Worker InstImpResults.push_back(Val->getDef());
2725*9880d681SAndroid Build Coastguard Worker } else {
2726*9880d681SAndroid Build Coastguard Worker I->error("set destination should be a register!");
2727*9880d681SAndroid Build Coastguard Worker }
2728*9880d681SAndroid Build Coastguard Worker }
2729*9880d681SAndroid Build Coastguard Worker
2730*9880d681SAndroid Build Coastguard Worker // Verify and collect info from the computation.
2731*9880d681SAndroid Build Coastguard Worker FindPatternInputsAndOutputs(I, Pat->getChild(NumDests),
2732*9880d681SAndroid Build Coastguard Worker InstInputs, InstResults, InstImpResults);
2733*9880d681SAndroid Build Coastguard Worker }
2734*9880d681SAndroid Build Coastguard Worker
2735*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
2736*9880d681SAndroid Build Coastguard Worker // Instruction Analysis
2737*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
2738*9880d681SAndroid Build Coastguard Worker
2739*9880d681SAndroid Build Coastguard Worker class InstAnalyzer {
2740*9880d681SAndroid Build Coastguard Worker const CodeGenDAGPatterns &CDP;
2741*9880d681SAndroid Build Coastguard Worker public:
2742*9880d681SAndroid Build Coastguard Worker bool hasSideEffects;
2743*9880d681SAndroid Build Coastguard Worker bool mayStore;
2744*9880d681SAndroid Build Coastguard Worker bool mayLoad;
2745*9880d681SAndroid Build Coastguard Worker bool isBitcast;
2746*9880d681SAndroid Build Coastguard Worker bool isVariadic;
2747*9880d681SAndroid Build Coastguard Worker
InstAnalyzer(const CodeGenDAGPatterns & cdp)2748*9880d681SAndroid Build Coastguard Worker InstAnalyzer(const CodeGenDAGPatterns &cdp)
2749*9880d681SAndroid Build Coastguard Worker : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false),
2750*9880d681SAndroid Build Coastguard Worker isBitcast(false), isVariadic(false) {}
2751*9880d681SAndroid Build Coastguard Worker
Analyze(const TreePattern * Pat)2752*9880d681SAndroid Build Coastguard Worker void Analyze(const TreePattern *Pat) {
2753*9880d681SAndroid Build Coastguard Worker // Assume only the first tree is the pattern. The others are clobber nodes.
2754*9880d681SAndroid Build Coastguard Worker AnalyzeNode(Pat->getTree(0));
2755*9880d681SAndroid Build Coastguard Worker }
2756*9880d681SAndroid Build Coastguard Worker
Analyze(const PatternToMatch * Pat)2757*9880d681SAndroid Build Coastguard Worker void Analyze(const PatternToMatch *Pat) {
2758*9880d681SAndroid Build Coastguard Worker AnalyzeNode(Pat->getSrcPattern());
2759*9880d681SAndroid Build Coastguard Worker }
2760*9880d681SAndroid Build Coastguard Worker
2761*9880d681SAndroid Build Coastguard Worker private:
IsNodeBitcast(const TreePatternNode * N) const2762*9880d681SAndroid Build Coastguard Worker bool IsNodeBitcast(const TreePatternNode *N) const {
2763*9880d681SAndroid Build Coastguard Worker if (hasSideEffects || mayLoad || mayStore || isVariadic)
2764*9880d681SAndroid Build Coastguard Worker return false;
2765*9880d681SAndroid Build Coastguard Worker
2766*9880d681SAndroid Build Coastguard Worker if (N->getNumChildren() != 2)
2767*9880d681SAndroid Build Coastguard Worker return false;
2768*9880d681SAndroid Build Coastguard Worker
2769*9880d681SAndroid Build Coastguard Worker const TreePatternNode *N0 = N->getChild(0);
2770*9880d681SAndroid Build Coastguard Worker if (!N0->isLeaf() || !isa<DefInit>(N0->getLeafValue()))
2771*9880d681SAndroid Build Coastguard Worker return false;
2772*9880d681SAndroid Build Coastguard Worker
2773*9880d681SAndroid Build Coastguard Worker const TreePatternNode *N1 = N->getChild(1);
2774*9880d681SAndroid Build Coastguard Worker if (N1->isLeaf())
2775*9880d681SAndroid Build Coastguard Worker return false;
2776*9880d681SAndroid Build Coastguard Worker if (N1->getNumChildren() != 1 || !N1->getChild(0)->isLeaf())
2777*9880d681SAndroid Build Coastguard Worker return false;
2778*9880d681SAndroid Build Coastguard Worker
2779*9880d681SAndroid Build Coastguard Worker const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N1->getOperator());
2780*9880d681SAndroid Build Coastguard Worker if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1)
2781*9880d681SAndroid Build Coastguard Worker return false;
2782*9880d681SAndroid Build Coastguard Worker return OpInfo.getEnumName() == "ISD::BITCAST";
2783*9880d681SAndroid Build Coastguard Worker }
2784*9880d681SAndroid Build Coastguard Worker
2785*9880d681SAndroid Build Coastguard Worker public:
AnalyzeNode(const TreePatternNode * N)2786*9880d681SAndroid Build Coastguard Worker void AnalyzeNode(const TreePatternNode *N) {
2787*9880d681SAndroid Build Coastguard Worker if (N->isLeaf()) {
2788*9880d681SAndroid Build Coastguard Worker if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
2789*9880d681SAndroid Build Coastguard Worker Record *LeafRec = DI->getDef();
2790*9880d681SAndroid Build Coastguard Worker // Handle ComplexPattern leaves.
2791*9880d681SAndroid Build Coastguard Worker if (LeafRec->isSubClassOf("ComplexPattern")) {
2792*9880d681SAndroid Build Coastguard Worker const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
2793*9880d681SAndroid Build Coastguard Worker if (CP.hasProperty(SDNPMayStore)) mayStore = true;
2794*9880d681SAndroid Build Coastguard Worker if (CP.hasProperty(SDNPMayLoad)) mayLoad = true;
2795*9880d681SAndroid Build Coastguard Worker if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true;
2796*9880d681SAndroid Build Coastguard Worker }
2797*9880d681SAndroid Build Coastguard Worker }
2798*9880d681SAndroid Build Coastguard Worker return;
2799*9880d681SAndroid Build Coastguard Worker }
2800*9880d681SAndroid Build Coastguard Worker
2801*9880d681SAndroid Build Coastguard Worker // Analyze children.
2802*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2803*9880d681SAndroid Build Coastguard Worker AnalyzeNode(N->getChild(i));
2804*9880d681SAndroid Build Coastguard Worker
2805*9880d681SAndroid Build Coastguard Worker // Ignore set nodes, which are not SDNodes.
2806*9880d681SAndroid Build Coastguard Worker if (N->getOperator()->getName() == "set") {
2807*9880d681SAndroid Build Coastguard Worker isBitcast = IsNodeBitcast(N);
2808*9880d681SAndroid Build Coastguard Worker return;
2809*9880d681SAndroid Build Coastguard Worker }
2810*9880d681SAndroid Build Coastguard Worker
2811*9880d681SAndroid Build Coastguard Worker // Notice properties of the node.
2812*9880d681SAndroid Build Coastguard Worker if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true;
2813*9880d681SAndroid Build Coastguard Worker if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true;
2814*9880d681SAndroid Build Coastguard Worker if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true;
2815*9880d681SAndroid Build Coastguard Worker if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true;
2816*9880d681SAndroid Build Coastguard Worker
2817*9880d681SAndroid Build Coastguard Worker if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
2818*9880d681SAndroid Build Coastguard Worker // If this is an intrinsic, analyze it.
2819*9880d681SAndroid Build Coastguard Worker if (IntInfo->ModRef & CodeGenIntrinsic::MR_Ref)
2820*9880d681SAndroid Build Coastguard Worker mayLoad = true;// These may load memory.
2821*9880d681SAndroid Build Coastguard Worker
2822*9880d681SAndroid Build Coastguard Worker if (IntInfo->ModRef & CodeGenIntrinsic::MR_Mod)
2823*9880d681SAndroid Build Coastguard Worker mayStore = true;// Intrinsics that can write to memory are 'mayStore'.
2824*9880d681SAndroid Build Coastguard Worker
2825*9880d681SAndroid Build Coastguard Worker if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem)
2826*9880d681SAndroid Build Coastguard Worker // ReadWriteMem intrinsics can have other strange effects.
2827*9880d681SAndroid Build Coastguard Worker hasSideEffects = true;
2828*9880d681SAndroid Build Coastguard Worker }
2829*9880d681SAndroid Build Coastguard Worker }
2830*9880d681SAndroid Build Coastguard Worker
2831*9880d681SAndroid Build Coastguard Worker };
2832*9880d681SAndroid Build Coastguard Worker
InferFromPattern(CodeGenInstruction & InstInfo,const InstAnalyzer & PatInfo,Record * PatDef)2833*9880d681SAndroid Build Coastguard Worker static bool InferFromPattern(CodeGenInstruction &InstInfo,
2834*9880d681SAndroid Build Coastguard Worker const InstAnalyzer &PatInfo,
2835*9880d681SAndroid Build Coastguard Worker Record *PatDef) {
2836*9880d681SAndroid Build Coastguard Worker bool Error = false;
2837*9880d681SAndroid Build Coastguard Worker
2838*9880d681SAndroid Build Coastguard Worker // Remember where InstInfo got its flags.
2839*9880d681SAndroid Build Coastguard Worker if (InstInfo.hasUndefFlags())
2840*9880d681SAndroid Build Coastguard Worker InstInfo.InferredFrom = PatDef;
2841*9880d681SAndroid Build Coastguard Worker
2842*9880d681SAndroid Build Coastguard Worker // Check explicitly set flags for consistency.
2843*9880d681SAndroid Build Coastguard Worker if (InstInfo.hasSideEffects != PatInfo.hasSideEffects &&
2844*9880d681SAndroid Build Coastguard Worker !InstInfo.hasSideEffects_Unset) {
2845*9880d681SAndroid Build Coastguard Worker // Allow explicitly setting hasSideEffects = 1 on instructions, even when
2846*9880d681SAndroid Build Coastguard Worker // the pattern has no side effects. That could be useful for div/rem
2847*9880d681SAndroid Build Coastguard Worker // instructions that may trap.
2848*9880d681SAndroid Build Coastguard Worker if (!InstInfo.hasSideEffects) {
2849*9880d681SAndroid Build Coastguard Worker Error = true;
2850*9880d681SAndroid Build Coastguard Worker PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " +
2851*9880d681SAndroid Build Coastguard Worker Twine(InstInfo.hasSideEffects));
2852*9880d681SAndroid Build Coastguard Worker }
2853*9880d681SAndroid Build Coastguard Worker }
2854*9880d681SAndroid Build Coastguard Worker
2855*9880d681SAndroid Build Coastguard Worker if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) {
2856*9880d681SAndroid Build Coastguard Worker Error = true;
2857*9880d681SAndroid Build Coastguard Worker PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " +
2858*9880d681SAndroid Build Coastguard Worker Twine(InstInfo.mayStore));
2859*9880d681SAndroid Build Coastguard Worker }
2860*9880d681SAndroid Build Coastguard Worker
2861*9880d681SAndroid Build Coastguard Worker if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) {
2862*9880d681SAndroid Build Coastguard Worker // Allow explicitly setting mayLoad = 1, even when the pattern has no loads.
2863*9880d681SAndroid Build Coastguard Worker // Some targets translate immediates to loads.
2864*9880d681SAndroid Build Coastguard Worker if (!InstInfo.mayLoad) {
2865*9880d681SAndroid Build Coastguard Worker Error = true;
2866*9880d681SAndroid Build Coastguard Worker PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " +
2867*9880d681SAndroid Build Coastguard Worker Twine(InstInfo.mayLoad));
2868*9880d681SAndroid Build Coastguard Worker }
2869*9880d681SAndroid Build Coastguard Worker }
2870*9880d681SAndroid Build Coastguard Worker
2871*9880d681SAndroid Build Coastguard Worker // Transfer inferred flags.
2872*9880d681SAndroid Build Coastguard Worker InstInfo.hasSideEffects |= PatInfo.hasSideEffects;
2873*9880d681SAndroid Build Coastguard Worker InstInfo.mayStore |= PatInfo.mayStore;
2874*9880d681SAndroid Build Coastguard Worker InstInfo.mayLoad |= PatInfo.mayLoad;
2875*9880d681SAndroid Build Coastguard Worker
2876*9880d681SAndroid Build Coastguard Worker // These flags are silently added without any verification.
2877*9880d681SAndroid Build Coastguard Worker InstInfo.isBitcast |= PatInfo.isBitcast;
2878*9880d681SAndroid Build Coastguard Worker
2879*9880d681SAndroid Build Coastguard Worker // Don't infer isVariadic. This flag means something different on SDNodes and
2880*9880d681SAndroid Build Coastguard Worker // instructions. For example, a CALL SDNode is variadic because it has the
2881*9880d681SAndroid Build Coastguard Worker // call arguments as operands, but a CALL instruction is not variadic - it
2882*9880d681SAndroid Build Coastguard Worker // has argument registers as implicit, not explicit uses.
2883*9880d681SAndroid Build Coastguard Worker
2884*9880d681SAndroid Build Coastguard Worker return Error;
2885*9880d681SAndroid Build Coastguard Worker }
2886*9880d681SAndroid Build Coastguard Worker
2887*9880d681SAndroid Build Coastguard Worker /// hasNullFragReference - Return true if the DAG has any reference to the
2888*9880d681SAndroid Build Coastguard Worker /// null_frag operator.
hasNullFragReference(DagInit * DI)2889*9880d681SAndroid Build Coastguard Worker static bool hasNullFragReference(DagInit *DI) {
2890*9880d681SAndroid Build Coastguard Worker DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator());
2891*9880d681SAndroid Build Coastguard Worker if (!OpDef) return false;
2892*9880d681SAndroid Build Coastguard Worker Record *Operator = OpDef->getDef();
2893*9880d681SAndroid Build Coastguard Worker
2894*9880d681SAndroid Build Coastguard Worker // If this is the null fragment, return true.
2895*9880d681SAndroid Build Coastguard Worker if (Operator->getName() == "null_frag") return true;
2896*9880d681SAndroid Build Coastguard Worker // If any of the arguments reference the null fragment, return true.
2897*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
2898*9880d681SAndroid Build Coastguard Worker DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i));
2899*9880d681SAndroid Build Coastguard Worker if (Arg && hasNullFragReference(Arg))
2900*9880d681SAndroid Build Coastguard Worker return true;
2901*9880d681SAndroid Build Coastguard Worker }
2902*9880d681SAndroid Build Coastguard Worker
2903*9880d681SAndroid Build Coastguard Worker return false;
2904*9880d681SAndroid Build Coastguard Worker }
2905*9880d681SAndroid Build Coastguard Worker
2906*9880d681SAndroid Build Coastguard Worker /// hasNullFragReference - Return true if any DAG in the list references
2907*9880d681SAndroid Build Coastguard Worker /// the null_frag operator.
hasNullFragReference(ListInit * LI)2908*9880d681SAndroid Build Coastguard Worker static bool hasNullFragReference(ListInit *LI) {
2909*9880d681SAndroid Build Coastguard Worker for (Init *I : LI->getValues()) {
2910*9880d681SAndroid Build Coastguard Worker DagInit *DI = dyn_cast<DagInit>(I);
2911*9880d681SAndroid Build Coastguard Worker assert(DI && "non-dag in an instruction Pattern list?!");
2912*9880d681SAndroid Build Coastguard Worker if (hasNullFragReference(DI))
2913*9880d681SAndroid Build Coastguard Worker return true;
2914*9880d681SAndroid Build Coastguard Worker }
2915*9880d681SAndroid Build Coastguard Worker return false;
2916*9880d681SAndroid Build Coastguard Worker }
2917*9880d681SAndroid Build Coastguard Worker
2918*9880d681SAndroid Build Coastguard Worker /// Get all the instructions in a tree.
2919*9880d681SAndroid Build Coastguard Worker static void
getInstructionsInTree(TreePatternNode * Tree,SmallVectorImpl<Record * > & Instrs)2920*9880d681SAndroid Build Coastguard Worker getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) {
2921*9880d681SAndroid Build Coastguard Worker if (Tree->isLeaf())
2922*9880d681SAndroid Build Coastguard Worker return;
2923*9880d681SAndroid Build Coastguard Worker if (Tree->getOperator()->isSubClassOf("Instruction"))
2924*9880d681SAndroid Build Coastguard Worker Instrs.push_back(Tree->getOperator());
2925*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i)
2926*9880d681SAndroid Build Coastguard Worker getInstructionsInTree(Tree->getChild(i), Instrs);
2927*9880d681SAndroid Build Coastguard Worker }
2928*9880d681SAndroid Build Coastguard Worker
2929*9880d681SAndroid Build Coastguard Worker /// Check the class of a pattern leaf node against the instruction operand it
2930*9880d681SAndroid Build Coastguard Worker /// represents.
checkOperandClass(CGIOperandList::OperandInfo & OI,Record * Leaf)2931*9880d681SAndroid Build Coastguard Worker static bool checkOperandClass(CGIOperandList::OperandInfo &OI,
2932*9880d681SAndroid Build Coastguard Worker Record *Leaf) {
2933*9880d681SAndroid Build Coastguard Worker if (OI.Rec == Leaf)
2934*9880d681SAndroid Build Coastguard Worker return true;
2935*9880d681SAndroid Build Coastguard Worker
2936*9880d681SAndroid Build Coastguard Worker // Allow direct value types to be used in instruction set patterns.
2937*9880d681SAndroid Build Coastguard Worker // The type will be checked later.
2938*9880d681SAndroid Build Coastguard Worker if (Leaf->isSubClassOf("ValueType"))
2939*9880d681SAndroid Build Coastguard Worker return true;
2940*9880d681SAndroid Build Coastguard Worker
2941*9880d681SAndroid Build Coastguard Worker // Patterns can also be ComplexPattern instances.
2942*9880d681SAndroid Build Coastguard Worker if (Leaf->isSubClassOf("ComplexPattern"))
2943*9880d681SAndroid Build Coastguard Worker return true;
2944*9880d681SAndroid Build Coastguard Worker
2945*9880d681SAndroid Build Coastguard Worker return false;
2946*9880d681SAndroid Build Coastguard Worker }
2947*9880d681SAndroid Build Coastguard Worker
parseInstructionPattern(CodeGenInstruction & CGI,ListInit * Pat,DAGInstMap & DAGInsts)2948*9880d681SAndroid Build Coastguard Worker const DAGInstruction &CodeGenDAGPatterns::parseInstructionPattern(
2949*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) {
2950*9880d681SAndroid Build Coastguard Worker
2951*9880d681SAndroid Build Coastguard Worker assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!");
2952*9880d681SAndroid Build Coastguard Worker
2953*9880d681SAndroid Build Coastguard Worker // Parse the instruction.
2954*9880d681SAndroid Build Coastguard Worker TreePattern *I = new TreePattern(CGI.TheDef, Pat, true, *this);
2955*9880d681SAndroid Build Coastguard Worker // Inline pattern fragments into it.
2956*9880d681SAndroid Build Coastguard Worker I->InlinePatternFragments();
2957*9880d681SAndroid Build Coastguard Worker
2958*9880d681SAndroid Build Coastguard Worker // Infer as many types as possible. If we cannot infer all of them, we can
2959*9880d681SAndroid Build Coastguard Worker // never do anything with this instruction pattern: report it to the user.
2960*9880d681SAndroid Build Coastguard Worker if (!I->InferAllTypes())
2961*9880d681SAndroid Build Coastguard Worker I->error("Could not infer all types in pattern!");
2962*9880d681SAndroid Build Coastguard Worker
2963*9880d681SAndroid Build Coastguard Worker // InstInputs - Keep track of all of the inputs of the instruction, along
2964*9880d681SAndroid Build Coastguard Worker // with the record they are declared as.
2965*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> InstInputs;
2966*9880d681SAndroid Build Coastguard Worker
2967*9880d681SAndroid Build Coastguard Worker // InstResults - Keep track of all the virtual registers that are 'set'
2968*9880d681SAndroid Build Coastguard Worker // in the instruction, including what reg class they are.
2969*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> InstResults;
2970*9880d681SAndroid Build Coastguard Worker
2971*9880d681SAndroid Build Coastguard Worker std::vector<Record*> InstImpResults;
2972*9880d681SAndroid Build Coastguard Worker
2973*9880d681SAndroid Build Coastguard Worker // Verify that the top-level forms in the instruction are of void type, and
2974*9880d681SAndroid Build Coastguard Worker // fill in the InstResults map.
2975*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) {
2976*9880d681SAndroid Build Coastguard Worker TreePatternNode *Pat = I->getTree(j);
2977*9880d681SAndroid Build Coastguard Worker if (Pat->getNumTypes() != 0) {
2978*9880d681SAndroid Build Coastguard Worker std::string Types;
2979*9880d681SAndroid Build Coastguard Worker for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) {
2980*9880d681SAndroid Build Coastguard Worker if (k > 0)
2981*9880d681SAndroid Build Coastguard Worker Types += ", ";
2982*9880d681SAndroid Build Coastguard Worker Types += Pat->getExtType(k).getName();
2983*9880d681SAndroid Build Coastguard Worker }
2984*9880d681SAndroid Build Coastguard Worker I->error("Top-level forms in instruction pattern should have"
2985*9880d681SAndroid Build Coastguard Worker " void types, has types " + Types);
2986*9880d681SAndroid Build Coastguard Worker }
2987*9880d681SAndroid Build Coastguard Worker
2988*9880d681SAndroid Build Coastguard Worker // Find inputs and outputs, and verify the structure of the uses/defs.
2989*9880d681SAndroid Build Coastguard Worker FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults,
2990*9880d681SAndroid Build Coastguard Worker InstImpResults);
2991*9880d681SAndroid Build Coastguard Worker }
2992*9880d681SAndroid Build Coastguard Worker
2993*9880d681SAndroid Build Coastguard Worker // Now that we have inputs and outputs of the pattern, inspect the operands
2994*9880d681SAndroid Build Coastguard Worker // list for the instruction. This determines the order that operands are
2995*9880d681SAndroid Build Coastguard Worker // added to the machine instruction the node corresponds to.
2996*9880d681SAndroid Build Coastguard Worker unsigned NumResults = InstResults.size();
2997*9880d681SAndroid Build Coastguard Worker
2998*9880d681SAndroid Build Coastguard Worker // Parse the operands list from the (ops) list, validating it.
2999*9880d681SAndroid Build Coastguard Worker assert(I->getArgList().empty() && "Args list should still be empty here!");
3000*9880d681SAndroid Build Coastguard Worker
3001*9880d681SAndroid Build Coastguard Worker // Check that all of the results occur first in the list.
3002*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Results;
3003*9880d681SAndroid Build Coastguard Worker SmallVector<TreePatternNode *, 2> ResNodes;
3004*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumResults; ++i) {
3005*9880d681SAndroid Build Coastguard Worker if (i == CGI.Operands.size())
3006*9880d681SAndroid Build Coastguard Worker I->error("'" + InstResults.begin()->first +
3007*9880d681SAndroid Build Coastguard Worker "' set but does not appear in operand list!");
3008*9880d681SAndroid Build Coastguard Worker const std::string &OpName = CGI.Operands[i].Name;
3009*9880d681SAndroid Build Coastguard Worker
3010*9880d681SAndroid Build Coastguard Worker // Check that it exists in InstResults.
3011*9880d681SAndroid Build Coastguard Worker TreePatternNode *RNode = InstResults[OpName];
3012*9880d681SAndroid Build Coastguard Worker if (!RNode)
3013*9880d681SAndroid Build Coastguard Worker I->error("Operand $" + OpName + " does not exist in operand list!");
3014*9880d681SAndroid Build Coastguard Worker
3015*9880d681SAndroid Build Coastguard Worker ResNodes.push_back(RNode);
3016*9880d681SAndroid Build Coastguard Worker
3017*9880d681SAndroid Build Coastguard Worker Record *R = cast<DefInit>(RNode->getLeafValue())->getDef();
3018*9880d681SAndroid Build Coastguard Worker if (!R)
3019*9880d681SAndroid Build Coastguard Worker I->error("Operand $" + OpName + " should be a set destination: all "
3020*9880d681SAndroid Build Coastguard Worker "outputs must occur before inputs in operand list!");
3021*9880d681SAndroid Build Coastguard Worker
3022*9880d681SAndroid Build Coastguard Worker if (!checkOperandClass(CGI.Operands[i], R))
3023*9880d681SAndroid Build Coastguard Worker I->error("Operand $" + OpName + " class mismatch!");
3024*9880d681SAndroid Build Coastguard Worker
3025*9880d681SAndroid Build Coastguard Worker // Remember the return type.
3026*9880d681SAndroid Build Coastguard Worker Results.push_back(CGI.Operands[i].Rec);
3027*9880d681SAndroid Build Coastguard Worker
3028*9880d681SAndroid Build Coastguard Worker // Okay, this one checks out.
3029*9880d681SAndroid Build Coastguard Worker InstResults.erase(OpName);
3030*9880d681SAndroid Build Coastguard Worker }
3031*9880d681SAndroid Build Coastguard Worker
3032*9880d681SAndroid Build Coastguard Worker // Loop over the inputs next. Make a copy of InstInputs so we can destroy
3033*9880d681SAndroid Build Coastguard Worker // the copy while we're checking the inputs.
3034*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs);
3035*9880d681SAndroid Build Coastguard Worker
3036*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> ResultNodeOperands;
3037*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Operands;
3038*9880d681SAndroid Build Coastguard Worker for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
3039*9880d681SAndroid Build Coastguard Worker CGIOperandList::OperandInfo &Op = CGI.Operands[i];
3040*9880d681SAndroid Build Coastguard Worker const std::string &OpName = Op.Name;
3041*9880d681SAndroid Build Coastguard Worker if (OpName.empty())
3042*9880d681SAndroid Build Coastguard Worker I->error("Operand #" + utostr(i) + " in operands list has no name!");
3043*9880d681SAndroid Build Coastguard Worker
3044*9880d681SAndroid Build Coastguard Worker if (!InstInputsCheck.count(OpName)) {
3045*9880d681SAndroid Build Coastguard Worker // If this is an operand with a DefaultOps set filled in, we can ignore
3046*9880d681SAndroid Build Coastguard Worker // this. When we codegen it, we will do so as always executed.
3047*9880d681SAndroid Build Coastguard Worker if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) {
3048*9880d681SAndroid Build Coastguard Worker // Does it have a non-empty DefaultOps field? If so, ignore this
3049*9880d681SAndroid Build Coastguard Worker // operand.
3050*9880d681SAndroid Build Coastguard Worker if (!getDefaultOperand(Op.Rec).DefaultOps.empty())
3051*9880d681SAndroid Build Coastguard Worker continue;
3052*9880d681SAndroid Build Coastguard Worker }
3053*9880d681SAndroid Build Coastguard Worker I->error("Operand $" + OpName +
3054*9880d681SAndroid Build Coastguard Worker " does not appear in the instruction pattern");
3055*9880d681SAndroid Build Coastguard Worker }
3056*9880d681SAndroid Build Coastguard Worker TreePatternNode *InVal = InstInputsCheck[OpName];
3057*9880d681SAndroid Build Coastguard Worker InstInputsCheck.erase(OpName); // It occurred, remove from map.
3058*9880d681SAndroid Build Coastguard Worker
3059*9880d681SAndroid Build Coastguard Worker if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) {
3060*9880d681SAndroid Build Coastguard Worker Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef();
3061*9880d681SAndroid Build Coastguard Worker if (!checkOperandClass(Op, InRec))
3062*9880d681SAndroid Build Coastguard Worker I->error("Operand $" + OpName + "'s register class disagrees"
3063*9880d681SAndroid Build Coastguard Worker " between the operand and pattern");
3064*9880d681SAndroid Build Coastguard Worker }
3065*9880d681SAndroid Build Coastguard Worker Operands.push_back(Op.Rec);
3066*9880d681SAndroid Build Coastguard Worker
3067*9880d681SAndroid Build Coastguard Worker // Construct the result for the dest-pattern operand list.
3068*9880d681SAndroid Build Coastguard Worker TreePatternNode *OpNode = InVal->clone();
3069*9880d681SAndroid Build Coastguard Worker
3070*9880d681SAndroid Build Coastguard Worker // No predicate is useful on the result.
3071*9880d681SAndroid Build Coastguard Worker OpNode->clearPredicateFns();
3072*9880d681SAndroid Build Coastguard Worker
3073*9880d681SAndroid Build Coastguard Worker // Promote the xform function to be an explicit node if set.
3074*9880d681SAndroid Build Coastguard Worker if (Record *Xform = OpNode->getTransformFn()) {
3075*9880d681SAndroid Build Coastguard Worker OpNode->setTransformFn(nullptr);
3076*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> Children;
3077*9880d681SAndroid Build Coastguard Worker Children.push_back(OpNode);
3078*9880d681SAndroid Build Coastguard Worker OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes());
3079*9880d681SAndroid Build Coastguard Worker }
3080*9880d681SAndroid Build Coastguard Worker
3081*9880d681SAndroid Build Coastguard Worker ResultNodeOperands.push_back(OpNode);
3082*9880d681SAndroid Build Coastguard Worker }
3083*9880d681SAndroid Build Coastguard Worker
3084*9880d681SAndroid Build Coastguard Worker if (!InstInputsCheck.empty())
3085*9880d681SAndroid Build Coastguard Worker I->error("Input operand $" + InstInputsCheck.begin()->first +
3086*9880d681SAndroid Build Coastguard Worker " occurs in pattern but not in operands list!");
3087*9880d681SAndroid Build Coastguard Worker
3088*9880d681SAndroid Build Coastguard Worker TreePatternNode *ResultPattern =
3089*9880d681SAndroid Build Coastguard Worker new TreePatternNode(I->getRecord(), ResultNodeOperands,
3090*9880d681SAndroid Build Coastguard Worker GetNumNodeResults(I->getRecord(), *this));
3091*9880d681SAndroid Build Coastguard Worker // Copy fully inferred output node types to instruction result pattern.
3092*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumResults; ++i) {
3093*9880d681SAndroid Build Coastguard Worker assert(ResNodes[i]->getNumTypes() == 1 && "FIXME: Unhandled");
3094*9880d681SAndroid Build Coastguard Worker ResultPattern->setType(i, ResNodes[i]->getExtType(0));
3095*9880d681SAndroid Build Coastguard Worker }
3096*9880d681SAndroid Build Coastguard Worker
3097*9880d681SAndroid Build Coastguard Worker // Create and insert the instruction.
3098*9880d681SAndroid Build Coastguard Worker // FIXME: InstImpResults should not be part of DAGInstruction.
3099*9880d681SAndroid Build Coastguard Worker DAGInstruction TheInst(I, Results, Operands, InstImpResults);
3100*9880d681SAndroid Build Coastguard Worker DAGInsts.insert(std::make_pair(I->getRecord(), TheInst));
3101*9880d681SAndroid Build Coastguard Worker
3102*9880d681SAndroid Build Coastguard Worker // Use a temporary tree pattern to infer all types and make sure that the
3103*9880d681SAndroid Build Coastguard Worker // constructed result is correct. This depends on the instruction already
3104*9880d681SAndroid Build Coastguard Worker // being inserted into the DAGInsts map.
3105*9880d681SAndroid Build Coastguard Worker TreePattern Temp(I->getRecord(), ResultPattern, false, *this);
3106*9880d681SAndroid Build Coastguard Worker Temp.InferAllTypes(&I->getNamedNodesMap());
3107*9880d681SAndroid Build Coastguard Worker
3108*9880d681SAndroid Build Coastguard Worker DAGInstruction &TheInsertedInst = DAGInsts.find(I->getRecord())->second;
3109*9880d681SAndroid Build Coastguard Worker TheInsertedInst.setResultPattern(Temp.getOnlyTree());
3110*9880d681SAndroid Build Coastguard Worker
3111*9880d681SAndroid Build Coastguard Worker return TheInsertedInst;
3112*9880d681SAndroid Build Coastguard Worker }
3113*9880d681SAndroid Build Coastguard Worker
3114*9880d681SAndroid Build Coastguard Worker /// ParseInstructions - Parse all of the instructions, inlining and resolving
3115*9880d681SAndroid Build Coastguard Worker /// any fragments involved. This populates the Instructions list with fully
3116*9880d681SAndroid Build Coastguard Worker /// resolved instructions.
ParseInstructions()3117*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParseInstructions() {
3118*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction");
3119*9880d681SAndroid Build Coastguard Worker
3120*9880d681SAndroid Build Coastguard Worker for (Record *Instr : Instrs) {
3121*9880d681SAndroid Build Coastguard Worker ListInit *LI = nullptr;
3122*9880d681SAndroid Build Coastguard Worker
3123*9880d681SAndroid Build Coastguard Worker if (isa<ListInit>(Instr->getValueInit("Pattern")))
3124*9880d681SAndroid Build Coastguard Worker LI = Instr->getValueAsListInit("Pattern");
3125*9880d681SAndroid Build Coastguard Worker
3126*9880d681SAndroid Build Coastguard Worker // If there is no pattern, only collect minimal information about the
3127*9880d681SAndroid Build Coastguard Worker // instruction for its operand list. We have to assume that there is one
3128*9880d681SAndroid Build Coastguard Worker // result, as we have no detailed info. A pattern which references the
3129*9880d681SAndroid Build Coastguard Worker // null_frag operator is as-if no pattern were specified. Normally this
3130*9880d681SAndroid Build Coastguard Worker // is from a multiclass expansion w/ a SDPatternOperator passed in as
3131*9880d681SAndroid Build Coastguard Worker // null_frag.
3132*9880d681SAndroid Build Coastguard Worker if (!LI || LI->empty() || hasNullFragReference(LI)) {
3133*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Results;
3134*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Operands;
3135*9880d681SAndroid Build Coastguard Worker
3136*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
3137*9880d681SAndroid Build Coastguard Worker
3138*9880d681SAndroid Build Coastguard Worker if (InstInfo.Operands.size() != 0) {
3139*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, e = InstInfo.Operands.NumDefs; j < e; ++j)
3140*9880d681SAndroid Build Coastguard Worker Results.push_back(InstInfo.Operands[j].Rec);
3141*9880d681SAndroid Build Coastguard Worker
3142*9880d681SAndroid Build Coastguard Worker // The rest are inputs.
3143*9880d681SAndroid Build Coastguard Worker for (unsigned j = InstInfo.Operands.NumDefs,
3144*9880d681SAndroid Build Coastguard Worker e = InstInfo.Operands.size(); j < e; ++j)
3145*9880d681SAndroid Build Coastguard Worker Operands.push_back(InstInfo.Operands[j].Rec);
3146*9880d681SAndroid Build Coastguard Worker }
3147*9880d681SAndroid Build Coastguard Worker
3148*9880d681SAndroid Build Coastguard Worker // Create and insert the instruction.
3149*9880d681SAndroid Build Coastguard Worker std::vector<Record*> ImpResults;
3150*9880d681SAndroid Build Coastguard Worker Instructions.insert(std::make_pair(Instr,
3151*9880d681SAndroid Build Coastguard Worker DAGInstruction(nullptr, Results, Operands, ImpResults)));
3152*9880d681SAndroid Build Coastguard Worker continue; // no pattern.
3153*9880d681SAndroid Build Coastguard Worker }
3154*9880d681SAndroid Build Coastguard Worker
3155*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &CGI = Target.getInstruction(Instr);
3156*9880d681SAndroid Build Coastguard Worker const DAGInstruction &DI = parseInstructionPattern(CGI, LI, Instructions);
3157*9880d681SAndroid Build Coastguard Worker
3158*9880d681SAndroid Build Coastguard Worker (void)DI;
3159*9880d681SAndroid Build Coastguard Worker DEBUG(DI.getPattern()->dump());
3160*9880d681SAndroid Build Coastguard Worker }
3161*9880d681SAndroid Build Coastguard Worker
3162*9880d681SAndroid Build Coastguard Worker // If we can, convert the instructions to be patterns that are matched!
3163*9880d681SAndroid Build Coastguard Worker for (auto &Entry : Instructions) {
3164*9880d681SAndroid Build Coastguard Worker DAGInstruction &TheInst = Entry.second;
3165*9880d681SAndroid Build Coastguard Worker TreePattern *I = TheInst.getPattern();
3166*9880d681SAndroid Build Coastguard Worker if (!I) continue; // No pattern.
3167*9880d681SAndroid Build Coastguard Worker
3168*9880d681SAndroid Build Coastguard Worker // FIXME: Assume only the first tree is the pattern. The others are clobber
3169*9880d681SAndroid Build Coastguard Worker // nodes.
3170*9880d681SAndroid Build Coastguard Worker TreePatternNode *Pattern = I->getTree(0);
3171*9880d681SAndroid Build Coastguard Worker TreePatternNode *SrcPattern;
3172*9880d681SAndroid Build Coastguard Worker if (Pattern->getOperator()->getName() == "set") {
3173*9880d681SAndroid Build Coastguard Worker SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone();
3174*9880d681SAndroid Build Coastguard Worker } else{
3175*9880d681SAndroid Build Coastguard Worker // Not a set (store or something?)
3176*9880d681SAndroid Build Coastguard Worker SrcPattern = Pattern;
3177*9880d681SAndroid Build Coastguard Worker }
3178*9880d681SAndroid Build Coastguard Worker
3179*9880d681SAndroid Build Coastguard Worker Record *Instr = Entry.first;
3180*9880d681SAndroid Build Coastguard Worker AddPatternToMatch(I,
3181*9880d681SAndroid Build Coastguard Worker PatternToMatch(Instr,
3182*9880d681SAndroid Build Coastguard Worker Instr->getValueAsListInit("Predicates"),
3183*9880d681SAndroid Build Coastguard Worker SrcPattern,
3184*9880d681SAndroid Build Coastguard Worker TheInst.getResultPattern(),
3185*9880d681SAndroid Build Coastguard Worker TheInst.getImpResults(),
3186*9880d681SAndroid Build Coastguard Worker Instr->getValueAsInt("AddedComplexity"),
3187*9880d681SAndroid Build Coastguard Worker Instr->getID()));
3188*9880d681SAndroid Build Coastguard Worker }
3189*9880d681SAndroid Build Coastguard Worker }
3190*9880d681SAndroid Build Coastguard Worker
3191*9880d681SAndroid Build Coastguard Worker
3192*9880d681SAndroid Build Coastguard Worker typedef std::pair<const TreePatternNode*, unsigned> NameRecord;
3193*9880d681SAndroid Build Coastguard Worker
FindNames(const TreePatternNode * P,std::map<std::string,NameRecord> & Names,TreePattern * PatternTop)3194*9880d681SAndroid Build Coastguard Worker static void FindNames(const TreePatternNode *P,
3195*9880d681SAndroid Build Coastguard Worker std::map<std::string, NameRecord> &Names,
3196*9880d681SAndroid Build Coastguard Worker TreePattern *PatternTop) {
3197*9880d681SAndroid Build Coastguard Worker if (!P->getName().empty()) {
3198*9880d681SAndroid Build Coastguard Worker NameRecord &Rec = Names[P->getName()];
3199*9880d681SAndroid Build Coastguard Worker // If this is the first instance of the name, remember the node.
3200*9880d681SAndroid Build Coastguard Worker if (Rec.second++ == 0)
3201*9880d681SAndroid Build Coastguard Worker Rec.first = P;
3202*9880d681SAndroid Build Coastguard Worker else if (Rec.first->getExtTypes() != P->getExtTypes())
3203*9880d681SAndroid Build Coastguard Worker PatternTop->error("repetition of value: $" + P->getName() +
3204*9880d681SAndroid Build Coastguard Worker " where different uses have different types!");
3205*9880d681SAndroid Build Coastguard Worker }
3206*9880d681SAndroid Build Coastguard Worker
3207*9880d681SAndroid Build Coastguard Worker if (!P->isLeaf()) {
3208*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
3209*9880d681SAndroid Build Coastguard Worker FindNames(P->getChild(i), Names, PatternTop);
3210*9880d681SAndroid Build Coastguard Worker }
3211*9880d681SAndroid Build Coastguard Worker }
3212*9880d681SAndroid Build Coastguard Worker
AddPatternToMatch(TreePattern * Pattern,const PatternToMatch & PTM)3213*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern,
3214*9880d681SAndroid Build Coastguard Worker const PatternToMatch &PTM) {
3215*9880d681SAndroid Build Coastguard Worker // Do some sanity checking on the pattern we're about to match.
3216*9880d681SAndroid Build Coastguard Worker std::string Reason;
3217*9880d681SAndroid Build Coastguard Worker if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) {
3218*9880d681SAndroid Build Coastguard Worker PrintWarning(Pattern->getRecord()->getLoc(),
3219*9880d681SAndroid Build Coastguard Worker Twine("Pattern can never match: ") + Reason);
3220*9880d681SAndroid Build Coastguard Worker return;
3221*9880d681SAndroid Build Coastguard Worker }
3222*9880d681SAndroid Build Coastguard Worker
3223*9880d681SAndroid Build Coastguard Worker // If the source pattern's root is a complex pattern, that complex pattern
3224*9880d681SAndroid Build Coastguard Worker // must specify the nodes it can potentially match.
3225*9880d681SAndroid Build Coastguard Worker if (const ComplexPattern *CP =
3226*9880d681SAndroid Build Coastguard Worker PTM.getSrcPattern()->getComplexPatternInfo(*this))
3227*9880d681SAndroid Build Coastguard Worker if (CP->getRootNodes().empty())
3228*9880d681SAndroid Build Coastguard Worker Pattern->error("ComplexPattern at root must specify list of opcodes it"
3229*9880d681SAndroid Build Coastguard Worker " could match");
3230*9880d681SAndroid Build Coastguard Worker
3231*9880d681SAndroid Build Coastguard Worker
3232*9880d681SAndroid Build Coastguard Worker // Find all of the named values in the input and output, ensure they have the
3233*9880d681SAndroid Build Coastguard Worker // same type.
3234*9880d681SAndroid Build Coastguard Worker std::map<std::string, NameRecord> SrcNames, DstNames;
3235*9880d681SAndroid Build Coastguard Worker FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
3236*9880d681SAndroid Build Coastguard Worker FindNames(PTM.getDstPattern(), DstNames, Pattern);
3237*9880d681SAndroid Build Coastguard Worker
3238*9880d681SAndroid Build Coastguard Worker // Scan all of the named values in the destination pattern, rejecting them if
3239*9880d681SAndroid Build Coastguard Worker // they don't exist in the input pattern.
3240*9880d681SAndroid Build Coastguard Worker for (const auto &Entry : DstNames) {
3241*9880d681SAndroid Build Coastguard Worker if (SrcNames[Entry.first].first == nullptr)
3242*9880d681SAndroid Build Coastguard Worker Pattern->error("Pattern has input without matching name in output: $" +
3243*9880d681SAndroid Build Coastguard Worker Entry.first);
3244*9880d681SAndroid Build Coastguard Worker }
3245*9880d681SAndroid Build Coastguard Worker
3246*9880d681SAndroid Build Coastguard Worker // Scan all of the named values in the source pattern, rejecting them if the
3247*9880d681SAndroid Build Coastguard Worker // name isn't used in the dest, and isn't used to tie two values together.
3248*9880d681SAndroid Build Coastguard Worker for (const auto &Entry : SrcNames)
3249*9880d681SAndroid Build Coastguard Worker if (DstNames[Entry.first].first == nullptr &&
3250*9880d681SAndroid Build Coastguard Worker SrcNames[Entry.first].second == 1)
3251*9880d681SAndroid Build Coastguard Worker Pattern->error("Pattern has dead named input: $" + Entry.first);
3252*9880d681SAndroid Build Coastguard Worker
3253*9880d681SAndroid Build Coastguard Worker PatternsToMatch.push_back(PTM);
3254*9880d681SAndroid Build Coastguard Worker }
3255*9880d681SAndroid Build Coastguard Worker
3256*9880d681SAndroid Build Coastguard Worker
3257*9880d681SAndroid Build Coastguard Worker
InferInstructionFlags()3258*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::InferInstructionFlags() {
3259*9880d681SAndroid Build Coastguard Worker ArrayRef<const CodeGenInstruction*> Instructions =
3260*9880d681SAndroid Build Coastguard Worker Target.getInstructionsByEnumValue();
3261*9880d681SAndroid Build Coastguard Worker
3262*9880d681SAndroid Build Coastguard Worker // First try to infer flags from the primary instruction pattern, if any.
3263*9880d681SAndroid Build Coastguard Worker SmallVector<CodeGenInstruction*, 8> Revisit;
3264*9880d681SAndroid Build Coastguard Worker unsigned Errors = 0;
3265*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
3266*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &InstInfo =
3267*9880d681SAndroid Build Coastguard Worker const_cast<CodeGenInstruction &>(*Instructions[i]);
3268*9880d681SAndroid Build Coastguard Worker
3269*9880d681SAndroid Build Coastguard Worker // Get the primary instruction pattern.
3270*9880d681SAndroid Build Coastguard Worker const TreePattern *Pattern = getInstruction(InstInfo.TheDef).getPattern();
3271*9880d681SAndroid Build Coastguard Worker if (!Pattern) {
3272*9880d681SAndroid Build Coastguard Worker if (InstInfo.hasUndefFlags())
3273*9880d681SAndroid Build Coastguard Worker Revisit.push_back(&InstInfo);
3274*9880d681SAndroid Build Coastguard Worker continue;
3275*9880d681SAndroid Build Coastguard Worker }
3276*9880d681SAndroid Build Coastguard Worker InstAnalyzer PatInfo(*this);
3277*9880d681SAndroid Build Coastguard Worker PatInfo.Analyze(Pattern);
3278*9880d681SAndroid Build Coastguard Worker Errors += InferFromPattern(InstInfo, PatInfo, InstInfo.TheDef);
3279*9880d681SAndroid Build Coastguard Worker }
3280*9880d681SAndroid Build Coastguard Worker
3281*9880d681SAndroid Build Coastguard Worker // Second, look for single-instruction patterns defined outside the
3282*9880d681SAndroid Build Coastguard Worker // instruction.
3283*9880d681SAndroid Build Coastguard Worker for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) {
3284*9880d681SAndroid Build Coastguard Worker const PatternToMatch &PTM = *I;
3285*9880d681SAndroid Build Coastguard Worker
3286*9880d681SAndroid Build Coastguard Worker // We can only infer from single-instruction patterns, otherwise we won't
3287*9880d681SAndroid Build Coastguard Worker // know which instruction should get the flags.
3288*9880d681SAndroid Build Coastguard Worker SmallVector<Record*, 8> PatInstrs;
3289*9880d681SAndroid Build Coastguard Worker getInstructionsInTree(PTM.getDstPattern(), PatInstrs);
3290*9880d681SAndroid Build Coastguard Worker if (PatInstrs.size() != 1)
3291*9880d681SAndroid Build Coastguard Worker continue;
3292*9880d681SAndroid Build Coastguard Worker
3293*9880d681SAndroid Build Coastguard Worker // Get the single instruction.
3294*9880d681SAndroid Build Coastguard Worker CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front());
3295*9880d681SAndroid Build Coastguard Worker
3296*9880d681SAndroid Build Coastguard Worker // Only infer properties from the first pattern. We'll verify the others.
3297*9880d681SAndroid Build Coastguard Worker if (InstInfo.InferredFrom)
3298*9880d681SAndroid Build Coastguard Worker continue;
3299*9880d681SAndroid Build Coastguard Worker
3300*9880d681SAndroid Build Coastguard Worker InstAnalyzer PatInfo(*this);
3301*9880d681SAndroid Build Coastguard Worker PatInfo.Analyze(&PTM);
3302*9880d681SAndroid Build Coastguard Worker Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord());
3303*9880d681SAndroid Build Coastguard Worker }
3304*9880d681SAndroid Build Coastguard Worker
3305*9880d681SAndroid Build Coastguard Worker if (Errors)
3306*9880d681SAndroid Build Coastguard Worker PrintFatalError("pattern conflicts");
3307*9880d681SAndroid Build Coastguard Worker
3308*9880d681SAndroid Build Coastguard Worker // Revisit instructions with undefined flags and no pattern.
3309*9880d681SAndroid Build Coastguard Worker if (Target.guessInstructionProperties()) {
3310*9880d681SAndroid Build Coastguard Worker for (CodeGenInstruction *InstInfo : Revisit) {
3311*9880d681SAndroid Build Coastguard Worker if (InstInfo->InferredFrom)
3312*9880d681SAndroid Build Coastguard Worker continue;
3313*9880d681SAndroid Build Coastguard Worker // The mayLoad and mayStore flags default to false.
3314*9880d681SAndroid Build Coastguard Worker // Conservatively assume hasSideEffects if it wasn't explicit.
3315*9880d681SAndroid Build Coastguard Worker if (InstInfo->hasSideEffects_Unset)
3316*9880d681SAndroid Build Coastguard Worker InstInfo->hasSideEffects = true;
3317*9880d681SAndroid Build Coastguard Worker }
3318*9880d681SAndroid Build Coastguard Worker return;
3319*9880d681SAndroid Build Coastguard Worker }
3320*9880d681SAndroid Build Coastguard Worker
3321*9880d681SAndroid Build Coastguard Worker // Complain about any flags that are still undefined.
3322*9880d681SAndroid Build Coastguard Worker for (CodeGenInstruction *InstInfo : Revisit) {
3323*9880d681SAndroid Build Coastguard Worker if (InstInfo->InferredFrom)
3324*9880d681SAndroid Build Coastguard Worker continue;
3325*9880d681SAndroid Build Coastguard Worker if (InstInfo->hasSideEffects_Unset)
3326*9880d681SAndroid Build Coastguard Worker PrintError(InstInfo->TheDef->getLoc(),
3327*9880d681SAndroid Build Coastguard Worker "Can't infer hasSideEffects from patterns");
3328*9880d681SAndroid Build Coastguard Worker if (InstInfo->mayStore_Unset)
3329*9880d681SAndroid Build Coastguard Worker PrintError(InstInfo->TheDef->getLoc(),
3330*9880d681SAndroid Build Coastguard Worker "Can't infer mayStore from patterns");
3331*9880d681SAndroid Build Coastguard Worker if (InstInfo->mayLoad_Unset)
3332*9880d681SAndroid Build Coastguard Worker PrintError(InstInfo->TheDef->getLoc(),
3333*9880d681SAndroid Build Coastguard Worker "Can't infer mayLoad from patterns");
3334*9880d681SAndroid Build Coastguard Worker }
3335*9880d681SAndroid Build Coastguard Worker }
3336*9880d681SAndroid Build Coastguard Worker
3337*9880d681SAndroid Build Coastguard Worker
3338*9880d681SAndroid Build Coastguard Worker /// Verify instruction flags against pattern node properties.
VerifyInstructionFlags()3339*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::VerifyInstructionFlags() {
3340*9880d681SAndroid Build Coastguard Worker unsigned Errors = 0;
3341*9880d681SAndroid Build Coastguard Worker for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) {
3342*9880d681SAndroid Build Coastguard Worker const PatternToMatch &PTM = *I;
3343*9880d681SAndroid Build Coastguard Worker SmallVector<Record*, 8> Instrs;
3344*9880d681SAndroid Build Coastguard Worker getInstructionsInTree(PTM.getDstPattern(), Instrs);
3345*9880d681SAndroid Build Coastguard Worker if (Instrs.empty())
3346*9880d681SAndroid Build Coastguard Worker continue;
3347*9880d681SAndroid Build Coastguard Worker
3348*9880d681SAndroid Build Coastguard Worker // Count the number of instructions with each flag set.
3349*9880d681SAndroid Build Coastguard Worker unsigned NumSideEffects = 0;
3350*9880d681SAndroid Build Coastguard Worker unsigned NumStores = 0;
3351*9880d681SAndroid Build Coastguard Worker unsigned NumLoads = 0;
3352*9880d681SAndroid Build Coastguard Worker for (const Record *Instr : Instrs) {
3353*9880d681SAndroid Build Coastguard Worker const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
3354*9880d681SAndroid Build Coastguard Worker NumSideEffects += InstInfo.hasSideEffects;
3355*9880d681SAndroid Build Coastguard Worker NumStores += InstInfo.mayStore;
3356*9880d681SAndroid Build Coastguard Worker NumLoads += InstInfo.mayLoad;
3357*9880d681SAndroid Build Coastguard Worker }
3358*9880d681SAndroid Build Coastguard Worker
3359*9880d681SAndroid Build Coastguard Worker // Analyze the source pattern.
3360*9880d681SAndroid Build Coastguard Worker InstAnalyzer PatInfo(*this);
3361*9880d681SAndroid Build Coastguard Worker PatInfo.Analyze(&PTM);
3362*9880d681SAndroid Build Coastguard Worker
3363*9880d681SAndroid Build Coastguard Worker // Collect error messages.
3364*9880d681SAndroid Build Coastguard Worker SmallVector<std::string, 4> Msgs;
3365*9880d681SAndroid Build Coastguard Worker
3366*9880d681SAndroid Build Coastguard Worker // Check for missing flags in the output.
3367*9880d681SAndroid Build Coastguard Worker // Permit extra flags for now at least.
3368*9880d681SAndroid Build Coastguard Worker if (PatInfo.hasSideEffects && !NumSideEffects)
3369*9880d681SAndroid Build Coastguard Worker Msgs.push_back("pattern has side effects, but hasSideEffects isn't set");
3370*9880d681SAndroid Build Coastguard Worker
3371*9880d681SAndroid Build Coastguard Worker // Don't verify store flags on instructions with side effects. At least for
3372*9880d681SAndroid Build Coastguard Worker // intrinsics, side effects implies mayStore.
3373*9880d681SAndroid Build Coastguard Worker if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores)
3374*9880d681SAndroid Build Coastguard Worker Msgs.push_back("pattern may store, but mayStore isn't set");
3375*9880d681SAndroid Build Coastguard Worker
3376*9880d681SAndroid Build Coastguard Worker // Similarly, mayStore implies mayLoad on intrinsics.
3377*9880d681SAndroid Build Coastguard Worker if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads)
3378*9880d681SAndroid Build Coastguard Worker Msgs.push_back("pattern may load, but mayLoad isn't set");
3379*9880d681SAndroid Build Coastguard Worker
3380*9880d681SAndroid Build Coastguard Worker // Print error messages.
3381*9880d681SAndroid Build Coastguard Worker if (Msgs.empty())
3382*9880d681SAndroid Build Coastguard Worker continue;
3383*9880d681SAndroid Build Coastguard Worker ++Errors;
3384*9880d681SAndroid Build Coastguard Worker
3385*9880d681SAndroid Build Coastguard Worker for (const std::string &Msg : Msgs)
3386*9880d681SAndroid Build Coastguard Worker PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msg) + " on the " +
3387*9880d681SAndroid Build Coastguard Worker (Instrs.size() == 1 ?
3388*9880d681SAndroid Build Coastguard Worker "instruction" : "output instructions"));
3389*9880d681SAndroid Build Coastguard Worker // Provide the location of the relevant instruction definitions.
3390*9880d681SAndroid Build Coastguard Worker for (const Record *Instr : Instrs) {
3391*9880d681SAndroid Build Coastguard Worker if (Instr != PTM.getSrcRecord())
3392*9880d681SAndroid Build Coastguard Worker PrintError(Instr->getLoc(), "defined here");
3393*9880d681SAndroid Build Coastguard Worker const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
3394*9880d681SAndroid Build Coastguard Worker if (InstInfo.InferredFrom &&
3395*9880d681SAndroid Build Coastguard Worker InstInfo.InferredFrom != InstInfo.TheDef &&
3396*9880d681SAndroid Build Coastguard Worker InstInfo.InferredFrom != PTM.getSrcRecord())
3397*9880d681SAndroid Build Coastguard Worker PrintError(InstInfo.InferredFrom->getLoc(), "inferred from pattern");
3398*9880d681SAndroid Build Coastguard Worker }
3399*9880d681SAndroid Build Coastguard Worker }
3400*9880d681SAndroid Build Coastguard Worker if (Errors)
3401*9880d681SAndroid Build Coastguard Worker PrintFatalError("Errors in DAG patterns");
3402*9880d681SAndroid Build Coastguard Worker }
3403*9880d681SAndroid Build Coastguard Worker
3404*9880d681SAndroid Build Coastguard Worker /// Given a pattern result with an unresolved type, see if we can find one
3405*9880d681SAndroid Build Coastguard Worker /// instruction with an unresolved result type. Force this result type to an
3406*9880d681SAndroid Build Coastguard Worker /// arbitrary element if it's possible types to converge results.
ForceArbitraryInstResultType(TreePatternNode * N,TreePattern & TP)3407*9880d681SAndroid Build Coastguard Worker static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) {
3408*9880d681SAndroid Build Coastguard Worker if (N->isLeaf())
3409*9880d681SAndroid Build Coastguard Worker return false;
3410*9880d681SAndroid Build Coastguard Worker
3411*9880d681SAndroid Build Coastguard Worker // Analyze children.
3412*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
3413*9880d681SAndroid Build Coastguard Worker if (ForceArbitraryInstResultType(N->getChild(i), TP))
3414*9880d681SAndroid Build Coastguard Worker return true;
3415*9880d681SAndroid Build Coastguard Worker
3416*9880d681SAndroid Build Coastguard Worker if (!N->getOperator()->isSubClassOf("Instruction"))
3417*9880d681SAndroid Build Coastguard Worker return false;
3418*9880d681SAndroid Build Coastguard Worker
3419*9880d681SAndroid Build Coastguard Worker // If this type is already concrete or completely unknown we can't do
3420*9880d681SAndroid Build Coastguard Worker // anything.
3421*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) {
3422*9880d681SAndroid Build Coastguard Worker if (N->getExtType(i).isCompletelyUnknown() || N->getExtType(i).isConcrete())
3423*9880d681SAndroid Build Coastguard Worker continue;
3424*9880d681SAndroid Build Coastguard Worker
3425*9880d681SAndroid Build Coastguard Worker // Otherwise, force its type to the first possibility (an arbitrary choice).
3426*9880d681SAndroid Build Coastguard Worker if (N->getExtType(i).MergeInTypeInfo(N->getExtType(i).getTypeList()[0], TP))
3427*9880d681SAndroid Build Coastguard Worker return true;
3428*9880d681SAndroid Build Coastguard Worker }
3429*9880d681SAndroid Build Coastguard Worker
3430*9880d681SAndroid Build Coastguard Worker return false;
3431*9880d681SAndroid Build Coastguard Worker }
3432*9880d681SAndroid Build Coastguard Worker
ParsePatterns()3433*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::ParsePatterns() {
3434*9880d681SAndroid Build Coastguard Worker std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
3435*9880d681SAndroid Build Coastguard Worker
3436*9880d681SAndroid Build Coastguard Worker for (Record *CurPattern : Patterns) {
3437*9880d681SAndroid Build Coastguard Worker DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
3438*9880d681SAndroid Build Coastguard Worker
3439*9880d681SAndroid Build Coastguard Worker // If the pattern references the null_frag, there's nothing to do.
3440*9880d681SAndroid Build Coastguard Worker if (hasNullFragReference(Tree))
3441*9880d681SAndroid Build Coastguard Worker continue;
3442*9880d681SAndroid Build Coastguard Worker
3443*9880d681SAndroid Build Coastguard Worker TreePattern *Pattern = new TreePattern(CurPattern, Tree, true, *this);
3444*9880d681SAndroid Build Coastguard Worker
3445*9880d681SAndroid Build Coastguard Worker // Inline pattern fragments into it.
3446*9880d681SAndroid Build Coastguard Worker Pattern->InlinePatternFragments();
3447*9880d681SAndroid Build Coastguard Worker
3448*9880d681SAndroid Build Coastguard Worker ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs");
3449*9880d681SAndroid Build Coastguard Worker if (LI->empty()) continue; // no pattern.
3450*9880d681SAndroid Build Coastguard Worker
3451*9880d681SAndroid Build Coastguard Worker // Parse the instruction.
3452*9880d681SAndroid Build Coastguard Worker TreePattern Result(CurPattern, LI, false, *this);
3453*9880d681SAndroid Build Coastguard Worker
3454*9880d681SAndroid Build Coastguard Worker // Inline pattern fragments into it.
3455*9880d681SAndroid Build Coastguard Worker Result.InlinePatternFragments();
3456*9880d681SAndroid Build Coastguard Worker
3457*9880d681SAndroid Build Coastguard Worker if (Result.getNumTrees() != 1)
3458*9880d681SAndroid Build Coastguard Worker Result.error("Cannot handle instructions producing instructions "
3459*9880d681SAndroid Build Coastguard Worker "with temporaries yet!");
3460*9880d681SAndroid Build Coastguard Worker
3461*9880d681SAndroid Build Coastguard Worker bool IterateInference;
3462*9880d681SAndroid Build Coastguard Worker bool InferredAllPatternTypes, InferredAllResultTypes;
3463*9880d681SAndroid Build Coastguard Worker do {
3464*9880d681SAndroid Build Coastguard Worker // Infer as many types as possible. If we cannot infer all of them, we
3465*9880d681SAndroid Build Coastguard Worker // can never do anything with this pattern: report it to the user.
3466*9880d681SAndroid Build Coastguard Worker InferredAllPatternTypes =
3467*9880d681SAndroid Build Coastguard Worker Pattern->InferAllTypes(&Pattern->getNamedNodesMap());
3468*9880d681SAndroid Build Coastguard Worker
3469*9880d681SAndroid Build Coastguard Worker // Infer as many types as possible. If we cannot infer all of them, we
3470*9880d681SAndroid Build Coastguard Worker // can never do anything with this pattern: report it to the user.
3471*9880d681SAndroid Build Coastguard Worker InferredAllResultTypes =
3472*9880d681SAndroid Build Coastguard Worker Result.InferAllTypes(&Pattern->getNamedNodesMap());
3473*9880d681SAndroid Build Coastguard Worker
3474*9880d681SAndroid Build Coastguard Worker IterateInference = false;
3475*9880d681SAndroid Build Coastguard Worker
3476*9880d681SAndroid Build Coastguard Worker // Apply the type of the result to the source pattern. This helps us
3477*9880d681SAndroid Build Coastguard Worker // resolve cases where the input type is known to be a pointer type (which
3478*9880d681SAndroid Build Coastguard Worker // is considered resolved), but the result knows it needs to be 32- or
3479*9880d681SAndroid Build Coastguard Worker // 64-bits. Infer the other way for good measure.
3480*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = std::min(Result.getTree(0)->getNumTypes(),
3481*9880d681SAndroid Build Coastguard Worker Pattern->getTree(0)->getNumTypes());
3482*9880d681SAndroid Build Coastguard Worker i != e; ++i) {
3483*9880d681SAndroid Build Coastguard Worker IterateInference = Pattern->getTree(0)->UpdateNodeType(
3484*9880d681SAndroid Build Coastguard Worker i, Result.getTree(0)->getExtType(i), Result);
3485*9880d681SAndroid Build Coastguard Worker IterateInference |= Result.getTree(0)->UpdateNodeType(
3486*9880d681SAndroid Build Coastguard Worker i, Pattern->getTree(0)->getExtType(i), Result);
3487*9880d681SAndroid Build Coastguard Worker }
3488*9880d681SAndroid Build Coastguard Worker
3489*9880d681SAndroid Build Coastguard Worker // If our iteration has converged and the input pattern's types are fully
3490*9880d681SAndroid Build Coastguard Worker // resolved but the result pattern is not fully resolved, we may have a
3491*9880d681SAndroid Build Coastguard Worker // situation where we have two instructions in the result pattern and
3492*9880d681SAndroid Build Coastguard Worker // the instructions require a common register class, but don't care about
3493*9880d681SAndroid Build Coastguard Worker // what actual MVT is used. This is actually a bug in our modelling:
3494*9880d681SAndroid Build Coastguard Worker // output patterns should have register classes, not MVTs.
3495*9880d681SAndroid Build Coastguard Worker //
3496*9880d681SAndroid Build Coastguard Worker // In any case, to handle this, we just go through and disambiguate some
3497*9880d681SAndroid Build Coastguard Worker // arbitrary types to the result pattern's nodes.
3498*9880d681SAndroid Build Coastguard Worker if (!IterateInference && InferredAllPatternTypes &&
3499*9880d681SAndroid Build Coastguard Worker !InferredAllResultTypes)
3500*9880d681SAndroid Build Coastguard Worker IterateInference =
3501*9880d681SAndroid Build Coastguard Worker ForceArbitraryInstResultType(Result.getTree(0), Result);
3502*9880d681SAndroid Build Coastguard Worker } while (IterateInference);
3503*9880d681SAndroid Build Coastguard Worker
3504*9880d681SAndroid Build Coastguard Worker // Verify that we inferred enough types that we can do something with the
3505*9880d681SAndroid Build Coastguard Worker // pattern and result. If these fire the user has to add type casts.
3506*9880d681SAndroid Build Coastguard Worker if (!InferredAllPatternTypes)
3507*9880d681SAndroid Build Coastguard Worker Pattern->error("Could not infer all types in pattern!");
3508*9880d681SAndroid Build Coastguard Worker if (!InferredAllResultTypes) {
3509*9880d681SAndroid Build Coastguard Worker Pattern->dump();
3510*9880d681SAndroid Build Coastguard Worker Result.error("Could not infer all types in pattern result!");
3511*9880d681SAndroid Build Coastguard Worker }
3512*9880d681SAndroid Build Coastguard Worker
3513*9880d681SAndroid Build Coastguard Worker // Validate that the input pattern is correct.
3514*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> InstInputs;
3515*9880d681SAndroid Build Coastguard Worker std::map<std::string, TreePatternNode*> InstResults;
3516*9880d681SAndroid Build Coastguard Worker std::vector<Record*> InstImpResults;
3517*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j)
3518*9880d681SAndroid Build Coastguard Worker FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j),
3519*9880d681SAndroid Build Coastguard Worker InstInputs, InstResults,
3520*9880d681SAndroid Build Coastguard Worker InstImpResults);
3521*9880d681SAndroid Build Coastguard Worker
3522*9880d681SAndroid Build Coastguard Worker // Promote the xform function to be an explicit node if set.
3523*9880d681SAndroid Build Coastguard Worker TreePatternNode *DstPattern = Result.getOnlyTree();
3524*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> ResultNodeOperands;
3525*9880d681SAndroid Build Coastguard Worker for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) {
3526*9880d681SAndroid Build Coastguard Worker TreePatternNode *OpNode = DstPattern->getChild(ii);
3527*9880d681SAndroid Build Coastguard Worker if (Record *Xform = OpNode->getTransformFn()) {
3528*9880d681SAndroid Build Coastguard Worker OpNode->setTransformFn(nullptr);
3529*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> Children;
3530*9880d681SAndroid Build Coastguard Worker Children.push_back(OpNode);
3531*9880d681SAndroid Build Coastguard Worker OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes());
3532*9880d681SAndroid Build Coastguard Worker }
3533*9880d681SAndroid Build Coastguard Worker ResultNodeOperands.push_back(OpNode);
3534*9880d681SAndroid Build Coastguard Worker }
3535*9880d681SAndroid Build Coastguard Worker DstPattern = Result.getOnlyTree();
3536*9880d681SAndroid Build Coastguard Worker if (!DstPattern->isLeaf())
3537*9880d681SAndroid Build Coastguard Worker DstPattern = new TreePatternNode(DstPattern->getOperator(),
3538*9880d681SAndroid Build Coastguard Worker ResultNodeOperands,
3539*9880d681SAndroid Build Coastguard Worker DstPattern->getNumTypes());
3540*9880d681SAndroid Build Coastguard Worker
3541*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Result.getOnlyTree()->getNumTypes(); i != e; ++i)
3542*9880d681SAndroid Build Coastguard Worker DstPattern->setType(i, Result.getOnlyTree()->getExtType(i));
3543*9880d681SAndroid Build Coastguard Worker
3544*9880d681SAndroid Build Coastguard Worker TreePattern Temp(Result.getRecord(), DstPattern, false, *this);
3545*9880d681SAndroid Build Coastguard Worker Temp.InferAllTypes();
3546*9880d681SAndroid Build Coastguard Worker
3547*9880d681SAndroid Build Coastguard Worker
3548*9880d681SAndroid Build Coastguard Worker AddPatternToMatch(Pattern,
3549*9880d681SAndroid Build Coastguard Worker PatternToMatch(CurPattern,
3550*9880d681SAndroid Build Coastguard Worker CurPattern->getValueAsListInit("Predicates"),
3551*9880d681SAndroid Build Coastguard Worker Pattern->getTree(0),
3552*9880d681SAndroid Build Coastguard Worker Temp.getOnlyTree(), InstImpResults,
3553*9880d681SAndroid Build Coastguard Worker CurPattern->getValueAsInt("AddedComplexity"),
3554*9880d681SAndroid Build Coastguard Worker CurPattern->getID()));
3555*9880d681SAndroid Build Coastguard Worker }
3556*9880d681SAndroid Build Coastguard Worker }
3557*9880d681SAndroid Build Coastguard Worker
3558*9880d681SAndroid Build Coastguard Worker /// CombineChildVariants - Given a bunch of permutations of each child of the
3559*9880d681SAndroid Build Coastguard Worker /// 'operator' node, put them together in all possible ways.
CombineChildVariants(TreePatternNode * Orig,const std::vector<std::vector<TreePatternNode * >> & ChildVariants,std::vector<TreePatternNode * > & OutVariants,CodeGenDAGPatterns & CDP,const MultipleUseVarSet & DepVars)3560*9880d681SAndroid Build Coastguard Worker static void CombineChildVariants(TreePatternNode *Orig,
3561*9880d681SAndroid Build Coastguard Worker const std::vector<std::vector<TreePatternNode*> > &ChildVariants,
3562*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> &OutVariants,
3563*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &CDP,
3564*9880d681SAndroid Build Coastguard Worker const MultipleUseVarSet &DepVars) {
3565*9880d681SAndroid Build Coastguard Worker // Make sure that each operand has at least one variant to choose from.
3566*9880d681SAndroid Build Coastguard Worker for (const auto &Variants : ChildVariants)
3567*9880d681SAndroid Build Coastguard Worker if (Variants.empty())
3568*9880d681SAndroid Build Coastguard Worker return;
3569*9880d681SAndroid Build Coastguard Worker
3570*9880d681SAndroid Build Coastguard Worker // The end result is an all-pairs construction of the resultant pattern.
3571*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> Idxs;
3572*9880d681SAndroid Build Coastguard Worker Idxs.resize(ChildVariants.size());
3573*9880d681SAndroid Build Coastguard Worker bool NotDone;
3574*9880d681SAndroid Build Coastguard Worker do {
3575*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
3576*9880d681SAndroid Build Coastguard Worker DEBUG(if (!Idxs.empty()) {
3577*9880d681SAndroid Build Coastguard Worker errs() << Orig->getOperator()->getName() << ": Idxs = [ ";
3578*9880d681SAndroid Build Coastguard Worker for (unsigned Idx : Idxs) {
3579*9880d681SAndroid Build Coastguard Worker errs() << Idx << " ";
3580*9880d681SAndroid Build Coastguard Worker }
3581*9880d681SAndroid Build Coastguard Worker errs() << "]\n";
3582*9880d681SAndroid Build Coastguard Worker });
3583*9880d681SAndroid Build Coastguard Worker #endif
3584*9880d681SAndroid Build Coastguard Worker // Create the variant and add it to the output list.
3585*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> NewChildren;
3586*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
3587*9880d681SAndroid Build Coastguard Worker NewChildren.push_back(ChildVariants[i][Idxs[i]]);
3588*9880d681SAndroid Build Coastguard Worker auto R = llvm::make_unique<TreePatternNode>(
3589*9880d681SAndroid Build Coastguard Worker Orig->getOperator(), NewChildren, Orig->getNumTypes());
3590*9880d681SAndroid Build Coastguard Worker
3591*9880d681SAndroid Build Coastguard Worker // Copy over properties.
3592*9880d681SAndroid Build Coastguard Worker R->setName(Orig->getName());
3593*9880d681SAndroid Build Coastguard Worker R->setPredicateFns(Orig->getPredicateFns());
3594*9880d681SAndroid Build Coastguard Worker R->setTransformFn(Orig->getTransformFn());
3595*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i)
3596*9880d681SAndroid Build Coastguard Worker R->setType(i, Orig->getExtType(i));
3597*9880d681SAndroid Build Coastguard Worker
3598*9880d681SAndroid Build Coastguard Worker // If this pattern cannot match, do not include it as a variant.
3599*9880d681SAndroid Build Coastguard Worker std::string ErrString;
3600*9880d681SAndroid Build Coastguard Worker // Scan to see if this pattern has already been emitted. We can get
3601*9880d681SAndroid Build Coastguard Worker // duplication due to things like commuting:
3602*9880d681SAndroid Build Coastguard Worker // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
3603*9880d681SAndroid Build Coastguard Worker // which are the same pattern. Ignore the dups.
3604*9880d681SAndroid Build Coastguard Worker if (R->canPatternMatch(ErrString, CDP) &&
3605*9880d681SAndroid Build Coastguard Worker std::none_of(OutVariants.begin(), OutVariants.end(),
3606*9880d681SAndroid Build Coastguard Worker [&](TreePatternNode *Variant) {
3607*9880d681SAndroid Build Coastguard Worker return R->isIsomorphicTo(Variant, DepVars);
3608*9880d681SAndroid Build Coastguard Worker }))
3609*9880d681SAndroid Build Coastguard Worker OutVariants.push_back(R.release());
3610*9880d681SAndroid Build Coastguard Worker
3611*9880d681SAndroid Build Coastguard Worker // Increment indices to the next permutation by incrementing the
3612*9880d681SAndroid Build Coastguard Worker // indices from last index backward, e.g., generate the sequence
3613*9880d681SAndroid Build Coastguard Worker // [0, 0], [0, 1], [1, 0], [1, 1].
3614*9880d681SAndroid Build Coastguard Worker int IdxsIdx;
3615*9880d681SAndroid Build Coastguard Worker for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
3616*9880d681SAndroid Build Coastguard Worker if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size())
3617*9880d681SAndroid Build Coastguard Worker Idxs[IdxsIdx] = 0;
3618*9880d681SAndroid Build Coastguard Worker else
3619*9880d681SAndroid Build Coastguard Worker break;
3620*9880d681SAndroid Build Coastguard Worker }
3621*9880d681SAndroid Build Coastguard Worker NotDone = (IdxsIdx >= 0);
3622*9880d681SAndroid Build Coastguard Worker } while (NotDone);
3623*9880d681SAndroid Build Coastguard Worker }
3624*9880d681SAndroid Build Coastguard Worker
3625*9880d681SAndroid Build Coastguard Worker /// CombineChildVariants - A helper function for binary operators.
3626*9880d681SAndroid Build Coastguard Worker ///
CombineChildVariants(TreePatternNode * Orig,const std::vector<TreePatternNode * > & LHS,const std::vector<TreePatternNode * > & RHS,std::vector<TreePatternNode * > & OutVariants,CodeGenDAGPatterns & CDP,const MultipleUseVarSet & DepVars)3627*9880d681SAndroid Build Coastguard Worker static void CombineChildVariants(TreePatternNode *Orig,
3628*9880d681SAndroid Build Coastguard Worker const std::vector<TreePatternNode*> &LHS,
3629*9880d681SAndroid Build Coastguard Worker const std::vector<TreePatternNode*> &RHS,
3630*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> &OutVariants,
3631*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &CDP,
3632*9880d681SAndroid Build Coastguard Worker const MultipleUseVarSet &DepVars) {
3633*9880d681SAndroid Build Coastguard Worker std::vector<std::vector<TreePatternNode*> > ChildVariants;
3634*9880d681SAndroid Build Coastguard Worker ChildVariants.push_back(LHS);
3635*9880d681SAndroid Build Coastguard Worker ChildVariants.push_back(RHS);
3636*9880d681SAndroid Build Coastguard Worker CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars);
3637*9880d681SAndroid Build Coastguard Worker }
3638*9880d681SAndroid Build Coastguard Worker
3639*9880d681SAndroid Build Coastguard Worker
GatherChildrenOfAssociativeOpcode(TreePatternNode * N,std::vector<TreePatternNode * > & Children)3640*9880d681SAndroid Build Coastguard Worker static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N,
3641*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode *> &Children) {
3642*9880d681SAndroid Build Coastguard Worker assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!");
3643*9880d681SAndroid Build Coastguard Worker Record *Operator = N->getOperator();
3644*9880d681SAndroid Build Coastguard Worker
3645*9880d681SAndroid Build Coastguard Worker // Only permit raw nodes.
3646*9880d681SAndroid Build Coastguard Worker if (!N->getName().empty() || !N->getPredicateFns().empty() ||
3647*9880d681SAndroid Build Coastguard Worker N->getTransformFn()) {
3648*9880d681SAndroid Build Coastguard Worker Children.push_back(N);
3649*9880d681SAndroid Build Coastguard Worker return;
3650*9880d681SAndroid Build Coastguard Worker }
3651*9880d681SAndroid Build Coastguard Worker
3652*9880d681SAndroid Build Coastguard Worker if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
3653*9880d681SAndroid Build Coastguard Worker Children.push_back(N->getChild(0));
3654*9880d681SAndroid Build Coastguard Worker else
3655*9880d681SAndroid Build Coastguard Worker GatherChildrenOfAssociativeOpcode(N->getChild(0), Children);
3656*9880d681SAndroid Build Coastguard Worker
3657*9880d681SAndroid Build Coastguard Worker if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
3658*9880d681SAndroid Build Coastguard Worker Children.push_back(N->getChild(1));
3659*9880d681SAndroid Build Coastguard Worker else
3660*9880d681SAndroid Build Coastguard Worker GatherChildrenOfAssociativeOpcode(N->getChild(1), Children);
3661*9880d681SAndroid Build Coastguard Worker }
3662*9880d681SAndroid Build Coastguard Worker
3663*9880d681SAndroid Build Coastguard Worker /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
3664*9880d681SAndroid Build Coastguard Worker /// the (potentially recursive) pattern by using algebraic laws.
3665*9880d681SAndroid Build Coastguard Worker ///
GenerateVariantsOf(TreePatternNode * N,std::vector<TreePatternNode * > & OutVariants,CodeGenDAGPatterns & CDP,const MultipleUseVarSet & DepVars)3666*9880d681SAndroid Build Coastguard Worker static void GenerateVariantsOf(TreePatternNode *N,
3667*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> &OutVariants,
3668*9880d681SAndroid Build Coastguard Worker CodeGenDAGPatterns &CDP,
3669*9880d681SAndroid Build Coastguard Worker const MultipleUseVarSet &DepVars) {
3670*9880d681SAndroid Build Coastguard Worker // We cannot permute leaves or ComplexPattern uses.
3671*9880d681SAndroid Build Coastguard Worker if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) {
3672*9880d681SAndroid Build Coastguard Worker OutVariants.push_back(N);
3673*9880d681SAndroid Build Coastguard Worker return;
3674*9880d681SAndroid Build Coastguard Worker }
3675*9880d681SAndroid Build Coastguard Worker
3676*9880d681SAndroid Build Coastguard Worker // Look up interesting info about the node.
3677*9880d681SAndroid Build Coastguard Worker const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator());
3678*9880d681SAndroid Build Coastguard Worker
3679*9880d681SAndroid Build Coastguard Worker // If this node is associative, re-associate.
3680*9880d681SAndroid Build Coastguard Worker if (NodeInfo.hasProperty(SDNPAssociative)) {
3681*9880d681SAndroid Build Coastguard Worker // Re-associate by pulling together all of the linked operators
3682*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> MaximalChildren;
3683*9880d681SAndroid Build Coastguard Worker GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
3684*9880d681SAndroid Build Coastguard Worker
3685*9880d681SAndroid Build Coastguard Worker // Only handle child sizes of 3. Otherwise we'll end up trying too many
3686*9880d681SAndroid Build Coastguard Worker // permutations.
3687*9880d681SAndroid Build Coastguard Worker if (MaximalChildren.size() == 3) {
3688*9880d681SAndroid Build Coastguard Worker // Find the variants of all of our maximal children.
3689*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> AVariants, BVariants, CVariants;
3690*9880d681SAndroid Build Coastguard Worker GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars);
3691*9880d681SAndroid Build Coastguard Worker GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars);
3692*9880d681SAndroid Build Coastguard Worker GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars);
3693*9880d681SAndroid Build Coastguard Worker
3694*9880d681SAndroid Build Coastguard Worker // There are only two ways we can permute the tree:
3695*9880d681SAndroid Build Coastguard Worker // (A op B) op C and A op (B op C)
3696*9880d681SAndroid Build Coastguard Worker // Within these forms, we can also permute A/B/C.
3697*9880d681SAndroid Build Coastguard Worker
3698*9880d681SAndroid Build Coastguard Worker // Generate legal pair permutations of A/B/C.
3699*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> ABVariants;
3700*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> BAVariants;
3701*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> ACVariants;
3702*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> CAVariants;
3703*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> BCVariants;
3704*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> CBVariants;
3705*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars);
3706*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars);
3707*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars);
3708*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars);
3709*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars);
3710*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars);
3711*9880d681SAndroid Build Coastguard Worker
3712*9880d681SAndroid Build Coastguard Worker // Combine those into the result: (x op x) op x
3713*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars);
3714*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars);
3715*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars);
3716*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars);
3717*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars);
3718*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars);
3719*9880d681SAndroid Build Coastguard Worker
3720*9880d681SAndroid Build Coastguard Worker // Combine those into the result: x op (x op x)
3721*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars);
3722*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars);
3723*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars);
3724*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars);
3725*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars);
3726*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars);
3727*9880d681SAndroid Build Coastguard Worker return;
3728*9880d681SAndroid Build Coastguard Worker }
3729*9880d681SAndroid Build Coastguard Worker }
3730*9880d681SAndroid Build Coastguard Worker
3731*9880d681SAndroid Build Coastguard Worker // Compute permutations of all children.
3732*9880d681SAndroid Build Coastguard Worker std::vector<std::vector<TreePatternNode*> > ChildVariants;
3733*9880d681SAndroid Build Coastguard Worker ChildVariants.resize(N->getNumChildren());
3734*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
3735*9880d681SAndroid Build Coastguard Worker GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars);
3736*9880d681SAndroid Build Coastguard Worker
3737*9880d681SAndroid Build Coastguard Worker // Build all permutations based on how the children were formed.
3738*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars);
3739*9880d681SAndroid Build Coastguard Worker
3740*9880d681SAndroid Build Coastguard Worker // If this node is commutative, consider the commuted order.
3741*9880d681SAndroid Build Coastguard Worker bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP);
3742*9880d681SAndroid Build Coastguard Worker if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
3743*9880d681SAndroid Build Coastguard Worker assert((N->getNumChildren()==2 || isCommIntrinsic) &&
3744*9880d681SAndroid Build Coastguard Worker "Commutative but doesn't have 2 children!");
3745*9880d681SAndroid Build Coastguard Worker // Don't count children which are actually register references.
3746*9880d681SAndroid Build Coastguard Worker unsigned NC = 0;
3747*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
3748*9880d681SAndroid Build Coastguard Worker TreePatternNode *Child = N->getChild(i);
3749*9880d681SAndroid Build Coastguard Worker if (Child->isLeaf())
3750*9880d681SAndroid Build Coastguard Worker if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) {
3751*9880d681SAndroid Build Coastguard Worker Record *RR = DI->getDef();
3752*9880d681SAndroid Build Coastguard Worker if (RR->isSubClassOf("Register"))
3753*9880d681SAndroid Build Coastguard Worker continue;
3754*9880d681SAndroid Build Coastguard Worker }
3755*9880d681SAndroid Build Coastguard Worker NC++;
3756*9880d681SAndroid Build Coastguard Worker }
3757*9880d681SAndroid Build Coastguard Worker // Consider the commuted order.
3758*9880d681SAndroid Build Coastguard Worker if (isCommIntrinsic) {
3759*9880d681SAndroid Build Coastguard Worker // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd
3760*9880d681SAndroid Build Coastguard Worker // operands are the commutative operands, and there might be more operands
3761*9880d681SAndroid Build Coastguard Worker // after those.
3762*9880d681SAndroid Build Coastguard Worker assert(NC >= 3 &&
3763*9880d681SAndroid Build Coastguard Worker "Commutative intrinsic should have at least 3 children!");
3764*9880d681SAndroid Build Coastguard Worker std::vector<std::vector<TreePatternNode*> > Variants;
3765*9880d681SAndroid Build Coastguard Worker Variants.push_back(ChildVariants[0]); // Intrinsic id.
3766*9880d681SAndroid Build Coastguard Worker Variants.push_back(ChildVariants[2]);
3767*9880d681SAndroid Build Coastguard Worker Variants.push_back(ChildVariants[1]);
3768*9880d681SAndroid Build Coastguard Worker for (unsigned i = 3; i != NC; ++i)
3769*9880d681SAndroid Build Coastguard Worker Variants.push_back(ChildVariants[i]);
3770*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
3771*9880d681SAndroid Build Coastguard Worker } else if (NC == 2)
3772*9880d681SAndroid Build Coastguard Worker CombineChildVariants(N, ChildVariants[1], ChildVariants[0],
3773*9880d681SAndroid Build Coastguard Worker OutVariants, CDP, DepVars);
3774*9880d681SAndroid Build Coastguard Worker }
3775*9880d681SAndroid Build Coastguard Worker }
3776*9880d681SAndroid Build Coastguard Worker
3777*9880d681SAndroid Build Coastguard Worker
3778*9880d681SAndroid Build Coastguard Worker // GenerateVariants - Generate variants. For example, commutative patterns can
3779*9880d681SAndroid Build Coastguard Worker // match multiple ways. Add them to PatternsToMatch as well.
GenerateVariants()3780*9880d681SAndroid Build Coastguard Worker void CodeGenDAGPatterns::GenerateVariants() {
3781*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "Generating instruction variants.\n");
3782*9880d681SAndroid Build Coastguard Worker
3783*9880d681SAndroid Build Coastguard Worker // Loop over all of the patterns we've collected, checking to see if we can
3784*9880d681SAndroid Build Coastguard Worker // generate variants of the instruction, through the exploitation of
3785*9880d681SAndroid Build Coastguard Worker // identities. This permits the target to provide aggressive matching without
3786*9880d681SAndroid Build Coastguard Worker // the .td file having to contain tons of variants of instructions.
3787*9880d681SAndroid Build Coastguard Worker //
3788*9880d681SAndroid Build Coastguard Worker // Note that this loop adds new patterns to the PatternsToMatch list, but we
3789*9880d681SAndroid Build Coastguard Worker // intentionally do not reconsider these. Any variants of added patterns have
3790*9880d681SAndroid Build Coastguard Worker // already been added.
3791*9880d681SAndroid Build Coastguard Worker //
3792*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) {
3793*9880d681SAndroid Build Coastguard Worker MultipleUseVarSet DepVars;
3794*9880d681SAndroid Build Coastguard Worker std::vector<TreePatternNode*> Variants;
3795*9880d681SAndroid Build Coastguard Worker FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars);
3796*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "Dependent/multiply used variables: ");
3797*9880d681SAndroid Build Coastguard Worker DEBUG(DumpDepVars(DepVars));
3798*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "\n");
3799*9880d681SAndroid Build Coastguard Worker GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this,
3800*9880d681SAndroid Build Coastguard Worker DepVars);
3801*9880d681SAndroid Build Coastguard Worker
3802*9880d681SAndroid Build Coastguard Worker assert(!Variants.empty() && "Must create at least original variant!");
3803*9880d681SAndroid Build Coastguard Worker Variants.erase(Variants.begin()); // Remove the original pattern.
3804*9880d681SAndroid Build Coastguard Worker
3805*9880d681SAndroid Build Coastguard Worker if (Variants.empty()) // No variants for this pattern.
3806*9880d681SAndroid Build Coastguard Worker continue;
3807*9880d681SAndroid Build Coastguard Worker
3808*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "FOUND VARIANTS OF: ";
3809*9880d681SAndroid Build Coastguard Worker PatternsToMatch[i].getSrcPattern()->dump();
3810*9880d681SAndroid Build Coastguard Worker errs() << "\n");
3811*9880d681SAndroid Build Coastguard Worker
3812*9880d681SAndroid Build Coastguard Worker for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
3813*9880d681SAndroid Build Coastguard Worker TreePatternNode *Variant = Variants[v];
3814*9880d681SAndroid Build Coastguard Worker
3815*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << " VAR#" << v << ": ";
3816*9880d681SAndroid Build Coastguard Worker Variant->dump();
3817*9880d681SAndroid Build Coastguard Worker errs() << "\n");
3818*9880d681SAndroid Build Coastguard Worker
3819*9880d681SAndroid Build Coastguard Worker // Scan to see if an instruction or explicit pattern already matches this.
3820*9880d681SAndroid Build Coastguard Worker bool AlreadyExists = false;
3821*9880d681SAndroid Build Coastguard Worker for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
3822*9880d681SAndroid Build Coastguard Worker // Skip if the top level predicates do not match.
3823*9880d681SAndroid Build Coastguard Worker if (PatternsToMatch[i].getPredicates() !=
3824*9880d681SAndroid Build Coastguard Worker PatternsToMatch[p].getPredicates())
3825*9880d681SAndroid Build Coastguard Worker continue;
3826*9880d681SAndroid Build Coastguard Worker // Check to see if this variant already exists.
3827*9880d681SAndroid Build Coastguard Worker if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
3828*9880d681SAndroid Build Coastguard Worker DepVars)) {
3829*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n");
3830*9880d681SAndroid Build Coastguard Worker AlreadyExists = true;
3831*9880d681SAndroid Build Coastguard Worker break;
3832*9880d681SAndroid Build Coastguard Worker }
3833*9880d681SAndroid Build Coastguard Worker }
3834*9880d681SAndroid Build Coastguard Worker // If we already have it, ignore the variant.
3835*9880d681SAndroid Build Coastguard Worker if (AlreadyExists) continue;
3836*9880d681SAndroid Build Coastguard Worker
3837*9880d681SAndroid Build Coastguard Worker // Otherwise, add it to the list of patterns we have.
3838*9880d681SAndroid Build Coastguard Worker PatternsToMatch.emplace_back(
3839*9880d681SAndroid Build Coastguard Worker PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(),
3840*9880d681SAndroid Build Coastguard Worker Variant, PatternsToMatch[i].getDstPattern(),
3841*9880d681SAndroid Build Coastguard Worker PatternsToMatch[i].getDstRegs(),
3842*9880d681SAndroid Build Coastguard Worker PatternsToMatch[i].getAddedComplexity(), Record::getNewUID());
3843*9880d681SAndroid Build Coastguard Worker }
3844*9880d681SAndroid Build Coastguard Worker
3845*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "\n");
3846*9880d681SAndroid Build Coastguard Worker }
3847*9880d681SAndroid Build Coastguard Worker }
3848