2 * Copyright 2021 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/sksl/SkSLAnalysis.h"
9 #include "src/sksl/SkSLConstantFolder.h"
10 #include "src/sksl/SkSLContext.h"
11 #include "src/sksl/SkSLProgramSettings.h"
12 #include "src/sksl/ir/SkSLExpressionStatement.h"
13 #include "src/sksl/ir/SkSLIfStatement.h"
14 #include "src/sksl/ir/SkSLLiteral.h"
15 #include "src/sksl/ir/SkSLNop.h"
16 #include "src/sksl/ir/SkSLType.h"
20 std::unique_ptr<Statement> IfStatement::clone() const {
21 return std::make_unique<IfStatement>(fPosition, this->isStatic(), this->test()->clone(),
22 this->ifTrue()->clone(),
23 this->ifFalse() ? this->ifFalse()->clone() : nullptr);
26 std::string IfStatement::description() const {
28 if (this->isStatic()) {
31 result += "if (" + this->test()->description() + ") " + this->ifTrue()->description();
32 if (this->ifFalse()) {
33 result += " else " + this->ifFalse()->description();
38 std::unique_ptr<Statement> IfStatement::Convert(const Context& context, Position pos,
39 bool isStatic, std::unique_ptr<Expression> test, std::unique_ptr<Statement> ifTrue,
40 std::unique_ptr<Statement> ifFalse) {
41 test = context.fTypes.fBool->coerceExpression(std::move(test), context);
46 if (Analysis::DetectVarDeclarationWithoutScope(*ifTrue, context.fErrors)) {
49 if (ifFalse && Analysis::DetectVarDeclarationWithoutScope(*ifFalse, context.fErrors)) {
52 return IfStatement::Make(context, pos, isStatic, std::move(test),
53 std::move(ifTrue), std::move(ifFalse));
56 static std::unique_ptr<Statement> replace_empty_with_nop(std::unique_ptr<Statement> stmt,
58 return (stmt && (!isEmpty || stmt->is<Nop>())) ? std::move(stmt)
62 std::unique_ptr<Statement> IfStatement::Make(const Context& context, Position pos, bool isStatic,
63 std::unique_ptr<Expression> test, std::unique_ptr<Statement> ifTrue,
64 std::unique_ptr<Statement> ifFalse) {
65 SkASSERT(test->type().matches(*context.fTypes.fBool));
66 SkASSERT(!Analysis::DetectVarDeclarationWithoutScope(*ifTrue));
67 SkASSERT(!ifFalse || !Analysis::DetectVarDeclarationWithoutScope(*ifFalse));
69 const bool optimize = context.fConfig->fSettings.fOptimize;
70 bool trueIsEmpty = false;
71 bool falseIsEmpty = false;
74 // If both sides are empty, the if statement can be reduced to its test expression.
75 trueIsEmpty = ifTrue->isEmpty();
76 falseIsEmpty = !ifFalse || ifFalse->isEmpty();
77 if (trueIsEmpty && falseIsEmpty) {
78 return ExpressionStatement::Make(context, std::move(test));
82 if (isStatic || optimize) {
83 // Static Boolean values can fold down to a single branch.
84 const Expression* testValue = ConstantFolder::GetConstantValueForVariable(*test);
85 if (testValue->isBoolLiteral()) {
86 if (testValue->as<Literal>().boolValue()) {
87 return replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
89 return replace_empty_with_nop(std::move(ifFalse), falseIsEmpty);
95 // Replace an empty if-true branches with Nop; eliminate empty if-false branches entirely.
96 ifTrue = replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
102 return std::make_unique<IfStatement>(pos, isStatic, std::move(test),
103 std::move(ifTrue), std::move(ifFalse));