Bug Summary

File:lib/TableGen/Record.cpp
Warning:line 427, column 11
Value stored to 'NewBit' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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