blob: 5b047bc182d56c4b271c9c376a649a94c6d91eab [file] [log] [blame]
Chris Lattnerab3242f2008-01-06 01:10:31 +00001//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
Chris Lattner8cab0212008-01-05 22:25:12 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerab3242f2008-01-06 01:10:31 +000010// This file declares the CodeGenDAGPatterns class, which is used to read and
Chris Lattner8cab0212008-01-05 22:25:12 +000011// represent the patterns present in a .td file for instructions.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
16#define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
Chris Lattner8cab0212008-01-05 22:25:12 +000017
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000018#include "CodeGenHwModes.h"
Chris Lattner8cab0212008-01-05 22:25:12 +000019#include "CodeGenIntrinsics.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "CodeGenTarget.h"
Chris Lattnercabe0372010-03-15 06:00:16 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringMap.h"
Zachary Turner249dc142017-09-20 18:01:40 +000023#include "llvm/ADT/StringSet.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000024#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner1802b172010-03-19 01:07:44 +000026#include <algorithm>
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000027#include <array>
Chris Lattner1802b172010-03-19 01:07:44 +000028#include <map>
Chandler Carruth91d19d82012-12-04 10:37:14 +000029#include <set>
30#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000031
32namespace llvm {
Chris Lattner8cab0212008-01-05 22:25:12 +000033
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000034class Record;
35class Init;
36class ListInit;
37class DagInit;
38class SDNodeInfo;
39class TreePattern;
40class TreePatternNode;
41class CodeGenDAGPatterns;
42class ComplexPattern;
43
44/// This represents a set of MVTs. Since the underlying type for the MVT
45/// is uint8_t, there are at most 256 values. To reduce the number of memory
46/// allocations and deallocations, represent the set as a sequence of bits.
47/// To reduce the allocations even further, make MachineValueTypeSet own
48/// the storage and use std::array as the bit container.
49struct MachineValueTypeSet {
50 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
51 uint8_t>::value,
52 "Change uint8_t here to the SimpleValueType's type");
53 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
54 using WordType = uint64_t;
Craig Topperd022d252017-09-21 04:55:04 +000055 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000056 static unsigned constexpr NumWords = Capacity/WordWidth;
57 static_assert(NumWords*WordWidth == Capacity,
58 "Capacity should be a multiple of WordWidth");
59
60 LLVM_ATTRIBUTE_ALWAYS_INLINE
61 MachineValueTypeSet() {
62 clear();
63 }
64
65 LLVM_ATTRIBUTE_ALWAYS_INLINE
66 unsigned size() const {
67 unsigned Count = 0;
68 for (WordType W : Words)
69 Count += countPopulation(W);
70 return Count;
71 }
72 LLVM_ATTRIBUTE_ALWAYS_INLINE
73 void clear() {
74 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
75 }
76 LLVM_ATTRIBUTE_ALWAYS_INLINE
77 bool empty() const {
78 for (WordType W : Words)
79 if (W != 0)
80 return false;
81 return true;
82 }
83 LLVM_ATTRIBUTE_ALWAYS_INLINE
84 unsigned count(MVT T) const {
85 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
86 }
87 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
88 bool V = count(T.SimpleTy);
89 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
90 return {*this, V};
91 }
92 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
93 for (unsigned i = 0; i != NumWords; ++i)
94 Words[i] |= S.Words[i];
95 return *this;
96 }
97 LLVM_ATTRIBUTE_ALWAYS_INLINE
98 void erase(MVT T) {
99 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
100 }
101
102 struct const_iterator {
103 // Some implementations of the C++ library require these traits to be
104 // defined.
105 using iterator_category = std::forward_iterator_tag;
106 using value_type = MVT;
107 using difference_type = ptrdiff_t;
108 using pointer = const MVT*;
109 using reference = const MVT&;
110
111 LLVM_ATTRIBUTE_ALWAYS_INLINE
112 MVT operator*() const {
113 assert(Pos != Capacity);
114 return MVT::SimpleValueType(Pos);
115 }
116 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
118 Pos = End ? Capacity : find_from_pos(0);
119 }
120 LLVM_ATTRIBUTE_ALWAYS_INLINE
121 const_iterator &operator++() {
122 assert(Pos != Capacity);
123 Pos = find_from_pos(Pos+1);
124 return *this;
125 }
126
127 LLVM_ATTRIBUTE_ALWAYS_INLINE
128 bool operator==(const const_iterator &It) const {
129 return Set == It.Set && Pos == It.Pos;
130 }
131 LLVM_ATTRIBUTE_ALWAYS_INLINE
132 bool operator!=(const const_iterator &It) const {
133 return !operator==(It);
134 }
135
136 private:
137 unsigned find_from_pos(unsigned P) const {
138 unsigned SkipWords = P / WordWidth;
139 unsigned SkipBits = P % WordWidth;
140 unsigned Count = SkipWords * WordWidth;
141
142 // If P is in the middle of a word, process it manually here, because
143 // the trailing bits need to be masked off to use findFirstSet.
144 if (SkipBits != 0) {
145 WordType W = Set->Words[SkipWords];
146 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
147 if (W != 0)
148 return Count + findFirstSet(W);
149 Count += WordWidth;
150 SkipWords++;
151 }
152
153 for (unsigned i = SkipWords; i != NumWords; ++i) {
154 WordType W = Set->Words[i];
155 if (W != 0)
156 return Count + findFirstSet(W);
157 Count += WordWidth;
158 }
159 return Capacity;
160 }
161
162 const MachineValueTypeSet *Set;
163 unsigned Pos;
164 };
165
166 LLVM_ATTRIBUTE_ALWAYS_INLINE
167 const_iterator begin() const { return const_iterator(this, false); }
168 LLVM_ATTRIBUTE_ALWAYS_INLINE
169 const_iterator end() const { return const_iterator(this, true); }
170
171 LLVM_ATTRIBUTE_ALWAYS_INLINE
172 bool operator==(const MachineValueTypeSet &S) const {
173 return Words == S.Words;
174 }
175 LLVM_ATTRIBUTE_ALWAYS_INLINE
176 bool operator!=(const MachineValueTypeSet &S) const {
177 return !operator==(S);
178 }
179
180private:
181 friend struct const_iterator;
182 std::array<WordType,NumWords> Words;
183};
184
185struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
186 using SetType = MachineValueTypeSet;
Jim Grosbach50986b52010-12-24 05:06:32 +0000187
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000188 TypeSetByHwMode() = default;
189 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
190 TypeSetByHwMode(MVT::SimpleValueType VT)
191 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
192 TypeSetByHwMode(ValueTypeByHwMode VT)
193 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
194 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000195
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000196 SetType &getOrCreate(unsigned Mode) {
197 if (hasMode(Mode))
198 return get(Mode);
199 return Map.insert({Mode,SetType()}).first->second;
200 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000201
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000202 bool isValueTypeByHwMode(bool AllowEmpty) const;
203 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000204
205 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000206 bool isMachineValueType() const {
207 return isDefaultOnly() && Map.begin()->second.size() == 1;
208 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000209
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000210 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000211 MVT getMachineValueType() const {
212 assert(isMachineValueType());
213 return *Map.begin()->second.begin();
214 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000215
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000216 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000217
218 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000219 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000220 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000221 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000222
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000223 bool insert(const ValueTypeByHwMode &VVT);
224 bool constrain(const TypeSetByHwMode &VTS);
225 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000226 template <typename Predicate>
227 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000228
Zachary Turner249dc142017-09-20 18:01:40 +0000229 void writeToStream(raw_ostream &OS) const;
230 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000231
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000232 bool operator==(const TypeSetByHwMode &VTS) const;
233 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000234
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000235 void dump() const;
236 void validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000237
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000238private:
239 /// Intersect two sets. Return true if anything has changed.
240 bool intersect(SetType &Out, const SetType &In);
241};
Jim Grosbach50986b52010-12-24 05:06:32 +0000242
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000243raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
244
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000245struct TypeInfer {
246 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000247
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000248 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
249 return VTS.isValueTypeByHwMode(AllowEmpty);
250 }
251 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
252 bool AllowEmpty) const {
253 assert(VTS.isValueTypeByHwMode(AllowEmpty));
254 return VTS.getValueTypeByHwMode();
255 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000256
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000257 /// The protocol in the following functions (Merge*, force*, Enforce*,
258 /// expand*) is to return "true" if a change has been made, "false"
259 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000260
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000261 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
262 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
263 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
264 }
265 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
266 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
267 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000268
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000269 /// Reduce the set \p Out to have at most one element for each mode.
270 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000271
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000272 /// The following four functions ensure that upon return the set \p Out
273 /// will only contain types of the specified kind: integer, floating-point,
274 /// scalar, or vector.
275 /// If \p Out is empty, all legal types of the specified kind will be added
276 /// to it. Otherwise, all types that are not of the specified kind will be
277 /// removed from \p Out.
278 bool EnforceInteger(TypeSetByHwMode &Out);
279 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
280 bool EnforceScalar(TypeSetByHwMode &Out);
281 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000282
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000283 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
284 /// unchanged.
285 bool EnforceAny(TypeSetByHwMode &Out);
286 /// Make sure that for each type in \p Small, there exists a larger type
287 /// in \p Big.
288 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
289 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
290 /// for each type U in \p Elem, U is a scalar type.
291 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
292 /// (vector) type T in \p Vec, such that U is the element type of T.
293 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
294 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
295 const ValueTypeByHwMode &VVT);
296 /// Ensure that for each type T in \p Sub, T is a vector type, and there
297 /// exists a type U in \p Vec such that U is a vector type with the same
298 /// element type as T and at least as many elements as T.
299 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
300 TypeSetByHwMode &Sub);
301 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
302 /// 2. Ensure that for each vector type T in \p V, there exists a vector
303 /// type U in \p W, such that T and U have the same number of elements.
304 /// 3. Ensure that for each vector type U in \p W, there exists a vector
305 /// type T in \p V, such that T and U have the same number of elements
306 /// (reverse of 2).
307 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
308 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
309 /// such that T and U have equal size in bits.
310 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
311 /// such that T and U have equal size in bits (reverse of 1).
312 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000313
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000314 /// For each overloaded type (i.e. of form *Any), replace it with the
315 /// corresponding subset of legal, specific types.
316 void expandOverloads(TypeSetByHwMode &VTS);
317 void expandOverloads(TypeSetByHwMode::SetType &Out,
318 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000319
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000320 struct ValidateOnExit {
321 ValidateOnExit(TypeSetByHwMode &T) : VTS(T) {}
322 ~ValidateOnExit() { VTS.validate(); }
323 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000324 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000325
326 TreePattern &TP;
327 unsigned ForceMode; // Mode to use when set.
328 bool CodeGen = false; // Set during generation of matcher code.
329
330private:
331 TypeSetByHwMode getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000332
333 /// Cached legal types.
334 bool LegalTypesCached = false;
335 TypeSetByHwMode::SetType LegalCache = {};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000336};
Chris Lattner8cab0212008-01-05 22:25:12 +0000337
Scott Michel94420742008-03-05 17:49:05 +0000338/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000339typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000340
Chris Lattner8cab0212008-01-05 22:25:12 +0000341/// SDTypeConstraint - This is a discriminated union of constraints,
342/// corresponding to the SDTypeConstraint tablegen class in Target.td.
343struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000344 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000345
Chris Lattner8cab0212008-01-05 22:25:12 +0000346 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000347 enum {
348 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000349 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000350 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000351 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000352
Chris Lattner8cab0212008-01-05 22:25:12 +0000353 union { // The discriminated union.
354 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000355 unsigned OtherOperandNum;
356 } SDTCisSameAs_Info;
357 struct {
358 unsigned OtherOperandNum;
359 } SDTCisVTSmallerThanOp_Info;
360 struct {
361 unsigned BigOperandNum;
362 } SDTCisOpSmallerThanOp_Info;
363 struct {
364 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000365 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000366 struct {
367 unsigned OtherOperandNum;
368 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000369 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000370 unsigned OtherOperandNum;
371 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000372 struct {
373 unsigned OtherOperandNum;
374 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000375 } x;
376
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000377 // The VT for SDTCisVT and SDTCVecEltisVT.
378 // Must not be in the union because it has a non-trivial destructor.
379 ValueTypeByHwMode VVT;
380
Chris Lattner8cab0212008-01-05 22:25:12 +0000381 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
382 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000383 /// change, false otherwise. If a type contradiction is found, an error
384 /// is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000385 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
386 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000387};
388
389/// SDNodeInfo - One of these records is created for each SDNode instance in
390/// the target .td file. This represents the various dag nodes we will be
391/// processing.
392class SDNodeInfo {
393 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000394 StringRef EnumName;
395 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000396 unsigned Properties;
397 unsigned NumResults;
398 int NumOperands;
399 std::vector<SDTypeConstraint> TypeConstraints;
400public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000401 // Parse the specified record.
402 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000403
Chris Lattner8cab0212008-01-05 22:25:12 +0000404 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000405
Chris Lattner135091b2010-03-28 08:48:47 +0000406 /// getNumOperands - This is the number of operands required or -1 if
407 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000408 int getNumOperands() const { return NumOperands; }
409 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000410 StringRef getEnumName() const { return EnumName; }
411 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000412
Chris Lattner8cab0212008-01-05 22:25:12 +0000413 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
414 return TypeConstraints;
415 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000416
Chris Lattner99e53b32010-02-28 00:22:30 +0000417 /// getKnownType - If the type constraints on this node imply a fixed type
418 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000419 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000420 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000421
Chris Lattner8cab0212008-01-05 22:25:12 +0000422 /// hasProperty - Return true if this node has the specified property.
423 ///
424 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
425
426 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
427 /// constraints for this node to the operands of the node. This returns
428 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000429 /// found, an error is flagged.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000430 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000431};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000432
Chris Lattner514e2922011-04-17 21:38:24 +0000433/// TreePredicateFn - This is an abstraction that represents the predicates on
434/// a PatFrag node. This is a simple one-word wrapper around a pointer to
435/// provide nice accessors.
436class TreePredicateFn {
437 /// PatFragRec - This is the TreePattern for the PatFrag that we
438 /// originally came from.
439 TreePattern *PatFragRec;
440public:
441 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000442 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000443
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000444
Chris Lattner514e2922011-04-17 21:38:24 +0000445 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000446
Chris Lattner514e2922011-04-17 21:38:24 +0000447 /// isAlwaysTrue - Return true if this is a noop predicate.
448 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000449
Chris Lattner07add492011-04-18 06:22:33 +0000450 bool isImmediatePattern() const { return !getImmCode().empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000451
Chris Lattner07add492011-04-18 06:22:33 +0000452 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
453 /// this is an immediate predicate. It is an error to call this on a
454 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000455 std::string getImmediatePredicateCode() const {
456 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000457 assert(!Result.empty() && "Isn't an immediate pattern!");
458 return Result;
459 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000460
Chris Lattner514e2922011-04-17 21:38:24 +0000461 bool operator==(const TreePredicateFn &RHS) const {
462 return PatFragRec == RHS.PatFragRec;
463 }
464
465 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
466
467 /// Return the name to use in the generated code to reference this, this is
468 /// "Predicate_foo" if from a pattern fragment "foo".
469 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000470
Chris Lattner514e2922011-04-17 21:38:24 +0000471 /// getCodeToRunOnSDNode - Return the code for the function body that
472 /// evaluates this predicate. The argument is expected to be in "Node",
473 /// not N. This handles casting and conversion to a concrete node type as
474 /// appropriate.
475 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000476
Daniel Sanders649c5852017-10-13 20:42:18 +0000477 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000478 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000479
Daniel Sanders11300ce2017-10-13 21:28:03 +0000480 /// Get a string that describes the type returned by getImmType() but is
481 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000482 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000483
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000484 // Is the desired predefined predicate for a load?
485 bool isLoad() const;
486 // Is the desired predefined predicate for a store?
487 bool isStore() const;
488
489 /// Is this predicate the predefined unindexed load predicate?
490 /// Is this predicate the predefined unindexed store predicate?
491 bool isUnindexed() const;
492 /// Is this predicate the predefined non-extending load predicate?
493 bool isNonExtLoad() const;
494 /// Is this predicate the predefined any-extend load predicate?
495 bool isAnyExtLoad() const;
496 /// Is this predicate the predefined sign-extend load predicate?
497 bool isSignExtLoad() const;
498 /// Is this predicate the predefined zero-extend load predicate?
499 bool isZeroExtLoad() const;
500 /// Is this predicate the predefined non-truncating store predicate?
501 bool isNonTruncStore() const;
502 /// Is this predicate the predefined truncating store predicate?
503 bool isTruncStore() const;
504
505 /// If non-null, indicates that this predicate is a predefined memory VT
506 /// predicate for a load/store and returns the ValueType record for the memory VT.
507 Record *getMemoryVT() const;
508 /// If non-null, indicates that this predicate is a predefined memory VT
509 /// predicate (checking only the scalar type) for load/store and returns the
510 /// ValueType record for the memory VT.
511 Record *getScalarMemoryVT() const;
512
Chris Lattner514e2922011-04-17 21:38:24 +0000513private:
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000514 std::string getPredCode() const;
515 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000516 bool immCodeUsesAPInt() const;
517 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000518
519 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000520};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000521
Chris Lattner8cab0212008-01-05 22:25:12 +0000522
523/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
524/// patterns), and as such should be ref counted. We currently just leak all
525/// TreePatternNode objects!
526class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000527 /// The type of each node result. Before and during type inference, each
528 /// result may be a set of possible types. After (successful) type inference,
529 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000530 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000531
Chris Lattner8cab0212008-01-05 22:25:12 +0000532 /// Operator - The Record for the operator if this is an interior node (not
533 /// a leaf).
534 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000535
Chris Lattner8cab0212008-01-05 22:25:12 +0000536 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
537 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000538 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000539
Chris Lattner8cab0212008-01-05 22:25:12 +0000540 /// Name - The name given to this node with the :$foo notation.
541 ///
542 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000543
Dan Gohman6e979022008-10-15 06:17:21 +0000544 /// PredicateFns - The predicate functions to execute on this node to check
545 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000546 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000547
Chris Lattner8cab0212008-01-05 22:25:12 +0000548 /// TransformFn - The transformation function to execute on this node before
549 /// it can be substituted into the resulting instruction on a pattern match.
550 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000551
Chris Lattner8cab0212008-01-05 22:25:12 +0000552 std::vector<TreePatternNode*> Children;
553public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000554 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000555 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000556 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000557 Types.resize(NumResults);
558 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000559 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000560 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000561 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000562 }
563 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000564
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000565 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000566 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000567 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000568
Craig Topperada08572014-04-16 04:21:27 +0000569 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000570
Chris Lattnercabe0372010-03-15 06:00:16 +0000571 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000572 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000573 ValueTypeByHwMode getType(unsigned ResNo) const {
574 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000575 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000576 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
577 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
578 return Types[ResNo];
579 }
580 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
581 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
582 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
583 return Types[ResNo].getMachineValueType().SimpleTy;
584 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000585
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000586 bool hasConcreteType(unsigned ResNo) const {
587 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000588 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000589 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
590 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000591 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000592
David Greeneaf8ee2c2011-07-29 22:43:06 +0000593 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000594 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000595
Chris Lattner8cab0212008-01-05 22:25:12 +0000596 unsigned getNumChildren() const { return Children.size(); }
597 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
598 void setChild(unsigned i, TreePatternNode *N) {
599 Children[i] = N;
600 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000601
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000602 /// hasChild - Return true if N is any of our children.
603 bool hasChild(const TreePatternNode *N) const {
604 for (unsigned i = 0, e = Children.size(); i != e; ++i)
605 if (Children[i] == N) return true;
606 return false;
607 }
Chris Lattner89c65662008-01-06 05:36:50 +0000608
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000609 bool hasProperTypeByHwMode() const;
610 bool hasPossibleType() const;
611 bool setDefaultMode(unsigned Mode);
612
Chris Lattner514e2922011-04-17 21:38:24 +0000613 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000614
Chris Lattner514e2922011-04-17 21:38:24 +0000615 const std::vector<TreePredicateFn> &getPredicateFns() const {
616 return PredicateFns;
617 }
Dan Gohman6e979022008-10-15 06:17:21 +0000618 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000619 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000620 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
621 PredicateFns = Fns;
622 }
Chris Lattner514e2922011-04-17 21:38:24 +0000623 void addPredicateFn(const TreePredicateFn &Fn) {
624 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000625 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000626 PredicateFns.push_back(Fn);
627 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000628
629 Record *getTransformFn() const { return TransformFn; }
630 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000631
Chris Lattner89c65662008-01-06 05:36:50 +0000632 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
633 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
634 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000635
Chris Lattner53c39ba2010-02-14 22:22:58 +0000636 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
637 /// return the ComplexPattern information, otherwise return null.
638 const ComplexPattern *
639 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
640
Tim Northoverc807a172014-05-20 11:52:46 +0000641 /// Returns the number of MachineInstr operands that would be produced by this
642 /// node if it mapped directly to an output Instruction's
643 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
644 /// for Operands; otherwise 1.
645 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
646
Chris Lattner53c39ba2010-02-14 22:22:58 +0000647 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000648 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000649
Chris Lattner53c39ba2010-02-14 22:22:58 +0000650 /// TreeHasProperty - Return true if any node in this tree has the specified
651 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000652 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000653
Evan Cheng49bad4c2008-06-16 20:29:38 +0000654 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
655 /// marked isCommutative.
656 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000657
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000658 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000659 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000660
Chris Lattner8cab0212008-01-05 22:25:12 +0000661public: // Higher level manipulation routines.
662
663 /// clone - Return a new copy of this tree.
664 ///
665 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000666
667 /// RemoveAllTypes - Recursively strip all the types of this tree.
668 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000669
Chris Lattner8cab0212008-01-05 22:25:12 +0000670 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
671 /// the specified node. For this comparison, all of the state of the node
672 /// is considered, except for the assigned name. Nodes with differing names
673 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000674 bool isIsomorphicTo(const TreePatternNode *N,
675 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000676
Chris Lattner8cab0212008-01-05 22:25:12 +0000677 /// SubstituteFormalArguments - Replace the formal arguments in this tree
678 /// with actual values specified by ArgMap.
679 void SubstituteFormalArguments(std::map<std::string,
680 TreePatternNode*> &ArgMap);
681
682 /// InlinePatternFragments - If this pattern refers to any pattern
683 /// fragments, inline them into place, giving us a pattern without any
684 /// PatFrag references.
685 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000686
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000687 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000688 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000689 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000690 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000691
Chris Lattner8cab0212008-01-05 22:25:12 +0000692 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000693 /// information. If N already contains a conflicting type, then flag an
694 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000695 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000696 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
697 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000698 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000699 TreePattern &TP);
700 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
701 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000702
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000703 // Update node type with types inferred from an instruction operand or result
704 // def from the ins/outs lists.
705 // Return true if the type changed.
706 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
707
Chris Lattner8cab0212008-01-05 22:25:12 +0000708 /// ContainsUnresolvedType - Return true if this tree contains any
709 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000710 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000711
Chris Lattner8cab0212008-01-05 22:25:12 +0000712 /// canPatternMatch - If it is impossible for this pattern to match on this
713 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000714 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000715};
716
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000717inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
718 TPN.print(OS);
719 return OS;
720}
Jim Grosbach50986b52010-12-24 05:06:32 +0000721
Chris Lattner8cab0212008-01-05 22:25:12 +0000722
723/// TreePattern - Represent a pattern, used for instructions, pattern
724/// fragments, etc.
725///
726class TreePattern {
727 /// Trees - The list of pattern trees which corresponds to this pattern.
728 /// Note that PatFrag's only have a single tree.
729 ///
David Blaikiecf195302014-11-17 22:55:41 +0000730 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000731
Chris Lattnercabe0372010-03-15 06:00:16 +0000732 /// NamedNodes - This is all of the nodes that have names in the trees in this
733 /// pattern.
734 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000735
Chris Lattner8cab0212008-01-05 22:25:12 +0000736 /// TheRecord - The actual TableGen record corresponding to this pattern.
737 ///
738 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000739
Chris Lattner8cab0212008-01-05 22:25:12 +0000740 /// Args - This is a list of all of the arguments to this pattern (for
741 /// PatFrag patterns), which are the 'node' markers in this pattern.
742 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000743
Chris Lattner8cab0212008-01-05 22:25:12 +0000744 /// CDP - the top-level object coordinating this madness.
745 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000746 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000747
748 /// isInputPattern - True if this is an input pattern, something to match.
749 /// False if this is an output pattern, something to emit.
750 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000751
752 /// hasError - True if the currently processed nodes have unresolvable types
753 /// or other non-fatal errors
754 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000755
756 /// It's important that the usage of operands in ComplexPatterns is
757 /// consistent: each named operand can be defined by at most one
758 /// ComplexPattern. This records the ComplexPattern instance and the operand
759 /// number for each operand encountered in a ComplexPattern to aid in that
760 /// check.
761 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000762
763 TypeInfer Infer;
764
Chris Lattner8cab0212008-01-05 22:25:12 +0000765public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000766
Chris Lattner8cab0212008-01-05 22:25:12 +0000767 /// TreePattern constructor - Parse the specified DagInits into the
768 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000769 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000770 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000771 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000772 CodeGenDAGPatterns &ise);
David Blaikiecf195302014-11-17 22:55:41 +0000773 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
774 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000775
Chris Lattner8cab0212008-01-05 22:25:12 +0000776 /// getTrees - Return the tree patterns which corresponds to this pattern.
777 ///
David Blaikiecf195302014-11-17 22:55:41 +0000778 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000779 unsigned getNumTrees() const { return Trees.size(); }
David Blaikiecf195302014-11-17 22:55:41 +0000780 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
781 TreePatternNode *getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000782 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
783 return Trees[0];
784 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000785
Chris Lattnercabe0372010-03-15 06:00:16 +0000786 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
787 if (NamedNodes.empty())
788 ComputeNamedNodes();
789 return NamedNodes;
790 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000791
Chris Lattner8cab0212008-01-05 22:25:12 +0000792 /// getRecord - Return the actual TableGen record corresponding to this
793 /// pattern.
794 ///
795 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000796
Chris Lattner8cab0212008-01-05 22:25:12 +0000797 unsigned getNumArgs() const { return Args.size(); }
798 const std::string &getArgName(unsigned i) const {
799 assert(i < Args.size() && "Argument reference out of range!");
800 return Args[i];
801 }
802 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000803
Chris Lattnerab3242f2008-01-06 01:10:31 +0000804 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000805
806 /// InlinePatternFragments - If this pattern refers to any pattern
807 /// fragments, inline them into place, giving us a pattern without any
808 /// PatFrag references.
809 void InlinePatternFragments() {
810 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
David Blaikiecf195302014-11-17 22:55:41 +0000811 Trees[i] = Trees[i]->InlinePatternFragments(*this);
Chris Lattner8cab0212008-01-05 22:25:12 +0000812 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000813
Chris Lattner8cab0212008-01-05 22:25:12 +0000814 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000815 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000816 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000817 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000818 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000819
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000820 /// error - If this is the first error in the current resolution step,
821 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000822 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000823 bool hasError() const {
824 return HasError;
825 }
826 void resetError() {
827 HasError = false;
828 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000829
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000830 TypeInfer &getInfer() { return Infer; }
831
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000832 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000833 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000834
Chris Lattner8cab0212008-01-05 22:25:12 +0000835private:
David Blaikiecf195302014-11-17 22:55:41 +0000836 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000837 void ComputeNamedNodes();
838 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000839};
840
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000841
842inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
843 const TypeSetByHwMode &InTy,
844 TreePattern &TP) {
845 TypeSetByHwMode VTS(InTy);
846 TP.getInfer().expandOverloads(VTS);
847 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
848}
849
850inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
851 MVT::SimpleValueType InTy,
852 TreePattern &TP) {
853 TypeSetByHwMode VTS(InTy);
854 TP.getInfer().expandOverloads(VTS);
855 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
856}
857
858inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
859 ValueTypeByHwMode InTy,
860 TreePattern &TP) {
861 TypeSetByHwMode VTS(InTy);
862 TP.getInfer().expandOverloads(VTS);
863 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
864}
865
866
Tom Stellardb7246a72012-09-06 14:15:52 +0000867/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
868/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000869struct DAGDefaultOperand {
870 std::vector<TreePatternNode*> DefaultOps;
871};
872
873class DAGInstruction {
874 TreePattern *Pattern;
875 std::vector<Record*> Results;
876 std::vector<Record*> Operands;
877 std::vector<Record*> ImpResults;
David Blaikiecf195302014-11-17 22:55:41 +0000878 TreePatternNode *ResultPattern;
Chris Lattner8cab0212008-01-05 22:25:12 +0000879public:
880 DAGInstruction(TreePattern *TP,
881 const std::vector<Record*> &results,
882 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000883 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000884 : Pattern(TP), Results(results), Operands(operands),
David Blaikiecf195302014-11-17 22:55:41 +0000885 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000886
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000887 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000888 unsigned getNumResults() const { return Results.size(); }
889 unsigned getNumOperands() const { return Operands.size(); }
890 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000891 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000892
David Blaikiecf195302014-11-17 22:55:41 +0000893 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000894
Chris Lattner8cab0212008-01-05 22:25:12 +0000895 Record *getResult(unsigned RN) const {
896 assert(RN < Results.size());
897 return Results[RN];
898 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000899
Chris Lattner8cab0212008-01-05 22:25:12 +0000900 Record *getOperand(unsigned ON) const {
901 assert(ON < Operands.size());
902 return Operands[ON];
903 }
904
905 Record *getImpResult(unsigned RN) const {
906 assert(RN < ImpResults.size());
907 return ImpResults[RN];
908 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000909
David Blaikiecf195302014-11-17 22:55:41 +0000910 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000911};
Jim Grosbach50986b52010-12-24 05:06:32 +0000912
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000913/// This class represents a condition that has to be satisfied for a pattern
914/// to be tried. It is a generalization of a class "Pattern" from Target.td:
915/// in addition to the Target.td's predicates, this class can also represent
916/// conditions associated with HW modes. Both types will eventually become
917/// strings containing C++ code to be executed, the difference is in how
918/// these strings are generated.
919class Predicate {
920public:
921 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
922 assert(R->isSubClassOf("Predicate") &&
923 "Predicate objects should only be created for records derived"
924 "from Predicate class");
925 }
926 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
927 IfCond(C), IsHwMode(true) {}
928
929 /// Return a string which contains the C++ condition code that will serve
930 /// as a predicate during instruction selection.
931 std::string getCondString() const {
932 // The string will excute in a subclass of SelectionDAGISel.
933 // Cast to std::string explicitly to avoid ambiguity with StringRef.
934 std::string C = IsHwMode
935 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
936 : std::string(Def->getValueAsString("CondString"));
937 return IfCond ? C : "!("+C+')';
938 }
939 bool operator==(const Predicate &P) const {
940 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
941 }
942 bool operator<(const Predicate &P) const {
943 if (IsHwMode != P.IsHwMode)
944 return IsHwMode < P.IsHwMode;
945 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
946 if (IfCond != P.IfCond)
947 return IfCond < P.IfCond;
948 if (Def)
949 return LessRecord()(Def, P.Def);
950 return Features < P.Features;
951 }
952 Record *Def; ///< Predicate definition from .td file, null for
953 ///< HW modes.
954 std::string Features; ///< Feature string for HW mode.
955 bool IfCond; ///< The boolean value that the condition has to
956 ///< evaluate to for this predicate to be true.
957 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
958};
959
Chris Lattnerab3242f2008-01-06 01:10:31 +0000960/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000961/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000962class PatternToMatch {
963public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000964 PatternToMatch(Record *srcrecord, const std::vector<Predicate> &preds,
965 TreePatternNode *src, TreePatternNode *dst,
966 const std::vector<Record*> &dstregs,
967 int complexity, unsigned uid, unsigned setmode = 0)
968 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
969 Predicates(preds), Dstregs(std::move(dstregs)),
970 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
971
972 PatternToMatch(Record *srcrecord, std::vector<Predicate> &&preds,
973 TreePatternNode *src, TreePatternNode *dst,
974 std::vector<Record*> &&dstregs,
975 int complexity, unsigned uid, unsigned setmode = 0)
976 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
977 Predicates(preds), Dstregs(std::move(dstregs)),
978 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000979
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000980 Record *SrcRecord; // Originating Record for the pattern.
Chris Lattner8cab0212008-01-05 22:25:12 +0000981 TreePatternNode *SrcPattern; // Source pattern to match.
982 TreePatternNode *DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000983 std::vector<Predicate> Predicates; // Top level predicate conditions
984 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +0000985 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +0000986 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000987 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000988 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +0000989
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000990 Record *getSrcRecord() const { return SrcRecord; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000991 TreePatternNode *getSrcPattern() const { return SrcPattern; }
992 TreePatternNode *getDstPattern() const { return DstPattern; }
993 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +0000994 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000995 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +0000996
997 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000998
Chris Lattner05925fe2010-03-29 01:40:38 +0000999 /// Compute the complexity metric for the input pattern. This roughly
1000 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001001 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001002};
1003
Chris Lattnerab3242f2008-01-06 01:10:31 +00001004class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001005 RecordKeeper &Records;
1006 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001007 CodeGenIntrinsicTable Intrinsics;
1008 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001009
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001010 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001011 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1012 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001013 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001014 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1015 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001016 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1017 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001018
Chris Lattner8cab0212008-01-05 22:25:12 +00001019 // Specific SDNode definitions:
1020 Record *intrinsic_void_sdnode;
1021 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001022
Chris Lattner8cab0212008-01-05 22:25:12 +00001023 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1024 /// value is the pattern to match, the second pattern is the result to
1025 /// emit.
1026 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001027
1028 TypeSetByHwMode LegalVTS;
1029
Chris Lattner8cab0212008-01-05 22:25:12 +00001030public:
Jim Grosbach50986b52010-12-24 05:06:32 +00001031 CodeGenDAGPatterns(RecordKeeper &R);
Jim Grosbach50986b52010-12-24 05:06:32 +00001032
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001033 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001034 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001035 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001036
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001037 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001038
Chris Lattner8cab0212008-01-05 22:25:12 +00001039 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001040 auto F = SDNodes.find(R);
1041 assert(F != SDNodes.end() && "Unknown node!");
1042 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001043 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001044
Chris Lattnercc43e792008-01-05 22:54:53 +00001045 // Node transformation lookups.
1046 typedef std::pair<Record*, std::string> NodeXForm;
1047 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001048 auto F = SDNodeXForms.find(R);
1049 assert(F != SDNodeXForms.end() && "Invalid transform!");
1050 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001051 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001052
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001053 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001054 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001055 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1056 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1057
Jim Grosbach50986b52010-12-24 05:06:32 +00001058
Chris Lattner8cab0212008-01-05 22:25:12 +00001059 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001060 auto F = ComplexPatterns.find(R);
1061 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1062 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001063 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001064
Chris Lattner8cab0212008-01-05 22:25:12 +00001065 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1066 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1067 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001068 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1069 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001070 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001071 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001072
Chris Lattner8cab0212008-01-05 22:25:12 +00001073 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001074 if (IID-1 < Intrinsics.size())
1075 return Intrinsics[IID-1];
1076 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1077 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001078 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001079 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001080
Chris Lattner8cab0212008-01-05 22:25:12 +00001081 unsigned getIntrinsicID(Record *R) const {
1082 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1083 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001084 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1085 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001086 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001087 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001088
Chris Lattner7ed81692010-02-18 06:47:49 +00001089 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001090 auto F = DefaultOperands.find(R);
1091 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1092 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001093 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001094
Chris Lattner8cab0212008-01-05 22:25:12 +00001095 // Pattern Fragment information.
1096 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001097 auto F = PatternFragments.find(R);
1098 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1099 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001100 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001101 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001102 auto F = PatternFragments.find(R);
1103 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001104 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001105 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001106 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001107
David Blaikiefcacc742014-11-13 21:56:57 +00001108 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1109 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001110 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1111 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001112 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001113
1114 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001115 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1116 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1117 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001118 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001119
Ahmed Bougacha14107512013-10-28 18:07:21 +00001120 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1121 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1122 const DAGInstruction &parseInstructionPattern(
1123 CodeGenInstruction &CGI, ListInit *Pattern,
1124 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001125
Chris Lattner8cab0212008-01-05 22:25:12 +00001126 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001127 auto F = Instructions.find(R);
1128 assert(F != Instructions.end() && "Unknown instruction!");
1129 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001130 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001131
Chris Lattner8cab0212008-01-05 22:25:12 +00001132 Record *get_intrinsic_void_sdnode() const {
1133 return intrinsic_void_sdnode;
1134 }
1135 Record *get_intrinsic_w_chain_sdnode() const {
1136 return intrinsic_w_chain_sdnode;
1137 }
1138 Record *get_intrinsic_wo_chain_sdnode() const {
1139 return intrinsic_wo_chain_sdnode;
1140 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001141
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001142 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1143
Chris Lattner8cab0212008-01-05 22:25:12 +00001144private:
1145 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001146 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001147 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001148 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001149 void ParseDefaultOperands();
1150 void ParseInstructions();
1151 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001152 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001153 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001154 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001155 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001156
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001157 std::vector<Predicate> makePredList(ListInit *L);
1158
Craig Topper18e6b572017-06-25 17:33:49 +00001159 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +00001160 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
1161 std::map<std::string,
1162 TreePatternNode*> &InstInputs,
1163 std::map<std::string,
1164 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +00001165 std::vector<Record*> &InstImpResults);
1166};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001167
1168
1169inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1170 TreePattern &TP) const {
1171 bool MadeChange = false;
1172 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1173 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1174 return MadeChange;
1175 }
Chris Lattner8cab0212008-01-05 22:25:12 +00001176} // end namespace llvm
1177
1178#endif