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

View file

@ -71,20 +71,18 @@ public:
class ASTProgram {
public:
explicit ASTProgram() = default;
ASTZipper nodes{};
};
class ASTIfThen {
public:
explicit ASTIfThen(Expr condition) : condition(condition) {}
explicit ASTIfThen(Expr condition) : condition{std::move(condition)} {}
Expr condition;
ASTZipper nodes{};
};
class ASTIfElse {
public:
explicit ASTIfElse() = default;
ASTZipper nodes{};
};
@ -103,7 +101,7 @@ public:
class ASTVarSet {
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;
Expr condition;
};
@ -117,42 +115,45 @@ public:
class ASTGoto {
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;
u32 label;
};
class ASTDoWhile {
public:
explicit ASTDoWhile(Expr condition) : condition(condition) {}
explicit ASTDoWhile(Expr condition) : condition{std::move(condition)} {}
Expr condition;
ASTZipper nodes{};
};
class ASTReturn {
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;
bool kills;
};
class ASTBreak {
public:
explicit ASTBreak(Expr condition) : condition{condition} {}
explicit ASTBreak(Expr condition) : condition{std::move(condition)} {}
Expr condition;
};
class ASTBase {
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>
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) {
parent = new_parent;
parent = std::move(new_parent);
}
ASTNode& GetParent() {
@ -247,7 +248,7 @@ public:
void SetGotoCondition(Expr new_condition) {
auto inner = std::get_if<ASTGoto>(&data);
if (inner) {
inner->condition = new_condition;
inner->condition = std::move(new_condition);
}
}
@ -370,9 +371,9 @@ public:
private:
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);

View file

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

View file

@ -28,7 +28,7 @@ using Expr = std::shared_ptr<ExprData>;
class ExprAnd final {
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;
@ -38,7 +38,7 @@ public:
class ExprOr final {
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;
@ -48,7 +48,7 @@ public:
class ExprNot final {
public:
explicit ExprNot(Expr a) : operand1{a} {}
explicit ExprNot(Expr a) : operand1{std::move(a)} {}
bool operator==(const ExprNot& b) const;
@ -105,9 +105,9 @@ Expr MakeExpr(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);
@ -115,6 +115,6 @@ Expr MakeExprAnd(Expr first, Expr second);
Expr MakeExprOr(Expr first, Expr second);
bool ExprIsTrue(Expr first);
bool ExprIsTrue(const Expr& first);
} // namespace VideoCommon::Shader