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