User account creation filtered due to spam.

Bug 20487 - Bad code generation caused by -fschedule-insns2
Summary: Bad code generation caused by -fschedule-insns2
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-15 20:20 UTC by Claudiu Pop
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu, x86_64-unknown-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Claudiu Pop 2005-03-15 20:20:32 UTC
This bug was detected in gcc 3.3.1 and is still here in gcc-3.4.3 and gcc-4.0.0
The bug is not in gcc-3.2.2

-O2 and -Os produces bad code.
Switching optimization level to something less than -O2 produces a valid code.
Using -O2 -fno-schedule-insns2 also produces valid code.

./g++-mainline-4.0 -O2 -o foo ~/bug.ii
./foo
foo: /bug.cc:59: int main(int, char**): Assertion `zone1->offset == 2 * 20' failed.
Aborted

./g++-mainline-4.0 -O2 -o foo ~/bug.ii -fno-schedule-insns2
./foo
works just fine. 


cat bug.ii

# 1 "/bug.cc"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "/bug.cc"
# 1 "/usr/include/assert.h" 1 3 4
# 36 "/usr/include/assert.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 296 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 297 "/usr/include/features.h" 2 3 4
# 319 "/usr/include/features.h" 3 4
# 1 "/usr/include/gnu/stubs.h" 1 3 4
# 320 "/usr/include/features.h" 2 3 4
# 37 "/usr/include/assert.h" 2 3 4
# 65 "/usr/include/assert.h" 3 4
extern "C" {


extern void __assert_fail (__const char *__assertion, __const char *__file,
      unsigned int __line, __const char *__function)
     throw () __attribute__ ((__noreturn__));


extern void __assert_perror_fail (int __errnum, __const char *__file,
      unsigned int __line,
      __const char *__function)
     throw () __attribute__ ((__noreturn__));




extern void __assert (const char *__assertion, const char *__file, int __line)
     throw () __attribute__ ((__noreturn__));


}
# 2 "/bug.cc" 2




struct Type1
{
  long offset;
};

struct Decaler
{
  long bar;
  long foo;
};

struct Type3
{
  int value;
};

struct mem_block
{
  mem_block() { m_ShMem = new char[4096]; }

  void* operator[](const long Offset)
  {
    return (reinterpret_cast<char *>(m_ShMem) + Offset);
  }

  void* m_ShMem;
};


int main(int, char**)
{
  mem_block memory_block;
  Decaler* decaler = new Decaler();

  Type1* zone1 = reinterpret_cast<Type1*>(memory_block[0]);
  zone1->offset = 20;

  Type1* zone2 = reinterpret_cast<Type1*>(memory_block[20]);
  zone2->offset = 2 * 20;

  Type3* pageToUse = reinterpret_cast<Type3*>(memory_block[20]);


  decaler->bar = 123456;
  decaler->foo = zone1->offset;

  zone1->offset = zone2->offset;



  pageToUse->value = 5;


  (static_cast<void> (__builtin_expect (!!(zone1->offset == 2 * 20), 1) ? 0 :
(__assert_fail ("zone1->offset == 2 * 20", "/bug.cc", 59, __PRETTY_FUNCTION__),
0)));

  return 0;
}
Comment 1 Andrew Pinski 2005-03-15 20:25:29 UTC
  Type1* zone1 = reinterpret_cast<Type1*>(memory_block[0]);
  zone1->offset = 20;

  Type1* zone2 = reinterpret_cast<Type1*>(memory_block[20]);
  zone2->offset = 2 * 20;

  Type3* pageToUse = reinterpret_cast<Type3*>(memory_block[20]);


You are violating C++ aliasing rules. reinterpret_cast is still a normal cast.
Comment 2 Andrew Pinski 2005-06-05 09:12:08 UTC
Reopening to ...
Comment 3 Andrew Pinski 2005-06-05 09:12:24 UTC
Mark as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***