Bug Summary

File:llvm/utils/TableGen/CodeGenDAGPatterns.cpp
Warning:line 1822, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CodeGenDAGPatterns.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/utils/TableGen -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/utils/TableGen -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/utils/TableGen -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/utils/TableGen -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/utils/TableGen/CodeGenDAGPatterns.cpp

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/utils/TableGen/CodeGenDAGPatterns.cpp

1//===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
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 implements the CodeGenDAGPatterns class, which is used to read and
10// represent the patterns present in a .td file for instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenDAGPatterns.h"
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/MapVector.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/Twine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/TypeSize.h"
26#include "llvm/TableGen/Error.h"
27#include "llvm/TableGen/Record.h"
28#include <algorithm>
29#include <cstdio>
30#include <iterator>
31#include <set>
32using namespace llvm;
33
34#define DEBUG_TYPE"dag-patterns" "dag-patterns"
35
36static inline bool isIntegerOrPtr(MVT VT) {
37 return VT.isInteger() || VT == MVT::iPTR;
38}
39static inline bool isFloatingPoint(MVT VT) {
40 return VT.isFloatingPoint();
41}
42static inline bool isVector(MVT VT) {
43 return VT.isVector();
44}
45static inline bool isScalar(MVT VT) {
46 return !VT.isVector();
47}
48
49template <typename Predicate>
50static bool berase_if(MachineValueTypeSet &S, Predicate P) {
51 bool Erased = false;
52 // It is ok to iterate over MachineValueTypeSet and remove elements from it
53 // at the same time.
54 for (MVT T : S) {
55 if (!P(T))
56 continue;
57 Erased = true;
58 S.erase(T);
59 }
60 return Erased;
61}
62
63// --- TypeSetByHwMode
64
65// This is a parameterized type-set class. For each mode there is a list
66// of types that are currently possible for a given tree node. Type
67// inference will apply to each mode separately.
68
69TypeSetByHwMode::TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList) {
70 for (const ValueTypeByHwMode &VVT : VTList) {
71 insert(VVT);
72 AddrSpaces.push_back(VVT.PtrAddrSpace);
73 }
74}
75
76bool TypeSetByHwMode::isValueTypeByHwMode(bool AllowEmpty) const {
77 for (const auto &I : *this) {
78 if (I.second.size() > 1)
79 return false;
80 if (!AllowEmpty && I.second.empty())
81 return false;
82 }
83 return true;
84}
85
86ValueTypeByHwMode TypeSetByHwMode::getValueTypeByHwMode() const {
87 assert(isValueTypeByHwMode(true) &&(static_cast<void> (0))
88 "The type set has multiple types for at least one HW mode")(static_cast<void> (0));
89 ValueTypeByHwMode VVT;
90 auto ASI = AddrSpaces.begin();
91
92 for (const auto &I : *this) {
93 MVT T = I.second.empty() ? MVT::Other : *I.second.begin();
94 VVT.getOrCreateTypeForMode(I.first, T);
95 if (ASI != AddrSpaces.end())
96 VVT.PtrAddrSpace = *ASI++;
97 }
98 return VVT;
99}
100
101bool TypeSetByHwMode::isPossible() const {
102 for (const auto &I : *this)
103 if (!I.second.empty())
104 return true;
105 return false;
106}
107
108bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
109 bool Changed = false;
110 bool ContainsDefault = false;
111 MVT DT = MVT::Other;
112
113 for (const auto &P : VVT) {
114 unsigned M = P.first;
115 // Make sure there exists a set for each specific mode from VVT.
116 Changed |= getOrCreate(M).insert(P.second).second;
117 // Cache VVT's default mode.
118 if (DefaultMode == M) {
119 ContainsDefault = true;
120 DT = P.second;
121 }
122 }
123
124 // If VVT has a default mode, add the corresponding type to all
125 // modes in "this" that do not exist in VVT.
126 if (ContainsDefault)
127 for (auto &I : *this)
128 if (!VVT.hasMode(I.first))
129 Changed |= I.second.insert(DT).second;
130
131 return Changed;
132}
133
134// Constrain the type set to be the intersection with VTS.
135bool TypeSetByHwMode::constrain(const TypeSetByHwMode &VTS) {
136 bool Changed = false;
137 if (hasDefault()) {
138 for (const auto &I : VTS) {
139 unsigned M = I.first;
140 if (M == DefaultMode || hasMode(M))
141 continue;
142 Map.insert({M, Map.at(DefaultMode)});
143 Changed = true;
144 }
145 }
146
147 for (auto &I : *this) {
148 unsigned M = I.first;
149 SetType &S = I.second;
150 if (VTS.hasMode(M) || VTS.hasDefault()) {
151 Changed |= intersect(I.second, VTS.get(M));
152 } else if (!S.empty()) {
153 S.clear();
154 Changed = true;
155 }
156 }
157 return Changed;
158}
159
160template <typename Predicate>
161bool TypeSetByHwMode::constrain(Predicate P) {
162 bool Changed = false;
163 for (auto &I : *this)
164 Changed |= berase_if(I.second, [&P](MVT VT) { return !P(VT); });
165 return Changed;
166}
167
168template <typename Predicate>
169bool TypeSetByHwMode::assign_if(const TypeSetByHwMode &VTS, Predicate P) {
170 assert(empty())(static_cast<void> (0));
171 for (const auto &I : VTS) {
172 SetType &S = getOrCreate(I.first);
173 for (auto J : I.second)
174 if (P(J))
175 S.insert(J);
176 }
177 return !empty();
178}
179
180void TypeSetByHwMode::writeToStream(raw_ostream &OS) const {
181 SmallVector<unsigned, 4> Modes;
182 Modes.reserve(Map.size());
183
184 for (const auto &I : *this)
185 Modes.push_back(I.first);
186 if (Modes.empty()) {
187 OS << "{}";
188 return;
189 }
190 array_pod_sort(Modes.begin(), Modes.end());
191
192 OS << '{';
193 for (unsigned M : Modes) {
194 OS << ' ' << getModeName(M) << ':';
195 writeToStream(get(M), OS);
196 }
197 OS << " }";
198}
199
200void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) {
201 SmallVector<MVT, 4> Types(S.begin(), S.end());
202 array_pod_sort(Types.begin(), Types.end());
203
204 OS << '[';
205 ListSeparator LS(" ");
206 for (const MVT &T : Types)
207 OS << LS << ValueTypeByHwMode::getMVTName(T);
208 OS << ']';
209}
210
211bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
212 // The isSimple call is much quicker than hasDefault - check this first.
213 bool IsSimple = isSimple();
214 bool VTSIsSimple = VTS.isSimple();
215 if (IsSimple && VTSIsSimple)
216 return *begin() == *VTS.begin();
217
218 // Speedup: We have a default if the set is simple.
219 bool HaveDefault = IsSimple || hasDefault();
220 bool VTSHaveDefault = VTSIsSimple || VTS.hasDefault();
221 if (HaveDefault != VTSHaveDefault)
222 return false;
223
224 SmallSet<unsigned, 4> Modes;
225 for (auto &I : *this)
226 Modes.insert(I.first);
227 for (const auto &I : VTS)
228 Modes.insert(I.first);
229
230 if (HaveDefault) {
231 // Both sets have default mode.
232 for (unsigned M : Modes) {
233 if (get(M) != VTS.get(M))
234 return false;
235 }
236 } else {
237 // Neither set has default mode.
238 for (unsigned M : Modes) {
239 // If there is no default mode, an empty set is equivalent to not having
240 // the corresponding mode.
241 bool NoModeThis = !hasMode(M) || get(M).empty();
242 bool NoModeVTS = !VTS.hasMode(M) || VTS.get(M).empty();
243 if (NoModeThis != NoModeVTS)
244 return false;
245 if (!NoModeThis)
246 if (get(M) != VTS.get(M))
247 return false;
248 }
249 }
250
251 return true;
252}
253
254namespace llvm {
255 raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T) {
256 T.writeToStream(OS);
257 return OS;
258 }
259}
260
261LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__))
262void TypeSetByHwMode::dump() const {
263 dbgs() << *this << '\n';
264}
265
266bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
267 bool OutP = Out.count(MVT::iPTR), InP = In.count(MVT::iPTR);
268 auto Int = [&In](MVT T) -> bool { return !In.count(T); };
269
270 if (OutP == InP)
271 return berase_if(Out, Int);
272
273 // Compute the intersection of scalars separately to account for only
274 // one set containing iPTR.
275 // The intersection of iPTR with a set of integer scalar types that does not
276 // include iPTR will result in the most specific scalar type:
277 // - iPTR is more specific than any set with two elements or more
278 // - iPTR is less specific than any single integer scalar type.
279 // For example
280 // { iPTR } * { i32 } -> { i32 }
281 // { iPTR } * { i32 i64 } -> { iPTR }
282 // and
283 // { iPTR i32 } * { i32 } -> { i32 }
284 // { iPTR i32 } * { i32 i64 } -> { i32 i64 }
285 // { iPTR i32 } * { i32 i64 i128 } -> { iPTR i32 }
286
287 // Compute the difference between the two sets in such a way that the
288 // iPTR is in the set that is being subtracted. This is to see if there
289 // are any extra scalars in the set without iPTR that are not in the
290 // set containing iPTR. Then the iPTR could be considered a "wildcard"
291 // matching these scalars. If there is only one such scalar, it would
292 // replace the iPTR, if there are more, the iPTR would be retained.
293 SetType Diff;
294 if (InP) {
295 Diff = Out;
296 berase_if(Diff, [&In](MVT T) { return In.count(T); });
297 // Pre-remove these elements and rely only on InP/OutP to determine
298 // whether a change has been made.
299 berase_if(Out, [&Diff](MVT T) { return Diff.count(T); });
300 } else {
301 Diff = In;
302 berase_if(Diff, [&Out](MVT T) { return Out.count(T); });
303 Out.erase(MVT::iPTR);
304 }
305
306 // The actual intersection.
307 bool Changed = berase_if(Out, Int);
308 unsigned NumD = Diff.size();
309 if (NumD == 0)
310 return Changed;
311
312 if (NumD == 1) {
313 Out.insert(*Diff.begin());
314 // This is a change only if Out was the one with iPTR (which is now
315 // being replaced).
316 Changed |= OutP;
317 } else {
318 // Multiple elements from Out are now replaced with iPTR.
319 Out.insert(MVT::iPTR);
320 Changed |= !OutP;
321 }
322 return Changed;
323}
324
325bool TypeSetByHwMode::validate() const {
326#ifndef NDEBUG1
327 if (empty())
328 return true;
329 bool AllEmpty = true;
330 for (const auto &I : *this)
331 AllEmpty &= I.second.empty();
332 return !AllEmpty;
333#endif
334 return true;
335}
336
337// --- TypeInfer
338
339bool TypeInfer::MergeInTypeInfo(TypeSetByHwMode &Out,
340 const TypeSetByHwMode &In) {
341 ValidateOnExit _1(Out, *this);
342 In.validate();
343 if (In.empty() || Out == In || TP.hasError())
344 return false;
345 if (Out.empty()) {
346 Out = In;
347 return true;
348 }
349
350 bool Changed = Out.constrain(In);
351 if (Changed && Out.empty())
352 TP.error("Type contradiction");
353
354 return Changed;
355}
356
357bool TypeInfer::forceArbitrary(TypeSetByHwMode &Out) {
358 ValidateOnExit _1(Out, *this);
359 if (TP.hasError())
360 return false;
361 assert(!Out.empty() && "cannot pick from an empty set")(static_cast<void> (0));
362
363 bool Changed = false;
364 for (auto &I : Out) {
365 TypeSetByHwMode::SetType &S = I.second;
366 if (S.size() <= 1)
367 continue;
368 MVT T = *S.begin(); // Pick the first element.
369 S.clear();
370 S.insert(T);
371 Changed = true;
372 }
373 return Changed;
374}
375
376bool TypeInfer::EnforceInteger(TypeSetByHwMode &Out) {
377 ValidateOnExit _1(Out, *this);
378 if (TP.hasError())
379 return false;
380 if (!Out.empty())
381 return Out.constrain(isIntegerOrPtr);
382
383 return Out.assign_if(getLegalTypes(), isIntegerOrPtr);
384}
385
386bool TypeInfer::EnforceFloatingPoint(TypeSetByHwMode &Out) {
387 ValidateOnExit _1(Out, *this);
388 if (TP.hasError())
389 return false;
390 if (!Out.empty())
391 return Out.constrain(isFloatingPoint);
392
393 return Out.assign_if(getLegalTypes(), isFloatingPoint);
394}
395
396bool TypeInfer::EnforceScalar(TypeSetByHwMode &Out) {
397 ValidateOnExit _1(Out, *this);
398 if (TP.hasError())
399 return false;
400 if (!Out.empty())
401 return Out.constrain(isScalar);
402
403 return Out.assign_if(getLegalTypes(), isScalar);
404}
405
406bool TypeInfer::EnforceVector(TypeSetByHwMode &Out) {
407 ValidateOnExit _1(Out, *this);
408 if (TP.hasError())
409 return false;
410 if (!Out.empty())
411 return Out.constrain(isVector);
412
413 return Out.assign_if(getLegalTypes(), isVector);
414}
415
416bool TypeInfer::EnforceAny(TypeSetByHwMode &Out) {
417 ValidateOnExit _1(Out, *this);
418 if (TP.hasError() || !Out.empty())
419 return false;
420
421 Out = getLegalTypes();
422 return true;
423}
424
425template <typename Iter, typename Pred, typename Less>
426static Iter min_if(Iter B, Iter E, Pred P, Less L) {
427 if (B == E)
428 return E;
429 Iter Min = E;
430 for (Iter I = B; I != E; ++I) {
431 if (!P(*I))
432 continue;
433 if (Min == E || L(*I, *Min))
434 Min = I;
435 }
436 return Min;
437}
438
439template <typename Iter, typename Pred, typename Less>
440static Iter max_if(Iter B, Iter E, Pred P, Less L) {
441 if (B == E)
442 return E;
443 Iter Max = E;
444 for (Iter I = B; I != E; ++I) {
445 if (!P(*I))
446 continue;
447 if (Max == E || L(*Max, *I))
448 Max = I;
449 }
450 return Max;
451}
452
453/// Make sure that for each type in Small, there exists a larger type in Big.
454bool TypeInfer::EnforceSmallerThan(TypeSetByHwMode &Small,
455 TypeSetByHwMode &Big) {
456 ValidateOnExit _1(Small, *this), _2(Big, *this);
457 if (TP.hasError())
458 return false;
459 bool Changed = false;
460
461 if (Small.empty())
462 Changed |= EnforceAny(Small);
463 if (Big.empty())
464 Changed |= EnforceAny(Big);
465
466 assert(Small.hasDefault() && Big.hasDefault())(static_cast<void> (0));
467
468 SmallVector<unsigned, 4> Modes;
469 union_modes(Small, Big, Modes);
470
471 // 1. Only allow integer or floating point types and make sure that
472 // both sides are both integer or both floating point.
473 // 2. Make sure that either both sides have vector types, or neither
474 // of them does.
475 for (unsigned M : Modes) {
476 TypeSetByHwMode::SetType &S = Small.get(M);
477 TypeSetByHwMode::SetType &B = Big.get(M);
478
479 if (any_of(S, isIntegerOrPtr) && any_of(S, isIntegerOrPtr)) {
480 auto NotInt = [](MVT VT) { return !isIntegerOrPtr(VT); };
481 Changed |= berase_if(S, NotInt);
482 Changed |= berase_if(B, NotInt);
483 } else if (any_of(S, isFloatingPoint) && any_of(B, isFloatingPoint)) {
484 auto NotFP = [](MVT VT) { return !isFloatingPoint(VT); };
485 Changed |= berase_if(S, NotFP);
486 Changed |= berase_if(B, NotFP);
487 } else if (S.empty() || B.empty()) {
488 Changed = !S.empty() || !B.empty();
489 S.clear();
490 B.clear();
491 } else {
492 TP.error("Incompatible types");
493 return Changed;
494 }
495
496 if (none_of(S, isVector) || none_of(B, isVector)) {
497 Changed |= berase_if(S, isVector);
498 Changed |= berase_if(B, isVector);
499 }
500 }
501
502 auto LT = [](MVT A, MVT B) -> bool {
503 // Always treat non-scalable MVTs as smaller than scalable MVTs for the
504 // purposes of ordering.
505 auto ASize = std::make_tuple(A.isScalableVector(), A.getScalarSizeInBits(),
506 A.getSizeInBits().getKnownMinSize());
507 auto BSize = std::make_tuple(B.isScalableVector(), B.getScalarSizeInBits(),
508 B.getSizeInBits().getKnownMinSize());
509 return ASize < BSize;
510 };
511 auto SameKindLE = [](MVT A, MVT B) -> bool {
512 // This function is used when removing elements: when a vector is compared
513 // to a non-vector or a scalable vector to any non-scalable MVT, it should
514 // return false (to avoid removal).
515 if (std::make_tuple(A.isVector(), A.isScalableVector()) !=
516 std::make_tuple(B.isVector(), B.isScalableVector()))
517 return false;
518
519 return std::make_tuple(A.getScalarSizeInBits(),
520 A.getSizeInBits().getKnownMinSize()) <=
521 std::make_tuple(B.getScalarSizeInBits(),
522 B.getSizeInBits().getKnownMinSize());
523 };
524
525 for (unsigned M : Modes) {
526 TypeSetByHwMode::SetType &S = Small.get(M);
527 TypeSetByHwMode::SetType &B = Big.get(M);
528 // MinS = min scalar in Small, remove all scalars from Big that are
529 // smaller-or-equal than MinS.
530 auto MinS = min_if(S.begin(), S.end(), isScalar, LT);
531 if (MinS != S.end())
532 Changed |= berase_if(B, std::bind(SameKindLE,
533 std::placeholders::_1, *MinS));
534
535 // MaxS = max scalar in Big, remove all scalars from Small that are
536 // larger than MaxS.
537 auto MaxS = max_if(B.begin(), B.end(), isScalar, LT);
538 if (MaxS != B.end())
539 Changed |= berase_if(S, std::bind(SameKindLE,
540 *MaxS, std::placeholders::_1));
541
542 // MinV = min vector in Small, remove all vectors from Big that are
543 // smaller-or-equal than MinV.
544 auto MinV = min_if(S.begin(), S.end(), isVector, LT);
545 if (MinV != S.end())
546 Changed |= berase_if(B, std::bind(SameKindLE,
547 std::placeholders::_1, *MinV));
548
549 // MaxV = max vector in Big, remove all vectors from Small that are
550 // larger than MaxV.
551 auto MaxV = max_if(B.begin(), B.end(), isVector, LT);
552 if (MaxV != B.end())
553 Changed |= berase_if(S, std::bind(SameKindLE,
554 *MaxV, std::placeholders::_1));
555 }
556
557 return Changed;
558}
559
560/// 1. Ensure that for each type T in Vec, T is a vector type, and that
561/// for each type U in Elem, U is a scalar type.
562/// 2. Ensure that for each (scalar) type U in Elem, there exists a (vector)
563/// type T in Vec, such that U is the element type of T.
564bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
565 TypeSetByHwMode &Elem) {
566 ValidateOnExit _1(Vec, *this), _2(Elem, *this);
567 if (TP.hasError())
568 return false;
569 bool Changed = false;
570
571 if (Vec.empty())
572 Changed |= EnforceVector(Vec);
573 if (Elem.empty())
574 Changed |= EnforceScalar(Elem);
575
576 SmallVector<unsigned, 4> Modes;
577 union_modes(Vec, Elem, Modes);
578 for (unsigned M : Modes) {
579 TypeSetByHwMode::SetType &V = Vec.get(M);
580 TypeSetByHwMode::SetType &E = Elem.get(M);
581
582 Changed |= berase_if(V, isScalar); // Scalar = !vector
583 Changed |= berase_if(E, isVector); // Vector = !scalar
584 assert(!V.empty() && !E.empty())(static_cast<void> (0));
585
586 MachineValueTypeSet VT, ST;
587 // Collect element types from the "vector" set.
588 for (MVT T : V)
589 VT.insert(T.getVectorElementType());
590 // Collect scalar types from the "element" set.
591 for (MVT T : E)
592 ST.insert(T);
593
594 // Remove from V all (vector) types whose element type is not in S.
595 Changed |= berase_if(V, [&ST](MVT T) -> bool {
596 return !ST.count(T.getVectorElementType());
597 });
598 // Remove from E all (scalar) types, for which there is no corresponding
599 // type in V.
600 Changed |= berase_if(E, [&VT](MVT T) -> bool { return !VT.count(T); });
601 }
602
603 return Changed;
604}
605
606bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
607 const ValueTypeByHwMode &VVT) {
608 TypeSetByHwMode Tmp(VVT);
609 ValidateOnExit _1(Vec, *this), _2(Tmp, *this);
610 return EnforceVectorEltTypeIs(Vec, Tmp);
611}
612
613/// Ensure that for each type T in Sub, T is a vector type, and there
614/// exists a type U in Vec such that U is a vector type with the same
615/// element type as T and at least as many elements as T.
616bool TypeInfer::EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
617 TypeSetByHwMode &Sub) {
618 ValidateOnExit _1(Vec, *this), _2(Sub, *this);
619 if (TP.hasError())
620 return false;
621
622 /// Return true if B is a suB-vector of P, i.e. P is a suPer-vector of B.
623 auto IsSubVec = [](MVT B, MVT P) -> bool {
624 if (!B.isVector() || !P.isVector())
625 return false;
626 // Logically a <4 x i32> is a valid subvector of <n x 4 x i32>
627 // but until there are obvious use-cases for this, keep the
628 // types separate.
629 if (B.isScalableVector() != P.isScalableVector())
630 return false;
631 if (B.getVectorElementType() != P.getVectorElementType())
632 return false;
633 return B.getVectorMinNumElements() < P.getVectorMinNumElements();
634 };
635
636 /// Return true if S has no element (vector type) that T is a sub-vector of,
637 /// i.e. has the same element type as T and more elements.
638 auto NoSubV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool {
639 for (auto I : S)
640 if (IsSubVec(T, I))
641 return false;
642 return true;
643 };
644
645 /// Return true if S has no element (vector type) that T is a super-vector
646 /// of, i.e. has the same element type as T and fewer elements.
647 auto NoSupV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool {
648 for (auto I : S)
649 if (IsSubVec(I, T))
650 return false;
651 return true;
652 };
653
654 bool Changed = false;
655
656 if (Vec.empty())
657 Changed |= EnforceVector(Vec);
658 if (Sub.empty())
659 Changed |= EnforceVector(Sub);
660
661 SmallVector<unsigned, 4> Modes;
662 union_modes(Vec, Sub, Modes);
663 for (unsigned M : Modes) {
664 TypeSetByHwMode::SetType &S = Sub.get(M);
665 TypeSetByHwMode::SetType &V = Vec.get(M);
666
667 Changed |= berase_if(S, isScalar);
668
669 // Erase all types from S that are not sub-vectors of a type in V.
670 Changed |= berase_if(S, std::bind(NoSubV, V, std::placeholders::_1));
671
672 // Erase all types from V that are not super-vectors of a type in S.
673 Changed |= berase_if(V, std::bind(NoSupV, S, std::placeholders::_1));
674 }
675
676 return Changed;
677}
678
679/// 1. Ensure that V has a scalar type iff W has a scalar type.
680/// 2. Ensure that for each vector type T in V, there exists a vector
681/// type U in W, such that T and U have the same number of elements.
682/// 3. Ensure that for each vector type U in W, there exists a vector
683/// type T in V, such that T and U have the same number of elements
684/// (reverse of 2).
685bool TypeInfer::EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W) {
686 ValidateOnExit _1(V, *this), _2(W, *this);
687 if (TP.hasError())
688 return false;
689
690 bool Changed = false;
691 if (V.empty())
692 Changed |= EnforceAny(V);
693 if (W.empty())
694 Changed |= EnforceAny(W);
695
696 // An actual vector type cannot have 0 elements, so we can treat scalars
697 // as zero-length vectors. This way both vectors and scalars can be
698 // processed identically.
699 auto NoLength = [](const SmallDenseSet<ElementCount> &Lengths,
700 MVT T) -> bool {
701 return !Lengths.count(T.isVector() ? T.getVectorElementCount()
702 : ElementCount::getNull());
703 };
704
705 SmallVector<unsigned, 4> Modes;
706 union_modes(V, W, Modes);
707 for (unsigned M : Modes) {
708 TypeSetByHwMode::SetType &VS = V.get(M);
709 TypeSetByHwMode::SetType &WS = W.get(M);
710
711 SmallDenseSet<ElementCount> VN, WN;
712 for (MVT T : VS)
713 VN.insert(T.isVector() ? T.getVectorElementCount()
714 : ElementCount::getNull());
715 for (MVT T : WS)
716 WN.insert(T.isVector() ? T.getVectorElementCount()
717 : ElementCount::getNull());
718
719 Changed |= berase_if(VS, std::bind(NoLength, WN, std::placeholders::_1));
720 Changed |= berase_if(WS, std::bind(NoLength, VN, std::placeholders::_1));
721 }
722 return Changed;
723}
724
725namespace {
726struct TypeSizeComparator {
727 bool operator()(const TypeSize &LHS, const TypeSize &RHS) const {
728 return std::make_tuple(LHS.isScalable(), LHS.getKnownMinValue()) <
729 std::make_tuple(RHS.isScalable(), RHS.getKnownMinValue());
730 }
731};
732} // end anonymous namespace
733
734/// 1. Ensure that for each type T in A, there exists a type U in B,
735/// such that T and U have equal size in bits.
736/// 2. Ensure that for each type U in B, there exists a type T in A
737/// such that T and U have equal size in bits (reverse of 1).
738bool TypeInfer::EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B) {
739 ValidateOnExit _1(A, *this), _2(B, *this);
740 if (TP.hasError())
741 return false;
742 bool Changed = false;
743 if (A.empty())
744 Changed |= EnforceAny(A);
745 if (B.empty())
746 Changed |= EnforceAny(B);
747
748 typedef SmallSet<TypeSize, 2, TypeSizeComparator> TypeSizeSet;
749
750 auto NoSize = [](const TypeSizeSet &Sizes, MVT T) -> bool {
751 return !Sizes.count(T.getSizeInBits());
752 };
753
754 SmallVector<unsigned, 4> Modes;
755 union_modes(A, B, Modes);
756 for (unsigned M : Modes) {
757 TypeSetByHwMode::SetType &AS = A.get(M);
758 TypeSetByHwMode::SetType &BS = B.get(M);
759 TypeSizeSet AN, BN;
760
761 for (MVT T : AS)
762 AN.insert(T.getSizeInBits());
763 for (MVT T : BS)
764 BN.insert(T.getSizeInBits());
765
766 Changed |= berase_if(AS, std::bind(NoSize, BN, std::placeholders::_1));
767 Changed |= berase_if(BS, std::bind(NoSize, AN, std::placeholders::_1));
768 }
769
770 return Changed;
771}
772
773void TypeInfer::expandOverloads(TypeSetByHwMode &VTS) {
774 ValidateOnExit _1(VTS, *this);
775 const TypeSetByHwMode &Legal = getLegalTypes();
776 assert(Legal.isDefaultOnly() && "Default-mode only expected")(static_cast<void> (0));
777 const TypeSetByHwMode::SetType &LegalTypes = Legal.get(DefaultMode);
778
779 for (auto &I : VTS)
780 expandOverloads(I.second, LegalTypes);
781}
782
783void TypeInfer::expandOverloads(TypeSetByHwMode::SetType &Out,
784 const TypeSetByHwMode::SetType &Legal) {
785 std::set<MVT> Ovs;
786 for (MVT T : Out) {
787 if (!T.isOverloaded())
788 continue;
789
790 Ovs.insert(T);
791 // MachineValueTypeSet allows iteration and erasing.
792 Out.erase(T);
793 }
794
795 for (MVT Ov : Ovs) {
796 switch (Ov.SimpleTy) {
797 case MVT::iPTRAny:
798 Out.insert(MVT::iPTR);
799 return;
800 case MVT::iAny:
801 for (MVT T : MVT::integer_valuetypes())
802 if (Legal.count(T))
803 Out.insert(T);
804 for (MVT T : MVT::integer_fixedlen_vector_valuetypes())
805 if (Legal.count(T))
806 Out.insert(T);
807 for (MVT T : MVT::integer_scalable_vector_valuetypes())
808 if (Legal.count(T))
809 Out.insert(T);
810 return;
811 case MVT::fAny:
812 for (MVT T : MVT::fp_valuetypes())
813 if (Legal.count(T))
814 Out.insert(T);
815 for (MVT T : MVT::fp_fixedlen_vector_valuetypes())
816 if (Legal.count(T))
817 Out.insert(T);
818 for (MVT T : MVT::fp_scalable_vector_valuetypes())
819 if (Legal.count(T))
820 Out.insert(T);
821 return;
822 case MVT::vAny:
823 for (MVT T : MVT::vector_valuetypes())
824 if (Legal.count(T))
825 Out.insert(T);
826 return;
827 case MVT::Any:
828 for (MVT T : MVT::all_valuetypes())
829 if (Legal.count(T))
830 Out.insert(T);
831 return;
832 default:
833 break;
834 }
835 }
836}
837
838const TypeSetByHwMode &TypeInfer::getLegalTypes() {
839 if (!LegalTypesCached) {
840 TypeSetByHwMode::SetType &LegalTypes = LegalCache.getOrCreate(DefaultMode);
841 // Stuff all types from all modes into the default mode.
842 const TypeSetByHwMode &LTS = TP.getDAGPatterns().getLegalTypes();
843 for (const auto &I : LTS)
844 LegalTypes.insert(I.second);
845 LegalTypesCached = true;
846 }
847 assert(LegalCache.isDefaultOnly() && "Default-mode only expected")(static_cast<void> (0));
848 return LegalCache;
849}
850
851#ifndef NDEBUG1
852TypeInfer::ValidateOnExit::~ValidateOnExit() {
853 if (Infer.Validate && !VTS.validate()) {
854 dbgs() << "Type set is empty for each HW mode:\n"
855 "possible type contradiction in the pattern below "
856 "(use -print-records with llvm-tblgen to see all "
857 "expanded records).\n";
858 Infer.TP.dump();
859 dbgs() << "Generated from record:\n";
860 Infer.TP.getRecord()->dump();
861 PrintFatalError(Infer.TP.getRecord()->getLoc(),
862 "Type set is empty for each HW mode in '" +
863 Infer.TP.getRecord()->getName() + "'");
864 }
865}
866#endif
867
868
869//===----------------------------------------------------------------------===//
870// ScopedName Implementation
871//===----------------------------------------------------------------------===//
872
873bool ScopedName::operator==(const ScopedName &o) const {
874 return Scope == o.Scope && Identifier == o.Identifier;
875}
876
877bool ScopedName::operator!=(const ScopedName &o) const {
878 return !(*this == o);
879}
880
881
882//===----------------------------------------------------------------------===//
883// TreePredicateFn Implementation
884//===----------------------------------------------------------------------===//
885
886/// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
887TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) {
888 assert((static_cast<void> (0))
889 (!hasPredCode() || !hasImmCode()) &&(static_cast<void> (0))
890 ".td file corrupt: can't have a node predicate *and* an imm predicate")(static_cast<void> (0));
891}
892
893bool TreePredicateFn::hasPredCode() const {
894 return isLoad() || isStore() || isAtomic() ||
895 !PatFragRec->getRecord()->getValueAsString("PredicateCode").empty();
896}
897
898std::string TreePredicateFn::getPredCode() const {
899 std::string Code;
900
901 if (!isLoad() && !isStore() && !isAtomic()) {
902 Record *MemoryVT = getMemoryVT();
903
904 if (MemoryVT)
905 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
906 "MemoryVT requires IsLoad or IsStore");
907 }
908
909 if (!isLoad() && !isStore()) {
910 if (isUnindexed())
911 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
912 "IsUnindexed requires IsLoad or IsStore");
913
914 Record *ScalarMemoryVT = getScalarMemoryVT();
915
916 if (ScalarMemoryVT)
917 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
918 "ScalarMemoryVT requires IsLoad or IsStore");
919 }
920
921 if (isLoad() + isStore() + isAtomic() > 1)
922 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
923 "IsLoad, IsStore, and IsAtomic are mutually exclusive");
924
925 if (isLoad()) {
926 if (!isUnindexed() && !isNonExtLoad() && !isAnyExtLoad() &&
927 !isSignExtLoad() && !isZeroExtLoad() && getMemoryVT() == nullptr &&
928 getScalarMemoryVT() == nullptr && getAddressSpaces() == nullptr &&
929 getMinAlignment() < 1)
930 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
931 "IsLoad cannot be used by itself");
932 } else {
933 if (isNonExtLoad())
934 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
935 "IsNonExtLoad requires IsLoad");
936 if (isAnyExtLoad())
937 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
938 "IsAnyExtLoad requires IsLoad");
939 if (isSignExtLoad())
940 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
941 "IsSignExtLoad requires IsLoad");
942 if (isZeroExtLoad())
943 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
944 "IsZeroExtLoad requires IsLoad");
945 }
946
947 if (isStore()) {
948 if (!isUnindexed() && !isTruncStore() && !isNonTruncStore() &&
949 getMemoryVT() == nullptr && getScalarMemoryVT() == nullptr &&
950 getAddressSpaces() == nullptr && getMinAlignment() < 1)
951 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
952 "IsStore cannot be used by itself");
953 } else {
954 if (isNonTruncStore())
955 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
956 "IsNonTruncStore requires IsStore");
957 if (isTruncStore())
958 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
959 "IsTruncStore requires IsStore");
960 }
961
962 if (isAtomic()) {
963 if (getMemoryVT() == nullptr && !isAtomicOrderingMonotonic() &&
964 getAddressSpaces() == nullptr &&
965 !isAtomicOrderingAcquire() && !isAtomicOrderingRelease() &&
966 !isAtomicOrderingAcquireRelease() &&
967 !isAtomicOrderingSequentiallyConsistent() &&
968 !isAtomicOrderingAcquireOrStronger() &&
969 !isAtomicOrderingReleaseOrStronger() &&
970 !isAtomicOrderingWeakerThanAcquire() &&
971 !isAtomicOrderingWeakerThanRelease())
972 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
973 "IsAtomic cannot be used by itself");
974 } else {
975 if (isAtomicOrderingMonotonic())
976 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
977 "IsAtomicOrderingMonotonic requires IsAtomic");
978 if (isAtomicOrderingAcquire())
979 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
980 "IsAtomicOrderingAcquire requires IsAtomic");
981 if (isAtomicOrderingRelease())
982 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
983 "IsAtomicOrderingRelease requires IsAtomic");
984 if (isAtomicOrderingAcquireRelease())
985 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
986 "IsAtomicOrderingAcquireRelease requires IsAtomic");
987 if (isAtomicOrderingSequentiallyConsistent())
988 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
989 "IsAtomicOrderingSequentiallyConsistent requires IsAtomic");
990 if (isAtomicOrderingAcquireOrStronger())
991 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
992 "IsAtomicOrderingAcquireOrStronger requires IsAtomic");
993 if (isAtomicOrderingReleaseOrStronger())
994 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
995 "IsAtomicOrderingReleaseOrStronger requires IsAtomic");
996 if (isAtomicOrderingWeakerThanAcquire())
997 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
998 "IsAtomicOrderingWeakerThanAcquire requires IsAtomic");
999 }
1000
1001 if (isLoad() || isStore() || isAtomic()) {
1002 if (ListInit *AddressSpaces = getAddressSpaces()) {
1003 Code += "unsigned AddrSpace = cast<MemSDNode>(N)->getAddressSpace();\n"
1004 " if (";
1005
1006 ListSeparator LS(" && ");
1007 for (Init *Val : AddressSpaces->getValues()) {
1008 Code += LS;
1009
1010 IntInit *IntVal = dyn_cast<IntInit>(Val);
1011 if (!IntVal) {
1012 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1013 "AddressSpaces element must be integer");
1014 }
1015
1016 Code += "AddrSpace != " + utostr(IntVal->getValue());
1017 }
1018
1019 Code += ")\nreturn false;\n";
1020 }
1021
1022 int64_t MinAlign = getMinAlignment();
1023 if (MinAlign > 0) {
1024 Code += "if (cast<MemSDNode>(N)->getAlign() < Align(";
1025 Code += utostr(MinAlign);
1026 Code += "))\nreturn false;\n";
1027 }
1028
1029 Record *MemoryVT = getMemoryVT();
1030
1031 if (MemoryVT)
1032 Code += ("if (cast<MemSDNode>(N)->getMemoryVT() != MVT::" +
1033 MemoryVT->getName() + ") return false;\n")
1034 .str();
1035 }
1036
1037 if (isAtomic() && isAtomicOrderingMonotonic())
1038 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != "
1039 "AtomicOrdering::Monotonic) return false;\n";
1040 if (isAtomic() && isAtomicOrderingAcquire())
1041 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != "
1042 "AtomicOrdering::Acquire) return false;\n";
1043 if (isAtomic() && isAtomicOrderingRelease())
1044 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != "
1045 "AtomicOrdering::Release) return false;\n";
1046 if (isAtomic() && isAtomicOrderingAcquireRelease())
1047 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != "
1048 "AtomicOrdering::AcquireRelease) return false;\n";
1049 if (isAtomic() && isAtomicOrderingSequentiallyConsistent())
1050 Code += "if (cast<AtomicSDNode>(N)->getMergedOrdering() != "
1051 "AtomicOrdering::SequentiallyConsistent) return false;\n";
1052
1053 if (isAtomic() && isAtomicOrderingAcquireOrStronger())
1054 Code += "if (!isAcquireOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) "
1055 "return false;\n";
1056 if (isAtomic() && isAtomicOrderingWeakerThanAcquire())
1057 Code += "if (isAcquireOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) "
1058 "return false;\n";
1059
1060 if (isAtomic() && isAtomicOrderingReleaseOrStronger())
1061 Code += "if (!isReleaseOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) "
1062 "return false;\n";
1063 if (isAtomic() && isAtomicOrderingWeakerThanRelease())
1064 Code += "if (isReleaseOrStronger(cast<AtomicSDNode>(N)->getMergedOrdering())) "
1065 "return false;\n";
1066
1067 if (isLoad() || isStore()) {
1068 StringRef SDNodeName = isLoad() ? "LoadSDNode" : "StoreSDNode";
1069
1070 if (isUnindexed())
1071 Code += ("if (cast<" + SDNodeName +
1072 ">(N)->getAddressingMode() != ISD::UNINDEXED) "
1073 "return false;\n")
1074 .str();
1075
1076 if (isLoad()) {
1077 if ((isNonExtLoad() + isAnyExtLoad() + isSignExtLoad() +
1078 isZeroExtLoad()) > 1)
1079 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1080 "IsNonExtLoad, IsAnyExtLoad, IsSignExtLoad, and "
1081 "IsZeroExtLoad are mutually exclusive");
1082 if (isNonExtLoad())
1083 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != "
1084 "ISD::NON_EXTLOAD) return false;\n";
1085 if (isAnyExtLoad())
1086 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::EXTLOAD) "
1087 "return false;\n";
1088 if (isSignExtLoad())
1089 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::SEXTLOAD) "
1090 "return false;\n";
1091 if (isZeroExtLoad())
1092 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::ZEXTLOAD) "
1093 "return false;\n";
1094 } else {
1095 if ((isNonTruncStore() + isTruncStore()) > 1)
1096 PrintFatalError(
1097 getOrigPatFragRecord()->getRecord()->getLoc(),
1098 "IsNonTruncStore, and IsTruncStore are mutually exclusive");
1099 if (isNonTruncStore())
1100 Code +=
1101 " if (cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
1102 if (isTruncStore())
1103 Code +=
1104 " if (!cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
1105 }
1106
1107 Record *ScalarMemoryVT = getScalarMemoryVT();
1108
1109 if (ScalarMemoryVT)
1110 Code += ("if (cast<" + SDNodeName +
1111 ">(N)->getMemoryVT().getScalarType() != MVT::" +
1112 ScalarMemoryVT->getName() + ") return false;\n")
1113 .str();
1114 }
1115
1116 std::string PredicateCode =
1117 std::string(PatFragRec->getRecord()->getValueAsString("PredicateCode"));
1118
1119 Code += PredicateCode;
1120
1121 if (PredicateCode.empty() && !Code.empty())
1122 Code += "return true;\n";
1123
1124 return Code;
1125}
1126
1127bool TreePredicateFn::hasImmCode() const {
1128 return !PatFragRec->getRecord()->getValueAsString("ImmediateCode").empty();
1129}
1130
1131std::string TreePredicateFn::getImmCode() const {
1132 return std::string(
1133 PatFragRec->getRecord()->getValueAsString("ImmediateCode"));
1134}
1135
1136bool TreePredicateFn::immCodeUsesAPInt() const {
1137 return getOrigPatFragRecord()->getRecord()->getValueAsBit("IsAPInt");
1138}
1139
1140bool TreePredicateFn::immCodeUsesAPFloat() const {
1141 bool Unset;
1142 // The return value will be false when IsAPFloat is unset.
1143 return getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset("IsAPFloat",
1144 Unset);
1145}
1146
1147bool TreePredicateFn::isPredefinedPredicateEqualTo(StringRef Field,
1148 bool Value) const {
1149 bool Unset;
1150 bool Result =
1151 getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset(Field, Unset);
1152 if (Unset)
1153 return false;
1154 return Result == Value;
1155}
1156bool TreePredicateFn::usesOperands() const {
1157 return isPredefinedPredicateEqualTo("PredicateCodeUsesOperands", true);
1158}
1159bool TreePredicateFn::isLoad() const {
1160 return isPredefinedPredicateEqualTo("IsLoad", true);
1161}
1162bool TreePredicateFn::isStore() const {
1163 return isPredefinedPredicateEqualTo("IsStore", true);
1164}
1165bool TreePredicateFn::isAtomic() const {
1166 return isPredefinedPredicateEqualTo("IsAtomic", true);
1167}
1168bool TreePredicateFn::isUnindexed() const {
1169 return isPredefinedPredicateEqualTo("IsUnindexed", true);
1170}
1171bool TreePredicateFn::isNonExtLoad() const {
1172 return isPredefinedPredicateEqualTo("IsNonExtLoad", true);
1173}
1174bool TreePredicateFn::isAnyExtLoad() const {
1175 return isPredefinedPredicateEqualTo("IsAnyExtLoad", true);
1176}
1177bool TreePredicateFn::isSignExtLoad() const {
1178 return isPredefinedPredicateEqualTo("IsSignExtLoad", true);
1179}
1180bool TreePredicateFn::isZeroExtLoad() const {
1181 return isPredefinedPredicateEqualTo("IsZeroExtLoad", true);
1182}
1183bool TreePredicateFn::isNonTruncStore() const {
1184 return isPredefinedPredicateEqualTo("IsTruncStore", false);
1185}
1186bool TreePredicateFn::isTruncStore() const {
1187 return isPredefinedPredicateEqualTo("IsTruncStore", true);
1188}
1189bool TreePredicateFn::isAtomicOrderingMonotonic() const {
1190 return isPredefinedPredicateEqualTo("IsAtomicOrderingMonotonic", true);
1191}
1192bool TreePredicateFn::isAtomicOrderingAcquire() const {
1193 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquire", true);
1194}
1195bool TreePredicateFn::isAtomicOrderingRelease() const {
1196 return isPredefinedPredicateEqualTo("IsAtomicOrderingRelease", true);
1197}
1198bool TreePredicateFn::isAtomicOrderingAcquireRelease() const {
1199 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireRelease", true);
1200}
1201bool TreePredicateFn::isAtomicOrderingSequentiallyConsistent() const {
1202 return isPredefinedPredicateEqualTo("IsAtomicOrderingSequentiallyConsistent",
1203 true);
1204}
1205bool TreePredicateFn::isAtomicOrderingAcquireOrStronger() const {
1206 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", true);
1207}
1208bool TreePredicateFn::isAtomicOrderingWeakerThanAcquire() const {
1209 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", false);
1210}
1211bool TreePredicateFn::isAtomicOrderingReleaseOrStronger() const {
1212 return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", true);
1213}
1214bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const {
1215 return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", false);
1216}
1217Record *TreePredicateFn::getMemoryVT() const {
1218 Record *R = getOrigPatFragRecord()->getRecord();
1219 if (R->isValueUnset("MemoryVT"))
1220 return nullptr;
1221 return R->getValueAsDef("MemoryVT");
1222}
1223
1224ListInit *TreePredicateFn::getAddressSpaces() const {
1225 Record *R = getOrigPatFragRecord()->getRecord();
1226 if (R->isValueUnset("AddressSpaces"))
1227 return nullptr;
1228 return R->getValueAsListInit("AddressSpaces");
1229}
1230
1231int64_t TreePredicateFn::getMinAlignment() const {
1232 Record *R = getOrigPatFragRecord()->getRecord();
1233 if (R->isValueUnset("MinAlignment"))
1234 return 0;
1235 return R->getValueAsInt("MinAlignment");
1236}
1237
1238Record *TreePredicateFn::getScalarMemoryVT() const {
1239 Record *R = getOrigPatFragRecord()->getRecord();
1240 if (R->isValueUnset("ScalarMemoryVT"))
1241 return nullptr;
1242 return R->getValueAsDef("ScalarMemoryVT");
1243}
1244bool TreePredicateFn::hasGISelPredicateCode() const {
1245 return !PatFragRec->getRecord()
1246 ->getValueAsString("GISelPredicateCode")
1247 .empty();
1248}
1249std::string TreePredicateFn::getGISelPredicateCode() const {
1250 return std::string(
1251 PatFragRec->getRecord()->getValueAsString("GISelPredicateCode"));
1252}
1253
1254StringRef TreePredicateFn::getImmType() const {
1255 if (immCodeUsesAPInt())
1256 return "const APInt &";
1257 if (immCodeUsesAPFloat())
1258 return "const APFloat &";
1259 return "int64_t";
1260}
1261
1262StringRef TreePredicateFn::getImmTypeIdentifier() const {
1263 if (immCodeUsesAPInt())
1264 return "APInt";
1265 if (immCodeUsesAPFloat())
1266 return "APFloat";
1267 return "I64";
1268}
1269
1270/// isAlwaysTrue - Return true if this is a noop predicate.
1271bool TreePredicateFn::isAlwaysTrue() const {
1272 return !hasPredCode() && !hasImmCode();
1273}
1274
1275/// Return the name to use in the generated code to reference this, this is
1276/// "Predicate_foo" if from a pattern fragment "foo".
1277std::string TreePredicateFn::getFnName() const {
1278 return "Predicate_" + PatFragRec->getRecord()->getName().str();
1279}
1280
1281/// getCodeToRunOnSDNode - Return the code for the function body that
1282/// evaluates this predicate. The argument is expected to be in "Node",
1283/// not N. This handles casting and conversion to a concrete node type as
1284/// appropriate.
1285std::string TreePredicateFn::getCodeToRunOnSDNode() const {
1286 // Handle immediate predicates first.
1287 std::string ImmCode = getImmCode();
1288 if (!ImmCode.empty()) {
1289 if (isLoad())
1290 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1291 "IsLoad cannot be used with ImmLeaf or its subclasses");
1292 if (isStore())
1293 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1294 "IsStore cannot be used with ImmLeaf or its subclasses");
1295 if (isUnindexed())
1296 PrintFatalError(
1297 getOrigPatFragRecord()->getRecord()->getLoc(),
1298 "IsUnindexed cannot be used with ImmLeaf or its subclasses");
1299 if (isNonExtLoad())
1300 PrintFatalError(
1301 getOrigPatFragRecord()->getRecord()->getLoc(),
1302 "IsNonExtLoad cannot be used with ImmLeaf or its subclasses");
1303 if (isAnyExtLoad())
1304 PrintFatalError(
1305 getOrigPatFragRecord()->getRecord()->getLoc(),
1306 "IsAnyExtLoad cannot be used with ImmLeaf or its subclasses");
1307 if (isSignExtLoad())
1308 PrintFatalError(
1309 getOrigPatFragRecord()->getRecord()->getLoc(),
1310 "IsSignExtLoad cannot be used with ImmLeaf or its subclasses");
1311 if (isZeroExtLoad())
1312 PrintFatalError(
1313 getOrigPatFragRecord()->getRecord()->getLoc(),
1314 "IsZeroExtLoad cannot be used with ImmLeaf or its subclasses");
1315 if (isNonTruncStore())
1316 PrintFatalError(
1317 getOrigPatFragRecord()->getRecord()->getLoc(),
1318 "IsNonTruncStore cannot be used with ImmLeaf or its subclasses");
1319 if (isTruncStore())
1320 PrintFatalError(
1321 getOrigPatFragRecord()->getRecord()->getLoc(),
1322 "IsTruncStore cannot be used with ImmLeaf or its subclasses");
1323 if (getMemoryVT())
1324 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1325 "MemoryVT cannot be used with ImmLeaf or its subclasses");
1326 if (getScalarMemoryVT())
1327 PrintFatalError(
1328 getOrigPatFragRecord()->getRecord()->getLoc(),
1329 "ScalarMemoryVT cannot be used with ImmLeaf or its subclasses");
1330
1331 std::string Result = (" " + getImmType() + " Imm = ").str();
1332 if (immCodeUsesAPFloat())
1333 Result += "cast<ConstantFPSDNode>(Node)->getValueAPF();\n";
1334 else if (immCodeUsesAPInt())
1335 Result += "cast<ConstantSDNode>(Node)->getAPIntValue();\n";
1336 else
1337 Result += "cast<ConstantSDNode>(Node)->getSExtValue();\n";
1338 return Result + ImmCode;
1339 }
1340
1341 // Handle arbitrary node predicates.
1342 assert(hasPredCode() && "Don't have any predicate code!")(static_cast<void> (0));
1343
1344 // If this is using PatFrags, there are multiple trees to search. They should
1345 // all have the same class. FIXME: Is there a way to find a common
1346 // superclass?
1347 StringRef ClassName;
1348 for (const auto &Tree : PatFragRec->getTrees()) {
1349 StringRef TreeClassName;
1350 if (Tree->isLeaf())
1351 TreeClassName = "SDNode";
1352 else {
1353 Record *Op = Tree->getOperator();
1354 const SDNodeInfo &Info = PatFragRec->getDAGPatterns().getSDNodeInfo(Op);
1355 TreeClassName = Info.getSDClassName();
1356 }
1357
1358 if (ClassName.empty())
1359 ClassName = TreeClassName;
1360 else if (ClassName != TreeClassName) {
1361 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1362 "PatFrags trees do not have consistent class");
1363 }
1364 }
1365
1366 std::string Result;
1367 if (ClassName == "SDNode")
1368 Result = " SDNode *N = Node;\n";
1369 else
1370 Result = " auto *N = cast<" + ClassName.str() + ">(Node);\n";
1371
1372 return (Twine(Result) + " (void)N;\n" + getPredCode()).str();
1373}
1374
1375//===----------------------------------------------------------------------===//
1376// PatternToMatch implementation
1377//
1378
1379static bool isImmAllOnesAllZerosMatch(const TreePatternNode *P) {
1380 if (!P->isLeaf())
1381 return false;
1382 DefInit *DI = dyn_cast<DefInit>(P->getLeafValue());
1383 if (!DI)
1384 return false;
1385
1386 Record *R = DI->getDef();
1387 return R->getName() == "immAllOnesV" || R->getName() == "immAllZerosV";
1388}
1389
1390/// getPatternSize - Return the 'size' of this pattern. We want to match large
1391/// patterns before small ones. This is used to determine the size of a
1392/// pattern.
1393static unsigned getPatternSize(const TreePatternNode *P,
1394 const CodeGenDAGPatterns &CGP) {
1395 unsigned Size = 3; // The node itself.
1396 // If the root node is a ConstantSDNode, increases its size.
1397 // e.g. (set R32:$dst, 0).
1398 if (P->isLeaf() && isa<IntInit>(P->getLeafValue()))
1399 Size += 2;
1400
1401 if (const ComplexPattern *AM = P->getComplexPatternInfo(CGP)) {
1402 Size += AM->getComplexity();
1403 // We don't want to count any children twice, so return early.
1404 return Size;
1405 }
1406
1407 // If this node has some predicate function that must match, it adds to the
1408 // complexity of this node.
1409 if (!P->getPredicateCalls().empty())
1410 ++Size;
1411
1412 // Count children in the count if they are also nodes.
1413 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
1414 const TreePatternNode *Child = P->getChild(i);
1415 if (!Child->isLeaf() && Child->getNumTypes()) {
1416 const TypeSetByHwMode &T0 = Child->getExtType(0);
1417 // At this point, all variable type sets should be simple, i.e. only
1418 // have a default mode.
1419 if (T0.getMachineValueType() != MVT::Other) {
1420 Size += getPatternSize(Child, CGP);
1421 continue;
1422 }
1423 }
1424 if (Child->isLeaf()) {
1425 if (isa<IntInit>(Child->getLeafValue()))
1426 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
1427 else if (Child->getComplexPatternInfo(CGP))
1428 Size += getPatternSize(Child, CGP);
1429 else if (isImmAllOnesAllZerosMatch(Child))
1430 Size += 4; // Matches a build_vector(+3) and a predicate (+1).
1431 else if (!Child->getPredicateCalls().empty())
1432 ++Size;
1433 }
1434 }
1435
1436 return Size;
1437}
1438
1439/// Compute the complexity metric for the input pattern. This roughly
1440/// corresponds to the number of nodes that are covered.
1441int PatternToMatch::
1442getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
1443 return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
1444}
1445
1446void PatternToMatch::getPredicateRecords(
1447 SmallVectorImpl<Record *> &PredicateRecs) const {
1448 for (Init *I : Predicates->getValues()) {
1449 if (DefInit *Pred = dyn_cast<DefInit>(I)) {
1450 Record *Def = Pred->getDef();
1451 if (!Def->isSubClassOf("Predicate")) {
1452#ifndef NDEBUG1
1453 Def->dump();
1454#endif
1455 llvm_unreachable("Unknown predicate type!")__builtin_unreachable();
1456 }
1457 PredicateRecs.push_back(Def);
1458 }
1459 }
1460 // Sort so that different orders get canonicalized to the same string.
1461 llvm::sort(PredicateRecs, LessRecord());
1462}
1463
1464/// getPredicateCheck - Return a single string containing all of this
1465/// pattern's predicates concatenated with "&&" operators.
1466///
1467std::string PatternToMatch::getPredicateCheck() const {
1468 SmallVector<Record *, 4> PredicateRecs;
1469 getPredicateRecords(PredicateRecs);
1470
1471 SmallString<128> PredicateCheck;
1472 for (Record *Pred : PredicateRecs) {
1473 StringRef CondString = Pred->getValueAsString("CondString");
1474 if (CondString.empty())
1475 continue;
1476 if (!PredicateCheck.empty())
1477 PredicateCheck += " && ";
1478 PredicateCheck += "(";
1479 PredicateCheck += CondString;
1480 PredicateCheck += ")";
1481 }
1482
1483 if (!HwModeFeatures.empty()) {
1484 if (!PredicateCheck.empty())
1485 PredicateCheck += " && ";
1486 PredicateCheck += HwModeFeatures;
1487 }
1488
1489 return std::string(PredicateCheck);
1490}
1491
1492//===----------------------------------------------------------------------===//
1493// SDTypeConstraint implementation
1494//
1495
1496SDTypeConstraint::SDTypeConstraint(Record *R, const CodeGenHwModes &CGH) {
1497 OperandNo = R->getValueAsInt("OperandNum");
1498
1499 if (R->isSubClassOf("SDTCisVT")) {
1500 ConstraintType = SDTCisVT;
1501 VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH);
1502 for (const auto &P : VVT)
1503 if (P.second == MVT::isVoid)
1504 PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT");
1505 } else if (R->isSubClassOf("SDTCisPtrTy")) {
1506 ConstraintType = SDTCisPtrTy;
1507 } else if (R->isSubClassOf("SDTCisInt")) {
1508 ConstraintType = SDTCisInt;
1509 } else if (R->isSubClassOf("SDTCisFP")) {
1510 ConstraintType = SDTCisFP;
1511 } else if (R->isSubClassOf("SDTCisVec")) {
1512 ConstraintType = SDTCisVec;
1513 } else if (R->isSubClassOf("SDTCisSameAs")) {
1514 ConstraintType = SDTCisSameAs;
1515 x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum");
1516 } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) {
1517 ConstraintType = SDTCisVTSmallerThanOp;
1518 x.SDTCisVTSmallerThanOp_Info.OtherOperandNum =
1519 R->getValueAsInt("OtherOperandNum");
1520 } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) {
1521 ConstraintType = SDTCisOpSmallerThanOp;
1522 x.SDTCisOpSmallerThanOp_Info.BigOperandNum =
1523 R->getValueAsInt("BigOperandNum");
1524 } else if (R->isSubClassOf("SDTCisEltOfVec")) {
1525 ConstraintType = SDTCisEltOfVec;
1526 x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum");
1527 } else if (R->isSubClassOf("SDTCisSubVecOfVec")) {
1528 ConstraintType = SDTCisSubVecOfVec;
1529 x.SDTCisSubVecOfVec_Info.OtherOperandNum =
1530 R->getValueAsInt("OtherOpNum");
1531 } else if (R->isSubClassOf("SDTCVecEltisVT")) {
1532 ConstraintType = SDTCVecEltisVT;
1533 VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH);
1534 for (const auto &P : VVT) {
1535 MVT T = P.second;
1536 if (T.isVector())
1537 PrintFatalError(R->getLoc(),
1538 "Cannot use vector type as SDTCVecEltisVT");
1539 if (!T.isInteger() && !T.isFloatingPoint())
1540 PrintFatalError(R->getLoc(), "Must use integer or floating point type "
1541 "as SDTCVecEltisVT");
1542 }
1543 } else if (R->isSubClassOf("SDTCisSameNumEltsAs")) {
1544 ConstraintType = SDTCisSameNumEltsAs;
1545 x.SDTCisSameNumEltsAs_Info.OtherOperandNum =
1546 R->getValueAsInt("OtherOperandNum");
1547 } else if (R->isSubClassOf("SDTCisSameSizeAs")) {
1548 ConstraintType = SDTCisSameSizeAs;
1549 x.SDTCisSameSizeAs_Info.OtherOperandNum =
1550 R->getValueAsInt("OtherOperandNum");
1551 } else {
1552 PrintFatalError(R->getLoc(),
1553 "Unrecognized SDTypeConstraint '" + R->getName() + "'!\n");
1554 }
1555}
1556
1557/// getOperandNum - Return the node corresponding to operand #OpNo in tree
1558/// N, and the result number in ResNo.
1559static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
1560 const SDNodeInfo &NodeInfo,
1561 unsigned &ResNo) {
1562 unsigned NumResults = NodeInfo.getNumResults();
1563 if (OpNo < NumResults) {
1564 ResNo = OpNo;
1565 return N;
1566 }
1567
1568 OpNo -= NumResults;
1569
1570 if (OpNo >= N->getNumChildren()) {
1571 std::string S;
1572 raw_string_ostream OS(S);
1573 OS << "Invalid operand number in type constraint "
1574 << (OpNo+NumResults) << " ";
1575 N->print(OS);
1576 PrintFatalError(OS.str());
1577 }
1578
1579 return N->getChild(OpNo);
1580}
1581
1582/// ApplyTypeConstraint - Given a node in a pattern, apply this type
1583/// constraint to the nodes operands. This returns true if it makes a
1584/// change, false otherwise. If a type contradiction is found, flag an error.
1585bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
1586 const SDNodeInfo &NodeInfo,
1587 TreePattern &TP) const {
1588 if (TP.hasError())
1589 return false;
1590
1591 unsigned ResNo = 0; // The result number being referenced.
1592 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo);
1593 TypeInfer &TI = TP.getInfer();
1594
1595 switch (ConstraintType) {
1596 case SDTCisVT:
1597 // Operand must be a particular type.
1598 return NodeToApply->UpdateNodeType(ResNo, VVT, TP);
1599 case SDTCisPtrTy:
1600 // Operand must be same as target pointer type.
1601 return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP);
1602 case SDTCisInt:
1603 // Require it to be one of the legal integer VTs.
1604 return TI.EnforceInteger(NodeToApply->getExtType(ResNo));
1605 case SDTCisFP:
1606 // Require it to be one of the legal fp VTs.
1607 return TI.EnforceFloatingPoint(NodeToApply->getExtType(ResNo));
1608 case SDTCisVec:
1609 // Require it to be one of the legal vector VTs.
1610 return TI.EnforceVector(NodeToApply->getExtType(ResNo));
1611 case SDTCisSameAs: {
1612 unsigned OResNo = 0;
1613 TreePatternNode *OtherNode =
1614 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo);
1615 return NodeToApply->UpdateNodeType(ResNo, OtherNode->getExtType(OResNo),TP)|
1616 OtherNode->UpdateNodeType(OResNo,NodeToApply->getExtType(ResNo),TP);
1617 }
1618 case SDTCisVTSmallerThanOp: {
1619 // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must
1620 // have an integer type that is smaller than the VT.
1621 if (!NodeToApply->isLeaf() ||
1622 !isa<DefInit>(NodeToApply->getLeafValue()) ||
1623 !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()
1624 ->isSubClassOf("ValueType")) {
1625 TP.error(N->getOperator()->getName() + " expects a VT operand!");
1626 return false;
1627 }
1628 DefInit *DI = static_cast<DefInit*>(NodeToApply->getLeafValue());
1629 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1630 auto VVT = getValueTypeByHwMode(DI->getDef(), T.getHwModes());
1631 TypeSetByHwMode TypeListTmp(VVT);
1632
1633 unsigned OResNo = 0;
1634 TreePatternNode *OtherNode =
1635 getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo,
1636 OResNo);
1637
1638 return TI.EnforceSmallerThan(TypeListTmp, OtherNode->getExtType(OResNo));
1639 }
1640 case SDTCisOpSmallerThanOp: {
1641 unsigned BResNo = 0;
1642 TreePatternNode *BigOperand =
1643 getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo,
1644 BResNo);
1645 return TI.EnforceSmallerThan(NodeToApply->getExtType(ResNo),
1646 BigOperand->getExtType(BResNo));
1647 }
1648 case SDTCisEltOfVec: {
1649 unsigned VResNo = 0;
1650 TreePatternNode *VecOperand =
1651 getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo,
1652 VResNo);
1653 // Filter vector types out of VecOperand that don't have the right element
1654 // type.
1655 return TI.EnforceVectorEltTypeIs(VecOperand->getExtType(VResNo),
1656 NodeToApply->getExtType(ResNo));
1657 }
1658 case SDTCisSubVecOfVec: {
1659 unsigned VResNo = 0;
1660 TreePatternNode *BigVecOperand =
1661 getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo,
1662 VResNo);
1663
1664 // Filter vector types out of BigVecOperand that don't have the
1665 // right subvector type.
1666 return TI.EnforceVectorSubVectorTypeIs(BigVecOperand->getExtType(VResNo),
1667 NodeToApply->getExtType(ResNo));
1668 }
1669 case SDTCVecEltisVT: {
1670 return TI.EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), VVT);
1671 }
1672 case SDTCisSameNumEltsAs: {
1673 unsigned OResNo = 0;
1674 TreePatternNode *OtherNode =
1675 getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum,
1676 N, NodeInfo, OResNo);
1677 return TI.EnforceSameNumElts(OtherNode->getExtType(OResNo),
1678 NodeToApply->getExtType(ResNo));
1679 }
1680 case SDTCisSameSizeAs: {
1681 unsigned OResNo = 0;
1682 TreePatternNode *OtherNode =
1683 getOperandNum(x.SDTCisSameSizeAs_Info.OtherOperandNum,
1684 N, NodeInfo, OResNo);
1685 return TI.EnforceSameSize(OtherNode->getExtType(OResNo),
1686 NodeToApply->getExtType(ResNo));
1687 }
1688 }
1689 llvm_unreachable("Invalid ConstraintType!")__builtin_unreachable();
1690}
1691
1692// Update the node type to match an instruction operand or result as specified
1693// in the ins or outs lists on the instruction definition. Return true if the
1694// type was actually changed.
1695bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
1696 Record *Operand,
1697 TreePattern &TP) {
1698 // The 'unknown' operand indicates that types should be inferred from the
1699 // context.
1700 if (Operand->isSubClassOf("unknown_class"))
1701 return false;
1702
1703 // The Operand class specifies a type directly.
1704 if (Operand->isSubClassOf("Operand")) {
1705 Record *R = Operand->getValueAsDef("Type");
1706 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1707 return UpdateNodeType(ResNo, getValueTypeByHwMode(R, T.getHwModes()), TP);
1708 }
1709
1710 // PointerLikeRegClass has a type that is determined at runtime.
1711 if (Operand->isSubClassOf("PointerLikeRegClass"))
1712 return UpdateNodeType(ResNo, MVT::iPTR, TP);
1713
1714 // Both RegisterClass and RegisterOperand operands derive their types from a
1715 // register class def.
1716 Record *RC = nullptr;
1717 if (Operand->isSubClassOf("RegisterClass"))
1718 RC = Operand;
1719 else if (Operand->isSubClassOf("RegisterOperand"))
1720 RC = Operand->getValueAsDef("RegClass");
1721
1722 assert(RC && "Unknown operand type")(static_cast<void> (0));
1723 CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo();
1724 return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP);
1725}
1726
1727bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
1728 for (unsigned i = 0, e = Types.size(); i != e; ++i)
1729 if (!TP.getInfer().isConcrete(Types[i], true))
1730 return true;
1731 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1732 if (getChild(i)->ContainsUnresolvedType(TP))
1733 return true;
1734 return false;
1735}
1736
1737bool TreePatternNode::hasProperTypeByHwMode() const {
1738 for (const TypeSetByHwMode &S : Types)
1739 if (!S.isDefaultOnly())
1740 return true;
1741 for (const TreePatternNodePtr &C : Children)
1742 if (C->hasProperTypeByHwMode())
1743 return true;
1744 return false;
1745}
1746
1747bool TreePatternNode::hasPossibleType() const {
1748 for (const TypeSetByHwMode &S : Types)
1749 if (!S.isPossible())
1750 return false;
1751 for (const TreePatternNodePtr &C : Children)
1752 if (!C->hasPossibleType())
1753 return false;
1754 return true;
1755}
1756
1757bool TreePatternNode::setDefaultMode(unsigned Mode) {
1758 for (TypeSetByHwMode &S : Types) {
1759 S.makeSimple(Mode);
1760 // Check if the selected mode had a type conflict.
1761 if (S.get(DefaultMode).empty())
1762 return false;
1763 }
1764 for (const TreePatternNodePtr &C : Children)
1765 if (!C->setDefaultMode(Mode))
1766 return false;
1767 return true;
1768}
1769
1770//===----------------------------------------------------------------------===//
1771// SDNodeInfo implementation
1772//
1773SDNodeInfo::SDNodeInfo(Record *R, const CodeGenHwModes &CGH) : Def(R) {
1774 EnumName = R->getValueAsString("Opcode");
1775 SDClassName = R->getValueAsString("SDClass");
1776 Record *TypeProfile = R->getValueAsDef("TypeProfile");
1777 NumResults = TypeProfile->getValueAsInt("NumResults");
1778 NumOperands = TypeProfile->getValueAsInt("NumOperands");
1779
1780 // Parse the properties.
1781 Properties = parseSDPatternOperatorProperties(R);
1782
1783 // Parse the type constraints.
1784 std::vector<Record*> ConstraintList =
1785 TypeProfile->getValueAsListOfDefs("Constraints");
1786 for (Record *R : ConstraintList)
1787 TypeConstraints.emplace_back(R, CGH);
1788}
1789
1790/// getKnownType - If the type constraints on this node imply a fixed type
1791/// (e.g. all stores return void, etc), then return it as an
1792/// MVT::SimpleValueType. Otherwise, return EEVT::Other.
1793MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
1794 unsigned NumResults = getNumResults();
1795 assert(NumResults <= 1 &&(static_cast<void> (0))
1796 "We only work with nodes with zero or one result so far!")(static_cast<void> (0));
1797 assert(ResNo == 0 && "Only handles single result nodes so far")(static_cast<void> (0));
1798
1799 for (const SDTypeConstraint &Constraint : TypeConstraints) {
1800 // Make sure that this applies to the correct node result.
1801 if (Constraint.OperandNo >= NumResults) // FIXME: need value #
1802 continue;
1803
1804 switch (Constraint.ConstraintType) {
1805 default: break;
1806 case SDTypeConstraint::SDTCisVT:
1807 if (Constraint.VVT.isSimple())
1808 return Constraint.VVT.getSimple().SimpleTy;
1809 break;
1810 case SDTypeConstraint::SDTCisPtrTy:
1811 return MVT::iPTR;
1812 }
1813 }
1814 return MVT::Other;
1815}
1816
1817//===----------------------------------------------------------------------===//
1818// TreePatternNode implementation
1819//
1820
1821static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
1822 if (Operator->getName() == "set" ||
10
Assuming the condition is false
11
Assuming the condition is false
12
Taking false branch
48
Called C++ object pointer is null
1823 Operator->getName() == "implicit")
1824 return 0; // All return nothing.
1825
1826 if (Operator->isSubClassOf("Intrinsic"))
13
Calling 'Record::isSubClassOf'
16
Returning from 'Record::isSubClassOf'
17
Taking false branch
1827 return CDP.getIntrinsic(Operator).IS.RetVTs.size();
1828
1829 if (Operator->isSubClassOf("SDNode"))
18
Calling 'Record::isSubClassOf'
21
Returning from 'Record::isSubClassOf'
22
Taking false branch
1830 return CDP.getSDNodeInfo(Operator).getNumResults();
1831
1832 if (Operator->isSubClassOf("PatFrags")) {
23
Calling 'Record::isSubClassOf'
30
Returning from 'Record::isSubClassOf'
31
Taking true branch
1833 // If we've already parsed this pattern fragment, get it. Otherwise, handle
1834 // the forward reference case where one pattern fragment references another
1835 // before it is processed.
1836 if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) {
32
Calling 'CodeGenDAGPatterns::getPatternFragmentIfRead'
39
Returning from 'CodeGenDAGPatterns::getPatternFragmentIfRead'
40
Assuming 'PFRec' is null
41
Taking false branch
1837 // The number of results of a fragment with alternative records is the
1838 // maximum number of results across all alternatives.
1839 unsigned NumResults = 0;
1840 for (const auto &T : PFRec->getTrees())
1841 NumResults = std::max(NumResults, T->getNumTypes());
1842 return NumResults;
1843 }
1844
1845 ListInit *LI = Operator->getValueAsListInit("Fragments");
1846 assert(LI && "Invalid Fragment")(static_cast<void> (0));
1847 unsigned NumResults = 0;
1848 for (Init *I : LI->getValues()) {
42
Assuming '__begin2' is not equal to '__end2'
1849 Record *Op = nullptr;
43
'Op' initialized to a null pointer value
1850 if (DagInit *Dag
44.1
'Dag' is null
44.1
'Dag' is null
44.1
'Dag' is null
44.1
'Dag' is null
= dyn_cast<DagInit>(I))
44
Assuming 'I' is not a 'DagInit'
45
Taking false branch
1851 if (DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
1852 Op = DI->getDef();
1853 assert(Op && "Invalid Fragment")(static_cast<void> (0));
1854 NumResults = std::max(NumResults, GetNumNodeResults(Op, CDP));
46
Passing null pointer value via 1st parameter 'Operator'
47
Calling 'GetNumNodeResults'
1855 }
1856 return NumResults;
1857 }
1858
1859 if (Operator->isSubClassOf("Instruction")) {
1860 CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator);
1861
1862 unsigned NumDefsToAdd = InstInfo.Operands.NumDefs;
1863
1864 // Subtract any defaulted outputs.
1865 for (unsigned i = 0; i != InstInfo.Operands.NumDefs; ++i) {
1866 Record *OperandNode = InstInfo.Operands[i].Rec;
1867
1868 if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
1869 !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1870 --NumDefsToAdd;
1871 }
1872
1873 // Add on one implicit def if it has a resolvable type.
1874 if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other)
1875 ++NumDefsToAdd;
1876 return NumDefsToAdd;
1877 }
1878
1879 if (Operator->isSubClassOf("SDNodeXForm"))
1880 return 1; // FIXME: Generalize SDNodeXForm
1881
1882 if (Operator->isSubClassOf("ValueType"))
1883 return 1; // A type-cast of one result.
1884
1885 if (Operator->isSubClassOf("ComplexPattern"))
1886 return 1;
1887
1888 errs() << *Operator;
1889 PrintFatalError("Unhandled node in GetNumNodeResults");
1890}
1891
1892void TreePatternNode::print(raw_ostream &OS) const {
1893 if (isLeaf())
1894 OS << *getLeafValue();
1895 else
1896 OS << '(' << getOperator()->getName();
1897
1898 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1899 OS << ':';
1900 getExtType(i).writeToStream(OS);
1901 }
1902
1903 if (!isLeaf()) {
1904 if (getNumChildren() != 0) {
1905 OS << " ";
1906 ListSeparator LS;
1907 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1908 OS << LS;
1909 getChild(i)->print(OS);
1910 }
1911 }
1912 OS << ")";
1913 }
1914
1915 for (const TreePredicateCall &Pred : PredicateCalls) {
1916 OS << "<<P:";
1917 if (Pred.Scope)
1918 OS << Pred.Scope << ":";
1919 OS << Pred.Fn.getFnName() << ">>";
1920 }
1921 if (TransformFn)
1922 OS << "<<X:" << TransformFn->getName() << ">>";
1923 if (!getName().empty())
1924 OS << ":$" << getName();
1925
1926 for (const ScopedName &Name : NamesAsPredicateArg)
1927 OS << ":$pred:" << Name.getScope() << ":" << Name.getIdentifier();
1928}
1929void TreePatternNode::dump() const {
1930 print(errs());
1931}
1932
1933/// isIsomorphicTo - Return true if this node is recursively
1934/// isomorphic to the specified node. For this comparison, the node's
1935/// entire state is considered. The assigned name is ignored, since
1936/// nodes with differing names are considered isomorphic. However, if
1937/// the assigned name is present in the dependent variable set, then
1938/// the assigned name is considered significant and the node is
1939/// isomorphic if the names match.
1940bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N,
1941 const MultipleUseVarSet &DepVars) const {
1942 if (N == this) return true;
1943 if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() ||
1944 getPredicateCalls() != N->getPredicateCalls() ||
1945 getTransformFn() != N->getTransformFn())
1946 return false;
1947
1948 if (isLeaf()) {
1949 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
1950 if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) {
1951 return ((DI->getDef() == NDI->getDef())
1952 && (DepVars.find(getName()) == DepVars.end()
1953 || getName() == N->getName()));
1954 }
1955 }
1956 return getLeafValue() == N->getLeafValue();
1957 }
1958
1959 if (N->getOperator() != getOperator() ||
1960 N->getNumChildren() != getNumChildren()) return false;
1961 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1962 if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars))
1963 return false;
1964 return true;
1965}
1966
1967/// clone - Make a copy of this tree and all of its children.
1968///
1969TreePatternNodePtr TreePatternNode::clone() const {
1970 TreePatternNodePtr New;
1971 if (isLeaf()) {
1972 New = std::make_shared<TreePatternNode>(getLeafValue(), getNumTypes());
1973 } else {
1974 std::vector<TreePatternNodePtr> CChildren;
1975 CChildren.reserve(Children.size());
1976 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1977 CChildren.push_back(getChild(i)->clone());
1978 New = std::make_shared<TreePatternNode>(getOperator(), std::move(CChildren),
1979 getNumTypes());
1980 }
1981 New->setName(getName());
1982 New->setNamesAsPredicateArg(getNamesAsPredicateArg());
1983 New->Types = Types;
1984 New->setPredicateCalls(getPredicateCalls());
1985 New->setTransformFn(getTransformFn());
1986 return New;
1987}
1988
1989/// RemoveAllTypes - Recursively strip all the types of this tree.
1990void TreePatternNode::RemoveAllTypes() {
1991 // Reset to unknown type.
1992 std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
1993 if (isLeaf()) return;
1994 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1995 getChild(i)->RemoveAllTypes();
1996}
1997
1998
1999/// SubstituteFormalArguments - Replace the formal arguments in this tree
2000/// with actual values specified by ArgMap.
2001void TreePatternNode::SubstituteFormalArguments(
2002 std::map<std::string, TreePatternNodePtr> &ArgMap) {
2003 if (isLeaf()) return;
2004
2005 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
2006 TreePatternNode *Child = getChild(i);
2007 if (Child->isLeaf()) {
2008 Init *Val = Child->getLeafValue();
2009 // Note that, when substituting into an output pattern, Val might be an
2010 // UnsetInit.
2011 if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) &&
2012 cast<DefInit>(Val)->getDef()->getName() == "node")) {
2013 // We found a use of a formal argument, replace it with its value.
2014 TreePatternNodePtr NewChild = ArgMap[Child->getName()];
2015 assert(NewChild && "Couldn't find formal argument!")(static_cast<void> (0));
2016 assert((Child->getPredicateCalls().empty() ||(static_cast<void> (0))
2017 NewChild->getPredicateCalls() == Child->getPredicateCalls()) &&(static_cast<void> (0))
2018 "Non-empty child predicate clobbered!")(static_cast<void> (0));
2019 setChild(i, std::move(NewChild));
2020 }
2021 } else {
2022 getChild(i)->SubstituteFormalArguments(ArgMap);
2023 }
2024 }
2025}
2026
2027
2028/// InlinePatternFragments - If this pattern refers to any pattern
2029/// fragments, return the set of inlined versions (this can be more than
2030/// one if a PatFrags record has multiple alternatives).
2031void TreePatternNode::InlinePatternFragments(
2032 TreePatternNodePtr T, TreePattern &TP,
2033 std::vector<TreePatternNodePtr> &OutAlternatives) {
2034
2035 if (TP.hasError())
2036 return;
2037
2038 if (isLeaf()) {
2039 OutAlternatives.push_back(T); // nothing to do.
2040 return;
2041 }
2042
2043 Record *Op = getOperator();
2044
2045 if (!Op->isSubClassOf("PatFrags")) {
2046 if (getNumChildren() == 0) {
2047 OutAlternatives.push_back(T);
2048 return;
2049 }
2050
2051 // Recursively inline children nodes.
2052 std::vector<std::vector<TreePatternNodePtr> > ChildAlternatives;
2053 ChildAlternatives.resize(getNumChildren());
2054 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
2055 TreePatternNodePtr Child = getChildShared(i);
2056 Child->InlinePatternFragments(Child, TP, ChildAlternatives[i]);
2057 // If there are no alternatives for any child, there are no
2058 // alternatives for this expression as whole.
2059 if (ChildAlternatives[i].empty())
2060 return;
2061
2062 assert((Child->getPredicateCalls().empty() ||(static_cast<void> (0))
2063 llvm::all_of(ChildAlternatives[i],(static_cast<void> (0))
2064 [&](const TreePatternNodePtr &NewChild) {(static_cast<void> (0))
2065 return NewChild->getPredicateCalls() ==(static_cast<void> (0))
2066 Child->getPredicateCalls();(static_cast<void> (0))
2067 })) &&(static_cast<void> (0))
2068 "Non-empty child predicate clobbered!")(static_cast<void> (0));
2069 }
2070
2071 // The end result is an all-pairs construction of the resultant pattern.
2072 std::vector<unsigned> Idxs;
2073 Idxs.resize(ChildAlternatives.size());
2074 bool NotDone;
2075 do {
2076 // Create the variant and add it to the output list.
2077 std::vector<TreePatternNodePtr> NewChildren;
2078 for (unsigned i = 0, e = ChildAlternatives.size(); i != e; ++i)
2079 NewChildren.push_back(ChildAlternatives[i][Idxs[i]]);
2080 TreePatternNodePtr R = std::make_shared<TreePatternNode>(
2081 getOperator(), std::move(NewChildren), getNumTypes());
2082
2083 // Copy over properties.
2084 R->setName(getName());
2085 R->setNamesAsPredicateArg(getNamesAsPredicateArg());
2086 R->setPredicateCalls(getPredicateCalls());
2087 R->setTransformFn(getTransformFn());
2088 for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
2089 R->setType(i, getExtType(i));
2090 for (unsigned i = 0, e = getNumResults(); i != e; ++i)
2091 R->setResultIndex(i, getResultIndex(i));
2092
2093 // Register alternative.
2094 OutAlternatives.push_back(R);
2095
2096 // Increment indices to the next permutation by incrementing the
2097 // indices from last index backward, e.g., generate the sequence
2098 // [0, 0], [0, 1], [1, 0], [1, 1].
2099 int IdxsIdx;
2100 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
2101 if (++Idxs[IdxsIdx] == ChildAlternatives[IdxsIdx].size())
2102 Idxs[IdxsIdx] = 0;
2103 else
2104 break;
2105 }
2106 NotDone = (IdxsIdx >= 0);
2107 } while (NotDone);
2108
2109 return;
2110 }
2111
2112 // Otherwise, we found a reference to a fragment. First, look up its
2113 // TreePattern record.
2114 TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op);
2115
2116 // Verify that we are passing the right number of operands.
2117 if (Frag->getNumArgs() != Children.size()) {
2118 TP.error("'" + Op->getName() + "' fragment requires " +
2119 Twine(Frag->getNumArgs()) + " operands!");
2120 return;
2121 }
2122
2123 TreePredicateFn PredFn(Frag);
2124 unsigned Scope = 0;
2125 if (TreePredicateFn(Frag).usesOperands())
2126 Scope = TP.getDAGPatterns().allocateScope();
2127
2128 // Compute the map of formal to actual arguments.
2129 std::map<std::string, TreePatternNodePtr> ArgMap;
2130 for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) {
2131 TreePatternNodePtr Child = getChildShared(i);
2132 if (Scope != 0) {
2133 Child = Child->clone();
2134 Child->addNameAsPredicateArg(ScopedName(Scope, Frag->getArgName(i)));
2135 }
2136 ArgMap[Frag->getArgName(i)] = Child;
2137 }
2138
2139 // Loop over all fragment alternatives.
2140 for (const auto &Alternative : Frag->getTrees()) {
2141 TreePatternNodePtr FragTree = Alternative->clone();
2142
2143 if (!PredFn.isAlwaysTrue())
2144 FragTree->addPredicateCall(PredFn, Scope);
2145
2146 // Resolve formal arguments to their actual value.
2147 if (Frag->getNumArgs())
2148 FragTree->SubstituteFormalArguments(ArgMap);
2149
2150 // Transfer types. Note that the resolved alternative may have fewer
2151 // (but not more) results than the PatFrags node.
2152 FragTree->setName(getName());
2153 for (unsigned i = 0, e = FragTree->getNumTypes(); i != e; ++i)
2154 FragTree->UpdateNodeType(i, getExtType(i), TP);
2155
2156 // Transfer in the old predicates.
2157 for (const TreePredicateCall &Pred : getPredicateCalls())
2158 FragTree->addPredicateCall(Pred);
2159
2160 // The fragment we inlined could have recursive inlining that is needed. See
2161 // if there are any pattern fragments in it and inline them as needed.
2162 FragTree->InlinePatternFragments(FragTree, TP, OutAlternatives);
2163 }
2164}
2165
2166/// getImplicitType - Check to see if the specified record has an implicit
2167/// type which should be applied to it. This will infer the type of register
2168/// references from the register file information, for example.
2169///
2170/// When Unnamed is set, return the type of a DAG operand with no name, such as
2171/// the F8RC register class argument in:
2172///
2173/// (COPY_TO_REGCLASS GPR:$src, F8RC)
2174///
2175/// When Unnamed is false, return the type of a named DAG operand such as the
2176/// GPR:$src operand above.
2177///
2178static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
2179 bool NotRegisters,
2180 bool Unnamed,
2181 TreePattern &TP) {
2182 CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
2183
2184 // Check to see if this is a register operand.
2185 if (R->isSubClassOf("RegisterOperand")) {
2186 assert(ResNo == 0 && "Regoperand ref only has one result!")(static_cast<void> (0));
2187 if (NotRegisters)
2188 return TypeSetByHwMode(); // Unknown.
2189 Record *RegClass = R->getValueAsDef("RegClass");
2190 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2191 return TypeSetByHwMode(T.getRegisterClass(RegClass).getValueTypes());
2192 }
2193
2194 // Check to see if this is a register or a register class.
2195 if (R->isSubClassOf("RegisterClass")) {
2196 assert(ResNo == 0 && "Regclass ref only has one result!")(static_cast<void> (0));
2197 // An unnamed register class represents itself as an i32 immediate, for
2198 // example on a COPY_TO_REGCLASS instruction.
2199 if (Unnamed)
2200 return TypeSetByHwMode(MVT::i32);
2201
2202 // In a named operand, the register class provides the possible set of
2203 // types.
2204 if (NotRegisters)
2205 return TypeSetByHwMode(); // Unknown.
2206 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2207 return TypeSetByHwMode(T.getRegisterClass(R).getValueTypes());
2208 }
2209
2210 if (R->isSubClassOf("PatFrags")) {
2211 assert(ResNo == 0 && "FIXME: PatFrag with multiple results?")(static_cast<void> (0));
2212 // Pattern fragment types will be resolved when they are inlined.
2213 return TypeSetByHwMode(); // Unknown.
2214 }
2215
2216 if (R->isSubClassOf("Register")) {
2217 assert(ResNo == 0 && "Registers only produce one result!")(static_cast<void> (0));
2218 if (NotRegisters)
2219 return TypeSetByHwMode(); // Unknown.
2220 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2221 return TypeSetByHwMode(T.getRegisterVTs(R));
2222 }
2223
2224 if (R->isSubClassOf("SubRegIndex")) {
2225 assert(ResNo == 0 && "SubRegisterIndices only produce one result!")(static_cast<void> (0));
2226 return TypeSetByHwMode(MVT::i32);
2227 }
2228
2229 if (R->isSubClassOf("ValueType")) {
2230 assert(ResNo == 0 && "This node only has one result!")(static_cast<void> (0));
2231 // An unnamed VTSDNode represents itself as an MVT::Other immediate.
2232 //
2233 // (sext_inreg GPR:$src, i16)
2234 // ~~~
2235 if (Unnamed)
2236 return TypeSetByHwMode(MVT::Other);
2237 // With a name, the ValueType simply provides the type of the named
2238 // variable.
2239 //
2240 // (sext_inreg i32:$src, i16)
2241 // ~~~~~~~~
2242 if (NotRegisters)
2243 return TypeSetByHwMode(); // Unknown.
2244 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
2245 return TypeSetByHwMode(getValueTypeByHwMode(R, CGH));
2246 }
2247
2248 if (R->isSubClassOf("CondCode")) {
2249 assert(ResNo == 0 && "This node only has one result!")(static_cast<void> (0));
2250 // Using a CondCodeSDNode.
2251 return TypeSetByHwMode(MVT::Other);
2252 }
2253
2254 if (R->isSubClassOf("ComplexPattern")) {
2255 assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?")(static_cast<void> (0));
2256 if (NotRegisters)
2257 return TypeSetByHwMode(); // Unknown.
2258 return TypeSetByHwMode(CDP.getComplexPattern(R).getValueType());
2259 }
2260 if (R->isSubClassOf("PointerLikeRegClass")) {
2261 assert(ResNo == 0 && "Regclass can only have one result!")(static_cast<void> (0));
2262 TypeSetByHwMode VTS(MVT::iPTR);
2263 TP.getInfer().expandOverloads(VTS);
2264 return VTS;
2265 }
2266
2267 if (R->getName() == "node" || R->getName() == "srcvalue" ||
2268 R->getName() == "zero_reg" || R->getName() == "immAllOnesV" ||
2269 R->getName() == "immAllZerosV" || R->getName() == "undef_tied_input") {
2270 // Placeholder.
2271 return TypeSetByHwMode(); // Unknown.
2272 }
2273
2274 if (R->isSubClassOf("Operand")) {
2275 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
2276 Record *T = R->getValueAsDef("Type");
2277 return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
2278 }
2279
2280 TP.error("Unknown node flavor used in pattern: " + R->getName());
2281 return TypeSetByHwMode(MVT::Other);
2282}
2283
2284
2285/// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
2286/// CodeGenIntrinsic information for it, otherwise return a null pointer.
2287const CodeGenIntrinsic *TreePatternNode::
2288getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
2289 if (getOperator() != CDP.get_intrinsic_void_sdnode() &&
2290 getOperator() != CDP.get_intrinsic_w_chain_sdnode() &&
2291 getOperator() != CDP.get_intrinsic_wo_chain_sdnode())
2292 return nullptr;
2293
2294 unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue();
2295 return &CDP.getIntrinsicInfo(IID);
2296}
2297
2298/// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
2299/// return the ComplexPattern information, otherwise return null.
2300const ComplexPattern *
2301TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
2302 Record *Rec;
2303 if (isLeaf()) {
2304 DefInit *DI = dyn_cast<DefInit>(getLeafValue());
2305 if (!DI)
2306 return nullptr;
2307 Rec = DI->getDef();
2308 } else
2309 Rec = getOperator();
2310
2311 if (!Rec->isSubClassOf("ComplexPattern"))
2312 return nullptr;
2313 return &CGP.getComplexPattern(Rec);
2314}
2315
2316unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
2317 // A ComplexPattern specifically declares how many results it fills in.
2318 if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
2319 return CP->getNumOperands();
2320
2321 // If MIOperandInfo is specified, that gives the count.
2322 if (isLeaf()) {
2323 DefInit *DI = dyn_cast<DefInit>(getLeafValue());
2324 if (DI && DI->getDef()->isSubClassOf("Operand")) {
2325 DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
2326 if (MIOps->getNumArgs())
2327 return MIOps->getNumArgs();
2328 }
2329 }
2330
2331 // Otherwise there is just one result.
2332 return 1;
2333}
2334
2335/// NodeHasProperty - Return true if this node has the specified property.
2336bool TreePatternNode::NodeHasProperty(SDNP Property,
2337 const CodeGenDAGPatterns &CGP) const {
2338 if (isLeaf()) {
2339 if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
2340 return CP->hasProperty(Property);
2341
2342 return false;
2343 }
2344
2345 if (Property != SDNPHasChain) {
2346 // The chain proprety is already present on the different intrinsic node
2347 // types (intrinsic_w_chain, intrinsic_void), and is not explicitly listed
2348 // on the intrinsic. Anything else is specific to the individual intrinsic.
2349 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CGP))
2350 return Int->hasProperty(Property);
2351 }
2352
2353 if (!Operator->isSubClassOf("SDPatternOperator"))
2354 return false;
2355
2356 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
2357}
2358
2359
2360
2361
2362/// TreeHasProperty - Return true if any node in this tree has the specified
2363/// property.
2364bool TreePatternNode::TreeHasProperty(SDNP Property,
2365 const CodeGenDAGPatterns &CGP) const {
2366 if (NodeHasProperty(Property, CGP))
2367 return true;
2368 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2369 if (getChild(i)->TreeHasProperty(Property, CGP))
2370 return true;
2371 return false;
2372}
2373
2374/// isCommutativeIntrinsic - Return true if the node corresponds to a
2375/// commutative intrinsic.
2376bool
2377TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const {
2378 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP))
2379 return Int->isCommutative;
2380 return false;
2381}
2382
2383static bool isOperandClass(const TreePatternNode *N, StringRef Class) {
2384 if (!N->isLeaf())
2385 return N->getOperator()->isSubClassOf(Class);
2386
2387 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
2388 if (DI && DI->getDef()->isSubClassOf(Class))
2389 return true;
2390
2391 return false;
2392}
2393
2394static void emitTooManyOperandsError(TreePattern &TP,
2395 StringRef InstName,
2396 unsigned Expected,
2397 unsigned Actual) {
2398 TP.error("Instruction '" + InstName + "' was provided " + Twine(Actual) +
2399 " operands but expected only " + Twine(Expected) + "!");
2400}
2401
2402static void emitTooFewOperandsError(TreePattern &TP,
2403 StringRef InstName,
2404 unsigned Actual) {
2405 TP.error("Instruction '" + InstName +
2406 "' expects more than the provided " + Twine(Actual) + " operands!");
2407}
2408
2409/// ApplyTypeConstraints - Apply all of the type constraints relevant to
2410/// this node and its children in the tree. This returns true if it makes a
2411/// change, false otherwise. If a type contradiction is found, flag an error.
2412bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
2413 if (TP.hasError())
2414 return false;
2415
2416 CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
2417 if (isLeaf()) {
2418 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
2419 // If it's a regclass or something else known, include the type.
2420 bool MadeChange = false;
2421 for (unsigned i = 0, e = Types.size(); i != e; ++i)
2422 MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
2423 NotRegisters,
2424 !hasName(), TP), TP);
2425 return MadeChange;
2426 }
2427
2428 if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
2429 assert(Types.size() == 1 && "Invalid IntInit")(static_cast<void> (0));
2430
2431 // Int inits are always integers. :)
2432 bool MadeChange = TP.getInfer().EnforceInteger(Types[0]);
2433
2434 if (!TP.getInfer().isConcrete(Types[0], false))
2435 return MadeChange;
2436
2437 ValueTypeByHwMode VVT = TP.getInfer().getConcrete(Types[0], false);
2438 for (auto &P : VVT) {
2439 MVT::SimpleValueType VT = P.second.SimpleTy;
2440 if (VT == MVT::iPTR || VT == MVT::iPTRAny)
2441 continue;
2442 unsigned Size = MVT(VT).getFixedSizeInBits();
2443 // Make sure that the value is representable for this type.
2444 if (Size >= 32)
2445 continue;
2446 // Check that the value doesn't use more bits than we have. It must
2447 // either be a sign- or zero-extended equivalent of the original.
2448 int64_t SignBitAndAbove = II->getValue() >> (Size - 1);
2449 if (SignBitAndAbove == -1 || SignBitAndAbove == 0 ||
2450 SignBitAndAbove == 1)
2451 continue;
2452
2453 TP.error("Integer value '" + Twine(II->getValue()) +
2454 "' is out of range for type '" + getEnumName(VT) + "'!");
2455 break;
2456 }
2457 return MadeChange;
2458 }
2459
2460 return false;
2461 }
2462
2463 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) {
2464 bool MadeChange = false;
2465
2466 // Apply the result type to the node.
2467 unsigned NumRetVTs = Int->IS.RetVTs.size();
2468 unsigned NumParamVTs = Int->IS.ParamVTs.size();
2469
2470 for (unsigned i = 0, e = NumRetVTs; i != e; ++i)
2471 MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP);
2472
2473 if (getNumChildren() != NumParamVTs + 1) {
2474 TP.error("Intrinsic '" + Int->Name + "' expects " + Twine(NumParamVTs) +
2475 " operands, not " + Twine(getNumChildren() - 1) + " operands!");
2476 return false;
2477 }
2478
2479 // Apply type info to the intrinsic ID.
2480 MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP);
2481
2482 for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) {
2483 MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters);
2484
2485 MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i];
2486 assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case")(static_cast<void> (0));
2487 MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP);
2488 }
2489 return MadeChange;
2490 }
2491
2492 if (getOperator()->isSubClassOf("SDNode")) {
2493 const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator());
2494
2495 // Check that the number of operands is sane. Negative operands -> varargs.
2496 if (NI.getNumOperands() >= 0 &&
2497 getNumChildren() != (unsigned)NI.getNumOperands()) {
2498 TP.error(getOperator()->getName() + " node requires exactly " +
2499 Twine(NI.getNumOperands()) + " operands!");
2500 return false;
2501 }
2502
2503 bool MadeChange = false;
2504 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2505 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2506 MadeChange |= NI.ApplyTypeConstraints(this, TP);
2507 return MadeChange;
2508 }
2509
2510 if (getOperator()->isSubClassOf("Instruction")) {
2511 const DAGInstruction &Inst = CDP.getInstruction(getOperator());
2512 CodeGenInstruction &InstInfo =
2513 CDP.getTargetInfo().getInstruction(getOperator());
2514
2515 bool MadeChange = false;
2516
2517 // Apply the result types to the node, these come from the things in the
2518 // (outs) list of the instruction.
2519 unsigned NumResultsToAdd = std::min(InstInfo.Operands.NumDefs,
2520 Inst.getNumResults());
2521 for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo)
2522 MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP);
2523
2524 // If the instruction has implicit defs, we apply the first one as a result.
2525 // FIXME: This sucks, it should apply all implicit defs.
2526 if (!InstInfo.ImplicitDefs.empty()) {
2527 unsigned ResNo = NumResultsToAdd;
2528
2529 // FIXME: Generalize to multiple possible types and multiple possible
2530 // ImplicitDefs.
2531 MVT::SimpleValueType VT =
2532 InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo());
2533
2534 if (VT != MVT::Other)
2535 MadeChange |= UpdateNodeType(ResNo, VT, TP);
2536 }
2537
2538 // If this is an INSERT_SUBREG, constrain the source and destination VTs to
2539 // be the same.
2540 if (getOperator()->getName() == "INSERT_SUBREG") {
2541 assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled")(static_cast<void> (0));
2542 MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP);
2543 MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP);
2544 } else if (getOperator()->getName() == "REG_SEQUENCE") {
2545 // We need to do extra, custom typechecking for REG_SEQUENCE since it is
2546 // variadic.
2547
2548 unsigned NChild = getNumChildren();
2549 if (NChild < 3) {
2550 TP.error("REG_SEQUENCE requires at least 3 operands!");
2551 return false;
2552 }
2553
2554 if (NChild % 2 == 0) {
2555 TP.error("REG_SEQUENCE requires an odd number of operands!");
2556 return false;
2557 }
2558
2559 if (!isOperandClass(getChild(0), "RegisterClass")) {
2560 TP.error("REG_SEQUENCE requires a RegisterClass for first operand!");
2561 return false;
2562 }
2563
2564 for (unsigned I = 1; I < NChild; I += 2) {
2565 TreePatternNode *SubIdxChild = getChild(I + 1);
2566 if (!isOperandClass(SubIdxChild, "SubRegIndex")) {
2567 TP.error("REG_SEQUENCE requires a SubRegIndex for operand " +
2568 Twine(I + 1) + "!");
2569 return false;
2570 }
2571 }
2572 }
2573
2574 unsigned NumResults = Inst.getNumResults();
2575 unsigned NumFixedOperands = InstInfo.Operands.size();
2576
2577 // If one or more operands with a default value appear at the end of the
2578 // formal operand list for an instruction, we allow them to be overridden
2579 // by optional operands provided in the pattern.
2580 //
2581 // But if an operand B without a default appears at any point after an
2582 // operand A with a default, then we don't allow A to be overridden,
2583 // because there would be no way to specify whether the next operand in
2584 // the pattern was intended to override A or skip it.
2585 unsigned NonOverridableOperands = NumFixedOperands;
2586 while (NonOverridableOperands > NumResults &&
2587 CDP.operandHasDefault(InstInfo.Operands[NonOverridableOperands-1].Rec))
2588 --NonOverridableOperands;
2589
2590 unsigned ChildNo = 0;
2591 assert(NumResults <= NumFixedOperands)(static_cast<void> (0));
2592 for (unsigned i = NumResults, e = NumFixedOperands; i != e; ++i) {
2593 Record *OperandNode = InstInfo.Operands[i].Rec;
2594
2595 // If the operand has a default value, do we use it? We must use the
2596 // default if we've run out of children of the pattern DAG to consume,
2597 // or if the operand is followed by a non-defaulted one.
2598 if (CDP.operandHasDefault(OperandNode) &&
2599 (i < NonOverridableOperands || ChildNo >= getNumChildren()))
2600 continue;
2601
2602 // If we have run out of child nodes and there _isn't_ a default
2603 // value we can use for the next operand, give an error.
2604 if (ChildNo >= getNumChildren()) {
2605 emitTooFewOperandsError(TP, getOperator()->getName(), getNumChildren());
2606 return false;
2607 }
2608
2609 TreePatternNode *Child = getChild(ChildNo++);
2610 unsigned ChildResNo = 0; // Instructions always use res #0 of their op.
2611
2612 // If the operand has sub-operands, they may be provided by distinct
2613 // child patterns, so attempt to match each sub-operand separately.
2614 if (OperandNode->isSubClassOf("Operand")) {
2615 DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
2616 if (unsigned NumArgs = MIOpInfo->getNumArgs()) {
2617 // But don't do that if the whole operand is being provided by
2618 // a single ComplexPattern-related Operand.
2619
2620 if (Child->getNumMIResults(CDP) < NumArgs) {
2621 // Match first sub-operand against the child we already have.
2622 Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
2623 MadeChange |=
2624 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
2625
2626 // And the remaining sub-operands against subsequent children.
2627 for (unsigned Arg = 1; Arg < NumArgs; ++Arg) {
2628 if (ChildNo >= getNumChildren()) {
2629 emitTooFewOperandsError(TP, getOperator()->getName(),
2630 getNumChildren());
2631 return false;
2632 }
2633 Child = getChild(ChildNo++);
2634
2635 SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef();
2636 MadeChange |=
2637 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
2638 }
2639 continue;
2640 }
2641 }
2642 }
2643
2644 // If we didn't match by pieces above, attempt to match the whole
2645 // operand now.
2646 MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP);
2647 }
2648
2649 if (!InstInfo.Operands.isVariadic && ChildNo != getNumChildren()) {
2650 emitTooManyOperandsError(TP, getOperator()->getName(),
2651 ChildNo, getNumChildren());
2652 return false;
2653 }
2654
2655 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2656 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2657 return MadeChange;
2658 }
2659
2660 if (getOperator()->isSubClassOf("ComplexPattern")) {
2661 bool MadeChange = false;
2662
2663 for (unsigned i = 0; i < getNumChildren(); ++i)
2664 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2665
2666 return MadeChange;
2667 }
2668
2669 assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!")(static_cast<void> (0));
2670
2671 // Node transforms always take one operand.
2672 if (getNumChildren() != 1) {
2673 TP.error("Node transform '" + getOperator()->getName() +
2674 "' requires one operand!");
2675 return false;
2676 }
2677
2678 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
2679 return MadeChange;
2680}
2681
2682/// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
2683/// RHS of a commutative operation, not the on LHS.
2684static bool OnlyOnRHSOfCommutative(TreePatternNode *N) {
2685 if (!N->isLeaf() && N->getOperator()->getName() == "imm")
2686 return true;
2687 if (N->isLeaf() && isa<IntInit>(N->getLeafValue()))
2688 return true;
2689 if (isImmAllOnesAllZerosMatch(N))
2690 return true;
2691 return false;
2692}
2693
2694
2695/// canPatternMatch - If it is impossible for this pattern to match on this
2696/// target, fill in Reason and return false. Otherwise, return true. This is
2697/// used as a sanity check for .td files (to prevent people from writing stuff
2698/// that can never possibly work), and to prevent the pattern permuter from
2699/// generating stuff that is useless.
2700bool TreePatternNode::canPatternMatch(std::string &Reason,
2701 const CodeGenDAGPatterns &CDP) {
2702 if (isLeaf()) return true;
2703
2704 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2705 if (!getChild(i)->canPatternMatch(Reason, CDP))
2706 return false;
2707
2708 // If this is an intrinsic, handle cases that would make it not match. For
2709 // example, if an operand is required to be an immediate.
2710 if (getOperator()->isSubClassOf("Intrinsic")) {
2711 // TODO:
2712 return true;
2713 }
2714
2715 if (getOperator()->isSubClassOf("ComplexPattern"))
2716 return true;
2717
2718 // If this node is a commutative operator, check that the LHS isn't an
2719 // immediate.
2720 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator());
2721 bool isCommIntrinsic = isCommutativeIntrinsic(CDP);
2722 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
2723 // Scan all of the operands of the node and make sure that only the last one
2724 // is a constant node, unless the RHS also is.
2725 if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) {
2726 unsigned Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id.
2727 for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i)
2728 if (OnlyOnRHSOfCommutative(getChild(i))) {
2729 Reason="Immediate value must be on the RHS of commutative operators!";
2730 return false;
2731 }
2732 }
2733 }
2734
2735 return true;
2736}
2737
2738//===----------------------------------------------------------------------===//
2739// TreePattern implementation
2740//
2741
2742TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
2743 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2744 isInputPattern(isInput), HasError(false),
2745 Infer(*this) {
2746 for (Init *I : RawPat->getValues())
2747 Trees.push_back(ParseTreePattern(I, ""));
2748}
2749
2750TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
2751 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2752 isInputPattern(isInput), HasError(false),
2753 Infer(*this) {
2754 Trees.push_back(ParseTreePattern(Pat, ""));
2755}
2756
2757TreePattern::TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
2758 CodeGenDAGPatterns &cdp)
2759 : TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
2760 Infer(*this) {
2761 Trees.push_back(Pat);
2762}
2763
2764void TreePattern::error(const Twine &Msg) {
2765 if (HasError)
2766 return;
2767 dump();
2768 PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg);
2769 HasError = true;
2770}
2771
2772void TreePattern::ComputeNamedNodes() {
2773 for (TreePatternNodePtr &Tree : Trees)
2774 ComputeNamedNodes(Tree.get());
2775}
2776
2777void TreePattern::ComputeNamedNodes(TreePatternNode *N) {
2778 if (!N->getName().empty())
2779 NamedNodes[N->getName()].push_back(N);
2780
2781 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2782 ComputeNamedNodes(N->getChild(i));
2783}
2784
2785TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
2786 StringRef OpName) {
2787 if (DefInit *DI = dyn_cast<DefInit>(TheInit)) {
2788 Record *R = DI->getDef();
2789
2790 // Direct reference to a leaf DagNode or PatFrag? Turn it into a
2791 // TreePatternNode of its own. For example:
2792 /// (foo GPR, imm) -> (foo GPR, (imm))
2793 if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrags"))
2794 return ParseTreePattern(
2795 DagInit::get(DI, nullptr,
2796 std::vector<std::pair<Init*, StringInit*> >()),
2797 OpName);
2798
2799 // Input argument?
2800 TreePatternNodePtr Res = std::make_shared<TreePatternNode>(DI, 1);
2801 if (R->getName() == "node" && !OpName.empty()) {
2802 if (OpName.empty())
2803 error("'node' argument requires a name to match with operand list");
2804 Args.push_back(std::string(OpName));
2805 }
2806
2807 Res->setName(OpName);
2808 return Res;
2809 }
2810
2811 // ?:$name or just $name.
2812 if (isa<UnsetInit>(TheInit)) {
2813 if (OpName.empty())
2814 error("'?' argument requires a name to match with operand list");
2815 TreePatternNodePtr Res = std::make_shared<TreePatternNode>(TheInit, 1);
2816 Args.push_back(std::string(OpName));
2817 Res->setName(OpName);
2818 return Res;
2819 }
2820
2821 if (isa<IntInit>(TheInit) || isa<BitInit>(TheInit)) {
2822 if (!OpName.empty())
2823 error("Constant int or bit argument should not have a name!");
2824 if (isa<BitInit>(TheInit))
2825 TheInit = TheInit->convertInitializerTo(IntRecTy::get());
2826 return std::make_shared<TreePatternNode>(TheInit, 1);
2827 }
2828
2829 if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) {
2830 // Turn this into an IntInit.
2831 Init *II = BI->convertInitializerTo(IntRecTy::get());
2832 if (!II || !isa<IntInit>(II))
2833 error("Bits value must be constants!");
2834 return ParseTreePattern(II, OpName);
2835 }
2836
2837 DagInit *Dag = dyn_cast<DagInit>(TheInit);
2838 if (!Dag) {
2839 TheInit->print(errs());
2840 error("Pattern has unexpected init kind!");
2841 }
2842 DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
2843 if (!OpDef) error("Pattern has unexpected operator type!");
2844 Record *Operator = OpDef->getDef();
2845
2846 if (Operator->isSubClassOf("ValueType")) {
2847 // If the operator is a ValueType, then this must be "type cast" of a leaf
2848 // node.
2849 if (Dag->getNumArgs() != 1)
2850 error("Type cast only takes one operand!");
2851
2852 TreePatternNodePtr New =
2853 ParseTreePattern(Dag->getArg(0), Dag->getArgNameStr(0));
2854
2855 // Apply the type cast.
2856 if (New->getNumTypes() != 1)
2857 error("Type cast can only have one type!");
2858 const CodeGenHwModes &CGH = getDAGPatterns().getTargetInfo().getHwModes();
2859 New->UpdateNodeType(0, getValueTypeByHwMode(Operator, CGH), *this);
2860
2861 if (!OpName.empty())
2862 error("ValueType cast should not have a name!");
2863 return New;
2864 }
2865
2866 // Verify that this is something that makes sense for an operator.
2867 if (!Operator->isSubClassOf("PatFrags") &&
2868 !Operator->isSubClassOf("SDNode") &&
2869 !Operator->isSubClassOf("Instruction") &&
2870 !Operator->isSubClassOf("SDNodeXForm") &&
2871 !Operator->isSubClassOf("Intrinsic") &&
2872 !Operator->isSubClassOf("ComplexPattern") &&
2873 Operator->getName() != "set" &&
2874 Operator->getName() != "implicit")
2875 error("Unrecognized node '" + Operator->getName() + "'!");
2876
2877 // Check to see if this is something that is illegal in an input pattern.
2878 if (isInputPattern) {
2879 if (Operator->isSubClassOf("Instruction") ||
2880 Operator->isSubClassOf("SDNodeXForm"))
2881 error("Cannot use '" + Operator->getName() + "' in an input pattern!");
2882 } else {
2883 if (Operator->isSubClassOf("Intrinsic"))
2884 error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2885
2886 if (Operator->isSubClassOf("SDNode") &&
2887 Operator->getName() != "imm" &&
2888 Operator->getName() != "timm" &&
2889 Operator->getName() != "fpimm" &&
2890 Operator->getName() != "tglobaltlsaddr" &&
2891 Operator->getName() != "tconstpool" &&
2892 Operator->getName() != "tjumptable" &&
2893 Operator->getName() != "tframeindex" &&
2894 Operator->getName() != "texternalsym" &&
2895 Operator->getName() != "tblockaddress" &&
2896 Operator->getName() != "tglobaladdr" &&
2897 Operator->getName() != "bb" &&
2898 Operator->getName() != "vt" &&
2899 Operator->getName() != "mcsym")
2900 error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2901 }
2902
2903 std::vector<TreePatternNodePtr> Children;
2904
2905 // Parse all the operands.
2906 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i)
2907 Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgNameStr(i)));
2908
2909 // Get the actual number of results before Operator is converted to an intrinsic
2910 // node (which is hard-coded to have either zero or one result).
2911 unsigned NumResults = GetNumNodeResults(Operator, CDP);
2912
2913 // If the operator is an intrinsic, then this is just syntactic sugar for
2914 // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and
2915 // convert the intrinsic name to a number.
2916 if (Operator->isSubClassOf("Intrinsic")) {
2917 const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator);
2918 unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1;
2919
2920 // If this intrinsic returns void, it must have side-effects and thus a
2921 // chain.
2922 if (Int.IS.RetVTs.empty())
2923 Operator = getDAGPatterns().get_intrinsic_void_sdnode();
2924 else if (Int.ModRef != CodeGenIntrinsic::NoMem || Int.hasSideEffects)
2925 // Has side-effects, requires chain.
2926 Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode();
2927 else // Otherwise, no chain.
2928 Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode();
2929
2930 Children.insert(Children.begin(),
2931 std::make_shared<TreePatternNode>(IntInit::get(IID), 1));
2932 }
2933
2934 if (Operator->isSubClassOf("ComplexPattern")) {
2935 for (unsigned i = 0; i < Children.size(); ++i) {
2936 TreePatternNodePtr Child = Children[i];
2937
2938 if (Child->getName().empty())
2939 error("All arguments to a ComplexPattern must be named");
2940
2941 // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
2942 // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
2943 // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
2944 auto OperandId = std::make_pair(Operator, i);
2945 auto PrevOp = ComplexPatternOperands.find(Child->getName());
2946 if (PrevOp != ComplexPatternOperands.end()) {
2947 if (PrevOp->getValue() != OperandId)
2948 error("All ComplexPattern operands must appear consistently: "
2949 "in the same order in just one ComplexPattern instance.");
2950 } else
2951 ComplexPatternOperands[Child->getName()] = OperandId;
2952 }
2953 }
2954
2955 TreePatternNodePtr Result =
2956 std::make_shared<TreePatternNode>(Operator, std::move(Children),
2957 NumResults);
2958 Result->setName(OpName);
2959
2960 if (Dag->getName()) {
2961 assert(Result->getName().empty())(static_cast<void> (0));
2962 Result->setName(Dag->getNameStr());
2963 }
2964 return Result;
2965}
2966
2967/// SimplifyTree - See if we can simplify this tree to eliminate something that
2968/// will never match in favor of something obvious that will. This is here
2969/// strictly as a convenience to target authors because it allows them to write
2970/// more type generic things and have useless type casts fold away.
2971///
2972/// This returns true if any change is made.
2973static bool SimplifyTree(TreePatternNodePtr &N) {
2974 if (N->isLeaf())
2975 return false;
2976
2977 // If we have a bitconvert with a resolved type and if the source and
2978 // destination types are the same, then the bitconvert is useless, remove it.
2979 //
2980 // We make an exception if the types are completely empty. This can come up
2981 // when the pattern being simplified is in the Fragments list of a PatFrags,
2982 // so that the operand is just an untyped "node". In that situation we leave
2983 // bitconverts unsimplified, and simplify them later once the fragment is
2984 // expanded into its true context.
2985 if (N->getOperator()->getName() == "bitconvert" &&
2986 N->getExtType(0).isValueTypeByHwMode(false) &&
2987 !N->getExtType(0).empty() &&
2988 N->getExtType(0) == N->getChild(0)->getExtType(0) &&
2989 N->getName().empty()) {
2990 N = N->getChildShared(0);
2991 SimplifyTree(N);
2992 return true;
2993 }
2994
2995 // Walk all children.
2996 bool MadeChange = false;
2997 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
2998 TreePatternNodePtr Child = N->getChildShared(i);
2999 MadeChange |= SimplifyTree(Child);
3000 N->setChild(i, std::move(Child));
3001 }
3002 return MadeChange;
3003}
3004
3005
3006
3007/// InferAllTypes - Infer/propagate as many types throughout the expression
3008/// patterns as possible. Return true if all types are inferred, false
3009/// otherwise. Flags an error if a type contradiction is found.
3010bool TreePattern::
3011InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) {
3012 if (NamedNodes.empty())
3013 ComputeNamedNodes();
3014
3015 bool MadeChange = true;
3016 while (MadeChange) {
3017 MadeChange = false;
3018 for (TreePatternNodePtr &Tree : Trees) {
3019 MadeChange |= Tree->ApplyTypeConstraints(*this, false);
3020 MadeChange |= SimplifyTree(Tree);
3021 }
3022
3023 // If there are constraints on our named nodes, apply them.
3024 for (auto &Entry : NamedNodes) {
3025 SmallVectorImpl<TreePatternNode*> &Nodes = Entry.second;
3026
3027 // If we have input named node types, propagate their types to the named
3028 // values here.
3029 if (InNamedTypes) {
3030 if (!InNamedTypes->count(Entry.getKey())) {
3031 error("Node '" + std::string(Entry.getKey()) +
3032 "' in output pattern but not input pattern");
3033 return true;
3034 }
3035
3036 const SmallVectorImpl<TreePatternNode*> &InNodes =
3037 InNamedTypes->find(Entry.getKey())->second;
3038
3039 // The input types should be fully resolved by now.
3040 for (TreePatternNode *Node : Nodes) {
3041 // If this node is a register class, and it is the root of the pattern
3042 // then we're mapping something onto an input register. We allow
3043 // changing the type of the input register in this case. This allows
3044 // us to match things like:
3045 // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
3046 if (Node == Trees[0].get() && Node->isLeaf()) {
3047 DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
3048 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
3049 DI->getDef()->isSubClassOf("RegisterOperand")))
3050 continue;
3051 }
3052
3053 assert(Node->getNumTypes() == 1 &&(static_cast<void> (0))
3054 InNodes[0]->getNumTypes() == 1 &&(static_cast<void> (0))
3055 "FIXME: cannot name multiple result nodes yet")(static_cast<void> (0));
3056 MadeChange |= Node->UpdateNodeType(0, InNodes[0]->getExtType(0),
3057 *this);
3058 }
3059 }
3060
3061 // If there are multiple nodes with the same name, they must all have the
3062 // same type.
3063 if (Entry.second.size() > 1) {
3064 for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) {
3065 TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1];
3066 assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&(static_cast<void> (0))
3067 "FIXME: cannot name multiple result nodes yet")(static_cast<void> (0));
3068
3069 MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this);
3070 MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this);
3071 }
3072 }
3073 }
3074 }
3075
3076 bool HasUnresolvedTypes = false;
3077 for (const TreePatternNodePtr &Tree : Trees)
3078 HasUnresolvedTypes |= Tree->ContainsUnresolvedType(*this);
3079 return !HasUnresolvedTypes;
3080}
3081
3082void TreePattern::print(raw_ostream &OS) const {
3083 OS << getRecord()->getName();
3084 if (!Args.empty()) {
3085 OS << "(";
3086 ListSeparator LS;
3087 for (const std::string &Arg : Args)
3088 OS << LS << Arg;
3089 OS << ")";
3090 }
3091 OS << ": ";
3092
3093 if (Trees.size() > 1)
3094 OS << "[\n";
3095 for (const TreePatternNodePtr &Tree : Trees) {
3096 OS << "\t";
3097 Tree->print(OS);
3098 OS << "\n";
3099 }
3100
3101 if (Trees.size() > 1)
3102 OS << "]\n";
3103}
3104
3105void TreePattern::dump() const { print(errs()); }
3106
3107//===----------------------------------------------------------------------===//
3108// CodeGenDAGPatterns implementation
3109//
3110
3111CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R,
3112 PatternRewriterFn PatternRewriter)
3113 : Records(R), Target(R), LegalVTS(Target.getLegalValueTypes()),
3114 PatternRewriter(PatternRewriter) {
3115
3116 Intrinsics = CodeGenIntrinsicTable(Records);
3117 ParseNodeInfo();
3118 ParseNodeTransforms();
3119 ParseComplexPatterns();
3120 ParsePatternFragments();
3121 ParseDefaultOperands();
3122 ParseInstructions();
3123 ParsePatternFragments(/*OutFrags*/true);
3124 ParsePatterns();
3125
3126 // Generate variants. For example, commutative patterns can match
3127 // multiple ways. Add them to PatternsToMatch as well.
3128 GenerateVariants();
3129
3130 // Break patterns with parameterized types into a series of patterns,
3131 // where each one has a fixed type and is predicated on the conditions
3132 // of the associated HW mode.
3133 ExpandHwModeBasedTypes();
3134
3135 // Infer instruction flags. For example, we can detect loads,
3136 // stores, and side effects in many cases by examining an
3137 // instruction's pattern.
3138 InferInstructionFlags();
3139
3140 // Verify that instruction flags match the patterns.
3141 VerifyInstructionFlags();
3142}
3143
3144Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
3145 Record *N = Records.getDef(Name);
3146 if (!N || !N->isSubClassOf("SDNode"))
3147 PrintFatalError("Error getting SDNode '" + Name + "'!");
3148
3149 return N;
3150}
3151
3152// Parse all of the SDNode definitions for the target, populating SDNodes.
3153void CodeGenDAGPatterns::ParseNodeInfo() {
3154 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode");
3155 const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
3156
3157 while (!Nodes.empty()) {
3158 Record *R = Nodes.back();
3159 SDNodes.insert(std::make_pair(R, SDNodeInfo(R, CGH)));
3160 Nodes.pop_back();
3161 }
3162
3163 // Get the builtin intrinsic nodes.
3164 intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void");
3165 intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain");
3166 intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain");
3167}
3168
3169/// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
3170/// map, and emit them to the file as functions.
3171void CodeGenDAGPatterns::ParseNodeTransforms() {
3172 std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm");
3173 while (!Xforms.empty()) {
3174 Record *XFormNode = Xforms.back();
3175 Record *SDNode = XFormNode->getValueAsDef("Opcode");
3176 StringRef Code = XFormNode->getValueAsString("XFormFunction");
3177 SDNodeXForms.insert(
3178 std::make_pair(XFormNode, NodeXForm(SDNode, std::string(Code))));
3179
3180 Xforms.pop_back();
3181 }
3182}
3183
3184void CodeGenDAGPatterns::ParseComplexPatterns() {
3185 std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern");
3186 while (!AMs.empty()) {
3187 ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back()));
3188 AMs.pop_back();
3189 }
3190}
3191
3192
3193/// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
3194/// file, building up the PatternFragments map. After we've collected them all,
3195/// inline fragments together as necessary, so that there are no references left
3196/// inside a pattern fragment to a pattern fragment.
3197///
3198void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
3199 std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrags");
3200
3201 // First step, parse all of the fragments.
3202 for (Record *Frag : Fragments) {
3203 if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
3204 continue;
3205
3206 ListInit *LI = Frag->getValueAsListInit("Fragments");
3207 TreePattern *P =
3208 (PatternFragments[Frag] = std::make_unique<TreePattern>(
3209 Frag, LI, !Frag->isSubClassOf("OutPatFrag"),
3210 *this)).get();
3211
3212 // Validate the argument list, converting it to set, to discard duplicates.
3213 std::vector<std::string> &Args = P->getArgList();
3214 // Copy the args so we can take StringRefs to them.
3215 auto ArgsCopy = Args;
3216 SmallDenseSet<StringRef, 4> OperandsSet;
3217 OperandsSet.insert(ArgsCopy.begin(), ArgsCopy.end());
3218
3219 if (OperandsSet.count(""))
3220 P->error("Cannot have unnamed 'node' values in pattern fragment!");
3221
3222 // Parse the operands list.
3223 DagInit *OpsList = Frag->getValueAsDag("Operands");
3224 DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator());
3225 // Special cases: ops == outs == ins. Different names are used to
3226 // improve readability.
3227 if (!OpsOp ||
3228 (OpsOp->getDef()->getName() != "ops" &&
3229 OpsOp->getDef()->getName() != "outs" &&
3230 OpsOp->getDef()->getName() != "ins"))
3231 P->error("Operands list should start with '(ops ... '!");
3232
3233 // Copy over the arguments.
3234 Args.clear();
3235 for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) {
3236 if (!isa<DefInit>(OpsList->getArg(j)) ||
3237 cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node")
3238 P->error("Operands list should all be 'node' values.");
3239 if (!OpsList->getArgName(j))
3240 P->error("Operands list should have names for each operand!");
3241 StringRef ArgNameStr = OpsList->getArgNameStr(j);
3242 if (!OperandsSet.count(ArgNameStr))
3243 P->error("'" + ArgNameStr +
3244 "' does not occur in pattern or was multiply specified!");
3245 OperandsSet.erase(ArgNameStr);
3246 Args.push_back(std::string(ArgNameStr));
3247 }
3248
3249 if (!OperandsSet.empty())
3250 P->error("Operands list does not contain an entry for operand '" +
3251 *OperandsSet.begin() + "'!");
3252
3253 // If there is a node transformation corresponding to this, keep track of
3254 // it.
3255 Record *Transform = Frag->getValueAsDef("OperandTransform");
3256 if (!getSDNodeTransform(Transform).second.empty()) // not noop xform?
3257 for (const auto &T : P->getTrees())
3258 T->setTransformFn(Transform);
3259 }
3260
3261 // Now that we've parsed all of the tree fragments, do a closure on them so
3262 // that there are not references to PatFrags left inside of them.
3263 for (Record *Frag : Fragments) {
3264 if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
3265 continue;
3266
3267 TreePattern &ThePat = *PatternFragments[Frag];
3268 ThePat.InlinePatternFragments();
3269
3270 // Infer as many types as possible. Don't worry about it if we don't infer
3271 // all of them, some may depend on the inputs of the pattern. Also, don't
3272 // validate type sets; validation may cause spurious failures e.g. if a
3273 // fragment needs floating-point types but the current target does not have
3274 // any (this is only an error if that fragment is ever used!).
3275 {
3276 TypeInfer::SuppressValidation SV(ThePat.getInfer());
3277 ThePat.InferAllTypes();
3278 ThePat.resetError();
3279 }
3280
3281 // If debugging, print out the pattern fragment result.
3282 LLVM_DEBUG(ThePat.dump())do { } while (false);
3283 }
3284}
3285
3286void CodeGenDAGPatterns::ParseDefaultOperands() {
3287 std::vector<Record*> DefaultOps;
3288 DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps");
3289
3290 // Find some SDNode.
3291 assert(!SDNodes.empty() && "No SDNodes parsed?")(static_cast<void> (0));
3292 Init *SomeSDNode = DefInit::get(SDNodes.begin()->first);
3293
3294 for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) {
3295 DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps");
3296
3297 // Clone the DefaultInfo dag node, changing the operator from 'ops' to
3298 // SomeSDnode so that we can parse this.
3299 std::vector<std::pair<Init*, StringInit*> > Ops;
3300 for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op)
3301 Ops.push_back(std::make_pair(DefaultInfo->getArg(op),
3302 DefaultInfo->getArgName(op)));
3303 DagInit *DI = DagInit::get(SomeSDNode, nullptr, Ops);
3304
3305 // Create a TreePattern to parse this.
3306 TreePattern P(DefaultOps[i], DI, false, *this);
3307 assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!")(static_cast<void> (0));
3308
3309 // Copy the operands over into a DAGDefaultOperand.
3310 DAGDefaultOperand DefaultOpInfo;
3311
3312 const TreePatternNodePtr &T = P.getTree(0);
3313 for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) {
3314 TreePatternNodePtr TPN = T->getChildShared(op);
3315 while (TPN->ApplyTypeConstraints(P, false))
3316 /* Resolve all types */;
3317
3318 if (TPN->ContainsUnresolvedType(P)) {
3319 PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" +
3320 DefaultOps[i]->getName() +
3321 "' doesn't have a concrete type!");
3322 }
3323 DefaultOpInfo.DefaultOps.push_back(std::move(TPN));
3324 }
3325
3326 // Insert it into the DefaultOperands map so we can find it later.
3327 DefaultOperands[DefaultOps[i]] = DefaultOpInfo;
3328 }
3329}
3330
3331/// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
3332/// instruction input. Return true if this is a real use.
3333static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
3334 std::map<std::string, TreePatternNodePtr> &InstInputs) {
3335 // No name -> not interesting.
3336 if (Pat->getName().empty()) {
3337 if (Pat->isLeaf()) {
3338 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
3339 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
3340 DI->getDef()->isSubClassOf("RegisterOperand")))
3341 I.error("Input " + DI->getDef()->getName() + " must be named!");
3342 }
3343 return false;
3344 }
3345
3346 Record *Rec;
3347 if (Pat->isLeaf()) {
3348 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
3349 if (!DI)
3350 I.error("Input $" + Pat->getName() + " must be an identifier!");
3351 Rec = DI->getDef();
3352 } else {
3353 Rec = Pat->getOperator();
3354 }
3355
3356 // SRCVALUE nodes are ignored.
3357 if (Rec->getName() == "srcvalue")
3358 return false;
3359
3360 TreePatternNodePtr &Slot = InstInputs[Pat->getName()];
3361 if (!Slot) {
3362 Slot = Pat;
3363 return true;
3364 }
3365 Record *SlotRec;
3366 if (Slot->isLeaf()) {
3367 SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef();
3368 } else {
3369 assert(Slot->getNumChildren() == 0 && "can't be a use with children!")(static_cast<void> (0));
3370 SlotRec = Slot->getOperator();
3371 }
3372
3373 // Ensure that the inputs agree if we've already seen this input.
3374 if (Rec != SlotRec)
3375 I.error("All $" + Pat->getName() + " inputs must agree with each other");
3376 // Ensure that the types can agree as well.
3377 Slot->UpdateNodeType(0, Pat->getExtType(0), I);
3378 Pat->UpdateNodeType(0, Slot->getExtType(0), I);
3379 if (Slot->getExtTypes() != Pat->getExtTypes())
3380 I.error("All $" + Pat->getName() + " inputs must agree with each other");
3381 return true;
3382}
3383
3384/// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is
3385/// part of "I", the instruction), computing the set of inputs and outputs of
3386/// the pattern. Report errors if we see anything naughty.
3387void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
3388 TreePattern &I, TreePatternNodePtr Pat,
3389 std::map<std::string, TreePatternNodePtr> &InstInputs,
3390 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
3391 &InstResults,
3392 std::vector<Record *> &InstImpResults) {
3393
3394 // The instruction pattern still has unresolved fragments. For *named*
3395 // nodes we must resolve those here. This may not result in multiple
3396 // alternatives.
3397 if (!Pat->getName().empty()) {
3398 TreePattern SrcPattern(I.getRecord(), Pat, true, *this);
3399 SrcPattern.InlinePatternFragments();
3400 SrcPattern.InferAllTypes();
3401 Pat = SrcPattern.getOnlyTree();
3402 }
3403
3404 if (Pat->isLeaf()) {
3405 bool isUse = HandleUse(I, Pat, InstInputs);
3406 if (!isUse && Pat->getTransformFn())
3407 I.error("Cannot specify a transform function for a non-input value!");
3408 return;
3409 }
3410
3411 if (Pat->getOperator()->getName() == "implicit") {
3412 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3413 TreePatternNode *Dest = Pat->getChild(i);
3414 if (!Dest->isLeaf())
3415 I.error("implicitly defined value should be a register!");
3416
3417 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
3418 if (!Val || !Val->getDef()->isSubClassOf("Register"))
3419 I.error("implicitly defined value should be a register!");
3420 InstImpResults.push_back(Val->getDef());
3421 }
3422 return;
3423 }
3424
3425 if (Pat->getOperator()->getName() != "set") {
3426 // If this is not a set, verify that the children nodes are not void typed,
3427 // and recurse.
3428 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3429 if (Pat->getChild(i)->getNumTypes() == 0)
3430 I.error("Cannot have void nodes inside of patterns!");
3431 FindPatternInputsAndOutputs(I, Pat->getChildShared(i), InstInputs,
3432 InstResults, InstImpResults);
3433 }
3434
3435 // If this is a non-leaf node with no children, treat it basically as if
3436 // it were a leaf. This handles nodes like (imm).
3437 bool isUse = HandleUse(I, Pat, InstInputs);
3438
3439 if (!isUse && Pat->getTransformFn())
3440 I.error("Cannot specify a transform function for a non-input value!");
3441 return;
3442 }
3443
3444 // Otherwise, this is a set, validate and collect instruction results.
3445 if (Pat->getNumChildren() == 0)
3446 I.error("set requires operands!");
3447
3448 if (Pat->getTransformFn())
3449 I.error("Cannot specify a transform function on a set node!");
3450
3451 // Check the set destinations.
3452 unsigned NumDests = Pat->getNumChildren()-1;
3453 for (unsigned i = 0; i != NumDests; ++i) {
3454 TreePatternNodePtr Dest = Pat->getChildShared(i);
3455 // For set destinations we also must resolve fragments here.
3456 TreePattern DestPattern(I.getRecord(), Dest, false, *this);
3457 DestPattern.InlinePatternFragments();
3458 DestPattern.InferAllTypes();
3459 Dest = DestPattern.getOnlyTree();
3460
3461 if (!Dest->isLeaf())
3462 I.error("set destination should be a register!");
3463
3464 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
3465 if (!Val) {
3466 I.error("set destination should be a register!");
3467 continue;
3468 }
3469
3470 if (Val->getDef()->isSubClassOf("RegisterClass") ||
3471 Val->getDef()->isSubClassOf("ValueType") ||
3472 Val->getDef()->isSubClassOf("RegisterOperand") ||
3473 Val->getDef()->isSubClassOf("PointerLikeRegClass")) {
3474 if (Dest->getName().empty())
3475 I.error("set destination must have a name!");
3476 if (InstResults.count(Dest->getName()))
3477 I.error("cannot set '" + Dest->getName() + "' multiple times");
3478 InstResults[Dest->getName()] = Dest;
3479 } else if (Val->getDef()->isSubClassOf("Register")) {
3480 InstImpResults.push_back(Val->getDef());
3481 } else {
3482 I.error("set destination should be a register!");
3483 }
3484 }
3485
3486 // Verify and collect info from the computation.
3487 FindPatternInputsAndOutputs(I, Pat->getChildShared(NumDests), InstInputs,
3488 InstResults, InstImpResults);
3489}
3490
3491//===----------------------------------------------------------------------===//
3492// Instruction Analysis
3493//===----------------------------------------------------------------------===//
3494
3495class InstAnalyzer {
3496 const CodeGenDAGPatterns &CDP;
3497public:
3498 bool hasSideEffects;
3499 bool mayStore;
3500 bool mayLoad;
3501 bool isBitcast;
3502 bool isVariadic;
3503 bool hasChain;
3504
3505 InstAnalyzer(const CodeGenDAGPatterns &cdp)
3506 : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false),
3507 isBitcast(false), isVariadic(false), hasChain(false) {}
3508
3509 void Analyze(const PatternToMatch &Pat) {
3510 const TreePatternNode *N = Pat.getSrcPattern();
3511 AnalyzeNode(N);
3512 // These properties are detected only on the root node.
3513 isBitcast = IsNodeBitcast(N);
3514 }
3515
3516private:
3517 bool IsNodeBitcast(const TreePatternNode *N) const {
3518 if (hasSideEffects || mayLoad || mayStore || isVariadic)
3519 return false;
3520
3521 if (N->isLeaf())
3522 return false;
3523 if (N->getNumChildren() != 1 || !N->getChild(0)->isLeaf())
3524 return false;
3525
3526 if (N->getOperator()->isSubClassOf("ComplexPattern"))
3527 return false;
3528
3529 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator());
3530 if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1)
3531 return false;
3532 return OpInfo.getEnumName() == "ISD::BITCAST";
3533 }
3534
3535public:
3536 void AnalyzeNode(const TreePatternNode *N) {
3537 if (N->isLeaf()) {
3538 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
3539 Record *LeafRec = DI->getDef();
3540 // Handle ComplexPattern leaves.
3541 if (LeafRec->isSubClassOf("ComplexPattern")) {
3542 const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
3543 if (CP.hasProperty(SDNPMayStore)) mayStore = true;
3544 if (CP.hasProperty(SDNPMayLoad)) mayLoad = true;
3545 if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true;
3546 }
3547 }
3548 return;
3549 }
3550
3551 // Analyze children.
3552 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
3553 AnalyzeNode(N->getChild(i));
3554
3555 // Notice properties of the node.
3556 if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true;
3557 if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true;
3558 if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true;
3559 if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true;
3560 if (N->NodeHasProperty(SDNPHasChain, CDP)) hasChain = true;
3561
3562 if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
3563 // If this is an intrinsic, analyze it.
3564 if (IntInfo->ModRef & CodeGenIntrinsic::MR_Ref)
3565 mayLoad = true;// These may load memory.
3566
3567 if (IntInfo->ModRef & CodeGenIntrinsic::MR_Mod)
3568 mayStore = true;// Intrinsics that can write to memory are 'mayStore'.
3569
3570 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem ||
3571 IntInfo->hasSideEffects)
3572 // ReadWriteMem intrinsics can have other strange effects.
3573 hasSideEffects = true;
3574 }
3575 }
3576
3577};
3578
3579static bool InferFromPattern(CodeGenInstruction &InstInfo,
3580 const InstAnalyzer &PatInfo,
3581 Record *PatDef) {
3582 bool Error = false;
3583
3584 // Remember where InstInfo got its flags.
3585 if (InstInfo.hasUndefFlags())
3586 InstInfo.InferredFrom = PatDef;
3587
3588 // Check explicitly set flags for consistency.
3589 if (InstInfo.hasSideEffects != PatInfo.hasSideEffects &&
3590 !InstInfo.hasSideEffects_Unset) {
3591 // Allow explicitly setting hasSideEffects = 1 on instructions, even when
3592 // the pattern has no side effects. That could be useful for div/rem
3593 // instructions that may trap.
3594 if (!InstInfo.hasSideEffects) {
3595 Error = true;
3596 PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " +
3597 Twine(InstInfo.hasSideEffects));
3598 }
3599 }
3600
3601 if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) {
3602 Error = true;
3603 PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " +
3604 Twine(InstInfo.mayStore));
3605 }
3606
3607 if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) {
3608 // Allow explicitly setting mayLoad = 1, even when the pattern has no loads.
3609 // Some targets translate immediates to loads.
3610 if (!InstInfo.mayLoad) {
3611 Error = true;
3612 PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " +
3613 Twine(InstInfo.mayLoad));
3614 }
3615 }
3616
3617 // Transfer inferred flags.
3618 InstInfo.hasSideEffects |= PatInfo.hasSideEffects;
3619 InstInfo.mayStore |= PatInfo.mayStore;
3620 InstInfo.mayLoad |= PatInfo.mayLoad;
3621
3622 // These flags are silently added without any verification.
3623 // FIXME: To match historical behavior of TableGen, for now add those flags
3624 // only when we're inferring from the primary instruction pattern.
3625 if (PatDef->isSubClassOf("Instruction")) {
3626 InstInfo.isBitcast |= PatInfo.isBitcast;
3627 InstInfo.hasChain |= PatInfo.hasChain;
3628 InstInfo.hasChain_Inferred = true;
3629 }
3630
3631 // Don't infer isVariadic. This flag means something different on SDNodes and
3632 // instructions. For example, a CALL SDNode is variadic because it has the
3633 // call arguments as operands, but a CALL instruction is not variadic - it
3634 // has argument registers as implicit, not explicit uses.
3635
3636 return Error;
3637}
3638
3639/// hasNullFragReference - Return true if the DAG has any reference to the
3640/// null_frag operator.
3641static bool hasNullFragReference(DagInit *DI) {
3642 DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator());
3643 if (!OpDef) return false;
3644 Record *Operator = OpDef->getDef();
3645
3646 // If this is the null fragment, return true.
3647 if (Operator->getName() == "null_frag") return true;
3648 // If any of the arguments reference the null fragment, return true.
3649 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
3650 if (auto Arg = dyn_cast<DefInit>(DI->getArg(i)))
3651 if (Arg->getDef()->getName() == "null_frag")
3652 return true;
3653 DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i));
3654 if (Arg && hasNullFragReference(Arg))
3655 return true;
3656 }
3657
3658 return false;
3659}
3660
3661/// hasNullFragReference - Return true if any DAG in the list references
3662/// the null_frag operator.
3663static bool hasNullFragReference(ListInit *LI) {
3664 for (Init *I : LI->getValues()) {
3665 DagInit *DI = dyn_cast<DagInit>(I);
3666 assert(DI && "non-dag in an instruction Pattern list?!")(static_cast<void> (0));
3667 if (hasNullFragReference(DI))
3668 return true;
3669 }
3670 return false;
3671}
3672
3673/// Get all the instructions in a tree.
3674static void
3675getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) {
3676 if (Tree->isLeaf())
3677 return;
3678 if (Tree->getOperator()->isSubClassOf("Instruction"))
3679 Instrs.push_back(Tree->getOperator());
3680 for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i)
3681 getInstructionsInTree(Tree->getChild(i), Instrs);
3682}
3683
3684/// Check the class of a pattern leaf node against the instruction operand it
3685/// represents.
3686static bool checkOperandClass(CGIOperandList::OperandInfo &OI,
3687 Record *Leaf) {
3688 if (OI.Rec == Leaf)
3689 return true;
3690
3691 // Allow direct value types to be used in instruction set patterns.
3692 // The type will be checked later.
3693 if (Leaf->isSubClassOf("ValueType"))
3694 return true;
3695
3696 // Patterns can also be ComplexPattern instances.
3697 if (Leaf->isSubClassOf("ComplexPattern"))
3698 return true;
3699
3700 return false;
3701}
3702
3703void CodeGenDAGPatterns::parseInstructionPattern(
3704 CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) {
3705
3706 assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!")(static_cast<void> (0));
3707
3708 // Parse the instruction.
3709 TreePattern I(CGI.TheDef, Pat, true, *this);
3710
3711 // InstInputs - Keep track of all of the inputs of the instruction, along
3712 // with the record they are declared as.
3713 std::map<std::string, TreePatternNodePtr> InstInputs;
3714
3715 // InstResults - Keep track of all the virtual registers that are 'set'
3716 // in the instruction, including what reg class they are.
3717 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
3718 InstResults;
3719
3720 std::vector<Record*> InstImpResults;
3721
3722 // Verify that the top-level forms in the instruction are of void type, and
3723 // fill in the InstResults map.
3724 SmallString<32> TypesString;
3725 for (unsigned j = 0, e = I.getNumTrees(); j != e; ++j) {
1
Assuming 'j' is equal to 'e'
2
Loop condition is false. Execution continues on line 3748
3726 TypesString.clear();
3727 TreePatternNodePtr Pat = I.getTree(j);
3728 if (Pat->getNumTypes() != 0) {
3729 raw_svector_ostream OS(TypesString);
3730 ListSeparator LS;
3731 for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) {
3732 OS << LS;
3733 Pat->getExtType(k).writeToStream(OS);
3734 }
3735 I.error("Top-level forms in instruction pattern should have"
3736 " void types, has types " +
3737 OS.str());
3738 }
3739
3740 // Find inputs and outputs, and verify the structure of the uses/defs.
3741 FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults,
3742 InstImpResults);
3743 }
3744
3745 // Now that we have inputs and outputs of the pattern, inspect the operands
3746 // list for the instruction. This determines the order that operands are
3747 // added to the machine instruction the node corresponds to.
3748 unsigned NumResults = InstResults.size();
3749
3750 // Parse the operands list from the (ops) list, validating it.
3751 assert(I.getArgList().empty() && "Args list should still be empty here!")(static_cast<void> (0));
3752
3753 // Check that all of the results occur first in the list.
3754 std::vector<Record*> Results;
3755 std::vector<unsigned> ResultIndices;
3756 SmallVector<TreePatternNodePtr, 2> ResNodes;
3757 for (unsigned i = 0; i != NumResults; ++i) {
3
Assuming 'i' is equal to 'NumResults'
4
Loop condition is false. Execution continues on line 3798
3758 if (i == CGI.Operands.size()) {
3759 const std::string &OpName =
3760 llvm::find_if(
3761 InstResults,
3762 [](const std::pair<std::string, TreePatternNodePtr> &P) {
3763 return P.second;
3764 })
3765 ->first;
3766
3767 I.error("'" + OpName + "' set but does not appear in operand list!");
3768 }
3769
3770 const std::string &OpName = CGI.Operands[i].Name;
3771
3772 // Check that it exists in InstResults.
3773 auto InstResultIter = InstResults.find(OpName);
3774 if (InstResultIter == InstResults.end() || !InstResultIter->second)
3775 I.error("Operand $" + OpName + " does not exist in operand list!");
3776
3777 TreePatternNodePtr RNode = InstResultIter->second;
3778 Record *R = cast<DefInit>(RNode->getLeafValue())->getDef();
3779 ResNodes.push_back(std::move(RNode));
3780 if (!R)
3781 I.error("Operand $" + OpName + " should be a set destination: all "
3782 "outputs must occur before inputs in operand list!");
3783
3784 if (!checkOperandClass(CGI.Operands[i], R))
3785 I.error("Operand $" + OpName + " class mismatch!");
3786
3787 // Remember the return type.
3788 Results.push_back(CGI.Operands[i].Rec);
3789
3790 // Remember the result index.
3791 ResultIndices.push_back(std::distance(InstResults.begin(), InstResultIter));
3792
3793 // Okay, this one checks out.
3794 InstResultIter->second = nullptr;
3795 }
3796
3797 // Loop over the inputs next.
3798 std::vector<TreePatternNodePtr> ResultNodeOperands;
3799 std::vector<Record*> Operands;
3800 for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
5
Assuming 'i' is equal to 'e'
6
Loop condition is false. Execution continues on line 3847
3801 CGIOperandList::OperandInfo &Op = CGI.Operands[i];
3802 const std::string &OpName = Op.Name;
3803 if (OpName.empty())
3804 I.error("Operand #" + Twine(i) + " in operands list has no name!");
3805
3806 if (!InstInputs.count(OpName)) {
3807 // If this is an operand with a DefaultOps set filled in, we can ignore
3808 // this. When we codegen it, we will do so as always executed.
3809 if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) {
3810 // Does it have a non-empty DefaultOps field? If so, ignore this
3811 // operand.
3812 if (!getDefaultOperand(Op.Rec).DefaultOps.empty())
3813 continue;
3814 }
3815 I.error("Operand $" + OpName +
3816 " does not appear in the instruction pattern");
3817 }
3818 TreePatternNodePtr InVal = InstInputs[OpName];
3819 InstInputs.erase(OpName); // It occurred, remove from map.
3820
3821 if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) {
3822 Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef();
3823 if (!checkOperandClass(Op, InRec))
3824 I.error("Operand $" + OpName + "'s register class disagrees"
3825 " between the operand and pattern");
3826 }
3827 Operands.push_back(Op.Rec);
3828
3829 // Construct the result for the dest-pattern operand list.
3830 TreePatternNodePtr OpNode = InVal->clone();
3831
3832 // No predicate is useful on the result.
3833 OpNode->clearPredicateCalls();
3834
3835 // Promote the xform function to be an explicit node if set.
3836 if (Record *Xform = OpNode->getTransformFn()) {
3837 OpNode->setTransformFn(nullptr);
3838 std::vector<TreePatternNodePtr> Children;
3839 Children.push_back(OpNode);
3840 OpNode = std::make_shared<TreePatternNode>(Xform, std::move(Children),
3841 OpNode->getNumTypes());
3842 }
3843
3844 ResultNodeOperands.push_back(std::move(OpNode));
3845 }
3846
3847 if (!InstInputs.empty())
7
Assuming the condition is false
8
Taking false branch
3848 I.error("Input operand $" + InstInputs.begin()->first +
3849 " occurs in pattern but not in operands list!");
3850
3851 TreePatternNodePtr ResultPattern = std::make_shared<TreePatternNode>(
3852 I.getRecord(), std::move(ResultNodeOperands),
3853 GetNumNodeResults(I.getRecord(), *this));
9
Calling 'GetNumNodeResults'
3854 // Copy fully inferred output node types to instruction result pattern.
3855 for (unsigned i = 0; i != NumResults; ++i) {
3856 assert(ResNodes[i]->getNumTypes() == 1 && "FIXME: Unhandled")(static_cast<void> (0));
3857 ResultPattern->setType(i, ResNodes[i]->getExtType(0));
3858 ResultPattern->setResultIndex(i, ResultIndices[i]);
3859 }
3860
3861 // FIXME: Assume only the first tree is the pattern. The others are clobber
3862 // nodes.
3863 TreePatternNodePtr Pattern = I.getTree(0);
3864 TreePatternNodePtr SrcPattern;
3865 if (Pattern->getOperator()->getName() == "set") {
3866 SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone();
3867 } else{
3868 // Not a set (store or something?)
3869 SrcPattern = Pattern;
3870 }
3871
3872 // Create and insert the instruction.
3873 // FIXME: InstImpResults should not be part of DAGInstruction.
3874 Record *R = I.getRecord();
3875 DAGInsts.emplace(std::piecewise_construct, std::forward_as_tuple(R),
3876 std::forward_as_tuple(Results, Operands, InstImpResults,
3877 SrcPattern, ResultPattern));
3878
3879 LLVM_DEBUG(I.dump())do { } while (false);
3880}
3881
3882/// ParseInstructions - Parse all of the instructions, inlining and resolving
3883/// any fragments involved. This populates the Instructions list with fully
3884/// resolved instructions.
3885void CodeGenDAGPatterns::ParseInstructions() {
3886 std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction");
3887
3888 for (Record *Instr : Instrs) {
3889 ListInit *LI = nullptr;
3890
3891 if (isa<ListInit>(Instr->getValueInit("Pattern")))
3892 LI = Instr->getValueAsListInit("Pattern");
3893
3894 // If there is no pattern, only collect minimal information about the
3895 // instruction for its operand list. We have to assume that there is one
3896 // result, as we have no detailed info. A pattern which references the
3897 // null_frag operator is as-if no pattern were specified. Normally this
3898 // is from a multiclass expansion w/ a SDPatternOperator passed in as
3899 // null_frag.
3900 if (!LI || LI->empty() || hasNullFragReference(LI)) {
3901 std::vector<Record*> Results;
3902 std::vector<Record*> Operands;
3903
3904 CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
3905
3906 if (InstInfo.Operands.size() != 0) {
3907 for (unsigned j = 0, e = InstInfo.Operands.NumDefs; j < e; ++j)
3908 Results.push_back(InstInfo.Operands[j].Rec);
3909
3910 // The rest are inputs.
3911 for (unsigned j = InstInfo.Operands.NumDefs,
3912 e = InstInfo.Operands.size(); j < e; ++j)
3913 Operands.push_back(InstInfo.Operands[j].Rec);
3914 }
3915
3916 // Create and insert the instruction.
3917 std::vector<Record*> ImpResults;
3918 Instructions.insert(std::make_pair(Instr,
3919 DAGInstruction(Results, Operands, ImpResults)));
3920 continue; // no pattern.
3921 }
3922
3923 CodeGenInstruction &CGI = Target.getInstruction(Instr);
3924 parseInstructionPattern(CGI, LI, Instructions);
3925 }
3926
3927 // If we can, convert the instructions to be patterns that are matched!
3928 for (auto &Entry : Instructions) {
3929 Record *Instr = Entry.first;
3930 DAGInstruction &TheInst = Entry.second;
3931 TreePatternNodePtr SrcPattern = TheInst.getSrcPattern();
3932 TreePatternNodePtr ResultPattern = TheInst.getResultPattern();
3933
3934 if (SrcPattern && ResultPattern) {
3935 TreePattern Pattern(Instr, SrcPattern, true, *this);
3936 TreePattern Result(Instr, ResultPattern, false, *this);
3937 ParseOnePattern(Instr, Pattern, Result, TheInst.getImpResults());
3938 }
3939 }
3940}
3941
3942typedef std::pair<TreePatternNode *, unsigned> NameRecord;
3943
3944static void FindNames(TreePatternNode *P,
3945 std::map<std::string, NameRecord> &Names,
3946 TreePattern *PatternTop) {
3947 if (!P->getName().empty()) {
3948 NameRecord &Rec = Names[P->getName()];
3949 // If this is the first instance of the name, remember the node.
3950 if (Rec.second++ == 0)
3951 Rec.first = P;
3952 else if (Rec.first->getExtTypes() != P->getExtTypes())
3953 PatternTop->error("repetition of value: $" + P->getName() +
3954 " where different uses have different types!");
3955 }
3956
3957 if (!P->isLeaf()) {
3958 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
3959 FindNames(P->getChild(i), Names, PatternTop);
3960 }
3961}
3962
3963void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern,
3964 PatternToMatch &&PTM) {
3965 // Do some sanity checking on the pattern we're about to match.
3966 std::string Reason;
3967 if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) {
3968 PrintWarning(Pattern->getRecord()->getLoc(),
3969 Twine("Pattern can never match: ") + Reason);
3970 return;
3971 }
3972
3973 // If the source pattern's root is a complex pattern, that complex pattern
3974 // must specify the nodes it can potentially match.
3975 if (const ComplexPattern *CP =
3976 PTM.getSrcPattern()->getComplexPatternInfo(*this))
3977 if (CP->getRootNodes().empty())
3978 Pattern->error("ComplexPattern at root must specify list of opcodes it"
3979 " could match");
3980
3981
3982 // Find all of the named values in the input and output, ensure they have the
3983 // same type.
3984 std::map<std::string, NameRecord> SrcNames, DstNames;
3985 FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
3986 FindNames(PTM.getDstPattern(), DstNames, Pattern);
3987
3988 // Scan all of the named values in the destination pattern, rejecting them if
3989 // they don't exist in the input pattern.
3990 for (const auto &Entry : DstNames) {
3991 if (SrcNames[Entry.first].first == nullptr)
3992 Pattern->error("Pattern has input without matching name in output: $" +
3993 Entry.first);
3994 }
3995
3996 // Scan all of the named values in the source pattern, rejecting them if the
3997 // name isn't used in the dest, and isn't used to tie two values together.
3998 for (const auto &Entry : SrcNames)
3999 if (DstNames[Entry.first].first == nullptr &&
4000 SrcNames[Entry.first].second == 1)
4001 Pattern->error("Pattern has dead named input: $" + Entry.first);
4002
4003 PatternsToMatch.push_back(std::move(PTM));
4004}
4005
4006void CodeGenDAGPatterns::InferInstructionFlags() {
4007 ArrayRef<const CodeGenInstruction*> Instructions =
4008 Target.getInstructionsByEnumValue();
4009
4010 unsigned Errors = 0;
4011
4012 // Try to infer flags from all patterns in PatternToMatch. These include
4013 // both the primary instruction patterns (which always come first) and
4014 // patterns defined outside the instruction.
4015 for (const PatternToMatch &PTM : ptms()) {
4016 // We can only infer from single-instruction patterns, otherwise we won't
4017 // know which instruction should get the flags.
4018 SmallVector<Record*, 8> PatInstrs;
4019 getInstructionsInTree(PTM.getDstPattern(), PatInstrs);
4020 if (PatInstrs.size() != 1)
4021 continue;
4022
4023 // Get the single instruction.
4024 CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front());
4025
4026 // Only infer properties from the first pattern. We'll verify the others.
4027 if (InstInfo.InferredFrom)
4028 continue;
4029
4030 InstAnalyzer PatInfo(*this);
4031 PatInfo.Analyze(PTM);
4032 Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord());
4033 }
4034
4035 if (Errors)
4036 PrintFatalError("pattern conflicts");
4037
4038 // If requested by the target, guess any undefined properties.
4039 if (Target.guessInstructionProperties()) {
4040 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
4041 CodeGenInstruction *InstInfo =
4042 const_cast<CodeGenInstruction *>(Instructions[i]);
4043 if (InstInfo->InferredFrom)
4044 continue;
4045 // The mayLoad and mayStore flags default to false.
4046 // Conservatively assume hasSideEffects if it wasn't explicit.
4047 if (InstInfo->hasSideEffects_Unset)
4048 InstInfo->hasSideEffects = true;
4049 }
4050 return;
4051 }
4052
4053 // Complain about any flags that are still undefined.
4054 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
4055 CodeGenInstruction *InstInfo =
4056 const_cast<CodeGenInstruction *>(Instructions[i]);
4057 if (InstInfo->InferredFrom)
4058 continue;
4059 if (InstInfo->hasSideEffects_Unset)
4060 PrintError(InstInfo->TheDef->getLoc(),
4061 "Can't infer hasSideEffects from patterns");
4062 if (InstInfo->mayStore_Unset)
4063 PrintError(InstInfo->TheDef->getLoc(),
4064 "Can't infer mayStore from patterns");
4065 if (InstInfo->mayLoad_Unset)
4066 PrintError(InstInfo->TheDef->getLoc(),
4067 "Can't infer mayLoad from patterns");
4068 }
4069}
4070
4071
4072/// Verify instruction flags against pattern node properties.
4073void CodeGenDAGPatterns::VerifyInstructionFlags() {
4074 unsigned Errors = 0;
4075 for (const PatternToMatch &PTM : ptms()) {
4076 SmallVector<Record*, 8> Instrs;
4077 getInstructionsInTree(PTM.getDstPattern(), Instrs);
4078 if (Instrs.empty())
4079 continue;
4080
4081 // Count the number of instructions with each flag set.
4082 unsigned NumSideEffects = 0;
4083 unsigned NumStores = 0;
4084 unsigned NumLoads = 0;
4085 for (const Record *Instr : Instrs) {
4086 const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
4087 NumSideEffects += InstInfo.hasSideEffects;
4088 NumStores += InstInfo.mayStore;
4089 NumLoads += InstInfo.mayLoad;
4090 }
4091
4092 // Analyze the source pattern.
4093 InstAnalyzer PatInfo(*this);
4094 PatInfo.Analyze(PTM);
4095
4096 // Collect error messages.
4097 SmallVector<std::string, 4> Msgs;
4098
4099 // Check for missing flags in the output.
4100 // Permit extra flags for now at least.
4101 if (PatInfo.hasSideEffects && !NumSideEffects)
4102 Msgs.push_back("pattern has side effects, but hasSideEffects isn't set");
4103
4104 // Don't verify store flags on instructions with side effects. At least for
4105 // intrinsics, side effects implies mayStore.
4106 if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores)
4107 Msgs.push_back("pattern may store, but mayStore isn't set");
4108
4109 // Similarly, mayStore implies mayLoad on intrinsics.
4110 if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads)
4111 Msgs.push_back("pattern may load, but mayLoad isn't set");
4112
4113 // Print error messages.
4114 if (Msgs.empty())
4115 continue;
4116 ++Errors;
4117
4118 for (const std::string &Msg : Msgs)
4119 PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msg) + " on the " +
4120 (Instrs.size() == 1 ?
4121 "instruction" : "output instructions"));
4122 // Provide the location of the relevant instruction definitions.
4123 for (const Record *Instr : Instrs) {
4124 if (Instr != PTM.getSrcRecord())
4125 PrintError(Instr->getLoc(), "defined here");
4126 const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
4127 if (InstInfo.InferredFrom &&
4128 InstInfo.InferredFrom != InstInfo.TheDef &&
4129 InstInfo.InferredFrom != PTM.getSrcRecord())
4130 PrintError(InstInfo.InferredFrom->getLoc(), "inferred from pattern");
4131 }
4132 }
4133 if (Errors)
4134 PrintFatalError("Errors in DAG patterns");
4135}
4136
4137/// Given a pattern result with an unresolved type, see if we can find one
4138/// instruction with an unresolved result type. Force this result type to an
4139/// arbitrary element if it's possible types to converge results.
4140static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) {
4141 if (N->isLeaf())
4142 return false;
4143
4144 // Analyze children.
4145 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4146 if (ForceArbitraryInstResultType(N->getChild(i), TP))
4147 return true;
4148
4149 if (!N->getOperator()->isSubClassOf("Instruction"))
4150 return false;
4151
4152 // If this type is already concrete or completely unknown we can't do
4153 // anything.
4154 TypeInfer &TI = TP.getInfer();
4155 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) {
4156 if (N->getExtType(i).empty() || TI.isConcrete(N->getExtType(i), false))
4157 continue;
4158
4159 // Otherwise, force its type to an arbitrary choice.
4160 if (TI.forceArbitrary(N->getExtType(i)))
4161 return true;
4162 }
4163
4164 return false;
4165}
4166
4167// Promote xform function to be an explicit node wherever set.
4168static TreePatternNodePtr PromoteXForms(TreePatternNodePtr N) {
4169 if (Record *Xform = N->getTransformFn()) {
4170 N->setTransformFn(nullptr);
4171 std::vector<TreePatternNodePtr> Children;
4172 Children.push_back(PromoteXForms(N));
4173 return std::make_shared<TreePatternNode>(Xform, std::move(Children),
4174 N->getNumTypes());
4175 }
4176
4177 if (!N->isLeaf())
4178 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
4179 TreePatternNodePtr Child = N->getChildShared(i);
4180 N->setChild(i, PromoteXForms(Child));
4181 }
4182 return N;
4183}
4184
4185void CodeGenDAGPatterns::ParseOnePattern(Record *TheDef,
4186 TreePattern &Pattern, TreePattern &Result,
4187 const std::vector<Record *> &InstImpResults) {
4188
4189 // Inline pattern fragments and expand multiple alternatives.
4190 Pattern.InlinePatternFragments();
4191 Result.InlinePatternFragments();
4192
4193 if (Result.getNumTrees() != 1)
4194 Result.error("Cannot use multi-alternative fragments in result pattern!");
4195
4196 // Infer types.
4197 bool IterateInference;
4198 bool InferredAllPatternTypes, InferredAllResultTypes;
4199 do {
4200 // Infer as many types as possible. If we cannot infer all of them, we
4201 // can never do anything with this pattern: report it to the user.
4202 InferredAllPatternTypes =
4203 Pattern.InferAllTypes(&Pattern.getNamedNodesMap());
4204
4205 // Infer as many types as possible. If we cannot infer all of them, we
4206 // can never do anything with this pattern: report it to the user.
4207 InferredAllResultTypes =
4208 Result.InferAllTypes(&Pattern.getNamedNodesMap());
4209
4210 IterateInference = false;
4211
4212 // Apply the type of the result to the source pattern. This helps us
4213 // resolve cases where the input type is known to be a pointer type (which
4214 // is considered resolved), but the result knows it needs to be 32- or
4215 // 64-bits. Infer the other way for good measure.
4216 for (const auto &T : Pattern.getTrees())
4217 for (unsigned i = 0, e = std::min(Result.getOnlyTree()->getNumTypes(),
4218 T->getNumTypes());
4219 i != e; ++i) {
4220 IterateInference |= T->UpdateNodeType(
4221 i, Result.getOnlyTree()->getExtType(i), Result);
4222 IterateInference |= Result.getOnlyTree()->UpdateNodeType(
4223 i, T->getExtType(i), Result);
4224 }
4225
4226 // If our iteration has converged and the input pattern's types are fully
4227 // resolved but the result pattern is not fully resolved, we may have a
4228 // situation where we have two instructions in the result pattern and
4229 // the instructions require a common register class, but don't care about
4230 // what actual MVT is used. This is actually a bug in our modelling:
4231 // output patterns should have register classes, not MVTs.
4232 //
4233 // In any case, to handle this, we just go through and disambiguate some
4234 // arbitrary types to the result pattern's nodes.
4235 if (!IterateInference && InferredAllPatternTypes &&
4236 !InferredAllResultTypes)
4237 IterateInference =
4238 ForceArbitraryInstResultType(Result.getTree(0).get(), Result);
4239 } while (IterateInference);
4240
4241 // Verify that we inferred enough types that we can do something with the
4242 // pattern and result. If these fire the user has to add type casts.
4243 if (!InferredAllPatternTypes)
4244 Pattern.error("Could not infer all types in pattern!");
4245 if (!InferredAllResultTypes) {
4246 Pattern.dump();
4247 Result.error("Could not infer all types in pattern result!");
4248 }
4249
4250 // Promote xform function to be an explicit node wherever set.
4251 TreePatternNodePtr DstShared = PromoteXForms(Result.getOnlyTree());
4252
4253 TreePattern Temp(Result.getRecord(), DstShared, false, *this);
4254 Temp.InferAllTypes();
4255
4256 ListInit *Preds = TheDef->getValueAsListInit("Predicates");
4257 int Complexity = TheDef->getValueAsInt("AddedComplexity");
4258
4259 if (PatternRewriter)
4260 PatternRewriter(&Pattern);
4261
4262 // A pattern may end up with an "impossible" type, i.e. a situation
4263 // where all types have been eliminated for some node in this pattern.
4264 // This could occur for intrinsics that only make sense for a specific
4265 // value type, and use a specific register class. If, for some mode,
4266 // that register class does not accept that type, the type inference
4267 // will lead to a contradiction, which is not an error however, but
4268 // a sign that this pattern will simply never match.
4269 if (Temp.getOnlyTree()->hasPossibleType())
4270 for (const auto &T : Pattern.getTrees())
4271 if (T->hasPossibleType())
4272 AddPatternToMatch(&Pattern,
4273 PatternToMatch(TheDef, Preds, T, Temp.getOnlyTree(),
4274 InstImpResults, Complexity,
4275 TheDef->getID()));
4276}
4277
4278void CodeGenDAGPatterns::ParsePatterns() {
4279 std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
4280
4281 for (Record *CurPattern : Patterns) {
4282 DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
4283
4284 // If the pattern references the null_frag, there's nothing to do.
4285 if (hasNullFragReference(Tree))
4286 continue;
4287
4288 TreePattern Pattern(CurPattern, Tree, true, *this);
4289
4290 ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs");
4291 if (LI->empty()) continue; // no pattern.
4292
4293 // Parse the instruction.
4294 TreePattern Result(CurPattern, LI, false, *this);
4295
4296 if (Result.getNumTrees() != 1)
4297 Result.error("Cannot handle instructions producing instructions "
4298 "with temporaries yet!");
4299
4300 // Validate that the input pattern is correct.
4301 std::map<std::string, TreePatternNodePtr> InstInputs;
4302 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
4303 InstResults;
4304 std::vector<Record*> InstImpResults;
4305 for (unsigned j = 0, ee = Pattern.getNumTrees(); j != ee; ++j)
4306 FindPatternInputsAndOutputs(Pattern, Pattern.getTree(j), InstInputs,
4307 InstResults, InstImpResults);
4308
4309 ParseOnePattern(CurPattern, Pattern, Result, InstImpResults);
4310 }
4311}
4312
4313static void collectModes(std::set<unsigned> &Modes, const TreePatternNode *N) {
4314 for (const TypeSetByHwMode &VTS : N->getExtTypes())
4315 for (const auto &I : VTS)
4316 Modes.insert(I.first);
4317
4318 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4319 collectModes(Modes, N->getChild(i));
4320}
4321
4322void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
4323 const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
4324 std::vector<PatternToMatch> Copy;
4325 PatternsToMatch.swap(Copy);
4326
4327 auto AppendPattern = [this](PatternToMatch &P, unsigned Mode,
4328 StringRef Check) {
4329 TreePatternNodePtr NewSrc = P.getSrcPattern()->clone();
4330 TreePatternNodePtr NewDst = P.getDstPattern()->clone();
4331 if (!NewSrc->setDefaultMode(Mode) || !NewDst->setDefaultMode(Mode)) {
4332 return;
4333 }
4334
4335 PatternsToMatch.emplace_back(P.getSrcRecord(), P.getPredicates(),
4336 std::move(NewSrc), std::move(NewDst),
4337 P.getDstRegs(), P.getAddedComplexity(),
4338 Record::getNewUID(), Mode, Check);
4339 };
4340
4341 for (PatternToMatch &P : Copy) {
4342 TreePatternNodePtr SrcP = nullptr, DstP = nullptr;
4343 if (P.getSrcPattern()->hasProperTypeByHwMode())
4344 SrcP = P.getSrcPatternShared();
4345 if (P.getDstPattern()->hasProperTypeByHwMode())
4346 DstP = P.getDstPatternShared();
4347 if (!SrcP && !DstP) {
4348 PatternsToMatch.push_back(P);
4349 continue;
4350 }
4351
4352 std::set<unsigned> Modes;
4353 if (SrcP)
4354 collectModes(Modes, SrcP.get());
4355 if (DstP)
4356 collectModes(Modes, DstP.get());
4357
4358 // The predicate for the default mode needs to be constructed for each
4359 // pattern separately.
4360 // Since not all modes must be present in each pattern, if a mode m is
4361 // absent, then there is no point in constructing a check for m. If such
4362 // a check was created, it would be equivalent to checking the default
4363 // mode, except not all modes' predicates would be a part of the checking
4364 // code. The subsequently generated check for the default mode would then
4365 // have the exact same patterns, but a different predicate code. To avoid
4366 // duplicated patterns with different predicate checks, construct the
4367 // default check as a negation of all predicates that are actually present
4368 // in the source/destination patterns.
4369 SmallString<128> DefaultCheck;
4370
4371 for (unsigned M : Modes) {
4372 if (M == DefaultMode)
4373 continue;
4374
4375 // Fill the map entry for this mode.
4376 const HwMode &HM = CGH.getMode(M);
4377 AppendPattern(P, M, "(MF->getSubtarget().checkFeatures(\"" + HM.Features + "\"))");
4378
4379 // Add negations of the HM's predicates to the default predicate.
4380 if (!DefaultCheck.empty())
4381 DefaultCheck += " && ";
4382 DefaultCheck += "(!(MF->getSubtarget().checkFeatures(\"";
4383 DefaultCheck += HM.Features;
4384 DefaultCheck += "\")))";
4385 }
4386
4387 bool HasDefault = Modes.count(DefaultMode);
4388 if (HasDefault)
4389 AppendPattern(P, DefaultMode, DefaultCheck);
4390 }
4391}
4392
4393/// Dependent variable map for CodeGenDAGPattern variant generation
4394typedef StringMap<int> DepVarMap;
4395
4396static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
4397 if (N->isLeaf()) {
4398 if (N->hasName() && isa<DefInit>(N->getLeafValue()))
4399 DepMap[N->getName()]++;
4400 } else {
4401 for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
4402 FindDepVarsOf(N->getChild(i), DepMap);
4403 }
4404}
4405
4406/// Find dependent variables within child patterns
4407static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
4408 DepVarMap depcounts;
4409 FindDepVarsOf(N, depcounts);
4410 for (const auto &Pair : depcounts) {
4411 if (Pair.getValue() > 1)
4412 DepVars.insert(Pair.getKey());
4413 }
4414}
4415
4416#ifndef NDEBUG1
4417/// Dump the dependent variable set:
4418static void DumpDepVars(MultipleUseVarSet &DepVars) {
4419 if (DepVars.empty()) {
4420 LLVM_DEBUG(errs() << "<empty set>")do { } while (false);
4421 } else {
4422 LLVM_DEBUG(errs() << "[ ")do { } while (false);
4423 for (const auto &DepVar : DepVars) {
4424 LLVM_DEBUG(errs() << DepVar.getKey() << " ")do { } while (false);
4425 }
4426 LLVM_DEBUG(errs() << "]")do { } while (false);
4427 }
4428}
4429#endif
4430
4431
4432/// CombineChildVariants - Given a bunch of permutations of each child of the
4433/// 'operator' node, put them together in all possible ways.
4434static void CombineChildVariants(
4435 TreePatternNodePtr Orig,
4436 const std::vector<std::vector<TreePatternNodePtr>> &ChildVariants,
4437 std::vector<TreePatternNodePtr> &OutVariants, CodeGenDAGPatterns &CDP,
4438 const MultipleUseVarSet &DepVars) {
4439 // Make sure that each operand has at least one variant to choose from.
4440 for (const auto &Variants : ChildVariants)
4441 if (Variants.empty())
4442 return;
4443
4444 // The end result is an all-pairs construction of the resultant pattern.
4445 std::vector<unsigned> Idxs;
4446 Idxs.resize(ChildVariants.size());
4447 bool NotDone;
4448 do {
4449#ifndef NDEBUG1
4450 LLVM_DEBUG(if (!Idxs.empty()) {do { } while (false)
4451 errs() << Orig->getOperator()->getName() << ": Idxs = [ ";do { } while (false)
4452 for (unsigned Idx : Idxs) {do { } while (false)
4453 errs() << Idx << " ";do { } while (false)
4454 }do { } while (false)
4455 errs() << "]\n";do { } while (false)
4456 })do { } while (false);
4457#endif
4458 // Create the variant and add it to the output list.
4459 std::vector<TreePatternNodePtr> NewChildren;
4460 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
4461 NewChildren.push_back(ChildVariants[i][Idxs[i]]);
4462 TreePatternNodePtr R = std::make_shared<TreePatternNode>(
4463 Orig->getOperator(), std::move(NewChildren), Orig->getNumTypes());
4464
4465 // Copy over properties.
4466 R->setName(Orig->getName());
4467 R->setNamesAsPredicateArg(Orig->getNamesAsPredicateArg());
4468 R->setPredicateCalls(Orig->getPredicateCalls());
4469 R->setTransformFn(Orig->getTransformFn());
4470 for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i)
4471 R->setType(i, Orig->getExtType(i));
4472
4473 // If this pattern cannot match, do not include it as a variant.
4474 std::string ErrString;
4475 // Scan to see if this pattern has already been emitted. We can get
4476 // duplication due to things like commuting:
4477 // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
4478 // which are the same pattern. Ignore the dups.
4479 if (R->canPatternMatch(ErrString, CDP) &&
4480 none_of(OutVariants, [&](TreePatternNodePtr Variant) {
4481 return R->isIsomorphicTo(Variant.get(), DepVars);
4482 }))
4483 OutVariants.push_back(R);
4484
4485 // Increment indices to the next permutation by incrementing the
4486 // indices from last index backward, e.g., generate the sequence
4487 // [0, 0], [0, 1], [1, 0], [1, 1].
4488 int IdxsIdx;
4489 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
4490 if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size())
4491 Idxs[IdxsIdx] = 0;
4492 else
4493 break;
4494 }
4495 NotDone = (IdxsIdx >= 0);
4496 } while (NotDone);
4497}
4498
4499/// CombineChildVariants - A helper function for binary operators.
4500///
4501static void CombineChildVariants(TreePatternNodePtr Orig,
4502 const std::vector<TreePatternNodePtr> &LHS,
4503 const std::vector<TreePatternNodePtr> &RHS,
4504 std::vector<TreePatternNodePtr> &OutVariants,
4505 CodeGenDAGPatterns &CDP,
4506 const MultipleUseVarSet &DepVars) {
4507 std::vector<std::vector<TreePatternNodePtr>> ChildVariants;
4508 ChildVariants.push_back(LHS);
4509 ChildVariants.push_back(RHS);
4510 CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars);
4511}
4512
4513static void
4514GatherChildrenOfAssociativeOpcode(TreePatternNodePtr N,
4515 std::vector<TreePatternNodePtr> &Children) {
4516 assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!")(static_cast<void> (0));
4517 Record *Operator = N->getOperator();
4518
4519 // Only permit raw nodes.
4520 if (!N->getName().empty() || !N->getPredicateCalls().empty() ||
4521 N->getTransformFn()) {
4522 Children.push_back(N);
4523 return;
4524 }
4525
4526 if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
4527 Children.push_back(N->getChildShared(0));
4528 else
4529 GatherChildrenOfAssociativeOpcode(N->getChildShared(0), Children);
4530
4531 if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
4532 Children.push_back(N->getChildShared(1));
4533 else
4534 GatherChildrenOfAssociativeOpcode(N->getChildShared(1), Children);
4535}
4536
4537/// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
4538/// the (potentially recursive) pattern by using algebraic laws.
4539///
4540static void GenerateVariantsOf(TreePatternNodePtr N,
4541 std::vector<TreePatternNodePtr> &OutVariants,
4542 CodeGenDAGPatterns &CDP,
4543 const MultipleUseVarSet &DepVars) {
4544 // We cannot permute leaves or ComplexPattern uses.
4545 if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) {
4546 OutVariants.push_back(N);
4547 return;
4548 }
4549
4550 // Look up interesting info about the node.
4551 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator());
4552
4553 // If this node is associative, re-associate.
4554 if (NodeInfo.hasProperty(SDNPAssociative)) {
4555 // Re-associate by pulling together all of the linked operators
4556 std::vector<TreePatternNodePtr> MaximalChildren;
4557 GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
4558
4559 // Only handle child sizes of 3. Otherwise we'll end up trying too many
4560 // permutations.
4561 if (MaximalChildren.size() == 3) {
4562 // Find the variants of all of our maximal children.
4563 std::vector<TreePatternNodePtr> AVariants, BVariants, CVariants;
4564 GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars);
4565 GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars);
4566 GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars);
4567
4568 // There are only two ways we can permute the tree:
4569 // (A op B) op C and A op (B op C)
4570 // Within these forms, we can also permute A/B/C.
4571
4572 // Generate legal pair permutations of A/B/C.
4573 std::vector<TreePatternNodePtr> ABVariants;
4574 std::vector<TreePatternNodePtr> BAVariants;
4575 std::vector<TreePatternNodePtr> ACVariants;
4576 std::vector<TreePatternNodePtr> CAVariants;
4577 std::vector<TreePatternNodePtr> BCVariants;
4578 std::vector<TreePatternNodePtr> CBVariants;
4579 CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars);
4580 CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars);
4581 CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars);
4582 CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars);
4583 CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars);
4584 CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars);
4585
4586 // Combine those into the result: (x op x) op x
4587 CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars);
4588 CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars);
4589 CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars);
4590 CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars);
4591 CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars);
4592 CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars);
4593
4594 // Combine those into the result: x op (x op x)
4595 CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars);
4596 CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars);
4597 CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars);
4598 CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars);
4599 CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars);
4600 CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars);
4601 return;
4602 }
4603 }
4604
4605 // Compute permutations of all children.
4606 std::vector<std::vector<TreePatternNodePtr>> ChildVariants;
4607 ChildVariants.resize(N->getNumChildren());
4608 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4609 GenerateVariantsOf(N->getChildShared(i), ChildVariants[i], CDP, DepVars);
4610
4611 // Build all permutations based on how the children were formed.
4612 CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars);
4613
4614 // If this node is commutative, consider the commuted order.
4615 bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP);
4616 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
4617 assert((N->getNumChildren()>=2 || isCommIntrinsic) &&(static_cast<void> (0))
4618 "Commutative but doesn't have 2 children!")(static_cast<void> (0));
4619 // Don't count children which are actually register references.
4620 unsigned NC = 0;
4621 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
4622 TreePatternNode *Child = N->getChild(i);
4623 if (Child->isLeaf())
4624 if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) {
4625 Record *RR = DI->getDef();
4626 if (RR->isSubClassOf("Register"))
4627 continue;
4628 }
4629 NC++;
4630 }
4631 // Consider the commuted order.
4632 if (isCommIntrinsic) {
4633 // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd
4634 // operands are the commutative operands, and there might be more operands
4635 // after those.
4636 assert(NC >= 3 &&(static_cast<void> (0))
4637 "Commutative intrinsic should have at least 3 children!")(static_cast<void> (0));
4638 std::vector<std::vector<TreePatternNodePtr>> Variants;
4639 Variants.push_back(std::move(ChildVariants[0])); // Intrinsic id.
4640 Variants.push_back(std::move(ChildVariants[2]));
4641 Variants.push_back(std::move(ChildVariants[1]));
4642 for (unsigned i = 3; i != NC; ++i)
4643 Variants.push_back(std::move(ChildVariants[i]));
4644 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
4645 } else if (NC == N->getNumChildren()) {
4646 std::vector<std::vector<TreePatternNodePtr>> Variants;
4647 Variants.push_back(std::move(ChildVariants[1]));
4648 Variants.push_back(std::move(ChildVariants[0]));
4649 for (unsigned i = 2; i != NC; ++i)
4650 Variants.push_back(std::move(ChildVariants[i]));
4651 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
4652 }
4653 }
4654}
4655
4656
4657// GenerateVariants - Generate variants. For example, commutative patterns can
4658// match multiple ways. Add them to PatternsToMatch as well.
4659void CodeGenDAGPatterns::GenerateVariants() {
4660 LLVM_DEBUG(errs() << "Generating instruction variants.\n")do { } while (false);
4661
4662 // Loop over all of the patterns we've collected, checking to see if we can
4663 // generate variants of the instruction, through the exploitation of
4664 // identities. This permits the target to provide aggressive matching without
4665 // the .td file having to contain tons of variants of instructions.
4666 //
4667 // Note that this loop adds new patterns to the PatternsToMatch list, but we
4668 // intentionally do not reconsider these. Any variants of added patterns have
4669 // already been added.
4670 //
4671 for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) {
4672 MultipleUseVarSet DepVars;
4673 std::vector<TreePatternNodePtr> Variants;
4674 FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars);
4675 LLVM_DEBUG(errs() << "Dependent/multiply used variables: ")do { } while (false);
4676 LLVM_DEBUG(DumpDepVars(DepVars))do { } while (false);
4677 LLVM_DEBUG(errs() << "\n")do { } while (false);
4678 GenerateVariantsOf(PatternsToMatch[i].getSrcPatternShared(), Variants,
4679 *this, DepVars);
4680
4681 assert(PatternsToMatch[i].getHwModeFeatures().empty() &&(static_cast<void> (0))
4682 "HwModes should not have been expanded yet!")(static_cast<void> (0));
4683
4684 assert(!Variants.empty() && "Must create at least original variant!")(static_cast<void> (0));
4685 if (Variants.size() == 1) // No additional variants for this pattern.
4686 continue;
4687
4688 LLVM_DEBUG(errs() << "FOUND VARIANTS OF: ";do { } while (false)
4689 PatternsToMatch[i].getSrcPattern()->dump(); errs() << "\n")do { } while (false);
4690
4691 for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
4692 TreePatternNodePtr Variant = Variants[v];
4693
4694 LLVM_DEBUG(errs() << " VAR#" << v << ": "; Variant->dump();do { } while (false)
4695 errs() << "\n")do { } while (false);
4696
4697 // Scan to see if an instruction or explicit pattern already matches this.
4698 bool AlreadyExists = false;
4699 for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
4700 // Skip if the top level predicates do not match.
4701 if ((i != p) && (PatternsToMatch[i].getPredicates() !=
4702 PatternsToMatch[p].getPredicates()))
4703 continue;
4704 // Check to see if this variant already exists.
4705 if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
4706 DepVars)) {
4707 LLVM_DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n")do { } while (false);
4708 AlreadyExists = true;
4709 break;
4710 }
4711 }
4712 // If we already have it, ignore the variant.
4713 if (AlreadyExists) continue;
4714
4715 // Otherwise, add it to the list of patterns we have.
4716 PatternsToMatch.emplace_back(
4717 PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(),
4718 Variant, PatternsToMatch[i].getDstPatternShared(),
4719 PatternsToMatch[i].getDstRegs(),
4720 PatternsToMatch[i].getAddedComplexity(), Record::getNewUID(),
4721 PatternsToMatch[i].getForceMode(),
4722 PatternsToMatch[i].getHwModeFeatures());
4723 }
4724
4725 LLVM_DEBUG(errs() << "\n")do { } while (false);
4726 }
4727}

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include/llvm/TableGen/Record.h

1//===- llvm/TableGen/Record.h - Classes for Table Records -------*- 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 defines the main TableGen data structures, including the TableGen
10// types, values, and high-level data structures.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TABLEGEN_RECORD_H
15#define LLVM_TABLEGEN_RECORD_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/PointerIntPair.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/SMLoc.h"
28#include "llvm/Support/Timer.h"
29#include "llvm/Support/TrailingObjects.h"
30#include "llvm/Support/raw_ostream.h"
31#include <algorithm>
32#include <cassert>
33#include <cstddef>
34#include <cstdint>
35#include <map>
36#include <memory>
37#include <string>
38#include <utility>
39#include <vector>
40
41namespace llvm {
42
43class ListRecTy;
44struct MultiClass;
45class Record;
46class RecordKeeper;
47class RecordVal;
48class Resolver;
49class StringInit;
50class TypedInit;
51
52//===----------------------------------------------------------------------===//
53// Type Classes
54//===----------------------------------------------------------------------===//
55
56class RecTy {
57public:
58 /// Subclass discriminator (for dyn_cast<> et al.)
59 enum RecTyKind {
60 BitRecTyKind,
61 BitsRecTyKind,
62 IntRecTyKind,
63 StringRecTyKind,
64 ListRecTyKind,
65 DagRecTyKind,
66 RecordRecTyKind
67 };
68
69private:
70 RecTyKind Kind;
71 /// ListRecTy of the list that has elements of this type.
72 ListRecTy *ListTy = nullptr;
73
74public:
75 RecTy(RecTyKind K) : Kind(K) {}
76 virtual ~RecTy() = default;
77
78 RecTyKind getRecTyKind() const { return Kind; }
79
80 virtual std::string getAsString() const = 0;
81 void print(raw_ostream &OS) const { OS << getAsString(); }
82 void dump() const;
83
84 /// Return true if all values of 'this' type can be converted to the specified
85 /// type.
86 virtual bool typeIsConvertibleTo(const RecTy *RHS) const;
87
88 /// Return true if 'this' type is equal to or a subtype of RHS. For example,
89 /// a bit set is not an int, but they are convertible.
90 virtual bool typeIsA(const RecTy *RHS) const;
91
92 /// Returns the type representing list<thistype>.
93 ListRecTy *getListTy();
94};
95
96inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
97 Ty.print(OS);
98 return OS;
99}
100
101/// 'bit' - Represent a single bit
102class BitRecTy : public RecTy {
103 static BitRecTy Shared;
104
105 BitRecTy() : RecTy(BitRecTyKind) {}
106
107public:
108 static bool classof(const RecTy *RT) {
109 return RT->getRecTyKind() == BitRecTyKind;
110 }
111
112 static BitRecTy *get() { return &Shared; }
113
114 std::string getAsString() const override { return "bit"; }
115
116 bool typeIsConvertibleTo(const RecTy *RHS) const override;
117};
118
119/// 'bits<n>' - Represent a fixed number of bits
120class BitsRecTy : public RecTy {
121 unsigned Size;
122
123 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
124
125public:
126 static bool classof(const RecTy *RT) {
127 return RT->getRecTyKind() == BitsRecTyKind;
128 }
129
130 static BitsRecTy *get(unsigned Sz);
131
132 unsigned getNumBits() const { return Size; }
133
134 std::string getAsString() const override;
135
136 bool typeIsConvertibleTo(const RecTy *RHS) const override;
137
138 bool typeIsA(const RecTy *RHS) const override;
139};
140
141/// 'int' - Represent an integer value of no particular size
142class IntRecTy : public RecTy {
143 static IntRecTy Shared;
144
145 IntRecTy() : RecTy(IntRecTyKind) {}
146
147public:
148 static bool classof(const RecTy *RT) {
149 return RT->getRecTyKind() == IntRecTyKind;
150 }
151
152 static IntRecTy *get() { return &Shared; }
153
154 std::string getAsString() const override { return "int"; }
155
156 bool typeIsConvertibleTo(const RecTy *RHS) const override;
157};
158
159/// 'string' - Represent an string value
160class StringRecTy : public RecTy {
161 static StringRecTy Shared;
162
163 StringRecTy() : RecTy(StringRecTyKind) {}
164
165public:
166 static bool classof(const RecTy *RT) {
167 return RT->getRecTyKind() == StringRecTyKind;
168 }
169
170 static StringRecTy *get() { return &Shared; }
171
172 std::string getAsString() const override;
173
174 bool typeIsConvertibleTo(const RecTy *RHS) const override;
175};
176
177/// 'list<Ty>' - Represent a list of element values, all of which must be of
178/// the specified type. The type is stored in ElementTy.
179class ListRecTy : public RecTy {
180 friend ListRecTy *RecTy::getListTy();
181
182 RecTy *ElementTy;
183
184 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), ElementTy(T) {}
185
186public:
187 static bool classof(const RecTy *RT) {
188 return RT->getRecTyKind() == ListRecTyKind;
189 }
190
191 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
192 RecTy *getElementType() const { return ElementTy; }
193
194 std::string getAsString() const override;
195
196 bool typeIsConvertibleTo(const RecTy *RHS) const override;
197
198 bool typeIsA(const RecTy *RHS) const override;
199};
200
201/// 'dag' - Represent a dag fragment
202class DagRecTy : public RecTy {
203 static DagRecTy Shared;
204
205 DagRecTy() : RecTy(DagRecTyKind) {}
206
207public:
208 static bool classof(const RecTy *RT) {
209 return RT->getRecTyKind() == DagRecTyKind;
210 }
211
212 static DagRecTy *get() { return &Shared; }
213
214 std::string getAsString() const override;
215};
216
217/// '[classname]' - Type of record values that have zero or more superclasses.
218///
219/// The list of superclasses is non-redundant, i.e. only contains classes that
220/// are not the superclass of some other listed class.
221class RecordRecTy final : public RecTy, public FoldingSetNode,
222 public TrailingObjects<RecordRecTy, Record *> {
223 friend class Record;
224
225 unsigned NumClasses;
226
227 explicit RecordRecTy(unsigned Num)
228 : RecTy(RecordRecTyKind), NumClasses(Num) {}
229
230public:
231 RecordRecTy(const RecordRecTy &) = delete;
232 RecordRecTy &operator=(const RecordRecTy &) = delete;
233
234 // Do not use sized deallocation due to trailing objects.
235 void operator delete(void *p) { ::operator delete(p); }
236
237 static bool classof(const RecTy *RT) {
238 return RT->getRecTyKind() == RecordRecTyKind;
239 }
240
241 /// Get the record type with the given non-redundant list of superclasses.
242 static RecordRecTy *get(ArrayRef<Record *> Classes);
243
244 void Profile(FoldingSetNodeID &ID) const;
245
246 ArrayRef<Record *> getClasses() const {
247 return makeArrayRef(getTrailingObjects<Record *>(), NumClasses);
248 }
249
250 using const_record_iterator = Record * const *;
251
252 const_record_iterator classes_begin() const { return getClasses().begin(); }
253 const_record_iterator classes_end() const { return getClasses().end(); }
254
255 std::string getAsString() const override;
256
257 bool isSubClassOf(Record *Class) const;
258 bool typeIsConvertibleTo(const RecTy *RHS) const override;
259
260 bool typeIsA(const RecTy *RHS) const override;
261};
262
263/// Find a common type that T1 and T2 convert to.
264/// Return 0 if no such type exists.
265RecTy *resolveTypes(RecTy *T1, RecTy *T2);
266
267//===----------------------------------------------------------------------===//
268// Initializer Classes
269//===----------------------------------------------------------------------===//
270
271class Init {
272protected:
273 /// Discriminator enum (for isa<>, dyn_cast<>, et al.)
274 ///
275 /// This enum is laid out by a preorder traversal of the inheritance
276 /// hierarchy, and does not contain an entry for abstract classes, as per
277 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
278 ///
279 /// We also explicitly include "first" and "last" values for each
280 /// interior node of the inheritance tree, to make it easier to read the
281 /// corresponding classof().
282 ///
283 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
284 /// and IK_LastXXXInit be their own values, but that would degrade
285 /// readability for really no benefit.
286 enum InitKind : uint8_t {
287 IK_First, // unused; silence a spurious warning
288 IK_FirstTypedInit,
289 IK_BitInit,
290 IK_BitsInit,
291 IK_DagInit,
292 IK_DefInit,
293 IK_FieldInit,
294 IK_IntInit,
295 IK_ListInit,
296 IK_FirstOpInit,
297 IK_BinOpInit,
298 IK_TernOpInit,
299 IK_UnOpInit,
300 IK_LastOpInit,
301 IK_CondOpInit,
302 IK_FoldOpInit,
303 IK_IsAOpInit,
304 IK_AnonymousNameInit,
305 IK_StringInit,
306 IK_VarInit,
307 IK_VarListElementInit,
308 IK_VarBitInit,
309 IK_VarDefInit,
310 IK_LastTypedInit,
311 IK_UnsetInit
312 };
313
314private:
315 const InitKind Kind;
316
317protected:
318 uint8_t Opc; // Used by UnOpInit, BinOpInit, and TernOpInit
319
320private:
321 virtual void anchor();
322
323public:
324 /// Get the kind (type) of the value.
325 InitKind getKind() const { return Kind; }
326
327protected:
328 explicit Init(InitKind K, uint8_t Opc = 0) : Kind(K), Opc(Opc) {}
329
330public:
331 Init(const Init &) = delete;
332 Init &operator=(const Init &) = delete;
333 virtual ~Init() = default;
334
335 /// Is this a complete value with no unset (uninitialized) subvalues?
336 virtual bool isComplete() const { return true; }
337
338 /// Is this a concrete and fully resolved value without any references or
339 /// stuck operations? Unset values are concrete.
340 virtual bool isConcrete() const { return false; }
341
342 /// Print this value.
343 void print(raw_ostream &OS) const { OS << getAsString(); }
344
345 /// Convert this value to a literal form.
346 virtual std::string getAsString() const = 0;
347
348 /// Convert this value to a literal form,
349 /// without adding quotes around a string.
350 virtual std::string getAsUnquotedString() const { return getAsString(); }
351
352 /// Debugging method that may be called through a debugger; just
353 /// invokes print on stderr.
354 void dump() const;
355
356 /// If this value is convertible to type \p Ty, return a value whose
357 /// type is \p Ty, generating a !cast operation if required.
358 /// Otherwise, return null.
359 virtual Init *getCastTo(RecTy *Ty) const = 0;
360
361 /// Convert to a value whose type is \p Ty, or return null if this
362 /// is not possible. This can happen if the value's type is convertible
363 /// to \p Ty, but there are unresolved references.
364 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
365
366 /// This function is used to implement the bit range
367 /// selection operator. Given a value, it selects the specified bits,
368 /// returning them as a new \p Init of type \p bits. If it is not legal
369 /// to use the bit selection operator on this value, null is returned.
370 virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
371 return nullptr;
372 }
373
374 /// This function is used to implement the list slice
375 /// selection operator. Given a value, it selects the specified list
376 /// elements, returning them as a new \p Init of type \p list. If it
377 /// is not legal to use the slice operator, null is returned.
378 virtual Init *convertInitListSlice(ArrayRef<unsigned> Elements) const {
379 return nullptr;
380 }
381
382 /// This function is used to implement the FieldInit class.
383 /// Implementors of this method should return the type of the named
384 /// field if they are of type record.
385 virtual RecTy *getFieldType(StringInit *FieldName) const {
386 return nullptr;
387 }
388
389 /// This function is used by classes that refer to other
390 /// variables which may not be defined at the time the expression is formed.
391 /// If a value is set for the variable later, this method will be called on
392 /// users of the value to allow the value to propagate out.
393 virtual Init *resolveReferences(Resolver &R) const {
394 return const_cast<Init *>(this);
395 }
396
397 /// Get the \p Init value of the specified bit.
398 virtual Init *getBit(unsigned Bit) const = 0;
399};
400
401inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
402 I.print(OS); return OS;
403}
404
405/// This is the common superclass of types that have a specific,
406/// explicit type, stored in ValueTy.
407class TypedInit : public Init {
408 RecTy *ValueTy;
409
410protected:
411 explicit TypedInit(InitKind K, RecTy *T, uint8_t Opc = 0)
412 : Init(K, Opc), ValueTy(T) {}
413
414public:
415 TypedInit(const TypedInit &) = delete;
416 TypedInit &operator=(const TypedInit &) = delete;
417
418 static bool classof(const Init *I) {
419 return I->getKind() >= IK_FirstTypedInit &&
420 I->getKind() <= IK_LastTypedInit;
421 }
422
423 /// Get the type of the Init as a RecTy.
424 RecTy *getType() const { return ValueTy; }
425
426 Init *getCastTo(RecTy *Ty) const override;
427 Init *convertInitializerTo(RecTy *Ty) const override;
428
429 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
430 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override;
431
432 /// This method is used to implement the FieldInit class.
433 /// Implementors of this method should return the type of the named field if
434 /// they are of type record.
435 RecTy *getFieldType(StringInit *FieldName) const override;
436};
437
438/// '?' - Represents an uninitialized value.
439class UnsetInit : public Init {
440 UnsetInit() : Init(IK_UnsetInit) {}
441
442public:
443 UnsetInit(const UnsetInit &) = delete;
444 UnsetInit &operator=(const UnsetInit &) = delete;
445
446 static bool classof(const Init *I) {
447 return I->getKind() == IK_UnsetInit;
448 }
449
450 /// Get the singleton unset Init.
451 static UnsetInit *get();
452
453 Init *getCastTo(RecTy *Ty) const override;
454 Init *convertInitializerTo(RecTy *Ty) const override;
455
456 Init *getBit(unsigned Bit) const override {
457 return const_cast<UnsetInit*>(this);
458 }
459
460 /// Is this a complete value with no unset (uninitialized) subvalues?
461 bool isComplete() const override { return false; }
462
463 bool isConcrete() const override { return true; }
464
465 /// Get the string representation of the Init.
466 std::string getAsString() const override { return "?"; }
467};
468
469/// 'true'/'false' - Represent a concrete initializer for a bit.
470class BitInit final : public TypedInit {
471 bool Value;
472
473 explicit BitInit(bool V) : TypedInit(IK_BitInit, BitRecTy::get()), Value(V) {}
474
475public:
476 BitInit(const BitInit &) = delete;
477 BitInit &operator=(BitInit &) = delete;
478
479 static bool classof(const Init *I) {
480 return I->getKind() == IK_BitInit;
481 }
482
483 static BitInit *get(bool V);
484
485 bool getValue() const { return Value; }
486
487 Init *convertInitializerTo(RecTy *Ty) const override;
488
489 Init *getBit(unsigned Bit) const override {
490 assert(Bit < 1 && "Bit index out of range!")(static_cast<void> (0));
491 return const_cast<BitInit*>(this);
492 }
493
494 bool isConcrete() const override { return true; }
495 std::string getAsString() const override { return Value ? "1" : "0"; }
496};
497
498/// '{ a, b, c }' - Represents an initializer for a BitsRecTy value.
499/// It contains a vector of bits, whose size is determined by the type.
500class BitsInit final : public TypedInit, public FoldingSetNode,
501 public TrailingObjects<BitsInit, Init *> {
502 unsigned NumBits;
503
504 BitsInit(unsigned N)
505 : TypedInit(IK_BitsInit, BitsRecTy::get(N)), NumBits(N) {}
506
507public:
508 BitsInit(const BitsInit &) = delete;
509 BitsInit &operator=(const BitsInit &) = delete;
510
511 // Do not use sized deallocation due to trailing objects.
512 void operator delete(void *p) { ::operator delete(p); }
513
514 static bool classof(const Init *I) {
515 return I->getKind() == IK_BitsInit;
516 }
517
518 static BitsInit *get(ArrayRef<Init *> Range);
519
520 void Profile(FoldingSetNodeID &ID) const;
521
522 unsigned getNumBits() const { return NumBits; }
523
524 Init *convertInitializerTo(RecTy *Ty) const override;
525 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
526
527 bool isComplete() const override {
528 for (unsigned i = 0; i != getNumBits(); ++i)
529 if (!getBit(i)->isComplete()) return false;
530 return true;
531 }
532
533 bool allInComplete() const {
534 for (unsigned i = 0; i != getNumBits(); ++i)
535 if (getBit(i)->isComplete()) return false;
536 return true;
537 }
538
539 bool isConcrete() const override;
540 std::string getAsString() const override;
541
542 Init *resolveReferences(Resolver &R) const override;
543
544 Init *getBit(unsigned Bit) const override {
545 assert(Bit < NumBits && "Bit index out of range!")(static_cast<void> (0));
546 return getTrailingObjects<Init *>()[Bit];
547 }
548};
549
550/// '7' - Represent an initialization by a literal integer value.
551class IntInit : public TypedInit {
552 int64_t Value;
553
554 explicit IntInit(int64_t V)
555 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
556
557public:
558 IntInit(const IntInit &) = delete;
559 IntInit &operator=(const IntInit &) = delete;
560
561 static bool classof(const Init *I) {
562 return I->getKind() == IK_IntInit;
563 }
564
565 static IntInit *get(int64_t V);
566
567 int64_t getValue() const { return Value; }
568
569 Init *convertInitializerTo(RecTy *Ty) const override;
570 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
571
572 bool isConcrete() const override { return true; }
573 std::string getAsString() const override;
574
575 Init *getBit(unsigned Bit) const override {
576 return BitInit::get((Value & (1ULL << Bit)) != 0);
577 }
578};
579
580/// "anonymous_n" - Represent an anonymous record name
581class AnonymousNameInit : public TypedInit {
582 unsigned Value;
583
584 explicit AnonymousNameInit(unsigned V)
585 : TypedInit(IK_AnonymousNameInit, StringRecTy::get()), Value(V) {}
586
587public:
588 AnonymousNameInit(const AnonymousNameInit &) = delete;
589 AnonymousNameInit &operator=(const AnonymousNameInit &) = delete;
590
591 static bool classof(const Init *I) {
592 return I->getKind() == IK_AnonymousNameInit;
593 }
594
595 static AnonymousNameInit *get(unsigned);
596
597 unsigned getValue() const { return Value; }
598
599 StringInit *getNameInit() const;
600
601 std::string getAsString() const override;
602
603 Init *resolveReferences(Resolver &R) const override;
604
605 Init *getBit(unsigned Bit) const override {
606 llvm_unreachable("Illegal bit reference off string")__builtin_unreachable();
607 }
608};
609
610/// "foo" - Represent an initialization by a string value.
611class StringInit : public TypedInit {
612public:
613 enum StringFormat {
614 SF_String, // Format as "text"
615 SF_Code, // Format as [{text}]
616 };
617
618private:
619 StringRef Value;
620 StringFormat Format;
621
622 explicit StringInit(StringRef V, StringFormat Fmt)
623 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V), Format(Fmt) {}
624
625public:
626 StringInit(const StringInit &) = delete;
627 StringInit &operator=(const StringInit &) = delete;
628
629 static bool classof(const Init *I) {
630 return I->getKind() == IK_StringInit;
631 }
632
633 static StringInit *get(StringRef, StringFormat Fmt = SF_String);
634
635 static StringFormat determineFormat(StringFormat Fmt1, StringFormat Fmt2) {
636 return (Fmt1 == SF_Code || Fmt2 == SF_Code) ? SF_Code : SF_String;
637 }
638
639 StringRef getValue() const { return Value; }
640 StringFormat getFormat() const { return Format; }
641 bool hasCodeFormat() const { return Format == SF_Code; }
642
643 Init *convertInitializerTo(RecTy *Ty) const override;
644
645 bool isConcrete() const override { return true; }
646
647 std::string getAsString() const override {
648 if (Format == SF_String)
649 return "\"" + Value.str() + "\"";
650 else
651 return "[{" + Value.str() + "}]";
652 }
653
654 std::string getAsUnquotedString() const override {
655 return std::string(Value);
656 }
657
658 Init *getBit(unsigned Bit) const override {
659 llvm_unreachable("Illegal bit reference off string")__builtin_unreachable();
660 }
661};
662
663/// [AL, AH, CL] - Represent a list of defs
664///
665class ListInit final : public TypedInit, public FoldingSetNode,
666 public TrailingObjects<ListInit, Init *> {
667 unsigned NumValues;
668
669public:
670 using const_iterator = Init *const *;
671
672private:
673 explicit ListInit(unsigned N, RecTy *EltTy)
674 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), NumValues(N) {}
675
676public:
677 ListInit(const ListInit &) = delete;
678 ListInit &operator=(const ListInit &) = delete;
679
680 // Do not use sized deallocation due to trailing objects.
681 void operator delete(void *p) { ::operator delete(p); }
682
683 static bool classof(const Init *I) {
684 return I->getKind() == IK_ListInit;
685 }
686 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
687
688 void Profile(FoldingSetNodeID &ID) const;
689
690 Init *getElement(unsigned i) const {
691 assert(i < NumValues && "List element index out of range!")(static_cast<void> (0));
692 return getTrailingObjects<Init *>()[i];
693 }
694 RecTy *getElementType() const {
695 return cast<ListRecTy>(getType())->getElementType();
696 }
697
698 Record *getElementAsRecord(unsigned i) const;
699
700 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override;
701
702 Init *convertInitializerTo(RecTy *Ty) const override;
703
704 /// This method is used by classes that refer to other
705 /// variables which may not be defined at the time they expression is formed.
706 /// If a value is set for the variable later, this method will be called on
707 /// users of the value to allow the value to propagate out.
708 ///
709 Init *resolveReferences(Resolver &R) const override;
710
711 bool isComplete() const override;
712 bool isConcrete() const override;
713 std::string getAsString() const override;
714
715 ArrayRef<Init*> getValues() const {
716 return makeArrayRef(getTrailingObjects<Init *>(), NumValues);
717 }
718
719 const_iterator begin() const { return getTrailingObjects<Init *>(); }
720 const_iterator end () const { return begin() + NumValues; }
721
722 size_t size () const { return NumValues; }
723 bool empty() const { return NumValues == 0; }
724
725 Init *getBit(unsigned Bit) const override {
726 llvm_unreachable("Illegal bit reference off list")__builtin_unreachable();
727 }
728};
729
730/// Base class for operators
731///
732class OpInit : public TypedInit {
733protected:
734 explicit OpInit(InitKind K, RecTy *Type, uint8_t Opc)
735 : TypedInit(K, Type, Opc) {}
736
737public:
738 OpInit(const OpInit &) = delete;
739 OpInit &operator=(OpInit &) = delete;
740
741 static bool classof(const Init *I) {
742 return I->getKind() >= IK_FirstOpInit &&
743 I->getKind() <= IK_LastOpInit;
744 }
745
746 // Clone - Clone this operator, replacing arguments with the new list
747 virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0;
748
749 virtual unsigned getNumOperands() const = 0;
750 virtual Init *getOperand(unsigned i) const = 0;
751
752 Init *getBit(unsigned Bit) const override;
753};
754
755/// !op (X) - Transform an init.
756///
757class UnOpInit : public OpInit, public FoldingSetNode {
758public:
759 enum UnaryOp : uint8_t { CAST, NOT, HEAD, TAIL, SIZE, EMPTY, GETDAGOP };
760
761private:
762 Init *LHS;
763
764 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
765 : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {}
766
767public:
768 UnOpInit(const UnOpInit &) = delete;
769 UnOpInit &operator=(const UnOpInit &) = delete;
770
771 static bool classof(const Init *I) {
772 return I->getKind() == IK_UnOpInit;
773 }
774
775 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
776
777 void Profile(FoldingSetNodeID &ID) const;
778
779 // Clone - Clone this operator, replacing arguments with the new list
780 OpInit *clone(ArrayRef<Init *> Operands) const override {
781 assert(Operands.size() == 1 &&(static_cast<void> (0))
782 "Wrong number of operands for unary operation")(static_cast<void> (0));
783 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
784 }
785
786 unsigned getNumOperands() const override { return 1; }
787
788 Init *getOperand(unsigned i) const override {
789 assert(i == 0 && "Invalid operand id for unary operator")(static_cast<void> (0));
790 return getOperand();
791 }
792
793 UnaryOp getOpcode() const { return (UnaryOp)Opc; }
794 Init *getOperand() const { return LHS; }
795
796 // Fold - If possible, fold this to a simpler init. Return this if not
797 // possible to fold.
798 Init *Fold(Record *CurRec, bool IsFinal = false) const;
799
800 Init *resolveReferences(Resolver &R) const override;
801
802 std::string getAsString() const override;
803};
804
805/// !op (X, Y) - Combine two inits.
806class BinOpInit : public OpInit, public FoldingSetNode {
807public:
808 enum BinaryOp : uint8_t { ADD, SUB, MUL, AND, OR, XOR, SHL, SRA, SRL, LISTCONCAT,
809 LISTSPLAT, STRCONCAT, INTERLEAVE, CONCAT, EQ,
810 NE, LE, LT, GE, GT, SETDAGOP };
811
812private:
813 Init *LHS, *RHS;
814
815 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
816 OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {}
817
818public:
819 BinOpInit(const BinOpInit &) = delete;
820 BinOpInit &operator=(const BinOpInit &) = delete;
821
822 static bool classof(const Init *I) {
823 return I->getKind() == IK_BinOpInit;
824 }
825
826 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
827 RecTy *Type);
828 static Init *getStrConcat(Init *lhs, Init *rhs);
829 static Init *getListConcat(TypedInit *lhs, Init *rhs);
830
831 void Profile(FoldingSetNodeID &ID) const;
832
833 // Clone - Clone this operator, replacing arguments with the new list
834 OpInit *clone(ArrayRef<Init *> Operands) const override {
835 assert(Operands.size() == 2 &&(static_cast<void> (0))
836 "Wrong number of operands for binary operation")(static_cast<void> (0));
837 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
838 }
839
840 unsigned getNumOperands() const override { return 2; }
841 Init *getOperand(unsigned i) const override {
842 switch (i) {
843 default: llvm_unreachable("Invalid operand id for binary operator")__builtin_unreachable();
844 case 0: return getLHS();
845 case 1: return getRHS();
846 }
847 }
848
849 BinaryOp getOpcode() const { return (BinaryOp)Opc; }
850 Init *getLHS() const { return LHS; }
851 Init *getRHS() const { return RHS; }
852
853 // Fold - If possible, fold this to a simpler init. Return this if not
854 // possible to fold.
855 Init *Fold(Record *CurRec) const;
856
857 Init *resolveReferences(Resolver &R) const override;
858
859 std::string getAsString() const override;
860};
861
862/// !op (X, Y, Z) - Combine two inits.
863class TernOpInit : public OpInit, public FoldingSetNode {
864public:
865 enum TernaryOp : uint8_t { SUBST, FOREACH, FILTER, IF, DAG, SUBSTR, FIND };
866
867private:
868 Init *LHS, *MHS, *RHS;
869
870 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
871 RecTy *Type) :
872 OpInit(IK_TernOpInit, Type, opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
873
874public:
875 TernOpInit(const TernOpInit &) = delete;
876 TernOpInit &operator=(const TernOpInit &) = delete;
877
878 static bool classof(const Init *I) {
879 return I->getKind() == IK_TernOpInit;
880 }
881
882 static TernOpInit *get(TernaryOp opc, Init *lhs,
883 Init *mhs, Init *rhs,
884 RecTy *Type);
885
886 void Profile(FoldingSetNodeID &ID) const;
887
888 // Clone - Clone this operator, replacing arguments with the new list
889 OpInit *clone(ArrayRef<Init *> Operands) const override {
890 assert(Operands.size() == 3 &&(static_cast<void> (0))
891 "Wrong number of operands for ternary operation")(static_cast<void> (0));
892 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
893 getType());
894 }
895
896 unsigned getNumOperands() const override { return 3; }
897 Init *getOperand(unsigned i) const override {
898 switch (i) {
899 default: llvm_unreachable("Invalid operand id for ternary operator")__builtin_unreachable();
900 case 0: return getLHS();
901 case 1: return getMHS();
902 case 2: return getRHS();
903 }
904 }
905
906 TernaryOp getOpcode() const { return (TernaryOp)Opc; }
907 Init *getLHS() const { return LHS; }
908 Init *getMHS() const { return MHS; }
909 Init *getRHS() const { return RHS; }
910
911 // Fold - If possible, fold this to a simpler init. Return this if not
912 // possible to fold.
913 Init *Fold(Record *CurRec) const;
914
915 bool isComplete() const override {
916 return LHS->isComplete() && MHS->isComplete() && RHS->isComplete();
917 }
918
919 Init *resolveReferences(Resolver &R) const override;
920
921 std::string getAsString() const override;
922};
923
924/// !cond(condition_1: value1, ... , condition_n: value)
925/// Selects the first value for which condition is true.
926/// Otherwise reports an error.
927class CondOpInit final : public TypedInit, public FoldingSetNode,
928 public TrailingObjects<CondOpInit, Init *> {
929 unsigned NumConds;
930 RecTy *ValType;
931
932 CondOpInit(unsigned NC, RecTy *Type)
933 : TypedInit(IK_CondOpInit, Type),
934 NumConds(NC), ValType(Type) {}
935
936 size_t numTrailingObjects(OverloadToken<Init *>) const {
937 return 2*NumConds;
938 }
939
940public:
941 CondOpInit(const CondOpInit &) = delete;
942 CondOpInit &operator=(const CondOpInit &) = delete;
943
944 static bool classof(const Init *I) {
945 return I->getKind() == IK_CondOpInit;
946 }
947
948 static CondOpInit *get(ArrayRef<Init*> C, ArrayRef<Init*> V,
949 RecTy *Type);
950
951 void Profile(FoldingSetNodeID &ID) const;
952
953 RecTy *getValType() const { return ValType; }
954
955 unsigned getNumConds() const { return NumConds; }
956
957 Init *getCond(unsigned Num) const {
958 assert(Num < NumConds && "Condition number out of range!")(static_cast<void> (0));
959 return getTrailingObjects<Init *>()[Num];
960 }
961
962 Init *getVal(unsigned Num) const {
963 assert(Num < NumConds && "Val number out of range!")(static_cast<void> (0));
964 return getTrailingObjects<Init *>()[Num+NumConds];
965 }
966
967 ArrayRef<Init *> getConds() const {
968 return makeArrayRef(getTrailingObjects<Init *>(), NumConds);
969 }
970
971 ArrayRef<Init *> getVals() const {
972 return makeArrayRef(getTrailingObjects<Init *>()+NumConds, NumConds);
973 }
974
975 Init *Fold(Record *CurRec) const;
976
977 Init *resolveReferences(Resolver &R) const override;
978
979 bool isConcrete() const override;
980 bool isComplete() const override;
981 std::string getAsString() const override;
982
983 using const_case_iterator = SmallVectorImpl<Init*>::const_iterator;
984 using const_val_iterator = SmallVectorImpl<Init*>::const_iterator;
985
986 inline const_case_iterator arg_begin() const { return getConds().begin(); }
987 inline const_case_iterator arg_end () const { return getConds().end(); }
988
989 inline size_t case_size () const { return NumConds; }
990 inline bool case_empty() const { return NumConds == 0; }
991
992 inline const_val_iterator name_begin() const { return getVals().begin();}
993 inline const_val_iterator name_end () const { return getVals().end(); }
994
995 inline size_t val_size () const { return NumConds; }
996 inline bool val_empty() const { return NumConds == 0; }
997
998 Init *getBit(unsigned Bit) const override;
999};
1000
1001/// !foldl (a, b, expr, start, lst) - Fold over a list.
1002class FoldOpInit : public TypedInit, public FoldingSetNode {
1003private:
1004 Init *Start;
1005 Init *List;
1006 Init *A;
1007 Init *B;
1008 Init *Expr;
1009
1010 FoldOpInit(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
1011 : TypedInit(IK_FoldOpInit, Type), Start(Start), List(List), A(A), B(B),
1012 Expr(Expr) {}
1013
1014public:
1015 FoldOpInit(const FoldOpInit &) = delete;
1016 FoldOpInit &operator=(const FoldOpInit &) = delete;
1017
1018 static bool classof(const Init *I) { return I->getKind() == IK_FoldOpInit; }
1019
1020 static FoldOpInit *get(Init *Start, Init *List, Init *A, Init *B, Init *Expr,
1021 RecTy *Type);
1022
1023 void Profile(FoldingSetNodeID &ID) const;
1024
1025 // Fold - If possible, fold this to a simpler init. Return this if not
1026 // possible to fold.
1027 Init *Fold(Record *CurRec) const;
1028
1029 bool isComplete() const override { return false; }
1030
1031 Init *resolveReferences(Resolver &R) const override;
1032
1033 Init *getBit(unsigned Bit) const override;
1034
1035 std::string getAsString() const override;
1036};
1037
1038/// !isa<type>(expr) - Dynamically determine the type of an expression.
1039class IsAOpInit : public TypedInit, public FoldingSetNode {
1040private:
1041 RecTy *CheckType;
1042 Init *Expr;
1043
1044 IsAOpInit(RecTy *CheckType, Init *Expr)
1045 : TypedInit(IK_IsAOpInit, IntRecTy::get()), CheckType(CheckType),
1046 Expr(Expr) {}
1047
1048public:
1049 IsAOpInit(const IsAOpInit &) = delete;
1050 IsAOpInit &operator=(const IsAOpInit &) = delete;
1051
1052 static bool classof(const Init *I) { return I->getKind() == IK_IsAOpInit; }
1053
1054 static IsAOpInit *get(RecTy *CheckType, Init *Expr);
1055
1056 void Profile(FoldingSetNodeID &ID) const;
1057
1058 // Fold - If possible, fold this to a simpler init. Return this if not
1059 // possible to fold.
1060 Init *Fold() const;
1061
1062 bool isComplete() const override { return false; }
1063
1064 Init *resolveReferences(Resolver &R) const override;
1065
1066 Init *getBit(unsigned Bit) const override;
1067
1068 std::string getAsString() const override;
1069};
1070
1071/// 'Opcode' - Represent a reference to an entire variable object.
1072class VarInit : public TypedInit {
1073 Init *VarName;
1074
1075 explicit VarInit(Init *VN, RecTy *T)
1076 : TypedInit(IK_VarInit, T), VarName(VN) {}
1077
1078public:
1079 VarInit(const VarInit &) = delete;
1080 VarInit &operator=(const VarInit &) = delete;
1081
1082 static bool classof(const Init *I) {
1083 return I->getKind() == IK_VarInit;
1084 }
1085
1086 static VarInit *get(StringRef VN, RecTy *T);
1087 static VarInit *get(Init *VN, RecTy *T);
1088
1089 StringRef getName() const;
1090 Init *getNameInit() const { return VarName; }
1091
1092 std::string getNameInitAsString() const {
1093 return getNameInit()->getAsUnquotedString();
1094 }
1095
1096 /// This method is used by classes that refer to other
1097 /// variables which may not be defined at the time they expression is formed.
1098 /// If a value is set for the variable later, this method will be called on
1099 /// users of the value to allow the value to propagate out.
1100 ///
1101 Init *resolveReferences(Resolver &R) const override;
1102
1103 Init *getBit(unsigned Bit) const override;
1104
1105 std::string getAsString() const override { return std::string(getName()); }
1106};
1107
1108/// Opcode{0} - Represent access to one bit of a variable or field.
1109class VarBitInit final : public TypedInit {
1110 TypedInit *TI;
1111 unsigned Bit;
1112
1113 VarBitInit(TypedInit *T, unsigned B)
1114 : TypedInit(IK_VarBitInit, BitRecTy::get()), TI(T), Bit(B) {
1115 assert(T->getType() &&(static_cast<void> (0))
1116 (isa<IntRecTy>(T->getType()) ||(static_cast<void> (0))
1117 (isa<BitsRecTy>(T->getType()) &&(static_cast<void> (0))
1118 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&(static_cast<void> (0))
1119 "Illegal VarBitInit expression!")(static_cast<void> (0));
1120 }
1121
1122public:
1123 VarBitInit(const VarBitInit &) = delete;
1124 VarBitInit &operator=(const VarBitInit &) = delete;
1125
1126 static bool classof(const Init *I) {
1127 return I->getKind() == IK_VarBitInit;
1128 }
1129
1130 static VarBitInit *get(TypedInit *T, unsigned B);
1131
1132 Init *getBitVar() const { return TI; }
1133 unsigned getBitNum() const { return Bit; }
1134
1135 std::string getAsString() const override;
1136 Init *resolveReferences(Resolver &R) const override;
1137
1138 Init *getBit(unsigned B) const override {
1139 assert(B < 1 && "Bit index out of range!")(static_cast<void> (0));
1140 return const_cast<VarBitInit*>(this);
1141 }
1142};
1143
1144/// List[4] - Represent access to one element of a var or
1145/// field.
1146class VarListElementInit : public TypedInit {
1147 TypedInit *TI;
1148 unsigned Element;
1149
1150 VarListElementInit(TypedInit *T, unsigned E)
1151 : TypedInit(IK_VarListElementInit,
1152 cast<ListRecTy>(T->getType())->getElementType()),
1153 TI(T), Element(E) {
1154 assert(T->getType() && isa<ListRecTy>(T->getType()) &&(static_cast<void> (0))
1155 "Illegal VarBitInit expression!")(static_cast<void> (0));
1156 }
1157
1158public:
1159 VarListElementInit(const VarListElementInit &) = delete;
1160 VarListElementInit &operator=(const VarListElementInit &) = delete;
1161
1162 static bool classof(const Init *I) {
1163 return I->getKind() == IK_VarListElementInit;
1164 }
1165
1166 static VarListElementInit *get(TypedInit *T, unsigned E);
1167
1168 TypedInit *getVariable() const { return TI; }
1169 unsigned getElementNum() const { return Element; }
1170
1171 std::string getAsString() const override;
1172 Init *resolveReferences(Resolver &R) const override;
1173
1174 Init *getBit(unsigned Bit) const override;
1175};
1176
1177/// AL - Represent a reference to a 'def' in the description
1178class DefInit : public TypedInit {
1179 friend class Record;
1180
1181 Record *Def;
1182
1183 explicit DefInit(Record *D);
1184
1185public:
1186 DefInit(const DefInit &) = delete;
1187 DefInit &operator=(const DefInit &) = delete;
1188
1189 static bool classof(const Init *I) {
1190 return I->getKind() == IK_DefInit;
1191 }
1192
1193 static DefInit *get(Record*);
1194
1195 Init *convertInitializerTo(RecTy *Ty) const override;
1196
1197 Record *getDef() const { return Def; }
1198
1199 //virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits);
1200
1201 RecTy *getFieldType(StringInit *FieldName) const override;
1202
1203 bool isConcrete() const override { return true; }
1204 std::string getAsString() const override;
1205
1206 Init *getBit(unsigned Bit) const override {
1207 llvm_unreachable("Illegal bit reference off def")__builtin_unreachable();
1208 }
1209};
1210
1211/// classname<targs...> - Represent an uninstantiated anonymous class
1212/// instantiation.
1213class VarDefInit final : public TypedInit, public FoldingSetNode,
1214 public TrailingObjects<VarDefInit, Init *> {
1215 Record *Class;
1216 DefInit *Def = nullptr; // after instantiation
1217 unsigned NumArgs;
1218
1219 explicit VarDefInit(Record *Class, unsigned N)
1220 : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), NumArgs(N) {}
1221
1222 DefInit *instantiate();
1223
1224public:
1225 VarDefInit(const VarDefInit &) = delete;
1226 VarDefInit &operator=(const VarDefInit &) = delete;
1227
1228 // Do not use sized deallocation due to trailing objects.
1229 void operator delete(void *p) { ::operator delete(p); }
1230
1231 static bool classof(const Init *I) {
1232 return I->getKind() == IK_VarDefInit;
1233 }
1234 static VarDefInit *get(Record *Class, ArrayRef<Init *> Args);
1235
1236 void Profile(FoldingSetNodeID &ID) const;
1237
1238 Init *resolveReferences(Resolver &R) const override;
1239 Init *Fold() const;
1240
1241 std::string getAsString() const override;
1242
1243 Init *getArg(unsigned i) const {
1244 assert(i < NumArgs && "Argument index out of range!")(static_cast<void> (0));
1245 return getTrailingObjects<Init *>()[i];
1246 }
1247
1248 using const_iterator = Init *const *;
1249
1250 const_iterator args_begin() const { return getTrailingObjects<Init *>(); }
1251 const_iterator args_end () const { return args_begin() + NumArgs; }
1252
1253 size_t args_size () const { return NumArgs; }
1254 bool args_empty() const { return NumArgs == 0; }
1255
1256 ArrayRef<Init *> args() const { return makeArrayRef(args_begin(), NumArgs); }
1257
1258 Init *getBit(unsigned Bit) const override {
1259 llvm_unreachable("Illegal bit reference off anonymous def")__builtin_unreachable();
1260 }
1261};
1262
1263/// X.Y - Represent a reference to a subfield of a variable
1264class FieldInit : public TypedInit {
1265 Init *Rec; // Record we are referring to
1266 StringInit *FieldName; // Field we are accessing
1267
1268 FieldInit(Init *R, StringInit *FN)
1269 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1270#ifndef NDEBUG1
1271 if (!getType()) {
1272 llvm::errs() << "In Record = " << Rec->getAsString()
1273 << ", got FieldName = " << *FieldName
1274 << " with non-record type!\n";
1275 llvm_unreachable("FieldInit with non-record type!")__builtin_unreachable();
1276 }
1277#endif
1278 }
1279
1280public:
1281 FieldInit(const FieldInit &) = delete;
1282 FieldInit &operator=(const FieldInit &) = delete;
1283
1284 static bool classof(const Init *I) {
1285 return I->getKind() == IK_FieldInit;
1286 }
1287
1288 static FieldInit *get(Init *R, StringInit *FN);
1289
1290 Init *getRecord() const { return Rec; }
1291 StringInit *getFieldName() const { return FieldName; }
1292
1293 Init *getBit(unsigned Bit) const override;
1294
1295 Init *resolveReferences(Resolver &R) const override;
1296 Init *Fold(Record *CurRec) const;
1297
1298 bool isConcrete() const override;
1299 std::string getAsString() const override {
1300 return Rec->getAsString() + "." + FieldName->getValue().str();
1301 }
1302};
1303
1304/// (v a, b) - Represent a DAG tree value. DAG inits are required
1305/// to have at least one value then a (possibly empty) list of arguments. Each
1306/// argument can have a name associated with it.
1307class DagInit final : public TypedInit, public FoldingSetNode,
1308 public TrailingObjects<DagInit, Init *, StringInit *> {
1309 friend TrailingObjects;
1310
1311 Init *Val;
1312 StringInit *ValName;
1313 unsigned NumArgs;
1314 unsigned NumArgNames;
1315
1316 DagInit(Init *V, StringInit *VN, unsigned NumArgs, unsigned NumArgNames)
1317 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1318 NumArgs(NumArgs), NumArgNames(NumArgNames) {}
1319
1320 size_t numTrailingObjects(OverloadToken<Init *>) const { return NumArgs; }
1321
1322public:
1323 DagInit(const DagInit &) = delete;
1324 DagInit &operator=(const DagInit &) = delete;
1325
1326 static bool classof(const Init *I) {
1327 return I->getKind() == IK_DagInit;
1328 }
1329
1330 static DagInit *get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1331 ArrayRef<StringInit*> NameRange);
1332 static DagInit *get(Init *V, StringInit *VN,
1333 ArrayRef<std::pair<Init*, StringInit*>> Args);
1334
1335 void Profile(FoldingSetNodeID &ID) const;
1336
1337 Init *getOperator() const { return Val; }
1338 Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const;
1339
1340 StringInit *getName() const { return ValName; }
1341
1342 StringRef getNameStr() const {
1343 return ValName ? ValName->getValue() : StringRef();
1344 }
1345
1346 unsigned getNumArgs() const { return NumArgs; }
1347
1348 Init *getArg(unsigned Num) const {
1349 assert(Num < NumArgs && "Arg number out of range!")(static_cast<void> (0));
1350 return getTrailingObjects<Init *>()[Num];
1351 }
1352
1353 StringInit *getArgName(unsigned Num) const {
1354 assert(Num < NumArgNames && "Arg number out of range!")(static_cast<void> (0));
1355 return getTrailingObjects<StringInit *>()[Num];
1356 }
1357
1358 StringRef getArgNameStr(unsigned Num) const {
1359 StringInit *Init = getArgName(Num);
1360 return Init ? Init->getValue() : StringRef();
1361 }
1362
1363 ArrayRef<Init *> getArgs() const {
1364 return makeArrayRef(getTrailingObjects<Init *>(), NumArgs);
1365 }
1366
1367 ArrayRef<StringInit *> getArgNames() const {
1368 return makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames);
1369 }
1370
1371 Init *resolveReferences(Resolver &R) const override;
1372
1373 bool isConcrete() const override;
1374 std::string getAsString() const override;
1375
1376 using const_arg_iterator = SmallVectorImpl<Init*>::const_iterator;
1377 using const_name_iterator = SmallVectorImpl<StringInit*>::const_iterator;
1378
1379 inline const_arg_iterator arg_begin() const { return getArgs().begin(); }
1380 inline const_arg_iterator arg_end () const { return getArgs().end(); }
1381
1382 inline size_t arg_size () const { return NumArgs; }
1383 inline bool arg_empty() const { return NumArgs == 0; }
1384
1385 inline const_name_iterator name_begin() const { return getArgNames().begin();}
1386 inline const_name_iterator name_end () const { return getArgNames().end(); }
1387
1388 inline size_t name_size () const { return NumArgNames; }
1389 inline bool name_empty() const { return NumArgNames == 0; }
1390
1391 Init *getBit(unsigned Bit) const override {
1392 llvm_unreachable("Illegal bit reference off dag")__builtin_unreachable();
1393 }
1394};
1395
1396//===----------------------------------------------------------------------===//
1397// High-Level Classes
1398//===----------------------------------------------------------------------===//
1399
1400/// This class represents a field in a record, including its name, type,
1401/// value, and source location.
1402class RecordVal {
1403 friend class Record;
1404
1405public:
1406 enum FieldKind {
1407 FK_Normal, // A normal record field.
1408 FK_NonconcreteOK, // A field that can be nonconcrete ('field' keyword).
1409 FK_TemplateArg, // A template argument.
1410 };
1411
1412private:
1413 Init *Name;
1414 SMLoc Loc; // Source location of definition of name.
1415 PointerIntPair<RecTy *, 2, FieldKind> TyAndKind;
1416 Init *Value;
1417
1418public:
1419 RecordVal(Init *N, RecTy *T, FieldKind K);
1420 RecordVal(Init *N, SMLoc Loc, RecTy *T, FieldKind K);
1421
1422 /// Get the name of the field as a StringRef.
1423 StringRef getName() const;
1424
1425 /// Get the name of the field as an Init.
1426 Init *getNameInit() const { return Name; }
1427
1428 /// Get the name of the field as a std::string.
1429 std::string getNameInitAsString() const {
1430 return getNameInit()->getAsUnquotedString();
1431 }
1432
1433 /// Get the source location of the point where the field was defined.
1434 const SMLoc &getLoc() const { return Loc; }
1435
1436 /// Is this a field where nonconcrete values are okay?
1437 bool isNonconcreteOK() const {
1438 return TyAndKind.getInt() == FK_NonconcreteOK;
1439 }
1440
1441 /// Is this a template argument?
1442 bool isTemplateArg() const {
1443 return TyAndKind.getInt() == FK_TemplateArg;
1444 }
1445
1446 /// Get the type of the field value as a RecTy.
1447 RecTy *getType() const { return TyAndKind.getPointer(); }
1448
1449 /// Get the type of the field for printing purposes.
1450 std::string getPrintType() const;
1451
1452 /// Get the value of the field as an Init.
1453 Init *getValue() const { return Value; }
1454
1455 /// Set the value of the field from an Init.
1456 bool setValue(Init *V);
1457
1458 /// Set the value and source location of the field.
1459 bool setValue(Init *V, SMLoc NewLoc);
1460
1461 void dump() const;
1462
1463 /// Print the value to an output stream, possibly with a semicolon.
1464 void print(raw_ostream &OS, bool PrintSem = true) const;
1465};
1466
1467inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1468 RV.print(OS << " ");
1469 return OS;
1470}
1471
1472class Record {
1473public:
1474 struct AssertionInfo {
1475 SMLoc Loc;
1476 Init *Condition;
1477 Init *Message;
1478
1479 // User-defined constructor to support std::make_unique(). It can be
1480 // removed in C++20 when braced initialization is supported.
1481 AssertionInfo(SMLoc Loc, Init *Condition, Init *Message)
1482 : Loc(Loc), Condition(Condition), Message(Message) {}
1483 };
1484
1485private:
1486 static unsigned LastID;
1487
1488 Init *Name;
1489 // Location where record was instantiated, followed by the location of
1490 // multiclass prototypes used.
1491 SmallVector<SMLoc, 4> Locs;
1492 SmallVector<Init *, 0> TemplateArgs;
1493 SmallVector<RecordVal, 0> Values;
1494 SmallVector<AssertionInfo, 0> Assertions;
1495
1496 // All superclasses in the inheritance forest in post-order (yes, it
1497 // must be a forest; diamond-shaped inheritance is not allowed).
1498 SmallVector<std::pair<Record *, SMRange>, 0> SuperClasses;
1499
1500 // Tracks Record instances. Not owned by Record.
1501 RecordKeeper &TrackedRecords;
1502
1503 // The DefInit corresponding to this record.
1504 DefInit *CorrespondingDefInit = nullptr;
1505
1506 // Unique record ID.
1507 unsigned ID;
1508
1509 bool IsAnonymous;
1510 bool IsClass;
1511
1512 void checkName();
1513
1514public:
1515 // Constructs a record.
1516 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1517 bool Anonymous = false, bool Class = false)
1518 : Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records),
1519 ID(LastID++), IsAnonymous(Anonymous), IsClass(Class) {
1520 checkName();
1521 }
1522
1523 explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1524 bool Class = false)
1525 : Record(StringInit::get(N), locs, records, false, Class) {}
1526
1527 // When copy-constructing a Record, we must still guarantee a globally unique
1528 // ID number. Don't copy CorrespondingDefInit either, since it's owned by the
1529 // original record. All other fields can be copied normally.
1530 Record(const Record &O)
1531 : Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1532 Values(O.Values), Assertions(O.Assertions), SuperClasses(O.SuperClasses),
1533 TrackedRecords(O.TrackedRecords), ID(LastID++),
1534 IsAnonymous(O.IsAnonymous), IsClass(O.IsClass) { }
1535
1536 static unsigned getNewUID() { return LastID++; }
1537
1538 unsigned getID() const { return ID; }
1539
1540 StringRef getName() const { return cast<StringInit>(Name)->getValue(); }
1541
1542 Init *getNameInit() const {
1543 return Name;
1544 }
1545
1546 const std::string getNameInitAsString() const {
1547 return getNameInit()->getAsUnquotedString();
1548 }
1549
1550 void setName(Init *Name); // Also updates RecordKeeper.
1551
1552 ArrayRef<SMLoc> getLoc() const { return Locs; }
1553 void appendLoc(SMLoc Loc) { Locs.push_back(Loc); }
1554
1555 // Make the type that this record should have based on its superclasses.
1556 RecordRecTy *getType();
1557
1558 /// get the corresponding DefInit.
1559 DefInit *getDefInit();
1560
1561 bool isClass() const { return IsClass; }
1562
1563 ArrayRef<Init *> getTemplateArgs() const {
1564 return TemplateArgs;
1565 }
1566
1567 ArrayRef<RecordVal> getValues() const { return Values; }
1568
1569 ArrayRef<AssertionInfo> getAssertions() const { return Assertions; }
1570
1571 ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
1572 return SuperClasses;
1573 }
1574
1575 /// Determine whether this record has the specified direct superclass.
1576 bool hasDirectSuperClass(const Record *SuperClass) const;
1577
1578 /// Append the direct superclasses of this record to Classes.
1579 void getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const;
1580
1581 bool isTemplateArg(Init *Name) const {
1582 return llvm::is_contained(TemplateArgs, Name);
1583 }
1584
1585 const RecordVal *getValue(const Init *Name) const {
1586 for (const RecordVal &Val : Values)
1587 if (Val.Name == Name) return &Val;
1588 return nullptr;
1589 }
1590
1591 const RecordVal *getValue(StringRef Name) const {
1592 return getValue(StringInit::get(Name));
1593 }
1594
1595 RecordVal *getValue(const Init *Name) {
1596 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1597 }
1598
1599 RecordVal *getValue(StringRef Name) {
1600 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1601 }
1602
1603 void addTemplateArg(Init *Name) {
1604 assert(!isTemplateArg(Name) && "Template arg already defined!")(static_cast<void> (0));
1605 TemplateArgs.push_back(Name);
1606 }
1607
1608 void addValue(const RecordVal &RV) {
1609 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!")(static_cast<void> (0));
1610 Values.push_back(RV);
1611 }
1612
1613 void removeValue(Init *Name) {
1614 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1615 if (Values[i].getNameInit() == Name) {
1616 Values.erase(Values.begin()+i);
1617 return;
1618 }
1619 llvm_unreachable("Cannot remove an entry that does not exist!")__builtin_unreachable();
1620 }
1621
1622 void removeValue(StringRef Name) {
1623 removeValue(StringInit::get(Name));
1624 }
1625
1626 void addAssertion(SMLoc Loc, Init *Condition, Init *Message) {
1627 Assertions.push_back(AssertionInfo(Loc, Condition, Message));
1628 }
1629
1630 void appendAssertions(const Record *Rec) {
1631 Assertions.append(Rec->Assertions);
1632 }
1633
1634 void checkRecordAssertions();
1635
1636 bool isSubClassOf(const Record *R) const {
1637 for (const auto &SCPair : SuperClasses)
1638 if (SCPair.first == R)
1639 return true;
1640 return false;
1641 }
1642
1643 bool isSubClassOf(StringRef Name) const {
1644 for (const auto &SCPair : SuperClasses) {
14
Assuming '__begin2' is equal to '__end2'
19
Assuming '__begin2' is equal to '__end2'
24
Assuming '__begin2' is not equal to '__end2'
1645 if (const auto *SI
25.1
'SI' is null
25.1
'SI' is null
25.1
'SI' is null
25.1
'SI' is null
= dyn_cast<StringInit>(SCPair.first->getNameInit())) {
25
Assuming the object is not a 'StringInit'
26
Taking false branch
1646 if (SI->getValue() == Name)
1647 return true;
1648 } else if (SCPair.first->getNameInitAsString() == Name) {
27
Assuming the condition is true
28
Taking true branch
1649 return true;
29
Returning the value 1, which participates in a condition later
1650 }
1651 }
1652 return false;
15
Returning zero, which participates in a condition later
20
Returning zero, which participates in a condition later
1653 }
1654
1655 void addSuperClass(Record *R, SMRange Range) {
1656 assert(!CorrespondingDefInit &&(static_cast<void> (0))
1657 "changing type of record after it has been referenced")(static_cast<void> (0));
1658 assert(!isSubClassOf(R) && "Already subclassing record!")(static_cast<void> (0));
1659 SuperClasses.push_back(std::make_pair(R, Range));
1660 }
1661
1662 /// If there are any field references that refer to fields
1663 /// that have been filled in, we can propagate the values now.
1664 ///
1665 /// This is a final resolve: any error messages, e.g. due to undefined
1666 /// !cast references, are generated now.
1667 void resolveReferences(Init *NewName = nullptr);
1668
1669 /// Apply the resolver to the name of the record as well as to the
1670 /// initializers of all fields of the record except SkipVal.
1671 ///
1672 /// The resolver should not resolve any of the fields itself, to avoid
1673 /// recursion / infinite loops.
1674 void resolveReferences(Resolver &R, const RecordVal *SkipVal = nullptr);
1675
1676 RecordKeeper &getRecords() const {
1677 return TrackedRecords;
1678 }
1679
1680 bool isAnonymous() const {
1681 return IsAnonymous;
1682 }
1683
1684 void dump() const;
1685
1686 //===--------------------------------------------------------------------===//
1687 // High-level methods useful to tablegen back-ends
1688 //
1689
1690 ///Return the source location for the named field.
1691 SMLoc getFieldLoc(StringRef FieldName) const;
1692
1693 /// Return the initializer for a value with the specified name,
1694 /// or throw an exception if the field does not exist.
1695 Init *getValueInit(StringRef FieldName) const;
1696
1697 /// Return true if the named field is unset.
1698 bool isValueUnset(StringRef FieldName) const {
1699 return isa<UnsetInit>(getValueInit(FieldName));
1700 }
1701
1702 /// This method looks up the specified field and returns
1703 /// its value as a string, throwing an exception if the field does not exist
1704 /// or if the value is not a string.
1705 StringRef getValueAsString(StringRef FieldName) const;
1706
1707 /// This method looks up the specified field and returns
1708 /// its value as a string, throwing an exception if the field if the value is
1709 /// not a string and llvm::Optional() if the field does not exist.
1710 llvm::Optional<StringRef> getValueAsOptionalString(StringRef FieldName) const;
1711
1712 /// This method looks up the specified field and returns
1713 /// its value as a BitsInit, throwing an exception if the field does not exist
1714 /// or if the value is not the right type.
1715 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1716
1717 /// This method looks up the specified field and returns
1718 /// its value as a ListInit, throwing an exception if the field does not exist
1719 /// or if the value is not the right type.
1720 ListInit *getValueAsListInit(StringRef FieldName) const;
1721
1722 /// This method looks up the specified field and
1723 /// returns its value as a vector of records, throwing an exception if the
1724 /// field does not exist or if the value is not the right type.
1725 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1726
1727 /// This method looks up the specified field and
1728 /// returns its value as a vector of integers, throwing an exception if the
1729 /// field does not exist or if the value is not the right type.
1730 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1731
1732 /// This method looks up the specified field and
1733 /// returns its value as a vector of strings, throwing an exception if the
1734 /// field does not exist or if the value is not the right type.
1735 std::vector<StringRef> getValueAsListOfStrings(StringRef FieldName) const;
1736
1737 /// This method looks up the specified field and returns its
1738 /// value as a Record, throwing an exception if the field does not exist or if
1739 /// the value is not the right type.
1740 Record *getValueAsDef(StringRef FieldName) const;
1741
1742 /// This method looks up the specified field and returns its value as a
1743 /// Record, returning null if the field exists but is "uninitialized"
1744 /// (i.e. set to `?`), and throwing an exception if the field does not
1745 /// exist or if its value is not the right type.
1746 Record *getValueAsOptionalDef(StringRef FieldName) const;
1747
1748 /// This method looks up the specified field and returns its
1749 /// value as a bit, throwing an exception if the field does not exist or if
1750 /// the value is not the right type.
1751 bool getValueAsBit(StringRef FieldName) const;
1752
1753 /// This method looks up the specified field and
1754 /// returns its value as a bit. If the field is unset, sets Unset to true and
1755 /// returns false.
1756 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1757
1758 /// This method looks up the specified field and returns its
1759 /// value as an int64_t, throwing an exception if the field does not exist or
1760 /// if the value is not the right type.
1761 int64_t getValueAsInt(StringRef FieldName) const;
1762
1763 /// This method looks up the specified field and returns its
1764 /// value as an Dag, throwing an exception if the field does not exist or if
1765 /// the value is not the right type.
1766 DagInit *getValueAsDag(StringRef FieldName) const;
1767};
1768
1769raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1770
1771class RecordKeeper {
1772 friend class RecordRecTy;
1773
1774 using RecordMap = std::map<std::string, std::unique_ptr<Record>, std::less<>>;
1775 using GlobalMap = std::map<std::string, Init *, std::less<>>;
1776
1777 std::string InputFilename;
1778 RecordMap Classes, Defs;
1779 mutable StringMap<std::vector<Record *>> ClassRecordsMap;
1780 FoldingSet<RecordRecTy> RecordTypePool;
1781 std::map<std::string, Init *, std::less<>> ExtraGlobals;
1782 unsigned AnonCounter = 0;
1783
1784 // These members are for the phase timing feature. We need a timer group,
1785 // the last timer started, and a flag to say whether the last timer
1786 // is the special "backend overall timer."
1787 TimerGroup *TimingGroup = nullptr;
1788 Timer *LastTimer = nullptr;
1789 bool BackendTimer = false;
1790
1791public:
1792 /// Get the main TableGen input file's name.
1793 const std::string getInputFilename() const { return InputFilename; }
1794
1795 /// Get the map of classes.
1796 const RecordMap &getClasses() const { return Classes; }
1797
1798 /// Get the map of records (defs).
1799 const RecordMap &getDefs() const { return Defs; }
1800
1801 /// Get the map of global variables.
1802 const GlobalMap &getGlobals() const { return ExtraGlobals; }
1803
1804 /// Get the class with the specified name.
1805 Record *getClass(StringRef Name) const {
1806 auto I = Classes.find(Name);
1807 return I == Classes.end() ? nullptr : I->second.get();
1808 }
1809
1810 /// Get the concrete record with the specified name.
1811 Record *getDef(StringRef Name) const {
1812 auto I = Defs.find(Name);
1813 return I == Defs.end() ? nullptr : I->second.get();
1814 }
1815
1816 /// Get the \p Init value of the specified global variable.
1817 Init *getGlobal(StringRef Name) const {
1818 if (Record *R = getDef(Name))
1819 return R->getDefInit();
1820 auto It = ExtraGlobals.find(Name);
1821 return It == ExtraGlobals.end() ? nullptr : It->second;
1822 }
1823
1824 void saveInputFilename(std::string Filename) {
1825 InputFilename = Filename;
1826 }
1827
1828 void addClass(std::unique_ptr<Record> R) {
1829 bool Ins = Classes.insert(std::make_pair(std::string(R->getName()),
1830 std::move(R))).second;
1831 (void)Ins;
1832 assert(Ins && "Class already exists")(static_cast<void> (0));
1833 }
1834
1835 void addDef(std::unique_ptr<Record> R) {
1836 bool Ins = Defs.insert(std::make_pair(std::string(R->getName()),
1837 std::move(R))).second;
1838 (void)Ins;
1839 assert(Ins && "Record already exists")(static_cast<void> (0));
1840 }
1841
1842 void addExtraGlobal(StringRef Name, Init *I) {
1843 bool Ins = ExtraGlobals.insert(std::make_pair(std::string(Name), I)).second;
1844 (void)Ins;
1845 assert(!getDef(Name))(static_cast<void> (0));
1846 assert(Ins && "Global already exists")(static_cast<void> (0));
1847 }
1848
1849 Init *getNewAnonymousName();
1850
1851 /// Start phase timing; called if the --time-phases option is specified.
1852 void startPhaseTiming() {
1853 TimingGroup = new TimerGroup("TableGen", "TableGen Phase Timing");
1854 }
1855
1856 /// Start timing a phase. Automatically stops any previous phase timer.
1857 void startTimer(StringRef Name);
1858
1859 /// Stop timing a phase.
1860 void stopTimer();
1861
1862 /// Start timing the overall backend. If the backend itself starts a timer,
1863 /// then this timer is cleared.
1864 void startBackendTimer(StringRef Name);
1865
1866 /// Stop timing the overall backend.
1867 void stopBackendTimer();
1868
1869 /// Stop phase timing and print the report.
1870 void stopPhaseTiming() {
1871 if (TimingGroup)
1872 delete TimingGroup;
1873 }
1874
1875 //===--------------------------------------------------------------------===//
1876 // High-level helper methods, useful for tablegen backends.
1877
1878 /// Get all the concrete records that inherit from the one specified
1879 /// class. The class must be defined.
1880 std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
1881
1882 /// Get all the concrete records that inherit from all the specified
1883 /// classes. The classes must be defined.
1884 std::vector<Record *> getAllDerivedDefinitions(
1885 ArrayRef<StringRef> ClassNames) const;
1886
1887 void dump() const;
1888};
1889
1890/// Sorting predicate to sort record pointers by name.
1891struct LessRecord {
1892 bool operator()(const Record *Rec1, const Record *Rec2) const {
1893 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1894 }
1895};
1896
1897/// Sorting predicate to sort record pointers by their
1898/// unique ID. If you just need a deterministic order, use this, since it
1899/// just compares two `unsigned`; the other sorting predicates require
1900/// string manipulation.
1901struct LessRecordByID {
1902 bool operator()(const Record *LHS, const Record *RHS) const {
1903 return LHS->getID() < RHS->getID();
1904 }
1905};
1906
1907/// Sorting predicate to sort record pointers by their
1908/// name field.
1909struct LessRecordFieldName {
1910 bool operator()(const Record *Rec1, const Record *Rec2) const {
1911 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1912 }
1913};
1914
1915struct LessRecordRegister {
1916 struct RecordParts {
1917 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1918
1919 RecordParts(StringRef Rec) {
1920 if (Rec.empty())
1921 return;
1922
1923 size_t Len = 0;
1924 const char *Start = Rec.data();
1925 const char *Curr = Start;
1926 bool IsDigitPart = isDigit(Curr[0]);
1927 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1928 bool IsDigit = isDigit(Curr[I]);
1929 if (IsDigit != IsDigitPart) {
1930 Parts.push_back(std::make_pair(IsDigitPart, StringRef(Start, Len)));
1931 Len = 0;
1932 Start = &Curr[I];
1933 IsDigitPart = isDigit(Curr[I]);
1934 }
1935 }
1936 // Push the last part.
1937 Parts.push_back(std::make_pair(IsDigitPart, StringRef(Start, Len)));
1938 }
1939
1940 size_t size() { return Parts.size(); }
1941
1942 std::pair<bool, StringRef> getPart(size_t i) {
1943 assert (i < Parts.size() && "Invalid idx!")(static_cast<void> (0));
1944 return Parts[i];
1945 }
1946 };
1947
1948 bool operator()(const Record *Rec1, const Record *Rec2) const {
1949 RecordParts LHSParts(StringRef(Rec1->getName()));
1950 RecordParts RHSParts(StringRef(Rec2->getName()));
1951
1952 size_t LHSNumParts = LHSParts.size();
1953 size_t RHSNumParts = RHSParts.size();
1954 assert (LHSNumParts && RHSNumParts && "Expected at least one part!")(static_cast<void> (0));
1955
1956 if (LHSNumParts != RHSNumParts)
1957 return LHSNumParts < RHSNumParts;
1958
1959 // We expect the registers to be of the form [_a-zA-Z]+([0-9]*[_a-zA-Z]*)*.
1960 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1961 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1962 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1963 // Expect even part to always be alpha.
1964 assert (LHSPart.first == false && RHSPart.first == false &&(static_cast<void> (0))
1965 "Expected both parts to be alpha.")(static_cast<void> (0));
1966 if (int Res = LHSPart.second.compare(RHSPart.second))
1967 return Res < 0;
1968 }
1969 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1970 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1971 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1972 // Expect odd part to always be numeric.
1973 assert (LHSPart.first == true && RHSPart.first == true &&(static_cast<void> (0))
1974 "Expected both parts to be numeric.")(static_cast<void> (0));
1975 if (LHSPart.second.size() != RHSPart.second.size())
1976 return LHSPart.second.size() < RHSPart.second.size();
1977
1978 unsigned LHSVal, RHSVal;
1979
1980 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1981 assert(!LHSFailed && "Unable to convert LHS to integer.")(static_cast<void> (0));
1982 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1983 assert(!RHSFailed && "Unable to convert RHS to integer.")(static_cast<void> (0));
1984
1985 if (LHSVal != RHSVal)
1986 return LHSVal < RHSVal;
1987 }
1988 return LHSNumParts < RHSNumParts;
1989 }
1990};
1991
1992raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1993
1994//===----------------------------------------------------------------------===//
1995// Resolvers
1996//===----------------------------------------------------------------------===//
1997
1998/// Interface for looking up the initializer for a variable name, used by
1999/// Init::resolveReferences.
2000class Resolver {
2001 Record *CurRec;
2002 bool IsFinal = false;
2003
2004public:
2005 explicit Resolver(Record *CurRec) : CurRec(CurRec) {}
2006 virtual ~Resolver() {}
2007
2008 Record *getCurrentRecord() const { return CurRec; }
2009
2010 /// Return the initializer for the given variable name (should normally be a
2011 /// StringInit), or nullptr if the name could not be resolved.
2012 virtual Init *resolve(Init *VarName) = 0;
2013
2014 // Whether bits in a BitsInit should stay unresolved if resolving them would
2015 // result in a ? (UnsetInit). This behavior is used to represent instruction
2016 // encodings by keeping references to unset variables within a record.
2017 virtual bool keepUnsetBits() const { return false; }
2018
2019 // Whether this is the final resolve step before adding a record to the
2020 // RecordKeeper. Error reporting during resolve and related constant folding
2021 // should only happen when this is true.
2022 bool isFinal() const { return IsFinal; }
2023
2024 void setFinal(bool Final) { IsFinal = Final; }
2025};
2026
2027/// Resolve arbitrary mappings.
2028class MapResolver final : public Resolver {
2029 struct MappedValue {
2030 Init *V;
2031 bool Resolved;
2032
2033 MappedValue() : V(nullptr), Resolved(false) {}
2034 MappedValue(Init *V, bool Resolved) : V(V), Resolved(Resolved) {}
2035 };
2036
2037 DenseMap<Init *, MappedValue> Map;
2038
2039public:
2040 explicit MapResolver(Record *CurRec = nullptr) : Resolver(CurRec) {}
2041
2042 void set(Init *Key, Init *Value) { Map[Key] = {Value, false}; }
2043
2044 bool isComplete(Init *VarName) const {
2045 auto It = Map.find(VarName);
2046 assert(It != Map.end() && "key must be present in map")(static_cast<void> (0));
2047 return It->second.V->isComplete();
2048 }
2049
2050 Init *resolve(Init *VarName) override;
2051};
2052
2053/// Resolve all variables from a record except for unset variables.
2054class RecordResolver final : public Resolver {
2055 DenseMap<Init *, Init *> Cache;
2056 SmallVector<Init *, 4> Stack;
2057 Init *Name = nullptr;
2058
2059public:
2060 explicit RecordResolver(Record &R) : Resolver(&R) {}
2061
2062 void setName(Init *NewName) { Name = NewName; }
2063
2064 Init *resolve(Init *VarName) override;
2065
2066 bool keepUnsetBits() const override { return true; }
2067};
2068
2069/// Delegate resolving to a sub-resolver, but shadow some variable names.
2070class ShadowResolver final : public Resolver {
2071 Resolver &R;
2072 DenseSet<Init *> Shadowed;
2073
2074public:
2075 explicit ShadowResolver(Resolver &R)
2076 : Resolver(R.getCurrentRecord()), R(R) {
2077 setFinal(R.isFinal());
2078 }
2079
2080 void addShadow(Init *Key) { Shadowed.insert(Key); }
2081
2082 Init *resolve(Init *VarName) override {
2083 if (Shadowed.count(VarName))
2084 return nullptr;
2085 return R.resolve(VarName);
2086 }
2087};
2088
2089/// (Optionally) delegate resolving to a sub-resolver, and keep track whether
2090/// there were unresolved references.
2091class TrackUnresolvedResolver final : public Resolver {
2092 Resolver *R;
2093 bool FoundUnresolved = false;
2094
2095public:
2096 explicit TrackUnresolvedResolver(Resolver *R = nullptr)
2097 : Resolver(R ? R->getCurrentRecord() : nullptr), R(R) {}
2098
2099 bool foundUnresolved() const { return FoundUnresolved; }
2100
2101 Init *resolve(Init *VarName) override;
2102};
2103
2104/// Do not resolve anything, but keep track of whether a given variable was
2105/// referenced.
2106class HasReferenceResolver final : public Resolver {
2107 Init *VarNameToTrack;
2108 bool Found = false;
2109
2110public:
2111 explicit HasReferenceResolver(Init *VarNameToTrack)
2112 : Resolver(nullptr), VarNameToTrack(VarNameToTrack) {}
2113
2114 bool found() const { return Found; }
2115
2116 Init *resolve(Init *VarName) override;
2117};
2118
2119void EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS);
2120void EmitJSON(RecordKeeper &RK, raw_ostream &OS);
2121
2122} // end namespace llvm
2123
2124#endif // LLVM_TABLEGEN_RECORD_H

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/utils/TableGen/CodeGenDAGPatterns.h

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

/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_tree.h

1// RB tree implementation -*- C++ -*-
2
3// Copyright (C) 2001-2020 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1996,1997
28 * Silicon Graphics Computer Systems, Inc.
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1994
40 * Hewlett-Packard Company
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Hewlett-Packard Company makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 *
50 *
51 */
52
53/** @file bits/stl_tree.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{map,set}
56 */
57
58#ifndef _STL_TREE_H1
59#define _STL_TREE_H1 1
60
61#pragma GCC system_header
62
63#include <bits/stl_algobase.h>
64#include <bits/allocator.h>
65#include <bits/stl_function.h>
66#include <bits/cpp_type_traits.h>
67#include <ext/alloc_traits.h>
68#if __cplusplus201402L >= 201103L
69# include <ext/aligned_buffer.h>
70#endif
71#if __cplusplus201402L > 201402L
72# include <bits/node_handle.h>
73#endif
74
75namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
76{
77_GLIBCXX_BEGIN_NAMESPACE_VERSION
78
79#if __cplusplus201402L > 201103L
80# define __cpp_lib_generic_associative_lookup201304 201304
81#endif
82
83 // Red-black tree class, designed for use in implementing STL
84 // associative containers (set, multiset, map, and multimap). The
85 // insertion and deletion algorithms are based on those in Cormen,
86 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
87 // 1990), except that
88 //
89 // (1) the header cell is maintained with links not only to the root
90 // but also to the leftmost node of the tree, to enable constant
91 // time begin(), and to the rightmost node of the tree, to enable
92 // linear time performance when used with the generic set algorithms
93 // (set_union, etc.)
94 //
95 // (2) when a node being deleted has two children its successor node
96 // is relinked into its place, rather than copied, so that the only
97 // iterators invalidated are those referring to the deleted node.
98
99 enum _Rb_tree_color { _S_red = false, _S_black = true };
100
101 struct _Rb_tree_node_base
102 {
103 typedef _Rb_tree_node_base* _Base_ptr;
104 typedef const _Rb_tree_node_base* _Const_Base_ptr;
105
106 _Rb_tree_color _M_color;
107 _Base_ptr _M_parent;
108 _Base_ptr _M_left;
109 _Base_ptr _M_right;
110
111 static _Base_ptr
112 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
113 {
114 while (__x->_M_left != 0) __x = __x->_M_left;
115 return __x;
116 }
117
118 static _Const_Base_ptr
119 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
120 {
121 while (__x->_M_left != 0) __x = __x->_M_left;
122 return __x;
123 }
124
125 static _Base_ptr
126 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
127 {
128 while (__x->_M_right != 0) __x = __x->_M_right;
129 return __x;
130 }
131
132 static _Const_Base_ptr
133 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
134 {
135 while (__x->_M_right != 0) __x = __x->_M_right;
136 return __x;
137 }
138 };
139
140 // Helper type offering value initialization guarantee on the compare functor.
141 template<typename _Key_compare>
142 struct _Rb_tree_key_compare
143 {
144 _Key_compare _M_key_compare;
145
146 _Rb_tree_key_compare()
147 _GLIBCXX_NOEXCEPT_IF(noexcept(is_nothrow_default_constructible<_Key_compare>
::value)
148 is_nothrow_default_constructible<_Key_compare>::value)noexcept(is_nothrow_default_constructible<_Key_compare>
::value)
149 : _M_key_compare()
150 { }
151
152 _Rb_tree_key_compare(const _Key_compare& __comp)
153 : _M_key_compare(__comp)
154 { }
155
156#if __cplusplus201402L >= 201103L
157 // Copy constructor added for consistency with C++98 mode.
158 _Rb_tree_key_compare(const _Rb_tree_key_compare&) = default;
159
160 _Rb_tree_key_compare(_Rb_tree_key_compare&& __x)
161 noexcept(is_nothrow_copy_constructible<_Key_compare>::value)
162 : _M_key_compare(__x._M_key_compare)
163 { }
164#endif
165 };
166
167 // Helper type to manage default initialization of node count and header.
168 struct _Rb_tree_header
169 {
170 _Rb_tree_node_base _M_header;
171 size_t _M_node_count; // Keeps track of size of tree.
172
173 _Rb_tree_header() _GLIBCXX_NOEXCEPTnoexcept
174 {
175 _M_header._M_color = _S_red;
176 _M_reset();
177 }
178
179#if __cplusplus201402L >= 201103L
180 _Rb_tree_header(_Rb_tree_header&& __x) noexcept
181 {
182 if (__x._M_header._M_parent != nullptr)
183 _M_move_data(__x);
184 else
185 {
186 _M_header._M_color = _S_red;
187 _M_reset();
188 }
189 }
190#endif
191
192 void
193 _M_move_data(_Rb_tree_header& __from)
194 {
195 _M_header._M_color = __from._M_header._M_color;
196 _M_header._M_parent = __from._M_header._M_parent;
197 _M_header._M_left = __from._M_header._M_left;
198 _M_header._M_right = __from._M_header._M_right;
199 _M_header._M_parent->_M_parent = &_M_header;
200 _M_node_count = __from._M_node_count;
201
202 __from._M_reset();
203 }
204
205 void
206 _M_reset()
207 {
208 _M_header._M_parent = 0;
209 _M_header._M_left = &_M_header;
210 _M_header._M_right = &_M_header;
211 _M_node_count = 0;
212 }
213 };
214
215 template<typename _Val>
216 struct _Rb_tree_node : public _Rb_tree_node_base
217 {
218 typedef _Rb_tree_node<_Val>* _Link_type;
219
220#if __cplusplus201402L < 201103L
221 _Val _M_value_field;
222
223 _Val*
224 _M_valptr()
225 { return std::__addressof(_M_value_field); }
226
227 const _Val*
228 _M_valptr() const
229 { return std::__addressof(_M_value_field); }
230#else
231 __gnu_cxx::__aligned_membuf<_Val> _M_storage;
232
233 _Val*
234 _M_valptr()
235 { return _M_storage._M_ptr(); }
236
237 const _Val*
238 _M_valptr() const
239 { return _M_storage._M_ptr(); }
240#endif
241 };
242
243 _GLIBCXX_PURE__attribute__ ((__pure__)) _Rb_tree_node_base*
244 _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
245
246 _GLIBCXX_PURE__attribute__ ((__pure__)) const _Rb_tree_node_base*
247 _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
248
249 _GLIBCXX_PURE__attribute__ ((__pure__)) _Rb_tree_node_base*
250 _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
251
252 _GLIBCXX_PURE__attribute__ ((__pure__)) const _Rb_tree_node_base*
253 _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
254
255 template<typename _Tp>
256 struct _Rb_tree_iterator
257 {
258 typedef _Tp value_type;
259 typedef _Tp& reference;
260 typedef _Tp* pointer;
261
262 typedef bidirectional_iterator_tag iterator_category;
263 typedef ptrdiff_t difference_type;
264
265 typedef _Rb_tree_iterator<_Tp> _Self;
266 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
267 typedef _Rb_tree_node<_Tp>* _Link_type;
268
269 _Rb_tree_iterator() _GLIBCXX_NOEXCEPTnoexcept
270 : _M_node() { }
271
272 explicit
273 _Rb_tree_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
274 : _M_node(__x) { }
275
276 reference
277 operator*() const _GLIBCXX_NOEXCEPTnoexcept
278 { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
279
280 pointer
281 operator->() const _GLIBCXX_NOEXCEPTnoexcept
282 { return static_cast<_Link_type> (_M_node)->_M_valptr(); }
283
284 _Self&
285 operator++() _GLIBCXX_NOEXCEPTnoexcept
286 {
287 _M_node = _Rb_tree_increment(_M_node);
288 return *this;
289 }
290
291 _Self
292 operator++(int) _GLIBCXX_NOEXCEPTnoexcept
293 {
294 _Self __tmp = *this;
295 _M_node = _Rb_tree_increment(_M_node);
296 return __tmp;
297 }
298
299 _Self&
300 operator--() _GLIBCXX_NOEXCEPTnoexcept
301 {
302 _M_node = _Rb_tree_decrement(_M_node);
303 return *this;
304 }
305
306 _Self
307 operator--(int) _GLIBCXX_NOEXCEPTnoexcept
308 {
309 _Self __tmp = *this;
310 _M_node = _Rb_tree_decrement(_M_node);
311 return __tmp;
312 }
313
314 friend bool
315 operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPTnoexcept
316 { return __x._M_node == __y._M_node; }
317
318#if ! __cpp_lib_three_way_comparison
319 friend bool
320 operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPTnoexcept
321 { return __x._M_node != __y._M_node; }
322#endif
323
324 _Base_ptr _M_node;
325 };
326
327 template<typename _Tp>
328 struct _Rb_tree_const_iterator
329 {
330 typedef _Tp value_type;
331 typedef const _Tp& reference;
332 typedef const _Tp* pointer;
333
334 typedef _Rb_tree_iterator<_Tp> iterator;
335
336 typedef bidirectional_iterator_tag iterator_category;
337 typedef ptrdiff_t difference_type;
338
339 typedef _Rb_tree_const_iterator<_Tp> _Self;
340 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
341 typedef const _Rb_tree_node<_Tp>* _Link_type;
342
343 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPTnoexcept
344 : _M_node() { }
345
346 explicit
347 _Rb_tree_const_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
348 : _M_node(__x) { }
349
350 _Rb_tree_const_iterator(const iterator& __it) _GLIBCXX_NOEXCEPTnoexcept
351 : _M_node(__it._M_node) { }
352
353 iterator
354 _M_const_cast() const _GLIBCXX_NOEXCEPTnoexcept
355 { return iterator(const_cast<typename iterator::_Base_ptr>(_M_node)); }
356
357 reference
358 operator*() const _GLIBCXX_NOEXCEPTnoexcept
359 { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
360
361 pointer
362 operator->() const _GLIBCXX_NOEXCEPTnoexcept
363 { return static_cast<_Link_type>(_M_node)->_M_valptr(); }
364
365 _Self&
366 operator++() _GLIBCXX_NOEXCEPTnoexcept
367 {
368 _M_node = _Rb_tree_increment(_M_node);
369 return *this;
370 }
371
372 _Self
373 operator++(int) _GLIBCXX_NOEXCEPTnoexcept
374 {
375 _Self __tmp = *this;
376 _M_node = _Rb_tree_increment(_M_node);
377 return __tmp;
378 }
379
380 _Self&
381 operator--() _GLIBCXX_NOEXCEPTnoexcept
382 {
383 _M_node = _Rb_tree_decrement(_M_node);
384 return *this;
385 }
386
387 _Self
388 operator--(int) _GLIBCXX_NOEXCEPTnoexcept
389 {
390 _Self __tmp = *this;
391 _M_node = _Rb_tree_decrement(_M_node);
392 return __tmp;
393 }
394
395 friend bool
396 operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPTnoexcept
397 { return __x._M_node == __y._M_node; }
34
Assuming '__x._M_node' is not equal to '__y._M_node'
35
Returning zero, which participates in a condition later
398
399#if ! __cpp_lib_three_way_comparison
400 friend bool
401 operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPTnoexcept
402 { return __x._M_node != __y._M_node; }
403#endif
404
405 _Base_ptr _M_node;
406 };
407
408 void
409 _Rb_tree_insert_and_rebalance(const bool __insert_left,
410 _Rb_tree_node_base* __x,
411 _Rb_tree_node_base* __p,
412 _Rb_tree_node_base& __header) throw ();
413
414 _Rb_tree_node_base*
415 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
416 _Rb_tree_node_base& __header) throw ();
417
418#if __cplusplus201402L >= 201402L
419 template<typename _Cmp, typename _SfinaeType, typename = __void_t<>>
420 struct __has_is_transparent
421 { };
422
423 template<typename _Cmp, typename _SfinaeType>
424 struct __has_is_transparent<_Cmp, _SfinaeType,
425 __void_t<typename _Cmp::is_transparent>>
426 { typedef void type; };
427
428 template<typename _Cmp, typename _SfinaeType>
429 using __has_is_transparent_t
430 = typename __has_is_transparent<_Cmp, _SfinaeType>::type;
431#endif
432
433#if __cplusplus201402L > 201402L
434 template<typename _Tree1, typename _Cmp2>
435 struct _Rb_tree_merge_helper { };
436#endif
437
438 template<typename _Key, typename _Val, typename _KeyOfValue,
439 typename _Compare, typename _Alloc = allocator<_Val> >
440 class _Rb_tree
441 {
442 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
443 rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
444
445 typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
446
447 protected:
448 typedef _Rb_tree_node_base* _Base_ptr;
449 typedef const _Rb_tree_node_base* _Const_Base_ptr;
450 typedef _Rb_tree_node<_Val>* _Link_type;
451 typedef const _Rb_tree_node<_Val>* _Const_Link_type;
452
453 private:
454 // Functor recycling a pool of nodes and using allocation once the pool
455 // is empty.
456 struct _Reuse_or_alloc_node
457 {
458 _Reuse_or_alloc_node(_Rb_tree& __t)
459 : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
460 {
461 if (_M_root)
462 {
463 _M_root->_M_parent = 0;
464
465 if (_M_nodes->_M_left)
466 _M_nodes = _M_nodes->_M_left;
467 }
468 else
469 _M_nodes = 0;
470 }
471
472#if __cplusplus201402L >= 201103L
473 _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
474#endif
475
476 ~_Reuse_or_alloc_node()
477 { _M_t._M_erase(static_cast<_Link_type>(_M_root)); }
478
479 template<typename _Arg>
480 _Link_type
481#if __cplusplus201402L < 201103L
482 operator()(const _Arg& __arg)
483#else
484 operator()(_Arg&& __arg)
485#endif
486 {
487 _Link_type __node = static_cast<_Link_type>(_M_extract());
488 if (__node)
489 {
490 _M_t._M_destroy_node(__node);
491 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg)std::forward<_Arg>(__arg));
492 return __node;
493 }
494
495 return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)std::forward<_Arg>(__arg));
496 }
497
498 private:
499 _Base_ptr
500 _M_extract()
501 {
502 if (!_M_nodes)
503 return _M_nodes;
504
505 _Base_ptr __node = _M_nodes;
506 _M_nodes = _M_nodes->_M_parent;
507 if (_M_nodes)
508 {
509 if (_M_nodes->_M_right == __node)
510 {
511 _M_nodes->_M_right = 0;
512
513 if (_M_nodes->_M_left)
514 {
515 _M_nodes = _M_nodes->_M_left;
516
517 while (_M_nodes->_M_right)
518 _M_nodes = _M_nodes->_M_right;
519
520 if (_M_nodes->_M_left)
521 _M_nodes = _M_nodes->_M_left;
522 }
523 }
524 else // __node is on the left.
525 _M_nodes->_M_left = 0;
526 }
527 else
528 _M_root = 0;
529
530 return __node;
531 }
532
533 _Base_ptr _M_root;
534 _Base_ptr _M_nodes;
535 _Rb_tree& _M_t;
536 };
537
538 // Functor similar to the previous one but without any pool of nodes to
539 // recycle.
540 struct _Alloc_node
541 {
542 _Alloc_node(_Rb_tree& __t)
543 : _M_t(__t) { }
544
545 template<typename _Arg>
546 _Link_type
547#if __cplusplus201402L < 201103L
548 operator()(const _Arg& __arg) const
549#else
550 operator()(_Arg&& __arg) const
551#endif
552 { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)std::forward<_Arg>(__arg)); }
553
554 private:
555 _Rb_tree& _M_t;
556 };
557
558 public:
559 typedef _Key key_type;
560 typedef _Val value_type;
561 typedef value_type* pointer;
562 typedef const value_type* const_pointer;
563 typedef value_type& reference;
564 typedef const value_type& const_reference;
565 typedef size_t size_type;
566 typedef ptrdiff_t difference_type;
567 typedef _Alloc allocator_type;
568
569 _Node_allocator&
570 _M_get_Node_allocator() _GLIBCXX_NOEXCEPTnoexcept
571 { return this->_M_impl; }
572
573 const _Node_allocator&
574 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPTnoexcept
575 { return this->_M_impl; }
576
577 allocator_type
578 get_allocator() const _GLIBCXX_NOEXCEPTnoexcept
579 { return allocator_type(_M_get_Node_allocator()); }
580
581 protected:
582 _Link_type
583 _M_get_node()
584 { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
585
586 void
587 _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPTnoexcept
588 { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); }
589
590#if __cplusplus201402L < 201103L
591 void
592 _M_construct_node(_Link_type __node, const value_type& __x)
593 {
594 __tryif (true)
595 { get_allocator().construct(__node->_M_valptr(), __x); }
596 __catch(...)if (false)
597 {
598 _M_put_node(__node);
599 __throw_exception_again;
600 }
601 }
602
603 _Link_type
604 _M_create_node(const value_type& __x)
605 {
606 _Link_type __tmp = _M_get_node();
607 _M_construct_node(__tmp, __x);
608 return __tmp;
609 }
610#else
611 template<typename... _Args>
612 void
613 _M_construct_node(_Link_type __node, _Args&&... __args)
614 {
615 __tryif (true)
616 {
617 ::new(__node) _Rb_tree_node<_Val>;
618 _Alloc_traits::construct(_M_get_Node_allocator(),
619 __node->_M_valptr(),
620 std::forward<_Args>(__args)...);
621 }
622 __catch(...)if (false)
623 {
624 __node->~_Rb_tree_node<_Val>();
625 _M_put_node(__node);
626 __throw_exception_again;
627 }
628 }
629
630 template<typename... _Args>
631 _Link_type
632 _M_create_node(_Args&&... __args)
633 {
634 _Link_type __tmp = _M_get_node();
635 _M_construct_node(__tmp, std::forward<_Args>(__args)...);
636 return __tmp;
637 }
638#endif
639
640 void
641 _M_destroy_node(_Link_type __p) _GLIBCXX_NOEXCEPTnoexcept
642 {
643#if __cplusplus201402L < 201103L
644 get_allocator().destroy(__p->_M_valptr());
645#else
646 _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
647 __p->~_Rb_tree_node<_Val>();
648#endif
649 }
650
651 void
652 _M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPTnoexcept
653 {
654 _M_destroy_node(__p);
655 _M_put_node(__p);
656 }
657
658 template<typename _NodeGen>
659 _Link_type
660 _M_clone_node(_Const_Link_type __x, _NodeGen& __node_gen)
661 {
662 _Link_type __tmp = __node_gen(*__x->_M_valptr());
663 __tmp->_M_color = __x->_M_color;
664 __tmp->_M_left = 0;
665 __tmp->_M_right = 0;
666 return __tmp;
667 }
668
669 protected:
670#if _GLIBCXX_INLINE_VERSION0
671 template<typename _Key_compare>
672#else
673 // Unused _Is_pod_comparator is kept as it is part of mangled name.
674 template<typename _Key_compare,
675 bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
676#endif
677 struct _Rb_tree_impl
678 : public _Node_allocator
679 , public _Rb_tree_key_compare<_Key_compare>
680 , public _Rb_tree_header
681 {
682 typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
683
684 _Rb_tree_impl()
685 _GLIBCXX_NOEXCEPT_IF(noexcept(is_nothrow_default_constructible<_Node_allocator>
::value && is_nothrow_default_constructible<_Base_key_compare
>::value)
686 is_nothrow_default_constructible<_Node_allocator>::valuenoexcept(is_nothrow_default_constructible<_Node_allocator>
::value && is_nothrow_default_constructible<_Base_key_compare
>::value)
687 && is_nothrow_default_constructible<_Base_key_compare>::value )noexcept(is_nothrow_default_constructible<_Node_allocator>
::value && is_nothrow_default_constructible<_Base_key_compare
>::value)
688 : _Node_allocator()
689 { }
690
691 _Rb_tree_impl(const _Rb_tree_impl& __x)
692 : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x))
693 , _Base_key_compare(__x._M_key_compare)
694 { }
695
696#if __cplusplus201402L < 201103L
697 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
698 : _Node_allocator(__a), _Base_key_compare(__comp)
699 { }
700#else
701 _Rb_tree_impl(_Rb_tree_impl&&) = default;
702
703 explicit
704 _Rb_tree_impl(_Node_allocator&& __a)
705 : _Node_allocator(std::move(__a))
706 { }
707
708 _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
709 : _Node_allocator(std::move(__a)),
710 _Base_key_compare(std::move(__x)),
711 _Rb_tree_header(std::move(__x))
712 { }
713
714 _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
715 : _Node_allocator(std::move(__a)), _Base_key_compare(__comp)
716 { }
717#endif
718 };
719
720 _Rb_tree_impl<_Compare> _M_impl;
721
722 protected:
723 _Base_ptr&
724 _M_root() _GLIBCXX_NOEXCEPTnoexcept
725 { return this->_M_impl._M_header._M_parent; }
726
727 _Const_Base_ptr
728 _M_root() const _GLIBCXX_NOEXCEPTnoexcept
729 { return this->_M_impl._M_header._M_parent; }
730
731 _Base_ptr&
732 _M_leftmost() _GLIBCXX_NOEXCEPTnoexcept
733 { return this->_M_impl._M_header._M_left; }
734
735 _Const_Base_ptr
736 _M_leftmost() const _GLIBCXX_NOEXCEPTnoexcept
737 { return this->_M_impl._M_header._M_left; }
738
739 _Base_ptr&
740 _M_rightmost() _GLIBCXX_NOEXCEPTnoexcept
741 { return this->_M_impl._M_header._M_right; }
742
743 _Const_Base_ptr
744 _M_rightmost() const _GLIBCXX_NOEXCEPTnoexcept
745 { return this->_M_impl._M_header._M_right; }
746
747 _Link_type
748 _M_begin() _GLIBCXX_NOEXCEPTnoexcept
749 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
750
751 _Const_Link_type
752 _M_begin() const _GLIBCXX_NOEXCEPTnoexcept
753 {
754 return static_cast<_Const_Link_type>
755 (this->_M_impl._M_header._M_parent);
756 }
757
758 _Base_ptr
759 _M_end() _GLIBCXX_NOEXCEPTnoexcept
760 { return &this->_M_impl._M_header; }
761
762 _Const_Base_ptr
763 _M_end() const _GLIBCXX_NOEXCEPTnoexcept
764 { return &this->_M_impl._M_header; }
765
766 static const _Key&
767 _S_key(_Const_Link_type __x)
768 {
769#if __cplusplus201402L >= 201103L
770 // If we're asking for the key we're presumably using the comparison
771 // object, and so this is a good place to sanity check it.
772 static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},
773 "comparison object must be invocable "
774 "with two arguments of key type");
775# if __cplusplus201402L >= 201703L
776 // _GLIBCXX_RESOLVE_LIB_DEFECTS
777 // 2542. Missing const requirements for associative containers
778 if constexpr (__is_invocable<_Compare&, const _Key&, const _Key&>{})
779 static_assert(
780 is_invocable_v<const _Compare&, const _Key&, const _Key&>,
781 "comparison object must be invocable as const");
782# endif // C++17
783#endif // C++11
784
785 return _KeyOfValue()(*__x->_M_valptr());
786 }
787
788 static _Link_type
789 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
790 { return static_cast<_Link_type>(__x->_M_left); }
791
792 static _Const_Link_type
793 _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
794 { return static_cast<_Const_Link_type>(__x->_M_left); }
795
796 static _Link_type
797 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
798 { return static_cast<_Link_type>(__x->_M_right); }
799
800 static _Const_Link_type
801 _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
802 { return static_cast<_Const_Link_type>(__x->_M_right); }
803
804 static const _Key&
805 _S_key(_Const_Base_ptr __x)
806 { return _S_key(static_cast<_Const_Link_type>(__x)); }
807
808 static _Base_ptr
809 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
810 { return _Rb_tree_node_base::_S_minimum(__x); }
811
812 static _Const_Base_ptr
813 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
814 { return _Rb_tree_node_base::_S_minimum(__x); }
815
816 static _Base_ptr
817 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
818 { return _Rb_tree_node_base::_S_maximum(__x); }
819
820 static _Const_Base_ptr
821 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPTnoexcept
822 { return _Rb_tree_node_base::_S_maximum(__x); }
823
824 public:
825 typedef _Rb_tree_iterator<value_type> iterator;
826 typedef _Rb_tree_const_iterator<value_type> const_iterator;
827
828 typedef std::reverse_iterator<iterator> reverse_iterator;
829 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
830
831#if __cplusplus201402L > 201402L
832 using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
833 using insert_return_type = _Node_insert_return<
834 conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
835 node_type>;
836#endif
837
838 pair<_Base_ptr, _Base_ptr>
839 _M_get_insert_unique_pos(const key_type& __k);
840
841 pair<_Base_ptr, _Base_ptr>
842 _M_get_insert_equal_pos(const key_type& __k);
843
844 pair<_Base_ptr, _Base_ptr>
845 _M_get_insert_hint_unique_pos(const_iterator __pos,
846 const key_type& __k);
847
848 pair<_Base_ptr, _Base_ptr>
849 _M_get_insert_hint_equal_pos(const_iterator __pos,
850 const key_type& __k);
851
852 private:
853#if __cplusplus201402L >= 201103L
854 template<typename _Arg, typename _NodeGen>
855 iterator
856 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
857
858 iterator
859 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
860
861 template<typename _Arg>
862 iterator
863 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
864
865 template<typename _Arg>
866 iterator
867 _M_insert_equal_lower(_Arg&& __x);
868
869 iterator
870 _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
871
872 iterator
873 _M_insert_equal_lower_node(_Link_type __z);
874#else
875 template<typename _NodeGen>
876 iterator
877 _M_insert_(_Base_ptr __x, _Base_ptr __y,
878 const value_type& __v, _NodeGen&);
879
880 // _GLIBCXX_RESOLVE_LIB_DEFECTS
881 // 233. Insertion hints in associative containers.
882 iterator
883 _M_insert_lower(_Base_ptr __y, const value_type& __v);
884
885 iterator
886 _M_insert_equal_lower(const value_type& __x);
887#endif
888
889 template<typename _NodeGen>
890 _Link_type
891 _M_copy(_Const_Link_type __x, _Base_ptr __p, _NodeGen&);
892
893 template<typename _NodeGen>
894 _Link_type
895 _M_copy(const _Rb_tree& __x, _NodeGen& __gen)
896 {
897 _Link_type __root = _M_copy(__x._M_begin(), _M_end(), __gen);
898 _M_leftmost() = _S_minimum(__root);
899 _M_rightmost() = _S_maximum(__root);
900 _M_impl._M_node_count = __x._M_impl._M_node_count;
901 return __root;
902 }
903
904 _Link_type
905 _M_copy(const _Rb_tree& __x)
906 {
907 _Alloc_node __an(*this);
908 return _M_copy(__x, __an);
909 }
910
911 void
912 _M_erase(_Link_type __x);
913
914 iterator
915 _M_lower_bound(_Link_type __x, _Base_ptr __y,
916 const _Key& __k);
917
918 const_iterator
919 _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
920 const _Key& __k) const;
921
922 iterator
923 _M_upper_bound(_Link_type __x, _Base_ptr __y,
924 const _Key& __k);
925
926 const_iterator
927 _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
928 const _Key& __k) const;
929
930 public:
931 // allocation/deallocation
932#if __cplusplus201402L < 201103L
933 _Rb_tree() { }
934#else
935 _Rb_tree() = default;
936#endif
937
938 _Rb_tree(const _Compare& __comp,
939 const allocator_type& __a = allocator_type())
940 : _M_impl(__comp, _Node_allocator(__a)) { }
941
942 _Rb_tree(const _Rb_tree& __x)
943 : _M_impl(__x._M_impl)
944 {
945 if (__x._M_root() != 0)
946 _M_root() = _M_copy(__x);
947 }
948
949#if __cplusplus201402L >= 201103L
950 _Rb_tree(const allocator_type& __a)
951 : _M_impl(_Node_allocator(__a))
952 { }
953
954 _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
955 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
956 {
957 if (__x._M_root() != nullptr)
958 _M_root() = _M_copy(__x);
959 }
960
961 _Rb_tree(_Rb_tree&&) = default;
962
963 _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
964 : _Rb_tree(std::move(__x), _Node_allocator(__a))
965 { }
966
967 private:
968 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, true_type)
969 noexcept(is_nothrow_default_constructible<_Compare>::value)
970 : _M_impl(std::move(__x._M_impl), std::move(__a))
971 { }
972
973 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, false_type)
974 : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
975 {
976 if (__x._M_root() != nullptr)
977 _M_move_data(__x, false_type{});
978 }
979
980 public:
981 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
982 noexcept( noexcept(
983 _Rb_tree(std::declval<_Rb_tree&&>(), std::declval<_Node_allocator&&>(),
984 std::declval<typename _Alloc_traits::is_always_equal>())) )
985 : _Rb_tree(std::move(__x), std::move(__a),
986 typename _Alloc_traits::is_always_equal{})
987 { }
988#endif
989
990 ~_Rb_tree() _GLIBCXX_NOEXCEPTnoexcept
991 { _M_erase(_M_begin()); }
992
993 _Rb_tree&
994 operator=(const _Rb_tree& __x);
995
996 // Accessors.
997 _Compare
998 key_comp() const
999 { return _M_impl._M_key_compare; }
1000
1001 iterator
1002 begin() _GLIBCXX_NOEXCEPTnoexcept
1003 { return iterator(this->_M_impl._M_header._M_left); }
1004
1005 const_iterator
1006 begin() const _GLIBCXX_NOEXCEPTnoexcept
1007 { return const_iterator(this->_M_impl._M_header._M_left); }
1008
1009 iterator
1010 end() _GLIBCXX_NOEXCEPTnoexcept
1011 { return iterator(&this->_M_impl._M_header); }
1012
1013 const_iterator
1014 end() const _GLIBCXX_NOEXCEPTnoexcept
1015 { return const_iterator(&this->_M_impl._M_header); }
1016
1017 reverse_iterator
1018 rbegin() _GLIBCXX_NOEXCEPTnoexcept
1019 { return reverse_iterator(end()); }
1020
1021 const_reverse_iterator
1022 rbegin() const _GLIBCXX_NOEXCEPTnoexcept
1023 { return const_reverse_iterator(end()); }
1024
1025 reverse_iterator
1026 rend() _GLIBCXX_NOEXCEPTnoexcept
1027 { return reverse_iterator(begin()); }
1028
1029 const_reverse_iterator
1030 rend() const _GLIBCXX_NOEXCEPTnoexcept
1031 { return const_reverse_iterator(begin()); }
1032
1033 _GLIBCXX_NODISCARD bool
1034 empty() const _GLIBCXX_NOEXCEPTnoexcept
1035 { return _M_impl._M_node_count == 0; }
1036
1037 size_type
1038 size() const _GLIBCXX_NOEXCEPTnoexcept
1039 { return _M_impl._M_node_count; }
1040
1041 size_type
1042 max_size() const _GLIBCXX_NOEXCEPTnoexcept
1043 { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
1044
1045 void
1046 swap(_Rb_tree& __t)
1047 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)noexcept(__is_nothrow_swappable<_Compare>::value);
1048
1049 // Insert/erase.
1050#if __cplusplus201402L >= 201103L
1051 template<typename _Arg>
1052 pair<iterator, bool>
1053 _M_insert_unique(_Arg&& __x);
1054
1055 template<typename _Arg>
1056 iterator
1057 _M_insert_equal(_Arg&& __x);
1058
1059 template<typename _Arg, typename _NodeGen>
1060 iterator
1061 _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1062
1063 template<typename _Arg>
1064 iterator
1065 _M_insert_unique_(const_iterator __pos, _Arg&& __x)
1066 {
1067 _Alloc_node __an(*this);
1068 return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
1069 }
1070
1071 template<typename _Arg, typename _NodeGen>
1072 iterator
1073 _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1074
1075 template<typename _Arg>
1076 iterator
1077 _M_insert_equal_(const_iterator __pos, _Arg&& __x)
1078 {
1079 _Alloc_node __an(*this);
1080 return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
1081 }
1082
1083 template<typename... _Args>
1084 pair<iterator, bool>
1085 _M_emplace_unique(_Args&&... __args);
1086
1087 template<typename... _Args>
1088 iterator
1089 _M_emplace_equal(_Args&&... __args);
1090
1091 template<typename... _Args>
1092 iterator
1093 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
1094
1095 template<typename... _Args>
1096 iterator
1097 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
1098
1099 template<typename _Iter>
1100 using __same_value_type
1101 = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
1102
1103 template<typename _InputIterator>
1104 __enable_if_t<__same_value_type<_InputIterator>::value>
1105 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1106 {
1107 _Alloc_node __an(*this);
1108 for (; __first != __last; ++__first)
1109 _M_insert_unique_(end(), *__first, __an);
1110 }
1111
1112 template<typename _InputIterator>
1113 __enable_if_t<!__same_value_type<_InputIterator>::value>
1114 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1115 {
1116 for (; __first != __last; ++__first)
1117 _M_emplace_unique(*__first);
1118 }
1119
1120 template<typename _InputIterator>
1121 __enable_if_t<__same_value_type<_InputIterator>::value>
1122 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1123 {
1124 _Alloc_node __an(*this);
1125 for (; __first != __last; ++__first)
1126 _M_insert_equal_(end(), *__first, __an);
1127 }
1128
1129 template<typename _InputIterator>
1130 __enable_if_t<!__same_value_type<_InputIterator>::value>
1131 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1132 {
1133 _Alloc_node __an(*this);
1134 for (; __first != __last; ++__first)
1135 _M_emplace_equal(*__first);
1136 }
1137#else
1138 pair<iterator, bool>
1139 _M_insert_unique(const value_type& __x);
1140
1141 iterator
1142 _M_insert_equal(const value_type& __x);
1143
1144 template<typename _NodeGen>
1145 iterator
1146 _M_insert_unique_(const_iterator __pos, const value_type& __x,
1147 _NodeGen&);
1148
1149 iterator
1150 _M_insert_unique_(const_iterator __pos, const value_type& __x)
1151 {
1152 _Alloc_node __an(*this);
1153 return _M_insert_unique_(__pos, __x, __an);
1154 }
1155
1156 template<typename _NodeGen>
1157 iterator
1158 _M_insert_equal_(const_iterator __pos, const value_type& __x,
1159 _NodeGen&);
1160 iterator
1161 _M_insert_equal_(const_iterator __pos, const value_type& __x)
1162 {
1163 _Alloc_node __an(*this);
1164 return _M_insert_equal_(__pos, __x, __an);
1165 }
1166
1167 template<typename _InputIterator>
1168 void
1169 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1170 {
1171 _Alloc_node __an(*this);
1172 for (; __first != __last; ++__first)
1173 _M_insert_unique_(end(), *__first, __an);
1174 }
1175
1176 template<typename _InputIterator>
1177 void
1178 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1179 {
1180 _Alloc_node __an(*this);
1181 for (; __first != __last; ++__first)
1182 _M_insert_equal_(end(), *__first, __an);
1183 }
1184#endif
1185
1186 private:
1187 void
1188 _M_erase_aux(const_iterator __position);
1189
1190 void
1191 _M_erase_aux(const_iterator __first, const_iterator __last);
1192
1193 public:
1194#if __cplusplus201402L >= 201103L
1195 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1196 // DR 130. Associative erase should return an iterator.
1197 _GLIBCXX_ABI_TAG_CXX11__attribute ((__abi_tag__ ("cxx11")))
1198 iterator
1199 erase(const_iterator __position)
1200 {
1201 __glibcxx_assert(__position != end());
1202 const_iterator __result = __position;
1203 ++__result;
1204 _M_erase_aux(__position);
1205 return __result._M_const_cast();
1206 }
1207
1208 // LWG 2059.
1209 _GLIBCXX_ABI_TAG_CXX11__attribute ((__abi_tag__ ("cxx11")))
1210 iterator
1211 erase(iterator __position)
1212 {
1213 __glibcxx_assert(__position != end());
1214 iterator __result = __position;
1215 ++__result;
1216 _M_erase_aux(__position);
1217 return __result;
1218 }
1219#else
1220 void
1221 erase(iterator __position)
1222 {
1223 __glibcxx_assert(__position != end());
1224 _M_erase_aux(__position);
1225 }
1226
1227 void
1228 erase(const_iterator __position)
1229 {
1230 __glibcxx_assert(__position != end());
1231 _M_erase_aux(__position);
1232 }
1233#endif
1234
1235 size_type
1236 erase(const key_type& __x);
1237
1238#if __cplusplus201402L >= 201103L
1239 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1240 // DR 130. Associative erase should return an iterator.
1241 _GLIBCXX_ABI_TAG_CXX11__attribute ((__abi_tag__ ("cxx11")))
1242 iterator
1243 erase(const_iterator __first, const_iterator __last)
1244 {
1245 _M_erase_aux(__first, __last);
1246 return __last._M_const_cast();
1247 }
1248#else
1249 void
1250 erase(iterator __first, iterator __last)
1251 { _M_erase_aux(__first, __last); }
1252
1253 void
1254 erase(const_iterator __first, const_iterator __last)
1255 { _M_erase_aux(__first, __last); }
1256#endif
1257
1258 void
1259 clear() _GLIBCXX_NOEXCEPTnoexcept
1260 {
1261 _M_erase(_M_begin());
1262 _M_impl._M_reset();
1263 }
1264
1265 // Set operations.
1266 iterator
1267 find(const key_type& __k);
1268
1269 const_iterator
1270 find(const key_type& __k) const;
1271
1272 size_type
1273 count(const key_type& __k) const;
1274
1275 iterator
1276 lower_bound(const key_type& __k)
1277 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
1278
1279 const_iterator
1280 lower_bound(const key_type& __k) const
1281 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
1282
1283 iterator
1284 upper_bound(const key_type& __k)
1285 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
1286
1287 const_iterator
1288 upper_bound(const key_type& __k) const
1289 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
1290
1291 pair<iterator, iterator>
1292 equal_range(const key_type& __k);
1293
1294 pair<const_iterator, const_iterator>
1295 equal_range(const key_type& __k) const;
1296
1297#if __cplusplus201402L >= 201402L
1298 template<typename _Kt,
1299 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1300 iterator
1301 _M_find_tr(const _Kt& __k)
1302 {
1303 const _Rb_tree* __const_this = this;
1304 return __const_this->_M_find_tr(__k)._M_const_cast();
1305 }
1306
1307 template<typename _Kt,
1308 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1309 const_iterator
1310 _M_find_tr(const _Kt& __k) const
1311 {
1312 auto __j = _M_lower_bound_tr(__k);
1313 if (__j != end() && _M_impl._M_key_compare(__k, _S_key(__j._M_node)))
1314 __j = end();
1315 return __j;
1316 }
1317
1318 template<typename _Kt,
1319 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1320 size_type
1321 _M_count_tr(const _Kt& __k) const
1322 {
1323 auto __p = _M_equal_range_tr(__k);
1324 return std::distance(__p.first, __p.second);
1325 }
1326
1327 template<typename _Kt,
1328 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1329 iterator
1330 _M_lower_bound_tr(const _Kt& __k)
1331 {
1332 const _Rb_tree* __const_this = this;
1333 return __const_this->_M_lower_bound_tr(__k)._M_const_cast();
1334 }
1335
1336 template<typename _Kt,
1337 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1338 const_iterator
1339 _M_lower_bound_tr(const _Kt& __k) const
1340 {
1341 auto __x = _M_begin();
1342 auto __y = _M_end();
1343 while (__x != 0)
1344 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1345 {
1346 __y = __x;
1347 __x = _S_left(__x);
1348 }
1349 else
1350 __x = _S_right(__x);
1351 return const_iterator(__y);
1352 }
1353
1354 template<typename _Kt,
1355 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1356 iterator
1357 _M_upper_bound_tr(const _Kt& __k)
1358 {
1359 const _Rb_tree* __const_this = this;
1360 return __const_this->_M_upper_bound_tr(__k)._M_const_cast();
1361 }
1362
1363 template<typename _Kt,
1364 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1365 const_iterator
1366 _M_upper_bound_tr(const _Kt& __k) const
1367 {
1368 auto __x = _M_begin();
1369 auto __y = _M_end();
1370 while (__x != 0)
1371 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1372 {
1373 __y = __x;
1374 __x = _S_left(__x);
1375 }
1376 else
1377 __x = _S_right(__x);
1378 return const_iterator(__y);
1379 }
1380
1381 template<typename _Kt,
1382 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1383 pair<iterator, iterator>
1384 _M_equal_range_tr(const _Kt& __k)
1385 {
1386 const _Rb_tree* __const_this = this;
1387 auto __ret = __const_this->_M_equal_range_tr(__k);
1388 return { __ret.first._M_const_cast(), __ret.second._M_const_cast() };
1389 }
1390
1391 template<typename _Kt,
1392 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1393 pair<const_iterator, const_iterator>
1394 _M_equal_range_tr(const _Kt& __k) const
1395 {
1396 auto __low = _M_lower_bound_tr(__k);
1397 auto __high = __low;
1398 auto& __cmp = _M_impl._M_key_compare;
1399 while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
1400 ++__high;
1401 return { __low, __high };
1402 }
1403#endif
1404
1405 // Debugging.
1406 bool
1407 __rb_verify() const;
1408
1409#if __cplusplus201402L >= 201103L
1410 _Rb_tree&
1411 operator=(_Rb_tree&&)
1412 noexcept(_Alloc_traits::_S_nothrow_move()
1413 && is_nothrow_move_assignable<_Compare>::value);
1414
1415 template<typename _Iterator>
1416 void
1417 _M_assign_unique(_Iterator, _Iterator);
1418
1419 template<typename _Iterator>
1420 void
1421 _M_assign_equal(_Iterator, _Iterator);
1422
1423 private:
1424 // Move elements from container with equal allocator.
1425 void
1426 _M_move_data(_Rb_tree& __x, true_type)
1427 { _M_impl._M_move_data(__x._M_impl); }
1428
1429 // Move elements from container with possibly non-equal allocator,
1430 // which might result in a copy not a move.
1431 void
1432 _M_move_data(_Rb_tree&, false_type);
1433
1434 // Move assignment from container with equal allocator.
1435 void
1436 _M_move_assign(_Rb_tree&, true_type);
1437
1438 // Move assignment from container with possibly non-equal allocator,
1439 // which might result in a copy not a move.
1440 void
1441 _M_move_assign(_Rb_tree&, false_type);
1442#endif
1443
1444#if __cplusplus201402L > 201402L
1445 public:
1446 /// Re-insert an extracted node.
1447 insert_return_type
1448 _M_reinsert_node_unique(node_type&& __nh)
1449 {
1450 insert_return_type __ret;
1451 if (__nh.empty())
1452 __ret.position = end();
1453 else
1454 {
1455 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1456
1457 auto __res = _M_get_insert_unique_pos(__nh._M_key());
1458 if (__res.second)
1459 {
1460 __ret.position
1461 = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1462 __nh._M_ptr = nullptr;
1463 __ret.inserted = true;
1464 }
1465 else
1466 {
1467 __ret.node = std::move(__nh);
1468 __ret.position = iterator(__res.first);
1469 __ret.inserted = false;
1470 }
1471 }
1472 return __ret;
1473 }
1474
1475 /// Re-insert an extracted node.
1476 iterator
1477 _M_reinsert_node_equal(node_type&& __nh)
1478 {
1479 iterator __ret;
1480 if (__nh.empty())
1481 __ret = end();
1482 else
1483 {
1484 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1485 auto __res = _M_get_insert_equal_pos(__nh._M_key());
1486 if (__res.second)
1487 __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1488 else
1489 __ret = _M_insert_equal_lower_node(__nh._M_ptr);
1490 __nh._M_ptr = nullptr;
1491 }
1492 return __ret;
1493 }
1494
1495 /// Re-insert an extracted node.
1496 iterator
1497 _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
1498 {
1499 iterator __ret;
1500 if (__nh.empty())
1501 __ret = end();
1502 else
1503 {
1504 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1505 auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
1506 if (__res.second)
1507 {
1508 __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1509 __nh._M_ptr = nullptr;
1510 }
1511 else
1512 __ret = iterator(__res.first);
1513 }
1514 return __ret;
1515 }
1516
1517 /// Re-insert an extracted node.
1518 iterator
1519 _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
1520 {
1521 iterator __ret;
1522 if (__nh.empty())
1523 __ret = end();
1524 else
1525 {
1526 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
1527 auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
1528 if (__res.second)
1529 __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
1530 else
1531 __ret = _M_insert_equal_lower_node(__nh._M_ptr);
1532 __nh._M_ptr = nullptr;
1533 }
1534 return __ret;
1535 }
1536
1537 /// Extract a node.
1538 node_type
1539 extract(const_iterator __pos)
1540 {
1541 auto __ptr = _Rb_tree_rebalance_for_erase(
1542 __pos._M_const_cast()._M_node, _M_impl._M_header);
1543 --_M_impl._M_node_count;
1544 return { static_cast<_Link_type>(__ptr), _M_get_Node_allocator() };
1545 }
1546
1547 /// Extract a node.
1548 node_type
1549 extract(const key_type& __k)
1550 {
1551 node_type __nh;
1552 auto __pos = find(__k);
1553 if (__pos != end())
1554 __nh = extract(const_iterator(__pos));
1555 return __nh;
1556 }
1557
1558 template<typename _Compare2>
1559 using _Compatible_tree
1560 = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
1561
1562 template<typename, typename>
1563 friend class _Rb_tree_merge_helper;
1564
1565 /// Merge from a compatible container into one with unique keys.
1566 template<typename _Compare2>
1567 void
1568 _M_merge_unique(_Compatible_tree<_Compare2>& __src) noexcept
1569 {
1570 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
1571 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1572 {
1573 auto __pos = __i++;
1574 auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
1575 if (__res.second)
1576 {
1577 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
1578 auto __ptr = _Rb_tree_rebalance_for_erase(
1579 __pos._M_node, __src_impl._M_header);
1580 --__src_impl._M_node_count;
1581 _M_insert_node(__res.first, __res.second,
1582 static_cast<_Link_type>(__ptr));
1583 }
1584 }
1585 }
1586
1587 /// Merge from a compatible container into one with equivalent keys.
1588 template<typename _Compare2>
1589 void
1590 _M_merge_equal(_Compatible_tree<_Compare2>& __src) noexcept
1591 {
1592 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
1593 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1594 {
1595 auto __pos = __i++;
1596 auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
1597 if (__res.second)
1598 {
1599 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
1600 auto __ptr = _Rb_tree_rebalance_for_erase(
1601 __pos._M_node, __src_impl._M_header);
1602 --__src_impl._M_node_count;
1603 _M_insert_node(__res.first, __res.second,
1604 static_cast<_Link_type>(__ptr));
1605 }
1606 }
1607 }
1608#endif // C++17
1609
1610 friend bool
1611 operator==(const _Rb_tree& __x, const _Rb_tree& __y)
1612 {
1613 return __x.size() == __y.size()
1614 && std::equal(__x.begin(), __x.end(), __y.begin());
1615 }
1616
1617#if __cpp_lib_three_way_comparison
1618 friend auto
1619 operator<=>(const _Rb_tree& __x, const _Rb_tree& __y)
1620 {
1621 if constexpr (requires { typename __detail::__synth3way_t<_Val>; })
1622 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
1623 __y.begin(), __y.end(),
1624 __detail::__synth3way);
1625 }
1626#else
1627 friend bool
1628 operator<(const _Rb_tree& __x, const _Rb_tree& __y)
1629 {
1630 return std::lexicographical_compare(__x.begin(), __x.end(),
1631 __y.begin(), __y.end());
1632 }
1633
1634 friend bool _GLIBCXX_DEPRECATED__attribute__ ((__deprecated__))
1635 operator!=(const _Rb_tree& __x, const _Rb_tree& __y)
1636 { return !(__x == __y); }
1637
1638 friend bool _GLIBCXX_DEPRECATED__attribute__ ((__deprecated__))
1639 operator>(const _Rb_tree& __x, const _Rb_tree& __y)
1640 { return __y < __x; }
1641
1642 friend bool _GLIBCXX_DEPRECATED__attribute__ ((__deprecated__))
1643 operator<=(const _Rb_tree& __x, const _Rb_tree& __y)
1644 { return !(__y < __x); }
1645
1646 friend bool _GLIBCXX_DEPRECATED__attribute__ ((__deprecated__))
1647 operator>=(const _Rb_tree& __x, const _Rb_tree& __y)
1648 { return !(__x < __y); }
1649#endif
1650 };
1651
1652 template<typename _Key, typename _Val, typename _KeyOfValue,
1653 typename _Compare, typename _Alloc>
1654 inline void
1655 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1656 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1657 { __x.swap(__y); }
1658
1659#if __cplusplus201402L >= 201103L
1660 template<typename _Key, typename _Val, typename _KeyOfValue,
1661 typename _Compare, typename _Alloc>
1662 void
1663 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1664 _M_move_data(_Rb_tree& __x, false_type)
1665 {
1666 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
1667 _M_move_data(__x, true_type());
1668 else
1669 {
1670 _Alloc_node __an(*this);
1671 auto __lbd =
1672 [&__an](const value_type& __cval)
1673 {
1674 auto& __val = const_cast<value_type&>(__cval);
1675 return __an(std::move_if_noexcept(__val));
1676 };
1677 _M_root() = _M_copy(__x, __lbd);
1678 }
1679 }
1680
1681 template<typename _Key, typename _Val, typename _KeyOfValue,
1682 typename _Compare, typename _Alloc>
1683 inline void
1684 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1685 _M_move_assign(_Rb_tree& __x, true_type)
1686 {
1687 clear();
1688 if (__x._M_root() != nullptr)
1689 _M_move_data(__x, true_type());
1690 std::__alloc_on_move(_M_get_Node_allocator(),
1691 __x._M_get_Node_allocator());
1692 }
1693
1694 template<typename _Key, typename _Val, typename _KeyOfValue,
1695 typename _Compare, typename _Alloc>
1696 void
1697 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1698 _M_move_assign(_Rb_tree& __x, false_type)
1699 {
1700 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
1701 return _M_move_assign(__x, true_type{});
1702
1703 // Try to move each node reusing existing nodes and copying __x nodes
1704 // structure.
1705 _Reuse_or_alloc_node __roan(*this);
1706 _M_impl._M_reset();
1707 if (__x._M_root() != nullptr)
1708 {
1709 auto __lbd =
1710 [&__roan](const value_type& __cval)
1711 {
1712 auto& __val = const_cast<value_type&>(__cval);
1713 return __roan(std::move(__val));
1714 };
1715 _M_root() = _M_copy(__x, __lbd);
1716 __x.clear();
1717 }
1718 }
1719
1720 template<typename _Key, typename _Val, typename _KeyOfValue,
1721 typename _Compare, typename _Alloc>
1722 inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1723 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1724 operator=(_Rb_tree&& __x)
1725 noexcept(_Alloc_traits::_S_nothrow_move()
1726 && is_nothrow_move_assignable<_Compare>::value)
1727 {
1728 _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare);
1729 _M_move_assign(__x, __bool_constant<_Alloc_traits::_S_nothrow_move()>());
1730 return *this;
1731 }
1732
1733 template<typename _Key, typename _Val, typename _KeyOfValue,
1734 typename _Compare, typename _Alloc>
1735 template<typename _Iterator>
1736 void
1737 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1738 _M_assign_unique(_Iterator __first, _Iterator __last)
1739 {
1740 _Reuse_or_alloc_node __roan(*this);
1741 _M_impl._M_reset();
1742 for (; __first != __last; ++__first)
1743 _M_insert_unique_(end(), *__first, __roan);
1744 }
1745
1746 template<typename _Key, typename _Val, typename _KeyOfValue,
1747 typename _Compare, typename _Alloc>
1748 template<typename _Iterator>
1749 void
1750 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1751 _M_assign_equal(_Iterator __first, _Iterator __last)
1752 {
1753 _Reuse_or_alloc_node __roan(*this);
1754 _M_impl._M_reset();
1755 for (; __first != __last; ++__first)
1756 _M_insert_equal_(end(), *__first, __roan);
1757 }
1758#endif
1759
1760 template<typename _Key, typename _Val, typename _KeyOfValue,
1761 typename _Compare, typename _Alloc>
1762 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1763 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1764 operator=(const _Rb_tree& __x)
1765 {
1766 if (this != &__x)
1767 {
1768 // Note that _Key may be a constant type.
1769#if __cplusplus201402L >= 201103L
1770 if (_Alloc_traits::_S_propagate_on_copy_assign())
1771 {
1772 auto& __this_alloc = this->_M_get_Node_allocator();
1773 auto& __that_alloc = __x._M_get_Node_allocator();
1774 if (!_Alloc_traits::_S_always_equal()
1775 && __this_alloc != __that_alloc)
1776 {
1777 // Replacement allocator cannot free existing storage, we need
1778 // to erase nodes first.
1779 clear();
1780 std::__alloc_on_copy(__this_alloc, __that_alloc);
1781 }
1782 }
1783#endif
1784
1785 _Reuse_or_alloc_node __roan(*this);
1786 _M_impl._M_reset();
1787 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
1788 if (__x._M_root() != 0)
1789 _M_root() = _M_copy(__x, __roan);
1790 }
1791
1792 return *this;
1793 }
1794
1795 template<typename _Key, typename _Val, typename _KeyOfValue,
1796 typename _Compare, typename _Alloc>
1797#if __cplusplus201402L >= 201103L
1798 template<typename _Arg, typename _NodeGen>
1799#else
1800 template<typename _NodeGen>
1801#endif
1802 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1803 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1804 _M_insert_(_Base_ptr __x, _Base_ptr __p,
1805#if __cplusplus201402L >= 201103L
1806 _Arg&& __v,
1807#else
1808 const _Val& __v,
1809#endif
1810 _NodeGen& __node_gen)
1811 {
1812 bool __insert_left = (__x != 0 || __p == _M_end()
1813 || _M_impl._M_key_compare(_KeyOfValue()(__v),
1814 _S_key(__p)));
1815
1816 _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v));
1817
1818 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1819 this->_M_impl._M_header);
1820 ++_M_impl._M_node_count;
1821 return iterator(__z);
1822 }
1823
1824 template<typename _Key, typename _Val, typename _KeyOfValue,
1825 typename _Compare, typename _Alloc>
1826#if __cplusplus201402L >= 201103L
1827 template<typename _Arg>
1828#endif
1829 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1830 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1831#if __cplusplus201402L >= 201103L
1832 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
1833#else
1834 _M_insert_lower(_Base_ptr __p, const _Val& __v)
1835#endif
1836 {
1837 bool __insert_left = (__p == _M_end()
1838 || !_M_impl._M_key_compare(_S_key(__p),
1839 _KeyOfValue()(__v)));
1840
1841 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v));
1842
1843 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1844 this->_M_impl._M_header);
1845 ++_M_impl._M_node_count;
1846 return iterator(__z);
1847 }
1848
1849 template<typename _Key, typename _Val, typename _KeyOfValue,
1850 typename _Compare, typename _Alloc>
1851#if __cplusplus201402L >= 201103L
1852 template<typename _Arg>
1853#endif
1854 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1855 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1856#if __cplusplus201402L >= 201103L
1857 _M_insert_equal_lower(_Arg&& __v)
1858#else
1859 _M_insert_equal_lower(const _Val& __v)
1860#endif
1861 {
1862 _Link_type __x = _M_begin();
1863 _Base_ptr __y = _M_end();
1864 while (__x != 0)
1865 {
1866 __y = __x;
1867 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
1868 _S_left(__x) : _S_right(__x);
1869 }
1870 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v));
1871 }
1872
1873 template<typename _Key, typename _Val, typename _KoV,
1874 typename _Compare, typename _Alloc>
1875 template<typename _NodeGen>
1876 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1877 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1878 _M_copy(_Const_Link_type __x, _Base_ptr __p, _NodeGen& __node_gen)
1879 {
1880 // Structural copy. __x and __p must be non-null.
1881 _Link_type __top = _M_clone_node(__x, __node_gen);
1882 __top->_M_parent = __p;
1883
1884 __tryif (true)
1885 {
1886 if (__x->_M_right)
1887 __top->_M_right = _M_copy(_S_right(__x), __top, __node_gen);
1888 __p = __top;
1889 __x = _S_left(__x);
1890
1891 while (__x != 0)
1892 {
1893 _Link_type __y = _M_clone_node(__x, __node_gen);
1894 __p->_M_left = __y;
1895 __y->_M_parent = __p;
1896 if (__x->_M_right)
1897 __y->_M_right = _M_copy(_S_right(__x), __y, __node_gen);
1898 __p = __y;
1899 __x = _S_left(__x);
1900 }
1901 }
1902 __catch(...)if (false)
1903 {
1904 _M_erase(__top);
1905 __throw_exception_again;
1906 }
1907 return __top;
1908 }
1909
1910 template<typename _Key, typename _Val, typename _KeyOfValue,
1911 typename _Compare, typename _Alloc>
1912 void
1913 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1914 _M_erase(_Link_type __x)
1915 {
1916 // Erase without rebalancing.
1917 while (__x != 0)
1918 {
1919 _M_erase(_S_right(__x));
1920 _Link_type __y = _S_left(__x);
1921 _M_drop_node(__x);
1922 __x = __y;
1923 }
1924 }
1925
1926 template<typename _Key, typename _Val, typename _KeyOfValue,
1927 typename _Compare, typename _Alloc>
1928 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1929 _Compare, _Alloc>::iterator
1930 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1931 _M_lower_bound(_Link_type __x, _Base_ptr __y,
1932 const _Key& __k)
1933 {
1934 while (__x != 0)
1935 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1936 __y = __x, __x = _S_left(__x);
1937 else
1938 __x = _S_right(__x);
1939 return iterator(__y);
1940 }
1941
1942 template<typename _Key, typename _Val, typename _KeyOfValue,
1943 typename _Compare, typename _Alloc>
1944 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1945 _Compare, _Alloc>::const_iterator
1946 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1947 _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
1948 const _Key& __k) const
1949 {
1950 while (__x != 0)
1951 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1952 __y = __x, __x = _S_left(__x);
1953 else
1954 __x = _S_right(__x);
1955 return const_iterator(__y);
1956 }
1957
1958 template<typename _Key, typename _Val, typename _KeyOfValue,
1959 typename _Compare, typename _Alloc>
1960 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1961 _Compare, _Alloc>::iterator
1962 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1963 _M_upper_bound(_Link_type __x, _Base_ptr __y,
1964 const _Key& __k)
1965 {
1966 while (__x != 0)
1967 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1968 __y = __x, __x = _S_left(__x);
1969 else
1970 __x = _S_right(__x);
1971 return iterator(__y);
1972 }
1973
1974 template<typename _Key, typename _Val, typename _KeyOfValue,
1975 typename _Compare, typename _Alloc>
1976 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1977 _Compare, _Alloc>::const_iterator
1978 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1979 _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
1980 const _Key& __k) const
1981 {
1982 while (__x != 0)
1983 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1984 __y = __x, __x = _S_left(__x);
1985 else
1986 __x = _S_right(__x);
1987 return const_iterator(__y);
1988 }
1989
1990 template<typename _Key, typename _Val, typename _KeyOfValue,
1991 typename _Compare, typename _Alloc>
1992 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1993 _Compare, _Alloc>::iterator,
1994 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1995 _Compare, _Alloc>::iterator>
1996 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1997 equal_range(const _Key& __k)
1998 {
1999 _Link_type __x = _M_begin();
2000 _Base_ptr __y = _M_end();
2001 while (__x != 0)
2002 {
2003 if (_M_impl._M_key_compare(_S_key(__x), __k))
2004 __x = _S_right(__x);
2005 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
2006 __y = __x, __x = _S_left(__x);
2007 else
2008 {
2009 _Link_type __xu(__x);
2010 _Base_ptr __yu(__y);
2011 __y = __x, __x = _S_left(__x);
2012 __xu = _S_right(__xu);
2013 return pair<iterator,
2014 iterator>(_M_lower_bound(__x, __y, __k),
2015 _M_upper_bound(__xu, __yu, __k));
2016 }
2017 }
2018 return pair<iterator, iterator>(iterator(__y),
2019 iterator(__y));
2020 }
2021
2022 template<typename _Key, typename _Val, typename _KeyOfValue,
2023 typename _Compare, typename _Alloc>
2024 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2025 _Compare, _Alloc>::const_iterator,
2026 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2027 _Compare, _Alloc>::const_iterator>
2028 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2029 equal_range(const _Key& __k) const
2030 {
2031 _Const_Link_type __x = _M_begin();
2032 _Const_Base_ptr __y = _M_end();
2033 while (__x != 0)
2034 {
2035 if (_M_impl._M_key_compare(_S_key(__x), __k))
2036 __x = _S_right(__x);
2037 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
2038 __y = __x, __x = _S_left(__x);
2039 else
2040 {
2041 _Const_Link_type __xu(__x);
2042 _Const_Base_ptr __yu(__y);
2043 __y = __x, __x = _S_left(__x);
2044 __xu = _S_right(__xu);
2045 return pair<const_iterator,
2046 const_iterator>(_M_lower_bound(__x, __y, __k),
2047 _M_upper_bound(__xu, __yu, __k));
2048 }
2049 }
2050 return pair<const_iterator, const_iterator>(const_iterator(__y),
2051 const_iterator(__y));
2052 }
2053
2054 template<typename _Key, typename _Val, typename _KeyOfValue,
2055 typename _Compare, typename _Alloc>
2056 void
2057 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2058 swap(_Rb_tree& __t)
2059 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)noexcept(__is_nothrow_swappable<_Compare>::value)
2060 {
2061 if (_M_root() == 0)
2062 {
2063 if (__t._M_root() != 0)
2064 _M_impl._M_move_data(__t._M_impl);
2065 }
2066 else if (__t._M_root() == 0)
2067 __t._M_impl._M_move_data(_M_impl);
2068 else
2069 {
2070 std::swap(_M_root(),__t._M_root());
2071 std::swap(_M_leftmost(),__t._M_leftmost());
2072 std::swap(_M_rightmost(),__t._M_rightmost());
2073
2074 _M_root()->_M_parent = _M_end();
2075 __t._M_root()->_M_parent = __t._M_end();
2076 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
2077 }
2078 // No need to swap header's color as it does not change.
2079 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
2080
2081 _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2082 __t._M_get_Node_allocator());
2083 }
2084
2085 template<typename _Key, typename _Val, typename _KeyOfValue,
2086 typename _Compare, typename _Alloc>
2087 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2088 _Compare, _Alloc>::_Base_ptr,
2089 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2090 _Compare, _Alloc>::_Base_ptr>
2091 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2092 _M_get_insert_unique_pos(const key_type& __k)
2093 {
2094 typedef pair<_Base_ptr, _Base_ptr> _Res;
2095 _Link_type __x = _M_begin();
2096 _Base_ptr __y = _M_end();
2097 bool __comp = true;
2098 while (__x != 0)
2099 {
2100 __y = __x;
2101 __comp = _M_impl._M_key_compare(__k, _S_key(__x));
2102 __x = __comp ? _S_left(__x) : _S_right(__x);
2103 }
2104 iterator __j = iterator(__y);
2105 if (__comp)
2106 {
2107 if (__j == begin())
2108 return _Res(__x, __y);
2109 else
2110 --__j;
2111 }
2112 if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
2113 return _Res(__x, __y);
2114 return _Res(__j._M_node, 0);
2115 }
2116
2117 template<typename _Key, typename _Val, typename _KeyOfValue,
2118 typename _Compare, typename _Alloc>
2119 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2120 _Compare, _Alloc>::_Base_ptr,
2121 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2122 _Compare, _Alloc>::_Base_ptr>
2123 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2124 _M_get_insert_equal_pos(const key_type& __k)
2125 {
2126 typedef pair<_Base_ptr, _Base_ptr> _Res;
2127 _Link_type __x = _M_begin();
2128 _Base_ptr __y = _M_end();
2129 while (__x != 0)
2130 {
2131 __y = __x;
2132 __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
2133 _S_left(__x) : _S_right(__x);
2134 }
2135 return _Res(__x, __y);
2136 }
2137
2138 template<typename _Key, typename _Val, typename _KeyOfValue,
2139 typename _Compare, typename _Alloc>
2140#if __cplusplus201402L >= 201103L
2141 template<typename _Arg>
2142#endif
2143 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2144 _Compare, _Alloc>::iterator, bool>
2145 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2146#if __cplusplus201402L >= 201103L
2147 _M_insert_unique(_Arg&& __v)
2148#else
2149 _M_insert_unique(const _Val& __v)
2150#endif
2151 {
2152 typedef pair<iterator, bool> _Res;
2153 pair<_Base_ptr, _Base_ptr> __res
2154 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
2155
2156 if (__res.second)
2157 {
2158 _Alloc_node __an(*this);
2159 return _Res(_M_insert_(__res.first, __res.second,
2160 _GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v), __an),
2161 true);
2162 }
2163
2164 return _Res(iterator(__res.first), false);
2165 }
2166
2167 template<typename _Key, typename _Val, typename _KeyOfValue,
2168 typename _Compare, typename _Alloc>
2169#if __cplusplus201402L >= 201103L
2170 template<typename _Arg>
2171#endif
2172 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2173 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2174#if __cplusplus201402L >= 201103L
2175 _M_insert_equal(_Arg&& __v)
2176#else
2177 _M_insert_equal(const _Val& __v)
2178#endif
2179 {
2180 pair<_Base_ptr, _Base_ptr> __res
2181 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
2182 _Alloc_node __an(*this);
2183 return _M_insert_(__res.first, __res.second,
2184 _GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v), __an);
2185 }
2186
2187 template<typename _Key, typename _Val, typename _KeyOfValue,
2188 typename _Compare, typename _Alloc>
2189 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2190 _Compare, _Alloc>::_Base_ptr,
2191 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2192 _Compare, _Alloc>::_Base_ptr>
2193 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2194 _M_get_insert_hint_unique_pos(const_iterator __position,
2195 const key_type& __k)
2196 {
2197 iterator __pos = __position._M_const_cast();
2198 typedef pair<_Base_ptr, _Base_ptr> _Res;
2199
2200 // end()
2201 if (__pos._M_node == _M_end())
2202 {
2203 if (size() > 0
2204 && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
2205 return _Res(0, _M_rightmost());
2206 else
2207 return _M_get_insert_unique_pos(__k);
2208 }
2209 else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
2210 {
2211 // First, try before...
2212 iterator __before = __pos;
2213 if (__pos._M_node == _M_leftmost()) // begin()
2214 return _Res(_M_leftmost(), _M_leftmost());
2215 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
2216 {
2217 if (_S_right(__before._M_node) == 0)
2218 return _Res(0, __before._M_node);
2219 else
2220 return _Res(__pos._M_node, __pos._M_node);
2221 }
2222 else
2223 return _M_get_insert_unique_pos(__k);
2224 }
2225 else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
2226 {
2227 // ... then try after.
2228 iterator __after = __pos;
2229 if (__pos._M_node == _M_rightmost())
2230 return _Res(0, _M_rightmost());
2231 else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
2232 {
2233 if (_S_right(__pos._M_node) == 0)
2234 return _Res(0, __pos._M_node);
2235 else
2236 return _Res(__after._M_node, __after._M_node);
2237 }
2238 else
2239 return _M_get_insert_unique_pos(__k);
2240 }
2241 else
2242 // Equivalent keys.
2243 return _Res(__pos._M_node, 0);
2244 }
2245
2246 template<typename _Key, typename _Val, typename _KeyOfValue,
2247 typename _Compare, typename _Alloc>
2248#if __cplusplus201402L >= 201103L
2249 template<typename _Arg, typename _NodeGen>
2250#else
2251 template<typename _NodeGen>
2252#endif
2253 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2254 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2255 _M_insert_unique_(const_iterator __position,
2256#if __cplusplus201402L >= 201103L
2257 _Arg&& __v,
2258#else
2259 const _Val& __v,
2260#endif
2261 _NodeGen& __node_gen)
2262 {
2263 pair<_Base_ptr, _Base_ptr> __res
2264 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
2265
2266 if (__res.second)
2267 return _M_insert_(__res.first, __res.second,
2268 _GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v),
2269 __node_gen);
2270 return iterator(__res.first);
2271 }
2272
2273 template<typename _Key, typename _Val, typename _KeyOfValue,
2274 typename _Compare, typename _Alloc>
2275 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2276 _Compare, _Alloc>::_Base_ptr,
2277 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2278 _Compare, _Alloc>::_Base_ptr>
2279 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2280 _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
2281 {
2282 iterator __pos = __position._M_const_cast();
2283 typedef pair<_Base_ptr, _Base_ptr> _Res;
2284
2285 // end()
2286 if (__pos._M_node == _M_end())
2287 {
2288 if (size() > 0
2289 && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
2290 return _Res(0, _M_rightmost());
2291 else
2292 return _M_get_insert_equal_pos(__k);
2293 }
2294 else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
2295 {
2296 // First, try before...
2297 iterator __before = __pos;
2298 if (__pos._M_node == _M_leftmost()) // begin()
2299 return _Res(_M_leftmost(), _M_leftmost());
2300 else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
2301 {
2302 if (_S_right(__before._M_node) == 0)
2303 return _Res(0, __before._M_node);
2304 else
2305 return _Res(__pos._M_node, __pos._M_node);
2306 }
2307 else
2308 return _M_get_insert_equal_pos(__k);
2309 }
2310 else
2311 {
2312 // ... then try after.
2313 iterator __after = __pos;
2314 if (__pos._M_node == _M_rightmost())
2315 return _Res(0, _M_rightmost());
2316 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
2317 {
2318 if (_S_right(__pos._M_node) == 0)
2319 return _Res(0, __pos._M_node);
2320 else
2321 return _Res(__after._M_node, __after._M_node);
2322 }
2323 else
2324 return _Res(0, 0);
2325 }
2326 }
2327
2328 template<typename _Key, typename _Val, typename _KeyOfValue,
2329 typename _Compare, typename _Alloc>
2330#if __cplusplus201402L >= 201103L
2331 template<typename _Arg, typename _NodeGen>
2332#else
2333 template<typename _NodeGen>
2334#endif
2335 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2336 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2337 _M_insert_equal_(const_iterator __position,
2338#if __cplusplus201402L >= 201103L
2339 _Arg&& __v,
2340#else
2341 const _Val& __v,
2342#endif
2343 _NodeGen& __node_gen)
2344 {
2345 pair<_Base_ptr, _Base_ptr> __res
2346 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
2347
2348 if (__res.second)
2349 return _M_insert_(__res.first, __res.second,
2350 _GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v),
2351 __node_gen);
2352
2353 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v)std::forward<_Arg>(__v));
2354 }
2355
2356#if __cplusplus201402L >= 201103L
2357 template<typename _Key, typename _Val, typename _KeyOfValue,
2358 typename _Compare, typename _Alloc>
2359 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2360 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2361 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
2362 {
2363 bool __insert_left = (__x != 0 || __p == _M_end()
2364 || _M_impl._M_key_compare(_S_key(__z),
2365 _S_key(__p)));
2366
2367 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
2368 this->_M_impl._M_header);
2369 ++_M_impl._M_node_count;
2370 return iterator(__z);
2371 }
2372
2373 template<typename _Key, typename _Val, typename _KeyOfValue,
2374 typename _Compare, typename _Alloc>
2375 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2376 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2377 _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
2378 {
2379 bool __insert_left = (__p == _M_end()
2380 || !_M_impl._M_key_compare(_S_key(__p),
2381 _S_key(__z)));
2382
2383 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
2384 this->_M_impl._M_header);
2385 ++_M_impl._M_node_count;
2386 return iterator(__z);
2387 }
2388
2389 template<typename _Key, typename _Val, typename _KeyOfValue,
2390 typename _Compare, typename _Alloc>
2391 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2392 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2393 _M_insert_equal_lower_node(_Link_type __z)
2394 {
2395 _Link_type __x = _M_begin();
2396 _Base_ptr __y = _M_end();
2397 while (__x != 0)
2398 {
2399 __y = __x;
2400 __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
2401 _S_left(__x) : _S_right(__x);
2402 }
2403 return _M_insert_lower_node(__y, __z);
2404 }
2405
2406 template<typename _Key, typename _Val, typename _KeyOfValue,
2407 typename _Compare, typename _Alloc>
2408 template<typename... _Args>
2409 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2410 _Compare, _Alloc>::iterator, bool>
2411 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2412 _M_emplace_unique(_Args&&... __args)
2413 {
2414 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2415
2416 __tryif (true)
2417 {
2418 typedef pair<iterator, bool> _Res;
2419 auto __res = _M_get_insert_unique_pos(_S_key(__z));
2420 if (__res.second)
2421 return _Res(_M_insert_node(__res.first, __res.second, __z), true);
2422
2423 _M_drop_node(__z);
2424 return _Res(iterator(__res.first), false);
2425 }
2426 __catch(...)if (false)
2427 {
2428 _M_drop_node(__z);
2429 __throw_exception_again;
2430 }
2431 }
2432
2433 template<typename _Key, typename _Val, typename _KeyOfValue,
2434 typename _Compare, typename _Alloc>
2435 template<typename... _Args>
2436 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2437 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2438 _M_emplace_equal(_Args&&... __args)
2439 {
2440 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2441
2442 __tryif (true)
2443 {
2444 auto __res = _M_get_insert_equal_pos(_S_key(__z));
2445 return _M_insert_node(__res.first, __res.second, __z);
2446 }
2447 __catch(...)if (false)
2448 {
2449 _M_drop_node(__z);
2450 __throw_exception_again;
2451 }
2452 }
2453
2454 template<typename _Key, typename _Val, typename _KeyOfValue,
2455 typename _Compare, typename _Alloc>
2456 template<typename... _Args>
2457 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2458 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2459 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
2460 {
2461 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2462
2463 __tryif (true)
2464 {
2465 auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
2466
2467 if (__res.second)
2468 return _M_insert_node(__res.first, __res.second, __z);
2469
2470 _M_drop_node(__z);
2471 return iterator(__res.first);
2472 }
2473 __catch(...)if (false)
2474 {
2475 _M_drop_node(__z);
2476 __throw_exception_again;
2477 }
2478 }
2479
2480 template<typename _Key, typename _Val, typename _KeyOfValue,
2481 typename _Compare, typename _Alloc>
2482 template<typename... _Args>
2483 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2484 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2485 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
2486 {
2487 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2488
2489 __tryif (true)
2490 {
2491 auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
2492
2493 if (__res.second)
2494 return _M_insert_node(__res.first, __res.second, __z);
2495
2496 return _M_insert_equal_lower_node(__z);
2497 }
2498 __catch(...)if (false)
2499 {
2500 _M_drop_node(__z);
2501 __throw_exception_again;
2502 }
2503 }
2504#endif
2505
2506
2507 template<typename _Key, typename _Val, typename _KeyOfValue,
2508 typename _Compare, typename _Alloc>
2509 void
2510 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2511 _M_erase_aux(const_iterator __position)
2512 {
2513 _Link_type __y =
2514 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
2515 (const_cast<_Base_ptr>(__position._M_node),
2516 this->_M_impl._M_header));
2517 _M_drop_node(__y);
2518 --_M_impl._M_node_count;
2519 }
2520
2521 template<typename _Key, typename _Val, typename _KeyOfValue,
2522 typename _Compare, typename _Alloc>
2523 void
2524 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2525 _M_erase_aux(const_iterator __first, const_iterator __last)
2526 {
2527 if (__first == begin() && __last == end())
2528 clear();
2529 else
2530 while (__first != __last)
2531 _M_erase_aux(__first++);
2532 }
2533
2534 template<typename _Key, typename _Val, typename _KeyOfValue,
2535 typename _Compare, typename _Alloc>
2536 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
2537 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2538 erase(const _Key& __x)
2539 {
2540 pair<iterator, iterator> __p = equal_range(__x);
2541 const size_type __old_size = size();
2542 _M_erase_aux(__p.first, __p.second);
2543 return __old_size - size();
2544 }
2545
2546 template<typename _Key, typename _Val, typename _KeyOfValue,
2547 typename _Compare, typename _Alloc>
2548 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2549 _Compare, _Alloc>::iterator
2550 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2551 find(const _Key& __k)
2552 {
2553 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
2554 return (__j == end()
2555 || _M_impl._M_key_compare(__k,
2556 _S_key(__j._M_node))) ? end() : __j;
2557 }
2558
2559 template<typename _Key, typename _Val, typename _KeyOfValue,
2560 typename _Compare, typename _Alloc>
2561 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2562 _Compare, _Alloc>::const_iterator
2563 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2564 find(const _Key& __k) const
2565 {
2566 const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
2567 return (__j == end()
2568 || _M_impl._M_key_compare(__k,
2569 _S_key(__j._M_node))) ? end() : __j;
2570 }
2571
2572 template<typename _Key, typename _Val, typename _KeyOfValue,
2573 typename _Compare, typename _Alloc>
2574 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
2575 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2576 count(const _Key& __k) const
2577 {
2578 pair<const_iterator, const_iterator> __p = equal_range(__k);
2579 const size_type __n = std::distance(__p.first, __p.second);
2580 return __n;
2581 }
2582
2583 _GLIBCXX_PURE__attribute__ ((__pure__)) unsigned int
2584 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
2585 const _Rb_tree_node_base* __root) throw ();
2586
2587 template<typename _Key, typename _Val, typename _KeyOfValue,
2588 typename _Compare, typename _Alloc>
2589 bool
2590 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
2591 {
2592 if (_M_impl._M_node_count == 0 || begin() == end())
2593 return _M_impl._M_node_count == 0 && begin() == end()
2594 && this->_M_impl._M_header._M_left == _M_end()
2595 && this->_M_impl._M_header._M_right == _M_end();
2596
2597 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
2598 for (const_iterator __it = begin(); __it != end(); ++__it)
2599 {
2600 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
2601 _Const_Link_type __L = _S_left(__x);
2602 _Const_Link_type __R = _S_right(__x);
2603
2604 if (__x->_M_color == _S_red)
2605 if ((__L && __L->_M_color == _S_red)
2606 || (__R && __R->_M_color == _S_red))
2607 return false;
2608
2609 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
2610 return false;
2611 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
2612 return false;
2613
2614 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
2615 return false;
2616 }
2617
2618 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
2619 return false;
2620 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
2621 return false;
2622 return true;
2623 }
2624
2625#if __cplusplus201402L > 201402L
2626 // Allow access to internals of compatible _Rb_tree specializations.
2627 template<typename _Key, typename _Val, typename _Sel, typename _Cmp1,
2628 typename _Alloc, typename _Cmp2>
2629 struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
2630 _Cmp2>
2631 {
2632 private:
2633 friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
2634
2635 static auto&
2636 _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
2637 { return __tree._M_impl; }
2638 };
2639#endif // C++17
2640
2641_GLIBCXX_END_NAMESPACE_VERSION
2642} // namespace
2643
2644#endif