Bug 52478 - -ftrapv calls the wrong functions in libgcc
Summary: -ftrapv calls the wrong functions in libgcc
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
: 54846 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-03-04 12:58 UTC by Tobias Burnus
Modified: 2014-07-29 11:11 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.10.0
Known to fail:
Last reconfirmed: 2012-03-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2012-03-04 12:58:24 UTC
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);
}
Comment 1 Richard Biener 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.
Comment 2 Tobias Burnus 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!
Comment 3 Richard Biener 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.
Comment 4 Andrew Pinski 2012-10-07 19:15:25 UTC
*** Bug 54846 has been marked as a duplicate of this bug. ***
Comment 5 Richard Biener 2014-07-24 09:17:36 UTC
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.
Comment 6 Richard Biener 2014-07-28 08:48:10 UTC
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
Comment 7 Richard Biener 2014-07-28 09:06:01 UTC
Fixed on trunk.
Comment 8 Richard Biener 2014-07-29 11:11:22 UTC
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