blob: d583e73b88e742f846a27b863f414c8f2eda16e3 [file] [log] [blame]
Jamie Madille53c98b2014-02-03 11:57:13 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// RewriteElseBlocks.cpp: Implementation for tree transform to change
7// all if-else blocks to if-if blocks.
8//
9
10#include "compiler/translator/RewriteElseBlocks.h"
11#include "compiler/translator/NodeSearch.h"
12#include "compiler/translator/SymbolTable.h"
13
14namespace sh
15{
16
Jamie Madill4836d222014-07-24 06:55:51 -040017namespace
18{
19
20class ElseBlockRewriter : public TIntermTraverser
21{
22 public:
23 ElseBlockRewriter();
24
25 protected:
Corentin Wallez2d58f7a2015-09-28 10:44:55 -070026 bool visitAggregate(Visit visit, TIntermAggregate *aggregate) override;
Jamie Madill4836d222014-07-24 06:55:51 -040027
28 private:
Jamie Madill4836d222014-07-24 06:55:51 -040029 const TType *mFunctionType;
30
31 TIntermNode *rewriteSelection(TIntermSelection *selection);
32};
33
Jamie Madill787fc032014-07-07 12:49:45 -040034ElseBlockRewriter::ElseBlockRewriter()
Olli Etuaho64f0be92015-06-03 17:38:34 +030035 : TIntermTraverser(true, false, true),
Jamie Madill787fc032014-07-07 12:49:45 -040036 mFunctionType(NULL)
37{}
38
Jamie Madille53c98b2014-02-03 11:57:13 -050039bool ElseBlockRewriter::visitAggregate(Visit visit, TIntermAggregate *node)
40{
41 switch (node->getOp())
42 {
43 case EOpSequence:
Jamie Madill787fc032014-07-07 12:49:45 -040044 if (visit == PostVisit)
Jamie Madille53c98b2014-02-03 11:57:13 -050045 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070046 for (size_t statementIndex = 0; statementIndex != node->getSequence()->size(); statementIndex++)
Jamie Madille53c98b2014-02-03 11:57:13 -050047 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070048 TIntermNode *statement = (*node->getSequence())[statementIndex];
Jamie Madille53c98b2014-02-03 11:57:13 -050049 TIntermSelection *selection = statement->getAsSelectionNode();
Olli Etuaho4d59f3c2015-05-28 17:33:53 +030050 if (selection && selection->getFalseBlock() != nullptr)
Jamie Madille53c98b2014-02-03 11:57:13 -050051 {
Jamie Madilled3eef12014-02-26 09:47:11 -050052 // Check for if / else if
53 TIntermSelection *elseIfBranch = selection->getFalseBlock()->getAsSelectionNode();
54 if (elseIfBranch)
55 {
56 selection->replaceChildNode(elseIfBranch, rewriteSelection(elseIfBranch));
57 delete elseIfBranch;
58 }
59
Zhenyao Moe40d1e92014-07-16 17:40:36 -070060 (*node->getSequence())[statementIndex] = rewriteSelection(selection);
Jamie Madille53c98b2014-02-03 11:57:13 -050061 delete selection;
62 }
63 }
64 }
65 break;
66
Jamie Madill787fc032014-07-07 12:49:45 -040067 case EOpFunction:
68 // Store the current function context (see comment below)
69 mFunctionType = ((visit == PreVisit) ? &node->getType() : NULL);
70 break;
71
Jamie Madille53c98b2014-02-03 11:57:13 -050072 default: break;
73 }
74
75 return true;
76}
77
78TIntermNode *ElseBlockRewriter::rewriteSelection(TIntermSelection *selection)
79{
Olli Etuaho4d59f3c2015-05-28 17:33:53 +030080 ASSERT(selection != nullptr);
Jamie Madille53c98b2014-02-03 11:57:13 -050081
Olli Etuaho4d59f3c2015-05-28 17:33:53 +030082 nextTemporaryIndex();
83
Jamie Madille53c98b2014-02-03 11:57:13 -050084 TIntermTyped *typedCondition = selection->getCondition()->getAsTyped();
Olli Etuaho4d59f3c2015-05-28 17:33:53 +030085 TIntermAggregate *storeCondition = createTempInitDeclaration(typedCondition);
Jamie Madill787fc032014-07-07 12:49:45 -040086
Olli Etuaho4d59f3c2015-05-28 17:33:53 +030087 TIntermSelection *falseBlock = nullptr;
88
89 TType boolType(EbtBool, EbpUndefined, EvqTemporary);
Jamie Madill4836d222014-07-24 06:55:51 -040090
91 if (selection->getFalseBlock())
Jamie Madill787fc032014-07-07 12:49:45 -040092 {
Olli Etuaho4d59f3c2015-05-28 17:33:53 +030093 TIntermAggregate *negatedElse = nullptr;
Jamie Madill4836d222014-07-24 06:55:51 -040094 // crbug.com/346463
95 // D3D generates error messages claiming a function has no return value, when rewriting
96 // an if-else clause that returns something non-void in a function. By appending dummy
97 // returns (that are unreachable) we can silence this compile error.
98 if (mFunctionType && mFunctionType->getBasicType() != EbtVoid)
99 {
100 TString typeString = mFunctionType->getStruct() ? mFunctionType->getStruct()->name() :
101 mFunctionType->getBasicString();
102 TString rawText = "return (" + typeString + ")0";
Olli Etuaho4d59f3c2015-05-28 17:33:53 +0300103 TIntermRaw *returnNode = new TIntermRaw(*mFunctionType, rawText);
104 negatedElse = new TIntermAggregate(EOpSequence);
105 negatedElse->getSequence()->push_back(returnNode);
Jamie Madill4836d222014-07-24 06:55:51 -0400106 }
107
Olli Etuaho4d59f3c2015-05-28 17:33:53 +0300108 TIntermSymbol *conditionSymbolElse = createTempSymbol(boolType);
Olli Etuahoa2234302016-08-31 12:05:39 +0300109 TIntermUnary *negatedCondition = new TIntermUnary(EOpLogicalNot, conditionSymbolElse);
Jamie Madill4836d222014-07-24 06:55:51 -0400110 falseBlock = new TIntermSelection(negatedCondition,
111 selection->getFalseBlock(), negatedElse);
Jamie Madill787fc032014-07-07 12:49:45 -0400112 }
113
Olli Etuaho4d59f3c2015-05-28 17:33:53 +0300114 TIntermSymbol *conditionSymbolSel = createTempSymbol(boolType);
115 TIntermSelection *newSelection = new TIntermSelection(conditionSymbolSel, selection->getTrueBlock(), falseBlock);
Jamie Madille53c98b2014-02-03 11:57:13 -0500116
117 TIntermAggregate *block = new TIntermAggregate(EOpSequence);
Olli Etuaho4d59f3c2015-05-28 17:33:53 +0300118 block->getSequence()->push_back(storeCondition);
Jamie Madill4836d222014-07-24 06:55:51 -0400119 block->getSequence()->push_back(newSelection);
Jamie Madille53c98b2014-02-03 11:57:13 -0500120
121 return block;
122}
123
Jamie Madill4836d222014-07-24 06:55:51 -0400124}
125
Olli Etuaho4d59f3c2015-05-28 17:33:53 +0300126void RewriteElseBlocks(TIntermNode *node, unsigned int *temporaryIndex)
Jamie Madille53c98b2014-02-03 11:57:13 -0500127{
128 ElseBlockRewriter rewriter;
Olli Etuaho4d59f3c2015-05-28 17:33:53 +0300129 rewriter.useTemporaryIndex(temporaryIndex);
Jamie Madille53c98b2014-02-03 11:57:13 -0500130 node->traverse(&rewriter);
131}
132
133}