2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / c-family / c-cilkplus.c
blobc61cf47b824af285b0b5cb1986db8390d6b6597b
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 "alias.h"
27 #include "symtab.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "c-common.h"
32 /* Validate the body of a _Cilk_for construct or a <#pragma simd> for
33 loop.
35 Returns true if there were no errors, false otherwise. */
37 bool
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");
43 return false;
45 return true;
48 /* Validate and emit code for <#pragma simd> clauses. */
50 tree
51 c_finish_cilk_clauses (tree clauses)
53 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
55 tree prev = clauses;
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))
62 if (c == c2)
63 continue;
64 enum omp_clause_code code = OMP_CLAUSE_CODE (c2);
66 switch (code)
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:
73 break;
75 case OMP_CLAUSE_SAFELEN:
76 goto next;
78 default:
79 gcc_unreachable ();
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);
91 next:
92 prev = c2;
95 return clauses;
98 /* Calculate number of iterations of CILK_FOR. */
100 tree
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);
118 switch (cond_code)
120 case LT_EXPR:
121 case GT_EXPR:
122 case NE_EXPR:
123 break;
124 case LE_EXPR:
125 if (POINTER_TYPE_P (TREE_TYPE (n2)))
126 n2 = fold_build_pointer_plus_hwi_loc (loc, n2, 1);
127 else
128 n2 = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (n2), n2,
129 build_int_cst (TREE_TYPE (n2), 1));
130 cond_code = LT_EXPR;
131 break;
132 case GE_EXPR:
133 if (POINTER_TYPE_P (TREE_TYPE (n2)))
134 n2 = fold_build_pointer_plus_hwi_loc (loc, n2, -1);
135 else
136 n2 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (n2), n2,
137 build_int_cst (TREE_TYPE (n2), 1));
138 cond_code = GT_EXPR;
139 break;
140 default:
141 gcc_unreachable ();
144 step = NULL_TREE;
145 switch (TREE_CODE (incr))
147 case PREINCREMENT_EXPR:
148 case POSTINCREMENT_EXPR:
149 step = build_int_cst (TREE_TYPE (v), 1);
150 break;
151 case PREDECREMENT_EXPR:
152 case POSTDECREMENT_EXPR:
153 step = build_int_cst (TREE_TYPE (v), -1);
154 break;
155 case MODIFY_EXPR:
156 t = TREE_OPERAND (incr, 1);
157 gcc_assert (TREE_OPERAND (t, 0) == v);
158 switch (TREE_CODE (t))
160 case PLUS_EXPR:
161 step = TREE_OPERAND (t, 1);
162 break;
163 case POINTER_PLUS_EXPR:
164 step = fold_convert (ssizetype, TREE_OPERAND (t, 1));
165 break;
166 case MINUS_EXPR:
167 step = TREE_OPERAND (t, 1);
168 step = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (step), step);
169 break;
170 default:
171 gcc_unreachable ();
173 break;
174 default:
175 gcc_unreachable ();
178 itype = type;
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. */
185 tree stype = itype;
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));
195 else
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,
208 step)));
209 else if (TYPE_UNSIGNED (itype) && cond_code == NE_EXPR)
211 tree t1
212 = fold_build2_loc (loc, TRUNC_DIV_EXPR, itype, t,
213 fold_convert_loc (loc, itype, step));
214 tree t2
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,
219 step)));
220 t = fold_build3_loc (loc, COND_EXPR, itype, cond, t1, t2);
222 else
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));
228 return t;