1 /* This file contains routines to construct and validate Cilk Plus
2 constructs within the C and C++ front ends.
4 Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 Contributed by Aldy Hernandez <aldyh@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 /* Validate the body of a _Cilk_for construct or a <#pragma simd> for
31 Returns true if there were no errors, false otherwise. */
34 c_check_cilk_loop (location_t loc
, tree decl
)
36 if (TREE_THIS_VOLATILE (decl
))
38 error_at (loc
, "iteration variable cannot be volatile");
44 /* Calculate number of iterations of CILK_FOR. */
47 cilk_for_number_of_iterations (tree cilk_for
)
49 tree t
, v
, n1
, n2
, step
, type
, init
, cond
, incr
, itype
;
50 enum tree_code cond_code
;
51 location_t loc
= EXPR_LOCATION (cilk_for
);
53 init
= TREE_VEC_ELT (OMP_FOR_INIT (cilk_for
), 0);
54 v
= TREE_OPERAND (init
, 0);
55 cond
= TREE_VEC_ELT (OMP_FOR_COND (cilk_for
), 0);
56 incr
= TREE_VEC_ELT (OMP_FOR_INCR (cilk_for
), 0);
59 gcc_assert (TREE_CODE (TREE_TYPE (v
)) == INTEGER_TYPE
60 || TREE_CODE (TREE_TYPE (v
)) == POINTER_TYPE
);
61 n1
= TREE_OPERAND (init
, 1);
62 cond_code
= TREE_CODE (cond
);
63 n2
= TREE_OPERAND (cond
, 1);
71 if (POINTER_TYPE_P (TREE_TYPE (n2
)))
72 n2
= fold_build_pointer_plus_hwi_loc (loc
, n2
, 1);
74 n2
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (n2
), n2
,
75 build_int_cst (TREE_TYPE (n2
), 1));
79 if (POINTER_TYPE_P (TREE_TYPE (n2
)))
80 n2
= fold_build_pointer_plus_hwi_loc (loc
, n2
, -1);
82 n2
= fold_build2_loc (loc
, MINUS_EXPR
, TREE_TYPE (n2
), n2
,
83 build_int_cst (TREE_TYPE (n2
), 1));
91 switch (TREE_CODE (incr
))
93 case PREINCREMENT_EXPR
:
94 case POSTINCREMENT_EXPR
:
95 step
= build_int_cst (TREE_TYPE (v
), 1);
97 case PREDECREMENT_EXPR
:
98 case POSTDECREMENT_EXPR
:
99 step
= build_int_cst (TREE_TYPE (v
), -1);
102 t
= TREE_OPERAND (incr
, 1);
103 gcc_assert (TREE_OPERAND (t
, 0) == v
);
104 switch (TREE_CODE (t
))
107 step
= TREE_OPERAND (t
, 1);
109 case POINTER_PLUS_EXPR
:
110 step
= fold_convert (ssizetype
, TREE_OPERAND (t
, 1));
113 step
= TREE_OPERAND (t
, 1);
114 step
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (step
), step
);
125 if (POINTER_TYPE_P (itype
))
126 itype
= signed_type_for (itype
);
127 if (cond_code
== NE_EXPR
)
129 /* For NE_EXPR, we need to find out if the iterator increases
130 or decreases from whether step is positive or negative. */
132 if (TYPE_UNSIGNED (stype
))
133 stype
= signed_type_for (stype
);
134 cond
= fold_build2_loc (loc
, GE_EXPR
, boolean_type_node
,
135 fold_convert_loc (loc
, stype
, step
),
136 build_int_cst (stype
, 0));
137 t
= fold_build3_loc (loc
, COND_EXPR
, itype
, cond
,
138 build_int_cst (itype
, -1),
139 build_int_cst (itype
, 1));
142 t
= build_int_cst (itype
, (cond_code
== LT_EXPR
? -1 : 1));
143 t
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
144 fold_convert_loc (loc
, itype
, step
), t
);
145 t
= fold_build2_loc (loc
, PLUS_EXPR
, itype
, t
,
146 fold_convert_loc (loc
, itype
, n2
));
147 t
= fold_build2_loc (loc
, MINUS_EXPR
, itype
, t
,
148 fold_convert_loc (loc
, itype
, n1
));
149 if (TYPE_UNSIGNED (itype
) && cond_code
== GT_EXPR
)
150 t
= fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
,
151 fold_build1_loc (loc
, NEGATE_EXPR
, itype
, t
),
152 fold_build1_loc (loc
, NEGATE_EXPR
, itype
,
153 fold_convert_loc (loc
, itype
,
155 else if (TYPE_UNSIGNED (itype
) && cond_code
== NE_EXPR
)
158 = fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
, t
,
159 fold_convert_loc (loc
, itype
, step
));
161 = fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
,
162 fold_build1_loc (loc
, NEGATE_EXPR
, itype
, t
),
163 fold_build1_loc (loc
, NEGATE_EXPR
, itype
,
164 fold_convert_loc (loc
, itype
,
166 t
= fold_build3_loc (loc
, COND_EXPR
, itype
, cond
, t1
, t2
);
169 t
= fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
, t
,
170 fold_convert_loc (loc
, itype
, step
));
171 cond
= fold_build2_loc (loc
, cond_code
, boolean_type_node
, n1
, n2
);
172 t
= fold_build3_loc (loc
, COND_EXPR
, itype
, cond
, t
,
173 build_int_cst (itype
, 0));