1f22ef01cSRoman Divacky //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This file implements the CodeGenDAGPatterns class, which is used to read and 11f22ef01cSRoman Divacky // represent the patterns present in a .td file for instructions. 12f22ef01cSRoman Divacky // 13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 14f22ef01cSRoman Divacky 15f22ef01cSRoman Divacky #include "CodeGenDAGPatterns.h" 16f22ef01cSRoman Divacky #include "llvm/ADT/STLExtras.h" 17139f7f9bSDimitry Andric #include "llvm/ADT/StringExtras.h" 18cb4dff85SDimitry Andric #include "llvm/ADT/Twine.h" 19f22ef01cSRoman Divacky #include "llvm/Support/Debug.h" 20dff0c46cSDimitry Andric #include "llvm/Support/ErrorHandling.h" 21139f7f9bSDimitry Andric #include "llvm/TableGen/Error.h" 22139f7f9bSDimitry Andric #include "llvm/TableGen/Record.h" 23f22ef01cSRoman Divacky #include <algorithm> 24dff0c46cSDimitry Andric #include <cstdio> 25dff0c46cSDimitry Andric #include <set> 26f22ef01cSRoman Divacky using namespace llvm; 27f22ef01cSRoman Divacky 2891bc56edSDimitry Andric #define DEBUG_TYPE "dag-patterns" 2991bc56edSDimitry Andric 30f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 31f22ef01cSRoman Divacky // EEVT::TypeSet Implementation 32f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 33f22ef01cSRoman Divacky 34f22ef01cSRoman Divacky static inline bool isInteger(MVT::SimpleValueType VT) { 35f785676fSDimitry Andric return MVT(VT).isInteger(); 36f22ef01cSRoman Divacky } 37f22ef01cSRoman Divacky static inline bool isFloatingPoint(MVT::SimpleValueType VT) { 38f785676fSDimitry Andric return MVT(VT).isFloatingPoint(); 39f22ef01cSRoman Divacky } 40f22ef01cSRoman Divacky static inline bool isVector(MVT::SimpleValueType VT) { 41f785676fSDimitry Andric return MVT(VT).isVector(); 42f22ef01cSRoman Divacky } 43f22ef01cSRoman Divacky static inline bool isScalar(MVT::SimpleValueType VT) { 44f785676fSDimitry Andric return !MVT(VT).isVector(); 45f22ef01cSRoman Divacky } 46f22ef01cSRoman Divacky 47f22ef01cSRoman Divacky EEVT::TypeSet::TypeSet(MVT::SimpleValueType VT, TreePattern &TP) { 48f22ef01cSRoman Divacky if (VT == MVT::iAny) 49f22ef01cSRoman Divacky EnforceInteger(TP); 50f22ef01cSRoman Divacky else if (VT == MVT::fAny) 51f22ef01cSRoman Divacky EnforceFloatingPoint(TP); 52f22ef01cSRoman Divacky else if (VT == MVT::vAny) 53f22ef01cSRoman Divacky EnforceVector(TP); 54f22ef01cSRoman Divacky else { 55f22ef01cSRoman Divacky assert((VT < MVT::LAST_VALUETYPE || VT == MVT::iPTR || 56f22ef01cSRoman Divacky VT == MVT::iPTRAny) && "Not a concrete type!"); 57f22ef01cSRoman Divacky TypeVec.push_back(VT); 58f22ef01cSRoman Divacky } 59f22ef01cSRoman Divacky } 60f22ef01cSRoman Divacky 61f22ef01cSRoman Divacky 62139f7f9bSDimitry Andric EEVT::TypeSet::TypeSet(ArrayRef<MVT::SimpleValueType> VTList) { 63f22ef01cSRoman Divacky assert(!VTList.empty() && "empty list?"); 64f22ef01cSRoman Divacky TypeVec.append(VTList.begin(), VTList.end()); 65f22ef01cSRoman Divacky 66f22ef01cSRoman Divacky if (!VTList.empty()) 67f22ef01cSRoman Divacky assert(VTList[0] != MVT::iAny && VTList[0] != MVT::vAny && 68f22ef01cSRoman Divacky VTList[0] != MVT::fAny); 69f22ef01cSRoman Divacky 70f22ef01cSRoman Divacky // Verify no duplicates. 71f22ef01cSRoman Divacky array_pod_sort(TypeVec.begin(), TypeVec.end()); 72f22ef01cSRoman Divacky assert(std::unique(TypeVec.begin(), TypeVec.end()) == TypeVec.end()); 73f22ef01cSRoman Divacky } 74f22ef01cSRoman Divacky 75f22ef01cSRoman Divacky /// FillWithPossibleTypes - Set to all legal types and return true, only valid 76f22ef01cSRoman Divacky /// on completely unknown type sets. 77f22ef01cSRoman Divacky bool EEVT::TypeSet::FillWithPossibleTypes(TreePattern &TP, 78f22ef01cSRoman Divacky bool (*Pred)(MVT::SimpleValueType), 79f22ef01cSRoman Divacky const char *PredicateName) { 80f22ef01cSRoman Divacky assert(isCompletelyUnknown()); 81139f7f9bSDimitry Andric ArrayRef<MVT::SimpleValueType> LegalTypes = 82f22ef01cSRoman Divacky TP.getDAGPatterns().getTargetInfo().getLegalValueTypes(); 83f22ef01cSRoman Divacky 843861d79fSDimitry Andric if (TP.hasError()) 853861d79fSDimitry Andric return false; 863861d79fSDimitry Andric 87f22ef01cSRoman Divacky for (unsigned i = 0, e = LegalTypes.size(); i != e; ++i) 8891bc56edSDimitry Andric if (!Pred || Pred(LegalTypes[i])) 89f22ef01cSRoman Divacky TypeVec.push_back(LegalTypes[i]); 90f22ef01cSRoman Divacky 91f22ef01cSRoman Divacky // If we have nothing that matches the predicate, bail out. 923861d79fSDimitry Andric if (TypeVec.empty()) { 93f22ef01cSRoman Divacky TP.error("Type inference contradiction found, no " + 94f22ef01cSRoman Divacky std::string(PredicateName) + " types found"); 953861d79fSDimitry Andric return false; 963861d79fSDimitry Andric } 97f22ef01cSRoman Divacky // No need to sort with one element. 98f22ef01cSRoman Divacky if (TypeVec.size() == 1) return true; 99f22ef01cSRoman Divacky 100f22ef01cSRoman Divacky // Remove duplicates. 101f22ef01cSRoman Divacky array_pod_sort(TypeVec.begin(), TypeVec.end()); 102f22ef01cSRoman Divacky TypeVec.erase(std::unique(TypeVec.begin(), TypeVec.end()), TypeVec.end()); 103f22ef01cSRoman Divacky 104f22ef01cSRoman Divacky return true; 105f22ef01cSRoman Divacky } 106f22ef01cSRoman Divacky 107f22ef01cSRoman Divacky /// hasIntegerTypes - Return true if this TypeSet contains iAny or an 108f22ef01cSRoman Divacky /// integer value type. 109f22ef01cSRoman Divacky bool EEVT::TypeSet::hasIntegerTypes() const { 110f22ef01cSRoman Divacky for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 111f22ef01cSRoman Divacky if (isInteger(TypeVec[i])) 112f22ef01cSRoman Divacky return true; 113f22ef01cSRoman Divacky return false; 114f22ef01cSRoman Divacky } 115f22ef01cSRoman Divacky 116f22ef01cSRoman Divacky /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or 117f22ef01cSRoman Divacky /// a floating point value type. 118f22ef01cSRoman Divacky bool EEVT::TypeSet::hasFloatingPointTypes() const { 119f22ef01cSRoman Divacky for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 120f22ef01cSRoman Divacky if (isFloatingPoint(TypeVec[i])) 121f22ef01cSRoman Divacky return true; 122f22ef01cSRoman Divacky return false; 123f22ef01cSRoman Divacky } 124f22ef01cSRoman Divacky 12591bc56edSDimitry Andric /// hasScalarTypes - Return true if this TypeSet contains a scalar value type. 12691bc56edSDimitry Andric bool EEVT::TypeSet::hasScalarTypes() const { 12791bc56edSDimitry Andric for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 12891bc56edSDimitry Andric if (isScalar(TypeVec[i])) 12991bc56edSDimitry Andric return true; 13091bc56edSDimitry Andric return false; 13191bc56edSDimitry Andric } 13291bc56edSDimitry Andric 133f22ef01cSRoman Divacky /// hasVectorTypes - Return true if this TypeSet contains a vAny or a vector 134f22ef01cSRoman Divacky /// value type. 135f22ef01cSRoman Divacky bool EEVT::TypeSet::hasVectorTypes() const { 136f22ef01cSRoman Divacky for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) 137f22ef01cSRoman Divacky if (isVector(TypeVec[i])) 138f22ef01cSRoman Divacky return true; 139f22ef01cSRoman Divacky return false; 140f22ef01cSRoman Divacky } 141f22ef01cSRoman Divacky 142f22ef01cSRoman Divacky 143f22ef01cSRoman Divacky std::string EEVT::TypeSet::getName() const { 144f22ef01cSRoman Divacky if (TypeVec.empty()) return "<empty>"; 145f22ef01cSRoman Divacky 146f22ef01cSRoman Divacky std::string Result; 147f22ef01cSRoman Divacky 148f22ef01cSRoman Divacky for (unsigned i = 0, e = TypeVec.size(); i != e; ++i) { 149f22ef01cSRoman Divacky std::string VTName = llvm::getEnumName(TypeVec[i]); 150f22ef01cSRoman Divacky // Strip off MVT:: prefix if present. 151f22ef01cSRoman Divacky if (VTName.substr(0,5) == "MVT::") 152f22ef01cSRoman Divacky VTName = VTName.substr(5); 153f22ef01cSRoman Divacky if (i) Result += ':'; 154f22ef01cSRoman Divacky Result += VTName; 155f22ef01cSRoman Divacky } 156f22ef01cSRoman Divacky 157f22ef01cSRoman Divacky if (TypeVec.size() == 1) 158f22ef01cSRoman Divacky return Result; 159f22ef01cSRoman Divacky return "{" + Result + "}"; 160f22ef01cSRoman Divacky } 161f22ef01cSRoman Divacky 162f22ef01cSRoman Divacky /// MergeInTypeInfo - This merges in type information from the specified 163f22ef01cSRoman Divacky /// argument. If 'this' changes, it returns true. If the two types are 1643861d79fSDimitry Andric /// contradictory (e.g. merge f32 into i32) then this flags an error. 165f22ef01cSRoman Divacky bool EEVT::TypeSet::MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP){ 1663861d79fSDimitry Andric if (InVT.isCompletelyUnknown() || *this == InVT || TP.hasError()) 167f22ef01cSRoman Divacky return false; 168f22ef01cSRoman Divacky 169f22ef01cSRoman Divacky if (isCompletelyUnknown()) { 170f22ef01cSRoman Divacky *this = InVT; 171f22ef01cSRoman Divacky return true; 172f22ef01cSRoman Divacky } 173f22ef01cSRoman Divacky 174f22ef01cSRoman Divacky assert(TypeVec.size() >= 1 && InVT.TypeVec.size() >= 1 && "No unknowns"); 175f22ef01cSRoman Divacky 176f22ef01cSRoman Divacky // Handle the abstract cases, seeing if we can resolve them better. 177f22ef01cSRoman Divacky switch (TypeVec[0]) { 178f22ef01cSRoman Divacky default: break; 179f22ef01cSRoman Divacky case MVT::iPTR: 180f22ef01cSRoman Divacky case MVT::iPTRAny: 181f22ef01cSRoman Divacky if (InVT.hasIntegerTypes()) { 182f22ef01cSRoman Divacky EEVT::TypeSet InCopy(InVT); 183f22ef01cSRoman Divacky InCopy.EnforceInteger(TP); 184f22ef01cSRoman Divacky InCopy.EnforceScalar(TP); 185f22ef01cSRoman Divacky 186f22ef01cSRoman Divacky if (InCopy.isConcrete()) { 187f22ef01cSRoman Divacky // If the RHS has one integer type, upgrade iPTR to i32. 188f22ef01cSRoman Divacky TypeVec[0] = InVT.TypeVec[0]; 189f22ef01cSRoman Divacky return true; 190f22ef01cSRoman Divacky } 191f22ef01cSRoman Divacky 192f22ef01cSRoman Divacky // If the input has multiple scalar integers, this doesn't add any info. 193f22ef01cSRoman Divacky if (!InCopy.isCompletelyUnknown()) 194f22ef01cSRoman Divacky return false; 195f22ef01cSRoman Divacky } 196f22ef01cSRoman Divacky break; 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 199f22ef01cSRoman Divacky // If the input constraint is iAny/iPTR and this is an integer type list, 200f22ef01cSRoman Divacky // remove non-integer types from the list. 201f22ef01cSRoman Divacky if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && 202f22ef01cSRoman Divacky hasIntegerTypes()) { 203f22ef01cSRoman Divacky bool MadeChange = EnforceInteger(TP); 204f22ef01cSRoman Divacky 205f22ef01cSRoman Divacky // If we're merging in iPTR/iPTRAny and the node currently has a list of 206f22ef01cSRoman Divacky // multiple different integer types, replace them with a single iPTR. 207f22ef01cSRoman Divacky if ((InVT.TypeVec[0] == MVT::iPTR || InVT.TypeVec[0] == MVT::iPTRAny) && 208f22ef01cSRoman Divacky TypeVec.size() != 1) { 209f22ef01cSRoman Divacky TypeVec.resize(1); 210f22ef01cSRoman Divacky TypeVec[0] = InVT.TypeVec[0]; 211f22ef01cSRoman Divacky MadeChange = true; 212f22ef01cSRoman Divacky } 213f22ef01cSRoman Divacky 214f22ef01cSRoman Divacky return MadeChange; 215f22ef01cSRoman Divacky } 216f22ef01cSRoman Divacky 217f22ef01cSRoman Divacky // If this is a type list and the RHS is a typelist as well, eliminate entries 218f22ef01cSRoman Divacky // from this list that aren't in the other one. 219f22ef01cSRoman Divacky bool MadeChange = false; 220f22ef01cSRoman Divacky TypeSet InputSet(*this); 221f22ef01cSRoman Divacky 222f22ef01cSRoman Divacky for (unsigned i = 0; i != TypeVec.size(); ++i) { 223f22ef01cSRoman Divacky bool InInVT = false; 224f22ef01cSRoman Divacky for (unsigned j = 0, e = InVT.TypeVec.size(); j != e; ++j) 225f22ef01cSRoman Divacky if (TypeVec[i] == InVT.TypeVec[j]) { 226f22ef01cSRoman Divacky InInVT = true; 227f22ef01cSRoman Divacky break; 228f22ef01cSRoman Divacky } 229f22ef01cSRoman Divacky 230f22ef01cSRoman Divacky if (InInVT) continue; 231f22ef01cSRoman Divacky TypeVec.erase(TypeVec.begin()+i--); 232f22ef01cSRoman Divacky MadeChange = true; 233f22ef01cSRoman Divacky } 234f22ef01cSRoman Divacky 235f22ef01cSRoman Divacky // If we removed all of our types, we have a type contradiction. 236f22ef01cSRoman Divacky if (!TypeVec.empty()) 237f22ef01cSRoman Divacky return MadeChange; 238f22ef01cSRoman Divacky 239f22ef01cSRoman Divacky // FIXME: Really want an SMLoc here! 240f22ef01cSRoman Divacky TP.error("Type inference contradiction found, merging '" + 241f22ef01cSRoman Divacky InVT.getName() + "' into '" + InputSet.getName() + "'"); 2423861d79fSDimitry Andric return false; 243f22ef01cSRoman Divacky } 244f22ef01cSRoman Divacky 245f22ef01cSRoman Divacky /// EnforceInteger - Remove all non-integer types from this set. 246f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceInteger(TreePattern &TP) { 2473861d79fSDimitry Andric if (TP.hasError()) 2483861d79fSDimitry Andric return false; 249f22ef01cSRoman Divacky // If we know nothing, then get the full set. 250f22ef01cSRoman Divacky if (TypeVec.empty()) 251f22ef01cSRoman Divacky return FillWithPossibleTypes(TP, isInteger, "integer"); 252f22ef01cSRoman Divacky if (!hasFloatingPointTypes()) 253f22ef01cSRoman Divacky return false; 254f22ef01cSRoman Divacky 255f22ef01cSRoman Divacky TypeSet InputSet(*this); 256f22ef01cSRoman Divacky 257f22ef01cSRoman Divacky // Filter out all the fp types. 258f22ef01cSRoman Divacky for (unsigned i = 0; i != TypeVec.size(); ++i) 259f22ef01cSRoman Divacky if (!isInteger(TypeVec[i])) 260f22ef01cSRoman Divacky TypeVec.erase(TypeVec.begin()+i--); 261f22ef01cSRoman Divacky 2623861d79fSDimitry Andric if (TypeVec.empty()) { 263f22ef01cSRoman Divacky TP.error("Type inference contradiction found, '" + 264f22ef01cSRoman Divacky InputSet.getName() + "' needs to be integer"); 2653861d79fSDimitry Andric return false; 2663861d79fSDimitry Andric } 267f22ef01cSRoman Divacky return true; 268f22ef01cSRoman Divacky } 269f22ef01cSRoman Divacky 270f22ef01cSRoman Divacky /// EnforceFloatingPoint - Remove all integer types from this set. 271f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceFloatingPoint(TreePattern &TP) { 2723861d79fSDimitry Andric if (TP.hasError()) 2733861d79fSDimitry Andric return false; 274f22ef01cSRoman Divacky // If we know nothing, then get the full set. 275f22ef01cSRoman Divacky if (TypeVec.empty()) 276f22ef01cSRoman Divacky return FillWithPossibleTypes(TP, isFloatingPoint, "floating point"); 277f22ef01cSRoman Divacky 278f22ef01cSRoman Divacky if (!hasIntegerTypes()) 279f22ef01cSRoman Divacky return false; 280f22ef01cSRoman Divacky 281f22ef01cSRoman Divacky TypeSet InputSet(*this); 282f22ef01cSRoman Divacky 283f22ef01cSRoman Divacky // Filter out all the fp types. 284f22ef01cSRoman Divacky for (unsigned i = 0; i != TypeVec.size(); ++i) 285f22ef01cSRoman Divacky if (!isFloatingPoint(TypeVec[i])) 286f22ef01cSRoman Divacky TypeVec.erase(TypeVec.begin()+i--); 287f22ef01cSRoman Divacky 2883861d79fSDimitry Andric if (TypeVec.empty()) { 289f22ef01cSRoman Divacky TP.error("Type inference contradiction found, '" + 290f22ef01cSRoman Divacky InputSet.getName() + "' needs to be floating point"); 2913861d79fSDimitry Andric return false; 2923861d79fSDimitry Andric } 293f22ef01cSRoman Divacky return true; 294f22ef01cSRoman Divacky } 295f22ef01cSRoman Divacky 296f22ef01cSRoman Divacky /// EnforceScalar - Remove all vector types from this. 297f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceScalar(TreePattern &TP) { 2983861d79fSDimitry Andric if (TP.hasError()) 2993861d79fSDimitry Andric return false; 3003861d79fSDimitry Andric 301f22ef01cSRoman Divacky // If we know nothing, then get the full set. 302f22ef01cSRoman Divacky if (TypeVec.empty()) 303f22ef01cSRoman Divacky return FillWithPossibleTypes(TP, isScalar, "scalar"); 304f22ef01cSRoman Divacky 305f22ef01cSRoman Divacky if (!hasVectorTypes()) 306f22ef01cSRoman Divacky return false; 307f22ef01cSRoman Divacky 308f22ef01cSRoman Divacky TypeSet InputSet(*this); 309f22ef01cSRoman Divacky 310f22ef01cSRoman Divacky // Filter out all the vector types. 311f22ef01cSRoman Divacky for (unsigned i = 0; i != TypeVec.size(); ++i) 312f22ef01cSRoman Divacky if (!isScalar(TypeVec[i])) 313f22ef01cSRoman Divacky TypeVec.erase(TypeVec.begin()+i--); 314f22ef01cSRoman Divacky 3153861d79fSDimitry Andric if (TypeVec.empty()) { 316f22ef01cSRoman Divacky TP.error("Type inference contradiction found, '" + 317f22ef01cSRoman Divacky InputSet.getName() + "' needs to be scalar"); 3183861d79fSDimitry Andric return false; 3193861d79fSDimitry Andric } 320f22ef01cSRoman Divacky return true; 321f22ef01cSRoman Divacky } 322f22ef01cSRoman Divacky 323f22ef01cSRoman Divacky /// EnforceVector - Remove all vector types from this. 324f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceVector(TreePattern &TP) { 3253861d79fSDimitry Andric if (TP.hasError()) 3263861d79fSDimitry Andric return false; 3273861d79fSDimitry Andric 328f22ef01cSRoman Divacky // If we know nothing, then get the full set. 329f22ef01cSRoman Divacky if (TypeVec.empty()) 330f22ef01cSRoman Divacky return FillWithPossibleTypes(TP, isVector, "vector"); 331f22ef01cSRoman Divacky 332f22ef01cSRoman Divacky TypeSet InputSet(*this); 333f22ef01cSRoman Divacky bool MadeChange = false; 334f22ef01cSRoman Divacky 335f22ef01cSRoman Divacky // Filter out all the scalar types. 336f22ef01cSRoman Divacky for (unsigned i = 0; i != TypeVec.size(); ++i) 337f22ef01cSRoman Divacky if (!isVector(TypeVec[i])) { 338f22ef01cSRoman Divacky TypeVec.erase(TypeVec.begin()+i--); 339f22ef01cSRoman Divacky MadeChange = true; 340f22ef01cSRoman Divacky } 341f22ef01cSRoman Divacky 3423861d79fSDimitry Andric if (TypeVec.empty()) { 343f22ef01cSRoman Divacky TP.error("Type inference contradiction found, '" + 344f22ef01cSRoman Divacky InputSet.getName() + "' needs to be a vector"); 3453861d79fSDimitry Andric return false; 3463861d79fSDimitry Andric } 347f22ef01cSRoman Divacky return MadeChange; 348f22ef01cSRoman Divacky } 349f22ef01cSRoman Divacky 350f22ef01cSRoman Divacky 351f22ef01cSRoman Divacky 35291bc56edSDimitry Andric /// EnforceSmallerThan - 'this' must be a smaller VT than Other. For vectors 35391bc56edSDimitry Andric /// this shoud be based on the element type. Update this and other based on 35491bc56edSDimitry Andric /// this information. 355f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP) { 3563861d79fSDimitry Andric if (TP.hasError()) 3573861d79fSDimitry Andric return false; 3583861d79fSDimitry Andric 359f22ef01cSRoman Divacky // Both operands must be integer or FP, but we don't care which. 360f22ef01cSRoman Divacky bool MadeChange = false; 361f22ef01cSRoman Divacky 362f22ef01cSRoman Divacky if (isCompletelyUnknown()) 363f22ef01cSRoman Divacky MadeChange = FillWithPossibleTypes(TP); 364f22ef01cSRoman Divacky 365f22ef01cSRoman Divacky if (Other.isCompletelyUnknown()) 366f22ef01cSRoman Divacky MadeChange = Other.FillWithPossibleTypes(TP); 367f22ef01cSRoman Divacky 368f22ef01cSRoman Divacky // If one side is known to be integer or known to be FP but the other side has 369f22ef01cSRoman Divacky // no information, get at least the type integrality info in there. 370f22ef01cSRoman Divacky if (!hasFloatingPointTypes()) 371f22ef01cSRoman Divacky MadeChange |= Other.EnforceInteger(TP); 372f22ef01cSRoman Divacky else if (!hasIntegerTypes()) 373f22ef01cSRoman Divacky MadeChange |= Other.EnforceFloatingPoint(TP); 374f22ef01cSRoman Divacky if (!Other.hasFloatingPointTypes()) 375f22ef01cSRoman Divacky MadeChange |= EnforceInteger(TP); 376f22ef01cSRoman Divacky else if (!Other.hasIntegerTypes()) 377f22ef01cSRoman Divacky MadeChange |= EnforceFloatingPoint(TP); 378f22ef01cSRoman Divacky 379f22ef01cSRoman Divacky assert(!isCompletelyUnknown() && !Other.isCompletelyUnknown() && 380f22ef01cSRoman Divacky "Should have a type list now"); 381f22ef01cSRoman Divacky 382f22ef01cSRoman Divacky // If one contains vectors but the other doesn't pull vectors out. 383f22ef01cSRoman Divacky if (!hasVectorTypes()) 384f22ef01cSRoman Divacky MadeChange |= Other.EnforceScalar(TP); 38591bc56edSDimitry Andric else if (!hasScalarTypes()) 38691bc56edSDimitry Andric MadeChange |= Other.EnforceVector(TP); 38791bc56edSDimitry Andric if (!Other.hasVectorTypes()) 388f22ef01cSRoman Divacky MadeChange |= EnforceScalar(TP); 38991bc56edSDimitry Andric else if (!Other.hasScalarTypes()) 39091bc56edSDimitry Andric MadeChange |= EnforceVector(TP); 391f22ef01cSRoman Divacky 39291bc56edSDimitry Andric // For vectors we need to ensure that smaller size doesn't produce larger 39391bc56edSDimitry Andric // vector and vice versa. 39491bc56edSDimitry Andric if (isConcrete() && isVector(getConcrete())) { 39591bc56edSDimitry Andric MVT IVT = getConcrete(); 39691bc56edSDimitry Andric unsigned Size = IVT.getSizeInBits(); 39791bc56edSDimitry Andric 39891bc56edSDimitry Andric // Only keep types that have at least as many bits. 39991bc56edSDimitry Andric TypeSet InputSet(Other); 40091bc56edSDimitry Andric 40191bc56edSDimitry Andric for (unsigned i = 0; i != Other.TypeVec.size(); ++i) { 40291bc56edSDimitry Andric assert(isVector(Other.TypeVec[i]) && "EnforceVector didn't work"); 40391bc56edSDimitry Andric if (MVT(Other.TypeVec[i]).getSizeInBits() < Size) { 40491bc56edSDimitry Andric Other.TypeVec.erase(Other.TypeVec.begin()+i--); 40591bc56edSDimitry Andric MadeChange = true; 40691bc56edSDimitry Andric } 40791bc56edSDimitry Andric } 40891bc56edSDimitry Andric 40991bc56edSDimitry Andric if (Other.TypeVec.empty()) { // FIXME: Really want an SMLoc here! 41091bc56edSDimitry Andric TP.error("Type inference contradiction found, forcing '" + 41191bc56edSDimitry Andric InputSet.getName() + "' to have at least as many bits as " + 41291bc56edSDimitry Andric getName() + "'"); 41391bc56edSDimitry Andric return false; 41491bc56edSDimitry Andric } 41591bc56edSDimitry Andric } else if (Other.isConcrete() && isVector(Other.getConcrete())) { 41691bc56edSDimitry Andric MVT IVT = Other.getConcrete(); 41791bc56edSDimitry Andric unsigned Size = IVT.getSizeInBits(); 41891bc56edSDimitry Andric 41991bc56edSDimitry Andric // Only keep types with the same or fewer total bits 42091bc56edSDimitry Andric TypeSet InputSet(*this); 42191bc56edSDimitry Andric 42291bc56edSDimitry Andric for (unsigned i = 0; i != TypeVec.size(); ++i) { 42391bc56edSDimitry Andric assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 42491bc56edSDimitry Andric if (MVT(TypeVec[i]).getSizeInBits() > Size) { 42591bc56edSDimitry Andric TypeVec.erase(TypeVec.begin()+i--); 42691bc56edSDimitry Andric MadeChange = true; 42791bc56edSDimitry Andric } 42891bc56edSDimitry Andric } 42991bc56edSDimitry Andric 43091bc56edSDimitry Andric if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 43191bc56edSDimitry Andric TP.error("Type inference contradiction found, forcing '" + 43291bc56edSDimitry Andric InputSet.getName() + "' to have the same or fewer bits than " + 43391bc56edSDimitry Andric Other.getName() + "'"); 43491bc56edSDimitry Andric return false; 43591bc56edSDimitry Andric } 43691bc56edSDimitry Andric } 43791bc56edSDimitry Andric 43891bc56edSDimitry Andric // This code does not currently handle nodes which have multiple types, 43991bc56edSDimitry Andric // where some types are integer, and some are fp. Assert that this is not 44091bc56edSDimitry Andric // the case. 441f22ef01cSRoman Divacky assert(!(hasIntegerTypes() && hasFloatingPointTypes()) && 442f22ef01cSRoman Divacky !(Other.hasIntegerTypes() && Other.hasFloatingPointTypes()) && 443f22ef01cSRoman Divacky "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); 444f22ef01cSRoman Divacky 44591bc56edSDimitry Andric if (TP.hasError()) 4463861d79fSDimitry Andric return false; 4472754fe60SDimitry Andric 44891bc56edSDimitry Andric // Okay, find the smallest scalar type from the other set and remove 44991bc56edSDimitry Andric // anything the same or smaller from the current set. 45091bc56edSDimitry Andric TypeSet InputSet(Other); 45191bc56edSDimitry Andric MVT::SimpleValueType Smallest = TypeVec[0]; 45291bc56edSDimitry Andric for (unsigned i = 0; i != Other.TypeVec.size(); ++i) { 45391bc56edSDimitry Andric if (Other.TypeVec[i] <= Smallest) { 45491bc56edSDimitry Andric Other.TypeVec.erase(Other.TypeVec.begin()+i--); 4552754fe60SDimitry Andric MadeChange = true; 4562754fe60SDimitry Andric } 4572754fe60SDimitry Andric } 458f22ef01cSRoman Divacky 45991bc56edSDimitry Andric if (Other.TypeVec.empty()) { 46091bc56edSDimitry Andric TP.error("Type inference contradiction found, '" + InputSet.getName() + 46191bc56edSDimitry Andric "' has nothing larger than '" + getName() +"'!"); 4623861d79fSDimitry Andric return false; 4633861d79fSDimitry Andric } 464f22ef01cSRoman Divacky 46591bc56edSDimitry Andric // Okay, find the largest scalar type from the other set and remove 46691bc56edSDimitry Andric // anything the same or larger from the current set. 46791bc56edSDimitry Andric InputSet = TypeSet(*this); 46891bc56edSDimitry Andric MVT::SimpleValueType Largest = Other.TypeVec[Other.TypeVec.size()-1]; 46991bc56edSDimitry Andric for (unsigned i = 0; i != TypeVec.size(); ++i) { 47091bc56edSDimitry Andric if (TypeVec[i] >= Largest) { 47191bc56edSDimitry Andric TypeVec.erase(TypeVec.begin()+i--); 4722754fe60SDimitry Andric MadeChange = true; 4732754fe60SDimitry Andric } 4742754fe60SDimitry Andric } 475f22ef01cSRoman Divacky 47691bc56edSDimitry Andric if (TypeVec.empty()) { 47791bc56edSDimitry Andric TP.error("Type inference contradiction found, '" + InputSet.getName() + 47891bc56edSDimitry Andric "' has nothing smaller than '" + Other.getName() +"'!"); 4793861d79fSDimitry Andric return false; 4803861d79fSDimitry Andric } 481f22ef01cSRoman Divacky 482f22ef01cSRoman Divacky return MadeChange; 483f22ef01cSRoman Divacky } 484f22ef01cSRoman Divacky 485f22ef01cSRoman Divacky /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type 486f22ef01cSRoman Divacky /// whose element is specified by VTOperand. 487f22ef01cSRoman Divacky bool EEVT::TypeSet::EnforceVectorEltTypeIs(EEVT::TypeSet &VTOperand, 488f22ef01cSRoman Divacky TreePattern &TP) { 4893861d79fSDimitry Andric if (TP.hasError()) 4903861d79fSDimitry Andric return false; 4913861d79fSDimitry Andric 492f22ef01cSRoman Divacky // "This" must be a vector and "VTOperand" must be a scalar. 493f22ef01cSRoman Divacky bool MadeChange = false; 494f22ef01cSRoman Divacky MadeChange |= EnforceVector(TP); 495f22ef01cSRoman Divacky MadeChange |= VTOperand.EnforceScalar(TP); 496f22ef01cSRoman Divacky 497f22ef01cSRoman Divacky // If we know the vector type, it forces the scalar to agree. 498f22ef01cSRoman Divacky if (isConcrete()) { 499f785676fSDimitry Andric MVT IVT = getConcrete(); 500f22ef01cSRoman Divacky IVT = IVT.getVectorElementType(); 501f22ef01cSRoman Divacky return MadeChange | 502f785676fSDimitry Andric VTOperand.MergeInTypeInfo(IVT.SimpleTy, TP); 503f22ef01cSRoman Divacky } 504f22ef01cSRoman Divacky 505f22ef01cSRoman Divacky // If the scalar type is known, filter out vector types whose element types 506f22ef01cSRoman Divacky // disagree. 507f22ef01cSRoman Divacky if (!VTOperand.isConcrete()) 508f22ef01cSRoman Divacky return MadeChange; 509f22ef01cSRoman Divacky 510f22ef01cSRoman Divacky MVT::SimpleValueType VT = VTOperand.getConcrete(); 511f22ef01cSRoman Divacky 512f22ef01cSRoman Divacky TypeSet InputSet(*this); 513f22ef01cSRoman Divacky 514f22ef01cSRoman Divacky // Filter out all the types which don't have the right element type. 515f22ef01cSRoman Divacky for (unsigned i = 0; i != TypeVec.size(); ++i) { 516f22ef01cSRoman Divacky assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 517f785676fSDimitry Andric if (MVT(TypeVec[i]).getVectorElementType().SimpleTy != VT) { 518f22ef01cSRoman Divacky TypeVec.erase(TypeVec.begin()+i--); 519f22ef01cSRoman Divacky MadeChange = true; 520f22ef01cSRoman Divacky } 521f22ef01cSRoman Divacky } 522f22ef01cSRoman Divacky 5233861d79fSDimitry Andric if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 524f22ef01cSRoman Divacky TP.error("Type inference contradiction found, forcing '" + 525f22ef01cSRoman Divacky InputSet.getName() + "' to have a vector element"); 5263861d79fSDimitry Andric return false; 5273861d79fSDimitry Andric } 528f22ef01cSRoman Divacky return MadeChange; 529f22ef01cSRoman Divacky } 530f22ef01cSRoman Divacky 5312754fe60SDimitry Andric /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to be a 5322754fe60SDimitry Andric /// vector type specified by VTOperand. 5332754fe60SDimitry Andric bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand, 5342754fe60SDimitry Andric TreePattern &TP) { 53591bc56edSDimitry Andric if (TP.hasError()) 53691bc56edSDimitry Andric return false; 53791bc56edSDimitry Andric 5382754fe60SDimitry Andric // "This" must be a vector and "VTOperand" must be a vector. 5392754fe60SDimitry Andric bool MadeChange = false; 5402754fe60SDimitry Andric MadeChange |= EnforceVector(TP); 5412754fe60SDimitry Andric MadeChange |= VTOperand.EnforceVector(TP); 5422754fe60SDimitry Andric 54391bc56edSDimitry Andric // If one side is known to be integer or known to be FP but the other side has 54491bc56edSDimitry Andric // no information, get at least the type integrality info in there. 54591bc56edSDimitry Andric if (!hasFloatingPointTypes()) 54691bc56edSDimitry Andric MadeChange |= VTOperand.EnforceInteger(TP); 54791bc56edSDimitry Andric else if (!hasIntegerTypes()) 54891bc56edSDimitry Andric MadeChange |= VTOperand.EnforceFloatingPoint(TP); 54991bc56edSDimitry Andric if (!VTOperand.hasFloatingPointTypes()) 55091bc56edSDimitry Andric MadeChange |= EnforceInteger(TP); 55191bc56edSDimitry Andric else if (!VTOperand.hasIntegerTypes()) 55291bc56edSDimitry Andric MadeChange |= EnforceFloatingPoint(TP); 55391bc56edSDimitry Andric 55491bc56edSDimitry Andric assert(!isCompletelyUnknown() && !VTOperand.isCompletelyUnknown() && 55591bc56edSDimitry Andric "Should have a type list now"); 5562754fe60SDimitry Andric 5572754fe60SDimitry Andric // If we know the vector type, it forces the scalar types to agree. 55891bc56edSDimitry Andric // Also force one vector to have more elements than the other. 5592754fe60SDimitry Andric if (isConcrete()) { 560f785676fSDimitry Andric MVT IVT = getConcrete(); 56191bc56edSDimitry Andric unsigned NumElems = IVT.getVectorNumElements(); 5622754fe60SDimitry Andric IVT = IVT.getVectorElementType(); 5632754fe60SDimitry Andric 564f785676fSDimitry Andric EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP); 5652754fe60SDimitry Andric MadeChange |= VTOperand.EnforceVectorEltTypeIs(EltTypeSet, TP); 56691bc56edSDimitry Andric 56791bc56edSDimitry Andric // Only keep types that have less elements than VTOperand. 56891bc56edSDimitry Andric TypeSet InputSet(VTOperand); 56991bc56edSDimitry Andric 57091bc56edSDimitry Andric for (unsigned i = 0; i != VTOperand.TypeVec.size(); ++i) { 57191bc56edSDimitry Andric assert(isVector(VTOperand.TypeVec[i]) && "EnforceVector didn't work"); 57291bc56edSDimitry Andric if (MVT(VTOperand.TypeVec[i]).getVectorNumElements() >= NumElems) { 57391bc56edSDimitry Andric VTOperand.TypeVec.erase(VTOperand.TypeVec.begin()+i--); 57491bc56edSDimitry Andric MadeChange = true; 57591bc56edSDimitry Andric } 57691bc56edSDimitry Andric } 57791bc56edSDimitry Andric if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here! 57891bc56edSDimitry Andric TP.error("Type inference contradiction found, forcing '" + 57991bc56edSDimitry Andric InputSet.getName() + "' to have less vector elements than '" + 58091bc56edSDimitry Andric getName() + "'"); 58191bc56edSDimitry Andric return false; 58291bc56edSDimitry Andric } 5832754fe60SDimitry Andric } else if (VTOperand.isConcrete()) { 584f785676fSDimitry Andric MVT IVT = VTOperand.getConcrete(); 58591bc56edSDimitry Andric unsigned NumElems = IVT.getVectorNumElements(); 5862754fe60SDimitry Andric IVT = IVT.getVectorElementType(); 5872754fe60SDimitry Andric 588f785676fSDimitry Andric EEVT::TypeSet EltTypeSet(IVT.SimpleTy, TP); 5892754fe60SDimitry Andric MadeChange |= EnforceVectorEltTypeIs(EltTypeSet, TP); 59091bc56edSDimitry Andric 59191bc56edSDimitry Andric // Only keep types that have more elements than 'this'. 59291bc56edSDimitry Andric TypeSet InputSet(*this); 59391bc56edSDimitry Andric 59491bc56edSDimitry Andric for (unsigned i = 0; i != TypeVec.size(); ++i) { 59591bc56edSDimitry Andric assert(isVector(TypeVec[i]) && "EnforceVector didn't work"); 59691bc56edSDimitry Andric if (MVT(TypeVec[i]).getVectorNumElements() <= NumElems) { 59791bc56edSDimitry Andric TypeVec.erase(TypeVec.begin()+i--); 59891bc56edSDimitry Andric MadeChange = true; 59991bc56edSDimitry Andric } 60091bc56edSDimitry Andric } 60191bc56edSDimitry Andric if (TypeVec.empty()) { // FIXME: Really want an SMLoc here! 60291bc56edSDimitry Andric TP.error("Type inference contradiction found, forcing '" + 60391bc56edSDimitry Andric InputSet.getName() + "' to have more vector elements than '" + 60491bc56edSDimitry Andric VTOperand.getName() + "'"); 60591bc56edSDimitry Andric return false; 60691bc56edSDimitry Andric } 6072754fe60SDimitry Andric } 6082754fe60SDimitry Andric 6092754fe60SDimitry Andric return MadeChange; 6102754fe60SDimitry Andric } 6112754fe60SDimitry Andric 612f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 613f22ef01cSRoman Divacky // Helpers for working with extended types. 614f22ef01cSRoman Divacky 615f22ef01cSRoman Divacky /// Dependent variable map for CodeGenDAGPattern variant generation 616f22ef01cSRoman Divacky typedef std::map<std::string, int> DepVarMap; 617f22ef01cSRoman Divacky 618f22ef01cSRoman Divacky /// Const iterator shorthand for DepVarMap 619f22ef01cSRoman Divacky typedef DepVarMap::const_iterator DepVarMap_citer; 620f22ef01cSRoman Divacky 6213b0f4066SDimitry Andric static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) { 622f22ef01cSRoman Divacky if (N->isLeaf()) { 6233861d79fSDimitry Andric if (isa<DefInit>(N->getLeafValue())) 624f22ef01cSRoman Divacky DepMap[N->getName()]++; 625f22ef01cSRoman Divacky } else { 626f22ef01cSRoman Divacky for (size_t i = 0, e = N->getNumChildren(); i != e; ++i) 627f22ef01cSRoman Divacky FindDepVarsOf(N->getChild(i), DepMap); 628f22ef01cSRoman Divacky } 629f22ef01cSRoman Divacky } 630f22ef01cSRoman Divacky 6313b0f4066SDimitry Andric /// Find dependent variables within child patterns 6323b0f4066SDimitry Andric static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) { 633f22ef01cSRoman Divacky DepVarMap depcounts; 634f22ef01cSRoman Divacky FindDepVarsOf(N, depcounts); 635f22ef01cSRoman Divacky for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) { 6363b0f4066SDimitry Andric if (i->second > 1) // std::pair<std::string, int> 637f22ef01cSRoman Divacky DepVars.insert(i->first); 638f22ef01cSRoman Divacky } 639f22ef01cSRoman Divacky } 640f22ef01cSRoman Divacky 6412754fe60SDimitry Andric #ifndef NDEBUG 6423b0f4066SDimitry Andric /// Dump the dependent variable set: 6433b0f4066SDimitry Andric static void DumpDepVars(MultipleUseVarSet &DepVars) { 644f22ef01cSRoman Divacky if (DepVars.empty()) { 645f22ef01cSRoman Divacky DEBUG(errs() << "<empty set>"); 646f22ef01cSRoman Divacky } else { 647f22ef01cSRoman Divacky DEBUG(errs() << "[ "); 6482754fe60SDimitry Andric for (MultipleUseVarSet::const_iterator i = DepVars.begin(), 6492754fe60SDimitry Andric e = DepVars.end(); i != e; ++i) { 650f22ef01cSRoman Divacky DEBUG(errs() << (*i) << " "); 651f22ef01cSRoman Divacky } 652f22ef01cSRoman Divacky DEBUG(errs() << "]"); 653f22ef01cSRoman Divacky } 654f22ef01cSRoman Divacky } 6552754fe60SDimitry Andric #endif 6562754fe60SDimitry Andric 6573b0f4066SDimitry Andric 6583b0f4066SDimitry Andric //===----------------------------------------------------------------------===// 6593b0f4066SDimitry Andric // TreePredicateFn Implementation 6603b0f4066SDimitry Andric //===----------------------------------------------------------------------===// 6613b0f4066SDimitry Andric 6623b0f4066SDimitry Andric /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag. 6633b0f4066SDimitry Andric TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) { 6643b0f4066SDimitry Andric assert((getPredCode().empty() || getImmCode().empty()) && 6653b0f4066SDimitry Andric ".td file corrupt: can't have a node predicate *and* an imm predicate"); 6663b0f4066SDimitry Andric } 6673b0f4066SDimitry Andric 6683b0f4066SDimitry Andric std::string TreePredicateFn::getPredCode() const { 669dff0c46cSDimitry Andric return PatFragRec->getRecord()->getValueAsString("PredicateCode"); 6703b0f4066SDimitry Andric } 6713b0f4066SDimitry Andric 6723b0f4066SDimitry Andric std::string TreePredicateFn::getImmCode() const { 673dff0c46cSDimitry Andric return PatFragRec->getRecord()->getValueAsString("ImmediateCode"); 6743b0f4066SDimitry Andric } 6753b0f4066SDimitry Andric 6763b0f4066SDimitry Andric 6773b0f4066SDimitry Andric /// isAlwaysTrue - Return true if this is a noop predicate. 6783b0f4066SDimitry Andric bool TreePredicateFn::isAlwaysTrue() const { 6793b0f4066SDimitry Andric return getPredCode().empty() && getImmCode().empty(); 6803b0f4066SDimitry Andric } 6813b0f4066SDimitry Andric 6823b0f4066SDimitry Andric /// Return the name to use in the generated code to reference this, this is 6833b0f4066SDimitry Andric /// "Predicate_foo" if from a pattern fragment "foo". 6843b0f4066SDimitry Andric std::string TreePredicateFn::getFnName() const { 6853b0f4066SDimitry Andric return "Predicate_" + PatFragRec->getRecord()->getName(); 6863b0f4066SDimitry Andric } 6873b0f4066SDimitry Andric 6883b0f4066SDimitry Andric /// getCodeToRunOnSDNode - Return the code for the function body that 6893b0f4066SDimitry Andric /// evaluates this predicate. The argument is expected to be in "Node", 6903b0f4066SDimitry Andric /// not N. This handles casting and conversion to a concrete node type as 6913b0f4066SDimitry Andric /// appropriate. 6923b0f4066SDimitry Andric std::string TreePredicateFn::getCodeToRunOnSDNode() const { 6933b0f4066SDimitry Andric // Handle immediate predicates first. 6943b0f4066SDimitry Andric std::string ImmCode = getImmCode(); 6953b0f4066SDimitry Andric if (!ImmCode.empty()) { 6963b0f4066SDimitry Andric std::string Result = 6973b0f4066SDimitry Andric " int64_t Imm = cast<ConstantSDNode>(Node)->getSExtValue();\n"; 6983b0f4066SDimitry Andric return Result + ImmCode; 6993b0f4066SDimitry Andric } 7003b0f4066SDimitry Andric 7013b0f4066SDimitry Andric // Handle arbitrary node predicates. 7023b0f4066SDimitry Andric assert(!getPredCode().empty() && "Don't have any predicate code!"); 7033b0f4066SDimitry Andric std::string ClassName; 7043b0f4066SDimitry Andric if (PatFragRec->getOnlyTree()->isLeaf()) 7053b0f4066SDimitry Andric ClassName = "SDNode"; 7063b0f4066SDimitry Andric else { 7073b0f4066SDimitry Andric Record *Op = PatFragRec->getOnlyTree()->getOperator(); 7083b0f4066SDimitry Andric ClassName = PatFragRec->getDAGPatterns().getSDNodeInfo(Op).getSDClassName(); 7093b0f4066SDimitry Andric } 7103b0f4066SDimitry Andric std::string Result; 7113b0f4066SDimitry Andric if (ClassName == "SDNode") 7123b0f4066SDimitry Andric Result = " SDNode *N = Node;\n"; 7133b0f4066SDimitry Andric else 7143b0f4066SDimitry Andric Result = " " + ClassName + "*N = cast<" + ClassName + ">(Node);\n"; 7153b0f4066SDimitry Andric 7163b0f4066SDimitry Andric return Result + getPredCode(); 717f22ef01cSRoman Divacky } 718f22ef01cSRoman Divacky 719f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 720f22ef01cSRoman Divacky // PatternToMatch implementation 721f22ef01cSRoman Divacky // 722f22ef01cSRoman Divacky 723f22ef01cSRoman Divacky 724f22ef01cSRoman Divacky /// getPatternSize - Return the 'size' of this pattern. We want to match large 725f22ef01cSRoman Divacky /// patterns before small ones. This is used to determine the size of a 726f22ef01cSRoman Divacky /// pattern. 727f22ef01cSRoman Divacky static unsigned getPatternSize(const TreePatternNode *P, 728f22ef01cSRoman Divacky const CodeGenDAGPatterns &CGP) { 729f22ef01cSRoman Divacky unsigned Size = 3; // The node itself. 730f22ef01cSRoman Divacky // If the root node is a ConstantSDNode, increases its size. 731f22ef01cSRoman Divacky // e.g. (set R32:$dst, 0). 7323861d79fSDimitry Andric if (P->isLeaf() && isa<IntInit>(P->getLeafValue())) 733f22ef01cSRoman Divacky Size += 2; 734f22ef01cSRoman Divacky 735f22ef01cSRoman Divacky // FIXME: This is a hack to statically increase the priority of patterns 736f22ef01cSRoman Divacky // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. 737f22ef01cSRoman Divacky // Later we can allow complexity / cost for each pattern to be (optionally) 738f22ef01cSRoman Divacky // specified. To get best possible pattern match we'll need to dynamically 739f22ef01cSRoman Divacky // calculate the complexity of all patterns a dag can potentially map to. 740f22ef01cSRoman Divacky const ComplexPattern *AM = P->getComplexPatternInfo(CGP); 74191bc56edSDimitry Andric if (AM) { 742f22ef01cSRoman Divacky Size += AM->getNumOperands() * 3; 743f22ef01cSRoman Divacky 74491bc56edSDimitry Andric // We don't want to count any children twice, so return early. 74591bc56edSDimitry Andric return Size; 74691bc56edSDimitry Andric } 74791bc56edSDimitry Andric 748f22ef01cSRoman Divacky // If this node has some predicate function that must match, it adds to the 749f22ef01cSRoman Divacky // complexity of this node. 750f22ef01cSRoman Divacky if (!P->getPredicateFns().empty()) 751f22ef01cSRoman Divacky ++Size; 752f22ef01cSRoman Divacky 753f22ef01cSRoman Divacky // Count children in the count if they are also nodes. 754f22ef01cSRoman Divacky for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { 755f22ef01cSRoman Divacky TreePatternNode *Child = P->getChild(i); 756f22ef01cSRoman Divacky if (!Child->isLeaf() && Child->getNumTypes() && 757f22ef01cSRoman Divacky Child->getType(0) != MVT::Other) 758f22ef01cSRoman Divacky Size += getPatternSize(Child, CGP); 759f22ef01cSRoman Divacky else if (Child->isLeaf()) { 7603861d79fSDimitry Andric if (isa<IntInit>(Child->getLeafValue())) 761f22ef01cSRoman Divacky Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2). 762f22ef01cSRoman Divacky else if (Child->getComplexPatternInfo(CGP)) 763f22ef01cSRoman Divacky Size += getPatternSize(Child, CGP); 764f22ef01cSRoman Divacky else if (!Child->getPredicateFns().empty()) 765f22ef01cSRoman Divacky ++Size; 766f22ef01cSRoman Divacky } 767f22ef01cSRoman Divacky } 768f22ef01cSRoman Divacky 769f22ef01cSRoman Divacky return Size; 770f22ef01cSRoman Divacky } 771f22ef01cSRoman Divacky 772f22ef01cSRoman Divacky /// Compute the complexity metric for the input pattern. This roughly 773f22ef01cSRoman Divacky /// corresponds to the number of nodes that are covered. 774f22ef01cSRoman Divacky unsigned PatternToMatch:: 775f22ef01cSRoman Divacky getPatternComplexity(const CodeGenDAGPatterns &CGP) const { 776f22ef01cSRoman Divacky return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity(); 777f22ef01cSRoman Divacky } 778f22ef01cSRoman Divacky 779f22ef01cSRoman Divacky 780f22ef01cSRoman Divacky /// getPredicateCheck - Return a single string containing all of this 781f22ef01cSRoman Divacky /// pattern's predicates concatenated with "&&" operators. 782f22ef01cSRoman Divacky /// 783f22ef01cSRoman Divacky std::string PatternToMatch::getPredicateCheck() const { 784f22ef01cSRoman Divacky std::string PredicateCheck; 785f22ef01cSRoman Divacky for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { 7863861d79fSDimitry Andric if (DefInit *Pred = dyn_cast<DefInit>(Predicates->getElement(i))) { 787f22ef01cSRoman Divacky Record *Def = Pred->getDef(); 788f22ef01cSRoman Divacky if (!Def->isSubClassOf("Predicate")) { 789f22ef01cSRoman Divacky #ifndef NDEBUG 790f22ef01cSRoman Divacky Def->dump(); 791f22ef01cSRoman Divacky #endif 792dff0c46cSDimitry Andric llvm_unreachable("Unknown predicate type!"); 793f22ef01cSRoman Divacky } 794f22ef01cSRoman Divacky if (!PredicateCheck.empty()) 795f22ef01cSRoman Divacky PredicateCheck += " && "; 796f22ef01cSRoman Divacky PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; 797f22ef01cSRoman Divacky } 798f22ef01cSRoman Divacky } 799f22ef01cSRoman Divacky 800f22ef01cSRoman Divacky return PredicateCheck; 801f22ef01cSRoman Divacky } 802f22ef01cSRoman Divacky 803f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 804f22ef01cSRoman Divacky // SDTypeConstraint implementation 805f22ef01cSRoman Divacky // 806f22ef01cSRoman Divacky 807f22ef01cSRoman Divacky SDTypeConstraint::SDTypeConstraint(Record *R) { 808f22ef01cSRoman Divacky OperandNo = R->getValueAsInt("OperandNum"); 809f22ef01cSRoman Divacky 810f22ef01cSRoman Divacky if (R->isSubClassOf("SDTCisVT")) { 811f22ef01cSRoman Divacky ConstraintType = SDTCisVT; 812f22ef01cSRoman Divacky x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); 813f22ef01cSRoman Divacky if (x.SDTCisVT_Info.VT == MVT::isVoid) 8143861d79fSDimitry Andric PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT"); 815f22ef01cSRoman Divacky 816f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisPtrTy")) { 817f22ef01cSRoman Divacky ConstraintType = SDTCisPtrTy; 818f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisInt")) { 819f22ef01cSRoman Divacky ConstraintType = SDTCisInt; 820f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisFP")) { 821f22ef01cSRoman Divacky ConstraintType = SDTCisFP; 822f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisVec")) { 823f22ef01cSRoman Divacky ConstraintType = SDTCisVec; 824f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisSameAs")) { 825f22ef01cSRoman Divacky ConstraintType = SDTCisSameAs; 826f22ef01cSRoman Divacky x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); 827f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { 828f22ef01cSRoman Divacky ConstraintType = SDTCisVTSmallerThanOp; 829f22ef01cSRoman Divacky x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = 830f22ef01cSRoman Divacky R->getValueAsInt("OtherOperandNum"); 831f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { 832f22ef01cSRoman Divacky ConstraintType = SDTCisOpSmallerThanOp; 833f22ef01cSRoman Divacky x.SDTCisOpSmallerThanOp_Info.BigOperandNum = 834f22ef01cSRoman Divacky R->getValueAsInt("BigOperandNum"); 835f22ef01cSRoman Divacky } else if (R->isSubClassOf("SDTCisEltOfVec")) { 836f22ef01cSRoman Divacky ConstraintType = SDTCisEltOfVec; 837f22ef01cSRoman Divacky x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum"); 8382754fe60SDimitry Andric } else if (R->isSubClassOf("SDTCisSubVecOfVec")) { 8392754fe60SDimitry Andric ConstraintType = SDTCisSubVecOfVec; 8402754fe60SDimitry Andric x.SDTCisSubVecOfVec_Info.OtherOperandNum = 8412754fe60SDimitry Andric R->getValueAsInt("OtherOpNum"); 842f22ef01cSRoman Divacky } else { 843f22ef01cSRoman Divacky errs() << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; 844f22ef01cSRoman Divacky exit(1); 845f22ef01cSRoman Divacky } 846f22ef01cSRoman Divacky } 847f22ef01cSRoman Divacky 848f22ef01cSRoman Divacky /// getOperandNum - Return the node corresponding to operand #OpNo in tree 849f22ef01cSRoman Divacky /// N, and the result number in ResNo. 850f22ef01cSRoman Divacky static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N, 851f22ef01cSRoman Divacky const SDNodeInfo &NodeInfo, 852f22ef01cSRoman Divacky unsigned &ResNo) { 853f22ef01cSRoman Divacky unsigned NumResults = NodeInfo.getNumResults(); 854f22ef01cSRoman Divacky if (OpNo < NumResults) { 855f22ef01cSRoman Divacky ResNo = OpNo; 856f22ef01cSRoman Divacky return N; 857f22ef01cSRoman Divacky } 858f22ef01cSRoman Divacky 859f22ef01cSRoman Divacky OpNo -= NumResults; 860f22ef01cSRoman Divacky 861f22ef01cSRoman Divacky if (OpNo >= N->getNumChildren()) { 862f22ef01cSRoman Divacky errs() << "Invalid operand number in type constraint " 863f22ef01cSRoman Divacky << (OpNo+NumResults) << " "; 864f22ef01cSRoman Divacky N->dump(); 865f22ef01cSRoman Divacky errs() << '\n'; 866f22ef01cSRoman Divacky exit(1); 867f22ef01cSRoman Divacky } 868f22ef01cSRoman Divacky 869f22ef01cSRoman Divacky return N->getChild(OpNo); 870f22ef01cSRoman Divacky } 871f22ef01cSRoman Divacky 872f22ef01cSRoman Divacky /// ApplyTypeConstraint - Given a node in a pattern, apply this type 873f22ef01cSRoman Divacky /// constraint to the nodes operands. This returns true if it makes a 8743861d79fSDimitry Andric /// change, false otherwise. If a type contradiction is found, flag an error. 875f22ef01cSRoman Divacky bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, 876f22ef01cSRoman Divacky const SDNodeInfo &NodeInfo, 877f22ef01cSRoman Divacky TreePattern &TP) const { 8783861d79fSDimitry Andric if (TP.hasError()) 8793861d79fSDimitry Andric return false; 8803861d79fSDimitry Andric 881f22ef01cSRoman Divacky unsigned ResNo = 0; // The result number being referenced. 882f22ef01cSRoman Divacky TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo); 883f22ef01cSRoman Divacky 884f22ef01cSRoman Divacky switch (ConstraintType) { 885f22ef01cSRoman Divacky case SDTCisVT: 886f22ef01cSRoman Divacky // Operand must be a particular type. 887f22ef01cSRoman Divacky return NodeToApply->UpdateNodeType(ResNo, x.SDTCisVT_Info.VT, TP); 888f22ef01cSRoman Divacky case SDTCisPtrTy: 889f22ef01cSRoman Divacky // Operand must be same as target pointer type. 890f22ef01cSRoman Divacky return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP); 891f22ef01cSRoman Divacky case SDTCisInt: 892f22ef01cSRoman Divacky // Require it to be one of the legal integer VTs. 893f22ef01cSRoman Divacky return NodeToApply->getExtType(ResNo).EnforceInteger(TP); 894f22ef01cSRoman Divacky case SDTCisFP: 895f22ef01cSRoman Divacky // Require it to be one of the legal fp VTs. 896f22ef01cSRoman Divacky return NodeToApply->getExtType(ResNo).EnforceFloatingPoint(TP); 897f22ef01cSRoman Divacky case SDTCisVec: 898f22ef01cSRoman Divacky // Require it to be one of the legal vector VTs. 899f22ef01cSRoman Divacky return NodeToApply->getExtType(ResNo).EnforceVector(TP); 900f22ef01cSRoman Divacky case SDTCisSameAs: { 901f22ef01cSRoman Divacky unsigned OResNo = 0; 902f22ef01cSRoman Divacky TreePatternNode *OtherNode = 903f22ef01cSRoman Divacky getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo); 904f22ef01cSRoman Divacky return NodeToApply->UpdateNodeType(OResNo, OtherNode->getExtType(ResNo),TP)| 905f22ef01cSRoman Divacky OtherNode->UpdateNodeType(ResNo,NodeToApply->getExtType(OResNo),TP); 906f22ef01cSRoman Divacky } 907f22ef01cSRoman Divacky case SDTCisVTSmallerThanOp: { 908f22ef01cSRoman Divacky // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must 909f22ef01cSRoman Divacky // have an integer type that is smaller than the VT. 910f22ef01cSRoman Divacky if (!NodeToApply->isLeaf() || 9113861d79fSDimitry Andric !isa<DefInit>(NodeToApply->getLeafValue()) || 912f22ef01cSRoman Divacky !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() 9133861d79fSDimitry Andric ->isSubClassOf("ValueType")) { 914f22ef01cSRoman Divacky TP.error(N->getOperator()->getName() + " expects a VT operand!"); 9153861d79fSDimitry Andric return false; 9163861d79fSDimitry Andric } 917f22ef01cSRoman Divacky MVT::SimpleValueType VT = 918f22ef01cSRoman Divacky getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); 919f22ef01cSRoman Divacky 920f22ef01cSRoman Divacky EEVT::TypeSet TypeListTmp(VT, TP); 921f22ef01cSRoman Divacky 922f22ef01cSRoman Divacky unsigned OResNo = 0; 923f22ef01cSRoman Divacky TreePatternNode *OtherNode = 924f22ef01cSRoman Divacky getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo, 925f22ef01cSRoman Divacky OResNo); 926f22ef01cSRoman Divacky 927f22ef01cSRoman Divacky return TypeListTmp.EnforceSmallerThan(OtherNode->getExtType(OResNo), TP); 928f22ef01cSRoman Divacky } 929f22ef01cSRoman Divacky case SDTCisOpSmallerThanOp: { 930f22ef01cSRoman Divacky unsigned BResNo = 0; 931f22ef01cSRoman Divacky TreePatternNode *BigOperand = 932f22ef01cSRoman Divacky getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo, 933f22ef01cSRoman Divacky BResNo); 934f22ef01cSRoman Divacky return NodeToApply->getExtType(ResNo). 935f22ef01cSRoman Divacky EnforceSmallerThan(BigOperand->getExtType(BResNo), TP); 936f22ef01cSRoman Divacky } 937f22ef01cSRoman Divacky case SDTCisEltOfVec: { 938f22ef01cSRoman Divacky unsigned VResNo = 0; 939f22ef01cSRoman Divacky TreePatternNode *VecOperand = 940f22ef01cSRoman Divacky getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo, 941f22ef01cSRoman Divacky VResNo); 942f22ef01cSRoman Divacky 943f22ef01cSRoman Divacky // Filter vector types out of VecOperand that don't have the right element 944f22ef01cSRoman Divacky // type. 945f22ef01cSRoman Divacky return VecOperand->getExtType(VResNo). 946f22ef01cSRoman Divacky EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), TP); 947f22ef01cSRoman Divacky } 9482754fe60SDimitry Andric case SDTCisSubVecOfVec: { 9492754fe60SDimitry Andric unsigned VResNo = 0; 9502754fe60SDimitry Andric TreePatternNode *BigVecOperand = 9512754fe60SDimitry Andric getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo, 9522754fe60SDimitry Andric VResNo); 9532754fe60SDimitry Andric 9542754fe60SDimitry Andric // Filter vector types out of BigVecOperand that don't have the 9552754fe60SDimitry Andric // right subvector type. 9562754fe60SDimitry Andric return BigVecOperand->getExtType(VResNo). 9572754fe60SDimitry Andric EnforceVectorSubVectorTypeIs(NodeToApply->getExtType(ResNo), TP); 9582754fe60SDimitry Andric } 959f22ef01cSRoman Divacky } 960dff0c46cSDimitry Andric llvm_unreachable("Invalid ConstraintType!"); 961f22ef01cSRoman Divacky } 962f22ef01cSRoman Divacky 963139f7f9bSDimitry Andric // Update the node type to match an instruction operand or result as specified 964139f7f9bSDimitry Andric // in the ins or outs lists on the instruction definition. Return true if the 965139f7f9bSDimitry Andric // type was actually changed. 966139f7f9bSDimitry Andric bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo, 967139f7f9bSDimitry Andric Record *Operand, 968139f7f9bSDimitry Andric TreePattern &TP) { 969139f7f9bSDimitry Andric // The 'unknown' operand indicates that types should be inferred from the 970139f7f9bSDimitry Andric // context. 971139f7f9bSDimitry Andric if (Operand->isSubClassOf("unknown_class")) 972139f7f9bSDimitry Andric return false; 973139f7f9bSDimitry Andric 974139f7f9bSDimitry Andric // The Operand class specifies a type directly. 975139f7f9bSDimitry Andric if (Operand->isSubClassOf("Operand")) 976139f7f9bSDimitry Andric return UpdateNodeType(ResNo, getValueType(Operand->getValueAsDef("Type")), 977139f7f9bSDimitry Andric TP); 978139f7f9bSDimitry Andric 979139f7f9bSDimitry Andric // PointerLikeRegClass has a type that is determined at runtime. 980139f7f9bSDimitry Andric if (Operand->isSubClassOf("PointerLikeRegClass")) 981139f7f9bSDimitry Andric return UpdateNodeType(ResNo, MVT::iPTR, TP); 982139f7f9bSDimitry Andric 983139f7f9bSDimitry Andric // Both RegisterClass and RegisterOperand operands derive their types from a 984139f7f9bSDimitry Andric // register class def. 98591bc56edSDimitry Andric Record *RC = nullptr; 986139f7f9bSDimitry Andric if (Operand->isSubClassOf("RegisterClass")) 987139f7f9bSDimitry Andric RC = Operand; 988139f7f9bSDimitry Andric else if (Operand->isSubClassOf("RegisterOperand")) 989139f7f9bSDimitry Andric RC = Operand->getValueAsDef("RegClass"); 990139f7f9bSDimitry Andric 991139f7f9bSDimitry Andric assert(RC && "Unknown operand type"); 992139f7f9bSDimitry Andric CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo(); 993139f7f9bSDimitry Andric return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP); 994139f7f9bSDimitry Andric } 995139f7f9bSDimitry Andric 996139f7f9bSDimitry Andric 997f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 998f22ef01cSRoman Divacky // SDNodeInfo implementation 999f22ef01cSRoman Divacky // 1000f22ef01cSRoman Divacky SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { 1001f22ef01cSRoman Divacky EnumName = R->getValueAsString("Opcode"); 1002f22ef01cSRoman Divacky SDClassName = R->getValueAsString("SDClass"); 1003f22ef01cSRoman Divacky Record *TypeProfile = R->getValueAsDef("TypeProfile"); 1004f22ef01cSRoman Divacky NumResults = TypeProfile->getValueAsInt("NumResults"); 1005f22ef01cSRoman Divacky NumOperands = TypeProfile->getValueAsInt("NumOperands"); 1006f22ef01cSRoman Divacky 1007f22ef01cSRoman Divacky // Parse the properties. 1008f22ef01cSRoman Divacky Properties = 0; 1009f22ef01cSRoman Divacky std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); 1010f22ef01cSRoman Divacky for (unsigned i = 0, e = PropList.size(); i != e; ++i) { 1011f22ef01cSRoman Divacky if (PropList[i]->getName() == "SDNPCommutative") { 1012f22ef01cSRoman Divacky Properties |= 1 << SDNPCommutative; 1013f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPAssociative") { 1014f22ef01cSRoman Divacky Properties |= 1 << SDNPAssociative; 1015f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPHasChain") { 1016f22ef01cSRoman Divacky Properties |= 1 << SDNPHasChain; 10172754fe60SDimitry Andric } else if (PropList[i]->getName() == "SDNPOutGlue") { 10182754fe60SDimitry Andric Properties |= 1 << SDNPOutGlue; 10192754fe60SDimitry Andric } else if (PropList[i]->getName() == "SDNPInGlue") { 10202754fe60SDimitry Andric Properties |= 1 << SDNPInGlue; 10212754fe60SDimitry Andric } else if (PropList[i]->getName() == "SDNPOptInGlue") { 10222754fe60SDimitry Andric Properties |= 1 << SDNPOptInGlue; 1023f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPMayStore") { 1024f22ef01cSRoman Divacky Properties |= 1 << SDNPMayStore; 1025f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPMayLoad") { 1026f22ef01cSRoman Divacky Properties |= 1 << SDNPMayLoad; 1027f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPSideEffect") { 1028f22ef01cSRoman Divacky Properties |= 1 << SDNPSideEffect; 1029f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPMemOperand") { 1030f22ef01cSRoman Divacky Properties |= 1 << SDNPMemOperand; 1031f22ef01cSRoman Divacky } else if (PropList[i]->getName() == "SDNPVariadic") { 1032f22ef01cSRoman Divacky Properties |= 1 << SDNPVariadic; 1033f22ef01cSRoman Divacky } else { 1034f22ef01cSRoman Divacky errs() << "Unknown SD Node property '" << PropList[i]->getName() 1035f22ef01cSRoman Divacky << "' on node '" << R->getName() << "'!\n"; 1036f22ef01cSRoman Divacky exit(1); 1037f22ef01cSRoman Divacky } 1038f22ef01cSRoman Divacky } 1039f22ef01cSRoman Divacky 1040f22ef01cSRoman Divacky 1041f22ef01cSRoman Divacky // Parse the type constraints. 1042f22ef01cSRoman Divacky std::vector<Record*> ConstraintList = 1043f22ef01cSRoman Divacky TypeProfile->getValueAsListOfDefs("Constraints"); 1044f22ef01cSRoman Divacky TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); 1045f22ef01cSRoman Divacky } 1046f22ef01cSRoman Divacky 1047f22ef01cSRoman Divacky /// getKnownType - If the type constraints on this node imply a fixed type 1048f22ef01cSRoman Divacky /// (e.g. all stores return void, etc), then return it as an 1049f22ef01cSRoman Divacky /// MVT::SimpleValueType. Otherwise, return EEVT::Other. 1050f22ef01cSRoman Divacky MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const { 1051f22ef01cSRoman Divacky unsigned NumResults = getNumResults(); 1052f22ef01cSRoman Divacky assert(NumResults <= 1 && 1053f22ef01cSRoman Divacky "We only work with nodes with zero or one result so far!"); 1054f22ef01cSRoman Divacky assert(ResNo == 0 && "Only handles single result nodes so far"); 1055f22ef01cSRoman Divacky 1056f22ef01cSRoman Divacky for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) { 1057f22ef01cSRoman Divacky // Make sure that this applies to the correct node result. 1058f22ef01cSRoman Divacky if (TypeConstraints[i].OperandNo >= NumResults) // FIXME: need value # 1059f22ef01cSRoman Divacky continue; 1060f22ef01cSRoman Divacky 1061f22ef01cSRoman Divacky switch (TypeConstraints[i].ConstraintType) { 1062f22ef01cSRoman Divacky default: break; 1063f22ef01cSRoman Divacky case SDTypeConstraint::SDTCisVT: 1064f22ef01cSRoman Divacky return TypeConstraints[i].x.SDTCisVT_Info.VT; 1065f22ef01cSRoman Divacky case SDTypeConstraint::SDTCisPtrTy: 1066f22ef01cSRoman Divacky return MVT::iPTR; 1067f22ef01cSRoman Divacky } 1068f22ef01cSRoman Divacky } 1069f22ef01cSRoman Divacky return MVT::Other; 1070f22ef01cSRoman Divacky } 1071f22ef01cSRoman Divacky 1072f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 1073f22ef01cSRoman Divacky // TreePatternNode implementation 1074f22ef01cSRoman Divacky // 1075f22ef01cSRoman Divacky 1076f22ef01cSRoman Divacky TreePatternNode::~TreePatternNode() { 1077f22ef01cSRoman Divacky #if 0 // FIXME: implement refcounted tree nodes! 1078f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1079f22ef01cSRoman Divacky delete getChild(i); 1080f22ef01cSRoman Divacky #endif 1081f22ef01cSRoman Divacky } 1082f22ef01cSRoman Divacky 1083f22ef01cSRoman Divacky static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) { 1084f22ef01cSRoman Divacky if (Operator->getName() == "set" || 1085f22ef01cSRoman Divacky Operator->getName() == "implicit") 1086f22ef01cSRoman Divacky return 0; // All return nothing. 1087f22ef01cSRoman Divacky 1088f22ef01cSRoman Divacky if (Operator->isSubClassOf("Intrinsic")) 1089f22ef01cSRoman Divacky return CDP.getIntrinsic(Operator).IS.RetVTs.size(); 1090f22ef01cSRoman Divacky 1091f22ef01cSRoman Divacky if (Operator->isSubClassOf("SDNode")) 1092f22ef01cSRoman Divacky return CDP.getSDNodeInfo(Operator).getNumResults(); 1093f22ef01cSRoman Divacky 1094f22ef01cSRoman Divacky if (Operator->isSubClassOf("PatFrag")) { 1095f22ef01cSRoman Divacky // If we've already parsed this pattern fragment, get it. Otherwise, handle 1096f22ef01cSRoman Divacky // the forward reference case where one pattern fragment references another 1097f22ef01cSRoman Divacky // before it is processed. 1098f22ef01cSRoman Divacky if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) 1099f22ef01cSRoman Divacky return PFRec->getOnlyTree()->getNumTypes(); 1100f22ef01cSRoman Divacky 1101f22ef01cSRoman Divacky // Get the result tree. 1102f22ef01cSRoman Divacky DagInit *Tree = Operator->getValueAsDag("Fragment"); 110391bc56edSDimitry Andric Record *Op = nullptr; 11043861d79fSDimitry Andric if (Tree) 11053861d79fSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(Tree->getOperator())) 11063861d79fSDimitry Andric Op = DI->getDef(); 1107f22ef01cSRoman Divacky assert(Op && "Invalid Fragment"); 1108f22ef01cSRoman Divacky return GetNumNodeResults(Op, CDP); 1109f22ef01cSRoman Divacky } 1110f22ef01cSRoman Divacky 1111f22ef01cSRoman Divacky if (Operator->isSubClassOf("Instruction")) { 1112f22ef01cSRoman Divacky CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator); 1113f22ef01cSRoman Divacky 1114f22ef01cSRoman Divacky // FIXME: Should allow access to all the results here. 11152754fe60SDimitry Andric unsigned NumDefsToAdd = InstInfo.Operands.NumDefs ? 1 : 0; 1116f22ef01cSRoman Divacky 1117f22ef01cSRoman Divacky // Add on one implicit def if it has a resolvable type. 1118f22ef01cSRoman Divacky if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other) 1119f22ef01cSRoman Divacky ++NumDefsToAdd; 1120f22ef01cSRoman Divacky return NumDefsToAdd; 1121f22ef01cSRoman Divacky } 1122f22ef01cSRoman Divacky 1123f22ef01cSRoman Divacky if (Operator->isSubClassOf("SDNodeXForm")) 1124f22ef01cSRoman Divacky return 1; // FIXME: Generalize SDNodeXForm 1125f22ef01cSRoman Divacky 112691bc56edSDimitry Andric if (Operator->isSubClassOf("ValueType")) 112791bc56edSDimitry Andric return 1; // A type-cast of one result. 112891bc56edSDimitry Andric 112991bc56edSDimitry Andric if (Operator->isSubClassOf("ComplexPattern")) 113091bc56edSDimitry Andric return 1; 113191bc56edSDimitry Andric 1132f22ef01cSRoman Divacky Operator->dump(); 1133f22ef01cSRoman Divacky errs() << "Unhandled node in GetNumNodeResults\n"; 1134f22ef01cSRoman Divacky exit(1); 1135f22ef01cSRoman Divacky } 1136f22ef01cSRoman Divacky 1137f22ef01cSRoman Divacky void TreePatternNode::print(raw_ostream &OS) const { 1138f22ef01cSRoman Divacky if (isLeaf()) 1139f22ef01cSRoman Divacky OS << *getLeafValue(); 1140f22ef01cSRoman Divacky else 1141f22ef01cSRoman Divacky OS << '(' << getOperator()->getName(); 1142f22ef01cSRoman Divacky 1143f22ef01cSRoman Divacky for (unsigned i = 0, e = Types.size(); i != e; ++i) 1144f22ef01cSRoman Divacky OS << ':' << getExtType(i).getName(); 1145f22ef01cSRoman Divacky 1146f22ef01cSRoman Divacky if (!isLeaf()) { 1147f22ef01cSRoman Divacky if (getNumChildren() != 0) { 1148f22ef01cSRoman Divacky OS << " "; 1149f22ef01cSRoman Divacky getChild(0)->print(OS); 1150f22ef01cSRoman Divacky for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { 1151f22ef01cSRoman Divacky OS << ", "; 1152f22ef01cSRoman Divacky getChild(i)->print(OS); 1153f22ef01cSRoman Divacky } 1154f22ef01cSRoman Divacky } 1155f22ef01cSRoman Divacky OS << ")"; 1156f22ef01cSRoman Divacky } 1157f22ef01cSRoman Divacky 1158f22ef01cSRoman Divacky for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i) 11593b0f4066SDimitry Andric OS << "<<P:" << PredicateFns[i].getFnName() << ">>"; 1160f22ef01cSRoman Divacky if (TransformFn) 1161f22ef01cSRoman Divacky OS << "<<X:" << TransformFn->getName() << ">>"; 1162f22ef01cSRoman Divacky if (!getName().empty()) 1163f22ef01cSRoman Divacky OS << ":$" << getName(); 1164f22ef01cSRoman Divacky 1165f22ef01cSRoman Divacky } 1166f22ef01cSRoman Divacky void TreePatternNode::dump() const { 1167f22ef01cSRoman Divacky print(errs()); 1168f22ef01cSRoman Divacky } 1169f22ef01cSRoman Divacky 1170f22ef01cSRoman Divacky /// isIsomorphicTo - Return true if this node is recursively 1171f22ef01cSRoman Divacky /// isomorphic to the specified node. For this comparison, the node's 1172f22ef01cSRoman Divacky /// entire state is considered. The assigned name is ignored, since 1173f22ef01cSRoman Divacky /// nodes with differing names are considered isomorphic. However, if 1174f22ef01cSRoman Divacky /// the assigned name is present in the dependent variable set, then 1175f22ef01cSRoman Divacky /// the assigned name is considered significant and the node is 1176f22ef01cSRoman Divacky /// isomorphic if the names match. 1177f22ef01cSRoman Divacky bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, 1178f22ef01cSRoman Divacky const MultipleUseVarSet &DepVars) const { 1179f22ef01cSRoman Divacky if (N == this) return true; 1180f22ef01cSRoman Divacky if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || 1181f22ef01cSRoman Divacky getPredicateFns() != N->getPredicateFns() || 1182f22ef01cSRoman Divacky getTransformFn() != N->getTransformFn()) 1183f22ef01cSRoman Divacky return false; 1184f22ef01cSRoman Divacky 1185f22ef01cSRoman Divacky if (isLeaf()) { 11863861d79fSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 11873861d79fSDimitry Andric if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) { 1188f22ef01cSRoman Divacky return ((DI->getDef() == NDI->getDef()) 1189f22ef01cSRoman Divacky && (DepVars.find(getName()) == DepVars.end() 1190f22ef01cSRoman Divacky || getName() == N->getName())); 1191f22ef01cSRoman Divacky } 1192f22ef01cSRoman Divacky } 1193f22ef01cSRoman Divacky return getLeafValue() == N->getLeafValue(); 1194f22ef01cSRoman Divacky } 1195f22ef01cSRoman Divacky 1196f22ef01cSRoman Divacky if (N->getOperator() != getOperator() || 1197f22ef01cSRoman Divacky N->getNumChildren() != getNumChildren()) return false; 1198f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1199f22ef01cSRoman Divacky if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars)) 1200f22ef01cSRoman Divacky return false; 1201f22ef01cSRoman Divacky return true; 1202f22ef01cSRoman Divacky } 1203f22ef01cSRoman Divacky 1204f22ef01cSRoman Divacky /// clone - Make a copy of this tree and all of its children. 1205f22ef01cSRoman Divacky /// 1206f22ef01cSRoman Divacky TreePatternNode *TreePatternNode::clone() const { 1207f22ef01cSRoman Divacky TreePatternNode *New; 1208f22ef01cSRoman Divacky if (isLeaf()) { 1209f22ef01cSRoman Divacky New = new TreePatternNode(getLeafValue(), getNumTypes()); 1210f22ef01cSRoman Divacky } else { 1211f22ef01cSRoman Divacky std::vector<TreePatternNode*> CChildren; 1212f22ef01cSRoman Divacky CChildren.reserve(Children.size()); 1213f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1214f22ef01cSRoman Divacky CChildren.push_back(getChild(i)->clone()); 1215f22ef01cSRoman Divacky New = new TreePatternNode(getOperator(), CChildren, getNumTypes()); 1216f22ef01cSRoman Divacky } 1217f22ef01cSRoman Divacky New->setName(getName()); 1218f22ef01cSRoman Divacky New->Types = Types; 1219f22ef01cSRoman Divacky New->setPredicateFns(getPredicateFns()); 1220f22ef01cSRoman Divacky New->setTransformFn(getTransformFn()); 1221f22ef01cSRoman Divacky return New; 1222f22ef01cSRoman Divacky } 1223f22ef01cSRoman Divacky 1224f22ef01cSRoman Divacky /// RemoveAllTypes - Recursively strip all the types of this tree. 1225f22ef01cSRoman Divacky void TreePatternNode::RemoveAllTypes() { 1226f22ef01cSRoman Divacky for (unsigned i = 0, e = Types.size(); i != e; ++i) 1227f22ef01cSRoman Divacky Types[i] = EEVT::TypeSet(); // Reset to unknown type. 1228f22ef01cSRoman Divacky if (isLeaf()) return; 1229f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1230f22ef01cSRoman Divacky getChild(i)->RemoveAllTypes(); 1231f22ef01cSRoman Divacky } 1232f22ef01cSRoman Divacky 1233f22ef01cSRoman Divacky 1234f22ef01cSRoman Divacky /// SubstituteFormalArguments - Replace the formal arguments in this tree 1235f22ef01cSRoman Divacky /// with actual values specified by ArgMap. 1236f22ef01cSRoman Divacky void TreePatternNode:: 1237f22ef01cSRoman Divacky SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { 1238f22ef01cSRoman Divacky if (isLeaf()) return; 1239f22ef01cSRoman Divacky 1240f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1241f22ef01cSRoman Divacky TreePatternNode *Child = getChild(i); 1242f22ef01cSRoman Divacky if (Child->isLeaf()) { 1243f22ef01cSRoman Divacky Init *Val = Child->getLeafValue(); 124491bc56edSDimitry Andric // Note that, when substituting into an output pattern, Val might be an 124591bc56edSDimitry Andric // UnsetInit. 124691bc56edSDimitry Andric if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) && 124791bc56edSDimitry Andric cast<DefInit>(Val)->getDef()->getName() == "node")) { 1248f22ef01cSRoman Divacky // We found a use of a formal argument, replace it with its value. 1249f22ef01cSRoman Divacky TreePatternNode *NewChild = ArgMap[Child->getName()]; 1250f22ef01cSRoman Divacky assert(NewChild && "Couldn't find formal argument!"); 1251f22ef01cSRoman Divacky assert((Child->getPredicateFns().empty() || 1252f22ef01cSRoman Divacky NewChild->getPredicateFns() == Child->getPredicateFns()) && 1253f22ef01cSRoman Divacky "Non-empty child predicate clobbered!"); 1254f22ef01cSRoman Divacky setChild(i, NewChild); 1255f22ef01cSRoman Divacky } 1256f22ef01cSRoman Divacky } else { 1257f22ef01cSRoman Divacky getChild(i)->SubstituteFormalArguments(ArgMap); 1258f22ef01cSRoman Divacky } 1259f22ef01cSRoman Divacky } 1260f22ef01cSRoman Divacky } 1261f22ef01cSRoman Divacky 1262f22ef01cSRoman Divacky 1263f22ef01cSRoman Divacky /// InlinePatternFragments - If this pattern refers to any pattern 1264f22ef01cSRoman Divacky /// fragments, inline them into place, giving us a pattern without any 1265f22ef01cSRoman Divacky /// PatFrag references. 1266f22ef01cSRoman Divacky TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { 12673861d79fSDimitry Andric if (TP.hasError()) 126891bc56edSDimitry Andric return nullptr; 12693861d79fSDimitry Andric 12703861d79fSDimitry Andric if (isLeaf()) 12713861d79fSDimitry Andric return this; // nothing to do. 1272f22ef01cSRoman Divacky Record *Op = getOperator(); 1273f22ef01cSRoman Divacky 1274f22ef01cSRoman Divacky if (!Op->isSubClassOf("PatFrag")) { 1275f22ef01cSRoman Divacky // Just recursively inline children nodes. 1276f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { 1277f22ef01cSRoman Divacky TreePatternNode *Child = getChild(i); 1278f22ef01cSRoman Divacky TreePatternNode *NewChild = Child->InlinePatternFragments(TP); 1279f22ef01cSRoman Divacky 1280f22ef01cSRoman Divacky assert((Child->getPredicateFns().empty() || 1281f22ef01cSRoman Divacky NewChild->getPredicateFns() == Child->getPredicateFns()) && 1282f22ef01cSRoman Divacky "Non-empty child predicate clobbered!"); 1283f22ef01cSRoman Divacky 1284f22ef01cSRoman Divacky setChild(i, NewChild); 1285f22ef01cSRoman Divacky } 1286f22ef01cSRoman Divacky return this; 1287f22ef01cSRoman Divacky } 1288f22ef01cSRoman Divacky 1289f22ef01cSRoman Divacky // Otherwise, we found a reference to a fragment. First, look up its 1290f22ef01cSRoman Divacky // TreePattern record. 1291f22ef01cSRoman Divacky TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); 1292f22ef01cSRoman Divacky 1293f22ef01cSRoman Divacky // Verify that we are passing the right number of operands. 12943861d79fSDimitry Andric if (Frag->getNumArgs() != Children.size()) { 1295f22ef01cSRoman Divacky TP.error("'" + Op->getName() + "' fragment requires " + 1296f22ef01cSRoman Divacky utostr(Frag->getNumArgs()) + " operands!"); 129791bc56edSDimitry Andric return nullptr; 12983861d79fSDimitry Andric } 1299f22ef01cSRoman Divacky 1300f22ef01cSRoman Divacky TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); 1301f22ef01cSRoman Divacky 13023b0f4066SDimitry Andric TreePredicateFn PredFn(Frag); 13033b0f4066SDimitry Andric if (!PredFn.isAlwaysTrue()) 13043b0f4066SDimitry Andric FragTree->addPredicateFn(PredFn); 1305f22ef01cSRoman Divacky 1306f22ef01cSRoman Divacky // Resolve formal arguments to their actual value. 1307f22ef01cSRoman Divacky if (Frag->getNumArgs()) { 1308f22ef01cSRoman Divacky // Compute the map of formal to actual arguments. 1309f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> ArgMap; 1310f22ef01cSRoman Divacky for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) 1311f22ef01cSRoman Divacky ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); 1312f22ef01cSRoman Divacky 1313f22ef01cSRoman Divacky FragTree->SubstituteFormalArguments(ArgMap); 1314f22ef01cSRoman Divacky } 1315f22ef01cSRoman Divacky 1316f22ef01cSRoman Divacky FragTree->setName(getName()); 1317f22ef01cSRoman Divacky for (unsigned i = 0, e = Types.size(); i != e; ++i) 1318f22ef01cSRoman Divacky FragTree->UpdateNodeType(i, getExtType(i), TP); 1319f22ef01cSRoman Divacky 1320f22ef01cSRoman Divacky // Transfer in the old predicates. 1321f22ef01cSRoman Divacky for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i) 1322f22ef01cSRoman Divacky FragTree->addPredicateFn(getPredicateFns()[i]); 1323f22ef01cSRoman Divacky 1324f22ef01cSRoman Divacky // Get a new copy of this fragment to stitch into here. 1325f22ef01cSRoman Divacky //delete this; // FIXME: implement refcounting! 1326f22ef01cSRoman Divacky 1327f22ef01cSRoman Divacky // The fragment we inlined could have recursive inlining that is needed. See 1328f22ef01cSRoman Divacky // if there are any pattern fragments in it and inline them as needed. 1329f22ef01cSRoman Divacky return FragTree->InlinePatternFragments(TP); 1330f22ef01cSRoman Divacky } 1331f22ef01cSRoman Divacky 1332f22ef01cSRoman Divacky /// getImplicitType - Check to see if the specified record has an implicit 1333f22ef01cSRoman Divacky /// type which should be applied to it. This will infer the type of register 1334f22ef01cSRoman Divacky /// references from the register file information, for example. 1335f22ef01cSRoman Divacky /// 1336139f7f9bSDimitry Andric /// When Unnamed is set, return the type of a DAG operand with no name, such as 1337139f7f9bSDimitry Andric /// the F8RC register class argument in: 1338139f7f9bSDimitry Andric /// 1339139f7f9bSDimitry Andric /// (COPY_TO_REGCLASS GPR:$src, F8RC) 1340139f7f9bSDimitry Andric /// 1341139f7f9bSDimitry Andric /// When Unnamed is false, return the type of a named DAG operand such as the 1342139f7f9bSDimitry Andric /// GPR:$src operand above. 1343139f7f9bSDimitry Andric /// 1344f22ef01cSRoman Divacky static EEVT::TypeSet getImplicitType(Record *R, unsigned ResNo, 1345139f7f9bSDimitry Andric bool NotRegisters, 1346139f7f9bSDimitry Andric bool Unnamed, 1347139f7f9bSDimitry Andric TreePattern &TP) { 134817a519f9SDimitry Andric // Check to see if this is a register operand. 134917a519f9SDimitry Andric if (R->isSubClassOf("RegisterOperand")) { 135017a519f9SDimitry Andric assert(ResNo == 0 && "Regoperand ref only has one result!"); 135117a519f9SDimitry Andric if (NotRegisters) 135217a519f9SDimitry Andric return EEVT::TypeSet(); // Unknown. 135317a519f9SDimitry Andric Record *RegClass = R->getValueAsDef("RegClass"); 135417a519f9SDimitry Andric const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 135517a519f9SDimitry Andric return EEVT::TypeSet(T.getRegisterClass(RegClass).getValueTypes()); 135617a519f9SDimitry Andric } 135717a519f9SDimitry Andric 1358f22ef01cSRoman Divacky // Check to see if this is a register or a register class. 1359f22ef01cSRoman Divacky if (R->isSubClassOf("RegisterClass")) { 1360f22ef01cSRoman Divacky assert(ResNo == 0 && "Regclass ref only has one result!"); 1361139f7f9bSDimitry Andric // An unnamed register class represents itself as an i32 immediate, for 1362139f7f9bSDimitry Andric // example on a COPY_TO_REGCLASS instruction. 1363139f7f9bSDimitry Andric if (Unnamed) 1364139f7f9bSDimitry Andric return EEVT::TypeSet(MVT::i32, TP); 1365139f7f9bSDimitry Andric 1366139f7f9bSDimitry Andric // In a named operand, the register class provides the possible set of 1367139f7f9bSDimitry Andric // types. 1368f22ef01cSRoman Divacky if (NotRegisters) 1369f22ef01cSRoman Divacky return EEVT::TypeSet(); // Unknown. 1370f22ef01cSRoman Divacky const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1371f22ef01cSRoman Divacky return EEVT::TypeSet(T.getRegisterClass(R).getValueTypes()); 1372f22ef01cSRoman Divacky } 1373f22ef01cSRoman Divacky 1374f22ef01cSRoman Divacky if (R->isSubClassOf("PatFrag")) { 1375f22ef01cSRoman Divacky assert(ResNo == 0 && "FIXME: PatFrag with multiple results?"); 1376f22ef01cSRoman Divacky // Pattern fragment types will be resolved when they are inlined. 1377f22ef01cSRoman Divacky return EEVT::TypeSet(); // Unknown. 1378f22ef01cSRoman Divacky } 1379f22ef01cSRoman Divacky 1380f22ef01cSRoman Divacky if (R->isSubClassOf("Register")) { 1381f22ef01cSRoman Divacky assert(ResNo == 0 && "Registers only produce one result!"); 1382f22ef01cSRoman Divacky if (NotRegisters) 1383f22ef01cSRoman Divacky return EEVT::TypeSet(); // Unknown. 1384f22ef01cSRoman Divacky const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); 1385f22ef01cSRoman Divacky return EEVT::TypeSet(T.getRegisterVTs(R)); 1386f22ef01cSRoman Divacky } 1387f22ef01cSRoman Divacky 1388f22ef01cSRoman Divacky if (R->isSubClassOf("SubRegIndex")) { 1389f22ef01cSRoman Divacky assert(ResNo == 0 && "SubRegisterIndices only produce one result!"); 1390f22ef01cSRoman Divacky return EEVT::TypeSet(); 1391f22ef01cSRoman Divacky } 1392f22ef01cSRoman Divacky 1393139f7f9bSDimitry Andric if (R->isSubClassOf("ValueType")) { 1394f22ef01cSRoman Divacky assert(ResNo == 0 && "This node only has one result!"); 1395139f7f9bSDimitry Andric // An unnamed VTSDNode represents itself as an MVT::Other immediate. 1396139f7f9bSDimitry Andric // 1397139f7f9bSDimitry Andric // (sext_inreg GPR:$src, i16) 1398139f7f9bSDimitry Andric // ~~~ 1399139f7f9bSDimitry Andric if (Unnamed) 1400139f7f9bSDimitry Andric return EEVT::TypeSet(MVT::Other, TP); 1401139f7f9bSDimitry Andric // With a name, the ValueType simply provides the type of the named 1402139f7f9bSDimitry Andric // variable. 1403139f7f9bSDimitry Andric // 1404139f7f9bSDimitry Andric // (sext_inreg i32:$src, i16) 1405139f7f9bSDimitry Andric // ~~~~~~~~ 1406139f7f9bSDimitry Andric if (NotRegisters) 1407139f7f9bSDimitry Andric return EEVT::TypeSet(); // Unknown. 1408139f7f9bSDimitry Andric return EEVT::TypeSet(getValueType(R), TP); 1409139f7f9bSDimitry Andric } 1410139f7f9bSDimitry Andric 1411139f7f9bSDimitry Andric if (R->isSubClassOf("CondCode")) { 1412139f7f9bSDimitry Andric assert(ResNo == 0 && "This node only has one result!"); 1413139f7f9bSDimitry Andric // Using a CondCodeSDNode. 1414f22ef01cSRoman Divacky return EEVT::TypeSet(MVT::Other, TP); 1415f22ef01cSRoman Divacky } 1416f22ef01cSRoman Divacky 1417f22ef01cSRoman Divacky if (R->isSubClassOf("ComplexPattern")) { 1418f22ef01cSRoman Divacky assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?"); 1419f22ef01cSRoman Divacky if (NotRegisters) 1420f22ef01cSRoman Divacky return EEVT::TypeSet(); // Unknown. 1421f22ef01cSRoman Divacky return EEVT::TypeSet(TP.getDAGPatterns().getComplexPattern(R).getValueType(), 1422f22ef01cSRoman Divacky TP); 1423f22ef01cSRoman Divacky } 1424f22ef01cSRoman Divacky if (R->isSubClassOf("PointerLikeRegClass")) { 1425f22ef01cSRoman Divacky assert(ResNo == 0 && "Regclass can only have one result!"); 1426f22ef01cSRoman Divacky return EEVT::TypeSet(MVT::iPTR, TP); 1427f22ef01cSRoman Divacky } 1428f22ef01cSRoman Divacky 1429f22ef01cSRoman Divacky if (R->getName() == "node" || R->getName() == "srcvalue" || 1430f22ef01cSRoman Divacky R->getName() == "zero_reg") { 1431f22ef01cSRoman Divacky // Placeholder. 1432f22ef01cSRoman Divacky return EEVT::TypeSet(); // Unknown. 1433f22ef01cSRoman Divacky } 1434f22ef01cSRoman Divacky 143591bc56edSDimitry Andric if (R->isSubClassOf("Operand")) 143691bc56edSDimitry Andric return EEVT::TypeSet(getValueType(R->getValueAsDef("Type"))); 143791bc56edSDimitry Andric 1438f22ef01cSRoman Divacky TP.error("Unknown node flavor used in pattern: " + R->getName()); 1439f22ef01cSRoman Divacky return EEVT::TypeSet(MVT::Other, TP); 1440f22ef01cSRoman Divacky } 1441f22ef01cSRoman Divacky 1442f22ef01cSRoman Divacky 1443f22ef01cSRoman Divacky /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the 1444f22ef01cSRoman Divacky /// CodeGenIntrinsic information for it, otherwise return a null pointer. 1445f22ef01cSRoman Divacky const CodeGenIntrinsic *TreePatternNode:: 1446f22ef01cSRoman Divacky getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { 1447f22ef01cSRoman Divacky if (getOperator() != CDP.get_intrinsic_void_sdnode() && 1448f22ef01cSRoman Divacky getOperator() != CDP.get_intrinsic_w_chain_sdnode() && 1449f22ef01cSRoman Divacky getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) 145091bc56edSDimitry Andric return nullptr; 1451f22ef01cSRoman Divacky 14523861d79fSDimitry Andric unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue(); 1453f22ef01cSRoman Divacky return &CDP.getIntrinsicInfo(IID); 1454f22ef01cSRoman Divacky } 1455f22ef01cSRoman Divacky 1456f22ef01cSRoman Divacky /// getComplexPatternInfo - If this node corresponds to a ComplexPattern, 1457f22ef01cSRoman Divacky /// return the ComplexPattern information, otherwise return null. 1458f22ef01cSRoman Divacky const ComplexPattern * 1459f22ef01cSRoman Divacky TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const { 146091bc56edSDimitry Andric Record *Rec; 146191bc56edSDimitry Andric if (isLeaf()) { 14623861d79fSDimitry Andric DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 146391bc56edSDimitry Andric if (!DI) 146491bc56edSDimitry Andric return nullptr; 146591bc56edSDimitry Andric Rec = DI->getDef(); 146691bc56edSDimitry Andric } else 146791bc56edSDimitry Andric Rec = getOperator(); 146891bc56edSDimitry Andric 146991bc56edSDimitry Andric if (!Rec->isSubClassOf("ComplexPattern")) 147091bc56edSDimitry Andric return nullptr; 147191bc56edSDimitry Andric return &CGP.getComplexPattern(Rec); 147291bc56edSDimitry Andric } 147391bc56edSDimitry Andric 147491bc56edSDimitry Andric unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const { 147591bc56edSDimitry Andric // A ComplexPattern specifically declares how many results it fills in. 147691bc56edSDimitry Andric if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) 147791bc56edSDimitry Andric return CP->getNumOperands(); 147891bc56edSDimitry Andric 147991bc56edSDimitry Andric // If MIOperandInfo is specified, that gives the count. 148091bc56edSDimitry Andric if (isLeaf()) { 148191bc56edSDimitry Andric DefInit *DI = dyn_cast<DefInit>(getLeafValue()); 148291bc56edSDimitry Andric if (DI && DI->getDef()->isSubClassOf("Operand")) { 148391bc56edSDimitry Andric DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo"); 148491bc56edSDimitry Andric if (MIOps->getNumArgs()) 148591bc56edSDimitry Andric return MIOps->getNumArgs(); 148691bc56edSDimitry Andric } 148791bc56edSDimitry Andric } 148891bc56edSDimitry Andric 148991bc56edSDimitry Andric // Otherwise there is just one result. 149091bc56edSDimitry Andric return 1; 1491f22ef01cSRoman Divacky } 1492f22ef01cSRoman Divacky 1493f22ef01cSRoman Divacky /// NodeHasProperty - Return true if this node has the specified property. 1494f22ef01cSRoman Divacky bool TreePatternNode::NodeHasProperty(SDNP Property, 1495f22ef01cSRoman Divacky const CodeGenDAGPatterns &CGP) const { 1496f22ef01cSRoman Divacky if (isLeaf()) { 1497f22ef01cSRoman Divacky if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) 1498f22ef01cSRoman Divacky return CP->hasProperty(Property); 1499f22ef01cSRoman Divacky return false; 1500f22ef01cSRoman Divacky } 1501f22ef01cSRoman Divacky 1502f22ef01cSRoman Divacky Record *Operator = getOperator(); 1503f22ef01cSRoman Divacky if (!Operator->isSubClassOf("SDNode")) return false; 1504f22ef01cSRoman Divacky 1505f22ef01cSRoman Divacky return CGP.getSDNodeInfo(Operator).hasProperty(Property); 1506f22ef01cSRoman Divacky } 1507f22ef01cSRoman Divacky 1508f22ef01cSRoman Divacky 1509f22ef01cSRoman Divacky 1510f22ef01cSRoman Divacky 1511f22ef01cSRoman Divacky /// TreeHasProperty - Return true if any node in this tree has the specified 1512f22ef01cSRoman Divacky /// property. 1513f22ef01cSRoman Divacky bool TreePatternNode::TreeHasProperty(SDNP Property, 1514f22ef01cSRoman Divacky const CodeGenDAGPatterns &CGP) const { 1515f22ef01cSRoman Divacky if (NodeHasProperty(Property, CGP)) 1516f22ef01cSRoman Divacky return true; 1517f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1518f22ef01cSRoman Divacky if (getChild(i)->TreeHasProperty(Property, CGP)) 1519f22ef01cSRoman Divacky return true; 1520f22ef01cSRoman Divacky return false; 1521f22ef01cSRoman Divacky } 1522f22ef01cSRoman Divacky 1523f22ef01cSRoman Divacky /// isCommutativeIntrinsic - Return true if the node corresponds to a 1524f22ef01cSRoman Divacky /// commutative intrinsic. 1525f22ef01cSRoman Divacky bool 1526f22ef01cSRoman Divacky TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const { 1527f22ef01cSRoman Divacky if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) 1528f22ef01cSRoman Divacky return Int->isCommutative; 1529f22ef01cSRoman Divacky return false; 1530f22ef01cSRoman Divacky } 1531f22ef01cSRoman Divacky 1532f22ef01cSRoman Divacky 1533f22ef01cSRoman Divacky /// ApplyTypeConstraints - Apply all of the type constraints relevant to 1534f22ef01cSRoman Divacky /// this node and its children in the tree. This returns true if it makes a 15353861d79fSDimitry Andric /// change, false otherwise. If a type contradiction is found, flag an error. 1536f22ef01cSRoman Divacky bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { 15373861d79fSDimitry Andric if (TP.hasError()) 15383861d79fSDimitry Andric return false; 15393861d79fSDimitry Andric 1540f22ef01cSRoman Divacky CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); 1541f22ef01cSRoman Divacky if (isLeaf()) { 15423861d79fSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) { 1543f22ef01cSRoman Divacky // If it's a regclass or something else known, include the type. 1544f22ef01cSRoman Divacky bool MadeChange = false; 1545f22ef01cSRoman Divacky for (unsigned i = 0, e = Types.size(); i != e; ++i) 1546f22ef01cSRoman Divacky MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i, 1547139f7f9bSDimitry Andric NotRegisters, 1548139f7f9bSDimitry Andric !hasName(), TP), TP); 1549f22ef01cSRoman Divacky return MadeChange; 1550f22ef01cSRoman Divacky } 1551f22ef01cSRoman Divacky 15523861d79fSDimitry Andric if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) { 1553f22ef01cSRoman Divacky assert(Types.size() == 1 && "Invalid IntInit"); 1554f22ef01cSRoman Divacky 1555f22ef01cSRoman Divacky // Int inits are always integers. :) 1556f22ef01cSRoman Divacky bool MadeChange = Types[0].EnforceInteger(TP); 1557f22ef01cSRoman Divacky 1558f22ef01cSRoman Divacky if (!Types[0].isConcrete()) 1559f22ef01cSRoman Divacky return MadeChange; 1560f22ef01cSRoman Divacky 1561f22ef01cSRoman Divacky MVT::SimpleValueType VT = getType(0); 1562f22ef01cSRoman Divacky if (VT == MVT::iPTR || VT == MVT::iPTRAny) 1563f22ef01cSRoman Divacky return MadeChange; 1564f22ef01cSRoman Divacky 1565f785676fSDimitry Andric unsigned Size = MVT(VT).getSizeInBits(); 1566f22ef01cSRoman Divacky // Make sure that the value is representable for this type. 1567f22ef01cSRoman Divacky if (Size >= 32) return MadeChange; 1568f22ef01cSRoman Divacky 15693861d79fSDimitry Andric // Check that the value doesn't use more bits than we have. It must either 15703861d79fSDimitry Andric // be a sign- or zero-extended equivalent of the original. 15713861d79fSDimitry Andric int64_t SignBitAndAbove = II->getValue() >> (Size - 1); 15723861d79fSDimitry Andric if (SignBitAndAbove == -1 || SignBitAndAbove == 0 || SignBitAndAbove == 1) 1573f22ef01cSRoman Divacky return MadeChange; 1574f22ef01cSRoman Divacky 1575f22ef01cSRoman Divacky TP.error("Integer value '" + itostr(II->getValue()) + 1576f22ef01cSRoman Divacky "' is out of range for type '" + getEnumName(getType(0)) + "'!"); 15773861d79fSDimitry Andric return false; 1578f22ef01cSRoman Divacky } 1579f22ef01cSRoman Divacky return false; 1580f22ef01cSRoman Divacky } 1581f22ef01cSRoman Divacky 1582f22ef01cSRoman Divacky // special handling for set, which isn't really an SDNode. 1583f22ef01cSRoman Divacky if (getOperator()->getName() == "set") { 1584f22ef01cSRoman Divacky assert(getNumTypes() == 0 && "Set doesn't produce a value"); 1585f22ef01cSRoman Divacky assert(getNumChildren() >= 2 && "Missing RHS of a set?"); 1586f22ef01cSRoman Divacky unsigned NC = getNumChildren(); 1587f22ef01cSRoman Divacky 1588f22ef01cSRoman Divacky TreePatternNode *SetVal = getChild(NC-1); 1589f22ef01cSRoman Divacky bool MadeChange = SetVal->ApplyTypeConstraints(TP, NotRegisters); 1590f22ef01cSRoman Divacky 1591f22ef01cSRoman Divacky for (unsigned i = 0; i < NC-1; ++i) { 1592f22ef01cSRoman Divacky TreePatternNode *Child = getChild(i); 1593f22ef01cSRoman Divacky MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters); 1594f22ef01cSRoman Divacky 1595f22ef01cSRoman Divacky // Types of operands must match. 1596f22ef01cSRoman Divacky MadeChange |= Child->UpdateNodeType(0, SetVal->getExtType(i), TP); 1597f22ef01cSRoman Divacky MadeChange |= SetVal->UpdateNodeType(i, Child->getExtType(0), TP); 1598f22ef01cSRoman Divacky } 1599f22ef01cSRoman Divacky return MadeChange; 1600f22ef01cSRoman Divacky } 1601f22ef01cSRoman Divacky 1602f22ef01cSRoman Divacky if (getOperator()->getName() == "implicit") { 1603f22ef01cSRoman Divacky assert(getNumTypes() == 0 && "Node doesn't produce a value"); 1604f22ef01cSRoman Divacky 1605f22ef01cSRoman Divacky bool MadeChange = false; 1606f22ef01cSRoman Divacky for (unsigned i = 0; i < getNumChildren(); ++i) 1607f22ef01cSRoman Divacky MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1608f22ef01cSRoman Divacky return MadeChange; 1609f22ef01cSRoman Divacky } 1610f22ef01cSRoman Divacky 1611f22ef01cSRoman Divacky if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) { 1612f22ef01cSRoman Divacky bool MadeChange = false; 1613f22ef01cSRoman Divacky 1614f22ef01cSRoman Divacky // Apply the result type to the node. 1615f22ef01cSRoman Divacky unsigned NumRetVTs = Int->IS.RetVTs.size(); 1616f22ef01cSRoman Divacky unsigned NumParamVTs = Int->IS.ParamVTs.size(); 1617f22ef01cSRoman Divacky 1618f22ef01cSRoman Divacky for (unsigned i = 0, e = NumRetVTs; i != e; ++i) 1619f22ef01cSRoman Divacky MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP); 1620f22ef01cSRoman Divacky 16213861d79fSDimitry Andric if (getNumChildren() != NumParamVTs + 1) { 1622f22ef01cSRoman Divacky TP.error("Intrinsic '" + Int->Name + "' expects " + 1623f22ef01cSRoman Divacky utostr(NumParamVTs) + " operands, not " + 1624f22ef01cSRoman Divacky utostr(getNumChildren() - 1) + " operands!"); 16253861d79fSDimitry Andric return false; 16263861d79fSDimitry Andric } 1627f22ef01cSRoman Divacky 1628f22ef01cSRoman Divacky // Apply type info to the intrinsic ID. 1629f22ef01cSRoman Divacky MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP); 1630f22ef01cSRoman Divacky 1631f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) { 1632f22ef01cSRoman Divacky MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters); 1633f22ef01cSRoman Divacky 1634f22ef01cSRoman Divacky MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i]; 1635f22ef01cSRoman Divacky assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case"); 1636f22ef01cSRoman Divacky MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP); 1637f22ef01cSRoman Divacky } 1638f22ef01cSRoman Divacky return MadeChange; 1639f22ef01cSRoman Divacky } 1640f22ef01cSRoman Divacky 1641f22ef01cSRoman Divacky if (getOperator()->isSubClassOf("SDNode")) { 1642f22ef01cSRoman Divacky const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator()); 1643f22ef01cSRoman Divacky 1644f22ef01cSRoman Divacky // Check that the number of operands is sane. Negative operands -> varargs. 1645f22ef01cSRoman Divacky if (NI.getNumOperands() >= 0 && 16463861d79fSDimitry Andric getNumChildren() != (unsigned)NI.getNumOperands()) { 1647f22ef01cSRoman Divacky TP.error(getOperator()->getName() + " node requires exactly " + 1648f22ef01cSRoman Divacky itostr(NI.getNumOperands()) + " operands!"); 16493861d79fSDimitry Andric return false; 16503861d79fSDimitry Andric } 1651f22ef01cSRoman Divacky 1652f22ef01cSRoman Divacky bool MadeChange = NI.ApplyTypeConstraints(this, TP); 1653f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1654f22ef01cSRoman Divacky MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1655f22ef01cSRoman Divacky return MadeChange; 1656f22ef01cSRoman Divacky } 1657f22ef01cSRoman Divacky 1658f22ef01cSRoman Divacky if (getOperator()->isSubClassOf("Instruction")) { 1659f22ef01cSRoman Divacky const DAGInstruction &Inst = CDP.getInstruction(getOperator()); 1660f22ef01cSRoman Divacky CodeGenInstruction &InstInfo = 1661f22ef01cSRoman Divacky CDP.getTargetInfo().getInstruction(getOperator()); 1662f22ef01cSRoman Divacky 1663f22ef01cSRoman Divacky bool MadeChange = false; 1664f22ef01cSRoman Divacky 1665f22ef01cSRoman Divacky // Apply the result types to the node, these come from the things in the 1666f22ef01cSRoman Divacky // (outs) list of the instruction. 1667f22ef01cSRoman Divacky // FIXME: Cap at one result so far. 16682754fe60SDimitry Andric unsigned NumResultsToAdd = InstInfo.Operands.NumDefs ? 1 : 0; 1669139f7f9bSDimitry Andric for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo) 1670139f7f9bSDimitry Andric MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP); 1671f22ef01cSRoman Divacky 1672f22ef01cSRoman Divacky // If the instruction has implicit defs, we apply the first one as a result. 1673f22ef01cSRoman Divacky // FIXME: This sucks, it should apply all implicit defs. 1674f22ef01cSRoman Divacky if (!InstInfo.ImplicitDefs.empty()) { 1675f22ef01cSRoman Divacky unsigned ResNo = NumResultsToAdd; 1676f22ef01cSRoman Divacky 1677f22ef01cSRoman Divacky // FIXME: Generalize to multiple possible types and multiple possible 1678f22ef01cSRoman Divacky // ImplicitDefs. 1679f22ef01cSRoman Divacky MVT::SimpleValueType VT = 1680f22ef01cSRoman Divacky InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()); 1681f22ef01cSRoman Divacky 1682f22ef01cSRoman Divacky if (VT != MVT::Other) 1683f22ef01cSRoman Divacky MadeChange |= UpdateNodeType(ResNo, VT, TP); 1684f22ef01cSRoman Divacky } 1685f22ef01cSRoman Divacky 1686f22ef01cSRoman Divacky // If this is an INSERT_SUBREG, constrain the source and destination VTs to 1687f22ef01cSRoman Divacky // be the same. 1688f22ef01cSRoman Divacky if (getOperator()->getName() == "INSERT_SUBREG") { 1689f22ef01cSRoman Divacky assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled"); 1690f22ef01cSRoman Divacky MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP); 1691f22ef01cSRoman Divacky MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP); 1692f22ef01cSRoman Divacky } 1693f22ef01cSRoman Divacky 1694f22ef01cSRoman Divacky unsigned ChildNo = 0; 1695f22ef01cSRoman Divacky for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { 1696f22ef01cSRoman Divacky Record *OperandNode = Inst.getOperand(i); 1697f22ef01cSRoman Divacky 1698f22ef01cSRoman Divacky // If the instruction expects a predicate or optional def operand, we 1699f22ef01cSRoman Divacky // codegen this by setting the operand to it's default value if it has a 1700f22ef01cSRoman Divacky // non-empty DefaultOps field. 17013861d79fSDimitry Andric if (OperandNode->isSubClassOf("OperandWithDefaultOps") && 1702f22ef01cSRoman Divacky !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) 1703f22ef01cSRoman Divacky continue; 1704f22ef01cSRoman Divacky 1705f22ef01cSRoman Divacky // Verify that we didn't run out of provided operands. 17063861d79fSDimitry Andric if (ChildNo >= getNumChildren()) { 1707f22ef01cSRoman Divacky TP.error("Instruction '" + getOperator()->getName() + 1708f22ef01cSRoman Divacky "' expects more operands than were provided."); 17093861d79fSDimitry Andric return false; 17103861d79fSDimitry Andric } 1711f22ef01cSRoman Divacky 1712f22ef01cSRoman Divacky TreePatternNode *Child = getChild(ChildNo++); 1713f22ef01cSRoman Divacky unsigned ChildResNo = 0; // Instructions always use res #0 of their op. 1714f22ef01cSRoman Divacky 1715139f7f9bSDimitry Andric // If the operand has sub-operands, they may be provided by distinct 1716139f7f9bSDimitry Andric // child patterns, so attempt to match each sub-operand separately. 1717139f7f9bSDimitry Andric if (OperandNode->isSubClassOf("Operand")) { 1718139f7f9bSDimitry Andric DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo"); 1719139f7f9bSDimitry Andric if (unsigned NumArgs = MIOpInfo->getNumArgs()) { 1720139f7f9bSDimitry Andric // But don't do that if the whole operand is being provided by 172191bc56edSDimitry Andric // a single ComplexPattern-related Operand. 172291bc56edSDimitry Andric 172391bc56edSDimitry Andric if (Child->getNumMIResults(CDP) < NumArgs) { 1724139f7f9bSDimitry Andric // Match first sub-operand against the child we already have. 1725139f7f9bSDimitry Andric Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef(); 1726139f7f9bSDimitry Andric MadeChange |= 1727139f7f9bSDimitry Andric Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); 1728dff0c46cSDimitry Andric 1729139f7f9bSDimitry Andric // And the remaining sub-operands against subsequent children. 1730139f7f9bSDimitry Andric for (unsigned Arg = 1; Arg < NumArgs; ++Arg) { 1731139f7f9bSDimitry Andric if (ChildNo >= getNumChildren()) { 1732139f7f9bSDimitry Andric TP.error("Instruction '" + getOperator()->getName() + 1733139f7f9bSDimitry Andric "' expects more operands than were provided."); 1734139f7f9bSDimitry Andric return false; 1735139f7f9bSDimitry Andric } 1736139f7f9bSDimitry Andric Child = getChild(ChildNo++); 1737139f7f9bSDimitry Andric 1738139f7f9bSDimitry Andric SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef(); 1739139f7f9bSDimitry Andric MadeChange |= 1740139f7f9bSDimitry Andric Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP); 1741139f7f9bSDimitry Andric } 1742139f7f9bSDimitry Andric continue; 1743139f7f9bSDimitry Andric } 1744139f7f9bSDimitry Andric } 1745139f7f9bSDimitry Andric } 1746139f7f9bSDimitry Andric 1747139f7f9bSDimitry Andric // If we didn't match by pieces above, attempt to match the whole 1748139f7f9bSDimitry Andric // operand now. 1749139f7f9bSDimitry Andric MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP); 1750f22ef01cSRoman Divacky } 1751f22ef01cSRoman Divacky 17523861d79fSDimitry Andric if (ChildNo != getNumChildren()) { 1753f22ef01cSRoman Divacky TP.error("Instruction '" + getOperator()->getName() + 1754f22ef01cSRoman Divacky "' was provided too many operands!"); 17553861d79fSDimitry Andric return false; 17563861d79fSDimitry Andric } 1757f22ef01cSRoman Divacky 1758139f7f9bSDimitry Andric for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1759139f7f9bSDimitry Andric MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 1760f22ef01cSRoman Divacky return MadeChange; 1761f22ef01cSRoman Divacky } 1762f22ef01cSRoman Divacky 176391bc56edSDimitry Andric if (getOperator()->isSubClassOf("ComplexPattern")) { 176491bc56edSDimitry Andric bool MadeChange = false; 176591bc56edSDimitry Andric 176691bc56edSDimitry Andric for (unsigned i = 0; i < getNumChildren(); ++i) 176791bc56edSDimitry Andric MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); 176891bc56edSDimitry Andric 176991bc56edSDimitry Andric return MadeChange; 177091bc56edSDimitry Andric } 177191bc56edSDimitry Andric 1772f22ef01cSRoman Divacky assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); 1773f22ef01cSRoman Divacky 1774f22ef01cSRoman Divacky // Node transforms always take one operand. 17753861d79fSDimitry Andric if (getNumChildren() != 1) { 1776f22ef01cSRoman Divacky TP.error("Node transform '" + getOperator()->getName() + 1777f22ef01cSRoman Divacky "' requires one operand!"); 17783861d79fSDimitry Andric return false; 17793861d79fSDimitry Andric } 1780f22ef01cSRoman Divacky 1781f22ef01cSRoman Divacky bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters); 1782f22ef01cSRoman Divacky 1783f22ef01cSRoman Divacky 1784f22ef01cSRoman Divacky // If either the output or input of the xform does not have exact 1785f22ef01cSRoman Divacky // type info. We assume they must be the same. Otherwise, it is perfectly 1786f22ef01cSRoman Divacky // legal to transform from one type to a completely different type. 1787f22ef01cSRoman Divacky #if 0 1788f22ef01cSRoman Divacky if (!hasTypeSet() || !getChild(0)->hasTypeSet()) { 1789f22ef01cSRoman Divacky bool MadeChange = UpdateNodeType(getChild(0)->getExtType(), TP); 1790f22ef01cSRoman Divacky MadeChange |= getChild(0)->UpdateNodeType(getExtType(), TP); 1791f22ef01cSRoman Divacky return MadeChange; 1792f22ef01cSRoman Divacky } 1793f22ef01cSRoman Divacky #endif 1794f22ef01cSRoman Divacky return MadeChange; 1795f22ef01cSRoman Divacky } 1796f22ef01cSRoman Divacky 1797f22ef01cSRoman Divacky /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the 1798f22ef01cSRoman Divacky /// RHS of a commutative operation, not the on LHS. 1799f22ef01cSRoman Divacky static bool OnlyOnRHSOfCommutative(TreePatternNode *N) { 1800f22ef01cSRoman Divacky if (!N->isLeaf() && N->getOperator()->getName() == "imm") 1801f22ef01cSRoman Divacky return true; 18023861d79fSDimitry Andric if (N->isLeaf() && isa<IntInit>(N->getLeafValue())) 1803f22ef01cSRoman Divacky return true; 1804f22ef01cSRoman Divacky return false; 1805f22ef01cSRoman Divacky } 1806f22ef01cSRoman Divacky 1807f22ef01cSRoman Divacky 1808f22ef01cSRoman Divacky /// canPatternMatch - If it is impossible for this pattern to match on this 1809f22ef01cSRoman Divacky /// target, fill in Reason and return false. Otherwise, return true. This is 1810f22ef01cSRoman Divacky /// used as a sanity check for .td files (to prevent people from writing stuff 1811f22ef01cSRoman Divacky /// that can never possibly work), and to prevent the pattern permuter from 1812f22ef01cSRoman Divacky /// generating stuff that is useless. 1813f22ef01cSRoman Divacky bool TreePatternNode::canPatternMatch(std::string &Reason, 1814f22ef01cSRoman Divacky const CodeGenDAGPatterns &CDP) { 1815f22ef01cSRoman Divacky if (isLeaf()) return true; 1816f22ef01cSRoman Divacky 1817f22ef01cSRoman Divacky for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 1818f22ef01cSRoman Divacky if (!getChild(i)->canPatternMatch(Reason, CDP)) 1819f22ef01cSRoman Divacky return false; 1820f22ef01cSRoman Divacky 1821f22ef01cSRoman Divacky // If this is an intrinsic, handle cases that would make it not match. For 1822f22ef01cSRoman Divacky // example, if an operand is required to be an immediate. 1823f22ef01cSRoman Divacky if (getOperator()->isSubClassOf("Intrinsic")) { 1824f22ef01cSRoman Divacky // TODO: 1825f22ef01cSRoman Divacky return true; 1826f22ef01cSRoman Divacky } 1827f22ef01cSRoman Divacky 182891bc56edSDimitry Andric if (getOperator()->isSubClassOf("ComplexPattern")) 182991bc56edSDimitry Andric return true; 183091bc56edSDimitry Andric 1831f22ef01cSRoman Divacky // If this node is a commutative operator, check that the LHS isn't an 1832f22ef01cSRoman Divacky // immediate. 1833f22ef01cSRoman Divacky const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); 1834f22ef01cSRoman Divacky bool isCommIntrinsic = isCommutativeIntrinsic(CDP); 1835f22ef01cSRoman Divacky if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { 1836f22ef01cSRoman Divacky // Scan all of the operands of the node and make sure that only the last one 1837f22ef01cSRoman Divacky // is a constant node, unless the RHS also is. 1838f22ef01cSRoman Divacky if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { 1839f22ef01cSRoman Divacky bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id. 1840f22ef01cSRoman Divacky for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i) 1841f22ef01cSRoman Divacky if (OnlyOnRHSOfCommutative(getChild(i))) { 1842f22ef01cSRoman Divacky Reason="Immediate value must be on the RHS of commutative operators!"; 1843f22ef01cSRoman Divacky return false; 1844f22ef01cSRoman Divacky } 1845f22ef01cSRoman Divacky } 1846f22ef01cSRoman Divacky } 1847f22ef01cSRoman Divacky 1848f22ef01cSRoman Divacky return true; 1849f22ef01cSRoman Divacky } 1850f22ef01cSRoman Divacky 1851f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 1852f22ef01cSRoman Divacky // TreePattern implementation 1853f22ef01cSRoman Divacky // 1854f22ef01cSRoman Divacky 1855f22ef01cSRoman Divacky TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, 18563861d79fSDimitry Andric CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 18573861d79fSDimitry Andric isInputPattern(isInput), HasError(false) { 1858f22ef01cSRoman Divacky for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) 1859f22ef01cSRoman Divacky Trees.push_back(ParseTreePattern(RawPat->getElement(i), "")); 1860f22ef01cSRoman Divacky } 1861f22ef01cSRoman Divacky 1862f22ef01cSRoman Divacky TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, 18633861d79fSDimitry Andric CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 18643861d79fSDimitry Andric isInputPattern(isInput), HasError(false) { 1865f22ef01cSRoman Divacky Trees.push_back(ParseTreePattern(Pat, "")); 1866f22ef01cSRoman Divacky } 1867f22ef01cSRoman Divacky 1868f22ef01cSRoman Divacky TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, 18693861d79fSDimitry Andric CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp), 18703861d79fSDimitry Andric isInputPattern(isInput), HasError(false) { 1871f22ef01cSRoman Divacky Trees.push_back(Pat); 1872f22ef01cSRoman Divacky } 1873f22ef01cSRoman Divacky 18743861d79fSDimitry Andric void TreePattern::error(const std::string &Msg) { 18753861d79fSDimitry Andric if (HasError) 18763861d79fSDimitry Andric return; 1877f22ef01cSRoman Divacky dump(); 18783861d79fSDimitry Andric PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg); 18793861d79fSDimitry Andric HasError = true; 1880f22ef01cSRoman Divacky } 1881f22ef01cSRoman Divacky 1882f22ef01cSRoman Divacky void TreePattern::ComputeNamedNodes() { 1883f22ef01cSRoman Divacky for (unsigned i = 0, e = Trees.size(); i != e; ++i) 1884f22ef01cSRoman Divacky ComputeNamedNodes(Trees[i]); 1885f22ef01cSRoman Divacky } 1886f22ef01cSRoman Divacky 1887f22ef01cSRoman Divacky void TreePattern::ComputeNamedNodes(TreePatternNode *N) { 1888f22ef01cSRoman Divacky if (!N->getName().empty()) 1889f22ef01cSRoman Divacky NamedNodes[N->getName()].push_back(N); 1890f22ef01cSRoman Divacky 1891f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 1892f22ef01cSRoman Divacky ComputeNamedNodes(N->getChild(i)); 1893f22ef01cSRoman Divacky } 1894f22ef01cSRoman Divacky 1895f22ef01cSRoman Divacky 1896f22ef01cSRoman Divacky TreePatternNode *TreePattern::ParseTreePattern(Init *TheInit, StringRef OpName){ 18973861d79fSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(TheInit)) { 1898f22ef01cSRoman Divacky Record *R = DI->getDef(); 1899f22ef01cSRoman Divacky 1900f22ef01cSRoman Divacky // Direct reference to a leaf DagNode or PatFrag? Turn it into a 190117a519f9SDimitry Andric // TreePatternNode of its own. For example: 1902f22ef01cSRoman Divacky /// (foo GPR, imm) -> (foo GPR, (imm)) 1903f22ef01cSRoman Divacky if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) 19046122f3e6SDimitry Andric return ParseTreePattern( 19056122f3e6SDimitry Andric DagInit::get(DI, "", 1906f22ef01cSRoman Divacky std::vector<std::pair<Init*, std::string> >()), 1907f22ef01cSRoman Divacky OpName); 1908f22ef01cSRoman Divacky 1909f22ef01cSRoman Divacky // Input argument? 1910f22ef01cSRoman Divacky TreePatternNode *Res = new TreePatternNode(DI, 1); 1911f22ef01cSRoman Divacky if (R->getName() == "node" && !OpName.empty()) { 1912f22ef01cSRoman Divacky if (OpName.empty()) 1913f22ef01cSRoman Divacky error("'node' argument requires a name to match with operand list"); 1914f22ef01cSRoman Divacky Args.push_back(OpName); 1915f22ef01cSRoman Divacky } 1916f22ef01cSRoman Divacky 1917f22ef01cSRoman Divacky Res->setName(OpName); 1918f22ef01cSRoman Divacky return Res; 1919f22ef01cSRoman Divacky } 1920f22ef01cSRoman Divacky 1921139f7f9bSDimitry Andric // ?:$name or just $name. 1922139f7f9bSDimitry Andric if (TheInit == UnsetInit::get()) { 1923139f7f9bSDimitry Andric if (OpName.empty()) 1924139f7f9bSDimitry Andric error("'?' argument requires a name to match with operand list"); 1925139f7f9bSDimitry Andric TreePatternNode *Res = new TreePatternNode(TheInit, 1); 1926139f7f9bSDimitry Andric Args.push_back(OpName); 1927139f7f9bSDimitry Andric Res->setName(OpName); 1928139f7f9bSDimitry Andric return Res; 1929139f7f9bSDimitry Andric } 1930139f7f9bSDimitry Andric 19313861d79fSDimitry Andric if (IntInit *II = dyn_cast<IntInit>(TheInit)) { 1932f22ef01cSRoman Divacky if (!OpName.empty()) 1933f22ef01cSRoman Divacky error("Constant int argument should not have a name!"); 1934f22ef01cSRoman Divacky return new TreePatternNode(II, 1); 1935f22ef01cSRoman Divacky } 1936f22ef01cSRoman Divacky 19373861d79fSDimitry Andric if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) { 1938f22ef01cSRoman Divacky // Turn this into an IntInit. 19396122f3e6SDimitry Andric Init *II = BI->convertInitializerTo(IntRecTy::get()); 194091bc56edSDimitry Andric if (!II || !isa<IntInit>(II)) 1941f22ef01cSRoman Divacky error("Bits value must be constants!"); 1942f22ef01cSRoman Divacky return ParseTreePattern(II, OpName); 1943f22ef01cSRoman Divacky } 1944f22ef01cSRoman Divacky 19453861d79fSDimitry Andric DagInit *Dag = dyn_cast<DagInit>(TheInit); 1946f22ef01cSRoman Divacky if (!Dag) { 1947f22ef01cSRoman Divacky TheInit->dump(); 1948f22ef01cSRoman Divacky error("Pattern has unexpected init kind!"); 1949f22ef01cSRoman Divacky } 19503861d79fSDimitry Andric DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator()); 1951f22ef01cSRoman Divacky if (!OpDef) error("Pattern has unexpected operator type!"); 1952f22ef01cSRoman Divacky Record *Operator = OpDef->getDef(); 1953f22ef01cSRoman Divacky 1954f22ef01cSRoman Divacky if (Operator->isSubClassOf("ValueType")) { 1955f22ef01cSRoman Divacky // If the operator is a ValueType, then this must be "type cast" of a leaf 1956f22ef01cSRoman Divacky // node. 1957f22ef01cSRoman Divacky if (Dag->getNumArgs() != 1) 1958f22ef01cSRoman Divacky error("Type cast only takes one operand!"); 1959f22ef01cSRoman Divacky 1960f22ef01cSRoman Divacky TreePatternNode *New = ParseTreePattern(Dag->getArg(0), Dag->getArgName(0)); 1961f22ef01cSRoman Divacky 1962f22ef01cSRoman Divacky // Apply the type cast. 1963f22ef01cSRoman Divacky assert(New->getNumTypes() == 1 && "FIXME: Unhandled"); 1964f22ef01cSRoman Divacky New->UpdateNodeType(0, getValueType(Operator), *this); 1965f22ef01cSRoman Divacky 1966f22ef01cSRoman Divacky if (!OpName.empty()) 1967f22ef01cSRoman Divacky error("ValueType cast should not have a name!"); 1968f22ef01cSRoman Divacky return New; 1969f22ef01cSRoman Divacky } 1970f22ef01cSRoman Divacky 1971f22ef01cSRoman Divacky // Verify that this is something that makes sense for an operator. 1972f22ef01cSRoman Divacky if (!Operator->isSubClassOf("PatFrag") && 1973f22ef01cSRoman Divacky !Operator->isSubClassOf("SDNode") && 1974f22ef01cSRoman Divacky !Operator->isSubClassOf("Instruction") && 1975f22ef01cSRoman Divacky !Operator->isSubClassOf("SDNodeXForm") && 1976f22ef01cSRoman Divacky !Operator->isSubClassOf("Intrinsic") && 197791bc56edSDimitry Andric !Operator->isSubClassOf("ComplexPattern") && 1978f22ef01cSRoman Divacky Operator->getName() != "set" && 1979f22ef01cSRoman Divacky Operator->getName() != "implicit") 1980f22ef01cSRoman Divacky error("Unrecognized node '" + Operator->getName() + "'!"); 1981f22ef01cSRoman Divacky 1982f22ef01cSRoman Divacky // Check to see if this is something that is illegal in an input pattern. 1983f22ef01cSRoman Divacky if (isInputPattern) { 1984f22ef01cSRoman Divacky if (Operator->isSubClassOf("Instruction") || 1985f22ef01cSRoman Divacky Operator->isSubClassOf("SDNodeXForm")) 1986f22ef01cSRoman Divacky error("Cannot use '" + Operator->getName() + "' in an input pattern!"); 1987f22ef01cSRoman Divacky } else { 1988f22ef01cSRoman Divacky if (Operator->isSubClassOf("Intrinsic")) 1989f22ef01cSRoman Divacky error("Cannot use '" + Operator->getName() + "' in an output pattern!"); 1990f22ef01cSRoman Divacky 1991f22ef01cSRoman Divacky if (Operator->isSubClassOf("SDNode") && 1992f22ef01cSRoman Divacky Operator->getName() != "imm" && 1993f22ef01cSRoman Divacky Operator->getName() != "fpimm" && 1994f22ef01cSRoman Divacky Operator->getName() != "tglobaltlsaddr" && 1995f22ef01cSRoman Divacky Operator->getName() != "tconstpool" && 1996f22ef01cSRoman Divacky Operator->getName() != "tjumptable" && 1997f22ef01cSRoman Divacky Operator->getName() != "tframeindex" && 1998f22ef01cSRoman Divacky Operator->getName() != "texternalsym" && 1999f22ef01cSRoman Divacky Operator->getName() != "tblockaddress" && 2000f22ef01cSRoman Divacky Operator->getName() != "tglobaladdr" && 2001f22ef01cSRoman Divacky Operator->getName() != "bb" && 2002f22ef01cSRoman Divacky Operator->getName() != "vt") 2003f22ef01cSRoman Divacky error("Cannot use '" + Operator->getName() + "' in an output pattern!"); 2004f22ef01cSRoman Divacky } 2005f22ef01cSRoman Divacky 2006f22ef01cSRoman Divacky std::vector<TreePatternNode*> Children; 2007f22ef01cSRoman Divacky 2008f22ef01cSRoman Divacky // Parse all the operands. 2009f22ef01cSRoman Divacky for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) 2010f22ef01cSRoman Divacky Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgName(i))); 2011f22ef01cSRoman Divacky 2012f22ef01cSRoman Divacky // If the operator is an intrinsic, then this is just syntactic sugar for for 2013f22ef01cSRoman Divacky // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and 2014f22ef01cSRoman Divacky // convert the intrinsic name to a number. 2015f22ef01cSRoman Divacky if (Operator->isSubClassOf("Intrinsic")) { 2016f22ef01cSRoman Divacky const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator); 2017f22ef01cSRoman Divacky unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1; 2018f22ef01cSRoman Divacky 2019f22ef01cSRoman Divacky // If this intrinsic returns void, it must have side-effects and thus a 2020f22ef01cSRoman Divacky // chain. 2021f22ef01cSRoman Divacky if (Int.IS.RetVTs.empty()) 2022f22ef01cSRoman Divacky Operator = getDAGPatterns().get_intrinsic_void_sdnode(); 2023f22ef01cSRoman Divacky else if (Int.ModRef != CodeGenIntrinsic::NoMem) 2024f22ef01cSRoman Divacky // Has side-effects, requires chain. 2025f22ef01cSRoman Divacky Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); 2026f22ef01cSRoman Divacky else // Otherwise, no chain. 2027f22ef01cSRoman Divacky Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); 2028f22ef01cSRoman Divacky 20296122f3e6SDimitry Andric TreePatternNode *IIDNode = new TreePatternNode(IntInit::get(IID), 1); 2030f22ef01cSRoman Divacky Children.insert(Children.begin(), IIDNode); 2031f22ef01cSRoman Divacky } 2032f22ef01cSRoman Divacky 203391bc56edSDimitry Andric if (Operator->isSubClassOf("ComplexPattern")) { 203491bc56edSDimitry Andric for (unsigned i = 0; i < Children.size(); ++i) { 203591bc56edSDimitry Andric TreePatternNode *Child = Children[i]; 203691bc56edSDimitry Andric 203791bc56edSDimitry Andric if (Child->getName().empty()) 203891bc56edSDimitry Andric error("All arguments to a ComplexPattern must be named"); 203991bc56edSDimitry Andric 204091bc56edSDimitry Andric // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)" 204191bc56edSDimitry Andric // and "(MY_PAT $b, $a)" should not be allowed in the same pattern; 204291bc56edSDimitry Andric // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)". 204391bc56edSDimitry Andric auto OperandId = std::make_pair(Operator, i); 204491bc56edSDimitry Andric auto PrevOp = ComplexPatternOperands.find(Child->getName()); 204591bc56edSDimitry Andric if (PrevOp != ComplexPatternOperands.end()) { 204691bc56edSDimitry Andric if (PrevOp->getValue() != OperandId) 204791bc56edSDimitry Andric error("All ComplexPattern operands must appear consistently: " 204891bc56edSDimitry Andric "in the same order in just one ComplexPattern instance."); 204991bc56edSDimitry Andric } else 205091bc56edSDimitry Andric ComplexPatternOperands[Child->getName()] = OperandId; 205191bc56edSDimitry Andric } 205291bc56edSDimitry Andric } 205391bc56edSDimitry Andric 2054f22ef01cSRoman Divacky unsigned NumResults = GetNumNodeResults(Operator, CDP); 2055f22ef01cSRoman Divacky TreePatternNode *Result = new TreePatternNode(Operator, Children, NumResults); 2056f22ef01cSRoman Divacky Result->setName(OpName); 2057f22ef01cSRoman Divacky 2058f22ef01cSRoman Divacky if (!Dag->getName().empty()) { 2059f22ef01cSRoman Divacky assert(Result->getName().empty()); 2060f22ef01cSRoman Divacky Result->setName(Dag->getName()); 2061f22ef01cSRoman Divacky } 2062f22ef01cSRoman Divacky return Result; 2063f22ef01cSRoman Divacky } 2064f22ef01cSRoman Divacky 2065f22ef01cSRoman Divacky /// SimplifyTree - See if we can simplify this tree to eliminate something that 2066f22ef01cSRoman Divacky /// will never match in favor of something obvious that will. This is here 2067f22ef01cSRoman Divacky /// strictly as a convenience to target authors because it allows them to write 2068f22ef01cSRoman Divacky /// more type generic things and have useless type casts fold away. 2069f22ef01cSRoman Divacky /// 2070f22ef01cSRoman Divacky /// This returns true if any change is made. 2071f22ef01cSRoman Divacky static bool SimplifyTree(TreePatternNode *&N) { 2072f22ef01cSRoman Divacky if (N->isLeaf()) 2073f22ef01cSRoman Divacky return false; 2074f22ef01cSRoman Divacky 2075f22ef01cSRoman Divacky // If we have a bitconvert with a resolved type and if the source and 2076f22ef01cSRoman Divacky // destination types are the same, then the bitconvert is useless, remove it. 2077f22ef01cSRoman Divacky if (N->getOperator()->getName() == "bitconvert" && 2078f22ef01cSRoman Divacky N->getExtType(0).isConcrete() && 2079f22ef01cSRoman Divacky N->getExtType(0) == N->getChild(0)->getExtType(0) && 2080f22ef01cSRoman Divacky N->getName().empty()) { 2081f22ef01cSRoman Divacky N = N->getChild(0); 2082f22ef01cSRoman Divacky SimplifyTree(N); 2083f22ef01cSRoman Divacky return true; 2084f22ef01cSRoman Divacky } 2085f22ef01cSRoman Divacky 2086f22ef01cSRoman Divacky // Walk all children. 2087f22ef01cSRoman Divacky bool MadeChange = false; 2088f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { 2089f22ef01cSRoman Divacky TreePatternNode *Child = N->getChild(i); 2090f22ef01cSRoman Divacky MadeChange |= SimplifyTree(Child); 2091f22ef01cSRoman Divacky N->setChild(i, Child); 2092f22ef01cSRoman Divacky } 2093f22ef01cSRoman Divacky return MadeChange; 2094f22ef01cSRoman Divacky } 2095f22ef01cSRoman Divacky 2096f22ef01cSRoman Divacky 2097f22ef01cSRoman Divacky 2098f22ef01cSRoman Divacky /// InferAllTypes - Infer/propagate as many types throughout the expression 2099f22ef01cSRoman Divacky /// patterns as possible. Return true if all types are inferred, false 21003861d79fSDimitry Andric /// otherwise. Flags an error if a type contradiction is found. 2101f22ef01cSRoman Divacky bool TreePattern:: 2102f22ef01cSRoman Divacky InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) { 2103f22ef01cSRoman Divacky if (NamedNodes.empty()) 2104f22ef01cSRoman Divacky ComputeNamedNodes(); 2105f22ef01cSRoman Divacky 2106f22ef01cSRoman Divacky bool MadeChange = true; 2107f22ef01cSRoman Divacky while (MadeChange) { 2108f22ef01cSRoman Divacky MadeChange = false; 2109f22ef01cSRoman Divacky for (unsigned i = 0, e = Trees.size(); i != e; ++i) { 2110f22ef01cSRoman Divacky MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); 2111f22ef01cSRoman Divacky MadeChange |= SimplifyTree(Trees[i]); 2112f22ef01cSRoman Divacky } 2113f22ef01cSRoman Divacky 2114f22ef01cSRoman Divacky // If there are constraints on our named nodes, apply them. 2115f22ef01cSRoman Divacky for (StringMap<SmallVector<TreePatternNode*,1> >::iterator 2116f22ef01cSRoman Divacky I = NamedNodes.begin(), E = NamedNodes.end(); I != E; ++I) { 2117f22ef01cSRoman Divacky SmallVectorImpl<TreePatternNode*> &Nodes = I->second; 2118f22ef01cSRoman Divacky 2119f22ef01cSRoman Divacky // If we have input named node types, propagate their types to the named 2120f22ef01cSRoman Divacky // values here. 2121f22ef01cSRoman Divacky if (InNamedTypes) { 212291bc56edSDimitry Andric if (!InNamedTypes->count(I->getKey())) { 212391bc56edSDimitry Andric error("Node '" + std::string(I->getKey()) + 212491bc56edSDimitry Andric "' in output pattern but not input pattern"); 212591bc56edSDimitry Andric return true; 212691bc56edSDimitry Andric } 2127f22ef01cSRoman Divacky 2128f22ef01cSRoman Divacky const SmallVectorImpl<TreePatternNode*> &InNodes = 2129f22ef01cSRoman Divacky InNamedTypes->find(I->getKey())->second; 2130f22ef01cSRoman Divacky 2131f22ef01cSRoman Divacky // The input types should be fully resolved by now. 2132f22ef01cSRoman Divacky for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 2133f22ef01cSRoman Divacky // If this node is a register class, and it is the root of the pattern 2134f22ef01cSRoman Divacky // then we're mapping something onto an input register. We allow 2135f22ef01cSRoman Divacky // changing the type of the input register in this case. This allows 2136f22ef01cSRoman Divacky // us to match things like: 2137f22ef01cSRoman Divacky // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>; 2138f22ef01cSRoman Divacky if (Nodes[i] == Trees[0] && Nodes[i]->isLeaf()) { 21393861d79fSDimitry Andric DefInit *DI = dyn_cast<DefInit>(Nodes[i]->getLeafValue()); 214017a519f9SDimitry Andric if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || 214117a519f9SDimitry Andric DI->getDef()->isSubClassOf("RegisterOperand"))) 2142f22ef01cSRoman Divacky continue; 2143f22ef01cSRoman Divacky } 2144f22ef01cSRoman Divacky 2145f22ef01cSRoman Divacky assert(Nodes[i]->getNumTypes() == 1 && 2146f22ef01cSRoman Divacky InNodes[0]->getNumTypes() == 1 && 2147f22ef01cSRoman Divacky "FIXME: cannot name multiple result nodes yet"); 2148f22ef01cSRoman Divacky MadeChange |= Nodes[i]->UpdateNodeType(0, InNodes[0]->getExtType(0), 2149f22ef01cSRoman Divacky *this); 2150f22ef01cSRoman Divacky } 2151f22ef01cSRoman Divacky } 2152f22ef01cSRoman Divacky 2153f22ef01cSRoman Divacky // If there are multiple nodes with the same name, they must all have the 2154f22ef01cSRoman Divacky // same type. 2155f22ef01cSRoman Divacky if (I->second.size() > 1) { 2156f22ef01cSRoman Divacky for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) { 2157f22ef01cSRoman Divacky TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1]; 2158f22ef01cSRoman Divacky assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 && 2159f22ef01cSRoman Divacky "FIXME: cannot name multiple result nodes yet"); 2160f22ef01cSRoman Divacky 2161f22ef01cSRoman Divacky MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this); 2162f22ef01cSRoman Divacky MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this); 2163f22ef01cSRoman Divacky } 2164f22ef01cSRoman Divacky } 2165f22ef01cSRoman Divacky } 2166f22ef01cSRoman Divacky } 2167f22ef01cSRoman Divacky 2168f22ef01cSRoman Divacky bool HasUnresolvedTypes = false; 2169f22ef01cSRoman Divacky for (unsigned i = 0, e = Trees.size(); i != e; ++i) 2170f22ef01cSRoman Divacky HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); 2171f22ef01cSRoman Divacky return !HasUnresolvedTypes; 2172f22ef01cSRoman Divacky } 2173f22ef01cSRoman Divacky 2174f22ef01cSRoman Divacky void TreePattern::print(raw_ostream &OS) const { 2175f22ef01cSRoman Divacky OS << getRecord()->getName(); 2176f22ef01cSRoman Divacky if (!Args.empty()) { 2177f22ef01cSRoman Divacky OS << "(" << Args[0]; 2178f22ef01cSRoman Divacky for (unsigned i = 1, e = Args.size(); i != e; ++i) 2179f22ef01cSRoman Divacky OS << ", " << Args[i]; 2180f22ef01cSRoman Divacky OS << ")"; 2181f22ef01cSRoman Divacky } 2182f22ef01cSRoman Divacky OS << ": "; 2183f22ef01cSRoman Divacky 2184f22ef01cSRoman Divacky if (Trees.size() > 1) 2185f22ef01cSRoman Divacky OS << "[\n"; 2186f22ef01cSRoman Divacky for (unsigned i = 0, e = Trees.size(); i != e; ++i) { 2187f22ef01cSRoman Divacky OS << "\t"; 2188f22ef01cSRoman Divacky Trees[i]->print(OS); 2189f22ef01cSRoman Divacky OS << "\n"; 2190f22ef01cSRoman Divacky } 2191f22ef01cSRoman Divacky 2192f22ef01cSRoman Divacky if (Trees.size() > 1) 2193f22ef01cSRoman Divacky OS << "]\n"; 2194f22ef01cSRoman Divacky } 2195f22ef01cSRoman Divacky 2196f22ef01cSRoman Divacky void TreePattern::dump() const { print(errs()); } 2197f22ef01cSRoman Divacky 2198f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2199f22ef01cSRoman Divacky // CodeGenDAGPatterns implementation 2200f22ef01cSRoman Divacky // 2201f22ef01cSRoman Divacky 22022754fe60SDimitry Andric CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : 22032754fe60SDimitry Andric Records(R), Target(R) { 22042754fe60SDimitry Andric 2205f22ef01cSRoman Divacky Intrinsics = LoadIntrinsics(Records, false); 2206f22ef01cSRoman Divacky TgtIntrinsics = LoadIntrinsics(Records, true); 2207f22ef01cSRoman Divacky ParseNodeInfo(); 2208f22ef01cSRoman Divacky ParseNodeTransforms(); 2209f22ef01cSRoman Divacky ParseComplexPatterns(); 2210f22ef01cSRoman Divacky ParsePatternFragments(); 2211f22ef01cSRoman Divacky ParseDefaultOperands(); 2212f22ef01cSRoman Divacky ParseInstructions(); 221391bc56edSDimitry Andric ParsePatternFragments(/*OutFrags*/true); 2214f22ef01cSRoman Divacky ParsePatterns(); 2215f22ef01cSRoman Divacky 2216f22ef01cSRoman Divacky // Generate variants. For example, commutative patterns can match 2217f22ef01cSRoman Divacky // multiple ways. Add them to PatternsToMatch as well. 2218f22ef01cSRoman Divacky GenerateVariants(); 2219f22ef01cSRoman Divacky 2220f22ef01cSRoman Divacky // Infer instruction flags. For example, we can detect loads, 2221f22ef01cSRoman Divacky // stores, and side effects in many cases by examining an 2222f22ef01cSRoman Divacky // instruction's pattern. 2223f22ef01cSRoman Divacky InferInstructionFlags(); 22243861d79fSDimitry Andric 22253861d79fSDimitry Andric // Verify that instruction flags match the patterns. 22263861d79fSDimitry Andric VerifyInstructionFlags(); 2227f22ef01cSRoman Divacky } 2228f22ef01cSRoman Divacky 2229f22ef01cSRoman Divacky CodeGenDAGPatterns::~CodeGenDAGPatterns() { 2230f22ef01cSRoman Divacky for (pf_iterator I = PatternFragments.begin(), 2231f22ef01cSRoman Divacky E = PatternFragments.end(); I != E; ++I) 2232f22ef01cSRoman Divacky delete I->second; 2233f22ef01cSRoman Divacky } 2234f22ef01cSRoman Divacky 2235f22ef01cSRoman Divacky 2236f22ef01cSRoman Divacky Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const { 2237f22ef01cSRoman Divacky Record *N = Records.getDef(Name); 2238f22ef01cSRoman Divacky if (!N || !N->isSubClassOf("SDNode")) { 2239f22ef01cSRoman Divacky errs() << "Error getting SDNode '" << Name << "'!\n"; 2240f22ef01cSRoman Divacky exit(1); 2241f22ef01cSRoman Divacky } 2242f22ef01cSRoman Divacky return N; 2243f22ef01cSRoman Divacky } 2244f22ef01cSRoman Divacky 2245f22ef01cSRoman Divacky // Parse all of the SDNode definitions for the target, populating SDNodes. 2246f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseNodeInfo() { 2247f22ef01cSRoman Divacky std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); 2248f22ef01cSRoman Divacky while (!Nodes.empty()) { 2249f22ef01cSRoman Divacky SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); 2250f22ef01cSRoman Divacky Nodes.pop_back(); 2251f22ef01cSRoman Divacky } 2252f22ef01cSRoman Divacky 2253f22ef01cSRoman Divacky // Get the builtin intrinsic nodes. 2254f22ef01cSRoman Divacky intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); 2255f22ef01cSRoman Divacky intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); 2256f22ef01cSRoman Divacky intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); 2257f22ef01cSRoman Divacky } 2258f22ef01cSRoman Divacky 2259f22ef01cSRoman Divacky /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms 2260f22ef01cSRoman Divacky /// map, and emit them to the file as functions. 2261f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseNodeTransforms() { 2262f22ef01cSRoman Divacky std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); 2263f22ef01cSRoman Divacky while (!Xforms.empty()) { 2264f22ef01cSRoman Divacky Record *XFormNode = Xforms.back(); 2265f22ef01cSRoman Divacky Record *SDNode = XFormNode->getValueAsDef("Opcode"); 2266dff0c46cSDimitry Andric std::string Code = XFormNode->getValueAsString("XFormFunction"); 2267f22ef01cSRoman Divacky SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code))); 2268f22ef01cSRoman Divacky 2269f22ef01cSRoman Divacky Xforms.pop_back(); 2270f22ef01cSRoman Divacky } 2271f22ef01cSRoman Divacky } 2272f22ef01cSRoman Divacky 2273f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseComplexPatterns() { 2274f22ef01cSRoman Divacky std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); 2275f22ef01cSRoman Divacky while (!AMs.empty()) { 2276f22ef01cSRoman Divacky ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); 2277f22ef01cSRoman Divacky AMs.pop_back(); 2278f22ef01cSRoman Divacky } 2279f22ef01cSRoman Divacky } 2280f22ef01cSRoman Divacky 2281f22ef01cSRoman Divacky 2282f22ef01cSRoman Divacky /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td 2283f22ef01cSRoman Divacky /// file, building up the PatternFragments map. After we've collected them all, 2284f22ef01cSRoman Divacky /// inline fragments together as necessary, so that there are no references left 2285f22ef01cSRoman Divacky /// inside a pattern fragment to a pattern fragment. 2286f22ef01cSRoman Divacky /// 228791bc56edSDimitry Andric void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) { 2288f22ef01cSRoman Divacky std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); 2289f22ef01cSRoman Divacky 2290f22ef01cSRoman Divacky // First step, parse all of the fragments. 2291f22ef01cSRoman Divacky for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { 229291bc56edSDimitry Andric if (OutFrags != Fragments[i]->isSubClassOf("OutPatFrag")) 229391bc56edSDimitry Andric continue; 229491bc56edSDimitry Andric 2295f22ef01cSRoman Divacky DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); 229691bc56edSDimitry Andric TreePattern *P = 229791bc56edSDimitry Andric new TreePattern(Fragments[i], Tree, 229891bc56edSDimitry Andric !Fragments[i]->isSubClassOf("OutPatFrag"), *this); 2299f22ef01cSRoman Divacky PatternFragments[Fragments[i]] = P; 2300f22ef01cSRoman Divacky 2301f22ef01cSRoman Divacky // Validate the argument list, converting it to set, to discard duplicates. 2302f22ef01cSRoman Divacky std::vector<std::string> &Args = P->getArgList(); 2303f22ef01cSRoman Divacky std::set<std::string> OperandsSet(Args.begin(), Args.end()); 2304f22ef01cSRoman Divacky 2305f22ef01cSRoman Divacky if (OperandsSet.count("")) 2306f22ef01cSRoman Divacky P->error("Cannot have unnamed 'node' values in pattern fragment!"); 2307f22ef01cSRoman Divacky 2308f22ef01cSRoman Divacky // Parse the operands list. 2309f22ef01cSRoman Divacky DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); 23103861d79fSDimitry Andric DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator()); 2311f22ef01cSRoman Divacky // Special cases: ops == outs == ins. Different names are used to 2312f22ef01cSRoman Divacky // improve readability. 2313f22ef01cSRoman Divacky if (!OpsOp || 2314f22ef01cSRoman Divacky (OpsOp->getDef()->getName() != "ops" && 2315f22ef01cSRoman Divacky OpsOp->getDef()->getName() != "outs" && 2316f22ef01cSRoman Divacky OpsOp->getDef()->getName() != "ins")) 2317f22ef01cSRoman Divacky P->error("Operands list should start with '(ops ... '!"); 2318f22ef01cSRoman Divacky 2319f22ef01cSRoman Divacky // Copy over the arguments. 2320f22ef01cSRoman Divacky Args.clear(); 2321f22ef01cSRoman Divacky for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { 23223861d79fSDimitry Andric if (!isa<DefInit>(OpsList->getArg(j)) || 23233861d79fSDimitry Andric cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node") 2324f22ef01cSRoman Divacky P->error("Operands list should all be 'node' values."); 2325f22ef01cSRoman Divacky if (OpsList->getArgName(j).empty()) 2326f22ef01cSRoman Divacky P->error("Operands list should have names for each operand!"); 2327f22ef01cSRoman Divacky if (!OperandsSet.count(OpsList->getArgName(j))) 2328f22ef01cSRoman Divacky P->error("'" + OpsList->getArgName(j) + 2329f22ef01cSRoman Divacky "' does not occur in pattern or was multiply specified!"); 2330f22ef01cSRoman Divacky OperandsSet.erase(OpsList->getArgName(j)); 2331f22ef01cSRoman Divacky Args.push_back(OpsList->getArgName(j)); 2332f22ef01cSRoman Divacky } 2333f22ef01cSRoman Divacky 2334f22ef01cSRoman Divacky if (!OperandsSet.empty()) 2335f22ef01cSRoman Divacky P->error("Operands list does not contain an entry for operand '" + 2336f22ef01cSRoman Divacky *OperandsSet.begin() + "'!"); 2337f22ef01cSRoman Divacky 2338f22ef01cSRoman Divacky // If there is a code init for this fragment, keep track of the fact that 2339f22ef01cSRoman Divacky // this fragment uses it. 23403b0f4066SDimitry Andric TreePredicateFn PredFn(P); 23413b0f4066SDimitry Andric if (!PredFn.isAlwaysTrue()) 23423b0f4066SDimitry Andric P->getOnlyTree()->addPredicateFn(PredFn); 2343f22ef01cSRoman Divacky 2344f22ef01cSRoman Divacky // If there is a node transformation corresponding to this, keep track of 2345f22ef01cSRoman Divacky // it. 2346f22ef01cSRoman Divacky Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); 2347f22ef01cSRoman Divacky if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? 2348f22ef01cSRoman Divacky P->getOnlyTree()->setTransformFn(Transform); 2349f22ef01cSRoman Divacky } 2350f22ef01cSRoman Divacky 2351f22ef01cSRoman Divacky // Now that we've parsed all of the tree fragments, do a closure on them so 2352f22ef01cSRoman Divacky // that there are not references to PatFrags left inside of them. 2353f22ef01cSRoman Divacky for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { 235491bc56edSDimitry Andric if (OutFrags != Fragments[i]->isSubClassOf("OutPatFrag")) 235591bc56edSDimitry Andric continue; 235691bc56edSDimitry Andric 2357f22ef01cSRoman Divacky TreePattern *ThePat = PatternFragments[Fragments[i]]; 2358f22ef01cSRoman Divacky ThePat->InlinePatternFragments(); 2359f22ef01cSRoman Divacky 2360f22ef01cSRoman Divacky // Infer as many types as possible. Don't worry about it if we don't infer 2361f22ef01cSRoman Divacky // all of them, some may depend on the inputs of the pattern. 2362f22ef01cSRoman Divacky ThePat->InferAllTypes(); 23633861d79fSDimitry Andric ThePat->resetError(); 2364f22ef01cSRoman Divacky 2365f22ef01cSRoman Divacky // If debugging, print out the pattern fragment result. 2366f22ef01cSRoman Divacky DEBUG(ThePat->dump()); 2367f22ef01cSRoman Divacky } 2368f22ef01cSRoman Divacky } 2369f22ef01cSRoman Divacky 2370f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParseDefaultOperands() { 23713861d79fSDimitry Andric std::vector<Record*> DefaultOps; 23723861d79fSDimitry Andric DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps"); 2373f22ef01cSRoman Divacky 2374f22ef01cSRoman Divacky // Find some SDNode. 2375f22ef01cSRoman Divacky assert(!SDNodes.empty() && "No SDNodes parsed?"); 23766122f3e6SDimitry Andric Init *SomeSDNode = DefInit::get(SDNodes.begin()->first); 2377f22ef01cSRoman Divacky 23783861d79fSDimitry Andric for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) { 23793861d79fSDimitry Andric DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps"); 2380f22ef01cSRoman Divacky 2381f22ef01cSRoman Divacky // Clone the DefaultInfo dag node, changing the operator from 'ops' to 2382f22ef01cSRoman Divacky // SomeSDnode so that we can parse this. 2383f22ef01cSRoman Divacky std::vector<std::pair<Init*, std::string> > Ops; 2384f22ef01cSRoman Divacky for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) 2385f22ef01cSRoman Divacky Ops.push_back(std::make_pair(DefaultInfo->getArg(op), 2386f22ef01cSRoman Divacky DefaultInfo->getArgName(op))); 23876122f3e6SDimitry Andric DagInit *DI = DagInit::get(SomeSDNode, "", Ops); 2388f22ef01cSRoman Divacky 2389f22ef01cSRoman Divacky // Create a TreePattern to parse this. 23903861d79fSDimitry Andric TreePattern P(DefaultOps[i], DI, false, *this); 2391f22ef01cSRoman Divacky assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!"); 2392f22ef01cSRoman Divacky 2393f22ef01cSRoman Divacky // Copy the operands over into a DAGDefaultOperand. 2394f22ef01cSRoman Divacky DAGDefaultOperand DefaultOpInfo; 2395f22ef01cSRoman Divacky 2396f22ef01cSRoman Divacky TreePatternNode *T = P.getTree(0); 2397f22ef01cSRoman Divacky for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) { 2398f22ef01cSRoman Divacky TreePatternNode *TPN = T->getChild(op); 2399f22ef01cSRoman Divacky while (TPN->ApplyTypeConstraints(P, false)) 2400f22ef01cSRoman Divacky /* Resolve all types */; 2401f22ef01cSRoman Divacky 2402f22ef01cSRoman Divacky if (TPN->ContainsUnresolvedType()) { 240391bc56edSDimitry Andric PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" + 240491bc56edSDimitry Andric DefaultOps[i]->getName() + 240591bc56edSDimitry Andric "' doesn't have a concrete type!"); 2406f22ef01cSRoman Divacky } 2407f22ef01cSRoman Divacky DefaultOpInfo.DefaultOps.push_back(TPN); 2408f22ef01cSRoman Divacky } 2409f22ef01cSRoman Divacky 2410f22ef01cSRoman Divacky // Insert it into the DefaultOperands map so we can find it later. 24113861d79fSDimitry Andric DefaultOperands[DefaultOps[i]] = DefaultOpInfo; 2412f22ef01cSRoman Divacky } 2413f22ef01cSRoman Divacky } 2414f22ef01cSRoman Divacky 2415f22ef01cSRoman Divacky /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an 2416f22ef01cSRoman Divacky /// instruction input. Return true if this is a real use. 2417f22ef01cSRoman Divacky static bool HandleUse(TreePattern *I, TreePatternNode *Pat, 2418f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> &InstInputs) { 2419f22ef01cSRoman Divacky // No name -> not interesting. 2420f22ef01cSRoman Divacky if (Pat->getName().empty()) { 2421f22ef01cSRoman Divacky if (Pat->isLeaf()) { 24223861d79fSDimitry Andric DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); 242317a519f9SDimitry Andric if (DI && (DI->getDef()->isSubClassOf("RegisterClass") || 242417a519f9SDimitry Andric DI->getDef()->isSubClassOf("RegisterOperand"))) 2425f22ef01cSRoman Divacky I->error("Input " + DI->getDef()->getName() + " must be named!"); 2426f22ef01cSRoman Divacky } 2427f22ef01cSRoman Divacky return false; 2428f22ef01cSRoman Divacky } 2429f22ef01cSRoman Divacky 2430f22ef01cSRoman Divacky Record *Rec; 2431f22ef01cSRoman Divacky if (Pat->isLeaf()) { 24323861d79fSDimitry Andric DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue()); 2433f22ef01cSRoman Divacky if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); 2434f22ef01cSRoman Divacky Rec = DI->getDef(); 2435f22ef01cSRoman Divacky } else { 2436f22ef01cSRoman Divacky Rec = Pat->getOperator(); 2437f22ef01cSRoman Divacky } 2438f22ef01cSRoman Divacky 2439f22ef01cSRoman Divacky // SRCVALUE nodes are ignored. 2440f22ef01cSRoman Divacky if (Rec->getName() == "srcvalue") 2441f22ef01cSRoman Divacky return false; 2442f22ef01cSRoman Divacky 2443f22ef01cSRoman Divacky TreePatternNode *&Slot = InstInputs[Pat->getName()]; 2444f22ef01cSRoman Divacky if (!Slot) { 2445f22ef01cSRoman Divacky Slot = Pat; 2446f22ef01cSRoman Divacky return true; 2447f22ef01cSRoman Divacky } 2448f22ef01cSRoman Divacky Record *SlotRec; 2449f22ef01cSRoman Divacky if (Slot->isLeaf()) { 24503861d79fSDimitry Andric SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef(); 2451f22ef01cSRoman Divacky } else { 2452f22ef01cSRoman Divacky assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); 2453f22ef01cSRoman Divacky SlotRec = Slot->getOperator(); 2454f22ef01cSRoman Divacky } 2455f22ef01cSRoman Divacky 2456f22ef01cSRoman Divacky // Ensure that the inputs agree if we've already seen this input. 2457f22ef01cSRoman Divacky if (Rec != SlotRec) 2458f22ef01cSRoman Divacky I->error("All $" + Pat->getName() + " inputs must agree with each other"); 2459f22ef01cSRoman Divacky if (Slot->getExtTypes() != Pat->getExtTypes()) 2460f22ef01cSRoman Divacky I->error("All $" + Pat->getName() + " inputs must agree with each other"); 2461f22ef01cSRoman Divacky return true; 2462f22ef01cSRoman Divacky } 2463f22ef01cSRoman Divacky 2464f22ef01cSRoman Divacky /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is 2465f22ef01cSRoman Divacky /// part of "I", the instruction), computing the set of inputs and outputs of 2466f22ef01cSRoman Divacky /// the pattern. Report errors if we see anything naughty. 2467f22ef01cSRoman Divacky void CodeGenDAGPatterns:: 2468f22ef01cSRoman Divacky FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, 2469f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> &InstInputs, 2470f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*>&InstResults, 2471f22ef01cSRoman Divacky std::vector<Record*> &InstImpResults) { 2472f22ef01cSRoman Divacky if (Pat->isLeaf()) { 2473f22ef01cSRoman Divacky bool isUse = HandleUse(I, Pat, InstInputs); 2474f22ef01cSRoman Divacky if (!isUse && Pat->getTransformFn()) 2475f22ef01cSRoman Divacky I->error("Cannot specify a transform function for a non-input value!"); 2476f22ef01cSRoman Divacky return; 2477f22ef01cSRoman Divacky } 2478f22ef01cSRoman Divacky 2479f22ef01cSRoman Divacky if (Pat->getOperator()->getName() == "implicit") { 2480f22ef01cSRoman Divacky for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { 2481f22ef01cSRoman Divacky TreePatternNode *Dest = Pat->getChild(i); 2482f22ef01cSRoman Divacky if (!Dest->isLeaf()) 2483f22ef01cSRoman Divacky I->error("implicitly defined value should be a register!"); 2484f22ef01cSRoman Divacky 24853861d79fSDimitry Andric DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); 2486f22ef01cSRoman Divacky if (!Val || !Val->getDef()->isSubClassOf("Register")) 2487f22ef01cSRoman Divacky I->error("implicitly defined value should be a register!"); 2488f22ef01cSRoman Divacky InstImpResults.push_back(Val->getDef()); 2489f22ef01cSRoman Divacky } 2490f22ef01cSRoman Divacky return; 2491f22ef01cSRoman Divacky } 2492f22ef01cSRoman Divacky 2493f22ef01cSRoman Divacky if (Pat->getOperator()->getName() != "set") { 2494f22ef01cSRoman Divacky // If this is not a set, verify that the children nodes are not void typed, 2495f22ef01cSRoman Divacky // and recurse. 2496f22ef01cSRoman Divacky for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { 2497f22ef01cSRoman Divacky if (Pat->getChild(i)->getNumTypes() == 0) 2498f22ef01cSRoman Divacky I->error("Cannot have void nodes inside of patterns!"); 2499f22ef01cSRoman Divacky FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, 2500f22ef01cSRoman Divacky InstImpResults); 2501f22ef01cSRoman Divacky } 2502f22ef01cSRoman Divacky 2503f22ef01cSRoman Divacky // If this is a non-leaf node with no children, treat it basically as if 2504f22ef01cSRoman Divacky // it were a leaf. This handles nodes like (imm). 2505f22ef01cSRoman Divacky bool isUse = HandleUse(I, Pat, InstInputs); 2506f22ef01cSRoman Divacky 2507f22ef01cSRoman Divacky if (!isUse && Pat->getTransformFn()) 2508f22ef01cSRoman Divacky I->error("Cannot specify a transform function for a non-input value!"); 2509f22ef01cSRoman Divacky return; 2510f22ef01cSRoman Divacky } 2511f22ef01cSRoman Divacky 2512f22ef01cSRoman Divacky // Otherwise, this is a set, validate and collect instruction results. 2513f22ef01cSRoman Divacky if (Pat->getNumChildren() == 0) 2514f22ef01cSRoman Divacky I->error("set requires operands!"); 2515f22ef01cSRoman Divacky 2516f22ef01cSRoman Divacky if (Pat->getTransformFn()) 2517f22ef01cSRoman Divacky I->error("Cannot specify a transform function on a set node!"); 2518f22ef01cSRoman Divacky 2519f22ef01cSRoman Divacky // Check the set destinations. 2520f22ef01cSRoman Divacky unsigned NumDests = Pat->getNumChildren()-1; 2521f22ef01cSRoman Divacky for (unsigned i = 0; i != NumDests; ++i) { 2522f22ef01cSRoman Divacky TreePatternNode *Dest = Pat->getChild(i); 2523f22ef01cSRoman Divacky if (!Dest->isLeaf()) 2524f22ef01cSRoman Divacky I->error("set destination should be a register!"); 2525f22ef01cSRoman Divacky 25263861d79fSDimitry Andric DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue()); 2527f22ef01cSRoman Divacky if (!Val) 2528f22ef01cSRoman Divacky I->error("set destination should be a register!"); 2529f22ef01cSRoman Divacky 2530f22ef01cSRoman Divacky if (Val->getDef()->isSubClassOf("RegisterClass") || 2531139f7f9bSDimitry Andric Val->getDef()->isSubClassOf("ValueType") || 253217a519f9SDimitry Andric Val->getDef()->isSubClassOf("RegisterOperand") || 2533f22ef01cSRoman Divacky Val->getDef()->isSubClassOf("PointerLikeRegClass")) { 2534f22ef01cSRoman Divacky if (Dest->getName().empty()) 2535f22ef01cSRoman Divacky I->error("set destination must have a name!"); 2536f22ef01cSRoman Divacky if (InstResults.count(Dest->getName())) 2537f22ef01cSRoman Divacky I->error("cannot set '" + Dest->getName() +"' multiple times"); 2538f22ef01cSRoman Divacky InstResults[Dest->getName()] = Dest; 2539f22ef01cSRoman Divacky } else if (Val->getDef()->isSubClassOf("Register")) { 2540f22ef01cSRoman Divacky InstImpResults.push_back(Val->getDef()); 2541f22ef01cSRoman Divacky } else { 2542f22ef01cSRoman Divacky I->error("set destination should be a register!"); 2543f22ef01cSRoman Divacky } 2544f22ef01cSRoman Divacky } 2545f22ef01cSRoman Divacky 2546f22ef01cSRoman Divacky // Verify and collect info from the computation. 2547f22ef01cSRoman Divacky FindPatternInputsAndOutputs(I, Pat->getChild(NumDests), 2548f22ef01cSRoman Divacky InstInputs, InstResults, InstImpResults); 2549f22ef01cSRoman Divacky } 2550f22ef01cSRoman Divacky 2551f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2552f22ef01cSRoman Divacky // Instruction Analysis 2553f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 2554f22ef01cSRoman Divacky 2555f22ef01cSRoman Divacky class InstAnalyzer { 2556f22ef01cSRoman Divacky const CodeGenDAGPatterns &CDP; 2557f22ef01cSRoman Divacky public: 25583861d79fSDimitry Andric bool hasSideEffects; 25593861d79fSDimitry Andric bool mayStore; 25603861d79fSDimitry Andric bool mayLoad; 25613861d79fSDimitry Andric bool isBitcast; 25623861d79fSDimitry Andric bool isVariadic; 25633861d79fSDimitry Andric 25643861d79fSDimitry Andric InstAnalyzer(const CodeGenDAGPatterns &cdp) 25653861d79fSDimitry Andric : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false), 25663861d79fSDimitry Andric isBitcast(false), isVariadic(false) {} 25673861d79fSDimitry Andric 25683861d79fSDimitry Andric void Analyze(const TreePattern *Pat) { 25693861d79fSDimitry Andric // Assume only the first tree is the pattern. The others are clobber nodes. 25703861d79fSDimitry Andric AnalyzeNode(Pat->getTree(0)); 2571f22ef01cSRoman Divacky } 2572f22ef01cSRoman Divacky 25733861d79fSDimitry Andric void Analyze(const PatternToMatch *Pat) { 25743861d79fSDimitry Andric AnalyzeNode(Pat->getSrcPattern()); 2575f22ef01cSRoman Divacky } 2576f22ef01cSRoman Divacky 2577f22ef01cSRoman Divacky private: 25783b0f4066SDimitry Andric bool IsNodeBitcast(const TreePatternNode *N) const { 25793861d79fSDimitry Andric if (hasSideEffects || mayLoad || mayStore || isVariadic) 25803b0f4066SDimitry Andric return false; 25813b0f4066SDimitry Andric 25823b0f4066SDimitry Andric if (N->getNumChildren() != 2) 25833b0f4066SDimitry Andric return false; 25843b0f4066SDimitry Andric 25853b0f4066SDimitry Andric const TreePatternNode *N0 = N->getChild(0); 25863861d79fSDimitry Andric if (!N0->isLeaf() || !isa<DefInit>(N0->getLeafValue())) 25873b0f4066SDimitry Andric return false; 25883b0f4066SDimitry Andric 25893b0f4066SDimitry Andric const TreePatternNode *N1 = N->getChild(1); 25903b0f4066SDimitry Andric if (N1->isLeaf()) 25913b0f4066SDimitry Andric return false; 25923b0f4066SDimitry Andric if (N1->getNumChildren() != 1 || !N1->getChild(0)->isLeaf()) 25933b0f4066SDimitry Andric return false; 25943b0f4066SDimitry Andric 25953b0f4066SDimitry Andric const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N1->getOperator()); 25963b0f4066SDimitry Andric if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1) 25973b0f4066SDimitry Andric return false; 25983b0f4066SDimitry Andric return OpInfo.getEnumName() == "ISD::BITCAST"; 25993b0f4066SDimitry Andric } 26003b0f4066SDimitry Andric 26013861d79fSDimitry Andric public: 2602f22ef01cSRoman Divacky void AnalyzeNode(const TreePatternNode *N) { 2603f22ef01cSRoman Divacky if (N->isLeaf()) { 26043861d79fSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) { 2605f22ef01cSRoman Divacky Record *LeafRec = DI->getDef(); 2606f22ef01cSRoman Divacky // Handle ComplexPattern leaves. 2607f22ef01cSRoman Divacky if (LeafRec->isSubClassOf("ComplexPattern")) { 2608f22ef01cSRoman Divacky const ComplexPattern &CP = CDP.getComplexPattern(LeafRec); 2609f22ef01cSRoman Divacky if (CP.hasProperty(SDNPMayStore)) mayStore = true; 2610f22ef01cSRoman Divacky if (CP.hasProperty(SDNPMayLoad)) mayLoad = true; 26113861d79fSDimitry Andric if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true; 2612f22ef01cSRoman Divacky } 2613f22ef01cSRoman Divacky } 2614f22ef01cSRoman Divacky return; 2615f22ef01cSRoman Divacky } 2616f22ef01cSRoman Divacky 2617f22ef01cSRoman Divacky // Analyze children. 2618f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 2619f22ef01cSRoman Divacky AnalyzeNode(N->getChild(i)); 2620f22ef01cSRoman Divacky 2621f22ef01cSRoman Divacky // Ignore set nodes, which are not SDNodes. 26223b0f4066SDimitry Andric if (N->getOperator()->getName() == "set") { 26233861d79fSDimitry Andric isBitcast = IsNodeBitcast(N); 2624f22ef01cSRoman Divacky return; 26253b0f4066SDimitry Andric } 2626f22ef01cSRoman Divacky 2627f22ef01cSRoman Divacky // Notice properties of the node. 262891bc56edSDimitry Andric if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true; 262991bc56edSDimitry Andric if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true; 263091bc56edSDimitry Andric if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true; 263191bc56edSDimitry Andric if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true; 2632f22ef01cSRoman Divacky 2633f22ef01cSRoman Divacky if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) { 2634f22ef01cSRoman Divacky // If this is an intrinsic, analyze it. 2635f22ef01cSRoman Divacky if (IntInfo->ModRef >= CodeGenIntrinsic::ReadArgMem) 2636f22ef01cSRoman Divacky mayLoad = true;// These may load memory. 2637f22ef01cSRoman Divacky 2638e580952dSDimitry Andric if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteArgMem) 2639f22ef01cSRoman Divacky mayStore = true;// Intrinsics that can write to memory are 'mayStore'. 2640f22ef01cSRoman Divacky 2641e580952dSDimitry Andric if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem) 2642f22ef01cSRoman Divacky // WriteMem intrinsics can have other strange effects. 26433861d79fSDimitry Andric hasSideEffects = true; 2644f22ef01cSRoman Divacky } 2645f22ef01cSRoman Divacky } 2646f22ef01cSRoman Divacky 2647f22ef01cSRoman Divacky }; 2648f22ef01cSRoman Divacky 26493861d79fSDimitry Andric static bool InferFromPattern(CodeGenInstruction &InstInfo, 26503861d79fSDimitry Andric const InstAnalyzer &PatInfo, 26513861d79fSDimitry Andric Record *PatDef) { 26523861d79fSDimitry Andric bool Error = false; 2653f22ef01cSRoman Divacky 26543861d79fSDimitry Andric // Remember where InstInfo got its flags. 26553861d79fSDimitry Andric if (InstInfo.hasUndefFlags()) 26563861d79fSDimitry Andric InstInfo.InferredFrom = PatDef; 2657f22ef01cSRoman Divacky 26583861d79fSDimitry Andric // Check explicitly set flags for consistency. 26593861d79fSDimitry Andric if (InstInfo.hasSideEffects != PatInfo.hasSideEffects && 26603861d79fSDimitry Andric !InstInfo.hasSideEffects_Unset) { 26613861d79fSDimitry Andric // Allow explicitly setting hasSideEffects = 1 on instructions, even when 26623861d79fSDimitry Andric // the pattern has no side effects. That could be useful for div/rem 26633861d79fSDimitry Andric // instructions that may trap. 26643861d79fSDimitry Andric if (!InstInfo.hasSideEffects) { 26653861d79fSDimitry Andric Error = true; 26663861d79fSDimitry Andric PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " + 26673861d79fSDimitry Andric Twine(InstInfo.hasSideEffects)); 26683861d79fSDimitry Andric } 2669f22ef01cSRoman Divacky } 2670f22ef01cSRoman Divacky 26713861d79fSDimitry Andric if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) { 26723861d79fSDimitry Andric Error = true; 26733861d79fSDimitry Andric PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " + 26743861d79fSDimitry Andric Twine(InstInfo.mayStore)); 2675f22ef01cSRoman Divacky } 2676f22ef01cSRoman Divacky 26773861d79fSDimitry Andric if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) { 26783861d79fSDimitry Andric // Allow explicitly setting mayLoad = 1, even when the pattern has no loads. 26793861d79fSDimitry Andric // Some targets translate imediates to loads. 26803861d79fSDimitry Andric if (!InstInfo.mayLoad) { 26813861d79fSDimitry Andric Error = true; 26823861d79fSDimitry Andric PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " + 26833861d79fSDimitry Andric Twine(InstInfo.mayLoad)); 26843861d79fSDimitry Andric } 2685f22ef01cSRoman Divacky } 2686f22ef01cSRoman Divacky 26873861d79fSDimitry Andric // Transfer inferred flags. 26883861d79fSDimitry Andric InstInfo.hasSideEffects |= PatInfo.hasSideEffects; 26893861d79fSDimitry Andric InstInfo.mayStore |= PatInfo.mayStore; 26903861d79fSDimitry Andric InstInfo.mayLoad |= PatInfo.mayLoad; 2691f22ef01cSRoman Divacky 26923861d79fSDimitry Andric // These flags are silently added without any verification. 26933861d79fSDimitry Andric InstInfo.isBitcast |= PatInfo.isBitcast; 26943861d79fSDimitry Andric 26953861d79fSDimitry Andric // Don't infer isVariadic. This flag means something different on SDNodes and 26963861d79fSDimitry Andric // instructions. For example, a CALL SDNode is variadic because it has the 26973861d79fSDimitry Andric // call arguments as operands, but a CALL instruction is not variadic - it 26983861d79fSDimitry Andric // has argument registers as implicit, not explicit uses. 26993861d79fSDimitry Andric 27003861d79fSDimitry Andric return Error; 2701f22ef01cSRoman Divacky } 2702f22ef01cSRoman Divacky 27037ae0e2c9SDimitry Andric /// hasNullFragReference - Return true if the DAG has any reference to the 27047ae0e2c9SDimitry Andric /// null_frag operator. 27057ae0e2c9SDimitry Andric static bool hasNullFragReference(DagInit *DI) { 27063861d79fSDimitry Andric DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator()); 27077ae0e2c9SDimitry Andric if (!OpDef) return false; 27087ae0e2c9SDimitry Andric Record *Operator = OpDef->getDef(); 27097ae0e2c9SDimitry Andric 27107ae0e2c9SDimitry Andric // If this is the null fragment, return true. 27117ae0e2c9SDimitry Andric if (Operator->getName() == "null_frag") return true; 27127ae0e2c9SDimitry Andric // If any of the arguments reference the null fragment, return true. 27137ae0e2c9SDimitry Andric for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) { 27143861d79fSDimitry Andric DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i)); 27157ae0e2c9SDimitry Andric if (Arg && hasNullFragReference(Arg)) 27167ae0e2c9SDimitry Andric return true; 27177ae0e2c9SDimitry Andric } 27187ae0e2c9SDimitry Andric 27197ae0e2c9SDimitry Andric return false; 27207ae0e2c9SDimitry Andric } 27217ae0e2c9SDimitry Andric 27227ae0e2c9SDimitry Andric /// hasNullFragReference - Return true if any DAG in the list references 27237ae0e2c9SDimitry Andric /// the null_frag operator. 27247ae0e2c9SDimitry Andric static bool hasNullFragReference(ListInit *LI) { 27257ae0e2c9SDimitry Andric for (unsigned i = 0, e = LI->getSize(); i != e; ++i) { 27263861d79fSDimitry Andric DagInit *DI = dyn_cast<DagInit>(LI->getElement(i)); 27277ae0e2c9SDimitry Andric assert(DI && "non-dag in an instruction Pattern list?!"); 27287ae0e2c9SDimitry Andric if (hasNullFragReference(DI)) 27297ae0e2c9SDimitry Andric return true; 27307ae0e2c9SDimitry Andric } 27317ae0e2c9SDimitry Andric return false; 27327ae0e2c9SDimitry Andric } 27337ae0e2c9SDimitry Andric 27343861d79fSDimitry Andric /// Get all the instructions in a tree. 27353861d79fSDimitry Andric static void 27363861d79fSDimitry Andric getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) { 27373861d79fSDimitry Andric if (Tree->isLeaf()) 27383861d79fSDimitry Andric return; 27393861d79fSDimitry Andric if (Tree->getOperator()->isSubClassOf("Instruction")) 27403861d79fSDimitry Andric Instrs.push_back(Tree->getOperator()); 27413861d79fSDimitry Andric for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i) 27423861d79fSDimitry Andric getInstructionsInTree(Tree->getChild(i), Instrs); 27433861d79fSDimitry Andric } 27443861d79fSDimitry Andric 2745139f7f9bSDimitry Andric /// Check the class of a pattern leaf node against the instruction operand it 2746139f7f9bSDimitry Andric /// represents. 2747139f7f9bSDimitry Andric static bool checkOperandClass(CGIOperandList::OperandInfo &OI, 2748139f7f9bSDimitry Andric Record *Leaf) { 2749139f7f9bSDimitry Andric if (OI.Rec == Leaf) 2750139f7f9bSDimitry Andric return true; 2751139f7f9bSDimitry Andric 2752139f7f9bSDimitry Andric // Allow direct value types to be used in instruction set patterns. 2753139f7f9bSDimitry Andric // The type will be checked later. 2754139f7f9bSDimitry Andric if (Leaf->isSubClassOf("ValueType")) 2755139f7f9bSDimitry Andric return true; 2756139f7f9bSDimitry Andric 2757139f7f9bSDimitry Andric // Patterns can also be ComplexPattern instances. 2758139f7f9bSDimitry Andric if (Leaf->isSubClassOf("ComplexPattern")) 2759139f7f9bSDimitry Andric return true; 2760139f7f9bSDimitry Andric 2761139f7f9bSDimitry Andric return false; 2762139f7f9bSDimitry Andric } 2763139f7f9bSDimitry Andric 2764f785676fSDimitry Andric const DAGInstruction &CodeGenDAGPatterns::parseInstructionPattern( 2765f785676fSDimitry Andric CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) { 2766f22ef01cSRoman Divacky 2767f785676fSDimitry Andric assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!"); 2768f22ef01cSRoman Divacky 2769f22ef01cSRoman Divacky // Parse the instruction. 2770f785676fSDimitry Andric TreePattern *I = new TreePattern(CGI.TheDef, Pat, true, *this); 2771f22ef01cSRoman Divacky // Inline pattern fragments into it. 2772f22ef01cSRoman Divacky I->InlinePatternFragments(); 2773f22ef01cSRoman Divacky 2774f22ef01cSRoman Divacky // Infer as many types as possible. If we cannot infer all of them, we can 2775f22ef01cSRoman Divacky // never do anything with this instruction pattern: report it to the user. 2776f22ef01cSRoman Divacky if (!I->InferAllTypes()) 2777f22ef01cSRoman Divacky I->error("Could not infer all types in pattern!"); 2778f22ef01cSRoman Divacky 2779f22ef01cSRoman Divacky // InstInputs - Keep track of all of the inputs of the instruction, along 2780f22ef01cSRoman Divacky // with the record they are declared as. 2781f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> InstInputs; 2782f22ef01cSRoman Divacky 2783f22ef01cSRoman Divacky // InstResults - Keep track of all the virtual registers that are 'set' 2784f22ef01cSRoman Divacky // in the instruction, including what reg class they are. 2785f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> InstResults; 2786f22ef01cSRoman Divacky 2787f22ef01cSRoman Divacky std::vector<Record*> InstImpResults; 2788f22ef01cSRoman Divacky 2789f22ef01cSRoman Divacky // Verify that the top-level forms in the instruction are of void type, and 2790f22ef01cSRoman Divacky // fill in the InstResults map. 2791f22ef01cSRoman Divacky for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { 2792f22ef01cSRoman Divacky TreePatternNode *Pat = I->getTree(j); 2793f22ef01cSRoman Divacky if (Pat->getNumTypes() != 0) 2794f22ef01cSRoman Divacky I->error("Top-level forms in instruction pattern should have" 2795f22ef01cSRoman Divacky " void types"); 2796f22ef01cSRoman Divacky 2797f22ef01cSRoman Divacky // Find inputs and outputs, and verify the structure of the uses/defs. 2798f22ef01cSRoman Divacky FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, 2799f22ef01cSRoman Divacky InstImpResults); 2800f22ef01cSRoman Divacky } 2801f22ef01cSRoman Divacky 2802f22ef01cSRoman Divacky // Now that we have inputs and outputs of the pattern, inspect the operands 2803f22ef01cSRoman Divacky // list for the instruction. This determines the order that operands are 2804f22ef01cSRoman Divacky // added to the machine instruction the node corresponds to. 2805f22ef01cSRoman Divacky unsigned NumResults = InstResults.size(); 2806f22ef01cSRoman Divacky 2807f22ef01cSRoman Divacky // Parse the operands list from the (ops) list, validating it. 2808f22ef01cSRoman Divacky assert(I->getArgList().empty() && "Args list should still be empty here!"); 2809f22ef01cSRoman Divacky 2810f22ef01cSRoman Divacky // Check that all of the results occur first in the list. 2811f22ef01cSRoman Divacky std::vector<Record*> Results; 281291bc56edSDimitry Andric TreePatternNode *Res0Node = nullptr; 2813f22ef01cSRoman Divacky for (unsigned i = 0; i != NumResults; ++i) { 28142754fe60SDimitry Andric if (i == CGI.Operands.size()) 2815f22ef01cSRoman Divacky I->error("'" + InstResults.begin()->first + 2816f22ef01cSRoman Divacky "' set but does not appear in operand list!"); 28172754fe60SDimitry Andric const std::string &OpName = CGI.Operands[i].Name; 2818f22ef01cSRoman Divacky 2819f22ef01cSRoman Divacky // Check that it exists in InstResults. 2820f22ef01cSRoman Divacky TreePatternNode *RNode = InstResults[OpName]; 282191bc56edSDimitry Andric if (!RNode) 2822f22ef01cSRoman Divacky I->error("Operand $" + OpName + " does not exist in operand list!"); 2823f22ef01cSRoman Divacky 2824f22ef01cSRoman Divacky if (i == 0) 2825f22ef01cSRoman Divacky Res0Node = RNode; 28263861d79fSDimitry Andric Record *R = cast<DefInit>(RNode->getLeafValue())->getDef(); 282791bc56edSDimitry Andric if (!R) 2828f22ef01cSRoman Divacky I->error("Operand $" + OpName + " should be a set destination: all " 2829f22ef01cSRoman Divacky "outputs must occur before inputs in operand list!"); 2830f22ef01cSRoman Divacky 2831139f7f9bSDimitry Andric if (!checkOperandClass(CGI.Operands[i], R)) 2832f22ef01cSRoman Divacky I->error("Operand $" + OpName + " class mismatch!"); 2833f22ef01cSRoman Divacky 2834f22ef01cSRoman Divacky // Remember the return type. 28352754fe60SDimitry Andric Results.push_back(CGI.Operands[i].Rec); 2836f22ef01cSRoman Divacky 2837f22ef01cSRoman Divacky // Okay, this one checks out. 2838f22ef01cSRoman Divacky InstResults.erase(OpName); 2839f22ef01cSRoman Divacky } 2840f22ef01cSRoman Divacky 2841f22ef01cSRoman Divacky // Loop over the inputs next. Make a copy of InstInputs so we can destroy 2842f22ef01cSRoman Divacky // the copy while we're checking the inputs. 2843f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); 2844f22ef01cSRoman Divacky 2845f22ef01cSRoman Divacky std::vector<TreePatternNode*> ResultNodeOperands; 2846f22ef01cSRoman Divacky std::vector<Record*> Operands; 28472754fe60SDimitry Andric for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) { 28482754fe60SDimitry Andric CGIOperandList::OperandInfo &Op = CGI.Operands[i]; 2849f22ef01cSRoman Divacky const std::string &OpName = Op.Name; 2850f22ef01cSRoman Divacky if (OpName.empty()) 2851f22ef01cSRoman Divacky I->error("Operand #" + utostr(i) + " in operands list has no name!"); 2852f22ef01cSRoman Divacky 2853f22ef01cSRoman Divacky if (!InstInputsCheck.count(OpName)) { 28543861d79fSDimitry Andric // If this is an operand with a DefaultOps set filled in, we can ignore 28553861d79fSDimitry Andric // this. When we codegen it, we will do so as always executed. 28563861d79fSDimitry Andric if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) { 2857f22ef01cSRoman Divacky // Does it have a non-empty DefaultOps field? If so, ignore this 2858f22ef01cSRoman Divacky // operand. 2859f22ef01cSRoman Divacky if (!getDefaultOperand(Op.Rec).DefaultOps.empty()) 2860f22ef01cSRoman Divacky continue; 2861f22ef01cSRoman Divacky } 2862f22ef01cSRoman Divacky I->error("Operand $" + OpName + 2863f22ef01cSRoman Divacky " does not appear in the instruction pattern"); 2864f22ef01cSRoman Divacky } 2865f22ef01cSRoman Divacky TreePatternNode *InVal = InstInputsCheck[OpName]; 2866f22ef01cSRoman Divacky InstInputsCheck.erase(OpName); // It occurred, remove from map. 2867f22ef01cSRoman Divacky 28683861d79fSDimitry Andric if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) { 2869f22ef01cSRoman Divacky Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); 2870139f7f9bSDimitry Andric if (!checkOperandClass(Op, InRec)) 2871f22ef01cSRoman Divacky I->error("Operand $" + OpName + "'s register class disagrees" 2872f22ef01cSRoman Divacky " between the operand and pattern"); 2873f22ef01cSRoman Divacky } 2874f22ef01cSRoman Divacky Operands.push_back(Op.Rec); 2875f22ef01cSRoman Divacky 2876f22ef01cSRoman Divacky // Construct the result for the dest-pattern operand list. 2877f22ef01cSRoman Divacky TreePatternNode *OpNode = InVal->clone(); 2878f22ef01cSRoman Divacky 2879f22ef01cSRoman Divacky // No predicate is useful on the result. 2880f22ef01cSRoman Divacky OpNode->clearPredicateFns(); 2881f22ef01cSRoman Divacky 2882f22ef01cSRoman Divacky // Promote the xform function to be an explicit node if set. 2883f22ef01cSRoman Divacky if (Record *Xform = OpNode->getTransformFn()) { 288491bc56edSDimitry Andric OpNode->setTransformFn(nullptr); 2885f22ef01cSRoman Divacky std::vector<TreePatternNode*> Children; 2886f22ef01cSRoman Divacky Children.push_back(OpNode); 2887f22ef01cSRoman Divacky OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes()); 2888f22ef01cSRoman Divacky } 2889f22ef01cSRoman Divacky 2890f22ef01cSRoman Divacky ResultNodeOperands.push_back(OpNode); 2891f22ef01cSRoman Divacky } 2892f22ef01cSRoman Divacky 2893f22ef01cSRoman Divacky if (!InstInputsCheck.empty()) 2894f22ef01cSRoman Divacky I->error("Input operand $" + InstInputsCheck.begin()->first + 2895f22ef01cSRoman Divacky " occurs in pattern but not in operands list!"); 2896f22ef01cSRoman Divacky 2897f22ef01cSRoman Divacky TreePatternNode *ResultPattern = 2898f22ef01cSRoman Divacky new TreePatternNode(I->getRecord(), ResultNodeOperands, 2899f22ef01cSRoman Divacky GetNumNodeResults(I->getRecord(), *this)); 2900f22ef01cSRoman Divacky // Copy fully inferred output node type to instruction result pattern. 2901f22ef01cSRoman Divacky for (unsigned i = 0; i != NumResults; ++i) 2902f22ef01cSRoman Divacky ResultPattern->setType(i, Res0Node->getExtType(i)); 2903f22ef01cSRoman Divacky 2904f22ef01cSRoman Divacky // Create and insert the instruction. 2905f22ef01cSRoman Divacky // FIXME: InstImpResults should not be part of DAGInstruction. 2906f22ef01cSRoman Divacky DAGInstruction TheInst(I, Results, Operands, InstImpResults); 2907f785676fSDimitry Andric DAGInsts.insert(std::make_pair(I->getRecord(), TheInst)); 2908f22ef01cSRoman Divacky 2909f22ef01cSRoman Divacky // Use a temporary tree pattern to infer all types and make sure that the 2910f22ef01cSRoman Divacky // constructed result is correct. This depends on the instruction already 2911f785676fSDimitry Andric // being inserted into the DAGInsts map. 2912f22ef01cSRoman Divacky TreePattern Temp(I->getRecord(), ResultPattern, false, *this); 2913f22ef01cSRoman Divacky Temp.InferAllTypes(&I->getNamedNodesMap()); 2914f22ef01cSRoman Divacky 2915f785676fSDimitry Andric DAGInstruction &TheInsertedInst = DAGInsts.find(I->getRecord())->second; 2916f22ef01cSRoman Divacky TheInsertedInst.setResultPattern(Temp.getOnlyTree()); 2917f22ef01cSRoman Divacky 2918f785676fSDimitry Andric return TheInsertedInst; 2919f785676fSDimitry Andric } 2920f785676fSDimitry Andric 2921f785676fSDimitry Andric /// ParseInstructions - Parse all of the instructions, inlining and resolving 2922f785676fSDimitry Andric /// any fragments involved. This populates the Instructions list with fully 2923f785676fSDimitry Andric /// resolved instructions. 2924f785676fSDimitry Andric void CodeGenDAGPatterns::ParseInstructions() { 2925f785676fSDimitry Andric std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); 2926f785676fSDimitry Andric 2927f785676fSDimitry Andric for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { 292891bc56edSDimitry Andric ListInit *LI = nullptr; 2929f785676fSDimitry Andric 2930f785676fSDimitry Andric if (isa<ListInit>(Instrs[i]->getValueInit("Pattern"))) 2931f785676fSDimitry Andric LI = Instrs[i]->getValueAsListInit("Pattern"); 2932f785676fSDimitry Andric 2933f785676fSDimitry Andric // If there is no pattern, only collect minimal information about the 2934f785676fSDimitry Andric // instruction for its operand list. We have to assume that there is one 2935f785676fSDimitry Andric // result, as we have no detailed info. A pattern which references the 2936f785676fSDimitry Andric // null_frag operator is as-if no pattern were specified. Normally this 2937f785676fSDimitry Andric // is from a multiclass expansion w/ a SDPatternOperator passed in as 2938f785676fSDimitry Andric // null_frag. 2939f785676fSDimitry Andric if (!LI || LI->getSize() == 0 || hasNullFragReference(LI)) { 2940f785676fSDimitry Andric std::vector<Record*> Results; 2941f785676fSDimitry Andric std::vector<Record*> Operands; 2942f785676fSDimitry Andric 2943f785676fSDimitry Andric CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); 2944f785676fSDimitry Andric 2945f785676fSDimitry Andric if (InstInfo.Operands.size() != 0) { 2946f785676fSDimitry Andric if (InstInfo.Operands.NumDefs == 0) { 2947f785676fSDimitry Andric // These produce no results 2948f785676fSDimitry Andric for (unsigned j = 0, e = InstInfo.Operands.size(); j < e; ++j) 2949f785676fSDimitry Andric Operands.push_back(InstInfo.Operands[j].Rec); 2950f785676fSDimitry Andric } else { 2951f785676fSDimitry Andric // Assume the first operand is the result. 2952f785676fSDimitry Andric Results.push_back(InstInfo.Operands[0].Rec); 2953f785676fSDimitry Andric 2954f785676fSDimitry Andric // The rest are inputs. 2955f785676fSDimitry Andric for (unsigned j = 1, e = InstInfo.Operands.size(); j < e; ++j) 2956f785676fSDimitry Andric Operands.push_back(InstInfo.Operands[j].Rec); 2957f785676fSDimitry Andric } 2958f785676fSDimitry Andric } 2959f785676fSDimitry Andric 2960f785676fSDimitry Andric // Create and insert the instruction. 2961f785676fSDimitry Andric std::vector<Record*> ImpResults; 2962f785676fSDimitry Andric Instructions.insert(std::make_pair(Instrs[i], 296391bc56edSDimitry Andric DAGInstruction(nullptr, Results, Operands, ImpResults))); 2964f785676fSDimitry Andric continue; // no pattern. 2965f785676fSDimitry Andric } 2966f785676fSDimitry Andric 2967f785676fSDimitry Andric CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]); 2968f785676fSDimitry Andric const DAGInstruction &DI = parseInstructionPattern(CGI, LI, Instructions); 2969f785676fSDimitry Andric 2970f785676fSDimitry Andric (void)DI; 2971f785676fSDimitry Andric DEBUG(DI.getPattern()->dump()); 2972f22ef01cSRoman Divacky } 2973f22ef01cSRoman Divacky 2974f22ef01cSRoman Divacky // If we can, convert the instructions to be patterns that are matched! 29753861d79fSDimitry Andric for (std::map<Record*, DAGInstruction, LessRecordByID>::iterator II = 2976f22ef01cSRoman Divacky Instructions.begin(), 2977f22ef01cSRoman Divacky E = Instructions.end(); II != E; ++II) { 2978f22ef01cSRoman Divacky DAGInstruction &TheInst = II->second; 29793861d79fSDimitry Andric TreePattern *I = TheInst.getPattern(); 298091bc56edSDimitry Andric if (!I) continue; // No pattern. 2981f22ef01cSRoman Divacky 2982f22ef01cSRoman Divacky // FIXME: Assume only the first tree is the pattern. The others are clobber 2983f22ef01cSRoman Divacky // nodes. 2984f22ef01cSRoman Divacky TreePatternNode *Pattern = I->getTree(0); 2985f22ef01cSRoman Divacky TreePatternNode *SrcPattern; 2986f22ef01cSRoman Divacky if (Pattern->getOperator()->getName() == "set") { 2987f22ef01cSRoman Divacky SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone(); 2988f22ef01cSRoman Divacky } else{ 2989f22ef01cSRoman Divacky // Not a set (store or something?) 2990f22ef01cSRoman Divacky SrcPattern = Pattern; 2991f22ef01cSRoman Divacky } 2992f22ef01cSRoman Divacky 2993f22ef01cSRoman Divacky Record *Instr = II->first; 2994f22ef01cSRoman Divacky AddPatternToMatch(I, 29952754fe60SDimitry Andric PatternToMatch(Instr, 29962754fe60SDimitry Andric Instr->getValueAsListInit("Predicates"), 2997f22ef01cSRoman Divacky SrcPattern, 2998f22ef01cSRoman Divacky TheInst.getResultPattern(), 2999f22ef01cSRoman Divacky TheInst.getImpResults(), 3000f22ef01cSRoman Divacky Instr->getValueAsInt("AddedComplexity"), 3001f22ef01cSRoman Divacky Instr->getID())); 3002f22ef01cSRoman Divacky } 3003f22ef01cSRoman Divacky } 3004f22ef01cSRoman Divacky 3005f22ef01cSRoman Divacky 3006f22ef01cSRoman Divacky typedef std::pair<const TreePatternNode*, unsigned> NameRecord; 3007f22ef01cSRoman Divacky 3008f22ef01cSRoman Divacky static void FindNames(const TreePatternNode *P, 3009f22ef01cSRoman Divacky std::map<std::string, NameRecord> &Names, 30103861d79fSDimitry Andric TreePattern *PatternTop) { 3011f22ef01cSRoman Divacky if (!P->getName().empty()) { 3012f22ef01cSRoman Divacky NameRecord &Rec = Names[P->getName()]; 3013f22ef01cSRoman Divacky // If this is the first instance of the name, remember the node. 3014f22ef01cSRoman Divacky if (Rec.second++ == 0) 3015f22ef01cSRoman Divacky Rec.first = P; 3016f22ef01cSRoman Divacky else if (Rec.first->getExtTypes() != P->getExtTypes()) 3017f22ef01cSRoman Divacky PatternTop->error("repetition of value: $" + P->getName() + 3018f22ef01cSRoman Divacky " where different uses have different types!"); 3019f22ef01cSRoman Divacky } 3020f22ef01cSRoman Divacky 3021f22ef01cSRoman Divacky if (!P->isLeaf()) { 3022f22ef01cSRoman Divacky for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) 3023f22ef01cSRoman Divacky FindNames(P->getChild(i), Names, PatternTop); 3024f22ef01cSRoman Divacky } 3025f22ef01cSRoman Divacky } 3026f22ef01cSRoman Divacky 30273861d79fSDimitry Andric void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern, 3028f22ef01cSRoman Divacky const PatternToMatch &PTM) { 3029f22ef01cSRoman Divacky // Do some sanity checking on the pattern we're about to match. 3030f22ef01cSRoman Divacky std::string Reason; 30313861d79fSDimitry Andric if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) { 30323861d79fSDimitry Andric PrintWarning(Pattern->getRecord()->getLoc(), 30333861d79fSDimitry Andric Twine("Pattern can never match: ") + Reason); 30343861d79fSDimitry Andric return; 30353861d79fSDimitry Andric } 3036f22ef01cSRoman Divacky 3037f22ef01cSRoman Divacky // If the source pattern's root is a complex pattern, that complex pattern 3038f22ef01cSRoman Divacky // must specify the nodes it can potentially match. 3039f22ef01cSRoman Divacky if (const ComplexPattern *CP = 3040f22ef01cSRoman Divacky PTM.getSrcPattern()->getComplexPatternInfo(*this)) 3041f22ef01cSRoman Divacky if (CP->getRootNodes().empty()) 3042f22ef01cSRoman Divacky Pattern->error("ComplexPattern at root must specify list of opcodes it" 3043f22ef01cSRoman Divacky " could match"); 3044f22ef01cSRoman Divacky 3045f22ef01cSRoman Divacky 3046f22ef01cSRoman Divacky // Find all of the named values in the input and output, ensure they have the 3047f22ef01cSRoman Divacky // same type. 3048f22ef01cSRoman Divacky std::map<std::string, NameRecord> SrcNames, DstNames; 3049f22ef01cSRoman Divacky FindNames(PTM.getSrcPattern(), SrcNames, Pattern); 3050f22ef01cSRoman Divacky FindNames(PTM.getDstPattern(), DstNames, Pattern); 3051f22ef01cSRoman Divacky 3052f22ef01cSRoman Divacky // Scan all of the named values in the destination pattern, rejecting them if 3053f22ef01cSRoman Divacky // they don't exist in the input pattern. 3054f22ef01cSRoman Divacky for (std::map<std::string, NameRecord>::iterator 3055f22ef01cSRoman Divacky I = DstNames.begin(), E = DstNames.end(); I != E; ++I) { 305691bc56edSDimitry Andric if (SrcNames[I->first].first == nullptr) 3057f22ef01cSRoman Divacky Pattern->error("Pattern has input without matching name in output: $" + 3058f22ef01cSRoman Divacky I->first); 3059f22ef01cSRoman Divacky } 3060f22ef01cSRoman Divacky 3061f22ef01cSRoman Divacky // Scan all of the named values in the source pattern, rejecting them if the 3062f22ef01cSRoman Divacky // name isn't used in the dest, and isn't used to tie two values together. 3063f22ef01cSRoman Divacky for (std::map<std::string, NameRecord>::iterator 3064f22ef01cSRoman Divacky I = SrcNames.begin(), E = SrcNames.end(); I != E; ++I) 306591bc56edSDimitry Andric if (DstNames[I->first].first == nullptr && SrcNames[I->first].second == 1) 3066f22ef01cSRoman Divacky Pattern->error("Pattern has dead named input: $" + I->first); 3067f22ef01cSRoman Divacky 3068f22ef01cSRoman Divacky PatternsToMatch.push_back(PTM); 3069f22ef01cSRoman Divacky } 3070f22ef01cSRoman Divacky 3071f22ef01cSRoman Divacky 3072f22ef01cSRoman Divacky 3073f22ef01cSRoman Divacky void CodeGenDAGPatterns::InferInstructionFlags() { 3074f22ef01cSRoman Divacky const std::vector<const CodeGenInstruction*> &Instructions = 3075f22ef01cSRoman Divacky Target.getInstructionsByEnumValue(); 30763861d79fSDimitry Andric 30773861d79fSDimitry Andric // First try to infer flags from the primary instruction pattern, if any. 30783861d79fSDimitry Andric SmallVector<CodeGenInstruction*, 8> Revisit; 30793861d79fSDimitry Andric unsigned Errors = 0; 3080f22ef01cSRoman Divacky for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { 3081f22ef01cSRoman Divacky CodeGenInstruction &InstInfo = 3082f22ef01cSRoman Divacky const_cast<CodeGenInstruction &>(*Instructions[i]); 30836122f3e6SDimitry Andric 30843861d79fSDimitry Andric // Treat neverHasSideEffects = 1 as the equivalent of hasSideEffects = 0. 30853861d79fSDimitry Andric // This flag is obsolete and will be removed. 30863861d79fSDimitry Andric if (InstInfo.neverHasSideEffects) { 30873861d79fSDimitry Andric assert(!InstInfo.hasSideEffects); 30883861d79fSDimitry Andric InstInfo.hasSideEffects_Unset = false; 3089f22ef01cSRoman Divacky } 30903861d79fSDimitry Andric 30913861d79fSDimitry Andric // Get the primary instruction pattern. 30923861d79fSDimitry Andric const TreePattern *Pattern = getInstruction(InstInfo.TheDef).getPattern(); 30933861d79fSDimitry Andric if (!Pattern) { 30943861d79fSDimitry Andric if (InstInfo.hasUndefFlags()) 30953861d79fSDimitry Andric Revisit.push_back(&InstInfo); 30963861d79fSDimitry Andric continue; 30973861d79fSDimitry Andric } 30983861d79fSDimitry Andric InstAnalyzer PatInfo(*this); 30993861d79fSDimitry Andric PatInfo.Analyze(Pattern); 31003861d79fSDimitry Andric Errors += InferFromPattern(InstInfo, PatInfo, InstInfo.TheDef); 31013861d79fSDimitry Andric } 31023861d79fSDimitry Andric 31033861d79fSDimitry Andric // Second, look for single-instruction patterns defined outside the 31043861d79fSDimitry Andric // instruction. 31053861d79fSDimitry Andric for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) { 31063861d79fSDimitry Andric const PatternToMatch &PTM = *I; 31073861d79fSDimitry Andric 31083861d79fSDimitry Andric // We can only infer from single-instruction patterns, otherwise we won't 31093861d79fSDimitry Andric // know which instruction should get the flags. 31103861d79fSDimitry Andric SmallVector<Record*, 8> PatInstrs; 31113861d79fSDimitry Andric getInstructionsInTree(PTM.getDstPattern(), PatInstrs); 31123861d79fSDimitry Andric if (PatInstrs.size() != 1) 31133861d79fSDimitry Andric continue; 31143861d79fSDimitry Andric 31153861d79fSDimitry Andric // Get the single instruction. 31163861d79fSDimitry Andric CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front()); 31173861d79fSDimitry Andric 31183861d79fSDimitry Andric // Only infer properties from the first pattern. We'll verify the others. 31193861d79fSDimitry Andric if (InstInfo.InferredFrom) 31203861d79fSDimitry Andric continue; 31213861d79fSDimitry Andric 31223861d79fSDimitry Andric InstAnalyzer PatInfo(*this); 31233861d79fSDimitry Andric PatInfo.Analyze(&PTM); 31243861d79fSDimitry Andric Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord()); 31253861d79fSDimitry Andric } 31263861d79fSDimitry Andric 31273861d79fSDimitry Andric if (Errors) 31283861d79fSDimitry Andric PrintFatalError("pattern conflicts"); 31293861d79fSDimitry Andric 31303861d79fSDimitry Andric // Revisit instructions with undefined flags and no pattern. 31313861d79fSDimitry Andric if (Target.guessInstructionProperties()) { 31323861d79fSDimitry Andric for (unsigned i = 0, e = Revisit.size(); i != e; ++i) { 31333861d79fSDimitry Andric CodeGenInstruction &InstInfo = *Revisit[i]; 31343861d79fSDimitry Andric if (InstInfo.InferredFrom) 31353861d79fSDimitry Andric continue; 31363861d79fSDimitry Andric // The mayLoad and mayStore flags default to false. 31373861d79fSDimitry Andric // Conservatively assume hasSideEffects if it wasn't explicit. 31383861d79fSDimitry Andric if (InstInfo.hasSideEffects_Unset) 31393861d79fSDimitry Andric InstInfo.hasSideEffects = true; 31403861d79fSDimitry Andric } 31413861d79fSDimitry Andric return; 31423861d79fSDimitry Andric } 31433861d79fSDimitry Andric 31443861d79fSDimitry Andric // Complain about any flags that are still undefined. 31453861d79fSDimitry Andric for (unsigned i = 0, e = Revisit.size(); i != e; ++i) { 31463861d79fSDimitry Andric CodeGenInstruction &InstInfo = *Revisit[i]; 31473861d79fSDimitry Andric if (InstInfo.InferredFrom) 31483861d79fSDimitry Andric continue; 31493861d79fSDimitry Andric if (InstInfo.hasSideEffects_Unset) 31503861d79fSDimitry Andric PrintError(InstInfo.TheDef->getLoc(), 31513861d79fSDimitry Andric "Can't infer hasSideEffects from patterns"); 31523861d79fSDimitry Andric if (InstInfo.mayStore_Unset) 31533861d79fSDimitry Andric PrintError(InstInfo.TheDef->getLoc(), 31543861d79fSDimitry Andric "Can't infer mayStore from patterns"); 31553861d79fSDimitry Andric if (InstInfo.mayLoad_Unset) 31563861d79fSDimitry Andric PrintError(InstInfo.TheDef->getLoc(), 31573861d79fSDimitry Andric "Can't infer mayLoad from patterns"); 31583861d79fSDimitry Andric } 31593861d79fSDimitry Andric } 31603861d79fSDimitry Andric 31613861d79fSDimitry Andric 31623861d79fSDimitry Andric /// Verify instruction flags against pattern node properties. 31633861d79fSDimitry Andric void CodeGenDAGPatterns::VerifyInstructionFlags() { 31643861d79fSDimitry Andric unsigned Errors = 0; 31653861d79fSDimitry Andric for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) { 31663861d79fSDimitry Andric const PatternToMatch &PTM = *I; 31673861d79fSDimitry Andric SmallVector<Record*, 8> Instrs; 31683861d79fSDimitry Andric getInstructionsInTree(PTM.getDstPattern(), Instrs); 31693861d79fSDimitry Andric if (Instrs.empty()) 31703861d79fSDimitry Andric continue; 31713861d79fSDimitry Andric 31723861d79fSDimitry Andric // Count the number of instructions with each flag set. 31733861d79fSDimitry Andric unsigned NumSideEffects = 0; 31743861d79fSDimitry Andric unsigned NumStores = 0; 31753861d79fSDimitry Andric unsigned NumLoads = 0; 31763861d79fSDimitry Andric for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { 31773861d79fSDimitry Andric const CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); 31783861d79fSDimitry Andric NumSideEffects += InstInfo.hasSideEffects; 31793861d79fSDimitry Andric NumStores += InstInfo.mayStore; 31803861d79fSDimitry Andric NumLoads += InstInfo.mayLoad; 31813861d79fSDimitry Andric } 31823861d79fSDimitry Andric 31833861d79fSDimitry Andric // Analyze the source pattern. 31843861d79fSDimitry Andric InstAnalyzer PatInfo(*this); 31853861d79fSDimitry Andric PatInfo.Analyze(&PTM); 31863861d79fSDimitry Andric 31873861d79fSDimitry Andric // Collect error messages. 31883861d79fSDimitry Andric SmallVector<std::string, 4> Msgs; 31893861d79fSDimitry Andric 31903861d79fSDimitry Andric // Check for missing flags in the output. 31913861d79fSDimitry Andric // Permit extra flags for now at least. 31923861d79fSDimitry Andric if (PatInfo.hasSideEffects && !NumSideEffects) 31933861d79fSDimitry Andric Msgs.push_back("pattern has side effects, but hasSideEffects isn't set"); 31943861d79fSDimitry Andric 31953861d79fSDimitry Andric // Don't verify store flags on instructions with side effects. At least for 31963861d79fSDimitry Andric // intrinsics, side effects implies mayStore. 31973861d79fSDimitry Andric if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores) 31983861d79fSDimitry Andric Msgs.push_back("pattern may store, but mayStore isn't set"); 31993861d79fSDimitry Andric 32003861d79fSDimitry Andric // Similarly, mayStore implies mayLoad on intrinsics. 32013861d79fSDimitry Andric if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads) 32023861d79fSDimitry Andric Msgs.push_back("pattern may load, but mayLoad isn't set"); 32033861d79fSDimitry Andric 32043861d79fSDimitry Andric // Print error messages. 32053861d79fSDimitry Andric if (Msgs.empty()) 32063861d79fSDimitry Andric continue; 32073861d79fSDimitry Andric ++Errors; 32083861d79fSDimitry Andric 32093861d79fSDimitry Andric for (unsigned i = 0, e = Msgs.size(); i != e; ++i) 32103861d79fSDimitry Andric PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msgs[i]) + " on the " + 32113861d79fSDimitry Andric (Instrs.size() == 1 ? 32123861d79fSDimitry Andric "instruction" : "output instructions")); 32133861d79fSDimitry Andric // Provide the location of the relevant instruction definitions. 32143861d79fSDimitry Andric for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { 32153861d79fSDimitry Andric if (Instrs[i] != PTM.getSrcRecord()) 32163861d79fSDimitry Andric PrintError(Instrs[i]->getLoc(), "defined here"); 32173861d79fSDimitry Andric const CodeGenInstruction &InstInfo = Target.getInstruction(Instrs[i]); 32183861d79fSDimitry Andric if (InstInfo.InferredFrom && 32193861d79fSDimitry Andric InstInfo.InferredFrom != InstInfo.TheDef && 32203861d79fSDimitry Andric InstInfo.InferredFrom != PTM.getSrcRecord()) 32213861d79fSDimitry Andric PrintError(InstInfo.InferredFrom->getLoc(), "inferred from patttern"); 32223861d79fSDimitry Andric } 32233861d79fSDimitry Andric } 32243861d79fSDimitry Andric if (Errors) 32253861d79fSDimitry Andric PrintFatalError("Errors in DAG patterns"); 3226f22ef01cSRoman Divacky } 3227f22ef01cSRoman Divacky 3228f22ef01cSRoman Divacky /// Given a pattern result with an unresolved type, see if we can find one 3229f22ef01cSRoman Divacky /// instruction with an unresolved result type. Force this result type to an 3230f22ef01cSRoman Divacky /// arbitrary element if it's possible types to converge results. 3231f22ef01cSRoman Divacky static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) { 3232f22ef01cSRoman Divacky if (N->isLeaf()) 3233f22ef01cSRoman Divacky return false; 3234f22ef01cSRoman Divacky 3235f22ef01cSRoman Divacky // Analyze children. 3236f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 3237f22ef01cSRoman Divacky if (ForceArbitraryInstResultType(N->getChild(i), TP)) 3238f22ef01cSRoman Divacky return true; 3239f22ef01cSRoman Divacky 3240f22ef01cSRoman Divacky if (!N->getOperator()->isSubClassOf("Instruction")) 3241f22ef01cSRoman Divacky return false; 3242f22ef01cSRoman Divacky 3243f22ef01cSRoman Divacky // If this type is already concrete or completely unknown we can't do 3244f22ef01cSRoman Divacky // anything. 3245f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) { 3246f22ef01cSRoman Divacky if (N->getExtType(i).isCompletelyUnknown() || N->getExtType(i).isConcrete()) 3247f22ef01cSRoman Divacky continue; 3248f22ef01cSRoman Divacky 3249f22ef01cSRoman Divacky // Otherwise, force its type to the first possibility (an arbitrary choice). 3250f22ef01cSRoman Divacky if (N->getExtType(i).MergeInTypeInfo(N->getExtType(i).getTypeList()[0], TP)) 3251f22ef01cSRoman Divacky return true; 3252f22ef01cSRoman Divacky } 3253f22ef01cSRoman Divacky 3254f22ef01cSRoman Divacky return false; 3255f22ef01cSRoman Divacky } 3256f22ef01cSRoman Divacky 3257f22ef01cSRoman Divacky void CodeGenDAGPatterns::ParsePatterns() { 3258f22ef01cSRoman Divacky std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); 3259f22ef01cSRoman Divacky 3260f22ef01cSRoman Divacky for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { 3261f22ef01cSRoman Divacky Record *CurPattern = Patterns[i]; 3262f22ef01cSRoman Divacky DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch"); 32637ae0e2c9SDimitry Andric 32647ae0e2c9SDimitry Andric // If the pattern references the null_frag, there's nothing to do. 32657ae0e2c9SDimitry Andric if (hasNullFragReference(Tree)) 32667ae0e2c9SDimitry Andric continue; 32677ae0e2c9SDimitry Andric 3268f22ef01cSRoman Divacky TreePattern *Pattern = new TreePattern(CurPattern, Tree, true, *this); 3269f22ef01cSRoman Divacky 3270f22ef01cSRoman Divacky // Inline pattern fragments into it. 3271f22ef01cSRoman Divacky Pattern->InlinePatternFragments(); 3272f22ef01cSRoman Divacky 3273f22ef01cSRoman Divacky ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs"); 3274f22ef01cSRoman Divacky if (LI->getSize() == 0) continue; // no pattern. 3275f22ef01cSRoman Divacky 3276f22ef01cSRoman Divacky // Parse the instruction. 3277f22ef01cSRoman Divacky TreePattern *Result = new TreePattern(CurPattern, LI, false, *this); 3278f22ef01cSRoman Divacky 3279f22ef01cSRoman Divacky // Inline pattern fragments into it. 3280f22ef01cSRoman Divacky Result->InlinePatternFragments(); 3281f22ef01cSRoman Divacky 3282f22ef01cSRoman Divacky if (Result->getNumTrees() != 1) 3283f22ef01cSRoman Divacky Result->error("Cannot handle instructions producing instructions " 3284f22ef01cSRoman Divacky "with temporaries yet!"); 3285f22ef01cSRoman Divacky 3286f22ef01cSRoman Divacky bool IterateInference; 3287f22ef01cSRoman Divacky bool InferredAllPatternTypes, InferredAllResultTypes; 3288f22ef01cSRoman Divacky do { 3289f22ef01cSRoman Divacky // Infer as many types as possible. If we cannot infer all of them, we 3290f22ef01cSRoman Divacky // can never do anything with this pattern: report it to the user. 3291f22ef01cSRoman Divacky InferredAllPatternTypes = 3292f22ef01cSRoman Divacky Pattern->InferAllTypes(&Pattern->getNamedNodesMap()); 3293f22ef01cSRoman Divacky 3294f22ef01cSRoman Divacky // Infer as many types as possible. If we cannot infer all of them, we 3295f22ef01cSRoman Divacky // can never do anything with this pattern: report it to the user. 3296f22ef01cSRoman Divacky InferredAllResultTypes = 3297f22ef01cSRoman Divacky Result->InferAllTypes(&Pattern->getNamedNodesMap()); 3298f22ef01cSRoman Divacky 3299f22ef01cSRoman Divacky IterateInference = false; 3300f22ef01cSRoman Divacky 3301f22ef01cSRoman Divacky // Apply the type of the result to the source pattern. This helps us 3302f22ef01cSRoman Divacky // resolve cases where the input type is known to be a pointer type (which 3303f22ef01cSRoman Divacky // is considered resolved), but the result knows it needs to be 32- or 3304f22ef01cSRoman Divacky // 64-bits. Infer the other way for good measure. 3305f22ef01cSRoman Divacky for (unsigned i = 0, e = std::min(Result->getTree(0)->getNumTypes(), 3306f22ef01cSRoman Divacky Pattern->getTree(0)->getNumTypes()); 3307f22ef01cSRoman Divacky i != e; ++i) { 3308f22ef01cSRoman Divacky IterateInference = Pattern->getTree(0)-> 3309f22ef01cSRoman Divacky UpdateNodeType(i, Result->getTree(0)->getExtType(i), *Result); 3310f22ef01cSRoman Divacky IterateInference |= Result->getTree(0)-> 3311f22ef01cSRoman Divacky UpdateNodeType(i, Pattern->getTree(0)->getExtType(i), *Result); 3312f22ef01cSRoman Divacky } 3313f22ef01cSRoman Divacky 3314f22ef01cSRoman Divacky // If our iteration has converged and the input pattern's types are fully 3315f22ef01cSRoman Divacky // resolved but the result pattern is not fully resolved, we may have a 3316f22ef01cSRoman Divacky // situation where we have two instructions in the result pattern and 3317f22ef01cSRoman Divacky // the instructions require a common register class, but don't care about 3318f22ef01cSRoman Divacky // what actual MVT is used. This is actually a bug in our modelling: 3319f22ef01cSRoman Divacky // output patterns should have register classes, not MVTs. 3320f22ef01cSRoman Divacky // 3321f22ef01cSRoman Divacky // In any case, to handle this, we just go through and disambiguate some 3322f22ef01cSRoman Divacky // arbitrary types to the result pattern's nodes. 3323f22ef01cSRoman Divacky if (!IterateInference && InferredAllPatternTypes && 3324f22ef01cSRoman Divacky !InferredAllResultTypes) 3325f22ef01cSRoman Divacky IterateInference = ForceArbitraryInstResultType(Result->getTree(0), 3326f22ef01cSRoman Divacky *Result); 3327f22ef01cSRoman Divacky } while (IterateInference); 3328f22ef01cSRoman Divacky 3329f22ef01cSRoman Divacky // Verify that we inferred enough types that we can do something with the 3330f22ef01cSRoman Divacky // pattern and result. If these fire the user has to add type casts. 3331f22ef01cSRoman Divacky if (!InferredAllPatternTypes) 3332f22ef01cSRoman Divacky Pattern->error("Could not infer all types in pattern!"); 3333f22ef01cSRoman Divacky if (!InferredAllResultTypes) { 3334f22ef01cSRoman Divacky Pattern->dump(); 3335f22ef01cSRoman Divacky Result->error("Could not infer all types in pattern result!"); 3336f22ef01cSRoman Divacky } 3337f22ef01cSRoman Divacky 3338f22ef01cSRoman Divacky // Validate that the input pattern is correct. 3339f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> InstInputs; 3340f22ef01cSRoman Divacky std::map<std::string, TreePatternNode*> InstResults; 3341f22ef01cSRoman Divacky std::vector<Record*> InstImpResults; 3342f22ef01cSRoman Divacky for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j) 3343f22ef01cSRoman Divacky FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j), 3344f22ef01cSRoman Divacky InstInputs, InstResults, 3345f22ef01cSRoman Divacky InstImpResults); 3346f22ef01cSRoman Divacky 3347f22ef01cSRoman Divacky // Promote the xform function to be an explicit node if set. 3348f22ef01cSRoman Divacky TreePatternNode *DstPattern = Result->getOnlyTree(); 3349f22ef01cSRoman Divacky std::vector<TreePatternNode*> ResultNodeOperands; 3350f22ef01cSRoman Divacky for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) { 3351f22ef01cSRoman Divacky TreePatternNode *OpNode = DstPattern->getChild(ii); 3352f22ef01cSRoman Divacky if (Record *Xform = OpNode->getTransformFn()) { 335391bc56edSDimitry Andric OpNode->setTransformFn(nullptr); 3354f22ef01cSRoman Divacky std::vector<TreePatternNode*> Children; 3355f22ef01cSRoman Divacky Children.push_back(OpNode); 3356f22ef01cSRoman Divacky OpNode = new TreePatternNode(Xform, Children, OpNode->getNumTypes()); 3357f22ef01cSRoman Divacky } 3358f22ef01cSRoman Divacky ResultNodeOperands.push_back(OpNode); 3359f22ef01cSRoman Divacky } 3360f22ef01cSRoman Divacky DstPattern = Result->getOnlyTree(); 3361f22ef01cSRoman Divacky if (!DstPattern->isLeaf()) 3362f22ef01cSRoman Divacky DstPattern = new TreePatternNode(DstPattern->getOperator(), 3363f22ef01cSRoman Divacky ResultNodeOperands, 3364f22ef01cSRoman Divacky DstPattern->getNumTypes()); 3365f22ef01cSRoman Divacky 3366f22ef01cSRoman Divacky for (unsigned i = 0, e = Result->getOnlyTree()->getNumTypes(); i != e; ++i) 3367f22ef01cSRoman Divacky DstPattern->setType(i, Result->getOnlyTree()->getExtType(i)); 3368f22ef01cSRoman Divacky 3369f22ef01cSRoman Divacky TreePattern Temp(Result->getRecord(), DstPattern, false, *this); 3370f22ef01cSRoman Divacky Temp.InferAllTypes(); 3371f22ef01cSRoman Divacky 3372f22ef01cSRoman Divacky 3373f22ef01cSRoman Divacky AddPatternToMatch(Pattern, 33742754fe60SDimitry Andric PatternToMatch(CurPattern, 33752754fe60SDimitry Andric CurPattern->getValueAsListInit("Predicates"), 3376f22ef01cSRoman Divacky Pattern->getTree(0), 3377f22ef01cSRoman Divacky Temp.getOnlyTree(), InstImpResults, 3378f22ef01cSRoman Divacky CurPattern->getValueAsInt("AddedComplexity"), 3379f22ef01cSRoman Divacky CurPattern->getID())); 3380f22ef01cSRoman Divacky } 3381f22ef01cSRoman Divacky } 3382f22ef01cSRoman Divacky 3383f22ef01cSRoman Divacky /// CombineChildVariants - Given a bunch of permutations of each child of the 3384f22ef01cSRoman Divacky /// 'operator' node, put them together in all possible ways. 3385f22ef01cSRoman Divacky static void CombineChildVariants(TreePatternNode *Orig, 3386f22ef01cSRoman Divacky const std::vector<std::vector<TreePatternNode*> > &ChildVariants, 3387f22ef01cSRoman Divacky std::vector<TreePatternNode*> &OutVariants, 3388f22ef01cSRoman Divacky CodeGenDAGPatterns &CDP, 3389f22ef01cSRoman Divacky const MultipleUseVarSet &DepVars) { 3390f22ef01cSRoman Divacky // Make sure that each operand has at least one variant to choose from. 3391f22ef01cSRoman Divacky for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) 3392f22ef01cSRoman Divacky if (ChildVariants[i].empty()) 3393f22ef01cSRoman Divacky return; 3394f22ef01cSRoman Divacky 3395f22ef01cSRoman Divacky // The end result is an all-pairs construction of the resultant pattern. 3396f22ef01cSRoman Divacky std::vector<unsigned> Idxs; 3397f22ef01cSRoman Divacky Idxs.resize(ChildVariants.size()); 3398f22ef01cSRoman Divacky bool NotDone; 3399f22ef01cSRoman Divacky do { 3400f22ef01cSRoman Divacky #ifndef NDEBUG 3401f22ef01cSRoman Divacky DEBUG(if (!Idxs.empty()) { 3402f22ef01cSRoman Divacky errs() << Orig->getOperator()->getName() << ": Idxs = [ "; 3403f22ef01cSRoman Divacky for (unsigned i = 0; i < Idxs.size(); ++i) { 3404f22ef01cSRoman Divacky errs() << Idxs[i] << " "; 3405f22ef01cSRoman Divacky } 3406f22ef01cSRoman Divacky errs() << "]\n"; 3407f22ef01cSRoman Divacky }); 3408f22ef01cSRoman Divacky #endif 3409f22ef01cSRoman Divacky // Create the variant and add it to the output list. 3410f22ef01cSRoman Divacky std::vector<TreePatternNode*> NewChildren; 3411f22ef01cSRoman Divacky for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) 3412f22ef01cSRoman Divacky NewChildren.push_back(ChildVariants[i][Idxs[i]]); 3413f22ef01cSRoman Divacky TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren, 3414f22ef01cSRoman Divacky Orig->getNumTypes()); 3415f22ef01cSRoman Divacky 3416f22ef01cSRoman Divacky // Copy over properties. 3417f22ef01cSRoman Divacky R->setName(Orig->getName()); 3418f22ef01cSRoman Divacky R->setPredicateFns(Orig->getPredicateFns()); 3419f22ef01cSRoman Divacky R->setTransformFn(Orig->getTransformFn()); 3420f22ef01cSRoman Divacky for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i) 3421f22ef01cSRoman Divacky R->setType(i, Orig->getExtType(i)); 3422f22ef01cSRoman Divacky 3423f22ef01cSRoman Divacky // If this pattern cannot match, do not include it as a variant. 3424f22ef01cSRoman Divacky std::string ErrString; 3425f22ef01cSRoman Divacky if (!R->canPatternMatch(ErrString, CDP)) { 3426f22ef01cSRoman Divacky delete R; 3427f22ef01cSRoman Divacky } else { 3428f22ef01cSRoman Divacky bool AlreadyExists = false; 3429f22ef01cSRoman Divacky 3430f22ef01cSRoman Divacky // Scan to see if this pattern has already been emitted. We can get 3431f22ef01cSRoman Divacky // duplication due to things like commuting: 3432f22ef01cSRoman Divacky // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) 3433f22ef01cSRoman Divacky // which are the same pattern. Ignore the dups. 3434f22ef01cSRoman Divacky for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) 3435f22ef01cSRoman Divacky if (R->isIsomorphicTo(OutVariants[i], DepVars)) { 3436f22ef01cSRoman Divacky AlreadyExists = true; 3437f22ef01cSRoman Divacky break; 3438f22ef01cSRoman Divacky } 3439f22ef01cSRoman Divacky 3440f22ef01cSRoman Divacky if (AlreadyExists) 3441f22ef01cSRoman Divacky delete R; 3442f22ef01cSRoman Divacky else 3443f22ef01cSRoman Divacky OutVariants.push_back(R); 3444f22ef01cSRoman Divacky } 3445f22ef01cSRoman Divacky 3446f22ef01cSRoman Divacky // Increment indices to the next permutation by incrementing the 3447f22ef01cSRoman Divacky // indicies from last index backward, e.g., generate the sequence 3448f22ef01cSRoman Divacky // [0, 0], [0, 1], [1, 0], [1, 1]. 3449f22ef01cSRoman Divacky int IdxsIdx; 3450f22ef01cSRoman Divacky for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) { 3451f22ef01cSRoman Divacky if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size()) 3452f22ef01cSRoman Divacky Idxs[IdxsIdx] = 0; 3453f22ef01cSRoman Divacky else 3454f22ef01cSRoman Divacky break; 3455f22ef01cSRoman Divacky } 3456f22ef01cSRoman Divacky NotDone = (IdxsIdx >= 0); 3457f22ef01cSRoman Divacky } while (NotDone); 3458f22ef01cSRoman Divacky } 3459f22ef01cSRoman Divacky 3460f22ef01cSRoman Divacky /// CombineChildVariants - A helper function for binary operators. 3461f22ef01cSRoman Divacky /// 3462f22ef01cSRoman Divacky static void CombineChildVariants(TreePatternNode *Orig, 3463f22ef01cSRoman Divacky const std::vector<TreePatternNode*> &LHS, 3464f22ef01cSRoman Divacky const std::vector<TreePatternNode*> &RHS, 3465f22ef01cSRoman Divacky std::vector<TreePatternNode*> &OutVariants, 3466f22ef01cSRoman Divacky CodeGenDAGPatterns &CDP, 3467f22ef01cSRoman Divacky const MultipleUseVarSet &DepVars) { 3468f22ef01cSRoman Divacky std::vector<std::vector<TreePatternNode*> > ChildVariants; 3469f22ef01cSRoman Divacky ChildVariants.push_back(LHS); 3470f22ef01cSRoman Divacky ChildVariants.push_back(RHS); 3471f22ef01cSRoman Divacky CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars); 3472f22ef01cSRoman Divacky } 3473f22ef01cSRoman Divacky 3474f22ef01cSRoman Divacky 3475f22ef01cSRoman Divacky static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, 3476f22ef01cSRoman Divacky std::vector<TreePatternNode *> &Children) { 3477f22ef01cSRoman Divacky assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); 3478f22ef01cSRoman Divacky Record *Operator = N->getOperator(); 3479f22ef01cSRoman Divacky 3480f22ef01cSRoman Divacky // Only permit raw nodes. 3481f22ef01cSRoman Divacky if (!N->getName().empty() || !N->getPredicateFns().empty() || 3482f22ef01cSRoman Divacky N->getTransformFn()) { 3483f22ef01cSRoman Divacky Children.push_back(N); 3484f22ef01cSRoman Divacky return; 3485f22ef01cSRoman Divacky } 3486f22ef01cSRoman Divacky 3487f22ef01cSRoman Divacky if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) 3488f22ef01cSRoman Divacky Children.push_back(N->getChild(0)); 3489f22ef01cSRoman Divacky else 3490f22ef01cSRoman Divacky GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); 3491f22ef01cSRoman Divacky 3492f22ef01cSRoman Divacky if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) 3493f22ef01cSRoman Divacky Children.push_back(N->getChild(1)); 3494f22ef01cSRoman Divacky else 3495f22ef01cSRoman Divacky GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); 3496f22ef01cSRoman Divacky } 3497f22ef01cSRoman Divacky 3498f22ef01cSRoman Divacky /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of 3499f22ef01cSRoman Divacky /// the (potentially recursive) pattern by using algebraic laws. 3500f22ef01cSRoman Divacky /// 3501f22ef01cSRoman Divacky static void GenerateVariantsOf(TreePatternNode *N, 3502f22ef01cSRoman Divacky std::vector<TreePatternNode*> &OutVariants, 3503f22ef01cSRoman Divacky CodeGenDAGPatterns &CDP, 3504f22ef01cSRoman Divacky const MultipleUseVarSet &DepVars) { 350591bc56edSDimitry Andric // We cannot permute leaves or ComplexPattern uses. 350691bc56edSDimitry Andric if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) { 3507f22ef01cSRoman Divacky OutVariants.push_back(N); 3508f22ef01cSRoman Divacky return; 3509f22ef01cSRoman Divacky } 3510f22ef01cSRoman Divacky 3511f22ef01cSRoman Divacky // Look up interesting info about the node. 3512f22ef01cSRoman Divacky const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator()); 3513f22ef01cSRoman Divacky 3514f22ef01cSRoman Divacky // If this node is associative, re-associate. 3515f22ef01cSRoman Divacky if (NodeInfo.hasProperty(SDNPAssociative)) { 3516f22ef01cSRoman Divacky // Re-associate by pulling together all of the linked operators 3517f22ef01cSRoman Divacky std::vector<TreePatternNode*> MaximalChildren; 3518f22ef01cSRoman Divacky GatherChildrenOfAssociativeOpcode(N, MaximalChildren); 3519f22ef01cSRoman Divacky 3520f22ef01cSRoman Divacky // Only handle child sizes of 3. Otherwise we'll end up trying too many 3521f22ef01cSRoman Divacky // permutations. 3522f22ef01cSRoman Divacky if (MaximalChildren.size() == 3) { 3523f22ef01cSRoman Divacky // Find the variants of all of our maximal children. 3524f22ef01cSRoman Divacky std::vector<TreePatternNode*> AVariants, BVariants, CVariants; 3525f22ef01cSRoman Divacky GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars); 3526f22ef01cSRoman Divacky GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars); 3527f22ef01cSRoman Divacky GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars); 3528f22ef01cSRoman Divacky 3529f22ef01cSRoman Divacky // There are only two ways we can permute the tree: 3530f22ef01cSRoman Divacky // (A op B) op C and A op (B op C) 3531f22ef01cSRoman Divacky // Within these forms, we can also permute A/B/C. 3532f22ef01cSRoman Divacky 3533f22ef01cSRoman Divacky // Generate legal pair permutations of A/B/C. 3534f22ef01cSRoman Divacky std::vector<TreePatternNode*> ABVariants; 3535f22ef01cSRoman Divacky std::vector<TreePatternNode*> BAVariants; 3536f22ef01cSRoman Divacky std::vector<TreePatternNode*> ACVariants; 3537f22ef01cSRoman Divacky std::vector<TreePatternNode*> CAVariants; 3538f22ef01cSRoman Divacky std::vector<TreePatternNode*> BCVariants; 3539f22ef01cSRoman Divacky std::vector<TreePatternNode*> CBVariants; 3540f22ef01cSRoman Divacky CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars); 3541f22ef01cSRoman Divacky CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars); 3542f22ef01cSRoman Divacky CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars); 3543f22ef01cSRoman Divacky CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars); 3544f22ef01cSRoman Divacky CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars); 3545f22ef01cSRoman Divacky CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars); 3546f22ef01cSRoman Divacky 3547f22ef01cSRoman Divacky // Combine those into the result: (x op x) op x 3548f22ef01cSRoman Divacky CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars); 3549f22ef01cSRoman Divacky CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars); 3550f22ef01cSRoman Divacky CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars); 3551f22ef01cSRoman Divacky CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars); 3552f22ef01cSRoman Divacky CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars); 3553f22ef01cSRoman Divacky CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars); 3554f22ef01cSRoman Divacky 3555f22ef01cSRoman Divacky // Combine those into the result: x op (x op x) 3556f22ef01cSRoman Divacky CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars); 3557f22ef01cSRoman Divacky CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars); 3558f22ef01cSRoman Divacky CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars); 3559f22ef01cSRoman Divacky CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars); 3560f22ef01cSRoman Divacky CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars); 3561f22ef01cSRoman Divacky CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars); 3562f22ef01cSRoman Divacky return; 3563f22ef01cSRoman Divacky } 3564f22ef01cSRoman Divacky } 3565f22ef01cSRoman Divacky 3566f22ef01cSRoman Divacky // Compute permutations of all children. 3567f22ef01cSRoman Divacky std::vector<std::vector<TreePatternNode*> > ChildVariants; 3568f22ef01cSRoman Divacky ChildVariants.resize(N->getNumChildren()); 3569f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) 3570f22ef01cSRoman Divacky GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars); 3571f22ef01cSRoman Divacky 3572f22ef01cSRoman Divacky // Build all permutations based on how the children were formed. 3573f22ef01cSRoman Divacky CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars); 3574f22ef01cSRoman Divacky 3575f22ef01cSRoman Divacky // If this node is commutative, consider the commuted order. 3576f22ef01cSRoman Divacky bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP); 3577f22ef01cSRoman Divacky if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { 3578f22ef01cSRoman Divacky assert((N->getNumChildren()==2 || isCommIntrinsic) && 3579f22ef01cSRoman Divacky "Commutative but doesn't have 2 children!"); 3580f22ef01cSRoman Divacky // Don't count children which are actually register references. 3581f22ef01cSRoman Divacky unsigned NC = 0; 3582f22ef01cSRoman Divacky for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { 3583f22ef01cSRoman Divacky TreePatternNode *Child = N->getChild(i); 3584f22ef01cSRoman Divacky if (Child->isLeaf()) 35853861d79fSDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) { 3586f22ef01cSRoman Divacky Record *RR = DI->getDef(); 3587f22ef01cSRoman Divacky if (RR->isSubClassOf("Register")) 3588f22ef01cSRoman Divacky continue; 3589f22ef01cSRoman Divacky } 3590f22ef01cSRoman Divacky NC++; 3591f22ef01cSRoman Divacky } 3592f22ef01cSRoman Divacky // Consider the commuted order. 3593f22ef01cSRoman Divacky if (isCommIntrinsic) { 3594f22ef01cSRoman Divacky // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd 3595f22ef01cSRoman Divacky // operands are the commutative operands, and there might be more operands 3596f22ef01cSRoman Divacky // after those. 3597f22ef01cSRoman Divacky assert(NC >= 3 && 3598f22ef01cSRoman Divacky "Commutative intrinsic should have at least 3 childrean!"); 3599f22ef01cSRoman Divacky std::vector<std::vector<TreePatternNode*> > Variants; 3600f22ef01cSRoman Divacky Variants.push_back(ChildVariants[0]); // Intrinsic id. 3601f22ef01cSRoman Divacky Variants.push_back(ChildVariants[2]); 3602f22ef01cSRoman Divacky Variants.push_back(ChildVariants[1]); 3603f22ef01cSRoman Divacky for (unsigned i = 3; i != NC; ++i) 3604f22ef01cSRoman Divacky Variants.push_back(ChildVariants[i]); 3605f22ef01cSRoman Divacky CombineChildVariants(N, Variants, OutVariants, CDP, DepVars); 3606f22ef01cSRoman Divacky } else if (NC == 2) 3607f22ef01cSRoman Divacky CombineChildVariants(N, ChildVariants[1], ChildVariants[0], 3608f22ef01cSRoman Divacky OutVariants, CDP, DepVars); 3609f22ef01cSRoman Divacky } 3610f22ef01cSRoman Divacky } 3611f22ef01cSRoman Divacky 3612f22ef01cSRoman Divacky 3613f22ef01cSRoman Divacky // GenerateVariants - Generate variants. For example, commutative patterns can 3614f22ef01cSRoman Divacky // match multiple ways. Add them to PatternsToMatch as well. 3615f22ef01cSRoman Divacky void CodeGenDAGPatterns::GenerateVariants() { 3616f22ef01cSRoman Divacky DEBUG(errs() << "Generating instruction variants.\n"); 3617f22ef01cSRoman Divacky 3618f22ef01cSRoman Divacky // Loop over all of the patterns we've collected, checking to see if we can 3619f22ef01cSRoman Divacky // generate variants of the instruction, through the exploitation of 3620f22ef01cSRoman Divacky // identities. This permits the target to provide aggressive matching without 3621f22ef01cSRoman Divacky // the .td file having to contain tons of variants of instructions. 3622f22ef01cSRoman Divacky // 3623f22ef01cSRoman Divacky // Note that this loop adds new patterns to the PatternsToMatch list, but we 3624f22ef01cSRoman Divacky // intentionally do not reconsider these. Any variants of added patterns have 3625f22ef01cSRoman Divacky // already been added. 3626f22ef01cSRoman Divacky // 3627f22ef01cSRoman Divacky for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { 3628f22ef01cSRoman Divacky MultipleUseVarSet DepVars; 3629f22ef01cSRoman Divacky std::vector<TreePatternNode*> Variants; 3630f22ef01cSRoman Divacky FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars); 3631f22ef01cSRoman Divacky DEBUG(errs() << "Dependent/multiply used variables: "); 3632f22ef01cSRoman Divacky DEBUG(DumpDepVars(DepVars)); 3633f22ef01cSRoman Divacky DEBUG(errs() << "\n"); 36342754fe60SDimitry Andric GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this, 36352754fe60SDimitry Andric DepVars); 3636f22ef01cSRoman Divacky 3637f22ef01cSRoman Divacky assert(!Variants.empty() && "Must create at least original variant!"); 3638f22ef01cSRoman Divacky Variants.erase(Variants.begin()); // Remove the original pattern. 3639f22ef01cSRoman Divacky 3640f22ef01cSRoman Divacky if (Variants.empty()) // No variants for this pattern. 3641f22ef01cSRoman Divacky continue; 3642f22ef01cSRoman Divacky 3643f22ef01cSRoman Divacky DEBUG(errs() << "FOUND VARIANTS OF: "; 3644f22ef01cSRoman Divacky PatternsToMatch[i].getSrcPattern()->dump(); 3645f22ef01cSRoman Divacky errs() << "\n"); 3646f22ef01cSRoman Divacky 3647f22ef01cSRoman Divacky for (unsigned v = 0, e = Variants.size(); v != e; ++v) { 3648f22ef01cSRoman Divacky TreePatternNode *Variant = Variants[v]; 3649f22ef01cSRoman Divacky 3650f22ef01cSRoman Divacky DEBUG(errs() << " VAR#" << v << ": "; 3651f22ef01cSRoman Divacky Variant->dump(); 3652f22ef01cSRoman Divacky errs() << "\n"); 3653f22ef01cSRoman Divacky 3654f22ef01cSRoman Divacky // Scan to see if an instruction or explicit pattern already matches this. 3655f22ef01cSRoman Divacky bool AlreadyExists = false; 3656f22ef01cSRoman Divacky for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { 3657f22ef01cSRoman Divacky // Skip if the top level predicates do not match. 3658f22ef01cSRoman Divacky if (PatternsToMatch[i].getPredicates() != 3659f22ef01cSRoman Divacky PatternsToMatch[p].getPredicates()) 3660f22ef01cSRoman Divacky continue; 3661f22ef01cSRoman Divacky // Check to see if this variant already exists. 36622754fe60SDimitry Andric if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(), 36632754fe60SDimitry Andric DepVars)) { 3664f22ef01cSRoman Divacky DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n"); 3665f22ef01cSRoman Divacky AlreadyExists = true; 3666f22ef01cSRoman Divacky break; 3667f22ef01cSRoman Divacky } 3668f22ef01cSRoman Divacky } 3669f22ef01cSRoman Divacky // If we already have it, ignore the variant. 3670f22ef01cSRoman Divacky if (AlreadyExists) continue; 3671f22ef01cSRoman Divacky 3672f22ef01cSRoman Divacky // Otherwise, add it to the list of patterns we have. 3673f22ef01cSRoman Divacky PatternsToMatch. 36742754fe60SDimitry Andric push_back(PatternToMatch(PatternsToMatch[i].getSrcRecord(), 36752754fe60SDimitry Andric PatternsToMatch[i].getPredicates(), 3676f22ef01cSRoman Divacky Variant, PatternsToMatch[i].getDstPattern(), 3677f22ef01cSRoman Divacky PatternsToMatch[i].getDstRegs(), 3678f22ef01cSRoman Divacky PatternsToMatch[i].getAddedComplexity(), 3679f22ef01cSRoman Divacky Record::getNewUID())); 3680f22ef01cSRoman Divacky } 3681f22ef01cSRoman Divacky 3682f22ef01cSRoman Divacky DEBUG(errs() << "\n"); 3683f22ef01cSRoman Divacky } 3684f22ef01cSRoman Divacky } 3685