1*e4b17023SJohn Marino /* This file contains routines to construct GNU OpenMP constructs,
2*e4b17023SJohn Marino called from parsing in the C and C++ front ends.
3*e4b17023SJohn Marino
4*e4b17023SJohn Marino Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
5*e4b17023SJohn Marino Free Software Foundation, Inc.
6*e4b17023SJohn Marino Contributed by Richard Henderson <rth@redhat.com>,
7*e4b17023SJohn Marino Diego Novillo <dnovillo@redhat.com>.
8*e4b17023SJohn Marino
9*e4b17023SJohn Marino This file is part of GCC.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
12*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
13*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
14*e4b17023SJohn Marino version.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
18*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19*e4b17023SJohn Marino for more details.
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
22*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
23*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino #include "config.h"
26*e4b17023SJohn Marino #include "system.h"
27*e4b17023SJohn Marino #include "coretypes.h"
28*e4b17023SJohn Marino #include "tree.h"
29*e4b17023SJohn Marino #include "c-common.h"
30*e4b17023SJohn Marino #include "gimple.h" /* For create_tmp_var_raw. */
31*e4b17023SJohn Marino #include "langhooks.h"
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino
34*e4b17023SJohn Marino /* Complete a #pragma omp master construct. STMT is the structured-block
35*e4b17023SJohn Marino that follows the pragma. LOC is the l*/
36*e4b17023SJohn Marino
37*e4b17023SJohn Marino tree
c_finish_omp_master(location_t loc,tree stmt)38*e4b17023SJohn Marino c_finish_omp_master (location_t loc, tree stmt)
39*e4b17023SJohn Marino {
40*e4b17023SJohn Marino tree t = add_stmt (build1 (OMP_MASTER, void_type_node, stmt));
41*e4b17023SJohn Marino SET_EXPR_LOCATION (t, loc);
42*e4b17023SJohn Marino return t;
43*e4b17023SJohn Marino }
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino /* Complete a #pragma omp critical construct. STMT is the structured-block
46*e4b17023SJohn Marino that follows the pragma, NAME is the identifier in the pragma, or null
47*e4b17023SJohn Marino if it was omitted. LOC is the location of the #pragma. */
48*e4b17023SJohn Marino
49*e4b17023SJohn Marino tree
c_finish_omp_critical(location_t loc,tree body,tree name)50*e4b17023SJohn Marino c_finish_omp_critical (location_t loc, tree body, tree name)
51*e4b17023SJohn Marino {
52*e4b17023SJohn Marino tree stmt = make_node (OMP_CRITICAL);
53*e4b17023SJohn Marino TREE_TYPE (stmt) = void_type_node;
54*e4b17023SJohn Marino OMP_CRITICAL_BODY (stmt) = body;
55*e4b17023SJohn Marino OMP_CRITICAL_NAME (stmt) = name;
56*e4b17023SJohn Marino SET_EXPR_LOCATION (stmt, loc);
57*e4b17023SJohn Marino return add_stmt (stmt);
58*e4b17023SJohn Marino }
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino /* Complete a #pragma omp ordered construct. STMT is the structured-block
61*e4b17023SJohn Marino that follows the pragma. LOC is the location of the #pragma. */
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino tree
c_finish_omp_ordered(location_t loc,tree stmt)64*e4b17023SJohn Marino c_finish_omp_ordered (location_t loc, tree stmt)
65*e4b17023SJohn Marino {
66*e4b17023SJohn Marino tree t = build1 (OMP_ORDERED, void_type_node, stmt);
67*e4b17023SJohn Marino SET_EXPR_LOCATION (t, loc);
68*e4b17023SJohn Marino return add_stmt (t);
69*e4b17023SJohn Marino }
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino /* Complete a #pragma omp barrier construct. LOC is the location of
73*e4b17023SJohn Marino the #pragma. */
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino void
c_finish_omp_barrier(location_t loc)76*e4b17023SJohn Marino c_finish_omp_barrier (location_t loc)
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino tree x;
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino x = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
81*e4b17023SJohn Marino x = build_call_expr_loc (loc, x, 0);
82*e4b17023SJohn Marino add_stmt (x);
83*e4b17023SJohn Marino }
84*e4b17023SJohn Marino
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino /* Complete a #pragma omp taskwait construct. LOC is the location of the
87*e4b17023SJohn Marino pragma. */
88*e4b17023SJohn Marino
89*e4b17023SJohn Marino void
c_finish_omp_taskwait(location_t loc)90*e4b17023SJohn Marino c_finish_omp_taskwait (location_t loc)
91*e4b17023SJohn Marino {
92*e4b17023SJohn Marino tree x;
93*e4b17023SJohn Marino
94*e4b17023SJohn Marino x = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
95*e4b17023SJohn Marino x = build_call_expr_loc (loc, x, 0);
96*e4b17023SJohn Marino add_stmt (x);
97*e4b17023SJohn Marino }
98*e4b17023SJohn Marino
99*e4b17023SJohn Marino
100*e4b17023SJohn Marino /* Complete a #pragma omp taskyield construct. LOC is the location of the
101*e4b17023SJohn Marino pragma. */
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino void
c_finish_omp_taskyield(location_t loc)104*e4b17023SJohn Marino c_finish_omp_taskyield (location_t loc)
105*e4b17023SJohn Marino {
106*e4b17023SJohn Marino tree x;
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino x = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
109*e4b17023SJohn Marino x = build_call_expr_loc (loc, x, 0);
110*e4b17023SJohn Marino add_stmt (x);
111*e4b17023SJohn Marino }
112*e4b17023SJohn Marino
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino /* Complete a #pragma omp atomic construct. For CODE OMP_ATOMIC
115*e4b17023SJohn Marino the expression to be implemented atomically is LHS opcode= RHS.
116*e4b17023SJohn Marino For OMP_ATOMIC_READ V = LHS, for OMP_ATOMIC_CAPTURE_{NEW,OLD} LHS
117*e4b17023SJohn Marino opcode= RHS with the new or old content of LHS returned.
118*e4b17023SJohn Marino LOC is the location of the atomic statement. The value returned
119*e4b17023SJohn Marino is either error_mark_node (if the construct was erroneous) or an
120*e4b17023SJohn Marino OMP_ATOMIC* node which should be added to the current statement
121*e4b17023SJohn Marino tree with add_stmt. */
122*e4b17023SJohn Marino
123*e4b17023SJohn Marino tree
c_finish_omp_atomic(location_t loc,enum tree_code code,enum tree_code opcode,tree lhs,tree rhs,tree v,tree lhs1,tree rhs1)124*e4b17023SJohn Marino c_finish_omp_atomic (location_t loc, enum tree_code code,
125*e4b17023SJohn Marino enum tree_code opcode, tree lhs, tree rhs,
126*e4b17023SJohn Marino tree v, tree lhs1, tree rhs1)
127*e4b17023SJohn Marino {
128*e4b17023SJohn Marino tree x, type, addr;
129*e4b17023SJohn Marino
130*e4b17023SJohn Marino if (lhs == error_mark_node || rhs == error_mark_node
131*e4b17023SJohn Marino || v == error_mark_node || lhs1 == error_mark_node
132*e4b17023SJohn Marino || rhs1 == error_mark_node)
133*e4b17023SJohn Marino return error_mark_node;
134*e4b17023SJohn Marino
135*e4b17023SJohn Marino /* ??? According to one reading of the OpenMP spec, complex type are
136*e4b17023SJohn Marino supported, but there are no atomic stores for any architecture.
137*e4b17023SJohn Marino But at least icc 9.0 doesn't support complex types here either.
138*e4b17023SJohn Marino And lets not even talk about vector types... */
139*e4b17023SJohn Marino type = TREE_TYPE (lhs);
140*e4b17023SJohn Marino if (!INTEGRAL_TYPE_P (type)
141*e4b17023SJohn Marino && !POINTER_TYPE_P (type)
142*e4b17023SJohn Marino && !SCALAR_FLOAT_TYPE_P (type))
143*e4b17023SJohn Marino {
144*e4b17023SJohn Marino error_at (loc, "invalid expression type for %<#pragma omp atomic%>");
145*e4b17023SJohn Marino return error_mark_node;
146*e4b17023SJohn Marino }
147*e4b17023SJohn Marino
148*e4b17023SJohn Marino /* ??? Validate that rhs does not overlap lhs. */
149*e4b17023SJohn Marino
150*e4b17023SJohn Marino /* Take and save the address of the lhs. From then on we'll reference it
151*e4b17023SJohn Marino via indirection. */
152*e4b17023SJohn Marino addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
153*e4b17023SJohn Marino if (addr == error_mark_node)
154*e4b17023SJohn Marino return error_mark_node;
155*e4b17023SJohn Marino addr = save_expr (addr);
156*e4b17023SJohn Marino if (TREE_CODE (addr) != SAVE_EXPR
157*e4b17023SJohn Marino && (TREE_CODE (addr) != ADDR_EXPR
158*e4b17023SJohn Marino || TREE_CODE (TREE_OPERAND (addr, 0)) != VAR_DECL))
159*e4b17023SJohn Marino {
160*e4b17023SJohn Marino /* Make sure LHS is simple enough so that goa_lhs_expr_p can recognize
161*e4b17023SJohn Marino it even after unsharing function body. */
162*e4b17023SJohn Marino tree var = create_tmp_var_raw (TREE_TYPE (addr), NULL);
163*e4b17023SJohn Marino DECL_CONTEXT (var) = current_function_decl;
164*e4b17023SJohn Marino addr = build4 (TARGET_EXPR, TREE_TYPE (addr), var, addr, NULL, NULL);
165*e4b17023SJohn Marino }
166*e4b17023SJohn Marino lhs = build_indirect_ref (loc, addr, RO_NULL);
167*e4b17023SJohn Marino
168*e4b17023SJohn Marino if (code == OMP_ATOMIC_READ)
169*e4b17023SJohn Marino {
170*e4b17023SJohn Marino x = build1 (OMP_ATOMIC_READ, type, addr);
171*e4b17023SJohn Marino SET_EXPR_LOCATION (x, loc);
172*e4b17023SJohn Marino return build_modify_expr (loc, v, NULL_TREE, NOP_EXPR,
173*e4b17023SJohn Marino loc, x, NULL_TREE);
174*e4b17023SJohn Marino return x;
175*e4b17023SJohn Marino }
176*e4b17023SJohn Marino
177*e4b17023SJohn Marino /* There are lots of warnings, errors, and conversions that need to happen
178*e4b17023SJohn Marino in the course of interpreting a statement. Use the normal mechanisms
179*e4b17023SJohn Marino to do this, and then take it apart again. */
180*e4b17023SJohn Marino x = build_modify_expr (input_location, lhs, NULL_TREE, opcode,
181*e4b17023SJohn Marino input_location, rhs, NULL_TREE);
182*e4b17023SJohn Marino if (x == error_mark_node)
183*e4b17023SJohn Marino return error_mark_node;
184*e4b17023SJohn Marino gcc_assert (TREE_CODE (x) == MODIFY_EXPR);
185*e4b17023SJohn Marino rhs = TREE_OPERAND (x, 1);
186*e4b17023SJohn Marino
187*e4b17023SJohn Marino /* Punt the actual generation of atomic operations to common code. */
188*e4b17023SJohn Marino if (code == OMP_ATOMIC)
189*e4b17023SJohn Marino type = void_type_node;
190*e4b17023SJohn Marino x = build2 (code, type, addr, rhs);
191*e4b17023SJohn Marino SET_EXPR_LOCATION (x, loc);
192*e4b17023SJohn Marino
193*e4b17023SJohn Marino /* Generally it is hard to prove lhs1 and lhs are the same memory
194*e4b17023SJohn Marino location, just diagnose different variables. */
195*e4b17023SJohn Marino if (rhs1
196*e4b17023SJohn Marino && TREE_CODE (rhs1) == VAR_DECL
197*e4b17023SJohn Marino && TREE_CODE (lhs) == VAR_DECL
198*e4b17023SJohn Marino && rhs1 != lhs)
199*e4b17023SJohn Marino {
200*e4b17023SJohn Marino if (code == OMP_ATOMIC)
201*e4b17023SJohn Marino error_at (loc, "%<#pragma omp atomic update%> uses two different variables for memory");
202*e4b17023SJohn Marino else
203*e4b17023SJohn Marino error_at (loc, "%<#pragma omp atomic capture%> uses two different variables for memory");
204*e4b17023SJohn Marino return error_mark_node;
205*e4b17023SJohn Marino }
206*e4b17023SJohn Marino
207*e4b17023SJohn Marino if (code != OMP_ATOMIC)
208*e4b17023SJohn Marino {
209*e4b17023SJohn Marino /* Generally it is hard to prove lhs1 and lhs are the same memory
210*e4b17023SJohn Marino location, just diagnose different variables. */
211*e4b17023SJohn Marino if (lhs1 && TREE_CODE (lhs1) == VAR_DECL && TREE_CODE (lhs) == VAR_DECL)
212*e4b17023SJohn Marino {
213*e4b17023SJohn Marino if (lhs1 != lhs)
214*e4b17023SJohn Marino {
215*e4b17023SJohn Marino error_at (loc, "%<#pragma omp atomic capture%> uses two different variables for memory");
216*e4b17023SJohn Marino return error_mark_node;
217*e4b17023SJohn Marino }
218*e4b17023SJohn Marino }
219*e4b17023SJohn Marino x = build_modify_expr (loc, v, NULL_TREE, NOP_EXPR,
220*e4b17023SJohn Marino loc, x, NULL_TREE);
221*e4b17023SJohn Marino if (rhs1 && rhs1 != lhs)
222*e4b17023SJohn Marino {
223*e4b17023SJohn Marino tree rhs1addr = build_unary_op (loc, ADDR_EXPR, rhs1, 0);
224*e4b17023SJohn Marino if (rhs1addr == error_mark_node)
225*e4b17023SJohn Marino return error_mark_node;
226*e4b17023SJohn Marino x = omit_one_operand_loc (loc, type, x, rhs1addr);
227*e4b17023SJohn Marino }
228*e4b17023SJohn Marino if (lhs1 && lhs1 != lhs)
229*e4b17023SJohn Marino {
230*e4b17023SJohn Marino tree lhs1addr = build_unary_op (loc, ADDR_EXPR, lhs1, 0);
231*e4b17023SJohn Marino if (lhs1addr == error_mark_node)
232*e4b17023SJohn Marino return error_mark_node;
233*e4b17023SJohn Marino if (code == OMP_ATOMIC_CAPTURE_OLD)
234*e4b17023SJohn Marino x = omit_one_operand_loc (loc, type, x, lhs1addr);
235*e4b17023SJohn Marino else
236*e4b17023SJohn Marino {
237*e4b17023SJohn Marino x = save_expr (x);
238*e4b17023SJohn Marino x = omit_two_operands_loc (loc, type, x, x, lhs1addr);
239*e4b17023SJohn Marino }
240*e4b17023SJohn Marino }
241*e4b17023SJohn Marino }
242*e4b17023SJohn Marino else if (rhs1 && rhs1 != lhs)
243*e4b17023SJohn Marino {
244*e4b17023SJohn Marino tree rhs1addr = build_unary_op (loc, ADDR_EXPR, rhs1, 0);
245*e4b17023SJohn Marino if (rhs1addr == error_mark_node)
246*e4b17023SJohn Marino return error_mark_node;
247*e4b17023SJohn Marino x = omit_one_operand_loc (loc, type, x, rhs1addr);
248*e4b17023SJohn Marino }
249*e4b17023SJohn Marino
250*e4b17023SJohn Marino return x;
251*e4b17023SJohn Marino }
252*e4b17023SJohn Marino
253*e4b17023SJohn Marino
254*e4b17023SJohn Marino /* Complete a #pragma omp flush construct. We don't do anything with
255*e4b17023SJohn Marino the variable list that the syntax allows. LOC is the location of
256*e4b17023SJohn Marino the #pragma. */
257*e4b17023SJohn Marino
258*e4b17023SJohn Marino void
c_finish_omp_flush(location_t loc)259*e4b17023SJohn Marino c_finish_omp_flush (location_t loc)
260*e4b17023SJohn Marino {
261*e4b17023SJohn Marino tree x;
262*e4b17023SJohn Marino
263*e4b17023SJohn Marino x = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
264*e4b17023SJohn Marino x = build_call_expr_loc (loc, x, 0);
265*e4b17023SJohn Marino add_stmt (x);
266*e4b17023SJohn Marino }
267*e4b17023SJohn Marino
268*e4b17023SJohn Marino
269*e4b17023SJohn Marino /* Check and canonicalize #pragma omp for increment expression.
270*e4b17023SJohn Marino Helper function for c_finish_omp_for. */
271*e4b17023SJohn Marino
272*e4b17023SJohn Marino static tree
check_omp_for_incr_expr(location_t loc,tree exp,tree decl)273*e4b17023SJohn Marino check_omp_for_incr_expr (location_t loc, tree exp, tree decl)
274*e4b17023SJohn Marino {
275*e4b17023SJohn Marino tree t;
276*e4b17023SJohn Marino
277*e4b17023SJohn Marino if (!INTEGRAL_TYPE_P (TREE_TYPE (exp))
278*e4b17023SJohn Marino || TYPE_PRECISION (TREE_TYPE (exp)) < TYPE_PRECISION (TREE_TYPE (decl)))
279*e4b17023SJohn Marino return error_mark_node;
280*e4b17023SJohn Marino
281*e4b17023SJohn Marino if (exp == decl)
282*e4b17023SJohn Marino return build_int_cst (TREE_TYPE (exp), 0);
283*e4b17023SJohn Marino
284*e4b17023SJohn Marino switch (TREE_CODE (exp))
285*e4b17023SJohn Marino {
286*e4b17023SJohn Marino CASE_CONVERT:
287*e4b17023SJohn Marino t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
288*e4b17023SJohn Marino if (t != error_mark_node)
289*e4b17023SJohn Marino return fold_convert_loc (loc, TREE_TYPE (exp), t);
290*e4b17023SJohn Marino break;
291*e4b17023SJohn Marino case MINUS_EXPR:
292*e4b17023SJohn Marino t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
293*e4b17023SJohn Marino if (t != error_mark_node)
294*e4b17023SJohn Marino return fold_build2_loc (loc, MINUS_EXPR,
295*e4b17023SJohn Marino TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
296*e4b17023SJohn Marino break;
297*e4b17023SJohn Marino case PLUS_EXPR:
298*e4b17023SJohn Marino t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
299*e4b17023SJohn Marino if (t != error_mark_node)
300*e4b17023SJohn Marino return fold_build2_loc (loc, PLUS_EXPR,
301*e4b17023SJohn Marino TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
302*e4b17023SJohn Marino t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 1), decl);
303*e4b17023SJohn Marino if (t != error_mark_node)
304*e4b17023SJohn Marino return fold_build2_loc (loc, PLUS_EXPR,
305*e4b17023SJohn Marino TREE_TYPE (exp), TREE_OPERAND (exp, 0), t);
306*e4b17023SJohn Marino break;
307*e4b17023SJohn Marino case COMPOUND_EXPR:
308*e4b17023SJohn Marino {
309*e4b17023SJohn Marino /* cp_build_modify_expr forces preevaluation of the RHS to make
310*e4b17023SJohn Marino sure that it is evaluated before the lvalue-rvalue conversion
311*e4b17023SJohn Marino is applied to the LHS. Reconstruct the original expression. */
312*e4b17023SJohn Marino tree op0 = TREE_OPERAND (exp, 0);
313*e4b17023SJohn Marino if (TREE_CODE (op0) == TARGET_EXPR
314*e4b17023SJohn Marino && !VOID_TYPE_P (TREE_TYPE (op0)))
315*e4b17023SJohn Marino {
316*e4b17023SJohn Marino tree op1 = TREE_OPERAND (exp, 1);
317*e4b17023SJohn Marino tree temp = TARGET_EXPR_SLOT (op0);
318*e4b17023SJohn Marino if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_binary
319*e4b17023SJohn Marino && TREE_OPERAND (op1, 1) == temp)
320*e4b17023SJohn Marino {
321*e4b17023SJohn Marino op1 = copy_node (op1);
322*e4b17023SJohn Marino TREE_OPERAND (op1, 1) = TARGET_EXPR_INITIAL (op0);
323*e4b17023SJohn Marino return check_omp_for_incr_expr (loc, op1, decl);
324*e4b17023SJohn Marino }
325*e4b17023SJohn Marino }
326*e4b17023SJohn Marino break;
327*e4b17023SJohn Marino }
328*e4b17023SJohn Marino default:
329*e4b17023SJohn Marino break;
330*e4b17023SJohn Marino }
331*e4b17023SJohn Marino
332*e4b17023SJohn Marino return error_mark_node;
333*e4b17023SJohn Marino }
334*e4b17023SJohn Marino
335*e4b17023SJohn Marino /* Validate and emit code for the OpenMP directive #pragma omp for.
336*e4b17023SJohn Marino DECLV is a vector of iteration variables, for each collapsed loop.
337*e4b17023SJohn Marino INITV, CONDV and INCRV are vectors containing initialization
338*e4b17023SJohn Marino expressions, controlling predicates and increment expressions.
339*e4b17023SJohn Marino BODY is the body of the loop and PRE_BODY statements that go before
340*e4b17023SJohn Marino the loop. */
341*e4b17023SJohn Marino
342*e4b17023SJohn Marino tree
c_finish_omp_for(location_t locus,tree declv,tree initv,tree condv,tree incrv,tree body,tree pre_body)343*e4b17023SJohn Marino c_finish_omp_for (location_t locus, tree declv, tree initv, tree condv,
344*e4b17023SJohn Marino tree incrv, tree body, tree pre_body)
345*e4b17023SJohn Marino {
346*e4b17023SJohn Marino location_t elocus;
347*e4b17023SJohn Marino bool fail = false;
348*e4b17023SJohn Marino int i;
349*e4b17023SJohn Marino
350*e4b17023SJohn Marino gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
351*e4b17023SJohn Marino gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
352*e4b17023SJohn Marino gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
353*e4b17023SJohn Marino for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
354*e4b17023SJohn Marino {
355*e4b17023SJohn Marino tree decl = TREE_VEC_ELT (declv, i);
356*e4b17023SJohn Marino tree init = TREE_VEC_ELT (initv, i);
357*e4b17023SJohn Marino tree cond = TREE_VEC_ELT (condv, i);
358*e4b17023SJohn Marino tree incr = TREE_VEC_ELT (incrv, i);
359*e4b17023SJohn Marino
360*e4b17023SJohn Marino elocus = locus;
361*e4b17023SJohn Marino if (EXPR_HAS_LOCATION (init))
362*e4b17023SJohn Marino elocus = EXPR_LOCATION (init);
363*e4b17023SJohn Marino
364*e4b17023SJohn Marino /* Validate the iteration variable. */
365*e4b17023SJohn Marino if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
366*e4b17023SJohn Marino && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
367*e4b17023SJohn Marino {
368*e4b17023SJohn Marino error_at (elocus, "invalid type for iteration variable %qE", decl);
369*e4b17023SJohn Marino fail = true;
370*e4b17023SJohn Marino }
371*e4b17023SJohn Marino
372*e4b17023SJohn Marino /* In the case of "for (int i = 0...)", init will be a decl. It should
373*e4b17023SJohn Marino have a DECL_INITIAL that we can turn into an assignment. */
374*e4b17023SJohn Marino if (init == decl)
375*e4b17023SJohn Marino {
376*e4b17023SJohn Marino elocus = DECL_SOURCE_LOCATION (decl);
377*e4b17023SJohn Marino
378*e4b17023SJohn Marino init = DECL_INITIAL (decl);
379*e4b17023SJohn Marino if (init == NULL)
380*e4b17023SJohn Marino {
381*e4b17023SJohn Marino error_at (elocus, "%qE is not initialized", decl);
382*e4b17023SJohn Marino init = integer_zero_node;
383*e4b17023SJohn Marino fail = true;
384*e4b17023SJohn Marino }
385*e4b17023SJohn Marino
386*e4b17023SJohn Marino init = build_modify_expr (elocus, decl, NULL_TREE, NOP_EXPR,
387*e4b17023SJohn Marino /* FIXME diagnostics: This should
388*e4b17023SJohn Marino be the location of the INIT. */
389*e4b17023SJohn Marino elocus,
390*e4b17023SJohn Marino init,
391*e4b17023SJohn Marino NULL_TREE);
392*e4b17023SJohn Marino }
393*e4b17023SJohn Marino gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
394*e4b17023SJohn Marino gcc_assert (TREE_OPERAND (init, 0) == decl);
395*e4b17023SJohn Marino
396*e4b17023SJohn Marino if (cond == NULL_TREE)
397*e4b17023SJohn Marino {
398*e4b17023SJohn Marino error_at (elocus, "missing controlling predicate");
399*e4b17023SJohn Marino fail = true;
400*e4b17023SJohn Marino }
401*e4b17023SJohn Marino else
402*e4b17023SJohn Marino {
403*e4b17023SJohn Marino bool cond_ok = false;
404*e4b17023SJohn Marino
405*e4b17023SJohn Marino if (EXPR_HAS_LOCATION (cond))
406*e4b17023SJohn Marino elocus = EXPR_LOCATION (cond);
407*e4b17023SJohn Marino
408*e4b17023SJohn Marino if (TREE_CODE (cond) == LT_EXPR
409*e4b17023SJohn Marino || TREE_CODE (cond) == LE_EXPR
410*e4b17023SJohn Marino || TREE_CODE (cond) == GT_EXPR
411*e4b17023SJohn Marino || TREE_CODE (cond) == GE_EXPR
412*e4b17023SJohn Marino || TREE_CODE (cond) == NE_EXPR
413*e4b17023SJohn Marino || TREE_CODE (cond) == EQ_EXPR)
414*e4b17023SJohn Marino {
415*e4b17023SJohn Marino tree op0 = TREE_OPERAND (cond, 0);
416*e4b17023SJohn Marino tree op1 = TREE_OPERAND (cond, 1);
417*e4b17023SJohn Marino
418*e4b17023SJohn Marino /* 2.5.1. The comparison in the condition is computed in
419*e4b17023SJohn Marino the type of DECL, otherwise the behavior is undefined.
420*e4b17023SJohn Marino
421*e4b17023SJohn Marino For example:
422*e4b17023SJohn Marino long n; int i;
423*e4b17023SJohn Marino i < n;
424*e4b17023SJohn Marino
425*e4b17023SJohn Marino according to ISO will be evaluated as:
426*e4b17023SJohn Marino (long)i < n;
427*e4b17023SJohn Marino
428*e4b17023SJohn Marino We want to force:
429*e4b17023SJohn Marino i < (int)n; */
430*e4b17023SJohn Marino if (TREE_CODE (op0) == NOP_EXPR
431*e4b17023SJohn Marino && decl == TREE_OPERAND (op0, 0))
432*e4b17023SJohn Marino {
433*e4b17023SJohn Marino TREE_OPERAND (cond, 0) = TREE_OPERAND (op0, 0);
434*e4b17023SJohn Marino TREE_OPERAND (cond, 1)
435*e4b17023SJohn Marino = fold_build1_loc (elocus, NOP_EXPR, TREE_TYPE (decl),
436*e4b17023SJohn Marino TREE_OPERAND (cond, 1));
437*e4b17023SJohn Marino }
438*e4b17023SJohn Marino else if (TREE_CODE (op1) == NOP_EXPR
439*e4b17023SJohn Marino && decl == TREE_OPERAND (op1, 0))
440*e4b17023SJohn Marino {
441*e4b17023SJohn Marino TREE_OPERAND (cond, 1) = TREE_OPERAND (op1, 0);
442*e4b17023SJohn Marino TREE_OPERAND (cond, 0)
443*e4b17023SJohn Marino = fold_build1_loc (elocus, NOP_EXPR, TREE_TYPE (decl),
444*e4b17023SJohn Marino TREE_OPERAND (cond, 0));
445*e4b17023SJohn Marino }
446*e4b17023SJohn Marino
447*e4b17023SJohn Marino if (decl == TREE_OPERAND (cond, 0))
448*e4b17023SJohn Marino cond_ok = true;
449*e4b17023SJohn Marino else if (decl == TREE_OPERAND (cond, 1))
450*e4b17023SJohn Marino {
451*e4b17023SJohn Marino TREE_SET_CODE (cond,
452*e4b17023SJohn Marino swap_tree_comparison (TREE_CODE (cond)));
453*e4b17023SJohn Marino TREE_OPERAND (cond, 1) = TREE_OPERAND (cond, 0);
454*e4b17023SJohn Marino TREE_OPERAND (cond, 0) = decl;
455*e4b17023SJohn Marino cond_ok = true;
456*e4b17023SJohn Marino }
457*e4b17023SJohn Marino
458*e4b17023SJohn Marino if (TREE_CODE (cond) == NE_EXPR
459*e4b17023SJohn Marino || TREE_CODE (cond) == EQ_EXPR)
460*e4b17023SJohn Marino {
461*e4b17023SJohn Marino if (!INTEGRAL_TYPE_P (TREE_TYPE (decl)))
462*e4b17023SJohn Marino cond_ok = false;
463*e4b17023SJohn Marino else if (operand_equal_p (TREE_OPERAND (cond, 1),
464*e4b17023SJohn Marino TYPE_MIN_VALUE (TREE_TYPE (decl)),
465*e4b17023SJohn Marino 0))
466*e4b17023SJohn Marino TREE_SET_CODE (cond, TREE_CODE (cond) == NE_EXPR
467*e4b17023SJohn Marino ? GT_EXPR : LE_EXPR);
468*e4b17023SJohn Marino else if (operand_equal_p (TREE_OPERAND (cond, 1),
469*e4b17023SJohn Marino TYPE_MAX_VALUE (TREE_TYPE (decl)),
470*e4b17023SJohn Marino 0))
471*e4b17023SJohn Marino TREE_SET_CODE (cond, TREE_CODE (cond) == NE_EXPR
472*e4b17023SJohn Marino ? LT_EXPR : GE_EXPR);
473*e4b17023SJohn Marino else
474*e4b17023SJohn Marino cond_ok = false;
475*e4b17023SJohn Marino }
476*e4b17023SJohn Marino }
477*e4b17023SJohn Marino
478*e4b17023SJohn Marino if (!cond_ok)
479*e4b17023SJohn Marino {
480*e4b17023SJohn Marino error_at (elocus, "invalid controlling predicate");
481*e4b17023SJohn Marino fail = true;
482*e4b17023SJohn Marino }
483*e4b17023SJohn Marino }
484*e4b17023SJohn Marino
485*e4b17023SJohn Marino if (incr == NULL_TREE)
486*e4b17023SJohn Marino {
487*e4b17023SJohn Marino error_at (elocus, "missing increment expression");
488*e4b17023SJohn Marino fail = true;
489*e4b17023SJohn Marino }
490*e4b17023SJohn Marino else
491*e4b17023SJohn Marino {
492*e4b17023SJohn Marino bool incr_ok = false;
493*e4b17023SJohn Marino
494*e4b17023SJohn Marino if (EXPR_HAS_LOCATION (incr))
495*e4b17023SJohn Marino elocus = EXPR_LOCATION (incr);
496*e4b17023SJohn Marino
497*e4b17023SJohn Marino /* Check all the valid increment expressions: v++, v--, ++v, --v,
498*e4b17023SJohn Marino v = v + incr, v = incr + v and v = v - incr. */
499*e4b17023SJohn Marino switch (TREE_CODE (incr))
500*e4b17023SJohn Marino {
501*e4b17023SJohn Marino case POSTINCREMENT_EXPR:
502*e4b17023SJohn Marino case PREINCREMENT_EXPR:
503*e4b17023SJohn Marino case POSTDECREMENT_EXPR:
504*e4b17023SJohn Marino case PREDECREMENT_EXPR:
505*e4b17023SJohn Marino if (TREE_OPERAND (incr, 0) != decl)
506*e4b17023SJohn Marino break;
507*e4b17023SJohn Marino
508*e4b17023SJohn Marino incr_ok = true;
509*e4b17023SJohn Marino if (POINTER_TYPE_P (TREE_TYPE (decl))
510*e4b17023SJohn Marino && TREE_OPERAND (incr, 1))
511*e4b17023SJohn Marino {
512*e4b17023SJohn Marino tree t = fold_convert_loc (elocus,
513*e4b17023SJohn Marino sizetype, TREE_OPERAND (incr, 1));
514*e4b17023SJohn Marino
515*e4b17023SJohn Marino if (TREE_CODE (incr) == POSTDECREMENT_EXPR
516*e4b17023SJohn Marino || TREE_CODE (incr) == PREDECREMENT_EXPR)
517*e4b17023SJohn Marino t = fold_build1_loc (elocus, NEGATE_EXPR, sizetype, t);
518*e4b17023SJohn Marino t = fold_build_pointer_plus (decl, t);
519*e4b17023SJohn Marino incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
520*e4b17023SJohn Marino }
521*e4b17023SJohn Marino break;
522*e4b17023SJohn Marino
523*e4b17023SJohn Marino case MODIFY_EXPR:
524*e4b17023SJohn Marino if (TREE_OPERAND (incr, 0) != decl)
525*e4b17023SJohn Marino break;
526*e4b17023SJohn Marino if (TREE_OPERAND (incr, 1) == decl)
527*e4b17023SJohn Marino break;
528*e4b17023SJohn Marino if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
529*e4b17023SJohn Marino && (TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl
530*e4b17023SJohn Marino || TREE_OPERAND (TREE_OPERAND (incr, 1), 1) == decl))
531*e4b17023SJohn Marino incr_ok = true;
532*e4b17023SJohn Marino else if ((TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR
533*e4b17023SJohn Marino || (TREE_CODE (TREE_OPERAND (incr, 1))
534*e4b17023SJohn Marino == POINTER_PLUS_EXPR))
535*e4b17023SJohn Marino && TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl)
536*e4b17023SJohn Marino incr_ok = true;
537*e4b17023SJohn Marino else
538*e4b17023SJohn Marino {
539*e4b17023SJohn Marino tree t = check_omp_for_incr_expr (elocus,
540*e4b17023SJohn Marino TREE_OPERAND (incr, 1),
541*e4b17023SJohn Marino decl);
542*e4b17023SJohn Marino if (t != error_mark_node)
543*e4b17023SJohn Marino {
544*e4b17023SJohn Marino incr_ok = true;
545*e4b17023SJohn Marino t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
546*e4b17023SJohn Marino incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
547*e4b17023SJohn Marino }
548*e4b17023SJohn Marino }
549*e4b17023SJohn Marino break;
550*e4b17023SJohn Marino
551*e4b17023SJohn Marino default:
552*e4b17023SJohn Marino break;
553*e4b17023SJohn Marino }
554*e4b17023SJohn Marino if (!incr_ok)
555*e4b17023SJohn Marino {
556*e4b17023SJohn Marino error_at (elocus, "invalid increment expression");
557*e4b17023SJohn Marino fail = true;
558*e4b17023SJohn Marino }
559*e4b17023SJohn Marino }
560*e4b17023SJohn Marino
561*e4b17023SJohn Marino TREE_VEC_ELT (initv, i) = init;
562*e4b17023SJohn Marino TREE_VEC_ELT (incrv, i) = incr;
563*e4b17023SJohn Marino }
564*e4b17023SJohn Marino
565*e4b17023SJohn Marino if (fail)
566*e4b17023SJohn Marino return NULL;
567*e4b17023SJohn Marino else
568*e4b17023SJohn Marino {
569*e4b17023SJohn Marino tree t = make_node (OMP_FOR);
570*e4b17023SJohn Marino
571*e4b17023SJohn Marino TREE_TYPE (t) = void_type_node;
572*e4b17023SJohn Marino OMP_FOR_INIT (t) = initv;
573*e4b17023SJohn Marino OMP_FOR_COND (t) = condv;
574*e4b17023SJohn Marino OMP_FOR_INCR (t) = incrv;
575*e4b17023SJohn Marino OMP_FOR_BODY (t) = body;
576*e4b17023SJohn Marino OMP_FOR_PRE_BODY (t) = pre_body;
577*e4b17023SJohn Marino
578*e4b17023SJohn Marino SET_EXPR_LOCATION (t, locus);
579*e4b17023SJohn Marino return add_stmt (t);
580*e4b17023SJohn Marino }
581*e4b17023SJohn Marino }
582*e4b17023SJohn Marino
583*e4b17023SJohn Marino
584*e4b17023SJohn Marino /* Divide CLAUSES into two lists: those that apply to a parallel
585*e4b17023SJohn Marino construct, and those that apply to a work-sharing construct. Place
586*e4b17023SJohn Marino the results in *PAR_CLAUSES and *WS_CLAUSES respectively. In
587*e4b17023SJohn Marino addition, add a nowait clause to the work-sharing list. LOC is the
588*e4b17023SJohn Marino location of the OMP_PARALLEL*. */
589*e4b17023SJohn Marino
590*e4b17023SJohn Marino void
c_split_parallel_clauses(location_t loc,tree clauses,tree * par_clauses,tree * ws_clauses)591*e4b17023SJohn Marino c_split_parallel_clauses (location_t loc, tree clauses,
592*e4b17023SJohn Marino tree *par_clauses, tree *ws_clauses)
593*e4b17023SJohn Marino {
594*e4b17023SJohn Marino tree next;
595*e4b17023SJohn Marino
596*e4b17023SJohn Marino *par_clauses = NULL;
597*e4b17023SJohn Marino *ws_clauses = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
598*e4b17023SJohn Marino
599*e4b17023SJohn Marino for (; clauses ; clauses = next)
600*e4b17023SJohn Marino {
601*e4b17023SJohn Marino next = OMP_CLAUSE_CHAIN (clauses);
602*e4b17023SJohn Marino
603*e4b17023SJohn Marino switch (OMP_CLAUSE_CODE (clauses))
604*e4b17023SJohn Marino {
605*e4b17023SJohn Marino case OMP_CLAUSE_PRIVATE:
606*e4b17023SJohn Marino case OMP_CLAUSE_SHARED:
607*e4b17023SJohn Marino case OMP_CLAUSE_FIRSTPRIVATE:
608*e4b17023SJohn Marino case OMP_CLAUSE_LASTPRIVATE:
609*e4b17023SJohn Marino case OMP_CLAUSE_REDUCTION:
610*e4b17023SJohn Marino case OMP_CLAUSE_COPYIN:
611*e4b17023SJohn Marino case OMP_CLAUSE_IF:
612*e4b17023SJohn Marino case OMP_CLAUSE_NUM_THREADS:
613*e4b17023SJohn Marino case OMP_CLAUSE_DEFAULT:
614*e4b17023SJohn Marino OMP_CLAUSE_CHAIN (clauses) = *par_clauses;
615*e4b17023SJohn Marino *par_clauses = clauses;
616*e4b17023SJohn Marino break;
617*e4b17023SJohn Marino
618*e4b17023SJohn Marino case OMP_CLAUSE_SCHEDULE:
619*e4b17023SJohn Marino case OMP_CLAUSE_ORDERED:
620*e4b17023SJohn Marino case OMP_CLAUSE_COLLAPSE:
621*e4b17023SJohn Marino OMP_CLAUSE_CHAIN (clauses) = *ws_clauses;
622*e4b17023SJohn Marino *ws_clauses = clauses;
623*e4b17023SJohn Marino break;
624*e4b17023SJohn Marino
625*e4b17023SJohn Marino default:
626*e4b17023SJohn Marino gcc_unreachable ();
627*e4b17023SJohn Marino }
628*e4b17023SJohn Marino }
629*e4b17023SJohn Marino }
630*e4b17023SJohn Marino
631*e4b17023SJohn Marino /* True if OpenMP sharing attribute of DECL is predetermined. */
632*e4b17023SJohn Marino
633*e4b17023SJohn Marino enum omp_clause_default_kind
c_omp_predetermined_sharing(tree decl)634*e4b17023SJohn Marino c_omp_predetermined_sharing (tree decl)
635*e4b17023SJohn Marino {
636*e4b17023SJohn Marino /* Variables with const-qualified type having no mutable member
637*e4b17023SJohn Marino are predetermined shared. */
638*e4b17023SJohn Marino if (TREE_READONLY (decl))
639*e4b17023SJohn Marino return OMP_CLAUSE_DEFAULT_SHARED;
640*e4b17023SJohn Marino
641*e4b17023SJohn Marino return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
642*e4b17023SJohn Marino }
643