0

I need to reduce amount of branches in code. There is exist benchmark called median it have some code like:

    if ( A < B )
            return A = foo[i];
        else
            return B = foo[i];

I wrote a pattern in machine description file *.md to avoid branches:

    (define_insn "smin<GPR:mode>3"
      [
        (set 
          (match_operand:GPR 0 "register_operand" "=r")
            (if_then_else:GPR
          (lt:GPR 
            (match_operand:GPR 1 "register_operand" " r")
            (match_operand:GPR 2 "register_operand" " r"))
        (match_dup 1)
        (match_dup 2)))
      ]
      ""
      "min\t%0,%1,%2"
      [(set_attr "type" "move")
       (set_attr "mode" "<MODE>")]) 

It works in case of simple comparison:

    if ( A < B )
            return A ;
        else
            return B;

GCC emit:

    min a0,a0,a1    # 9 smindi3 [length = 4]
    ret # 21    simple_return   [length = 4]

But if i try same, but with indexed variable( array ): it won`t works:

    if ( A < B )
            return A = foo[i];
        else
            return B = foo[i];

GCC emit:

    blt a0,a1,.L5   # 11    *branch_orderdi [length = 4]
    sd  a1,8(a2)    # 18    *movdi_64bit/4  [length = 4]
    mv  a0,a1   # 8 *movdi_64bit/1  [length = 4]
    ret # 34    simple_return   [length = 4]
    .L5:
    sd  a0,8(a2)    # 13    *movdi_64bit/4  [length = 4]
    ret # 28    simple_return   [length = 4]

I need to GCC emit something like this:

    min a0,a0,a1    # 9 smindi3 [length = 4]
    sd  a0,8(a2)    # 18    *movdi_64bit/4  [length = 4]
    ret # 34    simple_return   [length = 4]

I appreciate any help.

Alexy Khilaev
  • 415
  • 3
  • 13

1 Answers1

3
    if ( A < B )
        return A = foo[i];
    else
        return B = foo[i]

Huh?? Assuming that is inside a function called fx() why not

if (A < B) A = fx(); else B = fx();

and simplify fx(), getting rid of the use of global variables A and B in the process, to

    return foo[i];
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Please read carefully, there is Benchmark, and this Benchmark have code! Maybe you offer to optimize all existed code ever? I am just try to reduce a number of generated branches in situation, and this kind of is very common. Sorry for my Engrish. – Alexy Khilaev Jun 07 '19 at 03:33
  • Oops, my bad. Should have made the answer a comment. Feel free to ignore it. – pmg Jun 07 '19 at 07:27