[Bug rtl-optimization/97603] Failure to optimize out compare into reuse of subtraction result

ubizjak at gmail dot com via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97603

--- Comment #5 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Gabriel Ravier from comment #0)
> int g();
> 
> int f(int a, int b)
> {
>     if (a != b)
>         return a - b;
>     return g();
> }
> 
> This can be optimized to using the result of `a - b` to check for `a != b`.

The above testcase is expanded to:

(insn 7 4 8 2 (set (reg:CCGC 17 flags)
        (compare:CCGC (reg/v:SI 99 [ a ])
            (reg/v:SI 100 [ b ]))) "pr97603.c":5:8 15 {*cmpsi_1}
     (nil))
(jump_insn 8 7 9 2 (set (pc)
        (if_then_else (ge (reg:CCGC 17 flags)
                (const_int 0 [0]))
            (label_ref 13)
            (pc))) "pr97603.c":5:8 1490 {*jcc}
     (int_list:REG_BR_PROB 524845001 (nil))
 -> 13)

(note 9 8 10 4 [bb 4] NOTE_INSN_BASIC_BLOCK)
(insn 10 9 11 4 (parallel [
            (set (reg:SI 98 [ <retval> ])
                (minus:SI (reg/v:SI 99 [ a ])
                    (reg/v:SI 100 [ b ])))
            (clobber (reg:CC 17 flags))
        ]) "pr97603.c":6:18 393 {*subsi_1}
     (nil))

Please note that (insn 7) and (insn 10) could be merged to:

(define_insn "*sub<mode>_3"
  [(set (reg FLAGS_REG)
        (compare (match_operand:SWI 1 "nonimmediate_operand" "0,0,r,r,m")
                 (match_operand:SWI 2 "<general_operand>"
"<r><i>,<m>,r<i>,<m>,r<i>")))
   (set (match_operand:SWI 0 "nonimmediate_operand" "=<r>m,<r>,r,r,r")
        (minus:SWI (match_dup 1) (match_dup 2)))]
  "ix86_match_ccmode (insn, CCmode)
   && ix86_binary_operator_ok (MINUS, <MODE>mode, operands, TARGET_APX_NDD)"

As mentioned in PR3507 c#67, this is the job of pre-reload compare elimination
pass.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.