1 //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the CodeGenDAGPatterns class, which is used to read and
10 // represent the patterns present in a .td file for instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
15 #define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
16
17 #include "CodeGenIntrinsics.h"
18 #include "CodeGenTarget.h"
19 #include "SDNodeProperties.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include <algorithm>
27 #include <array>
28 #include <functional>
29 #include <map>
30 #include <numeric>
31 #include <vector>
32
33 namespace llvm {
34
35 class Record;
36 class Init;
37 class ListInit;
38 class DagInit;
39 class SDNodeInfo;
40 class TreePattern;
41 class TreePatternNode;
42 class CodeGenDAGPatterns;
43
44 /// Shared pointer for TreePatternNode.
45 using TreePatternNodePtr = std::shared_ptr<TreePatternNode>;
46
47 /// This represents a set of MVTs. Since the underlying type for the MVT
48 /// is uint8_t, there are at most 256 values. To reduce the number of memory
49 /// allocations and deallocations, represent the set as a sequence of bits.
50 /// To reduce the allocations even further, make MachineValueTypeSet own
51 /// the storage and use std::array as the bit container.
52 struct MachineValueTypeSet {
53 static_assert(std::is_same<std::underlying_type_t<MVT::SimpleValueType>,
54 uint8_t>::value,
55 "Change uint8_t here to the SimpleValueType's type");
56 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
57 using WordType = uint64_t;
58 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
59 static unsigned constexpr NumWords = Capacity/WordWidth;
60 static_assert(NumWords*WordWidth == Capacity,
61 "Capacity should be a multiple of WordWidth");
62
63 LLVM_ATTRIBUTE_ALWAYS_INLINE
MachineValueTypeSetMachineValueTypeSet64 MachineValueTypeSet() {
65 clear();
66 }
67
68 LLVM_ATTRIBUTE_ALWAYS_INLINE
sizeMachineValueTypeSet69 unsigned size() const {
70 unsigned Count = 0;
71 for (WordType W : Words)
72 Count += llvm::popcount(W);
73 return Count;
74 }
75 LLVM_ATTRIBUTE_ALWAYS_INLINE
clearMachineValueTypeSet76 void clear() {
77 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
78 }
79 LLVM_ATTRIBUTE_ALWAYS_INLINE
emptyMachineValueTypeSet80 bool empty() const {
81 for (WordType W : Words)
82 if (W != 0)
83 return false;
84 return true;
85 }
86 LLVM_ATTRIBUTE_ALWAYS_INLINE
countMachineValueTypeSet87 unsigned count(MVT T) const {
88 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
89 }
insertMachineValueTypeSet90 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
91 bool V = count(T.SimpleTy);
92 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
93 return {*this, V};
94 }
insertMachineValueTypeSet95 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
96 for (unsigned i = 0; i != NumWords; ++i)
97 Words[i] |= S.Words[i];
98 return *this;
99 }
100 LLVM_ATTRIBUTE_ALWAYS_INLINE
eraseMachineValueTypeSet101 void erase(MVT T) {
102 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
103 }
104
105 void writeToStream(raw_ostream &OS) const;
106
107 struct const_iterator {
108 // Some implementations of the C++ library require these traits to be
109 // defined.
110 using iterator_category = std::forward_iterator_tag;
111 using value_type = MVT;
112 using difference_type = ptrdiff_t;
113 using pointer = const MVT*;
114 using reference = const MVT&;
115
116 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 MVT operator*() const {
118 assert(Pos != Capacity);
119 return MVT::SimpleValueType(Pos);
120 }
121 LLVM_ATTRIBUTE_ALWAYS_INLINE
const_iteratorMachineValueTypeSet::const_iterator122 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
123 Pos = End ? Capacity : find_from_pos(0);
124 }
125 LLVM_ATTRIBUTE_ALWAYS_INLINE
126 const_iterator &operator++() {
127 assert(Pos != Capacity);
128 Pos = find_from_pos(Pos+1);
129 return *this;
130 }
131
132 LLVM_ATTRIBUTE_ALWAYS_INLINE
133 bool operator==(const const_iterator &It) const {
134 return Set == It.Set && Pos == It.Pos;
135 }
136 LLVM_ATTRIBUTE_ALWAYS_INLINE
137 bool operator!=(const const_iterator &It) const {
138 return !operator==(It);
139 }
140
141 private:
find_from_posMachineValueTypeSet::const_iterator142 unsigned find_from_pos(unsigned P) const {
143 unsigned SkipWords = P / WordWidth;
144 unsigned SkipBits = P % WordWidth;
145 unsigned Count = SkipWords * WordWidth;
146
147 // If P is in the middle of a word, process it manually here, because
148 // the trailing bits need to be masked off to use findFirstSet.
149 if (SkipBits != 0) {
150 WordType W = Set->Words[SkipWords];
151 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
152 if (W != 0)
153 return Count + llvm::countr_zero(W);
154 Count += WordWidth;
155 SkipWords++;
156 }
157
158 for (unsigned i = SkipWords; i != NumWords; ++i) {
159 WordType W = Set->Words[i];
160 if (W != 0)
161 return Count + llvm::countr_zero(W);
162 Count += WordWidth;
163 }
164 return Capacity;
165 }
166
167 const MachineValueTypeSet *Set;
168 unsigned Pos;
169 };
170
171 LLVM_ATTRIBUTE_ALWAYS_INLINE
beginMachineValueTypeSet172 const_iterator begin() const { return const_iterator(this, false); }
173 LLVM_ATTRIBUTE_ALWAYS_INLINE
endMachineValueTypeSet174 const_iterator end() const { return const_iterator(this, true); }
175
176 LLVM_ATTRIBUTE_ALWAYS_INLINE
177 bool operator==(const MachineValueTypeSet &S) const {
178 return Words == S.Words;
179 }
180 LLVM_ATTRIBUTE_ALWAYS_INLINE
181 bool operator!=(const MachineValueTypeSet &S) const {
182 return !operator==(S);
183 }
184
185 private:
186 friend struct const_iterator;
187 std::array<WordType,NumWords> Words;
188 };
189
190 raw_ostream &operator<<(raw_ostream &OS, const MachineValueTypeSet &T);
191
192 struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
193 using SetType = MachineValueTypeSet;
194 SmallVector<unsigned, 16> AddrSpaces;
195
196 TypeSetByHwMode() = default;
197 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
198 TypeSetByHwMode &operator=(const TypeSetByHwMode &) = default;
TypeSetByHwModeTypeSetByHwMode199 TypeSetByHwMode(MVT::SimpleValueType VT)
200 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
TypeSetByHwModeTypeSetByHwMode201 TypeSetByHwMode(ValueTypeByHwMode VT)
202 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
203 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
204
getOrCreateTypeSetByHwMode205 SetType &getOrCreate(unsigned Mode) {
206 return Map[Mode];
207 }
208
209 bool isValueTypeByHwMode(bool AllowEmpty) const;
210 ValueTypeByHwMode getValueTypeByHwMode() const;
211
212 LLVM_ATTRIBUTE_ALWAYS_INLINE
isMachineValueTypeTypeSetByHwMode213 bool isMachineValueType() const {
214 return isDefaultOnly() && Map.begin()->second.size() == 1;
215 }
216
217 LLVM_ATTRIBUTE_ALWAYS_INLINE
getMachineValueTypeTypeSetByHwMode218 MVT getMachineValueType() const {
219 assert(isMachineValueType());
220 return *Map.begin()->second.begin();
221 }
222
223 bool isPossible() const;
224
225 LLVM_ATTRIBUTE_ALWAYS_INLINE
isDefaultOnlyTypeSetByHwMode226 bool isDefaultOnly() const {
227 return Map.size() == 1 && Map.begin()->first == DefaultMode;
228 }
229
isPointerTypeSetByHwMode230 bool isPointer() const {
231 return getValueTypeByHwMode().isPointer();
232 }
233
getPtrAddrSpaceTypeSetByHwMode234 unsigned getPtrAddrSpace() const {
235 assert(isPointer());
236 return getValueTypeByHwMode().PtrAddrSpace;
237 }
238
239 bool insert(const ValueTypeByHwMode &VVT);
240 bool constrain(const TypeSetByHwMode &VTS);
241 template <typename Predicate> bool constrain(Predicate P);
242 template <typename Predicate>
243 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
244
245 void writeToStream(raw_ostream &OS) const;
246
247 bool operator==(const TypeSetByHwMode &VTS) const;
248 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
249
250 void dump() const;
251 bool validate() const;
252
253 private:
254 unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
255 /// Intersect two sets. Return true if anything has changed.
256 bool intersect(SetType &Out, const SetType &In);
257 };
258
259 raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
260
261 struct TypeInfer {
TypeInferTypeInfer262 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
263
isConcreteTypeInfer264 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
265 return VTS.isValueTypeByHwMode(AllowEmpty);
266 }
getConcreteTypeInfer267 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
268 bool AllowEmpty) const {
269 assert(VTS.isValueTypeByHwMode(AllowEmpty));
270 return VTS.getValueTypeByHwMode();
271 }
272
273 /// The protocol in the following functions (Merge*, force*, Enforce*,
274 /// expand*) is to return "true" if a change has been made, "false"
275 /// otherwise.
276
277 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
MergeInTypeInfoTypeInfer278 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
279 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
280 }
MergeInTypeInfoTypeInfer281 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
282 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
283 }
284
285 /// Reduce the set \p Out to have at most one element for each mode.
286 bool forceArbitrary(TypeSetByHwMode &Out);
287
288 /// The following four functions ensure that upon return the set \p Out
289 /// will only contain types of the specified kind: integer, floating-point,
290 /// scalar, or vector.
291 /// If \p Out is empty, all legal types of the specified kind will be added
292 /// to it. Otherwise, all types that are not of the specified kind will be
293 /// removed from \p Out.
294 bool EnforceInteger(TypeSetByHwMode &Out);
295 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
296 bool EnforceScalar(TypeSetByHwMode &Out);
297 bool EnforceVector(TypeSetByHwMode &Out);
298
299 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
300 /// unchanged.
301 bool EnforceAny(TypeSetByHwMode &Out);
302 /// Make sure that for each type in \p Small, there exists a larger type
303 /// in \p Big. \p SmallIsVT indicates that this is being called for
304 /// SDTCisVTSmallerThanOp. In that case the TypeSetByHwMode is re-created for
305 /// each call and needs special consideration in how we detect changes.
306 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big,
307 bool SmallIsVT = false);
308 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
309 /// for each type U in \p Elem, U is a scalar type.
310 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
311 /// (vector) type T in \p Vec, such that U is the element type of T.
312 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
313 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
314 const ValueTypeByHwMode &VVT);
315 /// Ensure that for each type T in \p Sub, T is a vector type, and there
316 /// exists a type U in \p Vec such that U is a vector type with the same
317 /// element type as T and at least as many elements as T.
318 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
319 TypeSetByHwMode &Sub);
320 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
321 /// 2. Ensure that for each vector type T in \p V, there exists a vector
322 /// type U in \p W, such that T and U have the same number of elements.
323 /// 3. Ensure that for each vector type U in \p W, there exists a vector
324 /// type T in \p V, such that T and U have the same number of elements
325 /// (reverse of 2).
326 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
327 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
328 /// such that T and U have equal size in bits.
329 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
330 /// such that T and U have equal size in bits (reverse of 1).
331 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
332
333 /// For each overloaded type (i.e. of form *Any), replace it with the
334 /// corresponding subset of legal, specific types.
335 void expandOverloads(TypeSetByHwMode &VTS);
336 void expandOverloads(TypeSetByHwMode::SetType &Out,
337 const TypeSetByHwMode::SetType &Legal);
338
339 struct ValidateOnExit {
ValidateOnExitTypeInfer::ValidateOnExit340 ValidateOnExit(TypeSetByHwMode &T, TypeInfer &TI) : Infer(TI), VTS(T) {}
341 #ifndef NDEBUG
342 ~ValidateOnExit();
343 #else
~ValidateOnExitTypeInfer::ValidateOnExit344 ~ValidateOnExit() {} // Empty destructor with NDEBUG.
345 #endif
346 TypeInfer &Infer;
347 TypeSetByHwMode &VTS;
348 };
349
350 struct SuppressValidation {
SuppressValidationTypeInfer::SuppressValidation351 SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
352 Infer.Validate = false;
353 }
~SuppressValidationTypeInfer::SuppressValidation354 ~SuppressValidation() {
355 Infer.Validate = SavedValidate;
356 }
357 TypeInfer &Infer;
358 bool SavedValidate;
359 };
360
361 TreePattern &TP;
362 unsigned ForceMode; // Mode to use when set.
363 bool CodeGen = false; // Set during generation of matcher code.
364 bool Validate = true; // Indicate whether to validate types.
365
366 private:
367 const TypeSetByHwMode &getLegalTypes();
368
369 /// Cached legal types (in default mode).
370 bool LegalTypesCached = false;
371 TypeSetByHwMode LegalCache;
372 };
373
374 /// Set type used to track multiply used variables in patterns
375 typedef StringSet<> MultipleUseVarSet;
376
377 /// SDTypeConstraint - This is a discriminated union of constraints,
378 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
379 struct SDTypeConstraint {
380 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
381
382 unsigned OperandNo; // The operand # this constraint applies to.
383 enum {
384 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
385 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
386 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
387 } ConstraintType;
388
389 union { // The discriminated union.
390 struct {
391 unsigned OtherOperandNum;
392 } SDTCisSameAs_Info;
393 struct {
394 unsigned OtherOperandNum;
395 } SDTCisVTSmallerThanOp_Info;
396 struct {
397 unsigned BigOperandNum;
398 } SDTCisOpSmallerThanOp_Info;
399 struct {
400 unsigned OtherOperandNum;
401 } SDTCisEltOfVec_Info;
402 struct {
403 unsigned OtherOperandNum;
404 } SDTCisSubVecOfVec_Info;
405 struct {
406 unsigned OtherOperandNum;
407 } SDTCisSameNumEltsAs_Info;
408 struct {
409 unsigned OtherOperandNum;
410 } SDTCisSameSizeAs_Info;
411 } x;
412
413 // The VT for SDTCisVT and SDTCVecEltisVT.
414 // Must not be in the union because it has a non-trivial destructor.
415 ValueTypeByHwMode VVT;
416
417 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
418 /// constraint to the nodes operands. This returns true if it makes a
419 /// change, false otherwise. If a type contradiction is found, an error
420 /// is flagged.
421 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
422 TreePattern &TP) const;
423 };
424
425 /// ScopedName - A name of a node associated with a "scope" that indicates
426 /// the context (e.g. instance of Pattern or PatFrag) in which the name was
427 /// used. This enables substitution of pattern fragments while keeping track
428 /// of what name(s) were originally given to various nodes in the tree.
429 class ScopedName {
430 unsigned Scope;
431 std::string Identifier;
432 public:
ScopedName(unsigned Scope,StringRef Identifier)433 ScopedName(unsigned Scope, StringRef Identifier)
434 : Scope(Scope), Identifier(std::string(Identifier)) {
435 assert(Scope != 0 &&
436 "Scope == 0 is used to indicate predicates without arguments");
437 }
438
getScope()439 unsigned getScope() const { return Scope; }
getIdentifier()440 const std::string &getIdentifier() const { return Identifier; }
441
442 bool operator==(const ScopedName &o) const;
443 bool operator!=(const ScopedName &o) const;
444 };
445
446 /// SDNodeInfo - One of these records is created for each SDNode instance in
447 /// the target .td file. This represents the various dag nodes we will be
448 /// processing.
449 class SDNodeInfo {
450 Record *Def;
451 StringRef EnumName;
452 StringRef SDClassName;
453 unsigned Properties;
454 unsigned NumResults;
455 int NumOperands;
456 std::vector<SDTypeConstraint> TypeConstraints;
457 public:
458 // Parse the specified record.
459 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
460
getNumResults()461 unsigned getNumResults() const { return NumResults; }
462
463 /// getNumOperands - This is the number of operands required or -1 if
464 /// variadic.
getNumOperands()465 int getNumOperands() const { return NumOperands; }
getRecord()466 Record *getRecord() const { return Def; }
getEnumName()467 StringRef getEnumName() const { return EnumName; }
getSDClassName()468 StringRef getSDClassName() const { return SDClassName; }
469
getTypeConstraints()470 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
471 return TypeConstraints;
472 }
473
474 /// getKnownType - If the type constraints on this node imply a fixed type
475 /// (e.g. all stores return void, etc), then return it as an
476 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
477 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
478
479 /// hasProperty - Return true if this node has the specified property.
480 ///
hasProperty(enum SDNP Prop)481 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
482
483 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
484 /// constraints for this node to the operands of the node. This returns
485 /// true if it makes a change, false otherwise. If a type contradiction is
486 /// found, an error is flagged.
487 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
488 };
489
490 /// TreePredicateFn - This is an abstraction that represents the predicates on
491 /// a PatFrag node. This is a simple one-word wrapper around a pointer to
492 /// provide nice accessors.
493 class TreePredicateFn {
494 /// PatFragRec - This is the TreePattern for the PatFrag that we
495 /// originally came from.
496 TreePattern *PatFragRec;
497 public:
498 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
499 TreePredicateFn(TreePattern *N);
500
501
getOrigPatFragRecord()502 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
503
504 /// isAlwaysTrue - Return true if this is a noop predicate.
505 bool isAlwaysTrue() const;
506
isImmediatePattern()507 bool isImmediatePattern() const { return hasImmCode(); }
508
509 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
510 /// this is an immediate predicate. It is an error to call this on a
511 /// non-immediate pattern.
getImmediatePredicateCode()512 std::string getImmediatePredicateCode() const {
513 std::string Result = getImmCode();
514 assert(!Result.empty() && "Isn't an immediate pattern!");
515 return Result;
516 }
517
518 bool operator==(const TreePredicateFn &RHS) const {
519 return PatFragRec == RHS.PatFragRec;
520 }
521
522 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
523
524 /// Return the name to use in the generated code to reference this, this is
525 /// "Predicate_foo" if from a pattern fragment "foo".
526 std::string getFnName() const;
527
528 /// getCodeToRunOnSDNode - Return the code for the function body that
529 /// evaluates this predicate. The argument is expected to be in "Node",
530 /// not N. This handles casting and conversion to a concrete node type as
531 /// appropriate.
532 std::string getCodeToRunOnSDNode() const;
533
534 /// Get the data type of the argument to getImmediatePredicateCode().
535 StringRef getImmType() const;
536
537 /// Get a string that describes the type returned by getImmType() but is
538 /// usable as part of an identifier.
539 StringRef getImmTypeIdentifier() const;
540
541 // Predicate code uses the PatFrag's captured operands.
542 bool usesOperands() const;
543
544 // Check if the HasNoUse predicate is set.
545 bool hasNoUse() const;
546
547 // Is the desired predefined predicate for a load?
548 bool isLoad() const;
549 // Is the desired predefined predicate for a store?
550 bool isStore() const;
551 // Is the desired predefined predicate for an atomic?
552 bool isAtomic() const;
553
554 /// Is this predicate the predefined unindexed load predicate?
555 /// Is this predicate the predefined unindexed store predicate?
556 bool isUnindexed() const;
557 /// Is this predicate the predefined non-extending load predicate?
558 bool isNonExtLoad() const;
559 /// Is this predicate the predefined any-extend load predicate?
560 bool isAnyExtLoad() const;
561 /// Is this predicate the predefined sign-extend load predicate?
562 bool isSignExtLoad() const;
563 /// Is this predicate the predefined zero-extend load predicate?
564 bool isZeroExtLoad() const;
565 /// Is this predicate the predefined non-truncating store predicate?
566 bool isNonTruncStore() const;
567 /// Is this predicate the predefined truncating store predicate?
568 bool isTruncStore() const;
569
570 /// Is this predicate the predefined monotonic atomic predicate?
571 bool isAtomicOrderingMonotonic() const;
572 /// Is this predicate the predefined acquire atomic predicate?
573 bool isAtomicOrderingAcquire() const;
574 /// Is this predicate the predefined release atomic predicate?
575 bool isAtomicOrderingRelease() const;
576 /// Is this predicate the predefined acquire-release atomic predicate?
577 bool isAtomicOrderingAcquireRelease() const;
578 /// Is this predicate the predefined sequentially consistent atomic predicate?
579 bool isAtomicOrderingSequentiallyConsistent() const;
580
581 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
582 bool isAtomicOrderingAcquireOrStronger() const;
583 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
584 bool isAtomicOrderingWeakerThanAcquire() const;
585
586 /// Is this predicate the predefined release-or-stronger atomic predicate?
587 bool isAtomicOrderingReleaseOrStronger() const;
588 /// Is this predicate the predefined weaker-than-release atomic predicate?
589 bool isAtomicOrderingWeakerThanRelease() const;
590
591 /// If non-null, indicates that this predicate is a predefined memory VT
592 /// predicate for a load/store and returns the ValueType record for the memory VT.
593 Record *getMemoryVT() const;
594 /// If non-null, indicates that this predicate is a predefined memory VT
595 /// predicate (checking only the scalar type) for load/store and returns the
596 /// ValueType record for the memory VT.
597 Record *getScalarMemoryVT() const;
598
599 ListInit *getAddressSpaces() const;
600 int64_t getMinAlignment() const;
601
602 // If true, indicates that GlobalISel-based C++ code was supplied.
603 bool hasGISelPredicateCode() const;
604 std::string getGISelPredicateCode() const;
605
606 private:
607 bool hasPredCode() const;
608 bool hasImmCode() const;
609 std::string getPredCode() const;
610 std::string getImmCode() const;
611 bool immCodeUsesAPInt() const;
612 bool immCodeUsesAPFloat() const;
613
614 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
615 };
616
617 struct TreePredicateCall {
618 TreePredicateFn Fn;
619
620 // Scope -- unique identifier for retrieving named arguments. 0 is used when
621 // the predicate does not use named arguments.
622 unsigned Scope;
623
TreePredicateCallTreePredicateCall624 TreePredicateCall(const TreePredicateFn &Fn, unsigned Scope)
625 : Fn(Fn), Scope(Scope) {}
626
627 bool operator==(const TreePredicateCall &o) const {
628 return Fn == o.Fn && Scope == o.Scope;
629 }
630 bool operator!=(const TreePredicateCall &o) const {
631 return !(*this == o);
632 }
633 };
634
635 class TreePatternNode {
636 /// The type of each node result. Before and during type inference, each
637 /// result may be a set of possible types. After (successful) type inference,
638 /// each is a single concrete type.
639 std::vector<TypeSetByHwMode> Types;
640
641 /// The index of each result in results of the pattern.
642 std::vector<unsigned> ResultPerm;
643
644 /// Operator - The Record for the operator if this is an interior node (not
645 /// a leaf).
646 Record *Operator;
647
648 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
649 ///
650 Init *Val;
651
652 /// Name - The name given to this node with the :$foo notation.
653 ///
654 std::string Name;
655
656 std::vector<ScopedName> NamesAsPredicateArg;
657
658 /// PredicateCalls - The predicate functions to execute on this node to check
659 /// for a match. If this list is empty, no predicate is involved.
660 std::vector<TreePredicateCall> PredicateCalls;
661
662 /// TransformFn - The transformation function to execute on this node before
663 /// it can be substituted into the resulting instruction on a pattern match.
664 Record *TransformFn;
665
666 std::vector<TreePatternNodePtr> Children;
667
668 public:
TreePatternNode(Record * Op,std::vector<TreePatternNodePtr> Ch,unsigned NumResults)669 TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
670 unsigned NumResults)
671 : Operator(Op), Val(nullptr), TransformFn(nullptr),
672 Children(std::move(Ch)) {
673 Types.resize(NumResults);
674 ResultPerm.resize(NumResults);
675 std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
676 }
TreePatternNode(Init * val,unsigned NumResults)677 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
678 : Operator(nullptr), Val(val), TransformFn(nullptr) {
679 Types.resize(NumResults);
680 ResultPerm.resize(NumResults);
681 std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
682 }
683
hasName()684 bool hasName() const { return !Name.empty(); }
getName()685 const std::string &getName() const { return Name; }
setName(StringRef N)686 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
687
getNamesAsPredicateArg()688 const std::vector<ScopedName> &getNamesAsPredicateArg() const {
689 return NamesAsPredicateArg;
690 }
setNamesAsPredicateArg(const std::vector<ScopedName> & Names)691 void setNamesAsPredicateArg(const std::vector<ScopedName>& Names) {
692 NamesAsPredicateArg = Names;
693 }
addNameAsPredicateArg(const ScopedName & N)694 void addNameAsPredicateArg(const ScopedName &N) {
695 NamesAsPredicateArg.push_back(N);
696 }
697
isLeaf()698 bool isLeaf() const { return Val != nullptr; }
699
700 // Type accessors.
getNumTypes()701 unsigned getNumTypes() const { return Types.size(); }
getType(unsigned ResNo)702 ValueTypeByHwMode getType(unsigned ResNo) const {
703 return Types[ResNo].getValueTypeByHwMode();
704 }
getExtTypes()705 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
getExtType(unsigned ResNo)706 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
707 return Types[ResNo];
708 }
getExtType(unsigned ResNo)709 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
setType(unsigned ResNo,const TypeSetByHwMode & T)710 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
getSimpleType(unsigned ResNo)711 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
712 return Types[ResNo].getMachineValueType().SimpleTy;
713 }
714
hasConcreteType(unsigned ResNo)715 bool hasConcreteType(unsigned ResNo) const {
716 return Types[ResNo].isValueTypeByHwMode(false);
717 }
isTypeCompletelyUnknown(unsigned ResNo,TreePattern & TP)718 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
719 return Types[ResNo].empty();
720 }
721
getNumResults()722 unsigned getNumResults() const { return ResultPerm.size(); }
getResultIndex(unsigned ResNo)723 unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
setResultIndex(unsigned ResNo,unsigned RI)724 void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
725
getLeafValue()726 Init *getLeafValue() const { assert(isLeaf()); return Val; }
getOperator()727 Record *getOperator() const { assert(!isLeaf()); return Operator; }
728
getNumChildren()729 unsigned getNumChildren() const { return Children.size(); }
getChild(unsigned N)730 TreePatternNode *getChild(unsigned N) const { return Children[N].get(); }
getChildShared(unsigned N)731 const TreePatternNodePtr &getChildShared(unsigned N) const {
732 return Children[N];
733 }
setChild(unsigned i,TreePatternNodePtr N)734 void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
735
736 /// hasChild - Return true if N is any of our children.
hasChild(const TreePatternNode * N)737 bool hasChild(const TreePatternNode *N) const {
738 for (unsigned i = 0, e = Children.size(); i != e; ++i)
739 if (Children[i].get() == N)
740 return true;
741 return false;
742 }
743
744 bool hasProperTypeByHwMode() const;
745 bool hasPossibleType() const;
746 bool setDefaultMode(unsigned Mode);
747
hasAnyPredicate()748 bool hasAnyPredicate() const { return !PredicateCalls.empty(); }
749
getPredicateCalls()750 const std::vector<TreePredicateCall> &getPredicateCalls() const {
751 return PredicateCalls;
752 }
clearPredicateCalls()753 void clearPredicateCalls() { PredicateCalls.clear(); }
setPredicateCalls(const std::vector<TreePredicateCall> & Calls)754 void setPredicateCalls(const std::vector<TreePredicateCall> &Calls) {
755 assert(PredicateCalls.empty() && "Overwriting non-empty predicate list!");
756 PredicateCalls = Calls;
757 }
addPredicateCall(const TreePredicateCall & Call)758 void addPredicateCall(const TreePredicateCall &Call) {
759 assert(!Call.Fn.isAlwaysTrue() && "Empty predicate string!");
760 assert(!is_contained(PredicateCalls, Call) && "predicate applied recursively");
761 PredicateCalls.push_back(Call);
762 }
addPredicateCall(const TreePredicateFn & Fn,unsigned Scope)763 void addPredicateCall(const TreePredicateFn &Fn, unsigned Scope) {
764 assert((Scope != 0) == Fn.usesOperands());
765 addPredicateCall(TreePredicateCall(Fn, Scope));
766 }
767
getTransformFn()768 Record *getTransformFn() const { return TransformFn; }
setTransformFn(Record * Fn)769 void setTransformFn(Record *Fn) { TransformFn = Fn; }
770
771 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
772 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
773 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
774
775 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
776 /// return the ComplexPattern information, otherwise return null.
777 const ComplexPattern *
778 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
779
780 /// Returns the number of MachineInstr operands that would be produced by this
781 /// node if it mapped directly to an output Instruction's
782 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
783 /// for Operands; otherwise 1.
784 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
785
786 /// NodeHasProperty - Return true if this node has the specified property.
787 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
788
789 /// TreeHasProperty - Return true if any node in this tree has the specified
790 /// property.
791 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
792
793 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
794 /// marked isCommutative.
795 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
796
797 void print(raw_ostream &OS) const;
798 void dump() const;
799
800 public: // Higher level manipulation routines.
801
802 /// clone - Return a new copy of this tree.
803 ///
804 TreePatternNodePtr clone() const;
805
806 /// RemoveAllTypes - Recursively strip all the types of this tree.
807 void RemoveAllTypes();
808
809 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
810 /// the specified node. For this comparison, all of the state of the node
811 /// is considered, except for the assigned name. Nodes with differing names
812 /// that are otherwise identical are considered isomorphic.
813 bool isIsomorphicTo(const TreePatternNode *N,
814 const MultipleUseVarSet &DepVars) const;
815
816 /// SubstituteFormalArguments - Replace the formal arguments in this tree
817 /// with actual values specified by ArgMap.
818 void
819 SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
820
821 /// InlinePatternFragments - If this pattern refers to any pattern
822 /// fragments, return the set of inlined versions (this can be more than
823 /// one if a PatFrags record has multiple alternatives).
824 void InlinePatternFragments(TreePatternNodePtr T,
825 TreePattern &TP,
826 std::vector<TreePatternNodePtr> &OutAlternatives);
827
828 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
829 /// this node and its children in the tree. This returns true if it makes a
830 /// change, false otherwise. If a type contradiction is found, flag an error.
831 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
832
833 /// UpdateNodeType - Set the node type of N to VT if VT contains
834 /// information. If N already contains a conflicting type, then flag an
835 /// error. This returns true if any information was updated.
836 ///
837 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
838 TreePattern &TP);
839 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
840 TreePattern &TP);
841 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
842 TreePattern &TP);
843
844 // Update node type with types inferred from an instruction operand or result
845 // def from the ins/outs lists.
846 // Return true if the type changed.
847 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
848
849 /// ContainsUnresolvedType - Return true if this tree contains any
850 /// unresolved types.
851 bool ContainsUnresolvedType(TreePattern &TP) const;
852
853 /// canPatternMatch - If it is impossible for this pattern to match on this
854 /// target, fill in Reason and return false. Otherwise, return true.
855 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
856 };
857
858 inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
859 TPN.print(OS);
860 return OS;
861 }
862
863
864 /// TreePattern - Represent a pattern, used for instructions, pattern
865 /// fragments, etc.
866 ///
867 class TreePattern {
868 /// Trees - The list of pattern trees which corresponds to this pattern.
869 /// Note that PatFrag's only have a single tree.
870 ///
871 std::vector<TreePatternNodePtr> Trees;
872
873 /// NamedNodes - This is all of the nodes that have names in the trees in this
874 /// pattern.
875 StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
876
877 /// TheRecord - The actual TableGen record corresponding to this pattern.
878 ///
879 Record *TheRecord;
880
881 /// Args - This is a list of all of the arguments to this pattern (for
882 /// PatFrag patterns), which are the 'node' markers in this pattern.
883 std::vector<std::string> Args;
884
885 /// CDP - the top-level object coordinating this madness.
886 ///
887 CodeGenDAGPatterns &CDP;
888
889 /// isInputPattern - True if this is an input pattern, something to match.
890 /// False if this is an output pattern, something to emit.
891 bool isInputPattern;
892
893 /// hasError - True if the currently processed nodes have unresolvable types
894 /// or other non-fatal errors
895 bool HasError;
896
897 /// It's important that the usage of operands in ComplexPatterns is
898 /// consistent: each named operand can be defined by at most one
899 /// ComplexPattern. This records the ComplexPattern instance and the operand
900 /// number for each operand encountered in a ComplexPattern to aid in that
901 /// check.
902 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
903
904 TypeInfer Infer;
905
906 public:
907
908 /// TreePattern constructor - Parse the specified DagInits into the
909 /// current record.
910 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
911 CodeGenDAGPatterns &ise);
912 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
913 CodeGenDAGPatterns &ise);
914 TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
915 CodeGenDAGPatterns &ise);
916
917 /// getTrees - Return the tree patterns which corresponds to this pattern.
918 ///
getTrees()919 const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
getNumTrees()920 unsigned getNumTrees() const { return Trees.size(); }
getTree(unsigned i)921 const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
setTree(unsigned i,TreePatternNodePtr Tree)922 void setTree(unsigned i, TreePatternNodePtr Tree) { Trees[i] = Tree; }
getOnlyTree()923 const TreePatternNodePtr &getOnlyTree() const {
924 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
925 return Trees[0];
926 }
927
getNamedNodesMap()928 const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
929 if (NamedNodes.empty())
930 ComputeNamedNodes();
931 return NamedNodes;
932 }
933
934 /// getRecord - Return the actual TableGen record corresponding to this
935 /// pattern.
936 ///
getRecord()937 Record *getRecord() const { return TheRecord; }
938
getNumArgs()939 unsigned getNumArgs() const { return Args.size(); }
getArgName(unsigned i)940 const std::string &getArgName(unsigned i) const {
941 assert(i < Args.size() && "Argument reference out of range!");
942 return Args[i];
943 }
getArgList()944 std::vector<std::string> &getArgList() { return Args; }
945
getDAGPatterns()946 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
947
948 /// InlinePatternFragments - If this pattern refers to any pattern
949 /// fragments, inline them into place, giving us a pattern without any
950 /// PatFrags references. This may increase the number of trees in the
951 /// pattern if a PatFrags has multiple alternatives.
InlinePatternFragments()952 void InlinePatternFragments() {
953 std::vector<TreePatternNodePtr> Copy = Trees;
954 Trees.clear();
955 for (unsigned i = 0, e = Copy.size(); i != e; ++i)
956 Copy[i]->InlinePatternFragments(Copy[i], *this, Trees);
957 }
958
959 /// InferAllTypes - Infer/propagate as many types throughout the expression
960 /// patterns as possible. Return true if all types are inferred, false
961 /// otherwise. Bail out if a type contradiction is found.
962 bool InferAllTypes(
963 const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
964
965 /// error - If this is the first error in the current resolution step,
966 /// print it and set the error flag. Otherwise, continue silently.
967 void error(const Twine &Msg);
hasError()968 bool hasError() const {
969 return HasError;
970 }
resetError()971 void resetError() {
972 HasError = false;
973 }
974
getInfer()975 TypeInfer &getInfer() { return Infer; }
976
977 void print(raw_ostream &OS) const;
978 void dump() const;
979
980 private:
981 TreePatternNodePtr ParseTreePattern(Init *DI, StringRef OpName);
982 void ComputeNamedNodes();
983 void ComputeNamedNodes(TreePatternNode *N);
984 };
985
986
UpdateNodeType(unsigned ResNo,const TypeSetByHwMode & InTy,TreePattern & TP)987 inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
988 const TypeSetByHwMode &InTy,
989 TreePattern &TP) {
990 TypeSetByHwMode VTS(InTy);
991 TP.getInfer().expandOverloads(VTS);
992 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
993 }
994
UpdateNodeType(unsigned ResNo,MVT::SimpleValueType InTy,TreePattern & TP)995 inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
996 MVT::SimpleValueType InTy,
997 TreePattern &TP) {
998 TypeSetByHwMode VTS(InTy);
999 TP.getInfer().expandOverloads(VTS);
1000 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
1001 }
1002
UpdateNodeType(unsigned ResNo,ValueTypeByHwMode InTy,TreePattern & TP)1003 inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
1004 ValueTypeByHwMode InTy,
1005 TreePattern &TP) {
1006 TypeSetByHwMode VTS(InTy);
1007 TP.getInfer().expandOverloads(VTS);
1008 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
1009 }
1010
1011
1012 /// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
1013 /// that has a set ExecuteAlways / DefaultOps field.
1014 struct DAGDefaultOperand {
1015 std::vector<TreePatternNodePtr> DefaultOps;
1016 };
1017
1018 class DAGInstruction {
1019 std::vector<Record*> Results;
1020 std::vector<Record*> Operands;
1021 std::vector<Record*> ImpResults;
1022 TreePatternNodePtr SrcPattern;
1023 TreePatternNodePtr ResultPattern;
1024
1025 public:
1026 DAGInstruction(const std::vector<Record*> &results,
1027 const std::vector<Record*> &operands,
1028 const std::vector<Record*> &impresults,
1029 TreePatternNodePtr srcpattern = nullptr,
1030 TreePatternNodePtr resultpattern = nullptr)
Results(results)1031 : Results(results), Operands(operands), ImpResults(impresults),
1032 SrcPattern(srcpattern), ResultPattern(resultpattern) {}
1033
getNumResults()1034 unsigned getNumResults() const { return Results.size(); }
getNumOperands()1035 unsigned getNumOperands() const { return Operands.size(); }
getNumImpResults()1036 unsigned getNumImpResults() const { return ImpResults.size(); }
getImpResults()1037 const std::vector<Record*>& getImpResults() const { return ImpResults; }
1038
getResult(unsigned RN)1039 Record *getResult(unsigned RN) const {
1040 assert(RN < Results.size());
1041 return Results[RN];
1042 }
1043
getOperand(unsigned ON)1044 Record *getOperand(unsigned ON) const {
1045 assert(ON < Operands.size());
1046 return Operands[ON];
1047 }
1048
getImpResult(unsigned RN)1049 Record *getImpResult(unsigned RN) const {
1050 assert(RN < ImpResults.size());
1051 return ImpResults[RN];
1052 }
1053
getSrcPattern()1054 TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
getResultPattern()1055 TreePatternNodePtr getResultPattern() const { return ResultPattern; }
1056 };
1057
1058 /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
1059 /// processed to produce isel.
1060 class PatternToMatch {
1061 Record *SrcRecord; // Originating Record for the pattern.
1062 ListInit *Predicates; // Top level predicate conditions to match.
1063 TreePatternNodePtr SrcPattern; // Source pattern to match.
1064 TreePatternNodePtr DstPattern; // Resulting pattern.
1065 std::vector<Record*> Dstregs; // Physical register defs being matched.
1066 std::string HwModeFeatures;
1067 int AddedComplexity; // Add to matching pattern complexity.
1068 unsigned ID; // Unique ID for the record.
1069 unsigned ForceMode; // Force this mode in type inference when set.
1070
1071 public:
1072 PatternToMatch(Record *srcrecord, ListInit *preds, TreePatternNodePtr src,
1073 TreePatternNodePtr dst, std::vector<Record *> dstregs,
1074 int complexity, unsigned uid, unsigned setmode = 0,
1075 const Twine &hwmodefeatures = "")
SrcRecord(srcrecord)1076 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src),
1077 DstPattern(dst), Dstregs(std::move(dstregs)),
1078 HwModeFeatures(hwmodefeatures.str()), AddedComplexity(complexity),
1079 ID(uid), ForceMode(setmode) {}
1080
getSrcRecord()1081 Record *getSrcRecord() const { return SrcRecord; }
getPredicates()1082 ListInit *getPredicates() const { return Predicates; }
getSrcPattern()1083 TreePatternNode *getSrcPattern() const { return SrcPattern.get(); }
getSrcPatternShared()1084 TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
getDstPattern()1085 TreePatternNode *getDstPattern() const { return DstPattern.get(); }
getDstPatternShared()1086 TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
getDstRegs()1087 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
getHwModeFeatures()1088 StringRef getHwModeFeatures() const { return HwModeFeatures; }
getAddedComplexity()1089 int getAddedComplexity() const { return AddedComplexity; }
getID()1090 unsigned getID() const { return ID; }
getForceMode()1091 unsigned getForceMode() const { return ForceMode; }
1092
1093 std::string getPredicateCheck() const;
1094 void getPredicateRecords(SmallVectorImpl<Record *> &PredicateRecs) const;
1095
1096 /// Compute the complexity metric for the input pattern. This roughly
1097 /// corresponds to the number of nodes that are covered.
1098 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
1099 };
1100
1101 class CodeGenDAGPatterns {
1102 RecordKeeper &Records;
1103 CodeGenTarget Target;
1104 CodeGenIntrinsicTable Intrinsics;
1105
1106 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
1107 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1108 SDNodeXForms;
1109 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
1110 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1111 PatternFragments;
1112 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1113 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
1114
1115 // Specific SDNode definitions:
1116 Record *intrinsic_void_sdnode;
1117 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
1118
1119 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1120 /// value is the pattern to match, the second pattern is the result to
1121 /// emit.
1122 std::vector<PatternToMatch> PatternsToMatch;
1123
1124 TypeSetByHwMode LegalVTS;
1125
1126 using PatternRewriterFn = std::function<void (TreePattern *)>;
1127 PatternRewriterFn PatternRewriter;
1128
1129 unsigned NumScopes = 0;
1130
1131 public:
1132 CodeGenDAGPatterns(RecordKeeper &R,
1133 PatternRewriterFn PatternRewriter = nullptr);
1134
getTargetInfo()1135 CodeGenTarget &getTargetInfo() { return Target; }
getTargetInfo()1136 const CodeGenTarget &getTargetInfo() const { return Target; }
getLegalTypes()1137 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
1138
1139 Record *getSDNodeNamed(StringRef Name) const;
1140
getSDNodeInfo(Record * R)1141 const SDNodeInfo &getSDNodeInfo(Record *R) const {
1142 auto F = SDNodes.find(R);
1143 assert(F != SDNodes.end() && "Unknown node!");
1144 return F->second;
1145 }
1146
1147 // Node transformation lookups.
1148 typedef std::pair<Record*, std::string> NodeXForm;
getSDNodeTransform(Record * R)1149 const NodeXForm &getSDNodeTransform(Record *R) const {
1150 auto F = SDNodeXForms.find(R);
1151 assert(F != SDNodeXForms.end() && "Invalid transform!");
1152 return F->second;
1153 }
1154
getComplexPattern(Record * R)1155 const ComplexPattern &getComplexPattern(Record *R) const {
1156 auto F = ComplexPatterns.find(R);
1157 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1158 return F->second;
1159 }
1160
getIntrinsic(Record * R)1161 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1162 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1163 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
1164 llvm_unreachable("Unknown intrinsic!");
1165 }
1166
getIntrinsicInfo(unsigned IID)1167 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
1168 if (IID-1 < Intrinsics.size())
1169 return Intrinsics[IID-1];
1170 llvm_unreachable("Bad intrinsic ID!");
1171 }
1172
getIntrinsicID(Record * R)1173 unsigned getIntrinsicID(Record *R) const {
1174 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1175 if (Intrinsics[i].TheDef == R) return i;
1176 llvm_unreachable("Unknown intrinsic!");
1177 }
1178
getDefaultOperand(Record * R)1179 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
1180 auto F = DefaultOperands.find(R);
1181 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1182 return F->second;
1183 }
1184
1185 // Pattern Fragment information.
getPatternFragment(Record * R)1186 TreePattern *getPatternFragment(Record *R) const {
1187 auto F = PatternFragments.find(R);
1188 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1189 return F->second.get();
1190 }
getPatternFragmentIfRead(Record * R)1191 TreePattern *getPatternFragmentIfRead(Record *R) const {
1192 auto F = PatternFragments.find(R);
1193 if (F == PatternFragments.end())
1194 return nullptr;
1195 return F->second.get();
1196 }
1197
1198 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1199 LessRecordByID>::const_iterator pf_iterator;
pf_begin()1200 pf_iterator pf_begin() const { return PatternFragments.begin(); }
pf_end()1201 pf_iterator pf_end() const { return PatternFragments.end(); }
ptfs()1202 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
1203
1204 // Patterns to match information.
1205 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
ptm_begin()1206 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
ptm_end()1207 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
ptms()1208 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
1209
1210 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1211 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1212 void parseInstructionPattern(
1213 CodeGenInstruction &CGI, ListInit *Pattern,
1214 DAGInstMap &DAGInsts);
1215
getInstruction(Record * R)1216 const DAGInstruction &getInstruction(Record *R) const {
1217 auto F = Instructions.find(R);
1218 assert(F != Instructions.end() && "Unknown instruction!");
1219 return F->second;
1220 }
1221
get_intrinsic_void_sdnode()1222 Record *get_intrinsic_void_sdnode() const {
1223 return intrinsic_void_sdnode;
1224 }
get_intrinsic_w_chain_sdnode()1225 Record *get_intrinsic_w_chain_sdnode() const {
1226 return intrinsic_w_chain_sdnode;
1227 }
get_intrinsic_wo_chain_sdnode()1228 Record *get_intrinsic_wo_chain_sdnode() const {
1229 return intrinsic_wo_chain_sdnode;
1230 }
1231
allocateScope()1232 unsigned allocateScope() { return ++NumScopes; }
1233
operandHasDefault(Record * Op)1234 bool operandHasDefault(Record *Op) const {
1235 return Op->isSubClassOf("OperandWithDefaultOps") &&
1236 !getDefaultOperand(Op).DefaultOps.empty();
1237 }
1238
1239 private:
1240 void ParseNodeInfo();
1241 void ParseNodeTransforms();
1242 void ParseComplexPatterns();
1243 void ParsePatternFragments(bool OutFrags = false);
1244 void ParseDefaultOperands();
1245 void ParseInstructions();
1246 void ParsePatterns();
1247 void ExpandHwModeBasedTypes();
1248 void InferInstructionFlags();
1249 void GenerateVariants();
1250 void VerifyInstructionFlags();
1251
1252 void ParseOnePattern(Record *TheDef,
1253 TreePattern &Pattern, TreePattern &Result,
1254 const std::vector<Record *> &InstImpResults);
1255 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
1256 void FindPatternInputsAndOutputs(
1257 TreePattern &I, TreePatternNodePtr Pat,
1258 std::map<std::string, TreePatternNodePtr> &InstInputs,
1259 MapVector<std::string, TreePatternNodePtr,
1260 std::map<std::string, unsigned>> &InstResults,
1261 std::vector<Record *> &InstImpResults);
1262 };
1263
1264
ApplyTypeConstraints(TreePatternNode * N,TreePattern & TP)1265 inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1266 TreePattern &TP) const {
1267 bool MadeChange = false;
1268 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1269 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1270 return MadeChange;
1271 }
1272
1273 } // end namespace llvm
1274
1275 #endif
1276