blob: 8a8132c7f894e468bdc1b18e8590c6127a153b48 [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"
Matt Arsenault303327d2017-12-20 19:36:28 +000021#include "SDNodeProperties.h"
Chris Lattnercabe0372010-03-15 06:00:16 +000022#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringMap.h"
Zachary Turner249dc142017-09-20 18:01:40 +000024#include "llvm/ADT/StringSet.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000025#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner1802b172010-03-19 01:07:44 +000027#include <algorithm>
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000028#include <array>
Daniel Sanders7e523672017-11-11 03:23:44 +000029#include <functional>
Chris Lattner1802b172010-03-19 01:07:44 +000030#include <map>
Chandler Carruth91d19d82012-12-04 10:37:14 +000031#include <set>
32#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000033
34namespace llvm {
Chris Lattner8cab0212008-01-05 22:25:12 +000035
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000036class Record;
37class Init;
38class ListInit;
39class DagInit;
40class SDNodeInfo;
41class TreePattern;
42class TreePatternNode;
43class CodeGenDAGPatterns;
44class ComplexPattern;
45
46/// This represents a set of MVTs. Since the underlying type for the MVT
47/// is uint8_t, there are at most 256 values. To reduce the number of memory
48/// allocations and deallocations, represent the set as a sequence of bits.
49/// To reduce the allocations even further, make MachineValueTypeSet own
50/// the storage and use std::array as the bit container.
51struct MachineValueTypeSet {
52 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
53 uint8_t>::value,
54 "Change uint8_t here to the SimpleValueType's type");
55 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
56 using WordType = uint64_t;
Craig Topperd022d252017-09-21 04:55:04 +000057 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000058 static unsigned constexpr NumWords = Capacity/WordWidth;
59 static_assert(NumWords*WordWidth == Capacity,
60 "Capacity should be a multiple of WordWidth");
61
62 LLVM_ATTRIBUTE_ALWAYS_INLINE
63 MachineValueTypeSet() {
64 clear();
65 }
66
67 LLVM_ATTRIBUTE_ALWAYS_INLINE
68 unsigned size() const {
69 unsigned Count = 0;
70 for (WordType W : Words)
71 Count += countPopulation(W);
72 return Count;
73 }
74 LLVM_ATTRIBUTE_ALWAYS_INLINE
75 void clear() {
76 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
77 }
78 LLVM_ATTRIBUTE_ALWAYS_INLINE
79 bool empty() const {
80 for (WordType W : Words)
81 if (W != 0)
82 return false;
83 return true;
84 }
85 LLVM_ATTRIBUTE_ALWAYS_INLINE
86 unsigned count(MVT T) const {
87 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
88 }
89 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
90 bool V = count(T.SimpleTy);
91 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
92 return {*this, V};
93 }
94 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
95 for (unsigned i = 0; i != NumWords; ++i)
96 Words[i] |= S.Words[i];
97 return *this;
98 }
99 LLVM_ATTRIBUTE_ALWAYS_INLINE
100 void erase(MVT T) {
101 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
102 }
103
104 struct const_iterator {
105 // Some implementations of the C++ library require these traits to be
106 // defined.
107 using iterator_category = std::forward_iterator_tag;
108 using value_type = MVT;
109 using difference_type = ptrdiff_t;
110 using pointer = const MVT*;
111 using reference = const MVT&;
112
113 LLVM_ATTRIBUTE_ALWAYS_INLINE
114 MVT operator*() const {
115 assert(Pos != Capacity);
116 return MVT::SimpleValueType(Pos);
117 }
118 LLVM_ATTRIBUTE_ALWAYS_INLINE
119 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
120 Pos = End ? Capacity : find_from_pos(0);
121 }
122 LLVM_ATTRIBUTE_ALWAYS_INLINE
123 const_iterator &operator++() {
124 assert(Pos != Capacity);
125 Pos = find_from_pos(Pos+1);
126 return *this;
127 }
128
129 LLVM_ATTRIBUTE_ALWAYS_INLINE
130 bool operator==(const const_iterator &It) const {
131 return Set == It.Set && Pos == It.Pos;
132 }
133 LLVM_ATTRIBUTE_ALWAYS_INLINE
134 bool operator!=(const const_iterator &It) const {
135 return !operator==(It);
136 }
137
138 private:
139 unsigned find_from_pos(unsigned P) const {
140 unsigned SkipWords = P / WordWidth;
141 unsigned SkipBits = P % WordWidth;
142 unsigned Count = SkipWords * WordWidth;
143
144 // If P is in the middle of a word, process it manually here, because
145 // the trailing bits need to be masked off to use findFirstSet.
146 if (SkipBits != 0) {
147 WordType W = Set->Words[SkipWords];
148 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
149 if (W != 0)
150 return Count + findFirstSet(W);
151 Count += WordWidth;
152 SkipWords++;
153 }
154
155 for (unsigned i = SkipWords; i != NumWords; ++i) {
156 WordType W = Set->Words[i];
157 if (W != 0)
158 return Count + findFirstSet(W);
159 Count += WordWidth;
160 }
161 return Capacity;
162 }
163
164 const MachineValueTypeSet *Set;
165 unsigned Pos;
166 };
167
168 LLVM_ATTRIBUTE_ALWAYS_INLINE
169 const_iterator begin() const { return const_iterator(this, false); }
170 LLVM_ATTRIBUTE_ALWAYS_INLINE
171 const_iterator end() const { return const_iterator(this, true); }
172
173 LLVM_ATTRIBUTE_ALWAYS_INLINE
174 bool operator==(const MachineValueTypeSet &S) const {
175 return Words == S.Words;
176 }
177 LLVM_ATTRIBUTE_ALWAYS_INLINE
178 bool operator!=(const MachineValueTypeSet &S) const {
179 return !operator==(S);
180 }
181
182private:
183 friend struct const_iterator;
184 std::array<WordType,NumWords> Words;
185};
186
187struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
188 using SetType = MachineValueTypeSet;
Jim Grosbach50986b52010-12-24 05:06:32 +0000189
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000190 TypeSetByHwMode() = default;
191 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
192 TypeSetByHwMode(MVT::SimpleValueType VT)
193 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
194 TypeSetByHwMode(ValueTypeByHwMode VT)
195 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
196 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000197
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000198 SetType &getOrCreate(unsigned Mode) {
199 if (hasMode(Mode))
200 return get(Mode);
201 return Map.insert({Mode,SetType()}).first->second;
202 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000203
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000204 bool isValueTypeByHwMode(bool AllowEmpty) const;
205 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000206
207 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000208 bool isMachineValueType() const {
209 return isDefaultOnly() && Map.begin()->second.size() == 1;
210 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000211
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000212 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000213 MVT getMachineValueType() const {
214 assert(isMachineValueType());
215 return *Map.begin()->second.begin();
216 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000217
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000218 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000219
220 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000221 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000222 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000223 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000224
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000225 bool insert(const ValueTypeByHwMode &VVT);
226 bool constrain(const TypeSetByHwMode &VTS);
227 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000228 template <typename Predicate>
229 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000230
Zachary Turner249dc142017-09-20 18:01:40 +0000231 void writeToStream(raw_ostream &OS) const;
232 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000233
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000234 bool operator==(const TypeSetByHwMode &VTS) const;
235 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000236
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000237 void dump() const;
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000238 bool validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000239
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000240private:
241 /// Intersect two sets. Return true if anything has changed.
242 bool intersect(SetType &Out, const SetType &In);
243};
Jim Grosbach50986b52010-12-24 05:06:32 +0000244
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000245raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
246
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000247struct TypeInfer {
248 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000249
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000250 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
251 return VTS.isValueTypeByHwMode(AllowEmpty);
252 }
253 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
254 bool AllowEmpty) const {
255 assert(VTS.isValueTypeByHwMode(AllowEmpty));
256 return VTS.getValueTypeByHwMode();
257 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000258
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000259 /// The protocol in the following functions (Merge*, force*, Enforce*,
260 /// expand*) is to return "true" if a change has been made, "false"
261 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000262
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000263 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
264 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
265 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
266 }
267 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
268 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
269 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000270
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000271 /// Reduce the set \p Out to have at most one element for each mode.
272 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000273
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000274 /// The following four functions ensure that upon return the set \p Out
275 /// will only contain types of the specified kind: integer, floating-point,
276 /// scalar, or vector.
277 /// If \p Out is empty, all legal types of the specified kind will be added
278 /// to it. Otherwise, all types that are not of the specified kind will be
279 /// removed from \p Out.
280 bool EnforceInteger(TypeSetByHwMode &Out);
281 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
282 bool EnforceScalar(TypeSetByHwMode &Out);
283 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000284
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000285 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
286 /// unchanged.
287 bool EnforceAny(TypeSetByHwMode &Out);
288 /// Make sure that for each type in \p Small, there exists a larger type
289 /// in \p Big.
290 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
291 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
292 /// for each type U in \p Elem, U is a scalar type.
293 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
294 /// (vector) type T in \p Vec, such that U is the element type of T.
295 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
296 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
297 const ValueTypeByHwMode &VVT);
298 /// Ensure that for each type T in \p Sub, T is a vector type, and there
299 /// exists a type U in \p Vec such that U is a vector type with the same
300 /// element type as T and at least as many elements as T.
301 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
302 TypeSetByHwMode &Sub);
303 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
304 /// 2. Ensure that for each vector type T in \p V, there exists a vector
305 /// type U in \p W, such that T and U have the same number of elements.
306 /// 3. Ensure that for each vector type U in \p W, there exists a vector
307 /// type T in \p V, such that T and U have the same number of elements
308 /// (reverse of 2).
309 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
310 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
311 /// such that T and U have equal size in bits.
312 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
313 /// such that T and U have equal size in bits (reverse of 1).
314 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000315
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000316 /// For each overloaded type (i.e. of form *Any), replace it with the
317 /// corresponding subset of legal, specific types.
318 void expandOverloads(TypeSetByHwMode &VTS);
319 void expandOverloads(TypeSetByHwMode::SetType &Out,
320 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000321
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000322 struct ValidateOnExit {
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000323 ValidateOnExit(TypeSetByHwMode &T, TypeInfer &TI) : Infer(TI), VTS(T) {}
324 #ifndef NDEBUG
325 ~ValidateOnExit();
326 #else
327 ~ValidateOnExit() {} // Empty destructor with NDEBUG.
328 #endif
329 TypeInfer &Infer;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000330 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000331 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000332
333 TreePattern &TP;
334 unsigned ForceMode; // Mode to use when set.
335 bool CodeGen = false; // Set during generation of matcher code.
336
337private:
338 TypeSetByHwMode getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000339
340 /// Cached legal types.
341 bool LegalTypesCached = false;
342 TypeSetByHwMode::SetType LegalCache = {};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000343};
Chris Lattner8cab0212008-01-05 22:25:12 +0000344
Scott Michel94420742008-03-05 17:49:05 +0000345/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000346typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000347
Chris Lattner8cab0212008-01-05 22:25:12 +0000348/// SDTypeConstraint - This is a discriminated union of constraints,
349/// corresponding to the SDTypeConstraint tablegen class in Target.td.
350struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000351 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000352
Chris Lattner8cab0212008-01-05 22:25:12 +0000353 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000354 enum {
355 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000356 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000357 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000358 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000359
Chris Lattner8cab0212008-01-05 22:25:12 +0000360 union { // The discriminated union.
361 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000362 unsigned OtherOperandNum;
363 } SDTCisSameAs_Info;
364 struct {
365 unsigned OtherOperandNum;
366 } SDTCisVTSmallerThanOp_Info;
367 struct {
368 unsigned BigOperandNum;
369 } SDTCisOpSmallerThanOp_Info;
370 struct {
371 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000372 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000373 struct {
374 unsigned OtherOperandNum;
375 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000376 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000377 unsigned OtherOperandNum;
378 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000379 struct {
380 unsigned OtherOperandNum;
381 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000382 } x;
383
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000384 // The VT for SDTCisVT and SDTCVecEltisVT.
385 // Must not be in the union because it has a non-trivial destructor.
386 ValueTypeByHwMode VVT;
387
Chris Lattner8cab0212008-01-05 22:25:12 +0000388 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
389 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000390 /// change, false otherwise. If a type contradiction is found, an error
391 /// is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000392 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
393 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000394};
395
396/// SDNodeInfo - One of these records is created for each SDNode instance in
397/// the target .td file. This represents the various dag nodes we will be
398/// processing.
399class SDNodeInfo {
400 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000401 StringRef EnumName;
402 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000403 unsigned Properties;
404 unsigned NumResults;
405 int NumOperands;
406 std::vector<SDTypeConstraint> TypeConstraints;
407public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000408 // Parse the specified record.
409 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000410
Chris Lattner8cab0212008-01-05 22:25:12 +0000411 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000412
Chris Lattner135091b2010-03-28 08:48:47 +0000413 /// getNumOperands - This is the number of operands required or -1 if
414 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000415 int getNumOperands() const { return NumOperands; }
416 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000417 StringRef getEnumName() const { return EnumName; }
418 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000419
Chris Lattner8cab0212008-01-05 22:25:12 +0000420 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
421 return TypeConstraints;
422 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000423
Chris Lattner99e53b32010-02-28 00:22:30 +0000424 /// getKnownType - If the type constraints on this node imply a fixed type
425 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000426 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000427 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000428
Chris Lattner8cab0212008-01-05 22:25:12 +0000429 /// hasProperty - Return true if this node has the specified property.
430 ///
431 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
432
433 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
434 /// constraints for this node to the operands of the node. This returns
435 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000436 /// found, an error is flagged.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000437 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000438};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000439
Chris Lattner514e2922011-04-17 21:38:24 +0000440/// TreePredicateFn - This is an abstraction that represents the predicates on
441/// a PatFrag node. This is a simple one-word wrapper around a pointer to
442/// provide nice accessors.
443class TreePredicateFn {
444 /// PatFragRec - This is the TreePattern for the PatFrag that we
445 /// originally came from.
446 TreePattern *PatFragRec;
447public:
448 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000449 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000450
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000451
Chris Lattner514e2922011-04-17 21:38:24 +0000452 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000453
Chris Lattner514e2922011-04-17 21:38:24 +0000454 /// isAlwaysTrue - Return true if this is a noop predicate.
455 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000456
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000457 bool isImmediatePattern() const { return hasImmCode(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000458
Chris Lattner07add492011-04-18 06:22:33 +0000459 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
460 /// this is an immediate predicate. It is an error to call this on a
461 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000462 std::string getImmediatePredicateCode() const {
463 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000464 assert(!Result.empty() && "Isn't an immediate pattern!");
465 return Result;
466 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000467
Chris Lattner514e2922011-04-17 21:38:24 +0000468 bool operator==(const TreePredicateFn &RHS) const {
469 return PatFragRec == RHS.PatFragRec;
470 }
471
472 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
473
474 /// Return the name to use in the generated code to reference this, this is
475 /// "Predicate_foo" if from a pattern fragment "foo".
476 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000477
Chris Lattner514e2922011-04-17 21:38:24 +0000478 /// getCodeToRunOnSDNode - Return the code for the function body that
479 /// evaluates this predicate. The argument is expected to be in "Node",
480 /// not N. This handles casting and conversion to a concrete node type as
481 /// appropriate.
482 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000483
Daniel Sanders649c5852017-10-13 20:42:18 +0000484 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000485 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000486
Daniel Sanders11300ce2017-10-13 21:28:03 +0000487 /// Get a string that describes the type returned by getImmType() but is
488 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000489 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000490
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000491 // Is the desired predefined predicate for a load?
492 bool isLoad() const;
493 // Is the desired predefined predicate for a store?
494 bool isStore() const;
Daniel Sanders87d196c2017-11-13 22:26:13 +0000495 // Is the desired predefined predicate for an atomic?
496 bool isAtomic() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000497
498 /// Is this predicate the predefined unindexed load predicate?
499 /// Is this predicate the predefined unindexed store predicate?
500 bool isUnindexed() const;
501 /// Is this predicate the predefined non-extending load predicate?
502 bool isNonExtLoad() const;
503 /// Is this predicate the predefined any-extend load predicate?
504 bool isAnyExtLoad() const;
505 /// Is this predicate the predefined sign-extend load predicate?
506 bool isSignExtLoad() const;
507 /// Is this predicate the predefined zero-extend load predicate?
508 bool isZeroExtLoad() const;
509 /// Is this predicate the predefined non-truncating store predicate?
510 bool isNonTruncStore() const;
511 /// Is this predicate the predefined truncating store predicate?
512 bool isTruncStore() const;
513
Daniel Sanders6d9d30a2017-11-13 23:03:47 +0000514 /// Is this predicate the predefined monotonic atomic predicate?
515 bool isAtomicOrderingMonotonic() const;
516 /// Is this predicate the predefined acquire atomic predicate?
517 bool isAtomicOrderingAcquire() const;
518 /// Is this predicate the predefined release atomic predicate?
519 bool isAtomicOrderingRelease() const;
520 /// Is this predicate the predefined acquire-release atomic predicate?
521 bool isAtomicOrderingAcquireRelease() const;
522 /// Is this predicate the predefined sequentially consistent atomic predicate?
523 bool isAtomicOrderingSequentiallyConsistent() const;
524
Daniel Sanders0c43b3a2017-11-30 21:05:59 +0000525 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
526 bool isAtomicOrderingAcquireOrStronger() const;
527 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
528 bool isAtomicOrderingWeakerThanAcquire() const;
529
530 /// Is this predicate the predefined release-or-stronger atomic predicate?
531 bool isAtomicOrderingReleaseOrStronger() const;
532 /// Is this predicate the predefined weaker-than-release atomic predicate?
533 bool isAtomicOrderingWeakerThanRelease() const;
534
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000535 /// If non-null, indicates that this predicate is a predefined memory VT
536 /// predicate for a load/store and returns the ValueType record for the memory VT.
537 Record *getMemoryVT() const;
538 /// If non-null, indicates that this predicate is a predefined memory VT
539 /// predicate (checking only the scalar type) for load/store and returns the
540 /// ValueType record for the memory VT.
541 Record *getScalarMemoryVT() const;
542
Chris Lattner514e2922011-04-17 21:38:24 +0000543private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000544 bool hasPredCode() const;
545 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000546 std::string getPredCode() const;
547 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000548 bool immCodeUsesAPInt() const;
549 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000550
551 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000552};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000553
Chris Lattner8cab0212008-01-05 22:25:12 +0000554
555/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
556/// patterns), and as such should be ref counted. We currently just leak all
557/// TreePatternNode objects!
558class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000559 /// The type of each node result. Before and during type inference, each
560 /// result may be a set of possible types. After (successful) type inference,
561 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000562 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000563
Chris Lattner8cab0212008-01-05 22:25:12 +0000564 /// Operator - The Record for the operator if this is an interior node (not
565 /// a leaf).
566 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000567
Chris Lattner8cab0212008-01-05 22:25:12 +0000568 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
569 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000570 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000571
Chris Lattner8cab0212008-01-05 22:25:12 +0000572 /// Name - The name given to this node with the :$foo notation.
573 ///
574 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000575
Dan Gohman6e979022008-10-15 06:17:21 +0000576 /// PredicateFns - The predicate functions to execute on this node to check
577 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000578 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000579
Chris Lattner8cab0212008-01-05 22:25:12 +0000580 /// TransformFn - The transformation function to execute on this node before
581 /// it can be substituted into the resulting instruction on a pattern match.
582 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000583
Chris Lattner8cab0212008-01-05 22:25:12 +0000584 std::vector<TreePatternNode*> Children;
585public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000586 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000587 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000588 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000589 Types.resize(NumResults);
590 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000591 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000592 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000593 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000594 }
595 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000596
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000597 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000598 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000599 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000600
Craig Topperada08572014-04-16 04:21:27 +0000601 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000602
Chris Lattnercabe0372010-03-15 06:00:16 +0000603 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000604 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000605 ValueTypeByHwMode getType(unsigned ResNo) const {
606 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000607 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000608 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
609 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
610 return Types[ResNo];
611 }
612 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
613 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
614 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
615 return Types[ResNo].getMachineValueType().SimpleTy;
616 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000617
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000618 bool hasConcreteType(unsigned ResNo) const {
619 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000620 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000621 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
622 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000623 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000624
David Greeneaf8ee2c2011-07-29 22:43:06 +0000625 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000626 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000627
Chris Lattner8cab0212008-01-05 22:25:12 +0000628 unsigned getNumChildren() const { return Children.size(); }
629 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
630 void setChild(unsigned i, TreePatternNode *N) {
631 Children[i] = N;
632 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000633
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000634 /// hasChild - Return true if N is any of our children.
635 bool hasChild(const TreePatternNode *N) const {
636 for (unsigned i = 0, e = Children.size(); i != e; ++i)
637 if (Children[i] == N) return true;
638 return false;
639 }
Chris Lattner89c65662008-01-06 05:36:50 +0000640
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000641 bool hasProperTypeByHwMode() const;
642 bool hasPossibleType() const;
643 bool setDefaultMode(unsigned Mode);
644
Chris Lattner514e2922011-04-17 21:38:24 +0000645 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000646
Chris Lattner514e2922011-04-17 21:38:24 +0000647 const std::vector<TreePredicateFn> &getPredicateFns() const {
648 return PredicateFns;
649 }
Dan Gohman6e979022008-10-15 06:17:21 +0000650 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000651 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000652 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
653 PredicateFns = Fns;
654 }
Chris Lattner514e2922011-04-17 21:38:24 +0000655 void addPredicateFn(const TreePredicateFn &Fn) {
656 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000657 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000658 PredicateFns.push_back(Fn);
659 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000660
661 Record *getTransformFn() const { return TransformFn; }
662 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000663
Chris Lattner89c65662008-01-06 05:36:50 +0000664 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
665 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
666 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000667
Chris Lattner53c39ba2010-02-14 22:22:58 +0000668 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
669 /// return the ComplexPattern information, otherwise return null.
670 const ComplexPattern *
671 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
672
Tim Northoverc807a172014-05-20 11:52:46 +0000673 /// Returns the number of MachineInstr operands that would be produced by this
674 /// node if it mapped directly to an output Instruction's
675 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
676 /// for Operands; otherwise 1.
677 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
678
Chris Lattner53c39ba2010-02-14 22:22:58 +0000679 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000680 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000681
Chris Lattner53c39ba2010-02-14 22:22:58 +0000682 /// TreeHasProperty - Return true if any node in this tree has the specified
683 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000684 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000685
Evan Cheng49bad4c2008-06-16 20:29:38 +0000686 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
687 /// marked isCommutative.
688 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000689
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000690 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000691 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000692
Chris Lattner8cab0212008-01-05 22:25:12 +0000693public: // Higher level manipulation routines.
694
695 /// clone - Return a new copy of this tree.
696 ///
697 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000698
699 /// RemoveAllTypes - Recursively strip all the types of this tree.
700 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000701
Chris Lattner8cab0212008-01-05 22:25:12 +0000702 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
703 /// the specified node. For this comparison, all of the state of the node
704 /// is considered, except for the assigned name. Nodes with differing names
705 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000706 bool isIsomorphicTo(const TreePatternNode *N,
707 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000708
Chris Lattner8cab0212008-01-05 22:25:12 +0000709 /// SubstituteFormalArguments - Replace the formal arguments in this tree
710 /// with actual values specified by ArgMap.
711 void SubstituteFormalArguments(std::map<std::string,
712 TreePatternNode*> &ArgMap);
713
714 /// InlinePatternFragments - If this pattern refers to any pattern
715 /// fragments, inline them into place, giving us a pattern without any
716 /// PatFrag references.
717 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000718
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000719 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000720 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000721 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000722 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000723
Chris Lattner8cab0212008-01-05 22:25:12 +0000724 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000725 /// information. If N already contains a conflicting type, then flag an
726 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000727 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000728 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
729 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000730 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000731 TreePattern &TP);
732 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
733 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000734
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000735 // Update node type with types inferred from an instruction operand or result
736 // def from the ins/outs lists.
737 // Return true if the type changed.
738 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
739
Chris Lattner8cab0212008-01-05 22:25:12 +0000740 /// ContainsUnresolvedType - Return true if this tree contains any
741 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000742 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000743
Chris Lattner8cab0212008-01-05 22:25:12 +0000744 /// canPatternMatch - If it is impossible for this pattern to match on this
745 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000746 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000747};
748
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000749inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
750 TPN.print(OS);
751 return OS;
752}
Jim Grosbach50986b52010-12-24 05:06:32 +0000753
Chris Lattner8cab0212008-01-05 22:25:12 +0000754
755/// TreePattern - Represent a pattern, used for instructions, pattern
756/// fragments, etc.
757///
758class TreePattern {
759 /// Trees - The list of pattern trees which corresponds to this pattern.
760 /// Note that PatFrag's only have a single tree.
761 ///
David Blaikiecf195302014-11-17 22:55:41 +0000762 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000763
Chris Lattnercabe0372010-03-15 06:00:16 +0000764 /// NamedNodes - This is all of the nodes that have names in the trees in this
765 /// pattern.
766 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000767
Chris Lattner8cab0212008-01-05 22:25:12 +0000768 /// TheRecord - The actual TableGen record corresponding to this pattern.
769 ///
770 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000771
Chris Lattner8cab0212008-01-05 22:25:12 +0000772 /// Args - This is a list of all of the arguments to this pattern (for
773 /// PatFrag patterns), which are the 'node' markers in this pattern.
774 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000775
Chris Lattner8cab0212008-01-05 22:25:12 +0000776 /// CDP - the top-level object coordinating this madness.
777 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000778 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000779
780 /// isInputPattern - True if this is an input pattern, something to match.
781 /// False if this is an output pattern, something to emit.
782 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000783
784 /// hasError - True if the currently processed nodes have unresolvable types
785 /// or other non-fatal errors
786 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000787
788 /// It's important that the usage of operands in ComplexPatterns is
789 /// consistent: each named operand can be defined by at most one
790 /// ComplexPattern. This records the ComplexPattern instance and the operand
791 /// number for each operand encountered in a ComplexPattern to aid in that
792 /// check.
793 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000794
795 TypeInfer Infer;
796
Chris Lattner8cab0212008-01-05 22:25:12 +0000797public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000798
Chris Lattner8cab0212008-01-05 22:25:12 +0000799 /// TreePattern constructor - Parse the specified DagInits into the
800 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000801 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000802 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000803 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000804 CodeGenDAGPatterns &ise);
David Blaikiecf195302014-11-17 22:55:41 +0000805 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
806 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000807
Chris Lattner8cab0212008-01-05 22:25:12 +0000808 /// getTrees - Return the tree patterns which corresponds to this pattern.
809 ///
David Blaikiecf195302014-11-17 22:55:41 +0000810 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000811 unsigned getNumTrees() const { return Trees.size(); }
David Blaikiecf195302014-11-17 22:55:41 +0000812 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Daniel Sanders7e523672017-11-11 03:23:44 +0000813 void setTree(unsigned i, TreePatternNode *Tree) { Trees[i] = Tree; }
David Blaikiecf195302014-11-17 22:55:41 +0000814 TreePatternNode *getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000815 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
816 return Trees[0];
817 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000818
Chris Lattnercabe0372010-03-15 06:00:16 +0000819 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
820 if (NamedNodes.empty())
821 ComputeNamedNodes();
822 return NamedNodes;
823 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000824
Chris Lattner8cab0212008-01-05 22:25:12 +0000825 /// getRecord - Return the actual TableGen record corresponding to this
826 /// pattern.
827 ///
828 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000829
Chris Lattner8cab0212008-01-05 22:25:12 +0000830 unsigned getNumArgs() const { return Args.size(); }
831 const std::string &getArgName(unsigned i) const {
832 assert(i < Args.size() && "Argument reference out of range!");
833 return Args[i];
834 }
835 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000836
Chris Lattnerab3242f2008-01-06 01:10:31 +0000837 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000838
839 /// InlinePatternFragments - If this pattern refers to any pattern
840 /// fragments, inline them into place, giving us a pattern without any
841 /// PatFrag references.
842 void InlinePatternFragments() {
843 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
David Blaikiecf195302014-11-17 22:55:41 +0000844 Trees[i] = Trees[i]->InlinePatternFragments(*this);
Chris Lattner8cab0212008-01-05 22:25:12 +0000845 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000846
Chris Lattner8cab0212008-01-05 22:25:12 +0000847 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000848 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000849 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000850 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000851 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000852
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000853 /// error - If this is the first error in the current resolution step,
854 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000855 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000856 bool hasError() const {
857 return HasError;
858 }
859 void resetError() {
860 HasError = false;
861 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000862
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000863 TypeInfer &getInfer() { return Infer; }
864
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000865 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000866 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000867
Chris Lattner8cab0212008-01-05 22:25:12 +0000868private:
David Blaikiecf195302014-11-17 22:55:41 +0000869 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000870 void ComputeNamedNodes();
871 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000872};
873
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000874
875inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
876 const TypeSetByHwMode &InTy,
877 TreePattern &TP) {
878 TypeSetByHwMode VTS(InTy);
879 TP.getInfer().expandOverloads(VTS);
880 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
881}
882
883inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
884 MVT::SimpleValueType InTy,
885 TreePattern &TP) {
886 TypeSetByHwMode VTS(InTy);
887 TP.getInfer().expandOverloads(VTS);
888 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
889}
890
891inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
892 ValueTypeByHwMode InTy,
893 TreePattern &TP) {
894 TypeSetByHwMode VTS(InTy);
895 TP.getInfer().expandOverloads(VTS);
896 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
897}
898
899
Tom Stellardb7246a72012-09-06 14:15:52 +0000900/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
901/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000902struct DAGDefaultOperand {
903 std::vector<TreePatternNode*> DefaultOps;
904};
905
906class DAGInstruction {
907 TreePattern *Pattern;
908 std::vector<Record*> Results;
909 std::vector<Record*> Operands;
910 std::vector<Record*> ImpResults;
David Blaikiecf195302014-11-17 22:55:41 +0000911 TreePatternNode *ResultPattern;
Chris Lattner8cab0212008-01-05 22:25:12 +0000912public:
913 DAGInstruction(TreePattern *TP,
914 const std::vector<Record*> &results,
915 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000916 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000917 : Pattern(TP), Results(results), Operands(operands),
David Blaikiecf195302014-11-17 22:55:41 +0000918 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000919
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000920 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000921 unsigned getNumResults() const { return Results.size(); }
922 unsigned getNumOperands() const { return Operands.size(); }
923 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000924 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000925
David Blaikiecf195302014-11-17 22:55:41 +0000926 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000927
Chris Lattner8cab0212008-01-05 22:25:12 +0000928 Record *getResult(unsigned RN) const {
929 assert(RN < Results.size());
930 return Results[RN];
931 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000932
Chris Lattner8cab0212008-01-05 22:25:12 +0000933 Record *getOperand(unsigned ON) const {
934 assert(ON < Operands.size());
935 return Operands[ON];
936 }
937
938 Record *getImpResult(unsigned RN) const {
939 assert(RN < ImpResults.size());
940 return ImpResults[RN];
941 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000942
David Blaikiecf195302014-11-17 22:55:41 +0000943 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000944};
Jim Grosbach50986b52010-12-24 05:06:32 +0000945
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000946/// This class represents a condition that has to be satisfied for a pattern
947/// to be tried. It is a generalization of a class "Pattern" from Target.td:
948/// in addition to the Target.td's predicates, this class can also represent
949/// conditions associated with HW modes. Both types will eventually become
950/// strings containing C++ code to be executed, the difference is in how
951/// these strings are generated.
952class Predicate {
953public:
954 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
955 assert(R->isSubClassOf("Predicate") &&
956 "Predicate objects should only be created for records derived"
957 "from Predicate class");
958 }
959 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
960 IfCond(C), IsHwMode(true) {}
961
962 /// Return a string which contains the C++ condition code that will serve
963 /// as a predicate during instruction selection.
964 std::string getCondString() const {
965 // The string will excute in a subclass of SelectionDAGISel.
966 // Cast to std::string explicitly to avoid ambiguity with StringRef.
967 std::string C = IsHwMode
968 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
969 : std::string(Def->getValueAsString("CondString"));
970 return IfCond ? C : "!("+C+')';
971 }
972 bool operator==(const Predicate &P) const {
973 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
974 }
975 bool operator<(const Predicate &P) const {
976 if (IsHwMode != P.IsHwMode)
977 return IsHwMode < P.IsHwMode;
978 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
979 if (IfCond != P.IfCond)
980 return IfCond < P.IfCond;
981 if (Def)
982 return LessRecord()(Def, P.Def);
983 return Features < P.Features;
984 }
985 Record *Def; ///< Predicate definition from .td file, null for
986 ///< HW modes.
987 std::string Features; ///< Feature string for HW mode.
988 bool IfCond; ///< The boolean value that the condition has to
989 ///< evaluate to for this predicate to be true.
990 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
991};
992
Chris Lattnerab3242f2008-01-06 01:10:31 +0000993/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000994/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000995class PatternToMatch {
996public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000997 PatternToMatch(Record *srcrecord, const std::vector<Predicate> &preds,
998 TreePatternNode *src, TreePatternNode *dst,
999 const std::vector<Record*> &dstregs,
1000 int complexity, unsigned uid, unsigned setmode = 0)
1001 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
1002 Predicates(preds), Dstregs(std::move(dstregs)),
1003 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
1004
1005 PatternToMatch(Record *srcrecord, std::vector<Predicate> &&preds,
1006 TreePatternNode *src, TreePatternNode *dst,
1007 std::vector<Record*> &&dstregs,
1008 int complexity, unsigned uid, unsigned setmode = 0)
1009 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
1010 Predicates(preds), Dstregs(std::move(dstregs)),
1011 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001012
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001013 Record *SrcRecord; // Originating Record for the pattern.
Chris Lattner8cab0212008-01-05 22:25:12 +00001014 TreePatternNode *SrcPattern; // Source pattern to match.
1015 TreePatternNode *DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001016 std::vector<Predicate> Predicates; // Top level predicate conditions
1017 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +00001018 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +00001019 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001020 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001021 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +00001022
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001023 Record *getSrcRecord() const { return SrcRecord; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001024 TreePatternNode *getSrcPattern() const { return SrcPattern; }
1025 TreePatternNode *getDstPattern() const { return DstPattern; }
1026 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +00001027 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001028 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001029
1030 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001031
Chris Lattner05925fe2010-03-29 01:40:38 +00001032 /// Compute the complexity metric for the input pattern. This roughly
1033 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001034 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001035};
1036
Chris Lattnerab3242f2008-01-06 01:10:31 +00001037class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001038 RecordKeeper &Records;
1039 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001040 CodeGenIntrinsicTable Intrinsics;
1041 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001042
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001043 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001044 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1045 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001046 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001047 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1048 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001049 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1050 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001051
Chris Lattner8cab0212008-01-05 22:25:12 +00001052 // Specific SDNode definitions:
1053 Record *intrinsic_void_sdnode;
1054 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001055
Chris Lattner8cab0212008-01-05 22:25:12 +00001056 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1057 /// value is the pattern to match, the second pattern is the result to
1058 /// emit.
1059 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001060
1061 TypeSetByHwMode LegalVTS;
1062
Daniel Sanders7e523672017-11-11 03:23:44 +00001063 using PatternRewriterFn = std::function<void (TreePattern *)>;
1064 PatternRewriterFn PatternRewriter;
1065
Chris Lattner8cab0212008-01-05 22:25:12 +00001066public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001067 CodeGenDAGPatterns(RecordKeeper &R,
1068 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001069
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001070 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001071 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001072 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001073
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001074 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001075
Chris Lattner8cab0212008-01-05 22:25:12 +00001076 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001077 auto F = SDNodes.find(R);
1078 assert(F != SDNodes.end() && "Unknown node!");
1079 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001080 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001081
Chris Lattnercc43e792008-01-05 22:54:53 +00001082 // Node transformation lookups.
1083 typedef std::pair<Record*, std::string> NodeXForm;
1084 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001085 auto F = SDNodeXForms.find(R);
1086 assert(F != SDNodeXForms.end() && "Invalid transform!");
1087 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001088 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001089
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001090 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001091 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001092 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1093 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1094
Jim Grosbach50986b52010-12-24 05:06:32 +00001095
Chris Lattner8cab0212008-01-05 22:25:12 +00001096 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001097 auto F = ComplexPatterns.find(R);
1098 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1099 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001100 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001101
Chris Lattner8cab0212008-01-05 22:25:12 +00001102 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1103 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1104 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001105 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1106 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001107 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001108 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001109
Chris Lattner8cab0212008-01-05 22:25:12 +00001110 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001111 if (IID-1 < Intrinsics.size())
1112 return Intrinsics[IID-1];
1113 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1114 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001115 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001116 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001117
Chris Lattner8cab0212008-01-05 22:25:12 +00001118 unsigned getIntrinsicID(Record *R) const {
1119 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1120 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001121 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1122 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001123 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001124 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001125
Chris Lattner7ed81692010-02-18 06:47:49 +00001126 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001127 auto F = DefaultOperands.find(R);
1128 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
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 // Pattern Fragment information.
1133 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001134 auto F = PatternFragments.find(R);
1135 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1136 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001137 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001138 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001139 auto F = PatternFragments.find(R);
1140 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001141 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001142 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001143 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001144
David Blaikiefcacc742014-11-13 21:56:57 +00001145 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1146 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001147 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1148 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001149 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001150
1151 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001152 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1153 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1154 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001155 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001156
Ahmed Bougacha14107512013-10-28 18:07:21 +00001157 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1158 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1159 const DAGInstruction &parseInstructionPattern(
1160 CodeGenInstruction &CGI, ListInit *Pattern,
1161 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001162
Chris Lattner8cab0212008-01-05 22:25:12 +00001163 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001164 auto F = Instructions.find(R);
1165 assert(F != Instructions.end() && "Unknown instruction!");
1166 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001167 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001168
Chris Lattner8cab0212008-01-05 22:25:12 +00001169 Record *get_intrinsic_void_sdnode() const {
1170 return intrinsic_void_sdnode;
1171 }
1172 Record *get_intrinsic_w_chain_sdnode() const {
1173 return intrinsic_w_chain_sdnode;
1174 }
1175 Record *get_intrinsic_wo_chain_sdnode() const {
1176 return intrinsic_wo_chain_sdnode;
1177 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001178
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001179 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1180
Chris Lattner8cab0212008-01-05 22:25:12 +00001181private:
1182 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001183 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001184 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001185 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001186 void ParseDefaultOperands();
1187 void ParseInstructions();
1188 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001189 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001190 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001191 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001192 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001193
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001194 std::vector<Predicate> makePredList(ListInit *L);
1195
Craig Topper18e6b572017-06-25 17:33:49 +00001196 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +00001197 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
1198 std::map<std::string,
1199 TreePatternNode*> &InstInputs,
1200 std::map<std::string,
1201 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +00001202 std::vector<Record*> &InstImpResults);
1203};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001204
1205
1206inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1207 TreePattern &TP) const {
1208 bool MadeChange = false;
1209 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1210 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1211 return MadeChange;
1212 }
Matt Arsenault303327d2017-12-20 19:36:28 +00001213
Chris Lattner8cab0212008-01-05 22:25:12 +00001214} // end namespace llvm
1215
1216#endif