video_core/{ast, expr}: Use std::move where applicable

Avoids unnecessary atomic reference count increments and decrements.
This commit is contained in:
Lioncash 2019-10-05 08:17:32 -04:00
parent 8e0c80f269
commit 8eb1398f8d
4 changed files with 47 additions and 45 deletions

View file

@ -17,6 +17,7 @@ void ASTZipper::Init(const ASTNode new_first, const ASTNode parent) {
ASSERT(new_first->manager == nullptr); ASSERT(new_first->manager == nullptr);
first = new_first; first = new_first;
last = new_first; last = new_first;
ASTNode current = first; ASTNode current = first;
while (current) { while (current) {
current->manager = this; current->manager = this;
@ -92,7 +93,7 @@ void ASTZipper::InsertBefore(const ASTNode new_node, const ASTNode at_node) {
new_node->manager = this; new_node->manager = this;
} }
void ASTZipper::DetachTail(const ASTNode node) { void ASTZipper::DetachTail(ASTNode node) {
ASSERT(node->manager == this); ASSERT(node->manager == this);
if (node == first) { if (node == first) {
first.reset(); first.reset();
@ -103,7 +104,8 @@ void ASTZipper::DetachTail(const ASTNode node) {
last = node->previous; last = node->previous;
last->next.reset(); last->next.reset();
node->previous.reset(); node->previous.reset();
ASTNode current = node;
ASTNode current = std::move(node);
while (current) { while (current) {
current->manager = nullptr; current->manager = nullptr;
current->parent.reset(); current->parent.reset();
@ -413,19 +415,19 @@ void ASTManager::InsertLabel(u32 address) {
void ASTManager::InsertGoto(Expr condition, u32 address) { void ASTManager::InsertGoto(Expr condition, u32 address) {
const u32 index = labels_map[address]; const u32 index = labels_map[address];
const ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, condition, index); const ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, std::move(condition), index);
gotos.push_back(goto_node); gotos.push_back(goto_node);
program->nodes.PushBack(goto_node); program->nodes.PushBack(goto_node);
} }
void ASTManager::InsertBlock(u32 start_address, u32 end_address) { void ASTManager::InsertBlock(u32 start_address, u32 end_address) {
const ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address); ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address);
program->nodes.PushBack(block); program->nodes.PushBack(std::move(block));
} }
void ASTManager::InsertReturn(Expr condition, bool kills) { void ASTManager::InsertReturn(Expr condition, bool kills) {
const ASTNode node = ASTBase::Make<ASTReturn>(main_node, condition, kills); ASTNode node = ASTBase::Make<ASTReturn>(main_node, std::move(condition), kills);
program->nodes.PushBack(node); program->nodes.PushBack(std::move(node));
} }
// The decompile algorithm is based on // The decompile algorithm is based on
@ -539,11 +541,11 @@ bool ASTManager::IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const {
return false; return false;
} }
bool ASTManager::IndirectlyRelated(ASTNode first, ASTNode second) { bool ASTManager::IndirectlyRelated(const ASTNode& first, const ASTNode& second) const {
return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second)); return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second));
} }
bool ASTManager::DirectlyRelated(ASTNode first, ASTNode second) { bool ASTManager::DirectlyRelated(const ASTNode& first, const ASTNode& second) const {
if (first->GetParent() == second->GetParent()) { if (first->GetParent() == second->GetParent()) {
return false; return false;
} }

View file

@ -71,20 +71,18 @@ public:
class ASTProgram { class ASTProgram {
public: public:
explicit ASTProgram() = default;
ASTZipper nodes{}; ASTZipper nodes{};
}; };
class ASTIfThen { class ASTIfThen {
public: public:
explicit ASTIfThen(Expr condition) : condition(condition) {} explicit ASTIfThen(Expr condition) : condition{std::move(condition)} {}
Expr condition; Expr condition;
ASTZipper nodes{}; ASTZipper nodes{};
}; };
class ASTIfElse { class ASTIfElse {
public: public:
explicit ASTIfElse() = default;
ASTZipper nodes{}; ASTZipper nodes{};
}; };
@ -103,7 +101,7 @@ public:
class ASTVarSet { class ASTVarSet {
public: public:
explicit ASTVarSet(u32 index, Expr condition) : index{index}, condition{condition} {} explicit ASTVarSet(u32 index, Expr condition) : index{index}, condition{std::move(condition)} {}
u32 index; u32 index;
Expr condition; Expr condition;
}; };
@ -117,42 +115,45 @@ public:
class ASTGoto { class ASTGoto {
public: public:
explicit ASTGoto(Expr condition, u32 label) : condition{condition}, label{label} {} explicit ASTGoto(Expr condition, u32 label) : condition{std::move(condition)}, label{label} {}
Expr condition; Expr condition;
u32 label; u32 label;
}; };
class ASTDoWhile { class ASTDoWhile {
public: public:
explicit ASTDoWhile(Expr condition) : condition(condition) {} explicit ASTDoWhile(Expr condition) : condition{std::move(condition)} {}
Expr condition; Expr condition;
ASTZipper nodes{}; ASTZipper nodes{};
}; };
class ASTReturn { class ASTReturn {
public: public:
explicit ASTReturn(Expr condition, bool kills) : condition{condition}, kills{kills} {} explicit ASTReturn(Expr condition, bool kills)
: condition{std::move(condition)}, kills{kills} {}
Expr condition; Expr condition;
bool kills; bool kills;
}; };
class ASTBreak { class ASTBreak {
public: public:
explicit ASTBreak(Expr condition) : condition{condition} {} explicit ASTBreak(Expr condition) : condition{std::move(condition)} {}
Expr condition; Expr condition;
}; };
class ASTBase { class ASTBase {
public: public:
explicit ASTBase(ASTNode parent, ASTData data) : parent{parent}, data{data} {} explicit ASTBase(ASTNode parent, ASTData data)
: data{std::move(data)}, parent{std::move(parent)} {}
template <class U, class... Args> template <class U, class... Args>
static ASTNode Make(ASTNode parent, Args&&... args) { static ASTNode Make(ASTNode parent, Args&&... args) {
return std::make_shared<ASTBase>(parent, ASTData(U(std::forward<Args>(args)...))); return std::make_shared<ASTBase>(std::move(parent),
ASTData(U(std::forward<Args>(args)...)));
} }
void SetParent(ASTNode new_parent) { void SetParent(ASTNode new_parent) {
parent = new_parent; parent = std::move(new_parent);
} }
ASTNode& GetParent() { ASTNode& GetParent() {
@ -247,7 +248,7 @@ public:
void SetGotoCondition(Expr new_condition) { void SetGotoCondition(Expr new_condition) {
auto inner = std::get_if<ASTGoto>(&data); auto inner = std::get_if<ASTGoto>(&data);
if (inner) { if (inner) {
inner->condition = new_condition; inner->condition = std::move(new_condition);
} }
} }
@ -370,9 +371,9 @@ public:
private: private:
bool IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const; bool IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const;
bool IndirectlyRelated(ASTNode first, ASTNode second); bool IndirectlyRelated(const ASTNode& first, const ASTNode& second) const;
bool DirectlyRelated(ASTNode first, ASTNode second); bool DirectlyRelated(const ASTNode& first, const ASTNode& second) const;
void EncloseDoWhile(ASTNode goto_node, ASTNode label); void EncloseDoWhile(ASTNode goto_node, ASTNode label);

View file

@ -2,14 +2,21 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#pragma once
#include <memory> #include <memory>
#include <variant> #include <variant>
#include "video_core/shader/expr.h" #include "video_core/shader/expr.h"
namespace VideoCommon::Shader { namespace VideoCommon::Shader {
namespace {
bool ExprIsBoolean(const Expr& expr) {
return std::holds_alternative<ExprBoolean>(*expr);
}
bool ExprBooleanGet(const Expr& expr) {
return std::get_if<ExprBoolean>(expr.get())->value;
}
} // Anonymous namespace
bool ExprAnd::operator==(const ExprAnd& b) const { bool ExprAnd::operator==(const ExprAnd& b) const {
return (*operand1 == *b.operand1) && (*operand2 == *b.operand2); return (*operand1 == *b.operand1) && (*operand2 == *b.operand2);
@ -23,19 +30,11 @@ bool ExprNot::operator==(const ExprNot& b) const {
return (*operand1 == *b.operand1); return (*operand1 == *b.operand1);
} }
bool ExprIsBoolean(Expr expr) {
return std::holds_alternative<ExprBoolean>(*expr);
}
bool ExprBooleanGet(Expr expr) {
return std::get_if<ExprBoolean>(expr.get())->value;
}
Expr MakeExprNot(Expr first) { Expr MakeExprNot(Expr first) {
if (std::holds_alternative<ExprNot>(*first)) { if (std::holds_alternative<ExprNot>(*first)) {
return std::get_if<ExprNot>(first.get())->operand1; return std::get_if<ExprNot>(first.get())->operand1;
} }
return MakeExpr<ExprNot>(first); return MakeExpr<ExprNot>(std::move(first));
} }
Expr MakeExprAnd(Expr first, Expr second) { Expr MakeExprAnd(Expr first, Expr second) {
@ -45,7 +44,7 @@ Expr MakeExprAnd(Expr first, Expr second) {
if (ExprIsBoolean(second)) { if (ExprIsBoolean(second)) {
return ExprBooleanGet(second) ? first : second; return ExprBooleanGet(second) ? first : second;
} }
return MakeExpr<ExprAnd>(first, second); return MakeExpr<ExprAnd>(std::move(first), std::move(second));
} }
Expr MakeExprOr(Expr first, Expr second) { Expr MakeExprOr(Expr first, Expr second) {
@ -55,14 +54,14 @@ Expr MakeExprOr(Expr first, Expr second) {
if (ExprIsBoolean(second)) { if (ExprIsBoolean(second)) {
return ExprBooleanGet(second) ? second : first; return ExprBooleanGet(second) ? second : first;
} }
return MakeExpr<ExprOr>(first, second); return MakeExpr<ExprOr>(std::move(first), std::move(second));
} }
bool ExprAreEqual(Expr first, Expr second) { bool ExprAreEqual(const Expr& first, const Expr& second) {
return (*first) == (*second); return (*first) == (*second);
} }
bool ExprAreOpposite(Expr first, Expr second) { bool ExprAreOpposite(const Expr& first, const Expr& second) {
if (std::holds_alternative<ExprNot>(*first)) { if (std::holds_alternative<ExprNot>(*first)) {
return ExprAreEqual(std::get_if<ExprNot>(first.get())->operand1, second); return ExprAreEqual(std::get_if<ExprNot>(first.get())->operand1, second);
} }
@ -72,7 +71,7 @@ bool ExprAreOpposite(Expr first, Expr second) {
return false; return false;
} }
bool ExprIsTrue(Expr first) { bool ExprIsTrue(const Expr& first) {
if (ExprIsBoolean(first)) { if (ExprIsBoolean(first)) {
return ExprBooleanGet(first); return ExprBooleanGet(first);
} }

View file

@ -28,7 +28,7 @@ using Expr = std::shared_ptr<ExprData>;
class ExprAnd final { class ExprAnd final {
public: public:
explicit ExprAnd(Expr a, Expr b) : operand1{a}, operand2{b} {} explicit ExprAnd(Expr a, Expr b) : operand1{std::move(a)}, operand2{std::move(b)} {}
bool operator==(const ExprAnd& b) const; bool operator==(const ExprAnd& b) const;
@ -38,7 +38,7 @@ public:
class ExprOr final { class ExprOr final {
public: public:
explicit ExprOr(Expr a, Expr b) : operand1{a}, operand2{b} {} explicit ExprOr(Expr a, Expr b) : operand1{std::move(a)}, operand2{std::move(b)} {}
bool operator==(const ExprOr& b) const; bool operator==(const ExprOr& b) const;
@ -48,7 +48,7 @@ public:
class ExprNot final { class ExprNot final {
public: public:
explicit ExprNot(Expr a) : operand1{a} {} explicit ExprNot(Expr a) : operand1{std::move(a)} {}
bool operator==(const ExprNot& b) const; bool operator==(const ExprNot& b) const;
@ -105,9 +105,9 @@ Expr MakeExpr(Args&&... args) {
return std::make_shared<ExprData>(T(std::forward<Args>(args)...)); return std::make_shared<ExprData>(T(std::forward<Args>(args)...));
} }
bool ExprAreEqual(Expr first, Expr second); bool ExprAreEqual(const Expr& first, const Expr& second);
bool ExprAreOpposite(Expr first, Expr second); bool ExprAreOpposite(const Expr& first, const Expr& second);
Expr MakeExprNot(Expr first); Expr MakeExprNot(Expr first);
@ -115,6 +115,6 @@ Expr MakeExprAnd(Expr first, Expr second);
Expr MakeExprOr(Expr first, Expr second); Expr MakeExprOr(Expr first, Expr second);
bool ExprIsTrue(Expr first); bool ExprIsTrue(const Expr& first);
} // namespace VideoCommon::Shader } // namespace VideoCommon::Shader