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