summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Lioncash2019-10-05 08:17:32 -0400
committerGravatar Lioncash2019-10-05 09:14:23 -0400
commit8eb1398f8d90fb2813f438b9fffac716b6ec51d2 (patch)
tree39a270023b64af2dce85e3d4a13484c95243b2b3 /src
parentvideo_core/ast: Supply const accessors for data where applicable (diff)
downloadyuzu-8eb1398f8d90fb2813f438b9fffac716b6ec51d2.tar.gz
yuzu-8eb1398f8d90fb2813f438b9fffac716b6ec51d2.tar.xz
yuzu-8eb1398f8d90fb2813f438b9fffac716b6ec51d2.zip
video_core/{ast, expr}: Use std::move where applicable
Avoids unnecessary atomic reference count increments and decrements.
Diffstat (limited to 'src')
-rw-r--r--src/video_core/shader/ast.cpp20
-rw-r--r--src/video_core/shader/ast.h29
-rw-r--r--src/video_core/shader/expr.cpp31
-rw-r--r--src/video_core/shader/expr.h12
4 files changed, 47 insertions, 45 deletions
diff --git a/src/video_core/shader/ast.cpp b/src/video_core/shader/ast.cpp
index 87c722682..986e4cd64 100644
--- a/src/video_core/shader/ast.cpp
+++ b/src/video_core/shader/ast.cpp
@@ -17,6 +17,7 @@ void ASTZipper::Init(const ASTNode new_first, const ASTNode parent) {
17 ASSERT(new_first->manager == nullptr); 17 ASSERT(new_first->manager == nullptr);
18 first = new_first; 18 first = new_first;
19 last = new_first; 19 last = new_first;
20
20 ASTNode current = first; 21 ASTNode current = first;
21 while (current) { 22 while (current) {
22 current->manager = this; 23 current->manager = this;
@@ -92,7 +93,7 @@ void ASTZipper::InsertBefore(const ASTNode new_node, const ASTNode at_node) {
92 new_node->manager = this; 93 new_node->manager = this;
93} 94}
94 95
95void ASTZipper::DetachTail(const ASTNode node) { 96void ASTZipper::DetachTail(ASTNode node) {
96 ASSERT(node->manager == this); 97 ASSERT(node->manager == this);
97 if (node == first) { 98 if (node == first) {
98 first.reset(); 99 first.reset();
@@ -103,7 +104,8 @@ void ASTZipper::DetachTail(const ASTNode node) {
103 last = node->previous; 104 last = node->previous;
104 last->next.reset(); 105 last->next.reset();
105 node->previous.reset(); 106 node->previous.reset();
106 ASTNode current = node; 107
108 ASTNode current = std::move(node);
107 while (current) { 109 while (current) {
108 current->manager = nullptr; 110 current->manager = nullptr;
109 current->parent.reset(); 111 current->parent.reset();
@@ -413,19 +415,19 @@ void ASTManager::InsertLabel(u32 address) {
413 415
414void ASTManager::InsertGoto(Expr condition, u32 address) { 416void ASTManager::InsertGoto(Expr condition, u32 address) {
415 const u32 index = labels_map[address]; 417 const u32 index = labels_map[address];
416 const ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, condition, index); 418 const ASTNode goto_node = ASTBase::Make<ASTGoto>(main_node, std::move(condition), index);
417 gotos.push_back(goto_node); 419 gotos.push_back(goto_node);
418 program->nodes.PushBack(goto_node); 420 program->nodes.PushBack(goto_node);
419} 421}
420 422
421void ASTManager::InsertBlock(u32 start_address, u32 end_address) { 423void ASTManager::InsertBlock(u32 start_address, u32 end_address) {
422 const ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address); 424 ASTNode block = ASTBase::Make<ASTBlockEncoded>(main_node, start_address, end_address);
423 program->nodes.PushBack(block); 425 program->nodes.PushBack(std::move(block));
424} 426}
425 427
426void ASTManager::InsertReturn(Expr condition, bool kills) { 428void ASTManager::InsertReturn(Expr condition, bool kills) {
427 const ASTNode node = ASTBase::Make<ASTReturn>(main_node, condition, kills); 429 ASTNode node = ASTBase::Make<ASTReturn>(main_node, std::move(condition), kills);
428 program->nodes.PushBack(node); 430 program->nodes.PushBack(std::move(node));
429} 431}
430 432
431// The decompile algorithm is based on 433// The decompile algorithm is based on
@@ -539,11 +541,11 @@ bool ASTManager::IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const {
539 return false; 541 return false;
540} 542}
541 543
542bool ASTManager::IndirectlyRelated(ASTNode first, ASTNode second) { 544bool ASTManager::IndirectlyRelated(const ASTNode& first, const ASTNode& second) const {
543 return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second)); 545 return !(first->GetParent() == second->GetParent() || DirectlyRelated(first, second));
544} 546}
545 547
546bool ASTManager::DirectlyRelated(ASTNode first, ASTNode second) { 548bool ASTManager::DirectlyRelated(const ASTNode& first, const ASTNode& second) const {
547 if (first->GetParent() == second->GetParent()) { 549 if (first->GetParent() == second->GetParent()) {
548 return false; 550 return false;
549 } 551 }
diff --git a/src/video_core/shader/ast.h b/src/video_core/shader/ast.h
index 39f500284..aad35c12e 100644
--- a/src/video_core/shader/ast.h
+++ b/src/video_core/shader/ast.h
@@ -71,20 +71,18 @@ public:
71 71
72class ASTProgram { 72class ASTProgram {
73public: 73public:
74 explicit ASTProgram() = default;
75 ASTZipper nodes{}; 74 ASTZipper nodes{};
76}; 75};
77 76
78class ASTIfThen { 77class ASTIfThen {
79public: 78public:
80 explicit ASTIfThen(Expr condition) : condition(condition) {} 79 explicit ASTIfThen(Expr condition) : condition{std::move(condition)} {}
81 Expr condition; 80 Expr condition;
82 ASTZipper nodes{}; 81 ASTZipper nodes{};
83}; 82};
84 83
85class ASTIfElse { 84class ASTIfElse {
86public: 85public:
87 explicit ASTIfElse() = default;
88 ASTZipper nodes{}; 86 ASTZipper nodes{};
89}; 87};
90 88
@@ -103,7 +101,7 @@ public:
103 101
104class ASTVarSet { 102class ASTVarSet {
105public: 103public:
106 explicit ASTVarSet(u32 index, Expr condition) : index{index}, condition{condition} {} 104 explicit ASTVarSet(u32 index, Expr condition) : index{index}, condition{std::move(condition)} {}
107 u32 index; 105 u32 index;
108 Expr condition; 106 Expr condition;
109}; 107};
@@ -117,42 +115,45 @@ public:
117 115
118class ASTGoto { 116class ASTGoto {
119public: 117public:
120 explicit ASTGoto(Expr condition, u32 label) : condition{condition}, label{label} {} 118 explicit ASTGoto(Expr condition, u32 label) : condition{std::move(condition)}, label{label} {}
121 Expr condition; 119 Expr condition;
122 u32 label; 120 u32 label;
123}; 121};
124 122
125class ASTDoWhile { 123class ASTDoWhile {
126public: 124public:
127 explicit ASTDoWhile(Expr condition) : condition(condition) {} 125 explicit ASTDoWhile(Expr condition) : condition{std::move(condition)} {}
128 Expr condition; 126 Expr condition;
129 ASTZipper nodes{}; 127 ASTZipper nodes{};
130}; 128};
131 129
132class ASTReturn { 130class ASTReturn {
133public: 131public:
134 explicit ASTReturn(Expr condition, bool kills) : condition{condition}, kills{kills} {} 132 explicit ASTReturn(Expr condition, bool kills)
133 : condition{std::move(condition)}, kills{kills} {}
135 Expr condition; 134 Expr condition;
136 bool kills; 135 bool kills;
137}; 136};
138 137
139class ASTBreak { 138class ASTBreak {
140public: 139public:
141 explicit ASTBreak(Expr condition) : condition{condition} {} 140 explicit ASTBreak(Expr condition) : condition{std::move(condition)} {}
142 Expr condition; 141 Expr condition;
143}; 142};
144 143
145class ASTBase { 144class ASTBase {
146public: 145public:
147 explicit ASTBase(ASTNode parent, ASTData data) : parent{parent}, data{data} {} 146 explicit ASTBase(ASTNode parent, ASTData data)
147 : data{std::move(data)}, parent{std::move(parent)} {}
148 148
149 template <class U, class... Args> 149 template <class U, class... Args>
150 static ASTNode Make(ASTNode parent, Args&&... args) { 150 static ASTNode Make(ASTNode parent, Args&&... args) {
151 return std::make_shared<ASTBase>(parent, ASTData(U(std::forward<Args>(args)...))); 151 return std::make_shared<ASTBase>(std::move(parent),
152 ASTData(U(std::forward<Args>(args)...)));
152 } 153 }
153 154
154 void SetParent(ASTNode new_parent) { 155 void SetParent(ASTNode new_parent) {
155 parent = new_parent; 156 parent = std::move(new_parent);
156 } 157 }
157 158
158 ASTNode& GetParent() { 159 ASTNode& GetParent() {
@@ -247,7 +248,7 @@ public:
247 void SetGotoCondition(Expr new_condition) { 248 void SetGotoCondition(Expr new_condition) {
248 auto inner = std::get_if<ASTGoto>(&data); 249 auto inner = std::get_if<ASTGoto>(&data);
249 if (inner) { 250 if (inner) {
250 inner->condition = new_condition; 251 inner->condition = std::move(new_condition);
251 } 252 }
252 } 253 }
253 254
@@ -370,9 +371,9 @@ public:
370private: 371private:
371 bool IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const; 372 bool IsBackwardsJump(ASTNode goto_node, ASTNode label_node) const;
372 373
373 bool IndirectlyRelated(ASTNode first, ASTNode second); 374 bool IndirectlyRelated(const ASTNode& first, const ASTNode& second) const;
374 375
375 bool DirectlyRelated(ASTNode first, ASTNode second); 376 bool DirectlyRelated(const ASTNode& first, const ASTNode& second) const;
376 377
377 void EncloseDoWhile(ASTNode goto_node, ASTNode label); 378 void EncloseDoWhile(ASTNode goto_node, ASTNode label);
378 379
diff --git a/src/video_core/shader/expr.cpp b/src/video_core/shader/expr.cpp
index ca633ffb1..39df7a927 100644
--- a/src/video_core/shader/expr.cpp
+++ b/src/video_core/shader/expr.cpp
@@ -2,14 +2,21 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#pragma once
6
7#include <memory> 5#include <memory>
8#include <variant> 6#include <variant>
9 7
10#include "video_core/shader/expr.h" 8#include "video_core/shader/expr.h"
11 9
12namespace VideoCommon::Shader { 10namespace VideoCommon::Shader {
11namespace {
12bool ExprIsBoolean(const Expr& expr) {
13 return std::holds_alternative<ExprBoolean>(*expr);
14}
15
16bool ExprBooleanGet(const Expr& expr) {
17 return std::get_if<ExprBoolean>(expr.get())->value;
18}
19} // Anonymous namespace
13 20
14bool ExprAnd::operator==(const ExprAnd& b) const { 21bool ExprAnd::operator==(const ExprAnd& b) const {
15 return (*operand1 == *b.operand1) && (*operand2 == *b.operand2); 22 return (*operand1 == *b.operand1) && (*operand2 == *b.operand2);
@@ -23,19 +30,11 @@ bool ExprNot::operator==(const ExprNot& b) const {
23 return (*operand1 == *b.operand1); 30 return (*operand1 == *b.operand1);
24} 31}
25 32
26bool ExprIsBoolean(Expr expr) {
27 return std::holds_alternative<ExprBoolean>(*expr);
28}
29
30bool ExprBooleanGet(Expr expr) {
31 return std::get_if<ExprBoolean>(expr.get())->value;
32}
33
34Expr MakeExprNot(Expr first) { 33Expr MakeExprNot(Expr first) {
35 if (std::holds_alternative<ExprNot>(*first)) { 34 if (std::holds_alternative<ExprNot>(*first)) {
36 return std::get_if<ExprNot>(first.get())->operand1; 35 return std::get_if<ExprNot>(first.get())->operand1;
37 } 36 }
38 return MakeExpr<ExprNot>(first); 37 return MakeExpr<ExprNot>(std::move(first));
39} 38}
40 39
41Expr MakeExprAnd(Expr first, Expr second) { 40Expr MakeExprAnd(Expr first, Expr second) {
@@ -45,7 +44,7 @@ Expr MakeExprAnd(Expr first, Expr second) {
45 if (ExprIsBoolean(second)) { 44 if (ExprIsBoolean(second)) {
46 return ExprBooleanGet(second) ? first : second; 45 return ExprBooleanGet(second) ? first : second;
47 } 46 }
48 return MakeExpr<ExprAnd>(first, second); 47 return MakeExpr<ExprAnd>(std::move(first), std::move(second));
49} 48}
50 49
51Expr MakeExprOr(Expr first, Expr second) { 50Expr MakeExprOr(Expr first, Expr second) {
@@ -55,14 +54,14 @@ Expr MakeExprOr(Expr first, Expr second) {
55 if (ExprIsBoolean(second)) { 54 if (ExprIsBoolean(second)) {
56 return ExprBooleanGet(second) ? second : first; 55 return ExprBooleanGet(second) ? second : first;
57 } 56 }
58 return MakeExpr<ExprOr>(first, second); 57 return MakeExpr<ExprOr>(std::move(first), std::move(second));
59} 58}
60 59
61bool ExprAreEqual(Expr first, Expr second) { 60bool ExprAreEqual(const Expr& first, const Expr& second) {
62 return (*first) == (*second); 61 return (*first) == (*second);
63} 62}
64 63
65bool ExprAreOpposite(Expr first, Expr second) { 64bool ExprAreOpposite(const Expr& first, const Expr& second) {
66 if (std::holds_alternative<ExprNot>(*first)) { 65 if (std::holds_alternative<ExprNot>(*first)) {
67 return ExprAreEqual(std::get_if<ExprNot>(first.get())->operand1, second); 66 return ExprAreEqual(std::get_if<ExprNot>(first.get())->operand1, second);
68 } 67 }
@@ -72,7 +71,7 @@ bool ExprAreOpposite(Expr first, Expr second) {
72 return false; 71 return false;
73} 72}
74 73
75bool ExprIsTrue(Expr first) { 74bool ExprIsTrue(const Expr& first) {
76 if (ExprIsBoolean(first)) { 75 if (ExprIsBoolean(first)) {
77 return ExprBooleanGet(first); 76 return ExprBooleanGet(first);
78 } 77 }
diff --git a/src/video_core/shader/expr.h b/src/video_core/shader/expr.h
index 4c399cef9..1f1638520 100644
--- a/src/video_core/shader/expr.h
+++ b/src/video_core/shader/expr.h
@@ -28,7 +28,7 @@ using Expr = std::shared_ptr<ExprData>;
28 28
29class ExprAnd final { 29class ExprAnd final {
30public: 30public:
31 explicit ExprAnd(Expr a, Expr b) : operand1{a}, operand2{b} {} 31 explicit ExprAnd(Expr a, Expr b) : operand1{std::move(a)}, operand2{std::move(b)} {}
32 32
33 bool operator==(const ExprAnd& b) const; 33 bool operator==(const ExprAnd& b) const;
34 34
@@ -38,7 +38,7 @@ public:
38 38
39class ExprOr final { 39class ExprOr final {
40public: 40public:
41 explicit ExprOr(Expr a, Expr b) : operand1{a}, operand2{b} {} 41 explicit ExprOr(Expr a, Expr b) : operand1{std::move(a)}, operand2{std::move(b)} {}
42 42
43 bool operator==(const ExprOr& b) const; 43 bool operator==(const ExprOr& b) const;
44 44
@@ -48,7 +48,7 @@ public:
48 48
49class ExprNot final { 49class ExprNot final {
50public: 50public:
51 explicit ExprNot(Expr a) : operand1{a} {} 51 explicit ExprNot(Expr a) : operand1{std::move(a)} {}
52 52
53 bool operator==(const ExprNot& b) const; 53 bool operator==(const ExprNot& b) const;
54 54
@@ -105,9 +105,9 @@ Expr MakeExpr(Args&&... args) {
105 return std::make_shared<ExprData>(T(std::forward<Args>(args)...)); 105 return std::make_shared<ExprData>(T(std::forward<Args>(args)...));
106} 106}
107 107
108bool ExprAreEqual(Expr first, Expr second); 108bool ExprAreEqual(const Expr& first, const Expr& second);
109 109
110bool ExprAreOpposite(Expr first, Expr second); 110bool ExprAreOpposite(const Expr& first, const Expr& second);
111 111
112Expr MakeExprNot(Expr first); 112Expr MakeExprNot(Expr first);
113 113
@@ -115,6 +115,6 @@ Expr MakeExprAnd(Expr first, Expr second);
115 115
116Expr MakeExprOr(Expr first, Expr second); 116Expr MakeExprOr(Expr first, Expr second);
117 117
118bool ExprIsTrue(Expr first); 118bool ExprIsTrue(const Expr& first);
119 119
120} // namespace VideoCommon::Shader 120} // namespace VideoCommon::Shader