]>
Commit | Line | Data |
---|---|---|
18f429e2 AM |
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. | |
5624e564 | 4 | Copyright (C) 2013-2015 Free Software Foundation, Inc. |
18f429e2 AM |
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" | |
40e23961 MC |
27 | #include "alias.h" |
28 | #include "symtab.h" | |
29 | #include "options.h" | |
18f429e2 | 30 | #include "tree.h" |
40e23961 | 31 | #include "fold-const.h" |
d8a2d370 DN |
32 | #include "stmt.h" |
33 | #include "stor-layout.h" | |
60393bbc | 34 | #include "predict.h" |
60393bbc AM |
35 | #include "tm.h" |
36 | #include "hard-reg-set.h" | |
60393bbc | 37 | #include "function.h" |
2fb9a547 AM |
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" | |
18f429e2 AM |
43 | #include "gimple.h" |
44 | #include "gimple-iterator.h" | |
45 | #include "gimplify.h" | |
46 | #include "gimplify-me.h" | |
47 | #include "gimple-ssa.h" | |
d8a2d370 | 48 | #include "stringpool.h" |
18f429e2 AM |
49 | #include "tree-ssanames.h" |
50 | ||
51 | ||
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. */ | |
55 | ||
56 | tree | |
57 | force_gimple_operand_1 (tree expr, gimple_seq *stmts, | |
58 | gimple_predicate gimple_test_f, tree var) | |
59 | { | |
60 | enum gimplify_status ret; | |
18f429e2 AM |
61 | location_t saved_location; |
62 | ||
63 | *stmts = NULL; | |
64 | ||
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; | |
71 | ||
45852dcc | 72 | push_gimplify_context (gimple_in_ssa_p (cfun), true); |
18f429e2 AM |
73 | saved_location = input_location; |
74 | input_location = UNKNOWN_LOCATION; | |
75 | ||
76 | if (var) | |
77 | { | |
45852dcc | 78 | if (gimple_in_ssa_p (cfun) && is_gimple_reg (var)) |
b731b390 | 79 | var = make_ssa_name (var); |
18f429e2 AM |
80 | expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr); |
81 | } | |
82 | ||
83 | if (TREE_CODE (expr) != MODIFY_EXPR | |
84 | && TREE_TYPE (expr) == void_type_node) | |
85 | { | |
86 | gimplify_and_add (expr, stmts); | |
87 | expr = NULL_TREE; | |
88 | } | |
89 | else | |
90 | { | |
91 | ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue); | |
92 | gcc_assert (ret != GS_ERROR); | |
93 | } | |
94 | ||
95 | input_location = saved_location; | |
96 | pop_gimplify_context (NULL); | |
97 | ||
98 | return expr; | |
99 | } | |
100 | ||
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. */ | |
105 | ||
106 | tree | |
107 | force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var) | |
108 | { | |
109 | return force_gimple_operand_1 (expr, stmts, | |
110 | simple ? is_gimple_val : is_gimple_reg_rhs, | |
111 | var); | |
112 | } | |
113 | ||
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). */ | |
119 | ||
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) | |
125 | { | |
126 | gimple_seq stmts; | |
127 | ||
128 | expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var); | |
129 | ||
130 | if (!gimple_seq_empty_p (stmts)) | |
131 | { | |
132 | if (before) | |
133 | gsi_insert_seq_before (gsi, stmts, m); | |
134 | else | |
135 | gsi_insert_seq_after (gsi, stmts, m); | |
136 | } | |
137 | ||
138 | return expr; | |
139 | } | |
140 | ||
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). */ | |
148 | ||
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) | |
153 | { | |
154 | return force_gimple_operand_gsi_1 (gsi, expr, | |
155 | simple_p | |
156 | ? is_gimple_val : is_gimple_reg_rhs, | |
157 | var, before, m); | |
158 | } | |
159 | ||
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. */ | |
164 | ||
165 | void | |
166 | gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p) | |
167 | { | |
168 | size_t i, num_ops; | |
169 | tree lhs; | |
170 | gimple_seq pre = NULL; | |
171 | gimple post_stmt = NULL; | |
18f429e2 | 172 | |
45852dcc | 173 | push_gimplify_context (gimple_in_ssa_p (cfun)); |
18f429e2 AM |
174 | |
175 | switch (gimple_code (stmt)) | |
176 | { | |
177 | case GIMPLE_COND: | |
538dd0b7 DM |
178 | { |
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); | |
184 | } | |
18f429e2 AM |
185 | break; |
186 | case GIMPLE_SWITCH: | |
538dd0b7 DM |
187 | gimplify_expr (gimple_switch_index_ptr (as_a <gswitch *> (stmt)), |
188 | &pre, NULL, is_gimple_val, fb_rvalue); | |
18f429e2 AM |
189 | break; |
190 | case GIMPLE_OMP_ATOMIC_LOAD: | |
538dd0b7 DM |
191 | gimplify_expr (gimple_omp_atomic_load_rhs_ptr ( |
192 | as_a <gomp_atomic_load *> (stmt)), | |
193 | &pre, NULL, is_gimple_val, fb_rvalue); | |
18f429e2 AM |
194 | break; |
195 | case GIMPLE_ASM: | |
196 | { | |
538dd0b7 DM |
197 | gasm *asm_stmt = as_a <gasm *> (stmt); |
198 | size_t i, noutputs = gimple_asm_noutputs (asm_stmt); | |
18f429e2 AM |
199 | const char *constraint, **oconstraints; |
200 | bool allows_mem, allows_reg, is_inout; | |
201 | ||
202 | oconstraints | |
203 | = (const char **) alloca ((noutputs) * sizeof (const char *)); | |
204 | for (i = 0; i < noutputs; i++) | |
205 | { | |
538dd0b7 | 206 | tree op = gimple_asm_output_op (asm_stmt, i); |
18f429e2 AM |
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); | |
214 | } | |
538dd0b7 | 215 | for (i = 0; i < gimple_asm_ninputs (asm_stmt); i++) |
18f429e2 | 216 | { |
538dd0b7 | 217 | tree op = gimple_asm_input_op (asm_stmt, i); |
18f429e2 AM |
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); | |
229 | } | |
230 | } | |
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--) | |
239 | { | |
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)) | |
254 | { | |
255 | if (TREE_CODE (op) == FUNCTION_DECL) | |
256 | continue; | |
257 | gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue); | |
258 | } | |
259 | else | |
260 | gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue); | |
261 | gimple_set_op (stmt, i - 1, op); | |
262 | } | |
263 | ||
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)) | |
268 | { | |
269 | bool need_temp = false; | |
270 | ||
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)) | |
279 | { | |
280 | if (is_gimple_reg_type (TREE_TYPE (lhs))) | |
281 | { | |
282 | if (is_gimple_call (stmt)) | |
283 | { | |
284 | i = gimple_call_flags (stmt); | |
285 | if ((i & ECF_LOOPING_CONST_OR_PURE) | |
286 | || !(i & (ECF_CONST | ECF_PURE))) | |
287 | need_temp = true; | |
288 | } | |
289 | if (stmt_can_throw_internal (stmt)) | |
290 | need_temp = true; | |
291 | } | |
292 | } | |
293 | else | |
294 | { | |
295 | if (is_gimple_reg_type (TREE_TYPE (lhs))) | |
296 | need_temp = true; | |
297 | else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode) | |
298 | { | |
299 | if (is_gimple_call (stmt)) | |
300 | { | |
301 | tree fndecl = gimple_call_fndecl (stmt); | |
302 | ||
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; | |
307 | } | |
308 | else | |
309 | need_temp = true; | |
310 | } | |
311 | } | |
312 | if (need_temp) | |
313 | { | |
b731b390 | 314 | tree temp = create_tmp_reg (TREE_TYPE (lhs)); |
18f429e2 | 315 | if (gimple_in_ssa_p (cfun)) |
b731b390 | 316 | temp = make_ssa_name (temp); |
18f429e2 AM |
317 | gimple_set_lhs (stmt, temp); |
318 | post_stmt = gimple_build_assign (lhs, temp); | |
319 | } | |
320 | } | |
321 | break; | |
322 | } | |
323 | ||
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); | |
328 | ||
329 | pop_gimplify_context (NULL); | |
40b0722f RB |
330 | |
331 | update_stmt (stmt); | |
18f429e2 AM |
332 | } |
333 | ||
334 |