public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH, middle-end]: Fix PR 37286, ICE subst_stack_regs_pat, at reg-stack.c:1537
@ 2008-11-03 15:09 Uros Bizjak
  2008-11-05  9:24 ` Richard Guenther
  0 siblings, 1 reply; 2+ messages in thread
From: Uros Bizjak @ 2008-11-03 15:09 UTC (permalink / raw)
  To: GCC Patches

[-- Attachment #1: Type: text/plain, Size: 924 bytes --]

Hello!

This is another example of uninitialized variable confusing regstack.
Instead of ICEing on uninitialized stack slot, initialize variable
with a NaN. This way, we will generate FP exception if program flow
reaches the instruction (if exceptions are enabled), but we will be
able to compile the source.

BTW: Compiling original fortran testcase from the PR with -Wall, we indeed get:

pr37286.f90: In function 'gn_monte_rand':
pr37286.f90:31: warning: 'monte_temp' may be used uninitialized in this function

2008-11-03  Uros Bizjak  <ubizjak@gmail.com>

	PR middle-end/37286
	* reg-stack.c (subst_stack_regs_pat) [MINUS, DIV, MULT, PLUS]:
	Initialize uninitialized input registers with a NaN.

testsuite/ChangeLog:

2008-11-03  Uros Bizjak  <ubizjak@gmail.com>

	PR middle-end/37286
	* gfortran.dg/pr37286.f90: New test.

The patch was bootstrapped and regression tested on i686-pc-linux-gnu.
OK for mainline?

Uros.

[-- Attachment #2: p.diff.txt --]
[-- Type: text/plain, Size: 2986 bytes --]

Index: reg-stack.c
===================================================================
--- reg-stack.c	(revision 141546)
+++ reg-stack.c	(working copy)
@@ -1527,15 +1527,30 @@ subst_stack_regs_pat (rtx insn, stack re
 	    else
 	      {
 		/* Both operands are REG.  If neither operand is already
-		   at the top of stack, choose to make the one that is the dest
-		   the new top of stack.  */
+		   at the top of stack, choose to make the one that is the
+		   dest the new top of stack.  */
 
 		int src1_hard_regnum, src2_hard_regnum;
 
 		src1_hard_regnum = get_hard_regnum (regstack, *src1);
 		src2_hard_regnum = get_hard_regnum (regstack, *src2);
-		gcc_assert (src1_hard_regnum != -1);
-		gcc_assert (src2_hard_regnum != -1);
+
+		/* If the source is not live, this is yet another case of
+		   uninitialized variables.  Load up a NaN instead.  */
+		if (src1_hard_regnum == -1)
+		  {
+		    rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src1);
+		    rtx insn2 = emit_insn_before (pat2, insn);
+		    control_flow_insn_deleted
+		      |= move_nan_for_stack_reg (insn2, regstack, *src1);
+		  }
+		if (src2_hard_regnum == -1)
+		  {
+		    rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src2);
+		    rtx insn2 = emit_insn_before (pat2, insn);
+		    control_flow_insn_deleted
+		      |= move_nan_for_stack_reg (insn2, regstack, *src2);
+		  }
 
 		if (src1_hard_regnum != FIRST_STACK_REG
 		    && src2_hard_regnum != FIRST_STACK_REG)
Index: testsuite/gfortran.dg/pr37286.f90
===================================================================
--- testsuite/gfortran.dg/pr37286.f90	(revision 0)
+++ testsuite/gfortran.dg/pr37286.f90	(revision 0)
@@ -0,0 +1,58 @@
+! { dg-do compile }
+
+module general_rand
+  implicit none
+  private
+
+  integer, public, parameter :: GNDP = kind(1.0d0)
+
+  real(kind = GNDP), save :: &
+    gnc = 362436.0 / 16777216.0, &
+    gncd = 7654321.0 / 16777216.0, &
+    gncm = 16777213.0 / 16777216.0
+  integer, save :: &
+    gni97 = 97, &
+    gnj97 = 33
+
+  real(kind = GNDP), save :: gnu(97)
+
+contains
+  subroutine gn_fatal(message)
+    character(len = *), intent(in) :: message
+
+    stop 1 
+  end subroutine gn_fatal
+
+  function gn_monte_rand(min, max) result(monte)
+    real(kind = GNDP), intent(in) :: min 
+    real(kind = GNDP), intent(in) :: max
+    real(kind = GNDP) :: monte
+
+    real :: monte_temp
+
+    if (min > max) then
+      call gn_fatal('gn_monte_rand: min > max')
+    else if (min == max) then
+      call gn_fatal('gn_monte_rand: min = max: returning min')
+      monte_temp = min
+    else
+
+      monte_temp = gnu(gni97) - gnu(gnj97)
+      if (monte_temp < 0.0) then
+        monte_temp = monte_temp + 1.0
+      end if
+
+      gnu(gni97) = monte_temp
+      gni97 = gni97 - 1
+      if (gni97 == 0) then
+        gni97 = 97
+      end if
+    end if
+
+    monte = min + monte_temp * (max - min)
+
+  end function gn_monte_rand
+
+end module general_rand
+
+! { dg-final { cleanup-modules "general_rand" } }

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

* Re: [PATCH, middle-end]: Fix PR 37286, ICE subst_stack_regs_pat, at reg-stack.c:1537
  2008-11-03 15:09 [PATCH, middle-end]: Fix PR 37286, ICE subst_stack_regs_pat, at reg-stack.c:1537 Uros Bizjak
@ 2008-11-05  9:24 ` Richard Guenther
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Guenther @ 2008-11-05  9:24 UTC (permalink / raw)
  To: Uros Bizjak; +Cc: GCC Patches

On Mon, Nov 3, 2008 at 4:08 PM, Uros Bizjak <ubizjak@gmail.com> wrote:
> Hello!
>
> This is another example of uninitialized variable confusing regstack.
> Instead of ICEing on uninitialized stack slot, initialize variable
> with a NaN. This way, we will generate FP exception if program flow
> reaches the instruction (if exceptions are enabled), but we will be
> able to compile the source.
>
> BTW: Compiling original fortran testcase from the PR with -Wall, we indeed get:
>
> pr37286.f90: In function 'gn_monte_rand':
> pr37286.f90:31: warning: 'monte_temp' may be used uninitialized in this function
>
> 2008-11-03  Uros Bizjak  <ubizjak@gmail.com>
>
>        PR middle-end/37286
>        * reg-stack.c (subst_stack_regs_pat) [MINUS, DIV, MULT, PLUS]:
>        Initialize uninitialized input registers with a NaN.
>
> testsuite/ChangeLog:
>
> 2008-11-03  Uros Bizjak  <ubizjak@gmail.com>
>
>        PR middle-end/37286
>        * gfortran.dg/pr37286.f90: New test.
>
> The patch was bootstrapped and regression tested on i686-pc-linux-gnu.
> OK for mainline?

Ok.

Thanks,
Richard.

> Uros.
>

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

end of thread, other threads:[~2008-11-05  9:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-03 15:09 [PATCH, middle-end]: Fix PR 37286, ICE subst_stack_regs_pat, at reg-stack.c:1537 Uros Bizjak
2008-11-05  9:24 ` Richard Guenther

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).