Subject: compiler bug (egcs-1.1.2)
To: None <tech-toolchain@netbsd.org>
From: Matthias Drochner <drochner@zel459.zel.kfa-juelich.de>
List: tech-toolchain
Date: 08/17/1999 14:35:56
This comes from a Linux list.
The following program gets misoptimized with -O2, at least
on i386 and mipsel.

#include <stdio.h>

/* If one=1, then the output should be: bit&1 */
unsigned test(unsigned one , unsigned  bit)
{
    unsigned val=  bit & 1;
    unsigned zero= one >> 1;

    val++;
    return zero + ( val>> 1 );
}

int main()
{
  printf("ret=%d\n", test(1,0)); /* should print 0 */
  printf("ret=%d\n", test(1,1)); /* should print 1 */
  printf("ret=%d\n", test(1,65535)); /* should print 1 */

  return 0;
}


In test(), "val" gets optimized away, resulting in
test:
        pushl %ebp
        movl %esp,%ebp
        movl 8(%ebp),%eax
        shrl $1,%eax
        leave
        ret

on i386.

best regards
Matthias