public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc
@ 2012-03-04 12:59 burnus at gcc dot gnu.org
  2012-03-05 10:19 ` [Bug middle-end/52478] " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: burnus at gcc dot gnu.org @ 2012-03-04 12:59 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 52478
           Summary: -ftrapv calls the wrong functions in libgcc
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: burnus@gcc.gnu.org


As PR 19020 has been closed ("let's ditch antiquated stuff and start afresh") -
open a fresh bug.


Using -ftrapv works with i368-gnu-linux:

$ gcc -m32 -ftrapv hjfff.c && ./a.out
Aborted (core dumped)
$

But not on x86-64-gnu-linux:
$ gcc -m64 -ftrapv hjfff.c && ./a.out
$


In the debugger, one sees:

a) i386:

__addvsi3 (a=2147483647, b=1) at
/home/tob/projects/gcc-trunk-checkout/libgcc/libgcc2.c:79
(gdb) pt a
type = int


b) x86-64:

(gdb)
__addvdi3 (a=2147483647, b=1) at
/home/tob/projects/gcc-trunk-checkout/libgcc/libgcc2.c:82
(gdb) pt a
type = long int

Thus, there is no overflow in that function.


THUS: On i386 it calls the correct function but on x86-64 it should call the SI
not the DI function.

 * * *

For long/LONG_MAX, one has again:

a) -m32

__addvsi3 (a=2147483647, b=1) at
/home/tob/projects/gcc-trunk-checkout/libgcc/libgcc2.c:79
79      {
(gdb) pt a
type = int

b) -m64

__addvdi3 (a=9223372036854775807, b=1) at
/home/tob/projects/gcc-trunk-checkout/libgcc/libgcc2.c:82
82        if (b >= 0 ? w < a : w > a)
(gdb) pt a
type = long int


THUS: x86-64 calls correctly the DI function but i386 wrongly the SI function

 * * *

long long / LLONG_MAX:

a) -m32:

__addvdi3 (a=9223372036854775807, b=1) at
/home/tob/projects/gcc-trunk-checkout/libgcc/libgcc2.c:82
82        if (b >= 0 ? w < a : w > a)
(gdb) pt a
type = long int

b) -m64

__addvdi3 (a=9223372036854775807, b=1) at
/home/tob/projects/gcc-trunk-checkout/libgcc/libgcc2.c:82
82        if (b >= 0 ? w < a : w > a)
(gdb) pt a  
type = long int


THUS: Both x86-64 and i386 call the "long int" instead of the "long long"
function. I had exepected a TI version, which does not seem to exist.

 * * *


#include <limits.h>

int __attribute__((noinline))
iaddv (int a, int b)
{
  return a + b;
}

int main(void)
{
  return iaddv (INT_MAX, 1);
}


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
@ 2012-03-05 10:19 ` rguenth at gcc dot gnu.org
  2012-03-05 10:43 ` burnus at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-03-05 10:19 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2012-03-05
         AssignedTo|unassigned at gcc dot       |rguenth at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-03-05 10:18:55 UTC ---
Confirmed, and I think we have a dup for this somewhere, we use OPTAB_LIB_WIDEN
which is of course bogus for the trapv variants.  See also PR52353 for
similar mis-generic handling of trapv optabs.

I'll take this one as well for now.


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
  2012-03-05 10:19 ` [Bug middle-end/52478] " rguenth at gcc dot gnu.org
@ 2012-03-05 10:43 ` burnus at gcc dot gnu.org
  2012-03-05 12:02 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: burnus at gcc dot gnu.org @ 2012-03-05 10:43 UTC (permalink / raw)
  To: gcc-bugs

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

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |burnus at gcc dot gnu.org

--- Comment #2 from Tobias Burnus <burnus at gcc dot gnu.org> 2012-03-05 10:42:46 UTC ---
(In reply to comment #1)
> Confirmed, and I think we have a dup for this somewhere.

Maybe PR 35412 - its bug description is a bit unclear. Other bugs related to
this PR have been closed, e.g. PR19020 ("fixed"), PR40143, PR39771, PR36868,
PR27261 (duplicate).

> I'll take this one as well for now.
Thanks!


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
  2012-03-05 10:19 ` [Bug middle-end/52478] " rguenth at gcc dot gnu.org
  2012-03-05 10:43 ` burnus at gcc dot gnu.org
@ 2012-03-05 12:02 ` rguenth at gcc dot gnu.org
  2012-10-07 19:15 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-03-05 12:02 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |NEW
         AssignedTo|rguenth at gcc dot gnu.org  |unassigned at gcc dot
                   |                            |gnu.org

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-03-05 12:01:23 UTC ---
This seems to be by design, libgcc only provides >= word_mode trapping ops. 
Which
means we'd need to expand this all using inline code:

/* Like gen_libfunc, but verify that integer operation is involved.  */

static void
gen_int_libfunc (optab optable, const char *opname, char suffix,
                 enum machine_mode mode)
{
  int maxsize = 2 * BITS_PER_WORD;

  if (GET_MODE_CLASS (mode) != MODE_INT)
    return;
  if (maxsize < LONG_LONG_TYPE_SIZE)
    maxsize = LONG_LONG_TYPE_SIZE;
  if (GET_MODE_CLASS (mode) != MODE_INT
      || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
    return;

oops.

Otherwise a simple

Index: gcc/expr.c
===================================================================
--- gcc/expr.c  (revision 184918)
+++ gcc/expr.c  (working copy)
@@ -8938,7 +8938,9 @@ expand_expr_real_2 (sepops ops, rtx targ
   if (modifier == EXPAND_STACK_PARM)
     target = 0;
   temp = expand_binop (mode, this_optab, op0, op1, target,
-                      unsignedp, OPTAB_LIB_WIDEN);
+                      unsignedp,
+                      trapv_binoptab_p (this_optab)
+                      ? OPTAB_LIB : OPTAB_LIB_WIDEN);
   gcc_assert (temp);
   /* Bitwise operations do not need bitfield reduction as we expect their
      operands being properly truncated.  */

should have worked.

So, it's a "not implemented thing" currently ... unlike to change until
somebody implements the ftrapv proposal from Joseph.


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2012-03-05 12:02 ` rguenth at gcc dot gnu.org
@ 2012-10-07 19:15 ` pinskia at gcc dot gnu.org
  2014-07-24  9:17 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-10-07 19:15 UTC (permalink / raw)
  To: gcc-bugs


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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mikulas at artax dot
                   |                            |karlin.mff.cuni.cz

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-10-07 19:15:25 UTC ---
*** Bug 54846 has been marked as a duplicate of this bug. ***


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2012-10-07 19:15 ` pinskia at gcc dot gnu.org
@ 2014-07-24  9:17 ` rguenth at gcc dot gnu.org
  2014-07-28  8:48 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-07-24  9:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52478

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
It seems that the libgcc functions for SImode are present (at least on x86_64),
so sth like

Index: gcc/optabs.c
===================================================================
--- gcc/optabs.c        (revision 212970)
+++ gcc/optabs.c        (working copy)
@@ -5559,13 +5559,17 @@ gen_int_libfunc (optab optable, const ch
                 enum machine_mode mode)
 {
   int maxsize = 2 * BITS_PER_WORD;
+  int minsize = BITS_PER_WORD;

   if (GET_MODE_CLASS (mode) != MODE_INT)
     return;
   if (maxsize < LONG_LONG_TYPE_SIZE)
     maxsize = LONG_LONG_TYPE_SIZE;
-  if (GET_MODE_CLASS (mode) != MODE_INT
-      || GET_MODE_BITSIZE (mode) < BITS_PER_WORD
+  if (minsize > INT_TYPE_SIZE
+      && (trapv_binoptab_p (optable)
+         || trapv_unoptab_p (optable)))
+    minsize = INT_TYPE_SIZE;
+  if (GET_MODE_BITSIZE (mode) < minsize
       || GET_MODE_BITSIZE (mode) > maxsize)
     return;
   gen_libfunc (optable, opname, suffix, mode);
Index: gcc/expr.c
===================================================================
--- gcc/expr.c  (revision 212970)
+++ gcc/expr.c  (working copy)
@@ -9212,7 +9212,9 @@ expand_expr_real_2 (sepops ops, rtx targ
   if (modifier == EXPAND_STACK_PARM)
     target = 0;
   temp = expand_binop (mode, this_optab, op0, op1, target,
-                      unsignedp, OPTAB_LIB_WIDEN);
+                      unsignedp,
+                      trapv_binoptab_p (this_optab)
+                      ? OPTAB_LIB : OPTAB_LIB_WIDEN);
   gcc_assert (temp);
   /* Bitwise operations do not need bitfield reduction as we expect their
      operands being properly truncated.  */

makes the testcase pass.


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2014-07-24  9:17 ` rguenth at gcc dot gnu.org
@ 2014-07-28  8:48 ` rguenth at gcc dot gnu.org
  2014-07-28  9:06 ` rguenth at gcc dot gnu.org
  2014-07-29 11:11 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-07-28  8:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52478

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
Author: rguenth
Date: Mon Jul 28 08:47:38 2014
New Revision: 213117

URL: https://gcc.gnu.org/viewcvs?rev=213117&root=gcc&view=rev
Log:
2014-07-28  Richard Biener  <rguenther@suse.de>

    PR middle-end/52478
    * optabs.c (gen_int_libfunc): For -ftrapv libfuncs make
    sure to register SImode ones, not only >= word_mode ones.
    * expr.c (expand_expr_real_2): When expanding -ftrapv
    binops do not use OPTAB_LIB_WIDEN.

    * gcc.dg/torture/ftrapv-1.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/ftrapv-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/expr.c
    trunk/gcc/optabs.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2014-07-28  8:48 ` rguenth at gcc dot gnu.org
@ 2014-07-28  9:06 ` rguenth at gcc dot gnu.org
  2014-07-29 11:11 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-07-28  9:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52478

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
      Known to work|                            |4.10.0
         Resolution|---                         |FIXED
   Target Milestone|---                         |4.10.0

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed on trunk.


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

* [Bug middle-end/52478] -ftrapv calls the wrong functions in libgcc
  2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2014-07-28  9:06 ` rguenth at gcc dot gnu.org
@ 2014-07-29 11:11 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-07-29 11:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52478

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
Author: rguenth
Date: Tue Jul 29 11:10:49 2014
New Revision: 213153

URL: https://gcc.gnu.org/viewcvs?rev=213153&root=gcc&view=rev
Log:
2014-07-29  Richard Biener  <rguenther@suse.de>

    PR middle-end/52478
    * expr.c (expand_expr_real_2): Revert last change.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/expr.c


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

end of thread, other threads:[~2014-07-29 11:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-04 12:59 [Bug middle-end/52478] New: -ftrapv calls the wrong functions in libgcc burnus at gcc dot gnu.org
2012-03-05 10:19 ` [Bug middle-end/52478] " rguenth at gcc dot gnu.org
2012-03-05 10:43 ` burnus at gcc dot gnu.org
2012-03-05 12:02 ` rguenth at gcc dot gnu.org
2012-10-07 19:15 ` pinskia at gcc dot gnu.org
2014-07-24  9:17 ` rguenth at gcc dot gnu.org
2014-07-28  8:48 ` rguenth at gcc dot gnu.org
2014-07-28  9:06 ` rguenth at gcc dot gnu.org
2014-07-29 11:11 ` rguenth at gcc dot gnu.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).