Debian Codegraph @

gcc/tree-complex.cc

raw
1 /* Lower complex number operations to scalar operations.
2 Copyright (C) 2004-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "cfghooks.h"
29 #include "tree-pass.h"
30 #include "ssa.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "tree-eh.h"
34 #include "gimplify.h"
35 #include "gimple-iterator.h"
36 #include "gimplify-me.h"
37 #include "tree-cfg.h"
38 #include "tree-dfa.h"
39 #include "tree-ssa.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-hasher.h"
42 #include "cfgloop.h"
43 #include "cfganal.h"
44 #include "gimple-fold.h"
45 #include "diagnostic-core.h"
46 #include "case-cfn-macros.h"
47 #include "builtins.h"
48 #include "optabs-tree.h"
49 #include "tree-ssa-dce.h"
50
51 /* For each complex ssa name, a lattice value. We're interested in finding
52 out whether a complex number is degenerate in some way, having only real
53 or only complex parts. */
54
55 enum
56 {
57 UNINITIALIZED = 0,
58 ONLY_REAL = 1,
59 ONLY_IMAG = 2,
60 VARYING = 3
61 };
62
63 /* The type complex_lattice_t holds combinations of the above
64 constants. */
65 typedef int complex_lattice_t;
66
67 #define PAIR(a, b) ((a) << 2 | (b))
68
69 class complex_propagate : public ssa_propagation_engine
70 {
71 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) final override;
72 enum ssa_prop_result visit_phi (gphi *) final override;
73 };
74
75 static vec<complex_lattice_t> complex_lattice_values;
76
77 /* For each complex variable, a pair of variables for the components exists in
78 the hashtable. */
79 static int_tree_htab_type *complex_variable_components;
80
81 /* For each complex SSA_NAME, a pair of ssa names for the components. */
82 static vec<tree> complex_ssa_name_components;
83
84 /* Vector of PHI triplets (original complex PHI and corresponding real and
85 imag PHIs if real and/or imag PHIs contain temporarily
86 non-SSA_NAME/non-invariant args that need to be replaced by SSA_NAMEs. */
87 static vec<gphi *> phis_to_revisit;
88
89 /* BBs that need EH cleanup. */
90 static bitmap need_eh_cleanup;
91
92 /* SSA defs we should try to DCE. */
93 static bitmap dce_worklist;
94
95 /* Lookup UID in the complex_variable_components hashtable and return the
96 associated tree. */
97 static tree
98 cvc_lookup (unsigned int uid)
99 {
100 struct int_tree_map in;
101 in.uid = uid;
102 return complex_variable_components->find_with_hash (in, uid).to;
103 }
104
105 /* Insert the pair UID, TO into the complex_variable_components hashtable. */
106
107 static void
108 cvc_insert (unsigned int uid, tree to)
109 {
110 int_tree_map h;
111 int_tree_map *loc;
112
113 h.uid = uid;
114 loc = complex_variable_components->find_slot_with_hash (h, uid, INSERT);
115 loc->uid = uid;
116 loc->to = to;
117 }
118
119 /* Return true if T is not a zero constant. In the case of real values,
120 we're only interested in +0.0. */
121
122 static int
123 some_nonzerop (tree t)
124 {
125 int zerop = false;
126
127 /* Operations with real or imaginary part of a complex number zero
128 cannot be treated the same as operations with a real or imaginary
129 operand if we care about the signs of zeros in the result. */
130 if (TREE_CODE (t) == REAL_CST && !flag_signed_zeros)
131 zerop = real_identical (&TREE_REAL_CST (t), &dconst0);
132 else if (TREE_CODE (t) == FIXED_CST)
133 zerop = fixed_zerop (t);
134 else if (TREE_CODE (t) == INTEGER_CST)
135 zerop = integer_zerop (t);
136
137 return !zerop;
138 }
139
140
141 /* Compute a lattice value from the components of a complex type REAL
142 and IMAG. */
143
144 static complex_lattice_t
145 find_lattice_value_parts (tree real, tree imag)
146 {
147 int r, i;
148 complex_lattice_t ret;
149
150 r = some_nonzerop (real);
151 i = some_nonzerop (imag);
152 ret = r * ONLY_REAL + i * ONLY_IMAG;
153
154 /* ??? On occasion we could do better than mapping 0+0i to real, but we
155 certainly don't want to leave it UNINITIALIZED, which eventually gets
156 mapped to VARYING. */
157 if (ret == UNINITIALIZED)
158 ret = ONLY_REAL;
159
160 return ret;
161 }
162
163
164 /* Compute a lattice value from gimple_val T. */
165
166 static complex_lattice_t
167 find_lattice_value (tree t)
168 {
169 tree real, imag;
170
171 switch (TREE_CODE (t))
172 {
173 case SSA_NAME:
174 return complex_lattice_values[SSA_NAME_VERSION (t)];
175
176 case COMPLEX_CST:
177 real = TREE_REALPART (t);
178 imag = TREE_IMAGPART (t);
179 break;
180
181 default:
182 gcc_unreachable ();
183 }
184
185 return find_lattice_value_parts (real, imag);
186 }
187
188 /* Determine if LHS is something for which we're interested in seeing
189 simulation results. */
190
191 static bool
192 is_complex_reg (tree lhs)
193 {
194 return TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE && is_gimple_reg (lhs);
195 }
196
197 /* Mark the incoming parameters to the function as VARYING. */
198
199 static void
200 init_parameter_lattice_values (void)
201 {
202 tree parm, ssa_name;
203
204 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
205 if (is_complex_reg (parm)
206 && (ssa_name = ssa_default_def (cfun, parm)) != NULL_TREE)
207 complex_lattice_values[SSA_NAME_VERSION (ssa_name)] = VARYING;
208 }
209
210 /* Initialize simulation state for each statement. Return false if we
211 found no statements we want to simulate, and thus there's nothing
212 for the entire pass to do. */
213
214 static bool
215 init_dont_simulate_again (void)
216 {
217 basic_block bb;
218 bool saw_a_complex_op = false;
219
220 FOR_EACH_BB_FN (bb, cfun)
221 {
222 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
223 gsi_next (&gsi))
224 {
225 gphi *phi = gsi.phi ();
226 prop_set_simulate_again (phi,
227 is_complex_reg (gimple_phi_result (phi)));
228 }
229
230 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
231 gsi_next (&gsi))
232 {
233 gimple *stmt;
234 tree op0, op1;
235 bool sim_again_p;
236
237 stmt = gsi_stmt (gsi);
238 op0 = op1 = NULL_TREE;
239
240 /* Most control-altering statements must be initially
241 simulated, else we won't cover the entire cfg. */
242 sim_again_p = stmt_ends_bb_p (stmt);
243
244 switch (gimple_code (stmt))
245 {
246 case GIMPLE_CALL:
247 if (gimple_call_lhs (stmt))
248 {
249 sim_again_p = is_complex_reg (gimple_call_lhs (stmt));
250 switch (gimple_call_combined_fn (stmt))
251 {
252 CASE_CFN_CABS:
253 /* Expand cabs only if unsafe math and optimizing. */
254 if (optimize && flag_unsafe_math_optimizations)
255 saw_a_complex_op = true;
256 break;
257 default:;
258 }
259 }
260 break;
261
262 case GIMPLE_ASSIGN:
263 sim_again_p = is_complex_reg (gimple_assign_lhs (stmt));
264 if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
265 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
266 op0 = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
267 else
268 op0 = gimple_assign_rhs1 (stmt);
269 if (gimple_num_ops (stmt) > 2)
270 op1 = gimple_assign_rhs2 (stmt);
271 break;
272
273 case GIMPLE_COND:
274 op0 = gimple_cond_lhs (stmt);
275 op1 = gimple_cond_rhs (stmt);
276 break;
277
278 default:
279 break;
280 }
281
282 if (op0 || op1)
283 switch (gimple_expr_code (stmt))
284 {
285 case EQ_EXPR:
286 case NE_EXPR:
287 case PLUS_EXPR:
288 case MINUS_EXPR:
289 case MULT_EXPR:
290 case TRUNC_DIV_EXPR:
291 case CEIL_DIV_EXPR:
292 case FLOOR_DIV_EXPR:
293 case ROUND_DIV_EXPR:
294 case RDIV_EXPR:
295 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE
296 || TREE_CODE (TREE_TYPE (op1)) == COMPLEX_TYPE)
297 saw_a_complex_op = true;
298 break;
299
300 case NEGATE_EXPR:
301 case CONJ_EXPR:
302 case PAREN_EXPR:
303 if (TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
304 saw_a_complex_op = true;
305 break;
306
307 case REALPART_EXPR:
308 case IMAGPART_EXPR:
309 /* The total store transformation performed during
310 gimplification creates such uninitialized loads
311 and we need to lower the statement to be able
312 to fix things up. */
313 if (TREE_CODE (op0) == SSA_NAME
314 && ssa_undefined_value_p (op0))
315 saw_a_complex_op = true;
316 break;
317
318 default:
319 /* When expand_complex_move would trigger make sure we
320 perform lowering even when there is no actual complex
321 operation. This helps consistency and vectorization. */
322 if (TREE_CODE (TREE_TYPE (gimple_op (stmt, 0))) == COMPLEX_TYPE)
323 saw_a_complex_op = true;
324 break;
325 }
326
327 prop_set_simulate_again (stmt, sim_again_p);
328 }
329 }
330
331 return saw_a_complex_op;
332 }
333
334
335 /* Evaluate statement STMT against the complex lattice defined above. */
336
337 enum ssa_prop_result
338 complex_propagate::visit_stmt (gimple *stmt, edge *taken_edge_p ATTRIBUTE_UNUSED,
339 tree *result_p)
340 {
341 complex_lattice_t new_l, old_l, op1_l, op2_l;
342 unsigned int ver;
343 tree lhs;
344
345 lhs = gimple_get_lhs (stmt);
346 /* Skip anything but GIMPLE_ASSIGN and GIMPLE_CALL with a lhs. */
347 if (!lhs || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
348 return SSA_PROP_VARYING;
349
350 /* These conditions should be satisfied due to the initial filter
351 set up in init_dont_simulate_again. */
352 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
353 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
354
355 *result_p = lhs;
356 ver = SSA_NAME_VERSION (lhs);
357 old_l = complex_lattice_values[ver];
358
359 switch (gimple_expr_code (stmt))
360 {
361 case SSA_NAME:
362 case COMPLEX_CST:
363 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
364 break;
365
366 case COMPLEX_EXPR:
367 new_l = find_lattice_value_parts (gimple_assign_rhs1 (stmt),
368 gimple_assign_rhs2 (stmt));
369 break;
370
371 case PLUS_EXPR:
372 case MINUS_EXPR:
373 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
374 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
375
376 /* We've set up the lattice values such that IOR neatly
377 models addition. */
378 new_l = op1_l | op2_l;
379 break;
380
381 case MULT_EXPR:
382 case RDIV_EXPR:
383 case TRUNC_DIV_EXPR:
384 case CEIL_DIV_EXPR:
385 case FLOOR_DIV_EXPR:
386 case ROUND_DIV_EXPR:
387 op1_l = find_lattice_value (gimple_assign_rhs1 (stmt));
388 op2_l = find_lattice_value (gimple_assign_rhs2 (stmt));
389
390 /* Obviously, if either varies, so does the result. */
391 if (op1_l == VARYING || op2_l == VARYING)
392 new_l = VARYING;
393 /* Don't prematurely promote variables if we've not yet seen
394 their inputs. */
395 else if (op1_l == UNINITIALIZED)
396 new_l = op2_l;
397 else if (op2_l == UNINITIALIZED)
398 new_l = op1_l;
399 else
400 {
401 /* At this point both numbers have only one component. If the
402 numbers are of opposite kind, the result is imaginary,
403 otherwise the result is real. The add/subtract translates
404 the real/imag from/to 0/1; the ^ performs the comparison. */
405 new_l = ((op1_l - ONLY_REAL) ^ (op2_l - ONLY_REAL)) + ONLY_REAL;
406
407 /* Don't allow the lattice value to flip-flop indefinitely. */
408 new_l |= old_l;
409 }
410 break;
411
412 case NEGATE_EXPR:
413 case PAREN_EXPR:
414 case CONJ_EXPR:
415 new_l = find_lattice_value (gimple_assign_rhs1 (stmt));
416 break;
417
418 default:
419 new_l = VARYING;
420 break;
421 }
422
423 /* If nothing changed this round, let the propagator know. */
424 if (new_l == old_l)
425 return SSA_PROP_NOT_INTERESTING;
426
427 complex_lattice_values[ver] = new_l;
428 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
429 }
430
431 /* Evaluate a PHI node against the complex lattice defined above. */
432
433 enum ssa_prop_result
434 complex_propagate::visit_phi (gphi *phi)
435 {
436 complex_lattice_t new_l, old_l;
437 unsigned int ver;
438 tree lhs;
439 int i;
440
441 lhs = gimple_phi_result (phi);
442
443 /* This condition should be satisfied due to the initial filter
444 set up in init_dont_simulate_again. */
445 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
446
447 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
448 return SSA_PROP_VARYING;
449
450 /* We've set up the lattice values such that IOR neatly models PHI meet. */
451 new_l = UNINITIALIZED;
452 for (i = gimple_phi_num_args (phi) - 1; i >= 0; --i)
453 new_l |= find_lattice_value (gimple_phi_arg_def (phi, i));
454
455 ver = SSA_NAME_VERSION (lhs);
456 old_l = complex_lattice_values[ver];
457
458 if (new_l == old_l)
459 return SSA_PROP_NOT_INTERESTING;
460
461 complex_lattice_values[ver] = new_l;
462 return new_l == VARYING ? SSA_PROP_VARYING : SSA_PROP_INTERESTING;
463 }
464
465 /* Create one backing variable for a complex component of ORIG. */
466
467 static tree
468 create_one_component_var (tree type, tree orig, const char *prefix,
469 const char *suffix, enum tree_code code)
470 {
471 tree r = create_tmp_var (type, prefix);
472
473 DECL_SOURCE_LOCATION (r) = DECL_SOURCE_LOCATION (orig);
474 DECL_ARTIFICIAL (r) = 1;
475
476 if (DECL_NAME (orig) && !DECL_IGNORED_P (orig))
477 {
478 const char *name = IDENTIFIER_POINTER (DECL_NAME (orig));
479 name = ACONCAT ((name, suffix, NULL));
480 DECL_NAME (r) = get_identifier (name);
481
482 SET_DECL_DEBUG_EXPR (r, build1 (code, type, orig));
483 DECL_HAS_DEBUG_EXPR_P (r) = 1;
484 DECL_IGNORED_P (r) = 0;
485 copy_warning (r, orig);
486 }
487 else
488 {
489 DECL_IGNORED_P (r) = 1;
490 suppress_warning (r);
491 }
492
493 return r;
494 }
495
496 /* Retrieve a value for a complex component of VAR. */
497
498 static tree
499 get_component_var (tree var, bool imag_p)
500 {
501 size_t decl_index = DECL_UID (var) * 2 + imag_p;
502 tree ret = cvc_lookup (decl_index);
503
504 if (ret == NULL)
505 {
506 ret = create_one_component_var (TREE_TYPE (TREE_TYPE (var)), var,
507 imag_p ? "CI" : "CR",
508 imag_p ? "$imag" : "$real",
509 imag_p ? IMAGPART_EXPR : REALPART_EXPR);
510 cvc_insert (decl_index, ret);
511 }
512
513 return ret;
514 }
515
516 /* Retrieve a value for a complex component of SSA_NAME. */
517
518 static tree
519 get_component_ssa_name (tree ssa_name, bool imag_p)
520 {
521 complex_lattice_t lattice = find_lattice_value (ssa_name);
522 size_t ssa_name_index;
523 tree ret;
524
525 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
526 {
527 tree inner_type = TREE_TYPE (TREE_TYPE (ssa_name));
528 if (SCALAR_FLOAT_TYPE_P (inner_type))
529 return build_real (inner_type, dconst0);
530 else
531 return build_int_cst (inner_type, 0);
532 }
533
534 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
535 ret = complex_ssa_name_components[ssa_name_index];
536 if (ret == NULL)
537 {
538 if (SSA_NAME_VAR (ssa_name))
539 ret = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
540 else
541 ret = TREE_TYPE (TREE_TYPE (ssa_name));
542 ret = make_ssa_name (ret);
543
544 /* Copy some properties from the original. In particular, whether it
545 is used in an abnormal phi, and whether it's uninitialized. */
546 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ret)
547 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name);
548 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
549 && VAR_P (SSA_NAME_VAR (ssa_name)))
550 {
551 SSA_NAME_DEF_STMT (ret) = SSA_NAME_DEF_STMT (ssa_name);
552 set_ssa_default_def (cfun, SSA_NAME_VAR (ret), ret);
553 }
554
555 complex_ssa_name_components[ssa_name_index] = ret;
556 }
557
558 return ret;
559 }
560
561 /* Set a value for a complex component of SSA_NAME, return a
562 gimple_seq of stuff that needs doing. */
563
564 static gimple_seq
565 set_component_ssa_name (tree ssa_name, bool imag_p, tree value)
566 {
567 complex_lattice_t lattice = find_lattice_value (ssa_name);
568 size_t ssa_name_index;
569 tree comp;
570 gimple *last;
571 gimple_seq list;
572
573 /* We know the value must be zero, else there's a bug in our lattice
574 analysis. But the value may well be a variable known to contain
575 zero. We should be safe ignoring it. */
576 if (lattice == (imag_p ? ONLY_REAL : ONLY_IMAG))
577 return NULL;
578
579 /* If we've already assigned an SSA_NAME to this component, then this
580 means that our walk of the basic blocks found a use before the set.
581 This is fine. Now we should create an initialization for the value
582 we created earlier. */
583 ssa_name_index = SSA_NAME_VERSION (ssa_name) * 2 + imag_p;
584 comp = complex_ssa_name_components[ssa_name_index];
585 if (comp)
586 ;
587
588 /* If we've nothing assigned, and the value we're given is already stable,
589 then install that as the value for this SSA_NAME. This preemptively
590 copy-propagates the value, which avoids unnecessary memory allocation. */
591 else if (is_gimple_min_invariant (value)
592 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
593 {
594 complex_ssa_name_components[ssa_name_index] = value;
595 return NULL;
596 }
597 else if (TREE_CODE (value) == SSA_NAME
598 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
599 {
600 /* Replace an anonymous base value with the variable from cvc_lookup.
601 This should result in better debug info. */
602 if (!SSA_NAME_IS_DEFAULT_DEF (value)
603 && SSA_NAME_VAR (ssa_name)
604 && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
605 && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
606 {
607 comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
608 replace_ssa_name_symbol (value, comp);
609 }
610
611 complex_ssa_name_components[ssa_name_index] = value;
612 return NULL;
613 }
614
615 /* Finally, we need to stabilize the result by installing the value into
616 a new ssa name. */
617 else
618 comp = get_component_ssa_name (ssa_name, imag_p);
619
620 /* Do all the work to assign VALUE to COMP. */
621 list = NULL;
622 value = force_gimple_operand (value, &list, false, NULL);
623 last = gimple_build_assign (comp, value);
624 gimple_seq_add_stmt (&list, last);
625 gcc_assert (SSA_NAME_DEF_STMT (comp) == last);
626
627 return list;
628 }
629
630 /* Extract the real or imaginary part of a complex variable or constant.
631 Make sure that it's a proper gimple_val and gimplify it if not.
632 Emit any new code before gsi. */
633
634 static tree
635 extract_component (gimple_stmt_iterator *gsi, tree t, bool imagpart_p,
636 bool gimple_p, bool phiarg_p = false)
637 {
638 switch (TREE_CODE (t))
639 {
640 case COMPLEX_CST:
641 return imagpart_p ? TREE_IMAGPART (t) : TREE_REALPART (t);
642
643 case COMPLEX_EXPR:
644 gcc_unreachable ();
645
646 case BIT_FIELD_REF:
647 {
648 tree inner_type = TREE_TYPE (TREE_TYPE (t));
649 t = unshare_expr (t);
650 TREE_TYPE (t) = inner_type;
651 TREE_OPERAND (t, 1) = TYPE_SIZE (inner_type);
652 if (imagpart_p)
653 TREE_OPERAND (t, 2) = size_binop (PLUS_EXPR, TREE_OPERAND (t, 2),
654 TYPE_SIZE (inner_type));
655
656 if (gimple_p)
657 {
658 tree new_lhs = make_ssa_name (inner_type);
659 gimple *new_load = gimple_build_assign (new_lhs, t);
660 gsi_insert_before (gsi, new_load, GSI_SAME_STMT);
661 t = new_lhs;
662 }
663 return t;
664 }
665
666 case VIEW_CONVERT_EXPR:
667 /* Getting the real/imag parts of a VCE of a ssa-name
668 (or gimple invariant) requires to place the complex
669 into a ssa name before getting the 2 parts.
670 As `IMAGPART_EXPR<VIEW_CONVERT_EXPR<a_BN>>` is an invalid
671 gimple. This will only show up when gimplifying it.
672 Note this creates an extra copy. The call to
673 force_gimple_operand_gsi would create one too. */
674 tree expr;
675 expr = TREE_OPERAND (t, 0);
676 if (TREE_CODE (expr) == SSA_NAME
677 || (is_gimple_min_invariant (expr)
678 && TREE_CODE (expr) != STRING_CST))
679 {
680 gcc_assert (gimple_p);
681 tree new_cplx = make_ssa_name (TREE_TYPE (t));
682 gimple *vce = gimple_build_assign (new_cplx, unshare_expr (t));
683 gsi_insert_before (gsi, vce, GSI_SAME_STMT);
684
685 tree new_lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (t)));
686 t = build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
687 TREE_TYPE (TREE_TYPE (t)), new_cplx);
688 gimple *new_ri = gimple_build_assign (new_lhs, t);
689 gsi_insert_before (gsi, new_ri, GSI_SAME_STMT);
690 t = new_lhs;
691 return t;
692 }
693 /* FALLTHRU */
694 case VAR_DECL:
695 case RESULT_DECL:
696 case PARM_DECL:
697 case COMPONENT_REF:
698 case ARRAY_REF:
699 case MEM_REF:
700 {
701 tree inner_type = TREE_TYPE (TREE_TYPE (t));
702
703 t = fold_build1 ((imagpart_p ? IMAGPART_EXPR : REALPART_EXPR),
704 inner_type, unshare_expr (t));
705
706 if (gimple_p)
707 {
708 tree new_lhs = make_ssa_name (inner_type);
709 gimple *new_load = gimple_build_assign (new_lhs, t);
710 gsi_insert_before (gsi, new_load, GSI_SAME_STMT);
711 t = new_lhs;
712 }
713
714 return t;
715 }
716
717 case SSA_NAME:
718 t = get_component_ssa_name (t, imagpart_p);
719 if (TREE_CODE (t) == SSA_NAME && SSA_NAME_DEF_STMT (t) == NULL)
720 gcc_assert (phiarg_p);
721 return t;
722
723 default:
724 gcc_unreachable ();
725 }
726 }
727
728 /* Update the complex components of the ssa name on the lhs of STMT. */
729
730 static void
731 update_complex_components (gimple_stmt_iterator *gsi, gimple *stmt, tree r,
732 tree i)
733 {
734 tree lhs;
735 gimple_seq list;
736
737 lhs = gimple_get_lhs (stmt);
738
739 list = set_component_ssa_name (lhs, false, r);
740 if (list)
741 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
742
743 list = set_component_ssa_name (lhs, true, i);
744 if (list)
745 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
746 }
747
748 static void
749 update_complex_components_on_edge (edge e, tree lhs, tree r, tree i)
750 {
751 gimple_seq list;
752
753 list = set_component_ssa_name (lhs, false, r);
754 if (list)
755 gsi_insert_seq_on_edge (e, list);
756
757 list = set_component_ssa_name (lhs, true, i);
758 if (list)
759 gsi_insert_seq_on_edge (e, list);
760 }
761
762
763 /* Update an assignment to a complex variable in place. */
764
765 static void
766 update_complex_assignment (gimple_stmt_iterator *gsi, tree r, tree i)
767 {
768 gimple *old_stmt = gsi_stmt (*gsi);
769 gimple_assign_set_rhs_with_ops (gsi, COMPLEX_EXPR, r, i);
770 gimple *stmt = gsi_stmt (*gsi);
771 update_stmt (stmt);
772 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
773 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
774 if (optimize)
775 bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (gimple_assign_lhs (stmt)));
776
777 update_complex_components (gsi, gsi_stmt (*gsi), r, i);
778 }
779
780
781 /* Generate code at the entry point of the function to initialize the
782 component variables for a complex parameter. */
783
784 static void
785 update_parameter_components (void)
786 {
787 edge entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
788 tree parm;
789
790 for (parm = DECL_ARGUMENTS (cfun->decl); parm ; parm = DECL_CHAIN (parm))
791 {
792 tree type = TREE_TYPE (parm);
793 tree ssa_name, r, i;
794
795 if (TREE_CODE (type) != COMPLEX_TYPE || !is_gimple_reg (parm))
796 continue;
797
798 type = TREE_TYPE (type);
799 ssa_name = ssa_default_def (cfun, parm);
800 if (!ssa_name)
801 continue;
802
803 r = build1 (REALPART_EXPR, type, ssa_name);
804 i = build1 (IMAGPART_EXPR, type, ssa_name);
805 update_complex_components_on_edge (entry_edge, ssa_name, r, i);
806 }
807 }
808
809 /* Generate code to set the component variables of a complex variable
810 to match the PHI statements in block BB. */
811
812 static void
813 update_phi_components (basic_block bb)
814 {
815 gphi_iterator gsi;
816
817 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
818 {
819 gphi *phi = gsi.phi ();
820
821 if (is_complex_reg (gimple_phi_result (phi)))
822 {
823 gphi *p[2] = { NULL, NULL };
824 unsigned int i, j, n;
825 bool revisit_phi = false;
826
827 for (j = 0; j < 2; j++)
828 {
829 tree l = get_component_ssa_name (gimple_phi_result (phi), j > 0);
830 if (TREE_CODE (l) == SSA_NAME)
831 p[j] = create_phi_node (l, bb);
832 }
833
834 for (i = 0, n = gimple_phi_num_args (phi); i < n; ++i)
835 {
836 tree comp, arg = gimple_phi_arg_def (phi, i);
837 for (j = 0; j < 2; j++)
838 if (p[j])
839 {
840 comp = extract_component (NULL, arg, j > 0, false, true);
841 if (TREE_CODE (comp) == SSA_NAME
842 && SSA_NAME_DEF_STMT (comp) == NULL)
843 {
844 /* For the benefit of any gimple simplification during
845 this pass that might walk SSA_NAME def stmts,
846 don't add SSA_NAMEs without definitions into the
847 PHI arguments, but put a decl in there instead
848 temporarily, and revisit this PHI later on. */
849 if (SSA_NAME_VAR (comp))
850 comp = SSA_NAME_VAR (comp);
851 else
852 comp = create_tmp_reg (TREE_TYPE (comp),
853 get_name (comp));
854 revisit_phi = true;
855 }
856 SET_PHI_ARG_DEF (p[j], i, comp);
857 }
858 }
859
860 if (revisit_phi)
861 {
862 phis_to_revisit.safe_push (phi);
863 phis_to_revisit.safe_push (p[0]);
864 phis_to_revisit.safe_push (p[1]);
865 }
866 }
867 }
868 }
869
870 /* Expand a complex move to scalars. */
871
872 static void
873 expand_complex_move (gimple_stmt_iterator *gsi, tree type)
874 {
875 tree inner_type = TREE_TYPE (type);
876 tree r, i, lhs, rhs;
877 gimple *stmt = gsi_stmt (*gsi);
878
879 if (is_gimple_assign (stmt))
880 {
881 lhs = gimple_assign_lhs (stmt);
882 if (gimple_num_ops (stmt) == 2)
883 rhs = gimple_assign_rhs1 (stmt);
884 else
885 rhs = NULL_TREE;
886 }
887 else if (is_gimple_call (stmt))
888 {
889 lhs = gimple_call_lhs (stmt);
890 rhs = NULL_TREE;
891 }
892 else
893 gcc_unreachable ();
894
895 if (TREE_CODE (lhs) == SSA_NAME)
896 {
897 if (is_ctrl_altering_stmt (stmt))
898 {
899 edge e;
900
901 /* The value is not assigned on the exception edges, so we need not
902 concern ourselves there. We do need to update on the fallthru
903 edge. Find it. */
904 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
905 if (!e)
906 gcc_unreachable ();
907
908 r = build1 (REALPART_EXPR, inner_type, lhs);
909 i = build1 (IMAGPART_EXPR, inner_type, lhs);
910 update_complex_components_on_edge (e, lhs, r, i);
911 }
912 else if (is_gimple_call (stmt)
913 || gimple_has_side_effects (stmt))
914 {
915 r = build1 (REALPART_EXPR, inner_type, lhs);
916 i = build1 (IMAGPART_EXPR, inner_type, lhs);
917 update_complex_components (gsi, stmt, r, i);
918 }
919 else
920 {
921 if (gimple_assign_rhs_code (stmt) != COMPLEX_EXPR)
922 {
923 r = extract_component (gsi, rhs, 0, true);
924 i = extract_component (gsi, rhs, 1, true);
925 }
926 else
927 {
928 r = gimple_assign_rhs1 (stmt);
929 i = gimple_assign_rhs2 (stmt);
930 }
931 update_complex_assignment (gsi, r, i);
932 }
933 }
934 else if (rhs
935 && (TREE_CODE (rhs) == SSA_NAME || TREE_CODE (rhs) == COMPLEX_CST)
936 && !TREE_SIDE_EFFECTS (lhs))
937 {
938 tree x;
939 gimple *t;
940 location_t loc;
941
942 loc = gimple_location (stmt);
943 r = extract_component (gsi, rhs, 0, false);
944 i = extract_component (gsi, rhs, 1, false);
945
946 x = build1 (REALPART_EXPR, inner_type, unshare_expr (lhs));
947 t = gimple_build_assign (x, r);
948 gimple_set_location (t, loc);
949 gsi_insert_before (gsi, t, GSI_SAME_STMT);
950
951 if (stmt == gsi_stmt (*gsi))
952 {
953 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
954 gimple_assign_set_lhs (stmt, x);
955 gimple_assign_set_rhs1 (stmt, i);
956 }
957 else
958 {
959 x = build1 (IMAGPART_EXPR, inner_type, unshare_expr (lhs));
960 t = gimple_build_assign (x, i);
961 gimple_set_location (t, loc);
962 gsi_insert_before (gsi, t, GSI_SAME_STMT);
963
964 stmt = gsi_stmt (*gsi);
965 gcc_assert (gimple_code (stmt) == GIMPLE_RETURN);
966 gimple_return_set_retval (as_a <greturn *> (stmt), lhs);
967 }
968
969 update_stmt (stmt);
970 }
971 }
972
973 /* Expand complex addition to scalars:
974 a + b = (ar + br) + i(ai + bi)
975 a - b = (ar - br) + i(ai + bi)
976 */
977
978 static void
979 expand_complex_addition (gimple_stmt_iterator *gsi, tree inner_type,
980 tree ar, tree ai, tree br, tree bi,
981 enum tree_code code,
982 complex_lattice_t al, complex_lattice_t bl)
983 {
984 tree rr, ri;
985 gimple_seq stmts = NULL;
986 location_t loc = gimple_location (gsi_stmt (*gsi));
987
988 switch (PAIR (al, bl))
989 {
990 case PAIR (ONLY_REAL, ONLY_REAL):
991 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
992 ri = ai;
993 break;
994
995 case PAIR (ONLY_REAL, ONLY_IMAG):
996 rr = ar;
997 if (code == MINUS_EXPR)
998 ri = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, ai, bi);
999 else
1000 ri = bi;
1001 break;
1002
1003 case PAIR (ONLY_IMAG, ONLY_REAL):
1004 if (code == MINUS_EXPR)
1005 rr = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, ar, br);
1006 else
1007 rr = br;
1008 ri = ai;
1009 break;
1010
1011 case PAIR (ONLY_IMAG, ONLY_IMAG):
1012 rr = ar;
1013 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1014 break;
1015
1016 case PAIR (VARYING, ONLY_REAL):
1017 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1018 ri = ai;
1019 break;
1020
1021 case PAIR (VARYING, ONLY_IMAG):
1022 rr = ar;
1023 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1024 break;
1025
1026 case PAIR (ONLY_REAL, VARYING):
1027 if (code == MINUS_EXPR)
1028 goto general;
1029 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1030 ri = bi;
1031 break;
1032
1033 case PAIR (ONLY_IMAG, VARYING):
1034 if (code == MINUS_EXPR)
1035 goto general;
1036 rr = br;
1037 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1038 break;
1039
1040 case PAIR (VARYING, VARYING):
1041 general:
1042 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1043 /* (a+ai) + (b+bi) -> (a+b)+(a+b)i
1044 small optimization to remove one new statement. */
1045 if (operand_equal_p (ar, ai) && operand_equal_p (br, bi))
1046 ri = rr;
1047 else
1048 ri = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1049 break;
1050
1051 default:
1052 gcc_unreachable ();
1053 }
1054
1055 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1056 update_complex_assignment (gsi, rr, ri);
1057 }
1058
1059 /* Expand a complex multiplication or division to a libcall to the c99
1060 compliant routines. TYPE is the complex type of the operation.
1061 If INPLACE_P replace the statement at GSI with
1062 the libcall and return NULL_TREE. Else insert the call, assign its
1063 result to an output variable and return that variable. If INPLACE_P
1064 is true then the statement being replaced should be an assignment
1065 statement. */
1066
1067 static tree
1068 expand_complex_libcall (gimple_stmt_iterator *gsi, tree type, tree ar, tree ai,
1069 tree br, tree bi, enum tree_code code, bool inplace_p)
1070 {
1071 machine_mode mode;
1072 enum built_in_function bcode;
1073 tree fn, lhs;
1074 gcall *stmt;
1075
1076 mode = TYPE_MODE (type);
1077 gcc_assert (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT);
1078
1079 if (code == MULT_EXPR)
1080 bcode = ((enum built_in_function)
1081 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
1082 else if (code == RDIV_EXPR)
1083 bcode = ((enum built_in_function)
1084 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
1085 else
1086 gcc_unreachable ();
1087 fn = builtin_decl_explicit (bcode);
1088 stmt = gimple_build_call (fn, 4, ar, ai, br, bi);
1089
1090 if (inplace_p)
1091 {
1092 gimple *old_stmt = gsi_stmt (*gsi);
1093 gimple_call_set_nothrow (stmt, !stmt_could_throw_p (cfun, old_stmt));
1094 lhs = gimple_assign_lhs (old_stmt);
1095 gimple_call_set_lhs (stmt, lhs);
1096 gsi_replace (gsi, stmt, true);
1097
1098 type = TREE_TYPE (type);
1099 if (stmt_can_throw_internal (cfun, stmt))
1100 {
1101 edge_iterator ei;
1102 edge e;
1103 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1104 if (!(e->flags & EDGE_EH))
1105 break;
1106 basic_block bb = split_edge (e);
1107 gimple_stmt_iterator gsi2 = gsi_start_bb (bb);
1108 update_complex_components (&gsi2, stmt,
1109 build1 (REALPART_EXPR, type, lhs),
1110 build1 (IMAGPART_EXPR, type, lhs));
1111 return NULL_TREE;
1112 }
1113 else
1114 update_complex_components (gsi, stmt,
1115 build1 (REALPART_EXPR, type, lhs),
1116 build1 (IMAGPART_EXPR, type, lhs));
1117 SSA_NAME_DEF_STMT (lhs) = stmt;
1118 return NULL_TREE;
1119 }
1120
1121 gimple_call_set_nothrow (stmt, true);
1122 lhs = make_ssa_name (type);
1123 gimple_call_set_lhs (stmt, lhs);
1124 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1125
1126 return lhs;
1127 }
1128
1129 /* Perform a complex multiplication on two complex constants A, B represented
1130 by AR, AI, BR, BI of type TYPE.
1131 The operation we want is: a * b = (ar*br - ai*bi) + i(ar*bi + br*ai).
1132 Insert the GIMPLE statements into GSI. Store the real and imaginary
1133 components of the result into RR and RI. */
1134
1135 static void
1136 expand_complex_multiplication_components (gimple_seq *stmts, location_t loc,
1137 tree type, tree ar, tree ai,
1138 tree br, tree bi,
1139 tree *rr, tree *ri)
1140 {
1141 tree t1, t2, t3, t4;
1142
1143 t1 = gimple_build (stmts, loc, MULT_EXPR, type, ar, br);
1144 t2 = gimple_build (stmts, loc, MULT_EXPR, type, ai, bi);
1145 t3 = gimple_build (stmts, loc, MULT_EXPR, type, ar, bi);
1146
1147 /* Avoid expanding redundant multiplication for the common
1148 case of squaring a complex number. */
1149 if (ar == br && ai == bi)
1150 t4 = t3;
1151 else
1152 t4 = gimple_build (stmts, loc, MULT_EXPR, type, ai, br);
1153
1154 *rr = gimple_build (stmts, loc, MINUS_EXPR, type, t1, t2);
1155 *ri = gimple_build (stmts, loc, PLUS_EXPR, type, t3, t4);
1156 }
1157
1158 /* Expand complex multiplication to scalars:
1159 a * b = (ar*br - ai*bi) + i(ar*bi + br*ai)
1160 */
1161
1162 static void
1163 expand_complex_multiplication (gimple_stmt_iterator *gsi, tree type,
1164 tree ar, tree ai, tree br, tree bi,
1165 complex_lattice_t al, complex_lattice_t bl)
1166 {
1167 tree rr, ri;
1168 tree inner_type = TREE_TYPE (type);
1169 location_t loc = gimple_location (gsi_stmt (*gsi));
1170 gimple_seq stmts = NULL;
1171
1172 if (al < bl)
1173 {
1174 complex_lattice_t tl;
1175 rr = ar, ar = br, br = rr;
1176 ri = ai, ai = bi, bi = ri;
1177 tl = al, al = bl, bl = tl;
1178 }
1179
1180 switch (PAIR (al, bl))
1181 {
1182 case PAIR (ONLY_REAL, ONLY_REAL):
1183 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, br);
1184 ri = ai;
1185 break;
1186
1187 case PAIR (ONLY_IMAG, ONLY_REAL):
1188 rr = ar;
1189 if (TREE_CODE (ai) == REAL_CST
1190 && real_identical (&TREE_REAL_CST (ai), &dconst1))
1191 ri = br;
1192 else
1193 ri = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, br);
1194 break;
1195
1196 case PAIR (ONLY_IMAG, ONLY_IMAG):
1197 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, bi);
1198 rr = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, rr);
1199 ri = ar;
1200 break;
1201
1202 case PAIR (VARYING, ONLY_REAL):
1203 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, br);
1204 ri = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, br);
1205 break;
1206
1207 case PAIR (VARYING, ONLY_IMAG):
1208 rr = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, bi);
1209 rr = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, rr);
1210 ri = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, bi);
1211 break;
1212
1213 case PAIR (VARYING, VARYING):
1214 if (flag_complex_method == 2 && SCALAR_FLOAT_TYPE_P (inner_type))
1215 {
1216 /* If optimizing for size or not at all just do a libcall.
1217 Same if there are exception-handling edges or signaling NaNs. */
1218 if (optimize == 0 || optimize_bb_for_size_p (gsi_bb (*gsi))
1219 || stmt_can_throw_internal (cfun, gsi_stmt (*gsi))
1220 || flag_signaling_nans)
1221 {
1222 expand_complex_libcall (gsi, type, ar, ai, br, bi,
1223 MULT_EXPR, true);
1224 return;
1225 }
1226
1227 if (!HONOR_NANS (inner_type))
1228 {
1229 /* If we are not worrying about NaNs expand to
1230 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1231 expand_complex_multiplication_components (&stmts, loc, inner_type,
1232 ar, ai, br, bi,
1233 &rr, &ri);
1234 break;
1235 }
1236
1237 /* Else, expand x = a * b into
1238 x = (ar*br - ai*bi) + i(ar*bi + br*ai);
1239 if (isunordered (__real__ x, __imag__ x))
1240 x = __muldc3 (a, b); */
1241
1242 tree tmpr, tmpi;
1243 expand_complex_multiplication_components (&stmts, loc,
1244 inner_type, ar, ai,
1245 br, bi, &tmpr, &tmpi);
1246 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1247 stmts = NULL;
1248
1249 gimple *check
1250 = gimple_build_cond (UNORDERED_EXPR, tmpr, tmpi,
1251 NULL_TREE, NULL_TREE);
1252
1253 basic_block orig_bb = gsi_bb (*gsi);
1254 /* We want to keep track of the original complex multiplication
1255 statement as we're going to modify it later in
1256 update_complex_assignment. Make sure that insert_cond_bb leaves
1257 that statement in the join block. */
1258 gsi_prev (gsi);
1259 basic_block cond_bb
1260 = insert_cond_bb (gsi_bb (*gsi), gsi_stmt (*gsi), check,
1261 profile_probability::very_unlikely ());
1262
1263 gimple_stmt_iterator cond_bb_gsi = gsi_last_bb (cond_bb);
1264 gsi_insert_after (&cond_bb_gsi, gimple_build_nop (), GSI_NEW_STMT);
1265
1266 tree libcall_res
1267 = expand_complex_libcall (&cond_bb_gsi, type, ar, ai, br,
1268 bi, MULT_EXPR, false);
1269 gimple_seq stmts2 = NULL;
1270 tree cond_real = gimple_build (&stmts2, loc, REALPART_EXPR,
1271 inner_type, libcall_res);
1272 tree cond_imag = gimple_build (&stmts2, loc, IMAGPART_EXPR,
1273 inner_type, libcall_res);
1274 gsi_insert_seq_before (&cond_bb_gsi, stmts2, GSI_SAME_STMT);
1275
1276 basic_block join_bb = single_succ_edge (cond_bb)->dest;
1277 *gsi = gsi_start_nondebug_after_labels_bb (join_bb);
1278
1279 /* We have a conditional block with some assignments in cond_bb.
1280 Wire up the PHIs to wrap up. */
1281 rr = make_ssa_name (inner_type);
1282 ri = make_ssa_name (inner_type);
1283 edge cond_to_join = single_succ_edge (cond_bb);
1284 edge orig_to_join = find_edge (orig_bb, join_bb);
1285
1286 gphi *real_phi = create_phi_node (rr, gsi_bb (*gsi));
1287 add_phi_arg (real_phi, cond_real, cond_to_join, UNKNOWN_LOCATION);
1288 add_phi_arg (real_phi, tmpr, orig_to_join, UNKNOWN_LOCATION);
1289
1290 gphi *imag_phi = create_phi_node (ri, gsi_bb (*gsi));
1291 add_phi_arg (imag_phi, cond_imag, cond_to_join, UNKNOWN_LOCATION);
1292 add_phi_arg (imag_phi, tmpi, orig_to_join, UNKNOWN_LOCATION);
1293 }
1294 else
1295 /* If we are not worrying about NaNs expand to
1296 (ar*br - ai*bi) + i(ar*bi + br*ai) directly. */
1297 expand_complex_multiplication_components (&stmts, loc,
1298 inner_type, ar, ai,
1299 br, bi, &rr, &ri);
1300 break;
1301
1302 default:
1303 gcc_unreachable ();
1304 }
1305
1306 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1307 update_complex_assignment (gsi, rr, ri);
1308 }
1309
1310 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1311
1312 Expand complex division to scalars, straightforward algorithm.
1313 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1314 t = br*br + bi*bi
1315 */
1316
1317 static void
1318 expand_complex_div_straight (gimple_stmt_iterator *gsi, tree inner_type,
1319 tree ar, tree ai, tree br, tree bi,
1320 enum tree_code code)
1321 {
1322 gimple_seq stmts = NULL;
1323 location_t loc = gimple_location (gsi_stmt (*gsi));
1324 tree rr, ri, div, t1, t2, t3;
1325
1326 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, br, br);
1327 t2 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, bi, bi);
1328 div = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, t2);
1329
1330 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, br);
1331 t2 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, bi);
1332 t3 = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, t2);
1333 rr = gimple_build (&stmts, loc, code, inner_type, t3, div);
1334
1335 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, br);
1336 t2 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, bi);
1337 t3 = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, t1, t2);
1338 ri = gimple_build (&stmts, loc, code, inner_type, t3, div);
1339
1340 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1341 update_complex_assignment (gsi, rr, ri);
1342 }
1343
1344 /* Keep this algorithm in sync with fold-const.cc:const_binop().
1345
1346 Expand complex division to scalars, modified algorithm to minimize
1347 overflow with wide input ranges. */
1348
1349 static void
1350 expand_complex_div_wide (gimple_stmt_iterator *gsi, tree inner_type,
1351 tree ar, tree ai, tree br, tree bi,
1352 enum tree_code code)
1353 {
1354 tree rr, ri, ratio, div, t1, t2, tr, ti, compare;
1355 basic_block bb_cond, bb_true, bb_false, bb_join;
1356 gimple *stmt;
1357 gimple_seq stmts = NULL;
1358 location_t loc = gimple_location (gsi_stmt (*gsi));
1359
1360 /* Examine |br| < |bi|, and branch. */
1361 t1 = gimple_build (&stmts, loc, ABS_EXPR, inner_type, br);
1362 t2 = gimple_build (&stmts, loc, ABS_EXPR, inner_type, bi);
1363 compare = gimple_build (&stmts, loc,
1364 LT_EXPR, boolean_type_node, t1, t2);
1365
1366 bb_cond = bb_true = bb_false = bb_join = NULL;
1367 rr = ri = tr = ti = NULL;
1368 if (TREE_CODE (compare) != INTEGER_CST)
1369 {
1370 edge e;
1371 gimple *stmt;
1372
1373 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1374 stmts = NULL;
1375 stmt = gimple_build_cond (NE_EXPR, compare, boolean_false_node,
1376 NULL_TREE, NULL_TREE);
1377 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1378
1379 /* Split the original block, and create the TRUE and FALSE blocks. */
1380 e = split_block (gsi_bb (*gsi), stmt);
1381 bb_cond = e->src;
1382 bb_join = e->dest;
1383 bb_true = create_empty_bb (bb_cond);
1384 bb_false = create_empty_bb (bb_true);
1385 bb_true->count = bb_false->count
1386 = bb_cond->count.apply_probability (profile_probability::even ());
1387
1388 /* Wire the blocks together. */
1389 e->flags = EDGE_TRUE_VALUE;
1390 /* TODO: With value profile we could add an historgram to determine real
1391 branch outcome. */
1392 e->probability = profile_probability::even ();
1393 redirect_edge_succ (e, bb_true);
1394 edge e2 = make_edge (bb_cond, bb_false, EDGE_FALSE_VALUE);
1395 e2->probability = profile_probability::even ();
1396 make_single_succ_edge (bb_true, bb_join, EDGE_FALLTHRU);
1397 make_single_succ_edge (bb_false, bb_join, EDGE_FALLTHRU);
1398 add_bb_to_loop (bb_true, bb_cond->loop_father);
1399 add_bb_to_loop (bb_false, bb_cond->loop_father);
1400
1401 /* Update dominance info. Note that bb_join's data was
1402 updated by split_block. */
1403 if (dom_info_available_p (CDI_DOMINATORS))
1404 {
1405 set_immediate_dominator (CDI_DOMINATORS, bb_true, bb_cond);
1406 set_immediate_dominator (CDI_DOMINATORS, bb_false, bb_cond);
1407 }
1408
1409 rr = create_tmp_reg (inner_type);
1410 ri = create_tmp_reg (inner_type);
1411 }
1412 else
1413 {
1414 gimple_seq_discard (stmts);
1415 stmts = NULL;
1416 }
1417
1418 /* In the TRUE branch, we compute
1419 ratio = br/bi;
1420 div = (br * ratio) + bi;
1421 tr = (ar * ratio) + ai;
1422 ti = (ai * ratio) - ar;
1423 tr = tr / div;
1424 ti = ti / div; */
1425 if (bb_true || integer_nonzerop (compare))
1426 {
1427 if (bb_true)
1428 {
1429 *gsi = gsi_last_bb (bb_true);
1430 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1431 }
1432
1433 ratio = gimple_build (&stmts, loc, code, inner_type, br, bi);
1434
1435 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, br, ratio);
1436 div = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, bi);
1437
1438 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, ratio);
1439 tr = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, ai);
1440
1441 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, ratio);
1442 ti = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, t1, ar);
1443
1444 tr = gimple_build (&stmts, loc, code, inner_type, tr, div);
1445 ti = gimple_build (&stmts, loc, code, inner_type, ti, div);
1446 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1447 stmts = NULL;
1448
1449 if (bb_true)
1450 {
1451 stmt = gimple_build_assign (rr, tr);
1452 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1453 stmt = gimple_build_assign (ri, ti);
1454 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1455 gsi_remove (gsi, true);
1456 }
1457 }
1458
1459 /* In the FALSE branch, we compute
1460 ratio = d/c;
1461 divisor = (d * ratio) + c;
1462 tr = (b * ratio) + a;
1463 ti = b - (a * ratio);
1464 tr = tr / div;
1465 ti = ti / div; */
1466 if (bb_false || integer_zerop (compare))
1467 {
1468 if (bb_false)
1469 {
1470 *gsi = gsi_last_bb (bb_false);
1471 gsi_insert_after (gsi, gimple_build_nop (), GSI_NEW_STMT);
1472 }
1473
1474 ratio = gimple_build (&stmts, loc, code, inner_type, bi, br);
1475
1476 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, bi, ratio);
1477 div = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, br);
1478
1479 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ai, ratio);
1480 tr = gimple_build (&stmts, loc, PLUS_EXPR, inner_type, t1, ar);
1481
1482 t1 = gimple_build (&stmts, loc, MULT_EXPR, inner_type, ar, ratio);
1483 ti = gimple_build (&stmts, loc, MINUS_EXPR, inner_type, ai, t1);
1484
1485 tr = gimple_build (&stmts, loc, code, inner_type, tr, div);
1486 ti = gimple_build (&stmts, loc, code, inner_type, ti, div);
1487 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1488 stmts = NULL;
1489
1490 if (bb_false)
1491 {
1492 stmt = gimple_build_assign (rr, tr);
1493 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1494 stmt = gimple_build_assign (ri, ti);
1495 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1496 gsi_remove (gsi, true);
1497 }
1498 }
1499
1500 if (bb_join)
1501 *gsi = gsi_start_bb (bb_join);
1502 else
1503 rr = tr, ri = ti;
1504
1505 update_complex_assignment (gsi, rr, ri);
1506 }
1507
1508 /* Expand complex division to scalars. */
1509
1510 static void
1511 expand_complex_division (gimple_stmt_iterator *gsi, tree type,
1512 tree ar, tree ai, tree br, tree bi,
1513 enum tree_code code,
1514 complex_lattice_t al, complex_lattice_t bl)
1515 {
1516 tree rr, ri;
1517 gimple_seq stmts = NULL;
1518 location_t loc = gimple_location (gsi_stmt (*gsi));
1519
1520 tree inner_type = TREE_TYPE (type);
1521 switch (PAIR (al, bl))
1522 {
1523 case PAIR (ONLY_REAL, ONLY_REAL):
1524 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1525 ri = ai;
1526 break;
1527
1528 case PAIR (ONLY_REAL, ONLY_IMAG):
1529 rr = ai;
1530 ri = gimple_build (&stmts, loc, code, inner_type, ar, bi);
1531 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ri);
1532 break;
1533
1534 case PAIR (ONLY_IMAG, ONLY_REAL):
1535 rr = ar;
1536 ri = gimple_build (&stmts, loc, code, inner_type, ai, br);
1537 break;
1538
1539 case PAIR (ONLY_IMAG, ONLY_IMAG):
1540 rr = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1541 ri = ar;
1542 break;
1543
1544 case PAIR (VARYING, ONLY_REAL):
1545 rr = gimple_build (&stmts, loc, code, inner_type, ar, br);
1546 ri = gimple_build (&stmts, loc, code, inner_type, ai, br);
1547 break;
1548
1549 case PAIR (VARYING, ONLY_IMAG):
1550 rr = gimple_build (&stmts, loc, code, inner_type, ai, bi);
1551 ri = gimple_build (&stmts, loc, code, inner_type, ar, bi);
1552 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ri);
1553 break;
1554
1555 case PAIR (ONLY_REAL, VARYING):
1556 case PAIR (ONLY_IMAG, VARYING):
1557 case PAIR (VARYING, VARYING):
1558 switch (flag_complex_method)
1559 {
1560 case 0:
1561 /* straightforward implementation of complex divide acceptable. */
1562 expand_complex_div_straight (gsi, inner_type, ar, ai, br, bi, code);
1563 break;
1564
1565 case 2:
1566 if (SCALAR_FLOAT_TYPE_P (inner_type))
1567 {
1568 expand_complex_libcall (gsi, type, ar, ai, br, bi, code, true);
1569 break;
1570 }
1571 /* FALLTHRU */
1572
1573 case 1:
1574 /* wide ranges of inputs must work for complex divide. */
1575 expand_complex_div_wide (gsi, inner_type, ar, ai, br, bi, code);
1576 break;
1577
1578 default:
1579 gcc_unreachable ();
1580 }
1581 return;
1582
1583 default:
1584 gcc_unreachable ();
1585 }
1586
1587 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1588 update_complex_assignment (gsi, rr, ri);
1589 }
1590
1591 /* Expand complex negation to scalars:
1592 -a = (-ar) + i(-ai)
1593 */
1594
1595 static void
1596 expand_complex_negation (gimple_stmt_iterator *gsi, tree inner_type,
1597 tree ar, tree ai)
1598 {
1599 tree rr, ri;
1600 gimple_seq stmts = NULL;
1601 location_t loc = gimple_location (gsi_stmt (*gsi));
1602
1603 rr = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ar);
1604 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ai);
1605
1606 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1607 update_complex_assignment (gsi, rr, ri);
1608 }
1609
1610 /* Expand complex paren to scalars:
1611 ((a)) = ((ar)) + i((ai))
1612 */
1613
1614 static void
1615 expand_complex_paren (gimple_stmt_iterator *gsi, tree inner_type,
1616 tree ar, tree ai)
1617 {
1618 tree rr, ri;
1619 gimple_seq stmts = NULL;
1620 location_t loc = gimple_location (gsi_stmt (*gsi));
1621
1622 rr = gimple_build (&stmts, loc, PAREN_EXPR, inner_type, ar);
1623 ri = gimple_build (&stmts, loc, PAREN_EXPR, inner_type, ai);
1624
1625 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1626 update_complex_assignment (gsi, rr, ri);
1627 }
1628
1629 /* Expand complex conjugate to scalars:
1630 ~a = (ar) + i(-ai)
1631 */
1632
1633 static void
1634 expand_complex_conjugate (gimple_stmt_iterator *gsi, tree inner_type,
1635 tree ar, tree ai)
1636 {
1637 tree ri;
1638 gimple_seq stmts = NULL;
1639 location_t loc = gimple_location (gsi_stmt (*gsi));
1640
1641 ri = gimple_build (&stmts, loc, NEGATE_EXPR, inner_type, ai);
1642
1643 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1644 update_complex_assignment (gsi, ar, ri);
1645 }
1646
1647 /* Expand complex comparison (EQ or NE only). */
1648
1649 static void
1650 expand_complex_comparison (gimple_stmt_iterator *gsi, tree ar, tree ai,
1651 tree br, tree bi, enum tree_code code)
1652 {
1653 tree cr, ci, cc, type;
1654 gimple *stmt = gsi_stmt (*gsi);
1655 gimple_seq stmts = NULL;
1656 location_t loc = gimple_location (stmt);
1657
1658 cr = gimple_build (&stmts, loc, code, boolean_type_node, ar, br);
1659 ci = gimple_build (&stmts, loc, code, boolean_type_node, ai, bi);
1660 cc = gimple_build (&stmts, loc,
1661 (code == EQ_EXPR ? BIT_AND_EXPR : BIT_IOR_EXPR),
1662 boolean_type_node, cr, ci);
1663 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1664
1665 switch (gimple_code (stmt))
1666 {
1667 case GIMPLE_ASSIGN:
1668 type = TREE_TYPE (gimple_assign_lhs (stmt));
1669 gimple_assign_set_rhs_from_tree (gsi, fold_convert (type, cc));
1670 stmt = gsi_stmt (*gsi);
1671 break;
1672
1673 case GIMPLE_COND:
1674 {
1675 gcond *cond_stmt = as_a <gcond *> (stmt);
1676 gimple_cond_set_code (cond_stmt, EQ_EXPR);
1677 gimple_cond_set_lhs (cond_stmt, cc);
1678 gimple_cond_set_rhs (cond_stmt, boolean_true_node);
1679 }
1680 break;
1681
1682 default:
1683 gcc_unreachable ();
1684 }
1685
1686 update_stmt (stmt);
1687 if (maybe_clean_eh_stmt (stmt))
1688 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
1689 }
1690
1691 /* Expand inline asm that sets some complex SSA_NAMEs. */
1692
1693 static void
1694 expand_complex_asm (gimple_stmt_iterator *gsi)
1695 {
1696 gasm *stmt = as_a <gasm *> (gsi_stmt (*gsi));
1697 unsigned int i;
1698 bool diagnosed_p = false;
1699
1700 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
1701 {
1702 tree link = gimple_asm_output_op (stmt, i);
1703 tree op = TREE_VALUE (link);
1704 if (TREE_CODE (op) == SSA_NAME
1705 && TREE_CODE (TREE_TYPE (op)) == COMPLEX_TYPE)
1706 {
1707 if (gimple_asm_nlabels (stmt) > 0)
1708 {
1709 if (!diagnosed_p)
1710 {
1711 sorry_at (gimple_location (stmt),
1712 "%<asm goto%> with complex typed outputs");
1713 diagnosed_p = true;
1714 }
1715 /* Make sure to not ICE later, see PR105165. */
1716 tree zero = build_zero_cst (TREE_TYPE (TREE_TYPE (op)));
1717 set_component_ssa_name (op, false, zero);
1718 set_component_ssa_name (op, true, zero);
1719 continue;
1720 }
1721 tree type = TREE_TYPE (op);
1722 tree inner_type = TREE_TYPE (type);
1723 tree r = build1 (REALPART_EXPR, inner_type, op);
1724 tree i = build1 (IMAGPART_EXPR, inner_type, op);
1725 gimple_seq list = set_component_ssa_name (op, false, r);
1726
1727 if (list)
1728 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1729
1730 list = set_component_ssa_name (op, true, i);
1731 if (list)
1732 gsi_insert_seq_after (gsi, list, GSI_CONTINUE_LINKING);
1733 }
1734 }
1735 }
1736
1737
1738 /* ARG is the argument to a cabs builtin call in GSI from the
1739 original OLD_STMT. Create a sequence of statements prior
1740 to GSI that calculates sqrt(R*R + I*I), where R and
1741 I are the real and imaginary components of ARG, respectively. */
1742
1743 static void
1744 gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, gimple *old_stmt)
1745 {
1746 tree real_part, imag_part, addend1, addend2, sum;
1747 tree arg = gimple_call_arg (old_stmt, 0);
1748 tree type = TREE_TYPE (TREE_TYPE (arg));
1749 machine_mode mode = TYPE_MODE (type);
1750 gimple *new_stmt;
1751
1752 tree lhs = gimple_call_lhs (old_stmt);
1753
1754 /* If there is not a LHS, then just keep the statement around. */
1755 if (!lhs)
1756 return;
1757
1758 real_part = extract_component (gsi, arg, false, true);
1759 imag_part = extract_component (gsi, arg, true, true);
1760 location_t loc = gimple_location (old_stmt);
1761
1762 gimple_seq stmts = NULL;
1763
1764 /* cabs(x+0i) -> abs(x).
1765 cabs(0+xi) -> abs(x).
1766 These 2 can be done even without unsafe math optimizations. */
1767 if (real_zerop (imag_part)
1768 || real_zerop (real_part))
1769 {
1770 tree other = real_zerop (imag_part) ? real_part : imag_part;
1771 sum = gimple_build (&stmts, loc, ABS_EXPR, type, other);
1772 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1773 new_stmt = gimple_build_assign (lhs, sum);
1774 gimple_set_location (new_stmt, loc);
1775 gsi_replace (gsi, new_stmt, true);
1776 return;
1777 }
1778
1779 if (!flag_unsafe_math_optimizations)
1780 return;
1781
1782 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
1783 if (operand_equal_p (real_part, imag_part))
1784 {
1785 tree sqrt2 = build_real_truncate (type, dconst_sqrt2 ());
1786 sum = gimple_build (&stmts, loc, ABS_EXPR, type, real_part);
1787 sum = gimple_build (&stmts, loc, MULT_EXPR, type, sum, sqrt2);
1788 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1789 new_stmt = gimple_build_assign (lhs, sum);
1790 gimple_set_location (new_stmt, loc);
1791 gsi_replace (gsi, new_stmt, true);
1792 return;
1793 }
1794
1795 /* cabs(a+bi) -> sqrt(a*a+b*b) if sqrt exists on the target
1796 and optimizing for speed. */
1797 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
1798 if (!optimize_bb_for_speed_p (gimple_bb (old_stmt))
1799 || !sqrtfn
1800 || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing)
1801 return;
1802
1803 addend1 = gimple_build (&stmts, loc, MULT_EXPR, type, real_part, real_part);
1804 addend2 = gimple_build (&stmts, loc, MULT_EXPR, type, imag_part, imag_part);
1805 sum = gimple_build (&stmts, loc, PLUS_EXPR, type, addend1, addend2);
1806 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1807
1808 /* Build the sqrt call. */
1809 new_stmt = gimple_build_call (sqrtfn, 1, sum);
1810 gimple_set_location (new_stmt, loc);
1811 gimple_call_set_lhs (new_stmt, lhs);
1812 gsi_replace (gsi, new_stmt, true);
1813 }
1814
1815 /* Process one statement. If we identify a complex operation, expand it. */
1816
1817 static void
1818 expand_complex_operations_1 (gimple_stmt_iterator *gsi)
1819 {
1820 gimple *stmt = gsi_stmt (*gsi);
1821 tree type, inner_type, lhs;
1822 tree ac, ar, ai, bc, br, bi;
1823 complex_lattice_t al, bl;
1824 enum tree_code code;
1825 if (gimple_code (stmt) == GIMPLE_CALL)
1826 {
1827 switch (gimple_call_combined_fn (stmt))
1828 {
1829 CASE_CFN_CABS:
1830 gimple_expand_builtin_cabs (gsi, stmt);
1831 return;
1832 default:;
1833 }
1834 }
1835
1836 if (gimple_code (stmt) == GIMPLE_ASM)
1837 {
1838 expand_complex_asm (gsi);
1839 return;
1840 }
1841
1842 lhs = gimple_get_lhs (stmt);
1843 if (!lhs && gimple_code (stmt) != GIMPLE_COND)
1844 return;
1845
1846 type = TREE_TYPE (gimple_op (stmt, 0));
1847 code = gimple_expr_code (stmt);
1848
1849 /* Initial filter for operations we handle. */
1850 switch (code)
1851 {
1852 case PLUS_EXPR:
1853 case MINUS_EXPR:
1854 case MULT_EXPR:
1855 case TRUNC_DIV_EXPR:
1856 case CEIL_DIV_EXPR:
1857 case FLOOR_DIV_EXPR:
1858 case ROUND_DIV_EXPR:
1859 case RDIV_EXPR:
1860 case NEGATE_EXPR:
1861 case PAREN_EXPR:
1862 case CONJ_EXPR:
1863 if (TREE_CODE (type) != COMPLEX_TYPE)
1864 return;
1865 inner_type = TREE_TYPE (type);
1866 break;
1867
1868 case EQ_EXPR:
1869 case NE_EXPR:
1870 /* Note, both GIMPLE_ASSIGN and GIMPLE_COND may have an EQ_EXPR
1871 subcode, so we need to access the operands using gimple_op. */
1872 inner_type = TREE_TYPE (gimple_op (stmt, 1));
1873 if (TREE_CODE (inner_type) != COMPLEX_TYPE)
1874 return;
1875 break;
1876
1877 default:
1878 {
1879 tree rhs;
1880
1881 /* GIMPLE_COND may also fallthru here, but we do not need to
1882 do anything with it. */
1883 if (gimple_code (stmt) == GIMPLE_COND)
1884 return;
1885
1886 if (TREE_CODE (type) == COMPLEX_TYPE)
1887 expand_complex_move (gsi, type);
1888 else if (is_gimple_assign (stmt)
1889 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1890 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1891 && TREE_CODE (lhs) == SSA_NAME)
1892 {
1893 rhs = gimple_assign_rhs1 (stmt);
1894 rhs = extract_component (gsi, TREE_OPERAND (rhs, 0),
1895 gimple_assign_rhs_code (stmt)
1896 == IMAGPART_EXPR,
1897 false);
1898 gimple_assign_set_rhs_from_tree (gsi, rhs);
1899 stmt = gsi_stmt (*gsi);
1900 update_stmt (stmt);
1901 }
1902 }
1903 return;
1904 }
1905
1906 /* Extract the components of the two complex values. Make sure and
1907 handle the common case of the same value used twice specially. */
1908 if (is_gimple_assign (stmt))
1909 {
1910 ac = gimple_assign_rhs1 (stmt);
1911 bc = (gimple_num_ops (stmt) > 2) ? gimple_assign_rhs2 (stmt) : NULL;
1912 }
1913 /* GIMPLE_CALL cannot get here. */
1914 else
1915 {
1916 ac = gimple_cond_lhs (stmt);
1917 bc = gimple_cond_rhs (stmt);
1918 }
1919
1920 ar = extract_component (gsi, ac, false, true);
1921 ai = extract_component (gsi, ac, true, true);
1922
1923 if (ac == bc)
1924 br = ar, bi = ai;
1925 else if (bc)
1926 {
1927 br = extract_component (gsi, bc, 0, true);
1928 bi = extract_component (gsi, bc, 1, true);
1929 }
1930 else
1931 br = bi = NULL_TREE;
1932
1933 al = find_lattice_value (ac);
1934 if (al == UNINITIALIZED)
1935 al = VARYING;
1936
1937 if (TREE_CODE_CLASS (code) == tcc_unary)
1938 bl = UNINITIALIZED;
1939 else if (ac == bc)
1940 bl = al;
1941 else
1942 {
1943 bl = find_lattice_value (bc);
1944 if (bl == UNINITIALIZED)
1945 bl = VARYING;
1946 }
1947
1948 switch (code)
1949 {
1950 case PLUS_EXPR:
1951 case MINUS_EXPR:
1952 expand_complex_addition (gsi, inner_type, ar, ai, br, bi, code, al, bl);
1953 break;
1954
1955 case MULT_EXPR:
1956 expand_complex_multiplication (gsi, type, ar, ai, br, bi, al, bl);
1957 break;
1958
1959 case TRUNC_DIV_EXPR:
1960 case CEIL_DIV_EXPR:
1961 case FLOOR_DIV_EXPR:
1962 case ROUND_DIV_EXPR:
1963 case RDIV_EXPR:
1964 expand_complex_division (gsi, type, ar, ai, br, bi, code, al, bl);
1965 break;
1966
1967 case NEGATE_EXPR:
1968 expand_complex_negation (gsi, inner_type, ar, ai);
1969 break;
1970
1971 case CONJ_EXPR:
1972 expand_complex_conjugate (gsi, inner_type, ar, ai);
1973 break;
1974
1975 case EQ_EXPR:
1976 case NE_EXPR:
1977 expand_complex_comparison (gsi, ar, ai, br, bi, code);
1978 break;
1979
1980 case PAREN_EXPR:
1981 expand_complex_paren (gsi, inner_type, ar, ai);
1982 break;
1983
1984 default:
1985 gcc_unreachable ();
1986 }
1987 }
1988
1989
1990 /* Entry point for complex operation lowering during optimization. */
1991
1992 static unsigned int
1993 tree_lower_complex (void)
1994 {
1995 gimple_stmt_iterator gsi;
1996 basic_block bb;
1997 int n_bbs, i;
1998 int *rpo;
1999
2000 if (!init_dont_simulate_again ())
2001 return 0;
2002
2003 complex_lattice_values.create (num_ssa_names);
2004 complex_lattice_values.safe_grow_cleared (num_ssa_names, true);
2005
2006 init_parameter_lattice_values ();
2007 class complex_propagate complex_propagate;
2008 complex_propagate.ssa_propagate ();
2009
2010 need_eh_cleanup = BITMAP_ALLOC (NULL);
2011 if (optimize)
2012 dce_worklist = BITMAP_ALLOC (NULL);
2013
2014 complex_variable_components = new int_tree_htab_type (10);
2015
2016 complex_ssa_name_components.create (2 * num_ssa_names);
2017 complex_ssa_name_components.safe_grow_cleared (2 * num_ssa_names, true);
2018
2019 update_parameter_components ();
2020
2021 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
2022 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
2023 for (i = 0; i < n_bbs; i++)
2024 {
2025 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2026 if (!bb)
2027 continue;
2028 update_phi_components (bb);
2029 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2030 expand_complex_operations_1 (&gsi);
2031 }
2032
2033 free (rpo);
2034
2035 if (!phis_to_revisit.is_empty ())
2036 {
2037 unsigned int n = phis_to_revisit.length ();
2038 for (unsigned int j = 0; j < n; j += 3)
2039 for (unsigned int k = 0; k < 2; k++)
2040 if (gphi *phi = phis_to_revisit[j + k + 1])
2041 {
2042 unsigned int m = gimple_phi_num_args (phi);
2043 for (unsigned int l = 0; l < m; ++l)
2044 {
2045 tree op = gimple_phi_arg_def (phi, l);
2046 if (TREE_CODE (op) == SSA_NAME
2047 || is_gimple_min_invariant (op))
2048 continue;
2049 tree arg = gimple_phi_arg_def (phis_to_revisit[j], l);
2050 op = extract_component (NULL, arg, k > 0, false, false);
2051 SET_PHI_ARG_DEF (phi, l, op);
2052 }
2053 }
2054 phis_to_revisit.release ();
2055 }
2056
2057 gsi_commit_edge_inserts ();
2058
2059 if (optimize)
2060 {
2061 simple_dce_from_worklist (dce_worklist, need_eh_cleanup);
2062 BITMAP_FREE (dce_worklist);
2063 }
2064
2065 unsigned todo
2066 = gimple_purge_all_dead_eh_edges (need_eh_cleanup) ? TODO_cleanup_cfg : 0;
2067 BITMAP_FREE (need_eh_cleanup);
2068
2069 delete complex_variable_components;
2070 complex_variable_components = NULL;
2071 complex_ssa_name_components.release ();
2072 complex_lattice_values.release ();
2073 return todo;
2074 }
2075
2076 namespace {
2077
2078 const pass_data pass_data_lower_complex =
2079 {
2080 GIMPLE_PASS, /* type */
2081 "cplxlower", /* name */
2082 OPTGROUP_NONE, /* optinfo_flags */
2083 TV_NONE, /* tv_id */
2084 PROP_ssa, /* properties_required */
2085 PROP_gimple_lcx, /* properties_provided */
2086 0, /* properties_destroyed */
2087 0, /* todo_flags_start */
2088 TODO_update_ssa, /* todo_flags_finish */
2089 };
2090
2091 class pass_lower_complex : public gimple_opt_pass
2092 {
2093 public:
2094 pass_lower_complex (gcc::context *ctxt)
2095 : gimple_opt_pass (pass_data_lower_complex, ctxt)
2096 {}
2097
2098 /* opt_pass methods: */
2099 opt_pass * clone () final override { return new pass_lower_complex (m_ctxt); }
2100 unsigned int execute (function *) final override
2101 {
2102 return tree_lower_complex ();
2103 }
2104
2105 }; // class pass_lower_complex
2106
2107 } // anon namespace
2108
2109 gimple_opt_pass *
2110 make_pass_lower_complex (gcc::context *ctxt)
2111 {
2112 return new pass_lower_complex (ctxt);
2113 }
2114
2115
2116 namespace {
2117
2118 const pass_data pass_data_lower_complex_O0 =
2119 {
2120 GIMPLE_PASS, /* type */
2121 "cplxlower0", /* name */
2122 OPTGROUP_NONE, /* optinfo_flags */
2123 TV_NONE, /* tv_id */
2124 PROP_cfg, /* properties_required */
2125 PROP_gimple_lcx, /* properties_provided */
2126 0, /* properties_destroyed */
2127 0, /* todo_flags_start */
2128 TODO_update_ssa, /* todo_flags_finish */
2129 };
2130
2131 class pass_lower_complex_O0 : public gimple_opt_pass
2132 {
2133 public:
2134 pass_lower_complex_O0 (gcc::context *ctxt)
2135 : gimple_opt_pass (pass_data_lower_complex_O0, ctxt)
2136 {}
2137
2138 /* opt_pass methods: */
2139 bool gate (function *fun) final override
2140 {
2141 /* With errors, normal optimization passes are not run. If we don't
2142 lower complex operations at all, rtl expansion will abort. */
2143 return !(fun->curr_properties & PROP_gimple_lcx);
2144 }
2145
2146 unsigned int execute (function *) final override
2147 {
2148 return tree_lower_complex ();
2149 }
2150
2151 }; // class pass_lower_complex_O0
2152
2153 } // anon namespace
2154
2155 gimple_opt_pass *
2156 make_pass_lower_complex_O0 (gcc::context *ctxt)
2157 {
2158 return new pass_lower_complex_O0 (ctxt);
2159 }