This is another issue spotted by LLM review of assembly code for x264

The basic problem is we have gimple like this:
  _1 = x_22(D) + -1;
  _2 = _1 << 2;
  _3 = (long unsigned int) _2;
  _4 = _3 * 2;
  _5 = p_23(D) + _4;
  _6 = *_5;
  _7 = (int) _6;
  _8 = x_22(D) << 2;
  _9 = (long unsigned int) _8;
  _10 = _9 * 2;
  _11 = p_23(D) + _10;
  _12 = *_11;
  _13 = (int) _12;
  _14 = _7 + _13;
  _15 = x_22(D) + 1;
  _16 = _15 << 2;

Note the repeated forms of x_22 << 2.

We're essentially indexing into an array with [(index - 1) << 2], [(index) << 2], and [(index + 1) << 2] and fail to realize that the shifting is a common subexpression.

We get code like this:

        slli    a5,a1,2 # 50    [c=4 l=4]  ashldi3
        slliw   a4,a1,2 # 15    [c=8 l=4]  ashlsi3_extend
        addiw   a5,a5,-4        # 51    [c=8 l=4] addsi3_extended/1
        slli    a1,a1,2 # 52    [c=4 l=4]  ashldi3
        sh1add  a5,a5,a0        # 13    [c=4 l=4]  *shNadd
        sh1add  a4,a4,a0        # 19    [c=4 l=4]  *shNadd
        addiw   a1,a1,4 # 53    [c=8 l=4]  addsi3_extended/1
        lhu     a5,0(a5)        # 14    [c=28 l=4] *zero_extendhisi2_bitmanip/1         lhu     a4,0(a4)        # 20    [c=28 l=4] *zero_extendhisi2_bitmanip/1
        sh1add  a1,a1,a0        # 29    [c=4 l=4]  *shNadd
        lhu     a0,0(a1)        # 31    [c=28 l=4] *zero_extendhidi2_bitmanip/1
        addw    a5,a5,a4        # 21    [c=8 l=4] addsi3_extended/0
        add     a0,a5,a0        # 38    [c=4 l=4]  *adddi3/0
        ret             # 56    [c=0 l=4]  simple_return

Conceptually this could be solved by treating a left shift as-if it were a multiply in slsr and we get:



  _2 = _1 << 2;
  _3 = (long unsigned int) _2;
  _4 = _3 * 2;
  _5 = p_23(D) + _4;
  _6 = *_5;
  _7 = (int) _6;
  _10 = _4 + 8;
  _11 = p_23(D) + _10;
  _12 = *_11;
  _13 = (int) _12;
  _14 = _7 + _13;
  _18 = _4 + 16;
  _19 = p_23(D) + _18;
  _20 = *_19;
  _21 = (int) _20;
  _25 = _14 + _21;



        slli    a1,a1,2 # 40    [c=4 l=4]  ashldi3
        addiw   t0,a1,-4        # 41    [c=8 l=4] addsi3_extended/1
        sh1add  t1,t0,a0        # 13    [c=4 l=4]  *shNadd
        lhu     a5,8(t1)        # 14    [c=28 l=4] *zero_extendhisi2_bitmanip/1         lhu     a4,0(t1)        # 16    [c=28 l=4] *zero_extendhisi2_bitmanip/1         lhu     a0,16(t1)       # 21    [c=28 l=4] *zero_extendhidi2_bitmanip/1
        addw    t2,a5,a4        # 17    [c=8 l=4] addsi3_extended/0
        add     a0,t2,a0        # 28    [c=4 l=4]  *adddi3/0
        ret             # 44    [c=0 l=4]  simple_return


I've go a patch which does this, the problem is LLM review indicated it's going to break because of the semantics we've defined for overflowing left shifts.  Namely that they don't trigger undefined behavior while a multiply does trigger undefined behavior.

That matters in legal_cast_p which allows SLSR to look through some casts on the assumption that overflow is undefined.  But that's no longer a valid assumption if we let SLSR treat the shift as-if it were a multiply.

Given an lp64 platform, I suspect those casts are unavoidable and it looks like the range information in the key loop from Ranger isn't helpful -- essentially no information that would allow us to determine the shift doesn't overflow.

Any thoughts here?

The minimized testcase looks like this:

typedef unsigned short uint16_t;

int
f (uint16_t *p, int x)
{
 return p[(x - 1) << 2] + p[x << 2] + p[(x + 1) << 2];
}



But the real context is the cost[] assignments in x264_me_search_ref, specifically this in the cpp output:


                { uint8_t *pix_base = p_fref_w + bmx + bmy*stride; h->pixf.fpelcmp_x4[i_pixel]( p_fenc, pix_base + (0) + (-1)*stride, pix_base + (0) + (1)*stride, pix_base + (-1) + (0)*stride, pix_base + (1) + (0)*stride, stride, costs ); (costs)[0] += (p_cost_mvx[(bmx+(0))<<2] + p_cost_mvy[(bmy+(-1))<<2]); (costs)[1] += (p_cost_mvx[(bmx+(0))<<2] + p_cost_mvy[(bmy+(1))<<2]); (costs)[2] += (p_cost_mvx[(bmx+(-1))<<2] + p_cost_mvy[(bmy+(0))<<2]); (costs)[3] += (p_cost_mvx[(bmx+(1))<<2] + p_cost_mvy[(bmy+(0))<<2]);};
                if(((costs[0]<<4)+1)<(bcost)) (bcost)=((costs[0]<<4)+1);;



Barring any particularly helpful insight, I'm inclined to set this aside as unsolvable right now.  The WIP patch & testcase are attached if they prove useful to someone.

jeff



diff --git a/gcc/gimple-ssa-strength-reduction.cc 
b/gcc/gimple-ssa-strength-reduction.cc
index cac7a449d5a6..b4836cfa1c42 100644
--- a/gcc/gimple-ssa-strength-reduction.cc
+++ b/gcc/gimple-ssa-strength-reduction.cc
@@ -745,6 +745,10 @@ stmt_cost (gimple *gs, bool speed)
       gcc_assert (TREE_CODE (rhs1) != INTEGER_CST);
       return mul_cost (speed, lhs_mode);
 
+    case LSHIFT_EXPR:
+      rhs2 = gimple_assign_rhs2 (gs);
+      return shift_cost (speed, lhs_mode, tree_to_uhwi (rhs2));
+
     case PLUS_EXPR:
     case POINTER_PLUS_EXPR:
     case MINUS_EXPR:
@@ -1767,6 +1771,7 @@ find_candidates_dom_walker::before_dom_children 
(basic_block bb)
          switch (gimple_assign_rhs_code (gs))
            {
            case MULT_EXPR:
+           case LSHIFT_EXPR:
            case PLUS_EXPR:
              rhs1 = gimple_assign_rhs1 (gs);
              rhs2 = gimple_assign_rhs2 (gs);
@@ -1801,6 +1806,24 @@ find_candidates_dom_walker::before_dom_children 
(basic_block bb)
              slsr_process_mul (gs, rhs1, rhs2, speed);
              break;
 
+           /* Treat left shifts as-if they were multiplies.  */
+           case LSHIFT_EXPR:
+             if (TREE_CODE (rhs2) == INTEGER_CST
+                 && wi::ltu_p (wi::to_wide (rhs2),
+                               TYPE_PRECISION (TREE_TYPE (rhs1)) - 1))
+               {
+                 wide_int mul_const
+                   = wi::lshift (wi::one (TYPE_PRECISION (TREE_TYPE (rhs1))),
+                                          tree_to_uhwi (rhs2));
+                 tree mul_node = wide_int_to_tree (TREE_TYPE (rhs1), 
mul_const);
+                 slsr_process_mul (gs, rhs1, mul_node, speed);
+                 break;
+               }
+
+             /* If we didn't handle the LSHIFT as a multiply, then skip this
+                node.  */
+             continue;
+
            case PLUS_EXPR:
            case POINTER_PLUS_EXPR:
            case MINUS_EXPR:
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/slsr-43.c 
b/gcc/testsuite/gcc.dg/tree-ssa/slsr-43.c
new file mode 100644
index 000000000000..3e9f75dce57b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/slsr-43.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target lp64 } } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* The three index expressions (x - 1) << 2, x << 2 and (x + 1) << 2 differ
+   by constants, so straight-line strength reduction should compute the
+   shift once and express the other two as constant bumps of the scaled
+   index, which then fold into the addressing mode.  */
+
+typedef unsigned short uint16_t;
+
+int
+f (uint16_t *p, int x)
+{
+  return p[(x - 1) << 2] + p[x << 2] + p[(x + 1) << 2];
+}
+
+/* { dg-final { scan-tree-dump-times " << 2" 1 "optimized" } } */
+

Reply via email to