diff mbox

Reorder step != +-1 do code to allow empty latch (PR fortran/52865)

Message ID 20130116152733.GH7269@tucnak.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Jan. 16, 2013, 3:27 p.m. UTC
Hi!

As discussed in the PR, this patch performs the decrement of countm1
before the condition, so that the loop can have empty latch block.
The testcase from the PR then can be vectorized.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2013-01-16  Jakub Jelinek  <jakub@redhat.com>

	PR fortran/52865
	* trans-stmt.c (gfc_trans_do): Put countm1-- before conditional
	and use value of countm1 before the decrement in the condition.


	Jakub

Comments

Tobias Burnus Jan. 16, 2013, 3:46 p.m. UTC | #1
Jakub Jelinek wrote:
> As discussed in the PR, this patch performs the decrement of countm1
> before the condition, so that the loop can have empty latch block.
> The testcase from the PR then can be vectorized.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK from the FE side.

[I leave it to you and Richard whether you prefer this version or the 
Bool version; quoting from this PR: "Not sure if it is better this way, 
or with doing assignment of the condition result into a bool and using 
it later (as done in the patch for the other PR)." (Other PR = PR 53957.)]

Thanks for the patch!

Tobias

> 2013-01-16  Jakub Jelinek  <jakub@redhat.com>
>
> 	PR fortran/52865
> 	* trans-stmt.c (gfc_trans_do): Put countm1-- before conditional
> 	and use value of countm1 before the decrement in the condition.
Richard Biener Jan. 16, 2013, 3:48 p.m. UTC | #2
On Wed, 16 Jan 2013, Tobias Burnus wrote:

> Jakub Jelinek wrote:
> > As discussed in the PR, this patch performs the decrement of countm1
> > before the condition, so that the loop can have empty latch block.
> > The testcase from the PR then can be vectorized.
> > 
> > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> OK from the FE side.
> 
> [I leave it to you and Richard whether you prefer this version or the Bool
> version; quoting from this PR: "Not sure if it is better this way, or with
> doing assignment of the condition result into a bool and using it later (as
> done in the patch for the other PR)." (Other PR = PR 53957.)]

Jakubs version is better I suppose.

Richard.
diff mbox

Patch

--- gcc/fortran/trans-stmt.c.jj	2013-01-11 09:02:50.000000000 +0100
+++ gcc/fortran/trans-stmt.c	2013-01-16 12:47:44.191683738 +0100
@@ -1518,8 +1518,9 @@  gfc_trans_simple_do (gfc_code * code, st
        body;
 cycle_label:
        dovar += step
-       if (countm1 ==0) goto exit_label;
+       countm1t = countm1;
        countm1--;
+       if (countm1t == 0) goto exit_label;
      }
 exit_label:
 
@@ -1739,19 +1740,23 @@  gfc_trans_do (gfc_code * code, tree exit
   if (gfc_option.rtcheck & GFC_RTCHECK_DO)
     gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
 
-  /* End with the loop condition.  Loop until countm1 == 0.  */
-  cond = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, countm1,
-			  build_int_cst (utype, 0));
-  tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
-  tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
-			 cond, tmp, build_empty_stmt (loc));
-  gfc_add_expr_to_block (&body, tmp);
+  /* Initialize countm1t.  */
+  tree countm1t = gfc_create_var (utype, "countm1t");
+  gfc_add_modify_loc (loc, &body, countm1t, countm1);
 
   /* Decrement the loop count.  */
   tmp = fold_build2_loc (loc, MINUS_EXPR, utype, countm1,
 			 build_int_cst (utype, 1));
   gfc_add_modify_loc (loc, &body, countm1, tmp);
 
+  /* End with the loop condition.  Loop until countm1t == 0.  */
+  cond = fold_build2_loc (loc, EQ_EXPR, boolean_type_node, countm1t,
+			  build_int_cst (utype, 0));
+  tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
+  tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
+			 cond, tmp, build_empty_stmt (loc));
+  gfc_add_expr_to_block (&body, tmp);
+
   /* End of loop body.  */
   tmp = gfc_finish_block (&body);