Bug 52353 - -ftrapv -fnon-call-exceptions does not work
Summary: -ftrapv -fnon-call-exceptions does not work
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-02-23 13:00 UTC by Richard Biener
Modified: 2012-03-05 13:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.0
Known to fail:
Last reconfirmed:


Attachments
patch (758 bytes, patch)
2012-02-23 13:05 UTC, Richard Biener
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2012-02-23 13:00:25 UTC
The testcase from PR47086

void
foo ()
{
  int n = 0;
  while (1)
    {
      int i[n % 1];
      n++;
    }
}

reveals that while on the tree level n++ has an outgoing EH edge, RTL
expansion leaves us with

;; n_5 = n_1 + 1;

(insn 12 11 13 (set (reg:DI 67)
        (sign_extend:DI (reg/v:SI 61 [ n ]))) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (nil))

(insn 13 12 14 (set (reg:DI 4 si)
        (const_int 1 [0x1])) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (nil))

(insn 14 13 15 (set (reg:DI 5 di)
        (reg:DI 67)) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (nil))

(call_insn/u 15 14 16 (set (reg:DI 0 ax)
        (call (mem:QI (symbol_ref:DI ("__addvdi3") [flags 0x41]) [0 S1 A8])
            (const_int 0 [0]))) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (expr_list:REG_EH_REGION (const_int -2147483648 [0xffffffff80000000])
        (nil))
    (expr_list:REG_DEP_TRUE (use (reg:DI 4 si))
        (expr_list:REG_DEP_TRUE (use (reg:DI 5 di))
            (nil))))

(insn 16 15 17 (set (reg:DI 68)
        (reg:DI 0 ax)) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (expr_list:REG_EQUAL (plus:DI (reg:DI 67)
            (const_int 1 [0x1]))
        (nil)))

(insn 17 16 0 (set (reg/v:SI 63 [ n ])
        (subreg:SI (reg:DI 68) 0)) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (nil))

note that the call is marked CONST.  The EH edge is simply thrown away:

;; Basic block 4 , prev 3, next 5, loop_depth 1, count 0, freq 10000, maybe hot, flags: new reachable rtl modified.
;; Predecessors:  3 [100.0%]  (fallthru) 5 [100.0%]  (dfs_back)
...
(call_insn/u 15 14 16 4 (set (reg:DI 0 ax)
        (call (mem:QI (symbol_ref:DI ("__addvdi3") [flags 0x41]) [0 S1 A8])
            (const_int 0 [0]))) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (expr_list:REG_EH_REGION (const_int -2147483648 [0xffffffff80000000])
        (nil))
    (expr_list:REG_DEP_TRUE (use (reg:DI 4 si))
        (expr_list:REG_DEP_TRUE (use (reg:DI 5 di))
            (nil))))

(insn 16 15 17 4 (set (reg:DI 68)
        (reg:DI 0 ax)) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (expr_list:REG_EQUAL (plus:DI (reg:DI 67)
            (const_int 1 [0x1]))
        (nil)))

(insn 17 16 18 4 (set (reg/v:SI 63 [ n ])
        (subreg:SI (reg:DI 68) 0)) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 -1
     (nil))

;; Successors:  5 [100.0%]  (fallthru)


So we seem to leave around the libcall because of the EH side-effect
but do not consider that call as possibly throwing (as it is marked
nothrow on RTL) in find_many_sub_basic_blocks
(at least) and DCE it later, too (sounds similar to PR19020).

Currently generated assembly for gcc.dg/pr47086.c on x86_64 is

foo:
.LFB0:
        .cfi_startproc
        .cfi_personality 0x3,__gcc_personality_v0
        pushq   %rbx
        .cfi_def_cfa_offset 16
        .cfi_offset 3, -16
        movl    $0, %ebx
.L2:
        addl    $1, %ebx
        jmp     .L2


I'm running into this issue when removing unreachable blocks during
expansion, then the landing pad unwind info expansion ICEs because the landing
pad was removed.

We mark the call as not throwing via emit_libcall_block because we pass
it (plus (reg) 1) as EQUIV, which may not trap.
Comment 1 Richard Biener 2012-02-23 13:05:30 UTC
Created attachment 26732 [details]
patch

Patch that fixes my issue and works until rtl_dce, which still removes the call,
even though it throws internally

(call_insn/u 15 13 36 3 (set (reg:DI 0 ax)
        (call (mem:QI (symbol_ref:DI ("__addvdi3") [flags 0x41]) [0 S1 A8])
            (const_int 0 [0]))) /space/rguenther/src/svn/trunk/gcc/testsuite/gcc.dg/pr47086.c:11 636 {*call_value}
     (expr_list:REG_DEAD (reg:DI 5 di)
        (expr_list:REG_DEAD (reg:DI 4 si)
            (expr_list:REG_UNUSED (reg:DI 0 ax)
                (expr_list:REG_EH_REGION (const_int 1 [0x1])
                    (nil)))))
    (expr_list:REG_DEP_TRUE (use (reg:DI 4 si))
        (expr_list:REG_DEP_TRUE (use (reg:DI 5 di))
            (nil))))

;; Successors:  5 (ab,abcall,eh,loop_exit) 4 [100.0%]  (fallthru)
;; lr  out       3 [bx] 6 [bp] 7 [sp] 16 [argp] 20 [frame] 41 [r12]
Comment 2 Richard Biener 2012-03-05 13:08:59 UTC
Author: rguenth
Date: Mon Mar  5 13:08:55 2012
New Revision: 184932

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=184932
Log:
2012-03-05  Richard Guenther  <rguenther@suse.de>

	PR middle-end/52353
	* optabs.h (trapv_unoptab_p): New function.
	(trapv_binoptab_p): Likewise.
	* optabs.c (expand_binop): Use emit_libcall_block_1 with
	a proper equiv_may_trap argument.
	(expand_unop): Likewise.
	(emit_libcall_block_1): Take extra argument whether the
	instruction may trap.  Renamed from ...
	(emit_libcall_block): ... this.  New wrapper.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/optabs.c
    trunk/gcc/optabs.h
Comment 3 Richard Biener 2012-03-05 13:10:27 UTC
Fixed for trunk.