https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91232

            Bug ID: 91232
           Summary: Adding -fPIC with optimization level > 0 resulting a
                    busy hang in my program
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bharath.appali at gmail dot com
  Target Milestone: ---

Created attachment 46622
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46622&action=edit
Untar the attachment you can find these files

With GCC 8.3.0 on alpine:

I'm trying to compile my program 

```
#include <stdio.h>
#include <inttypes.h>

#define SPEXPONENT_BIAS 127

static int getMostSignificantBit(uint32_t u32val);
float convertIntegerToFloat(int32_t src);

int main(void) {
        int rc = convertIntegerToFloat(0x80000000);
        printf("rc: %d\n", rc);
        return 0;
}

float
convertIntegerToFloat(int32_t src)
{
        float tmpDst;

        {
                int idl1, sign;
                uint32_t spfInt, overflow;

                if (src == 0) {
                        return 0.0f;
                }

                if (src < 0) {
                        spfInt = (uint32_t)(0 - src);
                        sign = 1;
                } else {
                        spfInt = (uint32_t)src;
                        sign = 0;
                }

                /* Find out where the most significant bit is in the integer
value.
                 * We are only interested in keeping 24 of those bits. 
                 */
                idl1 = getMostSignificantBit(spfInt);
                printf("idl1: %d\n", idl1);
                if (idl1 >= 24) {

                        /* If it's more than 24 bits, we shift right and keep
track of
                         * the overflow for some possible rounding. 
                         */
                        overflow = spfInt << (32 - (idl1 - 23));
                        spfInt >>= (idl1 - 23);
                        spfInt &= 0x007FFFFF;
                        spfInt |= ((idl1+SPEXPONENT_BIAS) << 23);
                        if ((overflow & 0x80000000) != 0) {
                                if ((overflow & 0x7FF00000) != 0) {
                                        spfInt++;
                                } else {
                                        if((spfInt & 1) != 0) spfInt++;
                                }
                        }
                } else if (idl1 < 23) {

                        /* If it's less than 24 bits, we shift left, and no
                         * overflow and rounding to worry about. 
                         */
                        spfInt <<= (23 - idl1);
                        spfInt &= 0x007FFFFF;
                        spfInt |= ((idl1+SPEXPONENT_BIAS) << 23);
                } else {

                        /* It must be exactly 24 bits, so no shift is required. 
                         */
                        spfInt &= 0x007FFFFF;
                        spfInt |= ((idl1+SPEXPONENT_BIAS) << 23);
                }
                if (sign) {
                        spfInt |= 0x80000000;
                }

                *((uint32_t *)&tmpDst) = spfInt;
        }

        return tmpDst;
}

static int
getMostSignificantBit(uint32_t u32val)
{
        int leading;
        uint32_t mask;

        if (u32val == 0) {
                return -1;
        }
        if (u32val & 0xFF000000) {
                leading = 31;
                mask = 0x80000000;
        } else if (u32val & 0x00FF0000) {
                leading = 23;
                mask = 0x00800000;
        } else if (u32val & 0x0000FF00) {
                leading = 15;
                mask = 0x00008000;
        } else {
                leading = 7;
                mask = 0x00000080;
        }
        while((mask & u32val) == 0) {
                mask >>= 1;
                leading--;
        }
        return leading;
}

```

compiling options - `-O3 -fPIC`
command issued - `gcc -O3 -fPIC -o fltconv fltconv.c`

Exploration:

With `-O3` optimization level the compiler is inlining the function
`getMostSignificantBit` and the while loop in it is behaving like `do while` so
in a condition where the loop should not even been entered its entering and
going into a infinite loop (main function is calling with that corner
condition)

Without optimization `-O0` its working fine, and also without `-fPIC` and with
`-O3` its working fine. This particular combination `-O3 -fPIC` is causing the
issue.

I'm attaching the required files in the attachment

Reply via email to