gcc/
[official-gcc.git] / gcc / cp / cp-array-notation.c
bloba84efd5b1516dea5ca5d9d7947ac6e95f8a9ec0d
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-2015 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 access (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 "alias.h"
57 #include "symtab.h"
58 #include "options.h"
59 #include "tree.h"
60 #include "cp-tree.h"
61 #include "c-family/c-common.h"
62 #include "diagnostic.h"
63 #include "tree-iterator.h"
65 /* Creates a FOR_STMT with INIT, COND, INCR and BODY as the initializer,
66 condition, increment expression and the loop-body, respectively. */
68 static void
69 create_an_loop (tree init, tree cond, tree incr, tree body)
71 tree for_stmt;
73 finish_expr_stmt (init);
74 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
75 finish_for_init_stmt (for_stmt);
76 finish_for_cond (cond, for_stmt, false);
77 finish_for_expr (incr, for_stmt);
78 finish_expr_stmt (body);
79 finish_for_stmt (for_stmt);
82 /* If *VALUE is not a constant integer, then this function replaces it with
83 a variable to make it loop invariant for array notations. */
85 static inline void
86 make_triplet_val_inv (tree *value)
88 if (TREE_CODE (*value) != INTEGER_CST
89 && TREE_CODE (*value) != PARM_DECL
90 && !VAR_P (*value))
91 *value = get_temp_regvar (ptrdiff_type_node, *value);
94 /* Returns a vector of size RANK that contains an ARRAY_REF. This vector is
95 created using array notation-triplet information stored in AN_INFO. The
96 induction var is taken from AN_LOOP_INFO.
98 For example: For an array notation A[5:10:2], the vector start will be
99 of size 1 holding '5', stride of same size as start but holding the value of
100 as 2, and is_vector as true. Let's assume VAR is 'x'
101 This function returns a vector of size 1 with the following data:
102 A[5 + (x * 2)] .
105 static vec<tree, va_gc> *
106 create_array_refs (location_t loc, vec<vec<an_parts> > an_info,
107 vec<an_loop_parts> an_loop_info, size_t size, size_t rank)
109 tree ind_mult, ind_incr;
110 vec<tree, va_gc> *array_operand = NULL;
112 for (size_t ii = 0; ii < size; ii++)
113 if (an_info[ii][0].is_vector)
115 tree array_opr = an_info[ii][rank - 1].value;
116 for (int s_jj = rank -1; s_jj >= 0; s_jj--)
118 tree start = cp_fold_convert (ptrdiff_type_node,
119 an_info[ii][s_jj].start);
120 tree stride = cp_fold_convert (ptrdiff_type_node,
121 an_info[ii][s_jj].stride);
122 tree var = cp_fold_convert (ptrdiff_type_node,
123 an_loop_info[s_jj].var);
125 ind_mult = build2 (MULT_EXPR, TREE_TYPE (var), var, stride);
126 ind_incr = build2 (PLUS_EXPR, TREE_TYPE (var), start, ind_mult);
127 /* Array [ start_index + (induction_var * stride)] */
128 array_opr = grok_array_decl (loc, array_opr, ind_incr, false);
130 vec_safe_push (array_operand, array_opr);
132 else
133 vec_safe_push (array_operand, integer_one_node);
134 return array_operand;
137 /* Populates the INCR and CMP fields in *NODE with the increment
138 (of type POSTINCREMENT) and comparison (of TYPE LT_EXPR) expressions, using
139 data from AN_INFO. */
141 void
142 create_cmp_incr (location_t loc, vec <an_loop_parts> *node, size_t rank,
143 vec<vec<an_parts> > an_info, tsubst_flags_t complain)
145 for (size_t ii = 0; ii < rank; ii++)
147 (*node)[ii].incr = build_x_unary_op (loc, POSTINCREMENT_EXPR,
148 (*node)[ii].var, complain);
149 (*node)[ii].cmp = build_x_binary_op (loc, LT_EXPR, (*node)[ii].var,
150 TREE_CODE ((*node)[ii].var),
151 an_info[0][ii].length,
152 TREE_CODE (an_info[0][ii].length),
153 NULL, complain);
157 /* Replaces all the scalar expressions in *NODE. Returns a STATEMENT LIST that
158 holds the NODE along with the variables that hold the results of the
159 invariant expressions. */
161 static tree
162 replace_invariant_exprs (tree *node)
164 size_t ix = 0;
165 tree node_list = NULL_TREE;
166 tree t = NULL_TREE, new_var = NULL_TREE;
167 struct inv_list data;
169 data.list_values = NULL;
170 data.replacement = NULL;
171 data.additional_tcodes = NULL;
172 cp_walk_tree (node, find_inv_trees, (void *) &data, NULL);
174 if (vec_safe_length (data.list_values))
176 node_list = push_stmt_list ();
177 for (ix = 0; vec_safe_iterate (data.list_values, ix, &t); ix++)
179 /* Sometimes, when comma_expr has a function call in it, it will
180 typecast it to void. Find_inv_trees finds those nodes and so
181 if it void type, then don't bother creating a new var to hold
182 the return value. */
183 if (VOID_TYPE_P (TREE_TYPE (t)))
185 finish_expr_stmt (t);
186 new_var = void_node;
188 else
189 new_var = get_temp_regvar (TREE_TYPE (t), t);
190 vec_safe_push (data.replacement, new_var);
192 cp_walk_tree (node, replace_inv_trees, (void *) &data, NULL);
193 node_list = pop_stmt_list (node_list);
195 return node_list;
198 /* Replace array notation's built-in function passed in AN_BUILTIN_FN with
199 the appropriate loop and computation (all stored in variable LOOP of type
200 tree node). The output of the function function is always a scalar and that
201 result is returned in *NEW_VAR. *NEW_VAR is NULL_TREE if the function is
202 __sec_reduce_mutating. */
204 static tree
205 expand_sec_reduce_builtin (tree an_builtin_fn, tree *new_var)
207 tree new_var_type = NULL_TREE, func_parm, new_yes_expr, new_no_expr;
208 tree array_ind_value = NULL_TREE, new_no_ind, new_yes_ind, new_no_list;
209 tree new_yes_list, new_cond_expr, new_expr = NULL_TREE;
210 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
211 size_t list_size = 0, rank = 0, ii = 0;
212 tree body, an_init, loop_with_init = alloc_stmt_list ();
213 tree array_op0, comp_node = NULL_TREE;
214 tree call_fn = NULL_TREE, identity_value = NULL_TREE;
215 tree init = NULL_TREE, cond_init = NULL_TREE;
216 enum tree_code code = NOP_EXPR;
217 location_t location = UNKNOWN_LOCATION;
218 vec<vec<an_parts> > an_info = vNULL;
219 vec<an_loop_parts> an_loop_info = vNULL;
220 enum built_in_function an_type =
221 is_cilkplus_reduce_builtin (CALL_EXPR_FN (an_builtin_fn));
222 vec <tree, va_gc> *func_args;
224 if (an_type == BUILT_IN_NONE)
225 return NULL_TREE;
227 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE
228 && an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
229 func_parm = CALL_EXPR_ARG (an_builtin_fn, 0);
230 else
232 call_fn = CALL_EXPR_ARG (an_builtin_fn, 2);
234 /* We need to do this because we are "faking" the builtin function types,
235 so the compiler does a bunch of typecasts and this will get rid of
236 all that! */
237 STRIP_NOPS (call_fn);
238 if (TREE_CODE (call_fn) != OVERLOAD
239 && TREE_CODE (call_fn) != FUNCTION_DECL)
240 call_fn = TREE_OPERAND (call_fn, 0);
241 identity_value = CALL_EXPR_ARG (an_builtin_fn, 0);
242 func_parm = CALL_EXPR_ARG (an_builtin_fn, 1);
243 STRIP_NOPS (identity_value);
245 STRIP_NOPS (func_parm);
247 location = EXPR_LOCATION (an_builtin_fn);
249 /* Note about using find_rank (): If find_rank returns false, then it must
250 have already reported an error, thus we just return an error_mark_node
251 without any doing any error emission. */
252 if (!find_rank (location, an_builtin_fn, an_builtin_fn, true, &rank))
253 return error_mark_node;
254 if (rank == 0)
256 error_at (location, "Invalid builtin arguments");
257 return error_mark_node;
259 else if (rank > 1
260 && (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
261 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND))
263 error_at (location, "__sec_reduce_min_ind or __sec_reduce_max_ind cannot "
264 "have arrays with dimension greater than 1");
265 return error_mark_node;
268 extract_array_notation_exprs (func_parm, true, &array_list);
269 list_size = vec_safe_length (array_list);
270 switch (an_type)
272 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
273 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
274 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
275 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
276 new_var_type = TREE_TYPE ((*array_list)[0]);
277 break;
278 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
279 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
280 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
281 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
282 new_var_type = boolean_type_node;
283 break;
284 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
285 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
286 new_var_type = size_type_node;
287 break;
288 case BUILT_IN_CILKPLUS_SEC_REDUCE:
289 if (call_fn && identity_value)
290 new_var_type = TREE_TYPE ((*array_list)[0]);
291 break;
292 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
293 new_var_type = NULL_TREE;
294 break;
295 default:
296 gcc_unreachable ();
299 if (new_var_type && TREE_CODE (new_var_type) == ARRAY_TYPE)
300 new_var_type = TREE_TYPE (new_var_type);
301 an_loop_info.safe_grow_cleared (rank);
303 an_init = push_stmt_list ();
305 /* Assign the array notation components to variable so that they can satisfy
306 the exec-once rule. */
307 for (ii = 0; ii < list_size; ii++)
308 if (TREE_CODE ((*array_list)[ii]) == ARRAY_NOTATION_REF)
310 tree anode = (*array_list)[ii];
311 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
312 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
313 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
315 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
316 for (ii = 0; ii < rank; ii++)
318 tree typ = ptrdiff_type_node;
320 /* In this place, we are using get_temp_regvar instead of
321 create_temporary_var if an_type is SEC_REDUCE_MAX/MIN_IND because
322 the array_ind_value depends on this value being initalized to 0. */
323 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
324 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
325 an_loop_info[ii].var = get_temp_regvar (typ, build_zero_cst (typ));
326 else
328 an_loop_info[ii].var = create_temporary_var (typ);
329 add_decl_expr (an_loop_info[ii].var);
331 an_loop_info[ii].ind_init =
332 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
333 build_zero_cst (typ), tf_warning_or_error);
335 array_operand = create_array_refs (location, an_info, an_loop_info,
336 list_size, rank);
337 replace_array_notations (&func_parm, true, array_list, array_operand);
339 if (!TREE_TYPE (func_parm))
340 TREE_TYPE (func_parm) = TREE_TYPE ((*array_list)[0]);
342 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
343 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
344 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
345 array_ind_value = get_temp_regvar (TREE_TYPE (func_parm), func_parm);
347 array_op0 = (*array_operand)[0];
348 if (INDIRECT_REF_P (array_op0))
349 array_op0 = TREE_OPERAND (array_op0, 0);
350 switch (an_type)
352 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
353 code = PLUS_EXPR;
354 init = build_zero_cst (new_var_type);
355 break;
356 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
357 code = MULT_EXPR;
358 init = build_one_cst (new_var_type);
359 break;
360 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
361 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
362 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO) ? EQ_EXPR
363 : NE_EXPR);
364 init = build_zero_cst (new_var_type);
365 cond_init = build_one_cst (new_var_type);
366 comp_node = build_zero_cst (TREE_TYPE (func_parm));
367 break;
368 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
369 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
370 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO) ? NE_EXPR
371 : EQ_EXPR);
372 init = build_one_cst (new_var_type);
373 cond_init = build_zero_cst (new_var_type);
374 comp_node = build_zero_cst (TREE_TYPE (func_parm));
375 break;
376 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
377 code = MAX_EXPR;
378 init = (TYPE_MIN_VALUE (new_var_type) ? TYPE_MIN_VALUE (new_var_type)
379 : func_parm);
380 break;
381 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
382 code = MIN_EXPR;
383 init = (TYPE_MAX_VALUE (new_var_type) ? TYPE_MAX_VALUE (new_var_type)
384 : func_parm);
385 break;
386 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
387 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
388 code = (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND ? LE_EXPR
389 : GE_EXPR);
390 init = an_loop_info[0].var;
391 break;
392 case BUILT_IN_CILKPLUS_SEC_REDUCE:
393 init = identity_value;
394 break;
395 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
396 init = NULL_TREE;
397 break;
398 default:
399 gcc_unreachable ();
402 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
403 *new_var = get_temp_regvar (new_var_type, init);
404 else
405 *new_var = NULL_TREE;
407 switch (an_type)
409 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
410 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
411 new_expr = build_x_modify_expr (location, *new_var, code, func_parm,
412 tf_warning_or_error);
413 break;
414 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
415 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
416 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
417 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
418 /* In all these cases, assume the false case is true and as soon as
419 we find a true case, set the true flag on and latch it in. */
420 new_yes_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
421 cond_init, tf_warning_or_error);
422 new_no_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
423 *new_var, tf_warning_or_error);
424 new_cond_expr = build_x_binary_op
425 (location, code, func_parm, TREE_CODE (func_parm), comp_node,
426 TREE_CODE (comp_node), NULL, tf_warning_or_error);
427 new_expr = build_x_conditional_expr (location, new_cond_expr,
428 new_yes_expr, new_no_expr,
429 tf_warning_or_error);
430 break;
431 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
432 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
433 new_cond_expr = build_x_binary_op
434 (location, code, *new_var, TREE_CODE (*new_var), func_parm,
435 TREE_CODE (func_parm), NULL, tf_warning_or_error);
436 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, func_parm,
437 tf_warning_or_error);
438 break;
439 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
440 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
441 new_yes_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
442 func_parm, tf_warning_or_error);
443 new_no_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
444 array_ind_value, tf_warning_or_error);
445 if (list_size > 1)
446 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
447 an_loop_info[0].var,
448 tf_warning_or_error);
449 else
450 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
451 TREE_OPERAND (array_op0, 1),
452 tf_warning_or_error);
453 new_no_ind = build_x_modify_expr (location, *new_var, NOP_EXPR, *new_var,
454 tf_warning_or_error);
455 new_yes_list = alloc_stmt_list ();
456 append_to_statement_list (new_yes_ind, &new_yes_list);
457 append_to_statement_list (new_yes_expr, &new_yes_list);
459 new_no_list = alloc_stmt_list ();
460 append_to_statement_list (new_no_ind, &new_no_list);
461 append_to_statement_list (new_no_expr, &new_no_list);
463 new_cond_expr = build_x_binary_op (location, code, array_ind_value,
464 TREE_CODE (array_ind_value), func_parm,
465 TREE_CODE (func_parm), NULL,
466 tf_warning_or_error);
467 new_expr = build_x_conditional_expr (location, new_cond_expr,
468 new_yes_list, new_no_list,
469 tf_warning_or_error);
470 break;
471 case BUILT_IN_CILKPLUS_SEC_REDUCE:
472 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
473 func_args = make_tree_vector ();
474 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
475 vec_safe_push (func_args, *new_var);
476 else
477 vec_safe_push (func_args, identity_value);
478 vec_safe_push (func_args, func_parm);
480 new_expr = finish_call_expr (call_fn, &func_args, false, true,
481 tf_warning_or_error);
482 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
483 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, new_expr,
484 tf_warning_or_error);
485 release_tree_vector (func_args);
486 break;
487 default:
488 gcc_unreachable ();
490 an_init = pop_stmt_list (an_init);
491 append_to_statement_list (an_init, &loop_with_init);
492 body = new_expr;
494 for (ii = 0; ii < rank; ii++)
496 tree new_loop = push_stmt_list ();
497 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
498 an_loop_info[ii].incr, body);
499 body = pop_stmt_list (new_loop);
501 append_to_statement_list (body, &loop_with_init);
503 an_info.release ();
504 an_loop_info.release ();
506 return loop_with_init;
509 /* Returns a loop with ARRAY_REF inside it with an appropriate modify expr.
510 The LHS and/or RHS will be array notation expressions that have a
511 MODIFYCODE. The location of the variable is specified by LOCATION. */
513 static tree
514 expand_an_in_modify_expr (location_t location, tree lhs,
515 enum tree_code modifycode, tree rhs,
516 tsubst_flags_t complain)
518 tree array_expr_lhs = NULL_TREE, array_expr_rhs = NULL_TREE;
519 tree array_expr = NULL_TREE;
520 tree body = NULL_TREE;
521 vec<tree> cond_expr = vNULL;
522 vec<tree, va_gc> *lhs_array_operand = NULL, *rhs_array_operand = NULL;
523 size_t lhs_rank = 0, rhs_rank = 0, ii = 0;
524 vec<tree, va_gc> *rhs_list = NULL, *lhs_list = NULL;
525 size_t rhs_list_size = 0, lhs_list_size = 0;
526 tree new_modify_expr, new_var = NULL_TREE, builtin_loop, scalar_mods;
527 bool found_builtin_fn = false;
528 tree an_init, loop_with_init = alloc_stmt_list ();
529 vec<vec<an_parts> > lhs_an_info = vNULL, rhs_an_info = vNULL;
530 vec<an_loop_parts> lhs_an_loop_info = vNULL, rhs_an_loop_info = vNULL;
532 if (!find_rank (location, rhs, rhs, false, &rhs_rank))
533 return error_mark_node;
534 extract_array_notation_exprs (rhs, false, &rhs_list);
535 rhs_list_size = vec_safe_length (rhs_list);
536 an_init = push_stmt_list ();
537 if (rhs_rank)
539 scalar_mods = replace_invariant_exprs (&rhs);
540 if (scalar_mods)
541 finish_expr_stmt (scalar_mods);
543 for (ii = 0; ii < rhs_list_size; ii++)
545 tree rhs_node = (*rhs_list)[ii];
546 if (TREE_CODE (rhs_node) == CALL_EXPR)
548 builtin_loop = expand_sec_reduce_builtin (rhs_node, &new_var);
549 if (builtin_loop == error_mark_node)
550 return error_mark_node;
551 else if (builtin_loop)
553 finish_expr_stmt (builtin_loop);
554 found_builtin_fn = true;
555 if (new_var)
557 vec <tree, va_gc> *rhs_sub_list = NULL, *new_var_list = NULL;
558 vec_safe_push (rhs_sub_list, rhs_node);
559 vec_safe_push (new_var_list, new_var);
560 replace_array_notations (&rhs, false, rhs_sub_list,
561 new_var_list);
566 lhs_rank = 0;
567 rhs_rank = 0;
568 if (!find_rank (location, lhs, lhs, true, &lhs_rank)
569 || !find_rank (location, rhs, rhs, true, &rhs_rank))
571 pop_stmt_list (an_init);
572 return error_mark_node;
575 /* If both are scalar, then the only reason why we will get this far is if
576 there is some array notations inside it and was using a builtin array
577 notation functions. If so, we have already broken those guys up and now
578 a simple build_x_modify_expr would do. */
579 if (lhs_rank == 0 && rhs_rank == 0)
581 if (found_builtin_fn)
583 new_modify_expr = build_x_modify_expr (location, lhs,
584 modifycode, rhs, complain);
585 finish_expr_stmt (new_modify_expr);
586 pop_stmt_list (an_init);
587 return an_init;
589 else
590 gcc_unreachable ();
593 /* If for some reason location is not set, then find if LHS or RHS has
594 location info. If so, then use that so we atleast have an idea. */
595 if (location == UNKNOWN_LOCATION)
597 if (EXPR_LOCATION (lhs) != UNKNOWN_LOCATION)
598 location = EXPR_LOCATION (lhs);
599 else if (EXPR_LOCATION (rhs) != UNKNOWN_LOCATION)
600 location = EXPR_LOCATION (rhs);
603 /* We need this when we have a scatter issue. */
604 extract_array_notation_exprs (lhs, true, &lhs_list);
605 rhs_list = NULL;
606 extract_array_notation_exprs (rhs, true, &rhs_list);
607 rhs_list_size = vec_safe_length (rhs_list);
608 lhs_list_size = vec_safe_length (lhs_list);
610 if (lhs_rank == 0 && rhs_rank != 0)
612 error_at (location, "%qE cannot be scalar when %qE is not", lhs, rhs);
613 return error_mark_node;
615 if (lhs_rank != 0 && rhs_rank != 0 && lhs_rank != rhs_rank)
617 error_at (location, "rank mismatch between %qE and %qE", lhs, rhs);
618 return error_mark_node;
621 /* Assign the array notation components to variable so that they can satisfy
622 the execute-once rule. */
623 for (ii = 0; ii < lhs_list_size; ii++)
625 tree anode = (*lhs_list)[ii];
626 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
627 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
628 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
630 for (ii = 0; ii < rhs_list_size; ii++)
631 if ((*rhs_list)[ii] && TREE_CODE ((*rhs_list)[ii]) == ARRAY_NOTATION_REF)
633 tree aa = (*rhs_list)[ii];
634 make_triplet_val_inv (&ARRAY_NOTATION_START (aa));
635 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (aa));
636 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (aa));
638 lhs_an_loop_info.safe_grow_cleared (lhs_rank);
640 if (rhs_rank)
641 rhs_an_loop_info.safe_grow_cleared (rhs_rank);
643 cond_expr.safe_grow_cleared (MAX (lhs_rank, rhs_rank));
644 cilkplus_extract_an_triplets (lhs_list, lhs_list_size, lhs_rank,
645 &lhs_an_info);
646 if (rhs_list)
647 cilkplus_extract_an_triplets (rhs_list, rhs_list_size, rhs_rank,
648 &rhs_an_info);
649 if (length_mismatch_in_expr_p (EXPR_LOCATION (lhs), lhs_an_info)
650 || (rhs_list && length_mismatch_in_expr_p (EXPR_LOCATION (rhs),
651 rhs_an_info)))
653 pop_stmt_list (an_init);
654 return error_mark_node;
656 tree rhs_len = ((rhs_list_size > 0 && rhs_rank > 0) ?
657 rhs_an_info[0][0].length : NULL_TREE);
658 tree lhs_len = ((lhs_list_size > 0 && lhs_rank > 0) ?
659 lhs_an_info[0][0].length : NULL_TREE);
660 if (lhs_list_size > 0 && rhs_list_size > 0 && lhs_rank > 0 && rhs_rank > 0
661 && TREE_CODE (lhs_len) == INTEGER_CST && rhs_len
662 && TREE_CODE (rhs_len) == INTEGER_CST
663 && !tree_int_cst_equal (rhs_len, lhs_len))
665 error_at (location, "length mismatch between LHS and RHS");
666 pop_stmt_list (an_init);
667 return error_mark_node;
669 for (ii = 0; ii < lhs_rank; ii++)
671 tree typ = ptrdiff_type_node;
672 lhs_an_loop_info[ii].var = create_temporary_var (typ);
673 add_decl_expr (lhs_an_loop_info[ii].var);
674 lhs_an_loop_info[ii].ind_init = build_x_modify_expr
675 (location, lhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
676 complain);
679 if (rhs_list_size > 0)
681 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
682 lhs_an_loop_info, lhs_rank,
683 lhs);
684 if (!rhs_array_operand)
685 return error_mark_node;
687 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
688 rhs_list_size = 0;
689 rhs_list = NULL;
690 extract_array_notation_exprs (rhs, true, &rhs_list);
691 rhs_list_size = vec_safe_length (rhs_list);
693 for (ii = 0; ii < rhs_rank; ii++)
695 tree typ = ptrdiff_type_node;
696 rhs_an_loop_info[ii].var = create_temporary_var (typ);
697 add_decl_expr (rhs_an_loop_info[ii].var);
698 rhs_an_loop_info[ii].ind_init = build_x_modify_expr
699 (location, rhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
700 complain);
703 if (lhs_rank)
705 lhs_array_operand =
706 create_array_refs (location, lhs_an_info, lhs_an_loop_info,
707 lhs_list_size, lhs_rank);
708 replace_array_notations (&lhs, true, lhs_list, lhs_array_operand);
711 if (rhs_array_operand)
712 vec_safe_truncate (rhs_array_operand, 0);
713 if (rhs_rank)
715 rhs_array_operand = create_array_refs (location, rhs_an_info,
716 rhs_an_loop_info, rhs_list_size,
717 rhs_rank);
718 /* Replace all the array refs created by the above function because this
719 variable is blown away by the fix_sec_implicit_args function below. */
720 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
721 vec_safe_truncate (rhs_array_operand , 0);
722 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
723 rhs_an_loop_info, rhs_rank,
724 rhs);
725 if (!rhs_array_operand)
726 return error_mark_node;
727 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
730 array_expr_rhs = rhs;
731 array_expr_lhs = lhs;
733 array_expr = build_x_modify_expr (location, array_expr_lhs, modifycode,
734 array_expr_rhs, complain);
735 create_cmp_incr (location, &lhs_an_loop_info, lhs_rank, lhs_an_info,
736 complain);
737 if (rhs_rank)
738 create_cmp_incr (location, &rhs_an_loop_info, rhs_rank, rhs_an_info,
739 complain);
740 for (ii = 0; ii < MAX (rhs_rank, lhs_rank); ii++)
741 if (ii < lhs_rank && ii < rhs_rank)
742 cond_expr[ii] = build_x_binary_op
743 (location, TRUTH_ANDIF_EXPR, lhs_an_loop_info[ii].cmp,
744 TREE_CODE (lhs_an_loop_info[ii].cmp), rhs_an_loop_info[ii].cmp,
745 TREE_CODE (rhs_an_loop_info[ii].cmp), NULL, complain);
746 else if (ii < lhs_rank && ii >= rhs_rank)
747 cond_expr[ii] = lhs_an_loop_info[ii].cmp;
748 else
749 /* No need to compare ii < rhs_rank && ii >= lhs_rank because in a valid
750 Array notation expression, rank of RHS cannot be greater than LHS. */
751 gcc_unreachable ();
753 an_init = pop_stmt_list (an_init);
754 append_to_statement_list (an_init, &loop_with_init);
755 body = array_expr;
756 for (ii = 0; ii < MAX (lhs_rank, rhs_rank); ii++)
758 tree incr_list = alloc_stmt_list ();
759 tree init_list = alloc_stmt_list ();
760 tree new_loop = push_stmt_list ();
762 if (lhs_rank)
764 append_to_statement_list (lhs_an_loop_info[ii].ind_init, &init_list);
765 append_to_statement_list (lhs_an_loop_info[ii].incr, &incr_list);
767 if (rhs_rank)
769 append_to_statement_list (rhs_an_loop_info[ii].ind_init, &init_list);
770 append_to_statement_list (rhs_an_loop_info[ii].incr, &incr_list);
772 create_an_loop (init_list, cond_expr[ii], incr_list, body);
773 body = pop_stmt_list (new_loop);
775 append_to_statement_list (body, &loop_with_init);
777 lhs_an_info.release ();
778 lhs_an_loop_info.release ();
779 if (rhs_rank)
781 rhs_an_info.release ();
782 rhs_an_loop_info.release ();
784 cond_expr.release ();
786 return loop_with_init;
789 /* Helper function for expand_conditonal_array_notations. Encloses the
790 conditional statement passed in ORIG_STMT with a loop around it and
791 replaces the condition in STMT with a ARRAY_REF tree-node to the array.
792 The condition must have a ARRAY_NOTATION_REF tree. */
794 static tree
795 cp_expand_cond_array_notations (tree orig_stmt)
797 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
798 size_t list_size = 0;
799 size_t rank = 0, ii = 0;
800 tree an_init, body, stmt = NULL_TREE;
801 tree builtin_loop, new_var = NULL_TREE;
802 tree loop_with_init = alloc_stmt_list ();
803 location_t location = UNKNOWN_LOCATION;
804 vec<vec<an_parts> > an_info = vNULL;
805 vec<an_loop_parts> an_loop_info = vNULL;
807 if (TREE_CODE (orig_stmt) == COND_EXPR)
809 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
810 tree yes_expr = COND_EXPR_THEN (orig_stmt);
811 tree no_expr = COND_EXPR_ELSE (orig_stmt);
812 tree cond = COND_EXPR_COND (orig_stmt);
813 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
814 || !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
815 &yes_rank)
816 || find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
817 &no_rank))
818 return error_mark_node;
819 /* If the condition has a zero rank, then handle array notations in body
820 separately. */
821 if (cond_rank == 0)
822 return orig_stmt;
823 if (cond_rank != yes_rank && yes_rank != 0)
825 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
826 " expression of parent if-statement");
827 return error_mark_node;
829 else if (cond_rank != no_rank && no_rank != 0)
831 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
832 "expression of parent if-statement");
833 return error_mark_node;
836 else if (TREE_CODE (orig_stmt) == IF_STMT)
838 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
839 tree yes_expr = THEN_CLAUSE (orig_stmt);
840 tree no_expr = ELSE_CLAUSE (orig_stmt);
841 tree cond = IF_COND (orig_stmt);
842 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
843 || (yes_expr
844 && !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
845 &yes_rank))
846 || (no_expr
847 && !find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
848 &no_rank)))
849 return error_mark_node;
851 /* Same reasoning as for COND_EXPR. */
852 if (cond_rank == 0)
853 return orig_stmt;
854 else if (cond_rank != yes_rank && yes_rank != 0)
856 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
857 " expression of parent if-statement");
858 return error_mark_node;
860 else if (cond_rank != no_rank && no_rank != 0)
862 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
863 "expression of parent if-statement");
864 return error_mark_node;
867 else if (truth_value_p (TREE_CODE (orig_stmt)))
869 size_t left_rank = 0, right_rank = 0;
870 tree left_expr = TREE_OPERAND (orig_stmt, 0);
871 tree right_expr = TREE_OPERAND (orig_stmt, 1);
872 if (!find_rank (EXPR_LOCATION (left_expr), left_expr, left_expr, true,
873 &left_rank)
874 || !find_rank (EXPR_LOCATION (right_expr), right_expr, right_expr,
875 true, &right_rank))
876 return error_mark_node;
877 if (right_rank == 0 && left_rank == 0)
878 return orig_stmt;
881 if (!find_rank (EXPR_LOCATION (orig_stmt), orig_stmt, orig_stmt, true,
882 &rank))
883 return error_mark_node;
884 if (rank == 0)
885 return orig_stmt;
887 extract_array_notation_exprs (orig_stmt, false, &array_list);
888 stmt = alloc_stmt_list ();
889 for (ii = 0; ii < vec_safe_length (array_list); ii++)
891 tree array_node = (*array_list)[ii];
892 if (TREE_CODE (array_node) == CALL_EXPR
893 || TREE_CODE (array_node) == AGGR_INIT_EXPR)
895 builtin_loop = expand_sec_reduce_builtin (array_node, &new_var);
896 if (builtin_loop == error_mark_node)
897 finish_expr_stmt (error_mark_node);
898 else if (new_var)
900 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
901 vec_safe_push (sub_list, array_node);
902 vec_safe_push (new_var_list, new_var);
903 replace_array_notations (&orig_stmt, false, sub_list,
904 new_var_list);
905 append_to_statement_list (builtin_loop, &stmt);
909 append_to_statement_list (orig_stmt, &stmt);
910 rank = 0;
911 array_list = NULL;
912 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
913 return error_mark_node;
914 if (rank == 0)
915 return stmt;
917 extract_array_notation_exprs (stmt, true, &array_list);
918 list_size = vec_safe_length (array_list);
919 if (list_size == 0)
920 return stmt;
922 location = EXPR_LOCATION (orig_stmt);
923 list_size = vec_safe_length (array_list);
924 an_loop_info.safe_grow_cleared (rank);
926 an_init = push_stmt_list ();
928 /* Assign the array notation components to variable so that they can
929 satisfy the exec-once rule. */
930 for (ii = 0; ii < list_size; ii++)
932 tree anode = (*array_list)[ii];
933 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
934 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
935 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
937 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
939 for (ii = 0; ii < rank; ii++)
941 tree typ = ptrdiff_type_node;
942 an_loop_info[ii].var = create_temporary_var (typ);
943 add_decl_expr (an_loop_info[ii].var);
944 an_loop_info[ii].ind_init =
945 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
946 build_zero_cst (typ), tf_warning_or_error);
948 array_operand = create_array_refs (location, an_info, an_loop_info,
949 list_size, rank);
950 replace_array_notations (&stmt, true, array_list, array_operand);
951 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
953 an_init = pop_stmt_list (an_init);
954 append_to_statement_list (an_init, &loop_with_init);
955 body = stmt;
957 for (ii = 0; ii < rank; ii++)
959 tree new_loop = push_stmt_list ();
960 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
961 an_loop_info[ii].incr, body);
962 body = pop_stmt_list (new_loop);
964 append_to_statement_list (body, &loop_with_init);
966 an_info.release ();
967 an_loop_info.release ();
969 return loop_with_init;
972 /* Transforms array notations inside unary expression ORIG_STMT with an
973 appropriate loop and ARRAY_REF (and returns all this as a super-tree called
974 LOOP). */
976 static tree
977 expand_unary_array_notation_exprs (tree orig_stmt)
979 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
980 size_t list_size = 0, rank = 0, ii = 0;
981 tree body;
982 tree builtin_loop, stmt = NULL_TREE, new_var = NULL_TREE;
983 location_t location = EXPR_LOCATION (orig_stmt);
984 tree an_init, loop_with_init = alloc_stmt_list ();
985 vec<vec<an_parts> > an_info = vNULL;
986 vec<an_loop_parts> an_loop_info = vNULL;
988 if (!find_rank (location, orig_stmt, orig_stmt, true, &rank))
989 return error_mark_node;
990 if (rank == 0)
991 return orig_stmt;
993 extract_array_notation_exprs (orig_stmt, false, &array_list);
994 list_size = vec_safe_length (array_list);
995 location = EXPR_LOCATION (orig_stmt);
996 stmt = NULL_TREE;
997 for (ii = 0; ii < list_size; ii++)
998 if (TREE_CODE ((*array_list)[ii]) == CALL_EXPR
999 || TREE_CODE ((*array_list)[ii]) == AGGR_INIT_EXPR)
1001 tree list_node = (*array_list)[ii];
1002 builtin_loop = expand_sec_reduce_builtin (list_node, &new_var);
1003 if (builtin_loop == error_mark_node)
1004 return error_mark_node;
1005 else if (builtin_loop)
1007 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
1008 stmt = alloc_stmt_list ();
1009 append_to_statement_list (builtin_loop, &stmt);
1010 vec_safe_push (sub_list, list_node);
1011 vec_safe_push (new_var_list, new_var);
1012 replace_array_notations (&orig_stmt, false, sub_list, new_var_list);
1015 if (stmt != NULL_TREE)
1016 append_to_statement_list (finish_expr_stmt (orig_stmt), &stmt);
1017 else
1018 stmt = orig_stmt;
1019 rank = 0;
1020 list_size = 0;
1021 array_list = NULL;
1022 extract_array_notation_exprs (stmt, true, &array_list);
1023 list_size = vec_safe_length (array_list);
1025 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
1026 return error_mark_node;
1027 if (rank == 0 || list_size == 0)
1028 return stmt;
1029 an_loop_info.safe_grow_cleared (rank);
1030 an_init = push_stmt_list ();
1031 /* Assign the array notation components to variable so that they can satisfy
1032 the exec-once rule. */
1033 for (ii = 0; ii < list_size; ii++)
1035 tree array_node = (*array_list)[ii];
1036 make_triplet_val_inv (&ARRAY_NOTATION_START (array_node));
1037 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (array_node));
1038 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (array_node));
1040 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
1042 for (ii = 0; ii < rank; ii++)
1044 tree typ = ptrdiff_type_node;
1045 an_loop_info[ii].var = create_temporary_var (typ);
1046 add_decl_expr (an_loop_info[ii].var);
1047 an_loop_info[ii].ind_init = build_x_modify_expr
1048 (location, an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
1049 tf_warning_or_error);
1051 array_operand = create_array_refs (location, an_info, an_loop_info,
1052 list_size, rank);
1053 replace_array_notations (&stmt, true, array_list, array_operand);
1054 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
1056 an_init = pop_stmt_list (an_init);
1057 append_to_statement_list (an_init, &loop_with_init);
1058 body = stmt;
1060 for (ii = 0; ii < rank; ii++)
1062 tree new_loop = push_stmt_list ();
1063 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
1064 an_loop_info[ii].incr, body);
1065 body = pop_stmt_list (new_loop);
1067 append_to_statement_list (body, &loop_with_init);
1069 an_info.release ();
1070 an_loop_info.release ();
1072 return loop_with_init;
1075 /* Expands the array notation's builtin reduction function in EXPR
1076 (of type RETURN_EXPR) and returns a STATEMENT_LIST that contains a loop
1077 with the builtin function expansion and a return statement at the end. */
1079 static tree
1080 expand_return_expr (tree expr)
1082 tree new_mod_list, new_var, new_mod, retval_expr;
1083 size_t rank = 0;
1084 location_t loc = EXPR_LOCATION (expr);
1085 if (TREE_CODE (expr) != RETURN_EXPR)
1086 return expr;
1088 if (!find_rank (loc, expr, expr, false, &rank))
1089 return error_mark_node;
1091 /* If the return expression contains array notations, then flag it as
1092 error. */
1093 if (rank >= 1)
1095 error_at (loc, "array notation expression cannot be used as a return "
1096 "value");
1097 return error_mark_node;
1100 new_mod_list = push_stmt_list ();
1101 retval_expr = TREE_OPERAND (expr, 0);
1102 new_var = create_temporary_var (TREE_TYPE (retval_expr));
1103 add_decl_expr (new_var);
1104 new_mod = expand_an_in_modify_expr (loc, new_var, NOP_EXPR,
1105 TREE_OPERAND (retval_expr, 1),
1106 tf_warning_or_error);
1107 TREE_OPERAND (retval_expr, 1) = new_var;
1108 TREE_OPERAND (expr, 0) = retval_expr;
1109 add_stmt (new_mod);
1110 add_stmt (expr);
1111 new_mod_list = pop_stmt_list (new_mod_list);
1112 return new_mod_list;
1115 /* Expands ARRAY_NOTATION_REF and builtin functions in a compound statement,
1116 STMT. Returns the STMT with expanded array notations. */
1118 tree
1119 expand_array_notation_exprs (tree t)
1121 enum tree_code code;
1122 bool is_expr;
1123 location_t loc = UNKNOWN_LOCATION;
1125 if (!t)
1126 return t;
1128 loc = EXPR_LOCATION (t);
1130 code = TREE_CODE (t);
1131 is_expr = IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code));
1132 switch (code)
1134 case ERROR_MARK:
1135 case IDENTIFIER_NODE:
1136 case VOID_CST:
1137 case INTEGER_CST:
1138 case REAL_CST:
1139 case FIXED_CST:
1140 case STRING_CST:
1141 case BLOCK:
1142 case PLACEHOLDER_EXPR:
1143 case FIELD_DECL:
1144 case VOID_TYPE:
1145 case REAL_TYPE:
1146 case SSA_NAME:
1147 case LABEL_DECL:
1148 case RESULT_DECL:
1149 case VAR_DECL:
1150 case PARM_DECL:
1151 case NON_LVALUE_EXPR:
1152 case NOP_EXPR:
1153 case ADDR_EXPR:
1154 case ARRAY_REF:
1155 case BIT_FIELD_REF:
1156 case VECTOR_CST:
1157 case COMPLEX_CST:
1158 return t;
1159 case INIT_EXPR:
1160 case MODIFY_EXPR:
1161 if (contains_array_notation_expr (t))
1162 t = expand_an_in_modify_expr (loc, TREE_OPERAND (t, 0), NOP_EXPR,
1163 TREE_OPERAND (t, 1),
1164 tf_warning_or_error);
1165 return t;
1166 case MODOP_EXPR:
1167 if (contains_array_notation_expr (t) && !processing_template_decl)
1168 t = expand_an_in_modify_expr
1169 (loc, TREE_OPERAND (t, 0), TREE_CODE (TREE_OPERAND (t, 1)),
1170 TREE_OPERAND (t, 2), tf_warning_or_error);
1171 return t;
1172 case CONSTRUCTOR:
1173 return t;
1174 case BIND_EXPR:
1176 BIND_EXPR_BODY (t) =
1177 expand_array_notation_exprs (BIND_EXPR_BODY (t));
1178 return t;
1180 case DECL_EXPR:
1181 if (contains_array_notation_expr (t))
1183 tree x = DECL_EXPR_DECL (t);
1184 if (DECL_INITIAL (x))
1186 location_t loc = DECL_SOURCE_LOCATION (x);
1187 tree lhs = x;
1188 tree rhs = DECL_INITIAL (x);
1189 DECL_INITIAL (x) = NULL;
1190 tree new_modify_expr = build_modify_expr (loc, lhs,
1191 TREE_TYPE (lhs),
1192 NOP_EXPR,
1193 loc, rhs,
1194 TREE_TYPE(rhs));
1195 t = expand_array_notation_exprs (new_modify_expr);
1198 return t;
1199 case STATEMENT_LIST:
1201 tree_stmt_iterator i;
1202 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
1203 *tsi_stmt_ptr (i) =
1204 expand_array_notation_exprs (*tsi_stmt_ptr (i));
1205 return t;
1208 case OMP_PARALLEL:
1209 case OMP_TASK:
1210 case OMP_FOR:
1211 case OMP_SINGLE:
1212 case OMP_SECTION:
1213 case OMP_SECTIONS:
1214 case OMP_MASTER:
1215 case OMP_TASKGROUP:
1216 case OMP_ORDERED:
1217 case OMP_CRITICAL:
1218 case OMP_ATOMIC:
1219 case OMP_CLAUSE:
1220 case TARGET_EXPR:
1221 case INTEGER_TYPE:
1222 case ENUMERAL_TYPE:
1223 case BOOLEAN_TYPE:
1224 case POINTER_TYPE:
1225 case ARRAY_TYPE:
1226 case RECORD_TYPE:
1227 case METHOD_TYPE:
1228 return t;
1229 case RETURN_EXPR:
1230 if (contains_array_notation_expr (t))
1231 t = expand_return_expr (t);
1232 return t;
1233 case PREDECREMENT_EXPR:
1234 case PREINCREMENT_EXPR:
1235 case POSTDECREMENT_EXPR:
1236 case POSTINCREMENT_EXPR:
1237 case AGGR_INIT_EXPR:
1238 case CALL_EXPR:
1239 t = expand_unary_array_notation_exprs (t);
1240 return t;
1241 case CONVERT_EXPR:
1242 case CLEANUP_POINT_EXPR:
1243 case EXPR_STMT:
1244 TREE_OPERAND (t, 0) = expand_array_notation_exprs (TREE_OPERAND (t, 0));
1245 /* It is not necessary to wrap error_mark_node in EXPR_STMT. */
1246 if (TREE_OPERAND (t, 0) == error_mark_node)
1247 return TREE_OPERAND (t, 0);
1248 return t;
1249 case TRUTH_ANDIF_EXPR:
1250 case TRUTH_ORIF_EXPR:
1251 case TRUTH_AND_EXPR:
1252 case TRUTH_OR_EXPR:
1253 case TRUTH_XOR_EXPR:
1254 case TRUTH_NOT_EXPR:
1255 case COND_EXPR:
1256 t = cp_expand_cond_array_notations (t);
1257 if (TREE_CODE (t) == COND_EXPR)
1259 COND_EXPR_THEN (t) =
1260 expand_array_notation_exprs (COND_EXPR_THEN (t));
1261 COND_EXPR_ELSE (t) =
1262 expand_array_notation_exprs (COND_EXPR_ELSE (t));
1264 return t;
1265 case FOR_STMT:
1266 if (contains_array_notation_expr (FOR_COND (t)))
1268 error_at (EXPR_LOCATION (FOR_COND (t)),
1269 "array notation cannot be used in a condition for "
1270 "a for-loop");
1271 return error_mark_node;
1273 /* FIXME: Add a check for CILK_FOR_STMT here when we add Cilk tasking
1274 keywords. */
1275 if (TREE_CODE (t) == FOR_STMT)
1277 FOR_BODY (t) = expand_array_notation_exprs (FOR_BODY (t));
1278 FOR_EXPR (t) = expand_array_notation_exprs (FOR_EXPR (t));
1280 else
1281 t = expand_array_notation_exprs (t);
1282 return t;
1283 case IF_STMT:
1284 t = cp_expand_cond_array_notations (t);
1285 /* If the above function added some extra instructions above the original
1286 if statement, then we can't assume it is still IF_STMT so we have to
1287 check again. */
1288 if (TREE_CODE (t) == IF_STMT)
1290 if (THEN_CLAUSE (t))
1291 THEN_CLAUSE (t) = expand_array_notation_exprs (THEN_CLAUSE (t));
1292 if (ELSE_CLAUSE (t))
1293 ELSE_CLAUSE (t) = expand_array_notation_exprs (ELSE_CLAUSE (t));
1295 else
1296 t = expand_array_notation_exprs (t);
1297 return t;
1298 case SWITCH_STMT:
1299 if (contains_array_notation_expr (SWITCH_STMT_COND (t)))
1301 error_at (EXPR_LOCATION (SWITCH_STMT_COND (t)),
1302 "array notation cannot be used as a condition for "
1303 "switch statement");
1304 return error_mark_node;
1306 if (SWITCH_STMT_BODY (t))
1307 SWITCH_STMT_BODY (t) =
1308 expand_array_notation_exprs (SWITCH_STMT_BODY (t));
1309 return t;
1310 case WHILE_STMT:
1311 if (contains_array_notation_expr (WHILE_COND (t)))
1313 if (EXPR_LOCATION (WHILE_COND (t)) != UNKNOWN_LOCATION)
1314 loc = EXPR_LOCATION (WHILE_COND (t));
1315 error_at (loc, "array notation cannot be used as a condition for "
1316 "while statement");
1317 return error_mark_node;
1319 if (WHILE_BODY (t))
1320 WHILE_BODY (t) = expand_array_notation_exprs (WHILE_BODY (t));
1321 return t;
1322 case DO_STMT:
1323 if (contains_array_notation_expr (DO_COND (t)))
1325 error_at (EXPR_LOCATION (DO_COND (t)),
1326 "array notation cannot be used as a condition for a "
1327 "do-while statement");
1328 return error_mark_node;
1330 if (DO_BODY (t))
1331 DO_BODY (t) = expand_array_notation_exprs (DO_BODY (t));
1332 return t;
1333 default:
1334 if (is_expr)
1336 int i, len;
1338 /* Walk over all the sub-trees of this operand. */
1339 len = TREE_CODE_LENGTH (code);
1341 /* Go through the subtrees. We need to do this in forward order so
1342 that the scope of a FOR_EXPR is handled properly. */
1343 for (i = 0; i < len; ++i)
1344 TREE_OPERAND (t, i) =
1345 expand_array_notation_exprs (TREE_OPERAND (t, i));
1347 return t;
1349 return t;
1352 /* Given the base of an array (ARRAY), the START (start_index), the number of
1353 elements to be accessed (LENGTH) and the STRIDE, construct an
1354 ARRAY_NOTATION_REF tree of type TYPE and return it. Restrictions on START,
1355 LENGTH and STRIDE are the same as that of index field passed into ARRAY_REF.
1356 The only additional restriction is that, unlike index in ARRAY_REF, stride,
1357 length and start_index cannot contain array notations. */
1359 tree
1360 build_array_notation_ref (location_t loc, tree array, tree start, tree length,
1361 tree stride, tree type)
1363 tree array_ntn_expr = NULL_TREE;
1365 /* If we enter the then-case of the if-statement below, we have hit a case
1366 like this: ARRAY [:]. */
1367 if (!start && !length)
1369 if (TREE_CODE (type) != ARRAY_TYPE)
1371 error_at (loc, "start-index and length fields necessary for "
1372 "using array notation in pointers or records");
1373 return error_mark_node;
1375 tree domain = TYPE_DOMAIN (type);
1376 if (!domain)
1378 error_at (loc, "start-index and length fields necessary for "
1379 "using array notation with array of unknown bound");
1380 return error_mark_node;
1382 start = cp_fold_convert (ptrdiff_type_node, TYPE_MINVAL (domain));
1383 length = size_binop (PLUS_EXPR, TYPE_MAXVAL (domain), size_one_node);
1384 length = cp_fold_convert (ptrdiff_type_node, length);
1387 if (!stride)
1388 stride = build_one_cst (ptrdiff_type_node);
1390 /* When dealing with templates, triplet type-checking will be done in pt.c
1391 after type substitution. */
1392 if (processing_template_decl
1393 && (type_dependent_expression_p (array)
1394 || type_dependent_expression_p (length)
1395 || type_dependent_expression_p (start)
1396 || type_dependent_expression_p (stride)))
1397 array_ntn_expr = build_min_nt_loc (loc, ARRAY_NOTATION_REF, array, start,
1398 length, stride, NULL_TREE);
1399 else
1401 if (!cilkplus_an_triplet_types_ok_p (loc, start, length, stride, type))
1402 return error_mark_node;
1403 array_ntn_expr = build4 (ARRAY_NOTATION_REF, NULL_TREE, array, start,
1404 length, stride);
1406 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == POINTER_TYPE)
1407 TREE_TYPE (array_ntn_expr) = TREE_TYPE (type);
1408 else
1410 error_at (loc, "base of array section must be pointer or array type");
1411 return error_mark_node;
1414 SET_EXPR_LOCATION (array_ntn_expr, loc);
1415 return array_ntn_expr;
1418 /* Returns false if any of the Array notation triplet values: START_INDEX,
1419 LENGTH and STRIDE, are not of integral type and have a rank greater than
1420 zero. */
1422 bool
1423 cilkplus_an_triplet_types_ok_p (location_t loc, tree start_index, tree length,
1424 tree stride, tree type)
1426 size_t stride_rank = 0, length_rank = 0, start_rank = 0;
1427 if (!TREE_TYPE (start_index) || !INTEGRAL_TYPE_P (TREE_TYPE (start_index)))
1429 error_at (loc, "start-index of array notation triplet is not an integer");
1430 return false;
1432 if (!TREE_TYPE (length) || !INTEGRAL_TYPE_P (TREE_TYPE (length)))
1434 error_at (loc, "length of array notation triplet is not an integer");
1435 return false;
1437 if (!TREE_TYPE (stride) || !INTEGRAL_TYPE_P (TREE_TYPE (stride)))
1439 error_at (loc, "stride of array notation triplet is not an integer");
1440 return false;
1442 if (TREE_CODE (type) == FUNCTION_TYPE)
1444 error_at (loc, "array notation cannot be used with function type");
1445 return false;
1447 if (!find_rank (loc, start_index, start_index, false, &start_rank)
1448 || !find_rank (loc, length, length, false, &length_rank)
1449 || !find_rank (loc, stride, stride, false, &stride_rank))
1450 return false;
1452 if (start_rank != 0)
1454 error_at (loc, "rank of an array notation triplet%'s start-index is not "
1455 "zero");
1456 return false;
1458 if (length_rank != 0)
1460 error_at (loc, "rank of an array notation triplet%'s length is not zero");
1461 return false;
1463 if (stride_rank != 0)
1465 error_at (loc, "rank of array notation triplet%'s stride is not zero");
1466 return false;
1468 return true;