[AVX] Make Inits Foldable
Manage Inits in a FoldingSet. This provides several benefits:
- Memory for Inits is properly managed
- Duplicate Inits are folded into Flyweights, saving memory
- It enforces const-correctness, protecting against certain classes
of bugs
The above benefits allow Inits to be used in more contexts, which in
turn provides more dynamism to TableGen. This enhanced capability
will be used by the AVX code generator to a fold common patterns
together.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134907 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/TableGen/SetTheory.cpp b/utils/TableGen/SetTheory.cpp
index 21ac09c..a888bbc 100644
--- a/utils/TableGen/SetTheory.cpp
+++ b/utils/TableGen/SetTheory.cpp
@@ -27,14 +27,14 @@
// (add a, b, ...) Evaluate and union all arguments.
struct AddOp : public SetTheory::Operator {
- void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
+ void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts) {
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts);
}
};
// (sub Add, Sub, ...) Set difference.
struct SubOp : public SetTheory::Operator {
- void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
+ void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts) {
if (Expr->arg_size() < 2)
throw "Set difference needs at least two arguments: " +
Expr->getAsString();
@@ -49,7 +49,7 @@
// (and S1, S2) Set intersection.
struct AndOp : public SetTheory::Operator {
- void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
+ void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts) {
if (Expr->arg_size() != 2)
throw "Set intersection requires two arguments: " + Expr->getAsString();
RecSet S1, S2;
@@ -63,16 +63,16 @@
// SetIntBinOp - Abstract base class for (Op S, N) operators.
struct SetIntBinOp : public SetTheory::Operator {
- virtual void apply2(SetTheory &ST, DagInit *Expr,
+ virtual void apply2(SetTheory &ST, const DagInit *Expr,
RecSet &Set, int64_t N,
RecSet &Elts) =0;
- void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
+ void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts) {
if (Expr->arg_size() != 2)
throw "Operator requires (Op Set, Int) arguments: " + Expr->getAsString();
RecSet Set;
ST.evaluate(Expr->arg_begin()[0], Set);
- IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]);
+ const IntInit *II = dynamic_cast<const IntInit*>(Expr->arg_begin()[1]);
if (!II)
throw "Second argument must be an integer: " + Expr->getAsString();
apply2(ST, Expr, Set, II->getValue(), Elts);
@@ -81,7 +81,7 @@
// (shl S, N) Shift left, remove the first N elements.
struct ShlOp : public SetIntBinOp {
- void apply2(SetTheory &ST, DagInit *Expr,
+ void apply2(SetTheory &ST, const DagInit *Expr,
RecSet &Set, int64_t N,
RecSet &Elts) {
if (N < 0)
@@ -93,7 +93,7 @@
// (trunc S, N) Truncate after the first N elements.
struct TruncOp : public SetIntBinOp {
- void apply2(SetTheory &ST, DagInit *Expr,
+ void apply2(SetTheory &ST, const DagInit *Expr,
RecSet &Set, int64_t N,
RecSet &Elts) {
if (N < 0)
@@ -110,7 +110,7 @@
RotOp(bool Rev) : Reverse(Rev) {}
- void apply2(SetTheory &ST, DagInit *Expr,
+ void apply2(SetTheory &ST, const DagInit *Expr,
RecSet &Set, int64_t N,
RecSet &Elts) {
if (Reverse)
@@ -129,7 +129,7 @@
// (decimate S, N) Pick every N'th element of S.
struct DecimateOp : public SetIntBinOp {
- void apply2(SetTheory &ST, DagInit *Expr,
+ void apply2(SetTheory &ST, const DagInit *Expr,
RecSet &Set, int64_t N,
RecSet &Elts) {
if (N <= 0)
@@ -141,25 +141,26 @@
// (sequence "Format", From, To) Generate a sequence of records by name.
struct SequenceOp : public SetTheory::Operator {
- void apply(SetTheory &ST, DagInit *Expr, RecSet &Elts) {
+ void apply(SetTheory &ST, const DagInit *Expr, RecSet &Elts) {
if (Expr->arg_size() != 3)
throw "Bad args to (sequence \"Format\", From, To): " +
Expr->getAsString();
std::string Format;
- if (StringInit *SI = dynamic_cast<StringInit*>(Expr->arg_begin()[0]))
+ if (const StringInit *SI =
+ dynamic_cast<const StringInit*>(Expr->arg_begin()[0]))
Format = SI->getValue();
else
throw "Format must be a string: " + Expr->getAsString();
int64_t From, To;
- if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[1]))
+ if (const IntInit *II = dynamic_cast<const IntInit*>(Expr->arg_begin()[1]))
From = II->getValue();
else
throw "From must be an integer: " + Expr->getAsString();
if (From < 0 || From >= (1 << 30))
throw "From out of range";
- if (IntInit *II = dynamic_cast<IntInit*>(Expr->arg_begin()[2]))
+ if (const IntInit *II = dynamic_cast<const IntInit*>(Expr->arg_begin()[2]))
To = II->getValue();
else
throw "From must be an integer: " + Expr->getAsString();
@@ -167,7 +168,7 @@
throw "To out of range";
RecordKeeper &Records =
- dynamic_cast<DefInit&>(*Expr->getOperator()).getDef()->getRecords();
+ dynamic_cast<const DefInit&>(*Expr->getOperator()).getDef()->getRecords();
int Step = From <= To ? 1 : -1;
for (To += Step; From != To; From += Step) {
@@ -222,9 +223,9 @@
addExpander(ClassName, new FieldExpander(FieldName));
}
-void SetTheory::evaluate(Init *Expr, RecSet &Elts) {
+void SetTheory::evaluate(const Init *Expr, RecSet &Elts) {
// A def in a list can be a just an element, or it may expand.
- if (DefInit *Def = dynamic_cast<DefInit*>(Expr)) {
+ if (const DefInit *Def = dynamic_cast<const DefInit*>(Expr)) {
if (const RecVec *Result = expand(Def->getDef()))
return Elts.insert(Result->begin(), Result->end());
Elts.insert(Def->getDef());
@@ -232,14 +233,14 @@
}
// Lists simply expand.
- if (ListInit *LI = dynamic_cast<ListInit*>(Expr))
+ if (const ListInit *LI = dynamic_cast<const ListInit*>(Expr))
return evaluate(LI->begin(), LI->end(), Elts);
// Anything else must be a DAG.
- DagInit *DagExpr = dynamic_cast<DagInit*>(Expr);
+ const DagInit *DagExpr = dynamic_cast<const DagInit*>(Expr);
if (!DagExpr)
throw "Invalid set element: " + Expr->getAsString();
- DefInit *OpInit = dynamic_cast<DefInit*>(DagExpr->getOperator());
+ const DefInit *OpInit = dynamic_cast<const DefInit*>(DagExpr->getOperator());
if (!OpInit)
throw "Bad set expression: " + Expr->getAsString();
Operator *Op = Operators.lookup(OpInit->getDef()->getName());