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