Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Remove impossible branches #45931

Open Quuxplusone opened 4 years ago

Quuxplusone commented 4 years ago
Bugzilla Link PR46962
Status NEW
Importance P enhancement
Reported by David Bolvansky (david.bolvansky@gmail.com)
Reported on 2020-08-02 10:11:44 -0700
Last modified on 2020-08-03 05:39:02 -0700
Version trunk
Hardware PC Linux
CC efriedma@quicinc.com, florian_hahn@apple.com, jdoerfert@anl.gov, kazu@google.com, llvm-bugs@lists.llvm.org, nikita.ppv@gmail.com, spatel+llvm@rotateright.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
unsigned deadbranches(bool A, bool B)

{

 if (A || B) {
     if (A && B) {
       return foo1();
     }

     else if (A) {
       return foo2();
     }

     else if (B) {
        return foo3();
     }

     else {
        return foo5();
     }
    }
    return foo4();
}

else branch: return foo5(); is dead, but Clang does not remove it.

Clang:
deadbranches(bool, bool):                     # @deadbranches(bool, bool)
        test    edi, edi
        jne     .LBB0_2
        test    sil, sil
        jne     .LBB0_2
        jmp     foo4()                        # TAILCALL
.LBB0_2:
        test    dil, dil
        je      .LBB0_4
        test    sil, sil
        je      .LBB0_4
        jmp     foo1()                        # TAILCALL
.LBB0_4:
        test    dil, dil
        je      .LBB0_5
        jmp     foo2()                        # TAILCALL
.LBB0_5:
        test    sil, sil
        je      .LBB0_6
        jmp     foo3()                        # TAILCALL
.LBB0_6:
        jmp     foo5()                        # TAILCALL

GCC:

deadbranches(bool, bool):
        test    dil, dil
        jne     .L7
        test    sil, sil
        je      .L9
.L5:
        jmp     foo3()
.L9:
        jmp     foo4()
.L7:
        test    sil, sil
        je      .L4
        jmp     foo1()
.L4:
        test    dil, dil
        je      .L5
        jmp     foo2()

https://godbolt.org/z/Es5z1x
Quuxplusone commented 4 years ago
Probably Jump Threading issue but this part of codegen looks bad too..

LBB0_2:
        test    dil, dil
        je      .LBB0_4  ------
        test    sil, sil      |
        je      .LBB0_4       |
        jmp     foo1()        |                  # TAILCALL
.LBB0_4:                      |
        test    dil, dil <-----
        je      .LBB0_5
        jmp     foo2()