constant_propagation_pass.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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/ir_emitter.h"
  11. #include "shader_recompiler/frontend/ir/value.h"
  12. #include "shader_recompiler/ir_opt/passes.h"
  13. namespace Shader::Optimization {
  14. namespace {
  15. // Metaprogramming stuff to get arguments information out of a lambda
  16. template <typename Func>
  17. struct LambdaTraits : LambdaTraits<decltype(&std::remove_reference_t<Func>::operator())> {};
  18. template <typename ReturnType, typename LambdaType, typename... Args>
  19. struct LambdaTraits<ReturnType (LambdaType::*)(Args...) const> {
  20. template <size_t I>
  21. using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
  22. static constexpr size_t NUM_ARGS{sizeof...(Args)};
  23. };
  24. template <typename T>
  25. [[nodiscard]] T Arg(const IR::Value& value) {
  26. if constexpr (std::is_same_v<T, bool>) {
  27. return value.U1();
  28. } else if constexpr (std::is_same_v<T, u32>) {
  29. return value.U32();
  30. } else if constexpr (std::is_same_v<T, s32>) {
  31. return static_cast<s32>(value.U32());
  32. } else if constexpr (std::is_same_v<T, f32>) {
  33. return value.F32();
  34. } else if constexpr (std::is_same_v<T, u64>) {
  35. return value.U64();
  36. }
  37. }
  38. template <typename T, typename ImmFn>
  39. bool FoldCommutative(IR::Inst& inst, ImmFn&& imm_fn) {
  40. const IR::Value lhs{inst.Arg(0)};
  41. const IR::Value rhs{inst.Arg(1)};
  42. const bool is_lhs_immediate{lhs.IsImmediate()};
  43. const bool is_rhs_immediate{rhs.IsImmediate()};
  44. if (is_lhs_immediate && is_rhs_immediate) {
  45. const auto result{imm_fn(Arg<T>(lhs), Arg<T>(rhs))};
  46. inst.ReplaceUsesWith(IR::Value{result});
  47. return false;
  48. }
  49. if (is_lhs_immediate && !is_rhs_immediate) {
  50. IR::Inst* const rhs_inst{rhs.InstRecursive()};
  51. if (rhs_inst->GetOpcode() == inst.GetOpcode() && rhs_inst->Arg(1).IsImmediate()) {
  52. const auto combined{imm_fn(Arg<T>(lhs), Arg<T>(rhs_inst->Arg(1)))};
  53. inst.SetArg(0, rhs_inst->Arg(0));
  54. inst.SetArg(1, IR::Value{combined});
  55. } else {
  56. // Normalize
  57. inst.SetArg(0, rhs);
  58. inst.SetArg(1, lhs);
  59. }
  60. }
  61. if (!is_lhs_immediate && is_rhs_immediate) {
  62. const IR::Inst* const lhs_inst{lhs.InstRecursive()};
  63. if (lhs_inst->GetOpcode() == inst.GetOpcode() && lhs_inst->Arg(1).IsImmediate()) {
  64. const auto combined{imm_fn(Arg<T>(rhs), Arg<T>(lhs_inst->Arg(1)))};
  65. inst.SetArg(0, lhs_inst->Arg(0));
  66. inst.SetArg(1, IR::Value{combined});
  67. }
  68. }
  69. return true;
  70. }
  71. template <typename Func>
  72. bool FoldWhenAllImmediates(IR::Inst& inst, Func&& func) {
  73. if (!inst.AreAllArgsImmediates() || inst.HasAssociatedPseudoOperation()) {
  74. return false;
  75. }
  76. using Indices = std::make_index_sequence<LambdaTraits<decltype(func)>::NUM_ARGS>;
  77. inst.ReplaceUsesWith(EvalImmediates(inst, func, Indices{}));
  78. return true;
  79. }
  80. void FoldGetRegister(IR::Inst& inst) {
  81. if (inst.Arg(0).Reg() == IR::Reg::RZ) {
  82. inst.ReplaceUsesWith(IR::Value{u32{0}});
  83. }
  84. }
  85. void FoldGetPred(IR::Inst& inst) {
  86. if (inst.Arg(0).Pred() == IR::Pred::PT) {
  87. inst.ReplaceUsesWith(IR::Value{true});
  88. }
  89. }
  90. /// Replaces the pattern generated by two XMAD multiplications
  91. bool FoldXmadMultiply(IR::Block& block, IR::Inst& inst) {
  92. /*
  93. * We are looking for this pattern:
  94. * %rhs_bfe = BitFieldUExtract %factor_a, #0, #16
  95. * %rhs_mul = IMul32 %rhs_bfe, %factor_b
  96. * %lhs_bfe = BitFieldUExtract %factor_a, #16, #16
  97. * %rhs_mul = IMul32 %lhs_bfe, %factor_b
  98. * %lhs_shl = ShiftLeftLogical32 %rhs_mul, #16
  99. * %result = IAdd32 %lhs_shl, %rhs_mul
  100. *
  101. * And replacing it with
  102. * %result = IMul32 %factor_a, %factor_b
  103. *
  104. * This optimization has been proven safe by LLVM and MSVC.
  105. */
  106. IR::Inst* const lhs_shl{inst.Arg(0).TryInstRecursive()};
  107. IR::Inst* const rhs_mul{inst.Arg(1).TryInstRecursive()};
  108. if (!lhs_shl || !rhs_mul) {
  109. return false;
  110. }
  111. if (lhs_shl->GetOpcode() != IR::Opcode::ShiftLeftLogical32 ||
  112. lhs_shl->Arg(1) != IR::Value{16U}) {
  113. return false;
  114. }
  115. IR::Inst* const lhs_mul{lhs_shl->Arg(0).TryInstRecursive()};
  116. if (!lhs_mul) {
  117. return false;
  118. }
  119. if (lhs_mul->GetOpcode() != IR::Opcode::IMul32 || rhs_mul->GetOpcode() != IR::Opcode::IMul32) {
  120. return false;
  121. }
  122. const IR::U32 factor_b{lhs_mul->Arg(1)};
  123. if (factor_b.Resolve() != rhs_mul->Arg(1).Resolve()) {
  124. return false;
  125. }
  126. IR::Inst* const lhs_bfe{lhs_mul->Arg(0).TryInstRecursive()};
  127. IR::Inst* const rhs_bfe{rhs_mul->Arg(0).TryInstRecursive()};
  128. if (!lhs_bfe || !rhs_bfe) {
  129. return false;
  130. }
  131. if (lhs_bfe->GetOpcode() != IR::Opcode::BitFieldUExtract) {
  132. return false;
  133. }
  134. if (rhs_bfe->GetOpcode() != IR::Opcode::BitFieldUExtract) {
  135. return false;
  136. }
  137. if (lhs_bfe->Arg(1) != IR::Value{16U} || lhs_bfe->Arg(2) != IR::Value{16U}) {
  138. return false;
  139. }
  140. if (rhs_bfe->Arg(1) != IR::Value{0U} || rhs_bfe->Arg(2) != IR::Value{16U}) {
  141. return false;
  142. }
  143. const IR::U32 factor_a{lhs_bfe->Arg(0)};
  144. if (factor_a.Resolve() != rhs_bfe->Arg(0).Resolve()) {
  145. return false;
  146. }
  147. IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
  148. inst.ReplaceUsesWith(ir.IMul(factor_a, factor_b));
  149. return true;
  150. }
  151. template <typename T>
  152. void FoldAdd(IR::Block& block, IR::Inst& inst) {
  153. if (inst.HasAssociatedPseudoOperation()) {
  154. return;
  155. }
  156. if (!FoldCommutative<T>(inst, [](T a, T b) { return a + b; })) {
  157. return;
  158. }
  159. const IR::Value rhs{inst.Arg(1)};
  160. if (rhs.IsImmediate() && Arg<T>(rhs) == 0) {
  161. inst.ReplaceUsesWith(inst.Arg(0));
  162. return;
  163. }
  164. if constexpr (std::is_same_v<T, u32>) {
  165. if (FoldXmadMultiply(block, inst)) {
  166. return;
  167. }
  168. }
  169. }
  170. void FoldISub32(IR::Inst& inst) {
  171. if (FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a - b; })) {
  172. return;
  173. }
  174. if (inst.Arg(0).IsImmediate() || inst.Arg(1).IsImmediate()) {
  175. return;
  176. }
  177. // ISub32 is generally used to subtract two constant buffers, compare and replace this with
  178. // zero if they equal.
  179. const auto equal_cbuf{[](IR::Inst* a, IR::Inst* b) {
  180. return a->GetOpcode() == IR::Opcode::GetCbufU32 &&
  181. b->GetOpcode() == IR::Opcode::GetCbufU32 && a->Arg(0) == b->Arg(0) &&
  182. a->Arg(1) == b->Arg(1);
  183. }};
  184. IR::Inst* op_a{inst.Arg(0).InstRecursive()};
  185. IR::Inst* op_b{inst.Arg(1).InstRecursive()};
  186. if (equal_cbuf(op_a, op_b)) {
  187. inst.ReplaceUsesWith(IR::Value{u32{0}});
  188. return;
  189. }
  190. // It's also possible a value is being added to a cbuf and then subtracted
  191. if (op_b->GetOpcode() == IR::Opcode::IAdd32) {
  192. // Canonicalize local variables to simplify the following logic
  193. std::swap(op_a, op_b);
  194. }
  195. if (op_b->GetOpcode() != IR::Opcode::GetCbufU32) {
  196. return;
  197. }
  198. IR::Inst* const inst_cbuf{op_b};
  199. if (op_a->GetOpcode() != IR::Opcode::IAdd32) {
  200. return;
  201. }
  202. IR::Value add_op_a{op_a->Arg(0)};
  203. IR::Value add_op_b{op_a->Arg(1)};
  204. if (add_op_b.IsImmediate()) {
  205. // Canonicalize
  206. std::swap(add_op_a, add_op_b);
  207. }
  208. if (add_op_b.IsImmediate()) {
  209. return;
  210. }
  211. IR::Inst* const add_cbuf{add_op_b.InstRecursive()};
  212. if (equal_cbuf(add_cbuf, inst_cbuf)) {
  213. inst.ReplaceUsesWith(add_op_a);
  214. }
  215. }
  216. void FoldSelect(IR::Inst& inst) {
  217. const IR::Value cond{inst.Arg(0)};
  218. if (cond.IsImmediate()) {
  219. inst.ReplaceUsesWith(cond.U1() ? inst.Arg(1) : inst.Arg(2));
  220. }
  221. }
  222. void FoldFPMul32(IR::Inst& inst) {
  223. const auto control{inst.Flags<IR::FpControl>()};
  224. if (control.no_contraction) {
  225. return;
  226. }
  227. // Fold interpolation operations
  228. const IR::Value lhs_value{inst.Arg(0)};
  229. const IR::Value rhs_value{inst.Arg(1)};
  230. if (lhs_value.IsImmediate() || rhs_value.IsImmediate()) {
  231. return;
  232. }
  233. IR::Inst* const lhs_op{lhs_value.InstRecursive()};
  234. IR::Inst* const rhs_op{rhs_value.InstRecursive()};
  235. if (lhs_op->GetOpcode() != IR::Opcode::FPMul32 ||
  236. rhs_op->GetOpcode() != IR::Opcode::FPRecip32) {
  237. return;
  238. }
  239. const IR::Value recip_source{rhs_op->Arg(0)};
  240. const IR::Value lhs_mul_source{lhs_op->Arg(1).Resolve()};
  241. if (recip_source.IsImmediate() || lhs_mul_source.IsImmediate()) {
  242. return;
  243. }
  244. IR::Inst* const attr_a{recip_source.InstRecursive()};
  245. IR::Inst* const attr_b{lhs_mul_source.InstRecursive()};
  246. if (attr_a->GetOpcode() != IR::Opcode::GetAttribute ||
  247. attr_b->GetOpcode() != IR::Opcode::GetAttribute) {
  248. return;
  249. }
  250. if (attr_a->Arg(0).Attribute() == attr_b->Arg(0).Attribute()) {
  251. inst.ReplaceUsesWith(lhs_op->Arg(0));
  252. }
  253. }
  254. void FoldLogicalAnd(IR::Inst& inst) {
  255. if (!FoldCommutative<bool>(inst, [](bool a, bool b) { return a && b; })) {
  256. return;
  257. }
  258. const IR::Value rhs{inst.Arg(1)};
  259. if (rhs.IsImmediate()) {
  260. if (rhs.U1()) {
  261. inst.ReplaceUsesWith(inst.Arg(0));
  262. } else {
  263. inst.ReplaceUsesWith(IR::Value{false});
  264. }
  265. }
  266. }
  267. void FoldLogicalOr(IR::Inst& inst) {
  268. if (!FoldCommutative<bool>(inst, [](bool a, bool b) { return a || b; })) {
  269. return;
  270. }
  271. const IR::Value rhs{inst.Arg(1)};
  272. if (rhs.IsImmediate()) {
  273. if (rhs.U1()) {
  274. inst.ReplaceUsesWith(IR::Value{true});
  275. } else {
  276. inst.ReplaceUsesWith(inst.Arg(0));
  277. }
  278. }
  279. }
  280. void FoldLogicalNot(IR::Inst& inst) {
  281. const IR::U1 value{inst.Arg(0)};
  282. if (value.IsImmediate()) {
  283. inst.ReplaceUsesWith(IR::Value{!value.U1()});
  284. return;
  285. }
  286. IR::Inst* const arg{value.InstRecursive()};
  287. if (arg->GetOpcode() == IR::Opcode::LogicalNot) {
  288. inst.ReplaceUsesWith(arg->Arg(0));
  289. }
  290. }
  291. template <IR::Opcode op, typename Dest, typename Source>
  292. void FoldBitCast(IR::Inst& inst, IR::Opcode reverse) {
  293. const IR::Value value{inst.Arg(0)};
  294. if (value.IsImmediate()) {
  295. inst.ReplaceUsesWith(IR::Value{Common::BitCast<Dest>(Arg<Source>(value))});
  296. return;
  297. }
  298. IR::Inst* const arg_inst{value.InstRecursive()};
  299. if (arg_inst->GetOpcode() == reverse) {
  300. inst.ReplaceUsesWith(arg_inst->Arg(0));
  301. return;
  302. }
  303. if constexpr (op == IR::Opcode::BitCastF32U32) {
  304. if (arg_inst->GetOpcode() == IR::Opcode::GetCbufU32) {
  305. // Replace the bitcast with a typed constant buffer read
  306. inst.ReplaceOpcode(IR::Opcode::GetCbufF32);
  307. inst.SetArg(0, arg_inst->Arg(0));
  308. inst.SetArg(1, arg_inst->Arg(1));
  309. return;
  310. }
  311. }
  312. }
  313. void FoldInverseFunc(IR::Inst& inst, IR::Opcode reverse) {
  314. const IR::Value value{inst.Arg(0)};
  315. if (value.IsImmediate()) {
  316. return;
  317. }
  318. IR::Inst* const arg_inst{value.InstRecursive()};
  319. if (arg_inst->GetOpcode() == reverse) {
  320. inst.ReplaceUsesWith(arg_inst->Arg(0));
  321. return;
  322. }
  323. }
  324. template <typename Func, size_t... I>
  325. IR::Value EvalImmediates(const IR::Inst& inst, Func&& func, std::index_sequence<I...>) {
  326. using Traits = LambdaTraits<decltype(func)>;
  327. return IR::Value{func(Arg<typename Traits::template ArgType<I>>(inst.Arg(I))...)};
  328. }
  329. std::optional<IR::Value> FoldCompositeExtractImpl(IR::Value inst_value, IR::Opcode insert,
  330. IR::Opcode construct, u32 first_index) {
  331. IR::Inst* const inst{inst_value.InstRecursive()};
  332. if (inst->GetOpcode() == construct) {
  333. return inst->Arg(first_index);
  334. }
  335. if (inst->GetOpcode() != insert) {
  336. return std::nullopt;
  337. }
  338. IR::Value value_index{inst->Arg(2)};
  339. if (!value_index.IsImmediate()) {
  340. return std::nullopt;
  341. }
  342. const u32 second_index{value_index.U32()};
  343. if (first_index != second_index) {
  344. IR::Value value_composite{inst->Arg(0)};
  345. if (value_composite.IsImmediate()) {
  346. return std::nullopt;
  347. }
  348. return FoldCompositeExtractImpl(value_composite, insert, construct, first_index);
  349. }
  350. return inst->Arg(1);
  351. }
  352. void FoldCompositeExtract(IR::Inst& inst, IR::Opcode construct, IR::Opcode insert) {
  353. const IR::Value value_1{inst.Arg(0)};
  354. const IR::Value value_2{inst.Arg(1)};
  355. if (value_1.IsImmediate()) {
  356. return;
  357. }
  358. if (!value_2.IsImmediate()) {
  359. return;
  360. }
  361. const u32 first_index{value_2.U32()};
  362. const std::optional result{FoldCompositeExtractImpl(value_1, insert, construct, first_index)};
  363. if (!result) {
  364. return;
  365. }
  366. inst.ReplaceUsesWith(*result);
  367. }
  368. IR::Value GetThroughCast(IR::Value value, IR::Opcode expected_cast) {
  369. if (value.IsImmediate()) {
  370. return value;
  371. }
  372. IR::Inst* const inst{value.InstRecursive()};
  373. if (inst->GetOpcode() == expected_cast) {
  374. return inst->Arg(0).Resolve();
  375. }
  376. return value;
  377. }
  378. void FoldFSwizzleAdd(IR::Block& block, IR::Inst& inst) {
  379. const IR::Value swizzle{inst.Arg(2)};
  380. if (!swizzle.IsImmediate()) {
  381. return;
  382. }
  383. const IR::Value value_1{GetThroughCast(inst.Arg(0).Resolve(), IR::Opcode::BitCastF32U32)};
  384. const IR::Value value_2{GetThroughCast(inst.Arg(1).Resolve(), IR::Opcode::BitCastF32U32)};
  385. if (value_1.IsImmediate()) {
  386. return;
  387. }
  388. const u32 swizzle_value{swizzle.U32()};
  389. if (swizzle_value != 0x99 && swizzle_value != 0xA5) {
  390. return;
  391. }
  392. IR::Inst* const inst2{value_1.InstRecursive()};
  393. if (inst2->GetOpcode() != IR::Opcode::ShuffleButterfly) {
  394. return;
  395. }
  396. const IR::Value value_3{GetThroughCast(inst2->Arg(0).Resolve(), IR::Opcode::BitCastU32F32)};
  397. if (value_2 != value_3) {
  398. return;
  399. }
  400. const IR::Value index{inst2->Arg(1)};
  401. const IR::Value clamp{inst2->Arg(2)};
  402. const IR::Value segmentation_mask{inst2->Arg(3)};
  403. if (!index.IsImmediate() || !clamp.IsImmediate() || !segmentation_mask.IsImmediate()) {
  404. return;
  405. }
  406. if (clamp.U32() != 3 || segmentation_mask.U32() != 28) {
  407. return;
  408. }
  409. if (swizzle_value == 0x99) {
  410. // DPdxFine
  411. if (index.U32() == 1) {
  412. IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
  413. inst.ReplaceUsesWith(ir.DPdxFine(IR::F32{inst.Arg(1)}));
  414. }
  415. } else if (swizzle_value == 0xA5) {
  416. // DPdyFine
  417. if (index.U32() == 2) {
  418. IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
  419. inst.ReplaceUsesWith(ir.DPdyFine(IR::F32{inst.Arg(1)}));
  420. }
  421. }
  422. }
  423. void ConstantPropagation(IR::Block& block, IR::Inst& inst) {
  424. switch (inst.GetOpcode()) {
  425. case IR::Opcode::GetRegister:
  426. return FoldGetRegister(inst);
  427. case IR::Opcode::GetPred:
  428. return FoldGetPred(inst);
  429. case IR::Opcode::IAdd32:
  430. return FoldAdd<u32>(block, inst);
  431. case IR::Opcode::ISub32:
  432. return FoldISub32(inst);
  433. case IR::Opcode::IMul32:
  434. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a * b; });
  435. return;
  436. case IR::Opcode::ShiftRightArithmetic32:
  437. FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return static_cast<u32>(a >> b); });
  438. return;
  439. case IR::Opcode::BitCastF32U32:
  440. return FoldBitCast<IR::Opcode::BitCastF32U32, f32, u32>(inst, IR::Opcode::BitCastU32F32);
  441. case IR::Opcode::BitCastU32F32:
  442. return FoldBitCast<IR::Opcode::BitCastU32F32, u32, f32>(inst, IR::Opcode::BitCastF32U32);
  443. case IR::Opcode::IAdd64:
  444. return FoldAdd<u64>(block, inst);
  445. case IR::Opcode::PackHalf2x16:
  446. return FoldInverseFunc(inst, IR::Opcode::UnpackHalf2x16);
  447. case IR::Opcode::UnpackHalf2x16:
  448. return FoldInverseFunc(inst, IR::Opcode::PackHalf2x16);
  449. case IR::Opcode::SelectU1:
  450. case IR::Opcode::SelectU8:
  451. case IR::Opcode::SelectU16:
  452. case IR::Opcode::SelectU32:
  453. case IR::Opcode::SelectU64:
  454. case IR::Opcode::SelectF16:
  455. case IR::Opcode::SelectF32:
  456. case IR::Opcode::SelectF64:
  457. return FoldSelect(inst);
  458. case IR::Opcode::FPMul32:
  459. return FoldFPMul32(inst);
  460. case IR::Opcode::LogicalAnd:
  461. return FoldLogicalAnd(inst);
  462. case IR::Opcode::LogicalOr:
  463. return FoldLogicalOr(inst);
  464. case IR::Opcode::LogicalNot:
  465. return FoldLogicalNot(inst);
  466. case IR::Opcode::SLessThan:
  467. FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return a < b; });
  468. return;
  469. case IR::Opcode::ULessThan:
  470. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a < b; });
  471. return;
  472. case IR::Opcode::SLessThanEqual:
  473. FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return a <= b; });
  474. return;
  475. case IR::Opcode::ULessThanEqual:
  476. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a <= b; });
  477. return;
  478. case IR::Opcode::SGreaterThan:
  479. FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return a > b; });
  480. return;
  481. case IR::Opcode::UGreaterThan:
  482. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a > b; });
  483. return;
  484. case IR::Opcode::SGreaterThanEqual:
  485. FoldWhenAllImmediates(inst, [](s32 a, s32 b) { return a >= b; });
  486. return;
  487. case IR::Opcode::UGreaterThanEqual:
  488. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a >= b; });
  489. return;
  490. case IR::Opcode::IEqual:
  491. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a == b; });
  492. return;
  493. case IR::Opcode::INotEqual:
  494. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a != b; });
  495. return;
  496. case IR::Opcode::BitwiseAnd32:
  497. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a & b; });
  498. return;
  499. case IR::Opcode::BitwiseOr32:
  500. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a | b; });
  501. return;
  502. case IR::Opcode::BitwiseXor32:
  503. FoldWhenAllImmediates(inst, [](u32 a, u32 b) { return a ^ b; });
  504. return;
  505. case IR::Opcode::BitFieldUExtract:
  506. FoldWhenAllImmediates(inst, [](u32 base, u32 shift, u32 count) {
  507. if (static_cast<size_t>(shift) + static_cast<size_t>(count) > 32) {
  508. throw LogicError("Undefined result in {}({}, {}, {})", IR::Opcode::BitFieldUExtract,
  509. base, shift, count);
  510. }
  511. return (base >> shift) & ((1U << count) - 1);
  512. });
  513. return;
  514. case IR::Opcode::BitFieldSExtract:
  515. FoldWhenAllImmediates(inst, [](s32 base, u32 shift, u32 count) {
  516. const size_t back_shift{static_cast<size_t>(shift) + static_cast<size_t>(count)};
  517. const size_t left_shift{32 - back_shift};
  518. const size_t right_shift{static_cast<size_t>(32 - count)};
  519. if (back_shift > 32 || left_shift >= 32 || right_shift >= 32) {
  520. throw LogicError("Undefined result in {}({}, {}, {})", IR::Opcode::BitFieldSExtract,
  521. base, shift, count);
  522. }
  523. return static_cast<u32>((base << left_shift) >> right_shift);
  524. });
  525. return;
  526. case IR::Opcode::BitFieldInsert:
  527. FoldWhenAllImmediates(inst, [](u32 base, u32 insert, u32 offset, u32 bits) {
  528. if (bits >= 32 || offset >= 32) {
  529. throw LogicError("Undefined result in {}({}, {}, {}, {})",
  530. IR::Opcode::BitFieldInsert, base, insert, offset, bits);
  531. }
  532. return (base & ~(~(~0u << bits) << offset)) | (insert << offset);
  533. });
  534. return;
  535. case IR::Opcode::CompositeExtractU32x2:
  536. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructU32x2,
  537. IR::Opcode::CompositeInsertU32x2);
  538. case IR::Opcode::CompositeExtractU32x3:
  539. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructU32x3,
  540. IR::Opcode::CompositeInsertU32x3);
  541. case IR::Opcode::CompositeExtractU32x4:
  542. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructU32x4,
  543. IR::Opcode::CompositeInsertU32x4);
  544. case IR::Opcode::CompositeExtractF32x2:
  545. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructF32x2,
  546. IR::Opcode::CompositeInsertF32x2);
  547. case IR::Opcode::CompositeExtractF32x3:
  548. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructF32x3,
  549. IR::Opcode::CompositeInsertF32x3);
  550. case IR::Opcode::CompositeExtractF32x4:
  551. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructF32x4,
  552. IR::Opcode::CompositeInsertF32x4);
  553. case IR::Opcode::CompositeExtractF16x2:
  554. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructF16x2,
  555. IR::Opcode::CompositeInsertF16x2);
  556. case IR::Opcode::CompositeExtractF16x3:
  557. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructF16x3,
  558. IR::Opcode::CompositeInsertF16x3);
  559. case IR::Opcode::CompositeExtractF16x4:
  560. return FoldCompositeExtract(inst, IR::Opcode::CompositeConstructF16x4,
  561. IR::Opcode::CompositeInsertF16x4);
  562. case IR::Opcode::FSwizzleAdd:
  563. return FoldFSwizzleAdd(block, inst);
  564. default:
  565. break;
  566. }
  567. }
  568. } // Anonymous namespace
  569. void ConstantPropagationPass(IR::Program& program) {
  570. const auto end{program.post_order_blocks.rend()};
  571. for (auto it = program.post_order_blocks.rbegin(); it != end; ++it) {
  572. IR::Block* const block{*it};
  573. for (IR::Inst& inst : block->Instructions()) {
  574. ConstantPropagation(*block, inst);
  575. }
  576. }
  577. }
  578. } // namespace Shader::Optimization