* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / c-family / c-cilkplus.c
blob131da0b3fea0f153ced2b8de429904877c13a886
1 /* This file contains routines to construct and validate Cilk Plus
2 constructs within the C and C++ front ends.
4 Copyright (C) 2013-2014 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)
12 any later version.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "c-common.h"
29 /* Validate the body of a _Cilk_for construct or a <#pragma simd> for
30 loop.
32 Returns true if there were no errors, false otherwise. */
34 bool
35 c_check_cilk_loop (location_t loc, tree decl)
37 if (TREE_THIS_VOLATILE (decl))
39 error_at (loc, "iteration variable cannot be volatile");
40 return false;
42 return true;
45 /* Validate and emit code for <#pragma simd> clauses. */
47 tree
48 c_finish_cilk_clauses (tree clauses)
50 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
52 tree prev = clauses;
54 /* If a variable appears in a linear clause it cannot appear in
55 any other OMP clause. */
56 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
57 for (tree c2 = clauses; c2; c2 = OMP_CLAUSE_CHAIN (c2))
59 if (c == c2)
60 continue;
61 enum omp_clause_code code = OMP_CLAUSE_CODE (c2);
63 switch (code)
65 case OMP_CLAUSE_LINEAR:
66 case OMP_CLAUSE_PRIVATE:
67 case OMP_CLAUSE_FIRSTPRIVATE:
68 case OMP_CLAUSE_LASTPRIVATE:
69 case OMP_CLAUSE_REDUCTION:
70 break;
72 case OMP_CLAUSE_SAFELEN:
73 goto next;
75 default:
76 gcc_unreachable ();
79 if (OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (c2))
81 error_at (OMP_CLAUSE_LOCATION (c2),
82 "variable appears in more than one clause");
83 inform (OMP_CLAUSE_LOCATION (c),
84 "other clause defined here");
85 // Remove problematic clauses.
86 OMP_CLAUSE_CHAIN (prev) = OMP_CLAUSE_CHAIN (c2);
88 next:
89 prev = c2;
92 return clauses;
95 /* Calculate number of iterations of CILK_FOR. */
97 tree
98 cilk_for_number_of_iterations (tree cilk_for)
100 tree t, v, n1, n2, step, type, init, cond, incr, itype;
101 enum tree_code cond_code;
102 location_t loc = EXPR_LOCATION (cilk_for);
104 init = TREE_VEC_ELT (OMP_FOR_INIT (cilk_for), 0);
105 v = TREE_OPERAND (init, 0);
106 cond = TREE_VEC_ELT (OMP_FOR_COND (cilk_for), 0);
107 incr = TREE_VEC_ELT (OMP_FOR_INCR (cilk_for), 0);
108 type = TREE_TYPE (v);
110 gcc_assert (TREE_CODE (TREE_TYPE (v)) == INTEGER_TYPE
111 || TREE_CODE (TREE_TYPE (v)) == POINTER_TYPE);
112 n1 = TREE_OPERAND (init, 1);
113 cond_code = TREE_CODE (cond);
114 n2 = TREE_OPERAND (cond, 1);
115 switch (cond_code)
117 case LT_EXPR:
118 case GT_EXPR:
119 case NE_EXPR:
120 break;
121 case LE_EXPR:
122 if (POINTER_TYPE_P (TREE_TYPE (n2)))
123 n2 = fold_build_pointer_plus_hwi_loc (loc, n2, 1);
124 else
125 n2 = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (n2), n2,
126 build_int_cst (TREE_TYPE (n2), 1));
127 cond_code = LT_EXPR;
128 break;
129 case GE_EXPR:
130 if (POINTER_TYPE_P (TREE_TYPE (n2)))
131 n2 = fold_build_pointer_plus_hwi_loc (loc, n2, -1);
132 else
133 n2 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (n2), n2,
134 build_int_cst (TREE_TYPE (n2), 1));
135 cond_code = GT_EXPR;
136 break;
137 default:
138 gcc_unreachable ();
141 step = NULL_TREE;
142 switch (TREE_CODE (incr))
144 case PREINCREMENT_EXPR:
145 case POSTINCREMENT_EXPR:
146 step = build_int_cst (TREE_TYPE (v), 1);
147 break;
148 case PREDECREMENT_EXPR:
149 case POSTDECREMENT_EXPR:
150 step = build_int_cst (TREE_TYPE (v), -1);
151 break;
152 case MODIFY_EXPR:
153 t = TREE_OPERAND (incr, 1);
154 gcc_assert (TREE_OPERAND (t, 0) == v);
155 switch (TREE_CODE (t))
157 case PLUS_EXPR:
158 step = TREE_OPERAND (t, 1);
159 break;
160 case POINTER_PLUS_EXPR:
161 step = fold_convert (ssizetype, TREE_OPERAND (t, 1));
162 break;
163 case MINUS_EXPR:
164 step = TREE_OPERAND (t, 1);
165 step = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (step), step);
166 break;
167 default:
168 gcc_unreachable ();
170 break;
171 default:
172 gcc_unreachable ();
175 itype = type;
176 if (POINTER_TYPE_P (itype))
177 itype = signed_type_for (itype);
178 if (cond_code == NE_EXPR)
180 /* For NE_EXPR, we need to find out if the iterator increases
181 or decreases from whether step is positive or negative. */
182 tree stype = itype;
183 if (TYPE_UNSIGNED (stype))
184 stype = signed_type_for (stype);
185 cond = fold_build2_loc (loc, GE_EXPR, boolean_type_node,
186 fold_convert_loc (loc, stype, step),
187 build_int_cst (stype, 0));
188 t = fold_build3_loc (loc, COND_EXPR, itype, cond,
189 build_int_cst (itype, -1),
190 build_int_cst (itype, 1));
192 else
193 t = build_int_cst (itype, (cond_code == LT_EXPR ? -1 : 1));
194 t = fold_build2_loc (loc, PLUS_EXPR, itype,
195 fold_convert_loc (loc, itype, step), t);
196 t = fold_build2_loc (loc, PLUS_EXPR, itype, t,
197 fold_convert_loc (loc, itype, n2));
198 t = fold_build2_loc (loc, MINUS_EXPR, itype, t,
199 fold_convert_loc (loc, itype, n1));
200 if (TYPE_UNSIGNED (itype) && cond_code == GT_EXPR)
201 t = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype,
202 fold_build1_loc (loc, NEGATE_EXPR, itype, t),
203 fold_build1_loc (loc, NEGATE_EXPR, itype,
204 fold_convert_loc (loc, itype,
205 step)));
206 else if (TYPE_UNSIGNED (itype) && cond_code == NE_EXPR)
208 tree t1
209 = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
210 fold_convert_loc (loc, itype, step));
211 tree t2
212 = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype,
213 fold_build1_loc (loc, NEGATE_EXPR, itype, t),
214 fold_build1_loc (loc, NEGATE_EXPR, itype,
215 fold_convert_loc (loc, itype,
216 step)));
217 t = fold_build3_loc (loc, COND_EXPR, itype, cond, t1, t2);
219 else
220 t = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
221 fold_convert_loc (loc, itype, step));
222 cond = fold_build2_loc (loc, cond_code, boolean_type_node, n1, n2);
223 t = fold_build3_loc (loc, COND_EXPR, itype, cond, t,
224 build_int_cst (itype, 0));
225 return t;