Commit 3591813a by John Kessenich

Simplify and rationalize constant folding for dereferences (array, matrix,…

Simplify and rationalize constant folding for dereferences (array, matrix, vector, swizzle, struct). git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@24259 e7fa87d3-cd2b-0410-9028-fcbf551c1848
parent 1fbaa35c
......@@ -697,10 +697,11 @@ public:
virtual int getMatrixCols() const { return matrixCols; }
virtual int getMatrixRows() const { return matrixRows; }
virtual bool isScalar() const { return vectorSize == 1 && ! getStruct() && ! isArray(); }
virtual bool isScalar() const { return vectorSize == 1 && ! isStruct() && ! isArray(); }
virtual bool isVector() const { return vectorSize > 1; }
virtual bool isMatrix() const { return matrixCols ? true : false; }
virtual bool isArray() const { return arraySizes != 0; }
virtual bool isStruct() const { return structure != 0; }
// Recursively check the structure for any arrays, needed for some error checks
virtual bool containsArray() const
......
......@@ -391,6 +391,7 @@ public:
virtual bool isArray() const { return type.isArray(); }
virtual bool isVector() const { return type.isVector(); }
virtual bool isScalar() const { return type.isScalar(); }
virtual bool isStruct() const { return type.isStruct(); }
TString getCompleteString() const { return type.getCompleteString(); }
protected:
......
......@@ -753,4 +753,56 @@ TIntermTyped* TIntermediate::foldConstructor(TIntermAggregate* aggrNode)
return addConstantUnion(unionArray, aggrNode->getType(), aggrNode->getLoc());
}
//
// Constant folding of a bracket (array-style) dereference or struct-like dot
// dereference. Can handle any thing except a multi-character swizzle, though
// all swizzles may go to foldSwizzle().
//
TIntermTyped* TIntermediate::foldDereference(TIntermTyped* node, int index, TSourceLoc loc)
{
TType dereferencedType(node->getType(), index);
dereferencedType.getQualifier().storage = EvqConst;
TIntermTyped* result = 0;
int size = dereferencedType.getObjectSize();
int start;
if (node->isStruct()) {
start = 0;
for (int i = 0; i < index; ++i)
start += (*node->getType().getStruct())[i].type->getObjectSize();
} else
start = size * index;
result = addConstantUnion(TConstUnionArray(node->getAsConstantUnion()->getConstArray(), start, size), node->getType(), loc);
if (result == 0)
result = node;
else
result->setType(dereferencedType);
return result;
}
//
// Make a constant vector node or constant scalar node, representing a given
// constant vector and constant swizzle into it.
//
TIntermTyped* TIntermediate::foldSwizzle(TIntermTyped* node, TVectorFields& fields, TSourceLoc loc)
{
const TConstUnionArray& unionArray = node->getAsConstantUnion()->getConstArray();
TConstUnionArray constArray(fields.num);
for (int i = 0; i < fields.num; i++)
constArray[i] = unionArray[fields.offsets[i]];
TIntermTyped* result = addConstantUnion(constArray, node->getType(), loc);
if (result == 0)
result = node;
else
result->setType(TType(node->getBasicType(), EvqConst, fields.num));
return result;
}
} // end namespace glslang
......@@ -388,7 +388,7 @@ TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TInt
//
// If one's a structure, then no conversions.
//
if (type.getStruct() || node->getType().getStruct())
if (type.isStruct() || node->isStruct())
return 0;
//
......
......@@ -161,10 +161,6 @@ public:
void updateTypedDefaults(TSourceLoc, const TQualifier&, const TString* id);
void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode);
TIntermNode* addSwitch(TSourceLoc, TIntermTyped* expression, TIntermAggregate* body);
TIntermTyped* addConstVectorNode(TSourceLoc, TVectorFields&, TIntermTyped*);
TIntermTyped* addConstMatrixNode(TSourceLoc, int index, TIntermTyped*);
TIntermTyped* addConstArrayNode(TSourceLoc, int index, TIntermTyped* node);
TIntermTyped* addConstStruct(TSourceLoc, TString& , TIntermTyped*);
void updateMaxArraySize(TSourceLoc, TIntermNode*, int index);
......
......@@ -120,7 +120,7 @@ void TType::buildMangledName(TString& mangledName)
int TType::getStructSize() const
{
if (!getStruct()) {
if (! isStruct()) {
assert(false && "Not a struct");
return 0;
}
......@@ -255,7 +255,7 @@ TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
setExtensions(copyOf.numExtensions, copyOf.extensions);
if (! copyOf.unionArray.empty()) {
assert(!copyOf.type.getStruct());
assert(! copyOf.type.isStruct());
assert(copyOf.type.getObjectSize() == 1);
TConstUnionArray newArray(1);
newArray[0] = copyOf.unionArray[0];
......
......@@ -62,6 +62,9 @@ public:
explicit TIntermediate(EShLanguage l, int v = 0, EProfile p = ENoProfile) : language(l), treeRoot(0), profile(p), version(v),
numMains(0), numErrors(0), recursive(false),
invocations(0), maxVertices(0), inputPrimitive(ElgNone), outputPrimitive(ElgNone), pixelCenterInteger(false), originUpperLeft(false) { }
bool postProcess(TIntermNode*, EShLanguage);
void outputTree(TInfoSink&);
void removeTree();
void setVersion(int v) { version = v; }
int getVersion() const { return version; }
......@@ -88,8 +91,6 @@ public:
TIntermAggregate* makeAggregate(TIntermNode* node, TSourceLoc);
TIntermTyped* setAggregateOperator(TIntermNode*, TOperator, const TType& type, TSourceLoc);
bool areAllChildConst(TIntermAggregate* aggrNode);
TIntermTyped* fold(TIntermAggregate* aggrNode);
TIntermTyped* foldConstructor(TIntermAggregate* aggrNode);
TIntermNode* addSelection(TIntermTyped* cond, TIntermNodePair code, TSourceLoc);
TIntermTyped* addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc);
TIntermTyped* addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc);
......@@ -101,7 +102,14 @@ public:
TIntermBranch* addBranch(TOperator, TSourceLoc);
TIntermBranch* addBranch(TOperator, TIntermTyped*, TSourceLoc);
TIntermTyped* addSwizzle(TVectorFields&, TSourceLoc);
bool postProcess(TIntermNode*, EShLanguage);
// Constant folding (in Constant.cpp)
TIntermTyped* fold(TIntermAggregate* aggrNode);
TIntermTyped* foldConstructor(TIntermAggregate* aggrNode);
TIntermTyped* foldDereference(TIntermTyped* node, int index, TSourceLoc);
TIntermTyped* foldSwizzle(TIntermTyped* node, TVectorFields& fields, TSourceLoc);
// Linkage related
void addSymbolLinkageNodes(TIntermAggregate*& linkage, EShLanguage, TSymbolTable&);
void addSymbolLinkageNode(TIntermAggregate*& linkage, TSymbolTable&, const TString&);
void addSymbolLinkageNode(TIntermAggregate*& linkage, const TSymbol&);
......@@ -147,9 +155,6 @@ public:
void addIoAccessed(const TString& name) { ioAccessed.insert(name); }
bool inIoAccessed(const TString& name) const { return ioAccessed.find(name) != ioAccessed.end(); }
void outputTree(TInfoSink&);
void removeTree();
protected:
void error(TInfoSink& infoSink, const char*);
void mergeBodies(TInfoSink&, TIntermSequence& globals, const TIntermSequence& unitGlobals);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment