constant_propagation_pass.cpp 21 KB

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