]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimplify-me.c
coretypes.h: Include machmode.h...
[thirdparty/gcc.git] / gcc / gimplify-me.c
1 /* Tree lowering to gimple for middle end use only.
2 This converts the GENERIC functions-as-trees tree representation into
3 the GIMPLE form.
4 Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "hash-set.h"
28 #include "vec.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stmt.h"
37 #include "stor-layout.h"
38 #include "predict.h"
39 #include "tm.h"
40 #include "hard-reg-set.h"
41 #include "input.h"
42 #include "function.h"
43 #include "basic-block.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "tree-eh.h"
47 #include "gimple-expr.h"
48 #include "is-a.h"
49 #include "gimple.h"
50 #include "gimple-iterator.h"
51 #include "gimplify.h"
52 #include "gimplify-me.h"
53 #include "gimple-ssa.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56
57
58 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
59 the predicate that will hold for the result. If VAR is not NULL, make the
60 base variable of the final destination be VAR if suitable. */
61
62 tree
63 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
64 gimple_predicate gimple_test_f, tree var)
65 {
66 enum gimplify_status ret;
67 location_t saved_location;
68
69 *stmts = NULL;
70
71 /* gimple_test_f might be more strict than is_gimple_val, make
72 sure we pass both. Just checking gimple_test_f doesn't work
73 because most gimple predicates do not work recursively. */
74 if (is_gimple_val (expr)
75 && (*gimple_test_f) (expr))
76 return expr;
77
78 push_gimplify_context (gimple_in_ssa_p (cfun), true);
79 saved_location = input_location;
80 input_location = UNKNOWN_LOCATION;
81
82 if (var)
83 {
84 if (gimple_in_ssa_p (cfun) && is_gimple_reg (var))
85 var = make_ssa_name (var);
86 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
87 }
88
89 if (TREE_CODE (expr) != MODIFY_EXPR
90 && TREE_TYPE (expr) == void_type_node)
91 {
92 gimplify_and_add (expr, stmts);
93 expr = NULL_TREE;
94 }
95 else
96 {
97 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
98 gcc_assert (ret != GS_ERROR);
99 }
100
101 input_location = saved_location;
102 pop_gimplify_context (NULL);
103
104 return expr;
105 }
106
107 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
108 force the result to be either ssa_name or an invariant, otherwise
109 just force it to be a rhs expression. If VAR is not NULL, make the
110 base variable of the final destination be VAR if suitable. */
111
112 tree
113 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
114 {
115 return force_gimple_operand_1 (expr, stmts,
116 simple ? is_gimple_val : is_gimple_reg_rhs,
117 var);
118 }
119
120 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
121 and VAR. If some statements are produced, emits them at GSI.
122 If BEFORE is true. the statements are appended before GSI, otherwise
123 they are appended after it. M specifies the way GSI moves after
124 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
125
126 tree
127 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
128 gimple_predicate gimple_test_f,
129 tree var, bool before,
130 enum gsi_iterator_update m)
131 {
132 gimple_seq stmts;
133
134 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
135
136 if (!gimple_seq_empty_p (stmts))
137 {
138 if (before)
139 gsi_insert_seq_before (gsi, stmts, m);
140 else
141 gsi_insert_seq_after (gsi, stmts, m);
142 }
143
144 return expr;
145 }
146
147 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
148 If SIMPLE is true, force the result to be either ssa_name or an invariant,
149 otherwise just force it to be a rhs expression. If some statements are
150 produced, emits them at GSI. If BEFORE is true, the statements are
151 appended before GSI, otherwise they are appended after it. M specifies
152 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
153 are the usual values). */
154
155 tree
156 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
157 bool simple_p, tree var, bool before,
158 enum gsi_iterator_update m)
159 {
160 return force_gimple_operand_gsi_1 (gsi, expr,
161 simple_p
162 ? is_gimple_val : is_gimple_reg_rhs,
163 var, before, m);
164 }
165
166 /* Some transformations like inlining may invalidate the GIMPLE form
167 for operands. This function traverses all the operands in STMT and
168 gimplifies anything that is not a valid gimple operand. Any new
169 GIMPLE statements are inserted before *GSI_P. */
170
171 void
172 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
173 {
174 size_t i, num_ops;
175 tree lhs;
176 gimple_seq pre = NULL;
177 gimple post_stmt = NULL;
178
179 push_gimplify_context (gimple_in_ssa_p (cfun));
180
181 switch (gimple_code (stmt))
182 {
183 case GIMPLE_COND:
184 {
185 gcond *cond_stmt = as_a <gcond *> (stmt);
186 gimplify_expr (gimple_cond_lhs_ptr (cond_stmt), &pre, NULL,
187 is_gimple_val, fb_rvalue);
188 gimplify_expr (gimple_cond_rhs_ptr (cond_stmt), &pre, NULL,
189 is_gimple_val, fb_rvalue);
190 }
191 break;
192 case GIMPLE_SWITCH:
193 gimplify_expr (gimple_switch_index_ptr (as_a <gswitch *> (stmt)),
194 &pre, NULL, is_gimple_val, fb_rvalue);
195 break;
196 case GIMPLE_OMP_ATOMIC_LOAD:
197 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (
198 as_a <gomp_atomic_load *> (stmt)),
199 &pre, NULL, is_gimple_val, fb_rvalue);
200 break;
201 case GIMPLE_ASM:
202 {
203 gasm *asm_stmt = as_a <gasm *> (stmt);
204 size_t i, noutputs = gimple_asm_noutputs (asm_stmt);
205 const char *constraint, **oconstraints;
206 bool allows_mem, allows_reg, is_inout;
207
208 oconstraints
209 = (const char **) alloca ((noutputs) * sizeof (const char *));
210 for (i = 0; i < noutputs; i++)
211 {
212 tree op = gimple_asm_output_op (asm_stmt, i);
213 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
214 oconstraints[i] = constraint;
215 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
216 &allows_reg, &is_inout);
217 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
218 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
219 fb_lvalue | fb_mayfail);
220 }
221 for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
222 {
223 tree op = gimple_asm_input_op (asm_stmt, i);
224 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
225 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
226 oconstraints, &allows_mem, &allows_reg);
227 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
228 allows_reg = 0;
229 if (!allows_reg && allows_mem)
230 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
231 is_gimple_lvalue, fb_lvalue | fb_mayfail);
232 else
233 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
234 is_gimple_asm_val, fb_rvalue);
235 }
236 }
237 break;
238 default:
239 /* NOTE: We start gimplifying operands from last to first to
240 make sure that side-effects on the RHS of calls, assignments
241 and ASMs are executed before the LHS. The ordering is not
242 important for other statements. */
243 num_ops = gimple_num_ops (stmt);
244 for (i = num_ops; i > 0; i--)
245 {
246 tree op = gimple_op (stmt, i - 1);
247 if (op == NULL_TREE)
248 continue;
249 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
250 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
251 else if (i == 2
252 && is_gimple_assign (stmt)
253 && num_ops == 2
254 && get_gimple_rhs_class (gimple_expr_code (stmt))
255 == GIMPLE_SINGLE_RHS)
256 gimplify_expr (&op, &pre, NULL,
257 rhs_predicate_for (gimple_assign_lhs (stmt)),
258 fb_rvalue);
259 else if (i == 2 && is_gimple_call (stmt))
260 {
261 if (TREE_CODE (op) == FUNCTION_DECL)
262 continue;
263 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
264 }
265 else
266 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
267 gimple_set_op (stmt, i - 1, op);
268 }
269
270 lhs = gimple_get_lhs (stmt);
271 /* If the LHS changed it in a way that requires a simple RHS,
272 create temporary. */
273 if (lhs && !is_gimple_reg (lhs))
274 {
275 bool need_temp = false;
276
277 if (is_gimple_assign (stmt)
278 && num_ops == 2
279 && get_gimple_rhs_class (gimple_expr_code (stmt))
280 == GIMPLE_SINGLE_RHS)
281 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
282 rhs_predicate_for (gimple_assign_lhs (stmt)),
283 fb_rvalue);
284 else if (is_gimple_reg (lhs))
285 {
286 if (is_gimple_reg_type (TREE_TYPE (lhs)))
287 {
288 if (is_gimple_call (stmt))
289 {
290 i = gimple_call_flags (stmt);
291 if ((i & ECF_LOOPING_CONST_OR_PURE)
292 || !(i & (ECF_CONST | ECF_PURE)))
293 need_temp = true;
294 }
295 if (stmt_can_throw_internal (stmt))
296 need_temp = true;
297 }
298 }
299 else
300 {
301 if (is_gimple_reg_type (TREE_TYPE (lhs)))
302 need_temp = true;
303 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
304 {
305 if (is_gimple_call (stmt))
306 {
307 tree fndecl = gimple_call_fndecl (stmt);
308
309 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
310 && !(fndecl && DECL_RESULT (fndecl)
311 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
312 need_temp = true;
313 }
314 else
315 need_temp = true;
316 }
317 }
318 if (need_temp)
319 {
320 tree temp = create_tmp_reg (TREE_TYPE (lhs));
321 if (gimple_in_ssa_p (cfun))
322 temp = make_ssa_name (temp);
323 gimple_set_lhs (stmt, temp);
324 post_stmt = gimple_build_assign (lhs, temp);
325 }
326 }
327 break;
328 }
329
330 if (!gimple_seq_empty_p (pre))
331 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
332 if (post_stmt)
333 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
334
335 pop_gimplify_context (NULL);
336
337 update_stmt (stmt);
338 }
339
340