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>,
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)
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:
29 3. Number of elements we need to access (we call it length)
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>;
55 #include "coretypes.h"
57 #include "tree-iterator.h"
59 /* Creates a FOR_STMT with INIT, COND, INCR and BODY as the initializer,
60 condition, increment expression and the loop-body, respectively. */
63 create_an_loop (tree init
, tree cond
, tree incr
, tree body
)
67 finish_expr_stmt (init
);
68 for_stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
69 finish_for_init_stmt (for_stmt
);
70 finish_for_cond (cond
, for_stmt
, false);
71 finish_for_expr (incr
, for_stmt
);
72 finish_expr_stmt (body
);
73 finish_for_stmt (for_stmt
);
76 /* If *VALUE is not a constant integer, then this function replaces it with
77 a variable to make it loop invariant for array notations. */
80 make_triplet_val_inv (tree
*value
)
82 if (TREE_CODE (*value
) != INTEGER_CST
83 && TREE_CODE (*value
) != PARM_DECL
85 *value
= get_temp_regvar (ptrdiff_type_node
, *value
);
88 /* Returns a vector of size RANK that contains an ARRAY_REF. This vector is
89 created using array notation-triplet information stored in AN_INFO. The
90 induction var is taken from AN_LOOP_INFO.
92 For example: For an array notation A[5:10:2], the vector start will be
93 of size 1 holding '5', stride of same size as start but holding the value of
94 as 2, and is_vector as true. Let's assume VAR is 'x'
95 This function returns a vector of size 1 with the following data:
99 static vec
<tree
, va_gc
> *
100 create_array_refs (location_t loc
, vec
<vec
<an_parts
> > an_info
,
101 vec
<an_loop_parts
> an_loop_info
, size_t size
, size_t rank
)
103 tree ind_mult
, ind_incr
;
104 vec
<tree
, va_gc
> *array_operand
= NULL
;
106 for (size_t ii
= 0; ii
< size
; ii
++)
107 if (an_info
[ii
][0].is_vector
)
109 tree array_opr
= an_info
[ii
][rank
- 1].value
;
110 for (int s_jj
= rank
-1; s_jj
>= 0; s_jj
--)
112 tree start
= cp_fold_convert (ptrdiff_type_node
,
113 an_info
[ii
][s_jj
].start
);
114 tree stride
= cp_fold_convert (ptrdiff_type_node
,
115 an_info
[ii
][s_jj
].stride
);
116 tree var
= cp_fold_convert (ptrdiff_type_node
,
117 an_loop_info
[s_jj
].var
);
119 ind_mult
= build2 (MULT_EXPR
, TREE_TYPE (var
), var
, stride
);
120 ind_incr
= build2 (PLUS_EXPR
, TREE_TYPE (var
), start
, ind_mult
);
121 /* Array [ start_index + (induction_var * stride)] */
122 array_opr
= grok_array_decl (loc
, array_opr
, ind_incr
, false);
124 vec_safe_push (array_operand
, array_opr
);
127 vec_safe_push (array_operand
, integer_one_node
);
128 return array_operand
;
131 /* Populates the INCR and CMP fields in *NODE with the increment
132 (of type POSTINCREMENT) and comparison (of TYPE LT_EXPR) expressions, using
133 data from AN_INFO. */
136 create_cmp_incr (location_t loc
, vec
<an_loop_parts
> *node
, size_t rank
,
137 vec
<vec
<an_parts
> > an_info
, tsubst_flags_t complain
)
139 for (size_t ii
= 0; ii
< rank
; ii
++)
141 (*node
)[ii
].incr
= build_x_unary_op (loc
, POSTINCREMENT_EXPR
,
142 (*node
)[ii
].var
, complain
);
143 (*node
)[ii
].cmp
= build_x_binary_op (loc
, LT_EXPR
, (*node
)[ii
].var
,
144 TREE_CODE ((*node
)[ii
].var
),
145 an_info
[0][ii
].length
,
146 TREE_CODE (an_info
[0][ii
].length
),
151 /* Replaces all the scalar expressions in *NODE. Returns a STATEMENT LIST that
152 holds the NODE along with the variables that hold the results of the
153 invariant expressions. */
156 replace_invariant_exprs (tree
*node
)
159 tree node_list
= NULL_TREE
;
160 tree t
= NULL_TREE
, new_var
= NULL_TREE
;
161 struct inv_list data
;
163 data
.list_values
= NULL
;
164 data
.replacement
= NULL
;
165 data
.additional_tcodes
= NULL
;
166 cp_walk_tree (node
, find_inv_trees
, (void *) &data
, NULL
);
168 if (vec_safe_length (data
.list_values
))
170 node_list
= push_stmt_list ();
171 for (ix
= 0; vec_safe_iterate (data
.list_values
, ix
, &t
); ix
++)
173 /* Sometimes, when comma_expr has a function call in it, it will
174 typecast it to void. Find_inv_trees finds those nodes and so
175 if it void type, then don't bother creating a new var to hold
177 if (VOID_TYPE_P (TREE_TYPE (t
)))
179 finish_expr_stmt (t
);
183 new_var
= get_temp_regvar (TREE_TYPE (t
), t
);
184 vec_safe_push (data
.replacement
, new_var
);
186 cp_walk_tree (node
, replace_inv_trees
, (void *) &data
, NULL
);
187 node_list
= pop_stmt_list (node_list
);
192 /* Replace array notation's built-in function passed in AN_BUILTIN_FN with
193 the appropriate loop and computation (all stored in variable LOOP of type
194 tree node). The output of the function is always a scalar and that
195 result is returned in *NEW_VAR. *NEW_VAR is NULL_TREE if the function is
196 __sec_reduce_mutating. */
199 expand_sec_reduce_builtin (tree an_builtin_fn
, tree
*new_var
)
201 tree new_var_type
= NULL_TREE
, func_parm
, new_yes_expr
, new_no_expr
;
202 tree array_ind_value
= NULL_TREE
, new_no_ind
, new_yes_ind
, new_no_list
;
203 tree new_yes_list
, new_cond_expr
, new_expr
= NULL_TREE
;
204 vec
<tree
, va_gc
> *array_list
= NULL
, *array_operand
= NULL
;
205 size_t list_size
= 0, rank
= 0, ii
= 0;
206 tree body
, an_init
, loop_with_init
= alloc_stmt_list ();
207 tree array_op0
, comp_node
= NULL_TREE
;
208 tree call_fn
= NULL_TREE
, identity_value
= NULL_TREE
;
209 tree init
= NULL_TREE
, cond_init
= NULL_TREE
;
210 enum tree_code code
= NOP_EXPR
;
211 location_t location
= UNKNOWN_LOCATION
;
212 vec
<vec
<an_parts
> > an_info
= vNULL
;
213 vec
<an_loop_parts
> an_loop_info
= vNULL
;
214 enum built_in_function an_type
=
215 is_cilkplus_reduce_builtin (CALL_EXPR_FN (an_builtin_fn
));
216 vec
<tree
, va_gc
> *func_args
;
218 if (an_type
== BUILT_IN_NONE
)
221 if (an_type
!= BUILT_IN_CILKPLUS_SEC_REDUCE
222 && an_type
!= BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
)
223 func_parm
= CALL_EXPR_ARG (an_builtin_fn
, 0);
226 call_fn
= CALL_EXPR_ARG (an_builtin_fn
, 2);
228 /* We need to do this because we are "faking" the builtin function types,
229 so the compiler does a bunch of typecasts and this will get rid of
231 STRIP_NOPS (call_fn
);
232 if (TREE_CODE (call_fn
) != OVERLOAD
233 && TREE_CODE (call_fn
) != FUNCTION_DECL
)
234 call_fn
= TREE_OPERAND (call_fn
, 0);
235 identity_value
= CALL_EXPR_ARG (an_builtin_fn
, 0);
236 func_parm
= CALL_EXPR_ARG (an_builtin_fn
, 1);
237 STRIP_NOPS (identity_value
);
239 STRIP_NOPS (func_parm
);
241 location
= EXPR_LOCATION (an_builtin_fn
);
243 /* Note about using find_rank (): If find_rank returns false, then it must
244 have already reported an error, thus we just return an error_mark_node
245 without any doing any error emission. */
246 if (!find_rank (location
, an_builtin_fn
, an_builtin_fn
, true, &rank
))
247 return error_mark_node
;
250 error_at (location
, "Invalid builtin arguments");
251 return error_mark_node
;
254 && (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
255 || an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
))
257 error_at (location
, "__sec_reduce_min_ind or __sec_reduce_max_ind cannot "
258 "have arrays with dimension greater than 1");
259 return error_mark_node
;
262 extract_array_notation_exprs (func_parm
, true, &array_list
);
263 list_size
= vec_safe_length (array_list
);
266 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
:
267 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
:
268 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
:
269 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
:
270 new_var_type
= TREE_TYPE ((*array_list
)[0]);
272 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
:
273 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
:
274 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO
:
275 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO
:
276 new_var_type
= boolean_type_node
;
278 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
:
279 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
:
280 new_var_type
= size_type_node
;
282 case BUILT_IN_CILKPLUS_SEC_REDUCE
:
283 if (call_fn
&& identity_value
)
284 new_var_type
= TREE_TYPE ((*array_list
)[0]);
286 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
:
287 new_var_type
= NULL_TREE
;
293 if (new_var_type
&& TREE_CODE (new_var_type
) == ARRAY_TYPE
)
294 new_var_type
= TREE_TYPE (new_var_type
);
295 an_loop_info
.safe_grow_cleared (rank
);
297 an_init
= push_stmt_list ();
299 /* Assign the array notation components to variable so that they can satisfy
300 the exec-once rule. */
301 for (ii
= 0; ii
< list_size
; ii
++)
302 if (TREE_CODE ((*array_list
)[ii
]) == ARRAY_NOTATION_REF
)
304 tree anode
= (*array_list
)[ii
];
305 make_triplet_val_inv (&ARRAY_NOTATION_START (anode
));
306 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode
));
307 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode
));
309 cilkplus_extract_an_triplets (array_list
, list_size
, rank
, &an_info
);
310 for (ii
= 0; ii
< rank
; ii
++)
312 tree typ
= ptrdiff_type_node
;
314 /* In this place, we are using get_temp_regvar instead of
315 create_temporary_var if an_type is SEC_REDUCE_MAX/MIN_IND because
316 the array_ind_value depends on this value being initalized to 0. */
317 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
318 || an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
)
319 an_loop_info
[ii
].var
= get_temp_regvar (typ
, build_zero_cst (typ
));
322 an_loop_info
[ii
].var
= create_temporary_var (typ
);
323 add_decl_expr (an_loop_info
[ii
].var
);
325 an_loop_info
[ii
].ind_init
=
326 build_x_modify_expr (location
, an_loop_info
[ii
].var
, INIT_EXPR
,
327 build_zero_cst (typ
), tf_warning_or_error
);
329 array_operand
= create_array_refs (location
, an_info
, an_loop_info
,
331 replace_array_notations (&func_parm
, true, array_list
, array_operand
);
333 if (!TREE_TYPE (func_parm
))
334 TREE_TYPE (func_parm
) = TREE_TYPE ((*array_list
)[0]);
336 create_cmp_incr (location
, &an_loop_info
, rank
, an_info
, tf_warning_or_error
);
337 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
338 || an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
)
339 array_ind_value
= get_temp_regvar (TREE_TYPE (func_parm
), func_parm
);
341 array_op0
= (*array_operand
)[0];
342 if (INDIRECT_REF_P (array_op0
))
343 array_op0
= TREE_OPERAND (array_op0
, 0);
346 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
:
348 init
= build_zero_cst (new_var_type
);
350 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
:
352 init
= build_one_cst (new_var_type
);
354 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
:
355 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO
:
356 code
= ((an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
) ? EQ_EXPR
358 init
= build_zero_cst (new_var_type
);
359 cond_init
= build_one_cst (new_var_type
);
360 comp_node
= build_zero_cst (TREE_TYPE (func_parm
));
362 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
:
363 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO
:
364 code
= ((an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
) ? NE_EXPR
366 init
= build_one_cst (new_var_type
);
367 cond_init
= build_zero_cst (new_var_type
);
368 comp_node
= build_zero_cst (TREE_TYPE (func_parm
));
370 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
:
372 init
= (TYPE_MIN_VALUE (new_var_type
) ? TYPE_MIN_VALUE (new_var_type
)
375 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
:
377 init
= (TYPE_MAX_VALUE (new_var_type
) ? TYPE_MAX_VALUE (new_var_type
)
380 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
:
381 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
:
382 code
= (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
? LE_EXPR
384 init
= an_loop_info
[0].var
;
386 case BUILT_IN_CILKPLUS_SEC_REDUCE
:
387 init
= identity_value
;
389 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
:
396 if (an_type
!= BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
)
397 *new_var
= get_temp_regvar (new_var_type
, init
);
399 *new_var
= NULL_TREE
;
403 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
:
404 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
:
405 new_expr
= build_x_modify_expr (location
, *new_var
, code
, func_parm
,
406 tf_warning_or_error
);
408 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
:
409 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO
:
410 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
:
411 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO
:
412 /* In all these cases, assume the false case is true and as soon as
413 we find a true case, set the true flag on and latch it in. */
414 new_yes_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
415 cond_init
, tf_warning_or_error
);
416 new_no_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
417 *new_var
, tf_warning_or_error
);
418 new_cond_expr
= build_x_binary_op
419 (location
, code
, func_parm
, TREE_CODE (func_parm
), comp_node
,
420 TREE_CODE (comp_node
), NULL
, tf_warning_or_error
);
421 new_expr
= build_x_conditional_expr (location
, new_cond_expr
,
422 new_yes_expr
, new_no_expr
,
423 tf_warning_or_error
);
425 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
:
426 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
:
427 new_cond_expr
= build_x_binary_op
428 (location
, code
, *new_var
, TREE_CODE (*new_var
), func_parm
,
429 TREE_CODE (func_parm
), NULL
, tf_warning_or_error
);
430 new_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
, func_parm
,
431 tf_warning_or_error
);
433 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
:
434 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
:
435 new_yes_expr
= build_x_modify_expr (location
, array_ind_value
, NOP_EXPR
,
436 func_parm
, tf_warning_or_error
);
437 new_no_expr
= build_x_modify_expr (location
, array_ind_value
, NOP_EXPR
,
438 array_ind_value
, tf_warning_or_error
);
440 new_yes_ind
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
442 tf_warning_or_error
);
444 new_yes_ind
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
445 TREE_OPERAND (array_op0
, 1),
446 tf_warning_or_error
);
447 new_no_ind
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
, *new_var
,
448 tf_warning_or_error
);
449 new_yes_list
= alloc_stmt_list ();
450 append_to_statement_list (new_yes_ind
, &new_yes_list
);
451 append_to_statement_list (new_yes_expr
, &new_yes_list
);
453 new_no_list
= alloc_stmt_list ();
454 append_to_statement_list (new_no_ind
, &new_no_list
);
455 append_to_statement_list (new_no_expr
, &new_no_list
);
457 new_cond_expr
= build_x_binary_op (location
, code
, array_ind_value
,
458 TREE_CODE (array_ind_value
), func_parm
,
459 TREE_CODE (func_parm
), NULL
,
460 tf_warning_or_error
);
461 new_expr
= build_x_conditional_expr (location
, new_cond_expr
,
462 new_yes_list
, new_no_list
,
463 tf_warning_or_error
);
465 case BUILT_IN_CILKPLUS_SEC_REDUCE
:
466 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
:
467 func_args
= make_tree_vector ();
468 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE
)
469 vec_safe_push (func_args
, *new_var
);
471 vec_safe_push (func_args
, identity_value
);
472 vec_safe_push (func_args
, func_parm
);
474 new_expr
= finish_call_expr (call_fn
, &func_args
, false, true,
475 tf_warning_or_error
);
476 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE
)
477 new_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
, new_expr
,
478 tf_warning_or_error
);
479 release_tree_vector (func_args
);
484 an_init
= pop_stmt_list (an_init
);
485 append_to_statement_list (an_init
, &loop_with_init
);
488 for (ii
= 0; ii
< rank
; ii
++)
490 tree new_loop
= push_stmt_list ();
491 create_an_loop (an_loop_info
[ii
].ind_init
, an_loop_info
[ii
].cmp
,
492 an_loop_info
[ii
].incr
, body
);
493 body
= pop_stmt_list (new_loop
);
495 append_to_statement_list (body
, &loop_with_init
);
498 an_loop_info
.release ();
500 return loop_with_init
;
503 /* Returns a loop with ARRAY_REF inside it with an appropriate modify expr.
504 The LHS and/or RHS will be array notation expressions that have a
505 MODIFYCODE. The location of the variable is specified by LOCATION. */
508 expand_an_in_modify_expr (location_t location
, tree lhs
,
509 enum tree_code modifycode
, tree rhs
,
510 tsubst_flags_t complain
)
512 tree array_expr_lhs
= NULL_TREE
, array_expr_rhs
= NULL_TREE
;
513 tree array_expr
= NULL_TREE
;
514 tree body
= NULL_TREE
;
515 vec
<tree
> cond_expr
= vNULL
;
516 vec
<tree
, va_gc
> *lhs_array_operand
= NULL
, *rhs_array_operand
= NULL
;
517 size_t lhs_rank
= 0, rhs_rank
= 0, ii
= 0;
518 vec
<tree
, va_gc
> *rhs_list
= NULL
, *lhs_list
= NULL
;
519 size_t rhs_list_size
= 0, lhs_list_size
= 0;
520 tree new_modify_expr
, new_var
= NULL_TREE
, builtin_loop
, scalar_mods
;
521 bool found_builtin_fn
= false;
522 tree an_init
, loop_with_init
= alloc_stmt_list ();
523 vec
<vec
<an_parts
> > lhs_an_info
= vNULL
, rhs_an_info
= vNULL
;
524 vec
<an_loop_parts
> lhs_an_loop_info
= vNULL
, rhs_an_loop_info
= vNULL
;
526 if (!find_rank (location
, rhs
, rhs
, false, &rhs_rank
))
527 return error_mark_node
;
528 extract_array_notation_exprs (rhs
, false, &rhs_list
);
529 rhs_list_size
= vec_safe_length (rhs_list
);
530 an_init
= push_stmt_list ();
533 scalar_mods
= replace_invariant_exprs (&rhs
);
535 finish_expr_stmt (scalar_mods
);
537 for (ii
= 0; ii
< rhs_list_size
; ii
++)
539 tree rhs_node
= (*rhs_list
)[ii
];
540 if (TREE_CODE (rhs_node
) == CALL_EXPR
)
542 builtin_loop
= expand_sec_reduce_builtin (rhs_node
, &new_var
);
543 if (builtin_loop
== error_mark_node
)
544 return error_mark_node
;
545 else if (builtin_loop
)
547 finish_expr_stmt (builtin_loop
);
548 found_builtin_fn
= true;
551 vec
<tree
, va_gc
> *rhs_sub_list
= NULL
, *new_var_list
= NULL
;
552 vec_safe_push (rhs_sub_list
, rhs_node
);
553 vec_safe_push (new_var_list
, new_var
);
554 replace_array_notations (&rhs
, false, rhs_sub_list
,
562 if (!find_rank (location
, lhs
, lhs
, true, &lhs_rank
)
563 || !find_rank (location
, rhs
, rhs
, true, &rhs_rank
))
565 pop_stmt_list (an_init
);
566 return error_mark_node
;
569 /* If both are scalar, then the only reason why we will get this far is if
570 there is some array notations inside it and was using a builtin array
571 notation functions. If so, we have already broken those guys up and now
572 a simple build_x_modify_expr would do. */
573 if (lhs_rank
== 0 && rhs_rank
== 0)
575 if (found_builtin_fn
)
577 new_modify_expr
= build_x_modify_expr (location
, lhs
,
578 modifycode
, rhs
, complain
);
579 finish_expr_stmt (new_modify_expr
);
580 pop_stmt_list (an_init
);
587 /* If for some reason location is not set, then find if LHS or RHS has
588 location info. If so, then use that so we atleast have an idea. */
589 if (location
== UNKNOWN_LOCATION
)
591 if (EXPR_LOCATION (lhs
) != UNKNOWN_LOCATION
)
592 location
= EXPR_LOCATION (lhs
);
593 else if (EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
)
594 location
= EXPR_LOCATION (rhs
);
597 /* We need this when we have a scatter issue. */
598 extract_array_notation_exprs (lhs
, true, &lhs_list
);
600 extract_array_notation_exprs (rhs
, true, &rhs_list
);
601 rhs_list_size
= vec_safe_length (rhs_list
);
602 lhs_list_size
= vec_safe_length (lhs_list
);
604 if (lhs_rank
== 0 && rhs_rank
!= 0)
606 error_at (location
, "%qE cannot be scalar when %qE is not", lhs
, rhs
);
607 return error_mark_node
;
609 if (lhs_rank
!= 0 && rhs_rank
!= 0 && lhs_rank
!= rhs_rank
)
611 error_at (location
, "rank mismatch between %qE and %qE", lhs
, rhs
);
612 return error_mark_node
;
615 /* Assign the array notation components to variable so that they can satisfy
616 the execute-once rule. */
617 for (ii
= 0; ii
< lhs_list_size
; ii
++)
619 tree anode
= (*lhs_list
)[ii
];
620 make_triplet_val_inv (&ARRAY_NOTATION_START (anode
));
621 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode
));
622 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode
));
624 for (ii
= 0; ii
< rhs_list_size
; ii
++)
625 if ((*rhs_list
)[ii
] && TREE_CODE ((*rhs_list
)[ii
]) == ARRAY_NOTATION_REF
)
627 tree aa
= (*rhs_list
)[ii
];
628 make_triplet_val_inv (&ARRAY_NOTATION_START (aa
));
629 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (aa
));
630 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (aa
));
632 lhs_an_loop_info
.safe_grow_cleared (lhs_rank
);
635 rhs_an_loop_info
.safe_grow_cleared (rhs_rank
);
637 cond_expr
.safe_grow_cleared (MAX (lhs_rank
, rhs_rank
));
638 cilkplus_extract_an_triplets (lhs_list
, lhs_list_size
, lhs_rank
,
641 cilkplus_extract_an_triplets (rhs_list
, rhs_list_size
, rhs_rank
,
643 if (length_mismatch_in_expr_p (EXPR_LOCATION (lhs
), lhs_an_info
)
644 || (rhs_list
&& length_mismatch_in_expr_p (EXPR_LOCATION (rhs
),
647 pop_stmt_list (an_init
);
648 return error_mark_node
;
650 tree rhs_len
= ((rhs_list_size
> 0 && rhs_rank
> 0) ?
651 rhs_an_info
[0][0].length
: NULL_TREE
);
652 tree lhs_len
= ((lhs_list_size
> 0 && lhs_rank
> 0) ?
653 lhs_an_info
[0][0].length
: NULL_TREE
);
654 if (lhs_list_size
> 0 && rhs_list_size
> 0 && lhs_rank
> 0 && rhs_rank
> 0
655 && TREE_CODE (lhs_len
) == INTEGER_CST
&& rhs_len
656 && TREE_CODE (rhs_len
) == INTEGER_CST
657 && !tree_int_cst_equal (rhs_len
, lhs_len
))
659 error_at (location
, "length mismatch between LHS and RHS");
660 pop_stmt_list (an_init
);
661 return error_mark_node
;
663 for (ii
= 0; ii
< lhs_rank
; ii
++)
665 tree typ
= ptrdiff_type_node
;
666 lhs_an_loop_info
[ii
].var
= create_temporary_var (typ
);
667 add_decl_expr (lhs_an_loop_info
[ii
].var
);
668 lhs_an_loop_info
[ii
].ind_init
= build_x_modify_expr
669 (location
, lhs_an_loop_info
[ii
].var
, INIT_EXPR
, build_zero_cst (typ
),
673 if (rhs_list_size
> 0)
675 rhs_array_operand
= fix_sec_implicit_args (location
, rhs_list
,
676 lhs_an_loop_info
, lhs_rank
,
678 if (!rhs_array_operand
)
679 return error_mark_node
;
681 replace_array_notations (&rhs
, true, rhs_list
, rhs_array_operand
);
684 extract_array_notation_exprs (rhs
, true, &rhs_list
);
685 rhs_list_size
= vec_safe_length (rhs_list
);
687 for (ii
= 0; ii
< rhs_rank
; ii
++)
689 tree typ
= ptrdiff_type_node
;
690 rhs_an_loop_info
[ii
].var
= create_temporary_var (typ
);
691 add_decl_expr (rhs_an_loop_info
[ii
].var
);
692 rhs_an_loop_info
[ii
].ind_init
= build_x_modify_expr
693 (location
, rhs_an_loop_info
[ii
].var
, INIT_EXPR
, build_zero_cst (typ
),
700 create_array_refs (location
, lhs_an_info
, lhs_an_loop_info
,
701 lhs_list_size
, lhs_rank
);
702 replace_array_notations (&lhs
, true, lhs_list
, lhs_array_operand
);
705 if (rhs_array_operand
)
706 vec_safe_truncate (rhs_array_operand
, 0);
709 rhs_array_operand
= create_array_refs (location
, rhs_an_info
,
710 rhs_an_loop_info
, rhs_list_size
,
712 /* Replace all the array refs created by the above function because this
713 variable is blown away by the fix_sec_implicit_args function below. */
714 replace_array_notations (&rhs
, true, rhs_list
, rhs_array_operand
);
715 vec_safe_truncate (rhs_array_operand
, 0);
716 rhs_array_operand
= fix_sec_implicit_args (location
, rhs_list
,
717 rhs_an_loop_info
, rhs_rank
,
719 if (!rhs_array_operand
)
720 return error_mark_node
;
721 replace_array_notations (&rhs
, true, rhs_list
, rhs_array_operand
);
724 array_expr_rhs
= rhs
;
725 array_expr_lhs
= lhs
;
727 array_expr
= build_x_modify_expr (location
, array_expr_lhs
, modifycode
,
728 array_expr_rhs
, complain
);
729 create_cmp_incr (location
, &lhs_an_loop_info
, lhs_rank
, lhs_an_info
,
732 create_cmp_incr (location
, &rhs_an_loop_info
, rhs_rank
, rhs_an_info
,
734 for (ii
= 0; ii
< MAX (rhs_rank
, lhs_rank
); ii
++)
735 if (ii
< lhs_rank
&& ii
< rhs_rank
)
736 cond_expr
[ii
] = build_x_binary_op
737 (location
, TRUTH_ANDIF_EXPR
, lhs_an_loop_info
[ii
].cmp
,
738 TREE_CODE (lhs_an_loop_info
[ii
].cmp
), rhs_an_loop_info
[ii
].cmp
,
739 TREE_CODE (rhs_an_loop_info
[ii
].cmp
), NULL
, complain
);
740 else if (ii
< lhs_rank
&& ii
>= rhs_rank
)
741 cond_expr
[ii
] = lhs_an_loop_info
[ii
].cmp
;
743 /* No need to compare ii < rhs_rank && ii >= lhs_rank because in a valid
744 Array notation expression, rank of RHS cannot be greater than LHS. */
747 an_init
= pop_stmt_list (an_init
);
748 append_to_statement_list (an_init
, &loop_with_init
);
750 for (ii
= 0; ii
< MAX (lhs_rank
, rhs_rank
); ii
++)
752 tree incr_list
= alloc_stmt_list ();
753 tree init_list
= alloc_stmt_list ();
754 tree new_loop
= push_stmt_list ();
758 append_to_statement_list (lhs_an_loop_info
[ii
].ind_init
, &init_list
);
759 append_to_statement_list (lhs_an_loop_info
[ii
].incr
, &incr_list
);
763 append_to_statement_list (rhs_an_loop_info
[ii
].ind_init
, &init_list
);
764 append_to_statement_list (rhs_an_loop_info
[ii
].incr
, &incr_list
);
766 create_an_loop (init_list
, cond_expr
[ii
], incr_list
, body
);
767 body
= pop_stmt_list (new_loop
);
769 append_to_statement_list (body
, &loop_with_init
);
771 lhs_an_info
.release ();
772 lhs_an_loop_info
.release ();
775 rhs_an_info
.release ();
776 rhs_an_loop_info
.release ();
778 cond_expr
.release ();
780 return loop_with_init
;
783 /* Helper function for expand_conditonal_array_notations. Encloses the
784 conditional statement passed in ORIG_STMT with a loop around it and
785 replaces the condition in STMT with a ARRAY_REF tree-node to the array.
786 The condition must have a ARRAY_NOTATION_REF tree. */
789 cp_expand_cond_array_notations (tree orig_stmt
)
791 vec
<tree
, va_gc
> *array_list
= NULL
, *array_operand
= NULL
;
792 size_t list_size
= 0;
793 size_t rank
= 0, ii
= 0;
794 tree an_init
, body
, stmt
= NULL_TREE
;
795 tree builtin_loop
, new_var
= NULL_TREE
;
796 tree loop_with_init
= alloc_stmt_list ();
797 location_t location
= UNKNOWN_LOCATION
;
798 vec
<vec
<an_parts
> > an_info
= vNULL
;
799 vec
<an_loop_parts
> an_loop_info
= vNULL
;
801 if (TREE_CODE (orig_stmt
) == COND_EXPR
)
803 size_t cond_rank
= 0, yes_rank
= 0, no_rank
= 0;
804 tree yes_expr
= COND_EXPR_THEN (orig_stmt
);
805 tree no_expr
= COND_EXPR_ELSE (orig_stmt
);
806 tree cond
= COND_EXPR_COND (orig_stmt
);
807 if (!find_rank (EXPR_LOCATION (cond
), cond
, cond
, true, &cond_rank
)
808 || !find_rank (EXPR_LOCATION (yes_expr
), yes_expr
, yes_expr
, true,
810 || find_rank (EXPR_LOCATION (no_expr
), no_expr
, no_expr
, true,
812 return error_mark_node
;
813 /* If the condition has a zero rank, then handle array notations in body
817 if (cond_rank
!= yes_rank
&& yes_rank
!= 0)
819 error_at (EXPR_LOCATION (yes_expr
), "rank mismatch with controlling"
820 " expression of parent if-statement");
821 return error_mark_node
;
823 else if (cond_rank
!= no_rank
&& no_rank
!= 0)
825 error_at (EXPR_LOCATION (no_expr
), "rank mismatch with controlling "
826 "expression of parent if-statement");
827 return error_mark_node
;
830 else if (TREE_CODE (orig_stmt
) == IF_STMT
)
832 size_t cond_rank
= 0, yes_rank
= 0, no_rank
= 0;
833 tree yes_expr
= THEN_CLAUSE (orig_stmt
);
834 tree no_expr
= ELSE_CLAUSE (orig_stmt
);
835 tree cond
= IF_COND (orig_stmt
);
836 if (!find_rank (EXPR_LOCATION (cond
), cond
, cond
, true, &cond_rank
)
838 && !find_rank (EXPR_LOCATION (yes_expr
), yes_expr
, yes_expr
, true,
841 && !find_rank (EXPR_LOCATION (no_expr
), no_expr
, no_expr
, true,
843 return error_mark_node
;
845 /* Same reasoning as for COND_EXPR. */
848 else if (cond_rank
!= yes_rank
&& yes_rank
!= 0)
850 error_at (EXPR_LOCATION (yes_expr
), "rank mismatch with controlling"
851 " expression of parent if-statement");
852 return error_mark_node
;
854 else if (cond_rank
!= no_rank
&& no_rank
!= 0)
856 error_at (EXPR_LOCATION (no_expr
), "rank mismatch with controlling "
857 "expression of parent if-statement");
858 return error_mark_node
;
861 else if (truth_value_p (TREE_CODE (orig_stmt
)))
863 size_t left_rank
= 0, right_rank
= 0;
864 tree left_expr
= TREE_OPERAND (orig_stmt
, 0);
865 tree right_expr
= TREE_OPERAND (orig_stmt
, 1);
866 if (!find_rank (EXPR_LOCATION (left_expr
), left_expr
, left_expr
, true,
868 || !find_rank (EXPR_LOCATION (right_expr
), right_expr
, right_expr
,
870 return error_mark_node
;
871 if (right_rank
== 0 && left_rank
== 0)
875 if (!find_rank (EXPR_LOCATION (orig_stmt
), orig_stmt
, orig_stmt
, true,
877 return error_mark_node
;
881 extract_array_notation_exprs (orig_stmt
, false, &array_list
);
882 stmt
= alloc_stmt_list ();
883 for (ii
= 0; ii
< vec_safe_length (array_list
); ii
++)
885 tree array_node
= (*array_list
)[ii
];
886 if (TREE_CODE (array_node
) == CALL_EXPR
887 || TREE_CODE (array_node
) == AGGR_INIT_EXPR
)
889 builtin_loop
= expand_sec_reduce_builtin (array_node
, &new_var
);
890 if (builtin_loop
== error_mark_node
)
891 finish_expr_stmt (error_mark_node
);
894 vec
<tree
, va_gc
> *sub_list
= NULL
, *new_var_list
= NULL
;
895 vec_safe_push (sub_list
, array_node
);
896 vec_safe_push (new_var_list
, new_var
);
897 replace_array_notations (&orig_stmt
, false, sub_list
,
899 append_to_statement_list (builtin_loop
, &stmt
);
903 append_to_statement_list (orig_stmt
, &stmt
);
906 if (!find_rank (EXPR_LOCATION (stmt
), stmt
, stmt
, true, &rank
))
907 return error_mark_node
;
911 extract_array_notation_exprs (stmt
, true, &array_list
);
912 list_size
= vec_safe_length (array_list
);
916 location
= EXPR_LOCATION (orig_stmt
);
917 list_size
= vec_safe_length (array_list
);
918 an_loop_info
.safe_grow_cleared (rank
);
920 an_init
= push_stmt_list ();
922 /* Assign the array notation components to variable so that they can
923 satisfy the exec-once rule. */
924 for (ii
= 0; ii
< list_size
; ii
++)
926 tree anode
= (*array_list
)[ii
];
927 make_triplet_val_inv (&ARRAY_NOTATION_START (anode
));
928 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode
));
929 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode
));
931 cilkplus_extract_an_triplets (array_list
, list_size
, rank
, &an_info
);
933 for (ii
= 0; ii
< rank
; ii
++)
935 tree typ
= ptrdiff_type_node
;
936 an_loop_info
[ii
].var
= create_temporary_var (typ
);
937 add_decl_expr (an_loop_info
[ii
].var
);
938 an_loop_info
[ii
].ind_init
=
939 build_x_modify_expr (location
, an_loop_info
[ii
].var
, INIT_EXPR
,
940 build_zero_cst (typ
), tf_warning_or_error
);
942 array_operand
= create_array_refs (location
, an_info
, an_loop_info
,
944 replace_array_notations (&stmt
, true, array_list
, array_operand
);
945 create_cmp_incr (location
, &an_loop_info
, rank
, an_info
, tf_warning_or_error
);
947 an_init
= pop_stmt_list (an_init
);
948 append_to_statement_list (an_init
, &loop_with_init
);
951 for (ii
= 0; ii
< rank
; ii
++)
953 tree new_loop
= push_stmt_list ();
954 create_an_loop (an_loop_info
[ii
].ind_init
, an_loop_info
[ii
].cmp
,
955 an_loop_info
[ii
].incr
, body
);
956 body
= pop_stmt_list (new_loop
);
958 append_to_statement_list (body
, &loop_with_init
);
961 an_loop_info
.release ();
963 return loop_with_init
;
966 /* Transforms array notations inside unary expression ORIG_STMT with an
967 appropriate loop and ARRAY_REF (and returns all this as a super-tree called
971 expand_unary_array_notation_exprs (tree orig_stmt
)
973 vec
<tree
, va_gc
> *array_list
= NULL
, *array_operand
= NULL
;
974 size_t list_size
= 0, rank
= 0, ii
= 0;
976 tree builtin_loop
, stmt
= NULL_TREE
, new_var
= NULL_TREE
;
977 location_t location
= EXPR_LOCATION (orig_stmt
);
978 tree an_init
, loop_with_init
= alloc_stmt_list ();
979 vec
<vec
<an_parts
> > an_info
= vNULL
;
980 vec
<an_loop_parts
> an_loop_info
= vNULL
;
982 if (!find_rank (location
, orig_stmt
, orig_stmt
, true, &rank
))
983 return error_mark_node
;
987 extract_array_notation_exprs (orig_stmt
, false, &array_list
);
988 list_size
= vec_safe_length (array_list
);
989 location
= EXPR_LOCATION (orig_stmt
);
991 for (ii
= 0; ii
< list_size
; ii
++)
992 if (TREE_CODE ((*array_list
)[ii
]) == CALL_EXPR
993 || TREE_CODE ((*array_list
)[ii
]) == AGGR_INIT_EXPR
)
995 tree list_node
= (*array_list
)[ii
];
996 builtin_loop
= expand_sec_reduce_builtin (list_node
, &new_var
);
997 if (builtin_loop
== error_mark_node
)
998 return error_mark_node
;
999 else if (builtin_loop
)
1001 vec
<tree
, va_gc
> *sub_list
= NULL
, *new_var_list
= NULL
;
1002 stmt
= alloc_stmt_list ();
1003 append_to_statement_list (builtin_loop
, &stmt
);
1004 vec_safe_push (sub_list
, list_node
);
1005 vec_safe_push (new_var_list
, new_var
);
1006 replace_array_notations (&orig_stmt
, false, sub_list
, new_var_list
);
1009 if (stmt
!= NULL_TREE
)
1010 append_to_statement_list (finish_expr_stmt (orig_stmt
), &stmt
);
1016 extract_array_notation_exprs (stmt
, true, &array_list
);
1017 list_size
= vec_safe_length (array_list
);
1019 if (!find_rank (EXPR_LOCATION (stmt
), stmt
, stmt
, true, &rank
))
1020 return error_mark_node
;
1021 if (rank
== 0 || list_size
== 0)
1023 an_loop_info
.safe_grow_cleared (rank
);
1024 an_init
= push_stmt_list ();
1025 /* Assign the array notation components to variable so that they can satisfy
1026 the exec-once rule. */
1027 for (ii
= 0; ii
< list_size
; ii
++)
1029 tree array_node
= (*array_list
)[ii
];
1030 make_triplet_val_inv (&ARRAY_NOTATION_START (array_node
));
1031 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (array_node
));
1032 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (array_node
));
1034 cilkplus_extract_an_triplets (array_list
, list_size
, rank
, &an_info
);
1036 for (ii
= 0; ii
< rank
; ii
++)
1038 tree typ
= ptrdiff_type_node
;
1039 an_loop_info
[ii
].var
= create_temporary_var (typ
);
1040 add_decl_expr (an_loop_info
[ii
].var
);
1041 an_loop_info
[ii
].ind_init
= build_x_modify_expr
1042 (location
, an_loop_info
[ii
].var
, INIT_EXPR
, build_zero_cst (typ
),
1043 tf_warning_or_error
);
1045 array_operand
= create_array_refs (location
, an_info
, an_loop_info
,
1047 replace_array_notations (&stmt
, true, array_list
, array_operand
);
1048 create_cmp_incr (location
, &an_loop_info
, rank
, an_info
, tf_warning_or_error
);
1050 an_init
= pop_stmt_list (an_init
);
1051 append_to_statement_list (an_init
, &loop_with_init
);
1054 for (ii
= 0; ii
< rank
; ii
++)
1056 tree new_loop
= push_stmt_list ();
1057 create_an_loop (an_loop_info
[ii
].ind_init
, an_loop_info
[ii
].cmp
,
1058 an_loop_info
[ii
].incr
, body
);
1059 body
= pop_stmt_list (new_loop
);
1061 append_to_statement_list (body
, &loop_with_init
);
1064 an_loop_info
.release ();
1066 return loop_with_init
;
1069 /* Expands the array notation's builtin reduction function in EXPR
1070 (of type RETURN_EXPR) and returns a STATEMENT_LIST that contains a loop
1071 with the builtin function expansion and a return statement at the end. */
1074 expand_return_expr (tree expr
)
1076 tree new_mod_list
, new_var
, new_mod
, retval_expr
;
1078 location_t loc
= EXPR_LOCATION (expr
);
1079 if (TREE_CODE (expr
) != RETURN_EXPR
)
1082 if (!find_rank (loc
, expr
, expr
, false, &rank
))
1083 return error_mark_node
;
1085 /* If the return expression contains array notations, then flag it as
1089 error_at (loc
, "array notation expression cannot be used as a return "
1091 return error_mark_node
;
1094 new_mod_list
= push_stmt_list ();
1095 retval_expr
= TREE_OPERAND (expr
, 0);
1096 new_var
= create_temporary_var (TREE_TYPE (retval_expr
));
1097 add_decl_expr (new_var
);
1098 new_mod
= expand_an_in_modify_expr (loc
, new_var
, NOP_EXPR
,
1099 TREE_OPERAND (retval_expr
, 1),
1100 tf_warning_or_error
);
1101 TREE_OPERAND (retval_expr
, 1) = new_var
;
1102 TREE_OPERAND (expr
, 0) = retval_expr
;
1105 new_mod_list
= pop_stmt_list (new_mod_list
);
1106 return new_mod_list
;
1109 /* Expands ARRAY_NOTATION_REF and builtin functions in a compound statement,
1110 STMT. Returns the STMT with expanded array notations. */
1113 expand_array_notation_exprs (tree t
)
1115 enum tree_code code
;
1117 location_t loc
= UNKNOWN_LOCATION
;
1122 loc
= EXPR_LOCATION (t
);
1124 code
= TREE_CODE (t
);
1125 is_expr
= IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
));
1129 case IDENTIFIER_NODE
:
1136 case PLACEHOLDER_EXPR
:
1145 case NON_LVALUE_EXPR
:
1155 if (contains_array_notation_expr (t
))
1156 t
= expand_an_in_modify_expr (loc
, TREE_OPERAND (t
, 0), NOP_EXPR
,
1157 TREE_OPERAND (t
, 1),
1158 tf_warning_or_error
);
1161 if (contains_array_notation_expr (t
) && !processing_template_decl
)
1162 t
= expand_an_in_modify_expr
1163 (loc
, TREE_OPERAND (t
, 0), TREE_CODE (TREE_OPERAND (t
, 1)),
1164 TREE_OPERAND (t
, 2), tf_warning_or_error
);
1170 BIND_EXPR_BODY (t
) =
1171 expand_array_notation_exprs (BIND_EXPR_BODY (t
));
1175 if (contains_array_notation_expr (t
))
1177 tree x
= DECL_EXPR_DECL (t
);
1178 if (DECL_INITIAL (x
))
1180 location_t loc
= DECL_SOURCE_LOCATION (x
);
1182 tree rhs
= DECL_INITIAL (x
);
1183 DECL_INITIAL (x
) = NULL
;
1184 tree new_modify_expr
= build_modify_expr (loc
, lhs
,
1189 t
= expand_array_notation_exprs (new_modify_expr
);
1193 case STATEMENT_LIST
:
1195 tree_stmt_iterator i
;
1196 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
1198 expand_array_notation_exprs (*tsi_stmt_ptr (i
));
1224 if (contains_array_notation_expr (t
))
1225 t
= expand_return_expr (t
);
1227 case PREDECREMENT_EXPR
:
1228 case PREINCREMENT_EXPR
:
1229 case POSTDECREMENT_EXPR
:
1230 case POSTINCREMENT_EXPR
:
1231 case AGGR_INIT_EXPR
:
1233 t
= expand_unary_array_notation_exprs (t
);
1236 case CLEANUP_POINT_EXPR
:
1238 TREE_OPERAND (t
, 0) = expand_array_notation_exprs (TREE_OPERAND (t
, 0));
1239 /* It is not necessary to wrap error_mark_node in EXPR_STMT. */
1240 if (TREE_OPERAND (t
, 0) == error_mark_node
)
1241 return TREE_OPERAND (t
, 0);
1243 case TRUTH_ANDIF_EXPR
:
1244 case TRUTH_ORIF_EXPR
:
1245 case TRUTH_AND_EXPR
:
1247 case TRUTH_XOR_EXPR
:
1248 case TRUTH_NOT_EXPR
:
1250 t
= cp_expand_cond_array_notations (t
);
1251 if (TREE_CODE (t
) == COND_EXPR
)
1253 COND_EXPR_THEN (t
) =
1254 expand_array_notation_exprs (COND_EXPR_THEN (t
));
1255 COND_EXPR_ELSE (t
) =
1256 expand_array_notation_exprs (COND_EXPR_ELSE (t
));
1260 if (contains_array_notation_expr (FOR_COND (t
)))
1262 error_at (EXPR_LOCATION (FOR_COND (t
)),
1263 "array notation cannot be used in a condition for "
1265 return error_mark_node
;
1267 /* FIXME: Add a check for CILK_FOR_STMT here when we add Cilk tasking
1269 if (TREE_CODE (t
) == FOR_STMT
)
1271 FOR_BODY (t
) = expand_array_notation_exprs (FOR_BODY (t
));
1272 FOR_EXPR (t
) = expand_array_notation_exprs (FOR_EXPR (t
));
1275 t
= expand_array_notation_exprs (t
);
1278 t
= cp_expand_cond_array_notations (t
);
1279 /* If the above function added some extra instructions above the original
1280 if statement, then we can't assume it is still IF_STMT so we have to
1282 if (TREE_CODE (t
) == IF_STMT
)
1284 if (THEN_CLAUSE (t
))
1285 THEN_CLAUSE (t
) = expand_array_notation_exprs (THEN_CLAUSE (t
));
1286 if (ELSE_CLAUSE (t
))
1287 ELSE_CLAUSE (t
) = expand_array_notation_exprs (ELSE_CLAUSE (t
));
1290 t
= expand_array_notation_exprs (t
);
1293 if (contains_array_notation_expr (SWITCH_STMT_COND (t
)))
1295 error_at (EXPR_LOCATION (SWITCH_STMT_COND (t
)),
1296 "array notation cannot be used as a condition for "
1297 "switch statement");
1298 return error_mark_node
;
1300 if (SWITCH_STMT_BODY (t
))
1301 SWITCH_STMT_BODY (t
) =
1302 expand_array_notation_exprs (SWITCH_STMT_BODY (t
));
1305 if (contains_array_notation_expr (WHILE_COND (t
)))
1307 if (EXPR_LOCATION (WHILE_COND (t
)) != UNKNOWN_LOCATION
)
1308 loc
= EXPR_LOCATION (WHILE_COND (t
));
1309 error_at (loc
, "array notation cannot be used as a condition for "
1311 return error_mark_node
;
1314 WHILE_BODY (t
) = expand_array_notation_exprs (WHILE_BODY (t
));
1317 if (contains_array_notation_expr (DO_COND (t
)))
1319 error_at (EXPR_LOCATION (DO_COND (t
)),
1320 "array notation cannot be used as a condition for a "
1321 "do-while statement");
1322 return error_mark_node
;
1325 DO_BODY (t
) = expand_array_notation_exprs (DO_BODY (t
));
1332 /* Walk over all the sub-trees of this operand. */
1333 len
= TREE_CODE_LENGTH (code
);
1335 /* Go through the subtrees. We need to do this in forward order so
1336 that the scope of a FOR_EXPR is handled properly. */
1337 for (i
= 0; i
< len
; ++i
)
1338 TREE_OPERAND (t
, i
) =
1339 expand_array_notation_exprs (TREE_OPERAND (t
, i
));
1346 /* Given the base of an array (ARRAY), the START (start_index), the number of
1347 elements to be accessed (LENGTH) and the STRIDE, construct an
1348 ARRAY_NOTATION_REF tree of type TYPE and return it. Restrictions on START,
1349 LENGTH and STRIDE are the same as that of index field passed into ARRAY_REF.
1350 The only additional restriction is that, unlike index in ARRAY_REF, stride,
1351 length and start_index cannot contain array notations. */
1354 build_array_notation_ref (location_t loc
, tree array
, tree start
, tree length
,
1355 tree stride
, tree type
)
1357 tree array_ntn_expr
= NULL_TREE
;
1359 /* If we enter the then-case of the if-statement below, we have hit a case
1360 like this: ARRAY [:]. */
1361 if (!start
&& !length
)
1363 if (TREE_CODE (type
) != ARRAY_TYPE
)
1365 error_at (loc
, "start-index and length fields necessary for "
1366 "using array notation in pointers or records");
1367 return error_mark_node
;
1369 tree domain
= TYPE_DOMAIN (type
);
1372 error_at (loc
, "start-index and length fields necessary for "
1373 "using array notation with array of unknown bound");
1374 return error_mark_node
;
1376 start
= cp_fold_convert (ptrdiff_type_node
, TYPE_MINVAL (domain
));
1377 length
= size_binop (PLUS_EXPR
, TYPE_MAXVAL (domain
), size_one_node
);
1378 length
= cp_fold_convert (ptrdiff_type_node
, length
);
1382 stride
= build_one_cst (ptrdiff_type_node
);
1384 stride
= maybe_constant_value (stride
);
1385 length
= maybe_constant_value (length
);
1387 start
= maybe_constant_value (start
);
1389 /* When dealing with templates, triplet type-checking will be done in pt.c
1390 after type substitution. */
1391 if (processing_template_decl
1392 && (type_dependent_expression_p (array
)
1393 || type_dependent_expression_p (length
)
1394 || type_dependent_expression_p (start
)
1395 || type_dependent_expression_p (stride
)))
1396 array_ntn_expr
= build_min_nt_loc (loc
, ARRAY_NOTATION_REF
, array
, start
,
1397 length
, stride
, NULL_TREE
);
1400 if (!cilkplus_an_triplet_types_ok_p (loc
, start
, length
, stride
, type
))
1401 return error_mark_node
;
1402 array_ntn_expr
= build4 (ARRAY_NOTATION_REF
, NULL_TREE
, array
, start
,
1405 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == POINTER_TYPE
)
1406 TREE_TYPE (array_ntn_expr
) = TREE_TYPE (type
);
1409 error_at (loc
, "base of array section must be pointer or array type");
1410 return error_mark_node
;
1413 SET_EXPR_LOCATION (array_ntn_expr
, loc
);
1414 return array_ntn_expr
;
1417 /* Returns false if any of the Array notation triplet values: START_INDEX,
1418 LENGTH and STRIDE, are not of integral type and have a rank greater than
1422 cilkplus_an_triplet_types_ok_p (location_t loc
, tree start_index
, tree length
,
1423 tree stride
, tree type
)
1425 size_t stride_rank
= 0, length_rank
= 0, start_rank
= 0;
1426 if (!TREE_TYPE (start_index
) || !INTEGRAL_TYPE_P (TREE_TYPE (start_index
)))
1428 error_at (loc
, "start-index of array notation triplet is not an integer");
1431 if (!TREE_TYPE (length
) || !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
1433 error_at (loc
, "length of array notation triplet is not an integer");
1436 if (!TREE_TYPE (stride
) || !INTEGRAL_TYPE_P (TREE_TYPE (stride
)))
1438 error_at (loc
, "stride of array notation triplet is not an integer");
1441 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1443 error_at (loc
, "array notation cannot be used with function type");
1446 if (!find_rank (loc
, start_index
, start_index
, false, &start_rank
)
1447 || !find_rank (loc
, length
, length
, false, &length_rank
)
1448 || !find_rank (loc
, stride
, stride
, false, &stride_rank
))
1451 if (start_rank
!= 0)
1453 error_at (loc
, "rank of an array notation triplet%'s start-index is not "
1457 if (length_rank
!= 0)
1459 error_at (loc
, "rank of an array notation triplet%'s length is not zero");
1462 if (stride_rank
!= 0)
1464 error_at (loc
, "rank of array notation triplet%'s stride is not zero");