Several versions of gcc (trunk, 4.4.0, 4.3.1 and 4.3.3) mis-compiled this test
case.

----bug.c----
/* compile with -Os -mthumb */
extern void abort (void);

static int llcmp(long long a, long long b);

struct info {
  long unsigned ll;
};

int __attribute__((noinline))
cmp(const void *a, const void *b)
{
  struct info *pa = *((struct info **)a);
  struct info *pb = *((struct info **)b);
  return llcmp(pa->ll, pb->ll);
}

static int
llcmp(long long a, long long b)
{
  if (a < b)
    return -1;
  if (a > b)
    return 1;
  return 0;
}

int
main ()
{
  struct info pa, pb;
  struct info *unsorted[2];

  unsorted[0] = &pa;
  unsorted[1] = &pb;

  pa.ll = 1;
  pb.ll = 2;
  if (cmp (&unsorted[0], &unsorted[1]) != -1)
    abort();

  pa.ll = 2;
  pb.ll = 1;
  if (cmp (&unsorted[0], &unsorted[1]) != 1)
    abort();

  pa.ll = 1;
  pb.ll = 1;
  if (cmp (&unsorted[0], &unsorted[1]) != 0)
    abort();

  return 0;
}
------

sh-3.2$ arm-unknown-linux-gnueabi-gcc -Os -mthumb bug.c
sh-3.2$ /disk2/dougkwan/qemu/install/bin/qemu-arm -L
~/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sys-root a.out
qemu: uncaught target signal 6 (Aborted) - exiting
sh-3.2$ arm-unknown-linux-gnueabi-gcc  -mthumb bug.csh-3.2$
/disk2/dougkwan/qemu/install/bin/qemu-arm -L
~/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sys-root a.out
sh-3.2$ 

Below is code generate for the function cmp:
----
        .align  1
        .global cmp
        .code   16
        .thumb_func
        .type   cmp, %function
cmp:
        push    {lr}
        ldr     r3, [r0]
        ldr     r2, [r1]
        ldr     r3, [r3]
        ldr     r2, [r2]
        cmp     r2, r3
        bhi     .L6
        mov     r0, #0
        b       .L2
.L6:
        mov     r0, #1
        neg     r0, r0
.L2:
        @ sp needed for prologue
        pop     {pc}
        .size   cmp, .-cmp
        .align  1

Note that the compiled function only returns 0 and 1 where as the same function
in the source code return values -1, 0 and 1.


-- 
           Summary: Long long comparison optimized away incorrectly in Thumb
                    code.
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dougkwan at google dot com
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: arm-unknown-linux-gnueabi


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40153

Reply via email to