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