| 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 |
|
| 23 |
std::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 |
|
| 32 |
std::unique_ptr<Statement> IfStatement::Convert(const Context& context, |
| 33 |
Position pos, |
| 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 |
} |
| 41 |
SkASSERT(ifTrue); |
| 42 |
if (Analysis::DetectVarDeclarationWithoutScope(*ifTrue, context.fErrors)) { |
| 43 |
return nullptr; |
| 44 |
} |
| 45 |
if (ifFalse && Analysis::DetectVarDeclarationWithoutScope(*ifFalse, context.fErrors)) { |
| 46 |
return nullptr; |
| 47 |
} |
| 48 |
return IfStatement::Make(context, pos, std::move(test), std::move(ifTrue), std::move(ifFalse)); |
| 49 |
} |
| 50 |
|
| 51 |
static 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 |
|
| 57 |
std::unique_ptr<Statement> IfStatement::Make(const Context& context, |
| 58 |
Position pos, |
| 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)); |
| 63 |
SkASSERT(!Analysis::DetectVarDeclarationWithoutScope(*ifTrue)); |
| 64 |
SkASSERT(!ifFalse || !Analysis::DetectVarDeclarationWithoutScope(*ifFalse)); |
| 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. |
| 81 |
const Expression* testValue = ConstantFolder::GetConstantValueForVariable(*test); |
| 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 |