Commit eb7f90fd by Olli Etuaho Committed by Commit Bot

Remove simple Intermediate.h functions

Most of the functions were just simple wrappers around node constructors. Dropping this extra redirection makes the code simpler. The fold() functions of node types are simplified, so that if the node can't be folded the pointer to the node itself is returned. This makes the code in ParseContext more straightforward. The few remaining functions in Intermediate are a bit more complex so they should be handled separately, but they'll be removed eventually as well. BUG=angleproject:1490 TEST=angle_unittests Change-Id: I85e11919d1f62358cfba9c011b841e32bc25402f Reviewed-on: https://chromium-review.googlesource.com/563393Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent 70c95fa6
...@@ -1043,6 +1043,24 @@ TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond, ...@@ -1043,6 +1043,24 @@ TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
return EvqTemporary; return EvqTemporary;
} }
TIntermTyped *TIntermTernary::fold()
{
if (mCondition->getAsConstantUnion())
{
if (mCondition->getAsConstantUnion()->getBConst(0))
{
mTrueExpression->getTypePointer()->setQualifier(mType.getQualifier());
return mTrueExpression;
}
else
{
mFalseExpression->getTypePointer()->setQualifier(mType.getQualifier());
return mFalseExpression;
}
}
return this;
}
void TIntermSwizzle::promote() void TIntermSwizzle::promote()
{ {
TQualifier resultQualifier = EvqTemporary; TQualifier resultQualifier = EvqTemporary;
...@@ -1116,7 +1134,8 @@ void TIntermBinary::promote() ...@@ -1116,7 +1134,8 @@ void TIntermBinary::promote()
ASSERT(!isMultiplication() || ASSERT(!isMultiplication() ||
mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType())); mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
// Comma is handled as a special case. // Comma is handled as a special case. Note that the comma node qualifier depends on the shader
// version and so is not being set here.
if (mOp == EOpComma) if (mOp == EOpComma)
{ {
setType(mRight->getType()); setType(mRight->getType());
...@@ -1351,7 +1370,7 @@ TIntermTyped *TIntermSwizzle::fold() ...@@ -1351,7 +1370,7 @@ TIntermTyped *TIntermSwizzle::fold()
TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion(); TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
if (operandConstant == nullptr) if (operandConstant == nullptr)
{ {
return nullptr; return this;
} }
TConstantUnion *constArray = new TConstantUnion[mSwizzleOffsets.size()]; TConstantUnion *constArray = new TConstantUnion[mSwizzleOffsets.size()];
...@@ -1368,22 +1387,35 @@ TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics) ...@@ -1368,22 +1387,35 @@ TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion(); TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
switch (mOp) switch (mOp)
{ {
case EOpComma:
{
if (mLeft->hasSideEffects())
{
return this;
}
mRight->getTypePointer()->setQualifier(mType.getQualifier());
return mRight;
}
case EOpIndexDirect: case EOpIndexDirect:
{ {
if (leftConstant == nullptr || rightConstant == nullptr) if (leftConstant == nullptr || rightConstant == nullptr)
{ {
return nullptr; return this;
} }
int index = rightConstant->getIConst(0); int index = rightConstant->getIConst(0);
const TConstantUnion *constArray = leftConstant->foldIndexing(index); const TConstantUnion *constArray = leftConstant->foldIndexing(index);
if (!constArray)
{
return this;
}
return CreateFoldedNode(constArray, this, mType.getQualifier()); return CreateFoldedNode(constArray, this, mType.getQualifier());
} }
case EOpIndexDirectStruct: case EOpIndexDirectStruct:
{ {
if (leftConstant == nullptr || rightConstant == nullptr) if (leftConstant == nullptr || rightConstant == nullptr)
{ {
return nullptr; return this;
} }
const TFieldList &fields = mLeft->getType().getStruct()->fields(); const TFieldList &fields = mLeft->getType().getStruct()->fields();
size_t index = static_cast<size_t>(rightConstant->getIConst(0)); size_t index = static_cast<size_t>(rightConstant->getIConst(0));
...@@ -1400,15 +1432,19 @@ TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics) ...@@ -1400,15 +1432,19 @@ TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
case EOpIndexIndirect: case EOpIndexIndirect:
case EOpIndexDirectInterfaceBlock: case EOpIndexDirectInterfaceBlock:
// Can never be constant folded. // Can never be constant folded.
return nullptr; return this;
default: default:
{ {
if (leftConstant == nullptr || rightConstant == nullptr) if (leftConstant == nullptr || rightConstant == nullptr)
{ {
return nullptr; return this;
} }
TConstantUnion *constArray = TConstantUnion *constArray =
leftConstant->foldBinary(mOp, rightConstant, diagnostics, mLeft->getLine()); leftConstant->foldBinary(mOp, rightConstant, diagnostics, mLeft->getLine());
if (!constArray)
{
return this;
}
// Nodes may be constant folded without being qualified as constant. // Nodes may be constant folded without being qualified as constant.
return CreateFoldedNode(constArray, this, mType.getQualifier()); return CreateFoldedNode(constArray, this, mType.getQualifier());
...@@ -1421,7 +1457,7 @@ TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics) ...@@ -1421,7 +1457,7 @@ TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion(); TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
if (operandConstant == nullptr) if (operandConstant == nullptr)
{ {
return nullptr; return this;
} }
TConstantUnion *constArray = nullptr; TConstantUnion *constArray = nullptr;
...@@ -1449,6 +1485,10 @@ TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics) ...@@ -1449,6 +1485,10 @@ TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics); constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
break; break;
} }
if (constArray == nullptr)
{
return this;
}
// Nodes may be constant folded without being qualified as constant. // Nodes may be constant folded without being qualified as constant.
return CreateFoldedNode(constArray, this, mType.getQualifier()); return CreateFoldedNode(constArray, this, mType.getQualifier());
...@@ -1461,7 +1501,7 @@ TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics) ...@@ -1461,7 +1501,7 @@ TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
{ {
if (param->getAsConstantUnion() == nullptr) if (param->getAsConstantUnion() == nullptr)
{ {
return nullptr; return this;
} }
} }
TConstantUnion *constArray = nullptr; TConstantUnion *constArray = nullptr;
...@@ -2617,6 +2657,42 @@ TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate ...@@ -2617,6 +2657,42 @@ TConstantUnion *TIntermConstantUnion::FoldAggregateConstructor(TIntermAggregate
return resultArray; return resultArray;
} }
bool TIntermAggregate::CanFoldAggregateBuiltInOp(TOperator op)
{
switch (op)
{
case EOpAtan:
case EOpPow:
case EOpMod:
case EOpMin:
case EOpMax:
case EOpClamp:
case EOpMix:
case EOpStep:
case EOpSmoothStep:
case EOpLdexp:
case EOpMulMatrixComponentWise:
case EOpOuterProduct:
case EOpEqualComponentWise:
case EOpNotEqualComponentWise:
case EOpLessThanComponentWise:
case EOpLessThanEqualComponentWise:
case EOpGreaterThanComponentWise:
case EOpGreaterThanEqualComponentWise:
case EOpDistance:
case EOpDot:
case EOpCross:
case EOpFaceforward:
case EOpReflect:
case EOpRefract:
case EOpBitfieldExtract:
case EOpBitfieldInsert:
return true;
default:
return false;
}
}
// static // static
TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate, TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
TDiagnostics *diagnostics) TDiagnostics *diagnostics)
......
...@@ -630,6 +630,7 @@ class TIntermAggregate : public TIntermOperator, public TIntermAggregateBase ...@@ -630,6 +630,7 @@ class TIntermAggregate : public TIntermOperator, public TIntermAggregateBase
bool hasSideEffects() const override; bool hasSideEffects() const override;
static bool CanFoldAggregateBuiltInOp(TOperator op);
TIntermTyped *fold(TDiagnostics *diagnostics); TIntermTyped *fold(TDiagnostics *diagnostics);
TIntermSequence *getSequence() override { return &mArguments; } TIntermSequence *getSequence() override { return &mArguments; }
...@@ -831,13 +832,15 @@ class TIntermTernary : public TIntermTyped ...@@ -831,13 +832,15 @@ class TIntermTernary : public TIntermTyped
mFalseExpression->hasSideEffects(); mFalseExpression->hasSideEffects();
} }
static TQualifier DetermineQualifier(TIntermTyped *cond, TIntermTyped *fold();
TIntermTyped *trueExpression,
TIntermTyped *falseExpression);
private: private:
TIntermTernary(const TIntermTernary &node); // Note: not deleted, just private! TIntermTernary(const TIntermTernary &node); // Note: not deleted, just private!
static TQualifier DetermineQualifier(TIntermTyped *cond,
TIntermTyped *trueExpression,
TIntermTyped *falseExpression);
TIntermTyped *mCondition; TIntermTyped *mCondition;
TIntermTyped *mTrueExpression; TIntermTyped *mTrueExpression;
TIntermTyped *mFalseExpression; TIntermTyped *mFalseExpression;
......
...@@ -26,47 +26,6 @@ namespace sh ...@@ -26,47 +26,6 @@ namespace sh
// //
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
//
// Add a terminal node for an identifier in an expression.
//
// Returns the added node.
//
TIntermSymbol *TIntermediate::addSymbol(int id,
const TString &name,
const TType &type,
const TSourceLoc &line)
{
TIntermSymbol *node = new TIntermSymbol(id, name, type);
node->setLine(line);
return node;
}
//
// Connect two nodes through an index operator, where the left node is the base
// of an array or struct, and the right node is a direct or indirect offset.
//
// Returns the added node.
// The caller should set the type of the returned node.
//
TIntermTyped *TIntermediate::addIndex(TOperator op,
TIntermTyped *base,
TIntermTyped *index,
const TSourceLoc &line,
TDiagnostics *diagnostics)
{
TIntermBinary *node = new TIntermBinary(op, base, index);
node->setLine(line);
TIntermTyped *folded = node->fold(diagnostics);
if (folded)
{
return folded;
}
return node;
}
// If the input node is nullptr, return nullptr. // If the input node is nullptr, return nullptr.
// If the input node is a block node, return it. // If the input node is a block node, return it.
// If the input node is not a block node, put it inside a block node and return that. // If the input node is not a block node, put it inside a block node and return that.
...@@ -84,80 +43,6 @@ TIntermBlock *TIntermediate::EnsureBlock(TIntermNode *node) ...@@ -84,80 +43,6 @@ TIntermBlock *TIntermediate::EnsureBlock(TIntermNode *node)
return blockNode; return blockNode;
} }
TIntermTyped *TIntermediate::AddComma(TIntermTyped *left,
TIntermTyped *right,
const TSourceLoc &line,
int shaderVersion)
{
TIntermTyped *commaNode = nullptr;
if (!left->hasSideEffects())
{
commaNode = right;
}
else
{
commaNode = new TIntermBinary(EOpComma, left, right);
commaNode->setLine(line);
}
TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(shaderVersion, left, right);
commaNode->getTypePointer()->setQualifier(resultQualifier);
return commaNode;
}
// For "?:" test nodes. There are three children; a condition,
// a true path, and a false path. The two paths are specified
// as separate parameters.
//
// Returns the ternary node created, or one of trueExpression and falseExpression if the expression
// could be folded.
TIntermTyped *TIntermediate::AddTernarySelection(TIntermTyped *cond,
TIntermTyped *trueExpression,
TIntermTyped *falseExpression,
const TSourceLoc &line)
{
// Note that the node resulting from here can be a constant union without being qualified as
// constant.
if (cond->getAsConstantUnion())
{
TQualifier resultQualifier =
TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression);
if (cond->getAsConstantUnion()->getBConst(0))
{
trueExpression->getTypePointer()->setQualifier(resultQualifier);
return trueExpression;
}
else
{
falseExpression->getTypePointer()->setQualifier(resultQualifier);
return falseExpression;
}
}
// Make a ternary node.
TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
node->setLine(line);
return node;
}
TIntermSwitch *TIntermediate::addSwitch(TIntermTyped *init,
TIntermBlock *statementList,
const TSourceLoc &line)
{
TIntermSwitch *node = new TIntermSwitch(init, statementList);
node->setLine(line);
return node;
}
TIntermCase *TIntermediate::addCase(TIntermTyped *condition, const TSourceLoc &line)
{
TIntermCase *node = new TIntermCase(condition);
node->setLine(line);
return node;
}
// //
// Constant terminal nodes. Has a union that contains bool, float or int constants // Constant terminal nodes. Has a union that contains bool, float or int constants
// //
...@@ -174,68 +59,4 @@ TIntermConstantUnion *TIntermediate::addConstantUnion(const TConstantUnion *cons ...@@ -174,68 +59,4 @@ TIntermConstantUnion *TIntermediate::addConstantUnion(const TConstantUnion *cons
return node; return node;
} }
TIntermTyped *TIntermediate::AddSwizzle(TIntermTyped *baseExpression,
const TVectorFields &fields,
const TSourceLoc &dotLocation)
{
TVector<int> fieldsVector;
for (int i = 0; i < fields.num; ++i)
{
fieldsVector.push_back(fields.offsets[i]);
}
TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldsVector);
node->setLine(dotLocation);
TIntermTyped *folded = node->fold();
if (folded)
{
return folded;
}
return node;
}
TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate,
TDiagnostics *diagnostics)
{
switch (aggregate->getOp())
{
case EOpAtan:
case EOpPow:
case EOpMod:
case EOpMin:
case EOpMax:
case EOpClamp:
case EOpMix:
case EOpStep:
case EOpSmoothStep:
case EOpLdexp:
case EOpMulMatrixComponentWise:
case EOpOuterProduct:
case EOpEqualComponentWise:
case EOpNotEqualComponentWise:
case EOpLessThanComponentWise:
case EOpLessThanEqualComponentWise:
case EOpGreaterThanComponentWise:
case EOpGreaterThanEqualComponentWise:
case EOpDistance:
case EOpDot:
case EOpCross:
case EOpFaceforward:
case EOpReflect:
case EOpRefract:
case EOpBitfieldExtract:
case EOpBitfieldInsert:
return aggregate->fold(diagnostics);
default:
// TODO: Add support for folding array constructors
if (aggregate->isConstructor() && !aggregate->isArray())
{
return aggregate->fold(diagnostics);
}
// Constant folding not supported for the built-in.
return nullptr;
}
}
} // namespace sh } // namespace sh
...@@ -12,12 +12,6 @@ ...@@ -12,12 +12,6 @@
namespace sh namespace sh
{ {
struct TVectorFields
{
int offsets[4];
int num;
};
// //
// Set of helper functions to help build the tree. // Set of helper functions to help build the tree.
// //
...@@ -27,35 +21,11 @@ class TIntermediate ...@@ -27,35 +21,11 @@ class TIntermediate
POOL_ALLOCATOR_NEW_DELETE(); POOL_ALLOCATOR_NEW_DELETE();
TIntermediate() {} TIntermediate() {}
TIntermSymbol *addSymbol(int id, const TString &, const TType &, const TSourceLoc &);
TIntermTyped *addIndex(TOperator op,
TIntermTyped *base,
TIntermTyped *index,
const TSourceLoc &line,
TDiagnostics *diagnostics);
static TIntermBlock *EnsureBlock(TIntermNode *node); static TIntermBlock *EnsureBlock(TIntermNode *node);
static TIntermTyped *AddTernarySelection(TIntermTyped *cond,
TIntermTyped *trueExpression,
TIntermTyped *falseExpression,
const TSourceLoc &line);
TIntermSwitch *addSwitch(TIntermTyped *init,
TIntermBlock *statementList,
const TSourceLoc &line);
TIntermCase *addCase(TIntermTyped *condition, const TSourceLoc &line);
static TIntermTyped *AddComma(TIntermTyped *left,
TIntermTyped *right,
const TSourceLoc &line,
int shaderVersion);
TIntermConstantUnion *addConstantUnion(const TConstantUnion *constantUnion, TIntermConstantUnion *addConstantUnion(const TConstantUnion *constantUnion,
const TType &type, const TType &type,
const TSourceLoc &line); const TSourceLoc &line);
static TIntermTyped *AddSwizzle(TIntermTyped *baseExpression,
const TVectorFields &fields,
const TSourceLoc &dotLocation);
TIntermTyped *foldAggregateBuiltIn(TIntermAggregate *aggregate, TDiagnostics *diagnostics);
private: private:
void operator=(TIntermediate &); // prevent assignments void operator=(TIntermediate &); // prevent assignments
}; };
......
...@@ -98,7 +98,11 @@ class TParseContext : angle::NonCopyable ...@@ -98,7 +98,11 @@ class TParseContext : angle::NonCopyable
const TString *name, const TString *name,
const TSymbol *symbol); const TSymbol *symbol);
bool parseVectorFields(const TString &, int vecSize, TVectorFields &, const TSourceLoc &line); // Look at a '.' field selector string and change it into offsets for a vector.
bool parseVectorFields(const TSourceLoc &line,
const TString &compString,
int vecSize,
TVector<int> *fieldOffsets);
void assignError(const TSourceLoc &line, const char *op, TString left, TString right); void assignError(const TSourceLoc &line, const char *op, TString left, TString right);
void unaryOpError(const TSourceLoc &line, const char *op, TString operand); void unaryOpError(const TSourceLoc &line, const char *op, TString operand);
......
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