2013-09-06 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / cp-array-notation.c
blobf4581f01e57e2a2d47d45a33fea58ad3873a657e
1 /* This file is part of the Intel(R) Cilk(TM) Plus support
2 It contains routines to handle Array Notation expression
3 handling routines in the C++ Compiler.
4 Copyright (C) 2013 Free Software Foundation, Inc.
5 Contributed by Balaji V. Iyer <balaji.v.iyer@intel.com>,
6 Intel Corporation
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* The Array Notation Transformation Technique:
26 An array notation expression has 4 major components:
27 1. The array name
28 2. Start Index
29 3. Number of elements we need to acess (we call it length)
30 4. Stride
32 So, if we have something like A[0:5:2], we are accessing A[0], A[2], A[4],
33 A[6] and A[8]. The user is responsible to make sure the access length does
34 not step outside the array's size.
36 In this section, I highlight the overall method on how array notations are
37 broken up into C/C++ code. Almost all the functions follows this step:
39 Let's say the user has used the array notation in a statement like this:
41 A[St1:Ln:Str1] = B[St2:Ln:Str2] + <NON ARRAY_NOT STMT>
43 where St{1,2} = Starting index, Ln = Number of elements we need to access,
44 and Str{1,2} = the stride.
45 Note: The length of both the array notation expressions must be the same.
47 The above expression is broken into the following:
49 for (Tmp_Var = 0; Tmp_Var < Ln; Tmp_Var++)
50 A[St1 + Tmp_Var * Str1] = B[St1 + Tmp_Var * Str2] + <NON_ARRAY_NOT_STMT>;
53 #include "config.h"
54 #include "system.h"
55 #include "coretypes.h"
56 #include "tree.h"
57 #include "cp-tree.h"
58 #include "c-family/c-common.h"
59 #include "diagnostic.h"
60 #include "tree-iterator.h"
61 #include "vec.h"
63 /* Creates a FOR_STMT with INIT, COND, INCR and BODY as the initializer,
64 condition, increment expression and the loop-body, respectively. */
66 static void
67 create_an_loop (tree init, tree cond, tree incr, tree body)
69 tree for_stmt;
71 finish_expr_stmt (init);
72 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
73 finish_for_init_stmt (for_stmt);
74 finish_for_cond (cond, for_stmt);
75 finish_for_expr (incr, for_stmt);
76 finish_expr_stmt (body);
77 finish_for_stmt (for_stmt);
80 /* If *VALUE is not a constant integer, then this function replaces it with
81 a variable to make it loop invariant for array notations. */
83 static inline void
84 make_triplet_val_inv (tree *value)
86 if (TREE_CODE (*value) != INTEGER_CST
87 && TREE_CODE (*value) != PARM_DECL
88 && TREE_CODE (*value) != VAR_DECL)
89 *value = get_temp_regvar (ptrdiff_type_node, *value);
92 /* Returns a vector of size RANK that contains an ARRAY_REF. This vector is
93 created using array notation-triplet information stored in AN_INFO. The
94 induction var is taken from AN_LOOP_INFO.
96 For example: For an array notation A[5:10:2], the vector start will be
97 of size 1 holding '5', stride of same size as start but holding the value of
98 as 2, and is_vector as true. Let's assume VAR is 'x'
99 This function returns a vector of size 1 with the following data:
100 A[5 + (x * 2)] .
103 static vec<tree, va_gc> *
104 create_array_refs (location_t loc, vec<vec<an_parts> > an_info,
105 vec<an_loop_parts> an_loop_info, size_t size, size_t rank)
107 tree ind_mult, ind_incr;
108 vec<tree, va_gc> *array_operand = NULL;
110 for (size_t ii = 0; ii < size; ii++)
111 if (an_info[ii][0].is_vector)
113 tree array_opr = an_info[ii][rank - 1].value;
114 for (int s_jj = rank -1; s_jj >= 0; s_jj--)
116 tree start = cp_fold_convert (ptrdiff_type_node,
117 an_info[ii][s_jj].start);
118 tree stride = cp_fold_convert (ptrdiff_type_node,
119 an_info[ii][s_jj].stride);
120 tree var = cp_fold_convert (ptrdiff_type_node,
121 an_loop_info[s_jj].var);
123 ind_mult = build2 (MULT_EXPR, TREE_TYPE (var), var, stride);
124 ind_incr = build2 (PLUS_EXPR, TREE_TYPE (var), start, ind_mult);
125 /* Array [ start_index + (induction_var * stride)] */
126 array_opr = grok_array_decl (loc, array_opr, ind_incr, false);
128 vec_safe_push (array_operand, array_opr);
130 else
131 vec_safe_push (array_operand, integer_one_node);
132 return array_operand;
135 /* Populates the INCR and CMP fields in *NODE with the increment
136 (of type POSTINCREMENT) and comparison (of TYPE LT_EXPR) expressions, using
137 data from AN_INFO. */
139 void
140 create_cmp_incr (location_t loc, vec <an_loop_parts> *node, size_t rank,
141 vec<vec<an_parts> > an_info, tsubst_flags_t complain)
143 for (size_t ii = 0; ii < rank; ii++)
145 (*node)[ii].incr = build_x_unary_op (loc, POSTINCREMENT_EXPR,
146 (*node)[ii].var, complain);
147 (*node)[ii].cmp = build_x_binary_op (loc, LT_EXPR, (*node)[ii].var,
148 TREE_CODE ((*node)[ii].var),
149 an_info[0][ii].length,
150 TREE_CODE (an_info[0][ii].length),
151 NULL, complain);
155 /* Replaces all the scalar expressions in *NODE. Returns a STATEMENT LIST that
156 holds the NODE along with the variables that hold the results of the
157 invariant expressions. */
159 static tree
160 replace_invariant_exprs (tree *node)
162 size_t ix = 0;
163 tree node_list = NULL_TREE;
164 tree t = NULL_TREE, new_var = NULL_TREE;
165 struct inv_list data;
167 data.list_values = NULL;
168 data.replacement = NULL;
169 data.additional_tcodes = NULL;
170 cp_walk_tree (node, find_inv_trees, (void *) &data, NULL);
172 if (vec_safe_length (data.list_values))
174 node_list = push_stmt_list ();
175 for (ix = 0; vec_safe_iterate (data.list_values, ix, &t); ix++)
177 /* Sometimes, when comma_expr has a function call in it, it will
178 typecast it to void. Find_inv_trees finds those nodes and so
179 if it void type, then don't bother creating a new var to hold
180 the return value. */
181 if (VOID_TYPE_P (TREE_TYPE (t)))
183 finish_expr_stmt (t);
184 new_var = void_zero_node;
186 else
187 new_var = get_temp_regvar (TREE_TYPE (t), t);
188 vec_safe_push (data.replacement, new_var);
190 cp_walk_tree (node, replace_inv_trees, (void *) &data, NULL);
191 node_list = pop_stmt_list (node_list);
193 return node_list;
196 /* Replace array notation's built-in function passed in AN_BUILTIN_FN with
197 the appropriate loop and computation (all stored in variable LOOP of type
198 tree node). The output of the function function is always a scalar and that
199 result is returned in *NEW_VAR. *NEW_VAR is NULL_TREE if the function is
200 __sec_reduce_mutating. */
202 static tree
203 expand_sec_reduce_builtin (tree an_builtin_fn, tree *new_var)
205 tree new_var_type = NULL_TREE, func_parm, new_yes_expr, new_no_expr;
206 tree array_ind_value = NULL_TREE, new_no_ind, new_yes_ind, new_no_list;
207 tree new_yes_list, new_cond_expr, new_expr = NULL_TREE;
208 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
209 size_t list_size = 0, rank = 0, ii = 0;
210 tree body, an_init, loop_with_init = alloc_stmt_list ();
211 tree array_op0, comp_node = NULL_TREE;
212 tree call_fn = NULL_TREE, identity_value = NULL_TREE;
213 tree init = NULL_TREE, cond_init = NULL_TREE;
214 enum tree_code code = NOP_EXPR;
215 location_t location = UNKNOWN_LOCATION;
216 vec<vec<an_parts> > an_info = vNULL;
217 vec<an_loop_parts> an_loop_info = vNULL;
218 enum built_in_function an_type =
219 is_cilkplus_reduce_builtin (CALL_EXPR_FN (an_builtin_fn));
220 vec <tree, va_gc> *func_args;
222 if (an_type == BUILT_IN_NONE)
223 return NULL_TREE;
225 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE
226 && an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
227 func_parm = CALL_EXPR_ARG (an_builtin_fn, 0);
228 else
230 call_fn = CALL_EXPR_ARG (an_builtin_fn, 2);
232 /* We need to do this because we are "faking" the builtin function types,
233 so the compiler does a bunch of typecasts and this will get rid of
234 all that! */
235 STRIP_NOPS (call_fn);
236 if (TREE_CODE (call_fn) != OVERLOAD
237 && TREE_CODE (call_fn) != FUNCTION_DECL)
238 call_fn = TREE_OPERAND (call_fn, 0);
239 identity_value = CALL_EXPR_ARG (an_builtin_fn, 0);
240 func_parm = CALL_EXPR_ARG (an_builtin_fn, 1);
241 STRIP_NOPS (identity_value);
243 STRIP_NOPS (func_parm);
245 location = EXPR_LOCATION (an_builtin_fn);
247 /* Note about using find_rank (): If find_rank returns false, then it must
248 have already reported an error, thus we just return an error_mark_node
249 without any doing any error emission. */
250 if (!find_rank (location, an_builtin_fn, an_builtin_fn, true, &rank))
251 return error_mark_node;
252 if (rank == 0)
253 return an_builtin_fn;
254 else if (rank > 1
255 && (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
256 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND))
258 error_at (location, "__sec_reduce_min_ind or __sec_reduce_max_ind cannot "
259 "have arrays with dimension greater than 1");
260 return error_mark_node;
263 extract_array_notation_exprs (func_parm, true, &array_list);
264 list_size = vec_safe_length (array_list);
265 switch (an_type)
267 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
268 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
269 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
270 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
271 new_var_type = TREE_TYPE ((*array_list)[0]);
272 break;
273 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
274 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
275 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
276 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
277 new_var_type = boolean_type_node;
278 break;
279 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
280 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
281 new_var_type = size_type_node;
282 break;
283 case BUILT_IN_CILKPLUS_SEC_REDUCE:
284 if (call_fn && identity_value)
285 new_var_type = TREE_TYPE ((*array_list)[0]);
286 break;
287 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
288 new_var_type = NULL_TREE;
289 break;
290 default:
291 gcc_unreachable ();
294 if (new_var_type && TREE_CODE (new_var_type) == ARRAY_TYPE)
295 new_var_type = TREE_TYPE (new_var_type);
296 an_loop_info.safe_grow_cleared (rank);
298 an_init = push_stmt_list ();
300 /* Assign the array notation components to variable so that they can satisfy
301 the exec-once rule. */
302 for (ii = 0; ii < list_size; ii++)
303 if (TREE_CODE ((*array_list)[ii]) == ARRAY_NOTATION_REF)
305 tree anode = (*array_list)[ii];
306 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
307 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
308 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
310 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
311 for (ii = 0; ii < rank; ii++)
313 tree typ = ptrdiff_type_node;
315 /* In this place, we are using get_temp_regvar instead of
316 create_temporary_var if an_type is SEC_REDUCE_MAX/MIN_IND because
317 the array_ind_value depends on this value being initalized to 0. */
318 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
319 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
320 an_loop_info[ii].var = get_temp_regvar (typ, build_zero_cst (typ));
321 else
323 an_loop_info[ii].var = create_temporary_var (typ);
324 add_decl_expr (an_loop_info[ii].var);
326 an_loop_info[ii].ind_init =
327 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
328 build_zero_cst (typ), tf_warning_or_error);
330 array_operand = create_array_refs (location, an_info, an_loop_info,
331 list_size, rank);
332 replace_array_notations (&func_parm, true, array_list, array_operand);
334 if (!TREE_TYPE (func_parm))
335 TREE_TYPE (func_parm) = TREE_TYPE ((*array_list)[0]);
337 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
338 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
339 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
340 array_ind_value = get_temp_regvar (TREE_TYPE (func_parm), func_parm);
342 array_op0 = (*array_operand)[0];
343 switch (an_type)
345 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
346 code = PLUS_EXPR;
347 init = build_zero_cst (new_var_type);
348 break;
349 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
350 code = MULT_EXPR;
351 init = build_one_cst (new_var_type);
352 break;
353 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
354 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
355 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO) ? EQ_EXPR
356 : NE_EXPR);
357 init = build_zero_cst (new_var_type);
358 cond_init = build_one_cst (new_var_type);
359 comp_node = build_zero_cst (TREE_TYPE (func_parm));
360 break;
361 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
362 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
363 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO) ? NE_EXPR
364 : EQ_EXPR);
365 init = build_one_cst (new_var_type);
366 cond_init = build_zero_cst (new_var_type);
367 comp_node = build_zero_cst (TREE_TYPE (func_parm));
368 break;
369 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
370 code = MAX_EXPR;
371 init = (TYPE_MIN_VALUE (new_var_type) ? TYPE_MIN_VALUE (new_var_type)
372 : func_parm);
373 break;
374 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
375 code = MIN_EXPR;
376 init = (TYPE_MAX_VALUE (new_var_type) ? TYPE_MAX_VALUE (new_var_type)
377 : func_parm);
378 break;
379 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
380 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
381 code = (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND ? LE_EXPR
382 : GE_EXPR);
383 init = an_loop_info[0].var;
384 break;
385 case BUILT_IN_CILKPLUS_SEC_REDUCE:
386 init = identity_value;
387 break;
388 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
389 init = NULL_TREE;
390 break;
391 default:
392 gcc_unreachable ();
395 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
396 *new_var = get_temp_regvar (new_var_type, init);
397 else
398 *new_var = NULL_TREE;
400 switch (an_type)
402 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
403 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
404 new_expr = build_x_modify_expr (location, *new_var, code, func_parm,
405 tf_warning_or_error);
406 break;
407 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
408 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
409 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
410 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
411 /* In all these cases, assume the false case is true and as soon as
412 we find a true case, set the true flag on and latch it in. */
413 new_yes_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
414 cond_init, tf_warning_or_error);
415 new_no_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
416 *new_var, tf_warning_or_error);
417 new_cond_expr = build_x_binary_op
418 (location, code, func_parm, TREE_CODE (func_parm), comp_node,
419 TREE_CODE (comp_node), NULL, tf_warning_or_error);
420 new_expr = build_x_conditional_expr (location, new_cond_expr,
421 new_yes_expr, new_no_expr,
422 tf_warning_or_error);
423 break;
424 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
425 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
426 new_cond_expr = build_x_binary_op
427 (location, code, *new_var, TREE_CODE (*new_var), func_parm,
428 TREE_CODE (func_parm), NULL, tf_warning_or_error);
429 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, func_parm,
430 tf_warning_or_error);
431 break;
432 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
433 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
434 new_yes_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
435 func_parm, tf_warning_or_error);
436 new_no_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
437 array_ind_value, tf_warning_or_error);
438 if (list_size > 1)
439 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
440 an_loop_info[0].var,
441 tf_warning_or_error);
442 else
443 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
444 TREE_OPERAND (array_op0, 1),
445 tf_warning_or_error);
446 new_no_ind = build_x_modify_expr (location, *new_var, NOP_EXPR, *new_var,
447 tf_warning_or_error);
448 new_yes_list = alloc_stmt_list ();
449 append_to_statement_list (new_yes_ind, &new_yes_list);
450 append_to_statement_list (new_yes_expr, &new_yes_list);
452 new_no_list = alloc_stmt_list ();
453 append_to_statement_list (new_no_ind, &new_no_list);
454 append_to_statement_list (new_no_expr, &new_no_list);
456 new_cond_expr = build_x_binary_op (location, code, array_ind_value,
457 TREE_CODE (array_ind_value), func_parm,
458 TREE_CODE (func_parm), NULL,
459 tf_warning_or_error);
460 new_expr = build_x_conditional_expr (location, new_cond_expr,
461 new_yes_list, new_no_list,
462 tf_warning_or_error);
463 break;
464 case BUILT_IN_CILKPLUS_SEC_REDUCE:
465 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
466 func_args = make_tree_vector ();
467 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
468 vec_safe_push (func_args, *new_var);
469 else
470 vec_safe_push (func_args, identity_value);
471 vec_safe_push (func_args, func_parm);
473 new_expr = finish_call_expr (call_fn, &func_args, false, true,
474 tf_warning_or_error);
475 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
476 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, new_expr,
477 tf_warning_or_error);
478 release_tree_vector (func_args);
479 break;
480 default:
481 gcc_unreachable ();
483 an_init = pop_stmt_list (an_init);
484 append_to_statement_list (an_init, &loop_with_init);
485 body = new_expr;
487 for (ii = 0; ii < rank; ii++)
489 tree new_loop = push_stmt_list ();
490 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
491 an_loop_info[ii].incr, body);
492 body = pop_stmt_list (new_loop);
494 append_to_statement_list (body, &loop_with_init);
496 an_info.release ();
497 an_loop_info.release ();
499 return loop_with_init;
502 /* Returns a loop with ARRAY_REF inside it with an appropriate modify expr.
503 The LHS and/or RHS will be array notation expressions that have a
504 MODIFYCODE. The location of the variable is specified by LOCATION. */
506 static tree
507 expand_an_in_modify_expr (location_t location, tree lhs,
508 enum tree_code modifycode, tree rhs,
509 tsubst_flags_t complain)
511 tree array_expr_lhs = NULL_TREE, array_expr_rhs = NULL_TREE;
512 tree array_expr = NULL_TREE;
513 tree body = NULL_TREE;
514 vec<tree> cond_expr = vNULL;
515 vec<tree, va_gc> *lhs_array_operand = NULL, *rhs_array_operand = NULL;
516 size_t lhs_rank = 0, rhs_rank = 0, ii = 0;
517 vec<tree, va_gc> *rhs_list = NULL, *lhs_list = NULL;
518 size_t rhs_list_size = 0, lhs_list_size = 0;
519 tree new_modify_expr, new_var = NULL_TREE, builtin_loop, scalar_mods;
520 bool found_builtin_fn = false;
521 tree an_init, loop_with_init = alloc_stmt_list ();
522 vec<vec<an_parts> > lhs_an_info = vNULL, rhs_an_info = vNULL;
523 vec<an_loop_parts> lhs_an_loop_info = vNULL, rhs_an_loop_info = vNULL;
525 if (!find_rank (location, rhs, rhs, false, &rhs_rank))
526 return error_mark_node;
527 extract_array_notation_exprs (rhs, false, &rhs_list);
528 rhs_list_size = vec_safe_length (rhs_list);
529 an_init = push_stmt_list ();
530 if (rhs_rank)
532 scalar_mods = replace_invariant_exprs (&rhs);
533 if (scalar_mods)
534 finish_expr_stmt (scalar_mods);
536 for (ii = 0; ii < rhs_list_size; ii++)
538 tree rhs_node = (*rhs_list)[ii];
539 if (TREE_CODE (rhs_node) == CALL_EXPR)
541 builtin_loop = expand_sec_reduce_builtin (rhs_node, &new_var);
542 if (builtin_loop == error_mark_node)
543 return error_mark_node;
544 else if (builtin_loop)
546 finish_expr_stmt (builtin_loop);
547 found_builtin_fn = true;
548 if (new_var)
550 vec <tree, va_gc> *rhs_sub_list = NULL, *new_var_list = NULL;
551 vec_safe_push (rhs_sub_list, rhs_node);
552 vec_safe_push (new_var_list, new_var);
553 replace_array_notations (&rhs, false, rhs_sub_list,
554 new_var_list);
559 lhs_rank = 0;
560 rhs_rank = 0;
561 if (!find_rank (location, lhs, lhs, true, &lhs_rank)
562 || !find_rank (location, rhs, rhs, true, &rhs_rank))
564 pop_stmt_list (an_init);
565 return error_mark_node;
568 /* If both are scalar, then the only reason why we will get this far is if
569 there is some array notations inside it and was using a builtin array
570 notation functions. If so, we have already broken those guys up and now
571 a simple build_x_modify_expr would do. */
572 if (lhs_rank == 0 && rhs_rank == 0)
574 if (found_builtin_fn)
576 new_modify_expr = build_x_modify_expr (location, lhs,
577 modifycode, rhs, complain);
578 finish_expr_stmt (new_modify_expr);
579 pop_stmt_list (an_init);
580 return an_init;
582 else
583 gcc_unreachable ();
586 /* If for some reason location is not set, then find if LHS or RHS has
587 location info. If so, then use that so we atleast have an idea. */
588 if (location == UNKNOWN_LOCATION)
590 if (EXPR_LOCATION (lhs) != UNKNOWN_LOCATION)
591 location = EXPR_LOCATION (lhs);
592 else if (EXPR_LOCATION (rhs) != UNKNOWN_LOCATION)
593 location = EXPR_LOCATION (rhs);
596 /* We need this when we have a scatter issue. */
597 extract_array_notation_exprs (lhs, true, &lhs_list);
598 rhs_list = NULL;
599 extract_array_notation_exprs (rhs, true, &rhs_list);
600 rhs_list_size = vec_safe_length (rhs_list);
601 lhs_list_size = vec_safe_length (lhs_list);
603 if (lhs_rank == 0 && rhs_rank != 0)
605 error_at (location, "%qD cannot be scalar when %qD is not", lhs, rhs);
606 return error_mark_node;
608 if (lhs_rank != 0 && rhs_rank != 0 && lhs_rank != rhs_rank)
610 error_at (location, "rank mismatch between %qE and %qE", lhs, rhs);
611 return error_mark_node;
614 /* Assign the array notation components to variable so that they can satisfy
615 the execute-once rule. */
616 for (ii = 0; ii < lhs_list_size; ii++)
618 tree anode = (*lhs_list)[ii];
619 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
620 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
621 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
623 for (ii = 0; ii < rhs_list_size; ii++)
624 if ((*rhs_list)[ii] && TREE_CODE ((*rhs_list)[ii]) == ARRAY_NOTATION_REF)
626 tree aa = (*rhs_list)[ii];
627 make_triplet_val_inv (&ARRAY_NOTATION_START (aa));
628 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (aa));
629 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (aa));
631 lhs_an_loop_info.safe_grow_cleared (lhs_rank);
633 if (rhs_rank)
634 rhs_an_loop_info.safe_grow_cleared (rhs_rank);
636 cond_expr.safe_grow_cleared (MAX (lhs_rank, rhs_rank));
637 cilkplus_extract_an_triplets (lhs_list, lhs_list_size, lhs_rank,
638 &lhs_an_info);
639 if (rhs_list)
640 cilkplus_extract_an_triplets (rhs_list, rhs_list_size, rhs_rank,
641 &rhs_an_info);
642 if (length_mismatch_in_expr_p (EXPR_LOCATION (lhs), lhs_an_info)
643 || (rhs_list && length_mismatch_in_expr_p (EXPR_LOCATION (rhs),
644 rhs_an_info)))
646 pop_stmt_list (an_init);
647 return error_mark_node;
649 tree rhs_len = ((rhs_list_size > 0 && rhs_rank > 0) ?
650 rhs_an_info[0][0].length : NULL_TREE);
651 tree lhs_len = ((lhs_list_size > 0 && lhs_rank > 0) ?
652 lhs_an_info[0][0].length : NULL_TREE);
653 if (lhs_list_size > 0 && rhs_list_size > 0 && lhs_rank > 0 && rhs_rank > 0
654 && TREE_CODE (lhs_len) == INTEGER_CST && rhs_len
655 && TREE_CODE (rhs_len) == INTEGER_CST
656 && !tree_int_cst_equal (rhs_len, lhs_len))
658 error_at (location, "length mismatch between LHS and RHS");
659 pop_stmt_list (an_init);
660 return error_mark_node;
662 for (ii = 0; ii < lhs_rank; ii++)
664 tree typ = ptrdiff_type_node;
665 lhs_an_loop_info[ii].var = create_temporary_var (typ);
666 add_decl_expr (lhs_an_loop_info[ii].var);
667 lhs_an_loop_info[ii].ind_init = build_x_modify_expr
668 (location, lhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
669 complain);
672 if (rhs_list_size > 0)
674 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
675 lhs_an_loop_info, lhs_rank,
676 lhs);
677 if (!rhs_array_operand)
678 return error_mark_node;
680 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
681 rhs_list_size = 0;
682 rhs_list = NULL;
683 extract_array_notation_exprs (rhs, true, &rhs_list);
684 rhs_list_size = vec_safe_length (rhs_list);
686 for (ii = 0; ii < rhs_rank; ii++)
688 tree typ = ptrdiff_type_node;
689 rhs_an_loop_info[ii].var = create_temporary_var (typ);
690 add_decl_expr (rhs_an_loop_info[ii].var);
691 rhs_an_loop_info[ii].ind_init = build_x_modify_expr
692 (location, rhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
693 complain);
696 if (lhs_rank)
698 lhs_array_operand =
699 create_array_refs (location, lhs_an_info, lhs_an_loop_info,
700 lhs_list_size, lhs_rank);
701 replace_array_notations (&lhs, true, lhs_list, lhs_array_operand);
704 if (rhs_array_operand)
705 vec_safe_truncate (rhs_array_operand, 0);
706 if (rhs_rank)
708 rhs_array_operand = create_array_refs (location, rhs_an_info,
709 rhs_an_loop_info, rhs_list_size,
710 rhs_rank);
711 /* Replace all the array refs created by the above function because this
712 variable is blown away by the fix_sec_implicit_args function below. */
713 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
714 vec_safe_truncate (rhs_array_operand , 0);
715 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
716 rhs_an_loop_info, rhs_rank,
717 rhs);
718 if (!rhs_array_operand)
719 return error_mark_node;
720 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
723 array_expr_rhs = rhs;
724 array_expr_lhs = lhs;
726 array_expr = build_x_modify_expr (location, array_expr_lhs, modifycode,
727 array_expr_rhs, complain);
728 create_cmp_incr (location, &lhs_an_loop_info, lhs_rank, lhs_an_info,
729 complain);
730 if (rhs_rank)
731 create_cmp_incr (location, &rhs_an_loop_info, rhs_rank, rhs_an_info,
732 complain);
733 for (ii = 0; ii < MAX (rhs_rank, lhs_rank); ii++)
734 if (ii < lhs_rank && ii < rhs_rank)
735 cond_expr[ii] = build_x_binary_op
736 (location, TRUTH_ANDIF_EXPR, lhs_an_loop_info[ii].cmp,
737 TREE_CODE (lhs_an_loop_info[ii].cmp), rhs_an_loop_info[ii].cmp,
738 TREE_CODE (rhs_an_loop_info[ii].cmp), NULL, complain);
739 else if (ii < lhs_rank && ii >= rhs_rank)
740 cond_expr[ii] = lhs_an_loop_info[ii].cmp;
741 else
742 /* No need to compare ii < rhs_rank && ii >= lhs_rank because in a valid
743 Array notation expression, rank of RHS cannot be greater than LHS. */
744 gcc_unreachable ();
746 an_init = pop_stmt_list (an_init);
747 append_to_statement_list (an_init, &loop_with_init);
748 body = array_expr;
749 for (ii = 0; ii < MAX (lhs_rank, rhs_rank); ii++)
751 tree incr_list = alloc_stmt_list ();
752 tree init_list = alloc_stmt_list ();
753 tree new_loop = push_stmt_list ();
755 if (lhs_rank)
757 append_to_statement_list (lhs_an_loop_info[ii].ind_init, &init_list);
758 append_to_statement_list (lhs_an_loop_info[ii].incr, &incr_list);
760 if (rhs_rank)
762 append_to_statement_list (rhs_an_loop_info[ii].ind_init, &init_list);
763 append_to_statement_list (rhs_an_loop_info[ii].incr, &incr_list);
765 create_an_loop (init_list, cond_expr[ii], incr_list, body);
766 body = pop_stmt_list (new_loop);
768 append_to_statement_list (body, &loop_with_init);
770 lhs_an_info.release ();
771 lhs_an_loop_info.release ();
772 if (rhs_rank)
774 rhs_an_info.release ();
775 rhs_an_loop_info.release ();
777 cond_expr.release ();
779 return loop_with_init;
782 /* Helper function for expand_conditonal_array_notations. Encloses the
783 conditional statement passed in ORIG_STMT with a loop around it and
784 replaces the condition in STMT with a ARRAY_REF tree-node to the array.
785 The condition must have a ARRAY_NOTATION_REF tree. */
787 static tree
788 cp_expand_cond_array_notations (tree orig_stmt)
790 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
791 size_t list_size = 0;
792 size_t rank = 0, ii = 0;
793 tree an_init, body, stmt = NULL_TREE;
794 tree builtin_loop, new_var = NULL_TREE;
795 tree loop_with_init = alloc_stmt_list ();
796 location_t location = UNKNOWN_LOCATION;
797 vec<vec<an_parts> > an_info = vNULL;
798 vec<an_loop_parts> an_loop_info = vNULL;
800 if (TREE_CODE (orig_stmt) == COND_EXPR)
802 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
803 tree yes_expr = COND_EXPR_THEN (orig_stmt);
804 tree no_expr = COND_EXPR_ELSE (orig_stmt);
805 tree cond = COND_EXPR_COND (orig_stmt);
806 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
807 || !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
808 &yes_rank)
809 || find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
810 &no_rank))
811 return error_mark_node;
812 /* If the condition has a zero rank, then handle array notations in body
813 separately. */
814 if (cond_rank == 0)
815 return orig_stmt;
816 if (cond_rank != yes_rank && yes_rank != 0)
818 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
819 " expression of parent if-statement");
820 return error_mark_node;
822 else if (cond_rank != no_rank && no_rank != 0)
824 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
825 "expression of parent if-statement");
826 return error_mark_node;
829 else if (TREE_CODE (orig_stmt) == IF_STMT)
831 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
832 tree yes_expr = THEN_CLAUSE (orig_stmt);
833 tree no_expr = ELSE_CLAUSE (orig_stmt);
834 tree cond = IF_COND (orig_stmt);
835 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
836 || (yes_expr
837 && !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
838 &yes_rank))
839 || (no_expr
840 && !find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
841 &no_rank)))
842 return error_mark_node;
844 /* Same reasoning as for COND_EXPR. */
845 if (cond_rank == 0)
846 return orig_stmt;
847 else if (cond_rank != yes_rank && yes_rank != 0)
849 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
850 " expression of parent if-statement");
851 return error_mark_node;
853 else if (cond_rank != no_rank && no_rank != 0)
855 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
856 "expression of parent if-statement");
857 return error_mark_node;
860 else if (truth_value_p (TREE_CODE (orig_stmt)))
862 size_t left_rank = 0, right_rank = 0;
863 tree left_expr = TREE_OPERAND (orig_stmt, 0);
864 tree right_expr = TREE_OPERAND (orig_stmt, 1);
865 if (!find_rank (EXPR_LOCATION (left_expr), left_expr, left_expr, true,
866 &left_rank)
867 || !find_rank (EXPR_LOCATION (right_expr), right_expr, right_expr,
868 true, &right_rank))
869 return error_mark_node;
870 if (right_rank == 0 && left_rank == 0)
871 return orig_stmt;
874 if (!find_rank (EXPR_LOCATION (orig_stmt), orig_stmt, orig_stmt, true,
875 &rank))
876 return error_mark_node;
877 if (rank == 0)
878 return orig_stmt;
880 extract_array_notation_exprs (orig_stmt, false, &array_list);
881 stmt = alloc_stmt_list ();
882 for (ii = 0; ii < vec_safe_length (array_list); ii++)
884 tree array_node = (*array_list)[ii];
885 if (TREE_CODE (array_node) == CALL_EXPR
886 || TREE_CODE (array_node) == AGGR_INIT_EXPR)
888 builtin_loop = expand_sec_reduce_builtin (array_node, &new_var);
889 if (builtin_loop == error_mark_node)
890 finish_expr_stmt (error_mark_node);
891 else if (new_var)
893 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
894 vec_safe_push (sub_list, array_node);
895 vec_safe_push (new_var_list, new_var);
896 replace_array_notations (&orig_stmt, false, sub_list,
897 new_var_list);
898 append_to_statement_list (builtin_loop, &stmt);
902 append_to_statement_list (orig_stmt, &stmt);
903 rank = 0;
904 array_list = NULL;
905 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
906 return error_mark_node;
907 if (rank == 0)
908 return stmt;
910 extract_array_notation_exprs (stmt, true, &array_list);
911 list_size = vec_safe_length (array_list);
912 if (list_size == 0)
913 return stmt;
915 location = EXPR_LOCATION (orig_stmt);
916 list_size = vec_safe_length (array_list);
917 an_loop_info.safe_grow_cleared (rank);
919 an_init = push_stmt_list ();
921 /* Assign the array notation components to variable so that they can
922 satisfy the exec-once rule. */
923 for (ii = 0; ii < list_size; ii++)
925 tree anode = (*array_list)[ii];
926 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
927 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
928 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
930 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
932 for (ii = 0; ii < rank; ii++)
934 tree typ = ptrdiff_type_node;
935 an_loop_info[ii].var = create_temporary_var (typ);
936 add_decl_expr (an_loop_info[ii].var);
937 an_loop_info[ii].ind_init =
938 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
939 build_zero_cst (typ), tf_warning_or_error);
941 array_operand = create_array_refs (location, an_info, an_loop_info,
942 list_size, rank);
943 replace_array_notations (&stmt, true, array_list, array_operand);
944 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
946 an_init = pop_stmt_list (an_init);
947 append_to_statement_list (an_init, &loop_with_init);
948 body = stmt;
950 for (ii = 0; ii < rank; ii++)
952 tree new_loop = push_stmt_list ();
953 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
954 an_loop_info[ii].incr, body);
955 body = pop_stmt_list (new_loop);
957 append_to_statement_list (body, &loop_with_init);
959 an_info.release ();
960 an_loop_info.release ();
962 return loop_with_init;
965 /* Transforms array notations inside unary expression ORIG_STMT with an
966 appropriate loop and ARRAY_REF (and returns all this as a super-tree called
967 LOOP). */
969 static tree
970 expand_unary_array_notation_exprs (tree orig_stmt)
972 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
973 size_t list_size = 0, rank = 0, ii = 0;
974 tree body;
975 tree builtin_loop, stmt = NULL_TREE, new_var = NULL_TREE;
976 location_t location = EXPR_LOCATION (orig_stmt);
977 tree an_init, loop_with_init = alloc_stmt_list ();
978 vec<vec<an_parts> > an_info = vNULL;
979 vec<an_loop_parts> an_loop_info = vNULL;
981 if (!find_rank (location, orig_stmt, orig_stmt, true, &rank))
982 return error_mark_node;
983 if (rank == 0)
984 return orig_stmt;
986 extract_array_notation_exprs (orig_stmt, false, &array_list);
987 list_size = vec_safe_length (array_list);
988 location = EXPR_LOCATION (orig_stmt);
989 stmt = NULL_TREE;
990 for (ii = 0; ii < list_size; ii++)
991 if (TREE_CODE ((*array_list)[ii]) == CALL_EXPR
992 || TREE_CODE ((*array_list)[ii]) == AGGR_INIT_EXPR)
994 tree list_node = (*array_list)[ii];
995 builtin_loop = expand_sec_reduce_builtin (list_node, &new_var);
996 if (builtin_loop == error_mark_node)
997 return error_mark_node;
998 else if (builtin_loop)
1000 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
1001 stmt = alloc_stmt_list ();
1002 append_to_statement_list (builtin_loop, &stmt);
1003 vec_safe_push (sub_list, list_node);
1004 vec_safe_push (new_var_list, new_var);
1005 replace_array_notations (&orig_stmt, false, sub_list, new_var_list);
1008 if (stmt != NULL_TREE)
1009 append_to_statement_list (finish_expr_stmt (orig_stmt), &stmt);
1010 else
1011 stmt = orig_stmt;
1012 rank = 0;
1013 list_size = 0;
1014 array_list = NULL;
1015 extract_array_notation_exprs (stmt, true, &array_list);
1016 list_size = vec_safe_length (array_list);
1018 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
1019 return error_mark_node;
1020 if (rank == 0 || list_size == 0)
1021 return stmt;
1022 an_loop_info.safe_grow_cleared (rank);
1023 an_init = push_stmt_list ();
1024 /* Assign the array notation components to variable so that they can satisfy
1025 the exec-once rule. */
1026 for (ii = 0; ii < list_size; ii++)
1028 tree array_node = (*array_list)[ii];
1029 make_triplet_val_inv (&ARRAY_NOTATION_START (array_node));
1030 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (array_node));
1031 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (array_node));
1033 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
1035 for (ii = 0; ii < rank; ii++)
1037 tree typ = ptrdiff_type_node;
1038 an_loop_info[ii].var = create_temporary_var (typ);
1039 add_decl_expr (an_loop_info[ii].var);
1040 an_loop_info[ii].ind_init = build_x_modify_expr
1041 (location, an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
1042 tf_warning_or_error);
1044 array_operand = create_array_refs (location, an_info, an_loop_info,
1045 list_size, rank);
1046 replace_array_notations (&stmt, true, array_list, array_operand);
1047 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
1049 an_init = pop_stmt_list (an_init);
1050 append_to_statement_list (an_init, &loop_with_init);
1051 body = stmt;
1053 for (ii = 0; ii < rank; ii++)
1055 tree new_loop = push_stmt_list ();
1056 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
1057 an_loop_info[ii].incr, body);
1058 body = pop_stmt_list (new_loop);
1060 append_to_statement_list (body, &loop_with_init);
1062 an_info.release ();
1063 an_loop_info.release ();
1065 return loop_with_init;
1068 /* Expands the array notation's builtin reduction function in EXPR
1069 (of type RETURN_EXPR) and returns a STATEMENT_LIST that contains a loop
1070 with the builtin function expansion and a return statement at the end. */
1072 static tree
1073 expand_return_expr (tree expr)
1075 tree new_mod_list, new_var, new_mod, retval_expr;
1076 size_t rank = 0;
1077 location_t loc = EXPR_LOCATION (expr);
1078 if (TREE_CODE (expr) != RETURN_EXPR)
1079 return expr;
1081 if (!find_rank (loc, expr, expr, false, &rank))
1082 return error_mark_node;
1084 /* If the return expression contains array notations, then flag it as
1085 error. */
1086 if (rank >= 1)
1088 error_at (loc, "array notation expression cannot be used as a return "
1089 "value");
1090 return error_mark_node;
1093 new_mod_list = push_stmt_list ();
1094 retval_expr = TREE_OPERAND (expr, 0);
1095 new_var = create_temporary_var (TREE_TYPE (retval_expr));
1096 add_decl_expr (new_var);
1097 new_mod = expand_an_in_modify_expr (loc, new_var, NOP_EXPR,
1098 TREE_OPERAND (retval_expr, 1),
1099 tf_warning_or_error);
1100 TREE_OPERAND (retval_expr, 1) = new_var;
1101 TREE_OPERAND (expr, 0) = retval_expr;
1102 add_stmt (new_mod);
1103 add_stmt (expr);
1104 new_mod_list = pop_stmt_list (new_mod_list);
1105 return new_mod_list;
1108 /* Expands ARRAY_NOTATION_REF and builtin functions in a compound statement,
1109 STMT. Returns the STMT with expanded array notations. */
1111 tree
1112 expand_array_notation_exprs (tree t)
1114 enum tree_code code;
1115 bool is_expr;
1116 location_t loc = UNKNOWN_LOCATION;
1118 if (!t)
1119 return t;
1121 loc = EXPR_LOCATION (t);
1123 code = TREE_CODE (t);
1124 is_expr = IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code));
1125 switch (code)
1127 case ERROR_MARK:
1128 case IDENTIFIER_NODE:
1129 case INTEGER_CST:
1130 case REAL_CST:
1131 case FIXED_CST:
1132 case STRING_CST:
1133 case BLOCK:
1134 case PLACEHOLDER_EXPR:
1135 case FIELD_DECL:
1136 case VOID_TYPE:
1137 case REAL_TYPE:
1138 case SSA_NAME:
1139 case LABEL_DECL:
1140 case RESULT_DECL:
1141 case VAR_DECL:
1142 case PARM_DECL:
1143 case NON_LVALUE_EXPR:
1144 case NOP_EXPR:
1145 case INIT_EXPR:
1146 case ADDR_EXPR:
1147 case ARRAY_REF:
1148 case BIT_FIELD_REF:
1149 case VECTOR_CST:
1150 case COMPLEX_CST:
1151 return t;
1152 case MODIFY_EXPR:
1153 if (contains_array_notation_expr (t))
1154 t = expand_an_in_modify_expr (loc, TREE_OPERAND (t, 0), NOP_EXPR,
1155 TREE_OPERAND (t, 1),
1156 tf_warning_or_error);
1157 return t;
1158 case MODOP_EXPR:
1159 if (contains_array_notation_expr (t) && !processing_template_decl)
1160 t = expand_an_in_modify_expr
1161 (loc, TREE_OPERAND (t, 0), TREE_CODE (TREE_OPERAND (t, 1)),
1162 TREE_OPERAND (t, 2), tf_warning_or_error);
1163 return t;
1164 case CONSTRUCTOR:
1165 return t;
1166 case BIND_EXPR:
1168 BIND_EXPR_BODY (t) =
1169 expand_array_notation_exprs (BIND_EXPR_BODY (t));
1170 return t;
1172 case DECL_EXPR:
1174 tree x = DECL_EXPR_DECL (t);
1175 if (t && TREE_CODE (x) != FUNCTION_DECL)
1176 if (DECL_INITIAL (x))
1177 t = expand_unary_array_notation_exprs (t);
1178 return t;
1180 case STATEMENT_LIST:
1182 tree_stmt_iterator i;
1183 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
1184 *tsi_stmt_ptr (i) =
1185 expand_array_notation_exprs (*tsi_stmt_ptr (i));
1186 return t;
1189 case OMP_PARALLEL:
1190 case OMP_TASK:
1191 case OMP_FOR:
1192 case OMP_SINGLE:
1193 case OMP_SECTION:
1194 case OMP_SECTIONS:
1195 case OMP_MASTER:
1196 case OMP_ORDERED:
1197 case OMP_CRITICAL:
1198 case OMP_ATOMIC:
1199 case OMP_CLAUSE:
1200 case TARGET_EXPR:
1201 case INTEGER_TYPE:
1202 case ENUMERAL_TYPE:
1203 case BOOLEAN_TYPE:
1204 case POINTER_TYPE:
1205 case ARRAY_TYPE:
1206 case RECORD_TYPE:
1207 case METHOD_TYPE:
1208 return t;
1209 case RETURN_EXPR:
1210 if (contains_array_notation_expr (t))
1211 t = expand_return_expr (t);
1212 return t;
1213 case PREDECREMENT_EXPR:
1214 case PREINCREMENT_EXPR:
1215 case POSTDECREMENT_EXPR:
1216 case POSTINCREMENT_EXPR:
1217 case AGGR_INIT_EXPR:
1218 case CALL_EXPR:
1219 t = expand_unary_array_notation_exprs (t);
1220 return t;
1221 case CONVERT_EXPR:
1222 case CLEANUP_POINT_EXPR:
1223 case EXPR_STMT:
1224 TREE_OPERAND (t, 0) = expand_array_notation_exprs (TREE_OPERAND (t, 0));
1225 /* It is not necessary to wrap error_mark_node in EXPR_STMT. */
1226 if (TREE_OPERAND (t, 0) == error_mark_node)
1227 return TREE_OPERAND (t, 0);
1228 return t;
1229 case TRUTH_ANDIF_EXPR:
1230 case TRUTH_ORIF_EXPR:
1231 case TRUTH_AND_EXPR:
1232 case TRUTH_OR_EXPR:
1233 case TRUTH_XOR_EXPR:
1234 case TRUTH_NOT_EXPR:
1235 case COND_EXPR:
1236 t = cp_expand_cond_array_notations (t);
1237 if (TREE_CODE (t) == COND_EXPR)
1239 COND_EXPR_THEN (t) =
1240 expand_array_notation_exprs (COND_EXPR_THEN (t));
1241 COND_EXPR_ELSE (t) =
1242 expand_array_notation_exprs (COND_EXPR_ELSE (t));
1244 return t;
1245 case FOR_STMT:
1246 if (contains_array_notation_expr (FOR_COND (t)))
1248 error_at (EXPR_LOCATION (FOR_COND (t)),
1249 "array notation cannot be used in a condition for "
1250 "a for-loop");
1251 return error_mark_node;
1253 /* FIXME: Add a check for CILK_FOR_STMT here when we add Cilk tasking
1254 keywords. */
1255 if (TREE_CODE (t) == FOR_STMT)
1257 FOR_BODY (t) = expand_array_notation_exprs (FOR_BODY (t));
1258 FOR_EXPR (t) = expand_array_notation_exprs (FOR_EXPR (t));
1260 else
1261 t = expand_array_notation_exprs (t);
1262 return t;
1263 case IF_STMT:
1264 t = cp_expand_cond_array_notations (t);
1265 /* If the above function added some extra instructions above the original
1266 if statement, then we can't assume it is still IF_STMT so we have to
1267 check again. */
1268 if (TREE_CODE (t) == IF_STMT)
1270 if (THEN_CLAUSE (t))
1271 THEN_CLAUSE (t) = expand_array_notation_exprs (THEN_CLAUSE (t));
1272 if (ELSE_CLAUSE (t))
1273 ELSE_CLAUSE (t) = expand_array_notation_exprs (ELSE_CLAUSE (t));
1275 else
1276 t = expand_array_notation_exprs (t);
1277 return t;
1278 case SWITCH_STMT:
1279 if (contains_array_notation_expr (SWITCH_STMT_COND (t)))
1281 error_at (EXPR_LOCATION (SWITCH_STMT_COND (t)),
1282 "array notation cannot be used as a condition for "
1283 "switch statement");
1284 return error_mark_node;
1286 if (SWITCH_STMT_BODY (t))
1287 SWITCH_STMT_BODY (t) =
1288 expand_array_notation_exprs (SWITCH_STMT_BODY (t));
1289 return t;
1290 case WHILE_STMT:
1291 if (contains_array_notation_expr (WHILE_COND (t)))
1293 if (EXPR_LOCATION (WHILE_COND (t)) != UNKNOWN_LOCATION)
1294 loc = EXPR_LOCATION (WHILE_COND (t));
1295 error_at (loc, "array notation cannot be used as a condition for "
1296 "while statement");
1297 return error_mark_node;
1299 if (WHILE_BODY (t))
1300 WHILE_BODY (t) = expand_array_notation_exprs (WHILE_BODY (t));
1301 return t;
1302 case DO_STMT:
1303 if (contains_array_notation_expr (DO_COND (t)))
1305 error_at (EXPR_LOCATION (DO_COND (t)),
1306 "array notation cannot be used as a condition for a "
1307 "do-while statement");
1308 return error_mark_node;
1310 if (DO_BODY (t))
1311 DO_BODY (t) = expand_array_notation_exprs (DO_BODY (t));
1312 return t;
1313 default:
1314 if (is_expr)
1316 int i, len;
1318 /* Walk over all the sub-trees of this operand. */
1319 len = TREE_CODE_LENGTH (code);
1321 /* Go through the subtrees. We need to do this in forward order so
1322 that the scope of a FOR_EXPR is handled properly. */
1323 for (i = 0; i < len; ++i)
1324 TREE_OPERAND (t, i) =
1325 expand_array_notation_exprs (TREE_OPERAND (t, i));
1327 return t;
1329 return t;
1332 /* Given the base of an array (ARRAY), the START (start_index), the number of
1333 elements to be accessed (LENGTH) and the STRIDE, construct an
1334 ARRAY_NOTATION_REF tree of type TYPE and return it. Restrictions on START,
1335 LENGTH and STRIDE are the same as that of index field passed into ARRAY_REF.
1336 The only additional restriction is that, unlike index in ARRAY_REF, stride,
1337 length and start_index cannot contain array notations. */
1339 tree
1340 build_array_notation_ref (location_t loc, tree array, tree start, tree length,
1341 tree stride, tree type)
1343 tree array_ntn_expr = NULL_TREE;
1345 /* If we enter the then-case of the if-statement below, we have hit a case
1346 like this: ARRAY [:]. */
1347 if (!start && !length)
1349 if (TREE_CODE (type) != ARRAY_TYPE)
1351 error_at (loc, "start-index and length fields necessary for "
1352 "using array notation in pointers or records");
1353 return error_mark_node;
1355 tree domain = TYPE_DOMAIN (type);
1356 if (!domain)
1358 error_at (loc, "start-index and length fields necessary for "
1359 "using array notation with array of unknown bound");
1360 return error_mark_node;
1362 start = cp_fold_convert (ptrdiff_type_node, TYPE_MINVAL (domain));
1363 length = size_binop (PLUS_EXPR, TYPE_MAXVAL (domain), size_one_node);
1364 length = cp_fold_convert (ptrdiff_type_node, length);
1367 if (!stride)
1368 stride = build_one_cst (ptrdiff_type_node);
1370 /* When dealing with templates, triplet type-checking will be done in pt.c
1371 after type substitution. */
1372 if (processing_template_decl
1373 && (type_dependent_expression_p (array)
1374 || type_dependent_expression_p (length)
1375 || type_dependent_expression_p (start)
1376 || type_dependent_expression_p (stride)))
1377 array_ntn_expr = build_min_nt_loc (loc, ARRAY_NOTATION_REF, array, start,
1378 length, stride, NULL_TREE);
1379 else
1381 if (!cilkplus_an_triplet_types_ok_p (loc, start, length, stride, type))
1382 return error_mark_node;
1383 array_ntn_expr = build4 (ARRAY_NOTATION_REF, NULL_TREE, array, start,
1384 length, stride);
1386 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == POINTER_TYPE)
1387 TREE_TYPE (array_ntn_expr) = TREE_TYPE (type);
1388 else
1389 gcc_unreachable ();
1391 SET_EXPR_LOCATION (array_ntn_expr, loc);
1392 return array_ntn_expr;
1395 /* Returns false if any of the Array notation triplet values: START_INDEX,
1396 LENGTH and STRIDE, are not of integral type and have a rank greater than
1397 zero. */
1399 bool
1400 cilkplus_an_triplet_types_ok_p (location_t loc, tree start_index, tree length,
1401 tree stride, tree type)
1403 size_t stride_rank = 0, length_rank = 0, start_rank = 0;
1404 if (!TREE_TYPE (start_index) || !INTEGRAL_TYPE_P (TREE_TYPE (start_index)))
1406 error_at (loc, "start-index of array notation triplet is not an integer");
1407 return false;
1409 if (!TREE_TYPE (length) || !INTEGRAL_TYPE_P (TREE_TYPE (length)))
1411 error_at (loc, "length of array notation triplet is not an integer");
1412 return false;
1414 if (!TREE_TYPE (stride) || !INTEGRAL_TYPE_P (TREE_TYPE (stride)))
1416 error_at (loc, "stride of array notation triplet is not an integer");
1417 return false;
1419 if (!TREE_CODE (type) == FUNCTION_TYPE)
1421 error_at (loc, "array notation cannot be used with function type");
1422 return false;
1424 if (!find_rank (loc, start_index, start_index, false, &start_rank)
1425 || !find_rank (loc, length, length, false, &length_rank)
1426 || !find_rank (loc, stride, stride, false, &stride_rank))
1427 return false;
1429 if (start_rank != 0)
1431 error_at (loc, "rank of an array notation triplet%'s start-index is not "
1432 "zero");
1433 return false;
1435 if (length_rank != 0)
1437 error_at (loc, "rank of an array notation triplet%'s length is not zero");
1438 return false;
1440 if (stride_rank != 0)
1442 error_at (loc, "rank of array notation triplet%'s stride is not zero");
1443 return false;
1445 return true;