Daily bump.
[official-gcc.git] / gcc / gimplify-me.c
blobcaad2fc6954b754e9c8e7ebf1b5159b4cda5178d
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>.
8 This file is part of GCC.
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.
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.
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/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stmt.h"
33 #include "stor-layout.h"
34 #include "predict.h"
35 #include "tm.h"
36 #include "hard-reg-set.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "tree-eh.h"
42 #include "gimple-expr.h"
43 #include "gimple.h"
44 #include "gimple-iterator.h"
45 #include "gimplify.h"
46 #include "gimplify-me.h"
47 #include "gimple-ssa.h"
48 #include "stringpool.h"
49 #include "tree-ssanames.h"
52 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
53 the predicate that will hold for the result. If VAR is not NULL, make the
54 base variable of the final destination be VAR if suitable. */
56 tree
57 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
58 gimple_predicate gimple_test_f, tree var)
60 enum gimplify_status ret;
61 location_t saved_location;
63 *stmts = NULL;
65 /* gimple_test_f might be more strict than is_gimple_val, make
66 sure we pass both. Just checking gimple_test_f doesn't work
67 because most gimple predicates do not work recursively. */
68 if (is_gimple_val (expr)
69 && (*gimple_test_f) (expr))
70 return expr;
72 push_gimplify_context (gimple_in_ssa_p (cfun), true);
73 saved_location = input_location;
74 input_location = UNKNOWN_LOCATION;
76 if (var)
78 if (gimple_in_ssa_p (cfun) && is_gimple_reg (var))
79 var = make_ssa_name (var);
80 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
83 if (TREE_CODE (expr) != MODIFY_EXPR
84 && TREE_TYPE (expr) == void_type_node)
86 gimplify_and_add (expr, stmts);
87 expr = NULL_TREE;
89 else
91 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
92 gcc_assert (ret != GS_ERROR);
95 input_location = saved_location;
96 pop_gimplify_context (NULL);
98 return expr;
101 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
102 force the result to be either ssa_name or an invariant, otherwise
103 just force it to be a rhs expression. If VAR is not NULL, make the
104 base variable of the final destination be VAR if suitable. */
106 tree
107 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
109 return force_gimple_operand_1 (expr, stmts,
110 simple ? is_gimple_val : is_gimple_reg_rhs,
111 var);
114 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
115 and VAR. If some statements are produced, emits them at GSI.
116 If BEFORE is true. the statements are appended before GSI, otherwise
117 they are appended after it. M specifies the way GSI moves after
118 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
120 tree
121 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
122 gimple_predicate gimple_test_f,
123 tree var, bool before,
124 enum gsi_iterator_update m)
126 gimple_seq stmts;
128 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
130 if (!gimple_seq_empty_p (stmts))
132 if (before)
133 gsi_insert_seq_before (gsi, stmts, m);
134 else
135 gsi_insert_seq_after (gsi, stmts, m);
138 return expr;
141 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
142 If SIMPLE is true, force the result to be either ssa_name or an invariant,
143 otherwise just force it to be a rhs expression. If some statements are
144 produced, emits them at GSI. If BEFORE is true, the statements are
145 appended before GSI, otherwise they are appended after it. M specifies
146 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
147 are the usual values). */
149 tree
150 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
151 bool simple_p, tree var, bool before,
152 enum gsi_iterator_update m)
154 return force_gimple_operand_gsi_1 (gsi, expr,
155 simple_p
156 ? is_gimple_val : is_gimple_reg_rhs,
157 var, before, m);
160 /* Some transformations like inlining may invalidate the GIMPLE form
161 for operands. This function traverses all the operands in STMT and
162 gimplifies anything that is not a valid gimple operand. Any new
163 GIMPLE statements are inserted before *GSI_P. */
165 void
166 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
168 size_t i, num_ops;
169 tree lhs;
170 gimple_seq pre = NULL;
171 gimple post_stmt = NULL;
173 push_gimplify_context (gimple_in_ssa_p (cfun));
175 switch (gimple_code (stmt))
177 case GIMPLE_COND:
179 gcond *cond_stmt = as_a <gcond *> (stmt);
180 gimplify_expr (gimple_cond_lhs_ptr (cond_stmt), &pre, NULL,
181 is_gimple_val, fb_rvalue);
182 gimplify_expr (gimple_cond_rhs_ptr (cond_stmt), &pre, NULL,
183 is_gimple_val, fb_rvalue);
185 break;
186 case GIMPLE_SWITCH:
187 gimplify_expr (gimple_switch_index_ptr (as_a <gswitch *> (stmt)),
188 &pre, NULL, is_gimple_val, fb_rvalue);
189 break;
190 case GIMPLE_OMP_ATOMIC_LOAD:
191 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (
192 as_a <gomp_atomic_load *> (stmt)),
193 &pre, NULL, is_gimple_val, fb_rvalue);
194 break;
195 case GIMPLE_ASM:
197 gasm *asm_stmt = as_a <gasm *> (stmt);
198 size_t i, noutputs = gimple_asm_noutputs (asm_stmt);
199 const char *constraint, **oconstraints;
200 bool allows_mem, allows_reg, is_inout;
202 oconstraints
203 = (const char **) alloca ((noutputs) * sizeof (const char *));
204 for (i = 0; i < noutputs; i++)
206 tree op = gimple_asm_output_op (asm_stmt, i);
207 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
208 oconstraints[i] = constraint;
209 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
210 &allows_reg, &is_inout);
211 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
212 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
213 fb_lvalue | fb_mayfail);
215 for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
217 tree op = gimple_asm_input_op (asm_stmt, i);
218 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
219 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
220 oconstraints, &allows_mem, &allows_reg);
221 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
222 allows_reg = 0;
223 if (!allows_reg && allows_mem)
224 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
225 is_gimple_lvalue, fb_lvalue | fb_mayfail);
226 else
227 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
228 is_gimple_asm_val, fb_rvalue);
231 break;
232 default:
233 /* NOTE: We start gimplifying operands from last to first to
234 make sure that side-effects on the RHS of calls, assignments
235 and ASMs are executed before the LHS. The ordering is not
236 important for other statements. */
237 num_ops = gimple_num_ops (stmt);
238 for (i = num_ops; i > 0; i--)
240 tree op = gimple_op (stmt, i - 1);
241 if (op == NULL_TREE)
242 continue;
243 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
244 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
245 else if (i == 2
246 && is_gimple_assign (stmt)
247 && num_ops == 2
248 && get_gimple_rhs_class (gimple_expr_code (stmt))
249 == GIMPLE_SINGLE_RHS)
250 gimplify_expr (&op, &pre, NULL,
251 rhs_predicate_for (gimple_assign_lhs (stmt)),
252 fb_rvalue);
253 else if (i == 2 && is_gimple_call (stmt))
255 if (TREE_CODE (op) == FUNCTION_DECL)
256 continue;
257 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
259 else
260 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
261 gimple_set_op (stmt, i - 1, op);
264 lhs = gimple_get_lhs (stmt);
265 /* If the LHS changed it in a way that requires a simple RHS,
266 create temporary. */
267 if (lhs && !is_gimple_reg (lhs))
269 bool need_temp = false;
271 if (is_gimple_assign (stmt)
272 && num_ops == 2
273 && get_gimple_rhs_class (gimple_expr_code (stmt))
274 == GIMPLE_SINGLE_RHS)
275 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
276 rhs_predicate_for (gimple_assign_lhs (stmt)),
277 fb_rvalue);
278 else if (is_gimple_reg (lhs))
280 if (is_gimple_reg_type (TREE_TYPE (lhs)))
282 if (is_gimple_call (stmt))
284 i = gimple_call_flags (stmt);
285 if ((i & ECF_LOOPING_CONST_OR_PURE)
286 || !(i & (ECF_CONST | ECF_PURE)))
287 need_temp = true;
289 if (stmt_can_throw_internal (stmt))
290 need_temp = true;
293 else
295 if (is_gimple_reg_type (TREE_TYPE (lhs)))
296 need_temp = true;
297 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
299 if (is_gimple_call (stmt))
301 tree fndecl = gimple_call_fndecl (stmt);
303 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
304 && !(fndecl && DECL_RESULT (fndecl)
305 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
306 need_temp = true;
308 else
309 need_temp = true;
312 if (need_temp)
314 tree temp = create_tmp_reg (TREE_TYPE (lhs));
315 if (gimple_in_ssa_p (cfun))
316 temp = make_ssa_name (temp);
317 gimple_set_lhs (stmt, temp);
318 post_stmt = gimple_build_assign (lhs, temp);
321 break;
324 if (!gimple_seq_empty_p (pre))
325 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
326 if (post_stmt)
327 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
329 pop_gimplify_context (NULL);
331 update_stmt (stmt);