LLVM  10.0.0svn
Record.cpp
Go to the documentation of this file.
1 //===- Record.cpp - Record implementation ---------------------------------===//
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 // Implement the tablegen record classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSet.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/SMLoc.h"
30 #include "llvm/TableGen/Error.h"
31 #include "llvm/TableGen/Record.h"
32 #include <cassert>
33 #include <cstdint>
34 #include <memory>
35 #include <map>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 using namespace llvm;
41 
42 #define DEBUG_TYPE "tblgen-records"
43 
45 
46 STATISTIC(CodeInitsConstructed,
47  "The total number of unique CodeInits constructed");
48 
49 //===----------------------------------------------------------------------===//
50 // Type implementations
51 //===----------------------------------------------------------------------===//
52 
53 BitRecTy BitRecTy::Shared;
54 CodeRecTy CodeRecTy::Shared;
55 IntRecTy IntRecTy::Shared;
56 StringRecTy StringRecTy::Shared;
57 DagRecTy DagRecTy::Shared;
58 
59 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
61 #endif
62 
64  if (!ListTy)
65  ListTy = new(Allocator) ListRecTy(this);
66  return ListTy;
67 }
68 
69 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
70  assert(RHS && "NULL pointer");
71  return Kind == RHS->getRecTyKind();
72 }
73 
74 bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
75 
76 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
78  return true;
79  if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
80  return BitsTy->getNumBits() == 1;
81  return false;
82 }
83 
84 BitsRecTy *BitsRecTy::get(unsigned Sz) {
85  static std::vector<BitsRecTy*> Shared;
86  if (Sz >= Shared.size())
87  Shared.resize(Sz + 1);
88  BitsRecTy *&Ty = Shared[Sz];
89  if (!Ty)
90  Ty = new(Allocator) BitsRecTy(Sz);
91  return Ty;
92 }
93 
94 std::string BitsRecTy::getAsString() const {
95  return "bits<" + utostr(Size) + ">";
96 }
97 
98 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
99  if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
100  return cast<BitsRecTy>(RHS)->Size == Size;
101  RecTyKind kind = RHS->getRecTyKind();
102  return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
103 }
104 
105 bool BitsRecTy::typeIsA(const RecTy *RHS) const {
106  if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
107  return RHSb->Size == Size;
108  return false;
109 }
110 
111 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
112  RecTyKind kind = RHS->getRecTyKind();
113  return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
114 }
115 
116 bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
117  RecTyKind Kind = RHS->getRecTyKind();
118  return Kind == CodeRecTyKind || Kind == StringRecTyKind;
119 }
120 
121 std::string StringRecTy::getAsString() const {
122  return "string";
123 }
124 
125 bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
126  RecTyKind Kind = RHS->getRecTyKind();
127  return Kind == StringRecTyKind || Kind == CodeRecTyKind;
128 }
129 
130 std::string ListRecTy::getAsString() const {
131  return "list<" + Ty->getAsString() + ">";
132 }
133 
134 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
135  if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
136  return Ty->typeIsConvertibleTo(ListTy->getElementType());
137  return false;
138 }
139 
140 bool ListRecTy::typeIsA(const RecTy *RHS) const {
141  if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
142  return getElementType()->typeIsA(RHSl->getElementType());
143  return false;
144 }
145 
146 std::string DagRecTy::getAsString() const {
147  return "dag";
148 }
149 
151  ArrayRef<Record *> Classes) {
152  ID.AddInteger(Classes.size());
153  for (Record *R : Classes)
154  ID.AddPointer(R);
155 }
156 
158  if (UnsortedClasses.empty()) {
159  static RecordRecTy AnyRecord(0);
160  return &AnyRecord;
161  }
162 
163  FoldingSet<RecordRecTy> &ThePool =
164  UnsortedClasses[0]->getRecords().RecordTypePool;
165 
166  SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
167  UnsortedClasses.end());
168  llvm::sort(Classes, [](Record *LHS, Record *RHS) {
169  return LHS->getNameInitAsString() < RHS->getNameInitAsString();
170  });
171 
173  ProfileRecordRecTy(ID, Classes);
174 
175  void *IP = nullptr;
176  if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
177  return Ty;
178 
179 #ifndef NDEBUG
180  // Check for redundancy.
181  for (unsigned i = 0; i < Classes.size(); ++i) {
182  for (unsigned j = 0; j < Classes.size(); ++j) {
183  assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
184  }
185  assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
186  }
187 #endif
188 
189  void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()),
190  alignof(RecordRecTy));
191  RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
192  std::uninitialized_copy(Classes.begin(), Classes.end(),
193  Ty->getTrailingObjects<Record *>());
194  ThePool.InsertNode(Ty, IP);
195  return Ty;
196 }
197 
199  ProfileRecordRecTy(ID, getClasses());
200 }
201 
202 std::string RecordRecTy::getAsString() const {
203  if (NumClasses == 1)
204  return getClasses()[0]->getNameInitAsString();
205 
206  std::string Str = "{";
207  bool First = true;
208  for (Record *R : getClasses()) {
209  if (!First)
210  Str += ", ";
211  First = false;
212  Str += R->getNameInitAsString();
213  }
214  Str += "}";
215  return Str;
216 }
217 
218 bool RecordRecTy::isSubClassOf(Record *Class) const {
219  return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
220  return MySuperClass == Class ||
221  MySuperClass->isSubClassOf(Class);
222  });
223 }
224 
225 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
226  if (this == RHS)
227  return true;
228 
229  const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
230  if (!RTy)
231  return false;
232 
233  return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
234  return isSubClassOf(TargetClass);
235  });
236 }
237 
238 bool RecordRecTy::typeIsA(const RecTy *RHS) const {
239  return typeIsConvertibleTo(RHS);
240 }
241 
243  SmallVector<Record *, 4> CommonSuperClasses;
245 
246  Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end());
247 
248  while (!Stack.empty()) {
249  Record *R = Stack.back();
250  Stack.pop_back();
251 
252  if (T2->isSubClassOf(R)) {
253  CommonSuperClasses.push_back(R);
254  } else {
255  R->getDirectSuperClasses(Stack);
256  }
257  }
258 
259  return RecordRecTy::get(CommonSuperClasses);
260 }
261 
263  if (T1 == T2)
264  return T1;
265 
266  if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
267  if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
268  return resolveRecordTypes(RecTy1, RecTy2);
269  }
270 
271  if (T1->typeIsConvertibleTo(T2))
272  return T2;
273  if (T2->typeIsConvertibleTo(T1))
274  return T1;
275 
276  if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
277  if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
278  RecTy* NewType = resolveTypes(ListTy1->getElementType(),
279  ListTy2->getElementType());
280  if (NewType)
281  return NewType->getListTy();
282  }
283  }
284 
285  return nullptr;
286 }
287 
288 //===----------------------------------------------------------------------===//
289 // Initializer implementations
290 //===----------------------------------------------------------------------===//
291 
292 void Init::anchor() {}
293 
294 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
295 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
296 #endif
297 
299  static UnsetInit TheInit;
300  return &TheInit;
301 }
302 
304  return const_cast<UnsetInit *>(this);
305 }
306 
308  return const_cast<UnsetInit *>(this);
309 }
310 
312  static BitInit True(true);
313  static BitInit False(false);
314 
315  return V ? &True : &False;
316 }
317 
319  if (isa<BitRecTy>(Ty))
320  return const_cast<BitInit *>(this);
321 
322  if (isa<IntRecTy>(Ty))
323  return IntInit::get(getValue());
324 
325  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
326  // Can only convert single bit.
327  if (BRT->getNumBits() == 1)
328  return BitsInit::get(const_cast<BitInit *>(this));
329  }
330 
331  return nullptr;
332 }
333 
334 static void
336  ID.AddInteger(Range.size());
337 
338  for (Init *I : Range)
339  ID.AddPointer(I);
340 }
341 
343  static FoldingSet<BitsInit> ThePool;
344 
346  ProfileBitsInit(ID, Range);
347 
348  void *IP = nullptr;
349  if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
350  return I;
351 
352  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
353  alignof(BitsInit));
354  BitsInit *I = new(Mem) BitsInit(Range.size());
355  std::uninitialized_copy(Range.begin(), Range.end(),
356  I->getTrailingObjects<Init *>());
357  ThePool.InsertNode(I, IP);
358  return I;
359 }
360 
362  ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
363 }
364 
366  if (isa<BitRecTy>(Ty)) {
367  if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
368  return getBit(0);
369  }
370 
371  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
372  // If the number of bits is right, return it. Otherwise we need to expand
373  // or truncate.
374  if (getNumBits() != BRT->getNumBits()) return nullptr;
375  return const_cast<BitsInit *>(this);
376  }
377 
378  if (isa<IntRecTy>(Ty)) {
379  int64_t Result = 0;
380  for (unsigned i = 0, e = getNumBits(); i != e; ++i)
381  if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
382  Result |= static_cast<int64_t>(Bit->getValue()) << i;
383  else
384  return nullptr;
385  return IntInit::get(Result);
386  }
387 
388  return nullptr;
389 }
390 
391 Init *
393  SmallVector<Init *, 16> NewBits(Bits.size());
394 
395  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
396  if (Bits[i] >= getNumBits())
397  return nullptr;
398  NewBits[i] = getBit(Bits[i]);
399  }
400  return BitsInit::get(NewBits);
401 }
402 
403 bool BitsInit::isConcrete() const {
404  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
405  if (!getBit(i)->isConcrete())
406  return false;
407  }
408  return true;
409 }
410 
411 std::string BitsInit::getAsString() const {
412  std::string Result = "{ ";
413  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
414  if (i) Result += ", ";
415  if (Init *Bit = getBit(e-i-1))
416  Result += Bit->getAsString();
417  else
418  Result += "*";
419  }
420  return Result + " }";
421 }
422 
423 // resolveReferences - If there are any field references that refer to fields
424 // that have been filled in, we can propagate the values now.
426  bool Changed = false;
427  SmallVector<Init *, 16> NewBits(getNumBits());
428 
429  Init *CachedBitVarRef = nullptr;
430  Init *CachedBitVarResolved = nullptr;
431 
432  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
433  Init *CurBit = getBit(i);
434  Init *NewBit = CurBit;
435 
436  if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
437  if (CurBitVar->getBitVar() != CachedBitVarRef) {
438  CachedBitVarRef = CurBitVar->getBitVar();
439  CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
440  }
441 
442  NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
443  } else {
444  // getBit(0) implicitly converts int and bits<1> values to bit.
445  NewBit = CurBit->resolveReferences(R)->getBit(0);
446  }
447 
448  if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
449  NewBit = CurBit;
450  NewBits[i] = NewBit;
451  Changed |= CurBit != NewBit;
452  }
453 
454  if (Changed)
455  return BitsInit::get(NewBits);
456 
457  return const_cast<BitsInit *>(this);
458 }
459 
460 IntInit *IntInit::get(int64_t V) {
461  static std::map<int64_t, IntInit*> ThePool;
462 
463  IntInit *&I = ThePool[V];
464  if (!I) I = new(Allocator) IntInit(V);
465  return I;
466 }
467 
468 std::string IntInit::getAsString() const {
469  return itostr(Value);
470 }
471 
472 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
473  // For example, with NumBits == 4, we permit Values from [-7 .. 15].
474  return (NumBits >= sizeof(Value) * 8) ||
475  (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
476 }
477 
479  if (isa<IntRecTy>(Ty))
480  return const_cast<IntInit *>(this);
481 
482  if (isa<BitRecTy>(Ty)) {
483  int64_t Val = getValue();
484  if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
485  return BitInit::get(Val != 0);
486  }
487 
488  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
489  int64_t Value = getValue();
490  // Make sure this bitfield is large enough to hold the integer value.
491  if (!canFitInBitfield(Value, BRT->getNumBits()))
492  return nullptr;
493 
494  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
495  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
496  NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0));
497 
498  return BitsInit::get(NewBits);
499  }
500 
501  return nullptr;
502 }
503 
504 Init *
506  SmallVector<Init *, 16> NewBits(Bits.size());
507 
508  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
509  if (Bits[i] >= 64)
510  return nullptr;
511 
512  NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
513  }
514  return BitsInit::get(NewBits);
515 }
516 
518  static StringSet<BumpPtrAllocator &> ThePool(Allocator);
519 
520  CodeInitsConstructed++;
521 
522  // Unlike StringMap, StringSet doesn't accept empty keys.
523  if (V.empty())
524  return new (Allocator) CodeInit("", Loc);
525 
526  // Location tracking prevents us from de-duping CodeInits as we're never
527  // called with the same string and same location twice. However, we can at
528  // least de-dupe the strings for a modest saving.
529  auto &Entry = *ThePool.insert(V).first;
530  return new(Allocator) CodeInit(Entry.getKey(), Loc);
531 }
532 
534  static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
535 
536  auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
537  if (!Entry.second)
538  Entry.second = new(Allocator) StringInit(Entry.getKey());
539  return Entry.second;
540 }
541 
543  if (isa<StringRecTy>(Ty))
544  return const_cast<StringInit *>(this);
545  if (isa<CodeRecTy>(Ty))
546  return CodeInit::get(getValue(), SMLoc());
547 
548  return nullptr;
549 }
550 
552  if (isa<CodeRecTy>(Ty))
553  return const_cast<CodeInit *>(this);
554  if (isa<StringRecTy>(Ty))
555  return StringInit::get(getValue());
556 
557  return nullptr;
558 }
559 
561  ArrayRef<Init *> Range,
562  RecTy *EltTy) {
563  ID.AddInteger(Range.size());
564  ID.AddPointer(EltTy);
565 
566  for (Init *I : Range)
567  ID.AddPointer(I);
568 }
569 
571  static FoldingSet<ListInit> ThePool;
572 
574  ProfileListInit(ID, Range, EltTy);
575 
576  void *IP = nullptr;
577  if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
578  return I;
579 
580  assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
581  cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
582 
583  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
584  alignof(ListInit));
585  ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
586  std::uninitialized_copy(Range.begin(), Range.end(),
587  I->getTrailingObjects<Init *>());
588  ThePool.InsertNode(I, IP);
589  return I;
590 }
591 
593  RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
594 
595  ProfileListInit(ID, getValues(), EltTy);
596 }
597 
599  if (getType() == Ty)
600  return const_cast<ListInit*>(this);
601 
602  if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
603  SmallVector<Init*, 8> Elements;
604  Elements.reserve(getValues().size());
605 
606  // Verify that all of the elements of the list are subclasses of the
607  // appropriate class!
608  bool Changed = false;
609  RecTy *ElementType = LRT->getElementType();
610  for (Init *I : getValues())
611  if (Init *CI = I->convertInitializerTo(ElementType)) {
612  Elements.push_back(CI);
613  if (CI != I)
614  Changed = true;
615  } else
616  return nullptr;
617 
618  if (!Changed)
619  return const_cast<ListInit*>(this);
620  return ListInit::get(Elements, ElementType);
621  }
622 
623  return nullptr;
624 }
625 
628  Vals.reserve(Elements.size());
629  for (unsigned Element : Elements) {
630  if (Element >= size())
631  return nullptr;
632  Vals.push_back(getElement(Element));
633  }
634  return ListInit::get(Vals, getElementType());
635 }
636 
638  assert(i < NumValues && "List element index out of range!");
639  DefInit *DI = dyn_cast<DefInit>(getElement(i));
640  if (!DI)
641  PrintFatalError("Expected record in list!");
642  return DI->getDef();
643 }
644 
646  SmallVector<Init*, 8> Resolved;
647  Resolved.reserve(size());
648  bool Changed = false;
649 
650  for (Init *CurElt : getValues()) {
651  Init *E = CurElt->resolveReferences(R);
652  Changed |= E != CurElt;
653  Resolved.push_back(E);
654  }
655 
656  if (Changed)
657  return ListInit::get(Resolved, getElementType());
658  return const_cast<ListInit *>(this);
659 }
660 
661 bool ListInit::isConcrete() const {
662  for (Init *Element : *this) {
663  if (!Element->isConcrete())
664  return false;
665  }
666  return true;
667 }
668 
669 std::string ListInit::getAsString() const {
670  std::string Result = "[";
671  const char *sep = "";
672  for (Init *Element : *this) {
673  Result += sep;
674  sep = ", ";
675  Result += Element->getAsString();
676  }
677  return Result + "]";
678 }
679 
680 Init *OpInit::getBit(unsigned Bit) const {
681  if (getType() == BitRecTy::get())
682  return const_cast<OpInit*>(this);
683  return VarBitInit::get(const_cast<OpInit*>(this), Bit);
684 }
685 
686 static void
688  ID.AddInteger(Opcode);
689  ID.AddPointer(Op);
690  ID.AddPointer(Type);
691 }
692 
694  static FoldingSet<UnOpInit> ThePool;
695 
697  ProfileUnOpInit(ID, Opc, LHS, Type);
698 
699  void *IP = nullptr;
700  if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
701  return I;
702 
703  UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
704  ThePool.InsertNode(I, IP);
705  return I;
706 }
707 
709  ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
710 }
711 
712 Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
713  switch (getOpcode()) {
714  case CAST:
715  if (isa<StringRecTy>(getType())) {
716  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
717  return LHSs;
718 
719  if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
720  return StringInit::get(LHSd->getAsString());
721 
722  if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
723  return StringInit::get(LHSi->getAsString());
724  } else if (isa<RecordRecTy>(getType())) {
725  if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
726  if (!CurRec && !IsFinal)
727  break;
728  assert(CurRec && "NULL pointer");
729  Record *D;
730 
731  // Self-references are allowed, but their resolution is delayed until
732  // the final resolve to ensure that we get the correct type for them.
733  if (Name == CurRec->getNameInit()) {
734  if (!IsFinal)
735  break;
736  D = CurRec;
737  } else {
738  D = CurRec->getRecords().getDef(Name->getValue());
739  if (!D) {
740  if (IsFinal)
741  PrintFatalError(CurRec->getLoc(),
742  Twine("Undefined reference to record: '") +
743  Name->getValue() + "'\n");
744  break;
745  }
746  }
747 
748  DefInit *DI = DefInit::get(D);
749  if (!DI->getType()->typeIsA(getType())) {
750  PrintFatalError(CurRec->getLoc(),
751  Twine("Expected type '") +
752  getType()->getAsString() + "', got '" +
753  DI->getType()->getAsString() + "' in: " +
754  getAsString() + "\n");
755  }
756  return DI;
757  }
758  }
759 
760  if (Init *NewInit = LHS->convertInitializerTo(getType()))
761  return NewInit;
762  break;
763 
764  case HEAD:
765  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
766  assert(!LHSl->empty() && "Empty list in head");
767  return LHSl->getElement(0);
768  }
769  break;
770 
771  case TAIL:
772  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
773  assert(!LHSl->empty() && "Empty list in tail");
774  // Note the +1. We can't just pass the result of getValues()
775  // directly.
776  return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
777  }
778  break;
779 
780  case SIZE:
781  if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
782  return IntInit::get(LHSl->size());
783  break;
784 
785  case EMPTY:
786  if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
787  return IntInit::get(LHSl->empty());
788  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
789  return IntInit::get(LHSs->getValue().empty());
790  break;
791  }
792  return const_cast<UnOpInit *>(this);
793 }
794 
796  Init *lhs = LHS->resolveReferences(R);
797 
798  if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
799  return (UnOpInit::get(getOpcode(), lhs, getType()))
800  ->Fold(R.getCurrentRecord(), R.isFinal());
801  return const_cast<UnOpInit *>(this);
802 }
803 
804 std::string UnOpInit::getAsString() const {
805  std::string Result;
806  switch (getOpcode()) {
807  case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
808  case HEAD: Result = "!head"; break;
809  case TAIL: Result = "!tail"; break;
810  case SIZE: Result = "!size"; break;
811  case EMPTY: Result = "!empty"; break;
812  }
813  return Result + "(" + LHS->getAsString() + ")";
814 }
815 
816 static void
817 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
818  RecTy *Type) {
819  ID.AddInteger(Opcode);
820  ID.AddPointer(LHS);
821  ID.AddPointer(RHS);
822  ID.AddPointer(Type);
823 }
824 
826  Init *RHS, RecTy *Type) {
827  static FoldingSet<BinOpInit> ThePool;
828 
830  ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
831 
832  void *IP = nullptr;
833  if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
834  return I;
835 
836  BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
837  ThePool.InsertNode(I, IP);
838  return I;
839 }
840 
842  ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
843 }
844 
846  const StringInit *I1) {
848  Concat.append(I1->getValue());
849  return StringInit::get(Concat);
850 }
851 
853  // Shortcut for the common case of concatenating two strings.
854  if (const StringInit *I0s = dyn_cast<StringInit>(I0))
855  if (const StringInit *I1s = dyn_cast<StringInit>(I1))
856  return ConcatStringInits(I0s, I1s);
858 }
859 
860 static ListInit *ConcatListInits(const ListInit *LHS,
861  const ListInit *RHS) {
863  Args.insert(Args.end(), LHS->begin(), LHS->end());
864  Args.insert(Args.end(), RHS->begin(), RHS->end());
865  return ListInit::get(Args, LHS->getElementType());
866 }
867 
869  assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list");
870 
871  // Shortcut for the common case of concatenating two lists.
872  if (const ListInit *LHSList = dyn_cast<ListInit>(LHS))
873  if (const ListInit *RHSList = dyn_cast<ListInit>(RHS))
874  return ConcatListInits(LHSList, RHSList);
875  return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType());
876 }
877 
879  return BinOpInit::get(BinOpInit::LISTSPLAT, LHS, RHS, LHS->getType());
880 }
881 
882 Init *BinOpInit::Fold(Record *CurRec) const {
883  switch (getOpcode()) {
884  case CONCAT: {
885  DagInit *LHSs = dyn_cast<DagInit>(LHS);
886  DagInit *RHSs = dyn_cast<DagInit>(RHS);
887  if (LHSs && RHSs) {
888  DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
889  DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
890  if (!LOp || !ROp)
891  break;
892  if (LOp->getDef() != ROp->getDef()) {
893  PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
894  LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
895  "'");
896  }
899  for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
900  Args.push_back(LHSs->getArg(i));
901  ArgNames.push_back(LHSs->getArgName(i));
902  }
903  for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
904  Args.push_back(RHSs->getArg(i));
905  ArgNames.push_back(RHSs->getArgName(i));
906  }
907  return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
908  }
909  break;
910  }
911  case LISTCONCAT: {
912  ListInit *LHSs = dyn_cast<ListInit>(LHS);
913  ListInit *RHSs = dyn_cast<ListInit>(RHS);
914  if (LHSs && RHSs) {
916  Args.insert(Args.end(), LHSs->begin(), LHSs->end());
917  Args.insert(Args.end(), RHSs->begin(), RHSs->end());
918  return ListInit::get(Args, LHSs->getElementType());
919  }
920  break;
921  }
922  case LISTSPLAT: {
924  IntInit *Size = dyn_cast<IntInit>(RHS);
925  if (Value && Size) {
926  SmallVector<Init *, 8> Args(Size->getValue(), Value);
927  return ListInit::get(Args, Value->getType());
928  }
929  break;
930  }
931  case STRCONCAT: {
932  StringInit *LHSs = dyn_cast<StringInit>(LHS);
933  StringInit *RHSs = dyn_cast<StringInit>(RHS);
934  if (LHSs && RHSs)
935  return ConcatStringInits(LHSs, RHSs);
936  break;
937  }
938  case EQ:
939  case NE:
940  case LE:
941  case LT:
942  case GE:
943  case GT: {
944  // try to fold eq comparison for 'bit' and 'int', otherwise fallback
945  // to string objects.
946  IntInit *L =
947  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
948  IntInit *R =
949  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
950 
951  if (L && R) {
952  bool Result;
953  switch (getOpcode()) {
954  case EQ: Result = L->getValue() == R->getValue(); break;
955  case NE: Result = L->getValue() != R->getValue(); break;
956  case LE: Result = L->getValue() <= R->getValue(); break;
957  case LT: Result = L->getValue() < R->getValue(); break;
958  case GE: Result = L->getValue() >= R->getValue(); break;
959  case GT: Result = L->getValue() > R->getValue(); break;
960  default: llvm_unreachable("unhandled comparison");
961  }
962  return BitInit::get(Result);
963  }
964 
965  if (getOpcode() == EQ || getOpcode() == NE) {
966  StringInit *LHSs = dyn_cast<StringInit>(LHS);
967  StringInit *RHSs = dyn_cast<StringInit>(RHS);
968 
969  // Make sure we've resolved
970  if (LHSs && RHSs) {
971  bool Equal = LHSs->getValue() == RHSs->getValue();
972  return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
973  }
974  }
975 
976  break;
977  }
978  case ADD:
979  case MUL:
980  case AND:
981  case OR:
982  case SHL:
983  case SRA:
984  case SRL: {
985  IntInit *LHSi =
986  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
987  IntInit *RHSi =
988  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
989  if (LHSi && RHSi) {
990  int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
991  int64_t Result;
992  switch (getOpcode()) {
993  default: llvm_unreachable("Bad opcode!");
994  case ADD: Result = LHSv + RHSv; break;
995  case MUL: Result = LHSv * RHSv; break;
996  case AND: Result = LHSv & RHSv; break;
997  case OR: Result = LHSv | RHSv; break;
998  case SHL: Result = LHSv << RHSv; break;
999  case SRA: Result = LHSv >> RHSv; break;
1000  case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
1001  }
1002  return IntInit::get(Result);
1003  }
1004  break;
1005  }
1006  }
1007  return const_cast<BinOpInit *>(this);
1008 }
1009 
1011  Init *lhs = LHS->resolveReferences(R);
1012  Init *rhs = RHS->resolveReferences(R);
1013 
1014  if (LHS != lhs || RHS != rhs)
1015  return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
1016  ->Fold(R.getCurrentRecord());
1017  return const_cast<BinOpInit *>(this);
1018 }
1019 
1020 std::string BinOpInit::getAsString() const {
1021  std::string Result;
1022  switch (getOpcode()) {
1023  case CONCAT: Result = "!con"; break;
1024  case ADD: Result = "!add"; break;
1025  case MUL: Result = "!mul"; break;
1026  case AND: Result = "!and"; break;
1027  case OR: Result = "!or"; break;
1028  case SHL: Result = "!shl"; break;
1029  case SRA: Result = "!sra"; break;
1030  case SRL: Result = "!srl"; break;
1031  case EQ: Result = "!eq"; break;
1032  case NE: Result = "!ne"; break;
1033  case LE: Result = "!le"; break;
1034  case LT: Result = "!lt"; break;
1035  case GE: Result = "!ge"; break;
1036  case GT: Result = "!gt"; break;
1037  case LISTCONCAT: Result = "!listconcat"; break;
1038  case LISTSPLAT: Result = "!listsplat"; break;
1039  case STRCONCAT: Result = "!strconcat"; break;
1040  }
1041  return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
1042 }
1043 
1044 static void
1045 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
1046  Init *RHS, RecTy *Type) {
1047  ID.AddInteger(Opcode);
1048  ID.AddPointer(LHS);
1049  ID.AddPointer(MHS);
1050  ID.AddPointer(RHS);
1051  ID.AddPointer(Type);
1052 }
1053 
1055  RecTy *Type) {
1056  static FoldingSet<TernOpInit> ThePool;
1057 
1059  ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
1060 
1061  void *IP = nullptr;
1062  if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1063  return I;
1064 
1065  TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
1066  ThePool.InsertNode(I, IP);
1067  return I;
1068 }
1069 
1071  ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1072 }
1073 
1074 static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
1075  MapResolver R(CurRec);
1076  R.set(LHS, MHSe);
1077  return RHS->resolveReferences(R);
1078 }
1079 
1080 static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
1081  Record *CurRec) {
1082  bool Change = false;
1083  Init *Val = ForeachApply(LHS, MHSd->getOperator(), RHS, CurRec);
1084  if (Val != MHSd->getOperator())
1085  Change = true;
1086 
1088  for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1089  Init *Arg = MHSd->getArg(i);
1090  Init *NewArg;
1091  StringInit *ArgName = MHSd->getArgName(i);
1092 
1093  if (DagInit *Argd = dyn_cast<DagInit>(Arg))
1094  NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
1095  else
1096  NewArg = ForeachApply(LHS, Arg, RHS, CurRec);
1097 
1098  NewArgs.push_back(std::make_pair(NewArg, ArgName));
1099  if (Arg != NewArg)
1100  Change = true;
1101  }
1102 
1103  if (Change)
1104  return DagInit::get(Val, nullptr, NewArgs);
1105  return MHSd;
1106 }
1107 
1108 // Applies RHS to all elements of MHS, using LHS as a temp variable.
1109 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1110  Record *CurRec) {
1111  if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
1112  return ForeachDagApply(LHS, MHSd, RHS, CurRec);
1113 
1114  if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
1115  SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1116 
1117  for (Init *&Item : NewList) {
1118  Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec);
1119  if (NewItem != Item)
1120  Item = NewItem;
1121  }
1122  return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1123  }
1124 
1125  return nullptr;
1126 }
1127 
1128 Init *TernOpInit::Fold(Record *CurRec) const {
1129  switch (getOpcode()) {
1130  case SUBST: {
1131  DefInit *LHSd = dyn_cast<DefInit>(LHS);
1132  VarInit *LHSv = dyn_cast<VarInit>(LHS);
1133  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1134 
1135  DefInit *MHSd = dyn_cast<DefInit>(MHS);
1136  VarInit *MHSv = dyn_cast<VarInit>(MHS);
1137  StringInit *MHSs = dyn_cast<StringInit>(MHS);
1138 
1139  DefInit *RHSd = dyn_cast<DefInit>(RHS);
1140  VarInit *RHSv = dyn_cast<VarInit>(RHS);
1141  StringInit *RHSs = dyn_cast<StringInit>(RHS);
1142 
1143  if (LHSd && MHSd && RHSd) {
1144  Record *Val = RHSd->getDef();
1145  if (LHSd->getAsString() == RHSd->getAsString())
1146  Val = MHSd->getDef();
1147  return DefInit::get(Val);
1148  }
1149  if (LHSv && MHSv && RHSv) {
1150  std::string Val = RHSv->getName();
1151  if (LHSv->getAsString() == RHSv->getAsString())
1152  Val = MHSv->getName();
1153  return VarInit::get(Val, getType());
1154  }
1155  if (LHSs && MHSs && RHSs) {
1156  std::string Val = RHSs->getValue();
1157 
1158  std::string::size_type found;
1159  std::string::size_type idx = 0;
1160  while (true) {
1161  found = Val.find(LHSs->getValue(), idx);
1162  if (found == std::string::npos)
1163  break;
1164  Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1165  idx = found + MHSs->getValue().size();
1166  }
1167 
1168  return StringInit::get(Val);
1169  }
1170  break;
1171  }
1172 
1173  case FOREACH: {
1174  if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
1175  return Result;
1176  break;
1177  }
1178 
1179  case IF: {
1180  if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
1181  LHS->convertInitializerTo(IntRecTy::get()))) {
1182  if (LHSi->getValue())
1183  return MHS;
1184  return RHS;
1185  }
1186  break;
1187  }
1188 
1189  case DAG: {
1190  ListInit *MHSl = dyn_cast<ListInit>(MHS);
1191  ListInit *RHSl = dyn_cast<ListInit>(RHS);
1192  bool MHSok = MHSl || isa<UnsetInit>(MHS);
1193  bool RHSok = RHSl || isa<UnsetInit>(RHS);
1194 
1195  if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
1196  break; // Typically prevented by the parser, but might happen with template args
1197 
1198  if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
1200  unsigned Size = MHSl ? MHSl->size() : RHSl->size();
1201  for (unsigned i = 0; i != Size; ++i) {
1202  Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
1203  Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
1204  if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
1205  return const_cast<TernOpInit *>(this);
1206  Children.emplace_back(Node, dyn_cast<StringInit>(Name));
1207  }
1208  return DagInit::get(LHS, nullptr, Children);
1209  }
1210  break;
1211  }
1212  }
1213 
1214  return const_cast<TernOpInit *>(this);
1215 }
1216 
1218  Init *lhs = LHS->resolveReferences(R);
1219 
1220  if (getOpcode() == IF && lhs != LHS) {
1221  if (IntInit *Value = dyn_cast_or_null<IntInit>(
1223  // Short-circuit
1224  if (Value->getValue())
1225  return MHS->resolveReferences(R);
1226  return RHS->resolveReferences(R);
1227  }
1228  }
1229 
1230  Init *mhs = MHS->resolveReferences(R);
1231  Init *rhs;
1232 
1233  if (getOpcode() == FOREACH) {
1234  ShadowResolver SR(R);
1235  SR.addShadow(lhs);
1236  rhs = RHS->resolveReferences(SR);
1237  } else {
1238  rhs = RHS->resolveReferences(R);
1239  }
1240 
1241  if (LHS != lhs || MHS != mhs || RHS != rhs)
1242  return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
1243  ->Fold(R.getCurrentRecord());
1244  return const_cast<TernOpInit *>(this);
1245 }
1246 
1247 std::string TernOpInit::getAsString() const {
1248  std::string Result;
1249  bool UnquotedLHS = false;
1250  switch (getOpcode()) {
1251  case SUBST: Result = "!subst"; break;
1252  case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
1253  case IF: Result = "!if"; break;
1254  case DAG: Result = "!dag"; break;
1255  }
1256  return (Result + "(" +
1257  (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
1258  ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
1259 }
1260 
1262  Init *Start, Init *List, Init *Expr,
1263  RecTy *Type) {
1264  ID.AddPointer(Start);
1265  ID.AddPointer(List);
1266  ID.AddPointer(A);
1267  ID.AddPointer(B);
1268  ID.AddPointer(Expr);
1269  ID.AddPointer(Type);
1270 }
1271 
1273  Init *Expr, RecTy *Type) {
1274  static FoldingSet<FoldOpInit> ThePool;
1275 
1277  ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
1278 
1279  void *IP = nullptr;
1280  if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1281  return I;
1282 
1283  FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
1284  ThePool.InsertNode(I, IP);
1285  return I;
1286 }
1287 
1289  ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
1290 }
1291 
1292 Init *FoldOpInit::Fold(Record *CurRec) const {
1293  if (ListInit *LI = dyn_cast<ListInit>(List)) {
1294  Init *Accum = Start;
1295  for (Init *Elt : *LI) {
1296  MapResolver R(CurRec);
1297  R.set(A, Accum);
1298  R.set(B, Elt);
1299  Accum = Expr->resolveReferences(R);
1300  }
1301  return Accum;
1302  }
1303  return const_cast<FoldOpInit *>(this);
1304 }
1305 
1307  Init *NewStart = Start->resolveReferences(R);
1308  Init *NewList = List->resolveReferences(R);
1309  ShadowResolver SR(R);
1310  SR.addShadow(A);
1311  SR.addShadow(B);
1312  Init *NewExpr = Expr->resolveReferences(SR);
1313 
1314  if (Start == NewStart && List == NewList && Expr == NewExpr)
1315  return const_cast<FoldOpInit *>(this);
1316 
1317  return get(NewStart, NewList, A, B, NewExpr, getType())
1318  ->Fold(R.getCurrentRecord());
1319 }
1320 
1321 Init *FoldOpInit::getBit(unsigned Bit) const {
1322  return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
1323 }
1324 
1325 std::string FoldOpInit::getAsString() const {
1326  return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
1327  ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
1328  ", " + Expr->getAsString() + ")")
1329  .str();
1330 }
1331 
1333  Init *Expr) {
1334  ID.AddPointer(CheckType);
1335  ID.AddPointer(Expr);
1336 }
1337 
1339  static FoldingSet<IsAOpInit> ThePool;
1340 
1342  ProfileIsAOpInit(ID, CheckType, Expr);
1343 
1344  void *IP = nullptr;
1345  if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1346  return I;
1347 
1348  IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
1349  ThePool.InsertNode(I, IP);
1350  return I;
1351 }
1352 
1354  ProfileIsAOpInit(ID, CheckType, Expr);
1355 }
1356 
1358  if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
1359  // Is the expression type known to be (a subclass of) the desired type?
1360  if (TI->getType()->typeIsConvertibleTo(CheckType))
1361  return IntInit::get(1);
1362 
1363  if (isa<RecordRecTy>(CheckType)) {
1364  // If the target type is not a subclass of the expression type, or if
1365  // the expression has fully resolved to a record, we know that it can't
1366  // be of the required type.
1367  if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
1368  return IntInit::get(0);
1369  } else {
1370  // We treat non-record types as not castable.
1371  return IntInit::get(0);
1372  }
1373  }
1374  return const_cast<IsAOpInit *>(this);
1375 }
1376 
1378  Init *NewExpr = Expr->resolveReferences(R);
1379  if (Expr != NewExpr)
1380  return get(CheckType, NewExpr)->Fold();
1381  return const_cast<IsAOpInit *>(this);
1382 }
1383 
1384 Init *IsAOpInit::getBit(unsigned Bit) const {
1385  return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
1386 }
1387 
1388 std::string IsAOpInit::getAsString() const {
1389  return (Twine("!isa<") + CheckType->getAsString() + ">(" +
1390  Expr->getAsString() + ")")
1391  .str();
1392 }
1393 
1395  if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
1396  for (Record *Rec : RecordType->getClasses()) {
1397  if (RecordVal *Field = Rec->getValue(FieldName))
1398  return Field->getType();
1399  }
1400  }
1401  return nullptr;
1402 }
1403 
1404 Init *
1406  if (getType() == Ty || getType()->typeIsA(Ty))
1407  return const_cast<TypedInit *>(this);
1408 
1409  if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
1410  cast<BitsRecTy>(Ty)->getNumBits() == 1)
1411  return BitsInit::get({const_cast<TypedInit *>(this)});
1412 
1413  return nullptr;
1414 }
1415 
1418  if (!T) return nullptr; // Cannot subscript a non-bits variable.
1419  unsigned NumBits = T->getNumBits();
1420 
1421  SmallVector<Init *, 16> NewBits;
1422  NewBits.reserve(Bits.size());
1423  for (unsigned Bit : Bits) {
1424  if (Bit >= NumBits)
1425  return nullptr;
1426 
1427  NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1428  }
1429  return BitsInit::get(NewBits);
1430 }
1431 
1433  // Handle the common case quickly
1434  if (getType() == Ty || getType()->typeIsA(Ty))
1435  return const_cast<TypedInit *>(this);
1436 
1437  if (Init *Converted = convertInitializerTo(Ty)) {
1438  assert(!isa<TypedInit>(Converted) ||
1439  cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
1440  return Converted;
1441  }
1442 
1443  if (!getType()->typeIsConvertibleTo(Ty))
1444  return nullptr;
1445 
1446  return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
1447  ->Fold(nullptr);
1448 }
1449 
1452  if (!T) return nullptr; // Cannot subscript a non-list variable.
1453 
1454  if (Elements.size() == 1)
1455  return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1456 
1457  SmallVector<Init*, 8> ListInits;
1458  ListInits.reserve(Elements.size());
1459  for (unsigned Element : Elements)
1460  ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1461  Element));
1462  return ListInit::get(ListInits, T->getElementType());
1463 }
1464 
1465 
1467  Init *Value = StringInit::get(VN);
1468  return VarInit::get(Value, T);
1469 }
1470 
1472  using Key = std::pair<RecTy *, Init *>;
1473  static DenseMap<Key, VarInit*> ThePool;
1474 
1475  Key TheKey(std::make_pair(T, VN));
1476 
1477  VarInit *&I = ThePool[TheKey];
1478  if (!I)
1479  I = new(Allocator) VarInit(VN, T);
1480  return I;
1481 }
1482 
1484  StringInit *NameString = cast<StringInit>(getNameInit());
1485  return NameString->getValue();
1486 }
1487 
1488 Init *VarInit::getBit(unsigned Bit) const {
1489  if (getType() == BitRecTy::get())
1490  return const_cast<VarInit*>(this);
1491  return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1492 }
1493 
1495  if (Init *Val = R.resolve(VarName))
1496  return Val;
1497  return const_cast<VarInit *>(this);
1498 }
1499 
1501  using Key = std::pair<TypedInit *, unsigned>;
1502  static DenseMap<Key, VarBitInit*> ThePool;
1503 
1504  Key TheKey(std::make_pair(T, B));
1505 
1506  VarBitInit *&I = ThePool[TheKey];
1507  if (!I)
1508  I = new(Allocator) VarBitInit(T, B);
1509  return I;
1510 }
1511 
1512 std::string VarBitInit::getAsString() const {
1513  return TI->getAsString() + "{" + utostr(Bit) + "}";
1514 }
1515 
1517  Init *I = TI->resolveReferences(R);
1518  if (TI != I)
1519  return I->getBit(getBitNum());
1520 
1521  return const_cast<VarBitInit*>(this);
1522 }
1523 
1525  unsigned E) {
1526  using Key = std::pair<TypedInit *, unsigned>;
1527  static DenseMap<Key, VarListElementInit*> ThePool;
1528 
1529  Key TheKey(std::make_pair(T, E));
1530 
1531  VarListElementInit *&I = ThePool[TheKey];
1532  if (!I) I = new(Allocator) VarListElementInit(T, E);
1533  return I;
1534 }
1535 
1536 std::string VarListElementInit::getAsString() const {
1537  return TI->getAsString() + "[" + utostr(Element) + "]";
1538 }
1539 
1541  Init *NewTI = TI->resolveReferences(R);
1542  if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1543  // Leave out-of-bounds array references as-is. This can happen without
1544  // being an error, e.g. in the untaken "branch" of an !if expression.
1545  if (getElementNum() < List->size())
1546  return List->getElement(getElementNum());
1547  }
1548  if (NewTI != TI && isa<TypedInit>(NewTI))
1549  return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1550  return const_cast<VarListElementInit *>(this);
1551 }
1552 
1554  if (getType() == BitRecTy::get())
1555  return const_cast<VarListElementInit*>(this);
1556  return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1557 }
1558 
1559 DefInit::DefInit(Record *D)
1560  : TypedInit(IK_DefInit, D->getType()), Def(D) {}
1561 
1563  return R->getDefInit();
1564 }
1565 
1567  if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1568  if (getType()->typeIsConvertibleTo(RRT))
1569  return const_cast<DefInit *>(this);
1570  return nullptr;
1571 }
1572 
1574  if (const RecordVal *RV = Def->getValue(FieldName))
1575  return RV->getType();
1576  return nullptr;
1577 }
1578 
1579 std::string DefInit::getAsString() const {
1580  return Def->getName();
1581 }
1582 
1584  Record *Class,
1586  ID.AddInteger(Args.size());
1587  ID.AddPointer(Class);
1588 
1589  for (Init *I : Args)
1590  ID.AddPointer(I);
1591 }
1592 
1594  static FoldingSet<VarDefInit> ThePool;
1595 
1597  ProfileVarDefInit(ID, Class, Args);
1598 
1599  void *IP = nullptr;
1600  if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1601  return I;
1602 
1603  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
1604  alignof(VarDefInit));
1605  VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
1606  std::uninitialized_copy(Args.begin(), Args.end(),
1607  I->getTrailingObjects<Init *>());
1608  ThePool.InsertNode(I, IP);
1609  return I;
1610 }
1611 
1613  ProfileVarDefInit(ID, Class, args());
1614 }
1615 
1616 DefInit *VarDefInit::instantiate() {
1617  if (!Def) {
1618  RecordKeeper &Records = Class->getRecords();
1619  auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
1620  Class->getLoc(), Records,
1621  /*IsAnonymous=*/true);
1622  Record *NewRec = NewRecOwner.get();
1623 
1624  // Copy values from class to instance
1625  for (const RecordVal &Val : Class->getValues())
1626  NewRec->addValue(Val);
1627 
1628  // Substitute and resolve template arguments
1629  ArrayRef<Init *> TArgs = Class->getTemplateArgs();
1630  MapResolver R(NewRec);
1631 
1632  for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1633  if (i < args_size())
1634  R.set(TArgs[i], getArg(i));
1635  else
1636  R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
1637 
1638  NewRec->removeValue(TArgs[i]);
1639  }
1640 
1641  NewRec->resolveReferences(R);
1642 
1643  // Add superclasses.
1644  ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
1645  for (const auto &SCPair : SCs)
1646  NewRec->addSuperClass(SCPair.first, SCPair.second);
1647 
1648  NewRec->addSuperClass(Class,
1649  SMRange(Class->getLoc().back(),
1650  Class->getLoc().back()));
1651 
1652  // Resolve internal references and store in record keeper
1653  NewRec->resolveReferences();
1654  Records.addDef(std::move(NewRecOwner));
1655 
1656  Def = DefInit::get(NewRec);
1657  }
1658 
1659  return Def;
1660 }
1661 
1663  TrackUnresolvedResolver UR(&R);
1664  bool Changed = false;
1665  SmallVector<Init *, 8> NewArgs;
1666  NewArgs.reserve(args_size());
1667 
1668  for (Init *Arg : args()) {
1669  Init *NewArg = Arg->resolveReferences(UR);
1670  NewArgs.push_back(NewArg);
1671  Changed |= NewArg != Arg;
1672  }
1673 
1674  if (Changed) {
1675  auto New = VarDefInit::get(Class, NewArgs);
1676  if (!UR.foundUnresolved())
1677  return New->instantiate();
1678  return New;
1679  }
1680  return const_cast<VarDefInit *>(this);
1681 }
1682 
1684  if (Def)
1685  return Def;
1686 
1688  for (Init *Arg : args())
1689  Arg->resolveReferences(R);
1690 
1691  if (!R.foundUnresolved())
1692  return const_cast<VarDefInit *>(this)->instantiate();
1693  return const_cast<VarDefInit *>(this);
1694 }
1695 
1696 std::string VarDefInit::getAsString() const {
1697  std::string Result = Class->getNameInitAsString() + "<";
1698  const char *sep = "";
1699  for (Init *Arg : args()) {
1700  Result += sep;
1701  sep = ", ";
1702  Result += Arg->getAsString();
1703  }
1704  return Result + ">";
1705 }
1706 
1708  using Key = std::pair<Init *, StringInit *>;
1709  static DenseMap<Key, FieldInit*> ThePool;
1710 
1711  Key TheKey(std::make_pair(R, FN));
1712 
1713  FieldInit *&I = ThePool[TheKey];
1714  if (!I) I = new(Allocator) FieldInit(R, FN);
1715  return I;
1716 }
1717 
1718 Init *FieldInit::getBit(unsigned Bit) const {
1719  if (getType() == BitRecTy::get())
1720  return const_cast<FieldInit*>(this);
1721  return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1722 }
1723 
1725  Init *NewRec = Rec->resolveReferences(R);
1726  if (NewRec != Rec)
1727  return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
1728  return const_cast<FieldInit *>(this);
1729 }
1730 
1731 Init *FieldInit::Fold(Record *CurRec) const {
1732  if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1733  Record *Def = DI->getDef();
1734  if (Def == CurRec)
1735  PrintFatalError(CurRec->getLoc(),
1736  Twine("Attempting to access field '") +
1737  FieldName->getAsUnquotedString() + "' of '" +
1738  Rec->getAsString() + "' is a forbidden self-reference");
1739  Init *FieldVal = Def->getValue(FieldName)->getValue();
1740  if (FieldVal->isComplete())
1741  return FieldVal;
1742  }
1743  return const_cast<FieldInit *>(this);
1744 }
1745 
1747  ArrayRef<Init *> CondRange,
1748  ArrayRef<Init *> ValRange,
1749  const RecTy *ValType) {
1750  assert(CondRange.size() == ValRange.size() &&
1751  "Number of conditions and values must match!");
1752  ID.AddPointer(ValType);
1753  ArrayRef<Init *>::iterator Case = CondRange.begin();
1754  ArrayRef<Init *>::iterator Val = ValRange.begin();
1755 
1756  while (Case != CondRange.end()) {
1757  ID.AddPointer(*Case++);
1758  ID.AddPointer(*Val++);
1759  }
1760 }
1761 
1763  ProfileCondOpInit(ID,
1764  makeArrayRef(getTrailingObjects<Init *>(), NumConds),
1765  makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds),
1766  ValType);
1767 }
1768 
1769 CondOpInit *
1771  ArrayRef<Init *> ValRange, RecTy *Ty) {
1772  assert(CondRange.size() == ValRange.size() &&
1773  "Number of conditions and values must match!");
1774 
1775  static FoldingSet<CondOpInit> ThePool;
1777  ProfileCondOpInit(ID, CondRange, ValRange, Ty);
1778 
1779  void *IP = nullptr;
1780  if (CondOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1781  return I;
1782 
1783  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(2*CondRange.size()),
1784  alignof(BitsInit));
1785  CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty);
1786 
1787  std::uninitialized_copy(CondRange.begin(), CondRange.end(),
1788  I->getTrailingObjects<Init *>());
1789  std::uninitialized_copy(ValRange.begin(), ValRange.end(),
1790  I->getTrailingObjects<Init *>()+CondRange.size());
1791  ThePool.InsertNode(I, IP);
1792  return I;
1793 }
1794 
1796  SmallVector<Init*, 4> NewConds;
1797  bool Changed = false;
1798  for (const Init *Case : getConds()) {
1799  Init *NewCase = Case->resolveReferences(R);
1800  NewConds.push_back(NewCase);
1801  Changed |= NewCase != Case;
1802  }
1803 
1804  SmallVector<Init*, 4> NewVals;
1805  for (const Init *Val : getVals()) {
1806  Init *NewVal = Val->resolveReferences(R);
1807  NewVals.push_back(NewVal);
1808  Changed |= NewVal != Val;
1809  }
1810 
1811  if (Changed)
1812  return (CondOpInit::get(NewConds, NewVals,
1813  getValType()))->Fold(R.getCurrentRecord());
1814 
1815  return const_cast<CondOpInit *>(this);
1816 }
1817 
1818 Init *CondOpInit::Fold(Record *CurRec) const {
1819  for ( unsigned i = 0; i < NumConds; ++i) {
1820  Init *Cond = getCond(i);
1821  Init *Val = getVal(i);
1822 
1823  if (IntInit *CondI = dyn_cast_or_null<IntInit>(
1824  Cond->convertInitializerTo(IntRecTy::get()))) {
1825  if (CondI->getValue())
1826  return Val->convertInitializerTo(getValType());
1827  } else
1828  return const_cast<CondOpInit *>(this);
1829  }
1830 
1831  PrintFatalError(CurRec->getLoc(),
1832  CurRec->getName() +
1833  " does not have any true condition in:" +
1834  this->getAsString());
1835  return nullptr;
1836 }
1837 
1839  for (const Init *Case : getConds())
1840  if (!Case->isConcrete())
1841  return false;
1842 
1843  for (const Init *Val : getVals())
1844  if (!Val->isConcrete())
1845  return false;
1846 
1847  return true;
1848 }
1849 
1851  for (const Init *Case : getConds())
1852  if (!Case->isComplete())
1853  return false;
1854 
1855  for (const Init *Val : getVals())
1856  if (!Val->isConcrete())
1857  return false;
1858 
1859  return true;
1860 }
1861 
1862 std::string CondOpInit::getAsString() const {
1863  std::string Result = "!cond(";
1864  for (unsigned i = 0; i < getNumConds(); i++) {
1865  Result += getCond(i)->getAsString() + ": ";
1866  Result += getVal(i)->getAsString();
1867  if (i != getNumConds()-1)
1868  Result += ", ";
1869  }
1870  return Result + ")";
1871 }
1872 
1873 Init *CondOpInit::getBit(unsigned Bit) const {
1874  return VarBitInit::get(const_cast<CondOpInit *>(this), Bit);
1875 }
1876 
1878  ArrayRef<Init *> ArgRange,
1879  ArrayRef<StringInit *> NameRange) {
1880  ID.AddPointer(V);
1881  ID.AddPointer(VN);
1882 
1883  ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1885  while (Arg != ArgRange.end()) {
1886  assert(Name != NameRange.end() && "Arg name underflow!");
1887  ID.AddPointer(*Arg++);
1888  ID.AddPointer(*Name++);
1889  }
1890  assert(Name == NameRange.end() && "Arg name overflow!");
1891 }
1892 
1893 DagInit *
1895  ArrayRef<StringInit *> NameRange) {
1896  static FoldingSet<DagInit> ThePool;
1897 
1899  ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1900 
1901  void *IP = nullptr;
1902  if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1903  return I;
1904 
1905  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1906  DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1907  std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1908  I->getTrailingObjects<Init *>());
1909  std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1910  I->getTrailingObjects<StringInit *>());
1911  ThePool.InsertNode(I, IP);
1912  return I;
1913 }
1914 
1915 DagInit *
1917  ArrayRef<std::pair<Init*, StringInit*>> args) {
1920 
1921  for (const auto &Arg : args) {
1922  Args.push_back(Arg.first);
1923  Names.push_back(Arg.second);
1924  }
1925 
1926  return DagInit::get(V, VN, Args, Names);
1927 }
1928 
1930  ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1931 }
1932 
1934  SmallVector<Init*, 8> NewArgs;
1935  NewArgs.reserve(arg_size());
1936  bool ArgsChanged = false;
1937  for (const Init *Arg : getArgs()) {
1938  Init *NewArg = Arg->resolveReferences(R);
1939  NewArgs.push_back(NewArg);
1940  ArgsChanged |= NewArg != Arg;
1941  }
1942 
1943  Init *Op = Val->resolveReferences(R);
1944  if (Op != Val || ArgsChanged)
1945  return DagInit::get(Op, ValName, NewArgs, getArgNames());
1946 
1947  return const_cast<DagInit *>(this);
1948 }
1949 
1950 bool DagInit::isConcrete() const {
1951  if (!Val->isConcrete())
1952  return false;
1953  for (const Init *Elt : getArgs()) {
1954  if (!Elt->isConcrete())
1955  return false;
1956  }
1957  return true;
1958 }
1959 
1960 std::string DagInit::getAsString() const {
1961  std::string Result = "(" + Val->getAsString();
1962  if (ValName)
1963  Result += ":" + ValName->getAsUnquotedString();
1964  if (!arg_empty()) {
1965  Result += " " + getArg(0)->getAsString();
1966  if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1967  for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1968  Result += ", " + getArg(i)->getAsString();
1969  if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1970  }
1971  }
1972  return Result + ")";
1973 }
1974 
1975 //===----------------------------------------------------------------------===//
1976 // Other implementations
1977 //===----------------------------------------------------------------------===//
1978 
1980  : Name(N), TyAndPrefix(T, P) {
1982  assert(Value && "Cannot create unset value for current type!");
1983 }
1984 
1986  return cast<StringInit>(getNameInit())->getValue();
1987 }
1988 
1990  if (V) {
1991  Value = V->getCastTo(getType());
1992  if (Value) {
1993  assert(!isa<TypedInit>(Value) ||
1994  cast<TypedInit>(Value)->getType()->typeIsA(getType()));
1995  if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
1996  if (!isa<BitsInit>(Value)) {
1998  Bits.reserve(BTy->getNumBits());
1999  for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i)
2000  Bits.push_back(Value->getBit(i));
2001  Value = BitsInit::get(Bits);
2002  }
2003  }
2004  }
2005  return Value == nullptr;
2006  }
2007  Value = nullptr;
2008  return false;
2009 }
2010 
2011 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2012 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
2013 #endif
2014 
2015 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
2016  if (getPrefix()) OS << "field ";
2017  OS << *getType() << " " << getNameInitAsString();
2018 
2019  if (getValue())
2020  OS << " = " << *getValue();
2021 
2022  if (PrintSem) OS << ";\n";
2023 }
2024 
2025 unsigned Record::LastID = 0;
2026 
2027 void Record::checkName() {
2028  // Ensure the record name has string type.
2029  const TypedInit *TypedName = cast<const TypedInit>(Name);
2030  if (!isa<StringRecTy>(TypedName->getType()))
2031  PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
2032  "' is not a string!");
2033 }
2034 
2036  SmallVector<Record *, 4> DirectSCs;
2037  getDirectSuperClasses(DirectSCs);
2038  return RecordRecTy::get(DirectSCs);
2039 }
2040 
2042  if (!TheInit)
2043  TheInit = new(Allocator) DefInit(this);
2044  return TheInit;
2045 }
2046 
2047 void Record::setName(Init *NewName) {
2048  Name = NewName;
2049  checkName();
2050  // DO NOT resolve record values to the name at this point because
2051  // there might be default values for arguments of this def. Those
2052  // arguments might not have been resolved yet so we don't want to
2053  // prematurely assume values for those arguments were not passed to
2054  // this def.
2055  //
2056  // Nonetheless, it may be that some of this Record's values
2057  // reference the record name. Indeed, the reason for having the
2058  // record name be an Init is to provide this flexibility. The extra
2059  // resolve steps after completely instantiating defs takes care of
2060  // this. See TGParser::ParseDef and TGParser::ParseDefm.
2061 }
2062 
2064  ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
2065  while (!SCs.empty()) {
2066  // Superclasses are in reverse preorder, so 'back' is a direct superclass,
2067  // and its transitive superclasses are directly preceding it.
2068  Record *SC = SCs.back().first;
2069  SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
2070  Classes.push_back(SC);
2071  }
2072 }
2073 
2075  for (RecordVal &Value : Values) {
2076  if (SkipVal == &Value) // Skip resolve the same field as the given one
2077  continue;
2078  if (Init *V = Value.getValue()) {
2079  Init *VR = V->resolveReferences(R);
2080  if (Value.setValue(VR)) {
2081  std::string Type;
2082  if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
2083  Type =
2084  (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
2085  PrintFatalError(getLoc(), Twine("Invalid value ") + Type +
2086  "is found when setting '" +
2087  Value.getNameInitAsString() +
2088  "' of type '" +
2089  Value.getType()->getAsString() +
2090  "' after resolving references: " +
2091  VR->getAsUnquotedString() + "\n");
2092  }
2093  }
2094  }
2095  Init *OldName = getNameInit();
2096  Init *NewName = Name->resolveReferences(R);
2097  if (NewName != OldName) {
2098  // Re-register with RecordKeeper.
2099  setName(NewName);
2100  }
2101 }
2102 
2104  RecordResolver R(*this);
2105  R.setFinal(true);
2106  resolveReferences(R);
2107 }
2108 
2110  RecordValResolver R(*this, RV);
2111  resolveReferences(R, RV);
2112 }
2113 
2114 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2115 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
2116 #endif
2117 
2119  OS << R.getNameInitAsString();
2120 
2121  ArrayRef<Init *> TArgs = R.getTemplateArgs();
2122  if (!TArgs.empty()) {
2123  OS << "<";
2124  bool NeedComma = false;
2125  for (const Init *TA : TArgs) {
2126  if (NeedComma) OS << ", ";
2127  NeedComma = true;
2128  const RecordVal *RV = R.getValue(TA);
2129  assert(RV && "Template argument record not found??");
2130  RV->print(OS, false);
2131  }
2132  OS << ">";
2133  }
2134 
2135  OS << " {";
2137  if (!SC.empty()) {
2138  OS << "\t//";
2139  for (const auto &SuperPair : SC)
2140  OS << " " << SuperPair.first->getNameInitAsString();
2141  }
2142  OS << "\n";
2143 
2144  for (const RecordVal &Val : R.getValues())
2145  if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
2146  OS << Val;
2147  for (const RecordVal &Val : R.getValues())
2148  if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
2149  OS << Val;
2150 
2151  return OS << "}\n";
2152 }
2153 
2155  const RecordVal *R = getValue(FieldName);
2156  if (!R || !R->getValue())
2157  PrintFatalError(getLoc(), "Record `" + getName() +
2158  "' does not have a field named `" + FieldName + "'!\n");
2159  return R->getValue();
2160 }
2161 
2163  const RecordVal *R = getValue(FieldName);
2164  if (!R || !R->getValue())
2165  PrintFatalError(getLoc(), "Record `" + getName() +
2166  "' does not have a field named `" + FieldName + "'!\n");
2167 
2168  if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
2169  return SI->getValue();
2170  if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
2171  return CI->getValue();
2172 
2173  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2174  FieldName + "' does not have a string initializer!");
2175 }
2176 
2178  const RecordVal *R = getValue(FieldName);
2179  if (!R || !R->getValue())
2180  PrintFatalError(getLoc(), "Record `" + getName() +
2181  "' does not have a field named `" + FieldName + "'!\n");
2182 
2183  if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
2184  return BI;
2185  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2186  FieldName + "' does not have a BitsInit initializer!");
2187 }
2188 
2190  const RecordVal *R = getValue(FieldName);
2191  if (!R || !R->getValue())
2192  PrintFatalError(getLoc(), "Record `" + getName() +
2193  "' does not have a field named `" + FieldName + "'!\n");
2194 
2195  if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
2196  return LI;
2197  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2198  FieldName + "' does not have a list initializer!");
2199 }
2200 
2201 std::vector<Record*>
2203  ListInit *List = getValueAsListInit(FieldName);
2204  std::vector<Record*> Defs;
2205  for (Init *I : List->getValues()) {
2206  if (DefInit *DI = dyn_cast<DefInit>(I))
2207  Defs.push_back(DI->getDef());
2208  else
2209  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2210  FieldName + "' list is not entirely DefInit!");
2211  }
2212  return Defs;
2213 }
2214 
2215 int64_t Record::getValueAsInt(StringRef FieldName) const {
2216  const RecordVal *R = getValue(FieldName);
2217  if (!R || !R->getValue())
2218  PrintFatalError(getLoc(), "Record `" + getName() +
2219  "' does not have a field named `" + FieldName + "'!\n");
2220 
2221  if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
2222  return II->getValue();
2223  PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2224  FieldName +
2225  "' does not have an int initializer: " +
2226  R->getValue()->getAsString());
2227 }
2228 
2229 std::vector<int64_t>
2231  ListInit *List = getValueAsListInit(FieldName);
2232  std::vector<int64_t> Ints;
2233  for (Init *I : List->getValues()) {
2234  if (IntInit *II = dyn_cast<IntInit>(I))
2235  Ints.push_back(II->getValue());
2236  else
2237  PrintFatalError(getLoc(),
2238  Twine("Record `") + getName() + "', field `" + FieldName +
2239  "' does not have a list of ints initializer: " +
2240  I->getAsString());
2241  }
2242  return Ints;
2243 }
2244 
2245 std::vector<StringRef>
2247  ListInit *List = getValueAsListInit(FieldName);
2248  std::vector<StringRef> Strings;
2249  for (Init *I : List->getValues()) {
2250  if (StringInit *SI = dyn_cast<StringInit>(I))
2251  Strings.push_back(SI->getValue());
2252  else
2253  PrintFatalError(getLoc(),
2254  Twine("Record `") + getName() + "', field `" + FieldName +
2255  "' does not have a list of strings initializer: " +
2256  I->getAsString());
2257  }
2258  return Strings;
2259 }
2260 
2262  const RecordVal *R = getValue(FieldName);
2263  if (!R || !R->getValue())
2264  PrintFatalError(getLoc(), "Record `" + getName() +
2265  "' does not have a field named `" + FieldName + "'!\n");
2266 
2267  if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2268  return DI->getDef();
2269  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2270  FieldName + "' does not have a def initializer!");
2271 }
2272 
2273 bool Record::getValueAsBit(StringRef FieldName) const {
2274  const RecordVal *R = getValue(FieldName);
2275  if (!R || !R->getValue())
2276  PrintFatalError(getLoc(), "Record `" + getName() +
2277  "' does not have a field named `" + FieldName + "'!\n");
2278 
2279  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2280  return BI->getValue();
2281  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2282  FieldName + "' does not have a bit initializer!");
2283 }
2284 
2285 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
2286  const RecordVal *R = getValue(FieldName);
2287  if (!R || !R->getValue())
2288  PrintFatalError(getLoc(), "Record `" + getName() +
2289  "' does not have a field named `" + FieldName.str() + "'!\n");
2290 
2291  if (isa<UnsetInit>(R->getValue())) {
2292  Unset = true;
2293  return false;
2294  }
2295  Unset = false;
2296  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2297  return BI->getValue();
2298  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2299  FieldName + "' does not have a bit initializer!");
2300 }
2301 
2303  const RecordVal *R = getValue(FieldName);
2304  if (!R || !R->getValue())
2305  PrintFatalError(getLoc(), "Record `" + getName() +
2306  "' does not have a field named `" + FieldName + "'!\n");
2307 
2308  if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
2309  return DI;
2310  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2311  FieldName + "' does not have a dag initializer!");
2312 }
2313 
2314 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2315 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
2316 #endif
2317 
2319  OS << "------------- Classes -----------------\n";
2320  for (const auto &C : RK.getClasses())
2321  OS << "class " << *C.second;
2322 
2323  OS << "------------- Defs -----------------\n";
2324  for (const auto &D : RK.getDefs())
2325  OS << "def " << *D.second;
2326  return OS;
2327 }
2328 
2329 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2330 /// an identifier.
2332  return StringInit::get("anonymous_" + utostr(AnonCounter++));
2333 }
2334 
2335 std::vector<Record *>
2337  Record *Class = getClass(ClassName);
2338  if (!Class)
2339  PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
2340 
2341  std::vector<Record*> Defs;
2342  for (const auto &D : getDefs())
2343  if (D.second->isSubClassOf(Class))
2344  Defs.push_back(D.second.get());
2345 
2346  return Defs;
2347 }
2348 
2350  auto It = Map.find(VarName);
2351  if (It == Map.end())
2352  return nullptr;
2353 
2354  Init *I = It->second.V;
2355 
2356  if (!It->second.Resolved && Map.size() > 1) {
2357  // Resolve mutual references among the mapped variables, but prevent
2358  // infinite recursion.
2359  Map.erase(It);
2360  I = I->resolveReferences(*this);
2361  Map[VarName] = {I, true};
2362  }
2363 
2364  return I;
2365 }
2366 
2368  Init *Val = Cache.lookup(VarName);
2369  if (Val)
2370  return Val;
2371 
2372  for (Init *S : Stack) {
2373  if (S == VarName)
2374  return nullptr; // prevent infinite recursion
2375  }
2376 
2377  if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
2378  if (!isa<UnsetInit>(RV->getValue())) {
2379  Val = RV->getValue();
2380  Stack.push_back(VarName);
2381  Val = Val->resolveReferences(*this);
2382  Stack.pop_back();
2383  }
2384  }
2385 
2386  Cache[VarName] = Val;
2387  return Val;
2388 }
2389 
2391  Init *I = nullptr;
2392 
2393  if (R) {
2394  I = R->resolve(VarName);
2395  if (I && !FoundUnresolved) {
2396  // Do not recurse into the resolved initializer, as that would change
2397  // the behavior of the resolver we're delegating, but do check to see
2398  // if there are unresolved variables remaining.
2400  I->resolveReferences(Sub);
2401  FoundUnresolved |= Sub.FoundUnresolved;
2402  }
2403  }
2404 
2405  if (!I)
2406  FoundUnresolved = true;
2407  return I;
2408 }
2409 
2411 {
2412  if (VarName == VarNameToTrack)
2413  Found = true;
2414  return nullptr;
2415 }
Init * Fold() const
Definition: Record.cpp:1357
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:825
uint64_t CallInst * C
Represents a range in source code.
Definition: SMLoc.h:48
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:51
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:365
StringRef getName() const
Definition: Record.cpp:1985
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:641
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:795
RecTyKind
Subclass discriminator (for dyn_cast<> et al.)
Definition: Record.h:57
void print(raw_ostream &OS) const
Definition: Record.h:79
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1384
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
StringRef getName() const
Definition: Record.cpp:1483
Record * getValueAsDef(StringRef FieldName) const
This method looks up the specified field and returns its value as a Record, throwing an exception if ...
Definition: Record.cpp:2261
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
BitsInit * getValueAsBitsInit(StringRef FieldName) const
This method looks up the specified field and returns its value as a BitsInit, throwing an exception i...
Definition: Record.cpp:2177
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:157
Init * getValue() const
Definition: Record.h:1413
This class represents lattice values for constants.
Definition: AllocatorList.h:23
X.Y - Represent a reference to a subfield of a variable.
Definition: Record.h:1260
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:473
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:505
virtual std::string getAsUnquotedString() const
Convert this value to a string form, without adding quote markers.
Definition: Record.h:367
Record * getCurrentRecord() const
Definition: Record.h:1871
bool foundUnresolved() const
Definition: Record.h:1972
[AL, AH, CL] - Represent a list of defs
Definition: Record.h:661
iterator begin() const
Definition: ArrayRef.h:136
&#39;7&#39; - Represent an initialization by a literal integer value.
Definition: Record.h:564
static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B, Init *Start, Init *List, Init *Expr, RecTy *Type)
Definition: Record.cpp:1261
&#39;list<Ty>&#39; - Represent a list of values, all of which must be of the specified type.
Definition: Record.h:195
void getDirectSuperClasses(SmallVectorImpl< Record *> &Classes) const
Append the direct super classes of this record to Classes.
Definition: Record.cpp:2063
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
!op (X, Y) - Combine two inits.
Definition: Record.h:801
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1553
bool typeIsA(const RecTy *RHS) const override
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:105
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:592
DagInit * getValueAsDag(StringRef FieldName) const
This method looks up the specified field and returns its value as an Dag, throwing an exception if th...
Definition: Record.cpp:2302
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1696
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:318
AL - Represent a reference to a &#39;def&#39; in the description.
Definition: Record.h:1174
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1494
static Init * getListConcat(TypedInit *lhs, Init *rhs)
Definition: Record.cpp:868
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2367
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1321
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
Definition: FoldingSet.h:444
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
Definition: Record.cpp:693
void addDef(std::unique_ptr< Record > R)
Definition: Record.h:1724
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:1416
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1573
Init * getCastTo(RecTy *Ty) const override
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
Definition: Record.cpp:1432
Init * Fold(Record *CurRec, bool IsFinal=false) const
Definition: Record.cpp:712
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1512
static Init * getStrConcat(Init *lhs, Init *rhs)
Definition: Record.cpp:852
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1177
static VarInit * get(StringRef VN, RecTy *T)
Definition: Record.cpp:1466
static uint32_t Concat[]
STATISTIC(NumFunctions, "Total number of functions")
ListInit * getValueAsListInit(StringRef FieldName) const
This method looks up the specified field and returns its value as a ListInit, throwing an exception i...
Definition: Record.cpp:2189
int64_t getValueAsInt(StringRef FieldName) const
This method looks up the specified field and returns its value as an int64_t, throwing an exception i...
Definition: Record.cpp:2215
Resolve all references to a specific RecordVal.
Definition: Record.h:1928
const_record_iterator classes_begin() const
Definition: Record.h:268
static ListInit * ConcatListInits(const ListInit *LHS, const ListInit *RHS)
Definition: Record.cpp:860
static BumpPtrAllocator Allocator
Definition: Record.cpp:44
void reserve(size_type N)
Definition: SmallVector.h:369
virtual Init * getCastTo(RecTy *Ty) const =0
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
Resolve all variables from a record except for unset variables.
Definition: Record.h:1911
bool typeIsA(const RecTy *RHS) const override
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:238
&#39;{ a, b, c }&#39; - Represents an initializer for a BitsRecTy value.
Definition: Record.h:513
Record * getElementAsRecord(unsigned i) const
Definition: Record.cpp:637
std::string getAsString() const override
Definition: Record.cpp:130
std::string getNameInitAsString() const
Definition: Record.h:1407
Init * Fold() const
Definition: Record.cpp:1683
void dump() const
Debugging method that may be called through a debugger, just invokes print on stderr.
Definition: Record.cpp:295
static BitRecTy * get()
Definition: Record.h:110
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val)
static IntInit * get(int64_t V)
Definition: Record.cpp:460
static BitsInit * get(ArrayRef< Init *> Range)
Definition: Record.cpp:342
(Optionally) delegate resolving to a sub-resolver, and keep track whether there were unresolved refer...
Definition: Record.h:1964
bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const
This method looks up the specified field and returns its value as a bit.
Definition: Record.cpp:2285
Init * getArg(unsigned Num) const
Definition: Record.h:1342
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
Definition: Record.h:1508
&#39;?&#39; - Represents an uninitialized value
Definition: Record.h:457
&#39;true&#39;/&#39;false&#39; - Represent a concrete initializer for a bit.
Definition: Record.h:483
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:392
void dump() const
Definition: Record.cpp:2012
static Init * ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec)
Definition: Record.cpp:1074
static BitsRecTy * get(unsigned Sz)
Definition: Record.cpp:84
Record * getDef() const
Definition: Record.h:1193
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
This is the common super-class of types that have a specific, explicit, type.
Definition: Record.h:425
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
static RecordRecTy * resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2)
Definition: Record.cpp:242
Shift and rotation operations.
Definition: ISDOpcodes.h:434
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:98
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:361
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:680
RecTy * getElementType() const
Definition: Record.h:208
static StringRecTy * get()
Definition: Record.h:186
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:841
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:196
ArrayRef< Init * > getTemplateArgs() const
Definition: Record.h:1502
std::string getAsString() const override
Definition: Record.cpp:202
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
std::string getAsString() const override
Definition: Record.cpp:121
List[4] - Represent access to one element of a var or field.
Definition: Record.h:1142
&#39;[classname]&#39; - Type of record values that have zero or more superclasses.
Definition: Record.h:237
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:542
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:468
std::vector< Record * > getValueAsListOfDefs(StringRef FieldName) const
This method looks up the specified field and returns its value as a vector of records, throwing an exception if the field does not exist or if the value is not the right type.
Definition: Record.cpp:2202
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
!foldl (a, b, expr, start, lst) - Fold over a list.
Definition: Record.h:998
Init * getValueInit(StringRef FieldName) const
Return the initializer for a value with the specified name, or throw an exception if the field does n...
Definition: Record.cpp:2154
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
!cond(condition_1: value1, ...
Definition: Record.h:923
void print(raw_ostream &OS, bool PrintSem=true) const
Definition: Record.cpp:2015
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:804
const_iterator end() const
Definition: Record.h:715
virtual Init * resolve(Init *VarName)=0
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
StringInit * getArgName(unsigned Num) const
Definition: Record.h:1347
void InsertNode(T *N, void *InsertPos)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
Definition: FoldingSet.h:451
Key
PAL metadata keys.
ArrayRef< SMLoc > getLoc() const
Definition: Record.h:1491
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
&#39;bits<n>&#39; - Represent a fixed number of bits
Definition: Record.h:118
ArrayRef< Record * > getClasses() const
Definition: Record.h:262
std::string itostr(int64_t X)
Definition: StringExtras.h:238
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:198
bool isTemplateArg(Init *Name) const
Definition: Record.h:1515
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1217
RecTy * getType() const
Definition: Record.h:1412
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:200
bool isSubClassOf(const Record *R) const
Definition: Record.h:1562
ArrayRef< Init * > getValues() const
Definition: Record.h:710
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:669
bool isFinal() const
Definition: Record.h:1885
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
unsigned getNumBits() const
Definition: Record.h:130
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1540
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:125
#define EQ(a, b)
Definition: regexec.c:112
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2349
virtual Init * getBit(unsigned Bit) const =0
This method is used to return the initializer for the specified bit.
static CodeInit * get(StringRef, const SMLoc &Loc)
Definition: Record.cpp:517
ArrayRef< RecordVal > getValues() const
Definition: Record.h:1506
const_record_iterator classes_end() const
Definition: Record.h:269
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:626
&#39;code&#39; - Represent a code fragment
Definition: Record.h:140
void resolveReferencesTo(const RecordVal *RV)
If anything in this record refers to RV, replace the reference to RV with the RHS of RV...
Definition: Record.cpp:2109
bool isSubClassOf(Record *Class) const
Definition: Record.cpp:218
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:551
virtual Init * convertInitializerTo(RecTy *Ty) const =0
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
void addShadow(Init *Key)
Definition: Record.h:1953
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:598
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:111
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1762
Base class for operators.
Definition: Record.h:727
#define P(N)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1516
RecTy * getType() const
Definition: Record.h:441
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:225
Init * getElement(unsigned i) const
Definition: Record.h:686
Resolve arbitrary mappings.
Definition: Record.h:1891
void dump() const
Definition: Record.cpp:2115
Init * getNameInit() const
Definition: Record.h:1405
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
virtual bool isConcrete() const
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.h:356
void resolveReferences()
If there are any field references that refer to fields that have been filled in, we can propagate the...
Definition: Record.cpp:2103
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void set(Init *Key, Init *Value)
Definition: Record.h:1905
"foo" - Represent an initialization by a string value.
Definition: Record.h:594
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1394
static void ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:1045
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:214
void dump() const
Definition: Record.cpp:2315
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1863
static void ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type)
Definition: Record.cpp:687
static ManagedStatic< OptionRegistry > OR
Definition: Options.cpp:30
const T * getTrailingObjects() const
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:116
!op (X) - Transform an init.
Definition: Record.h:752
Init * getCastTo(RecTy *Ty) const override
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
Definition: Record.cpp:303
const std::string getNameInitAsString() const
Definition: Record.h:1485
static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
Definition: Record.cpp:1877
bool getPrefix() const
Definition: Record.h:1411
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
Definition: Record.cpp:1894
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1724
void dump() const
Definition: Record.cpp:60
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1184
StringRef getValueAsString(StringRef FieldName) const
This method looks up the specified field and returns its value as a string, throwing an exception if ...
Definition: Record.cpp:2162
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
int64_t getValue() const
Definition: Record.h:580
&#39;string&#39; - Represent an string value
Definition: Record.h:176
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:1838
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1662
static BitInit * get(bool V)
Definition: Record.cpp:311
static bool canFitInBitfield(int64_t Value, unsigned NumBits)
Definition: Record.cpp:472
bool setValue(Init *V)
Definition: Record.cpp:1989
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1579
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:425
static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:817
DefInit * getDefInit()
get the corresponding DefInit.
Definition: Record.cpp:2041
static wasm::ValType getType(const TargetRegisterClass *RC)
static Init * ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, Record *CurRec)
Definition: Record.cpp:1109
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
Definition: FoldingSet.h:473
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1960
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< typename base::iterator, bool > insert(StringRef Key)
Definition: StringSet.h:38
std::vector< int64_t > getValueAsListOfInts(StringRef FieldName) const
This method looks up the specified field and returns its value as a vector of integers, throwing an exception if the field does not exist or if the value is not the right type.
Definition: Record.cpp:2230
static RecordRecTy * get(ArrayRef< Record *> Classes)
Get the record type with the given non-redundant list of superclasses.
Definition: Record.cpp:157
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1107
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:1405
std::vector< Record * > getAllDerivedDefinitions(StringRef ClassName) const
This method returns all concrete definitions that derive from the specified class name...
Definition: Record.cpp:2336
Delegate resolving to a sub-resolver, but shadow some variable names.
Definition: Record.h:1943
void setFinal(bool Final)
Definition: Record.h:1887
std::vector< StringRef > getValueAsListOfStrings(StringRef FieldName) const
This method looks up the specified field and returns its value as a vector of strings, throwing an exception if the field does not exist or if the value is not the right type.
Definition: Record.cpp:2246
static FieldInit * get(Init *R, StringInit *FN)
Definition: Record.cpp:1707
virtual Init * resolveReferences(Resolver &R) const
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.h:410
Init * Fold(Record *CurRec) const
Definition: Record.cpp:882
std::string getAsString() const override
Definition: Record.cpp:94
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:1450
static StringInit * ConcatStringInits(const StringInit *I0, const StringInit *I1)
Definition: Record.cpp:845
bool typeIsA(const RecTy *RHS) const override
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:140
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1158
virtual bool typeIsA(const RecTy *RHS) const
Return true if &#39;this&#39; type is equal to or a subtype of RHS.
Definition: Record.cpp:74
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2390
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1521
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1795
const RecordMap & getClasses() const
Definition: Record.h:1697
Init * getOperator() const
Definition: Record.h:1332
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
static VarBitInit * get(TypedInit *T, unsigned B)
Definition: Record.cpp:1500
static StringInit * get(StringRef)
Definition: Record.cpp:533
RecordKeeper & getRecords() const
Definition: Record.h:1606
iterator end() const
Definition: ArrayRef.h:137
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:411
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1306
CHAIN = SC CHAIN, Imm128 - System call.
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1292
unsigned getNumArgs() const
Definition: Record.h:1340
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:1566
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1862
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
RecTy * getElementType() const
Definition: Record.h:690
static DefInit * get(Record *)
Definition: Record.cpp:1562
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:193
classname<targs...> - Represent an uninstantiated anonymous class instantiation.
Definition: Record.h:1209
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:223
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:478
static VarDefInit * get(Record *Class, ArrayRef< Init *> Args)
Definition: Record.cpp:1593
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1873
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:370
static IsAOpInit * get(RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1338
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1010
Init * resolve(Init *VarName) override
Return the initializer for the given variable name (should normally be a StringInit), or nullptr if the name could not be resolved.
Definition: Record.cpp:2410
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:645
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
Definition: Error.cpp:66
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1612
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:708
!isa<type>(expr) - Dynamically determine the type of an expression.
Definition: Record.h:1035
RecordRecTy * getType()
Definition: Record.cpp:2035
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:467
static FoldOpInit * get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
Definition: Record.cpp:1272
static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1332
&#39;int&#39; - Represent an integer value of no particular size
Definition: Record.h:158
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1488
bool getValueAsBit(StringRef FieldName) const
This method looks up the specified field and returns its value as a bit, throwing an exception if the...
Definition: Record.cpp:2273
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1020
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1929
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:134
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:411
Init * getNameInit() const
Definition: Record.h:1481
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:403
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:661
static Init * ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS, Record *CurRec)
Definition: Record.cpp:1080
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
static UnsetInit * get()
Definition: Record.cpp:298
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1128
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:76
void addValue(const RecordVal &RV)
Definition: Record.h:1544
const NodeList & List
Definition: RDFGraph.cpp:201
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:1054
const_iterator begin() const
Definition: Record.h:714
static VarListElementInit * get(TypedInit *T, unsigned E)
Definition: Record.cpp:1524
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition: Record.cpp:262
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1377
static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class, ArrayRef< Init *> Args)
Definition: Record.cpp:1583
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1288
Opcode{0} - Represent access to one bit of a variable or field.
Definition: Record.h:1105
&#39;Opcode&#39; - Represent a reference to an entire variable object.
Definition: Record.h:1068
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1353
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1247
static void ProfileCondOpInit(FoldingSetNodeID &ID, ArrayRef< Init *> CondRange, ArrayRef< Init *> ValRange, const RecTy *ValType)
Definition: Record.cpp:1746
Init * resolveReferences(Resolver &R) const override
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.cpp:1933
std::string getAsString() const override
Definition: Record.cpp:146
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
&#39;bit&#39; - Represent a single bit
Definition: Record.h:100
!op (X, Y, Z) - Combine two inits.
Definition: Record.h:859
static void ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef< Init *> Range)
Definition: Record.cpp:335
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2045
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1818
static ListInit * get(ArrayRef< Init *> Range, RecTy *EltTy)
Definition: Record.cpp:570
virtual bool keepUnsetBits() const
Definition: Record.h:1880
RecTyKind getRecTyKind() const
Definition: Record.h:76
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of &#39;this&#39; type can be converted to the specified type.
Definition: Record.cpp:69
StringRef getValue() const
Definition: Record.h:610
&#39;dag&#39; - Represent a dag fragment
Definition: Record.h:218
Record * getDef(StringRef Name) const
Definition: Record.h:1705
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckType(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, const TargetLowering *TLI, const DataLayout &DL)
StringRef getName() const
Definition: Record.h:1479
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1718
LLVM Value Representation.
Definition: Value.h:73
(v a, b) - Represent a DAG tree value.
Definition: Record.h:1302
virtual std::string getAsString() const =0
Convert this value to a string form.
size_t size() const
Definition: Record.h:717
bool isConcrete() const override
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
Definition: Record.cpp:1950
static void ProfileListInit(FoldingSetNodeID &ID, ArrayRef< Init *> Range, RecTy *EltTy)
Definition: Record.cpp:560
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:27
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
static CondOpInit * get(ArrayRef< Init *> C, ArrayRef< Init *> V, RecTy *Type)
Definition: Record.cpp:1770
void setName(Init *Name)
Definition: Record.cpp:2047
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1070
static void ProfileRecordRecTy(FoldingSetNodeID &ID, ArrayRef< Record *> Classes)
Definition: Record.cpp:150
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1388
bool isComplete() const override
This virtual method should be overridden by values that may not be completely specified yet...
Definition: Record.cpp:1850
virtual std::string getAsString() const =0
Init * convertInitializerTo(RecTy *Ty) const override
Convert to an initializer whose type is-a Ty, or return nullptr if this is not possible (this can hap...
Definition: Record.cpp:307
Represents a location in source code.
Definition: SMLoc.h:23
ListRecTy * getListTy()
Returns the type representing list<this>.
Definition: Record.cpp:63
Init * getNewAnonymousName()
GetNewAnonymousName - Generate a unique anonymous name that can be used as an identifier.
Definition: Record.cpp:2331
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
const RecordMap & getDefs() const
Definition: Record.h:1698
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1325
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1731
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
#define T1
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1536
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143
static IntRecTy * get()
Definition: Record.h:168
RecordVal(Init *N, RecTy *T, bool P)
Definition: Record.cpp:1979
static Init * getListSplat(TypedInit *lhs, Init *rhs)
Definition: Record.cpp:878