loongson-community / gcc

GCC source tree for Loongson
GNU General Public License v2.0
17 stars 1 forks source link

[cfgcleanup] MIPS BE: wrong code generates under "-Os -mbranch-cost=1" #4

Open xiangzhai opened 6 years ago

xiangzhai commented 6 years ago

Hi fellows,

As PR83496 mentioned: Testcase is wrongly lowering to:

    .file   1 "gcc-pr83496.c"
    .section .mdebug.abi64
    .previous
    .nan    legacy
    .module fp=64
    .module oddspreg
    .abicalls
    .text
    .align  2
    .globl  mytest
    .set    nomips16
    .set    nomicromips
    .ent    mytest
    .type   mytest, @function
mytest:
    .frame  $sp,0,$31       # vars= 0, regs= 0/0, args= 0, gp= 0
    .mask   0x00000000,0
    .fmask  0x00000000,0
    .set    noreorder
    .set    nomacro
    lw  $3,8($4)
    li  $2,1            # 0x1
    beq $3,$2,.L3
    nop # <-- it should say "li v0,1" instead of nop

    lw  $3,0($4)
    slt $3,$3,2
    beq $3,$0,.L5
    nop

    ld  $2,16($4)
    ld  $2,0($2)
    sltu    $3,$5,$2
    bne $3,$0,.L5
    nop

    sltu    $2,$2,$5
    j   $31
    subu    $2,$0,$2

.L3:
    j   $31
    li  $2,-1           # 0xffffffffffffffff

.L5:
    j   $31
    li  $2,1            # 0x1

    .set    macro
    .set    reorder
    .end    mytest
    .size   mytest, .-mytest
    .section    .text.startup,"ax",@progbits
    .align  2
    .globl  main
    .set    nomips16
    .set    nomicromips
    .ent    main
    .type   main, @function
main:
    .frame  $sp,0,$31       # vars= 0, regs= 0/0, args= 0, gp= 0
    .mask   0x00000000,0
    .fmask  0x00000000,0
    .set    noreorder
    .set    nomacro
    j   $31
    move    $2,$0

    .set    macro
    .set    reorder
    .end    main
    .size   main, .-main
    .ident  "GCC: (GNU) 5.5.0 20171010 (LLVM China GCC 5.5-2018.01)"

GCC 8.x still reproducible the key point might be bb_is_just_return:

commit 01b8ebb6e5940eca6d158dc793424a6580c90998
Author: segher <segher@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Wed May 4 20:54:47 2016 +0000

    cfgcleanup: Fold jumps and conditional branches with returns

    This patch makes cfgcleanup optimize jumps to returns.  There are three
    cases this handles:

    -- A jump to a return; this is simplified to just that return.
    -- A conditional branch to a return; simplified to a conditional return.
    -- A conditional branch that falls through to a return.  This is simplified
       to a conditional return (with the condition inverted), falling through
       to a jump to the original destination.  That jump can then be optimized
       further, as usual.

    This handles all cases the current function.c does, and a few it misses.

            * cfgcleanup.c (bb_is_just_return): New function.
            (try_optimize_cfg): Simplify jumps to return, branches to return,
            and branches around return.

Regards, Leslie Zhai