constant_propagation_pass.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <tuple>
  6. #include <type_traits>
  7. #include "common/bit_cast.h"
  8. #include "common/bit_util.h"
  9. #include "shader_recompiler/exception.h"
  10. #include "shader_recompiler/frontend/ir/microinstruction.h"
  11. #include "shader_recompiler/ir_opt/passes.h"
  12. namespace Shader::Optimization {
  13. namespace {
  14. // Metaprogramming stuff to get arguments information out of a lambda
  15. template <typename Func>
  16. struct LambdaTraits : LambdaTraits<decltype(&std::remove_reference_t<Func>::operator())> {};
  17. template <typename ReturnType, typename LambdaType, typename... Args>
  18. struct LambdaTraits<ReturnType (LambdaType::*)(Args...) const> {
  19. template <size_t I>
  20. using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
  21. static constexpr size_t NUM_ARGS{sizeof...(Args)};
  22. };
  23. template <typename T>
  24. [[nodiscard]] T Arg(const IR::Value& value) {
  25. if constexpr (std::is_same_v<T, bool>) {
  26. return value.U1();
  27. } else if constexpr (std::is_same_v<T, u32>) {
  28. return value.U32();
  29. } else if constexpr (std::is_same_v<T, s32>) {
  30. return static_cast<s32>(value.U32());
  31. } else if constexpr (std::is_same_v<T, f32>) {
  32. return value.F32();
  33. } else if constexpr (std::is_same_v<T, u64>) {
  34. return value.U64();
  35. }
  36. }
  37. template <typename T, typename ImmFn>
  38. bool FoldCommutative(IR::Inst& inst, ImmFn&& imm_fn) {
  39. const IR::Value lhs{inst.Arg(0)};
  40. const IR::Value rhs{inst.Arg(1)};
  41. const bool is_lhs_immediate{lhs.IsImmediate()};
  42. const bool is_rhs_immediate{rhs.IsImmediate()};
  43. if (is_lhs_immediate && is_rhs_immediate) {
  44. const auto result{imm_fn(Arg<T>(lhs), Arg<T>(rhs))};
  45. inst.ReplaceUsesWith(IR::Value{result});
  46. return false;
  47. }
  48. if (is_lhs_immediate && !is_rhs_immediate) {
  49. IR::Inst* const rhs_inst{rhs.InstRecursive()};
  50. if (rhs_inst->Opcode() == inst.Opcode() && rhs_inst->Arg(1).IsImmediate()) {
  51. const auto combined{imm_fn(Arg<T>(lhs), Arg<T>(rhs_inst->Arg(1)))};
  52. inst.SetArg(0, rhs_inst->Arg(0));
  53. inst.SetArg(1, IR::Value{combined});
  54. } else {
  55. // Normalize
  56. inst.SetArg(0, rhs);
  57. inst.SetArg(1, lhs);
  58. }
  59. }
  60. if (!is_lhs_immediate && is_rhs_immediate) {
  61. const IR::Inst* const lhs_inst{lhs.InstRecursive()};
  62. if (lhs_inst->Opcode() == inst.Opcode() && lhs_inst->Arg(1).IsImmediate()) {
  63. const auto combined{imm_fn(Arg<T>(rhs), Arg<T>(lhs_inst->Arg(1)))};
  64. inst.SetArg(0, lhs_inst->Arg(0));
  65. inst.SetArg(1, IR::Value{combined});
  66. }
  67. }
  68. return true;
  69. }
  70. void FoldGetRegister(IR::Inst& inst) {
  71. if (inst.Arg(0).Reg() == IR::Reg::RZ) {
  72. inst.ReplaceUsesWith(IR::Value{u32{0}});
  73. }
  74. }
  75. void FoldGetPred(IR::Inst& inst) {
  76. if (inst.Arg(0).Pred() == IR::Pred::PT) {
  77. inst.ReplaceUsesWith(IR::Value{true});
  78. }
  79. }
  80. template <typename T>
  81. void FoldAdd(IR::Inst& inst) {
  82. if (inst.HasAssociatedPseudoOperation()) {
  83. return;
  84. }
  85. if (!FoldCommutative<T>(inst, [](T a, T b) { return a + b; })) {
  86. return;
  87. }
  88. const IR::Value rhs{inst.Arg(1)};
  89. if (rhs.IsImmediate() && Arg<T>(rhs) == 0) {
  90. inst.ReplaceUsesWith(inst.Arg(0));
  91. }
  92. }
  93. template <typename T>
  94. void FoldSelect(IR::Inst& inst) {
  95. const IR::Value cond{inst.Arg(0)};
  96. if (cond.IsImmediate()) {
  97. inst.ReplaceUsesWith(cond.U1() ? inst.Arg(1) : inst.Arg(2));
  98. }
  99. }
  100. void FoldLogicalAnd(IR::Inst& inst) {
  101. if (!FoldCommutative<bool>(inst, [](bool a, bool b) { return a && b; })) {
  102. return;
  103. }
  104. const IR::Value rhs{inst.Arg(1)};
  105. if (rhs.IsImmediate()) {
  106. if (rhs.U1()) {
  107. inst.ReplaceUsesWith(inst.Arg(0));
  108. } else {
  109. inst.ReplaceUsesWith(IR::Value{false});
  110. }
  111. }
  112. }
  113. void FoldLogicalOr(IR::Inst& inst) {
  114. if (!FoldCommutative<bool>(inst, [](bool a, bool b) { return a || b; })) {
  115. return;
  116. }
  117. const IR::Value rhs{inst.Arg(1)};
  118. if (rhs.IsImmediate()) {
  119. if (rhs.U1()) {
  120. inst.ReplaceUsesWith(IR::Value{true});
  121. } else {
  122. inst.ReplaceUsesWith(inst.Arg(0));
  123. }
  124. }
  125. }
  126. void FoldLogicalNot(IR::Inst& inst) {
  127. const IR::U1 value{inst.Arg(0)};
  128. if (value.IsImmediate()) {
  129. inst.ReplaceUsesWith(IR::Value{!value.U1()});
  130. return;
  131. }
  132. IR::Inst* const arg{value.InstRecursive()};
  133. if (arg->Opcode() == IR::Opcode::LogicalNot) {
  134. inst.ReplaceUsesWith(arg->Arg(0));
  135. }
  136. }
  137. template <typename Dest, typename Source>
  138. void FoldBitCast(IR::Inst& inst, IR::Opcode reverse) {
  139. const IR::Value value{inst.Arg(0)};
  140. if (value.IsImmediate()) {
  141. inst.ReplaceUsesWith(IR::Value{Common::BitCast<Dest>(Arg<Source>(value))});
  142. return;
  143. }
  144. IR::Inst* const arg_inst{value.InstRecursive()};
  145. if (value.InstRecursive()->Opcode() == reverse) {
  146. inst.ReplaceUsesWith(arg_inst->Arg(0));
  147. }
  148. }
  149. template <typename Func, size_t... I>
  150. IR::Value EvalImmediates(const IR::Inst& inst, Func&& func, std::index_sequence<I...>) {
  151. using Traits = LambdaTraits<decltype(func)>;
  152. return IR::Value{func(Arg<Traits::ArgType<I>>(inst.Arg(I))...)};
  153. }
  154. template <typename Func>
  155. void FoldWhenAllImmediates(IR::Inst& inst, Func&& func) {
  156. if (!inst.AreAllArgsImmediates() || inst.HasAssociatedPseudoOperation()) {
  157. return;
  158. }
  159. using Indices = std::make_index_sequence<LambdaTraits<decltype(func)>::NUM_ARGS>;
  160. inst.ReplaceUsesWith(EvalImmediates(inst, func, Indices{}));
  161. }
  162. void FoldBranchConditional(IR::Inst& inst) {
  163. const IR::U1 cond{inst.Arg(0)};
  164. if (cond.IsImmediate()) {
  165. // TODO: Convert to Branch
  166. return;
  167. }
  168. const IR::Inst* cond_inst{cond.InstRecursive()};
  169. if (cond_inst->Opcode() == IR::Opcode::LogicalNot) {
  170. const IR::Value true_label{inst.Arg(1)};
  171. const IR::Value false_label{inst.Arg(2)};
  172. // Remove negation on the conditional (take the parameter out of LogicalNot) and swap
  173. // the branches
  174. inst.SetArg(0, cond_inst->Arg(0));
  175. inst.SetArg(1, false_label);
  176. inst.SetArg(2, true_label);
  177. }
  178. }
  179. void ConstantPropagation(IR::Inst& inst) {
  180. switch (inst.Opcode()) {
  181. case IR::Opcode::GetRegister:
  182. return FoldGetRegister(inst);
  183. case IR::Opcode::GetPred:
  184. return FoldGetPred(inst);
  185. case IR::Opcode::IAdd32:
  186. return FoldAdd<u32>(inst);
  187. case IR::Opcode::BitCastF32U32:
  188. return FoldBitCast<f32, u32>(inst, IR::Opcode::BitCastU32F32);
  189. case IR::Opcode::BitCastU32F32:
  190. return FoldBitCast<u32, f32>(inst, IR::Opcode::BitCastF32U32);
  191. case IR::Opcode::IAdd64:
  192. return FoldAdd<u64>(inst);
  193. case IR::Opcode::Select32:
  194. return FoldSelect<u32>(inst);
  195. case IR::Opcode::LogicalAnd:
  196. return FoldLogicalAnd(inst);
  197. case IR::Opcode::LogicalOr:
  198. return FoldLogicalOr(inst);
  199. case IR::Opcode::LogicalNot:
  200. return FoldLogicalNot(inst);
  201. case IR::Opcode::SLessThan:
  202. return FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return a < b; });
  203. case IR::Opcode::ULessThan:
  204. return FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a < b; });
  205. case IR::Opcode::BitFieldUExtract:
  206. return FoldWhenAllImmediates(inst, [](u32 base, u32 shift, u32 count) {
  207. if (static_cast<size_t>(shift) + static_cast<size_t>(count) > Common::BitSize<u32>()) {
  208. throw LogicError("Undefined result in {}({}, {}, {})", IR::Opcode::BitFieldUExtract,
  209. base, shift, count);
  210. }
  211. return (base >> shift) & ((1U << count) - 1);
  212. });
  213. case IR::Opcode::BranchConditional:
  214. return FoldBranchConditional(inst);
  215. default:
  216. break;
  217. }
  218. }
  219. } // Anonymous namespace
  220. void ConstantPropagationPass(IR::Block& block) {
  221. std::ranges::for_each(block, ConstantPropagation);
  222. }
  223. } // namespace Shader::Optimization