• 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/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"
17 
18 namespace SkSL {
19 
clone() const20 std::unique_ptr<Statement> IfStatement::clone() const {
21     return std::make_unique<IfStatement>(fLine, this->isStatic(), this->test()->clone(),
22                                          this->ifTrue()->clone(),
23                                          this->ifFalse() ? this->ifFalse()->clone() : nullptr);
24 }
25 
description() const26 String IfStatement::description() const {
27     String result;
28     if (this->isStatic()) {
29         result += "@";
30     }
31     result += "if (" + this->test()->description() + ") " + this->ifTrue()->description();
32     if (this->ifFalse()) {
33         result += " else " + this->ifFalse()->description();
34     }
35     return result;
36 }
37 
Convert(const Context & context,int line,bool isStatic,std::unique_ptr<Expression> test,std::unique_ptr<Statement> ifTrue,std::unique_ptr<Statement> ifFalse)38 std::unique_ptr<Statement> IfStatement::Convert(const Context& context, int line, bool isStatic,
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, line, isStatic, std::move(test),
54                              std::move(ifTrue), std::move(ifFalse));
55 }
56 
replace_empty_with_nop(std::unique_ptr<Statement> stmt,bool isEmpty)57 static std::unique_ptr<Statement> replace_empty_with_nop(std::unique_ptr<Statement> stmt,
58                                                          bool isEmpty) {
59     return (stmt && (!isEmpty || stmt->is<Nop>())) ? std::move(stmt)
60                                                    : Nop::Make();
61 }
62 
Make(const Context & context,int line,bool isStatic,std::unique_ptr<Expression> test,std::unique_ptr<Statement> ifTrue,std::unique_ptr<Statement> ifFalse)63 std::unique_ptr<Statement> IfStatement::Make(const Context& context, int line, bool isStatic,
64                                              std::unique_ptr<Expression> test,
65                                              std::unique_ptr<Statement> ifTrue,
66                                              std::unique_ptr<Statement> ifFalse) {
67     SkASSERT(test->type() == *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 (isStatic || 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>(line, isStatic, std::move(test),
105                                          std::move(ifTrue), std::move(ifFalse));
106 }
107 
108 }  // namespace SkSL
109