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: ...@@ -697,10 +697,11 @@ public:
virtual int getMatrixCols() const { return matrixCols; } virtual int getMatrixCols() const { return matrixCols; }
virtual int getMatrixRows() const { return matrixRows; } 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 isVector() const { return vectorSize > 1; }
virtual bool isMatrix() const { return matrixCols ? true : false; } virtual bool isMatrix() const { return matrixCols ? true : false; }
virtual bool isArray() const { return arraySizes != 0; } 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 // Recursively check the structure for any arrays, needed for some error checks
virtual bool containsArray() const virtual bool containsArray() const
......
...@@ -391,6 +391,7 @@ public: ...@@ -391,6 +391,7 @@ public:
virtual bool isArray() const { return type.isArray(); } virtual bool isArray() const { return type.isArray(); }
virtual bool isVector() const { return type.isVector(); } virtual bool isVector() const { return type.isVector(); }
virtual bool isScalar() const { return type.isScalar(); } virtual bool isScalar() const { return type.isScalar(); }
virtual bool isStruct() const { return type.isStruct(); }
TString getCompleteString() const { return type.getCompleteString(); } TString getCompleteString() const { return type.getCompleteString(); }
protected: protected:
......
...@@ -753,4 +753,56 @@ TIntermTyped* TIntermediate::foldConstructor(TIntermAggregate* aggrNode) ...@@ -753,4 +753,56 @@ TIntermTyped* TIntermediate::foldConstructor(TIntermAggregate* aggrNode)
return addConstantUnion(unionArray, aggrNode->getType(), aggrNode->getLoc()); 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 } // end namespace glslang
...@@ -388,7 +388,7 @@ TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TInt ...@@ -388,7 +388,7 @@ TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TInt
// //
// If one's a structure, then no conversions. // If one's a structure, then no conversions.
// //
if (type.getStruct() || node->getType().getStruct()) if (type.isStruct() || node->isStruct())
return 0; return 0;
// //
......
...@@ -161,10 +161,6 @@ public: ...@@ -161,10 +161,6 @@ public:
void updateTypedDefaults(TSourceLoc, const TQualifier&, const TString* id); void updateTypedDefaults(TSourceLoc, const TQualifier&, const TString* id);
void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode); void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode);
TIntermNode* addSwitch(TSourceLoc, TIntermTyped* expression, TIntermAggregate* body); 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); void updateMaxArraySize(TSourceLoc, TIntermNode*, int index);
......
...@@ -120,7 +120,7 @@ void TType::buildMangledName(TString& mangledName) ...@@ -120,7 +120,7 @@ void TType::buildMangledName(TString& mangledName)
int TType::getStructSize() const int TType::getStructSize() const
{ {
if (!getStruct()) { if (! isStruct()) {
assert(false && "Not a struct"); assert(false && "Not a struct");
return 0; return 0;
} }
...@@ -255,7 +255,7 @@ TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf) ...@@ -255,7 +255,7 @@ TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
setExtensions(copyOf.numExtensions, copyOf.extensions); setExtensions(copyOf.numExtensions, copyOf.extensions);
if (! copyOf.unionArray.empty()) { if (! copyOf.unionArray.empty()) {
assert(!copyOf.type.getStruct()); assert(! copyOf.type.isStruct());
assert(copyOf.type.getObjectSize() == 1); assert(copyOf.type.getObjectSize() == 1);
TConstUnionArray newArray(1); TConstUnionArray newArray(1);
newArray[0] = copyOf.unionArray[0]; newArray[0] = copyOf.unionArray[0];
......
...@@ -62,6 +62,9 @@ public: ...@@ -62,6 +62,9 @@ public:
explicit TIntermediate(EShLanguage l, int v = 0, EProfile p = ENoProfile) : language(l), treeRoot(0), profile(p), version(v), explicit TIntermediate(EShLanguage l, int v = 0, EProfile p = ENoProfile) : language(l), treeRoot(0), profile(p), version(v),
numMains(0), numErrors(0), recursive(false), numMains(0), numErrors(0), recursive(false),
invocations(0), maxVertices(0), inputPrimitive(ElgNone), outputPrimitive(ElgNone), pixelCenterInteger(false), originUpperLeft(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; } void setVersion(int v) { version = v; }
int getVersion() const { return version; } int getVersion() const { return version; }
...@@ -88,8 +91,6 @@ public: ...@@ -88,8 +91,6 @@ public:
TIntermAggregate* makeAggregate(TIntermNode* node, TSourceLoc); TIntermAggregate* makeAggregate(TIntermNode* node, TSourceLoc);
TIntermTyped* setAggregateOperator(TIntermNode*, TOperator, const TType& type, TSourceLoc); TIntermTyped* setAggregateOperator(TIntermNode*, TOperator, const TType& type, TSourceLoc);
bool areAllChildConst(TIntermAggregate* aggrNode); bool areAllChildConst(TIntermAggregate* aggrNode);
TIntermTyped* fold(TIntermAggregate* aggrNode);
TIntermTyped* foldConstructor(TIntermAggregate* aggrNode);
TIntermNode* addSelection(TIntermTyped* cond, TIntermNodePair code, TSourceLoc); TIntermNode* addSelection(TIntermTyped* cond, TIntermNodePair code, TSourceLoc);
TIntermTyped* addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc); TIntermTyped* addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc);
TIntermTyped* addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc); TIntermTyped* addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc);
...@@ -101,7 +102,14 @@ public: ...@@ -101,7 +102,14 @@ public:
TIntermBranch* addBranch(TOperator, TSourceLoc); TIntermBranch* addBranch(TOperator, TSourceLoc);
TIntermBranch* addBranch(TOperator, TIntermTyped*, TSourceLoc); TIntermBranch* addBranch(TOperator, TIntermTyped*, TSourceLoc);
TIntermTyped* addSwizzle(TVectorFields&, 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 addSymbolLinkageNodes(TIntermAggregate*& linkage, EShLanguage, TSymbolTable&);
void addSymbolLinkageNode(TIntermAggregate*& linkage, TSymbolTable&, const TString&); void addSymbolLinkageNode(TIntermAggregate*& linkage, TSymbolTable&, const TString&);
void addSymbolLinkageNode(TIntermAggregate*& linkage, const TSymbol&); void addSymbolLinkageNode(TIntermAggregate*& linkage, const TSymbol&);
...@@ -147,9 +155,6 @@ public: ...@@ -147,9 +155,6 @@ public:
void addIoAccessed(const TString& name) { ioAccessed.insert(name); } void addIoAccessed(const TString& name) { ioAccessed.insert(name); }
bool inIoAccessed(const TString& name) const { return ioAccessed.find(name) != ioAccessed.end(); } bool inIoAccessed(const TString& name) const { return ioAccessed.find(name) != ioAccessed.end(); }
void outputTree(TInfoSink&);
void removeTree();
protected: protected:
void error(TInfoSink& infoSink, const char*); void error(TInfoSink& infoSink, const char*);
void mergeBodies(TInfoSink&, TIntermSequence& globals, const TIntermSequence& unitGlobals); 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