This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gomp] PR24505 mixed types in predicate


Hi guys!

2.5.1 specifies that the condition in a for loop is computed in the
type of the loop variable, otherwise the behavior is undefined.

This patch fixes the problem in PR24505.

Bootstrapped and regtested on ppc-linux for C and C++.

OK?

	PR gomp/24505
	* c-omp.c (c_finish_omp_for): Canonicalize condition.
	* testsuite/gcc.dg/gomp/for-15.c: New.

Index: c-omp.c
===================================================================
--- c-omp.c	(revision 105937)
+++ c-omp.c	(working copy)
@@ -196,7 +196,36 @@
   else
     {
       bool cond_ok = false;
+      tree op0 = TREE_OPERAND (cond, 0);
+      tree op1 = TREE_OPERAND (cond, 1);
 
+      /* 2.5.1.  The comparison in the condition is computed in the type
+	 of DECL, otherwise the behavior is undefined.
+
+	 For example:
+	   long n; int i;
+	   i < n;
+
+	according to ISO will be evaluated as:
+	   (long)i < n;
+
+	We want to force:
+	   i < (int)n;  */
+      if (TREE_CODE (op0) == NOP_EXPR
+	  && decl == TREE_OPERAND (op0, 0))
+	{
+	  TREE_OPERAND (cond, 0) = TREE_OPERAND (op0, 0);
+	  TREE_OPERAND (cond, 1) = build1 (NOP_EXPR, TREE_TYPE (decl),
+					   TREE_OPERAND (cond, 1));
+	}
+      else if (TREE_CODE (op1) == NOP_EXPR
+	       && decl == TREE_OPERAND (op1, 0))
+	{
+	  TREE_OPERAND (cond, 1) = TREE_OPERAND (op1, 0);
+	  TREE_OPERAND (cond, 0) = build1 (NOP_EXPR, TREE_TYPE (decl),
+					   TREE_OPERAND (cond, 0));
+	}
+
       if (EXPR_HAS_LOCATION (cond))
 	elocus = EXPR_LOCATION (cond);
 
Index: testsuite/gcc.dg/gomp/for-15.c
===================================================================
--- testsuite/gcc.dg/gomp/for-15.c	(revision 0)
+++ testsuite/gcc.dg/gomp/for-15.c	(revision 0)
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fopenmp" } */
+
+void foo()
+{
+	long n = 10;
+	int i;
+#pragma omp for
+	for (i=0; i < n; ++i) ;
+#pragma omp for
+	for (i=0; n > i; ++i) ;
+}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]