]>
Commit | Line | Data |
---|---|---|
2b725155 | 1 | /* Lower complex number operations to scalar operations. |
5624e564 | 2 | Copyright (C) 2004-2015 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | |
4 | This file is part of GCC. | |
b8698a0f | 5 | |
6de9cd9a DN |
6 | GCC is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU General Public License as published by the | |
9dcd6f09 | 8 | Free Software Foundation; either version 3, or (at your option) any |
6de9cd9a | 9 | later version. |
b8698a0f | 10 | |
6de9cd9a DN |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
b8698a0f | 15 | |
6de9cd9a | 16 | You should have received a copy of the GNU General Public License |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
c7131fb2 | 23 | #include "backend.h" |
e41d82f5 | 24 | #include "tree.h" |
c7131fb2 AM |
25 | #include "gimple.h" |
26 | #include "rtl.h" | |
27 | #include "ssa.h" | |
28 | #include "alias.h" | |
40e23961 | 29 | #include "fold-const.h" |
d8a2d370 | 30 | #include "stor-layout.h" |
e41d82f5 | 31 | #include "flags.h" |
2fb9a547 AM |
32 | #include "internal-fn.h" |
33 | #include "tree-eh.h" | |
45b0be94 | 34 | #include "gimplify.h" |
5be5c238 | 35 | #include "gimple-iterator.h" |
18f429e2 | 36 | #include "gimplify-me.h" |
442b4905 | 37 | #include "tree-cfg.h" |
36566b39 PK |
38 | #include "insn-config.h" |
39 | #include "expmed.h" | |
40 | #include "dojump.h" | |
41 | #include "explow.h" | |
42 | #include "calls.h" | |
43 | #include "emit-rtl.h" | |
44 | #include "varasm.h" | |
45 | #include "stmt.h" | |
d8a2d370 | 46 | #include "expr.h" |
442b4905 AM |
47 | #include "tree-dfa.h" |
48 | #include "tree-ssa.h" | |
6de9cd9a DN |
49 | #include "tree-iterator.h" |
50 | #include "tree-pass.h" | |
e41d82f5 | 51 | #include "tree-ssa-propagate.h" |
4a8fb1a1 | 52 | #include "tree-hasher.h" |
a9e0d843 | 53 | #include "cfgloop.h" |
e41d82f5 RH |
54 | |
55 | ||
56 | /* For each complex ssa name, a lattice value. We're interested in finding | |
57 | out whether a complex number is degenerate in some way, having only real | |
58 | or only complex parts. */ | |
59 | ||
32e8bb8e | 60 | enum |
e41d82f5 RH |
61 | { |
62 | UNINITIALIZED = 0, | |
63 | ONLY_REAL = 1, | |
64 | ONLY_IMAG = 2, | |
65 | VARYING = 3 | |
32e8bb8e ILT |
66 | }; |
67 | ||
68 | /* The type complex_lattice_t holds combinations of the above | |
69 | constants. */ | |
70 | typedef int complex_lattice_t; | |
e41d82f5 RH |
71 | |
72 | #define PAIR(a, b) ((a) << 2 | (b)) | |
73 | ||
e41d82f5 | 74 | |
9771b263 | 75 | static vec<complex_lattice_t> complex_lattice_values; |
e41d82f5 | 76 | |
a3648cfc DB |
77 | /* For each complex variable, a pair of variables for the components exists in |
78 | the hashtable. */ | |
c203e8a7 | 79 | static int_tree_htab_type *complex_variable_components; |
a3648cfc | 80 | |
95a8c155 | 81 | /* For each complex SSA_NAME, a pair of ssa names for the components. */ |
9771b263 | 82 | static vec<tree> complex_ssa_name_components; |
95a8c155 | 83 | |
a3648cfc DB |
84 | /* Lookup UID in the complex_variable_components hashtable and return the |
85 | associated tree. */ | |
b8698a0f | 86 | static tree |
a3648cfc DB |
87 | cvc_lookup (unsigned int uid) |
88 | { | |
84baa4b9 | 89 | struct int_tree_map in; |
a3648cfc | 90 | in.uid = uid; |
84baa4b9 | 91 | return complex_variable_components->find_with_hash (in, uid).to; |
a3648cfc | 92 | } |
b8698a0f | 93 | |
a3648cfc DB |
94 | /* Insert the pair UID, TO into the complex_variable_components hashtable. */ |
95 | ||
b8698a0f | 96 | static void |
a3648cfc | 97 | cvc_insert (unsigned int uid, tree to) |
b8698a0f | 98 | { |
84baa4b9 TS |
99 | int_tree_map h; |
100 | int_tree_map *loc; | |
a3648cfc | 101 | |
84baa4b9 | 102 | h.uid = uid; |
c203e8a7 | 103 | loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT); |
84baa4b9 TS |
104 | loc->uid = uid; |
105 | loc->to = to; | |
a3648cfc | 106 | } |
e41d82f5 | 107 | |
e41d82f5 RH |
108 | /* Return true if T is not a zero constant. In the case of real values, |
109 | we're only interested in +0.0. */ | |
110 | ||
111 | static int | |
112 | some_nonzerop (tree t) | |
113 | { | |
114 | int zerop = false; | |
115 | ||
2ca862e9 JM |
116 | /* Operations with real or imaginary part of a complex number zero |
117 | cannot be treated the same as operations with a real or imaginary | |
118 | operand if we care about the signs of zeros in the result. */ | |
119 | if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros) | |
e41d82f5 | 120 | zerop = REAL_VALUES_IDENTICAL (TREE_REAL_CST (t), dconst0); |
325217ed CF |
121 | else if (TREE_CODE (t) == FIXED_CST) |
122 | zerop = fixed_zerop (t); | |
e41d82f5 RH |
123 | else if (TREE_CODE (t) == INTEGER_CST) |
124 | zerop = integer_zerop (t); | |
125 | ||
126 | return !zerop; | |
127 | } | |
128 | ||
726a989a RB |
129 | |
130 | /* Compute a lattice value from the components of a complex type REAL | |
131 | and IMAG. */ | |
6de9cd9a | 132 | |
e41d82f5 | 133 | static complex_lattice_t |
726a989a | 134 | find_lattice_value_parts (tree real, tree imag) |
e41d82f5 | 135 | { |
e41d82f5 RH |
136 | int r, i; |
137 | complex_lattice_t ret; | |
138 | ||
726a989a RB |
139 | r = some_nonzerop (real); |
140 | i = some_nonzerop (imag); | |
141 | ret = r * ONLY_REAL + i * ONLY_IMAG; | |
142 | ||
143 | /* ??? On occasion we could do better than mapping 0+0i to real, but we | |
144 | certainly don't want to leave it UNINITIALIZED, which eventually gets | |
145 | mapped to VARYING. */ | |
146 | if (ret == UNINITIALIZED) | |
147 | ret = ONLY_REAL; | |
148 | ||
149 | return ret; | |
150 | } | |
151 | ||
152 | ||
153 | /* Compute a lattice value from gimple_val T. */ | |
154 | ||
155 | static complex_lattice_t | |
156 | find_lattice_value (tree t) | |
157 | { | |
158 | tree real, imag; | |
159 | ||
e41d82f5 RH |
160 | switch (TREE_CODE (t)) |
161 | { | |
162 | case SSA_NAME: | |
9771b263 | 163 | return complex_lattice_values[SSA_NAME_VERSION (t)]; |
e41d82f5 RH |
164 | |
165 | case COMPLEX_CST: | |
166 | real = TREE_REALPART (t); | |
167 | imag = TREE_IMAGPART (t); | |
168 | break; | |
169 | ||
e41d82f5 RH |
170 | default: |
171 | gcc_unreachable (); | |
172 | } | |
173 | ||
726a989a | 174 | return find_lattice_value_parts (real, imag); |
e41d82f5 RH |
175 | } |
176 | ||
177 | /* Determine if LHS is something for which we're interested in seeing | |
178 | simulation results. */ | |
179 | ||
180 | static bool | |
181 | is_complex_reg (tree lhs) | |
182 | { | |
183 | return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs); | |
184 | } | |
185 | ||
186 | /* Mark the incoming parameters to the function as VARYING. */ | |
187 | ||
188 | static void | |
189 | init_parameter_lattice_values (void) | |
190 | { | |
f0a77246 | 191 | tree parm, ssa_name; |
e41d82f5 | 192 | |
910ad8de | 193 | for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm)) |
f0a77246 | 194 | if (is_complex_reg (parm) |
32244553 | 195 | && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE) |
9771b263 | 196 | complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING; |
e41d82f5 RH |
197 | } |
198 | ||
726a989a RB |
199 | /* Initialize simulation state for each statement. Return false if we |
200 | found no statements we want to simulate, and thus there's nothing | |
201 | for the entire pass to do. */ | |
e41d82f5 RH |
202 | |
203 | static bool | |
204 | init_dont_simulate_again (void) | |
205 | { | |
206 | basic_block bb; | |
8f8abce4 | 207 | bool saw_a_complex_op = false; |
e41d82f5 | 208 | |
11cd3bed | 209 | FOR_EACH_BB_FN (bb, cfun) |
e41d82f5 | 210 | { |
538dd0b7 DM |
211 | for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
212 | gsi_next (&gsi)) | |
726a989a | 213 | { |
538dd0b7 | 214 | gphi *phi = gsi.phi (); |
726a989a RB |
215 | prop_set_simulate_again (phi, |
216 | is_complex_reg (gimple_phi_result (phi))); | |
217 | } | |
e41d82f5 | 218 | |
538dd0b7 DM |
219 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); |
220 | gsi_next (&gsi)) | |
e41d82f5 | 221 | { |
726a989a RB |
222 | gimple stmt; |
223 | tree op0, op1; | |
224 | bool sim_again_p; | |
e41d82f5 | 225 | |
726a989a RB |
226 | stmt = gsi_stmt (gsi); |
227 | op0 = op1 = NULL_TREE; | |
99e6bdda | 228 | |
b8698a0f | 229 | /* Most control-altering statements must be initially |
99e6bdda | 230 | simulated, else we won't cover the entire cfg. */ |
726a989a | 231 | sim_again_p = stmt_ends_bb_p (stmt); |
99e6bdda | 232 | |
726a989a | 233 | switch (gimple_code (stmt)) |
e41d82f5 | 234 | { |
726a989a RB |
235 | case GIMPLE_CALL: |
236 | if (gimple_call_lhs (stmt)) | |
237 | sim_again_p = is_complex_reg (gimple_call_lhs (stmt)); | |
238 | break; | |
8f8abce4 | 239 | |
726a989a RB |
240 | case GIMPLE_ASSIGN: |
241 | sim_again_p = is_complex_reg (gimple_assign_lhs (stmt)); | |
242 | if (gimple_assign_rhs_code (stmt) == REALPART_EXPR | |
243 | || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR) | |
244 | op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0); | |
245 | else | |
246 | op0 = gimple_assign_rhs1 (stmt); | |
247 | if (gimple_num_ops (stmt) > 2) | |
248 | op1 = gimple_assign_rhs2 (stmt); | |
8f8abce4 RH |
249 | break; |
250 | ||
726a989a RB |
251 | case GIMPLE_COND: |
252 | op0 = gimple_cond_lhs (stmt); | |
253 | op1 = gimple_cond_rhs (stmt); | |
8f8abce4 RH |
254 | break; |
255 | ||
256 | default: | |
257 | break; | |
e41d82f5 RH |
258 | } |
259 | ||
726a989a RB |
260 | if (op0 || op1) |
261 | switch (gimple_expr_code (stmt)) | |
8f8abce4 RH |
262 | { |
263 | case EQ_EXPR: | |
264 | case NE_EXPR: | |
8f8abce4 RH |
265 | case PLUS_EXPR: |
266 | case MINUS_EXPR: | |
267 | case MULT_EXPR: | |
268 | case TRUNC_DIV_EXPR: | |
269 | case CEIL_DIV_EXPR: | |
270 | case FLOOR_DIV_EXPR: | |
271 | case ROUND_DIV_EXPR: | |
272 | case RDIV_EXPR: | |
726a989a RB |
273 | if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE |
274 | || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE) | |
275 | saw_a_complex_op = true; | |
276 | break; | |
277 | ||
8f8abce4 RH |
278 | case NEGATE_EXPR: |
279 | case CONJ_EXPR: | |
726a989a | 280 | if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE) |
8f8abce4 RH |
281 | saw_a_complex_op = true; |
282 | break; | |
283 | ||
7b7e6ecd EB |
284 | case REALPART_EXPR: |
285 | case IMAGPART_EXPR: | |
286 | /* The total store transformation performed during | |
726a989a RB |
287 | gimplification creates such uninitialized loads |
288 | and we need to lower the statement to be able | |
289 | to fix things up. */ | |
290 | if (TREE_CODE (op0) == SSA_NAME | |
291 | && ssa_undefined_value_p (op0)) | |
7b7e6ecd EB |
292 | saw_a_complex_op = true; |
293 | break; | |
294 | ||
8f8abce4 RH |
295 | default: |
296 | break; | |
297 | } | |
298 | ||
726a989a | 299 | prop_set_simulate_again (stmt, sim_again_p); |
e41d82f5 RH |
300 | } |
301 | } | |
302 | ||
8f8abce4 | 303 | return saw_a_complex_op; |
e41d82f5 RH |
304 | } |
305 | ||
306 | ||
307 | /* Evaluate statement STMT against the complex lattice defined above. */ | |
308 | ||
309 | static enum ssa_prop_result | |
726a989a | 310 | complex_visit_stmt (gimple stmt, edge *taken_edge_p ATTRIBUTE_UNUSED, |
e41d82f5 RH |
311 | tree *result_p) |
312 | { | |
313 | complex_lattice_t new_l, old_l, op1_l, op2_l; | |
314 | unsigned int ver; | |
726a989a | 315 | tree lhs; |
e41d82f5 | 316 | |
726a989a RB |
317 | lhs = gimple_get_lhs (stmt); |
318 | /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */ | |
319 | if (!lhs) | |
99e6bdda | 320 | return SSA_PROP_VARYING; |
e41d82f5 | 321 | |
99e6bdda RH |
322 | /* These conditions should be satisfied due to the initial filter |
323 | set up in init_dont_simulate_again. */ | |
e41d82f5 RH |
324 | gcc_assert (TREE_CODE (lhs) == SSA_NAME); |
325 | gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE); | |
326 | ||
327 | *result_p = lhs; | |
328 | ver = SSA_NAME_VERSION (lhs); | |
9771b263 | 329 | old_l = complex_lattice_values[ver]; |
e41d82f5 | 330 | |
726a989a | 331 | switch (gimple_expr_code (stmt)) |
e41d82f5 RH |
332 | { |
333 | case SSA_NAME: | |
e41d82f5 | 334 | case COMPLEX_CST: |
726a989a RB |
335 | new_l = find_lattice_value (gimple_assign_rhs1 (stmt)); |
336 | break; | |
337 | ||
338 | case COMPLEX_EXPR: | |
339 | new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt), | |
340 | gimple_assign_rhs2 (stmt)); | |
e41d82f5 RH |
341 | break; |
342 | ||
343 | case PLUS_EXPR: | |
344 | case MINUS_EXPR: | |
726a989a RB |
345 | op1_l = find_lattice_value (gimple_assign_rhs1 (stmt)); |
346 | op2_l = find_lattice_value (gimple_assign_rhs2 (stmt)); | |
e41d82f5 RH |
347 | |
348 | /* We've set up the lattice values such that IOR neatly | |
349 | models addition. */ | |
350 | new_l = op1_l | op2_l; | |
351 | break; | |
352 | ||
353 | case MULT_EXPR: | |
354 | case RDIV_EXPR: | |
355 | case TRUNC_DIV_EXPR: | |
356 | case CEIL_DIV_EXPR: | |
357 | case FLOOR_DIV_EXPR: | |
358 | case ROUND_DIV_EXPR: | |
726a989a RB |
359 | op1_l = find_lattice_value (gimple_assign_rhs1 (stmt)); |
360 | op2_l = find_lattice_value (gimple_assign_rhs2 (stmt)); | |
e41d82f5 RH |
361 | |
362 | /* Obviously, if either varies, so does the result. */ | |
363 | if (op1_l == VARYING || op2_l == VARYING) | |
364 | new_l = VARYING; | |
365 | /* Don't prematurely promote variables if we've not yet seen | |
366 | their inputs. */ | |
367 | else if (op1_l == UNINITIALIZED) | |
368 | new_l = op2_l; | |
369 | else if (op2_l == UNINITIALIZED) | |
370 | new_l = op1_l; | |
371 | else | |
372 | { | |
373 | /* At this point both numbers have only one component. If the | |
374 | numbers are of opposite kind, the result is imaginary, | |
375 | otherwise the result is real. The add/subtract translates | |
376 | the real/imag from/to 0/1; the ^ performs the comparison. */ | |
377 | new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL; | |
378 | ||
379 | /* Don't allow the lattice value to flip-flop indefinitely. */ | |
380 | new_l |= old_l; | |
381 | } | |
382 | break; | |
383 | ||
384 | case NEGATE_EXPR: | |
385 | case CONJ_EXPR: | |
726a989a | 386 | new_l = find_lattice_value (gimple_assign_rhs1 (stmt)); |
e41d82f5 RH |
387 | break; |
388 | ||
389 | default: | |
390 | new_l = VARYING; | |
391 | break; | |
392 | } | |
393 | ||
394 | /* If nothing changed this round, let the propagator know. */ | |
395 | if (new_l == old_l) | |
396 | return SSA_PROP_NOT_INTERESTING; | |
397 | ||
9771b263 | 398 | complex_lattice_values[ver] = new_l; |
e41d82f5 RH |
399 | return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING; |
400 | } | |
401 | ||
402 | /* Evaluate a PHI node against the complex lattice defined above. */ | |
403 | ||
404 | static enum ssa_prop_result | |
538dd0b7 | 405 | complex_visit_phi (gphi *phi) |
e41d82f5 RH |
406 | { |
407 | complex_lattice_t new_l, old_l; | |
408 | unsigned int ver; | |
409 | tree lhs; | |
410 | int i; | |
411 | ||
726a989a | 412 | lhs = gimple_phi_result (phi); |
e41d82f5 RH |
413 | |
414 | /* This condition should be satisfied due to the initial filter | |
415 | set up in init_dont_simulate_again. */ | |
416 | gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE); | |
417 | ||
418 | /* We've set up the lattice values such that IOR neatly models PHI meet. */ | |
419 | new_l = UNINITIALIZED; | |
726a989a RB |
420 | for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i) |
421 | new_l |= find_lattice_value (gimple_phi_arg_def (phi, i)); | |
e41d82f5 RH |
422 | |
423 | ver = SSA_NAME_VERSION (lhs); | |
9771b263 | 424 | old_l = complex_lattice_values[ver]; |
e41d82f5 RH |
425 | |
426 | if (new_l == old_l) | |
427 | return SSA_PROP_NOT_INTERESTING; | |
428 | ||
9771b263 | 429 | complex_lattice_values[ver] = new_l; |
e41d82f5 RH |
430 | return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING; |
431 | } | |
432 | ||
95a8c155 | 433 | /* Create one backing variable for a complex component of ORIG. */ |
e41d82f5 | 434 | |
95a8c155 RH |
435 | static tree |
436 | create_one_component_var (tree type, tree orig, const char *prefix, | |
437 | const char *suffix, enum tree_code code) | |
e41d82f5 | 438 | { |
95a8c155 | 439 | tree r = create_tmp_var (type, prefix); |
e41d82f5 | 440 | |
95a8c155 RH |
441 | DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig); |
442 | DECL_ARTIFICIAL (r) = 1; | |
8f8abce4 | 443 | |
95a8c155 RH |
444 | if (DECL_NAME (orig) && !DECL_IGNORED_P (orig)) |
445 | { | |
446 | const char *name = IDENTIFIER_POINTER (DECL_NAME (orig)); | |
95a8c155 RH |
447 | |
448 | DECL_NAME (r) = get_identifier (ACONCAT ((name, suffix, NULL))); | |
e41d82f5 | 449 | |
95a8c155 | 450 | SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig)); |
839b422f | 451 | DECL_HAS_DEBUG_EXPR_P (r) = 1; |
95a8c155 RH |
452 | DECL_IGNORED_P (r) = 0; |
453 | TREE_NO_WARNING (r) = TREE_NO_WARNING (orig); | |
454 | } | |
455 | else | |
e41d82f5 | 456 | { |
95a8c155 RH |
457 | DECL_IGNORED_P (r) = 1; |
458 | TREE_NO_WARNING (r) = 1; | |
459 | } | |
e41d82f5 | 460 | |
95a8c155 RH |
461 | return r; |
462 | } | |
e41d82f5 | 463 | |
95a8c155 | 464 | /* Retrieve a value for a complex component of VAR. */ |
e41d82f5 | 465 | |
95a8c155 RH |
466 | static tree |
467 | get_component_var (tree var, bool imag_p) | |
468 | { | |
469 | size_t decl_index = DECL_UID (var) * 2 + imag_p; | |
470 | tree ret = cvc_lookup (decl_index); | |
471 | ||
472 | if (ret == NULL) | |
473 | { | |
474 | ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var, | |
475 | imag_p ? "CI" : "CR", | |
476 | imag_p ? "$imag" : "$real", | |
477 | imag_p ? IMAGPART_EXPR : REALPART_EXPR); | |
478 | cvc_insert (decl_index, ret); | |
479 | } | |
480 | ||
481 | return ret; | |
482 | } | |
e41d82f5 | 483 | |
95a8c155 | 484 | /* Retrieve a value for a complex component of SSA_NAME. */ |
e41d82f5 | 485 | |
95a8c155 RH |
486 | static tree |
487 | get_component_ssa_name (tree ssa_name, bool imag_p) | |
488 | { | |
489 | complex_lattice_t lattice = find_lattice_value (ssa_name); | |
490 | size_t ssa_name_index; | |
491 | tree ret; | |
e41d82f5 | 492 | |
95a8c155 RH |
493 | if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG)) |
494 | { | |
495 | tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name)); | |
496 | if (SCALAR_FLOAT_TYPE_P (inner_type)) | |
497 | return build_real (inner_type, dconst0); | |
498 | else | |
499 | return build_int_cst (inner_type, 0); | |
500 | } | |
e41d82f5 | 501 | |
95a8c155 | 502 | ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p; |
9771b263 | 503 | ret = complex_ssa_name_components[ssa_name_index]; |
95a8c155 RH |
504 | if (ret == NULL) |
505 | { | |
70b5e7dc RG |
506 | if (SSA_NAME_VAR (ssa_name)) |
507 | ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p); | |
508 | else | |
509 | ret = TREE_TYPE (TREE_TYPE (ssa_name)); | |
b731b390 | 510 | ret = make_ssa_name (ret); |
95a8c155 RH |
511 | |
512 | /* Copy some properties from the original. In particular, whether it | |
513 | is used in an abnormal phi, and whether it's uninitialized. */ | |
514 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret) | |
515 | = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name); | |
67386041 RG |
516 | if (SSA_NAME_IS_DEFAULT_DEF (ssa_name) |
517 | && TREE_CODE (SSA_NAME_VAR (ssa_name)) == VAR_DECL) | |
95a8c155 RH |
518 | { |
519 | SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name); | |
32244553 | 520 | set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret); |
e41d82f5 RH |
521 | } |
522 | ||
9771b263 | 523 | complex_ssa_name_components[ssa_name_index] = ret; |
e41d82f5 | 524 | } |
95a8c155 RH |
525 | |
526 | return ret; | |
527 | } | |
528 | ||
726a989a RB |
529 | /* Set a value for a complex component of SSA_NAME, return a |
530 | gimple_seq of stuff that needs doing. */ | |
95a8c155 | 531 | |
726a989a | 532 | static gimple_seq |
95a8c155 RH |
533 | set_component_ssa_name (tree ssa_name, bool imag_p, tree value) |
534 | { | |
535 | complex_lattice_t lattice = find_lattice_value (ssa_name); | |
536 | size_t ssa_name_index; | |
726a989a RB |
537 | tree comp; |
538 | gimple last; | |
539 | gimple_seq list; | |
95a8c155 RH |
540 | |
541 | /* We know the value must be zero, else there's a bug in our lattice | |
542 | analysis. But the value may well be a variable known to contain | |
543 | zero. We should be safe ignoring it. */ | |
544 | if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG)) | |
545 | return NULL; | |
546 | ||
547 | /* If we've already assigned an SSA_NAME to this component, then this | |
548 | means that our walk of the basic blocks found a use before the set. | |
549 | This is fine. Now we should create an initialization for the value | |
550 | we created earlier. */ | |
551 | ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p; | |
9771b263 | 552 | comp = complex_ssa_name_components[ssa_name_index]; |
95a8c155 RH |
553 | if (comp) |
554 | ; | |
555 | ||
556 | /* If we've nothing assigned, and the value we're given is already stable, | |
536fa7b7 | 557 | then install that as the value for this SSA_NAME. This preemptively |
95a8c155 | 558 | copy-propagates the value, which avoids unnecessary memory allocation. */ |
35a45bd4 RH |
559 | else if (is_gimple_min_invariant (value) |
560 | && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name)) | |
95a8c155 | 561 | { |
9771b263 | 562 | complex_ssa_name_components[ssa_name_index] = value; |
95a8c155 RH |
563 | return NULL; |
564 | } | |
565 | else if (TREE_CODE (value) == SSA_NAME | |
566 | && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name)) | |
567 | { | |
568 | /* Replace an anonymous base value with the variable from cvc_lookup. | |
569 | This should result in better debug info. */ | |
70b5e7dc RG |
570 | if (SSA_NAME_VAR (ssa_name) |
571 | && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value))) | |
95a8c155 RH |
572 | && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name))) |
573 | { | |
574 | comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p); | |
db753fa1 | 575 | replace_ssa_name_symbol (value, comp); |
95a8c155 RH |
576 | } |
577 | ||
9771b263 | 578 | complex_ssa_name_components[ssa_name_index] = value; |
95a8c155 RH |
579 | return NULL; |
580 | } | |
581 | ||
582 | /* Finally, we need to stabilize the result by installing the value into | |
583 | a new ssa name. */ | |
584 | else | |
585 | comp = get_component_ssa_name (ssa_name, imag_p); | |
b8698a0f | 586 | |
95a8c155 | 587 | /* Do all the work to assign VALUE to COMP. */ |
726a989a | 588 | list = NULL; |
95a8c155 | 589 | value = force_gimple_operand (value, &list, false, NULL); |
726a989a RB |
590 | last = gimple_build_assign (comp, value); |
591 | gimple_seq_add_stmt (&list, last); | |
592 | gcc_assert (SSA_NAME_DEF_STMT (comp) == last); | |
95a8c155 RH |
593 | |
594 | return list; | |
e41d82f5 | 595 | } |
6de9cd9a | 596 | |
6de9cd9a DN |
597 | /* Extract the real or imaginary part of a complex variable or constant. |
598 | Make sure that it's a proper gimple_val and gimplify it if not. | |
726a989a | 599 | Emit any new code before gsi. */ |
6de9cd9a DN |
600 | |
601 | static tree | |
726a989a | 602 | extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p, |
e41d82f5 | 603 | bool gimple_p) |
6de9cd9a | 604 | { |
6de9cd9a DN |
605 | switch (TREE_CODE (t)) |
606 | { | |
607 | case COMPLEX_CST: | |
e41d82f5 | 608 | return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t); |
6de9cd9a DN |
609 | |
610 | case COMPLEX_EXPR: | |
726a989a | 611 | gcc_unreachable (); |
6de9cd9a DN |
612 | |
613 | case VAR_DECL: | |
f35a986c | 614 | case RESULT_DECL: |
6de9cd9a | 615 | case PARM_DECL: |
e41d82f5 RH |
616 | case COMPONENT_REF: |
617 | case ARRAY_REF: | |
7ec49257 | 618 | case VIEW_CONVERT_EXPR: |
70f34814 | 619 | case MEM_REF: |
e41d82f5 RH |
620 | { |
621 | tree inner_type = TREE_TYPE (TREE_TYPE (t)); | |
622 | ||
623 | t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR), | |
624 | inner_type, unshare_expr (t)); | |
625 | ||
626 | if (gimple_p) | |
726a989a RB |
627 | t = force_gimple_operand_gsi (gsi, t, true, NULL, true, |
628 | GSI_SAME_STMT); | |
e41d82f5 RH |
629 | |
630 | return t; | |
631 | } | |
632 | ||
633 | case SSA_NAME: | |
95a8c155 | 634 | return get_component_ssa_name (t, imagpart_p); |
6de9cd9a DN |
635 | |
636 | default: | |
1e128c5f | 637 | gcc_unreachable (); |
6de9cd9a | 638 | } |
e41d82f5 RH |
639 | } |
640 | ||
641 | /* Update the complex components of the ssa name on the lhs of STMT. */ | |
6de9cd9a | 642 | |
e41d82f5 | 643 | static void |
726a989a RB |
644 | update_complex_components (gimple_stmt_iterator *gsi, gimple stmt, tree r, |
645 | tree i) | |
e41d82f5 | 646 | { |
726a989a RB |
647 | tree lhs; |
648 | gimple_seq list; | |
649 | ||
650 | lhs = gimple_get_lhs (stmt); | |
95a8c155 RH |
651 | |
652 | list = set_component_ssa_name (lhs, false, r); | |
653 | if (list) | |
726a989a | 654 | gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING); |
95a8c155 RH |
655 | |
656 | list = set_component_ssa_name (lhs, true, i); | |
657 | if (list) | |
726a989a | 658 | gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING); |
6de9cd9a DN |
659 | } |
660 | ||
5d6b3bba | 661 | static void |
95a8c155 | 662 | update_complex_components_on_edge (edge e, tree lhs, tree r, tree i) |
5d6b3bba | 663 | { |
726a989a | 664 | gimple_seq list; |
5d6b3bba | 665 | |
95a8c155 RH |
666 | list = set_component_ssa_name (lhs, false, r); |
667 | if (list) | |
726a989a | 668 | gsi_insert_seq_on_edge (e, list); |
5d6b3bba | 669 | |
95a8c155 RH |
670 | list = set_component_ssa_name (lhs, true, i); |
671 | if (list) | |
726a989a | 672 | gsi_insert_seq_on_edge (e, list); |
5d6b3bba RH |
673 | } |
674 | ||
726a989a | 675 | |
6de9cd9a DN |
676 | /* Update an assignment to a complex variable in place. */ |
677 | ||
678 | static void | |
726a989a | 679 | update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i) |
6de9cd9a | 680 | { |
f5e5b46c | 681 | gimple stmt; |
726a989a | 682 | |
355a7673 MM |
683 | gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i); |
684 | stmt = gsi_stmt (*gsi); | |
f5e5b46c RG |
685 | update_stmt (stmt); |
686 | if (maybe_clean_eh_stmt (stmt)) | |
687 | gimple_purge_dead_eh_edges (gimple_bb (stmt)); | |
355a7673 MM |
688 | |
689 | if (gimple_in_ssa_p (cfun)) | |
690 | update_complex_components (gsi, gsi_stmt (*gsi), r, i); | |
e41d82f5 RH |
691 | } |
692 | ||
726a989a | 693 | |
e41d82f5 RH |
694 | /* Generate code at the entry point of the function to initialize the |
695 | component variables for a complex parameter. */ | |
696 | ||
697 | static void | |
698 | update_parameter_components (void) | |
699 | { | |
fefa31b5 | 700 | edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
e41d82f5 RH |
701 | tree parm; |
702 | ||
910ad8de | 703 | for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm)) |
e41d82f5 RH |
704 | { |
705 | tree type = TREE_TYPE (parm); | |
5d6b3bba | 706 | tree ssa_name, r, i; |
e41d82f5 RH |
707 | |
708 | if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm)) | |
709 | continue; | |
710 | ||
711 | type = TREE_TYPE (type); | |
32244553 | 712 | ssa_name = ssa_default_def (cfun, parm); |
c0a3f887 RG |
713 | if (!ssa_name) |
714 | continue; | |
e41d82f5 | 715 | |
5d6b3bba RH |
716 | r = build1 (REALPART_EXPR, type, ssa_name); |
717 | i = build1 (IMAGPART_EXPR, type, ssa_name); | |
95a8c155 | 718 | update_complex_components_on_edge (entry_edge, ssa_name, r, i); |
e41d82f5 RH |
719 | } |
720 | } | |
721 | ||
722 | /* Generate code to set the component variables of a complex variable | |
723 | to match the PHI statements in block BB. */ | |
724 | ||
725 | static void | |
726 | update_phi_components (basic_block bb) | |
727 | { | |
538dd0b7 | 728 | gphi_iterator gsi; |
e41d82f5 | 729 | |
726a989a RB |
730 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
731 | { | |
538dd0b7 | 732 | gphi *phi = gsi.phi (); |
e41d82f5 | 733 | |
726a989a RB |
734 | if (is_complex_reg (gimple_phi_result (phi))) |
735 | { | |
736 | tree lr, li; | |
737 | gimple pr = NULL, pi = NULL; | |
738 | unsigned int i, n; | |
95a8c155 | 739 | |
726a989a RB |
740 | lr = get_component_ssa_name (gimple_phi_result (phi), false); |
741 | if (TREE_CODE (lr) == SSA_NAME) | |
dcc748dd | 742 | pr = create_phi_node (lr, bb); |
726a989a RB |
743 | |
744 | li = get_component_ssa_name (gimple_phi_result (phi), true); | |
745 | if (TREE_CODE (li) == SSA_NAME) | |
dcc748dd | 746 | pi = create_phi_node (li, bb); |
726a989a RB |
747 | |
748 | for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i) | |
749 | { | |
750 | tree comp, arg = gimple_phi_arg_def (phi, i); | |
751 | if (pr) | |
752 | { | |
753 | comp = extract_component (NULL, arg, false, false); | |
754 | SET_PHI_ARG_DEF (pr, i, comp); | |
755 | } | |
756 | if (pi) | |
757 | { | |
758 | comp = extract_component (NULL, arg, true, false); | |
759 | SET_PHI_ARG_DEF (pi, i, comp); | |
760 | } | |
761 | } | |
762 | } | |
763 | } | |
e41d82f5 RH |
764 | } |
765 | ||
e41d82f5 RH |
766 | /* Expand a complex move to scalars. */ |
767 | ||
768 | static void | |
726a989a | 769 | expand_complex_move (gimple_stmt_iterator *gsi, tree type) |
e41d82f5 RH |
770 | { |
771 | tree inner_type = TREE_TYPE (type); | |
726a989a RB |
772 | tree r, i, lhs, rhs; |
773 | gimple stmt = gsi_stmt (*gsi); | |
774 | ||
775 | if (is_gimple_assign (stmt)) | |
776 | { | |
777 | lhs = gimple_assign_lhs (stmt); | |
778 | if (gimple_num_ops (stmt) == 2) | |
779 | rhs = gimple_assign_rhs1 (stmt); | |
780 | else | |
781 | rhs = NULL_TREE; | |
782 | } | |
783 | else if (is_gimple_call (stmt)) | |
784 | { | |
785 | lhs = gimple_call_lhs (stmt); | |
786 | rhs = NULL_TREE; | |
787 | } | |
788 | else | |
789 | gcc_unreachable (); | |
e41d82f5 RH |
790 | |
791 | if (TREE_CODE (lhs) == SSA_NAME) | |
792 | { | |
726a989a | 793 | if (is_ctrl_altering_stmt (stmt)) |
5d6b3bba | 794 | { |
5d6b3bba RH |
795 | edge e; |
796 | ||
797 | /* The value is not assigned on the exception edges, so we need not | |
798 | concern ourselves there. We do need to update on the fallthru | |
799 | edge. Find it. */ | |
0fd4b31d NF |
800 | e = find_fallthru_edge (gsi_bb (*gsi)->succs); |
801 | if (!e) | |
802 | gcc_unreachable (); | |
5d6b3bba RH |
803 | |
804 | r = build1 (REALPART_EXPR, inner_type, lhs); | |
805 | i = build1 (IMAGPART_EXPR, inner_type, lhs); | |
95a8c155 | 806 | update_complex_components_on_edge (e, lhs, r, i); |
5d6b3bba | 807 | } |
726a989a RB |
808 | else if (is_gimple_call (stmt) |
809 | || gimple_has_side_effects (stmt) | |
810 | || gimple_assign_rhs_code (stmt) == PAREN_EXPR) | |
e41d82f5 | 811 | { |
5d6b3bba RH |
812 | r = build1 (REALPART_EXPR, inner_type, lhs); |
813 | i = build1 (IMAGPART_EXPR, inner_type, lhs); | |
726a989a | 814 | update_complex_components (gsi, stmt, r, i); |
e41d82f5 RH |
815 | } |
816 | else | |
817 | { | |
726a989a RB |
818 | if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR) |
819 | { | |
820 | r = extract_component (gsi, rhs, 0, true); | |
821 | i = extract_component (gsi, rhs, 1, true); | |
822 | } | |
823 | else | |
824 | { | |
825 | r = gimple_assign_rhs1 (stmt); | |
826 | i = gimple_assign_rhs2 (stmt); | |
827 | } | |
828 | update_complex_assignment (gsi, r, i); | |
e41d82f5 RH |
829 | } |
830 | } | |
726a989a | 831 | else if (rhs && TREE_CODE (rhs) == SSA_NAME && !TREE_SIDE_EFFECTS (lhs)) |
e41d82f5 RH |
832 | { |
833 | tree x; | |
726a989a | 834 | gimple t; |
e1ec47c4 | 835 | location_t loc; |
e41d82f5 | 836 | |
e1ec47c4 | 837 | loc = gimple_location (stmt); |
726a989a RB |
838 | r = extract_component (gsi, rhs, 0, false); |
839 | i = extract_component (gsi, rhs, 1, false); | |
e41d82f5 RH |
840 | |
841 | x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs)); | |
726a989a | 842 | t = gimple_build_assign (x, r); |
e1ec47c4 | 843 | gimple_set_location (t, loc); |
726a989a | 844 | gsi_insert_before (gsi, t, GSI_SAME_STMT); |
e41d82f5 | 845 | |
726a989a | 846 | if (stmt == gsi_stmt (*gsi)) |
e41d82f5 RH |
847 | { |
848 | x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs)); | |
726a989a RB |
849 | gimple_assign_set_lhs (stmt, x); |
850 | gimple_assign_set_rhs1 (stmt, i); | |
e41d82f5 RH |
851 | } |
852 | else | |
853 | { | |
854 | x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs)); | |
726a989a | 855 | t = gimple_build_assign (x, i); |
e1ec47c4 | 856 | gimple_set_location (t, loc); |
726a989a | 857 | gsi_insert_before (gsi, t, GSI_SAME_STMT); |
e41d82f5 | 858 | |
726a989a RB |
859 | stmt = gsi_stmt (*gsi); |
860 | gcc_assert (gimple_code (stmt) == GIMPLE_RETURN); | |
538dd0b7 | 861 | gimple_return_set_retval (as_a <greturn *> (stmt), lhs); |
e41d82f5 RH |
862 | } |
863 | ||
e41d82f5 RH |
864 | update_stmt (stmt); |
865 | } | |
6de9cd9a DN |
866 | } |
867 | ||
868 | /* Expand complex addition to scalars: | |
869 | a + b = (ar + br) + i(ai + bi) | |
870 | a - b = (ar - br) + i(ai + bi) | |
871 | */ | |
872 | ||
873 | static void | |
726a989a | 874 | expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type, |
6de9cd9a | 875 | tree ar, tree ai, tree br, tree bi, |
e41d82f5 RH |
876 | enum tree_code code, |
877 | complex_lattice_t al, complex_lattice_t bl) | |
6de9cd9a DN |
878 | { |
879 | tree rr, ri; | |
880 | ||
e41d82f5 RH |
881 | switch (PAIR (al, bl)) |
882 | { | |
883 | case PAIR (ONLY_REAL, ONLY_REAL): | |
726a989a | 884 | rr = gimplify_build2 (gsi, code, inner_type, ar, br); |
e41d82f5 RH |
885 | ri = ai; |
886 | break; | |
887 | ||
888 | case PAIR (ONLY_REAL, ONLY_IMAG): | |
889 | rr = ar; | |
890 | if (code == MINUS_EXPR) | |
726a989a | 891 | ri = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, bi); |
e41d82f5 RH |
892 | else |
893 | ri = bi; | |
894 | break; | |
895 | ||
896 | case PAIR (ONLY_IMAG, ONLY_REAL): | |
897 | if (code == MINUS_EXPR) | |
726a989a | 898 | rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ar, br); |
e41d82f5 RH |
899 | else |
900 | rr = br; | |
901 | ri = ai; | |
902 | break; | |
903 | ||
904 | case PAIR (ONLY_IMAG, ONLY_IMAG): | |
905 | rr = ar; | |
726a989a | 906 | ri = gimplify_build2 (gsi, code, inner_type, ai, bi); |
e41d82f5 RH |
907 | break; |
908 | ||
909 | case PAIR (VARYING, ONLY_REAL): | |
726a989a | 910 | rr = gimplify_build2 (gsi, code, inner_type, ar, br); |
e41d82f5 RH |
911 | ri = ai; |
912 | break; | |
913 | ||
914 | case PAIR (VARYING, ONLY_IMAG): | |
915 | rr = ar; | |
726a989a | 916 | ri = gimplify_build2 (gsi, code, inner_type, ai, bi); |
e41d82f5 RH |
917 | break; |
918 | ||
919 | case PAIR (ONLY_REAL, VARYING): | |
920 | if (code == MINUS_EXPR) | |
921 | goto general; | |
726a989a | 922 | rr = gimplify_build2 (gsi, code, inner_type, ar, br); |
e41d82f5 RH |
923 | ri = bi; |
924 | break; | |
925 | ||
926 | case PAIR (ONLY_IMAG, VARYING): | |
927 | if (code == MINUS_EXPR) | |
928 | goto general; | |
929 | rr = br; | |
726a989a | 930 | ri = gimplify_build2 (gsi, code, inner_type, ai, bi); |
e41d82f5 RH |
931 | break; |
932 | ||
933 | case PAIR (VARYING, VARYING): | |
934 | general: | |
726a989a RB |
935 | rr = gimplify_build2 (gsi, code, inner_type, ar, br); |
936 | ri = gimplify_build2 (gsi, code, inner_type, ai, bi); | |
e41d82f5 RH |
937 | break; |
938 | ||
939 | default: | |
940 | gcc_unreachable (); | |
941 | } | |
6de9cd9a | 942 | |
726a989a | 943 | update_complex_assignment (gsi, rr, ri); |
6de9cd9a DN |
944 | } |
945 | ||
7e7e470f RH |
946 | /* Expand a complex multiplication or division to a libcall to the c99 |
947 | compliant routines. */ | |
948 | ||
949 | static void | |
726a989a | 950 | expand_complex_libcall (gimple_stmt_iterator *gsi, tree ar, tree ai, |
7e7e470f RH |
951 | tree br, tree bi, enum tree_code code) |
952 | { | |
ef4bddc2 | 953 | machine_mode mode; |
7e7e470f | 954 | enum built_in_function bcode; |
726a989a | 955 | tree fn, type, lhs; |
538dd0b7 DM |
956 | gimple old_stmt; |
957 | gcall *stmt; | |
7e7e470f | 958 | |
04be6ff5 JJ |
959 | old_stmt = gsi_stmt (*gsi); |
960 | lhs = gimple_assign_lhs (old_stmt); | |
726a989a | 961 | type = TREE_TYPE (lhs); |
7e7e470f RH |
962 | |
963 | mode = TYPE_MODE (type); | |
964 | gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT); | |
726a989a | 965 | |
7e7e470f | 966 | if (code == MULT_EXPR) |
32e8bb8e ILT |
967 | bcode = ((enum built_in_function) |
968 | (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT)); | |
7e7e470f | 969 | else if (code == RDIV_EXPR) |
32e8bb8e ILT |
970 | bcode = ((enum built_in_function) |
971 | (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT)); | |
7e7e470f RH |
972 | else |
973 | gcc_unreachable (); | |
e79983f4 | 974 | fn = builtin_decl_explicit (bcode); |
7e7e470f | 975 | |
726a989a RB |
976 | stmt = gimple_build_call (fn, 4, ar, ai, br, bi); |
977 | gimple_call_set_lhs (stmt, lhs); | |
f430bae8 | 978 | update_stmt (stmt); |
04be6ff5 JJ |
979 | gsi_replace (gsi, stmt, false); |
980 | ||
981 | if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)) | |
982 | gimple_purge_dead_eh_edges (gsi_bb (*gsi)); | |
e41d82f5 | 983 | |
5cd4ec7f | 984 | if (gimple_in_ssa_p (cfun)) |
e41d82f5 | 985 | { |
d5c77941 | 986 | type = TREE_TYPE (type); |
726a989a | 987 | update_complex_components (gsi, stmt, |
e41d82f5 RH |
988 | build1 (REALPART_EXPR, type, lhs), |
989 | build1 (IMAGPART_EXPR, type, lhs)); | |
726a989a | 990 | SSA_NAME_DEF_STMT (lhs) = stmt; |
e41d82f5 | 991 | } |
7e7e470f RH |
992 | } |
993 | ||
6de9cd9a DN |
994 | /* Expand complex multiplication to scalars: |
995 | a * b = (ar*br - ai*bi) + i(ar*bi + br*ai) | |
996 | */ | |
997 | ||
998 | static void | |
726a989a | 999 | expand_complex_multiplication (gimple_stmt_iterator *gsi, tree inner_type, |
e41d82f5 RH |
1000 | tree ar, tree ai, tree br, tree bi, |
1001 | complex_lattice_t al, complex_lattice_t bl) | |
6de9cd9a | 1002 | { |
e41d82f5 | 1003 | tree rr, ri; |
6de9cd9a | 1004 | |
e41d82f5 | 1005 | if (al < bl) |
7e7e470f | 1006 | { |
e41d82f5 RH |
1007 | complex_lattice_t tl; |
1008 | rr = ar, ar = br, br = rr; | |
1009 | ri = ai, ai = bi, bi = ri; | |
1010 | tl = al, al = bl, bl = tl; | |
7e7e470f RH |
1011 | } |
1012 | ||
e41d82f5 RH |
1013 | switch (PAIR (al, bl)) |
1014 | { | |
1015 | case PAIR (ONLY_REAL, ONLY_REAL): | |
726a989a | 1016 | rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br); |
e41d82f5 RH |
1017 | ri = ai; |
1018 | break; | |
6de9cd9a | 1019 | |
e41d82f5 RH |
1020 | case PAIR (ONLY_IMAG, ONLY_REAL): |
1021 | rr = ar; | |
1022 | if (TREE_CODE (ai) == REAL_CST | |
1023 | && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst1)) | |
1024 | ri = br; | |
1025 | else | |
726a989a | 1026 | ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br); |
e41d82f5 | 1027 | break; |
6de9cd9a | 1028 | |
e41d82f5 | 1029 | case PAIR (ONLY_IMAG, ONLY_IMAG): |
726a989a RB |
1030 | rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi); |
1031 | rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr); | |
e41d82f5 RH |
1032 | ri = ar; |
1033 | break; | |
1034 | ||
1035 | case PAIR (VARYING, ONLY_REAL): | |
726a989a RB |
1036 | rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br); |
1037 | ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br); | |
e41d82f5 RH |
1038 | break; |
1039 | ||
1040 | case PAIR (VARYING, ONLY_IMAG): | |
726a989a RB |
1041 | rr = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi); |
1042 | rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, rr); | |
1043 | ri = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi); | |
e41d82f5 RH |
1044 | break; |
1045 | ||
1046 | case PAIR (VARYING, VARYING): | |
1047 | if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type)) | |
1048 | { | |
726a989a | 1049 | expand_complex_libcall (gsi, ar, ai, br, bi, MULT_EXPR); |
e41d82f5 RH |
1050 | return; |
1051 | } | |
1052 | else | |
1053 | { | |
1054 | tree t1, t2, t3, t4; | |
1055 | ||
726a989a RB |
1056 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br); |
1057 | t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi); | |
1058 | t3 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi); | |
e41d82f5 RH |
1059 | |
1060 | /* Avoid expanding redundant multiplication for the common | |
1061 | case of squaring a complex number. */ | |
1062 | if (ar == br && ai == bi) | |
1063 | t4 = t3; | |
1064 | else | |
726a989a | 1065 | t4 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br); |
e41d82f5 | 1066 | |
726a989a RB |
1067 | rr = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2); |
1068 | ri = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t3, t4); | |
e41d82f5 RH |
1069 | } |
1070 | break; | |
1071 | ||
1072 | default: | |
1073 | gcc_unreachable (); | |
1074 | } | |
6de9cd9a | 1075 | |
726a989a | 1076 | update_complex_assignment (gsi, rr, ri); |
6de9cd9a DN |
1077 | } |
1078 | ||
e3d5405d | 1079 | /* Keep this algorithm in sync with fold-const.c:const_binop(). |
b8698a0f | 1080 | |
e3d5405d | 1081 | Expand complex division to scalars, straightforward algorithm. |
6de9cd9a DN |
1082 | a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t) |
1083 | t = br*br + bi*bi | |
1084 | */ | |
1085 | ||
1086 | static void | |
726a989a | 1087 | expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type, |
6de9cd9a DN |
1088 | tree ar, tree ai, tree br, tree bi, |
1089 | enum tree_code code) | |
1090 | { | |
1091 | tree rr, ri, div, t1, t2, t3; | |
1092 | ||
726a989a RB |
1093 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, br); |
1094 | t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, bi); | |
1095 | div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2); | |
6de9cd9a | 1096 | |
726a989a RB |
1097 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, br); |
1098 | t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, bi); | |
1099 | t3 = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, t2); | |
1100 | rr = gimplify_build2 (gsi, code, inner_type, t3, div); | |
6de9cd9a | 1101 | |
726a989a RB |
1102 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, br); |
1103 | t2 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, bi); | |
1104 | t3 = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, t2); | |
1105 | ri = gimplify_build2 (gsi, code, inner_type, t3, div); | |
6de9cd9a | 1106 | |
726a989a | 1107 | update_complex_assignment (gsi, rr, ri); |
6de9cd9a DN |
1108 | } |
1109 | ||
e3d5405d KG |
1110 | /* Keep this algorithm in sync with fold-const.c:const_binop(). |
1111 | ||
1112 | Expand complex division to scalars, modified algorithm to minimize | |
6de9cd9a DN |
1113 | overflow with wide input ranges. */ |
1114 | ||
1115 | static void | |
726a989a | 1116 | expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type, |
6de9cd9a DN |
1117 | tree ar, tree ai, tree br, tree bi, |
1118 | enum tree_code code) | |
1119 | { | |
04b03edb | 1120 | tree rr, ri, ratio, div, t1, t2, tr, ti, compare; |
c63f5a42 | 1121 | basic_block bb_cond, bb_true, bb_false, bb_join; |
726a989a | 1122 | gimple stmt; |
6de9cd9a DN |
1123 | |
1124 | /* Examine |br| < |bi|, and branch. */ | |
726a989a RB |
1125 | t1 = gimplify_build1 (gsi, ABS_EXPR, inner_type, br); |
1126 | t2 = gimplify_build1 (gsi, ABS_EXPR, inner_type, bi); | |
db3927fb | 1127 | compare = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), |
b57d8e6f | 1128 | LT_EXPR, boolean_type_node, t1, t2); |
04b03edb | 1129 | STRIP_NOPS (compare); |
6de9cd9a | 1130 | |
c63f5a42 RH |
1131 | bb_cond = bb_true = bb_false = bb_join = NULL; |
1132 | rr = ri = tr = ti = NULL; | |
b57d8e6f | 1133 | if (TREE_CODE (compare) != INTEGER_CST) |
6de9cd9a | 1134 | { |
6de9cd9a | 1135 | edge e; |
726a989a | 1136 | gimple stmt; |
04b03edb | 1137 | tree cond, tmp; |
6de9cd9a | 1138 | |
b731b390 | 1139 | tmp = create_tmp_var (boolean_type_node); |
726a989a | 1140 | stmt = gimple_build_assign (tmp, compare); |
04b03edb | 1141 | if (gimple_in_ssa_p (cfun)) |
726a989a | 1142 | { |
b731b390 | 1143 | tmp = make_ssa_name (tmp, stmt); |
726a989a RB |
1144 | gimple_assign_set_lhs (stmt, tmp); |
1145 | } | |
1146 | ||
1147 | gsi_insert_before (gsi, stmt, GSI_SAME_STMT); | |
04b03edb | 1148 | |
db3927fb AH |
1149 | cond = fold_build2_loc (gimple_location (stmt), |
1150 | EQ_EXPR, boolean_type_node, tmp, boolean_true_node); | |
726a989a RB |
1151 | stmt = gimple_build_cond_from_tree (cond, NULL_TREE, NULL_TREE); |
1152 | gsi_insert_before (gsi, stmt, GSI_SAME_STMT); | |
6de9cd9a | 1153 | |
6de9cd9a | 1154 | /* Split the original block, and create the TRUE and FALSE blocks. */ |
726a989a | 1155 | e = split_block (gsi_bb (*gsi), stmt); |
6de9cd9a DN |
1156 | bb_cond = e->src; |
1157 | bb_join = e->dest; | |
1158 | bb_true = create_empty_bb (bb_cond); | |
1159 | bb_false = create_empty_bb (bb_true); | |
1160 | ||
1161 | /* Wire the blocks together. */ | |
1162 | e->flags = EDGE_TRUE_VALUE; | |
1163 | redirect_edge_succ (e, bb_true); | |
1164 | make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE); | |
520f34fa RH |
1165 | make_edge (bb_true, bb_join, EDGE_FALLTHRU); |
1166 | make_edge (bb_false, bb_join, EDGE_FALLTHRU); | |
726338f4 RB |
1167 | add_bb_to_loop (bb_true, bb_cond->loop_father); |
1168 | add_bb_to_loop (bb_false, bb_cond->loop_father); | |
6de9cd9a DN |
1169 | |
1170 | /* Update dominance info. Note that bb_join's data was | |
1171 | updated by split_block. */ | |
fce22de5 | 1172 | if (dom_info_available_p (CDI_DOMINATORS)) |
6de9cd9a DN |
1173 | { |
1174 | set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond); | |
1175 | set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond); | |
1176 | } | |
1177 | ||
b731b390 JJ |
1178 | rr = create_tmp_reg (inner_type); |
1179 | ri = create_tmp_reg (inner_type); | |
6de9cd9a | 1180 | } |
c63f5a42 RH |
1181 | |
1182 | /* In the TRUE branch, we compute | |
1183 | ratio = br/bi; | |
1184 | div = (br * ratio) + bi; | |
1185 | tr = (ar * ratio) + ai; | |
1186 | ti = (ai * ratio) - ar; | |
1187 | tr = tr / div; | |
1188 | ti = ti / div; */ | |
04b03edb | 1189 | if (bb_true || integer_nonzerop (compare)) |
c63f5a42 RH |
1190 | { |
1191 | if (bb_true) | |
1192 | { | |
726a989a RB |
1193 | *gsi = gsi_last_bb (bb_true); |
1194 | gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT); | |
c63f5a42 RH |
1195 | } |
1196 | ||
726a989a | 1197 | ratio = gimplify_build2 (gsi, code, inner_type, br, bi); |
c63f5a42 | 1198 | |
726a989a RB |
1199 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, br, ratio); |
1200 | div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, bi); | |
c63f5a42 | 1201 | |
726a989a RB |
1202 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio); |
1203 | tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ai); | |
c63f5a42 | 1204 | |
726a989a RB |
1205 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio); |
1206 | ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, t1, ar); | |
c63f5a42 | 1207 | |
726a989a RB |
1208 | tr = gimplify_build2 (gsi, code, inner_type, tr, div); |
1209 | ti = gimplify_build2 (gsi, code, inner_type, ti, div); | |
c63f5a42 RH |
1210 | |
1211 | if (bb_true) | |
1212 | { | |
726a989a RB |
1213 | stmt = gimple_build_assign (rr, tr); |
1214 | gsi_insert_before (gsi, stmt, GSI_SAME_STMT); | |
1215 | stmt = gimple_build_assign (ri, ti); | |
1216 | gsi_insert_before (gsi, stmt, GSI_SAME_STMT); | |
1217 | gsi_remove (gsi, true); | |
c63f5a42 RH |
1218 | } |
1219 | } | |
1220 | ||
1221 | /* In the FALSE branch, we compute | |
1222 | ratio = d/c; | |
1223 | divisor = (d * ratio) + c; | |
1224 | tr = (b * ratio) + a; | |
1225 | ti = b - (a * ratio); | |
1226 | tr = tr / div; | |
1227 | ti = ti / div; */ | |
04b03edb | 1228 | if (bb_false || integer_zerop (compare)) |
c63f5a42 RH |
1229 | { |
1230 | if (bb_false) | |
1231 | { | |
726a989a RB |
1232 | *gsi = gsi_last_bb (bb_false); |
1233 | gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT); | |
c63f5a42 RH |
1234 | } |
1235 | ||
726a989a | 1236 | ratio = gimplify_build2 (gsi, code, inner_type, bi, br); |
c63f5a42 | 1237 | |
726a989a RB |
1238 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, bi, ratio); |
1239 | div = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, br); | |
c63f5a42 | 1240 | |
726a989a RB |
1241 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ai, ratio); |
1242 | tr = gimplify_build2 (gsi, PLUS_EXPR, inner_type, t1, ar); | |
c63f5a42 | 1243 | |
726a989a RB |
1244 | t1 = gimplify_build2 (gsi, MULT_EXPR, inner_type, ar, ratio); |
1245 | ti = gimplify_build2 (gsi, MINUS_EXPR, inner_type, ai, t1); | |
c63f5a42 | 1246 | |
726a989a RB |
1247 | tr = gimplify_build2 (gsi, code, inner_type, tr, div); |
1248 | ti = gimplify_build2 (gsi, code, inner_type, ti, div); | |
c63f5a42 RH |
1249 | |
1250 | if (bb_false) | |
1251 | { | |
726a989a RB |
1252 | stmt = gimple_build_assign (rr, tr); |
1253 | gsi_insert_before (gsi, stmt, GSI_SAME_STMT); | |
1254 | stmt = gimple_build_assign (ri, ti); | |
1255 | gsi_insert_before (gsi, stmt, GSI_SAME_STMT); | |
1256 | gsi_remove (gsi, true); | |
c63f5a42 RH |
1257 | } |
1258 | } | |
1259 | ||
1260 | if (bb_join) | |
726a989a | 1261 | *gsi = gsi_start_bb (bb_join); |
c63f5a42 RH |
1262 | else |
1263 | rr = tr, ri = ti; | |
6de9cd9a | 1264 | |
726a989a | 1265 | update_complex_assignment (gsi, rr, ri); |
6de9cd9a DN |
1266 | } |
1267 | ||
1268 | /* Expand complex division to scalars. */ | |
1269 | ||
1270 | static void | |
726a989a | 1271 | expand_complex_division (gimple_stmt_iterator *gsi, tree inner_type, |
6de9cd9a | 1272 | tree ar, tree ai, tree br, tree bi, |
e41d82f5 RH |
1273 | enum tree_code code, |
1274 | complex_lattice_t al, complex_lattice_t bl) | |
6de9cd9a | 1275 | { |
e41d82f5 RH |
1276 | tree rr, ri; |
1277 | ||
1278 | switch (PAIR (al, bl)) | |
6de9cd9a | 1279 | { |
e41d82f5 | 1280 | case PAIR (ONLY_REAL, ONLY_REAL): |
726a989a | 1281 | rr = gimplify_build2 (gsi, code, inner_type, ar, br); |
e41d82f5 | 1282 | ri = ai; |
6de9cd9a | 1283 | break; |
7e7e470f | 1284 | |
e41d82f5 RH |
1285 | case PAIR (ONLY_REAL, ONLY_IMAG): |
1286 | rr = ai; | |
726a989a RB |
1287 | ri = gimplify_build2 (gsi, code, inner_type, ar, bi); |
1288 | ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri); | |
e41d82f5 RH |
1289 | break; |
1290 | ||
1291 | case PAIR (ONLY_IMAG, ONLY_REAL): | |
1292 | rr = ar; | |
726a989a | 1293 | ri = gimplify_build2 (gsi, code, inner_type, ai, br); |
e41d82f5 | 1294 | break; |
7e7e470f | 1295 | |
e41d82f5 | 1296 | case PAIR (ONLY_IMAG, ONLY_IMAG): |
726a989a | 1297 | rr = gimplify_build2 (gsi, code, inner_type, ai, bi); |
e41d82f5 | 1298 | ri = ar; |
6de9cd9a | 1299 | break; |
7e7e470f | 1300 | |
e41d82f5 | 1301 | case PAIR (VARYING, ONLY_REAL): |
726a989a RB |
1302 | rr = gimplify_build2 (gsi, code, inner_type, ar, br); |
1303 | ri = gimplify_build2 (gsi, code, inner_type, ai, br); | |
e41d82f5 RH |
1304 | break; |
1305 | ||
1306 | case PAIR (VARYING, ONLY_IMAG): | |
726a989a RB |
1307 | rr = gimplify_build2 (gsi, code, inner_type, ai, bi); |
1308 | ri = gimplify_build2 (gsi, code, inner_type, ar, bi); | |
1309 | ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ri); | |
e41d82f5 RH |
1310 | |
1311 | case PAIR (ONLY_REAL, VARYING): | |
1312 | case PAIR (ONLY_IMAG, VARYING): | |
1313 | case PAIR (VARYING, VARYING): | |
1314 | switch (flag_complex_method) | |
1315 | { | |
1316 | case 0: | |
1317 | /* straightforward implementation of complex divide acceptable. */ | |
726a989a | 1318 | expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code); |
e41d82f5 RH |
1319 | break; |
1320 | ||
1321 | case 2: | |
1322 | if (SCALAR_FLOAT_TYPE_P (inner_type)) | |
1323 | { | |
726a989a | 1324 | expand_complex_libcall (gsi, ar, ai, br, bi, code); |
e41d82f5 RH |
1325 | break; |
1326 | } | |
1327 | /* FALLTHRU */ | |
1328 | ||
1329 | case 1: | |
1330 | /* wide ranges of inputs must work for complex divide. */ | |
726a989a | 1331 | expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code); |
e41d82f5 RH |
1332 | break; |
1333 | ||
1334 | default: | |
1335 | gcc_unreachable (); | |
1336 | } | |
1337 | return; | |
1338 | ||
6de9cd9a | 1339 | default: |
1e128c5f | 1340 | gcc_unreachable (); |
6de9cd9a | 1341 | } |
e41d82f5 | 1342 | |
726a989a | 1343 | update_complex_assignment (gsi, rr, ri); |
6de9cd9a DN |
1344 | } |
1345 | ||
1346 | /* Expand complex negation to scalars: | |
1347 | -a = (-ar) + i(-ai) | |
1348 | */ | |
1349 | ||
1350 | static void | |
726a989a | 1351 | expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type, |
6de9cd9a DN |
1352 | tree ar, tree ai) |
1353 | { | |
1354 | tree rr, ri; | |
1355 | ||
726a989a RB |
1356 | rr = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ar); |
1357 | ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai); | |
6de9cd9a | 1358 | |
726a989a | 1359 | update_complex_assignment (gsi, rr, ri); |
6de9cd9a DN |
1360 | } |
1361 | ||
1362 | /* Expand complex conjugate to scalars: | |
1363 | ~a = (ar) + i(-ai) | |
1364 | */ | |
1365 | ||
1366 | static void | |
726a989a | 1367 | expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type, |
6de9cd9a DN |
1368 | tree ar, tree ai) |
1369 | { | |
1370 | tree ri; | |
1371 | ||
726a989a | 1372 | ri = gimplify_build1 (gsi, NEGATE_EXPR, inner_type, ai); |
6de9cd9a | 1373 | |
726a989a | 1374 | update_complex_assignment (gsi, ar, ri); |
6de9cd9a DN |
1375 | } |
1376 | ||
1377 | /* Expand complex comparison (EQ or NE only). */ | |
1378 | ||
1379 | static void | |
726a989a | 1380 | expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai, |
6de9cd9a DN |
1381 | tree br, tree bi, enum tree_code code) |
1382 | { | |
726a989a RB |
1383 | tree cr, ci, cc, type; |
1384 | gimple stmt; | |
6de9cd9a | 1385 | |
726a989a RB |
1386 | cr = gimplify_build2 (gsi, code, boolean_type_node, ar, br); |
1387 | ci = gimplify_build2 (gsi, code, boolean_type_node, ai, bi); | |
1388 | cc = gimplify_build2 (gsi, | |
26277d41 PB |
1389 | (code == EQ_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR), |
1390 | boolean_type_node, cr, ci); | |
6de9cd9a | 1391 | |
726a989a | 1392 | stmt = gsi_stmt (*gsi); |
6de9cd9a | 1393 | |
726a989a | 1394 | switch (gimple_code (stmt)) |
6de9cd9a | 1395 | { |
726a989a | 1396 | case GIMPLE_RETURN: |
538dd0b7 DM |
1397 | { |
1398 | greturn *return_stmt = as_a <greturn *> (stmt); | |
1399 | type = TREE_TYPE (gimple_return_retval (return_stmt)); | |
1400 | gimple_return_set_retval (return_stmt, fold_convert (type, cc)); | |
1401 | } | |
6de9cd9a | 1402 | break; |
726a989a RB |
1403 | |
1404 | case GIMPLE_ASSIGN: | |
1405 | type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
1406 | gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc)); | |
1407 | stmt = gsi_stmt (*gsi); | |
6de9cd9a | 1408 | break; |
726a989a RB |
1409 | |
1410 | case GIMPLE_COND: | |
538dd0b7 DM |
1411 | { |
1412 | gcond *cond_stmt = as_a <gcond *> (stmt); | |
1413 | gimple_cond_set_code (cond_stmt, EQ_EXPR); | |
1414 | gimple_cond_set_lhs (cond_stmt, cc); | |
1415 | gimple_cond_set_rhs (cond_stmt, boolean_true_node); | |
1416 | } | |
726a989a RB |
1417 | break; |
1418 | ||
6de9cd9a | 1419 | default: |
1e128c5f | 1420 | gcc_unreachable (); |
6de9cd9a | 1421 | } |
68b9f53b | 1422 | |
e41d82f5 | 1423 | update_stmt (stmt); |
6de9cd9a DN |
1424 | } |
1425 | ||
a57fc743 JJ |
1426 | /* Expand inline asm that sets some complex SSA_NAMEs. */ |
1427 | ||
1428 | static void | |
1429 | expand_complex_asm (gimple_stmt_iterator *gsi) | |
1430 | { | |
538dd0b7 | 1431 | gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi)); |
a57fc743 JJ |
1432 | unsigned int i; |
1433 | ||
1434 | for (i = 0; i < gimple_asm_noutputs (stmt); ++i) | |
1435 | { | |
1436 | tree link = gimple_asm_output_op (stmt, i); | |
1437 | tree op = TREE_VALUE (link); | |
1438 | if (TREE_CODE (op) == SSA_NAME | |
1439 | && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE) | |
1440 | { | |
1441 | tree type = TREE_TYPE (op); | |
1442 | tree inner_type = TREE_TYPE (type); | |
1443 | tree r = build1 (REALPART_EXPR, inner_type, op); | |
1444 | tree i = build1 (IMAGPART_EXPR, inner_type, op); | |
1445 | gimple_seq list = set_component_ssa_name (op, false, r); | |
1446 | ||
1447 | if (list) | |
1448 | gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING); | |
1449 | ||
1450 | list = set_component_ssa_name (op, true, i); | |
1451 | if (list) | |
1452 | gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING); | |
1453 | } | |
1454 | } | |
1455 | } | |
726a989a | 1456 | |
6de9cd9a DN |
1457 | /* Process one statement. If we identify a complex operation, expand it. */ |
1458 | ||
1459 | static void | |
726a989a | 1460 | expand_complex_operations_1 (gimple_stmt_iterator *gsi) |
6de9cd9a | 1461 | { |
726a989a RB |
1462 | gimple stmt = gsi_stmt (*gsi); |
1463 | tree type, inner_type, lhs; | |
6de9cd9a | 1464 | tree ac, ar, ai, bc, br, bi; |
e41d82f5 | 1465 | complex_lattice_t al, bl; |
6de9cd9a DN |
1466 | enum tree_code code; |
1467 | ||
a57fc743 JJ |
1468 | if (gimple_code (stmt) == GIMPLE_ASM) |
1469 | { | |
1470 | expand_complex_asm (gsi); | |
1471 | return; | |
1472 | } | |
1473 | ||
726a989a RB |
1474 | lhs = gimple_get_lhs (stmt); |
1475 | if (!lhs && gimple_code (stmt) != GIMPLE_COND) | |
1476 | return; | |
6de9cd9a | 1477 | |
726a989a RB |
1478 | type = TREE_TYPE (gimple_op (stmt, 0)); |
1479 | code = gimple_expr_code (stmt); | |
6de9cd9a DN |
1480 | |
1481 | /* Initial filter for operations we handle. */ | |
1482 | switch (code) | |
1483 | { | |
1484 | case PLUS_EXPR: | |
1485 | case MINUS_EXPR: | |
1486 | case MULT_EXPR: | |
1487 | case TRUNC_DIV_EXPR: | |
1488 | case CEIL_DIV_EXPR: | |
1489 | case FLOOR_DIV_EXPR: | |
1490 | case ROUND_DIV_EXPR: | |
1491 | case RDIV_EXPR: | |
1492 | case NEGATE_EXPR: | |
1493 | case CONJ_EXPR: | |
1494 | if (TREE_CODE (type) != COMPLEX_TYPE) | |
1495 | return; | |
1496 | inner_type = TREE_TYPE (type); | |
1497 | break; | |
1498 | ||
1499 | case EQ_EXPR: | |
1500 | case NE_EXPR: | |
726a989a | 1501 | /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR |
207156e4 | 1502 | subcode, so we need to access the operands using gimple_op. */ |
726a989a | 1503 | inner_type = TREE_TYPE (gimple_op (stmt, 1)); |
6de9cd9a DN |
1504 | if (TREE_CODE (inner_type) != COMPLEX_TYPE) |
1505 | return; | |
1506 | break; | |
1507 | ||
1508 | default: | |
e41d82f5 | 1509 | { |
726a989a | 1510 | tree rhs; |
a9b77cd1 | 1511 | |
726a989a RB |
1512 | /* GIMPLE_COND may also fallthru here, but we do not need to |
1513 | do anything with it. */ | |
1514 | if (gimple_code (stmt) == GIMPLE_COND) | |
a9b77cd1 ZD |
1515 | return; |
1516 | ||
e41d82f5 | 1517 | if (TREE_CODE (type) == COMPLEX_TYPE) |
726a989a RB |
1518 | expand_complex_move (gsi, type); |
1519 | else if (is_gimple_assign (stmt) | |
1520 | && (gimple_assign_rhs_code (stmt) == REALPART_EXPR | |
1521 | || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR) | |
1522 | && TREE_CODE (lhs) == SSA_NAME) | |
e41d82f5 | 1523 | { |
726a989a RB |
1524 | rhs = gimple_assign_rhs1 (stmt); |
1525 | rhs = extract_component (gsi, TREE_OPERAND (rhs, 0), | |
1526 | gimple_assign_rhs_code (stmt) | |
1527 | == IMAGPART_EXPR, | |
1528 | false); | |
1529 | gimple_assign_set_rhs_from_tree (gsi, rhs); | |
1530 | stmt = gsi_stmt (*gsi); | |
e41d82f5 RH |
1531 | update_stmt (stmt); |
1532 | } | |
1533 | } | |
6de9cd9a DN |
1534 | return; |
1535 | } | |
1536 | ||
1537 | /* Extract the components of the two complex values. Make sure and | |
1538 | handle the common case of the same value used twice specially. */ | |
726a989a RB |
1539 | if (is_gimple_assign (stmt)) |
1540 | { | |
1541 | ac = gimple_assign_rhs1 (stmt); | |
1542 | bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL; | |
1543 | } | |
1544 | /* GIMPLE_CALL can not get here. */ | |
6de9cd9a DN |
1545 | else |
1546 | { | |
726a989a RB |
1547 | ac = gimple_cond_lhs (stmt); |
1548 | bc = gimple_cond_rhs (stmt); | |
1549 | } | |
1550 | ||
1551 | ar = extract_component (gsi, ac, false, true); | |
1552 | ai = extract_component (gsi, ac, true, true); | |
1553 | ||
1554 | if (ac == bc) | |
1555 | br = ar, bi = ai; | |
1556 | else if (bc) | |
1557 | { | |
1558 | br = extract_component (gsi, bc, 0, true); | |
1559 | bi = extract_component (gsi, bc, 1, true); | |
6de9cd9a | 1560 | } |
726a989a RB |
1561 | else |
1562 | br = bi = NULL_TREE; | |
6de9cd9a | 1563 | |
5cd4ec7f | 1564 | if (gimple_in_ssa_p (cfun)) |
e41d82f5 RH |
1565 | { |
1566 | al = find_lattice_value (ac); | |
1567 | if (al == UNINITIALIZED) | |
1568 | al = VARYING; | |
1569 | ||
1570 | if (TREE_CODE_CLASS (code) == tcc_unary) | |
1571 | bl = UNINITIALIZED; | |
1572 | else if (ac == bc) | |
1573 | bl = al; | |
1574 | else | |
1575 | { | |
1576 | bl = find_lattice_value (bc); | |
1577 | if (bl == UNINITIALIZED) | |
1578 | bl = VARYING; | |
1579 | } | |
1580 | } | |
1581 | else | |
1582 | al = bl = VARYING; | |
1583 | ||
6de9cd9a DN |
1584 | switch (code) |
1585 | { | |
1586 | case PLUS_EXPR: | |
1587 | case MINUS_EXPR: | |
726a989a | 1588 | expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl); |
6de9cd9a DN |
1589 | break; |
1590 | ||
1591 | case MULT_EXPR: | |
726a989a | 1592 | expand_complex_multiplication (gsi, inner_type, ar, ai, br, bi, al, bl); |
6de9cd9a DN |
1593 | break; |
1594 | ||
1595 | case TRUNC_DIV_EXPR: | |
1596 | case CEIL_DIV_EXPR: | |
1597 | case FLOOR_DIV_EXPR: | |
1598 | case ROUND_DIV_EXPR: | |
1599 | case RDIV_EXPR: | |
726a989a | 1600 | expand_complex_division (gsi, inner_type, ar, ai, br, bi, code, al, bl); |
6de9cd9a | 1601 | break; |
b8698a0f | 1602 | |
6de9cd9a | 1603 | case NEGATE_EXPR: |
726a989a | 1604 | expand_complex_negation (gsi, inner_type, ar, ai); |
6de9cd9a DN |
1605 | break; |
1606 | ||
1607 | case CONJ_EXPR: | |
726a989a | 1608 | expand_complex_conjugate (gsi, inner_type, ar, ai); |
6de9cd9a DN |
1609 | break; |
1610 | ||
1611 | case EQ_EXPR: | |
1612 | case NE_EXPR: | |
726a989a | 1613 | expand_complex_comparison (gsi, ar, ai, br, bi, code); |
6de9cd9a DN |
1614 | break; |
1615 | ||
1616 | default: | |
1e128c5f | 1617 | gcc_unreachable (); |
6de9cd9a DN |
1618 | } |
1619 | } | |
26277d41 | 1620 | |
e41d82f5 RH |
1621 | \f |
1622 | /* Entry point for complex operation lowering during optimization. */ | |
1623 | ||
c2924966 | 1624 | static unsigned int |
2b725155 | 1625 | tree_lower_complex (void) |
6de9cd9a | 1626 | { |
e41d82f5 | 1627 | int old_last_basic_block; |
726a989a | 1628 | gimple_stmt_iterator gsi; |
6de9cd9a DN |
1629 | basic_block bb; |
1630 | ||
e41d82f5 | 1631 | if (!init_dont_simulate_again ()) |
c2924966 | 1632 | return 0; |
e41d82f5 | 1633 | |
9771b263 DN |
1634 | complex_lattice_values.create (num_ssa_names); |
1635 | complex_lattice_values.safe_grow_cleared (num_ssa_names); | |
e41d82f5 | 1636 | |
95a8c155 | 1637 | init_parameter_lattice_values (); |
e41d82f5 RH |
1638 | ssa_propagate (complex_visit_stmt, complex_visit_phi); |
1639 | ||
c203e8a7 | 1640 | complex_variable_components = new int_tree_htab_type (10); |
95a8c155 | 1641 | |
9771b263 DN |
1642 | complex_ssa_name_components.create (2 * num_ssa_names); |
1643 | complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names); | |
95a8c155 | 1644 | |
e41d82f5 RH |
1645 | update_parameter_components (); |
1646 | ||
95a8c155 | 1647 | /* ??? Ideally we'd traverse the blocks in breadth-first order. */ |
8b1c6fd7 | 1648 | old_last_basic_block = last_basic_block_for_fn (cfun); |
11cd3bed | 1649 | FOR_EACH_BB_FN (bb, cfun) |
6de9cd9a DN |
1650 | { |
1651 | if (bb->index >= old_last_basic_block) | |
1652 | continue; | |
726a989a | 1653 | |
e41d82f5 | 1654 | update_phi_components (bb); |
726a989a RB |
1655 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1656 | expand_complex_operations_1 (&gsi); | |
6de9cd9a | 1657 | } |
6de9cd9a | 1658 | |
726a989a | 1659 | gsi_commit_edge_inserts (); |
e41d82f5 | 1660 | |
c203e8a7 TS |
1661 | delete complex_variable_components; |
1662 | complex_variable_components = NULL; | |
9771b263 DN |
1663 | complex_ssa_name_components.release (); |
1664 | complex_lattice_values.release (); | |
c2924966 | 1665 | return 0; |
e41d82f5 | 1666 | } |
26277d41 | 1667 | |
27a4cd48 DM |
1668 | namespace { |
1669 | ||
1670 | const pass_data pass_data_lower_complex = | |
26277d41 | 1671 | { |
27a4cd48 DM |
1672 | GIMPLE_PASS, /* type */ |
1673 | "cplxlower", /* name */ | |
1674 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
1675 | TV_NONE, /* tv_id */ |
1676 | PROP_ssa, /* properties_required */ | |
1677 | PROP_gimple_lcx, /* properties_provided */ | |
1678 | 0, /* properties_destroyed */ | |
1679 | 0, /* todo_flags_start */ | |
3bea341f | 1680 | TODO_update_ssa, /* todo_flags_finish */ |
e41d82f5 RH |
1681 | }; |
1682 | ||
27a4cd48 DM |
1683 | class pass_lower_complex : public gimple_opt_pass |
1684 | { | |
1685 | public: | |
c3284718 RS |
1686 | pass_lower_complex (gcc::context *ctxt) |
1687 | : gimple_opt_pass (pass_data_lower_complex, ctxt) | |
27a4cd48 DM |
1688 | {} |
1689 | ||
1690 | /* opt_pass methods: */ | |
65d3284b | 1691 | opt_pass * clone () { return new pass_lower_complex (m_ctxt); } |
be55bfe6 | 1692 | virtual unsigned int execute (function *) { return tree_lower_complex (); } |
27a4cd48 DM |
1693 | |
1694 | }; // class pass_lower_complex | |
1695 | ||
1696 | } // anon namespace | |
1697 | ||
1698 | gimple_opt_pass * | |
1699 | make_pass_lower_complex (gcc::context *ctxt) | |
1700 | { | |
1701 | return new pass_lower_complex (ctxt); | |
1702 | } | |
1703 | ||
e41d82f5 | 1704 | \f |
27a4cd48 DM |
1705 | namespace { |
1706 | ||
1707 | const pass_data pass_data_lower_complex_O0 = | |
e41d82f5 | 1708 | { |
27a4cd48 DM |
1709 | GIMPLE_PASS, /* type */ |
1710 | "cplxlower0", /* name */ | |
1711 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
1712 | TV_NONE, /* tv_id */ |
1713 | PROP_cfg, /* properties_required */ | |
1714 | PROP_gimple_lcx, /* properties_provided */ | |
1715 | 0, /* properties_destroyed */ | |
1716 | 0, /* todo_flags_start */ | |
3bea341f | 1717 | TODO_update_ssa, /* todo_flags_finish */ |
6de9cd9a | 1718 | }; |
27a4cd48 DM |
1719 | |
1720 | class pass_lower_complex_O0 : public gimple_opt_pass | |
1721 | { | |
1722 | public: | |
c3284718 RS |
1723 | pass_lower_complex_O0 (gcc::context *ctxt) |
1724 | : gimple_opt_pass (pass_data_lower_complex_O0, ctxt) | |
27a4cd48 DM |
1725 | {} |
1726 | ||
1727 | /* opt_pass methods: */ | |
1a3d085c TS |
1728 | virtual bool gate (function *fun) |
1729 | { | |
1730 | /* With errors, normal optimization passes are not run. If we don't | |
1731 | lower complex operations at all, rtl expansion will abort. */ | |
1732 | return !(fun->curr_properties & PROP_gimple_lcx); | |
1733 | } | |
1734 | ||
be55bfe6 | 1735 | virtual unsigned int execute (function *) { return tree_lower_complex (); } |
27a4cd48 DM |
1736 | |
1737 | }; // class pass_lower_complex_O0 | |
1738 | ||
1739 | } // anon namespace | |
1740 | ||
1741 | gimple_opt_pass * | |
1742 | make_pass_lower_complex_O0 (gcc::context *ctxt) | |
1743 | { | |
1744 | return new pass_lower_complex_O0 (ctxt); | |
1745 | } |