LLVM  6.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/Support/Allocator.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/SMLoc.h"
28 #include "llvm/TableGen/Error.h"
29 #include "llvm/TableGen/Record.h"
30 #include <cassert>
31 #include <cstdint>
32 #include <memory>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 using namespace llvm;
38 
40 
41 //===----------------------------------------------------------------------===//
42 // Type implementations
43 //===----------------------------------------------------------------------===//
44 
45 BitRecTy BitRecTy::Shared;
46 CodeRecTy CodeRecTy::Shared;
47 IntRecTy IntRecTy::Shared;
48 StringRecTy StringRecTy::Shared;
49 DagRecTy DagRecTy::Shared;
50 
51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
52 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
53 #endif
54 
56  if (!ListTy)
57  ListTy = new(Allocator) ListRecTy(this);
58  return ListTy;
59 }
60 
61 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
62  assert(RHS && "NULL pointer");
63  return Kind == RHS->getRecTyKind();
64 }
65 
66 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
68  return true;
69  if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
70  return BitsTy->getNumBits() == 1;
71  return false;
72 }
73 
74 BitsRecTy *BitsRecTy::get(unsigned Sz) {
75  static std::vector<BitsRecTy*> Shared;
76  if (Sz >= Shared.size())
77  Shared.resize(Sz + 1);
78  BitsRecTy *&Ty = Shared[Sz];
79  if (!Ty)
80  Ty = new(Allocator) BitsRecTy(Sz);
81  return Ty;
82 }
83 
84 std::string BitsRecTy::getAsString() const {
85  return "bits<" + utostr(Size) + ">";
86 }
87 
88 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
89  if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
90  return cast<BitsRecTy>(RHS)->Size == Size;
91  RecTyKind kind = RHS->getRecTyKind();
92  return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
93 }
94 
95 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
96  RecTyKind kind = RHS->getRecTyKind();
97  return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
98 }
99 
100 std::string StringRecTy::getAsString() const {
101  return "string";
102 }
103 
104 std::string ListRecTy::getAsString() const {
105  return "list<" + Ty->getAsString() + ">";
106 }
107 
108 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
109  if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
110  return Ty->typeIsConvertibleTo(ListTy->getElementType());
111  return false;
112 }
113 
114 std::string DagRecTy::getAsString() const {
115  return "dag";
116 }
117 
119  return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
120 }
121 
122 std::string RecordRecTy::getAsString() const {
123  return Rec->getName();
124 }
125 
126 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
127  const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
128  if (!RTy)
129  return false;
130 
131  if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
132  return true;
133 
134  for (const auto &SCPair : RTy->getRecord()->getSuperClasses())
135  if (Rec->isSubClassOf(SCPair.first))
136  return true;
137 
138  return false;
139 }
140 
142  if (T1->typeIsConvertibleTo(T2))
143  return T2;
144  if (T2->typeIsConvertibleTo(T1))
145  return T1;
146 
147  // If one is a Record type, check superclasses
148  if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
149  // See if T2 inherits from a type T1 also inherits from
150  for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) {
151  RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first);
152  RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
153  if (NewType1)
154  return NewType1;
155  }
156  }
157  if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
158  // See if T1 inherits from a type T2 also inherits from
159  for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) {
160  RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first);
161  RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
162  if (NewType2)
163  return NewType2;
164  }
165  }
166  return nullptr;
167 }
168 
169 //===----------------------------------------------------------------------===//
170 // Initializer implementations
171 //===----------------------------------------------------------------------===//
172 
173 void Init::anchor() {}
174 
175 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
176 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
177 #endif
178 
180  static UnsetInit TheInit;
181  return &TheInit;
182 }
183 
185  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
186  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
187 
188  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
189  NewBits[i] = UnsetInit::get();
190 
191  return BitsInit::get(NewBits);
192  }
193 
194  // All other types can just be returned.
195  return const_cast<UnsetInit *>(this);
196 }
197 
199  static BitInit True(true);
200  static BitInit False(false);
201 
202  return V ? &True : &False;
203 }
204 
206  if (isa<BitRecTy>(Ty))
207  return const_cast<BitInit *>(this);
208 
209  if (isa<IntRecTy>(Ty))
210  return IntInit::get(getValue());
211 
212  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
213  // Can only convert single bit.
214  if (BRT->getNumBits() == 1)
215  return BitsInit::get(const_cast<BitInit *>(this));
216  }
217 
218  return nullptr;
219 }
220 
221 static void
223  ID.AddInteger(Range.size());
224 
225  for (Init *I : Range)
226  ID.AddPointer(I);
227 }
228 
230  static FoldingSet<BitsInit> ThePool;
231 
233  ProfileBitsInit(ID, Range);
234 
235  void *IP = nullptr;
236  if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
237  return I;
238 
239  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
240  alignof(BitsInit));
241  BitsInit *I = new(Mem) BitsInit(Range.size());
242  std::uninitialized_copy(Range.begin(), Range.end(),
243  I->getTrailingObjects<Init *>());
244  ThePool.InsertNode(I, IP);
245  return I;
246 }
247 
249  ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
250 }
251 
253  if (isa<BitRecTy>(Ty)) {
254  if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
255  return getBit(0);
256  }
257 
258  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
259  // If the number of bits is right, return it. Otherwise we need to expand
260  // or truncate.
261  if (getNumBits() != BRT->getNumBits()) return nullptr;
262  return const_cast<BitsInit *>(this);
263  }
264 
265  if (isa<IntRecTy>(Ty)) {
266  int64_t Result = 0;
267  for (unsigned i = 0, e = getNumBits(); i != e; ++i)
268  if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
269  Result |= static_cast<int64_t>(Bit->getValue()) << i;
270  else
271  return nullptr;
272  return IntInit::get(Result);
273  }
274 
275  return nullptr;
276 }
277 
278 Init *
280  SmallVector<Init *, 16> NewBits(Bits.size());
281 
282  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
283  if (Bits[i] >= getNumBits())
284  return nullptr;
285  NewBits[i] = getBit(Bits[i]);
286  }
287  return BitsInit::get(NewBits);
288 }
289 
290 std::string BitsInit::getAsString() const {
291  std::string Result = "{ ";
292  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
293  if (i) Result += ", ";
294  if (Init *Bit = getBit(e-i-1))
295  Result += Bit->getAsString();
296  else
297  Result += "*";
298  }
299  return Result + " }";
300 }
301 
302 // Fix bit initializer to preserve the behavior that bit reference from a unset
303 // bits initializer will resolve into VarBitInit to keep the field name and bit
304 // number used in targets with fixed insn length.
305 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
306  if (RV || !isa<UnsetInit>(After))
307  return After;
308  return Before;
309 }
310 
311 // resolveReferences - If there are any field references that refer to fields
312 // that have been filled in, we can propagate the values now.
314  bool Changed = false;
315  SmallVector<Init *, 16> NewBits(getNumBits());
316 
317  Init *CachedInit = nullptr;
318  Init *CachedBitVar = nullptr;
319  bool CachedBitVarChanged = false;
320 
321  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
322  Init *CurBit = getBit(i);
323  Init *CurBitVar = CurBit->getBitVar();
324 
325  NewBits[i] = CurBit;
326 
327  if (CurBitVar == CachedBitVar) {
328  if (CachedBitVarChanged) {
329  Init *Bit = CachedInit->getBit(CurBit->getBitNum());
330  NewBits[i] = fixBitInit(RV, CurBit, Bit);
331  }
332  continue;
333  }
334  CachedBitVar = CurBitVar;
335  CachedBitVarChanged = false;
336 
337  Init *B;
338  do {
339  B = CurBitVar;
340  CurBitVar = CurBitVar->resolveReferences(R, RV);
341  CachedBitVarChanged |= B != CurBitVar;
342  Changed |= B != CurBitVar;
343  } while (B != CurBitVar);
344  CachedInit = CurBitVar;
345 
346  if (CachedBitVarChanged) {
347  Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
348  NewBits[i] = fixBitInit(RV, CurBit, Bit);
349  }
350  }
351 
352  if (Changed)
353  return BitsInit::get(NewBits);
354 
355  return const_cast<BitsInit *>(this);
356 }
357 
358 IntInit *IntInit::get(int64_t V) {
359  static DenseMap<int64_t, IntInit*> ThePool;
360 
361  IntInit *&I = ThePool[V];
362  if (!I) I = new(Allocator) IntInit(V);
363  return I;
364 }
365 
366 std::string IntInit::getAsString() const {
367  return itostr(Value);
368 }
369 
370 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
371  // For example, with NumBits == 4, we permit Values from [-7 .. 15].
372  return (NumBits >= sizeof(Value) * 8) ||
373  (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
374 }
375 
377  if (isa<IntRecTy>(Ty))
378  return const_cast<IntInit *>(this);
379 
380  if (isa<BitRecTy>(Ty)) {
381  int64_t Val = getValue();
382  if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
383  return BitInit::get(Val != 0);
384  }
385 
386  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
387  int64_t Value = getValue();
388  // Make sure this bitfield is large enough to hold the integer value.
389  if (!canFitInBitfield(Value, BRT->getNumBits()))
390  return nullptr;
391 
392  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
393  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
394  NewBits[i] = BitInit::get(Value & (1LL << i));
395 
396  return BitsInit::get(NewBits);
397  }
398 
399  return nullptr;
400 }
401 
402 Init *
404  SmallVector<Init *, 16> NewBits(Bits.size());
405 
406  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
407  if (Bits[i] >= 64)
408  return nullptr;
409 
410  NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
411  }
412  return BitsInit::get(NewBits);
413 }
414 
416  static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
417 
418  auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
419  if (!Entry.second)
420  Entry.second = new(Allocator) CodeInit(Entry.getKey());
421  return Entry.second;
422 }
423 
425  static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
426 
427  auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
428  if (!Entry.second)
429  Entry.second = new(Allocator) StringInit(Entry.getKey());
430  return Entry.second;
431 }
432 
434  if (isa<StringRecTy>(Ty))
435  return const_cast<StringInit *>(this);
436 
437  return nullptr;
438 }
439 
441  if (isa<CodeRecTy>(Ty))
442  return const_cast<CodeInit *>(this);
443 
444  return nullptr;
445 }
446 
448  ArrayRef<Init *> Range,
449  RecTy *EltTy) {
450  ID.AddInteger(Range.size());
451  ID.AddPointer(EltTy);
452 
453  for (Init *I : Range)
454  ID.AddPointer(I);
455 }
456 
458  static FoldingSet<ListInit> ThePool;
459 
461  ProfileListInit(ID, Range, EltTy);
462 
463  void *IP = nullptr;
464  if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
465  return I;
466 
467  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
468  alignof(ListInit));
469  ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
470  std::uninitialized_copy(Range.begin(), Range.end(),
471  I->getTrailingObjects<Init *>());
472  ThePool.InsertNode(I, IP);
473  return I;
474 }
475 
477  RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
478 
479  ProfileListInit(ID, getValues(), EltTy);
480 }
481 
483  if (getType() == Ty)
484  return const_cast<ListInit*>(this);
485 
486  if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
487  SmallVector<Init*, 8> Elements;
488  Elements.reserve(getValues().size());
489 
490  // Verify that all of the elements of the list are subclasses of the
491  // appropriate class!
492  bool Changed = false;
493  RecTy *ElementType = LRT->getElementType();
494  for (Init *I : getValues())
495  if (Init *CI = I->convertInitializerTo(ElementType)) {
496  Elements.push_back(CI);
497  if (CI != I)
498  Changed = true;
499  } else
500  return nullptr;
501 
502  if (!Changed)
503  return const_cast<ListInit*>(this);
504  return ListInit::get(Elements, Ty);
505  }
506 
507  return nullptr;
508 }
509 
512  Vals.reserve(Elements.size());
513  for (unsigned Element : Elements) {
514  if (Element >= size())
515  return nullptr;
516  Vals.push_back(getElement(Element));
517  }
518  return ListInit::get(Vals, getType());
519 }
520 
522  assert(i < NumValues && "List element index out of range!");
523  DefInit *DI = dyn_cast<DefInit>(getElement(i));
524  if (!DI)
525  PrintFatalError("Expected record in list!");
526  return DI->getDef();
527 }
528 
530  SmallVector<Init*, 8> Resolved;
531  Resolved.reserve(size());
532  bool Changed = false;
533 
534  for (Init *CurElt : getValues()) {
535  Init *E;
536 
537  do {
538  E = CurElt;
539  CurElt = CurElt->resolveReferences(R, RV);
540  Changed |= E != CurElt;
541  } while (E != CurElt);
542  Resolved.push_back(E);
543  }
544 
545  if (Changed)
546  return ListInit::get(Resolved, getType());
547  return const_cast<ListInit *>(this);
548 }
549 
551  unsigned Elt) const {
552  if (Elt >= size())
553  return nullptr; // Out of range reference.
554  Init *E = getElement(Elt);
555  // If the element is set to some value, or if we are resolving a reference
556  // to a specific variable and that variable is explicitly unset, then
557  // replace the VarListElementInit with it.
558  if (IRV || !isa<UnsetInit>(E))
559  return E;
560  return nullptr;
561 }
562 
563 std::string ListInit::getAsString() const {
564  std::string Result = "[";
565  const char *sep = "";
566  for (Init *Element : *this) {
567  Result += sep;
568  sep = ", ";
569  Result += Element->getAsString();
570  }
571  return Result + "]";
572 }
573 
575  unsigned Elt) const {
576  Init *Resolved = resolveReferences(R, IRV);
577  OpInit *OResolved = dyn_cast<OpInit>(Resolved);
578  if (OResolved) {
579  Resolved = OResolved->Fold(&R, nullptr);
580  }
581 
582  if (Resolved != this) {
583  TypedInit *Typed = cast<TypedInit>(Resolved);
584  if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
585  return New;
586  return VarListElementInit::get(Typed, Elt);
587  }
588 
589  return nullptr;
590 }
591 
592 Init *OpInit::getBit(unsigned Bit) const {
593  if (getType() == BitRecTy::get())
594  return const_cast<OpInit*>(this);
595  return VarBitInit::get(const_cast<OpInit*>(this), Bit);
596 }
597 
598 static void
600  ID.AddInteger(Opcode);
601  ID.AddPointer(Op);
602  ID.AddPointer(Type);
603 }
604 
606  static FoldingSet<UnOpInit> ThePool;
607 
609  ProfileUnOpInit(ID, Opc, LHS, Type);
610 
611  void *IP = nullptr;
612  if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
613  return I;
614 
615  UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
616  ThePool.InsertNode(I, IP);
617  return I;
618 }
619 
621  ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
622 }
623 
624 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
625  switch (getOpcode()) {
626  case CAST:
627  if (isa<StringRecTy>(getType())) {
628  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
629  return LHSs;
630 
631  if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
632  return StringInit::get(LHSd->getAsString());
633 
634  if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
635  return StringInit::get(LHSi->getAsString());
636  } else {
637  if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
638  // From TGParser::ParseIDValue
639  if (CurRec) {
640  if (const RecordVal *RV = CurRec->getValue(Name)) {
641  if (RV->getType() != getType())
642  PrintFatalError("type mismatch in cast");
643  return VarInit::get(Name, RV->getType());
644  }
645 
646  Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
647  ":");
648 
649  if (CurRec->isTemplateArg(TemplateArgName)) {
650  const RecordVal *RV = CurRec->getValue(TemplateArgName);
651  assert(RV && "Template arg doesn't exist??");
652 
653  if (RV->getType() != getType())
654  PrintFatalError("type mismatch in cast");
655 
656  return VarInit::get(TemplateArgName, RV->getType());
657  }
658  }
659 
660  if (CurMultiClass) {
661  Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
662  "::");
663 
664  if (CurMultiClass->Rec.isTemplateArg(MCName)) {
665  const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
666  assert(RV && "Template arg doesn't exist??");
667 
668  if (RV->getType() != getType())
669  PrintFatalError("type mismatch in cast");
670 
671  return VarInit::get(MCName, RV->getType());
672  }
673  }
674  assert(CurRec && "NULL pointer");
675  if (Record *D = (CurRec->getRecords()).getDef(Name->getValue()))
676  return DefInit::get(D);
677 
678  PrintFatalError(CurRec->getLoc(),
679  "Undefined reference:'" + Name->getValue() + "'\n");
680  }
681 
682  if (isa<IntRecTy>(getType())) {
683  if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
684  if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
685  return NewInit;
686  break;
687  }
688  }
689  }
690  break;
691 
692  case HEAD:
693  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
694  assert(!LHSl->empty() && "Empty list in head");
695  return LHSl->getElement(0);
696  }
697  break;
698 
699  case TAIL:
700  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
701  assert(!LHSl->empty() && "Empty list in tail");
702  // Note the +1. We can't just pass the result of getValues()
703  // directly.
704  return ListInit::get(LHSl->getValues().slice(1), LHSl->getType());
705  }
706  break;
707 
708  case EMPTY:
709  if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
710  return IntInit::get(LHSl->empty());
711  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
712  return IntInit::get(LHSs->getValue().empty());
713  break;
714  }
715  return const_cast<UnOpInit *>(this);
716 }
717 
719  Init *lhs = LHS->resolveReferences(R, RV);
720 
721  if (LHS != lhs)
722  return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
723  return Fold(&R, nullptr);
724 }
725 
726 std::string UnOpInit::getAsString() const {
727  std::string Result;
728  switch (getOpcode()) {
729  case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
730  case HEAD: Result = "!head"; break;
731  case TAIL: Result = "!tail"; break;
732  case EMPTY: Result = "!empty"; break;
733  }
734  return Result + "(" + LHS->getAsString() + ")";
735 }
736 
737 static void
738 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
739  RecTy *Type) {
740  ID.AddInteger(Opcode);
741  ID.AddPointer(LHS);
742  ID.AddPointer(RHS);
743  ID.AddPointer(Type);
744 }
745 
747  Init *RHS, RecTy *Type) {
748  static FoldingSet<BinOpInit> ThePool;
749 
751  ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
752 
753  void *IP = nullptr;
754  if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
755  return I;
756 
757  BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
758  ThePool.InsertNode(I, IP);
759  return I;
760 }
761 
763  ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
764 }
765 
767  const StringInit *I1) {
769  Concat.append(I1->getValue());
770  return StringInit::get(Concat);
771 }
772 
773 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
774  switch (getOpcode()) {
775  case CONCAT: {
776  DagInit *LHSs = dyn_cast<DagInit>(LHS);
777  DagInit *RHSs = dyn_cast<DagInit>(RHS);
778  if (LHSs && RHSs) {
779  DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
780  DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
781  if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
782  PrintFatalError("Concated Dag operators do not match!");
785  for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
786  Args.push_back(LHSs->getArg(i));
787  ArgNames.push_back(LHSs->getArgName(i));
788  }
789  for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
790  Args.push_back(RHSs->getArg(i));
791  ArgNames.push_back(RHSs->getArgName(i));
792  }
793  return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
794  }
795  break;
796  }
797  case LISTCONCAT: {
798  ListInit *LHSs = dyn_cast<ListInit>(LHS);
799  ListInit *RHSs = dyn_cast<ListInit>(RHS);
800  if (LHSs && RHSs) {
802  Args.insert(Args.end(), LHSs->begin(), LHSs->end());
803  Args.insert(Args.end(), RHSs->begin(), RHSs->end());
804  return ListInit::get(
805  Args, cast<ListRecTy>(LHSs->getType())->getElementType());
806  }
807  break;
808  }
809  case STRCONCAT: {
810  StringInit *LHSs = dyn_cast<StringInit>(LHS);
811  StringInit *RHSs = dyn_cast<StringInit>(RHS);
812  if (LHSs && RHSs)
813  return ConcatStringInits(LHSs, RHSs);
814  break;
815  }
816  case EQ: {
817  // try to fold eq comparison for 'bit' and 'int', otherwise fallback
818  // to string objects.
819  IntInit *L =
820  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
821  IntInit *R =
822  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
823 
824  if (L && R)
825  return IntInit::get(L->getValue() == R->getValue());
826 
827  StringInit *LHSs = dyn_cast<StringInit>(LHS);
828  StringInit *RHSs = dyn_cast<StringInit>(RHS);
829 
830  // Make sure we've resolved
831  if (LHSs && RHSs)
832  return IntInit::get(LHSs->getValue() == RHSs->getValue());
833 
834  break;
835  }
836  case ADD:
837  case AND:
838  case OR:
839  case SHL:
840  case SRA:
841  case SRL: {
842  IntInit *LHSi =
843  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
844  IntInit *RHSi =
845  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
846  if (LHSi && RHSi) {
847  int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
848  int64_t Result;
849  switch (getOpcode()) {
850  default: llvm_unreachable("Bad opcode!");
851  case ADD: Result = LHSv + RHSv; break;
852  case AND: Result = LHSv & RHSv; break;
853  case OR: Result = LHSv | RHSv; break;
854  case SHL: Result = LHSv << RHSv; break;
855  case SRA: Result = LHSv >> RHSv; break;
856  case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
857  }
858  return IntInit::get(Result);
859  }
860  break;
861  }
862  }
863  return const_cast<BinOpInit *>(this);
864 }
865 
867  Init *lhs = LHS->resolveReferences(R, RV);
868  Init *rhs = RHS->resolveReferences(R, RV);
869 
870  if (LHS != lhs || RHS != rhs)
871  return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
872  return Fold(&R, nullptr);
873 }
874 
875 std::string BinOpInit::getAsString() const {
876  std::string Result;
877  switch (getOpcode()) {
878  case CONCAT: Result = "!con"; break;
879  case ADD: Result = "!add"; break;
880  case AND: Result = "!and"; break;
881  case OR: Result = "!or"; break;
882  case SHL: Result = "!shl"; break;
883  case SRA: Result = "!sra"; break;
884  case SRL: Result = "!srl"; break;
885  case EQ: Result = "!eq"; break;
886  case LISTCONCAT: Result = "!listconcat"; break;
887  case STRCONCAT: Result = "!strconcat"; break;
888  }
889  return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
890 }
891 
892 static void
893 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
894  Init *RHS, RecTy *Type) {
895  ID.AddInteger(Opcode);
896  ID.AddPointer(LHS);
897  ID.AddPointer(MHS);
898  ID.AddPointer(RHS);
899  ID.AddPointer(Type);
900 }
901 
903  RecTy *Type) {
904  static FoldingSet<TernOpInit> ThePool;
905 
907  ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
908 
909  void *IP = nullptr;
910  if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
911  return I;
912 
913  TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
914  ThePool.InsertNode(I, IP);
915  return I;
916 }
917 
919  ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
920 }
921 
922 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
923  Record *CurRec, MultiClass *CurMultiClass);
924 
925 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
926  RecTy *Type, Record *CurRec,
927  MultiClass *CurMultiClass) {
928  // If this is a dag, recurse
929  if (auto *TArg = dyn_cast<TypedInit>(Arg))
930  if (isa<DagRecTy>(TArg->getType()))
931  return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass);
932 
933  SmallVector<Init *, 8> NewOperands;
934  NewOperands.reserve(RHSo->getNumOperands());
935  for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) {
936  if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
937  if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
938  Type, CurRec, CurMultiClass))
939  NewOperands.push_back(Result);
940  else
941  NewOperands.push_back(Arg);
942  } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
943  NewOperands.push_back(Arg);
944  } else {
945  NewOperands.push_back(RHSo->getOperand(i));
946  }
947  }
948 
949  // Now run the operator and use its result as the new leaf
950  const OpInit *NewOp = RHSo->clone(NewOperands);
951  Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
952  return (NewVal != NewOp) ? NewVal : nullptr;
953 }
954 
955 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
956  Record *CurRec, MultiClass *CurMultiClass) {
957  OpInit *RHSo = dyn_cast<OpInit>(RHS);
958 
959  if (!RHSo)
960  PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
961 
962  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
963 
964  if (!LHSt)
965  PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
966 
967  DagInit *MHSd = dyn_cast<DagInit>(MHS);
968  if (MHSd && isa<DagRecTy>(Type)) {
969  Init *Val = MHSd->getOperator();
970  if (Init *Result = EvaluateOperation(RHSo, LHS, Val,
971  Type, CurRec, CurMultiClass))
972  Val = Result;
973 
975  for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
976  Init *Arg = MHSd->getArg(i);
977  StringInit *ArgName = MHSd->getArgName(i);
978 
979  // Process args
980  if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
981  CurRec, CurMultiClass))
982  Arg = Result;
983 
984  // TODO: Process arg names
985  args.push_back(std::make_pair(Arg, ArgName));
986  }
987 
988  return DagInit::get(Val, nullptr, args);
989  }
990 
991  ListInit *MHSl = dyn_cast<ListInit>(MHS);
992  if (MHSl && isa<ListRecTy>(Type)) {
993  SmallVector<Init *, 8> NewOperands;
994  SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
995 
996  for (Init *&Item : NewList) {
997  NewOperands.clear();
998  for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
999  // First, replace the foreach variable with the list item
1000  if (LHS->getAsString() == RHSo->getOperand(i)->getAsString())
1001  NewOperands.push_back(Item);
1002  else
1003  NewOperands.push_back(RHSo->getOperand(i));
1004  }
1005 
1006  // Now run the operator and use its result as the new list item
1007  const OpInit *NewOp = RHSo->clone(NewOperands);
1008  Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1009  if (NewItem != NewOp)
1010  Item = NewItem;
1011  }
1012  return ListInit::get(NewList, MHSl->getType());
1013  }
1014  return nullptr;
1015 }
1016 
1017 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1018  switch (getOpcode()) {
1019  case SUBST: {
1020  DefInit *LHSd = dyn_cast<DefInit>(LHS);
1021  VarInit *LHSv = dyn_cast<VarInit>(LHS);
1022  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1023 
1024  DefInit *MHSd = dyn_cast<DefInit>(MHS);
1025  VarInit *MHSv = dyn_cast<VarInit>(MHS);
1026  StringInit *MHSs = dyn_cast<StringInit>(MHS);
1027 
1028  DefInit *RHSd = dyn_cast<DefInit>(RHS);
1029  VarInit *RHSv = dyn_cast<VarInit>(RHS);
1030  StringInit *RHSs = dyn_cast<StringInit>(RHS);
1031 
1032  if (LHSd && MHSd && RHSd) {
1033  Record *Val = RHSd->getDef();
1034  if (LHSd->getAsString() == RHSd->getAsString())
1035  Val = MHSd->getDef();
1036  return DefInit::get(Val);
1037  }
1038  if (LHSv && MHSv && RHSv) {
1039  std::string Val = RHSv->getName();
1040  if (LHSv->getAsString() == RHSv->getAsString())
1041  Val = MHSv->getName();
1042  return VarInit::get(Val, getType());
1043  }
1044  if (LHSs && MHSs && RHSs) {
1045  std::string Val = RHSs->getValue();
1046 
1047  std::string::size_type found;
1048  std::string::size_type idx = 0;
1049  while (true) {
1050  found = Val.find(LHSs->getValue(), idx);
1051  if (found == std::string::npos)
1052  break;
1053  Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1054  idx = found + MHSs->getValue().size();
1055  }
1056 
1057  return StringInit::get(Val);
1058  }
1059  break;
1060  }
1061 
1062  case FOREACH: {
1063  if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1064  CurRec, CurMultiClass))
1065  return Result;
1066  break;
1067  }
1068 
1069  case IF: {
1070  IntInit *LHSi = dyn_cast<IntInit>(LHS);
1071  if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1072  LHSi = dyn_cast<IntInit>(I);
1073  if (LHSi) {
1074  if (LHSi->getValue())
1075  return MHS;
1076  return RHS;
1077  }
1078  break;
1079  }
1080  }
1081 
1082  return const_cast<TernOpInit *>(this);
1083 }
1084 
1086  const RecordVal *RV) const {
1087  Init *lhs = LHS->resolveReferences(R, RV);
1088 
1089  if (getOpcode() == IF && lhs != LHS) {
1090  IntInit *Value = dyn_cast<IntInit>(lhs);
1091  if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1092  Value = dyn_cast<IntInit>(I);
1093  if (Value) {
1094  // Short-circuit
1095  if (Value->getValue()) {
1096  Init *mhs = MHS->resolveReferences(R, RV);
1097  return (TernOpInit::get(getOpcode(), lhs, mhs,
1098  RHS, getType()))->Fold(&R, nullptr);
1099  }
1100  Init *rhs = RHS->resolveReferences(R, RV);
1101  return (TernOpInit::get(getOpcode(), lhs, MHS,
1102  rhs, getType()))->Fold(&R, nullptr);
1103  }
1104  }
1105 
1106  Init *mhs = MHS->resolveReferences(R, RV);
1107  Init *rhs = RHS->resolveReferences(R, RV);
1108 
1109  if (LHS != lhs || MHS != mhs || RHS != rhs)
1110  return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1111  getType()))->Fold(&R, nullptr);
1112  return Fold(&R, nullptr);
1113 }
1114 
1115 std::string TernOpInit::getAsString() const {
1116  std::string Result;
1117  switch (getOpcode()) {
1118  case SUBST: Result = "!subst"; break;
1119  case FOREACH: Result = "!foreach"; break;
1120  case IF: Result = "!if"; break;
1121  }
1122  return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1123  RHS->getAsString() + ")";
1124 }
1125 
1127  if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1128  if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1129  return Field->getType();
1130  return nullptr;
1131 }
1132 
1133 Init *
1135  if (isa<IntRecTy>(Ty)) {
1136  if (getType()->typeIsConvertibleTo(Ty))
1137  return const_cast<TypedInit *>(this);
1138  return nullptr;
1139  }
1140 
1141  if (isa<StringRecTy>(Ty)) {
1142  if (isa<StringRecTy>(getType()))
1143  return const_cast<TypedInit *>(this);
1144  return nullptr;
1145  }
1146 
1147  if (isa<CodeRecTy>(Ty)) {
1148  if (isa<CodeRecTy>(getType()))
1149  return const_cast<TypedInit *>(this);
1150  return nullptr;
1151  }
1152 
1153  if (isa<BitRecTy>(Ty)) {
1154  // Accept variable if it is already of bit type!
1155  if (isa<BitRecTy>(getType()))
1156  return const_cast<TypedInit *>(this);
1157  if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1158  // Accept only bits<1> expression.
1159  if (BitsTy->getNumBits() == 1)
1160  return const_cast<TypedInit *>(this);
1161  return nullptr;
1162  }
1163  // Ternary !if can be converted to bit, but only if both sides are
1164  // convertible to a bit.
1165  if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1166  if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1167  TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1168  TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1169  return const_cast<TypedInit *>(this);
1170  return nullptr;
1171  }
1172  return nullptr;
1173  }
1174 
1175  if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1176  if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1177  return BitsInit::get(const_cast<TypedInit *>(this));
1178 
1179  if (getType()->typeIsConvertibleTo(BRT)) {
1180  SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1181 
1182  for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1183  NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1184  return BitsInit::get(NewBits);
1185  }
1186 
1187  return nullptr;
1188  }
1189 
1190  if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1191  if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1192  if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1193  return const_cast<TypedInit *>(this);
1194  return nullptr;
1195  }
1196 
1197  if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1198  if (getType()->typeIsConvertibleTo(DRT))
1199  return const_cast<TypedInit *>(this);
1200  return nullptr;
1201  }
1202 
1203  if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1204  // Ensure that this is compatible with Rec.
1205  if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1206  if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1207  DRRT->getRecord() == SRRT->getRecord())
1208  return const_cast<TypedInit *>(this);
1209  return nullptr;
1210  }
1211 
1212  return nullptr;
1213 }
1214 
1217  if (!T) return nullptr; // Cannot subscript a non-bits variable.
1218  unsigned NumBits = T->getNumBits();
1219 
1220  SmallVector<Init *, 16> NewBits;
1221  NewBits.reserve(Bits.size());
1222  for (unsigned Bit : Bits) {
1223  if (Bit >= NumBits)
1224  return nullptr;
1225 
1226  NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1227  }
1228  return BitsInit::get(NewBits);
1229 }
1230 
1233  if (!T) return nullptr; // Cannot subscript a non-list variable.
1234 
1235  if (Elements.size() == 1)
1236  return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1237 
1238  SmallVector<Init*, 8> ListInits;
1239  ListInits.reserve(Elements.size());
1240  for (unsigned Element : Elements)
1241  ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1242  Element));
1243  return ListInit::get(ListInits, T);
1244 }
1245 
1246 
1248  Init *Value = StringInit::get(VN);
1249  return VarInit::get(Value, T);
1250 }
1251 
1253  using Key = std::pair<RecTy *, Init *>;
1254  static DenseMap<Key, VarInit*> ThePool;
1255 
1256  Key TheKey(std::make_pair(T, VN));
1257 
1258  VarInit *&I = ThePool[TheKey];
1259  if (!I)
1260  I = new(Allocator) VarInit(VN, T);
1261  return I;
1262 }
1263 
1265  StringInit *NameString = cast<StringInit>(getNameInit());
1266  return NameString->getValue();
1267 }
1268 
1269 Init *VarInit::getBit(unsigned Bit) const {
1270  if (getType() == BitRecTy::get())
1271  return const_cast<VarInit*>(this);
1272  return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1273 }
1274 
1276  const RecordVal *IRV,
1277  unsigned Elt) const {
1278  if (R.isTemplateArg(getNameInit())) return nullptr;
1279  if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
1280 
1281  RecordVal *RV = R.getValue(getNameInit());
1282  assert(RV && "Reference to a non-existent variable?");
1283  ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1284  if (!LI)
1285  return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
1286 
1287  if (Elt >= LI->size())
1288  return nullptr; // Out of range reference.
1289  Init *E = LI->getElement(Elt);
1290  // If the element is set to some value, or if we are resolving a reference
1291  // to a specific variable and that variable is explicitly unset, then
1292  // replace the VarListElementInit with it.
1293  if (IRV || !isa<UnsetInit>(E))
1294  return E;
1295  return nullptr;
1296 }
1297 
1299  if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1300  if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1301  return RV->getType();
1302  return nullptr;
1303 }
1304 
1306  StringInit *FieldName) const {
1307  if (isa<RecordRecTy>(getType()))
1308  if (const RecordVal *Val = R.getValue(VarName)) {
1309  if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1310  return nullptr;
1311  Init *TheInit = Val->getValue();
1312  assert(TheInit != this && "Infinite loop detected!");
1313  if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1314  return I;
1315  return nullptr;
1316  }
1317  return nullptr;
1318 }
1319 
1321  if (RecordVal *Val = R.getValue(VarName))
1322  if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1323  return Val->getValue();
1324  return const_cast<VarInit *>(this);
1325 }
1326 
1328  using Key = std::pair<TypedInit *, unsigned>;
1329  static DenseMap<Key, VarBitInit*> ThePool;
1330 
1331  Key TheKey(std::make_pair(T, B));
1332 
1333  VarBitInit *&I = ThePool[TheKey];
1334  if (!I)
1335  I = new(Allocator) VarBitInit(T, B);
1336  return I;
1337 }
1338 
1340  if (isa<BitRecTy>(Ty))
1341  return const_cast<VarBitInit *>(this);
1342 
1343  return nullptr;
1344 }
1345 
1346 std::string VarBitInit::getAsString() const {
1347  return TI->getAsString() + "{" + utostr(Bit) + "}";
1348 }
1349 
1351  Init *I = TI->resolveReferences(R, RV);
1352  if (TI != I)
1353  return I->getBit(getBitNum());
1354 
1355  return const_cast<VarBitInit*>(this);
1356 }
1357 
1359  unsigned E) {
1360  using Key = std::pair<TypedInit *, unsigned>;
1361  static DenseMap<Key, VarListElementInit*> ThePool;
1362 
1363  Key TheKey(std::make_pair(T, E));
1364 
1365  VarListElementInit *&I = ThePool[TheKey];
1366  if (!I) I = new(Allocator) VarListElementInit(T, E);
1367  return I;
1368 }
1369 
1370 std::string VarListElementInit::getAsString() const {
1371  return TI->getAsString() + "[" + utostr(Element) + "]";
1372 }
1373 
1374 Init *
1376  if (Init *I = getVariable()->resolveListElementReference(R, RV,
1377  getElementNum()))
1378  return I;
1379  return const_cast<VarListElementInit *>(this);
1380 }
1381 
1383  if (getType() == BitRecTy::get())
1384  return const_cast<VarListElementInit*>(this);
1385  return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1386 }
1387 
1389  const RecordVal *RV,
1390  unsigned Elt) const {
1391  if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
1392  if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1393  if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
1394  return Result2;
1395  return VarListElementInit::get(TInit, Elt);
1396  }
1397  return Result;
1398  }
1399 
1400  return nullptr;
1401 }
1402 
1404  return R->getDefInit();
1405 }
1406 
1408  if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1409  if (getDef()->isSubClassOf(RRT->getRecord()))
1410  return const_cast<DefInit *>(this);
1411  return nullptr;
1412 }
1413 
1415  if (const RecordVal *RV = Def->getValue(FieldName))
1416  return RV->getType();
1417  return nullptr;
1418 }
1419 
1421  StringInit *FieldName) const {
1422  return Def->getValue(FieldName)->getValue();
1423 }
1424 
1425 std::string DefInit::getAsString() const {
1426  return Def->getName();
1427 }
1428 
1430  using Key = std::pair<Init *, StringInit *>;
1431  static DenseMap<Key, FieldInit*> ThePool;
1432 
1433  Key TheKey(std::make_pair(R, FN));
1434 
1435  FieldInit *&I = ThePool[TheKey];
1436  if (!I) I = new(Allocator) FieldInit(R, FN);
1437  return I;
1438 }
1439 
1440 Init *FieldInit::getBit(unsigned Bit) const {
1441  if (getType() == BitRecTy::get())
1442  return const_cast<FieldInit*>(this);
1443  return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1444 }
1445 
1447  unsigned Elt) const {
1448  if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1449  if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1450  if (Elt >= LI->size()) return nullptr;
1451  Init *E = LI->getElement(Elt);
1452 
1453  // If the element is set to some value, or if we are resolving a
1454  // reference to a specific variable and that variable is explicitly
1455  // unset, then replace the VarListElementInit with it.
1456  if (RV || !isa<UnsetInit>(E))
1457  return E;
1458  }
1459  return nullptr;
1460 }
1461 
1463  Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1464 
1465  if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
1466  Init *BVR = BitsVal->resolveReferences(R, RV);
1467  return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1468  }
1469 
1470  if (NewRec != Rec)
1471  return FieldInit::get(NewRec, FieldName);
1472  return const_cast<FieldInit *>(this);
1473 }
1474 
1476  ArrayRef<Init *> ArgRange,
1477  ArrayRef<StringInit *> NameRange) {
1478  ID.AddPointer(V);
1479  ID.AddPointer(VN);
1480 
1481  ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1483  while (Arg != ArgRange.end()) {
1484  assert(Name != NameRange.end() && "Arg name underflow!");
1485  ID.AddPointer(*Arg++);
1486  ID.AddPointer(*Name++);
1487  }
1488  assert(Name == NameRange.end() && "Arg name overflow!");
1489 }
1490 
1491 DagInit *
1493  ArrayRef<StringInit *> NameRange) {
1494  static FoldingSet<DagInit> ThePool;
1495 
1497  ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1498 
1499  void *IP = nullptr;
1500  if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1501  return I;
1502 
1503  void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1504  DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1505  std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1506  I->getTrailingObjects<Init *>());
1507  std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1508  I->getTrailingObjects<StringInit *>());
1509  ThePool.InsertNode(I, IP);
1510  return I;
1511 }
1512 
1513 DagInit *
1515  ArrayRef<std::pair<Init*, StringInit*>> args) {
1518 
1519  for (const auto &Arg : args) {
1520  Args.push_back(Arg.first);
1521  Names.push_back(Arg.second);
1522  }
1523 
1524  return DagInit::get(V, VN, Args, Names);
1525 }
1526 
1528  ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1529 }
1530 
1532  if (isa<DagRecTy>(Ty))
1533  return const_cast<DagInit *>(this);
1534 
1535  return nullptr;
1536 }
1537 
1539  SmallVector<Init*, 8> NewArgs;
1540  NewArgs.reserve(arg_size());
1541  bool ArgsChanged = false;
1542  for (const Init *Arg : getArgs()) {
1543  Init *NewArg = Arg->resolveReferences(R, RV);
1544  NewArgs.push_back(NewArg);
1545  ArgsChanged |= NewArg != Arg;
1546  }
1547 
1548  Init *Op = Val->resolveReferences(R, RV);
1549  if (Op != Val || ArgsChanged)
1550  return DagInit::get(Op, ValName, NewArgs, getArgNames());
1551 
1552  return const_cast<DagInit *>(this);
1553 }
1554 
1555 std::string DagInit::getAsString() const {
1556  std::string Result = "(" + Val->getAsString();
1557  if (ValName)
1558  Result += ":" + ValName->getAsUnquotedString();
1559  if (!arg_empty()) {
1560  Result += " " + getArg(0)->getAsString();
1561  if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1562  for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1563  Result += ", " + getArg(i)->getAsString();
1564  if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1565  }
1566  }
1567  return Result + ")";
1568 }
1569 
1570 //===----------------------------------------------------------------------===//
1571 // Other implementations
1572 //===----------------------------------------------------------------------===//
1573 
1575  : Name(N), TyAndPrefix(T, P) {
1577  assert(Value && "Cannot create unset value for current type!");
1578 }
1579 
1581  return cast<StringInit>(getNameInit())->getValue();
1582 }
1583 
1584 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1585 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1586 #endif
1587 
1588 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1589  if (getPrefix()) OS << "field ";
1590  OS << *getType() << " " << getNameInitAsString();
1591 
1592  if (getValue())
1593  OS << " = " << *getValue();
1594 
1595  if (PrintSem) OS << ";\n";
1596 }
1597 
1598 unsigned Record::LastID = 0;
1599 
1600 void Record::init() {
1601  checkName();
1602 
1603  // Every record potentially has a def at the top. This value is
1604  // replaced with the top-level def name at instantiation time.
1605  addValue(RecordVal(StringInit::get("NAME"), StringRecTy::get(), false));
1606 }
1607 
1608 void Record::checkName() {
1609  // Ensure the record name has string type.
1610  const TypedInit *TypedName = cast<const TypedInit>(Name);
1611  if (!isa<StringRecTy>(TypedName->getType()))
1612  PrintFatalError(getLoc(), "Record name is not a string!");
1613 }
1614 
1616  if (!TheInit)
1617  TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this));
1618  return TheInit;
1619 }
1620 
1622  return cast<StringInit>(Name)->getValue();
1623 }
1624 
1625 void Record::setName(Init *NewName) {
1626  Name = NewName;
1627  checkName();
1628  // DO NOT resolve record values to the name at this point because
1629  // there might be default values for arguments of this def. Those
1630  // arguments might not have been resolved yet so we don't want to
1631  // prematurely assume values for those arguments were not passed to
1632  // this def.
1633  //
1634  // Nonetheless, it may be that some of this Record's values
1635  // reference the record name. Indeed, the reason for having the
1636  // record name be an Init is to provide this flexibility. The extra
1637  // resolve steps after completely instantiating defs takes care of
1638  // this. See TGParser::ParseDef and TGParser::ParseDefm.
1639 }
1640 
1642  for (RecordVal &Value : Values) {
1643  if (RV == &Value) // Skip resolve the same field as the given one
1644  continue;
1645  if (Init *V = Value.getValue())
1646  if (Value.setValue(V->resolveReferences(*this, RV)))
1647  PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1648  Value.getNameInitAsString() +
1649  "' after resolving references" +
1650  (RV ? " against '" + RV->getNameInitAsString() +
1651  "' of (" + RV->getValue()->getAsUnquotedString() +
1652  ")"
1653  : "") + "\n");
1654  }
1655  Init *OldName = getNameInit();
1656  Init *NewName = Name->resolveReferences(*this, RV);
1657  if (NewName != OldName) {
1658  // Re-register with RecordKeeper.
1659  setName(NewName);
1660  }
1661 }
1662 
1663 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1664 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1665 #endif
1666 
1668  OS << R.getNameInitAsString();
1669 
1670  ArrayRef<Init *> TArgs = R.getTemplateArgs();
1671  if (!TArgs.empty()) {
1672  OS << "<";
1673  bool NeedComma = false;
1674  for (const Init *TA : TArgs) {
1675  if (NeedComma) OS << ", ";
1676  NeedComma = true;
1677  const RecordVal *RV = R.getValue(TA);
1678  assert(RV && "Template argument record not found??");
1679  RV->print(OS, false);
1680  }
1681  OS << ">";
1682  }
1683 
1684  OS << " {";
1686  if (!SC.empty()) {
1687  OS << "\t//";
1688  for (const auto &SuperPair : SC)
1689  OS << " " << SuperPair.first->getNameInitAsString();
1690  }
1691  OS << "\n";
1692 
1693  for (const RecordVal &Val : R.getValues())
1694  if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1695  OS << Val;
1696  for (const RecordVal &Val : R.getValues())
1697  if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1698  OS << Val;
1699 
1700  return OS << "}\n";
1701 }
1702 
1704  const RecordVal *R = getValue(FieldName);
1705  if (!R || !R->getValue())
1706  PrintFatalError(getLoc(), "Record `" + getName() +
1707  "' does not have a field named `" + FieldName + "'!\n");
1708  return R->getValue();
1709 }
1710 
1712  const RecordVal *R = getValue(FieldName);
1713  if (!R || !R->getValue())
1714  PrintFatalError(getLoc(), "Record `" + getName() +
1715  "' does not have a field named `" + FieldName + "'!\n");
1716 
1717  if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1718  return SI->getValue();
1719  if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1720  return CI->getValue();
1721 
1722  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1723  FieldName + "' does not have a string initializer!");
1724 }
1725 
1727  const RecordVal *R = getValue(FieldName);
1728  if (!R || !R->getValue())
1729  PrintFatalError(getLoc(), "Record `" + getName() +
1730  "' does not have a field named `" + FieldName + "'!\n");
1731 
1732  if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1733  return BI;
1734  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1735  FieldName + "' does not have a BitsInit initializer!");
1736 }
1737 
1739  const RecordVal *R = getValue(FieldName);
1740  if (!R || !R->getValue())
1741  PrintFatalError(getLoc(), "Record `" + getName() +
1742  "' does not have a field named `" + FieldName + "'!\n");
1743 
1744  if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1745  return LI;
1746  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1747  FieldName + "' does not have a list initializer!");
1748 }
1749 
1750 std::vector<Record*>
1752  ListInit *List = getValueAsListInit(FieldName);
1753  std::vector<Record*> Defs;
1754  for (Init *I : List->getValues()) {
1755  if (DefInit *DI = dyn_cast<DefInit>(I))
1756  Defs.push_back(DI->getDef());
1757  else
1758  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1759  FieldName + "' list is not entirely DefInit!");
1760  }
1761  return Defs;
1762 }
1763 
1764 int64_t Record::getValueAsInt(StringRef FieldName) const {
1765  const RecordVal *R = getValue(FieldName);
1766  if (!R || !R->getValue())
1767  PrintFatalError(getLoc(), "Record `" + getName() +
1768  "' does not have a field named `" + FieldName + "'!\n");
1769 
1770  if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1771  return II->getValue();
1772  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1773  FieldName + "' does not have an int initializer!");
1774 }
1775 
1776 std::vector<int64_t>
1778  ListInit *List = getValueAsListInit(FieldName);
1779  std::vector<int64_t> Ints;
1780  for (Init *I : List->getValues()) {
1781  if (IntInit *II = dyn_cast<IntInit>(I))
1782  Ints.push_back(II->getValue());
1783  else
1784  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1785  FieldName + "' does not have a list of ints initializer!");
1786  }
1787  return Ints;
1788 }
1789 
1790 std::vector<StringRef>
1792  ListInit *List = getValueAsListInit(FieldName);
1793  std::vector<StringRef> Strings;
1794  for (Init *I : List->getValues()) {
1795  if (StringInit *SI = dyn_cast<StringInit>(I))
1796  Strings.push_back(SI->getValue());
1797  else
1798  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1799  FieldName + "' does not have a list of strings initializer!");
1800  }
1801  return Strings;
1802 }
1803 
1805  const RecordVal *R = getValue(FieldName);
1806  if (!R || !R->getValue())
1807  PrintFatalError(getLoc(), "Record `" + getName() +
1808  "' does not have a field named `" + FieldName + "'!\n");
1809 
1810  if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1811  return DI->getDef();
1812  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1813  FieldName + "' does not have a def initializer!");
1814 }
1815 
1816 bool Record::getValueAsBit(StringRef FieldName) const {
1817  const RecordVal *R = getValue(FieldName);
1818  if (!R || !R->getValue())
1819  PrintFatalError(getLoc(), "Record `" + getName() +
1820  "' does not have a field named `" + FieldName + "'!\n");
1821 
1822  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1823  return BI->getValue();
1824  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1825  FieldName + "' does not have a bit initializer!");
1826 }
1827 
1828 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1829  const RecordVal *R = getValue(FieldName);
1830  if (!R || !R->getValue())
1831  PrintFatalError(getLoc(), "Record `" + getName() +
1832  "' does not have a field named `" + FieldName.str() + "'!\n");
1833 
1834  if (isa<UnsetInit>(R->getValue())) {
1835  Unset = true;
1836  return false;
1837  }
1838  Unset = false;
1839  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1840  return BI->getValue();
1841  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1842  FieldName + "' does not have a bit initializer!");
1843 }
1844 
1846  const RecordVal *R = getValue(FieldName);
1847  if (!R || !R->getValue())
1848  PrintFatalError(getLoc(), "Record `" + getName() +
1849  "' does not have a field named `" + FieldName + "'!\n");
1850 
1851  if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1852  return DI;
1853  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1854  FieldName + "' does not have a dag initializer!");
1855 }
1856 
1857 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1859  errs() << "Record:\n";
1860  Rec.dump();
1861 
1862  errs() << "Defs:\n";
1863  for (const auto &Proto : DefPrototypes)
1864  Proto->dump();
1865 }
1866 
1867 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
1868 #endif
1869 
1871  OS << "------------- Classes -----------------\n";
1872  for (const auto &C : RK.getClasses())
1873  OS << "class " << *C.second;
1874 
1875  OS << "------------- Defs -----------------\n";
1876  for (const auto &D : RK.getDefs())
1877  OS << "def " << *D.second;
1878  return OS;
1879 }
1880 
1881 std::vector<Record *>
1883  Record *Class = getClass(ClassName);
1884  if (!Class)
1885  PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1886 
1887  std::vector<Record*> Defs;
1888  for (const auto &D : getDefs())
1889  if (D.second->isSubClassOf(Class))
1890  Defs.push_back(D.second.get());
1891 
1892  return Defs;
1893 }
1894 
1895 static Init *GetStrConcat(Init *I0, Init *I1) {
1896  // Shortcut for the common case of concatenating two strings.
1897  if (const StringInit *I0s = dyn_cast<StringInit>(I0))
1898  if (const StringInit *I1s = dyn_cast<StringInit>(I1))
1899  return ConcatStringInits(I0s, I1s);
1901 }
1902 
1903 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1904  Init *Name, StringRef Scoper) {
1905  Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
1906  NewName = GetStrConcat(NewName, Name);
1907  if (CurMultiClass && Scoper != "::") {
1908  Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(),
1909  StringInit::get("::"));
1910  NewName = GetStrConcat(Prefix, NewName);
1911  }
1912 
1913  if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
1914  NewName = BinOp->Fold(&CurRec, CurMultiClass);
1915  return NewName;
1916 }
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1320
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:746
static Init * GetStrConcat(Init *I0, Init *I1)
Definition: Record.cpp:1895
uint64_t CallInst * C
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:52
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:252
StringRef getName() const
Definition: Record.cpp:1580
Init * resolveReferences(Record &R, const RecordVal *RV) 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:529
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1462
RecTyKind
Subclass discriminator (for dyn_cast<> et al.)
Definition: Record.h:54
void print(raw_ostream &OS) const
Definition: Record.h:76
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
StringRef getName() const
Definition: Record.cpp:1264
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:1804
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:1726
Init * getValue() const
Definition: Record.h:1230
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:1079
#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:403
virtual std::string getAsUnquotedString() const
Convert this value to a string form, without adding quote markers.
Definition: Record.h:321
[AL, AH, CL] - Represent a list of defs
Definition: Record.h:653
iterator begin() const
Definition: ArrayRef.h:137
&#39;7&#39; - Represent an initialization by a literal integer value.
Definition: Record.h:537
&#39;list<Ty>&#39; - Represent a list of values, all of which must be of the specified type.
Definition: Record.h:183
!op (X, Y) - Combine two inits.
Definition: Record.h:802
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1382
void dump() const
Definition: Record.cpp:1858
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:476
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:1845
Init * resolveReferences(Record &R, const RecordVal *RV) 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:718
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:205
AL - Represent a reference to a &#39;def&#39; in the description.
Definition: Record.h:1036
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:605
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:550
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:1215
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1414
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:574
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1085
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1346
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
Definition: Record.cpp:1017
static VarInit * get(StringRef VN, RecTy *T)
Definition: Record.cpp:1247
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:1738
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:1764
static BumpPtrAllocator Allocator
Definition: Record.cpp:39
void reserve(size_type N)
Definition: SmallVector.h:380
&#39;{ a, b, c }&#39; - Represents an initializer for a BitsRecTy value.
Definition: Record.h:479
Record * getElementAsRecord(unsigned i) const
Definition: Record.cpp:521
std::string getAsString() const override
Definition: Record.cpp:104
std::string getNameInitAsString() const
Definition: Record.h:1224
void dump() const
Debugging method that may be called through a debugger, just invokes print on stderr.
Definition: Record.cpp:176
static BitRecTy * get()
Definition: Record.h:103
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
static IntInit * get(int64_t V)
Definition: Record.cpp:358
static BitsInit * get(ArrayRef< Init *> Range)
Definition: Record.cpp:229
bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const
This method looks up the specified field and returns its value as a bit.
Definition: Record.cpp:1828
Init * getArg(unsigned Num) const
Definition: Record.h:1155
StringRef getName() const
Definition: Record.cpp:1621
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
Definition: Record.h:1335
&#39;?&#39; - Represents an uninitialized value
Definition: Record.h:426
&#39;true&#39;/&#39;false&#39; - Represent a concrete initializer for a bit.
Definition: Record.h:450
Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const override
This method is used to implement the bitrange selection operator.
Definition: Record.cpp:279
void dump() const
Definition: Record.cpp:1585
static BitsRecTy * get(unsigned Sz)
Definition: Record.cpp:74
Record * getDef() const
Definition: Record.h:1055
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const =0
This method is used to implement VarListElementInit::resolveReferences.
This is the common super-class of types that have a specific, explicit, type.
Definition: Record.h:389
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
Shift and rotation operations.
Definition: ISDOpcodes.h:379
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:88
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:248
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:592
RecTy * getElementType() const
Definition: Record.h:196
static StringRecTy * get()
Definition: Record.h:176
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:762
ArrayRef< Init * > getTemplateArgs() const
Definition: Record.h:1329
std::string getAsString() const override
Definition: Record.cpp:122
std::string getAsString() const override
Definition: Record.cpp:100
List[4] - Represent access to one element of a var or field.
Definition: Record.h:998
&#39;[classname]&#39; - Represent an instance of a class, such as: (R32 X = EAX).
Definition: Record.h:221
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:433
virtual bool isComplete() const
This virtual method should be overridden by values that may not be completely specified yet...
Definition: Record.h:310
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:366
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:1751
void AddInteger(signed I)
Definition: FoldingSet.cpp:61
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:1703
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1350
static Init * ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass)
Definition: Record.cpp:955
void print(raw_ostream &OS, bool PrintSem=true) const
Definition: Record.cpp:1588
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:726
virtual unsigned getNumOperands() const =0
const_iterator end() const
Definition: Record.h:703
StringInit * getArgName(unsigned Num) const
Definition: Record.h:1160
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:1324
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
This method is used by classes that refer to other variables which may not be defined at the time the...
Definition: Record.h:366
&#39;bits<n>&#39; - Represent a fixed number of bits
Definition: Record.h:111
std::string itostr(int64_t X)
Definition: StringExtras.h:189
bool isTemplateArg(Init *Name) const
Definition: Record.h:1339
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1538
RecTy * getType() const
Definition: Record.h:1229
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:200
bool isSubClassOf(const Record *R) const
Definition: Record.h:1393
ArrayRef< Init * > getValues() const
Definition: Record.h:698
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:563
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:123
#define EQ(a, b)
Definition: regexec.c:112
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
Definition: Record.cpp:624
virtual Init * getBit(unsigned Bit) const =0
This method is used to return the initializer for the specified bit.
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:1388
ArrayRef< RecordVal > getValues() const
Definition: Record.h:1333
virtual Init * getOperand(unsigned i) const =0
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:510
&#39;code&#39; - Represent a code fragment
Definition: Record.h:131
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:1641
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:440
virtual Init * convertInitializerTo(RecTy *Ty) const =0
This virtual function converts to the appropriate Init based on the passed in type.
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:482
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:95
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:721
#define P(N)
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1531
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
RecTy * getType() const
Definition: Record.h:405
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:126
Init * getElement(unsigned i) const
Definition: Record.h:678
void dump() const
Definition: Record.cpp:1664
Init * getNameInit() const
Definition: Record.h:1222
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:138
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:1275
"foo" - Represent an initialization by a string value.
Definition: Record.h:574
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1126
static void ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:893
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:212
void dump() const
Definition: Record.cpp:1867
Record * getRecord() const
Definition: Record.h:235
static void ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type)
Definition: Record.cpp:599
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...
!op (X) - Transform an init.
Definition: Record.h:753
const std::string getNameInitAsString() const
Definition: Record.h:1318
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1339
static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
Definition: Record.cpp:1475
bool getPrefix() const
Definition: Record.h:1228
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
Definition: Record.cpp:1492
void dump() const
Definition: Record.cpp:52
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:1711
int64_t getValue() const
Definition: Record.h:553
&#39;string&#39; - Represent an string value
Definition: Record.h:165
static BitInit * get(bool V)
Definition: Record.cpp:198
static bool canFitInBitfield(int64_t Value, unsigned NumBits)
Definition: Record.cpp:370
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1425
static CodeInit * get(StringRef)
Definition: Record.cpp:415
static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, RecTy *Type)
Definition: Record.cpp:738
DefInit * getDefInit()
get the corresponding DefInit.
Definition: Record.cpp:1615
static wasm::ValType getType(const TargetRegisterClass *RC)
virtual Init * getFieldInit(Record &R, const RecordVal *RV, StringInit *FieldName) const
This method complements getFieldType to return the initializer for the specified field.
Definition: Record.h:357
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:1555
#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:1777
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1134
virtual Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const =0
std::vector< Record * > getAllDerivedDefinitions(StringRef ClassName) const
This method returns all concrete definitions that derive from the specified class name...
Definition: Record.cpp:1882
virtual OpInit * clone(ArrayRef< Init *> Operands) const =0
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:1791
static FieldInit * get(Init *R, StringInit *FN)
Definition: Record.cpp:1429
static Init * EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass)
Definition: Record.cpp:925
std::string getAsString() const override
Definition: Record.cpp:84
Init * convertInitListSlice(ArrayRef< unsigned > Elements) const override
This method is used to implement the list slice selection operator.
Definition: Record.cpp:1231
static StringInit * ConcatStringInits(const StringInit *I0, const StringInit *I1)
Definition: Record.cpp:766
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1345
const RecordMap & getClasses() const
Definition: Record.h:1532
Init * getOperator() const
Definition: Record.h:1145
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
static VarBitInit * get(TypedInit *T, unsigned B)
Definition: Record.cpp:1327
static StringInit * get(StringRef)
Definition: Record.cpp:424
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:1903
static RecordRecTy * get(Record *R)
Definition: Record.cpp:118
RecordKeeper & getRecords() const
Definition: Record.h:1426
iterator end() const
Definition: ArrayRef.h:138
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:290
CHAIN = SC CHAIN, Imm128 - System call.
unsigned getNumArgs() const
Definition: Record.h:1153
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:1407
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static DefInit * get(Record *)
Definition: Record.cpp:1403
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:174
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:376
virtual Init * getBitVar() const
This method is used to retrieve the initializer for bit reference.
Definition: Record.h:376
Init * getFieldInit(Record &R, const RecordVal *RV, StringInit *FieldName) const override
This method complements getFieldType to return the initializer for the specified field.
Definition: Record.cpp:1420
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:370
Init * resolveReferences(Record &R, const RecordVal *RV) 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:866
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:224
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
Definition: Error.cpp:70
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:620
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:482
&#39;int&#39; - Represent an integer value of no particular size
Definition: Record.h:147
static Init * fixBitInit(const RecordVal *RV, Init *Before, Init *After)
Definition: Record.cpp:305
amdgpu Simplify well known AMD library false Value Value * Arg
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
This method is used to implement VarListElementInit::resolveReferences.
Definition: Record.cpp:1446
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
Definition: Record.cpp:1269
static MachineInstr * getDef(unsigned Reg, const MachineRegisterInfo *MRI)
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:1816
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:875
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1527
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:108
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:120
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:362
Init * getNameInit() const
Definition: Record.h:1314
static UnsetInit * get()
Definition: Record.cpp:179
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:66
const NodeList & List
Definition: RDFGraph.cpp:210
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:902
const_iterator begin() const
Definition: Record.h:702
static VarListElementInit * get(TypedInit *T, unsigned E)
Definition: Record.cpp:1358
#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:141
Opcode{0} - Represent access to one bit of a variable or field.
Definition: Record.h:960
&#39;Opcode&#39; - Represent a reference to an entire variable object.
Definition: Record.h:916
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1115
std::string getAsString() const override
Definition: Record.cpp:114
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:93
!op (X, Y, Z) - Combine two inits.
Definition: Record.h:856
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
Definition: Record.cpp:773
static void ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef< Init *> Range)
Definition: Record.cpp:222
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2018
Init * resolveReferences(Record &R, const RecordVal *RV) 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:1375
static ListInit * get(ArrayRef< Init *> Range, RecTy *EltTy)
Definition: Record.cpp:457
RecTyKind getRecTyKind() const
Definition: Record.h:73
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:61
StringRef getValue() const
Definition: Record.h:590
&#39;dag&#39; - Represent a dag fragment
Definition: Record.h:204
Init * resolveReferences(Record &R, const RecordVal *RV) 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:313
virtual unsigned getBitNum() const
This method is used to retrieve the bit number of a bit reference.
Definition: Record.h:380
Init * getFieldInit(Record &R, const RecordVal *RV, StringInit *FieldName) const override
This method complements getFieldType to return the initializer for the specified field.
Definition: Record.cpp:1305
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:1440
LLVM Value Representation.
Definition: Value.h:73
(v a, b) - Represent a DAG tree value.
Definition: Record.h:1113
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:705
static void ProfileListInit(FoldingSetNodeID &ID, ArrayRef< Init *> Range, RecTy *EltTy)
Definition: Record.cpp:447
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void setName(Init *Name)
Definition: Record.cpp:1625
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:918
Init * convertInitializerTo(RecTy *Ty) const override
This virtual function converts to the appropriate Init based on the passed in type.
Definition: Record.cpp:184
ListRecTy * getListTy()
Returns the type representing list<this>.
Definition: Record.cpp:55
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
const RecordMap & getDefs() const
Definition: Record.h:1533
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
RecTy * getFieldType(StringInit *FieldName) const override
This method is used to implement the FieldInit class.
Definition: Record.cpp:1298
#define T1
std::string getAsString() const override
Convert this value to a string form.
Definition: Record.cpp:1370
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
static IntRecTy * get()
Definition: Record.h:157
RecordVal(Init *N, RecTy *T, bool P)
Definition: Record.cpp:1574