1 /* This file contains routines to construct and validate Cilk Plus
2 constructs within the C and C++ front ends.
4 Copyright (C) 2013-2015 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"
32 /* Validate the body of a _Cilk_for construct or a <#pragma simd> for
35 Returns true if there were no errors, false otherwise. */
38 c_check_cilk_loop (location_t loc
, tree decl
)
40 if (TREE_THIS_VOLATILE (decl
))
42 error_at (loc
, "iteration variable cannot be volatile");
48 /* Validate and emit code for <#pragma simd> clauses. */
51 c_finish_cilk_clauses (tree clauses
)
53 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
57 /* If a variable appears in a linear clause it cannot appear in
58 any other OMP clause. */
59 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
60 for (tree c2
= clauses
; c2
; c2
= OMP_CLAUSE_CHAIN (c2
))
64 enum omp_clause_code code
= OMP_CLAUSE_CODE (c2
);
68 case OMP_CLAUSE_LINEAR
:
69 case OMP_CLAUSE_PRIVATE
:
70 case OMP_CLAUSE_FIRSTPRIVATE
:
71 case OMP_CLAUSE_LASTPRIVATE
:
72 case OMP_CLAUSE_REDUCTION
:
75 case OMP_CLAUSE_SAFELEN
:
82 if (OMP_CLAUSE_DECL (c
) == OMP_CLAUSE_DECL (c2
))
84 error_at (OMP_CLAUSE_LOCATION (c2
),
85 "variable appears in more than one clause");
86 inform (OMP_CLAUSE_LOCATION (c
),
87 "other clause defined here");
88 // Remove problematic clauses.
89 OMP_CLAUSE_CHAIN (prev
) = OMP_CLAUSE_CHAIN (c2
);
98 /* Calculate number of iterations of CILK_FOR. */
101 cilk_for_number_of_iterations (tree cilk_for
)
103 tree t
, v
, n1
, n2
, step
, type
, init
, cond
, incr
, itype
;
104 enum tree_code cond_code
;
105 location_t loc
= EXPR_LOCATION (cilk_for
);
107 init
= TREE_VEC_ELT (OMP_FOR_INIT (cilk_for
), 0);
108 v
= TREE_OPERAND (init
, 0);
109 cond
= TREE_VEC_ELT (OMP_FOR_COND (cilk_for
), 0);
110 incr
= TREE_VEC_ELT (OMP_FOR_INCR (cilk_for
), 0);
111 type
= TREE_TYPE (v
);
113 gcc_assert (TREE_CODE (TREE_TYPE (v
)) == INTEGER_TYPE
114 || TREE_CODE (TREE_TYPE (v
)) == POINTER_TYPE
);
115 n1
= TREE_OPERAND (init
, 1);
116 cond_code
= TREE_CODE (cond
);
117 n2
= TREE_OPERAND (cond
, 1);
125 if (POINTER_TYPE_P (TREE_TYPE (n2
)))
126 n2
= fold_build_pointer_plus_hwi_loc (loc
, n2
, 1);
128 n2
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (n2
), n2
,
129 build_int_cst (TREE_TYPE (n2
), 1));
133 if (POINTER_TYPE_P (TREE_TYPE (n2
)))
134 n2
= fold_build_pointer_plus_hwi_loc (loc
, n2
, -1);
136 n2
= fold_build2_loc (loc
, MINUS_EXPR
, TREE_TYPE (n2
), n2
,
137 build_int_cst (TREE_TYPE (n2
), 1));
145 switch (TREE_CODE (incr
))
147 case PREINCREMENT_EXPR
:
148 case POSTINCREMENT_EXPR
:
149 step
= build_int_cst (TREE_TYPE (v
), 1);
151 case PREDECREMENT_EXPR
:
152 case POSTDECREMENT_EXPR
:
153 step
= build_int_cst (TREE_TYPE (v
), -1);
156 t
= TREE_OPERAND (incr
, 1);
157 gcc_assert (TREE_OPERAND (t
, 0) == v
);
158 switch (TREE_CODE (t
))
161 step
= TREE_OPERAND (t
, 1);
163 case POINTER_PLUS_EXPR
:
164 step
= fold_convert (ssizetype
, TREE_OPERAND (t
, 1));
167 step
= TREE_OPERAND (t
, 1);
168 step
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (step
), step
);
179 if (POINTER_TYPE_P (itype
))
180 itype
= signed_type_for (itype
);
181 if (cond_code
== NE_EXPR
)
183 /* For NE_EXPR, we need to find out if the iterator increases
184 or decreases from whether step is positive or negative. */
186 if (TYPE_UNSIGNED (stype
))
187 stype
= signed_type_for (stype
);
188 cond
= fold_build2_loc (loc
, GE_EXPR
, boolean_type_node
,
189 fold_convert_loc (loc
, stype
, step
),
190 build_int_cst (stype
, 0));
191 t
= fold_build3_loc (loc
, COND_EXPR
, itype
, cond
,
192 build_int_cst (itype
, -1),
193 build_int_cst (itype
, 1));
196 t
= build_int_cst (itype
, (cond_code
== LT_EXPR
? -1 : 1));
197 t
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
198 fold_convert_loc (loc
, itype
, step
), t
);
199 t
= fold_build2_loc (loc
, PLUS_EXPR
, itype
, t
,
200 fold_convert_loc (loc
, itype
, n2
));
201 t
= fold_build2_loc (loc
, MINUS_EXPR
, itype
, t
,
202 fold_convert_loc (loc
, itype
, n1
));
203 if (TYPE_UNSIGNED (itype
) && cond_code
== GT_EXPR
)
204 t
= fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
,
205 fold_build1_loc (loc
, NEGATE_EXPR
, itype
, t
),
206 fold_build1_loc (loc
, NEGATE_EXPR
, itype
,
207 fold_convert_loc (loc
, itype
,
209 else if (TYPE_UNSIGNED (itype
) && cond_code
== NE_EXPR
)
212 = fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
, t
,
213 fold_convert_loc (loc
, itype
, step
));
215 = fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
,
216 fold_build1_loc (loc
, NEGATE_EXPR
, itype
, t
),
217 fold_build1_loc (loc
, NEGATE_EXPR
, itype
,
218 fold_convert_loc (loc
, itype
,
220 t
= fold_build3_loc (loc
, COND_EXPR
, itype
, cond
, t1
, t2
);
223 t
= fold_build2_loc (loc
, TRUNC_DIV_EXPR
, itype
, t
,
224 fold_convert_loc (loc
, itype
, step
));
225 cond
= fold_build2_loc (loc
, cond_code
, boolean_type_node
, n1
, n2
);
226 t
= fold_build3_loc (loc
, COND_EXPR
, itype
, cond
, t
,
227 build_int_cst (itype
, 0));