Edit

kc3-lang/angle/src/compiler/translator/UnfoldShortCircuitToIf.cpp

Branch :

  • Show log

    Commit

  • Author : Olli Etuaho
    Date : 2016-07-14 11:55:48
    Hash : 3cbb27a1
    Message : Simplify loop conditions so that they won't generate statements Introduce an AST traverser that can move the evaluation of certain types of loop conditions and loop expressions inside the loop. This way subsequent AST transformations don't have to worry about cases where they have to insert new statements to implement a loop condition or expression. This includes the revert of "Unfold short-circuiting operators in loop conditions correctly". The new traverser covers the loop cases that used to be handled in UnfoldShortCircuitToIf. BUG=angleproject:1465 TEST=WebGL conformance tests, dEQP-GLES2.functional.shaders.*select_iteration_count* Change-Id: I88e50e007e924d5884a217117690ac7fa2f96d38 Reviewed-on: https://chromium-review.googlesource.com/362570 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>

  • src/compiler/translator/UnfoldShortCircuitToIf.cpp
  • //
    // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    //
    // UnfoldShortCircuitToIf is an AST traverser to convert short-circuiting operators to if-else statements.
    // The results are assigned to s# temporaries, which are used by the main translator instead of
    // the original expression.
    //
    
    #include "compiler/translator/UnfoldShortCircuitToIf.h"
    
    #include "compiler/translator/IntermNode.h"
    #include "compiler/translator/IntermNodePatternMatcher.h"
    
    namespace
    {
    
    // Traverser that unfolds one short-circuiting operation at a time.
    class UnfoldShortCircuitTraverser : public TIntermTraverser
    {
      public:
        UnfoldShortCircuitTraverser();
    
        bool visitBinary(Visit visit, TIntermBinary *node) override;
        bool visitSelection(Visit visit, TIntermSelection *node) override;
    
        void nextIteration();
        bool foundShortCircuit() const { return mFoundShortCircuit; }
    
      protected:
        // Marked to true once an operation that needs to be unfolded has been found.
        // After that, no more unfolding is performed on that traversal.
        bool mFoundShortCircuit;
    
        IntermNodePatternMatcher mPatternToUnfoldMatcher;
    };
    
    UnfoldShortCircuitTraverser::UnfoldShortCircuitTraverser()
        : TIntermTraverser(true, false, true),
          mFoundShortCircuit(false),
          mPatternToUnfoldMatcher(IntermNodePatternMatcher::kUnfoldedShortCircuitExpression)
    {
    }
    
    bool UnfoldShortCircuitTraverser::visitBinary(Visit visit, TIntermBinary *node)
    {
        if (mFoundShortCircuit)
            return false;
    
        if (visit != PreVisit)
            return true;
    
        if (!mPatternToUnfoldMatcher.match(node, getParentNode()))
            return true;
    
        // If our right node doesn't have side effects, we know we don't need to unfold this
        // expression: there will be no short-circuiting side effects to avoid
        // (note: unfolding doesn't depend on the left node -- it will always be evaluated)
        ASSERT(node->getRight()->hasSideEffects());
    
        mFoundShortCircuit = true;
    
        switch (node->getOp())
        {
          case EOpLogicalOr:
          {
              // "x || y" is equivalent to "x ? true : y", which unfolds to "bool s; if(x) s = true;
              // else s = y;",
              // and then further simplifies down to "bool s = x; if(!s) s = y;".
    
              TIntermSequence insertions;
              TType boolType(EbtBool, EbpUndefined, EvqTemporary);
    
              ASSERT(node->getLeft()->getType() == boolType);
              insertions.push_back(createTempInitDeclaration(node->getLeft()));
    
              TIntermAggregate *assignRightBlock = new TIntermAggregate(EOpSequence);
              ASSERT(node->getRight()->getType() == boolType);
              assignRightBlock->getSequence()->push_back(createTempAssignment(node->getRight()));
    
              TIntermUnary *notTempSymbol = new TIntermUnary(EOpLogicalNot, boolType);
              notTempSymbol->setOperand(createTempSymbol(boolType));
              TIntermSelection *ifNode = new TIntermSelection(notTempSymbol, assignRightBlock, nullptr);
              insertions.push_back(ifNode);
    
              insertStatementsInParentBlock(insertions);
    
              queueReplacement(node, createTempSymbol(boolType), OriginalNode::IS_DROPPED);
              return false;
          }
          case EOpLogicalAnd:
          {
              // "x && y" is equivalent to "x ? y : false", which unfolds to "bool s; if(x) s = y;
              // else s = false;",
              // and then further simplifies down to "bool s = x; if(s) s = y;".
              TIntermSequence insertions;
              TType boolType(EbtBool, EbpUndefined, EvqTemporary);
    
              ASSERT(node->getLeft()->getType() == boolType);
              insertions.push_back(createTempInitDeclaration(node->getLeft()));
    
              TIntermAggregate *assignRightBlock = new TIntermAggregate(EOpSequence);
              ASSERT(node->getRight()->getType() == boolType);
              assignRightBlock->getSequence()->push_back(createTempAssignment(node->getRight()));
    
              TIntermSelection *ifNode =
                  new TIntermSelection(createTempSymbol(boolType), assignRightBlock, nullptr);
              insertions.push_back(ifNode);
    
              insertStatementsInParentBlock(insertions);
    
              queueReplacement(node, createTempSymbol(boolType), OriginalNode::IS_DROPPED);
              return false;
          }
          default:
              UNREACHABLE();
              return true;
        }
    }
    
    bool UnfoldShortCircuitTraverser::visitSelection(Visit visit, TIntermSelection *node)
    {
        if (mFoundShortCircuit)
            return false;
    
        if (visit != PreVisit)
            return true;
    
        if (!mPatternToUnfoldMatcher.match(node))
            return true;
    
        mFoundShortCircuit = true;
    
        ASSERT(node->usesTernaryOperator());
    
        // Unfold "b ? x : y" into "type s; if(b) s = x; else s = y;"
        TIntermSequence insertions;
    
        TIntermSymbol *tempSymbol         = createTempSymbol(node->getType());
        TIntermAggregate *tempDeclaration = new TIntermAggregate(EOpDeclaration);
        tempDeclaration->getSequence()->push_back(tempSymbol);
        insertions.push_back(tempDeclaration);
    
        TIntermAggregate *trueBlock   = new TIntermAggregate(EOpSequence);
        TIntermBinary *trueAssignment = createTempAssignment(node->getTrueBlock()->getAsTyped());
        trueBlock->getSequence()->push_back(trueAssignment);
    
        TIntermAggregate *falseBlock   = new TIntermAggregate(EOpSequence);
        TIntermBinary *falseAssignment = createTempAssignment(node->getFalseBlock()->getAsTyped());
        falseBlock->getSequence()->push_back(falseAssignment);
    
        TIntermSelection *ifNode =
            new TIntermSelection(node->getCondition()->getAsTyped(), trueBlock, falseBlock);
        insertions.push_back(ifNode);
    
        insertStatementsInParentBlock(insertions);
    
        TIntermSymbol *ternaryResult = createTempSymbol(node->getType());
        queueReplacement(node, ternaryResult, OriginalNode::IS_DROPPED);
    
        return false;
    }
    
    void UnfoldShortCircuitTraverser::nextIteration()
    {
        mFoundShortCircuit = false;
        nextTemporaryIndex();
    }
    
    } // namespace
    
    void UnfoldShortCircuitToIf(TIntermNode *root, unsigned int *temporaryIndex)
    {
        UnfoldShortCircuitTraverser traverser;
        ASSERT(temporaryIndex != nullptr);
        traverser.useTemporaryIndex(temporaryIndex);
        // Unfold one operator at a time, and reset the traverser between iterations.
        do
        {
            traverser.nextIteration();
            root->traverse(&traverser);
            if (traverser.foundShortCircuit())
                traverser.updateTree();
        }
        while (traverser.foundShortCircuit());
    }