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