Skip to content

Commit

Permalink
TableGen: Remove Init::resolveListElementReference
Browse files Browse the repository at this point in the history
Summary:
Resolving a VarListElementInit should just resolve the list and then
take its element. This eliminates a lot of duplicated logic and
simplifies the next steps of refactoring resolveReferences.

This does potentially cause sub-elements of the entire list to be
resolved resulting in more work, but I didn't notice a measurable
change in performance, and a later patch adds a caching mechanism that
covers at least the common case of `var[i]` in a more generic way.

Change-Id: I7b59185b855c7368585c329c31e5be38c5749dac

Reviewers: arsenm, craig.topper, tra, MartinO

Subscribers: wdng, llvm-commits

Differential Revision: https://reviews.llvm.org/D43562

llvm-svn: 326059
  • Loading branch information
nhaehnle committed Feb 25, 2018
1 parent 836684a commit 801403a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 170 deletions.
82 changes: 0 additions & 82 deletions llvm/include/llvm/TableGen/Record.h
Expand Up @@ -419,12 +419,6 @@ class TypedInit : public Init {
/// they are of record type.
///
RecTy *getFieldType(StringInit *FieldName) const override;

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const = 0;
};

/// '?' - Represents an uninitialized value
Expand Down Expand Up @@ -471,11 +465,6 @@ class BitInit final : public TypedInit {

Init *convertInitializerTo(RecTy *Ty) const override;

Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off bit");
}

Init *getBit(unsigned Bit) const override {
assert(Bit < 1 && "Bit index out of range!");
return const_cast<BitInit*>(this);
Expand Down Expand Up @@ -527,14 +516,6 @@ class BitsInit final : public TypedInit, public FoldingSetNode,

std::string getAsString() const override;

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off bits<n>");
}

Init *resolveReferences(Record &R, const RecordVal *RV) const override;

Init *getBit(unsigned Bit) const override {
Expand Down Expand Up @@ -567,14 +548,6 @@ class IntInit : public TypedInit {

std::string getAsString() const override;

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off int");
}

Init *getBit(unsigned Bit) const override {
return BitInit::get((Value & (1ULL << Bit)) != 0);
}
Expand Down Expand Up @@ -605,14 +578,6 @@ class StringInit : public TypedInit {

std::string getAsUnquotedString() const override { return Value; }

/// resolveListElementReference - This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off string");
}

Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off string");
}
Expand Down Expand Up @@ -645,14 +610,6 @@ class CodeInit : public TypedInit {

std::string getAsUnquotedString() const override { return Value; }

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off string");
}

Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off string");
}
Expand Down Expand Up @@ -718,12 +675,6 @@ class ListInit final : public TypedInit, public FoldingSetNode,
size_t size () const { return NumValues; }
bool empty() const { return NumValues == 0; }

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override;

Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off list");
}
Expand Down Expand Up @@ -755,9 +706,6 @@ class OpInit : public TypedInit {
// possible to fold.
virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;

Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override;

Init *getBit(unsigned Bit) const override;
};

Expand Down Expand Up @@ -952,9 +900,6 @@ class VarInit : public TypedInit {
return getNameInit()->getAsUnquotedString();
}

Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override;

RecTy *getFieldType(StringInit *FieldName) const override;
Init *getFieldInit(Record &R, const RecordVal *RV,
StringInit *FieldName) const override;
Expand Down Expand Up @@ -1003,11 +948,6 @@ class VarBitInit final : public TypedInit {
std::string getAsString() const override;
Init *resolveReferences(Record &R, const RecordVal *RV) const override;

Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off bit");
}

Init *getBit(unsigned B) const override {
assert(B < 1 && "Bit index out of range!");
return const_cast<VarBitInit*>(this);
Expand Down Expand Up @@ -1041,12 +981,6 @@ class VarListElementInit : public TypedInit {
TypedInit *getVariable() const { return TI; }
unsigned getElementNum() const { return Element; }

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override;

std::string getAsString() const override;
Init *resolveReferences(Record &R, const RecordVal *RV) const override;

Expand Down Expand Up @@ -1086,14 +1020,6 @@ class DefInit : public TypedInit {
Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off def");
}

/// This method is used to implement
/// VarListElementInit::resolveReferences. If the list element is resolvable
/// now, we return the resolved value, otherwise we return null.
Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off def");
}
};

/// X.Y - Represent a reference to a subfield of a variable
Expand All @@ -1118,9 +1044,6 @@ class FieldInit : public TypedInit {

Init *getBit(unsigned Bit) const override;

Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override;

Init *resolveReferences(Record &R, const RecordVal *RV) const override;

std::string getAsString() const override {
Expand Down Expand Up @@ -1218,11 +1141,6 @@ class DagInit final : public TypedInit, public FoldingSetNode,
Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off dag");
}

Init *resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const override {
llvm_unreachable("Illegal element reference off dag");
}
};

//===----------------------------------------------------------------------===//
Expand Down
97 changes: 9 additions & 88 deletions llvm/lib/TableGen/Record.cpp
Expand Up @@ -564,19 +564,6 @@ Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
return const_cast<ListInit *>(this);
}

Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
unsigned Elt) const {
if (Elt >= size())
return nullptr; // Out of range reference.
Init *E = getElement(Elt);
// If the element is set to some value, or if we are resolving a reference
// to a specific variable and that variable is explicitly unset, then
// replace the VarListElementInit with it.
if (IRV || !isa<UnsetInit>(E))
return E;
return nullptr;
}

std::string ListInit::getAsString() const {
std::string Result = "[";
const char *sep = "";
Expand All @@ -588,24 +575,6 @@ std::string ListInit::getAsString() const {
return Result + "]";
}

Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
unsigned Elt) const {
Init *Resolved = resolveReferences(R, IRV);
OpInit *OResolved = dyn_cast<OpInit>(Resolved);
if (OResolved) {
Resolved = OResolved->Fold(&R, nullptr);
}

if (Resolved != this) {
TypedInit *Typed = cast<TypedInit>(Resolved);
if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
return New;
return VarListElementInit::get(Typed, Elt);
}

return nullptr;
}

Init *OpInit::getBit(unsigned Bit) const {
if (getType() == BitRecTy::get())
return const_cast<OpInit*>(this);
Expand Down Expand Up @@ -1279,29 +1248,6 @@ Init *VarInit::getBit(unsigned Bit) const {
return VarBitInit::get(const_cast<VarInit*>(this), Bit);
}

Init *VarInit::resolveListElementReference(Record &R,
const RecordVal *IRV,
unsigned Elt) const {
if (R.isTemplateArg(getNameInit())) return nullptr;
if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;

RecordVal *RV = R.getValue(getNameInit());
assert(RV && "Reference to a non-existent variable?");
ListInit *LI = dyn_cast<ListInit>(RV->getValue());
if (!LI)
return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);

if (Elt >= LI->size())
return nullptr; // Out of range reference.
Init *E = LI->getElement(Elt);
// If the element is set to some value, or if we are resolving a reference
// to a specific variable and that variable is explicitly unset, then
// replace the VarListElementInit with it.
if (IRV || !isa<UnsetInit>(E))
return E;
return nullptr;
}

RecTy *VarInit::getFieldType(StringInit *FieldName) const {
if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
Expand Down Expand Up @@ -1380,9 +1326,15 @@ std::string VarListElementInit::getAsString() const {

Init *
VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
if (Init *I = getVariable()->resolveListElementReference(R, RV,
getElementNum()))
return I;
Init *NewTI = TI->resolveReferences(R, RV);
if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
// Leave out-of-bounds array references as-is. This can happen without
// being an error, e.g. in the untaken "branch" of an !if expression.
if (getElementNum() < List->size())
return List->getElement(getElementNum());
}
if (NewTI != TI && isa<TypedInit>(NewTI))
return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
return const_cast<VarListElementInit *>(this);
}

Expand All @@ -1392,21 +1344,6 @@ Init *VarListElementInit::getBit(unsigned Bit) const {
return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
}

Init *VarListElementInit:: resolveListElementReference(Record &R,
const RecordVal *RV,
unsigned Elt) const {
if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
return Result2;
return VarListElementInit::get(TInit, Elt);
}
return Result;
}

return nullptr;
}

DefInit *DefInit::get(Record *R) {
return R->getDefInit();
}
Expand Down Expand Up @@ -1450,22 +1387,6 @@ Init *FieldInit::getBit(unsigned Bit) const {
return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
}

Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
unsigned Elt) const {
if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
if (Elt >= LI->size()) return nullptr;
Init *E = LI->getElement(Elt);

// If the element is set to some value, or if we are resolving a
// reference to a specific variable and that variable is explicitly
// unset, then replace the VarListElementInit with it.
if (RV || !isa<UnsetInit>(E))
return E;
}
return nullptr;
}

Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;

Expand Down

0 comments on commit 801403a

Please sign in to comment.