Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nir_lower_to_source_mods.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Jason Ekstrand (jason@jlekstrand.net)
  25. *
  26. */
  27. #include "nir.h"
  28. /*
  29. * This pass lowers the neg, abs, and sat operations to source modifiers on
  30. * ALU operations to make things nicer for the backend. It's just much
  31. * easier to not have them when we're doing optimizations.
  32. */
  33. static bool
  34. nir_lower_to_source_mods_block(nir_block *block, void *state)
  35. {
  36. nir_foreach_instr(block, instr) {
  37. if (instr->type != nir_instr_type_alu)
  38. continue;
  39. nir_alu_instr *alu = nir_instr_as_alu(instr);
  40. for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
  41. if (!alu->src[i].src.is_ssa)
  42. continue;
  43. if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
  44. continue;
  45. nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
  46. if (parent->dest.saturate)
  47. continue;
  48. switch (nir_op_infos[alu->op].input_types[i]) {
  49. case nir_type_float:
  50. if (parent->op != nir_op_fmov)
  51. continue;
  52. break;
  53. case nir_type_int:
  54. if (parent->op != nir_op_imov)
  55. continue;
  56. break;
  57. default:
  58. continue;
  59. }
  60. /* We can only do a rewrite if the source we are copying is SSA.
  61. * Otherwise, moving the read might invalidly reorder reads/writes
  62. * on a register.
  63. */
  64. if (!parent->src[0].src.is_ssa)
  65. continue;
  66. nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
  67. if (alu->src[i].abs) {
  68. /* abs trumps both neg and abs, do nothing */
  69. } else {
  70. alu->src[i].negate = (alu->src[i].negate != parent->src[0].negate);
  71. alu->src[i].abs |= parent->src[0].abs;
  72. }
  73. for (int j = 0; j < 4; ++j) {
  74. if (!nir_alu_instr_channel_used(alu, i, j))
  75. continue;
  76. alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
  77. }
  78. if (list_empty(&parent->dest.dest.ssa.uses) &&
  79. list_empty(&parent->dest.dest.ssa.if_uses))
  80. nir_instr_remove(&parent->instr);
  81. }
  82. switch (alu->op) {
  83. case nir_op_fsat:
  84. alu->op = nir_op_fmov;
  85. alu->dest.saturate = true;
  86. break;
  87. case nir_op_ineg:
  88. alu->op = nir_op_imov;
  89. alu->src[0].negate = !alu->src[0].negate;
  90. break;
  91. case nir_op_fneg:
  92. alu->op = nir_op_fmov;
  93. alu->src[0].negate = !alu->src[0].negate;
  94. break;
  95. case nir_op_iabs:
  96. alu->op = nir_op_imov;
  97. alu->src[0].abs = true;
  98. alu->src[0].negate = false;
  99. break;
  100. case nir_op_fabs:
  101. alu->op = nir_op_fmov;
  102. alu->src[0].abs = true;
  103. alu->src[0].negate = false;
  104. break;
  105. default:
  106. break;
  107. }
  108. /* We've covered sources. Now we're going to try and saturate the
  109. * destination if we can.
  110. */
  111. if (!alu->dest.dest.is_ssa)
  112. continue;
  113. /* We can only saturate float destinations */
  114. if (nir_op_infos[alu->op].output_type != nir_type_float)
  115. continue;
  116. if (!list_empty(&alu->dest.dest.ssa.if_uses))
  117. continue;
  118. bool all_children_are_sat = true;
  119. nir_foreach_use(&alu->dest.dest.ssa, child_src) {
  120. assert(child_src->is_ssa);
  121. nir_instr *child = child_src->parent_instr;
  122. if (child->type != nir_instr_type_alu) {
  123. all_children_are_sat = false;
  124. continue;
  125. }
  126. nir_alu_instr *child_alu = nir_instr_as_alu(child);
  127. if (child_alu->src[0].negate || child_alu->src[0].abs) {
  128. all_children_are_sat = false;
  129. continue;
  130. }
  131. if (child_alu->op != nir_op_fsat &&
  132. !(child_alu->op == nir_op_fmov && child_alu->dest.saturate)) {
  133. all_children_are_sat = false;
  134. continue;
  135. }
  136. }
  137. if (!all_children_are_sat)
  138. continue;
  139. alu->dest.saturate = true;
  140. nir_foreach_use(&alu->dest.dest.ssa, child_src) {
  141. assert(child_src->is_ssa);
  142. nir_instr *child = child_src->parent_instr;
  143. assert(child->type == nir_instr_type_alu);
  144. nir_alu_instr *child_alu = nir_instr_as_alu(child);
  145. child_alu->op = nir_op_fmov;
  146. child_alu->dest.saturate = false;
  147. /* We could propagate the dest of our instruction to the
  148. * destinations of the uses here. However, one quick round of
  149. * copy propagation will clean that all up and then we don't have
  150. * the complexity.
  151. */
  152. }
  153. }
  154. return true;
  155. }
  156. static void
  157. nir_lower_to_source_mods_impl(nir_function_impl *impl)
  158. {
  159. nir_foreach_block(impl, nir_lower_to_source_mods_block, NULL);
  160. }
  161. void
  162. nir_lower_to_source_mods(nir_shader *shader)
  163. {
  164. nir_foreach_function(shader, function) {
  165. if (function->impl)
  166. nir_lower_to_source_mods_impl(function->impl);
  167. }
  168. }