1 /* This file contains routines to construct and validate Cilk Plus
2    constructs within the C and C++ front ends.
3 
4    Copyright (C) 2013-2016 Free Software Foundation, Inc.
5    Contributed by Aldy Hernandez <aldyh@redhat.com>.
6 
7 This file is part of GCC.
8 
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13 
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "c-common.h"
27 
28 /* Validate the body of a _Cilk_for construct or a <#pragma simd> for
29    loop.
30 
31    Returns true if there were no errors, false otherwise.  */
32 
33 bool
c_check_cilk_loop(location_t loc,tree decl)34 c_check_cilk_loop (location_t loc, tree decl)
35 {
36   if (TREE_THIS_VOLATILE (decl))
37     {
38       error_at (loc, "iteration variable cannot be volatile");
39       return false;
40     }
41   return true;
42 }
43 
44 /* Validate and emit code for <#pragma simd> clauses.  */
45 
46 tree
c_finish_cilk_clauses(tree clauses)47 c_finish_cilk_clauses (tree clauses)
48 {
49   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
50     {
51       tree prev = clauses;
52 
53       /* If a variable appears in a linear clause it cannot appear in
54 	 any other OMP clause.  */
55       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
56 	for (tree c2 = clauses; c2; c2 = OMP_CLAUSE_CHAIN (c2))
57 	  {
58 	    if (c == c2)
59 	      continue;
60 	    enum omp_clause_code code = OMP_CLAUSE_CODE (c2);
61 
62 	    switch (code)
63 	      {
64 	      case OMP_CLAUSE_LINEAR:
65 	      case OMP_CLAUSE_PRIVATE:
66 	      case OMP_CLAUSE_FIRSTPRIVATE:
67 	      case OMP_CLAUSE_LASTPRIVATE:
68 	      case OMP_CLAUSE_REDUCTION:
69 		break;
70 
71 	      case OMP_CLAUSE_SAFELEN:
72 		goto next;
73 
74 	      default:
75 		gcc_unreachable ();
76 	      }
77 
78 	    if (OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (c2))
79 	      {
80 		error_at (OMP_CLAUSE_LOCATION (c2),
81 			  "variable appears in more than one clause");
82 		inform (OMP_CLAUSE_LOCATION (c),
83 			"other clause defined here");
84 		// Remove problematic clauses.
85 		OMP_CLAUSE_CHAIN (prev) = OMP_CLAUSE_CHAIN (c2);
86 	      }
87 	  next:
88 	    prev = c2;
89 	  }
90     }
91   return clauses;
92 }
93 
94 /* Calculate number of iterations of CILK_FOR.  */
95 
96 tree
cilk_for_number_of_iterations(tree cilk_for)97 cilk_for_number_of_iterations (tree cilk_for)
98 {
99   tree t, v, n1, n2, step, type, init, cond, incr, itype;
100   enum tree_code cond_code;
101   location_t loc = EXPR_LOCATION (cilk_for);
102 
103   init = TREE_VEC_ELT (OMP_FOR_INIT (cilk_for), 0);
104   v = TREE_OPERAND (init, 0);
105   cond = TREE_VEC_ELT (OMP_FOR_COND (cilk_for), 0);
106   incr = TREE_VEC_ELT (OMP_FOR_INCR (cilk_for), 0);
107   type = TREE_TYPE (v);
108 
109   gcc_assert (TREE_CODE (TREE_TYPE (v)) == INTEGER_TYPE
110 	      || TREE_CODE (TREE_TYPE (v)) == POINTER_TYPE);
111   n1 = TREE_OPERAND (init, 1);
112   cond_code = TREE_CODE (cond);
113   n2 = TREE_OPERAND (cond, 1);
114   switch (cond_code)
115     {
116     case LT_EXPR:
117     case GT_EXPR:
118     case NE_EXPR:
119       break;
120     case LE_EXPR:
121       if (POINTER_TYPE_P (TREE_TYPE (n2)))
122 	n2 = fold_build_pointer_plus_hwi_loc (loc, n2, 1);
123       else
124 	n2 = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (n2), n2,
125 			      build_int_cst (TREE_TYPE (n2), 1));
126       cond_code = LT_EXPR;
127       break;
128     case GE_EXPR:
129       if (POINTER_TYPE_P (TREE_TYPE (n2)))
130 	n2 = fold_build_pointer_plus_hwi_loc (loc, n2, -1);
131       else
132 	n2 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (n2), n2,
133 			      build_int_cst (TREE_TYPE (n2), 1));
134       cond_code = GT_EXPR;
135       break;
136     default:
137       gcc_unreachable ();
138     }
139 
140   step = NULL_TREE;
141   switch (TREE_CODE (incr))
142     {
143     case PREINCREMENT_EXPR:
144     case POSTINCREMENT_EXPR:
145       step = build_int_cst (TREE_TYPE (v), 1);
146       break;
147     case PREDECREMENT_EXPR:
148     case POSTDECREMENT_EXPR:
149       step = build_int_cst (TREE_TYPE (v), -1);
150       break;
151     case MODIFY_EXPR:
152       t = TREE_OPERAND (incr, 1);
153       gcc_assert (TREE_OPERAND (t, 0) == v);
154       switch (TREE_CODE (t))
155 	{
156 	case PLUS_EXPR:
157 	  step = TREE_OPERAND (t, 1);
158 	  break;
159 	case POINTER_PLUS_EXPR:
160 	  step = fold_convert (ssizetype, TREE_OPERAND (t, 1));
161 	  break;
162 	case MINUS_EXPR:
163 	  step = TREE_OPERAND (t, 1);
164 	  step = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (step), step);
165 	  break;
166 	default:
167 	  gcc_unreachable ();
168 	}
169       break;
170     default:
171       gcc_unreachable ();
172     }
173 
174   itype = type;
175   if (POINTER_TYPE_P (itype))
176     itype = signed_type_for (itype);
177   if (cond_code == NE_EXPR)
178     {
179       /* For NE_EXPR, we need to find out if the iterator increases
180 	 or decreases from whether step is positive or negative.  */
181       tree stype = itype;
182       if (TYPE_UNSIGNED (stype))
183 	stype = signed_type_for (stype);
184       cond = fold_build2_loc (loc, GE_EXPR, boolean_type_node,
185 			      fold_convert_loc (loc, stype, step),
186 			      build_int_cst (stype, 0));
187       t = fold_build3_loc (loc, COND_EXPR, itype, cond,
188 			   build_int_cst (itype, -1),
189 			   build_int_cst (itype, 1));
190     }
191   else
192     t = build_int_cst (itype, (cond_code == LT_EXPR ? -1 : 1));
193   t = fold_build2_loc (loc, PLUS_EXPR, itype,
194 		       fold_convert_loc (loc, itype, step), t);
195   t = fold_build2_loc (loc, PLUS_EXPR, itype, t,
196 		       fold_convert_loc (loc, itype, n2));
197   t = fold_build2_loc (loc, MINUS_EXPR, itype, t,
198 		       fold_convert_loc (loc, itype, n1));
199   if (TYPE_UNSIGNED (itype) && cond_code == GT_EXPR)
200     t = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype,
201 			 fold_build1_loc (loc, NEGATE_EXPR, itype, t),
202 			 fold_build1_loc (loc, NEGATE_EXPR, itype,
203 					  fold_convert_loc (loc, itype,
204 							    step)));
205   else if (TYPE_UNSIGNED (itype) && cond_code == NE_EXPR)
206     {
207       tree t1
208 	= fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
209 			   fold_convert_loc (loc, itype, step));
210       tree t2
211 	= fold_build2_loc (loc, TRUNC_DIV_EXPR, itype,
212 			   fold_build1_loc (loc, NEGATE_EXPR, itype, t),
213 			   fold_build1_loc (loc, NEGATE_EXPR, itype,
214 					    fold_convert_loc (loc, itype,
215 							      step)));
216       t = fold_build3_loc (loc, COND_EXPR, itype, cond, t1, t2);
217     }
218   else
219     t = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
220 			 fold_convert_loc (loc, itype, step));
221   cond = fold_build2_loc (loc, cond_code, boolean_type_node, n1, n2);
222   t = fold_build3_loc (loc, COND_EXPR, itype, cond, t,
223 		       build_int_cst (itype, 0));
224   return t;
225 }
226