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