summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/ir_opt
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/ir_opt')
-rw-r--r--src/shader_recompiler/ir_opt/constant_propagation_pass.cpp27
-rw-r--r--src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp113
2 files changed, 70 insertions, 70 deletions
diff --git a/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp b/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp
index 9fba6ac23..cbde65b9b 100644
--- a/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp
+++ b/src/shader_recompiler/ir_opt/constant_propagation_pass.cpp
@@ -32,6 +32,8 @@ template <typename T>
32 return value.U1(); 32 return value.U1();
33 } else if constexpr (std::is_same_v<T, u32>) { 33 } else if constexpr (std::is_same_v<T, u32>) {
34 return value.U32(); 34 return value.U32();
35 } else if constexpr (std::is_same_v<T, s32>) {
36 return static_cast<s32>(value.U32());
35 } else if constexpr (std::is_same_v<T, f32>) { 37 } else if constexpr (std::is_same_v<T, f32>) {
36 return value.F32(); 38 return value.F32();
37 } else if constexpr (std::is_same_v<T, u64>) { 39 } else if constexpr (std::is_same_v<T, u64>) {
@@ -39,17 +41,8 @@ template <typename T>
39 } 41 }
40} 42}
41 43
42template <typename ImmFn> 44template <typename T, typename ImmFn>
43bool FoldCommutative(IR::Inst& inst, ImmFn&& imm_fn) { 45bool FoldCommutative(IR::Inst& inst, ImmFn&& imm_fn) {
44 const auto arg = [](const IR::Value& value) {
45 if constexpr (std::is_invocable_r_v<bool, ImmFn, bool, bool>) {
46 return value.U1();
47 } else if constexpr (std::is_invocable_r_v<u32, ImmFn, u32, u32>) {
48 return value.U32();
49 } else if constexpr (std::is_invocable_r_v<u64, ImmFn, u64, u64>) {
50 return value.U64();
51 }
52 };
53 const IR::Value lhs{inst.Arg(0)}; 46 const IR::Value lhs{inst.Arg(0)};
54 const IR::Value rhs{inst.Arg(1)}; 47 const IR::Value rhs{inst.Arg(1)};
55 48
@@ -57,14 +50,14 @@ bool FoldCommutative(IR::Inst& inst, ImmFn&& imm_fn) {
57 const bool is_rhs_immediate{rhs.IsImmediate()}; 50 const bool is_rhs_immediate{rhs.IsImmediate()};
58 51
59 if (is_lhs_immediate && is_rhs_immediate) { 52 if (is_lhs_immediate && is_rhs_immediate) {
60 const auto result{imm_fn(arg(lhs), arg(rhs))}; 53 const auto result{imm_fn(Arg<T>(lhs), Arg<T>(rhs))};
61 inst.ReplaceUsesWith(IR::Value{result}); 54 inst.ReplaceUsesWith(IR::Value{result});
62 return false; 55 return false;
63 } 56 }
64 if (is_lhs_immediate && !is_rhs_immediate) { 57 if (is_lhs_immediate && !is_rhs_immediate) {
65 IR::Inst* const rhs_inst{rhs.InstRecursive()}; 58 IR::Inst* const rhs_inst{rhs.InstRecursive()};
66 if (rhs_inst->Opcode() == inst.Opcode() && rhs_inst->Arg(1).IsImmediate()) { 59 if (rhs_inst->Opcode() == inst.Opcode() && rhs_inst->Arg(1).IsImmediate()) {
67 const auto combined{imm_fn(arg(lhs), arg(rhs_inst->Arg(1)))}; 60 const auto combined{imm_fn(Arg<T>(lhs), Arg<T>(rhs_inst->Arg(1)))};
68 inst.SetArg(0, rhs_inst->Arg(0)); 61 inst.SetArg(0, rhs_inst->Arg(0));
69 inst.SetArg(1, IR::Value{combined}); 62 inst.SetArg(1, IR::Value{combined});
70 } else { 63 } else {
@@ -76,7 +69,7 @@ bool FoldCommutative(IR::Inst& inst, ImmFn&& imm_fn) {
76 if (!is_lhs_immediate && is_rhs_immediate) { 69 if (!is_lhs_immediate && is_rhs_immediate) {
77 const IR::Inst* const lhs_inst{lhs.InstRecursive()}; 70 const IR::Inst* const lhs_inst{lhs.InstRecursive()};
78 if (lhs_inst->Opcode() == inst.Opcode() && lhs_inst->Arg(1).IsImmediate()) { 71 if (lhs_inst->Opcode() == inst.Opcode() && lhs_inst->Arg(1).IsImmediate()) {
79 const auto combined{imm_fn(arg(rhs), arg(lhs_inst->Arg(1)))}; 72 const auto combined{imm_fn(Arg<T>(rhs), Arg<T>(lhs_inst->Arg(1)))};
80 inst.SetArg(0, lhs_inst->Arg(0)); 73 inst.SetArg(0, lhs_inst->Arg(0));
81 inst.SetArg(1, IR::Value{combined}); 74 inst.SetArg(1, IR::Value{combined});
82 } 75 }
@@ -101,7 +94,7 @@ void FoldAdd(IR::Inst& inst) {
101 if (inst.HasAssociatedPseudoOperation()) { 94 if (inst.HasAssociatedPseudoOperation()) {
102 return; 95 return;
103 } 96 }
104 if (!FoldCommutative(inst, [](T a, T b) { return a + b; })) { 97 if (!FoldCommutative<T>(inst, [](T a, T b) { return a + b; })) {
105 return; 98 return;
106 } 99 }
107 const IR::Value rhs{inst.Arg(1)}; 100 const IR::Value rhs{inst.Arg(1)};
@@ -119,7 +112,7 @@ void FoldSelect(IR::Inst& inst) {
119} 112}
120 113
121void FoldLogicalAnd(IR::Inst& inst) { 114void FoldLogicalAnd(IR::Inst& inst) {
122 if (!FoldCommutative(inst, [](bool a, bool b) { return a && b; })) { 115 if (!FoldCommutative<bool>(inst, [](bool a, bool b) { return a && b; })) {
123 return; 116 return;
124 } 117 }
125 const IR::Value rhs{inst.Arg(1)}; 118 const IR::Value rhs{inst.Arg(1)};
@@ -133,7 +126,7 @@ void FoldLogicalAnd(IR::Inst& inst) {
133} 126}
134 127
135void FoldLogicalOr(IR::Inst& inst) { 128void FoldLogicalOr(IR::Inst& inst) {
136 if (!FoldCommutative(inst, [](bool a, bool b) { return a || b; })) { 129 if (!FoldCommutative<bool>(inst, [](bool a, bool b) { return a || b; })) {
137 return; 130 return;
138 } 131 }
139 const IR::Value rhs{inst.Arg(1)}; 132 const IR::Value rhs{inst.Arg(1)};
@@ -226,6 +219,8 @@ void ConstantPropagation(IR::Inst& inst) {
226 return FoldLogicalOr(inst); 219 return FoldLogicalOr(inst);
227 case IR::Opcode::LogicalNot: 220 case IR::Opcode::LogicalNot:
228 return FoldLogicalNot(inst); 221 return FoldLogicalNot(inst);
222 case IR::Opcode::SLessThan:
223 return FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return a < b; });
229 case IR::Opcode::ULessThan: 224 case IR::Opcode::ULessThan:
230 return FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a < b; }); 225 return FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a < b; });
231 case IR::Opcode::BitFieldUExtract: 226 case IR::Opcode::BitFieldUExtract:
diff --git a/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp b/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
index 8ca996e93..7eaf719c4 100644
--- a/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
+++ b/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
@@ -113,6 +113,7 @@ private:
113 IR::Value ReadVariableRecursive(auto variable, IR::Block* block) { 113 IR::Value ReadVariableRecursive(auto variable, IR::Block* block) {
114 IR::Value val; 114 IR::Value val;
115 if (const std::span preds{block->ImmediatePredecessors()}; preds.size() == 1) { 115 if (const std::span preds{block->ImmediatePredecessors()}; preds.size() == 1) {
116 // Optimize the common case of one predecessor: no phi needed
116 val = ReadVariable(variable, preds.front()); 117 val = ReadVariable(variable, preds.front());
117 } else { 118 } else {
118 // Break potential cycles with operandless phi 119 // Break potential cycles with operandless phi
@@ -160,66 +161,70 @@ private:
160 161
161 DefTable current_def; 162 DefTable current_def;
162}; 163};
164
165void VisitInst(Pass& pass, IR::Block* block, IR::Inst& inst) {
166 switch (inst.Opcode()) {
167 case IR::Opcode::SetRegister:
168 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
169 pass.WriteVariable(reg, block, inst.Arg(1));
170 }
171 break;
172 case IR::Opcode::SetPred:
173 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
174 pass.WriteVariable(pred, block, inst.Arg(1));
175 }
176 break;
177 case IR::Opcode::SetGotoVariable:
178 pass.WriteVariable(GotoVariable{inst.Arg(0).U32()}, block, inst.Arg(1));
179 break;
180 case IR::Opcode::SetZFlag:
181 pass.WriteVariable(ZeroFlagTag{}, block, inst.Arg(0));
182 break;
183 case IR::Opcode::SetSFlag:
184 pass.WriteVariable(SignFlagTag{}, block, inst.Arg(0));
185 break;
186 case IR::Opcode::SetCFlag:
187 pass.WriteVariable(CarryFlagTag{}, block, inst.Arg(0));
188 break;
189 case IR::Opcode::SetOFlag:
190 pass.WriteVariable(OverflowFlagTag{}, block, inst.Arg(0));
191 break;
192 case IR::Opcode::GetRegister:
193 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
194 inst.ReplaceUsesWith(pass.ReadVariable(reg, block));
195 }
196 break;
197 case IR::Opcode::GetPred:
198 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
199 inst.ReplaceUsesWith(pass.ReadVariable(pred, block));
200 }
201 break;
202 case IR::Opcode::GetGotoVariable:
203 inst.ReplaceUsesWith(pass.ReadVariable(GotoVariable{inst.Arg(0).U32()}, block));
204 break;
205 case IR::Opcode::GetZFlag:
206 inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block));
207 break;
208 case IR::Opcode::GetSFlag:
209 inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block));
210 break;
211 case IR::Opcode::GetCFlag:
212 inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block));
213 break;
214 case IR::Opcode::GetOFlag:
215 inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block));
216 break;
217 default:
218 break;
219 }
220}
163} // Anonymous namespace 221} // Anonymous namespace
164 222
165void SsaRewritePass(IR::Function& function) { 223void SsaRewritePass(IR::Function& function) {
166 Pass pass; 224 Pass pass;
167 for (IR::Block* const block : function.blocks) { 225 for (IR::Block* const block : function.blocks) {
168 for (IR::Inst& inst : block->Instructions()) { 226 for (IR::Inst& inst : block->Instructions()) {
169 switch (inst.Opcode()) { 227 VisitInst(pass, block, inst);
170 case IR::Opcode::SetRegister:
171 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
172 pass.WriteVariable(reg, block, inst.Arg(1));
173 }
174 break;
175 case IR::Opcode::SetPred:
176 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
177 pass.WriteVariable(pred, block, inst.Arg(1));
178 }
179 break;
180 case IR::Opcode::SetGotoVariable:
181 pass.WriteVariable(GotoVariable{inst.Arg(0).U32()}, block, inst.Arg(1));
182 break;
183 case IR::Opcode::SetZFlag:
184 pass.WriteVariable(ZeroFlagTag{}, block, inst.Arg(0));
185 break;
186 case IR::Opcode::SetSFlag:
187 pass.WriteVariable(SignFlagTag{}, block, inst.Arg(0));
188 break;
189 case IR::Opcode::SetCFlag:
190 pass.WriteVariable(CarryFlagTag{}, block, inst.Arg(0));
191 break;
192 case IR::Opcode::SetOFlag:
193 pass.WriteVariable(OverflowFlagTag{}, block, inst.Arg(0));
194 break;
195 case IR::Opcode::GetRegister:
196 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
197 inst.ReplaceUsesWith(pass.ReadVariable(reg, block));
198 }
199 break;
200 case IR::Opcode::GetPred:
201 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
202 inst.ReplaceUsesWith(pass.ReadVariable(pred, block));
203 }
204 break;
205 case IR::Opcode::GetGotoVariable:
206 inst.ReplaceUsesWith(pass.ReadVariable(GotoVariable{inst.Arg(0).U32()}, block));
207 break;
208 case IR::Opcode::GetZFlag:
209 inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block));
210 break;
211 case IR::Opcode::GetSFlag:
212 inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block));
213 break;
214 case IR::Opcode::GetCFlag:
215 inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block));
216 break;
217 case IR::Opcode::GetOFlag:
218 inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block));
219 break;
220 default:
221 break;
222 }
223 } 228 }
224 } 229 }
225} 230}