Bug 18032 - [4.0.0] SH: wrong code for EH
Summary: [4.0.0] SH: wrong code for EH
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2004-10-16 08:21 UTC by Kazumoto Kojima
Modified: 2005-07-23 22:49 UTC (History)
2 users (show)

See Also:
Host: sh4-unknown-linux-gnu
Target: sh4-unknown-linux-gnu
Build: sh4-unknown-linux-gnu
Known to work:
Known to fail: 3.4.2 4.0.0
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazumoto Kojima 2004-10-16 08:21:58 UTC
The program below doesn't exit with 0 as expected when it's compiled
with -O2.  It works fine with -O1 or -O2 -fno-delayed-branch.

--
int *dummy;

void
bar (int *p)
{
  dummy = p;
}

void
foo (int x)
{
  int var;

  bar (&var);
  if (x)
    throw 1;
}

int
main ()
{ 
  try {
    foo (1);
  } catch (...) {
    return 0;
  }
  return 1;
}
--

With -O2, the function foo looks like:

_Z3fooi:
.LFB3:
	mov.l	r8,@-r15
.LCFI3:
	mov	r4,r8
	mov.l	r14,@-r15
.LCFI4:
	sts.l	pr,@-r15
.LCFI5:
	mov.l	.L11,r1
	add	#-4,r15
.LCFI6:
	mov	r15,r14
.LCFI7:
	jsr	@r1
	mov	r14,r4
	tst	r8,r8
	bf/s	.L10
	add	#4,r14
	mov	r14,r15
	lds.l	@r15+,pr
	mov.l	@r15+,r14
	rts	
	mov.l	@r15+,r8
	.align 5
.L10:
	mov.l	.L12,r0
	jsr	@r0
	mov	#4,r4
	mov	#1,r1
	mov.l	r1,@r0
	mov.l	.L13,r1
	mov	r0,r4
	mov.l	.L14,r5
	jsr	@r1
	mov	#0,r6
	...

Thus the throw part starting with .L10 is called after the excution of
"add #4,r14" in the delayed slot.  It seems that this doesn't match
the frame info of foo:

$ readelf -a ./a.out | grep foo
    99: 00400760   100 FUNC    GLOBAL DEFAULT   11 _Z3fooi

$ readelf --debug-dump=frames ./a.out
The section .eh_frame contains:
...
0000001c 00000028 00000020 FDE cie=00000000 pc=00400760..004007c4
  Augmentation data:     00 00 00 00

  DW_CFA_advance_loc: 2 to 00400762
  DW_CFA_def_cfa_offset: 4
  DW_CFA_offset: r8 at cfa-4
  DW_CFA_advance_loc: 4 to 00400766
  DW_CFA_def_cfa_offset: 8
  DW_CFA_advance_loc: 2 to 00400768
  DW_CFA_def_cfa_offset: 12
  DW_CFA_advance_loc: 4 to 0040076c
  DW_CFA_def_cfa_offset: 16
  DW_CFA_offset: r17 at cfa-12
  DW_CFA_offset: r14 at cfa-8
  DW_CFA_advance_loc: 2 to 0040076e
  DW_CFA_def_cfa_reg: r14
  DW_CFA_nop
  DW_CFA_nop

which assumes that r14 points the bottom of the frame when the throw
part is called.
Comment 1 Jorn Wolfgang Rennecke 2004-10-18 15:49:11 UTC
We don't emit call frame information for the epilogue.  If we did, than any
basic blocks that happen to come after the epilogue due to block reordering
would end up with incorrect cfi information, since we don't have any machinery
for writing compensating cfi when we skip over an epilogue.

Thus, the only way we have to make this work with the current infrastructure
is to avoid scheduling the epilogue with previous basic blocks when exception
handling is enabled (this also affects debugging information, but I'd say we
shouldn't pessimize -O2 code to get better debugging information).

There is already code in sh.c:sh_expand_epilogue to emit a blockage instruction
before the stack adjustments.  However, if a frame pointer is needed, the
initial adjustment of the frame pointer isn't adjusted, since this doesn't
matter for interrupts:
 
  if (frame_pointer_needed)
    {
      output_stack_adjust (frame_size, frame_pointer_rtx, e, &live_regs_mask);

      /* We must avoid moving the stack pointer adjustment past code
         which reads from the local frame, else an interrupt could
         occur after the SP adjustment and clobber data in the local
         frame.  */
      emit_insn (gen_blockage ());
      emit_insn (GEN_MOV (stack_pointer_rtx, frame_pointer_rtx));
    }

So, if flag_exceptions is set, we should emit this blockage before the frame
pointer adjustment.
Comment 2 Kazumoto Kojima 2004-10-18 22:43:46 UTC
I'm testing a patch you suggest with the usual bootstrap and
regression test.  I'll send it to gcc-patches if it passes.

--- ORIG/gcc/gcc/config/sh/sh.c	2004-10-08 07:46:30.000000000 +0900
+++ LOCAL/gcc/gcc/config/sh/sh.c	2004-10-19 07:20:50.000000000 +0900
@@ -5828,6 +5828,10 @@ sh_expand_epilogue (bool sibcall_p)
 
   if (frame_pointer_needed)
     {
+      /* We must avoid scheduling the epilogue with previous basic blocks
+	 when exception handling is enabled.  See PR/18032.  */
+      if (flag_exceptions)
+	emit_insn (gen_blockage ());
       output_stack_adjust (frame_size, frame_pointer_rtx, e, &live_regs_mask);
 
       /* We must avoid moving the stack pointer adjustment past code
Comment 3 GCC Commits 2004-10-20 13:57:31 UTC
Subject: Bug 18032

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	kkojima@gcc.gnu.org	2004-10-20 13:57:23

Modified files:
	gcc            : ChangeLog 
	gcc/config/sh  : sh.c 

Log message:
	PR target/18032
	* config/sh/sh.c (sh_expand_epilogue): Emit a blockage insn before
	the frame pointer adjustment when exception handling is enabled.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.5957&r2=2.5958
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/sh/sh.c.diff?cvsroot=gcc&r1=1.306&r2=1.307

Comment 4 Andrew Pinski 2004-10-20 17:39:37 UTC
Fixed.