nir/builder: Emit better code for iadd/imul_imm
authorJason Ekstrand <jason.ekstrand@intel.com>
Wed, 6 Mar 2019 18:27:26 +0000 (12:27 -0600)
committerJason Ekstrand <jason@jlekstrand.net>
Thu, 7 Mar 2019 21:20:30 +0000 (21:20 +0000)
Because we already know the immediate right-hand parameter, we can
potentially save the optimizer a bit of work.

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
src/compiler/nir/nir_builder.h
src/compiler/spirv/vtn_variables.c

index 43edf0a9d36f0e247fe3db847de673e1c7e5ed76..42004a2fe54b33de294e4943c27a5da4b59b8282 100644 (file)
@@ -25,6 +25,7 @@
 #define NIR_BUILDER_H
 
 #include "nir_control_flow.h"
+#include "util/bitscan.h"
 #include "util/half_float.h"
 
 struct exec_list;
@@ -601,13 +602,33 @@ nir_u2u(nir_builder *build, nir_ssa_def *x, unsigned dest_bit_size)
 static inline nir_ssa_def *
 nir_iadd_imm(nir_builder *build, nir_ssa_def *x, uint64_t y)
 {
-   return nir_iadd(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   assert(x->bit_size <= 64);
+   if (x->bit_size < 64)
+      y &= (1ull << x->bit_size) - 1;
+
+   if (y == 0) {
+      return x;
+   } else {
+      return nir_iadd(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   }
 }
 
 static inline nir_ssa_def *
 nir_imul_imm(nir_builder *build, nir_ssa_def *x, uint64_t y)
 {
-   return nir_imul(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   assert(x->bit_size <= 64);
+   if (x->bit_size < 64)
+      y &= (1ull << x->bit_size) - 1;
+
+   if (y == 0) {
+      return nir_imm_intN_t(build, 0, x->bit_size);
+   } else if (y == 1) {
+      return x;
+   } else if (util_is_power_of_two_or_zero64(y)) {
+      return nir_ishl(build, x, nir_imm_int(build, ffsll(y) - 1));
+   } else {
+      return nir_imul(build, x, nir_imm_intN_t(build, y, x->bit_size));
+   }
 }
 
 static inline nir_ssa_def *
index d8a4407639f449ef730162f33658e0a42c5cbb7c..c3ce15aaaaf9d23f339b808aeeedc93bd51c4438 100644 (file)
@@ -79,9 +79,7 @@ vtn_access_link_as_ssa(struct vtn_builder *b, struct vtn_access_link link,
       nir_ssa_def *ssa = vtn_ssa_value(b, link.id)->def;
       if (ssa->bit_size != bit_size)
          ssa = nir_i2i(&b->nb, ssa, bit_size);
-      if (stride != 1)
-         ssa = nir_imul_imm(&b->nb, ssa, stride);
-      return ssa;
+      return nir_imul_imm(&b->nb, ssa, stride);
    }
 }