Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / c-family / c-cilkplus.c
blobd615bd936aac1c3a8e16563fa8d51d100d9374c7
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)
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 "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "tree.h"
31 #include "c-common.h"
33 /* Validate the body of a _Cilk_for construct or a <#pragma simd> for
34 loop.
36 Returns true if there were no errors, false otherwise. */
38 bool
39 c_check_cilk_loop (location_t loc, tree decl)
41 if (TREE_THIS_VOLATILE (decl))
43 error_at (loc, "iteration variable cannot be volatile");
44 return false;
46 return true;
49 /* Validate and emit code for <#pragma simd> clauses. */
51 tree
52 c_finish_cilk_clauses (tree clauses)
54 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
56 tree prev = clauses;
58 /* If a variable appears in a linear clause it cannot appear in
59 any other OMP clause. */
60 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
61 for (tree c2 = clauses; c2; c2 = OMP_CLAUSE_CHAIN (c2))
63 if (c == c2)
64 continue;
65 enum omp_clause_code code = OMP_CLAUSE_CODE (c2);
67 switch (code)
69 case OMP_CLAUSE_LINEAR:
70 case OMP_CLAUSE_PRIVATE:
71 case OMP_CLAUSE_FIRSTPRIVATE:
72 case OMP_CLAUSE_LASTPRIVATE:
73 case OMP_CLAUSE_REDUCTION:
74 break;
76 case OMP_CLAUSE_SAFELEN:
77 goto next;
79 default:
80 gcc_unreachable ();
83 if (OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (c2))
85 error_at (OMP_CLAUSE_LOCATION (c2),
86 "variable appears in more than one clause");
87 inform (OMP_CLAUSE_LOCATION (c),
88 "other clause defined here");
89 // Remove problematic clauses.
90 OMP_CLAUSE_CHAIN (prev) = OMP_CLAUSE_CHAIN (c2);
92 next:
93 prev = c2;
96 return clauses;
99 /* Calculate number of iterations of CILK_FOR. */
101 tree
102 cilk_for_number_of_iterations (tree cilk_for)
104 tree t, v, n1, n2, step, type, init, cond, incr, itype;
105 enum tree_code cond_code;
106 location_t loc = EXPR_LOCATION (cilk_for);
108 init = TREE_VEC_ELT (OMP_FOR_INIT (cilk_for), 0);
109 v = TREE_OPERAND (init, 0);
110 cond = TREE_VEC_ELT (OMP_FOR_COND (cilk_for), 0);
111 incr = TREE_VEC_ELT (OMP_FOR_INCR (cilk_for), 0);
112 type = TREE_TYPE (v);
114 gcc_assert (TREE_CODE (TREE_TYPE (v)) == INTEGER_TYPE
115 || TREE_CODE (TREE_TYPE (v)) == POINTER_TYPE);
116 n1 = TREE_OPERAND (init, 1);
117 cond_code = TREE_CODE (cond);
118 n2 = TREE_OPERAND (cond, 1);
119 switch (cond_code)
121 case LT_EXPR:
122 case GT_EXPR:
123 case NE_EXPR:
124 break;
125 case LE_EXPR:
126 if (POINTER_TYPE_P (TREE_TYPE (n2)))
127 n2 = fold_build_pointer_plus_hwi_loc (loc, n2, 1);
128 else
129 n2 = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (n2), n2,
130 build_int_cst (TREE_TYPE (n2), 1));
131 cond_code = LT_EXPR;
132 break;
133 case GE_EXPR:
134 if (POINTER_TYPE_P (TREE_TYPE (n2)))
135 n2 = fold_build_pointer_plus_hwi_loc (loc, n2, -1);
136 else
137 n2 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (n2), n2,
138 build_int_cst (TREE_TYPE (n2), 1));
139 cond_code = GT_EXPR;
140 break;
141 default:
142 gcc_unreachable ();
145 step = NULL_TREE;
146 switch (TREE_CODE (incr))
148 case PREINCREMENT_EXPR:
149 case POSTINCREMENT_EXPR:
150 step = build_int_cst (TREE_TYPE (v), 1);
151 break;
152 case PREDECREMENT_EXPR:
153 case POSTDECREMENT_EXPR:
154 step = build_int_cst (TREE_TYPE (v), -1);
155 break;
156 case MODIFY_EXPR:
157 t = TREE_OPERAND (incr, 1);
158 gcc_assert (TREE_OPERAND (t, 0) == v);
159 switch (TREE_CODE (t))
161 case PLUS_EXPR:
162 step = TREE_OPERAND (t, 1);
163 break;
164 case POINTER_PLUS_EXPR:
165 step = fold_convert (ssizetype, TREE_OPERAND (t, 1));
166 break;
167 case MINUS_EXPR:
168 step = TREE_OPERAND (t, 1);
169 step = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (step), step);
170 break;
171 default:
172 gcc_unreachable ();
174 break;
175 default:
176 gcc_unreachable ();
179 itype = type;
180 if (POINTER_TYPE_P (itype))
181 itype = signed_type_for (itype);
182 if (cond_code == NE_EXPR)
184 /* For NE_EXPR, we need to find out if the iterator increases
185 or decreases from whether step is positive or negative. */
186 tree stype = itype;
187 if (TYPE_UNSIGNED (stype))
188 stype = signed_type_for (stype);
189 cond = fold_build2_loc (loc, GE_EXPR, boolean_type_node,
190 fold_convert_loc (loc, stype, step),
191 build_int_cst (stype, 0));
192 t = fold_build3_loc (loc, COND_EXPR, itype, cond,
193 build_int_cst (itype, -1),
194 build_int_cst (itype, 1));
196 else
197 t = build_int_cst (itype, (cond_code == LT_EXPR ? -1 : 1));
198 t = fold_build2_loc (loc, PLUS_EXPR, itype,
199 fold_convert_loc (loc, itype, step), t);
200 t = fold_build2_loc (loc, PLUS_EXPR, itype, t,
201 fold_convert_loc (loc, itype, n2));
202 t = fold_build2_loc (loc, MINUS_EXPR, itype, t,
203 fold_convert_loc (loc, itype, n1));
204 if (TYPE_UNSIGNED (itype) && cond_code == GT_EXPR)
205 t = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype,
206 fold_build1_loc (loc, NEGATE_EXPR, itype, t),
207 fold_build1_loc (loc, NEGATE_EXPR, itype,
208 fold_convert_loc (loc, itype,
209 step)));
210 else if (TYPE_UNSIGNED (itype) && cond_code == NE_EXPR)
212 tree t1
213 = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
214 fold_convert_loc (loc, itype, step));
215 tree t2
216 = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype,
217 fold_build1_loc (loc, NEGATE_EXPR, itype, t),
218 fold_build1_loc (loc, NEGATE_EXPR, itype,
219 fold_convert_loc (loc, itype,
220 step)));
221 t = fold_build3_loc (loc, COND_EXPR, itype, cond, t1, t2);
223 else
224 t = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
225 fold_convert_loc (loc, itype, step));
226 cond = fold_build2_loc (loc, cond_code, boolean_type_node, n1, n2);
227 t = fold_build3_loc (loc, COND_EXPR, itype, cond, t,
228 build_int_cst (itype, 0));
229 return t;