Flutter Engine
The Flutter Engine
SkSLIfStatement.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
18#include "src/sksl/ir/SkSLNop.h"
20
21namespace SkSL {
22
23std::string IfStatement::description() const {
24 std::string result;
25 result += "if (" + this->test()->description() + ") " + this->ifTrue()->description();
26 if (this->ifFalse()) {
27 result += " else " + this->ifFalse()->description();
28 }
29 return result;
30}
31
32std::unique_ptr<Statement> IfStatement::Convert(const Context& context,
34 std::unique_ptr<Expression> test,
35 std::unique_ptr<Statement> ifTrue,
36 std::unique_ptr<Statement> ifFalse) {
37 test = context.fTypes.fBool->coerceExpression(std::move(test), context);
38 if (!test) {
39 return nullptr;
40 }
43 return nullptr;
44 }
46 return nullptr;
47 }
48 return IfStatement::Make(context, pos, std::move(test), std::move(ifTrue), std::move(ifFalse));
49}
50
51static std::unique_ptr<Statement> replace_empty_with_nop(std::unique_ptr<Statement> stmt,
52 bool isEmpty) {
53 return (stmt && (!isEmpty || stmt->is<Nop>())) ? std::move(stmt)
54 : Nop::Make();
55}
56
57std::unique_ptr<Statement> IfStatement::Make(const Context& context,
59 std::unique_ptr<Expression> test,
60 std::unique_ptr<Statement> ifTrue,
61 std::unique_ptr<Statement> ifFalse) {
62 SkASSERT(test->type().matches(*context.fTypes.fBool));
65
66 const bool optimize = context.fConfig->fSettings.fOptimize;
67 bool trueIsEmpty = false;
68 bool falseIsEmpty = false;
69
70 if (optimize) {
71 // If both sides are empty, the if statement can be reduced to its test expression.
72 trueIsEmpty = ifTrue->isEmpty();
73 falseIsEmpty = !ifFalse || ifFalse->isEmpty();
74 if (trueIsEmpty && falseIsEmpty) {
75 return ExpressionStatement::Make(context, std::move(test));
76 }
77 }
78
79 if (optimize) {
80 // Static Boolean values can fold down to a single branch.
82 if (testValue->isBoolLiteral()) {
83 if (testValue->as<Literal>().boolValue()) {
84 return replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
85 } else {
86 return replace_empty_with_nop(std::move(ifFalse), falseIsEmpty);
87 }
88 }
89 }
90
91 if (optimize) {
92 // Replace an empty if-true branches with Nop; eliminate empty if-false branches entirely.
93 ifTrue = replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
94 if (falseIsEmpty) {
95 ifFalse = nullptr;
96 }
97 }
98
99 return std::make_unique<IfStatement>(
100 pos, std::move(test), std::move(ifTrue), std::move(ifFalse));
101}
102
103} // namespace SkSL
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkTileMode optimize(SkTileMode tm, int dimension)
const std::unique_ptr< Type > fBool
static const Expression * GetConstantValueForVariable(const Expression &value)
const BuiltinTypes & fTypes
Definition: SkSLContext.h:30
ErrorReporter * fErrors
Definition: SkSLContext.h:36
ProgramConfig * fConfig
Definition: SkSLContext.h:33
static std::unique_ptr< Statement > Make(const Context &context, std::unique_ptr< Expression > expr)
bool isBoolLiteral() const
const T & as() const
Definition: SkSLIRNode.h:133
static std::unique_ptr< Statement > Convert(const Context &context, Position pos, std::unique_ptr< Expression > test, std::unique_ptr< Statement > ifTrue, std::unique_ptr< Statement > ifFalse)
std::string description() const override
static std::unique_ptr< Statement > Make(const Context &context, Position pos, std::unique_ptr< Expression > test, std::unique_ptr< Statement > ifTrue, std::unique_ptr< Statement > ifFalse)
std::unique_ptr< Expression > & test()
std::unique_ptr< Statement > & ifTrue()
std::unique_ptr< Statement > & ifFalse()
SKSL_INT boolValue() const
Definition: SkSLLiteral.h:102
static std::unique_ptr< Statement > Make()
Definition: SkSLNop.h:26
GAsyncResult * result
bool DetectVarDeclarationWithoutScope(const Statement &stmt, ErrorReporter *errors=nullptr)
static std::unique_ptr< Statement > replace_empty_with_nop(std::unique_ptr< Statement > stmt, bool isEmpty)
ProgramSettings fSettings