diff options
| author | 2019-10-05 08:17:32 -0400 | |
|---|---|---|
| committer | 2019-10-05 09:14:23 -0400 | |
| commit | 8eb1398f8d90fb2813f438b9fffac716b6ec51d2 (patch) | |
| tree | 39a270023b64af2dce85e3d4a13484c95243b2b3 /src | |
| parent | video_core/ast: Supply const accessors for data where applicable (diff) | |
| download | yuzu-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.cpp | 20 | ||||
| -rw-r--r-- | src/video_core/shader/ast.h | 29 | ||||
| -rw-r--r-- | src/video_core/shader/expr.cpp | 31 | ||||
| -rw-r--r-- | src/video_core/shader/expr.h | 12 |
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 | ||
| 95 | void ASTZipper::DetachTail(const ASTNode node) { | 96 | void 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 | ||
| 414 | void ASTManager::InsertGoto(Expr condition, u32 address) { | 416 | void 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 | ||
| 421 | void ASTManager::InsertBlock(u32 start_address, u32 end_address) { | 423 | void 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 | ||
| 426 | void ASTManager::InsertReturn(Expr condition, bool kills) { | 428 | void 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 | ||
| 542 | bool ASTManager::IndirectlyRelated(ASTNode first, ASTNode second) { | 544 | bool 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 | ||
| 546 | bool ASTManager::DirectlyRelated(ASTNode first, ASTNode second) { | 548 | bool 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 | ||
| 72 | class ASTProgram { | 72 | class ASTProgram { |
| 73 | public: | 73 | public: |
| 74 | explicit ASTProgram() = default; | ||
| 75 | ASTZipper nodes{}; | 74 | ASTZipper nodes{}; |
| 76 | }; | 75 | }; |
| 77 | 76 | ||
| 78 | class ASTIfThen { | 77 | class ASTIfThen { |
| 79 | public: | 78 | public: |
| 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 | ||
| 85 | class ASTIfElse { | 84 | class ASTIfElse { |
| 86 | public: | 85 | public: |
| 87 | explicit ASTIfElse() = default; | ||
| 88 | ASTZipper nodes{}; | 86 | ASTZipper nodes{}; |
| 89 | }; | 87 | }; |
| 90 | 88 | ||
| @@ -103,7 +101,7 @@ public: | |||
| 103 | 101 | ||
| 104 | class ASTVarSet { | 102 | class ASTVarSet { |
| 105 | public: | 103 | public: |
| 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 | ||
| 118 | class ASTGoto { | 116 | class ASTGoto { |
| 119 | public: | 117 | public: |
| 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 | ||
| 125 | class ASTDoWhile { | 123 | class ASTDoWhile { |
| 126 | public: | 124 | public: |
| 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 | ||
| 132 | class ASTReturn { | 130 | class ASTReturn { |
| 133 | public: | 131 | public: |
| 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 | ||
| 139 | class ASTBreak { | 138 | class ASTBreak { |
| 140 | public: | 139 | public: |
| 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 | ||
| 145 | class ASTBase { | 144 | class ASTBase { |
| 146 | public: | 145 | public: |
| 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: | |||
| 370 | private: | 371 | private: |
| 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 | ||
| 12 | namespace VideoCommon::Shader { | 10 | namespace VideoCommon::Shader { |
| 11 | namespace { | ||
| 12 | bool ExprIsBoolean(const Expr& expr) { | ||
| 13 | return std::holds_alternative<ExprBoolean>(*expr); | ||
| 14 | } | ||
| 15 | |||
| 16 | bool ExprBooleanGet(const Expr& expr) { | ||
| 17 | return std::get_if<ExprBoolean>(expr.get())->value; | ||
| 18 | } | ||
| 19 | } // Anonymous namespace | ||
| 13 | 20 | ||
| 14 | bool ExprAnd::operator==(const ExprAnd& b) const { | 21 | bool 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 | ||
| 26 | bool ExprIsBoolean(Expr expr) { | ||
| 27 | return std::holds_alternative<ExprBoolean>(*expr); | ||
| 28 | } | ||
| 29 | |||
| 30 | bool ExprBooleanGet(Expr expr) { | ||
| 31 | return std::get_if<ExprBoolean>(expr.get())->value; | ||
| 32 | } | ||
| 33 | |||
| 34 | Expr MakeExprNot(Expr first) { | 33 | Expr 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 | ||
| 41 | Expr MakeExprAnd(Expr first, Expr second) { | 40 | Expr 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 | ||
| 51 | Expr MakeExprOr(Expr first, Expr second) { | 50 | Expr 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 | ||
| 61 | bool ExprAreEqual(Expr first, Expr second) { | 60 | bool ExprAreEqual(const Expr& first, const Expr& second) { |
| 62 | return (*first) == (*second); | 61 | return (*first) == (*second); |
| 63 | } | 62 | } |
| 64 | 63 | ||
| 65 | bool ExprAreOpposite(Expr first, Expr second) { | 64 | bool 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 | ||
| 75 | bool ExprIsTrue(Expr first) { | 74 | bool 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 | ||
| 29 | class ExprAnd final { | 29 | class ExprAnd final { |
| 30 | public: | 30 | public: |
| 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 | ||
| 39 | class ExprOr final { | 39 | class ExprOr final { |
| 40 | public: | 40 | public: |
| 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 | ||
| 49 | class ExprNot final { | 49 | class ExprNot final { |
| 50 | public: | 50 | public: |
| 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 | ||
| 108 | bool ExprAreEqual(Expr first, Expr second); | 108 | bool ExprAreEqual(const Expr& first, const Expr& second); |
| 109 | 109 | ||
| 110 | bool ExprAreOpposite(Expr first, Expr second); | 110 | bool ExprAreOpposite(const Expr& first, const Expr& second); |
| 111 | 111 | ||
| 112 | Expr MakeExprNot(Expr first); | 112 | Expr MakeExprNot(Expr first); |
| 113 | 113 | ||
| @@ -115,6 +115,6 @@ Expr MakeExprAnd(Expr first, Expr second); | |||
| 115 | 115 | ||
| 116 | Expr MakeExprOr(Expr first, Expr second); | 116 | Expr MakeExprOr(Expr first, Expr second); |
| 117 | 117 | ||
| 118 | bool ExprIsTrue(Expr first); | 118 | bool ExprIsTrue(const Expr& first); |
| 119 | 119 | ||
| 120 | } // namespace VideoCommon::Shader | 120 | } // namespace VideoCommon::Shader |