LIKELY __builtin_expect deoptimzes code

Bug #1821703 reported by Morty on 2019-03-26
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Undecided
Unassigned

Bug Description

The following code is compiled on gcc 8.2 using -Os
---
#include <stdint.h>
#ifdef LK
#define LIKELY(x) __builtin_expect((x),1)
#else
#define LIKELY(x) (x)
#endif

float a = 66;

int test (float b, int test) {
    if(LIKELY(test)) {
        return b * a;
    } else {
        return b / a;
    }
}
---
Likely should have no effect on the code. However additional instructions are added / are not removed when setting -DLK. (Seems like there is something wrong with the register allocation.)

See https://godbolt.org/z/yVgVC6

-Os
test:
        ldr r3, .L6
        cmp r1, #0
        ldr r3, [r3] @ float
        push {r4, lr}
        mov r1, r3
        beq .L2
        bl __aeabi_fmul
.L5:
        bl __aeabi_f2iz
        pop {r4, pc}
.L2:
        bl __aeabi_fdiv
        b .L5
.L6:
        .word .LANCHOR0
a:
        .word 1115947008

-Os -DLK
test:
        ldr r3, .L6
        cmp r1, #0
        push {r4, lr}
        ldr r3, [r3] @ float
        beq .L2
        mov r1, r0
        mov r0, r3
        bl __aeabi_fmul
.L5:
        bl __aeabi_f2iz
        pop {r4, pc}
.L2:
        mov r1, r3
        bl __aeabi_fdiv
        b .L5
.L6:
        .word .LANCHOR0
a:
        .word 1115947008

Download full text (4.1 KiB)

Ich bin bis 08.04. nicht im Haus und kann Ihre Nachricht daher leider nicht bearbeiten. In dringenden Fällen wenden Sie sich bitte an <email address hidden> bzw. für technische Fragen an <email address hidden>.

I am out of office until April 8th and won't be able to read your message. In urgent cases, please refer to <email address hidden> or for technical questions to <email address hidden>.

Mit freundlichen Grüßen / Best regards

Steffen Wolfer

--
Dipl.-Inform. Steffen Wolfer
Software Engineer Embedded Systems

WEISS ROBOTICS GmbH & Co. KG
Karl-Heinrich-Käferle-Str. 8
D-71640 Ludwigsburg, Germany

Phone: +49 7141 94702-22
Fax: +49 7141 94702-99
http://www.weiss-robotics.com

Sitz der Gesellschaft: Ludwigsburg
Registergericht Stuttgart, HRA725006

Pers. haftende Gesellschafterin:
Weiss Robotics Verwaltungs-GmbH, Sitz Ludwigsburg
Registergericht Stuttgart, HRB73310
Geschäftsführer: Dr. Karsten Weiß

Public bug reported:

The following code is compiled on gcc 8.2 using -Os
---
#include <stdint.h>
#ifdef LK
#define LIKELY(x) __builtin_expect((x),1)
#else
#define LIKELY(x) (x)
#endif

float a = 66;

int test (float b, int test) {
    if(LIKELY(test)) {
        return b * a;
    } else {
        return b / a;
    }
}
---
Likely should have no effect on the code. However additional instructions are added / are not removed when setting -DLK. (Seems like there is something wrong with the register allocation.)

See https://godbolt.org/z/yVgVC6

-Os
test:
        ldr r3, .L6
        cmp r1, #0
        ldr r3, [r3] @ float
        push {r4, lr}
        mov r1, r3
        beq .L2
        bl __aeabi_fmul
.L5:
        bl __aeabi_f2iz
        pop {r4, pc}
.L2:
        bl __aeabi_fdiv
        b .L5
.L6:
        .word .LANCHOR0
a:
        .word 1115947008

-Os -DLK
test:
        ldr r3, .L6
        cmp r1, #0
        push {r4, lr}
        ldr r3, [r3] @ float
        beq .L2
        mov r1, r0
        mov r0, r3
        bl __aeabi_fmul
.L5:
        bl __aeabi_f2iz
        pop {r4, pc}
.L2:
        mov r1, r3
        bl __aeabi_fdiv
        b .L5
.L6:
        .word .LANCHOR0
a:
        .word 1115947008

** Affects: gcc-arm-embedded
     Importance: Undecided
         Status: New

--
You received this bug notification because you are subscribed to GNU Arm
Embedded Toolchain.
Matching subscriptions: Älles
https://bugs.launchpad.net/bugs/1821703

Title:
  LIKELY __builtin_expect deoptimzes code

Status in GNU Arm Embedded Toolchain:
  New

Bug description:
  The following code is compiled on gcc 8.2 using -Os
  ---
  #include <stdint.h>
  #ifdef LK
  #define LIKELY(x) __builtin_expect((x),1)
  #else
  #define LIKELY(x) (x)
  #endif

  float a = 66;

  int test (float b, int test) {
      if(LIKELY(test)) {
          return b * a;
      } else {
          return b / a;
      }
  }
  ---
  Likely should have no effect on the code. However additional instructions are added / are not removed when setting -DLK. (Seems like there is something wrong with the register allocation.)

  See https://godbolt.org/z/yVgVC6

  -Os...

Read more...

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers