|
| 1 | +/* |
| 2 | + * Copyright 2020 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/SkSLPrefixExpression.h" |
| 9 | + |
| 10 | +#include "src/sksl/ir/SkSLConstructor.h" |
| 11 | + |
| 12 | +namespace SkSL { |
| 13 | + |
| 14 | +std::unique_ptr<Expression> PrefixExpression::constantPropagate(const IRGenerator& irGenerator, |
| 15 | + const DefinitionMap& definitions) { |
| 16 | + if (this->isNegationOfCompileTimeConstant()) { |
| 17 | + if (this->operand()->is<FloatLiteral>()) { |
| 18 | + // Converts -floatLiteral(1) to floatLiteral(-1). |
| 19 | + return std::make_unique<FloatLiteral>(irGenerator.fContext, fOffset, |
| 20 | + -this->operand()->as<FloatLiteral>().value()); |
| 21 | + } |
| 22 | + if (this->operand()->is<IntLiteral>()) { |
| 23 | + // Converts -intLiteral(1) to intLiteral(-1). |
| 24 | + return std::make_unique<IntLiteral>(irGenerator.fContext, fOffset, |
| 25 | + -this->operand()->as<IntLiteral>().value()); |
| 26 | + } |
| 27 | + if (this->operand()->is<Constructor>()) { |
| 28 | + // We've found a negated constant vector, e.g.: |
| 29 | + // -float4(float3(floatLiteral(1)), floatLiteral(2)) |
| 30 | + // To optimize this, the outer negation is removed and each argument is negated: |
| 31 | + // float4(-float3(floatLiteral(1)), -floatLiteral(2)) |
| 32 | + // After another optimization pass: |
| 33 | + // float4(float3(-floatLiteral(1)), floatLiteral(-2)) |
| 34 | + // Steady state is reached after another optimization pass: |
| 35 | + // float4(float3(floatLiteral(-1)), floatLiteral(-2)) |
| 36 | + std::unique_ptr<Expression> result = this->operand()->clone(); |
| 37 | + for (std::unique_ptr<Expression>& arg : result->as<Constructor>().arguments()) { |
| 38 | + arg = std::make_unique<PrefixExpression>(Token::Kind::TK_MINUS, std::move(arg)); |
| 39 | + } |
| 40 | + return result; |
| 41 | + } |
| 42 | + } |
| 43 | + return nullptr; |
| 44 | +} |
| 45 | + |
| 46 | +} // namespace SkSL |
0 commit comments