• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/sksl/ir/SkSLIfStatement.h"
9 
10 #include "include/core/SkTypes.h"
11 #include "src/sksl/SkSLAnalysis.h"
12 #include "src/sksl/SkSLBuiltinTypes.h"
13 #include "src/sksl/SkSLConstantFolder.h"
14 #include "src/sksl/SkSLContext.h"
15 #include "src/sksl/SkSLProgramSettings.h"
16 #include "src/sksl/ir/SkSLExpressionStatement.h"
17 #include "src/sksl/ir/SkSLLiteral.h"
18 #include "src/sksl/ir/SkSLNop.h"
19 #include "src/sksl/ir/SkSLType.h"
20 
21 namespace SkSL {
22 
clone() const23 std::unique_ptr<Statement> IfStatement::clone() const {
24     return std::make_unique<IfStatement>(fPosition, this->test()->clone(), this->ifTrue()->clone(),
25                                          this->ifFalse() ? this->ifFalse()->clone() : nullptr);
26 }
27 
description() const28 std::string IfStatement::description() const {
29     std::string result;
30     result += "if (" + this->test()->description() + ") " + this->ifTrue()->description();
31     if (this->ifFalse()) {
32         result += " else " + this->ifFalse()->description();
33     }
34     return result;
35 }
36 
Convert(const Context & context,Position pos,std::unique_ptr<Expression> test,std::unique_ptr<Statement> ifTrue,std::unique_ptr<Statement> ifFalse)37 std::unique_ptr<Statement> IfStatement::Convert(const Context& context,
38                                                 Position pos,
39                                                 std::unique_ptr<Expression> test,
40                                                 std::unique_ptr<Statement> ifTrue,
41                                                 std::unique_ptr<Statement> ifFalse) {
42     test = context.fTypes.fBool->coerceExpression(std::move(test), context);
43     if (!test) {
44         return nullptr;
45     }
46     SkASSERT(ifTrue);
47     if (Analysis::DetectVarDeclarationWithoutScope(*ifTrue, context.fErrors)) {
48         return nullptr;
49     }
50     if (ifFalse && Analysis::DetectVarDeclarationWithoutScope(*ifFalse, context.fErrors)) {
51         return nullptr;
52     }
53     return IfStatement::Make(context, pos, std::move(test), std::move(ifTrue), std::move(ifFalse));
54 }
55 
replace_empty_with_nop(std::unique_ptr<Statement> stmt,bool isEmpty)56 static std::unique_ptr<Statement> replace_empty_with_nop(std::unique_ptr<Statement> stmt,
57                                                          bool isEmpty) {
58     return (stmt && (!isEmpty || stmt->is<Nop>())) ? std::move(stmt)
59                                                    : Nop::Make();
60 }
61 
Make(const Context & context,Position pos,std::unique_ptr<Expression> test,std::unique_ptr<Statement> ifTrue,std::unique_ptr<Statement> ifFalse)62 std::unique_ptr<Statement> IfStatement::Make(const Context& context,
63                                              Position pos,
64                                              std::unique_ptr<Expression> test,
65                                              std::unique_ptr<Statement> ifTrue,
66                                              std::unique_ptr<Statement> ifFalse) {
67     SkASSERT(test->type().matches(*context.fTypes.fBool));
68     SkASSERT(!Analysis::DetectVarDeclarationWithoutScope(*ifTrue));
69     SkASSERT(!ifFalse || !Analysis::DetectVarDeclarationWithoutScope(*ifFalse));
70 
71     const bool optimize = context.fConfig->fSettings.fOptimize;
72     bool trueIsEmpty = false;
73     bool falseIsEmpty = false;
74 
75     if (optimize) {
76         // If both sides are empty, the if statement can be reduced to its test expression.
77         trueIsEmpty = ifTrue->isEmpty();
78         falseIsEmpty = !ifFalse || ifFalse->isEmpty();
79         if (trueIsEmpty && falseIsEmpty) {
80             return ExpressionStatement::Make(context, std::move(test));
81         }
82     }
83 
84     if (optimize) {
85         // Static Boolean values can fold down to a single branch.
86         const Expression* testValue = ConstantFolder::GetConstantValueForVariable(*test);
87         if (testValue->isBoolLiteral()) {
88             if (testValue->as<Literal>().boolValue()) {
89                 return replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
90             } else {
91                 return replace_empty_with_nop(std::move(ifFalse), falseIsEmpty);
92             }
93         }
94     }
95 
96     if (optimize) {
97         // Replace an empty if-true branches with Nop; eliminate empty if-false branches entirely.
98         ifTrue = replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
99         if (falseIsEmpty) {
100             ifFalse = nullptr;
101         }
102     }
103 
104     return std::make_unique<IfStatement>(
105             pos, std::move(test), std::move(ifTrue), std::move(ifFalse));
106 }
107 
108 }  // namespace SkSL
109