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