d: Add testcase from PR108962
[official-gcc.git] / gcc / tree-ssa-math-opts.cc
blob701fce2ab612a41e9f730ef84c95231078d4cbbd
1 /* Global, SSA-based optimizations using mathematical identities.
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 /* Currently, the only mini-pass in this file tries to CSE reciprocal
21 operations. These are common in sequences such as this one:
23 modulus = sqrt(x*x + y*y + z*z);
24 x = x / modulus;
25 y = y / modulus;
26 z = z / modulus;
28 that can be optimized to
30 modulus = sqrt(x*x + y*y + z*z);
31 rmodulus = 1.0 / modulus;
32 x = x * rmodulus;
33 y = y * rmodulus;
34 z = z * rmodulus;
36 We do this for loop invariant divisors, and with this pass whenever
37 we notice that a division has the same divisor multiple times.
39 Of course, like in PRE, we don't insert a division if a dominator
40 already has one. However, this cannot be done as an extension of
41 PRE for several reasons.
43 First of all, with some experiments it was found out that the
44 transformation is not always useful if there are only two divisions
45 by the same divisor. This is probably because modern processors
46 can pipeline the divisions; on older, in-order processors it should
47 still be effective to optimize two divisions by the same number.
48 We make this a param, and it shall be called N in the remainder of
49 this comment.
51 Second, if trapping math is active, we have less freedom on where
52 to insert divisions: we can only do so in basic blocks that already
53 contain one. (If divisions don't trap, instead, we can insert
54 divisions elsewhere, which will be in blocks that are common dominators
55 of those that have the division).
57 We really don't want to compute the reciprocal unless a division will
58 be found. To do this, we won't insert the division in a basic block
59 that has less than N divisions *post-dominating* it.
61 The algorithm constructs a subset of the dominator tree, holding the
62 blocks containing the divisions and the common dominators to them,
63 and walk it twice. The first walk is in post-order, and it annotates
64 each block with the number of divisions that post-dominate it: this
65 gives information on where divisions can be inserted profitably.
66 The second walk is in pre-order, and it inserts divisions as explained
67 above, and replaces divisions by multiplications.
69 In the best case, the cost of the pass is O(n_statements). In the
70 worst-case, the cost is due to creating the dominator tree subset,
71 with a cost of O(n_basic_blocks ^ 2); however this can only happen
72 for n_statements / n_basic_blocks statements. So, the amortized cost
73 of creating the dominator tree subset is O(n_basic_blocks) and the
74 worst-case cost of the pass is O(n_statements * n_basic_blocks).
76 More practically, the cost will be small because there are few
77 divisions, and they tend to be in the same basic block, so insert_bb
78 is called very few times.
80 If we did this using domwalk.cc, an efficient implementation would have
81 to work on all the variables in a single pass, because we could not
82 work on just a subset of the dominator tree, as we do now, and the
83 cost would also be something like O(n_statements * n_basic_blocks).
84 The data structures would be more complex in order to work on all the
85 variables in a single pass. */
87 #include "config.h"
88 #include "system.h"
89 #include "coretypes.h"
90 #include "backend.h"
91 #include "target.h"
92 #include "rtl.h"
93 #include "tree.h"
94 #include "gimple.h"
95 #include "predict.h"
96 #include "alloc-pool.h"
97 #include "tree-pass.h"
98 #include "ssa.h"
99 #include "optabs-tree.h"
100 #include "gimple-pretty-print.h"
101 #include "alias.h"
102 #include "fold-const.h"
103 #include "gimple-iterator.h"
104 #include "gimple-fold.h"
105 #include "gimplify.h"
106 #include "gimplify-me.h"
107 #include "stor-layout.h"
108 #include "tree-cfg.h"
109 #include "tree-dfa.h"
110 #include "tree-ssa.h"
111 #include "builtins.h"
112 #include "internal-fn.h"
113 #include "case-cfn-macros.h"
114 #include "optabs-libfuncs.h"
115 #include "tree-eh.h"
116 #include "targhooks.h"
117 #include "domwalk.h"
118 #include "tree-ssa-math-opts.h"
120 /* This structure represents one basic block that either computes a
121 division, or is a common dominator for basic block that compute a
122 division. */
123 struct occurrence {
124 /* The basic block represented by this structure. */
125 basic_block bb = basic_block();
127 /* If non-NULL, the SSA_NAME holding the definition for a reciprocal
128 inserted in BB. */
129 tree recip_def = tree();
131 /* If non-NULL, the SSA_NAME holding the definition for a squared
132 reciprocal inserted in BB. */
133 tree square_recip_def = tree();
135 /* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that
136 was inserted in BB. */
137 gimple *recip_def_stmt = nullptr;
139 /* Pointer to a list of "struct occurrence"s for blocks dominated
140 by BB. */
141 struct occurrence *children = nullptr;
143 /* Pointer to the next "struct occurrence"s in the list of blocks
144 sharing a common dominator. */
145 struct occurrence *next = nullptr;
147 /* The number of divisions that are in BB before compute_merit. The
148 number of divisions that are in BB or post-dominate it after
149 compute_merit. */
150 int num_divisions = 0;
152 /* True if the basic block has a division, false if it is a common
153 dominator for basic blocks that do. If it is false and trapping
154 math is active, BB is not a candidate for inserting a reciprocal. */
155 bool bb_has_division = false;
157 /* Construct a struct occurrence for basic block BB, and whose
158 children list is headed by CHILDREN. */
159 occurrence (basic_block bb, struct occurrence *children)
160 : bb (bb), children (children)
162 bb->aux = this;
165 /* Destroy a struct occurrence and remove it from its basic block. */
166 ~occurrence ()
168 bb->aux = nullptr;
171 /* Allocate memory for a struct occurrence from OCC_POOL. */
172 static void* operator new (size_t);
174 /* Return memory for a struct occurrence to OCC_POOL. */
175 static void operator delete (void*, size_t);
178 static struct
180 /* Number of 1.0/X ops inserted. */
181 int rdivs_inserted;
183 /* Number of 1.0/FUNC ops inserted. */
184 int rfuncs_inserted;
185 } reciprocal_stats;
187 static struct
189 /* Number of cexpi calls inserted. */
190 int inserted;
192 /* Number of conversions removed. */
193 int conv_removed;
195 } sincos_stats;
197 static struct
199 /* Number of widening multiplication ops inserted. */
200 int widen_mults_inserted;
202 /* Number of integer multiply-and-accumulate ops inserted. */
203 int maccs_inserted;
205 /* Number of fp fused multiply-add ops inserted. */
206 int fmas_inserted;
208 /* Number of divmod calls inserted. */
209 int divmod_calls_inserted;
211 /* Number of highpart multiplication ops inserted. */
212 int highpart_mults_inserted;
213 } widen_mul_stats;
215 /* The instance of "struct occurrence" representing the highest
216 interesting block in the dominator tree. */
217 static struct occurrence *occ_head;
219 /* Allocation pool for getting instances of "struct occurrence". */
220 static object_allocator<occurrence> *occ_pool;
222 void* occurrence::operator new (size_t n)
224 gcc_assert (n == sizeof(occurrence));
225 return occ_pool->allocate_raw ();
228 void occurrence::operator delete (void *occ, size_t n)
230 gcc_assert (n == sizeof(occurrence));
231 occ_pool->remove_raw (occ);
234 /* Insert NEW_OCC into our subset of the dominator tree. P_HEAD points to a
235 list of "struct occurrence"s, one per basic block, having IDOM as
236 their common dominator.
238 We try to insert NEW_OCC as deep as possible in the tree, and we also
239 insert any other block that is a common dominator for BB and one
240 block already in the tree. */
242 static void
243 insert_bb (struct occurrence *new_occ, basic_block idom,
244 struct occurrence **p_head)
246 struct occurrence *occ, **p_occ;
248 for (p_occ = p_head; (occ = *p_occ) != NULL; )
250 basic_block bb = new_occ->bb, occ_bb = occ->bb;
251 basic_block dom = nearest_common_dominator (CDI_DOMINATORS, occ_bb, bb);
252 if (dom == bb)
254 /* BB dominates OCC_BB. OCC becomes NEW_OCC's child: remove OCC
255 from its list. */
256 *p_occ = occ->next;
257 occ->next = new_occ->children;
258 new_occ->children = occ;
260 /* Try the next block (it may as well be dominated by BB). */
263 else if (dom == occ_bb)
265 /* OCC_BB dominates BB. Tail recurse to look deeper. */
266 insert_bb (new_occ, dom, &occ->children);
267 return;
270 else if (dom != idom)
272 gcc_assert (!dom->aux);
274 /* There is a dominator between IDOM and BB, add it and make
275 two children out of NEW_OCC and OCC. First, remove OCC from
276 its list. */
277 *p_occ = occ->next;
278 new_occ->next = occ;
279 occ->next = NULL;
281 /* None of the previous blocks has DOM as a dominator: if we tail
282 recursed, we would reexamine them uselessly. Just switch BB with
283 DOM, and go on looking for blocks dominated by DOM. */
284 new_occ = new occurrence (dom, new_occ);
287 else
289 /* Nothing special, go on with the next element. */
290 p_occ = &occ->next;
294 /* No place was found as a child of IDOM. Make BB a sibling of IDOM. */
295 new_occ->next = *p_head;
296 *p_head = new_occ;
299 /* Register that we found a division in BB.
300 IMPORTANCE is a measure of how much weighting to give
301 that division. Use IMPORTANCE = 2 to register a single
302 division. If the division is going to be found multiple
303 times use 1 (as it is with squares). */
305 static inline void
306 register_division_in (basic_block bb, int importance)
308 struct occurrence *occ;
310 occ = (struct occurrence *) bb->aux;
311 if (!occ)
313 occ = new occurrence (bb, NULL);
314 insert_bb (occ, ENTRY_BLOCK_PTR_FOR_FN (cfun), &occ_head);
317 occ->bb_has_division = true;
318 occ->num_divisions += importance;
322 /* Compute the number of divisions that postdominate each block in OCC and
323 its children. */
325 static void
326 compute_merit (struct occurrence *occ)
328 struct occurrence *occ_child;
329 basic_block dom = occ->bb;
331 for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
333 basic_block bb;
334 if (occ_child->children)
335 compute_merit (occ_child);
337 if (flag_exceptions)
338 bb = single_noncomplex_succ (dom);
339 else
340 bb = dom;
342 if (dominated_by_p (CDI_POST_DOMINATORS, bb, occ_child->bb))
343 occ->num_divisions += occ_child->num_divisions;
348 /* Return whether USE_STMT is a floating-point division by DEF. */
349 static inline bool
350 is_division_by (gimple *use_stmt, tree def)
352 return is_gimple_assign (use_stmt)
353 && gimple_assign_rhs_code (use_stmt) == RDIV_EXPR
354 && gimple_assign_rhs2 (use_stmt) == def
355 /* Do not recognize x / x as valid division, as we are getting
356 confused later by replacing all immediate uses x in such
357 a stmt. */
358 && gimple_assign_rhs1 (use_stmt) != def
359 && !stmt_can_throw_internal (cfun, use_stmt);
362 /* Return TRUE if USE_STMT is a multiplication of DEF by A. */
363 static inline bool
364 is_mult_by (gimple *use_stmt, tree def, tree a)
366 if (gimple_code (use_stmt) == GIMPLE_ASSIGN
367 && gimple_assign_rhs_code (use_stmt) == MULT_EXPR)
369 tree op0 = gimple_assign_rhs1 (use_stmt);
370 tree op1 = gimple_assign_rhs2 (use_stmt);
372 return (op0 == def && op1 == a)
373 || (op0 == a && op1 == def);
375 return 0;
378 /* Return whether USE_STMT is DEF * DEF. */
379 static inline bool
380 is_square_of (gimple *use_stmt, tree def)
382 return is_mult_by (use_stmt, def, def);
385 /* Return whether USE_STMT is a floating-point division by
386 DEF * DEF. */
387 static inline bool
388 is_division_by_square (gimple *use_stmt, tree def)
390 if (gimple_code (use_stmt) == GIMPLE_ASSIGN
391 && gimple_assign_rhs_code (use_stmt) == RDIV_EXPR
392 && gimple_assign_rhs1 (use_stmt) != gimple_assign_rhs2 (use_stmt)
393 && !stmt_can_throw_internal (cfun, use_stmt))
395 tree denominator = gimple_assign_rhs2 (use_stmt);
396 if (TREE_CODE (denominator) == SSA_NAME)
397 return is_square_of (SSA_NAME_DEF_STMT (denominator), def);
399 return 0;
402 /* Walk the subset of the dominator tree rooted at OCC, setting the
403 RECIP_DEF field to a definition of 1.0 / DEF that can be used in
404 the given basic block. The field may be left NULL, of course,
405 if it is not possible or profitable to do the optimization.
407 DEF_BSI is an iterator pointing at the statement defining DEF.
408 If RECIP_DEF is set, a dominator already has a computation that can
409 be used.
411 If should_insert_square_recip is set, then this also inserts
412 the square of the reciprocal immediately after the definition
413 of the reciprocal. */
415 static void
416 insert_reciprocals (gimple_stmt_iterator *def_gsi, struct occurrence *occ,
417 tree def, tree recip_def, tree square_recip_def,
418 int should_insert_square_recip, int threshold)
420 tree type;
421 gassign *new_stmt, *new_square_stmt;
422 gimple_stmt_iterator gsi;
423 struct occurrence *occ_child;
425 if (!recip_def
426 && (occ->bb_has_division || !flag_trapping_math)
427 /* Divide by two as all divisions are counted twice in
428 the costing loop. */
429 && occ->num_divisions / 2 >= threshold)
431 /* Make a variable with the replacement and substitute it. */
432 type = TREE_TYPE (def);
433 recip_def = create_tmp_reg (type, "reciptmp");
434 new_stmt = gimple_build_assign (recip_def, RDIV_EXPR,
435 build_one_cst (type), def);
437 if (should_insert_square_recip)
439 square_recip_def = create_tmp_reg (type, "powmult_reciptmp");
440 new_square_stmt = gimple_build_assign (square_recip_def, MULT_EXPR,
441 recip_def, recip_def);
444 if (occ->bb_has_division)
446 /* Case 1: insert before an existing division. */
447 gsi = gsi_after_labels (occ->bb);
448 while (!gsi_end_p (gsi)
449 && (!is_division_by (gsi_stmt (gsi), def))
450 && (!is_division_by_square (gsi_stmt (gsi), def)))
451 gsi_next (&gsi);
453 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
454 if (should_insert_square_recip)
455 gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT);
457 else if (def_gsi && occ->bb == gsi_bb (*def_gsi))
459 /* Case 2: insert right after the definition. Note that this will
460 never happen if the definition statement can throw, because in
461 that case the sole successor of the statement's basic block will
462 dominate all the uses as well. */
463 gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT);
464 if (should_insert_square_recip)
465 gsi_insert_after (def_gsi, new_square_stmt, GSI_NEW_STMT);
467 else
469 /* Case 3: insert in a basic block not containing defs/uses. */
470 gsi = gsi_after_labels (occ->bb);
471 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
472 if (should_insert_square_recip)
473 gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT);
476 reciprocal_stats.rdivs_inserted++;
478 occ->recip_def_stmt = new_stmt;
481 occ->recip_def = recip_def;
482 occ->square_recip_def = square_recip_def;
483 for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
484 insert_reciprocals (def_gsi, occ_child, def, recip_def,
485 square_recip_def, should_insert_square_recip,
486 threshold);
489 /* Replace occurrences of expr / (x * x) with expr * ((1 / x) * (1 / x)).
490 Take as argument the use for (x * x). */
491 static inline void
492 replace_reciprocal_squares (use_operand_p use_p)
494 gimple *use_stmt = USE_STMT (use_p);
495 basic_block bb = gimple_bb (use_stmt);
496 struct occurrence *occ = (struct occurrence *) bb->aux;
498 if (optimize_bb_for_speed_p (bb) && occ->square_recip_def
499 && occ->recip_def)
501 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
502 gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
503 gimple_assign_set_rhs2 (use_stmt, occ->square_recip_def);
504 SET_USE (use_p, occ->square_recip_def);
505 fold_stmt_inplace (&gsi);
506 update_stmt (use_stmt);
511 /* Replace the division at USE_P with a multiplication by the reciprocal, if
512 possible. */
514 static inline void
515 replace_reciprocal (use_operand_p use_p)
517 gimple *use_stmt = USE_STMT (use_p);
518 basic_block bb = gimple_bb (use_stmt);
519 struct occurrence *occ = (struct occurrence *) bb->aux;
521 if (optimize_bb_for_speed_p (bb)
522 && occ->recip_def && use_stmt != occ->recip_def_stmt)
524 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
525 gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
526 SET_USE (use_p, occ->recip_def);
527 fold_stmt_inplace (&gsi);
528 update_stmt (use_stmt);
533 /* Free OCC and return one more "struct occurrence" to be freed. */
535 static struct occurrence *
536 free_bb (struct occurrence *occ)
538 struct occurrence *child, *next;
540 /* First get the two pointers hanging off OCC. */
541 next = occ->next;
542 child = occ->children;
543 delete occ;
545 /* Now ensure that we don't recurse unless it is necessary. */
546 if (!child)
547 return next;
548 else
550 while (next)
551 next = free_bb (next);
553 return child;
557 /* Transform sequences like
558 t = sqrt (a)
559 x = 1.0 / t;
560 r1 = x * x;
561 r2 = a * x;
562 into:
563 t = sqrt (a)
564 r1 = 1.0 / a;
565 r2 = t;
566 x = r1 * r2;
567 depending on the uses of x, r1, r2. This removes one multiplication and
568 allows the sqrt and division operations to execute in parallel.
569 DEF_GSI is the gsi of the initial division by sqrt that defines
570 DEF (x in the example above). */
572 static void
573 optimize_recip_sqrt (gimple_stmt_iterator *def_gsi, tree def)
575 gimple *use_stmt;
576 imm_use_iterator use_iter;
577 gimple *stmt = gsi_stmt (*def_gsi);
578 tree x = def;
579 tree orig_sqrt_ssa_name = gimple_assign_rhs2 (stmt);
580 tree div_rhs1 = gimple_assign_rhs1 (stmt);
582 if (TREE_CODE (orig_sqrt_ssa_name) != SSA_NAME
583 || TREE_CODE (div_rhs1) != REAL_CST
584 || !real_equal (&TREE_REAL_CST (div_rhs1), &dconst1))
585 return;
587 gcall *sqrt_stmt
588 = dyn_cast <gcall *> (SSA_NAME_DEF_STMT (orig_sqrt_ssa_name));
590 if (!sqrt_stmt || !gimple_call_lhs (sqrt_stmt))
591 return;
593 switch (gimple_call_combined_fn (sqrt_stmt))
595 CASE_CFN_SQRT:
596 CASE_CFN_SQRT_FN:
597 break;
599 default:
600 return;
602 tree a = gimple_call_arg (sqrt_stmt, 0);
604 /* We have 'a' and 'x'. Now analyze the uses of 'x'. */
606 /* Statements that use x in x * x. */
607 auto_vec<gimple *> sqr_stmts;
608 /* Statements that use x in a * x. */
609 auto_vec<gimple *> mult_stmts;
610 bool has_other_use = false;
611 bool mult_on_main_path = false;
613 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, x)
615 if (is_gimple_debug (use_stmt))
616 continue;
617 if (is_square_of (use_stmt, x))
619 sqr_stmts.safe_push (use_stmt);
620 if (gimple_bb (use_stmt) == gimple_bb (stmt))
621 mult_on_main_path = true;
623 else if (is_mult_by (use_stmt, x, a))
625 mult_stmts.safe_push (use_stmt);
626 if (gimple_bb (use_stmt) == gimple_bb (stmt))
627 mult_on_main_path = true;
629 else
630 has_other_use = true;
633 /* In the x * x and a * x cases we just rewire stmt operands or
634 remove multiplications. In the has_other_use case we introduce
635 a multiplication so make sure we don't introduce a multiplication
636 on a path where there was none. */
637 if (has_other_use && !mult_on_main_path)
638 return;
640 if (sqr_stmts.is_empty () && mult_stmts.is_empty ())
641 return;
643 /* If x = 1.0 / sqrt (a) has uses other than those optimized here we want
644 to be able to compose it from the sqr and mult cases. */
645 if (has_other_use && (sqr_stmts.is_empty () || mult_stmts.is_empty ()))
646 return;
648 if (dump_file)
650 fprintf (dump_file, "Optimizing reciprocal sqrt multiplications of\n");
651 print_gimple_stmt (dump_file, sqrt_stmt, 0, TDF_NONE);
652 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
653 fprintf (dump_file, "\n");
656 bool delete_div = !has_other_use;
657 tree sqr_ssa_name = NULL_TREE;
658 if (!sqr_stmts.is_empty ())
660 /* r1 = x * x. Transform the original
661 x = 1.0 / t
662 into
663 tmp1 = 1.0 / a
664 r1 = tmp1. */
666 sqr_ssa_name
667 = make_temp_ssa_name (TREE_TYPE (a), NULL, "recip_sqrt_sqr");
669 if (dump_file)
671 fprintf (dump_file, "Replacing original division\n");
672 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
673 fprintf (dump_file, "with new division\n");
675 stmt
676 = gimple_build_assign (sqr_ssa_name, gimple_assign_rhs_code (stmt),
677 gimple_assign_rhs1 (stmt), a);
678 gsi_insert_before (def_gsi, stmt, GSI_SAME_STMT);
679 gsi_remove (def_gsi, true);
680 *def_gsi = gsi_for_stmt (stmt);
681 fold_stmt_inplace (def_gsi);
682 update_stmt (stmt);
684 if (dump_file)
685 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
687 delete_div = false;
688 gimple *sqr_stmt;
689 unsigned int i;
690 FOR_EACH_VEC_ELT (sqr_stmts, i, sqr_stmt)
692 gimple_stmt_iterator gsi2 = gsi_for_stmt (sqr_stmt);
693 gimple_assign_set_rhs_from_tree (&gsi2, sqr_ssa_name);
694 update_stmt (sqr_stmt);
697 if (!mult_stmts.is_empty ())
699 /* r2 = a * x. Transform this into:
700 r2 = t (The original sqrt (a)). */
701 unsigned int i;
702 gimple *mult_stmt = NULL;
703 FOR_EACH_VEC_ELT (mult_stmts, i, mult_stmt)
705 gimple_stmt_iterator gsi2 = gsi_for_stmt (mult_stmt);
707 if (dump_file)
709 fprintf (dump_file, "Replacing squaring multiplication\n");
710 print_gimple_stmt (dump_file, mult_stmt, 0, TDF_NONE);
711 fprintf (dump_file, "with assignment\n");
713 gimple_assign_set_rhs_from_tree (&gsi2, orig_sqrt_ssa_name);
714 fold_stmt_inplace (&gsi2);
715 update_stmt (mult_stmt);
716 if (dump_file)
717 print_gimple_stmt (dump_file, mult_stmt, 0, TDF_NONE);
721 if (has_other_use)
723 /* Using the two temporaries tmp1, tmp2 from above
724 the original x is now:
725 x = tmp1 * tmp2. */
726 gcc_assert (orig_sqrt_ssa_name);
727 gcc_assert (sqr_ssa_name);
729 gimple *new_stmt
730 = gimple_build_assign (x, MULT_EXPR,
731 orig_sqrt_ssa_name, sqr_ssa_name);
732 gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT);
733 update_stmt (stmt);
735 else if (delete_div)
737 /* Remove the original division. */
738 gimple_stmt_iterator gsi2 = gsi_for_stmt (stmt);
739 gsi_remove (&gsi2, true);
740 release_defs (stmt);
742 else
743 release_ssa_name (x);
746 /* Look for floating-point divisions among DEF's uses, and try to
747 replace them by multiplications with the reciprocal. Add
748 as many statements computing the reciprocal as needed.
750 DEF must be a GIMPLE register of a floating-point type. */
752 static void
753 execute_cse_reciprocals_1 (gimple_stmt_iterator *def_gsi, tree def)
755 use_operand_p use_p, square_use_p;
756 imm_use_iterator use_iter, square_use_iter;
757 tree square_def;
758 struct occurrence *occ;
759 int count = 0;
760 int threshold;
761 int square_recip_count = 0;
762 int sqrt_recip_count = 0;
764 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def)) && TREE_CODE (def) == SSA_NAME);
765 threshold = targetm.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def)));
767 /* If DEF is a square (x * x), count the number of divisions by x.
768 If there are more divisions by x than by (DEF * DEF), prefer to optimize
769 the reciprocal of x instead of DEF. This improves cases like:
770 def = x * x
771 t0 = a / def
772 t1 = b / def
773 t2 = c / x
774 Reciprocal optimization of x results in 1 division rather than 2 or 3. */
775 gimple *def_stmt = SSA_NAME_DEF_STMT (def);
777 if (is_gimple_assign (def_stmt)
778 && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
779 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
780 && gimple_assign_rhs1 (def_stmt) == gimple_assign_rhs2 (def_stmt))
782 tree op0 = gimple_assign_rhs1 (def_stmt);
784 FOR_EACH_IMM_USE_FAST (use_p, use_iter, op0)
786 gimple *use_stmt = USE_STMT (use_p);
787 if (is_division_by (use_stmt, op0))
788 sqrt_recip_count++;
792 FOR_EACH_IMM_USE_FAST (use_p, use_iter, def)
794 gimple *use_stmt = USE_STMT (use_p);
795 if (is_division_by (use_stmt, def))
797 register_division_in (gimple_bb (use_stmt), 2);
798 count++;
801 if (is_square_of (use_stmt, def))
803 square_def = gimple_assign_lhs (use_stmt);
804 FOR_EACH_IMM_USE_FAST (square_use_p, square_use_iter, square_def)
806 gimple *square_use_stmt = USE_STMT (square_use_p);
807 if (is_division_by (square_use_stmt, square_def))
809 /* This is executed twice for each division by a square. */
810 register_division_in (gimple_bb (square_use_stmt), 1);
811 square_recip_count++;
817 /* Square reciprocals were counted twice above. */
818 square_recip_count /= 2;
820 /* If it is more profitable to optimize 1 / x, don't optimize 1 / (x * x). */
821 if (sqrt_recip_count > square_recip_count)
822 goto out;
824 /* Do the expensive part only if we can hope to optimize something. */
825 if (count + square_recip_count >= threshold && count >= 1)
827 gimple *use_stmt;
828 for (occ = occ_head; occ; occ = occ->next)
830 compute_merit (occ);
831 insert_reciprocals (def_gsi, occ, def, NULL, NULL,
832 square_recip_count, threshold);
835 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
837 if (is_division_by (use_stmt, def))
839 FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
840 replace_reciprocal (use_p);
842 else if (square_recip_count > 0 && is_square_of (use_stmt, def))
844 FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
846 /* Find all uses of the square that are divisions and
847 * replace them by multiplications with the inverse. */
848 imm_use_iterator square_iterator;
849 gimple *powmult_use_stmt = USE_STMT (use_p);
850 tree powmult_def_name = gimple_assign_lhs (powmult_use_stmt);
852 FOR_EACH_IMM_USE_STMT (powmult_use_stmt,
853 square_iterator, powmult_def_name)
854 FOR_EACH_IMM_USE_ON_STMT (square_use_p, square_iterator)
856 gimple *powmult_use_stmt = USE_STMT (square_use_p);
857 if (is_division_by (powmult_use_stmt, powmult_def_name))
858 replace_reciprocal_squares (square_use_p);
865 out:
866 for (occ = occ_head; occ; )
867 occ = free_bb (occ);
869 occ_head = NULL;
872 /* Return an internal function that implements the reciprocal of CALL,
873 or IFN_LAST if there is no such function that the target supports. */
875 internal_fn
876 internal_fn_reciprocal (gcall *call)
878 internal_fn ifn;
880 switch (gimple_call_combined_fn (call))
882 CASE_CFN_SQRT:
883 CASE_CFN_SQRT_FN:
884 ifn = IFN_RSQRT;
885 break;
887 default:
888 return IFN_LAST;
891 tree_pair types = direct_internal_fn_types (ifn, call);
892 if (!direct_internal_fn_supported_p (ifn, types, OPTIMIZE_FOR_SPEED))
893 return IFN_LAST;
895 return ifn;
898 /* Go through all the floating-point SSA_NAMEs, and call
899 execute_cse_reciprocals_1 on each of them. */
900 namespace {
902 const pass_data pass_data_cse_reciprocals =
904 GIMPLE_PASS, /* type */
905 "recip", /* name */
906 OPTGROUP_NONE, /* optinfo_flags */
907 TV_TREE_RECIP, /* tv_id */
908 PROP_ssa, /* properties_required */
909 0, /* properties_provided */
910 0, /* properties_destroyed */
911 0, /* todo_flags_start */
912 TODO_update_ssa, /* todo_flags_finish */
915 class pass_cse_reciprocals : public gimple_opt_pass
917 public:
918 pass_cse_reciprocals (gcc::context *ctxt)
919 : gimple_opt_pass (pass_data_cse_reciprocals, ctxt)
922 /* opt_pass methods: */
923 bool gate (function *) final override
925 return optimize && flag_reciprocal_math;
927 unsigned int execute (function *) final override;
929 }; // class pass_cse_reciprocals
931 unsigned int
932 pass_cse_reciprocals::execute (function *fun)
934 basic_block bb;
935 tree arg;
937 occ_pool = new object_allocator<occurrence> ("dominators for recip");
939 memset (&reciprocal_stats, 0, sizeof (reciprocal_stats));
940 calculate_dominance_info (CDI_DOMINATORS);
941 calculate_dominance_info (CDI_POST_DOMINATORS);
943 if (flag_checking)
944 FOR_EACH_BB_FN (bb, fun)
945 gcc_assert (!bb->aux);
947 for (arg = DECL_ARGUMENTS (fun->decl); arg; arg = DECL_CHAIN (arg))
948 if (FLOAT_TYPE_P (TREE_TYPE (arg))
949 && is_gimple_reg (arg))
951 tree name = ssa_default_def (fun, arg);
952 if (name)
953 execute_cse_reciprocals_1 (NULL, name);
956 FOR_EACH_BB_FN (bb, fun)
958 tree def;
960 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
961 gsi_next (&gsi))
963 gphi *phi = gsi.phi ();
964 def = PHI_RESULT (phi);
965 if (! virtual_operand_p (def)
966 && FLOAT_TYPE_P (TREE_TYPE (def)))
967 execute_cse_reciprocals_1 (NULL, def);
970 for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
971 gsi_next (&gsi))
973 gimple *stmt = gsi_stmt (gsi);
975 if (gimple_has_lhs (stmt)
976 && (def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF)) != NULL
977 && FLOAT_TYPE_P (TREE_TYPE (def))
978 && TREE_CODE (def) == SSA_NAME)
980 execute_cse_reciprocals_1 (&gsi, def);
981 stmt = gsi_stmt (gsi);
982 if (flag_unsafe_math_optimizations
983 && is_gimple_assign (stmt)
984 && gimple_assign_lhs (stmt) == def
985 && !stmt_can_throw_internal (cfun, stmt)
986 && gimple_assign_rhs_code (stmt) == RDIV_EXPR)
987 optimize_recip_sqrt (&gsi, def);
991 if (optimize_bb_for_size_p (bb))
992 continue;
994 /* Scan for a/func(b) and convert it to reciprocal a*rfunc(b). */
995 for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
996 gsi_next (&gsi))
998 gimple *stmt = gsi_stmt (gsi);
1000 if (is_gimple_assign (stmt)
1001 && gimple_assign_rhs_code (stmt) == RDIV_EXPR)
1003 tree arg1 = gimple_assign_rhs2 (stmt);
1004 gimple *stmt1;
1006 if (TREE_CODE (arg1) != SSA_NAME)
1007 continue;
1009 stmt1 = SSA_NAME_DEF_STMT (arg1);
1011 if (is_gimple_call (stmt1)
1012 && gimple_call_lhs (stmt1))
1014 bool fail;
1015 imm_use_iterator ui;
1016 use_operand_p use_p;
1017 tree fndecl = NULL_TREE;
1019 gcall *call = as_a <gcall *> (stmt1);
1020 internal_fn ifn = internal_fn_reciprocal (call);
1021 if (ifn == IFN_LAST)
1023 fndecl = gimple_call_fndecl (call);
1024 if (!fndecl
1025 || !fndecl_built_in_p (fndecl, BUILT_IN_MD))
1026 continue;
1027 fndecl = targetm.builtin_reciprocal (fndecl);
1028 if (!fndecl)
1029 continue;
1032 /* Check that all uses of the SSA name are divisions,
1033 otherwise replacing the defining statement will do
1034 the wrong thing. */
1035 fail = false;
1036 FOR_EACH_IMM_USE_FAST (use_p, ui, arg1)
1038 gimple *stmt2 = USE_STMT (use_p);
1039 if (is_gimple_debug (stmt2))
1040 continue;
1041 if (!is_gimple_assign (stmt2)
1042 || gimple_assign_rhs_code (stmt2) != RDIV_EXPR
1043 || gimple_assign_rhs1 (stmt2) == arg1
1044 || gimple_assign_rhs2 (stmt2) != arg1)
1046 fail = true;
1047 break;
1050 if (fail)
1051 continue;
1053 gimple_replace_ssa_lhs (call, arg1);
1054 if (gimple_call_internal_p (call) != (ifn != IFN_LAST))
1056 auto_vec<tree, 4> args;
1057 for (unsigned int i = 0;
1058 i < gimple_call_num_args (call); i++)
1059 args.safe_push (gimple_call_arg (call, i));
1060 gcall *stmt2;
1061 if (ifn == IFN_LAST)
1062 stmt2 = gimple_build_call_vec (fndecl, args);
1063 else
1064 stmt2 = gimple_build_call_internal_vec (ifn, args);
1065 gimple_call_set_lhs (stmt2, arg1);
1066 gimple_move_vops (stmt2, call);
1067 gimple_call_set_nothrow (stmt2,
1068 gimple_call_nothrow_p (call));
1069 gimple_stmt_iterator gsi2 = gsi_for_stmt (call);
1070 gsi_replace (&gsi2, stmt2, true);
1072 else
1074 if (ifn == IFN_LAST)
1075 gimple_call_set_fndecl (call, fndecl);
1076 else
1077 gimple_call_set_internal_fn (call, ifn);
1078 update_stmt (call);
1080 reciprocal_stats.rfuncs_inserted++;
1082 FOR_EACH_IMM_USE_STMT (stmt, ui, arg1)
1084 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1085 gimple_assign_set_rhs_code (stmt, MULT_EXPR);
1086 fold_stmt_inplace (&gsi);
1087 update_stmt (stmt);
1094 statistics_counter_event (fun, "reciprocal divs inserted",
1095 reciprocal_stats.rdivs_inserted);
1096 statistics_counter_event (fun, "reciprocal functions inserted",
1097 reciprocal_stats.rfuncs_inserted);
1099 free_dominance_info (CDI_DOMINATORS);
1100 free_dominance_info (CDI_POST_DOMINATORS);
1101 delete occ_pool;
1102 return 0;
1105 } // anon namespace
1107 gimple_opt_pass *
1108 make_pass_cse_reciprocals (gcc::context *ctxt)
1110 return new pass_cse_reciprocals (ctxt);
1113 /* If NAME is the result of a type conversion, look for other
1114 equivalent dominating or dominated conversions, and replace all
1115 uses with the earliest dominating name, removing the redundant
1116 conversions. Return the prevailing name. */
1118 static tree
1119 execute_cse_conv_1 (tree name, bool *cfg_changed)
1121 if (SSA_NAME_IS_DEFAULT_DEF (name)
1122 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1123 return name;
1125 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1127 if (!gimple_assign_cast_p (def_stmt))
1128 return name;
1130 tree src = gimple_assign_rhs1 (def_stmt);
1132 if (TREE_CODE (src) != SSA_NAME)
1133 return name;
1135 imm_use_iterator use_iter;
1136 gimple *use_stmt;
1138 /* Find the earliest dominating def. */
1139 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, src)
1141 if (use_stmt == def_stmt
1142 || !gimple_assign_cast_p (use_stmt))
1143 continue;
1145 tree lhs = gimple_assign_lhs (use_stmt);
1147 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
1148 || (gimple_assign_rhs1 (use_stmt)
1149 != gimple_assign_rhs1 (def_stmt))
1150 || !types_compatible_p (TREE_TYPE (name), TREE_TYPE (lhs)))
1151 continue;
1153 bool use_dominates;
1154 if (gimple_bb (def_stmt) == gimple_bb (use_stmt))
1156 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1157 while (!gsi_end_p (gsi) && gsi_stmt (gsi) != def_stmt)
1158 gsi_next (&gsi);
1159 use_dominates = !gsi_end_p (gsi);
1161 else if (dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt),
1162 gimple_bb (def_stmt)))
1163 use_dominates = false;
1164 else if (dominated_by_p (CDI_DOMINATORS, gimple_bb (def_stmt),
1165 gimple_bb (use_stmt)))
1166 use_dominates = true;
1167 else
1168 continue;
1170 if (use_dominates)
1172 std::swap (name, lhs);
1173 std::swap (def_stmt, use_stmt);
1177 /* Now go through all uses of SRC again, replacing the equivalent
1178 dominated conversions. We may replace defs that were not
1179 dominated by the then-prevailing defs when we first visited
1180 them. */
1181 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, src)
1183 if (use_stmt == def_stmt
1184 || !gimple_assign_cast_p (use_stmt))
1185 continue;
1187 tree lhs = gimple_assign_lhs (use_stmt);
1189 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
1190 || (gimple_assign_rhs1 (use_stmt)
1191 != gimple_assign_rhs1 (def_stmt))
1192 || !types_compatible_p (TREE_TYPE (name), TREE_TYPE (lhs)))
1193 continue;
1195 basic_block use_bb = gimple_bb (use_stmt);
1196 if (gimple_bb (def_stmt) == use_bb
1197 || dominated_by_p (CDI_DOMINATORS, use_bb, gimple_bb (def_stmt)))
1199 sincos_stats.conv_removed++;
1201 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1202 replace_uses_by (lhs, name);
1203 if (gsi_remove (&gsi, true)
1204 && gimple_purge_dead_eh_edges (use_bb))
1205 *cfg_changed = true;
1206 release_defs (use_stmt);
1210 return name;
1213 /* Records an occurrence at statement USE_STMT in the vector of trees
1214 STMTS if it is dominated by *TOP_BB or dominates it or this basic block
1215 is not yet initialized. Returns true if the occurrence was pushed on
1216 the vector. Adjusts *TOP_BB to be the basic block dominating all
1217 statements in the vector. */
1219 static bool
1220 maybe_record_sincos (vec<gimple *> *stmts,
1221 basic_block *top_bb, gimple *use_stmt)
1223 basic_block use_bb = gimple_bb (use_stmt);
1224 if (*top_bb
1225 && (*top_bb == use_bb
1226 || dominated_by_p (CDI_DOMINATORS, use_bb, *top_bb)))
1227 stmts->safe_push (use_stmt);
1228 else if (!*top_bb
1229 || dominated_by_p (CDI_DOMINATORS, *top_bb, use_bb))
1231 stmts->safe_push (use_stmt);
1232 *top_bb = use_bb;
1234 else
1235 return false;
1237 return true;
1240 /* Look for sin, cos and cexpi calls with the same argument NAME and
1241 create a single call to cexpi CSEing the result in this case.
1242 We first walk over all immediate uses of the argument collecting
1243 statements that we can CSE in a vector and in a second pass replace
1244 the statement rhs with a REALPART or IMAGPART expression on the
1245 result of the cexpi call we insert before the use statement that
1246 dominates all other candidates. */
1248 static bool
1249 execute_cse_sincos_1 (tree name)
1251 gimple_stmt_iterator gsi;
1252 imm_use_iterator use_iter;
1253 tree fndecl, res, type = NULL_TREE;
1254 gimple *def_stmt, *use_stmt, *stmt;
1255 int seen_cos = 0, seen_sin = 0, seen_cexpi = 0;
1256 auto_vec<gimple *> stmts;
1257 basic_block top_bb = NULL;
1258 int i;
1259 bool cfg_changed = false;
1261 name = execute_cse_conv_1 (name, &cfg_changed);
1263 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, name)
1265 if (gimple_code (use_stmt) != GIMPLE_CALL
1266 || !gimple_call_lhs (use_stmt))
1267 continue;
1269 switch (gimple_call_combined_fn (use_stmt))
1271 CASE_CFN_COS:
1272 seen_cos |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
1273 break;
1275 CASE_CFN_SIN:
1276 seen_sin |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
1277 break;
1279 CASE_CFN_CEXPI:
1280 seen_cexpi |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
1281 break;
1283 default:;
1284 continue;
1287 tree t = mathfn_built_in_type (gimple_call_combined_fn (use_stmt));
1288 if (!type)
1290 type = t;
1291 t = TREE_TYPE (name);
1293 /* This checks that NAME has the right type in the first round,
1294 and, in subsequent rounds, that the built_in type is the same
1295 type, or a compatible type. */
1296 if (type != t && !types_compatible_p (type, t))
1297 return false;
1299 if (seen_cos + seen_sin + seen_cexpi <= 1)
1300 return false;
1302 /* Simply insert cexpi at the beginning of top_bb but not earlier than
1303 the name def statement. */
1304 fndecl = mathfn_built_in (type, BUILT_IN_CEXPI);
1305 if (!fndecl)
1306 return false;
1307 stmt = gimple_build_call (fndecl, 1, name);
1308 res = make_temp_ssa_name (TREE_TYPE (TREE_TYPE (fndecl)), stmt, "sincostmp");
1309 gimple_call_set_lhs (stmt, res);
1311 def_stmt = SSA_NAME_DEF_STMT (name);
1312 if (!SSA_NAME_IS_DEFAULT_DEF (name)
1313 && gimple_code (def_stmt) != GIMPLE_PHI
1314 && gimple_bb (def_stmt) == top_bb)
1316 gsi = gsi_for_stmt (def_stmt);
1317 gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
1319 else
1321 gsi = gsi_after_labels (top_bb);
1322 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1324 sincos_stats.inserted++;
1326 /* And adjust the recorded old call sites. */
1327 for (i = 0; stmts.iterate (i, &use_stmt); ++i)
1329 tree rhs = NULL;
1331 switch (gimple_call_combined_fn (use_stmt))
1333 CASE_CFN_COS:
1334 rhs = fold_build1 (REALPART_EXPR, type, res);
1335 break;
1337 CASE_CFN_SIN:
1338 rhs = fold_build1 (IMAGPART_EXPR, type, res);
1339 break;
1341 CASE_CFN_CEXPI:
1342 rhs = res;
1343 break;
1345 default:;
1346 gcc_unreachable ();
1349 /* Replace call with a copy. */
1350 stmt = gimple_build_assign (gimple_call_lhs (use_stmt), rhs);
1352 gsi = gsi_for_stmt (use_stmt);
1353 gsi_replace (&gsi, stmt, true);
1354 if (gimple_purge_dead_eh_edges (gimple_bb (stmt)))
1355 cfg_changed = true;
1358 return cfg_changed;
1361 /* To evaluate powi(x,n), the floating point value x raised to the
1362 constant integer exponent n, we use a hybrid algorithm that
1363 combines the "window method" with look-up tables. For an
1364 introduction to exponentiation algorithms and "addition chains",
1365 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
1366 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
1367 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
1368 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
1370 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
1371 multiplications to inline before calling the system library's pow
1372 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
1373 so this default never requires calling pow, powf or powl. */
1375 #ifndef POWI_MAX_MULTS
1376 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
1377 #endif
1379 /* The size of the "optimal power tree" lookup table. All
1380 exponents less than this value are simply looked up in the
1381 powi_table below. This threshold is also used to size the
1382 cache of pseudo registers that hold intermediate results. */
1383 #define POWI_TABLE_SIZE 256
1385 /* The size, in bits of the window, used in the "window method"
1386 exponentiation algorithm. This is equivalent to a radix of
1387 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
1388 #define POWI_WINDOW_SIZE 3
1390 /* The following table is an efficient representation of an
1391 "optimal power tree". For each value, i, the corresponding
1392 value, j, in the table states than an optimal evaluation
1393 sequence for calculating pow(x,i) can be found by evaluating
1394 pow(x,j)*pow(x,i-j). An optimal power tree for the first
1395 100 integers is given in Knuth's "Seminumerical algorithms". */
1397 static const unsigned char powi_table[POWI_TABLE_SIZE] =
1399 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1400 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1401 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1402 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1403 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1404 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1405 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
1406 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
1407 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
1408 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
1409 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
1410 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
1411 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
1412 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
1413 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
1414 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
1415 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
1416 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
1417 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
1418 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
1419 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
1420 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
1421 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
1422 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
1423 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
1424 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
1425 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
1426 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
1427 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
1428 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
1429 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
1430 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
1434 /* Return the number of multiplications required to calculate
1435 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
1436 subroutine of powi_cost. CACHE is an array indicating
1437 which exponents have already been calculated. */
1439 static int
1440 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
1442 /* If we've already calculated this exponent, then this evaluation
1443 doesn't require any additional multiplications. */
1444 if (cache[n])
1445 return 0;
1447 cache[n] = true;
1448 return powi_lookup_cost (n - powi_table[n], cache)
1449 + powi_lookup_cost (powi_table[n], cache) + 1;
1452 /* Return the number of multiplications required to calculate
1453 powi(x,n) for an arbitrary x, given the exponent N. This
1454 function needs to be kept in sync with powi_as_mults below. */
1456 static int
1457 powi_cost (HOST_WIDE_INT n)
1459 bool cache[POWI_TABLE_SIZE];
1460 unsigned HOST_WIDE_INT digit;
1461 unsigned HOST_WIDE_INT val;
1462 int result;
1464 if (n == 0)
1465 return 0;
1467 /* Ignore the reciprocal when calculating the cost. */
1468 val = absu_hwi (n);
1470 /* Initialize the exponent cache. */
1471 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
1472 cache[1] = true;
1474 result = 0;
1476 while (val >= POWI_TABLE_SIZE)
1478 if (val & 1)
1480 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
1481 result += powi_lookup_cost (digit, cache)
1482 + POWI_WINDOW_SIZE + 1;
1483 val >>= POWI_WINDOW_SIZE;
1485 else
1487 val >>= 1;
1488 result++;
1492 return result + powi_lookup_cost (val, cache);
1495 /* Recursive subroutine of powi_as_mults. This function takes the
1496 array, CACHE, of already calculated exponents and an exponent N and
1497 returns a tree that corresponds to CACHE[1]**N, with type TYPE. */
1499 static tree
1500 powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type,
1501 unsigned HOST_WIDE_INT n, tree *cache)
1503 tree op0, op1, ssa_target;
1504 unsigned HOST_WIDE_INT digit;
1505 gassign *mult_stmt;
1507 if (n < POWI_TABLE_SIZE && cache[n])
1508 return cache[n];
1510 ssa_target = make_temp_ssa_name (type, NULL, "powmult");
1512 if (n < POWI_TABLE_SIZE)
1514 cache[n] = ssa_target;
1515 op0 = powi_as_mults_1 (gsi, loc, type, n - powi_table[n], cache);
1516 op1 = powi_as_mults_1 (gsi, loc, type, powi_table[n], cache);
1518 else if (n & 1)
1520 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
1521 op0 = powi_as_mults_1 (gsi, loc, type, n - digit, cache);
1522 op1 = powi_as_mults_1 (gsi, loc, type, digit, cache);
1524 else
1526 op0 = powi_as_mults_1 (gsi, loc, type, n >> 1, cache);
1527 op1 = op0;
1530 mult_stmt = gimple_build_assign (ssa_target, MULT_EXPR, op0, op1);
1531 gimple_set_location (mult_stmt, loc);
1532 gsi_insert_before (gsi, mult_stmt, GSI_SAME_STMT);
1534 return ssa_target;
1537 /* Convert ARG0**N to a tree of multiplications of ARG0 with itself.
1538 This function needs to be kept in sync with powi_cost above. */
1540 tree
1541 powi_as_mults (gimple_stmt_iterator *gsi, location_t loc,
1542 tree arg0, HOST_WIDE_INT n)
1544 tree cache[POWI_TABLE_SIZE], result, type = TREE_TYPE (arg0);
1545 gassign *div_stmt;
1546 tree target;
1548 if (n == 0)
1549 return build_one_cst (type);
1551 memset (cache, 0, sizeof (cache));
1552 cache[1] = arg0;
1554 result = powi_as_mults_1 (gsi, loc, type, absu_hwi (n), cache);
1555 if (n >= 0)
1556 return result;
1558 /* If the original exponent was negative, reciprocate the result. */
1559 target = make_temp_ssa_name (type, NULL, "powmult");
1560 div_stmt = gimple_build_assign (target, RDIV_EXPR,
1561 build_real (type, dconst1), result);
1562 gimple_set_location (div_stmt, loc);
1563 gsi_insert_before (gsi, div_stmt, GSI_SAME_STMT);
1565 return target;
1568 /* ARG0 and N are the two arguments to a powi builtin in GSI with
1569 location info LOC. If the arguments are appropriate, create an
1570 equivalent sequence of statements prior to GSI using an optimal
1571 number of multiplications, and return an expession holding the
1572 result. */
1574 static tree
1575 gimple_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc,
1576 tree arg0, HOST_WIDE_INT n)
1578 if ((n >= -1 && n <= 2)
1579 || (optimize_function_for_speed_p (cfun)
1580 && powi_cost (n) <= POWI_MAX_MULTS))
1581 return powi_as_mults (gsi, loc, arg0, n);
1583 return NULL_TREE;
1586 /* Build a gimple call statement that calls FN with argument ARG.
1587 Set the lhs of the call statement to a fresh SSA name. Insert the
1588 statement prior to GSI's current position, and return the fresh
1589 SSA name. */
1591 static tree
1592 build_and_insert_call (gimple_stmt_iterator *gsi, location_t loc,
1593 tree fn, tree arg)
1595 gcall *call_stmt;
1596 tree ssa_target;
1598 call_stmt = gimple_build_call (fn, 1, arg);
1599 ssa_target = make_temp_ssa_name (TREE_TYPE (arg), NULL, "powroot");
1600 gimple_set_lhs (call_stmt, ssa_target);
1601 gimple_set_location (call_stmt, loc);
1602 gsi_insert_before (gsi, call_stmt, GSI_SAME_STMT);
1604 return ssa_target;
1607 /* Build a gimple binary operation with the given CODE and arguments
1608 ARG0, ARG1, assigning the result to a new SSA name for variable
1609 TARGET. Insert the statement prior to GSI's current position, and
1610 return the fresh SSA name.*/
1612 static tree
1613 build_and_insert_binop (gimple_stmt_iterator *gsi, location_t loc,
1614 const char *name, enum tree_code code,
1615 tree arg0, tree arg1)
1617 tree result = make_temp_ssa_name (TREE_TYPE (arg0), NULL, name);
1618 gassign *stmt = gimple_build_assign (result, code, arg0, arg1);
1619 gimple_set_location (stmt, loc);
1620 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1621 return result;
1624 /* Build a gimple reference operation with the given CODE and argument
1625 ARG, assigning the result to a new SSA name of TYPE with NAME.
1626 Insert the statement prior to GSI's current position, and return
1627 the fresh SSA name. */
1629 static inline tree
1630 build_and_insert_ref (gimple_stmt_iterator *gsi, location_t loc, tree type,
1631 const char *name, enum tree_code code, tree arg0)
1633 tree result = make_temp_ssa_name (type, NULL, name);
1634 gimple *stmt = gimple_build_assign (result, build1 (code, type, arg0));
1635 gimple_set_location (stmt, loc);
1636 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1637 return result;
1640 /* Build a gimple assignment to cast VAL to TYPE. Insert the statement
1641 prior to GSI's current position, and return the fresh SSA name. */
1643 static tree
1644 build_and_insert_cast (gimple_stmt_iterator *gsi, location_t loc,
1645 tree type, tree val)
1647 tree result = make_ssa_name (type);
1648 gassign *stmt = gimple_build_assign (result, NOP_EXPR, val);
1649 gimple_set_location (stmt, loc);
1650 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1651 return result;
1654 struct pow_synth_sqrt_info
1656 bool *factors;
1657 unsigned int deepest;
1658 unsigned int num_mults;
1661 /* Return true iff the real value C can be represented as a
1662 sum of powers of 0.5 up to N. That is:
1663 C == SUM<i from 1..N> (a[i]*(0.5**i)) where a[i] is either 0 or 1.
1664 Record in INFO the various parameters of the synthesis algorithm such
1665 as the factors a[i], the maximum 0.5 power and the number of
1666 multiplications that will be required. */
1668 bool
1669 representable_as_half_series_p (REAL_VALUE_TYPE c, unsigned n,
1670 struct pow_synth_sqrt_info *info)
1672 REAL_VALUE_TYPE factor = dconsthalf;
1673 REAL_VALUE_TYPE remainder = c;
1675 info->deepest = 0;
1676 info->num_mults = 0;
1677 memset (info->factors, 0, n * sizeof (bool));
1679 for (unsigned i = 0; i < n; i++)
1681 REAL_VALUE_TYPE res;
1683 /* If something inexact happened bail out now. */
1684 if (real_arithmetic (&res, MINUS_EXPR, &remainder, &factor))
1685 return false;
1687 /* We have hit zero. The number is representable as a sum
1688 of powers of 0.5. */
1689 if (real_equal (&res, &dconst0))
1691 info->factors[i] = true;
1692 info->deepest = i + 1;
1693 return true;
1695 else if (!REAL_VALUE_NEGATIVE (res))
1697 remainder = res;
1698 info->factors[i] = true;
1699 info->num_mults++;
1701 else
1702 info->factors[i] = false;
1704 real_arithmetic (&factor, MULT_EXPR, &factor, &dconsthalf);
1706 return false;
1709 /* Return the tree corresponding to FN being applied
1710 to ARG N times at GSI and LOC.
1711 Look up previous results from CACHE if need be.
1712 cache[0] should contain just plain ARG i.e. FN applied to ARG 0 times. */
1714 static tree
1715 get_fn_chain (tree arg, unsigned int n, gimple_stmt_iterator *gsi,
1716 tree fn, location_t loc, tree *cache)
1718 tree res = cache[n];
1719 if (!res)
1721 tree prev = get_fn_chain (arg, n - 1, gsi, fn, loc, cache);
1722 res = build_and_insert_call (gsi, loc, fn, prev);
1723 cache[n] = res;
1726 return res;
1729 /* Print to STREAM the repeated application of function FNAME to ARG
1730 N times. So, for FNAME = "foo", ARG = "x", N = 2 it would print:
1731 "foo (foo (x))". */
1733 static void
1734 print_nested_fn (FILE* stream, const char *fname, const char* arg,
1735 unsigned int n)
1737 if (n == 0)
1738 fprintf (stream, "%s", arg);
1739 else
1741 fprintf (stream, "%s (", fname);
1742 print_nested_fn (stream, fname, arg, n - 1);
1743 fprintf (stream, ")");
1747 /* Print to STREAM the fractional sequence of sqrt chains
1748 applied to ARG, described by INFO. Used for the dump file. */
1750 static void
1751 dump_fractional_sqrt_sequence (FILE *stream, const char *arg,
1752 struct pow_synth_sqrt_info *info)
1754 for (unsigned int i = 0; i < info->deepest; i++)
1756 bool is_set = info->factors[i];
1757 if (is_set)
1759 print_nested_fn (stream, "sqrt", arg, i + 1);
1760 if (i != info->deepest - 1)
1761 fprintf (stream, " * ");
1766 /* Print to STREAM a representation of raising ARG to an integer
1767 power N. Used for the dump file. */
1769 static void
1770 dump_integer_part (FILE *stream, const char* arg, HOST_WIDE_INT n)
1772 if (n > 1)
1773 fprintf (stream, "powi (%s, " HOST_WIDE_INT_PRINT_DEC ")", arg, n);
1774 else if (n == 1)
1775 fprintf (stream, "%s", arg);
1778 /* Attempt to synthesize a POW[F] (ARG0, ARG1) call using chains of
1779 square roots. Place at GSI and LOC. Limit the maximum depth
1780 of the sqrt chains to MAX_DEPTH. Return the tree holding the
1781 result of the expanded sequence or NULL_TREE if the expansion failed.
1783 This routine assumes that ARG1 is a real number with a fractional part
1784 (the integer exponent case will have been handled earlier in
1785 gimple_expand_builtin_pow).
1787 For ARG1 > 0.0:
1788 * For ARG1 composed of a whole part WHOLE_PART and a fractional part
1789 FRAC_PART i.e. WHOLE_PART == floor (ARG1) and
1790 FRAC_PART == ARG1 - WHOLE_PART:
1791 Produce POWI (ARG0, WHOLE_PART) * POW (ARG0, FRAC_PART) where
1792 POW (ARG0, FRAC_PART) is expanded as a product of square root chains
1793 if it can be expressed as such, that is if FRAC_PART satisfies:
1794 FRAC_PART == <SUM from i = 1 until MAX_DEPTH> (a[i] * (0.5**i))
1795 where integer a[i] is either 0 or 1.
1797 Example:
1798 POW (x, 3.625) == POWI (x, 3) * POW (x, 0.625)
1799 --> POWI (x, 3) * SQRT (x) * SQRT (SQRT (SQRT (x)))
1801 For ARG1 < 0.0 there are two approaches:
1802 * (A) Expand to 1.0 / POW (ARG0, -ARG1) where POW (ARG0, -ARG1)
1803 is calculated as above.
1805 Example:
1806 POW (x, -5.625) == 1.0 / POW (x, 5.625)
1807 --> 1.0 / (POWI (x, 5) * SQRT (x) * SQRT (SQRT (SQRT (x))))
1809 * (B) : WHOLE_PART := - ceil (abs (ARG1))
1810 FRAC_PART := ARG1 - WHOLE_PART
1811 and expand to POW (x, FRAC_PART) / POWI (x, WHOLE_PART).
1812 Example:
1813 POW (x, -5.875) == POW (x, 0.125) / POWI (X, 6)
1814 --> SQRT (SQRT (SQRT (x))) / (POWI (x, 6))
1816 For ARG1 < 0.0 we choose between (A) and (B) depending on
1817 how many multiplications we'd have to do.
1818 So, for the example in (B): POW (x, -5.875), if we were to
1819 follow algorithm (A) we would produce:
1820 1.0 / POWI (X, 5) * SQRT (X) * SQRT (SQRT (X)) * SQRT (SQRT (SQRT (X)))
1821 which contains more multiplications than approach (B).
1823 Hopefully, this approach will eliminate potentially expensive POW library
1824 calls when unsafe floating point math is enabled and allow the compiler to
1825 further optimise the multiplies, square roots and divides produced by this
1826 function. */
1828 static tree
1829 expand_pow_as_sqrts (gimple_stmt_iterator *gsi, location_t loc,
1830 tree arg0, tree arg1, HOST_WIDE_INT max_depth)
1832 tree type = TREE_TYPE (arg0);
1833 machine_mode mode = TYPE_MODE (type);
1834 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
1835 bool one_over = true;
1837 if (!sqrtfn)
1838 return NULL_TREE;
1840 if (TREE_CODE (arg1) != REAL_CST)
1841 return NULL_TREE;
1843 REAL_VALUE_TYPE exp_init = TREE_REAL_CST (arg1);
1845 gcc_assert (max_depth > 0);
1846 tree *cache = XALLOCAVEC (tree, max_depth + 1);
1848 struct pow_synth_sqrt_info synth_info;
1849 synth_info.factors = XALLOCAVEC (bool, max_depth + 1);
1850 synth_info.deepest = 0;
1851 synth_info.num_mults = 0;
1853 bool neg_exp = REAL_VALUE_NEGATIVE (exp_init);
1854 REAL_VALUE_TYPE exp = real_value_abs (&exp_init);
1856 /* The whole and fractional parts of exp. */
1857 REAL_VALUE_TYPE whole_part;
1858 REAL_VALUE_TYPE frac_part;
1860 real_floor (&whole_part, mode, &exp);
1861 real_arithmetic (&frac_part, MINUS_EXPR, &exp, &whole_part);
1864 REAL_VALUE_TYPE ceil_whole = dconst0;
1865 REAL_VALUE_TYPE ceil_fract = dconst0;
1867 if (neg_exp)
1869 real_ceil (&ceil_whole, mode, &exp);
1870 real_arithmetic (&ceil_fract, MINUS_EXPR, &ceil_whole, &exp);
1873 if (!representable_as_half_series_p (frac_part, max_depth, &synth_info))
1874 return NULL_TREE;
1876 /* Check whether it's more profitable to not use 1.0 / ... */
1877 if (neg_exp)
1879 struct pow_synth_sqrt_info alt_synth_info;
1880 alt_synth_info.factors = XALLOCAVEC (bool, max_depth + 1);
1881 alt_synth_info.deepest = 0;
1882 alt_synth_info.num_mults = 0;
1884 if (representable_as_half_series_p (ceil_fract, max_depth,
1885 &alt_synth_info)
1886 && alt_synth_info.deepest <= synth_info.deepest
1887 && alt_synth_info.num_mults < synth_info.num_mults)
1889 whole_part = ceil_whole;
1890 frac_part = ceil_fract;
1891 synth_info.deepest = alt_synth_info.deepest;
1892 synth_info.num_mults = alt_synth_info.num_mults;
1893 memcpy (synth_info.factors, alt_synth_info.factors,
1894 (max_depth + 1) * sizeof (bool));
1895 one_over = false;
1899 HOST_WIDE_INT n = real_to_integer (&whole_part);
1900 REAL_VALUE_TYPE cint;
1901 real_from_integer (&cint, VOIDmode, n, SIGNED);
1903 if (!real_identical (&whole_part, &cint))
1904 return NULL_TREE;
1906 if (powi_cost (n) + synth_info.num_mults > POWI_MAX_MULTS)
1907 return NULL_TREE;
1909 memset (cache, 0, (max_depth + 1) * sizeof (tree));
1911 tree integer_res = n == 0 ? build_real (type, dconst1) : arg0;
1913 /* Calculate the integer part of the exponent. */
1914 if (n > 1)
1916 integer_res = gimple_expand_builtin_powi (gsi, loc, arg0, n);
1917 if (!integer_res)
1918 return NULL_TREE;
1921 if (dump_file)
1923 char string[64];
1925 real_to_decimal (string, &exp_init, sizeof (string), 0, 1);
1926 fprintf (dump_file, "synthesizing pow (x, %s) as:\n", string);
1928 if (neg_exp)
1930 if (one_over)
1932 fprintf (dump_file, "1.0 / (");
1933 dump_integer_part (dump_file, "x", n);
1934 if (n > 0)
1935 fprintf (dump_file, " * ");
1936 dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1937 fprintf (dump_file, ")");
1939 else
1941 dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1942 fprintf (dump_file, " / (");
1943 dump_integer_part (dump_file, "x", n);
1944 fprintf (dump_file, ")");
1947 else
1949 dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1950 if (n > 0)
1951 fprintf (dump_file, " * ");
1952 dump_integer_part (dump_file, "x", n);
1955 fprintf (dump_file, "\ndeepest sqrt chain: %d\n", synth_info.deepest);
1959 tree fract_res = NULL_TREE;
1960 cache[0] = arg0;
1962 /* Calculate the fractional part of the exponent. */
1963 for (unsigned i = 0; i < synth_info.deepest; i++)
1965 if (synth_info.factors[i])
1967 tree sqrt_chain = get_fn_chain (arg0, i + 1, gsi, sqrtfn, loc, cache);
1969 if (!fract_res)
1970 fract_res = sqrt_chain;
1972 else
1973 fract_res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1974 fract_res, sqrt_chain);
1978 tree res = NULL_TREE;
1980 if (neg_exp)
1982 if (one_over)
1984 if (n > 0)
1985 res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1986 fract_res, integer_res);
1987 else
1988 res = fract_res;
1990 res = build_and_insert_binop (gsi, loc, "powrootrecip", RDIV_EXPR,
1991 build_real (type, dconst1), res);
1993 else
1995 res = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR,
1996 fract_res, integer_res);
1999 else
2000 res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
2001 fract_res, integer_res);
2002 return res;
2005 /* ARG0 and ARG1 are the two arguments to a pow builtin call in GSI
2006 with location info LOC. If possible, create an equivalent and
2007 less expensive sequence of statements prior to GSI, and return an
2008 expession holding the result. */
2010 static tree
2011 gimple_expand_builtin_pow (gimple_stmt_iterator *gsi, location_t loc,
2012 tree arg0, tree arg1)
2014 REAL_VALUE_TYPE c, cint, dconst1_3, dconst1_4, dconst1_6;
2015 REAL_VALUE_TYPE c2, dconst3;
2016 HOST_WIDE_INT n;
2017 tree type, sqrtfn, cbrtfn, sqrt_arg0, result, cbrt_x, powi_cbrt_x;
2018 machine_mode mode;
2019 bool speed_p = optimize_bb_for_speed_p (gsi_bb (*gsi));
2020 bool hw_sqrt_exists, c_is_int, c2_is_int;
2022 dconst1_4 = dconst1;
2023 SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
2025 /* If the exponent isn't a constant, there's nothing of interest
2026 to be done. */
2027 if (TREE_CODE (arg1) != REAL_CST)
2028 return NULL_TREE;
2030 /* Don't perform the operation if flag_signaling_nans is on
2031 and the operand is a signaling NaN. */
2032 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2033 && ((TREE_CODE (arg0) == REAL_CST
2034 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0)))
2035 || REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1))))
2036 return NULL_TREE;
2038 /* If the exponent is equivalent to an integer, expand to an optimal
2039 multiplication sequence when profitable. */
2040 c = TREE_REAL_CST (arg1);
2041 n = real_to_integer (&c);
2042 real_from_integer (&cint, VOIDmode, n, SIGNED);
2043 c_is_int = real_identical (&c, &cint);
2045 if (c_is_int
2046 && ((n >= -1 && n <= 2)
2047 || (flag_unsafe_math_optimizations
2048 && speed_p
2049 && powi_cost (n) <= POWI_MAX_MULTS)))
2050 return gimple_expand_builtin_powi (gsi, loc, arg0, n);
2052 /* Attempt various optimizations using sqrt and cbrt. */
2053 type = TREE_TYPE (arg0);
2054 mode = TYPE_MODE (type);
2055 sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
2057 /* Optimize pow(x,0.5) = sqrt(x). This replacement is always safe
2058 unless signed zeros must be maintained. pow(-0,0.5) = +0, while
2059 sqrt(-0) = -0. */
2060 if (sqrtfn
2061 && real_equal (&c, &dconsthalf)
2062 && !HONOR_SIGNED_ZEROS (mode))
2063 return build_and_insert_call (gsi, loc, sqrtfn, arg0);
2065 hw_sqrt_exists = optab_handler (sqrt_optab, mode) != CODE_FOR_nothing;
2067 /* Optimize pow(x,1./3.) = cbrt(x). This requires unsafe math
2068 optimizations since 1./3. is not exactly representable. If x
2069 is negative and finite, the correct value of pow(x,1./3.) is
2070 a NaN with the "invalid" exception raised, because the value
2071 of 1./3. actually has an even denominator. The correct value
2072 of cbrt(x) is a negative real value. */
2073 cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
2074 dconst1_3 = real_value_truncate (mode, dconst_third ());
2076 if (flag_unsafe_math_optimizations
2077 && cbrtfn
2078 && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
2079 && real_equal (&c, &dconst1_3))
2080 return build_and_insert_call (gsi, loc, cbrtfn, arg0);
2082 /* Optimize pow(x,1./6.) = cbrt(sqrt(x)). Don't do this optimization
2083 if we don't have a hardware sqrt insn. */
2084 dconst1_6 = dconst1_3;
2085 SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
2087 if (flag_unsafe_math_optimizations
2088 && sqrtfn
2089 && cbrtfn
2090 && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
2091 && speed_p
2092 && hw_sqrt_exists
2093 && real_equal (&c, &dconst1_6))
2095 /* sqrt(x) */
2096 sqrt_arg0 = build_and_insert_call (gsi, loc, sqrtfn, arg0);
2098 /* cbrt(sqrt(x)) */
2099 return build_and_insert_call (gsi, loc, cbrtfn, sqrt_arg0);
2103 /* Attempt to expand the POW as a product of square root chains.
2104 Expand the 0.25 case even when otpimising for size. */
2105 if (flag_unsafe_math_optimizations
2106 && sqrtfn
2107 && hw_sqrt_exists
2108 && (speed_p || real_equal (&c, &dconst1_4))
2109 && !HONOR_SIGNED_ZEROS (mode))
2111 unsigned int max_depth = speed_p
2112 ? param_max_pow_sqrt_depth
2113 : 2;
2115 tree expand_with_sqrts
2116 = expand_pow_as_sqrts (gsi, loc, arg0, arg1, max_depth);
2118 if (expand_with_sqrts)
2119 return expand_with_sqrts;
2122 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2123 n = real_to_integer (&c2);
2124 real_from_integer (&cint, VOIDmode, n, SIGNED);
2125 c2_is_int = real_identical (&c2, &cint);
2127 /* Optimize pow(x,c), where 3c = n for some nonzero integer n, into
2129 powi(x, n/3) * powi(cbrt(x), n%3), n > 0;
2130 1.0 / (powi(x, abs(n)/3) * powi(cbrt(x), abs(n)%3)), n < 0.
2132 Do not calculate the first factor when n/3 = 0. As cbrt(x) is
2133 different from pow(x, 1./3.) due to rounding and behavior with
2134 negative x, we need to constrain this transformation to unsafe
2135 math and positive x or finite math. */
2136 real_from_integer (&dconst3, VOIDmode, 3, SIGNED);
2137 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2138 real_round (&c2, mode, &c2);
2139 n = real_to_integer (&c2);
2140 real_from_integer (&cint, VOIDmode, n, SIGNED);
2141 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2142 real_convert (&c2, mode, &c2);
2144 if (flag_unsafe_math_optimizations
2145 && cbrtfn
2146 && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
2147 && real_identical (&c2, &c)
2148 && !c2_is_int
2149 && optimize_function_for_speed_p (cfun)
2150 && powi_cost (n / 3) <= POWI_MAX_MULTS)
2152 tree powi_x_ndiv3 = NULL_TREE;
2154 /* Attempt to fold powi(arg0, abs(n/3)) into multiplies. If not
2155 possible or profitable, give up. Skip the degenerate case when
2156 abs(n) < 3, where the result is always 1. */
2157 if (absu_hwi (n) >= 3)
2159 powi_x_ndiv3 = gimple_expand_builtin_powi (gsi, loc, arg0,
2160 abs_hwi (n / 3));
2161 if (!powi_x_ndiv3)
2162 return NULL_TREE;
2165 /* Calculate powi(cbrt(x), n%3). Don't use gimple_expand_builtin_powi
2166 as that creates an unnecessary variable. Instead, just produce
2167 either cbrt(x) or cbrt(x) * cbrt(x). */
2168 cbrt_x = build_and_insert_call (gsi, loc, cbrtfn, arg0);
2170 if (absu_hwi (n) % 3 == 1)
2171 powi_cbrt_x = cbrt_x;
2172 else
2173 powi_cbrt_x = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
2174 cbrt_x, cbrt_x);
2176 /* Multiply the two subexpressions, unless powi(x,abs(n)/3) = 1. */
2177 if (absu_hwi (n) < 3)
2178 result = powi_cbrt_x;
2179 else
2180 result = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
2181 powi_x_ndiv3, powi_cbrt_x);
2183 /* If n is negative, reciprocate the result. */
2184 if (n < 0)
2185 result = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR,
2186 build_real (type, dconst1), result);
2188 return result;
2191 /* No optimizations succeeded. */
2192 return NULL_TREE;
2195 /* ARG is the argument to a cabs builtin call in GSI with location info
2196 LOC. Create a sequence of statements prior to GSI that calculates
2197 sqrt(R*R + I*I), where R and I are the real and imaginary components
2198 of ARG, respectively. Return an expression holding the result. */
2200 static tree
2201 gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, location_t loc, tree arg)
2203 tree real_part, imag_part, addend1, addend2, sum, result;
2204 tree type = TREE_TYPE (TREE_TYPE (arg));
2205 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
2206 machine_mode mode = TYPE_MODE (type);
2208 if (!flag_unsafe_math_optimizations
2209 || !optimize_bb_for_speed_p (gimple_bb (gsi_stmt (*gsi)))
2210 || !sqrtfn
2211 || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing)
2212 return NULL_TREE;
2214 real_part = build_and_insert_ref (gsi, loc, type, "cabs",
2215 REALPART_EXPR, arg);
2216 addend1 = build_and_insert_binop (gsi, loc, "cabs", MULT_EXPR,
2217 real_part, real_part);
2218 imag_part = build_and_insert_ref (gsi, loc, type, "cabs",
2219 IMAGPART_EXPR, arg);
2220 addend2 = build_and_insert_binop (gsi, loc, "cabs", MULT_EXPR,
2221 imag_part, imag_part);
2222 sum = build_and_insert_binop (gsi, loc, "cabs", PLUS_EXPR, addend1, addend2);
2223 result = build_and_insert_call (gsi, loc, sqrtfn, sum);
2225 return result;
2228 /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1
2229 on the SSA_NAME argument of each of them. */
2231 namespace {
2233 const pass_data pass_data_cse_sincos =
2235 GIMPLE_PASS, /* type */
2236 "sincos", /* name */
2237 OPTGROUP_NONE, /* optinfo_flags */
2238 TV_TREE_SINCOS, /* tv_id */
2239 PROP_ssa, /* properties_required */
2240 0, /* properties_provided */
2241 0, /* properties_destroyed */
2242 0, /* todo_flags_start */
2243 TODO_update_ssa, /* todo_flags_finish */
2246 class pass_cse_sincos : public gimple_opt_pass
2248 public:
2249 pass_cse_sincos (gcc::context *ctxt)
2250 : gimple_opt_pass (pass_data_cse_sincos, ctxt)
2253 /* opt_pass methods: */
2254 bool gate (function *) final override
2256 return optimize;
2259 unsigned int execute (function *) final override;
2261 }; // class pass_cse_sincos
2263 unsigned int
2264 pass_cse_sincos::execute (function *fun)
2266 basic_block bb;
2267 bool cfg_changed = false;
2269 calculate_dominance_info (CDI_DOMINATORS);
2270 memset (&sincos_stats, 0, sizeof (sincos_stats));
2272 FOR_EACH_BB_FN (bb, fun)
2274 gimple_stmt_iterator gsi;
2276 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2278 gimple *stmt = gsi_stmt (gsi);
2280 if (is_gimple_call (stmt)
2281 && gimple_call_lhs (stmt))
2283 tree arg;
2284 switch (gimple_call_combined_fn (stmt))
2286 CASE_CFN_COS:
2287 CASE_CFN_SIN:
2288 CASE_CFN_CEXPI:
2289 arg = gimple_call_arg (stmt, 0);
2290 /* Make sure we have either sincos or cexp. */
2291 if (!targetm.libc_has_function (function_c99_math_complex,
2292 TREE_TYPE (arg))
2293 && !targetm.libc_has_function (function_sincos,
2294 TREE_TYPE (arg)))
2295 break;
2297 if (TREE_CODE (arg) == SSA_NAME)
2298 cfg_changed |= execute_cse_sincos_1 (arg);
2299 break;
2300 default:
2301 break;
2307 statistics_counter_event (fun, "sincos statements inserted",
2308 sincos_stats.inserted);
2309 statistics_counter_event (fun, "conv statements removed",
2310 sincos_stats.conv_removed);
2312 return cfg_changed ? TODO_cleanup_cfg : 0;
2315 } // anon namespace
2317 gimple_opt_pass *
2318 make_pass_cse_sincos (gcc::context *ctxt)
2320 return new pass_cse_sincos (ctxt);
2323 /* Expand powi(x,n) into an optimal number of multiplies, when n is a constant.
2324 Also expand CABS. */
2325 namespace {
2327 const pass_data pass_data_expand_powcabs =
2329 GIMPLE_PASS, /* type */
2330 "powcabs", /* name */
2331 OPTGROUP_NONE, /* optinfo_flags */
2332 TV_TREE_POWCABS, /* tv_id */
2333 PROP_ssa, /* properties_required */
2334 PROP_gimple_opt_math, /* properties_provided */
2335 0, /* properties_destroyed */
2336 0, /* todo_flags_start */
2337 TODO_update_ssa, /* todo_flags_finish */
2340 class pass_expand_powcabs : public gimple_opt_pass
2342 public:
2343 pass_expand_powcabs (gcc::context *ctxt)
2344 : gimple_opt_pass (pass_data_expand_powcabs, ctxt)
2347 /* opt_pass methods: */
2348 bool gate (function *) final override
2350 return optimize;
2353 unsigned int execute (function *) final override;
2355 }; // class pass_expand_powcabs
2357 unsigned int
2358 pass_expand_powcabs::execute (function *fun)
2360 basic_block bb;
2361 bool cfg_changed = false;
2363 calculate_dominance_info (CDI_DOMINATORS);
2365 FOR_EACH_BB_FN (bb, fun)
2367 gimple_stmt_iterator gsi;
2368 bool cleanup_eh = false;
2370 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2372 gimple *stmt = gsi_stmt (gsi);
2374 /* Only the last stmt in a bb could throw, no need to call
2375 gimple_purge_dead_eh_edges if we change something in the middle
2376 of a basic block. */
2377 cleanup_eh = false;
2379 if (is_gimple_call (stmt)
2380 && gimple_call_lhs (stmt))
2382 tree arg0, arg1, result;
2383 HOST_WIDE_INT n;
2384 location_t loc;
2386 switch (gimple_call_combined_fn (stmt))
2388 CASE_CFN_POW:
2389 arg0 = gimple_call_arg (stmt, 0);
2390 arg1 = gimple_call_arg (stmt, 1);
2392 loc = gimple_location (stmt);
2393 result = gimple_expand_builtin_pow (&gsi, loc, arg0, arg1);
2395 if (result)
2397 tree lhs = gimple_get_lhs (stmt);
2398 gassign *new_stmt = gimple_build_assign (lhs, result);
2399 gimple_set_location (new_stmt, loc);
2400 unlink_stmt_vdef (stmt);
2401 gsi_replace (&gsi, new_stmt, true);
2402 cleanup_eh = true;
2403 if (gimple_vdef (stmt))
2404 release_ssa_name (gimple_vdef (stmt));
2406 break;
2408 CASE_CFN_POWI:
2409 arg0 = gimple_call_arg (stmt, 0);
2410 arg1 = gimple_call_arg (stmt, 1);
2411 loc = gimple_location (stmt);
2413 if (real_minus_onep (arg0))
2415 tree t0, t1, cond, one, minus_one;
2416 gassign *stmt;
2418 t0 = TREE_TYPE (arg0);
2419 t1 = TREE_TYPE (arg1);
2420 one = build_real (t0, dconst1);
2421 minus_one = build_real (t0, dconstm1);
2423 cond = make_temp_ssa_name (t1, NULL, "powi_cond");
2424 stmt = gimple_build_assign (cond, BIT_AND_EXPR,
2425 arg1, build_int_cst (t1, 1));
2426 gimple_set_location (stmt, loc);
2427 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
2429 result = make_temp_ssa_name (t0, NULL, "powi");
2430 stmt = gimple_build_assign (result, COND_EXPR, cond,
2431 minus_one, one);
2432 gimple_set_location (stmt, loc);
2433 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
2435 else
2437 if (!tree_fits_shwi_p (arg1))
2438 break;
2440 n = tree_to_shwi (arg1);
2441 result = gimple_expand_builtin_powi (&gsi, loc, arg0, n);
2444 if (result)
2446 tree lhs = gimple_get_lhs (stmt);
2447 gassign *new_stmt = gimple_build_assign (lhs, result);
2448 gimple_set_location (new_stmt, loc);
2449 unlink_stmt_vdef (stmt);
2450 gsi_replace (&gsi, new_stmt, true);
2451 cleanup_eh = true;
2452 if (gimple_vdef (stmt))
2453 release_ssa_name (gimple_vdef (stmt));
2455 break;
2457 CASE_CFN_CABS:
2458 arg0 = gimple_call_arg (stmt, 0);
2459 loc = gimple_location (stmt);
2460 result = gimple_expand_builtin_cabs (&gsi, loc, arg0);
2462 if (result)
2464 tree lhs = gimple_get_lhs (stmt);
2465 gassign *new_stmt = gimple_build_assign (lhs, result);
2466 gimple_set_location (new_stmt, loc);
2467 unlink_stmt_vdef (stmt);
2468 gsi_replace (&gsi, new_stmt, true);
2469 cleanup_eh = true;
2470 if (gimple_vdef (stmt))
2471 release_ssa_name (gimple_vdef (stmt));
2473 break;
2475 default:;
2479 if (cleanup_eh)
2480 cfg_changed |= gimple_purge_dead_eh_edges (bb);
2483 return cfg_changed ? TODO_cleanup_cfg : 0;
2486 } // anon namespace
2488 gimple_opt_pass *
2489 make_pass_expand_powcabs (gcc::context *ctxt)
2491 return new pass_expand_powcabs (ctxt);
2494 /* Return true if stmt is a type conversion operation that can be stripped
2495 when used in a widening multiply operation. */
2496 static bool
2497 widening_mult_conversion_strippable_p (tree result_type, gimple *stmt)
2499 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
2501 if (TREE_CODE (result_type) == INTEGER_TYPE)
2503 tree op_type;
2504 tree inner_op_type;
2506 if (!CONVERT_EXPR_CODE_P (rhs_code))
2507 return false;
2509 op_type = TREE_TYPE (gimple_assign_lhs (stmt));
2511 /* If the type of OP has the same precision as the result, then
2512 we can strip this conversion. The multiply operation will be
2513 selected to create the correct extension as a by-product. */
2514 if (TYPE_PRECISION (result_type) == TYPE_PRECISION (op_type))
2515 return true;
2517 /* We can also strip a conversion if it preserves the signed-ness of
2518 the operation and doesn't narrow the range. */
2519 inner_op_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
2521 /* If the inner-most type is unsigned, then we can strip any
2522 intermediate widening operation. If it's signed, then the
2523 intermediate widening operation must also be signed. */
2524 if ((TYPE_UNSIGNED (inner_op_type)
2525 || TYPE_UNSIGNED (op_type) == TYPE_UNSIGNED (inner_op_type))
2526 && TYPE_PRECISION (op_type) > TYPE_PRECISION (inner_op_type))
2527 return true;
2529 return false;
2532 return rhs_code == FIXED_CONVERT_EXPR;
2535 /* Return true if RHS is a suitable operand for a widening multiplication,
2536 assuming a target type of TYPE.
2537 There are two cases:
2539 - RHS makes some value at least twice as wide. Store that value
2540 in *NEW_RHS_OUT if so, and store its type in *TYPE_OUT.
2542 - RHS is an integer constant. Store that value in *NEW_RHS_OUT if so,
2543 but leave *TYPE_OUT untouched. */
2545 static bool
2546 is_widening_mult_rhs_p (tree type, tree rhs, tree *type_out,
2547 tree *new_rhs_out)
2549 gimple *stmt;
2550 tree type1, rhs1;
2552 if (TREE_CODE (rhs) == SSA_NAME)
2554 stmt = SSA_NAME_DEF_STMT (rhs);
2555 if (is_gimple_assign (stmt))
2557 if (! widening_mult_conversion_strippable_p (type, stmt))
2558 rhs1 = rhs;
2559 else
2561 rhs1 = gimple_assign_rhs1 (stmt);
2563 if (TREE_CODE (rhs1) == INTEGER_CST)
2565 *new_rhs_out = rhs1;
2566 *type_out = NULL;
2567 return true;
2571 else
2572 rhs1 = rhs;
2574 type1 = TREE_TYPE (rhs1);
2576 if (TREE_CODE (type1) != TREE_CODE (type)
2577 || TYPE_PRECISION (type1) * 2 > TYPE_PRECISION (type))
2578 return false;
2580 *new_rhs_out = rhs1;
2581 *type_out = type1;
2582 return true;
2585 if (TREE_CODE (rhs) == INTEGER_CST)
2587 *new_rhs_out = rhs;
2588 *type_out = NULL;
2589 return true;
2592 return false;
2595 /* Return true if STMT performs a widening multiplication, assuming the
2596 output type is TYPE. If so, store the unwidened types of the operands
2597 in *TYPE1_OUT and *TYPE2_OUT respectively. Also fill *RHS1_OUT and
2598 *RHS2_OUT such that converting those operands to types *TYPE1_OUT
2599 and *TYPE2_OUT would give the operands of the multiplication. */
2601 static bool
2602 is_widening_mult_p (gimple *stmt,
2603 tree *type1_out, tree *rhs1_out,
2604 tree *type2_out, tree *rhs2_out)
2606 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
2608 if (TREE_CODE (type) == INTEGER_TYPE)
2610 if (TYPE_OVERFLOW_TRAPS (type))
2611 return false;
2613 else if (TREE_CODE (type) != FIXED_POINT_TYPE)
2614 return false;
2616 if (!is_widening_mult_rhs_p (type, gimple_assign_rhs1 (stmt), type1_out,
2617 rhs1_out))
2618 return false;
2620 if (!is_widening_mult_rhs_p (type, gimple_assign_rhs2 (stmt), type2_out,
2621 rhs2_out))
2622 return false;
2624 if (*type1_out == NULL)
2626 if (*type2_out == NULL || !int_fits_type_p (*rhs1_out, *type2_out))
2627 return false;
2628 *type1_out = *type2_out;
2631 if (*type2_out == NULL)
2633 if (!int_fits_type_p (*rhs2_out, *type1_out))
2634 return false;
2635 *type2_out = *type1_out;
2638 /* Ensure that the larger of the two operands comes first. */
2639 if (TYPE_PRECISION (*type1_out) < TYPE_PRECISION (*type2_out))
2641 std::swap (*type1_out, *type2_out);
2642 std::swap (*rhs1_out, *rhs2_out);
2645 return true;
2648 /* Check to see if the CALL statement is an invocation of copysign
2649 with 1. being the first argument. */
2650 static bool
2651 is_copysign_call_with_1 (gimple *call)
2653 gcall *c = dyn_cast <gcall *> (call);
2654 if (! c)
2655 return false;
2657 enum combined_fn code = gimple_call_combined_fn (c);
2659 if (code == CFN_LAST)
2660 return false;
2662 if (builtin_fn_p (code))
2664 switch (as_builtin_fn (code))
2666 CASE_FLT_FN (BUILT_IN_COPYSIGN):
2667 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
2668 return real_onep (gimple_call_arg (c, 0));
2669 default:
2670 return false;
2674 if (internal_fn_p (code))
2676 switch (as_internal_fn (code))
2678 case IFN_COPYSIGN:
2679 return real_onep (gimple_call_arg (c, 0));
2680 default:
2681 return false;
2685 return false;
2688 /* Try to expand the pattern x * copysign (1, y) into xorsign (x, y).
2689 This only happens when the xorsign optab is defined, if the
2690 pattern is not a xorsign pattern or if expansion fails FALSE is
2691 returned, otherwise TRUE is returned. */
2692 static bool
2693 convert_expand_mult_copysign (gimple *stmt, gimple_stmt_iterator *gsi)
2695 tree treeop0, treeop1, lhs, type;
2696 location_t loc = gimple_location (stmt);
2697 lhs = gimple_assign_lhs (stmt);
2698 treeop0 = gimple_assign_rhs1 (stmt);
2699 treeop1 = gimple_assign_rhs2 (stmt);
2700 type = TREE_TYPE (lhs);
2701 machine_mode mode = TYPE_MODE (type);
2703 if (HONOR_SNANS (type))
2704 return false;
2706 if (TREE_CODE (treeop0) == SSA_NAME && TREE_CODE (treeop1) == SSA_NAME)
2708 gimple *call0 = SSA_NAME_DEF_STMT (treeop0);
2709 if (!has_single_use (treeop0) || !is_copysign_call_with_1 (call0))
2711 call0 = SSA_NAME_DEF_STMT (treeop1);
2712 if (!has_single_use (treeop1) || !is_copysign_call_with_1 (call0))
2713 return false;
2715 treeop1 = treeop0;
2717 if (optab_handler (xorsign_optab, mode) == CODE_FOR_nothing)
2718 return false;
2720 gcall *c = as_a<gcall*> (call0);
2721 treeop0 = gimple_call_arg (c, 1);
2723 gcall *call_stmt
2724 = gimple_build_call_internal (IFN_XORSIGN, 2, treeop1, treeop0);
2725 gimple_set_lhs (call_stmt, lhs);
2726 gimple_set_location (call_stmt, loc);
2727 gsi_replace (gsi, call_stmt, true);
2728 return true;
2731 return false;
2734 /* Process a single gimple statement STMT, which has a MULT_EXPR as
2735 its rhs, and try to convert it into a WIDEN_MULT_EXPR. The return
2736 value is true iff we converted the statement. */
2738 static bool
2739 convert_mult_to_widen (gimple *stmt, gimple_stmt_iterator *gsi)
2741 tree lhs, rhs1, rhs2, type, type1, type2;
2742 enum insn_code handler;
2743 scalar_int_mode to_mode, from_mode, actual_mode;
2744 optab op;
2745 int actual_precision;
2746 location_t loc = gimple_location (stmt);
2747 bool from_unsigned1, from_unsigned2;
2749 lhs = gimple_assign_lhs (stmt);
2750 type = TREE_TYPE (lhs);
2751 if (TREE_CODE (type) != INTEGER_TYPE)
2752 return false;
2754 if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2))
2755 return false;
2757 to_mode = SCALAR_INT_TYPE_MODE (type);
2758 from_mode = SCALAR_INT_TYPE_MODE (type1);
2759 if (to_mode == from_mode)
2760 return false;
2762 from_unsigned1 = TYPE_UNSIGNED (type1);
2763 from_unsigned2 = TYPE_UNSIGNED (type2);
2765 if (from_unsigned1 && from_unsigned2)
2766 op = umul_widen_optab;
2767 else if (!from_unsigned1 && !from_unsigned2)
2768 op = smul_widen_optab;
2769 else
2770 op = usmul_widen_optab;
2772 handler = find_widening_optab_handler_and_mode (op, to_mode, from_mode,
2773 &actual_mode);
2775 if (handler == CODE_FOR_nothing)
2777 if (op != smul_widen_optab)
2779 /* We can use a signed multiply with unsigned types as long as
2780 there is a wider mode to use, or it is the smaller of the two
2781 types that is unsigned. Note that type1 >= type2, always. */
2782 if ((TYPE_UNSIGNED (type1)
2783 && TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode))
2784 || (TYPE_UNSIGNED (type2)
2785 && TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode)))
2787 if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode)
2788 || GET_MODE_SIZE (to_mode) <= GET_MODE_SIZE (from_mode))
2789 return false;
2792 op = smul_widen_optab;
2793 handler = find_widening_optab_handler_and_mode (op, to_mode,
2794 from_mode,
2795 &actual_mode);
2797 if (handler == CODE_FOR_nothing)
2798 return false;
2800 from_unsigned1 = from_unsigned2 = false;
2802 else
2804 /* Expand can synthesize smul_widen_optab if the target
2805 supports umul_widen_optab. */
2806 op = umul_widen_optab;
2807 handler = find_widening_optab_handler_and_mode (op, to_mode,
2808 from_mode,
2809 &actual_mode);
2810 if (handler == CODE_FOR_nothing)
2811 return false;
2815 /* Ensure that the inputs to the handler are in the correct precison
2816 for the opcode. This will be the full mode size. */
2817 actual_precision = GET_MODE_PRECISION (actual_mode);
2818 if (2 * actual_precision > TYPE_PRECISION (type))
2819 return false;
2820 if (actual_precision != TYPE_PRECISION (type1)
2821 || from_unsigned1 != TYPE_UNSIGNED (type1))
2822 rhs1 = build_and_insert_cast (gsi, loc,
2823 build_nonstandard_integer_type
2824 (actual_precision, from_unsigned1), rhs1);
2825 if (actual_precision != TYPE_PRECISION (type2)
2826 || from_unsigned2 != TYPE_UNSIGNED (type2))
2827 rhs2 = build_and_insert_cast (gsi, loc,
2828 build_nonstandard_integer_type
2829 (actual_precision, from_unsigned2), rhs2);
2831 /* Handle constants. */
2832 if (TREE_CODE (rhs1) == INTEGER_CST)
2833 rhs1 = fold_convert (type1, rhs1);
2834 if (TREE_CODE (rhs2) == INTEGER_CST)
2835 rhs2 = fold_convert (type2, rhs2);
2837 gimple_assign_set_rhs1 (stmt, rhs1);
2838 gimple_assign_set_rhs2 (stmt, rhs2);
2839 gimple_assign_set_rhs_code (stmt, WIDEN_MULT_EXPR);
2840 update_stmt (stmt);
2841 widen_mul_stats.widen_mults_inserted++;
2842 return true;
2845 /* Process a single gimple statement STMT, which is found at the
2846 iterator GSI and has a either a PLUS_EXPR or a MINUS_EXPR as its
2847 rhs (given by CODE), and try to convert it into a
2848 WIDEN_MULT_PLUS_EXPR or a WIDEN_MULT_MINUS_EXPR. The return value
2849 is true iff we converted the statement. */
2851 static bool
2852 convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple *stmt,
2853 enum tree_code code)
2855 gimple *rhs1_stmt = NULL, *rhs2_stmt = NULL;
2856 gimple *conv1_stmt = NULL, *conv2_stmt = NULL, *conv_stmt;
2857 tree type, type1, type2, optype;
2858 tree lhs, rhs1, rhs2, mult_rhs1, mult_rhs2, add_rhs;
2859 enum tree_code rhs1_code = ERROR_MARK, rhs2_code = ERROR_MARK;
2860 optab this_optab;
2861 enum tree_code wmult_code;
2862 enum insn_code handler;
2863 scalar_mode to_mode, from_mode, actual_mode;
2864 location_t loc = gimple_location (stmt);
2865 int actual_precision;
2866 bool from_unsigned1, from_unsigned2;
2868 lhs = gimple_assign_lhs (stmt);
2869 type = TREE_TYPE (lhs);
2870 if (TREE_CODE (type) != INTEGER_TYPE
2871 && TREE_CODE (type) != FIXED_POINT_TYPE)
2872 return false;
2874 if (code == MINUS_EXPR)
2875 wmult_code = WIDEN_MULT_MINUS_EXPR;
2876 else
2877 wmult_code = WIDEN_MULT_PLUS_EXPR;
2879 rhs1 = gimple_assign_rhs1 (stmt);
2880 rhs2 = gimple_assign_rhs2 (stmt);
2882 if (TREE_CODE (rhs1) == SSA_NAME)
2884 rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
2885 if (is_gimple_assign (rhs1_stmt))
2886 rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
2889 if (TREE_CODE (rhs2) == SSA_NAME)
2891 rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
2892 if (is_gimple_assign (rhs2_stmt))
2893 rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
2896 /* Allow for one conversion statement between the multiply
2897 and addition/subtraction statement. If there are more than
2898 one conversions then we assume they would invalidate this
2899 transformation. If that's not the case then they should have
2900 been folded before now. */
2901 if (CONVERT_EXPR_CODE_P (rhs1_code))
2903 conv1_stmt = rhs1_stmt;
2904 rhs1 = gimple_assign_rhs1 (rhs1_stmt);
2905 if (TREE_CODE (rhs1) == SSA_NAME)
2907 rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
2908 if (is_gimple_assign (rhs1_stmt))
2909 rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
2911 else
2912 return false;
2914 if (CONVERT_EXPR_CODE_P (rhs2_code))
2916 conv2_stmt = rhs2_stmt;
2917 rhs2 = gimple_assign_rhs1 (rhs2_stmt);
2918 if (TREE_CODE (rhs2) == SSA_NAME)
2920 rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
2921 if (is_gimple_assign (rhs2_stmt))
2922 rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
2924 else
2925 return false;
2928 /* If code is WIDEN_MULT_EXPR then it would seem unnecessary to call
2929 is_widening_mult_p, but we still need the rhs returns.
2931 It might also appear that it would be sufficient to use the existing
2932 operands of the widening multiply, but that would limit the choice of
2933 multiply-and-accumulate instructions.
2935 If the widened-multiplication result has more than one uses, it is
2936 probably wiser not to do the conversion. Also restrict this operation
2937 to single basic block to avoid moving the multiply to a different block
2938 with a higher execution frequency. */
2939 if (code == PLUS_EXPR
2940 && (rhs1_code == MULT_EXPR || rhs1_code == WIDEN_MULT_EXPR))
2942 if (!has_single_use (rhs1)
2943 || gimple_bb (rhs1_stmt) != gimple_bb (stmt)
2944 || !is_widening_mult_p (rhs1_stmt, &type1, &mult_rhs1,
2945 &type2, &mult_rhs2))
2946 return false;
2947 add_rhs = rhs2;
2948 conv_stmt = conv1_stmt;
2950 else if (rhs2_code == MULT_EXPR || rhs2_code == WIDEN_MULT_EXPR)
2952 if (!has_single_use (rhs2)
2953 || gimple_bb (rhs2_stmt) != gimple_bb (stmt)
2954 || !is_widening_mult_p (rhs2_stmt, &type1, &mult_rhs1,
2955 &type2, &mult_rhs2))
2956 return false;
2957 add_rhs = rhs1;
2958 conv_stmt = conv2_stmt;
2960 else
2961 return false;
2963 to_mode = SCALAR_TYPE_MODE (type);
2964 from_mode = SCALAR_TYPE_MODE (type1);
2965 if (to_mode == from_mode)
2966 return false;
2968 from_unsigned1 = TYPE_UNSIGNED (type1);
2969 from_unsigned2 = TYPE_UNSIGNED (type2);
2970 optype = type1;
2972 /* There's no such thing as a mixed sign madd yet, so use a wider mode. */
2973 if (from_unsigned1 != from_unsigned2)
2975 if (!INTEGRAL_TYPE_P (type))
2976 return false;
2977 /* We can use a signed multiply with unsigned types as long as
2978 there is a wider mode to use, or it is the smaller of the two
2979 types that is unsigned. Note that type1 >= type2, always. */
2980 if ((from_unsigned1
2981 && TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode))
2982 || (from_unsigned2
2983 && TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode)))
2985 if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode)
2986 || GET_MODE_SIZE (from_mode) >= GET_MODE_SIZE (to_mode))
2987 return false;
2990 from_unsigned1 = from_unsigned2 = false;
2991 optype = build_nonstandard_integer_type (GET_MODE_PRECISION (from_mode),
2992 false);
2995 /* If there was a conversion between the multiply and addition
2996 then we need to make sure it fits a multiply-and-accumulate.
2997 The should be a single mode change which does not change the
2998 value. */
2999 if (conv_stmt)
3001 /* We use the original, unmodified data types for this. */
3002 tree from_type = TREE_TYPE (gimple_assign_rhs1 (conv_stmt));
3003 tree to_type = TREE_TYPE (gimple_assign_lhs (conv_stmt));
3004 int data_size = TYPE_PRECISION (type1) + TYPE_PRECISION (type2);
3005 bool is_unsigned = TYPE_UNSIGNED (type1) && TYPE_UNSIGNED (type2);
3007 if (TYPE_PRECISION (from_type) > TYPE_PRECISION (to_type))
3009 /* Conversion is a truncate. */
3010 if (TYPE_PRECISION (to_type) < data_size)
3011 return false;
3013 else if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type))
3015 /* Conversion is an extend. Check it's the right sort. */
3016 if (TYPE_UNSIGNED (from_type) != is_unsigned
3017 && !(is_unsigned && TYPE_PRECISION (from_type) > data_size))
3018 return false;
3020 /* else convert is a no-op for our purposes. */
3023 /* Verify that the machine can perform a widening multiply
3024 accumulate in this mode/signedness combination, otherwise
3025 this transformation is likely to pessimize code. */
3026 this_optab = optab_for_tree_code (wmult_code, optype, optab_default);
3027 handler = find_widening_optab_handler_and_mode (this_optab, to_mode,
3028 from_mode, &actual_mode);
3030 if (handler == CODE_FOR_nothing)
3031 return false;
3033 /* Ensure that the inputs to the handler are in the correct precison
3034 for the opcode. This will be the full mode size. */
3035 actual_precision = GET_MODE_PRECISION (actual_mode);
3036 if (actual_precision != TYPE_PRECISION (type1)
3037 || from_unsigned1 != TYPE_UNSIGNED (type1))
3038 mult_rhs1 = build_and_insert_cast (gsi, loc,
3039 build_nonstandard_integer_type
3040 (actual_precision, from_unsigned1),
3041 mult_rhs1);
3042 if (actual_precision != TYPE_PRECISION (type2)
3043 || from_unsigned2 != TYPE_UNSIGNED (type2))
3044 mult_rhs2 = build_and_insert_cast (gsi, loc,
3045 build_nonstandard_integer_type
3046 (actual_precision, from_unsigned2),
3047 mult_rhs2);
3049 if (!useless_type_conversion_p (type, TREE_TYPE (add_rhs)))
3050 add_rhs = build_and_insert_cast (gsi, loc, type, add_rhs);
3052 /* Handle constants. */
3053 if (TREE_CODE (mult_rhs1) == INTEGER_CST)
3054 mult_rhs1 = fold_convert (type1, mult_rhs1);
3055 if (TREE_CODE (mult_rhs2) == INTEGER_CST)
3056 mult_rhs2 = fold_convert (type2, mult_rhs2);
3058 gimple_assign_set_rhs_with_ops (gsi, wmult_code, mult_rhs1, mult_rhs2,
3059 add_rhs);
3060 update_stmt (gsi_stmt (*gsi));
3061 widen_mul_stats.maccs_inserted++;
3062 return true;
3065 /* Given a result MUL_RESULT which is a result of a multiplication of OP1 and
3066 OP2 and which we know is used in statements that can be, together with the
3067 multiplication, converted to FMAs, perform the transformation. */
3069 static void
3070 convert_mult_to_fma_1 (tree mul_result, tree op1, tree op2)
3072 tree type = TREE_TYPE (mul_result);
3073 gimple *use_stmt;
3074 imm_use_iterator imm_iter;
3075 gcall *fma_stmt;
3077 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, mul_result)
3079 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
3080 tree addop, mulop1 = op1, result = mul_result;
3081 bool negate_p = false;
3082 gimple_seq seq = NULL;
3084 if (is_gimple_debug (use_stmt))
3085 continue;
3087 if (is_gimple_assign (use_stmt)
3088 && gimple_assign_rhs_code (use_stmt) == NEGATE_EXPR)
3090 result = gimple_assign_lhs (use_stmt);
3091 use_operand_p use_p;
3092 gimple *neguse_stmt;
3093 single_imm_use (gimple_assign_lhs (use_stmt), &use_p, &neguse_stmt);
3094 gsi_remove (&gsi, true);
3095 release_defs (use_stmt);
3097 use_stmt = neguse_stmt;
3098 gsi = gsi_for_stmt (use_stmt);
3099 negate_p = true;
3102 tree cond, else_value, ops[3];
3103 tree_code code;
3104 if (!can_interpret_as_conditional_op_p (use_stmt, &cond, &code,
3105 ops, &else_value))
3106 gcc_unreachable ();
3107 addop = ops[0] == result ? ops[1] : ops[0];
3109 if (code == MINUS_EXPR)
3111 if (ops[0] == result)
3112 /* a * b - c -> a * b + (-c) */
3113 addop = gimple_build (&seq, NEGATE_EXPR, type, addop);
3114 else
3115 /* a - b * c -> (-b) * c + a */
3116 negate_p = !negate_p;
3119 if (negate_p)
3120 mulop1 = gimple_build (&seq, NEGATE_EXPR, type, mulop1);
3122 if (seq)
3123 gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
3125 if (cond)
3126 fma_stmt = gimple_build_call_internal (IFN_COND_FMA, 5, cond, mulop1,
3127 op2, addop, else_value);
3128 else
3129 fma_stmt = gimple_build_call_internal (IFN_FMA, 3, mulop1, op2, addop);
3130 gimple_set_lhs (fma_stmt, gimple_get_lhs (use_stmt));
3131 gimple_call_set_nothrow (fma_stmt, !stmt_can_throw_internal (cfun,
3132 use_stmt));
3133 gsi_replace (&gsi, fma_stmt, true);
3134 /* Follow all SSA edges so that we generate FMS, FNMA and FNMS
3135 regardless of where the negation occurs. */
3136 gimple *orig_stmt = gsi_stmt (gsi);
3137 if (fold_stmt (&gsi, follow_all_ssa_edges))
3139 if (maybe_clean_or_replace_eh_stmt (orig_stmt, gsi_stmt (gsi)))
3140 gcc_unreachable ();
3141 update_stmt (gsi_stmt (gsi));
3144 if (dump_file && (dump_flags & TDF_DETAILS))
3146 fprintf (dump_file, "Generated FMA ");
3147 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, TDF_NONE);
3148 fprintf (dump_file, "\n");
3151 /* If the FMA result is negated in a single use, fold the negation
3152 too. */
3153 orig_stmt = gsi_stmt (gsi);
3154 use_operand_p use_p;
3155 gimple *neg_stmt;
3156 if (is_gimple_call (orig_stmt)
3157 && gimple_call_internal_p (orig_stmt)
3158 && gimple_call_lhs (orig_stmt)
3159 && TREE_CODE (gimple_call_lhs (orig_stmt)) == SSA_NAME
3160 && single_imm_use (gimple_call_lhs (orig_stmt), &use_p, &neg_stmt)
3161 && is_gimple_assign (neg_stmt)
3162 && gimple_assign_rhs_code (neg_stmt) == NEGATE_EXPR
3163 && !stmt_could_throw_p (cfun, neg_stmt))
3165 gsi = gsi_for_stmt (neg_stmt);
3166 if (fold_stmt (&gsi, follow_all_ssa_edges))
3168 if (maybe_clean_or_replace_eh_stmt (neg_stmt, gsi_stmt (gsi)))
3169 gcc_unreachable ();
3170 update_stmt (gsi_stmt (gsi));
3171 if (dump_file && (dump_flags & TDF_DETAILS))
3173 fprintf (dump_file, "Folded FMA negation ");
3174 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, TDF_NONE);
3175 fprintf (dump_file, "\n");
3180 widen_mul_stats.fmas_inserted++;
3184 /* Data necessary to perform the actual transformation from a multiplication
3185 and an addition to an FMA after decision is taken it should be done and to
3186 then delete the multiplication statement from the function IL. */
3188 struct fma_transformation_info
3190 gimple *mul_stmt;
3191 tree mul_result;
3192 tree op1;
3193 tree op2;
3196 /* Structure containing the current state of FMA deferring, i.e. whether we are
3197 deferring, whether to continue deferring, and all data necessary to come
3198 back and perform all deferred transformations. */
3200 class fma_deferring_state
3202 public:
3203 /* Class constructor. Pass true as PERFORM_DEFERRING in order to actually
3204 do any deferring. */
3206 fma_deferring_state (bool perform_deferring)
3207 : m_candidates (), m_mul_result_set (), m_initial_phi (NULL),
3208 m_last_result (NULL_TREE), m_deferring_p (perform_deferring) {}
3210 /* List of FMA candidates for which we the transformation has been determined
3211 possible but we at this point in BB analysis we do not consider them
3212 beneficial. */
3213 auto_vec<fma_transformation_info, 8> m_candidates;
3215 /* Set of results of multiplication that are part of an already deferred FMA
3216 candidates. */
3217 hash_set<tree> m_mul_result_set;
3219 /* The PHI that supposedly feeds back result of a FMA to another over loop
3220 boundary. */
3221 gphi *m_initial_phi;
3223 /* Result of the last produced FMA candidate or NULL if there has not been
3224 one. */
3225 tree m_last_result;
3227 /* If true, deferring might still be profitable. If false, transform all
3228 candidates and no longer defer. */
3229 bool m_deferring_p;
3232 /* Transform all deferred FMA candidates and mark STATE as no longer
3233 deferring. */
3235 static void
3236 cancel_fma_deferring (fma_deferring_state *state)
3238 if (!state->m_deferring_p)
3239 return;
3241 for (unsigned i = 0; i < state->m_candidates.length (); i++)
3243 if (dump_file && (dump_flags & TDF_DETAILS))
3244 fprintf (dump_file, "Generating deferred FMA\n");
3246 const fma_transformation_info &fti = state->m_candidates[i];
3247 convert_mult_to_fma_1 (fti.mul_result, fti.op1, fti.op2);
3249 gimple_stmt_iterator gsi = gsi_for_stmt (fti.mul_stmt);
3250 gsi_remove (&gsi, true);
3251 release_defs (fti.mul_stmt);
3253 state->m_deferring_p = false;
3256 /* If OP is an SSA name defined by a PHI node, return the PHI statement.
3257 Otherwise return NULL. */
3259 static gphi *
3260 result_of_phi (tree op)
3262 if (TREE_CODE (op) != SSA_NAME)
3263 return NULL;
3265 return dyn_cast <gphi *> (SSA_NAME_DEF_STMT (op));
3268 /* After processing statements of a BB and recording STATE, return true if the
3269 initial phi is fed by the last FMA candidate result ore one such result from
3270 previously processed BBs marked in LAST_RESULT_SET. */
3272 static bool
3273 last_fma_candidate_feeds_initial_phi (fma_deferring_state *state,
3274 hash_set<tree> *last_result_set)
3276 ssa_op_iter iter;
3277 use_operand_p use;
3278 FOR_EACH_PHI_ARG (use, state->m_initial_phi, iter, SSA_OP_USE)
3280 tree t = USE_FROM_PTR (use);
3281 if (t == state->m_last_result
3282 || last_result_set->contains (t))
3283 return true;
3286 return false;
3289 /* Combine the multiplication at MUL_STMT with operands MULOP1 and MULOP2
3290 with uses in additions and subtractions to form fused multiply-add
3291 operations. Returns true if successful and MUL_STMT should be removed.
3292 If MUL_COND is nonnull, the multiplication in MUL_STMT is conditional
3293 on MUL_COND, otherwise it is unconditional.
3295 If STATE indicates that we are deferring FMA transformation, that means
3296 that we do not produce FMAs for basic blocks which look like:
3298 <bb 6>
3299 # accumulator_111 = PHI <0.0(5), accumulator_66(6)>
3300 _65 = _14 * _16;
3301 accumulator_66 = _65 + accumulator_111;
3303 or its unrolled version, i.e. with several FMA candidates that feed result
3304 of one into the addend of another. Instead, we add them to a list in STATE
3305 and if we later discover an FMA candidate that is not part of such a chain,
3306 we go back and perform all deferred past candidates. */
3308 static bool
3309 convert_mult_to_fma (gimple *mul_stmt, tree op1, tree op2,
3310 fma_deferring_state *state, tree mul_cond = NULL_TREE)
3312 tree mul_result = gimple_get_lhs (mul_stmt);
3313 /* If there isn't a LHS then this can't be an FMA. There can be no LHS
3314 if the statement was left just for the side-effects. */
3315 if (!mul_result)
3316 return false;
3317 tree type = TREE_TYPE (mul_result);
3318 gimple *use_stmt, *neguse_stmt;
3319 use_operand_p use_p;
3320 imm_use_iterator imm_iter;
3322 if (FLOAT_TYPE_P (type)
3323 && flag_fp_contract_mode != FP_CONTRACT_FAST)
3324 return false;
3326 /* We don't want to do bitfield reduction ops. */
3327 if (INTEGRAL_TYPE_P (type)
3328 && (!type_has_mode_precision_p (type) || TYPE_OVERFLOW_TRAPS (type)))
3329 return false;
3331 /* If the target doesn't support it, don't generate it. We assume that
3332 if fma isn't available then fms, fnma or fnms are not either. */
3333 optimization_type opt_type = bb_optimization_type (gimple_bb (mul_stmt));
3334 if (!direct_internal_fn_supported_p (IFN_FMA, type, opt_type))
3335 return false;
3337 /* If the multiplication has zero uses, it is kept around probably because
3338 of -fnon-call-exceptions. Don't optimize it away in that case,
3339 it is DCE job. */
3340 if (has_zero_uses (mul_result))
3341 return false;
3343 bool check_defer
3344 = (state->m_deferring_p
3345 && maybe_le (tree_to_poly_int64 (TYPE_SIZE (type)),
3346 param_avoid_fma_max_bits));
3347 bool defer = check_defer;
3348 bool seen_negate_p = false;
3350 /* There is no numerical difference between fused and unfused integer FMAs,
3351 and the assumption below that FMA is as cheap as addition is unlikely
3352 to be true, especially if the multiplication occurs multiple times on
3353 the same chain. E.g., for something like:
3355 (((a * b) + c) >> 1) + (a * b)
3357 we do not want to duplicate the a * b into two additions, not least
3358 because the result is not a natural FMA chain. */
3359 if (ANY_INTEGRAL_TYPE_P (type)
3360 && !has_single_use (mul_result))
3361 return false;
3363 /* Make sure that the multiplication statement becomes dead after
3364 the transformation, thus that all uses are transformed to FMAs.
3365 This means we assume that an FMA operation has the same cost
3366 as an addition. */
3367 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, mul_result)
3369 tree result = mul_result;
3370 bool negate_p = false;
3372 use_stmt = USE_STMT (use_p);
3374 if (is_gimple_debug (use_stmt))
3375 continue;
3377 /* For now restrict this operations to single basic blocks. In theory
3378 we would want to support sinking the multiplication in
3379 m = a*b;
3380 if ()
3381 ma = m + c;
3382 else
3383 d = m;
3384 to form a fma in the then block and sink the multiplication to the
3385 else block. */
3386 if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
3387 return false;
3389 /* A negate on the multiplication leads to FNMA. */
3390 if (is_gimple_assign (use_stmt)
3391 && gimple_assign_rhs_code (use_stmt) == NEGATE_EXPR)
3393 ssa_op_iter iter;
3394 use_operand_p usep;
3396 /* If (due to earlier missed optimizations) we have two
3397 negates of the same value, treat them as equivalent
3398 to a single negate with multiple uses. */
3399 if (seen_negate_p)
3400 return false;
3402 result = gimple_assign_lhs (use_stmt);
3404 /* Make sure the negate statement becomes dead with this
3405 single transformation. */
3406 if (!single_imm_use (gimple_assign_lhs (use_stmt),
3407 &use_p, &neguse_stmt))
3408 return false;
3410 /* Make sure the multiplication isn't also used on that stmt. */
3411 FOR_EACH_PHI_OR_STMT_USE (usep, neguse_stmt, iter, SSA_OP_USE)
3412 if (USE_FROM_PTR (usep) == mul_result)
3413 return false;
3415 /* Re-validate. */
3416 use_stmt = neguse_stmt;
3417 if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
3418 return false;
3420 negate_p = seen_negate_p = true;
3423 tree cond, else_value, ops[3];
3424 tree_code code;
3425 if (!can_interpret_as_conditional_op_p (use_stmt, &cond, &code, ops,
3426 &else_value))
3427 return false;
3429 switch (code)
3431 case MINUS_EXPR:
3432 if (ops[1] == result)
3433 negate_p = !negate_p;
3434 break;
3435 case PLUS_EXPR:
3436 break;
3437 default:
3438 /* FMA can only be formed from PLUS and MINUS. */
3439 return false;
3442 if (mul_cond && cond != mul_cond)
3443 return false;
3445 if (cond)
3447 if (cond == result || else_value == result)
3448 return false;
3449 if (!direct_internal_fn_supported_p (IFN_COND_FMA, type, opt_type))
3450 return false;
3453 /* If the subtrahend (OPS[1]) is computed by a MULT_EXPR that
3454 we'll visit later, we might be able to get a more profitable
3455 match with fnma.
3456 OTOH, if we don't, a negate / fma pair has likely lower latency
3457 that a mult / subtract pair. */
3458 if (code == MINUS_EXPR
3459 && !negate_p
3460 && ops[0] == result
3461 && !direct_internal_fn_supported_p (IFN_FMS, type, opt_type)
3462 && direct_internal_fn_supported_p (IFN_FNMA, type, opt_type)
3463 && TREE_CODE (ops[1]) == SSA_NAME
3464 && has_single_use (ops[1]))
3466 gimple *stmt2 = SSA_NAME_DEF_STMT (ops[1]);
3467 if (is_gimple_assign (stmt2)
3468 && gimple_assign_rhs_code (stmt2) == MULT_EXPR)
3469 return false;
3472 /* We can't handle a * b + a * b. */
3473 if (ops[0] == ops[1])
3474 return false;
3475 /* If deferring, make sure we are not looking at an instruction that
3476 wouldn't have existed if we were not. */
3477 if (state->m_deferring_p
3478 && (state->m_mul_result_set.contains (ops[0])
3479 || state->m_mul_result_set.contains (ops[1])))
3480 return false;
3482 if (check_defer)
3484 tree use_lhs = gimple_get_lhs (use_stmt);
3485 if (state->m_last_result)
3487 if (ops[1] == state->m_last_result
3488 || ops[0] == state->m_last_result)
3489 defer = true;
3490 else
3491 defer = false;
3493 else
3495 gcc_checking_assert (!state->m_initial_phi);
3496 gphi *phi;
3497 if (ops[0] == result)
3498 phi = result_of_phi (ops[1]);
3499 else
3501 gcc_assert (ops[1] == result);
3502 phi = result_of_phi (ops[0]);
3505 if (phi)
3507 state->m_initial_phi = phi;
3508 defer = true;
3510 else
3511 defer = false;
3514 state->m_last_result = use_lhs;
3515 check_defer = false;
3517 else
3518 defer = false;
3520 /* While it is possible to validate whether or not the exact form that
3521 we've recognized is available in the backend, the assumption is that
3522 if the deferring logic above did not trigger, the transformation is
3523 never a loss. For instance, suppose the target only has the plain FMA
3524 pattern available. Consider a*b-c -> fma(a,b,-c): we've exchanged
3525 MUL+SUB for FMA+NEG, which is still two operations. Consider
3526 -(a*b)-c -> fma(-a,b,-c): we still have 3 operations, but in the FMA
3527 form the two NEGs are independent and could be run in parallel. */
3530 if (defer)
3532 fma_transformation_info fti;
3533 fti.mul_stmt = mul_stmt;
3534 fti.mul_result = mul_result;
3535 fti.op1 = op1;
3536 fti.op2 = op2;
3537 state->m_candidates.safe_push (fti);
3538 state->m_mul_result_set.add (mul_result);
3540 if (dump_file && (dump_flags & TDF_DETAILS))
3542 fprintf (dump_file, "Deferred generating FMA for multiplication ");
3543 print_gimple_stmt (dump_file, mul_stmt, 0, TDF_NONE);
3544 fprintf (dump_file, "\n");
3547 return false;
3549 else
3551 if (state->m_deferring_p)
3552 cancel_fma_deferring (state);
3553 convert_mult_to_fma_1 (mul_result, op1, op2);
3554 return true;
3559 /* Helper function of match_arith_overflow. For MUL_OVERFLOW, if we have
3560 a check for non-zero like:
3561 _1 = x_4(D) * y_5(D);
3562 *res_7(D) = _1;
3563 if (x_4(D) != 0)
3564 goto <bb 3>; [50.00%]
3565 else
3566 goto <bb 4>; [50.00%]
3568 <bb 3> [local count: 536870913]:
3569 _2 = _1 / x_4(D);
3570 _9 = _2 != y_5(D);
3571 _10 = (int) _9;
3573 <bb 4> [local count: 1073741824]:
3574 # iftmp.0_3 = PHI <_10(3), 0(2)>
3575 then in addition to using .MUL_OVERFLOW (x_4(D), y_5(D)) we can also
3576 optimize the x_4(D) != 0 condition to 1. */
3578 static void
3579 maybe_optimize_guarding_check (vec<gimple *> &mul_stmts, gimple *cond_stmt,
3580 gimple *div_stmt, bool *cfg_changed)
3582 basic_block bb = gimple_bb (cond_stmt);
3583 if (gimple_bb (div_stmt) != bb || !single_pred_p (bb))
3584 return;
3585 edge pred_edge = single_pred_edge (bb);
3586 basic_block pred_bb = pred_edge->src;
3587 if (EDGE_COUNT (pred_bb->succs) != 2)
3588 return;
3589 edge other_edge = EDGE_SUCC (pred_bb, EDGE_SUCC (pred_bb, 0) == pred_edge);
3590 edge other_succ_edge = NULL;
3591 if (gimple_code (cond_stmt) == GIMPLE_COND)
3593 if (EDGE_COUNT (bb->succs) != 2)
3594 return;
3595 other_succ_edge = EDGE_SUCC (bb, 0);
3596 if (gimple_cond_code (cond_stmt) == NE_EXPR)
3598 if (other_succ_edge->flags & EDGE_TRUE_VALUE)
3599 other_succ_edge = EDGE_SUCC (bb, 1);
3601 else if (other_succ_edge->flags & EDGE_FALSE_VALUE)
3602 other_succ_edge = EDGE_SUCC (bb, 0);
3603 if (other_edge->dest != other_succ_edge->dest)
3604 return;
3606 else if (!single_succ_p (bb) || other_edge->dest != single_succ (bb))
3607 return;
3608 gcond *zero_cond = safe_dyn_cast <gcond *> (*gsi_last_bb (pred_bb));
3609 if (zero_cond == NULL
3610 || (gimple_cond_code (zero_cond)
3611 != ((pred_edge->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR))
3612 || !integer_zerop (gimple_cond_rhs (zero_cond)))
3613 return;
3614 tree zero_cond_lhs = gimple_cond_lhs (zero_cond);
3615 if (TREE_CODE (zero_cond_lhs) != SSA_NAME)
3616 return;
3617 if (gimple_assign_rhs2 (div_stmt) != zero_cond_lhs)
3619 /* Allow the divisor to be result of a same precision cast
3620 from zero_cond_lhs. */
3621 tree rhs2 = gimple_assign_rhs2 (div_stmt);
3622 if (TREE_CODE (rhs2) != SSA_NAME)
3623 return;
3624 gimple *g = SSA_NAME_DEF_STMT (rhs2);
3625 if (!gimple_assign_cast_p (g)
3626 || gimple_assign_rhs1 (g) != gimple_cond_lhs (zero_cond)
3627 || !INTEGRAL_TYPE_P (TREE_TYPE (zero_cond_lhs))
3628 || (TYPE_PRECISION (TREE_TYPE (zero_cond_lhs))
3629 != TYPE_PRECISION (TREE_TYPE (rhs2))))
3630 return;
3632 gimple_stmt_iterator gsi = gsi_after_labels (bb);
3633 mul_stmts.quick_push (div_stmt);
3634 if (is_gimple_debug (gsi_stmt (gsi)))
3635 gsi_next_nondebug (&gsi);
3636 unsigned cast_count = 0;
3637 while (gsi_stmt (gsi) != cond_stmt)
3639 /* If original mul_stmt has a single use, allow it in the same bb,
3640 we are looking then just at __builtin_mul_overflow_p.
3641 Though, in that case the original mul_stmt will be replaced
3642 by .MUL_OVERFLOW, REALPART_EXPR and IMAGPART_EXPR stmts. */
3643 gimple *mul_stmt;
3644 unsigned int i;
3645 bool ok = false;
3646 FOR_EACH_VEC_ELT (mul_stmts, i, mul_stmt)
3648 if (gsi_stmt (gsi) == mul_stmt)
3650 ok = true;
3651 break;
3654 if (!ok && gimple_assign_cast_p (gsi_stmt (gsi)) && ++cast_count < 4)
3655 ok = true;
3656 if (!ok)
3657 return;
3658 gsi_next_nondebug (&gsi);
3660 if (gimple_code (cond_stmt) == GIMPLE_COND)
3662 basic_block succ_bb = other_edge->dest;
3663 for (gphi_iterator gpi = gsi_start_phis (succ_bb); !gsi_end_p (gpi);
3664 gsi_next (&gpi))
3666 gphi *phi = gpi.phi ();
3667 tree v1 = gimple_phi_arg_def (phi, other_edge->dest_idx);
3668 tree v2 = gimple_phi_arg_def (phi, other_succ_edge->dest_idx);
3669 if (!operand_equal_p (v1, v2, 0))
3670 return;
3673 else
3675 tree lhs = gimple_assign_lhs (cond_stmt);
3676 if (!lhs || !INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
3677 return;
3678 gsi_next_nondebug (&gsi);
3679 if (!gsi_end_p (gsi))
3681 if (gimple_assign_rhs_code (cond_stmt) == COND_EXPR)
3682 return;
3683 gimple *cast_stmt = gsi_stmt (gsi);
3684 if (!gimple_assign_cast_p (cast_stmt))
3685 return;
3686 tree new_lhs = gimple_assign_lhs (cast_stmt);
3687 gsi_next_nondebug (&gsi);
3688 if (!gsi_end_p (gsi)
3689 || !new_lhs
3690 || !INTEGRAL_TYPE_P (TREE_TYPE (new_lhs))
3691 || TYPE_PRECISION (TREE_TYPE (new_lhs)) <= 1)
3692 return;
3693 lhs = new_lhs;
3695 edge succ_edge = single_succ_edge (bb);
3696 basic_block succ_bb = succ_edge->dest;
3697 gsi = gsi_start_phis (succ_bb);
3698 if (gsi_end_p (gsi))
3699 return;
3700 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
3701 gsi_next (&gsi);
3702 if (!gsi_end_p (gsi))
3703 return;
3704 if (gimple_phi_arg_def (phi, succ_edge->dest_idx) != lhs)
3705 return;
3706 tree other_val = gimple_phi_arg_def (phi, other_edge->dest_idx);
3707 if (gimple_assign_rhs_code (cond_stmt) == COND_EXPR)
3709 tree cond = gimple_assign_rhs1 (cond_stmt);
3710 if (TREE_CODE (cond) == NE_EXPR)
3712 if (!operand_equal_p (other_val,
3713 gimple_assign_rhs3 (cond_stmt), 0))
3714 return;
3716 else if (!operand_equal_p (other_val,
3717 gimple_assign_rhs2 (cond_stmt), 0))
3718 return;
3720 else if (gimple_assign_rhs_code (cond_stmt) == NE_EXPR)
3722 if (!integer_zerop (other_val))
3723 return;
3725 else if (!integer_onep (other_val))
3726 return;
3728 if (pred_edge->flags & EDGE_TRUE_VALUE)
3729 gimple_cond_make_true (zero_cond);
3730 else
3731 gimple_cond_make_false (zero_cond);
3732 update_stmt (zero_cond);
3733 *cfg_changed = true;
3736 /* Helper function for arith_overflow_check_p. Return true
3737 if VAL1 is equal to VAL2 cast to corresponding integral type
3738 with other signedness or vice versa. */
3740 static bool
3741 arith_cast_equal_p (tree val1, tree val2)
3743 if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
3744 return wi::eq_p (wi::to_wide (val1), wi::to_wide (val2));
3745 else if (TREE_CODE (val1) != SSA_NAME || TREE_CODE (val2) != SSA_NAME)
3746 return false;
3747 if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (val1))
3748 && gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val1)) == val2)
3749 return true;
3750 if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (val2))
3751 && gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val2)) == val1)
3752 return true;
3753 return false;
3756 /* Helper function of match_arith_overflow. Return 1
3757 if USE_STMT is unsigned overflow check ovf != 0 for
3758 STMT, -1 if USE_STMT is unsigned overflow check ovf == 0
3759 and 0 otherwise. */
3761 static int
3762 arith_overflow_check_p (gimple *stmt, gimple *cast_stmt, gimple *&use_stmt,
3763 tree maxval, tree *other)
3765 enum tree_code ccode = ERROR_MARK;
3766 tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
3767 enum tree_code code = gimple_assign_rhs_code (stmt);
3768 tree lhs = gimple_assign_lhs (cast_stmt ? cast_stmt : stmt);
3769 tree rhs1 = gimple_assign_rhs1 (stmt);
3770 tree rhs2 = gimple_assign_rhs2 (stmt);
3771 tree multop = NULL_TREE, divlhs = NULL_TREE;
3772 gimple *cur_use_stmt = use_stmt;
3774 if (code == MULT_EXPR)
3776 if (!is_gimple_assign (use_stmt))
3777 return 0;
3778 if (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR)
3779 return 0;
3780 if (gimple_assign_rhs1 (use_stmt) != lhs)
3781 return 0;
3782 if (cast_stmt)
3784 if (arith_cast_equal_p (gimple_assign_rhs2 (use_stmt), rhs1))
3785 multop = rhs2;
3786 else if (arith_cast_equal_p (gimple_assign_rhs2 (use_stmt), rhs2))
3787 multop = rhs1;
3788 else
3789 return 0;
3791 else if (gimple_assign_rhs2 (use_stmt) == rhs1)
3792 multop = rhs2;
3793 else if (operand_equal_p (gimple_assign_rhs2 (use_stmt), rhs2, 0))
3794 multop = rhs1;
3795 else
3796 return 0;
3797 if (stmt_ends_bb_p (use_stmt))
3798 return 0;
3799 divlhs = gimple_assign_lhs (use_stmt);
3800 if (!divlhs)
3801 return 0;
3802 use_operand_p use;
3803 if (!single_imm_use (divlhs, &use, &cur_use_stmt))
3804 return 0;
3805 if (cast_stmt && gimple_assign_cast_p (cur_use_stmt))
3807 tree cast_lhs = gimple_assign_lhs (cur_use_stmt);
3808 if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs))
3809 && TYPE_UNSIGNED (TREE_TYPE (cast_lhs))
3810 && (TYPE_PRECISION (TREE_TYPE (cast_lhs))
3811 == TYPE_PRECISION (TREE_TYPE (divlhs)))
3812 && single_imm_use (cast_lhs, &use, &cur_use_stmt))
3814 cast_stmt = NULL;
3815 divlhs = cast_lhs;
3817 else
3818 return 0;
3821 if (gimple_code (cur_use_stmt) == GIMPLE_COND)
3823 ccode = gimple_cond_code (cur_use_stmt);
3824 crhs1 = gimple_cond_lhs (cur_use_stmt);
3825 crhs2 = gimple_cond_rhs (cur_use_stmt);
3827 else if (is_gimple_assign (cur_use_stmt))
3829 if (gimple_assign_rhs_class (cur_use_stmt) == GIMPLE_BINARY_RHS)
3831 ccode = gimple_assign_rhs_code (cur_use_stmt);
3832 crhs1 = gimple_assign_rhs1 (cur_use_stmt);
3833 crhs2 = gimple_assign_rhs2 (cur_use_stmt);
3835 else if (gimple_assign_rhs_code (cur_use_stmt) == COND_EXPR)
3837 tree cond = gimple_assign_rhs1 (cur_use_stmt);
3838 if (COMPARISON_CLASS_P (cond))
3840 ccode = TREE_CODE (cond);
3841 crhs1 = TREE_OPERAND (cond, 0);
3842 crhs2 = TREE_OPERAND (cond, 1);
3844 else
3845 return 0;
3847 else
3848 return 0;
3850 else
3851 return 0;
3853 if (TREE_CODE_CLASS (ccode) != tcc_comparison)
3854 return 0;
3856 switch (ccode)
3858 case GT_EXPR:
3859 case LE_EXPR:
3860 if (maxval)
3862 /* r = a + b; r > maxval or r <= maxval */
3863 if (crhs1 == lhs
3864 && TREE_CODE (crhs2) == INTEGER_CST
3865 && tree_int_cst_equal (crhs2, maxval))
3866 return ccode == GT_EXPR ? 1 : -1;
3867 break;
3869 /* r = a - b; r > a or r <= a
3870 r = a + b; a > r or a <= r or b > r or b <= r. */
3871 if ((code == MINUS_EXPR && crhs1 == lhs && crhs2 == rhs1)
3872 || (code == PLUS_EXPR && (crhs1 == rhs1 || crhs1 == rhs2)
3873 && crhs2 == lhs))
3874 return ccode == GT_EXPR ? 1 : -1;
3875 /* r = ~a; b > r or b <= r. */
3876 if (code == BIT_NOT_EXPR && crhs2 == lhs)
3878 if (other)
3879 *other = crhs1;
3880 return ccode == GT_EXPR ? 1 : -1;
3882 break;
3883 case LT_EXPR:
3884 case GE_EXPR:
3885 if (maxval)
3886 break;
3887 /* r = a - b; a < r or a >= r
3888 r = a + b; r < a or r >= a or r < b or r >= b. */
3889 if ((code == MINUS_EXPR && crhs1 == rhs1 && crhs2 == lhs)
3890 || (code == PLUS_EXPR && crhs1 == lhs
3891 && (crhs2 == rhs1 || crhs2 == rhs2)))
3892 return ccode == LT_EXPR ? 1 : -1;
3893 /* r = ~a; r < b or r >= b. */
3894 if (code == BIT_NOT_EXPR && crhs1 == lhs)
3896 if (other)
3897 *other = crhs2;
3898 return ccode == LT_EXPR ? 1 : -1;
3900 break;
3901 case EQ_EXPR:
3902 case NE_EXPR:
3903 /* r = a * b; _1 = r / a; _1 == b
3904 r = a * b; _1 = r / b; _1 == a
3905 r = a * b; _1 = r / a; _1 != b
3906 r = a * b; _1 = r / b; _1 != a. */
3907 if (code == MULT_EXPR)
3909 if (cast_stmt)
3911 if ((crhs1 == divlhs && arith_cast_equal_p (crhs2, multop))
3912 || (crhs2 == divlhs && arith_cast_equal_p (crhs1, multop)))
3914 use_stmt = cur_use_stmt;
3915 return ccode == NE_EXPR ? 1 : -1;
3918 else if ((crhs1 == divlhs && operand_equal_p (crhs2, multop, 0))
3919 || (crhs2 == divlhs && crhs1 == multop))
3921 use_stmt = cur_use_stmt;
3922 return ccode == NE_EXPR ? 1 : -1;
3925 break;
3926 default:
3927 break;
3929 return 0;
3932 /* Recognize for unsigned x
3933 x = y - z;
3934 if (x > y)
3935 where there are other uses of x and replace it with
3936 _7 = .SUB_OVERFLOW (y, z);
3937 x = REALPART_EXPR <_7>;
3938 _8 = IMAGPART_EXPR <_7>;
3939 if (_8)
3940 and similarly for addition.
3942 Also recognize:
3943 yc = (type) y;
3944 zc = (type) z;
3945 x = yc + zc;
3946 if (x > max)
3947 where y and z have unsigned types with maximum max
3948 and there are other uses of x and all of those cast x
3949 back to that unsigned type and again replace it with
3950 _7 = .ADD_OVERFLOW (y, z);
3951 _9 = REALPART_EXPR <_7>;
3952 _8 = IMAGPART_EXPR <_7>;
3953 if (_8)
3954 and replace (utype) x with _9.
3956 Also recognize:
3957 x = ~z;
3958 if (y > x)
3959 and replace it with
3960 _7 = .ADD_OVERFLOW (y, z);
3961 _8 = IMAGPART_EXPR <_7>;
3962 if (_8)
3964 And also recognize:
3965 z = x * y;
3966 if (x != 0)
3967 goto <bb 3>; [50.00%]
3968 else
3969 goto <bb 4>; [50.00%]
3971 <bb 3> [local count: 536870913]:
3972 _2 = z / x;
3973 _9 = _2 != y;
3974 _10 = (int) _9;
3976 <bb 4> [local count: 1073741824]:
3977 # iftmp.0_3 = PHI <_10(3), 0(2)>
3978 and replace it with
3979 _7 = .MUL_OVERFLOW (x, y);
3980 z = IMAGPART_EXPR <_7>;
3981 _8 = IMAGPART_EXPR <_7>;
3982 _9 = _8 != 0;
3983 iftmp.0_3 = (int) _9; */
3985 static bool
3986 match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt,
3987 enum tree_code code, bool *cfg_changed)
3989 tree lhs = gimple_assign_lhs (stmt);
3990 tree type = TREE_TYPE (lhs);
3991 use_operand_p use_p;
3992 imm_use_iterator iter;
3993 bool use_seen = false;
3994 bool ovf_use_seen = false;
3995 gimple *use_stmt;
3996 gimple *add_stmt = NULL;
3997 bool add_first = false;
3998 gimple *cond_stmt = NULL;
3999 gimple *cast_stmt = NULL;
4000 tree cast_lhs = NULL_TREE;
4002 gcc_checking_assert (code == PLUS_EXPR
4003 || code == MINUS_EXPR
4004 || code == MULT_EXPR
4005 || code == BIT_NOT_EXPR);
4006 if (!INTEGRAL_TYPE_P (type)
4007 || !TYPE_UNSIGNED (type)
4008 || has_zero_uses (lhs)
4009 || (code != PLUS_EXPR
4010 && code != MULT_EXPR
4011 && optab_handler (code == MINUS_EXPR ? usubv4_optab : uaddv4_optab,
4012 TYPE_MODE (type)) == CODE_FOR_nothing))
4013 return false;
4015 tree rhs1 = gimple_assign_rhs1 (stmt);
4016 tree rhs2 = gimple_assign_rhs2 (stmt);
4017 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4019 use_stmt = USE_STMT (use_p);
4020 if (is_gimple_debug (use_stmt))
4021 continue;
4023 tree other = NULL_TREE;
4024 if (arith_overflow_check_p (stmt, NULL, use_stmt, NULL_TREE, &other))
4026 if (code == BIT_NOT_EXPR)
4028 gcc_assert (other);
4029 if (TREE_CODE (other) != SSA_NAME)
4030 return false;
4031 if (rhs2 == NULL)
4032 rhs2 = other;
4033 else
4034 return false;
4035 cond_stmt = use_stmt;
4037 ovf_use_seen = true;
4039 else
4041 use_seen = true;
4042 if (code == MULT_EXPR
4043 && cast_stmt == NULL
4044 && gimple_assign_cast_p (use_stmt))
4046 cast_lhs = gimple_assign_lhs (use_stmt);
4047 if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs))
4048 && !TYPE_UNSIGNED (TREE_TYPE (cast_lhs))
4049 && (TYPE_PRECISION (TREE_TYPE (cast_lhs))
4050 == TYPE_PRECISION (TREE_TYPE (lhs))))
4051 cast_stmt = use_stmt;
4052 else
4053 cast_lhs = NULL_TREE;
4056 if (ovf_use_seen && use_seen)
4057 break;
4060 if (!ovf_use_seen
4061 && code == MULT_EXPR
4062 && cast_stmt)
4064 if (TREE_CODE (rhs1) != SSA_NAME
4065 || (TREE_CODE (rhs2) != SSA_NAME && TREE_CODE (rhs2) != INTEGER_CST))
4066 return false;
4067 FOR_EACH_IMM_USE_FAST (use_p, iter, cast_lhs)
4069 use_stmt = USE_STMT (use_p);
4070 if (is_gimple_debug (use_stmt))
4071 continue;
4073 if (arith_overflow_check_p (stmt, cast_stmt, use_stmt,
4074 NULL_TREE, NULL))
4075 ovf_use_seen = true;
4078 else
4080 cast_stmt = NULL;
4081 cast_lhs = NULL_TREE;
4084 tree maxval = NULL_TREE;
4085 if (!ovf_use_seen
4086 || (code != MULT_EXPR && (code == BIT_NOT_EXPR ? use_seen : !use_seen))
4087 || (code == PLUS_EXPR
4088 && optab_handler (uaddv4_optab,
4089 TYPE_MODE (type)) == CODE_FOR_nothing)
4090 || (code == MULT_EXPR
4091 && optab_handler (cast_stmt ? mulv4_optab : umulv4_optab,
4092 TYPE_MODE (type)) == CODE_FOR_nothing
4093 && (use_seen
4094 || cast_stmt
4095 || !can_mult_highpart_p (TYPE_MODE (type), true))))
4097 if (code != PLUS_EXPR)
4098 return false;
4099 if (TREE_CODE (rhs1) != SSA_NAME
4100 || !gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs1)))
4101 return false;
4102 rhs1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs1));
4103 tree type1 = TREE_TYPE (rhs1);
4104 if (!INTEGRAL_TYPE_P (type1)
4105 || !TYPE_UNSIGNED (type1)
4106 || TYPE_PRECISION (type1) >= TYPE_PRECISION (type)
4107 || (TYPE_PRECISION (type1)
4108 != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type1))))
4109 return false;
4110 if (TREE_CODE (rhs2) == INTEGER_CST)
4112 if (wi::ne_p (wi::rshift (wi::to_wide (rhs2),
4113 TYPE_PRECISION (type1),
4114 UNSIGNED), 0))
4115 return false;
4116 rhs2 = fold_convert (type1, rhs2);
4118 else
4120 if (TREE_CODE (rhs2) != SSA_NAME
4121 || !gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs2)))
4122 return false;
4123 rhs2 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs2));
4124 tree type2 = TREE_TYPE (rhs2);
4125 if (!INTEGRAL_TYPE_P (type2)
4126 || !TYPE_UNSIGNED (type2)
4127 || TYPE_PRECISION (type2) >= TYPE_PRECISION (type)
4128 || (TYPE_PRECISION (type2)
4129 != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type2))))
4130 return false;
4132 if (TYPE_PRECISION (type1) >= TYPE_PRECISION (TREE_TYPE (rhs2)))
4133 type = type1;
4134 else
4135 type = TREE_TYPE (rhs2);
4137 if (TREE_CODE (type) != INTEGER_TYPE
4138 || optab_handler (uaddv4_optab,
4139 TYPE_MODE (type)) == CODE_FOR_nothing)
4140 return false;
4142 maxval = wide_int_to_tree (type, wi::max_value (TYPE_PRECISION (type),
4143 UNSIGNED));
4144 ovf_use_seen = false;
4145 use_seen = false;
4146 basic_block use_bb = NULL;
4147 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4149 use_stmt = USE_STMT (use_p);
4150 if (is_gimple_debug (use_stmt))
4151 continue;
4153 if (arith_overflow_check_p (stmt, NULL, use_stmt, maxval, NULL))
4155 ovf_use_seen = true;
4156 use_bb = gimple_bb (use_stmt);
4158 else
4160 if (!gimple_assign_cast_p (use_stmt)
4161 || gimple_assign_rhs_code (use_stmt) == VIEW_CONVERT_EXPR)
4162 return false;
4163 tree use_lhs = gimple_assign_lhs (use_stmt);
4164 if (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
4165 || (TYPE_PRECISION (TREE_TYPE (use_lhs))
4166 > TYPE_PRECISION (type)))
4167 return false;
4168 use_seen = true;
4171 if (!ovf_use_seen)
4172 return false;
4173 if (!useless_type_conversion_p (type, TREE_TYPE (rhs1)))
4175 if (!use_seen)
4176 return false;
4177 tree new_rhs1 = make_ssa_name (type);
4178 gimple *g = gimple_build_assign (new_rhs1, NOP_EXPR, rhs1);
4179 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4180 rhs1 = new_rhs1;
4182 else if (!useless_type_conversion_p (type, TREE_TYPE (rhs2)))
4184 if (!use_seen)
4185 return false;
4186 tree new_rhs2 = make_ssa_name (type);
4187 gimple *g = gimple_build_assign (new_rhs2, NOP_EXPR, rhs2);
4188 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4189 rhs2 = new_rhs2;
4191 else if (!use_seen)
4193 /* If there are no uses of the wider addition, check if
4194 forwprop has not created a narrower addition.
4195 Require it to be in the same bb as the overflow check. */
4196 FOR_EACH_IMM_USE_FAST (use_p, iter, rhs1)
4198 use_stmt = USE_STMT (use_p);
4199 if (is_gimple_debug (use_stmt))
4200 continue;
4202 if (use_stmt == stmt)
4203 continue;
4205 if (!is_gimple_assign (use_stmt)
4206 || gimple_bb (use_stmt) != use_bb
4207 || gimple_assign_rhs_code (use_stmt) != PLUS_EXPR)
4208 continue;
4210 if (gimple_assign_rhs1 (use_stmt) == rhs1)
4212 if (!operand_equal_p (gimple_assign_rhs2 (use_stmt),
4213 rhs2, 0))
4214 continue;
4216 else if (gimple_assign_rhs2 (use_stmt) == rhs1)
4218 if (gimple_assign_rhs1 (use_stmt) != rhs2)
4219 continue;
4221 else
4222 continue;
4224 add_stmt = use_stmt;
4225 break;
4227 if (add_stmt == NULL)
4228 return false;
4230 /* If stmt and add_stmt are in the same bb, we need to find out
4231 which one is earlier. If they are in different bbs, we've
4232 checked add_stmt is in the same bb as one of the uses of the
4233 stmt lhs, so stmt needs to dominate add_stmt too. */
4234 if (gimple_bb (stmt) == gimple_bb (add_stmt))
4236 gimple_stmt_iterator gsif = *gsi;
4237 gimple_stmt_iterator gsib = *gsi;
4238 int i;
4239 /* Search both forward and backward from stmt and have a small
4240 upper bound. */
4241 for (i = 0; i < 128; i++)
4243 if (!gsi_end_p (gsib))
4245 gsi_prev_nondebug (&gsib);
4246 if (gsi_stmt (gsib) == add_stmt)
4248 add_first = true;
4249 break;
4252 else if (gsi_end_p (gsif))
4253 break;
4254 if (!gsi_end_p (gsif))
4256 gsi_next_nondebug (&gsif);
4257 if (gsi_stmt (gsif) == add_stmt)
4258 break;
4261 if (i == 128)
4262 return false;
4263 if (add_first)
4264 *gsi = gsi_for_stmt (add_stmt);
4269 if (code == BIT_NOT_EXPR)
4270 *gsi = gsi_for_stmt (cond_stmt);
4272 auto_vec<gimple *, 8> mul_stmts;
4273 if (code == MULT_EXPR && cast_stmt)
4275 type = TREE_TYPE (cast_lhs);
4276 gimple *g = SSA_NAME_DEF_STMT (rhs1);
4277 if (gimple_assign_cast_p (g)
4278 && useless_type_conversion_p (type,
4279 TREE_TYPE (gimple_assign_rhs1 (g)))
4280 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g)))
4281 rhs1 = gimple_assign_rhs1 (g);
4282 else
4284 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, rhs1);
4285 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4286 rhs1 = gimple_assign_lhs (g);
4287 mul_stmts.quick_push (g);
4289 if (TREE_CODE (rhs2) == INTEGER_CST)
4290 rhs2 = fold_convert (type, rhs2);
4291 else
4293 g = SSA_NAME_DEF_STMT (rhs2);
4294 if (gimple_assign_cast_p (g)
4295 && useless_type_conversion_p (type,
4296 TREE_TYPE (gimple_assign_rhs1 (g)))
4297 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g)))
4298 rhs2 = gimple_assign_rhs1 (g);
4299 else
4301 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, rhs2);
4302 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4303 rhs2 = gimple_assign_lhs (g);
4304 mul_stmts.quick_push (g);
4308 tree ctype = build_complex_type (type);
4309 gcall *g = gimple_build_call_internal (code == MULT_EXPR
4310 ? IFN_MUL_OVERFLOW
4311 : code != MINUS_EXPR
4312 ? IFN_ADD_OVERFLOW : IFN_SUB_OVERFLOW,
4313 2, rhs1, rhs2);
4314 tree ctmp = make_ssa_name (ctype);
4315 gimple_call_set_lhs (g, ctmp);
4316 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4317 tree new_lhs = (maxval || cast_stmt) ? make_ssa_name (type) : lhs;
4318 gassign *g2;
4319 if (code != BIT_NOT_EXPR)
4321 g2 = gimple_build_assign (new_lhs, REALPART_EXPR,
4322 build1 (REALPART_EXPR, type, ctmp));
4323 if (maxval || cast_stmt)
4325 gsi_insert_before (gsi, g2, GSI_SAME_STMT);
4326 if (add_first)
4327 *gsi = gsi_for_stmt (stmt);
4329 else
4330 gsi_replace (gsi, g2, true);
4331 if (code == MULT_EXPR)
4333 mul_stmts.quick_push (g);
4334 mul_stmts.quick_push (g2);
4335 if (cast_stmt)
4337 g2 = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
4338 gsi_replace (gsi, g2, true);
4339 mul_stmts.quick_push (g2);
4343 tree ovf = make_ssa_name (type);
4344 g2 = gimple_build_assign (ovf, IMAGPART_EXPR,
4345 build1 (IMAGPART_EXPR, type, ctmp));
4346 if (code != BIT_NOT_EXPR)
4347 gsi_insert_after (gsi, g2, GSI_NEW_STMT);
4348 else
4349 gsi_insert_before (gsi, g2, GSI_SAME_STMT);
4350 if (code == MULT_EXPR)
4351 mul_stmts.quick_push (g2);
4353 FOR_EACH_IMM_USE_STMT (use_stmt, iter, cast_lhs ? cast_lhs : lhs)
4355 if (is_gimple_debug (use_stmt))
4356 continue;
4358 gimple *orig_use_stmt = use_stmt;
4359 int ovf_use = arith_overflow_check_p (stmt, cast_stmt, use_stmt,
4360 maxval, NULL);
4361 if (ovf_use == 0)
4363 gcc_assert (code != BIT_NOT_EXPR);
4364 if (maxval)
4366 tree use_lhs = gimple_assign_lhs (use_stmt);
4367 gimple_assign_set_rhs1 (use_stmt, new_lhs);
4368 if (useless_type_conversion_p (TREE_TYPE (use_lhs),
4369 TREE_TYPE (new_lhs)))
4370 gimple_assign_set_rhs_code (use_stmt, SSA_NAME);
4371 update_stmt (use_stmt);
4373 continue;
4375 if (gimple_code (use_stmt) == GIMPLE_COND)
4377 gcond *cond_stmt = as_a <gcond *> (use_stmt);
4378 gimple_cond_set_lhs (cond_stmt, ovf);
4379 gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0));
4380 gimple_cond_set_code (cond_stmt, ovf_use == 1 ? NE_EXPR : EQ_EXPR);
4382 else
4384 gcc_checking_assert (is_gimple_assign (use_stmt));
4385 if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
4387 gimple_assign_set_rhs1 (use_stmt, ovf);
4388 gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0));
4389 gimple_assign_set_rhs_code (use_stmt,
4390 ovf_use == 1 ? NE_EXPR : EQ_EXPR);
4392 else
4394 gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
4395 == COND_EXPR);
4396 tree cond = build2 (ovf_use == 1 ? NE_EXPR : EQ_EXPR,
4397 boolean_type_node, ovf,
4398 build_int_cst (type, 0));
4399 gimple_assign_set_rhs1 (use_stmt, cond);
4402 update_stmt (use_stmt);
4403 if (code == MULT_EXPR && use_stmt != orig_use_stmt)
4405 gimple_stmt_iterator gsi2 = gsi_for_stmt (orig_use_stmt);
4406 maybe_optimize_guarding_check (mul_stmts, use_stmt, orig_use_stmt,
4407 cfg_changed);
4408 use_operand_p use;
4409 gimple *cast_stmt;
4410 if (single_imm_use (gimple_assign_lhs (orig_use_stmt), &use,
4411 &cast_stmt)
4412 && gimple_assign_cast_p (cast_stmt))
4414 gimple_stmt_iterator gsi3 = gsi_for_stmt (cast_stmt);
4415 gsi_remove (&gsi3, true);
4416 release_ssa_name (gimple_assign_lhs (cast_stmt));
4418 gsi_remove (&gsi2, true);
4419 release_ssa_name (gimple_assign_lhs (orig_use_stmt));
4422 if (maxval)
4424 gimple_stmt_iterator gsi2 = gsi_for_stmt (stmt);
4425 gsi_remove (&gsi2, true);
4426 if (add_stmt)
4428 gimple *g = gimple_build_assign (gimple_assign_lhs (add_stmt),
4429 new_lhs);
4430 gsi2 = gsi_for_stmt (add_stmt);
4431 gsi_replace (&gsi2, g, true);
4434 else if (code == BIT_NOT_EXPR)
4436 *gsi = gsi_for_stmt (stmt);
4437 gsi_remove (gsi, true);
4438 release_ssa_name (lhs);
4439 return true;
4441 return false;
4444 /* Helper of match_uaddc_usubc. Look through an integral cast
4445 which should preserve [0, 1] range value (unless source has
4446 1-bit signed type) and the cast has single use. */
4448 static gimple *
4449 uaddc_cast (gimple *g)
4451 if (!gimple_assign_cast_p (g))
4452 return g;
4453 tree op = gimple_assign_rhs1 (g);
4454 if (TREE_CODE (op) == SSA_NAME
4455 && INTEGRAL_TYPE_P (TREE_TYPE (op))
4456 && (TYPE_PRECISION (TREE_TYPE (op)) > 1
4457 || TYPE_UNSIGNED (TREE_TYPE (op)))
4458 && has_single_use (gimple_assign_lhs (g)))
4459 return SSA_NAME_DEF_STMT (op);
4460 return g;
4463 /* Helper of match_uaddc_usubc. Look through a NE_EXPR
4464 comparison with 0 which also preserves [0, 1] value range. */
4466 static gimple *
4467 uaddc_ne0 (gimple *g)
4469 if (is_gimple_assign (g)
4470 && gimple_assign_rhs_code (g) == NE_EXPR
4471 && integer_zerop (gimple_assign_rhs2 (g))
4472 && TREE_CODE (gimple_assign_rhs1 (g)) == SSA_NAME
4473 && has_single_use (gimple_assign_lhs (g)))
4474 return SSA_NAME_DEF_STMT (gimple_assign_rhs1 (g));
4475 return g;
4478 /* Return true if G is {REAL,IMAG}PART_EXPR PART with SSA_NAME
4479 operand. */
4481 static bool
4482 uaddc_is_cplxpart (gimple *g, tree_code part)
4484 return (is_gimple_assign (g)
4485 && gimple_assign_rhs_code (g) == part
4486 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (g), 0)) == SSA_NAME);
4489 /* Try to match e.g.
4490 _29 = .ADD_OVERFLOW (_3, _4);
4491 _30 = REALPART_EXPR <_29>;
4492 _31 = IMAGPART_EXPR <_29>;
4493 _32 = .ADD_OVERFLOW (_30, _38);
4494 _33 = REALPART_EXPR <_32>;
4495 _34 = IMAGPART_EXPR <_32>;
4496 _35 = _31 + _34;
4498 _36 = .UADDC (_3, _4, _38);
4499 _33 = REALPART_EXPR <_36>;
4500 _35 = IMAGPART_EXPR <_36>;
4502 _22 = .SUB_OVERFLOW (_6, _5);
4503 _23 = REALPART_EXPR <_22>;
4504 _24 = IMAGPART_EXPR <_22>;
4505 _25 = .SUB_OVERFLOW (_23, _37);
4506 _26 = REALPART_EXPR <_25>;
4507 _27 = IMAGPART_EXPR <_25>;
4508 _28 = _24 | _27;
4510 _29 = .USUBC (_6, _5, _37);
4511 _26 = REALPART_EXPR <_29>;
4512 _288 = IMAGPART_EXPR <_29>;
4513 provided _38 or _37 above have [0, 1] range
4514 and _3, _4 and _30 or _6, _5 and _23 are unsigned
4515 integral types with the same precision. Whether + or | or ^ is
4516 used on the IMAGPART_EXPR results doesn't matter, with one of
4517 added or subtracted operands in [0, 1] range at most one
4518 .ADD_OVERFLOW or .SUB_OVERFLOW will indicate overflow. */
4520 static bool
4521 match_uaddc_usubc (gimple_stmt_iterator *gsi, gimple *stmt, tree_code code)
4523 tree rhs[4];
4524 rhs[0] = gimple_assign_rhs1 (stmt);
4525 rhs[1] = gimple_assign_rhs2 (stmt);
4526 rhs[2] = NULL_TREE;
4527 rhs[3] = NULL_TREE;
4528 tree type = TREE_TYPE (rhs[0]);
4529 if (!INTEGRAL_TYPE_P (type) || !TYPE_UNSIGNED (type))
4530 return false;
4532 if (code != BIT_IOR_EXPR && code != BIT_XOR_EXPR)
4534 /* If overflow flag is ignored on the MSB limb, we can end up with
4535 the most significant limb handled as r = op1 + op2 + ovf1 + ovf2;
4536 or r = op1 - op2 - ovf1 - ovf2; or various equivalent expressions
4537 thereof. Handle those like the ovf = ovf1 + ovf2; case to recognize
4538 the limb below the MSB, but also create another .UADDC/.USUBC call
4539 for the last limb.
4541 First look through assignments with the same rhs code as CODE,
4542 with the exception that subtraction of a constant is canonicalized
4543 into addition of its negation. rhs[0] will be minuend for
4544 subtractions and one of addends for addition, all other assigned
4545 rhs[i] operands will be subtrahends or other addends. */
4546 while (TREE_CODE (rhs[0]) == SSA_NAME && !rhs[3])
4548 gimple *g = SSA_NAME_DEF_STMT (rhs[0]);
4549 if (has_single_use (rhs[0])
4550 && is_gimple_assign (g)
4551 && (gimple_assign_rhs_code (g) == code
4552 || (code == MINUS_EXPR
4553 && gimple_assign_rhs_code (g) == PLUS_EXPR
4554 && TREE_CODE (gimple_assign_rhs2 (g)) == INTEGER_CST)))
4556 tree r2 = gimple_assign_rhs2 (g);
4557 if (gimple_assign_rhs_code (g) != code)
4559 r2 = const_unop (NEGATE_EXPR, TREE_TYPE (r2), r2);
4560 if (!r2)
4561 break;
4563 rhs[0] = gimple_assign_rhs1 (g);
4564 tree &r = rhs[2] ? rhs[3] : rhs[2];
4565 r = r2;
4567 else
4568 break;
4570 while (TREE_CODE (rhs[1]) == SSA_NAME && !rhs[3])
4572 gimple *g = SSA_NAME_DEF_STMT (rhs[1]);
4573 if (has_single_use (rhs[1])
4574 && is_gimple_assign (g)
4575 && gimple_assign_rhs_code (g) == PLUS_EXPR)
4577 rhs[1] = gimple_assign_rhs1 (g);
4578 if (rhs[2])
4579 rhs[3] = gimple_assign_rhs2 (g);
4580 else
4581 rhs[2] = gimple_assign_rhs2 (g);
4583 else
4584 break;
4586 /* If there are just 3 addends or one minuend and two subtrahends,
4587 check for UADDC or USUBC being pattern recognized earlier.
4588 Say r = op1 + op2 + ovf1 + ovf2; where the (ovf1 + ovf2) part
4589 got pattern matched earlier as __imag__ .UADDC (arg1, arg2, arg3)
4590 etc. */
4591 if (rhs[2] && !rhs[3])
4593 for (int i = (code == MINUS_EXPR ? 1 : 0); i < 3; ++i)
4594 if (TREE_CODE (rhs[i]) == SSA_NAME)
4596 gimple *im = uaddc_cast (SSA_NAME_DEF_STMT (rhs[i]));
4597 im = uaddc_ne0 (im);
4598 if (uaddc_is_cplxpart (im, IMAGPART_EXPR))
4600 /* We found one of the 3 addends or 2 subtrahends to be
4601 __imag__ of something, verify it is .UADDC/.USUBC. */
4602 tree rhs1 = gimple_assign_rhs1 (im);
4603 gimple *ovf = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs1, 0));
4604 if (gimple_call_internal_p (ovf, code == PLUS_EXPR
4605 ? IFN_UADDC : IFN_USUBC)
4606 && (optab_handler (code == PLUS_EXPR
4607 ? uaddc5_optab : usubc5_optab,
4608 TYPE_MODE (type))
4609 != CODE_FOR_nothing))
4611 /* And in that case build another .UADDC/.USUBC
4612 call for the most significand limb addition.
4613 Overflow bit is ignored here. */
4614 if (i != 2)
4615 std::swap (rhs[i], rhs[2]);
4616 gimple *g
4617 = gimple_build_call_internal (code == PLUS_EXPR
4618 ? IFN_UADDC
4619 : IFN_USUBC,
4620 3, rhs[0], rhs[1],
4621 rhs[2]);
4622 tree nlhs = make_ssa_name (build_complex_type (type));
4623 gimple_call_set_lhs (g, nlhs);
4624 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4625 tree ilhs = gimple_assign_lhs (stmt);
4626 g = gimple_build_assign (ilhs, REALPART_EXPR,
4627 build1 (REALPART_EXPR,
4628 TREE_TYPE (ilhs),
4629 nlhs));
4630 gsi_replace (gsi, g, true);
4631 return true;
4635 return false;
4637 if (code == MINUS_EXPR && !rhs[2])
4638 return false;
4639 if (code == MINUS_EXPR)
4640 /* Code below expects rhs[0] and rhs[1] to have the IMAGPART_EXPRs.
4641 So, for MINUS_EXPR swap the single added rhs operand (others are
4642 subtracted) to rhs[3]. */
4643 std::swap (rhs[0], rhs[3]);
4645 /* Walk from both operands of STMT (for +/- even sometimes from
4646 all the 4 addends or 3 subtrahends), see through casts and != 0
4647 statements which would preserve [0, 1] range of values and
4648 check which is initialized from __imag__. */
4649 gimple *im1 = NULL, *im2 = NULL;
4650 for (int i = 0; i < (code == MINUS_EXPR ? 3 : 4); i++)
4651 if (rhs[i] && TREE_CODE (rhs[i]) == SSA_NAME)
4653 gimple *im = uaddc_cast (SSA_NAME_DEF_STMT (rhs[i]));
4654 im = uaddc_ne0 (im);
4655 if (uaddc_is_cplxpart (im, IMAGPART_EXPR))
4657 if (im1 == NULL)
4659 im1 = im;
4660 if (i != 0)
4661 std::swap (rhs[0], rhs[i]);
4663 else
4665 im2 = im;
4666 if (i != 1)
4667 std::swap (rhs[1], rhs[i]);
4668 break;
4672 /* If we don't find at least two, punt. */
4673 if (!im2)
4674 return false;
4675 /* Check they are __imag__ of .ADD_OVERFLOW or .SUB_OVERFLOW call results,
4676 either both .ADD_OVERFLOW or both .SUB_OVERFLOW and that we have
4677 uaddc5/usubc5 named pattern for the corresponding mode. */
4678 gimple *ovf1
4679 = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im1), 0));
4680 gimple *ovf2
4681 = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im2), 0));
4682 internal_fn ifn;
4683 if (!is_gimple_call (ovf1)
4684 || !gimple_call_internal_p (ovf1)
4685 || ((ifn = gimple_call_internal_fn (ovf1)) != IFN_ADD_OVERFLOW
4686 && ifn != IFN_SUB_OVERFLOW)
4687 || !gimple_call_internal_p (ovf2, ifn)
4688 || optab_handler (ifn == IFN_ADD_OVERFLOW ? uaddc5_optab : usubc5_optab,
4689 TYPE_MODE (type)) == CODE_FOR_nothing
4690 || (rhs[2]
4691 && optab_handler (code == PLUS_EXPR ? uaddc5_optab : usubc5_optab,
4692 TYPE_MODE (type)) == CODE_FOR_nothing))
4693 return false;
4694 tree arg1, arg2, arg3 = NULL_TREE;
4695 gimple *re1 = NULL, *re2 = NULL;
4696 /* On one of the two calls, one of the .ADD_OVERFLOW/.SUB_OVERFLOW arguments
4697 should be initialized from __real__ of the other of the two calls.
4698 Though, for .SUB_OVERFLOW, it has to be the first argument, not the
4699 second one. */
4700 for (int i = (ifn == IFN_ADD_OVERFLOW ? 1 : 0); i >= 0; --i)
4701 for (gimple *ovf = ovf1; ovf; ovf = (ovf == ovf1 ? ovf2 : NULL))
4703 tree arg = gimple_call_arg (ovf, i);
4704 if (TREE_CODE (arg) != SSA_NAME)
4705 continue;
4706 re1 = SSA_NAME_DEF_STMT (arg);
4707 if (uaddc_is_cplxpart (re1, REALPART_EXPR)
4708 && (SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (re1), 0))
4709 == (ovf == ovf1 ? ovf2 : ovf1)))
4711 if (ovf == ovf1)
4713 /* Make sure ovf2 is the .*_OVERFLOW call with argument
4714 initialized from __real__ of ovf1. */
4715 std::swap (rhs[0], rhs[1]);
4716 std::swap (im1, im2);
4717 std::swap (ovf1, ovf2);
4719 arg3 = gimple_call_arg (ovf, 1 - i);
4720 i = -1;
4721 break;
4724 if (!arg3)
4725 return false;
4726 arg1 = gimple_call_arg (ovf1, 0);
4727 arg2 = gimple_call_arg (ovf1, 1);
4728 if (!types_compatible_p (type, TREE_TYPE (arg1)))
4729 return false;
4730 int kind[2] = { 0, 0 };
4731 tree arg_im[2] = { NULL_TREE, NULL_TREE };
4732 /* At least one of arg2 and arg3 should have type compatible
4733 with arg1/rhs[0], and the other one should have value in [0, 1]
4734 range. If both are in [0, 1] range and type compatible with
4735 arg1/rhs[0], try harder to find after looking through casts,
4736 != 0 comparisons which one is initialized to __imag__ of
4737 .{ADD,SUB}_OVERFLOW or .U{ADD,SUB}C call results. */
4738 for (int i = 0; i < 2; ++i)
4740 tree arg = i == 0 ? arg2 : arg3;
4741 if (types_compatible_p (type, TREE_TYPE (arg)))
4742 kind[i] = 1;
4743 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg))
4744 || (TYPE_PRECISION (TREE_TYPE (arg)) == 1
4745 && !TYPE_UNSIGNED (TREE_TYPE (arg))))
4746 continue;
4747 if (tree_zero_one_valued_p (arg))
4748 kind[i] |= 2;
4749 if (TREE_CODE (arg) == SSA_NAME)
4751 gimple *g = SSA_NAME_DEF_STMT (arg);
4752 if (gimple_assign_cast_p (g))
4754 tree op = gimple_assign_rhs1 (g);
4755 if (TREE_CODE (op) == SSA_NAME
4756 && INTEGRAL_TYPE_P (TREE_TYPE (op)))
4757 g = SSA_NAME_DEF_STMT (op);
4759 g = uaddc_ne0 (g);
4760 if (!uaddc_is_cplxpart (g, IMAGPART_EXPR))
4761 continue;
4762 arg_im[i] = gimple_assign_lhs (g);
4763 g = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (g), 0));
4764 if (!is_gimple_call (g) || !gimple_call_internal_p (g))
4765 continue;
4766 switch (gimple_call_internal_fn (g))
4768 case IFN_ADD_OVERFLOW:
4769 case IFN_SUB_OVERFLOW:
4770 case IFN_UADDC:
4771 case IFN_USUBC:
4772 break;
4773 default:
4774 continue;
4776 kind[i] |= 4;
4779 /* Make arg2 the one with compatible type and arg3 the one
4780 with [0, 1] range. If both is true for both operands,
4781 prefer as arg3 result of __imag__ of some ifn. */
4782 if ((kind[0] & 1) == 0 || ((kind[1] & 1) != 0 && kind[0] > kind[1]))
4784 std::swap (arg2, arg3);
4785 std::swap (kind[0], kind[1]);
4786 std::swap (arg_im[0], arg_im[1]);
4788 if ((kind[0] & 1) == 0 || (kind[1] & 6) == 0)
4789 return false;
4790 if (!has_single_use (gimple_assign_lhs (im1))
4791 || !has_single_use (gimple_assign_lhs (im2))
4792 || !has_single_use (gimple_assign_lhs (re1))
4793 || num_imm_uses (gimple_call_lhs (ovf1)) != 2)
4794 return false;
4795 /* Check that ovf2's result is used in __real__ and set re2
4796 to that statement. */
4797 use_operand_p use_p;
4798 imm_use_iterator iter;
4799 tree lhs = gimple_call_lhs (ovf2);
4800 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4802 gimple *use_stmt = USE_STMT (use_p);
4803 if (is_gimple_debug (use_stmt))
4804 continue;
4805 if (use_stmt == im2)
4806 continue;
4807 if (re2)
4808 return false;
4809 if (!uaddc_is_cplxpart (use_stmt, REALPART_EXPR))
4810 return false;
4811 re2 = use_stmt;
4813 /* Build .UADDC/.USUBC call which will be placed before the stmt. */
4814 gimple_stmt_iterator gsi2 = gsi_for_stmt (ovf2);
4815 gimple *g;
4816 if ((kind[1] & 4) != 0 && types_compatible_p (type, TREE_TYPE (arg_im[1])))
4817 arg3 = arg_im[1];
4818 if ((kind[1] & 1) == 0)
4820 if (TREE_CODE (arg3) == INTEGER_CST)
4821 arg3 = fold_convert (type, arg3);
4822 else
4824 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, arg3);
4825 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
4826 arg3 = gimple_assign_lhs (g);
4829 g = gimple_build_call_internal (ifn == IFN_ADD_OVERFLOW
4830 ? IFN_UADDC : IFN_USUBC,
4831 3, arg1, arg2, arg3);
4832 tree nlhs = make_ssa_name (TREE_TYPE (lhs));
4833 gimple_call_set_lhs (g, nlhs);
4834 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
4835 /* In the case where stmt is | or ^ of two overflow flags
4836 or addition of those, replace stmt with __imag__ of the above
4837 added call. In case of arg1 + arg2 + (ovf1 + ovf2) or
4838 arg1 - arg2 - (ovf1 + ovf2) just emit it before stmt. */
4839 tree ilhs = rhs[2] ? make_ssa_name (type) : gimple_assign_lhs (stmt);
4840 g = gimple_build_assign (ilhs, IMAGPART_EXPR,
4841 build1 (IMAGPART_EXPR, TREE_TYPE (ilhs), nlhs));
4842 if (rhs[2])
4843 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4844 else
4845 gsi_replace (gsi, g, true);
4846 /* Remove some statements which can't be kept in the IL because they
4847 use SSA_NAME whose setter is going to be removed too. */
4848 tree rhs1 = rhs[1];
4849 for (int i = 0; i < 2; i++)
4850 if (rhs1 == gimple_assign_lhs (im2))
4851 break;
4852 else
4854 g = SSA_NAME_DEF_STMT (rhs1);
4855 rhs1 = gimple_assign_rhs1 (g);
4856 gsi2 = gsi_for_stmt (g);
4857 gsi_remove (&gsi2, true);
4859 gcc_checking_assert (rhs1 == gimple_assign_lhs (im2));
4860 gsi2 = gsi_for_stmt (im2);
4861 gsi_remove (&gsi2, true);
4862 /* Replace the re2 statement with __real__ of the newly added
4863 .UADDC/.USUBC call. */
4864 gsi2 = gsi_for_stmt (re2);
4865 tree rlhs = gimple_assign_lhs (re2);
4866 g = gimple_build_assign (rlhs, REALPART_EXPR,
4867 build1 (REALPART_EXPR, TREE_TYPE (rlhs), nlhs));
4868 gsi_replace (&gsi2, g, true);
4869 if (rhs[2])
4871 /* If this is the arg1 + arg2 + (ovf1 + ovf2) or
4872 arg1 - arg2 - (ovf1 + ovf2) case for the most significant limb,
4873 replace stmt with __real__ of another .UADDC/.USUBC call which
4874 handles the most significant limb. Overflow flag from this is
4875 ignored. */
4876 g = gimple_build_call_internal (code == PLUS_EXPR
4877 ? IFN_UADDC : IFN_USUBC,
4878 3, rhs[3], rhs[2], ilhs);
4879 nlhs = make_ssa_name (TREE_TYPE (lhs));
4880 gimple_call_set_lhs (g, nlhs);
4881 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4882 ilhs = gimple_assign_lhs (stmt);
4883 g = gimple_build_assign (ilhs, REALPART_EXPR,
4884 build1 (REALPART_EXPR, TREE_TYPE (ilhs), nlhs));
4885 gsi_replace (gsi, g, true);
4887 if (TREE_CODE (arg3) == SSA_NAME)
4889 /* When pattern recognizing the second least significant limb
4890 above (i.e. first pair of .{ADD,SUB}_OVERFLOW calls for one limb),
4891 check if the [0, 1] range argument (i.e. carry in) isn't the
4892 result of another .{ADD,SUB}_OVERFLOW call (one handling the
4893 least significant limb). Again look through casts and != 0. */
4894 gimple *im3 = SSA_NAME_DEF_STMT (arg3);
4895 for (int i = 0; i < 2; ++i)
4897 gimple *im4 = uaddc_cast (im3);
4898 if (im4 == im3)
4899 break;
4900 else
4901 im3 = im4;
4903 im3 = uaddc_ne0 (im3);
4904 if (uaddc_is_cplxpart (im3, IMAGPART_EXPR))
4906 gimple *ovf3
4907 = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im3), 0));
4908 if (gimple_call_internal_p (ovf3, ifn))
4910 lhs = gimple_call_lhs (ovf3);
4911 arg1 = gimple_call_arg (ovf3, 0);
4912 arg2 = gimple_call_arg (ovf3, 1);
4913 if (types_compatible_p (type, TREE_TYPE (TREE_TYPE (lhs)))
4914 && types_compatible_p (type, TREE_TYPE (arg1))
4915 && types_compatible_p (type, TREE_TYPE (arg2)))
4917 /* And if it is initialized from result of __imag__
4918 of .{ADD,SUB}_OVERFLOW call, replace that
4919 call with .U{ADD,SUB}C call with the same arguments,
4920 just 0 added as third argument. This isn't strictly
4921 necessary, .ADD_OVERFLOW (x, y) and .UADDC (x, y, 0)
4922 produce the same result, but may result in better
4923 generated code on some targets where the backend can
4924 better prepare in how the result will be used. */
4925 g = gimple_build_call_internal (ifn == IFN_ADD_OVERFLOW
4926 ? IFN_UADDC : IFN_USUBC,
4927 3, arg1, arg2,
4928 build_zero_cst (type));
4929 gimple_call_set_lhs (g, lhs);
4930 gsi2 = gsi_for_stmt (ovf3);
4931 gsi_replace (&gsi2, g, true);
4936 return true;
4939 /* Return true if target has support for divmod. */
4941 static bool
4942 target_supports_divmod_p (optab divmod_optab, optab div_optab, machine_mode mode)
4944 /* If target supports hardware divmod insn, use it for divmod. */
4945 if (optab_handler (divmod_optab, mode) != CODE_FOR_nothing)
4946 return true;
4948 /* Check if libfunc for divmod is available. */
4949 rtx libfunc = optab_libfunc (divmod_optab, mode);
4950 if (libfunc != NULL_RTX)
4952 /* If optab_handler exists for div_optab, perhaps in a wider mode,
4953 we don't want to use the libfunc even if it exists for given mode. */
4954 machine_mode div_mode;
4955 FOR_EACH_MODE_FROM (div_mode, mode)
4956 if (optab_handler (div_optab, div_mode) != CODE_FOR_nothing)
4957 return false;
4959 return targetm.expand_divmod_libfunc != NULL;
4962 return false;
4965 /* Check if stmt is candidate for divmod transform. */
4967 static bool
4968 divmod_candidate_p (gassign *stmt)
4970 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
4971 machine_mode mode = TYPE_MODE (type);
4972 optab divmod_optab, div_optab;
4974 if (TYPE_UNSIGNED (type))
4976 divmod_optab = udivmod_optab;
4977 div_optab = udiv_optab;
4979 else
4981 divmod_optab = sdivmod_optab;
4982 div_optab = sdiv_optab;
4985 tree op1 = gimple_assign_rhs1 (stmt);
4986 tree op2 = gimple_assign_rhs2 (stmt);
4988 /* Disable the transform if either is a constant, since division-by-constant
4989 may have specialized expansion. */
4990 if (CONSTANT_CLASS_P (op1))
4991 return false;
4993 if (CONSTANT_CLASS_P (op2))
4995 if (integer_pow2p (op2))
4996 return false;
4998 if (element_precision (type) <= HOST_BITS_PER_WIDE_INT
4999 && element_precision (type) <= BITS_PER_WORD)
5000 return false;
5002 /* If the divisor is not power of 2 and the precision wider than
5003 HWI, expand_divmod punts on that, so in that case it is better
5004 to use divmod optab or libfunc. Similarly if choose_multiplier
5005 might need pre/post shifts of BITS_PER_WORD or more. */
5008 /* Exclude the case where TYPE_OVERFLOW_TRAPS (type) as that should
5009 expand using the [su]divv optabs. */
5010 if (TYPE_OVERFLOW_TRAPS (type))
5011 return false;
5013 if (!target_supports_divmod_p (divmod_optab, div_optab, mode))
5014 return false;
5016 return true;
5019 /* This function looks for:
5020 t1 = a TRUNC_DIV_EXPR b;
5021 t2 = a TRUNC_MOD_EXPR b;
5022 and transforms it to the following sequence:
5023 complex_tmp = DIVMOD (a, b);
5024 t1 = REALPART_EXPR(a);
5025 t2 = IMAGPART_EXPR(b);
5026 For conditions enabling the transform see divmod_candidate_p().
5028 The pass has three parts:
5029 1) Find top_stmt which is trunc_div or trunc_mod stmt and dominates all
5030 other trunc_div_expr and trunc_mod_expr stmts.
5031 2) Add top_stmt and all trunc_div and trunc_mod stmts dominated by top_stmt
5032 to stmts vector.
5033 3) Insert DIVMOD call just before top_stmt and update entries in
5034 stmts vector to use return value of DIMOVD (REALEXPR_PART for div,
5035 IMAGPART_EXPR for mod). */
5037 static bool
5038 convert_to_divmod (gassign *stmt)
5040 if (stmt_can_throw_internal (cfun, stmt)
5041 || !divmod_candidate_p (stmt))
5042 return false;
5044 tree op1 = gimple_assign_rhs1 (stmt);
5045 tree op2 = gimple_assign_rhs2 (stmt);
5047 imm_use_iterator use_iter;
5048 gimple *use_stmt;
5049 auto_vec<gimple *> stmts;
5051 gimple *top_stmt = stmt;
5052 basic_block top_bb = gimple_bb (stmt);
5054 /* Part 1: Try to set top_stmt to "topmost" stmt that dominates
5055 at-least stmt and possibly other trunc_div/trunc_mod stmts
5056 having same operands as stmt. */
5058 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, op1)
5060 if (is_gimple_assign (use_stmt)
5061 && (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR
5062 || gimple_assign_rhs_code (use_stmt) == TRUNC_MOD_EXPR)
5063 && operand_equal_p (op1, gimple_assign_rhs1 (use_stmt), 0)
5064 && operand_equal_p (op2, gimple_assign_rhs2 (use_stmt), 0))
5066 if (stmt_can_throw_internal (cfun, use_stmt))
5067 continue;
5069 basic_block bb = gimple_bb (use_stmt);
5071 if (bb == top_bb)
5073 if (gimple_uid (use_stmt) < gimple_uid (top_stmt))
5074 top_stmt = use_stmt;
5076 else if (dominated_by_p (CDI_DOMINATORS, top_bb, bb))
5078 top_bb = bb;
5079 top_stmt = use_stmt;
5084 tree top_op1 = gimple_assign_rhs1 (top_stmt);
5085 tree top_op2 = gimple_assign_rhs2 (top_stmt);
5087 stmts.safe_push (top_stmt);
5088 bool div_seen = (gimple_assign_rhs_code (top_stmt) == TRUNC_DIV_EXPR);
5090 /* Part 2: Add all trunc_div/trunc_mod statements domianted by top_bb
5091 to stmts vector. The 2nd loop will always add stmt to stmts vector, since
5092 gimple_bb (top_stmt) dominates gimple_bb (stmt), so the
5093 2nd loop ends up adding at-least single trunc_mod_expr stmt. */
5095 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, top_op1)
5097 if (is_gimple_assign (use_stmt)
5098 && (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR
5099 || gimple_assign_rhs_code (use_stmt) == TRUNC_MOD_EXPR)
5100 && operand_equal_p (top_op1, gimple_assign_rhs1 (use_stmt), 0)
5101 && operand_equal_p (top_op2, gimple_assign_rhs2 (use_stmt), 0))
5103 if (use_stmt == top_stmt
5104 || stmt_can_throw_internal (cfun, use_stmt)
5105 || !dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt), top_bb))
5106 continue;
5108 stmts.safe_push (use_stmt);
5109 if (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR)
5110 div_seen = true;
5114 if (!div_seen)
5115 return false;
5117 /* Part 3: Create libcall to internal fn DIVMOD:
5118 divmod_tmp = DIVMOD (op1, op2). */
5120 gcall *call_stmt = gimple_build_call_internal (IFN_DIVMOD, 2, op1, op2);
5121 tree res = make_temp_ssa_name (build_complex_type (TREE_TYPE (op1)),
5122 call_stmt, "divmod_tmp");
5123 gimple_call_set_lhs (call_stmt, res);
5124 /* We rejected throwing statements above. */
5125 gimple_call_set_nothrow (call_stmt, true);
5127 /* Insert the call before top_stmt. */
5128 gimple_stmt_iterator top_stmt_gsi = gsi_for_stmt (top_stmt);
5129 gsi_insert_before (&top_stmt_gsi, call_stmt, GSI_SAME_STMT);
5131 widen_mul_stats.divmod_calls_inserted++;
5133 /* Update all statements in stmts vector:
5134 lhs = op1 TRUNC_DIV_EXPR op2 -> lhs = REALPART_EXPR<divmod_tmp>
5135 lhs = op1 TRUNC_MOD_EXPR op2 -> lhs = IMAGPART_EXPR<divmod_tmp>. */
5137 for (unsigned i = 0; stmts.iterate (i, &use_stmt); ++i)
5139 tree new_rhs;
5141 switch (gimple_assign_rhs_code (use_stmt))
5143 case TRUNC_DIV_EXPR:
5144 new_rhs = fold_build1 (REALPART_EXPR, TREE_TYPE (op1), res);
5145 break;
5147 case TRUNC_MOD_EXPR:
5148 new_rhs = fold_build1 (IMAGPART_EXPR, TREE_TYPE (op1), res);
5149 break;
5151 default:
5152 gcc_unreachable ();
5155 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
5156 gimple_assign_set_rhs_from_tree (&gsi, new_rhs);
5157 update_stmt (use_stmt);
5160 return true;
5163 /* Process a single gimple assignment STMT, which has a RSHIFT_EXPR as
5164 its rhs, and try to convert it into a MULT_HIGHPART_EXPR. The return
5165 value is true iff we converted the statement. */
5167 static bool
5168 convert_mult_to_highpart (gassign *stmt, gimple_stmt_iterator *gsi)
5170 tree lhs = gimple_assign_lhs (stmt);
5171 tree stype = TREE_TYPE (lhs);
5172 tree sarg0 = gimple_assign_rhs1 (stmt);
5173 tree sarg1 = gimple_assign_rhs2 (stmt);
5175 if (TREE_CODE (stype) != INTEGER_TYPE
5176 || TREE_CODE (sarg1) != INTEGER_CST
5177 || TREE_CODE (sarg0) != SSA_NAME
5178 || !tree_fits_uhwi_p (sarg1)
5179 || !has_single_use (sarg0))
5180 return false;
5182 gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (sarg0));
5183 if (!def)
5184 return false;
5186 enum tree_code mcode = gimple_assign_rhs_code (def);
5187 if (mcode == NOP_EXPR)
5189 tree tmp = gimple_assign_rhs1 (def);
5190 if (TREE_CODE (tmp) != SSA_NAME || !has_single_use (tmp))
5191 return false;
5192 def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (tmp));
5193 if (!def)
5194 return false;
5195 mcode = gimple_assign_rhs_code (def);
5198 if (mcode != WIDEN_MULT_EXPR
5199 || gimple_bb (def) != gimple_bb (stmt))
5200 return false;
5201 tree mtype = TREE_TYPE (gimple_assign_lhs (def));
5202 if (TREE_CODE (mtype) != INTEGER_TYPE
5203 || TYPE_PRECISION (mtype) != TYPE_PRECISION (stype))
5204 return false;
5206 tree mop1 = gimple_assign_rhs1 (def);
5207 tree mop2 = gimple_assign_rhs2 (def);
5208 tree optype = TREE_TYPE (mop1);
5209 bool unsignedp = TYPE_UNSIGNED (optype);
5210 unsigned int prec = TYPE_PRECISION (optype);
5212 if (unsignedp != TYPE_UNSIGNED (mtype)
5213 || TYPE_PRECISION (mtype) != 2 * prec)
5214 return false;
5216 unsigned HOST_WIDE_INT bits = tree_to_uhwi (sarg1);
5217 if (bits < prec || bits >= 2 * prec)
5218 return false;
5220 /* For the time being, require operands to have the same sign. */
5221 if (unsignedp != TYPE_UNSIGNED (TREE_TYPE (mop2)))
5222 return false;
5224 machine_mode mode = TYPE_MODE (optype);
5225 optab tab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
5226 if (optab_handler (tab, mode) == CODE_FOR_nothing)
5227 return false;
5229 location_t loc = gimple_location (stmt);
5230 tree highpart1 = build_and_insert_binop (gsi, loc, "highparttmp",
5231 MULT_HIGHPART_EXPR, mop1, mop2);
5232 tree highpart2 = highpart1;
5233 tree ntype = optype;
5235 if (TYPE_UNSIGNED (stype) != TYPE_UNSIGNED (optype))
5237 ntype = TYPE_UNSIGNED (stype) ? unsigned_type_for (optype)
5238 : signed_type_for (optype);
5239 highpart2 = build_and_insert_cast (gsi, loc, ntype, highpart1);
5241 if (bits > prec)
5242 highpart2 = build_and_insert_binop (gsi, loc, "highparttmp",
5243 RSHIFT_EXPR, highpart2,
5244 build_int_cst (ntype, bits - prec));
5246 gassign *new_stmt = gimple_build_assign (lhs, NOP_EXPR, highpart2);
5247 gsi_replace (gsi, new_stmt, true);
5249 widen_mul_stats.highpart_mults_inserted++;
5250 return true;
5253 /* If target has spaceship<MODE>3 expander, pattern recognize
5254 <bb 2> [local count: 1073741824]:
5255 if (a_2(D) == b_3(D))
5256 goto <bb 6>; [34.00%]
5257 else
5258 goto <bb 3>; [66.00%]
5260 <bb 3> [local count: 708669601]:
5261 if (a_2(D) < b_3(D))
5262 goto <bb 6>; [1.04%]
5263 else
5264 goto <bb 4>; [98.96%]
5266 <bb 4> [local count: 701299439]:
5267 if (a_2(D) > b_3(D))
5268 goto <bb 5>; [48.89%]
5269 else
5270 goto <bb 6>; [51.11%]
5272 <bb 5> [local count: 342865295]:
5274 <bb 6> [local count: 1073741824]:
5275 and turn it into:
5276 <bb 2> [local count: 1073741824]:
5277 _1 = .SPACESHIP (a_2(D), b_3(D));
5278 if (_1 == 0)
5279 goto <bb 6>; [34.00%]
5280 else
5281 goto <bb 3>; [66.00%]
5283 <bb 3> [local count: 708669601]:
5284 if (_1 == -1)
5285 goto <bb 6>; [1.04%]
5286 else
5287 goto <bb 4>; [98.96%]
5289 <bb 4> [local count: 701299439]:
5290 if (_1 == 1)
5291 goto <bb 5>; [48.89%]
5292 else
5293 goto <bb 6>; [51.11%]
5295 <bb 5> [local count: 342865295]:
5297 <bb 6> [local count: 1073741824]:
5298 so that the backend can emit optimal comparison and
5299 conditional jump sequence. */
5301 static void
5302 optimize_spaceship (gcond *stmt)
5304 enum tree_code code = gimple_cond_code (stmt);
5305 if (code != EQ_EXPR && code != NE_EXPR)
5306 return;
5307 tree arg1 = gimple_cond_lhs (stmt);
5308 tree arg2 = gimple_cond_rhs (stmt);
5309 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1))
5310 || optab_handler (spaceship_optab,
5311 TYPE_MODE (TREE_TYPE (arg1))) == CODE_FOR_nothing
5312 || operand_equal_p (arg1, arg2, 0))
5313 return;
5315 basic_block bb0 = gimple_bb (stmt), bb1, bb2 = NULL;
5316 edge em1 = NULL, e1 = NULL, e2 = NULL;
5317 bb1 = EDGE_SUCC (bb0, 1)->dest;
5318 if (((EDGE_SUCC (bb0, 0)->flags & EDGE_TRUE_VALUE) != 0) ^ (code == EQ_EXPR))
5319 bb1 = EDGE_SUCC (bb0, 0)->dest;
5321 gcond *g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb1));
5322 if (g == NULL
5323 || !single_pred_p (bb1)
5324 || (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
5325 ? !operand_equal_p (gimple_cond_rhs (g), arg2, 0)
5326 : (!operand_equal_p (gimple_cond_lhs (g), arg2, 0)
5327 || !operand_equal_p (gimple_cond_rhs (g), arg1, 0)))
5328 || !cond_only_block_p (bb1))
5329 return;
5331 enum tree_code ccode = (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
5332 ? LT_EXPR : GT_EXPR);
5333 switch (gimple_cond_code (g))
5335 case LT_EXPR:
5336 case LE_EXPR:
5337 break;
5338 case GT_EXPR:
5339 case GE_EXPR:
5340 ccode = ccode == LT_EXPR ? GT_EXPR : LT_EXPR;
5341 break;
5342 default:
5343 return;
5346 for (int i = 0; i < 2; ++i)
5348 /* With NaNs, </<=/>/>= are false, so we need to look for the
5349 third comparison on the false edge from whatever non-equality
5350 comparison the second comparison is. */
5351 if (HONOR_NANS (TREE_TYPE (arg1))
5352 && (EDGE_SUCC (bb1, i)->flags & EDGE_TRUE_VALUE) != 0)
5353 continue;
5355 bb2 = EDGE_SUCC (bb1, i)->dest;
5356 g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb2));
5357 if (g == NULL
5358 || !single_pred_p (bb2)
5359 || (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
5360 ? !operand_equal_p (gimple_cond_rhs (g), arg2, 0)
5361 : (!operand_equal_p (gimple_cond_lhs (g), arg2, 0)
5362 || !operand_equal_p (gimple_cond_rhs (g), arg1, 0)))
5363 || !cond_only_block_p (bb2)
5364 || EDGE_SUCC (bb2, 0)->dest == EDGE_SUCC (bb2, 1)->dest)
5365 continue;
5367 enum tree_code ccode2
5368 = (operand_equal_p (gimple_cond_lhs (g), arg1, 0) ? LT_EXPR : GT_EXPR);
5369 switch (gimple_cond_code (g))
5371 case LT_EXPR:
5372 case LE_EXPR:
5373 break;
5374 case GT_EXPR:
5375 case GE_EXPR:
5376 ccode2 = ccode2 == LT_EXPR ? GT_EXPR : LT_EXPR;
5377 break;
5378 default:
5379 continue;
5381 if (HONOR_NANS (TREE_TYPE (arg1)) && ccode == ccode2)
5382 continue;
5384 if ((ccode == LT_EXPR)
5385 ^ ((EDGE_SUCC (bb1, i)->flags & EDGE_TRUE_VALUE) != 0))
5387 em1 = EDGE_SUCC (bb1, 1 - i);
5388 e1 = EDGE_SUCC (bb2, 0);
5389 e2 = EDGE_SUCC (bb2, 1);
5390 if ((ccode2 == LT_EXPR) ^ ((e1->flags & EDGE_TRUE_VALUE) == 0))
5391 std::swap (e1, e2);
5393 else
5395 e1 = EDGE_SUCC (bb1, 1 - i);
5396 em1 = EDGE_SUCC (bb2, 0);
5397 e2 = EDGE_SUCC (bb2, 1);
5398 if ((ccode2 != LT_EXPR) ^ ((em1->flags & EDGE_TRUE_VALUE) == 0))
5399 std::swap (em1, e2);
5401 break;
5404 if (em1 == NULL)
5406 if ((ccode == LT_EXPR)
5407 ^ ((EDGE_SUCC (bb1, 0)->flags & EDGE_TRUE_VALUE) != 0))
5409 em1 = EDGE_SUCC (bb1, 1);
5410 e1 = EDGE_SUCC (bb1, 0);
5411 e2 = (e1->flags & EDGE_TRUE_VALUE) ? em1 : e1;
5413 else
5415 em1 = EDGE_SUCC (bb1, 0);
5416 e1 = EDGE_SUCC (bb1, 1);
5417 e2 = (e1->flags & EDGE_TRUE_VALUE) ? em1 : e1;
5421 gcall *gc = gimple_build_call_internal (IFN_SPACESHIP, 2, arg1, arg2);
5422 tree lhs = make_ssa_name (integer_type_node);
5423 gimple_call_set_lhs (gc, lhs);
5424 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5425 gsi_insert_before (&gsi, gc, GSI_SAME_STMT);
5427 gimple_cond_set_lhs (stmt, lhs);
5428 gimple_cond_set_rhs (stmt, integer_zero_node);
5429 update_stmt (stmt);
5431 gcond *cond = as_a <gcond *> (*gsi_last_bb (bb1));
5432 gimple_cond_set_lhs (cond, lhs);
5433 if (em1->src == bb1 && e2 != em1)
5435 gimple_cond_set_rhs (cond, integer_minus_one_node);
5436 gimple_cond_set_code (cond, (em1->flags & EDGE_TRUE_VALUE)
5437 ? EQ_EXPR : NE_EXPR);
5439 else
5441 gcc_assert (e1->src == bb1 && e2 != e1);
5442 gimple_cond_set_rhs (cond, integer_one_node);
5443 gimple_cond_set_code (cond, (e1->flags & EDGE_TRUE_VALUE)
5444 ? EQ_EXPR : NE_EXPR);
5446 update_stmt (cond);
5448 if (e2 != e1 && e2 != em1)
5450 cond = as_a <gcond *> (*gsi_last_bb (bb2));
5451 gimple_cond_set_lhs (cond, lhs);
5452 if (em1->src == bb2)
5453 gimple_cond_set_rhs (cond, integer_minus_one_node);
5454 else
5456 gcc_assert (e1->src == bb2);
5457 gimple_cond_set_rhs (cond, integer_one_node);
5459 gimple_cond_set_code (cond,
5460 (e2->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR);
5461 update_stmt (cond);
5464 wide_int wm1 = wi::minus_one (TYPE_PRECISION (integer_type_node));
5465 wide_int w2 = wi::two (TYPE_PRECISION (integer_type_node));
5466 value_range vr (TREE_TYPE (lhs), wm1, w2);
5467 set_range_info (lhs, vr);
5471 /* Find integer multiplications where the operands are extended from
5472 smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR
5473 or MULT_HIGHPART_EXPR where appropriate. */
5475 namespace {
5477 const pass_data pass_data_optimize_widening_mul =
5479 GIMPLE_PASS, /* type */
5480 "widening_mul", /* name */
5481 OPTGROUP_NONE, /* optinfo_flags */
5482 TV_TREE_WIDEN_MUL, /* tv_id */
5483 PROP_ssa, /* properties_required */
5484 0, /* properties_provided */
5485 0, /* properties_destroyed */
5486 0, /* todo_flags_start */
5487 TODO_update_ssa, /* todo_flags_finish */
5490 class pass_optimize_widening_mul : public gimple_opt_pass
5492 public:
5493 pass_optimize_widening_mul (gcc::context *ctxt)
5494 : gimple_opt_pass (pass_data_optimize_widening_mul, ctxt)
5497 /* opt_pass methods: */
5498 bool gate (function *) final override
5500 return flag_expensive_optimizations && optimize;
5503 unsigned int execute (function *) final override;
5505 }; // class pass_optimize_widening_mul
5507 /* Walker class to perform the transformation in reverse dominance order. */
5509 class math_opts_dom_walker : public dom_walker
5511 public:
5512 /* Constructor, CFG_CHANGED is a pointer to a boolean flag that will be set
5513 if walking modidifes the CFG. */
5515 math_opts_dom_walker (bool *cfg_changed_p)
5516 : dom_walker (CDI_DOMINATORS), m_last_result_set (),
5517 m_cfg_changed_p (cfg_changed_p) {}
5519 /* The actual actions performed in the walk. */
5521 void after_dom_children (basic_block) final override;
5523 /* Set of results of chains of multiply and add statement combinations that
5524 were not transformed into FMAs because of active deferring. */
5525 hash_set<tree> m_last_result_set;
5527 /* Pointer to a flag of the user that needs to be set if CFG has been
5528 modified. */
5529 bool *m_cfg_changed_p;
5532 void
5533 math_opts_dom_walker::after_dom_children (basic_block bb)
5535 gimple_stmt_iterator gsi;
5537 fma_deferring_state fma_state (param_avoid_fma_max_bits > 0);
5539 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
5541 gimple *stmt = gsi_stmt (gsi);
5542 enum tree_code code;
5544 if (is_gimple_assign (stmt))
5546 code = gimple_assign_rhs_code (stmt);
5547 switch (code)
5549 case MULT_EXPR:
5550 if (!convert_mult_to_widen (stmt, &gsi)
5551 && !convert_expand_mult_copysign (stmt, &gsi)
5552 && convert_mult_to_fma (stmt,
5553 gimple_assign_rhs1 (stmt),
5554 gimple_assign_rhs2 (stmt),
5555 &fma_state))
5557 gsi_remove (&gsi, true);
5558 release_defs (stmt);
5559 continue;
5561 match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p);
5562 break;
5564 case PLUS_EXPR:
5565 case MINUS_EXPR:
5566 if (!convert_plusminus_to_widen (&gsi, stmt, code))
5568 match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p);
5569 if (gsi_stmt (gsi) == stmt)
5570 match_uaddc_usubc (&gsi, stmt, code);
5572 break;
5574 case BIT_NOT_EXPR:
5575 if (match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p))
5576 continue;
5577 break;
5579 case TRUNC_MOD_EXPR:
5580 convert_to_divmod (as_a<gassign *> (stmt));
5581 break;
5583 case RSHIFT_EXPR:
5584 convert_mult_to_highpart (as_a<gassign *> (stmt), &gsi);
5585 break;
5587 case BIT_IOR_EXPR:
5588 case BIT_XOR_EXPR:
5589 match_uaddc_usubc (&gsi, stmt, code);
5590 break;
5592 default:;
5595 else if (is_gimple_call (stmt))
5597 switch (gimple_call_combined_fn (stmt))
5599 CASE_CFN_POW:
5600 if (gimple_call_lhs (stmt)
5601 && TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
5602 && real_equal (&TREE_REAL_CST (gimple_call_arg (stmt, 1)),
5603 &dconst2)
5604 && convert_mult_to_fma (stmt,
5605 gimple_call_arg (stmt, 0),
5606 gimple_call_arg (stmt, 0),
5607 &fma_state))
5609 unlink_stmt_vdef (stmt);
5610 if (gsi_remove (&gsi, true)
5611 && gimple_purge_dead_eh_edges (bb))
5612 *m_cfg_changed_p = true;
5613 release_defs (stmt);
5614 continue;
5616 break;
5618 case CFN_COND_MUL:
5619 if (convert_mult_to_fma (stmt,
5620 gimple_call_arg (stmt, 1),
5621 gimple_call_arg (stmt, 2),
5622 &fma_state,
5623 gimple_call_arg (stmt, 0)))
5626 gsi_remove (&gsi, true);
5627 release_defs (stmt);
5628 continue;
5630 break;
5632 case CFN_LAST:
5633 cancel_fma_deferring (&fma_state);
5634 break;
5636 default:
5637 break;
5640 else if (gimple_code (stmt) == GIMPLE_COND)
5641 optimize_spaceship (as_a <gcond *> (stmt));
5642 gsi_next (&gsi);
5644 if (fma_state.m_deferring_p
5645 && fma_state.m_initial_phi)
5647 gcc_checking_assert (fma_state.m_last_result);
5648 if (!last_fma_candidate_feeds_initial_phi (&fma_state,
5649 &m_last_result_set))
5650 cancel_fma_deferring (&fma_state);
5651 else
5652 m_last_result_set.add (fma_state.m_last_result);
5657 unsigned int
5658 pass_optimize_widening_mul::execute (function *fun)
5660 bool cfg_changed = false;
5662 memset (&widen_mul_stats, 0, sizeof (widen_mul_stats));
5663 calculate_dominance_info (CDI_DOMINATORS);
5664 renumber_gimple_stmt_uids (cfun);
5666 math_opts_dom_walker (&cfg_changed).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5668 statistics_counter_event (fun, "widening multiplications inserted",
5669 widen_mul_stats.widen_mults_inserted);
5670 statistics_counter_event (fun, "widening maccs inserted",
5671 widen_mul_stats.maccs_inserted);
5672 statistics_counter_event (fun, "fused multiply-adds inserted",
5673 widen_mul_stats.fmas_inserted);
5674 statistics_counter_event (fun, "divmod calls inserted",
5675 widen_mul_stats.divmod_calls_inserted);
5676 statistics_counter_event (fun, "highpart multiplications inserted",
5677 widen_mul_stats.highpart_mults_inserted);
5679 return cfg_changed ? TODO_cleanup_cfg : 0;
5682 } // anon namespace
5684 gimple_opt_pass *
5685 make_pass_optimize_widening_mul (gcc::context *ctxt)
5687 return new pass_optimize_widening_mul (ctxt);