public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/32327]  New: Incorrect stack sharing causing removal of live code
@ 2007-06-13 19:57 dnovillo at gcc dot gnu dot org
  2007-06-13 21:20 ` [Bug middle-end/32327] " pinskia at gcc dot gnu dot org
                   ` (34 more replies)
  0 siblings, 35 replies; 36+ messages in thread
From: dnovillo at gcc dot gnu dot org @ 2007-06-13 19:57 UTC (permalink / raw)
  To: gcc-bugs

Given the following code:

------------------------------------------------------------------------------
typedef unsigned int size_t;
typedef unsigned long long uint64;

extern "C" {
extern void *memcpy (void *__restrict __dest,
      __const void *__restrict __src, size_t __n) /*throw ()*/;
}

extern void foo (void* p);

inline uint64
ghtonll(uint64 x)
{
 // __r is allocated the same stack slot as dest below
 union { unsigned long long int __ll;
         unsigned long int __l[2]; } __w, __r;
 __w.__ll = x;
 __r.__l[0] = (
   {
     register unsigned int __v;
     __asm__ __volatile__ ("bswap %0" : "=r" (__v) :
                           "0" ((unsigned int) (__w.__l[1])));
     __v; });

 __r.__l[1] = (
   {
     register unsigned int __v;
     __asm__ __volatile__ ("bswap %0" : "=r" (__v) :
                           "0" ((unsigned int) (__w.__l[0])));
     __v; });

 return __r.__ll;
}

inline uint64
double_2_uint64 (const double *source)
{
 uint64 dest;  // allocated the same stack slot as __r above
 memcpy(&dest, source, sizeof(dest));
 return dest;
}

inline void
KeyFromUint64(uint64 fp) {
 uint64 norder;
 norder = ghtonll (fp);
 foo((char*)(&norder));
}

void
KeyFromDouble(double x) {
 uint64 n = double_2_uint64 (&x);
 if (n >= 42) {
   n += 1;
 }

 KeyFromUint64(n);
}
---

Here is what gcc -O2 -fdump-tree-all (version 4.2) in at the end of the tree
passes.
Please take note of the assignment to dest after that of norder.

;; Function void KeyFromDouble(double) (_Z13KeyFromDoubled)

void KeyFromDouble(double) (x)
{
 uint64 n.50;
 char * norder.2;
 uint64 norder;
 register unsigned int __v;
 register unsigned int __v;
 union ._0 __r;
 union ._0 __w;
 uint64 dest;
 uint64 n;

<bb 2>:
 n.50 = VIEW_CONVERT_EXPR<uint64>(x);
 if (n.50 > 41) goto <L0>; else goto <L5>;

<L5>:;
 n = n.50;
 goto <bb 5> (<L1>);

<L0>:;
 n = n.50 + 1;

<L1>:;
 __w.__ll = n;
 __asm__ __volatile__("bswap %0":"=r" __v:"0" __w.__l[1]);
 __r.__l[0] = __v;
 __asm__ __volatile__("bswap %0":"=r" __v:"0" __w.__l[0]);
 __r.__l[1] = __v;
 norder = __r.__ll;
 norder.2 = (char *) &norder;
 dest = n.50;
 foo (norder.2);
 return;

}
----
Here is part of the RTL expansion:

(insn 45 43 47 9 (set (mem/c/i:DI (plus:SI (reg/f:SI 54 virtual-stack-vars)
               (const_int -8 [0xfffffff8])) [3 norder+0 S8 A32])
       (reg/v:DI 64 [ __r ])) -1 (nil)
   (nil))

(insn 47 45 49 9 (parallel [
           (set (reg:SI 59 [ norder.2 ])
               (plus:SI (reg/f:SI 54 virtual-stack-vars)
                   (const_int -8 [0xfffffff8])))
           (clobber (reg:CC 17 flags))
       ]) -1 (nil)
   (nil))

(insn 49 47 51 9 (set (mem/c/i:DI (plus:SI (reg/f:SI 54 virtual-stack-vars)
               (const_int -8 [0xfffffff8])) [3 dest+0 S8 A32])
       (reg/v:DI 58 [ n.50 ])) -1 (nil)
   (nil))

Both dest & norder are assigned the same memory location
(virtual-stack-vars - 8). So later lifeness analysis thinks that the
first assignment is dead and it removes it. norder contains results of
bswap but gcc cannot remove the asm statement. However, since the
output of the asms are dead so they both got eax as the output
register.


-- 
           Summary: Incorrect stack sharing causing removal of live code
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dnovillo at gcc dot gnu dot org
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


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


^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2009-03-30 22:06 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-13 19:57 [Bug c/32327] New: Incorrect stack sharing causing removal of live code dnovillo at gcc dot gnu dot org
2007-06-13 21:20 ` [Bug middle-end/32327] " pinskia at gcc dot gnu dot org
2007-06-13 21:25 ` dougkwan at google dot com
2007-06-13 21:32 ` pinskia at gcc dot gnu dot org
2007-06-13 21:37 ` pinskia at gcc dot gnu dot org
2007-06-13 21:42 ` [Bug middle-end/32327] [4.2 Regression] " pinskia at gcc dot gnu dot org
2007-06-13 21:50 ` dougkwan at google dot com
2007-06-13 21:53 ` pinskia at gcc dot gnu dot org
2007-06-14  0:14 ` dougkwan at google dot com
2007-06-14  0:28 ` pinskia at gcc dot gnu dot org
2007-06-14  0:35 ` dougkwan at google dot com
2007-06-14  0:42 ` pinskia at gcc dot gnu dot org
2007-06-14  0:42 ` pinskia at gcc dot gnu dot org
2007-06-14  0:46 ` dougkwan at google dot com
2007-06-14  0:52 ` pinskia at gmail dot com
2007-06-14  0:59 ` dougkwan at google dot com
2007-06-14  1:02 ` pinskia at gcc dot gnu dot org
2007-06-14  1:09 ` dougkwan at google dot com
2007-06-14  1:14 ` pinskia at gmail dot com
2007-06-14 17:58 ` ian at airs dot com
2007-06-14 18:05 ` dougkwan at google dot com
2007-06-14 18:13 ` rguenther at suse dot de
2007-06-15 13:15 ` mec at google dot com
2007-06-15 13:27 ` dnovillo at gcc dot gnu dot org
2007-06-15 22:10 ` dnovillo at gcc dot gnu dot org
2007-06-18 12:31 ` dnovillo at gcc dot gnu dot org
2007-06-19 17:26 ` rth at gcc dot gnu dot org
2007-06-19 17:40 ` dnovillo at google dot com
2007-06-19 18:58 ` amacleod at redhat dot com
2007-07-04  3:29 ` mmitchel at gcc dot gnu dot org
2007-07-04 11:23 ` dnovillo at google dot com
2007-07-20  3:46 ` mmitchel at gcc dot gnu dot org
2007-10-09 19:22 ` mmitchel at gcc dot gnu dot org
2008-02-01 16:58 ` jsm28 at gcc dot gnu dot org
2008-05-19 20:27 ` jsm28 at gcc dot gnu dot org
2009-03-30 22:06 ` jsm28 at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).