This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

optimization/763: GCC generates incorrect code when optimising



>Number:         763
>Category:       optimization
>Synopsis:       GCC generates incorrect code when optimising
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 09 17:36:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     richard.mitton@bigfoot.com
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
MS-DOS, DJGPP
>Description:
The first command in main() should read a value from a function, add 1, and print it. This should always result in 11 as the function always returns 10.
But if -O and -fomit-frame-pointer are enabled, it throws away the return value and prints an incorrect answer.

The annoying thing is, there's some other code after this bit that technically should have no effect, but if I remove it the bug goes away...
>How-To-Repeat:
Just compile and run, will print 11 on success or other value on failure.
>Fix:
Remove -O or -fomit-frame-pointer for a temporary solution, or go back to using GCC v2.8 in which it worked.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="test.c"
Content-Disposition: inline; filename="test.c"

/* Test program to display a possible GCC bug.
 * Submitted by Richard Mitton (richard.mitton@bigfoot.com)
 * 15th August 2000.
 *
 * Using a DJGPP system (latest version, v2.03), Windows 98.
 * (latest version, gcc 2.95.2 19991024 release):
 *
 * To make bug happen: gcc -S -O -fomit-frame-pointer test.c -o test.s
 * To make bug not happen, remove either -O or -fomit-frame-pointer.
 *
 * The bug happens when GCC throws away the result from GetValue().
 */

#include <stdio.h>

/* Just some functions to demonstrate */
unsigned char GetValue(void);
unsigned char EmptyFunction(void);
void Print(int a);
void DoNothing(int b);

int main(void)
{
   unsigned char foo, bar, flag;

   /* IMPORTANT PART!
    * This should print GetValue()+1.
    * Since GetValue always returns 10, it should print 11.
    */
   foo = GetValue() + 1;
   Print(foo);

   /* From here on is just misc code, but if I take any of it out then
    * the bug doesn't happen...
    */

   foo = EmptyFunction();
   bar = EmptyFunction();
   flag = foo;

   DoNothing(bar);
   DoNothing(bar);

   if (!flag) foo = 1;
   return 0;
}

/* Always return 10 */
unsigned char GetValue(void)
{
   return 10;
}

unsigned char EmptyFunction(void)
{
   return 0;
}

void Print(int a)
{
   printf("Answer is %i, but should be 11 (i.e. 10+1).\n", a);
}

void DoNothing(int b)
{
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]