]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-math-opts.c
PR tree-optimization/83521
[thirdparty/gcc.git] / gcc / tree-ssa-math-opts.c
CommitLineData
abacb398 1/* Global, SSA-based optimizations using mathematical identities.
aad93da1 2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
48e1416a 3
abacb398 4This file is part of GCC.
48e1416a 5
abacb398 6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8c4c00c1 8Free Software Foundation; either version 3, or (at your option) any
abacb398 9later version.
48e1416a 10
abacb398 11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
48e1416a 15
abacb398 16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
abacb398 19
20/* Currently, the only mini-pass in this file tries to CSE reciprocal
21 operations. These are common in sequences such as this one:
22
23 modulus = sqrt(x*x + y*y + z*z);
24 x = x / modulus;
25 y = y / modulus;
26 z = z / modulus;
27
28 that can be optimized to
29
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;
35
36 We do this for loop invariant divisors, and with this pass whenever
ac70caad 37 we notice that a division has the same divisor multiple times.
38
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.
42
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
24794e79 45 by the same divisor. This is probably because modern processors
ac70caad 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.
50
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).
56
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.
60
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.
68
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).
75
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.
79
80 If we did this using domwalk.c, 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. */
abacb398 86
87#include "config.h"
88#include "system.h"
89#include "coretypes.h"
9ef16211 90#include "backend.h"
7c29e30e 91#include "target.h"
92#include "rtl.h"
9ef16211 93#include "tree.h"
94#include "gimple.h"
7c29e30e 95#include "predict.h"
96#include "alloc-pool.h"
97#include "tree-pass.h"
9ef16211 98#include "ssa.h"
7c29e30e 99#include "optabs-tree.h"
100#include "gimple-pretty-print.h"
b20a8bb4 101#include "alias.h"
b20a8bb4 102#include "fold-const.h"
bc61cadb 103#include "gimple-fold.h"
dcf1a1ec 104#include "gimple-iterator.h"
470d5bb5 105#include "gimplify.h"
e795d6e1 106#include "gimplify-me.h"
9ed99284 107#include "stor-layout.h"
073c1fd5 108#include "tree-cfg.h"
073c1fd5 109#include "tree-dfa.h"
69ee5dbb 110#include "tree-ssa.h"
f7715905 111#include "builtins.h"
c3206272 112#include "params.h"
4cfd27a5 113#include "internal-fn.h"
fa0793ad 114#include "case-cfn-macros.h"
67f7b566 115#include "optabs-libfuncs.h"
116#include "tree-eh.h"
117#include "targhooks.h"
ac70caad 118
119/* This structure represents one basic block that either computes a
120 division, or is a common dominator for basic block that compute a
121 division. */
122struct occurrence {
123 /* The basic block represented by this structure. */
124 basic_block bb;
125
126 /* If non-NULL, the SSA_NAME holding the definition for a reciprocal
127 inserted in BB. */
128 tree recip_def;
129
472f3f23 130 /* If non-NULL, the SSA_NAME holding the definition for a squared
131 reciprocal inserted in BB. */
132 tree square_recip_def;
133
75a70cf9 134 /* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that
ac70caad 135 was inserted in BB. */
42acab1c 136 gimple *recip_def_stmt;
ac70caad 137
138 /* Pointer to a list of "struct occurrence"s for blocks dominated
139 by BB. */
140 struct occurrence *children;
141
142 /* Pointer to the next "struct occurrence"s in the list of blocks
143 sharing a common dominator. */
144 struct occurrence *next;
145
146 /* The number of divisions that are in BB before compute_merit. The
147 number of divisions that are in BB or post-dominate it after
148 compute_merit. */
149 int num_divisions;
150
151 /* True if the basic block has a division, false if it is a common
152 dominator for basic blocks that do. If it is false and trapping
153 math is active, BB is not a candidate for inserting a reciprocal. */
154 bool bb_has_division;
155};
156
30c4e60d 157static struct
158{
159 /* Number of 1.0/X ops inserted. */
160 int rdivs_inserted;
161
162 /* Number of 1.0/FUNC ops inserted. */
163 int rfuncs_inserted;
164} reciprocal_stats;
165
166static struct
167{
168 /* Number of cexpi calls inserted. */
169 int inserted;
170} sincos_stats;
171
30c4e60d 172static struct
173{
174 /* Number of widening multiplication ops inserted. */
175 int widen_mults_inserted;
176
177 /* Number of integer multiply-and-accumulate ops inserted. */
178 int maccs_inserted;
179
180 /* Number of fp fused multiply-add ops inserted. */
181 int fmas_inserted;
67f7b566 182
183 /* Number of divmod calls inserted. */
184 int divmod_calls_inserted;
30c4e60d 185} widen_mul_stats;
ac70caad 186
187/* The instance of "struct occurrence" representing the highest
188 interesting block in the dominator tree. */
189static struct occurrence *occ_head;
190
191/* Allocation pool for getting instances of "struct occurrence". */
e16712b1 192static object_allocator<occurrence> *occ_pool;
ac70caad 193
194
195
196/* Allocate and return a new struct occurrence for basic block BB, and
197 whose children list is headed by CHILDREN. */
198static struct occurrence *
199occ_new (basic_block bb, struct occurrence *children)
abacb398 200{
ac70caad 201 struct occurrence *occ;
202
d8e7268c 203 bb->aux = occ = occ_pool->allocate ();
ac70caad 204 memset (occ, 0, sizeof (struct occurrence));
205
206 occ->bb = bb;
207 occ->children = children;
208 return occ;
abacb398 209}
210
ac70caad 211
212/* Insert NEW_OCC into our subset of the dominator tree. P_HEAD points to a
213 list of "struct occurrence"s, one per basic block, having IDOM as
214 their common dominator.
215
216 We try to insert NEW_OCC as deep as possible in the tree, and we also
217 insert any other block that is a common dominator for BB and one
218 block already in the tree. */
219
220static void
221insert_bb (struct occurrence *new_occ, basic_block idom,
222 struct occurrence **p_head)
9e583fac 223{
ac70caad 224 struct occurrence *occ, **p_occ;
9e583fac 225
ac70caad 226 for (p_occ = p_head; (occ = *p_occ) != NULL; )
227 {
228 basic_block bb = new_occ->bb, occ_bb = occ->bb;
229 basic_block dom = nearest_common_dominator (CDI_DOMINATORS, occ_bb, bb);
230 if (dom == bb)
231 {
232 /* BB dominates OCC_BB. OCC becomes NEW_OCC's child: remove OCC
233 from its list. */
234 *p_occ = occ->next;
235 occ->next = new_occ->children;
236 new_occ->children = occ;
237
238 /* Try the next block (it may as well be dominated by BB). */
239 }
240
241 else if (dom == occ_bb)
242 {
243 /* OCC_BB dominates BB. Tail recurse to look deeper. */
244 insert_bb (new_occ, dom, &occ->children);
245 return;
246 }
247
248 else if (dom != idom)
249 {
250 gcc_assert (!dom->aux);
251
252 /* There is a dominator between IDOM and BB, add it and make
253 two children out of NEW_OCC and OCC. First, remove OCC from
254 its list. */
255 *p_occ = occ->next;
256 new_occ->next = occ;
257 occ->next = NULL;
258
259 /* None of the previous blocks has DOM as a dominator: if we tail
260 recursed, we would reexamine them uselessly. Just switch BB with
261 DOM, and go on looking for blocks dominated by DOM. */
262 new_occ = occ_new (dom, new_occ);
263 }
264
265 else
266 {
267 /* Nothing special, go on with the next element. */
268 p_occ = &occ->next;
269 }
270 }
271
272 /* No place was found as a child of IDOM. Make BB a sibling of IDOM. */
273 new_occ->next = *p_head;
274 *p_head = new_occ;
275}
276
472f3f23 277/* Register that we found a division in BB.
278 IMPORTANCE is a measure of how much weighting to give
279 that division. Use IMPORTANCE = 2 to register a single
280 division. If the division is going to be found multiple
281 times use 1 (as it is with squares). */
ac70caad 282
283static inline void
472f3f23 284register_division_in (basic_block bb, int importance)
ac70caad 285{
286 struct occurrence *occ;
287
288 occ = (struct occurrence *) bb->aux;
289 if (!occ)
290 {
291 occ = occ_new (bb, NULL);
34154e27 292 insert_bb (occ, ENTRY_BLOCK_PTR_FOR_FN (cfun), &occ_head);
ac70caad 293 }
294
295 occ->bb_has_division = true;
472f3f23 296 occ->num_divisions += importance;
ac70caad 297}
298
299
300/* Compute the number of divisions that postdominate each block in OCC and
301 its children. */
abacb398 302
abacb398 303static void
ac70caad 304compute_merit (struct occurrence *occ)
abacb398 305{
ac70caad 306 struct occurrence *occ_child;
307 basic_block dom = occ->bb;
abacb398 308
ac70caad 309 for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
abacb398 310 {
ac70caad 311 basic_block bb;
312 if (occ_child->children)
313 compute_merit (occ_child);
314
315 if (flag_exceptions)
316 bb = single_noncomplex_succ (dom);
317 else
318 bb = dom;
319
320 if (dominated_by_p (CDI_POST_DOMINATORS, bb, occ_child->bb))
321 occ->num_divisions += occ_child->num_divisions;
322 }
323}
324
325
326/* Return whether USE_STMT is a floating-point division by DEF. */
327static inline bool
42acab1c 328is_division_by (gimple *use_stmt, tree def)
ac70caad 329{
75a70cf9 330 return is_gimple_assign (use_stmt)
331 && gimple_assign_rhs_code (use_stmt) == RDIV_EXPR
332 && gimple_assign_rhs2 (use_stmt) == def
119368d7 333 /* Do not recognize x / x as valid division, as we are getting
334 confused later by replacing all immediate uses x in such
335 a stmt. */
75a70cf9 336 && gimple_assign_rhs1 (use_stmt) != def;
ac70caad 337}
338
472f3f23 339/* Return whether USE_STMT is DEF * DEF. */
340static inline bool
341is_square_of (gimple *use_stmt, tree def)
342{
343 if (gimple_code (use_stmt) == GIMPLE_ASSIGN
344 && gimple_assign_rhs_code (use_stmt) == MULT_EXPR)
345 {
346 tree op0 = gimple_assign_rhs1 (use_stmt);
347 tree op1 = gimple_assign_rhs2 (use_stmt);
348
349 return op0 == op1 && op0 == def;
350 }
351 return 0;
352}
353
354/* Return whether USE_STMT is a floating-point division by
355 DEF * DEF. */
356static inline bool
357is_division_by_square (gimple *use_stmt, tree def)
358{
359 if (gimple_code (use_stmt) == GIMPLE_ASSIGN
360 && gimple_assign_rhs_code (use_stmt) == RDIV_EXPR
361 && gimple_assign_rhs1 (use_stmt) != gimple_assign_rhs2 (use_stmt))
362 {
363 tree denominator = gimple_assign_rhs2 (use_stmt);
364 if (TREE_CODE (denominator) == SSA_NAME)
365 {
366 return is_square_of (SSA_NAME_DEF_STMT (denominator), def);
367 }
368 }
369 return 0;
370}
371
ac70caad 372/* Walk the subset of the dominator tree rooted at OCC, setting the
373 RECIP_DEF field to a definition of 1.0 / DEF that can be used in
374 the given basic block. The field may be left NULL, of course,
375 if it is not possible or profitable to do the optimization.
376
377 DEF_BSI is an iterator pointing at the statement defining DEF.
378 If RECIP_DEF is set, a dominator already has a computation that can
472f3f23 379 be used.
380
381 If should_insert_square_recip is set, then this also inserts
382 the square of the reciprocal immediately after the definition
383 of the reciprocal. */
ac70caad 384
385static void
75a70cf9 386insert_reciprocals (gimple_stmt_iterator *def_gsi, struct occurrence *occ,
472f3f23 387 tree def, tree recip_def, tree square_recip_def,
388 int should_insert_square_recip, int threshold)
ac70caad 389{
75a70cf9 390 tree type;
472f3f23 391 gassign *new_stmt, *new_square_stmt;
75a70cf9 392 gimple_stmt_iterator gsi;
ac70caad 393 struct occurrence *occ_child;
394
395 if (!recip_def
396 && (occ->bb_has_division || !flag_trapping_math)
472f3f23 397 /* Divide by two as all divisions are counted twice in
398 the costing loop. */
399 && occ->num_divisions / 2 >= threshold)
ac70caad 400 {
401 /* Make a variable with the replacement and substitute it. */
402 type = TREE_TYPE (def);
072f7ab1 403 recip_def = create_tmp_reg (type, "reciptmp");
e9cf809e 404 new_stmt = gimple_build_assign (recip_def, RDIV_EXPR,
405 build_one_cst (type), def);
48e1416a 406
472f3f23 407 if (should_insert_square_recip)
408 {
409 square_recip_def = create_tmp_reg (type, "powmult_reciptmp");
410 new_square_stmt = gimple_build_assign (square_recip_def, MULT_EXPR,
411 recip_def, recip_def);
412 }
413
ac70caad 414 if (occ->bb_has_division)
472f3f23 415 {
416 /* Case 1: insert before an existing division. */
417 gsi = gsi_after_labels (occ->bb);
418 while (!gsi_end_p (gsi)
419 && (!is_division_by (gsi_stmt (gsi), def))
420 && (!is_division_by_square (gsi_stmt (gsi), def)))
75a70cf9 421 gsi_next (&gsi);
ac70caad 422
472f3f23 423 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
424 }
75a70cf9 425 else if (def_gsi && occ->bb == def_gsi->bb)
472f3f23 426 {
427 /* Case 2: insert right after the definition. Note that this will
ac70caad 428 never happen if the definition statement can throw, because in
429 that case the sole successor of the statement's basic block will
430 dominate all the uses as well. */
472f3f23 431 gsi = *def_gsi;
432 gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT);
433 }
ac70caad 434 else
472f3f23 435 {
436 /* Case 3: insert in a basic block not containing defs/uses. */
437 gsi = gsi_after_labels (occ->bb);
438 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
439 }
440
441 /* Regardless of which case the reciprocal as inserted in,
442 we insert the square immediately after the reciprocal. */
443 if (should_insert_square_recip)
444 gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT);
ac70caad 445
30c4e60d 446 reciprocal_stats.rdivs_inserted++;
447
ac70caad 448 occ->recip_def_stmt = new_stmt;
abacb398 449 }
450
ac70caad 451 occ->recip_def = recip_def;
472f3f23 452 occ->square_recip_def = square_recip_def;
ac70caad 453 for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
472f3f23 454 insert_reciprocals (def_gsi, occ_child, def, recip_def,
455 square_recip_def, should_insert_square_recip,
456 threshold);
457}
458
459/* Replace occurrences of expr / (x * x) with expr * ((1 / x) * (1 / x)).
460 Take as argument the use for (x * x). */
461static inline void
462replace_reciprocal_squares (use_operand_p use_p)
463{
464 gimple *use_stmt = USE_STMT (use_p);
465 basic_block bb = gimple_bb (use_stmt);
466 struct occurrence *occ = (struct occurrence *) bb->aux;
467
468 if (optimize_bb_for_speed_p (bb) && occ->square_recip_def
469 && occ->recip_def)
470 {
471 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
472 gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
473 gimple_assign_set_rhs2 (use_stmt, occ->square_recip_def);
474 SET_USE (use_p, occ->square_recip_def);
475 fold_stmt_inplace (&gsi);
476 update_stmt (use_stmt);
477 }
ac70caad 478}
479
480
481/* Replace the division at USE_P with a multiplication by the reciprocal, if
482 possible. */
483
484static inline void
485replace_reciprocal (use_operand_p use_p)
486{
42acab1c 487 gimple *use_stmt = USE_STMT (use_p);
75a70cf9 488 basic_block bb = gimple_bb (use_stmt);
ac70caad 489 struct occurrence *occ = (struct occurrence *) bb->aux;
490
0bfd8d5c 491 if (optimize_bb_for_speed_p (bb)
492 && occ->recip_def && use_stmt != occ->recip_def_stmt)
ac70caad 493 {
50aacf4c 494 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
75a70cf9 495 gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
ac70caad 496 SET_USE (use_p, occ->recip_def);
50aacf4c 497 fold_stmt_inplace (&gsi);
ac70caad 498 update_stmt (use_stmt);
499 }
500}
501
502
503/* Free OCC and return one more "struct occurrence" to be freed. */
504
505static struct occurrence *
506free_bb (struct occurrence *occ)
507{
508 struct occurrence *child, *next;
509
510 /* First get the two pointers hanging off OCC. */
511 next = occ->next;
512 child = occ->children;
513 occ->bb->aux = NULL;
d8e7268c 514 occ_pool->remove (occ);
ac70caad 515
516 /* Now ensure that we don't recurse unless it is necessary. */
517 if (!child)
518 return next;
9e583fac 519 else
ac70caad 520 {
521 while (next)
522 next = free_bb (next);
523
524 return child;
525 }
526}
527
528
529/* Look for floating-point divisions among DEF's uses, and try to
530 replace them by multiplications with the reciprocal. Add
531 as many statements computing the reciprocal as needed.
532
533 DEF must be a GIMPLE register of a floating-point type. */
534
535static void
75a70cf9 536execute_cse_reciprocals_1 (gimple_stmt_iterator *def_gsi, tree def)
ac70caad 537{
472f3f23 538 use_operand_p use_p, square_use_p;
539 imm_use_iterator use_iter, square_use_iter;
540 tree square_def;
ac70caad 541 struct occurrence *occ;
472f3f23 542 int count = 0;
543 int threshold;
544 int square_recip_count = 0;
545 int sqrt_recip_count = 0;
abacb398 546
41e37ac9 547 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def)) && is_gimple_reg (def)
548 && TREE_CODE (def) == SSA_NAME);
472f3f23 549 threshold = targetm.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def)));
550
41e37ac9 551 /* If DEF is a square (x * x), count the number of divisions by x.
552 If there are more divisions by x than by (DEF * DEF), prefer to optimize
553 the reciprocal of x instead of DEF. This improves cases like:
554 def = x * x
555 t0 = a / def
556 t1 = b / def
557 t2 = c / x
558 Reciprocal optimization of x results in 1 division rather than 2 or 3. */
559 gimple *def_stmt = SSA_NAME_DEF_STMT (def);
560
561 if (is_gimple_assign (def_stmt)
562 && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
563 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
564 && gimple_assign_rhs1 (def_stmt) == gimple_assign_rhs2 (def_stmt))
472f3f23 565 {
41e37ac9 566 tree op0 = gimple_assign_rhs1 (def_stmt);
472f3f23 567
41e37ac9 568 FOR_EACH_IMM_USE_FAST (use_p, use_iter, op0)
472f3f23 569 {
41e37ac9 570 gimple *use_stmt = USE_STMT (use_p);
571 if (is_division_by (use_stmt, op0))
572 sqrt_recip_count++;
472f3f23 573 }
574 }
ac70caad 575
576 FOR_EACH_IMM_USE_FAST (use_p, use_iter, def)
abacb398 577 {
42acab1c 578 gimple *use_stmt = USE_STMT (use_p);
ac70caad 579 if (is_division_by (use_stmt, def))
abacb398 580 {
472f3f23 581 register_division_in (gimple_bb (use_stmt), 2);
ac70caad 582 count++;
abacb398 583 }
472f3f23 584
585 if (is_square_of (use_stmt, def))
586 {
587 square_def = gimple_assign_lhs (use_stmt);
588 FOR_EACH_IMM_USE_FAST (square_use_p, square_use_iter, square_def)
589 {
590 gimple *square_use_stmt = USE_STMT (square_use_p);
591 if (is_division_by (square_use_stmt, square_def))
592 {
41e37ac9 593 /* This is executed twice for each division by a square. */
472f3f23 594 register_division_in (gimple_bb (square_use_stmt), 1);
41e37ac9 595 square_recip_count++;
472f3f23 596 }
597 }
598 }
abacb398 599 }
48e1416a 600
41e37ac9 601 /* Square reciprocals were counted twice above. */
472f3f23 602 square_recip_count /= 2;
603
41e37ac9 604 /* If it is more profitable to optimize 1 / x, don't optimize 1 / (x * x). */
472f3f23 605 if (sqrt_recip_count > square_recip_count)
472f3f23 606 return;
607
ac70caad 608 /* Do the expensive part only if we can hope to optimize something. */
41e37ac9 609 if (count + square_recip_count >= threshold && count >= 1)
ac70caad 610 {
42acab1c 611 gimple *use_stmt;
ac70caad 612 for (occ = occ_head; occ; occ = occ->next)
613 {
614 compute_merit (occ);
472f3f23 615 insert_reciprocals (def_gsi, occ, def, NULL, NULL,
616 square_recip_count, threshold);
ac70caad 617 }
618
09aca5bc 619 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
ac70caad 620 {
ac70caad 621 if (is_division_by (use_stmt, def))
09aca5bc 622 {
623 FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
624 replace_reciprocal (use_p);
625 }
41e37ac9 626 else if (square_recip_count > 0 && is_square_of (use_stmt, def))
472f3f23 627 {
628 FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
629 {
630 /* Find all uses of the square that are divisions and
631 * replace them by multiplications with the inverse. */
632 imm_use_iterator square_iterator;
633 gimple *powmult_use_stmt = USE_STMT (use_p);
634 tree powmult_def_name = gimple_assign_lhs (powmult_use_stmt);
635
636 FOR_EACH_IMM_USE_STMT (powmult_use_stmt,
637 square_iterator, powmult_def_name)
638 FOR_EACH_IMM_USE_ON_STMT (square_use_p, square_iterator)
639 {
640 gimple *powmult_use_stmt = USE_STMT (square_use_p);
641 if (is_division_by (powmult_use_stmt, powmult_def_name))
642 replace_reciprocal_squares (square_use_p);
643 }
644 }
645 }
ac70caad 646 }
647 }
648
649 for (occ = occ_head; occ; )
650 occ = free_bb (occ);
651
652 occ_head = NULL;
abacb398 653}
654
4cfd27a5 655/* Return an internal function that implements the reciprocal of CALL,
656 or IFN_LAST if there is no such function that the target supports. */
657
658internal_fn
659internal_fn_reciprocal (gcall *call)
660{
661 internal_fn ifn;
662
663 switch (gimple_call_combined_fn (call))
664 {
665 CASE_CFN_SQRT:
8c32188e 666 CASE_CFN_SQRT_FN:
4cfd27a5 667 ifn = IFN_RSQRT;
668 break;
669
670 default:
671 return IFN_LAST;
672 }
673
674 tree_pair types = direct_internal_fn_types (ifn, call);
675 if (!direct_internal_fn_supported_p (ifn, types, OPTIMIZE_FOR_SPEED))
676 return IFN_LAST;
677
678 return ifn;
679}
680
ac70caad 681/* Go through all the floating-point SSA_NAMEs, and call
682 execute_cse_reciprocals_1 on each of them. */
65b0537f 683namespace {
684
685const pass_data pass_data_cse_reciprocals =
686{
687 GIMPLE_PASS, /* type */
688 "recip", /* name */
689 OPTGROUP_NONE, /* optinfo_flags */
8ed378fe 690 TV_TREE_RECIP, /* tv_id */
65b0537f 691 PROP_ssa, /* properties_required */
692 0, /* properties_provided */
693 0, /* properties_destroyed */
694 0, /* todo_flags_start */
8b88439e 695 TODO_update_ssa, /* todo_flags_finish */
65b0537f 696};
697
698class pass_cse_reciprocals : public gimple_opt_pass
699{
700public:
701 pass_cse_reciprocals (gcc::context *ctxt)
702 : gimple_opt_pass (pass_data_cse_reciprocals, ctxt)
703 {}
704
705 /* opt_pass methods: */
706 virtual bool gate (function *) { return optimize && flag_reciprocal_math; }
707 virtual unsigned int execute (function *);
708
709}; // class pass_cse_reciprocals
710
711unsigned int
712pass_cse_reciprocals::execute (function *fun)
abacb398 713{
714 basic_block bb;
51b60a11 715 tree arg;
685b24f5 716
1dc6c44d 717 occ_pool = new object_allocator<occurrence> ("dominators for recip");
685b24f5 718
30c4e60d 719 memset (&reciprocal_stats, 0, sizeof (reciprocal_stats));
c136ae61 720 calculate_dominance_info (CDI_DOMINATORS);
721 calculate_dominance_info (CDI_POST_DOMINATORS);
ac70caad 722
382ecba7 723 if (flag_checking)
724 FOR_EACH_BB_FN (bb, fun)
725 gcc_assert (!bb->aux);
ac70caad 726
65b0537f 727 for (arg = DECL_ARGUMENTS (fun->decl); arg; arg = DECL_CHAIN (arg))
c6dfe037 728 if (FLOAT_TYPE_P (TREE_TYPE (arg))
ac70caad 729 && is_gimple_reg (arg))
c6dfe037 730 {
65b0537f 731 tree name = ssa_default_def (fun, arg);
c6dfe037 732 if (name)
733 execute_cse_reciprocals_1 (NULL, name);
734 }
51b60a11 735
65b0537f 736 FOR_EACH_BB_FN (bb, fun)
abacb398 737 {
75a70cf9 738 tree def;
abacb398 739
1a91d914 740 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
741 gsi_next (&gsi))
abacb398 742 {
1a91d914 743 gphi *phi = gsi.phi ();
abacb398 744 def = PHI_RESULT (phi);
7c782c9b 745 if (! virtual_operand_p (def)
746 && FLOAT_TYPE_P (TREE_TYPE (def)))
ac70caad 747 execute_cse_reciprocals_1 (NULL, def);
abacb398 748 }
749
1a91d914 750 for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
751 gsi_next (&gsi))
abacb398 752 {
42acab1c 753 gimple *stmt = gsi_stmt (gsi);
a0315874 754
75a70cf9 755 if (gimple_has_lhs (stmt)
abacb398 756 && (def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF)) != NULL
757 && FLOAT_TYPE_P (TREE_TYPE (def))
51b60a11 758 && TREE_CODE (def) == SSA_NAME)
75a70cf9 759 execute_cse_reciprocals_1 (&gsi, def);
abacb398 760 }
e174638f 761
0bfd8d5c 762 if (optimize_bb_for_size_p (bb))
763 continue;
764
e174638f 765 /* Scan for a/func(b) and convert it to reciprocal a*rfunc(b). */
1a91d914 766 for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
767 gsi_next (&gsi))
e174638f 768 {
42acab1c 769 gimple *stmt = gsi_stmt (gsi);
e174638f 770
75a70cf9 771 if (is_gimple_assign (stmt)
772 && gimple_assign_rhs_code (stmt) == RDIV_EXPR)
e174638f 773 {
75a70cf9 774 tree arg1 = gimple_assign_rhs2 (stmt);
42acab1c 775 gimple *stmt1;
2cd360b6 776
777 if (TREE_CODE (arg1) != SSA_NAME)
778 continue;
779
780 stmt1 = SSA_NAME_DEF_STMT (arg1);
e174638f 781
75a70cf9 782 if (is_gimple_call (stmt1)
4cfd27a5 783 && gimple_call_lhs (stmt1))
e174638f 784 {
851c1b0c 785 bool fail;
774b1cdd 786 imm_use_iterator ui;
787 use_operand_p use_p;
4cfd27a5 788 tree fndecl = NULL_TREE;
e174638f 789
4cfd27a5 790 gcall *call = as_a <gcall *> (stmt1);
791 internal_fn ifn = internal_fn_reciprocal (call);
792 if (ifn == IFN_LAST)
793 {
794 fndecl = gimple_call_fndecl (call);
795 if (!fndecl
796 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
797 continue;
798 fndecl = targetm.builtin_reciprocal (fndecl);
799 if (!fndecl)
800 continue;
801 }
e174638f 802
774b1cdd 803 /* Check that all uses of the SSA name are divisions,
804 otherwise replacing the defining statement will do
805 the wrong thing. */
806 fail = false;
807 FOR_EACH_IMM_USE_FAST (use_p, ui, arg1)
808 {
42acab1c 809 gimple *stmt2 = USE_STMT (use_p);
774b1cdd 810 if (is_gimple_debug (stmt2))
811 continue;
812 if (!is_gimple_assign (stmt2)
813 || gimple_assign_rhs_code (stmt2) != RDIV_EXPR
814 || gimple_assign_rhs1 (stmt2) == arg1
815 || gimple_assign_rhs2 (stmt2) != arg1)
816 {
817 fail = true;
818 break;
819 }
820 }
821 if (fail)
822 continue;
823
4cfd27a5 824 gimple_replace_ssa_lhs (call, arg1);
825 if (gimple_call_internal_p (call) != (ifn != IFN_LAST))
851c1b0c 826 {
827 auto_vec<tree, 4> args;
828 for (unsigned int i = 0;
4cfd27a5 829 i < gimple_call_num_args (call); i++)
830 args.safe_push (gimple_call_arg (call, i));
831 gcall *stmt2;
832 if (ifn == IFN_LAST)
833 stmt2 = gimple_build_call_vec (fndecl, args);
834 else
835 stmt2 = gimple_build_call_internal_vec (ifn, args);
851c1b0c 836 gimple_call_set_lhs (stmt2, arg1);
4cfd27a5 837 if (gimple_vdef (call))
851c1b0c 838 {
4cfd27a5 839 gimple_set_vdef (stmt2, gimple_vdef (call));
851c1b0c 840 SSA_NAME_DEF_STMT (gimple_vdef (stmt2)) = stmt2;
841 }
989f02dc 842 gimple_call_set_nothrow (stmt2,
843 gimple_call_nothrow_p (call));
4cfd27a5 844 gimple_set_vuse (stmt2, gimple_vuse (call));
845 gimple_stmt_iterator gsi2 = gsi_for_stmt (call);
851c1b0c 846 gsi_replace (&gsi2, stmt2, true);
847 }
848 else
849 {
4cfd27a5 850 if (ifn == IFN_LAST)
851 gimple_call_set_fndecl (call, fndecl);
852 else
853 gimple_call_set_internal_fn (call, ifn);
854 update_stmt (call);
851c1b0c 855 }
30c4e60d 856 reciprocal_stats.rfuncs_inserted++;
e174638f 857
774b1cdd 858 FOR_EACH_IMM_USE_STMT (stmt, ui, arg1)
859 {
50aacf4c 860 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
774b1cdd 861 gimple_assign_set_rhs_code (stmt, MULT_EXPR);
50aacf4c 862 fold_stmt_inplace (&gsi);
774b1cdd 863 update_stmt (stmt);
864 }
e174638f 865 }
866 }
867 }
abacb398 868 }
685b24f5 869
65b0537f 870 statistics_counter_event (fun, "reciprocal divs inserted",
30c4e60d 871 reciprocal_stats.rdivs_inserted);
65b0537f 872 statistics_counter_event (fun, "reciprocal functions inserted",
30c4e60d 873 reciprocal_stats.rfuncs_inserted);
874
c136ae61 875 free_dominance_info (CDI_DOMINATORS);
876 free_dominance_info (CDI_POST_DOMINATORS);
d8e7268c 877 delete occ_pool;
2a1990e9 878 return 0;
abacb398 879}
880
cbe8bda8 881} // anon namespace
882
883gimple_opt_pass *
884make_pass_cse_reciprocals (gcc::context *ctxt)
885{
886 return new pass_cse_reciprocals (ctxt);
887}
888
0d424440 889/* Records an occurrence at statement USE_STMT in the vector of trees
a0315874 890 STMTS if it is dominated by *TOP_BB or dominates it or this basic block
0d424440 891 is not yet initialized. Returns true if the occurrence was pushed on
a0315874 892 the vector. Adjusts *TOP_BB to be the basic block dominating all
893 statements in the vector. */
894
895static bool
42acab1c 896maybe_record_sincos (vec<gimple *> *stmts,
897 basic_block *top_bb, gimple *use_stmt)
a0315874 898{
75a70cf9 899 basic_block use_bb = gimple_bb (use_stmt);
a0315874 900 if (*top_bb
901 && (*top_bb == use_bb
902 || dominated_by_p (CDI_DOMINATORS, use_bb, *top_bb)))
f1f41a6c 903 stmts->safe_push (use_stmt);
a0315874 904 else if (!*top_bb
905 || dominated_by_p (CDI_DOMINATORS, *top_bb, use_bb))
906 {
f1f41a6c 907 stmts->safe_push (use_stmt);
a0315874 908 *top_bb = use_bb;
909 }
910 else
911 return false;
912
913 return true;
914}
915
916/* Look for sin, cos and cexpi calls with the same argument NAME and
917 create a single call to cexpi CSEing the result in this case.
918 We first walk over all immediate uses of the argument collecting
919 statements that we can CSE in a vector and in a second pass replace
920 the statement rhs with a REALPART or IMAGPART expression on the
921 result of the cexpi call we insert before the use statement that
922 dominates all other candidates. */
923
4c80086d 924static bool
a0315874 925execute_cse_sincos_1 (tree name)
926{
75a70cf9 927 gimple_stmt_iterator gsi;
a0315874 928 imm_use_iterator use_iter;
75a70cf9 929 tree fndecl, res, type;
42acab1c 930 gimple *def_stmt, *use_stmt, *stmt;
a0315874 931 int seen_cos = 0, seen_sin = 0, seen_cexpi = 0;
42acab1c 932 auto_vec<gimple *> stmts;
a0315874 933 basic_block top_bb = NULL;
934 int i;
4c80086d 935 bool cfg_changed = false;
a0315874 936
937 type = TREE_TYPE (name);
938 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, name)
939 {
75a70cf9 940 if (gimple_code (use_stmt) != GIMPLE_CALL
fa0793ad 941 || !gimple_call_lhs (use_stmt))
a0315874 942 continue;
943
fa0793ad 944 switch (gimple_call_combined_fn (use_stmt))
a0315874 945 {
fa0793ad 946 CASE_CFN_COS:
a0315874 947 seen_cos |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
948 break;
949
fa0793ad 950 CASE_CFN_SIN:
a0315874 951 seen_sin |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
952 break;
953
fa0793ad 954 CASE_CFN_CEXPI:
a0315874 955 seen_cexpi |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
956 break;
957
958 default:;
959 }
960 }
961
962 if (seen_cos + seen_sin + seen_cexpi <= 1)
6702d09a 963 return false;
a0315874 964
965 /* Simply insert cexpi at the beginning of top_bb but not earlier than
966 the name def statement. */
967 fndecl = mathfn_built_in (type, BUILT_IN_CEXPI);
968 if (!fndecl)
4c80086d 969 return false;
75a70cf9 970 stmt = gimple_build_call (fndecl, 1, name);
03d37e4e 971 res = make_temp_ssa_name (TREE_TYPE (TREE_TYPE (fndecl)), stmt, "sincostmp");
75a70cf9 972 gimple_call_set_lhs (stmt, res);
973
a0315874 974 def_stmt = SSA_NAME_DEF_STMT (name);
8090c12d 975 if (!SSA_NAME_IS_DEFAULT_DEF (name)
75a70cf9 976 && gimple_code (def_stmt) != GIMPLE_PHI
977 && gimple_bb (def_stmt) == top_bb)
a0315874 978 {
75a70cf9 979 gsi = gsi_for_stmt (def_stmt);
980 gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
a0315874 981 }
982 else
983 {
75a70cf9 984 gsi = gsi_after_labels (top_bb);
985 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
a0315874 986 }
30c4e60d 987 sincos_stats.inserted++;
a0315874 988
989 /* And adjust the recorded old call sites. */
f1f41a6c 990 for (i = 0; stmts.iterate (i, &use_stmt); ++i)
a0315874 991 {
75a70cf9 992 tree rhs = NULL;
75a70cf9 993
fa0793ad 994 switch (gimple_call_combined_fn (use_stmt))
a0315874 995 {
fa0793ad 996 CASE_CFN_COS:
75a70cf9 997 rhs = fold_build1 (REALPART_EXPR, type, res);
a0315874 998 break;
999
fa0793ad 1000 CASE_CFN_SIN:
75a70cf9 1001 rhs = fold_build1 (IMAGPART_EXPR, type, res);
a0315874 1002 break;
1003
fa0793ad 1004 CASE_CFN_CEXPI:
75a70cf9 1005 rhs = res;
a0315874 1006 break;
1007
1008 default:;
1009 gcc_unreachable ();
1010 }
1011
75a70cf9 1012 /* Replace call with a copy. */
1013 stmt = gimple_build_assign (gimple_call_lhs (use_stmt), rhs);
1014
1015 gsi = gsi_for_stmt (use_stmt);
4c80086d 1016 gsi_replace (&gsi, stmt, true);
1017 if (gimple_purge_dead_eh_edges (gimple_bb (stmt)))
1018 cfg_changed = true;
a0315874 1019 }
1020
4c80086d 1021 return cfg_changed;
a0315874 1022}
1023
e9a6c4bc 1024/* To evaluate powi(x,n), the floating point value x raised to the
1025 constant integer exponent n, we use a hybrid algorithm that
1026 combines the "window method" with look-up tables. For an
1027 introduction to exponentiation algorithms and "addition chains",
1028 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
1029 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
1030 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
1031 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
1032
1033/* Provide a default value for POWI_MAX_MULTS, the maximum number of
1034 multiplications to inline before calling the system library's pow
1035 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
1036 so this default never requires calling pow, powf or powl. */
1037
1038#ifndef POWI_MAX_MULTS
1039#define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
1040#endif
1041
1042/* The size of the "optimal power tree" lookup table. All
1043 exponents less than this value are simply looked up in the
1044 powi_table below. This threshold is also used to size the
1045 cache of pseudo registers that hold intermediate results. */
1046#define POWI_TABLE_SIZE 256
1047
1048/* The size, in bits of the window, used in the "window method"
1049 exponentiation algorithm. This is equivalent to a radix of
1050 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
1051#define POWI_WINDOW_SIZE 3
1052
1053/* The following table is an efficient representation of an
1054 "optimal power tree". For each value, i, the corresponding
1055 value, j, in the table states than an optimal evaluation
1056 sequence for calculating pow(x,i) can be found by evaluating
1057 pow(x,j)*pow(x,i-j). An optimal power tree for the first
1058 100 integers is given in Knuth's "Seminumerical algorithms". */
1059
1060static const unsigned char powi_table[POWI_TABLE_SIZE] =
1061 {
1062 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1063 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1064 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1065 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1066 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1067 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1068 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
1069 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
1070 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
1071 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
1072 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
1073 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
1074 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
1075 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
1076 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
1077 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
1078 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
1079 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
1080 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
1081 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
1082 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
1083 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
1084 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
1085 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
1086 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
1087 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
1088 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
1089 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
1090 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
1091 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
1092 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
1093 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
1094 };
1095
1096
1097/* Return the number of multiplications required to calculate
1098 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
1099 subroutine of powi_cost. CACHE is an array indicating
1100 which exponents have already been calculated. */
1101
1102static int
1103powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
1104{
1105 /* If we've already calculated this exponent, then this evaluation
1106 doesn't require any additional multiplications. */
1107 if (cache[n])
1108 return 0;
1109
1110 cache[n] = true;
1111 return powi_lookup_cost (n - powi_table[n], cache)
1112 + powi_lookup_cost (powi_table[n], cache) + 1;
1113}
1114
1115/* Return the number of multiplications required to calculate
1116 powi(x,n) for an arbitrary x, given the exponent N. This
1117 function needs to be kept in sync with powi_as_mults below. */
1118
1119static int
1120powi_cost (HOST_WIDE_INT n)
1121{
1122 bool cache[POWI_TABLE_SIZE];
1123 unsigned HOST_WIDE_INT digit;
1124 unsigned HOST_WIDE_INT val;
1125 int result;
1126
1127 if (n == 0)
1128 return 0;
1129
1130 /* Ignore the reciprocal when calculating the cost. */
1131 val = (n < 0) ? -n : n;
1132
1133 /* Initialize the exponent cache. */
1134 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
1135 cache[1] = true;
1136
1137 result = 0;
1138
1139 while (val >= POWI_TABLE_SIZE)
1140 {
1141 if (val & 1)
1142 {
1143 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
1144 result += powi_lookup_cost (digit, cache)
1145 + POWI_WINDOW_SIZE + 1;
1146 val >>= POWI_WINDOW_SIZE;
1147 }
1148 else
1149 {
1150 val >>= 1;
1151 result++;
1152 }
1153 }
1154
1155 return result + powi_lookup_cost (val, cache);
1156}
1157
1158/* Recursive subroutine of powi_as_mults. This function takes the
1159 array, CACHE, of already calculated exponents and an exponent N and
1160 returns a tree that corresponds to CACHE[1]**N, with type TYPE. */
1161
1162static tree
1163powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type,
03d37e4e 1164 HOST_WIDE_INT n, tree *cache)
e9a6c4bc 1165{
1166 tree op0, op1, ssa_target;
1167 unsigned HOST_WIDE_INT digit;
1a91d914 1168 gassign *mult_stmt;
e9a6c4bc 1169
1170 if (n < POWI_TABLE_SIZE && cache[n])
1171 return cache[n];
1172
03d37e4e 1173 ssa_target = make_temp_ssa_name (type, NULL, "powmult");
e9a6c4bc 1174
1175 if (n < POWI_TABLE_SIZE)
1176 {
1177 cache[n] = ssa_target;
03d37e4e 1178 op0 = powi_as_mults_1 (gsi, loc, type, n - powi_table[n], cache);
1179 op1 = powi_as_mults_1 (gsi, loc, type, powi_table[n], cache);
e9a6c4bc 1180 }
1181 else if (n & 1)
1182 {
1183 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
03d37e4e 1184 op0 = powi_as_mults_1 (gsi, loc, type, n - digit, cache);
1185 op1 = powi_as_mults_1 (gsi, loc, type, digit, cache);
e9a6c4bc 1186 }
1187 else
1188 {
03d37e4e 1189 op0 = powi_as_mults_1 (gsi, loc, type, n >> 1, cache);
e9a6c4bc 1190 op1 = op0;
1191 }
1192
e9cf809e 1193 mult_stmt = gimple_build_assign (ssa_target, MULT_EXPR, op0, op1);
ae43b05e 1194 gimple_set_location (mult_stmt, loc);
e9a6c4bc 1195 gsi_insert_before (gsi, mult_stmt, GSI_SAME_STMT);
1196
1197 return ssa_target;
1198}
1199
1200/* Convert ARG0**N to a tree of multiplications of ARG0 with itself.
1201 This function needs to be kept in sync with powi_cost above. */
1202
1203static tree
1204powi_as_mults (gimple_stmt_iterator *gsi, location_t loc,
1205 tree arg0, HOST_WIDE_INT n)
1206{
03d37e4e 1207 tree cache[POWI_TABLE_SIZE], result, type = TREE_TYPE (arg0);
1a91d914 1208 gassign *div_stmt;
03d37e4e 1209 tree target;
e9a6c4bc 1210
1211 if (n == 0)
1212 return build_real (type, dconst1);
1213
1214 memset (cache, 0, sizeof (cache));
1215 cache[1] = arg0;
1216
03d37e4e 1217 result = powi_as_mults_1 (gsi, loc, type, (n < 0) ? -n : n, cache);
e9a6c4bc 1218 if (n >= 0)
1219 return result;
1220
1221 /* If the original exponent was negative, reciprocate the result. */
03d37e4e 1222 target = make_temp_ssa_name (type, NULL, "powmult");
e9cf809e 1223 div_stmt = gimple_build_assign (target, RDIV_EXPR,
1224 build_real (type, dconst1), result);
ae43b05e 1225 gimple_set_location (div_stmt, loc);
e9a6c4bc 1226 gsi_insert_before (gsi, div_stmt, GSI_SAME_STMT);
1227
1228 return target;
1229}
1230
1231/* ARG0 and N are the two arguments to a powi builtin in GSI with
1232 location info LOC. If the arguments are appropriate, create an
1233 equivalent sequence of statements prior to GSI using an optimal
1234 number of multiplications, and return an expession holding the
1235 result. */
1236
1237static tree
1238gimple_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc,
1239 tree arg0, HOST_WIDE_INT n)
1240{
1241 /* Avoid largest negative number. */
1242 if (n != -n
1243 && ((n >= -1 && n <= 2)
1244 || (optimize_function_for_speed_p (cfun)
1245 && powi_cost (n) <= POWI_MAX_MULTS)))
1246 return powi_as_mults (gsi, loc, arg0, n);
1247
1248 return NULL_TREE;
1249}
1250
ae43b05e 1251/* Build a gimple call statement that calls FN with argument ARG.
03d37e4e 1252 Set the lhs of the call statement to a fresh SSA name. Insert the
ae43b05e 1253 statement prior to GSI's current position, and return the fresh
1254 SSA name. */
1255
1256static tree
ca12eb68 1257build_and_insert_call (gimple_stmt_iterator *gsi, location_t loc,
03d37e4e 1258 tree fn, tree arg)
ae43b05e 1259{
1a91d914 1260 gcall *call_stmt;
ae43b05e 1261 tree ssa_target;
1262
ae43b05e 1263 call_stmt = gimple_build_call (fn, 1, arg);
03d37e4e 1264 ssa_target = make_temp_ssa_name (TREE_TYPE (arg), NULL, "powroot");
ae43b05e 1265 gimple_set_lhs (call_stmt, ssa_target);
1266 gimple_set_location (call_stmt, loc);
1267 gsi_insert_before (gsi, call_stmt, GSI_SAME_STMT);
1268
1269 return ssa_target;
1270}
1271
ca12eb68 1272/* Build a gimple binary operation with the given CODE and arguments
1273 ARG0, ARG1, assigning the result to a new SSA name for variable
1274 TARGET. Insert the statement prior to GSI's current position, and
1275 return the fresh SSA name.*/
1276
1277static tree
1278build_and_insert_binop (gimple_stmt_iterator *gsi, location_t loc,
03d37e4e 1279 const char *name, enum tree_code code,
1280 tree arg0, tree arg1)
ca12eb68 1281{
03d37e4e 1282 tree result = make_temp_ssa_name (TREE_TYPE (arg0), NULL, name);
e9cf809e 1283 gassign *stmt = gimple_build_assign (result, code, arg0, arg1);
ca12eb68 1284 gimple_set_location (stmt, loc);
1285 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1286 return result;
1287}
1288
a5c384c1 1289/* Build a gimple reference operation with the given CODE and argument
03d37e4e 1290 ARG, assigning the result to a new SSA name of TYPE with NAME.
a5c384c1 1291 Insert the statement prior to GSI's current position, and return
1292 the fresh SSA name. */
1293
1294static inline tree
1295build_and_insert_ref (gimple_stmt_iterator *gsi, location_t loc, tree type,
03d37e4e 1296 const char *name, enum tree_code code, tree arg0)
a5c384c1 1297{
03d37e4e 1298 tree result = make_temp_ssa_name (type, NULL, name);
42acab1c 1299 gimple *stmt = gimple_build_assign (result, build1 (code, type, arg0));
a5c384c1 1300 gimple_set_location (stmt, loc);
1301 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1302 return result;
1303}
1304
03d37e4e 1305/* Build a gimple assignment to cast VAL to TYPE. Insert the statement
aff5fb4d 1306 prior to GSI's current position, and return the fresh SSA name. */
1307
1308static tree
1309build_and_insert_cast (gimple_stmt_iterator *gsi, location_t loc,
03d37e4e 1310 tree type, tree val)
aff5fb4d 1311{
f9e245b2 1312 tree result = make_ssa_name (type);
e9cf809e 1313 gassign *stmt = gimple_build_assign (result, NOP_EXPR, val);
03d37e4e 1314 gimple_set_location (stmt, loc);
1315 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1316 return result;
aff5fb4d 1317}
1318
c3206272 1319struct pow_synth_sqrt_info
1320{
1321 bool *factors;
1322 unsigned int deepest;
1323 unsigned int num_mults;
1324};
1325
1326/* Return true iff the real value C can be represented as a
1327 sum of powers of 0.5 up to N. That is:
1328 C == SUM<i from 1..N> (a[i]*(0.5**i)) where a[i] is either 0 or 1.
1329 Record in INFO the various parameters of the synthesis algorithm such
1330 as the factors a[i], the maximum 0.5 power and the number of
1331 multiplications that will be required. */
1332
1333bool
1334representable_as_half_series_p (REAL_VALUE_TYPE c, unsigned n,
1335 struct pow_synth_sqrt_info *info)
1336{
1337 REAL_VALUE_TYPE factor = dconsthalf;
1338 REAL_VALUE_TYPE remainder = c;
1339
1340 info->deepest = 0;
1341 info->num_mults = 0;
1342 memset (info->factors, 0, n * sizeof (bool));
1343
1344 for (unsigned i = 0; i < n; i++)
1345 {
1346 REAL_VALUE_TYPE res;
1347
1348 /* If something inexact happened bail out now. */
f2ad9e38 1349 if (real_arithmetic (&res, MINUS_EXPR, &remainder, &factor))
c3206272 1350 return false;
1351
1352 /* We have hit zero. The number is representable as a sum
1353 of powers of 0.5. */
20cb53c9 1354 if (real_equal (&res, &dconst0))
c3206272 1355 {
1356 info->factors[i] = true;
1357 info->deepest = i + 1;
1358 return true;
1359 }
1360 else if (!REAL_VALUE_NEGATIVE (res))
1361 {
1362 remainder = res;
1363 info->factors[i] = true;
1364 info->num_mults++;
1365 }
1366 else
1367 info->factors[i] = false;
1368
f2ad9e38 1369 real_arithmetic (&factor, MULT_EXPR, &factor, &dconsthalf);
c3206272 1370 }
1371 return false;
1372}
1373
1374/* Return the tree corresponding to FN being applied
1375 to ARG N times at GSI and LOC.
1376 Look up previous results from CACHE if need be.
1377 cache[0] should contain just plain ARG i.e. FN applied to ARG 0 times. */
1378
1379static tree
1380get_fn_chain (tree arg, unsigned int n, gimple_stmt_iterator *gsi,
1381 tree fn, location_t loc, tree *cache)
1382{
1383 tree res = cache[n];
1384 if (!res)
1385 {
1386 tree prev = get_fn_chain (arg, n - 1, gsi, fn, loc, cache);
1387 res = build_and_insert_call (gsi, loc, fn, prev);
1388 cache[n] = res;
1389 }
1390
1391 return res;
1392}
1393
1394/* Print to STREAM the repeated application of function FNAME to ARG
1395 N times. So, for FNAME = "foo", ARG = "x", N = 2 it would print:
1396 "foo (foo (x))". */
1397
1398static void
1399print_nested_fn (FILE* stream, const char *fname, const char* arg,
1400 unsigned int n)
1401{
1402 if (n == 0)
1403 fprintf (stream, "%s", arg);
1404 else
1405 {
1406 fprintf (stream, "%s (", fname);
1407 print_nested_fn (stream, fname, arg, n - 1);
1408 fprintf (stream, ")");
1409 }
1410}
1411
1412/* Print to STREAM the fractional sequence of sqrt chains
1413 applied to ARG, described by INFO. Used for the dump file. */
1414
1415static void
1416dump_fractional_sqrt_sequence (FILE *stream, const char *arg,
1417 struct pow_synth_sqrt_info *info)
1418{
1419 for (unsigned int i = 0; i < info->deepest; i++)
1420 {
1421 bool is_set = info->factors[i];
1422 if (is_set)
1423 {
1424 print_nested_fn (stream, "sqrt", arg, i + 1);
1425 if (i != info->deepest - 1)
1426 fprintf (stream, " * ");
1427 }
1428 }
1429}
1430
1431/* Print to STREAM a representation of raising ARG to an integer
1432 power N. Used for the dump file. */
1433
1434static void
1435dump_integer_part (FILE *stream, const char* arg, HOST_WIDE_INT n)
1436{
1437 if (n > 1)
1438 fprintf (stream, "powi (%s, " HOST_WIDE_INT_PRINT_DEC ")", arg, n);
1439 else if (n == 1)
1440 fprintf (stream, "%s", arg);
1441}
1442
1443/* Attempt to synthesize a POW[F] (ARG0, ARG1) call using chains of
1444 square roots. Place at GSI and LOC. Limit the maximum depth
1445 of the sqrt chains to MAX_DEPTH. Return the tree holding the
1446 result of the expanded sequence or NULL_TREE if the expansion failed.
1447
1448 This routine assumes that ARG1 is a real number with a fractional part
1449 (the integer exponent case will have been handled earlier in
1450 gimple_expand_builtin_pow).
1451
1452 For ARG1 > 0.0:
1453 * For ARG1 composed of a whole part WHOLE_PART and a fractional part
1454 FRAC_PART i.e. WHOLE_PART == floor (ARG1) and
1455 FRAC_PART == ARG1 - WHOLE_PART:
1456 Produce POWI (ARG0, WHOLE_PART) * POW (ARG0, FRAC_PART) where
1457 POW (ARG0, FRAC_PART) is expanded as a product of square root chains
1458 if it can be expressed as such, that is if FRAC_PART satisfies:
1459 FRAC_PART == <SUM from i = 1 until MAX_DEPTH> (a[i] * (0.5**i))
1460 where integer a[i] is either 0 or 1.
1461
1462 Example:
1463 POW (x, 3.625) == POWI (x, 3) * POW (x, 0.625)
1464 --> POWI (x, 3) * SQRT (x) * SQRT (SQRT (SQRT (x)))
1465
1466 For ARG1 < 0.0 there are two approaches:
1467 * (A) Expand to 1.0 / POW (ARG0, -ARG1) where POW (ARG0, -ARG1)
1468 is calculated as above.
1469
1470 Example:
1471 POW (x, -5.625) == 1.0 / POW (x, 5.625)
1472 --> 1.0 / (POWI (x, 5) * SQRT (x) * SQRT (SQRT (SQRT (x))))
1473
1474 * (B) : WHOLE_PART := - ceil (abs (ARG1))
1475 FRAC_PART := ARG1 - WHOLE_PART
1476 and expand to POW (x, FRAC_PART) / POWI (x, WHOLE_PART).
1477 Example:
1478 POW (x, -5.875) == POW (x, 0.125) / POWI (X, 6)
1479 --> SQRT (SQRT (SQRT (x))) / (POWI (x, 6))
1480
1481 For ARG1 < 0.0 we choose between (A) and (B) depending on
1482 how many multiplications we'd have to do.
1483 So, for the example in (B): POW (x, -5.875), if we were to
1484 follow algorithm (A) we would produce:
1485 1.0 / POWI (X, 5) * SQRT (X) * SQRT (SQRT (X)) * SQRT (SQRT (SQRT (X)))
1486 which contains more multiplications than approach (B).
1487
1488 Hopefully, this approach will eliminate potentially expensive POW library
1489 calls when unsafe floating point math is enabled and allow the compiler to
1490 further optimise the multiplies, square roots and divides produced by this
1491 function. */
1492
1493static tree
1494expand_pow_as_sqrts (gimple_stmt_iterator *gsi, location_t loc,
1495 tree arg0, tree arg1, HOST_WIDE_INT max_depth)
1496{
1497 tree type = TREE_TYPE (arg0);
1498 machine_mode mode = TYPE_MODE (type);
1499 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
1500 bool one_over = true;
1501
1502 if (!sqrtfn)
1503 return NULL_TREE;
1504
1505 if (TREE_CODE (arg1) != REAL_CST)
1506 return NULL_TREE;
1507
1508 REAL_VALUE_TYPE exp_init = TREE_REAL_CST (arg1);
1509
1510 gcc_assert (max_depth > 0);
1511 tree *cache = XALLOCAVEC (tree, max_depth + 1);
1512
1513 struct pow_synth_sqrt_info synth_info;
1514 synth_info.factors = XALLOCAVEC (bool, max_depth + 1);
1515 synth_info.deepest = 0;
1516 synth_info.num_mults = 0;
1517
1518 bool neg_exp = REAL_VALUE_NEGATIVE (exp_init);
1519 REAL_VALUE_TYPE exp = real_value_abs (&exp_init);
1520
1521 /* The whole and fractional parts of exp. */
1522 REAL_VALUE_TYPE whole_part;
1523 REAL_VALUE_TYPE frac_part;
1524
1525 real_floor (&whole_part, mode, &exp);
f2ad9e38 1526 real_arithmetic (&frac_part, MINUS_EXPR, &exp, &whole_part);
c3206272 1527
1528
1529 REAL_VALUE_TYPE ceil_whole = dconst0;
1530 REAL_VALUE_TYPE ceil_fract = dconst0;
1531
1532 if (neg_exp)
1533 {
1534 real_ceil (&ceil_whole, mode, &exp);
f2ad9e38 1535 real_arithmetic (&ceil_fract, MINUS_EXPR, &ceil_whole, &exp);
c3206272 1536 }
1537
1538 if (!representable_as_half_series_p (frac_part, max_depth, &synth_info))
1539 return NULL_TREE;
1540
1541 /* Check whether it's more profitable to not use 1.0 / ... */
1542 if (neg_exp)
1543 {
1544 struct pow_synth_sqrt_info alt_synth_info;
1545 alt_synth_info.factors = XALLOCAVEC (bool, max_depth + 1);
1546 alt_synth_info.deepest = 0;
1547 alt_synth_info.num_mults = 0;
1548
1549 if (representable_as_half_series_p (ceil_fract, max_depth,
1550 &alt_synth_info)
1551 && alt_synth_info.deepest <= synth_info.deepest
1552 && alt_synth_info.num_mults < synth_info.num_mults)
1553 {
1554 whole_part = ceil_whole;
1555 frac_part = ceil_fract;
1556 synth_info.deepest = alt_synth_info.deepest;
1557 synth_info.num_mults = alt_synth_info.num_mults;
1558 memcpy (synth_info.factors, alt_synth_info.factors,
1559 (max_depth + 1) * sizeof (bool));
1560 one_over = false;
1561 }
1562 }
1563
1564 HOST_WIDE_INT n = real_to_integer (&whole_part);
1565 REAL_VALUE_TYPE cint;
1566 real_from_integer (&cint, VOIDmode, n, SIGNED);
1567
1568 if (!real_identical (&whole_part, &cint))
1569 return NULL_TREE;
1570
1571 if (powi_cost (n) + synth_info.num_mults > POWI_MAX_MULTS)
1572 return NULL_TREE;
1573
1574 memset (cache, 0, (max_depth + 1) * sizeof (tree));
1575
1576 tree integer_res = n == 0 ? build_real (type, dconst1) : arg0;
1577
1578 /* Calculate the integer part of the exponent. */
1579 if (n > 1)
1580 {
1581 integer_res = gimple_expand_builtin_powi (gsi, loc, arg0, n);
1582 if (!integer_res)
1583 return NULL_TREE;
1584 }
1585
1586 if (dump_file)
1587 {
1588 char string[64];
1589
1590 real_to_decimal (string, &exp_init, sizeof (string), 0, 1);
1591 fprintf (dump_file, "synthesizing pow (x, %s) as:\n", string);
1592
1593 if (neg_exp)
1594 {
1595 if (one_over)
1596 {
1597 fprintf (dump_file, "1.0 / (");
1598 dump_integer_part (dump_file, "x", n);
1599 if (n > 0)
1600 fprintf (dump_file, " * ");
1601 dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1602 fprintf (dump_file, ")");
1603 }
1604 else
1605 {
1606 dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1607 fprintf (dump_file, " / (");
1608 dump_integer_part (dump_file, "x", n);
1609 fprintf (dump_file, ")");
1610 }
1611 }
1612 else
1613 {
1614 dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1615 if (n > 0)
1616 fprintf (dump_file, " * ");
1617 dump_integer_part (dump_file, "x", n);
1618 }
1619
1620 fprintf (dump_file, "\ndeepest sqrt chain: %d\n", synth_info.deepest);
1621 }
1622
1623
1624 tree fract_res = NULL_TREE;
1625 cache[0] = arg0;
1626
1627 /* Calculate the fractional part of the exponent. */
1628 for (unsigned i = 0; i < synth_info.deepest; i++)
1629 {
1630 if (synth_info.factors[i])
1631 {
1632 tree sqrt_chain = get_fn_chain (arg0, i + 1, gsi, sqrtfn, loc, cache);
1633
1634 if (!fract_res)
1635 fract_res = sqrt_chain;
1636
1637 else
1638 fract_res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1639 fract_res, sqrt_chain);
1640 }
1641 }
1642
1643 tree res = NULL_TREE;
1644
1645 if (neg_exp)
1646 {
1647 if (one_over)
1648 {
1649 if (n > 0)
1650 res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1651 fract_res, integer_res);
1652 else
1653 res = fract_res;
1654
1655 res = build_and_insert_binop (gsi, loc, "powrootrecip", RDIV_EXPR,
1656 build_real (type, dconst1), res);
1657 }
1658 else
1659 {
1660 res = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR,
1661 fract_res, integer_res);
1662 }
1663 }
1664 else
1665 res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1666 fract_res, integer_res);
1667 return res;
1668}
1669
e78306af 1670/* ARG0 and ARG1 are the two arguments to a pow builtin call in GSI
1671 with location info LOC. If possible, create an equivalent and
1672 less expensive sequence of statements prior to GSI, and return an
1673 expession holding the result. */
1674
1675static tree
1676gimple_expand_builtin_pow (gimple_stmt_iterator *gsi, location_t loc,
1677 tree arg0, tree arg1)
1678{
c3206272 1679 REAL_VALUE_TYPE c, cint, dconst1_3, dconst1_4, dconst1_6;
ca12eb68 1680 REAL_VALUE_TYPE c2, dconst3;
e78306af 1681 HOST_WIDE_INT n;
c3206272 1682 tree type, sqrtfn, cbrtfn, sqrt_arg0, result, cbrt_x, powi_cbrt_x;
3754d046 1683 machine_mode mode;
c3206272 1684 bool speed_p = optimize_bb_for_speed_p (gsi_bb (*gsi));
0190fe95 1685 bool hw_sqrt_exists, c_is_int, c2_is_int;
e78306af 1686
c3206272 1687 dconst1_4 = dconst1;
1688 SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
1689
e78306af 1690 /* If the exponent isn't a constant, there's nothing of interest
1691 to be done. */
1692 if (TREE_CODE (arg1) != REAL_CST)
1693 return NULL_TREE;
1694
9f27d92a 1695 /* Don't perform the operation if flag_signaling_nans is on
1696 and the operand is a signaling NaN. */
1697 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2a659064 1698 && ((TREE_CODE (arg0) == REAL_CST
1699 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0)))
9f27d92a 1700 || REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1))))
1701 return NULL_TREE;
1702
ae43b05e 1703 /* If the exponent is equivalent to an integer, expand to an optimal
1704 multiplication sequence when profitable. */
e78306af 1705 c = TREE_REAL_CST (arg1);
1706 n = real_to_integer (&c);
e913b5cd 1707 real_from_integer (&cint, VOIDmode, n, SIGNED);
0190fe95 1708 c_is_int = real_identical (&c, &cint);
e78306af 1709
0190fe95 1710 if (c_is_int
e78306af 1711 && ((n >= -1 && n <= 2)
1712 || (flag_unsafe_math_optimizations
c3206272 1713 && speed_p
e78306af 1714 && powi_cost (n) <= POWI_MAX_MULTS)))
1715 return gimple_expand_builtin_powi (gsi, loc, arg0, n);
1716
ae43b05e 1717 /* Attempt various optimizations using sqrt and cbrt. */
1718 type = TREE_TYPE (arg0);
1719 mode = TYPE_MODE (type);
1720 sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
1721
1722 /* Optimize pow(x,0.5) = sqrt(x). This replacement is always safe
1723 unless signed zeros must be maintained. pow(-0,0.5) = +0, while
1724 sqrt(-0) = -0. */
1725 if (sqrtfn
20cb53c9 1726 && real_equal (&c, &dconsthalf)
ae43b05e 1727 && !HONOR_SIGNED_ZEROS (mode))
03d37e4e 1728 return build_and_insert_call (gsi, loc, sqrtfn, arg0);
ae43b05e 1729
a5c384c1 1730 hw_sqrt_exists = optab_handler (sqrt_optab, mode) != CODE_FOR_nothing;
ae43b05e 1731
ae43b05e 1732 /* Optimize pow(x,1./3.) = cbrt(x). This requires unsafe math
1733 optimizations since 1./3. is not exactly representable. If x
1734 is negative and finite, the correct value of pow(x,1./3.) is
1735 a NaN with the "invalid" exception raised, because the value
1736 of 1./3. actually has an even denominator. The correct value
1737 of cbrt(x) is a negative real value. */
1738 cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
1739 dconst1_3 = real_value_truncate (mode, dconst_third ());
1740
1741 if (flag_unsafe_math_optimizations
1742 && cbrtfn
ee230333 1743 && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
20cb53c9 1744 && real_equal (&c, &dconst1_3))
03d37e4e 1745 return build_and_insert_call (gsi, loc, cbrtfn, arg0);
ae43b05e 1746
1747 /* Optimize pow(x,1./6.) = cbrt(sqrt(x)). Don't do this optimization
1748 if we don't have a hardware sqrt insn. */
1749 dconst1_6 = dconst1_3;
1750 SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
1751
1752 if (flag_unsafe_math_optimizations
1753 && sqrtfn
1754 && cbrtfn
ee230333 1755 && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
c3206272 1756 && speed_p
ae43b05e 1757 && hw_sqrt_exists
20cb53c9 1758 && real_equal (&c, &dconst1_6))
ae43b05e 1759 {
1760 /* sqrt(x) */
03d37e4e 1761 sqrt_arg0 = build_and_insert_call (gsi, loc, sqrtfn, arg0);
ae43b05e 1762
1763 /* cbrt(sqrt(x)) */
03d37e4e 1764 return build_and_insert_call (gsi, loc, cbrtfn, sqrt_arg0);
ca12eb68 1765 }
1766
ca12eb68 1767
c3206272 1768 /* Attempt to expand the POW as a product of square root chains.
1769 Expand the 0.25 case even when otpimising for size. */
ca12eb68 1770 if (flag_unsafe_math_optimizations
1771 && sqrtfn
c3206272 1772 && hw_sqrt_exists
20cb53c9 1773 && (speed_p || real_equal (&c, &dconst1_4))
c3206272 1774 && !HONOR_SIGNED_ZEROS (mode))
ca12eb68 1775 {
c3206272 1776 unsigned int max_depth = speed_p
1777 ? PARAM_VALUE (PARAM_MAX_POW_SQRT_DEPTH)
1778 : 2;
ca12eb68 1779
c3206272 1780 tree expand_with_sqrts
1781 = expand_pow_as_sqrts (gsi, loc, arg0, arg1, max_depth);
ca12eb68 1782
c3206272 1783 if (expand_with_sqrts)
1784 return expand_with_sqrts;
ca12eb68 1785 }
1786
c3206272 1787 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
1788 n = real_to_integer (&c2);
1789 real_from_integer (&cint, VOIDmode, n, SIGNED);
1790 c2_is_int = real_identical (&c2, &cint);
1791
ca12eb68 1792 /* Optimize pow(x,c), where 3c = n for some nonzero integer n, into
1793
1794 powi(x, n/3) * powi(cbrt(x), n%3), n > 0;
1795 1.0 / (powi(x, abs(n)/3) * powi(cbrt(x), abs(n)%3)), n < 0.
1796
1797 Do not calculate the first factor when n/3 = 0. As cbrt(x) is
1798 different from pow(x, 1./3.) due to rounding and behavior with
1799 negative x, we need to constrain this transformation to unsafe
1800 math and positive x or finite math. */
e913b5cd 1801 real_from_integer (&dconst3, VOIDmode, 3, SIGNED);
ca12eb68 1802 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
1803 real_round (&c2, mode, &c2);
1804 n = real_to_integer (&c2);
e913b5cd 1805 real_from_integer (&cint, VOIDmode, n, SIGNED);
ca12eb68 1806 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
1807 real_convert (&c2, mode, &c2);
1808
1809 if (flag_unsafe_math_optimizations
1810 && cbrtfn
ee230333 1811 && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
ca12eb68 1812 && real_identical (&c2, &c)
0190fe95 1813 && !c2_is_int
ca12eb68 1814 && optimize_function_for_speed_p (cfun)
1815 && powi_cost (n / 3) <= POWI_MAX_MULTS)
1816 {
1817 tree powi_x_ndiv3 = NULL_TREE;
1818
1819 /* Attempt to fold powi(arg0, abs(n/3)) into multiplies. If not
1820 possible or profitable, give up. Skip the degenerate case when
1821 abs(n) < 3, where the result is always 1. */
b1757d46 1822 if (absu_hwi (n) >= 3)
ca12eb68 1823 {
1824 powi_x_ndiv3 = gimple_expand_builtin_powi (gsi, loc, arg0,
5ebd604f 1825 abs_hwi (n / 3));
ca12eb68 1826 if (!powi_x_ndiv3)
1827 return NULL_TREE;
1828 }
1829
1830 /* Calculate powi(cbrt(x), n%3). Don't use gimple_expand_builtin_powi
1831 as that creates an unnecessary variable. Instead, just produce
1832 either cbrt(x) or cbrt(x) * cbrt(x). */
03d37e4e 1833 cbrt_x = build_and_insert_call (gsi, loc, cbrtfn, arg0);
ca12eb68 1834
b1757d46 1835 if (absu_hwi (n) % 3 == 1)
ca12eb68 1836 powi_cbrt_x = cbrt_x;
1837 else
03d37e4e 1838 powi_cbrt_x = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
ca12eb68 1839 cbrt_x, cbrt_x);
1840
1841 /* Multiply the two subexpressions, unless powi(x,abs(n)/3) = 1. */
b1757d46 1842 if (absu_hwi (n) < 3)
ca12eb68 1843 result = powi_cbrt_x;
1844 else
03d37e4e 1845 result = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
ca12eb68 1846 powi_x_ndiv3, powi_cbrt_x);
1847
1848 /* If n is negative, reciprocate the result. */
1849 if (n < 0)
03d37e4e 1850 result = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR,
ca12eb68 1851 build_real (type, dconst1), result);
1852
1853 return result;
ae43b05e 1854 }
1855
ca12eb68 1856 /* No optimizations succeeded. */
e78306af 1857 return NULL_TREE;
1858}
1859
a5c384c1 1860/* ARG is the argument to a cabs builtin call in GSI with location info
1861 LOC. Create a sequence of statements prior to GSI that calculates
1862 sqrt(R*R + I*I), where R and I are the real and imaginary components
1863 of ARG, respectively. Return an expression holding the result. */
1864
1865static tree
1866gimple_expand_builtin_cabs (gimple_stmt_iterator *gsi, location_t loc, tree arg)
1867{
03d37e4e 1868 tree real_part, imag_part, addend1, addend2, sum, result;
a5c384c1 1869 tree type = TREE_TYPE (TREE_TYPE (arg));
1870 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
3754d046 1871 machine_mode mode = TYPE_MODE (type);
a5c384c1 1872
1873 if (!flag_unsafe_math_optimizations
1874 || !optimize_bb_for_speed_p (gimple_bb (gsi_stmt (*gsi)))
1875 || !sqrtfn
1876 || optab_handler (sqrt_optab, mode) == CODE_FOR_nothing)
1877 return NULL_TREE;
1878
03d37e4e 1879 real_part = build_and_insert_ref (gsi, loc, type, "cabs",
a5c384c1 1880 REALPART_EXPR, arg);
03d37e4e 1881 addend1 = build_and_insert_binop (gsi, loc, "cabs", MULT_EXPR,
a5c384c1 1882 real_part, real_part);
03d37e4e 1883 imag_part = build_and_insert_ref (gsi, loc, type, "cabs",
a5c384c1 1884 IMAGPART_EXPR, arg);
03d37e4e 1885 addend2 = build_and_insert_binop (gsi, loc, "cabs", MULT_EXPR,
a5c384c1 1886 imag_part, imag_part);
03d37e4e 1887 sum = build_and_insert_binop (gsi, loc, "cabs", PLUS_EXPR, addend1, addend2);
1888 result = build_and_insert_call (gsi, loc, sqrtfn, sum);
a5c384c1 1889
1890 return result;
1891}
1892
a0315874 1893/* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1
e9a6c4bc 1894 on the SSA_NAME argument of each of them. Also expand powi(x,n) into
1895 an optimal number of multiplies, when n is a constant. */
a0315874 1896
65b0537f 1897namespace {
1898
1899const pass_data pass_data_cse_sincos =
1900{
1901 GIMPLE_PASS, /* type */
1902 "sincos", /* name */
1903 OPTGROUP_NONE, /* optinfo_flags */
8ed378fe 1904 TV_TREE_SINCOS, /* tv_id */
65b0537f 1905 PROP_ssa, /* properties_required */
a153e7b3 1906 PROP_gimple_opt_math, /* properties_provided */
65b0537f 1907 0, /* properties_destroyed */
1908 0, /* todo_flags_start */
8b88439e 1909 TODO_update_ssa, /* todo_flags_finish */
65b0537f 1910};
1911
1912class pass_cse_sincos : public gimple_opt_pass
1913{
1914public:
1915 pass_cse_sincos (gcc::context *ctxt)
1916 : gimple_opt_pass (pass_data_cse_sincos, ctxt)
1917 {}
1918
1919 /* opt_pass methods: */
1920 virtual bool gate (function *)
1921 {
1922 /* We no longer require either sincos or cexp, since powi expansion
1923 piggybacks on this pass. */
1924 return optimize;
1925 }
1926
1927 virtual unsigned int execute (function *);
1928
1929}; // class pass_cse_sincos
1930
1931unsigned int
1932pass_cse_sincos::execute (function *fun)
a0315874 1933{
1934 basic_block bb;
4c80086d 1935 bool cfg_changed = false;
a0315874 1936
1937 calculate_dominance_info (CDI_DOMINATORS);
30c4e60d 1938 memset (&sincos_stats, 0, sizeof (sincos_stats));
a0315874 1939
65b0537f 1940 FOR_EACH_BB_FN (bb, fun)
a0315874 1941 {
75a70cf9 1942 gimple_stmt_iterator gsi;
2a155cf0 1943 bool cleanup_eh = false;
a0315874 1944
75a70cf9 1945 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
a0315874 1946 {
42acab1c 1947 gimple *stmt = gsi_stmt (gsi);
a0315874 1948
2a155cf0 1949 /* Only the last stmt in a bb could throw, no need to call
1950 gimple_purge_dead_eh_edges if we change something in the middle
1951 of a basic block. */
1952 cleanup_eh = false;
1953
fa0793ad 1954 if (is_gimple_call (stmt)
5e8b972c 1955 && gimple_call_lhs (stmt))
a0315874 1956 {
e9a6c4bc 1957 tree arg, arg0, arg1, result;
1958 HOST_WIDE_INT n;
1959 location_t loc;
a0315874 1960
fa0793ad 1961 switch (gimple_call_combined_fn (stmt))
a0315874 1962 {
fa0793ad 1963 CASE_CFN_COS:
1964 CASE_CFN_SIN:
1965 CASE_CFN_CEXPI:
d312d7df 1966 /* Make sure we have either sincos or cexp. */
30f690e0 1967 if (!targetm.libc_has_function (function_c99_math_complex)
1968 && !targetm.libc_has_function (function_sincos))
d312d7df 1969 break;
1970
75a70cf9 1971 arg = gimple_call_arg (stmt, 0);
a0315874 1972 if (TREE_CODE (arg) == SSA_NAME)
4c80086d 1973 cfg_changed |= execute_cse_sincos_1 (arg);
a0315874 1974 break;
1975
fa0793ad 1976 CASE_CFN_POW:
e78306af 1977 arg0 = gimple_call_arg (stmt, 0);
1978 arg1 = gimple_call_arg (stmt, 1);
1979
1980 loc = gimple_location (stmt);
1981 result = gimple_expand_builtin_pow (&gsi, loc, arg0, arg1);
1982
1983 if (result)
1984 {
1985 tree lhs = gimple_get_lhs (stmt);
1a91d914 1986 gassign *new_stmt = gimple_build_assign (lhs, result);
e78306af 1987 gimple_set_location (new_stmt, loc);
1988 unlink_stmt_vdef (stmt);
1989 gsi_replace (&gsi, new_stmt, true);
2a155cf0 1990 cleanup_eh = true;
bc8a8451 1991 if (gimple_vdef (stmt))
1992 release_ssa_name (gimple_vdef (stmt));
e78306af 1993 }
1994 break;
1995
fa0793ad 1996 CASE_CFN_POWI:
e9a6c4bc 1997 arg0 = gimple_call_arg (stmt, 0);
1998 arg1 = gimple_call_arg (stmt, 1);
e9a6c4bc 1999 loc = gimple_location (stmt);
377db285 2000
6dfe7d53 2001 if (real_minus_onep (arg0))
377db285 2002 {
2003 tree t0, t1, cond, one, minus_one;
1a91d914 2004 gassign *stmt;
377db285 2005
2006 t0 = TREE_TYPE (arg0);
2007 t1 = TREE_TYPE (arg1);
2008 one = build_real (t0, dconst1);
2009 minus_one = build_real (t0, dconstm1);
2010
2011 cond = make_temp_ssa_name (t1, NULL, "powi_cond");
e9cf809e 2012 stmt = gimple_build_assign (cond, BIT_AND_EXPR,
2013 arg1, build_int_cst (t1, 1));
377db285 2014 gimple_set_location (stmt, loc);
2015 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
2016
2017 result = make_temp_ssa_name (t0, NULL, "powi");
e9cf809e 2018 stmt = gimple_build_assign (result, COND_EXPR, cond,
2019 minus_one, one);
377db285 2020 gimple_set_location (stmt, loc);
2021 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
2022 }
2023 else
2024 {
e913b5cd 2025 if (!tree_fits_shwi_p (arg1))
d48be958 2026 break;
2027
e913b5cd 2028 n = tree_to_shwi (arg1);
377db285 2029 result = gimple_expand_builtin_powi (&gsi, loc, arg0, n);
2030 }
e9a6c4bc 2031
2032 if (result)
2033 {
2034 tree lhs = gimple_get_lhs (stmt);
1a91d914 2035 gassign *new_stmt = gimple_build_assign (lhs, result);
e9a6c4bc 2036 gimple_set_location (new_stmt, loc);
a5c384c1 2037 unlink_stmt_vdef (stmt);
2038 gsi_replace (&gsi, new_stmt, true);
2a155cf0 2039 cleanup_eh = true;
bc8a8451 2040 if (gimple_vdef (stmt))
2041 release_ssa_name (gimple_vdef (stmt));
a5c384c1 2042 }
2043 break;
2044
fa0793ad 2045 CASE_CFN_CABS:
a5c384c1 2046 arg0 = gimple_call_arg (stmt, 0);
2047 loc = gimple_location (stmt);
2048 result = gimple_expand_builtin_cabs (&gsi, loc, arg0);
2049
2050 if (result)
2051 {
2052 tree lhs = gimple_get_lhs (stmt);
1a91d914 2053 gassign *new_stmt = gimple_build_assign (lhs, result);
a5c384c1 2054 gimple_set_location (new_stmt, loc);
e9a6c4bc 2055 unlink_stmt_vdef (stmt);
2056 gsi_replace (&gsi, new_stmt, true);
2a155cf0 2057 cleanup_eh = true;
bc8a8451 2058 if (gimple_vdef (stmt))
2059 release_ssa_name (gimple_vdef (stmt));
e9a6c4bc 2060 }
2061 break;
2062
a0315874 2063 default:;
2064 }
2065 }
2066 }
2a155cf0 2067 if (cleanup_eh)
2068 cfg_changed |= gimple_purge_dead_eh_edges (bb);
a0315874 2069 }
2070
65b0537f 2071 statistics_counter_event (fun, "sincos statements inserted",
30c4e60d 2072 sincos_stats.inserted);
2073
4c80086d 2074 return cfg_changed ? TODO_cleanup_cfg : 0;
a0315874 2075}
2076
cbe8bda8 2077} // anon namespace
2078
2079gimple_opt_pass *
2080make_pass_cse_sincos (gcc::context *ctxt)
2081{
2082 return new pass_cse_sincos (ctxt);
2083}
2084
71dbd910 2085/* Return true if stmt is a type conversion operation that can be stripped
2086 when used in a widening multiply operation. */
2087static bool
42acab1c 2088widening_mult_conversion_strippable_p (tree result_type, gimple *stmt)
71dbd910 2089{
2090 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
2091
2092 if (TREE_CODE (result_type) == INTEGER_TYPE)
2093 {
2094 tree op_type;
2095 tree inner_op_type;
2096
2097 if (!CONVERT_EXPR_CODE_P (rhs_code))
2098 return false;
2099
2100 op_type = TREE_TYPE (gimple_assign_lhs (stmt));
2101
2102 /* If the type of OP has the same precision as the result, then
2103 we can strip this conversion. The multiply operation will be
2104 selected to create the correct extension as a by-product. */
2105 if (TYPE_PRECISION (result_type) == TYPE_PRECISION (op_type))
2106 return true;
2107
2108 /* We can also strip a conversion if it preserves the signed-ness of
2109 the operation and doesn't narrow the range. */
2110 inner_op_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
2111
8f9d1531 2112 /* If the inner-most type is unsigned, then we can strip any
2113 intermediate widening operation. If it's signed, then the
2114 intermediate widening operation must also be signed. */
2115 if ((TYPE_UNSIGNED (inner_op_type)
2116 || TYPE_UNSIGNED (op_type) == TYPE_UNSIGNED (inner_op_type))
71dbd910 2117 && TYPE_PRECISION (op_type) > TYPE_PRECISION (inner_op_type))
2118 return true;
2119
2120 return false;
2121 }
2122
2123 return rhs_code == FIXED_CONVERT_EXPR;
2124}
2125
0989f516 2126/* Return true if RHS is a suitable operand for a widening multiplication,
2127 assuming a target type of TYPE.
7e4c867e 2128 There are two cases:
2129
aff5fb4d 2130 - RHS makes some value at least twice as wide. Store that value
2131 in *NEW_RHS_OUT if so, and store its type in *TYPE_OUT.
7e4c867e 2132
2133 - RHS is an integer constant. Store that value in *NEW_RHS_OUT if so,
2134 but leave *TYPE_OUT untouched. */
00f4f705 2135
2136static bool
0989f516 2137is_widening_mult_rhs_p (tree type, tree rhs, tree *type_out,
2138 tree *new_rhs_out)
7e4c867e 2139{
42acab1c 2140 gimple *stmt;
0989f516 2141 tree type1, rhs1;
7e4c867e 2142
2143 if (TREE_CODE (rhs) == SSA_NAME)
2144 {
7e4c867e 2145 stmt = SSA_NAME_DEF_STMT (rhs);
0989f516 2146 if (is_gimple_assign (stmt))
2147 {
71dbd910 2148 if (! widening_mult_conversion_strippable_p (type, stmt))
0989f516 2149 rhs1 = rhs;
2150 else
ffebd9c5 2151 {
2152 rhs1 = gimple_assign_rhs1 (stmt);
2153
2154 if (TREE_CODE (rhs1) == INTEGER_CST)
2155 {
2156 *new_rhs_out = rhs1;
2157 *type_out = NULL;
2158 return true;
2159 }
2160 }
0989f516 2161 }
2162 else
2163 rhs1 = rhs;
7e4c867e 2164
7e4c867e 2165 type1 = TREE_TYPE (rhs1);
0989f516 2166
7e4c867e 2167 if (TREE_CODE (type1) != TREE_CODE (type)
aff5fb4d 2168 || TYPE_PRECISION (type1) * 2 > TYPE_PRECISION (type))
7e4c867e 2169 return false;
2170
2171 *new_rhs_out = rhs1;
2172 *type_out = type1;
2173 return true;
2174 }
2175
2176 if (TREE_CODE (rhs) == INTEGER_CST)
2177 {
2178 *new_rhs_out = rhs;
2179 *type_out = NULL;
2180 return true;
2181 }
2182
2183 return false;
2184}
2185
0989f516 2186/* Return true if STMT performs a widening multiplication, assuming the
2187 output type is TYPE. If so, store the unwidened types of the operands
2188 in *TYPE1_OUT and *TYPE2_OUT respectively. Also fill *RHS1_OUT and
2189 *RHS2_OUT such that converting those operands to types *TYPE1_OUT
2190 and *TYPE2_OUT would give the operands of the multiplication. */
7e4c867e 2191
2192static bool
42acab1c 2193is_widening_mult_p (gimple *stmt,
7e4c867e 2194 tree *type1_out, tree *rhs1_out,
2195 tree *type2_out, tree *rhs2_out)
00f4f705 2196{
4333b41f 2197 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
2198
7e4c867e 2199 if (TREE_CODE (type) != INTEGER_TYPE
2200 && TREE_CODE (type) != FIXED_POINT_TYPE)
2201 return false;
00f4f705 2202
0989f516 2203 if (!is_widening_mult_rhs_p (type, gimple_assign_rhs1 (stmt), type1_out,
2204 rhs1_out))
00f4f705 2205 return false;
2206
0989f516 2207 if (!is_widening_mult_rhs_p (type, gimple_assign_rhs2 (stmt), type2_out,
2208 rhs2_out))
7e4c867e 2209 return false;
00f4f705 2210
7e4c867e 2211 if (*type1_out == NULL)
00f4f705 2212 {
7e4c867e 2213 if (*type2_out == NULL || !int_fits_type_p (*rhs1_out, *type2_out))
00f4f705 2214 return false;
7e4c867e 2215 *type1_out = *type2_out;
00f4f705 2216 }
00f4f705 2217
7e4c867e 2218 if (*type2_out == NULL)
00f4f705 2219 {
7e4c867e 2220 if (!int_fits_type_p (*rhs2_out, *type1_out))
00f4f705 2221 return false;
7e4c867e 2222 *type2_out = *type1_out;
00f4f705 2223 }
00f4f705 2224
287c271c 2225 /* Ensure that the larger of the two operands comes first. */
2226 if (TYPE_PRECISION (*type1_out) < TYPE_PRECISION (*type2_out))
2227 {
dfcf26a5 2228 std::swap (*type1_out, *type2_out);
2229 std::swap (*rhs1_out, *rhs2_out);
287c271c 2230 }
aff5fb4d 2231
7e4c867e 2232 return true;
2233}
00f4f705 2234
eb728046 2235/* Check to see if the CALL statement is an invocation of copysign
2236 with 1. being the first argument. */
2237static bool
2238is_copysign_call_with_1 (gimple *call)
2239{
2240 gcall *c = dyn_cast <gcall *> (call);
2241 if (! c)
2242 return false;
2243
2244 enum combined_fn code = gimple_call_combined_fn (c);
2245
2246 if (code == CFN_LAST)
2247 return false;
2248
2249 if (builtin_fn_p (code))
2250 {
2251 switch (as_builtin_fn (code))
2252 {
2253 CASE_FLT_FN (BUILT_IN_COPYSIGN):
2254 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
2255 return real_onep (gimple_call_arg (c, 0));
2256 default:
2257 return false;
2258 }
2259 }
2260
2261 if (internal_fn_p (code))
2262 {
2263 switch (as_internal_fn (code))
2264 {
2265 case IFN_COPYSIGN:
2266 return real_onep (gimple_call_arg (c, 0));
2267 default:
2268 return false;
2269 }
2270 }
2271
2272 return false;
2273}
2274
2275/* Try to expand the pattern x * copysign (1, y) into xorsign (x, y).
2276 This only happens when the the xorsign optab is defined, if the
2277 pattern is not a xorsign pattern or if expansion fails FALSE is
2278 returned, otherwise TRUE is returned. */
2279static bool
2280convert_expand_mult_copysign (gimple *stmt, gimple_stmt_iterator *gsi)
2281{
2282 tree treeop0, treeop1, lhs, type;
2283 location_t loc = gimple_location (stmt);
2284 lhs = gimple_assign_lhs (stmt);
2285 treeop0 = gimple_assign_rhs1 (stmt);
2286 treeop1 = gimple_assign_rhs2 (stmt);
2287 type = TREE_TYPE (lhs);
2288 machine_mode mode = TYPE_MODE (type);
2289
3aa2a10c 2290 if (HONOR_SNANS (type))
eb728046 2291 return false;
2292
2293 if (TREE_CODE (treeop0) == SSA_NAME && TREE_CODE (treeop1) == SSA_NAME)
2294 {
2295 gimple *call0 = SSA_NAME_DEF_STMT (treeop0);
3aa2a10c 2296 if (!has_single_use (treeop0) || !is_copysign_call_with_1 (call0))
eb728046 2297 {
2298 call0 = SSA_NAME_DEF_STMT (treeop1);
3aa2a10c 2299 if (!has_single_use (treeop1) || !is_copysign_call_with_1 (call0))
eb728046 2300 return false;
2301
2302 treeop1 = treeop0;
2303 }
eb728046 2304 if (optab_handler (xorsign_optab, mode) == CODE_FOR_nothing)
2305 return false;
2306
2307 gcall *c = as_a<gcall*> (call0);
2308 treeop0 = gimple_call_arg (c, 1);
2309
2310 gcall *call_stmt
2311 = gimple_build_call_internal (IFN_XORSIGN, 2, treeop1, treeop0);
2312 gimple_set_lhs (call_stmt, lhs);
2313 gimple_set_location (call_stmt, loc);
2314 gsi_replace (gsi, call_stmt, true);
2315 return true;
2316 }
2317
2318 return false;
2319}
2320
7e4c867e 2321/* Process a single gimple statement STMT, which has a MULT_EXPR as
2322 its rhs, and try to convert it into a WIDEN_MULT_EXPR. The return
2323 value is true iff we converted the statement. */
2324
2325static bool
42acab1c 2326convert_mult_to_widen (gimple *stmt, gimple_stmt_iterator *gsi)
7e4c867e 2327{
03d37e4e 2328 tree lhs, rhs1, rhs2, type, type1, type2;
7e4c867e 2329 enum insn_code handler;
d2a1b453 2330 scalar_int_mode to_mode, from_mode, actual_mode;
5a574e8b 2331 optab op;
aff5fb4d 2332 int actual_precision;
2333 location_t loc = gimple_location (stmt);
3f2ab719 2334 bool from_unsigned1, from_unsigned2;
7e4c867e 2335
2336 lhs = gimple_assign_lhs (stmt);
2337 type = TREE_TYPE (lhs);
2338 if (TREE_CODE (type) != INTEGER_TYPE)
00f4f705 2339 return false;
2340
4333b41f 2341 if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2))
00f4f705 2342 return false;
2343
03b7a719 2344 to_mode = SCALAR_INT_TYPE_MODE (type);
2345 from_mode = SCALAR_INT_TYPE_MODE (type1);
f90f6ff1 2346 if (to_mode == from_mode)
2347 return false;
2348
3f2ab719 2349 from_unsigned1 = TYPE_UNSIGNED (type1);
2350 from_unsigned2 = TYPE_UNSIGNED (type2);
5a574e8b 2351
3f2ab719 2352 if (from_unsigned1 && from_unsigned2)
5a574e8b 2353 op = umul_widen_optab;
3f2ab719 2354 else if (!from_unsigned1 && !from_unsigned2)
5a574e8b 2355 op = smul_widen_optab;
00f4f705 2356 else
5a574e8b 2357 op = usmul_widen_optab;
2358
aff5fb4d 2359 handler = find_widening_optab_handler_and_mode (op, to_mode, from_mode,
d2a1b453 2360 &actual_mode);
7e4c867e 2361
2362 if (handler == CODE_FOR_nothing)
3f2ab719 2363 {
2364 if (op != smul_widen_optab)
2365 {
22ffd684 2366 /* We can use a signed multiply with unsigned types as long as
2367 there is a wider mode to use, or it is the smaller of the two
2368 types that is unsigned. Note that type1 >= type2, always. */
2369 if ((TYPE_UNSIGNED (type1)
2370 && TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode))
2371 || (TYPE_UNSIGNED (type2)
2372 && TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode)))
2373 {
28ebc73c 2374 if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode)
2375 || GET_MODE_SIZE (to_mode) <= GET_MODE_SIZE (from_mode))
22ffd684 2376 return false;
2377 }
3f2ab719 2378
2379 op = smul_widen_optab;
2380 handler = find_widening_optab_handler_and_mode (op, to_mode,
d2a1b453 2381 from_mode,
3f2ab719 2382 &actual_mode);
2383
2384 if (handler == CODE_FOR_nothing)
2385 return false;
2386
2387 from_unsigned1 = from_unsigned2 = false;
2388 }
2389 else
2390 return false;
2391 }
7e4c867e 2392
aff5fb4d 2393 /* Ensure that the inputs to the handler are in the correct precison
2394 for the opcode. This will be the full mode size. */
2395 actual_precision = GET_MODE_PRECISION (actual_mode);
b36be69d 2396 if (2 * actual_precision > TYPE_PRECISION (type))
2397 return false;
3f2ab719 2398 if (actual_precision != TYPE_PRECISION (type1)
2399 || from_unsigned1 != TYPE_UNSIGNED (type1))
03d37e4e 2400 rhs1 = build_and_insert_cast (gsi, loc,
2401 build_nonstandard_integer_type
2402 (actual_precision, from_unsigned1), rhs1);
3f2ab719 2403 if (actual_precision != TYPE_PRECISION (type2)
2404 || from_unsigned2 != TYPE_UNSIGNED (type2))
03d37e4e 2405 rhs2 = build_and_insert_cast (gsi, loc,
2406 build_nonstandard_integer_type
2407 (actual_precision, from_unsigned2), rhs2);
aff5fb4d 2408
ffebd9c5 2409 /* Handle constants. */
2410 if (TREE_CODE (rhs1) == INTEGER_CST)
2411 rhs1 = fold_convert (type1, rhs1);
2412 if (TREE_CODE (rhs2) == INTEGER_CST)
2413 rhs2 = fold_convert (type2, rhs2);
2414
aff5fb4d 2415 gimple_assign_set_rhs1 (stmt, rhs1);
2416 gimple_assign_set_rhs2 (stmt, rhs2);
00f4f705 2417 gimple_assign_set_rhs_code (stmt, WIDEN_MULT_EXPR);
2418 update_stmt (stmt);
30c4e60d 2419 widen_mul_stats.widen_mults_inserted++;
00f4f705 2420 return true;
2421}
2422
2423/* Process a single gimple statement STMT, which is found at the
2424 iterator GSI and has a either a PLUS_EXPR or a MINUS_EXPR as its
2425 rhs (given by CODE), and try to convert it into a
2426 WIDEN_MULT_PLUS_EXPR or a WIDEN_MULT_MINUS_EXPR. The return value
2427 is true iff we converted the statement. */
2428
2429static bool
42acab1c 2430convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple *stmt,
00f4f705 2431 enum tree_code code)
2432{
42acab1c 2433 gimple *rhs1_stmt = NULL, *rhs2_stmt = NULL;
2434 gimple *conv1_stmt = NULL, *conv2_stmt = NULL, *conv_stmt;
03d37e4e 2435 tree type, type1, type2, optype;
00f4f705 2436 tree lhs, rhs1, rhs2, mult_rhs1, mult_rhs2, add_rhs;
2437 enum tree_code rhs1_code = ERROR_MARK, rhs2_code = ERROR_MARK;
2438 optab this_optab;
2439 enum tree_code wmult_code;
aff5fb4d 2440 enum insn_code handler;
d2a1b453 2441 scalar_mode to_mode, from_mode, actual_mode;
aff5fb4d 2442 location_t loc = gimple_location (stmt);
2443 int actual_precision;
3f2ab719 2444 bool from_unsigned1, from_unsigned2;
00f4f705 2445
2446 lhs = gimple_assign_lhs (stmt);
2447 type = TREE_TYPE (lhs);
7e4c867e 2448 if (TREE_CODE (type) != INTEGER_TYPE
2449 && TREE_CODE (type) != FIXED_POINT_TYPE)
00f4f705 2450 return false;
2451
2452 if (code == MINUS_EXPR)
2453 wmult_code = WIDEN_MULT_MINUS_EXPR;
2454 else
2455 wmult_code = WIDEN_MULT_PLUS_EXPR;
2456
00f4f705 2457 rhs1 = gimple_assign_rhs1 (stmt);
2458 rhs2 = gimple_assign_rhs2 (stmt);
2459
2460 if (TREE_CODE (rhs1) == SSA_NAME)
2461 {
2462 rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
2463 if (is_gimple_assign (rhs1_stmt))
2464 rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
2465 }
00f4f705 2466
2467 if (TREE_CODE (rhs2) == SSA_NAME)
2468 {
2469 rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
2470 if (is_gimple_assign (rhs2_stmt))
2471 rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
2472 }
00f4f705 2473
07ea3e5c 2474 /* Allow for one conversion statement between the multiply
2475 and addition/subtraction statement. If there are more than
2476 one conversions then we assume they would invalidate this
2477 transformation. If that's not the case then they should have
2478 been folded before now. */
2479 if (CONVERT_EXPR_CODE_P (rhs1_code))
2480 {
2481 conv1_stmt = rhs1_stmt;
2482 rhs1 = gimple_assign_rhs1 (rhs1_stmt);
2483 if (TREE_CODE (rhs1) == SSA_NAME)
2484 {
2485 rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
2486 if (is_gimple_assign (rhs1_stmt))
2487 rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
2488 }
2489 else
2490 return false;
2491 }
2492 if (CONVERT_EXPR_CODE_P (rhs2_code))
2493 {
2494 conv2_stmt = rhs2_stmt;
2495 rhs2 = gimple_assign_rhs1 (rhs2_stmt);
2496 if (TREE_CODE (rhs2) == SSA_NAME)
2497 {
2498 rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
2499 if (is_gimple_assign (rhs2_stmt))
2500 rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
2501 }
2502 else
2503 return false;
2504 }
2505
aff5fb4d 2506 /* If code is WIDEN_MULT_EXPR then it would seem unnecessary to call
2507 is_widening_mult_p, but we still need the rhs returns.
2508
2509 It might also appear that it would be sufficient to use the existing
2510 operands of the widening multiply, but that would limit the choice of
e0df5be0 2511 multiply-and-accumulate instructions.
2512
2513 If the widened-multiplication result has more than one uses, it is
2514 probably wiser not to do the conversion. */
aff5fb4d 2515 if (code == PLUS_EXPR
2516 && (rhs1_code == MULT_EXPR || rhs1_code == WIDEN_MULT_EXPR))
00f4f705 2517 {
e0df5be0 2518 if (!has_single_use (rhs1)
2519 || !is_widening_mult_p (rhs1_stmt, &type1, &mult_rhs1,
2520 &type2, &mult_rhs2))
00f4f705 2521 return false;
7e4c867e 2522 add_rhs = rhs2;
07ea3e5c 2523 conv_stmt = conv1_stmt;
00f4f705 2524 }
aff5fb4d 2525 else if (rhs2_code == MULT_EXPR || rhs2_code == WIDEN_MULT_EXPR)
00f4f705 2526 {
e0df5be0 2527 if (!has_single_use (rhs2)
2528 || !is_widening_mult_p (rhs2_stmt, &type1, &mult_rhs1,
2529 &type2, &mult_rhs2))
00f4f705 2530 return false;
7e4c867e 2531 add_rhs = rhs1;
07ea3e5c 2532 conv_stmt = conv2_stmt;
00f4f705 2533 }
00f4f705 2534 else
2535 return false;
2536
3d2b0034 2537 to_mode = SCALAR_TYPE_MODE (type);
2538 from_mode = SCALAR_TYPE_MODE (type1);
f90f6ff1 2539 if (to_mode == from_mode)
2540 return false;
2541
3f2ab719 2542 from_unsigned1 = TYPE_UNSIGNED (type1);
2543 from_unsigned2 = TYPE_UNSIGNED (type2);
4ccf368d 2544 optype = type1;
aff5fb4d 2545
3f2ab719 2546 /* There's no such thing as a mixed sign madd yet, so use a wider mode. */
2547 if (from_unsigned1 != from_unsigned2)
2548 {
4ccf368d 2549 if (!INTEGRAL_TYPE_P (type))
2550 return false;
22ffd684 2551 /* We can use a signed multiply with unsigned types as long as
2552 there is a wider mode to use, or it is the smaller of the two
2553 types that is unsigned. Note that type1 >= type2, always. */
2554 if ((from_unsigned1
2555 && TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode))
2556 || (from_unsigned2
2557 && TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode)))
3f2ab719 2558 {
28ebc73c 2559 if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode)
2560 || GET_MODE_SIZE (from_mode) >= GET_MODE_SIZE (to_mode))
22ffd684 2561 return false;
3f2ab719 2562 }
22ffd684 2563
2564 from_unsigned1 = from_unsigned2 = false;
4ccf368d 2565 optype = build_nonstandard_integer_type (GET_MODE_PRECISION (from_mode),
2566 false);
3f2ab719 2567 }
815a0224 2568
07ea3e5c 2569 /* If there was a conversion between the multiply and addition
2570 then we need to make sure it fits a multiply-and-accumulate.
2571 The should be a single mode change which does not change the
2572 value. */
2573 if (conv_stmt)
2574 {
3f2ab719 2575 /* We use the original, unmodified data types for this. */
07ea3e5c 2576 tree from_type = TREE_TYPE (gimple_assign_rhs1 (conv_stmt));
2577 tree to_type = TREE_TYPE (gimple_assign_lhs (conv_stmt));
2578 int data_size = TYPE_PRECISION (type1) + TYPE_PRECISION (type2);
2579 bool is_unsigned = TYPE_UNSIGNED (type1) && TYPE_UNSIGNED (type2);
2580
2581 if (TYPE_PRECISION (from_type) > TYPE_PRECISION (to_type))
2582 {
2583 /* Conversion is a truncate. */
2584 if (TYPE_PRECISION (to_type) < data_size)
2585 return false;
2586 }
2587 else if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type))
2588 {
2589 /* Conversion is an extend. Check it's the right sort. */
2590 if (TYPE_UNSIGNED (from_type) != is_unsigned
2591 && !(is_unsigned && TYPE_PRECISION (from_type) > data_size))
2592 return false;
2593 }
2594 /* else convert is a no-op for our purposes. */
2595 }
2596
815a0224 2597 /* Verify that the machine can perform a widening multiply
2598 accumulate in this mode/signedness combination, otherwise
2599 this transformation is likely to pessimize code. */
3f2ab719 2600 this_optab = optab_for_tree_code (wmult_code, optype, optab_default);
aff5fb4d 2601 handler = find_widening_optab_handler_and_mode (this_optab, to_mode,
d2a1b453 2602 from_mode, &actual_mode);
aff5fb4d 2603
2604 if (handler == CODE_FOR_nothing)
815a0224 2605 return false;
2606
aff5fb4d 2607 /* Ensure that the inputs to the handler are in the correct precison
2608 for the opcode. This will be the full mode size. */
2609 actual_precision = GET_MODE_PRECISION (actual_mode);
3f2ab719 2610 if (actual_precision != TYPE_PRECISION (type1)
2611 || from_unsigned1 != TYPE_UNSIGNED (type1))
03d37e4e 2612 mult_rhs1 = build_and_insert_cast (gsi, loc,
2613 build_nonstandard_integer_type
2614 (actual_precision, from_unsigned1),
2615 mult_rhs1);
3f2ab719 2616 if (actual_precision != TYPE_PRECISION (type2)
2617 || from_unsigned2 != TYPE_UNSIGNED (type2))
03d37e4e 2618 mult_rhs2 = build_and_insert_cast (gsi, loc,
2619 build_nonstandard_integer_type
2620 (actual_precision, from_unsigned2),
2621 mult_rhs2);
00f4f705 2622
12421545 2623 if (!useless_type_conversion_p (type, TREE_TYPE (add_rhs)))
03d37e4e 2624 add_rhs = build_and_insert_cast (gsi, loc, type, add_rhs);
12421545 2625
ffebd9c5 2626 /* Handle constants. */
2627 if (TREE_CODE (mult_rhs1) == INTEGER_CST)
d5a3bb10 2628 mult_rhs1 = fold_convert (type1, mult_rhs1);
ffebd9c5 2629 if (TREE_CODE (mult_rhs2) == INTEGER_CST)
d5a3bb10 2630 mult_rhs2 = fold_convert (type2, mult_rhs2);
ffebd9c5 2631
806413d2 2632 gimple_assign_set_rhs_with_ops (gsi, wmult_code, mult_rhs1, mult_rhs2,
2633 add_rhs);
00f4f705 2634 update_stmt (gsi_stmt (*gsi));
30c4e60d 2635 widen_mul_stats.maccs_inserted++;
00f4f705 2636 return true;
2637}
2638
15dbdc8f 2639/* Combine the multiplication at MUL_STMT with operands MULOP1 and MULOP2
2640 with uses in additions and subtractions to form fused multiply-add
2641 operations. Returns true if successful and MUL_STMT should be removed. */
b9be572e 2642
2643static bool
42acab1c 2644convert_mult_to_fma (gimple *mul_stmt, tree op1, tree op2)
b9be572e 2645{
15dbdc8f 2646 tree mul_result = gimple_get_lhs (mul_stmt);
b9be572e 2647 tree type = TREE_TYPE (mul_result);
42acab1c 2648 gimple *use_stmt, *neguse_stmt;
1a91d914 2649 gassign *fma_stmt;
b9be572e 2650 use_operand_p use_p;
2651 imm_use_iterator imm_iter;
2652
2653 if (FLOAT_TYPE_P (type)
2654 && flag_fp_contract_mode == FP_CONTRACT_OFF)
2655 return false;
2656
2657 /* We don't want to do bitfield reduction ops. */
2658 if (INTEGRAL_TYPE_P (type)
654ba22c 2659 && !type_has_mode_precision_p (type))
b9be572e 2660 return false;
2661
2662 /* If the target doesn't support it, don't generate it. We assume that
2663 if fma isn't available then fms, fnma or fnms are not either. */
2664 if (optab_handler (fma_optab, TYPE_MODE (type)) == CODE_FOR_nothing)
2665 return false;
2666
5ed3d3b8 2667 /* If the multiplication has zero uses, it is kept around probably because
2668 of -fnon-call-exceptions. Don't optimize it away in that case,
2669 it is DCE job. */
2670 if (has_zero_uses (mul_result))
2671 return false;
2672
b9be572e 2673 /* Make sure that the multiplication statement becomes dead after
2674 the transformation, thus that all uses are transformed to FMAs.
2675 This means we assume that an FMA operation has the same cost
2676 as an addition. */
2677 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, mul_result)
2678 {
2679 enum tree_code use_code;
44579526 2680 tree result = mul_result;
2681 bool negate_p = false;
b9be572e 2682
2683 use_stmt = USE_STMT (use_p);
2684
17a2c727 2685 if (is_gimple_debug (use_stmt))
2686 continue;
2687
b9be572e 2688 /* For now restrict this operations to single basic blocks. In theory
2689 we would want to support sinking the multiplication in
2690 m = a*b;
2691 if ()
2692 ma = m + c;
2693 else
2694 d = m;
2695 to form a fma in the then block and sink the multiplication to the
2696 else block. */
2697 if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
2698 return false;
2699
44579526 2700 if (!is_gimple_assign (use_stmt))
b9be572e 2701 return false;
2702
44579526 2703 use_code = gimple_assign_rhs_code (use_stmt);
2704
2705 /* A negate on the multiplication leads to FNMA. */
2706 if (use_code == NEGATE_EXPR)
2707 {
805ad414 2708 ssa_op_iter iter;
5715c09b 2709 use_operand_p usep;
805ad414 2710
44579526 2711 result = gimple_assign_lhs (use_stmt);
2712
2713 /* Make sure the negate statement becomes dead with this
2714 single transformation. */
2715 if (!single_imm_use (gimple_assign_lhs (use_stmt),
2716 &use_p, &neguse_stmt))
2717 return false;
2718
805ad414 2719 /* Make sure the multiplication isn't also used on that stmt. */
5715c09b 2720 FOR_EACH_PHI_OR_STMT_USE (usep, neguse_stmt, iter, SSA_OP_USE)
2721 if (USE_FROM_PTR (usep) == mul_result)
805ad414 2722 return false;
2723
44579526 2724 /* Re-validate. */
2725 use_stmt = neguse_stmt;
2726 if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
2727 return false;
2728 if (!is_gimple_assign (use_stmt))
2729 return false;
2730
2731 use_code = gimple_assign_rhs_code (use_stmt);
2732 negate_p = true;
2733 }
b9be572e 2734
44579526 2735 switch (use_code)
2736 {
2737 case MINUS_EXPR:
8a9d0572 2738 if (gimple_assign_rhs2 (use_stmt) == result)
2739 negate_p = !negate_p;
2740 break;
44579526 2741 case PLUS_EXPR:
44579526 2742 break;
44579526 2743 default:
2744 /* FMA can only be formed from PLUS and MINUS. */
2745 return false;
2746 }
b9be572e 2747
b095bd6a 2748 /* If the subtrahend (gimple_assign_rhs2 (use_stmt)) is computed
2749 by a MULT_EXPR that we'll visit later, we might be able to
2750 get a more profitable match with fnma.
2751 OTOH, if we don't, a negate / fma pair has likely lower latency
2752 that a mult / subtract pair. */
2753 if (use_code == MINUS_EXPR && !negate_p
2754 && gimple_assign_rhs1 (use_stmt) == result
2755 && optab_handler (fms_optab, TYPE_MODE (type)) == CODE_FOR_nothing
2756 && optab_handler (fnma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
2757 {
2758 tree rhs2 = gimple_assign_rhs2 (use_stmt);
b095bd6a 2759
058e9571 2760 if (TREE_CODE (rhs2) == SSA_NAME)
2761 {
42acab1c 2762 gimple *stmt2 = SSA_NAME_DEF_STMT (rhs2);
058e9571 2763 if (has_single_use (rhs2)
2764 && is_gimple_assign (stmt2)
2765 && gimple_assign_rhs_code (stmt2) == MULT_EXPR)
2766 return false;
2767 }
b095bd6a 2768 }
2769
44579526 2770 /* We can't handle a * b + a * b. */
2771 if (gimple_assign_rhs1 (use_stmt) == gimple_assign_rhs2 (use_stmt))
2772 return false;
8a9d0572 2773
2774 /* While it is possible to validate whether or not the exact form
2775 that we've recognized is available in the backend, the assumption
2776 is that the transformation is never a loss. For instance, suppose
2777 the target only has the plain FMA pattern available. Consider
2778 a*b-c -> fma(a,b,-c): we've exchanged MUL+SUB for FMA+NEG, which
2779 is still two operations. Consider -(a*b)-c -> fma(-a,b,-c): we
2780 still have 3 operations, but in the FMA form the two NEGs are
9d75589a 2781 independent and could be run in parallel. */
b9be572e 2782 }
2783
2784 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, mul_result)
2785 {
b9be572e 2786 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
17a2c727 2787 enum tree_code use_code;
15dbdc8f 2788 tree addop, mulop1 = op1, result = mul_result;
44579526 2789 bool negate_p = false;
b9be572e 2790
17a2c727 2791 if (is_gimple_debug (use_stmt))
2792 continue;
2793
2794 use_code = gimple_assign_rhs_code (use_stmt);
44579526 2795 if (use_code == NEGATE_EXPR)
2796 {
2797 result = gimple_assign_lhs (use_stmt);
2798 single_imm_use (gimple_assign_lhs (use_stmt), &use_p, &neguse_stmt);
2799 gsi_remove (&gsi, true);
2800 release_defs (use_stmt);
2801
2802 use_stmt = neguse_stmt;
2803 gsi = gsi_for_stmt (use_stmt);
2804 use_code = gimple_assign_rhs_code (use_stmt);
2805 negate_p = true;
2806 }
2807
2808 if (gimple_assign_rhs1 (use_stmt) == result)
b9be572e 2809 {
2810 addop = gimple_assign_rhs2 (use_stmt);
2811 /* a * b - c -> a * b + (-c) */
2812 if (gimple_assign_rhs_code (use_stmt) == MINUS_EXPR)
2813 addop = force_gimple_operand_gsi (&gsi,
2814 build1 (NEGATE_EXPR,
2815 type, addop),
2816 true, NULL_TREE, true,
2817 GSI_SAME_STMT);
2818 }
2819 else
2820 {
2821 addop = gimple_assign_rhs1 (use_stmt);
2822 /* a - b * c -> (-b) * c + a */
2823 if (gimple_assign_rhs_code (use_stmt) == MINUS_EXPR)
44579526 2824 negate_p = !negate_p;
b9be572e 2825 }
2826
44579526 2827 if (negate_p)
2828 mulop1 = force_gimple_operand_gsi (&gsi,
2829 build1 (NEGATE_EXPR,
2830 type, mulop1),
2831 true, NULL_TREE, true,
2832 GSI_SAME_STMT);
2833
e9cf809e 2834 fma_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
2835 FMA_EXPR, mulop1, op2, addop);
b9be572e 2836 gsi_replace (&gsi, fma_stmt, true);
30c4e60d 2837 widen_mul_stats.fmas_inserted++;
b9be572e 2838 }
2839
2840 return true;
2841}
2842
e11a63e8 2843
2844/* Helper function of match_uaddsub_overflow. Return 1
2845 if USE_STMT is unsigned overflow check ovf != 0 for
2846 STMT, -1 if USE_STMT is unsigned overflow check ovf == 0
2847 and 0 otherwise. */
2848
2849static int
2850uaddsub_overflow_check_p (gimple *stmt, gimple *use_stmt)
2851{
2852 enum tree_code ccode = ERROR_MARK;
2853 tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
2854 if (gimple_code (use_stmt) == GIMPLE_COND)
2855 {
2856 ccode = gimple_cond_code (use_stmt);
2857 crhs1 = gimple_cond_lhs (use_stmt);
2858 crhs2 = gimple_cond_rhs (use_stmt);
2859 }
2860 else if (is_gimple_assign (use_stmt))
2861 {
2862 if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2863 {
2864 ccode = gimple_assign_rhs_code (use_stmt);
2865 crhs1 = gimple_assign_rhs1 (use_stmt);
2866 crhs2 = gimple_assign_rhs2 (use_stmt);
2867 }
2868 else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
2869 {
2870 tree cond = gimple_assign_rhs1 (use_stmt);
2871 if (COMPARISON_CLASS_P (cond))
2872 {
2873 ccode = TREE_CODE (cond);
2874 crhs1 = TREE_OPERAND (cond, 0);
2875 crhs2 = TREE_OPERAND (cond, 1);
2876 }
2877 else
2878 return 0;
2879 }
2880 else
2881 return 0;
2882 }
2883 else
2884 return 0;
2885
2886 if (TREE_CODE_CLASS (ccode) != tcc_comparison)
2887 return 0;
2888
2889 enum tree_code code = gimple_assign_rhs_code (stmt);
2890 tree lhs = gimple_assign_lhs (stmt);
2891 tree rhs1 = gimple_assign_rhs1 (stmt);
2892 tree rhs2 = gimple_assign_rhs2 (stmt);
2893
2894 switch (ccode)
2895 {
2896 case GT_EXPR:
2897 case LE_EXPR:
2898 /* r = a - b; r > a or r <= a
2899 r = a + b; a > r or a <= r or b > r or b <= r. */
2900 if ((code == MINUS_EXPR && crhs1 == lhs && crhs2 == rhs1)
2901 || (code == PLUS_EXPR && (crhs1 == rhs1 || crhs1 == rhs2)
2902 && crhs2 == lhs))
2903 return ccode == GT_EXPR ? 1 : -1;
2904 break;
2905 case LT_EXPR:
2906 case GE_EXPR:
2907 /* r = a - b; a < r or a >= r
2908 r = a + b; r < a or r >= a or r < b or r >= b. */
2909 if ((code == MINUS_EXPR && crhs1 == rhs1 && crhs2 == lhs)
2910 || (code == PLUS_EXPR && crhs1 == lhs
2911 && (crhs2 == rhs1 || crhs2 == rhs2)))
2912 return ccode == LT_EXPR ? 1 : -1;
2913 break;
2914 default:
2915 break;
2916 }
2917 return 0;
2918}
2919
2920/* Recognize for unsigned x
2921 x = y - z;
2922 if (x > y)
2923 where there are other uses of x and replace it with
2924 _7 = SUB_OVERFLOW (y, z);
2925 x = REALPART_EXPR <_7>;
2926 _8 = IMAGPART_EXPR <_7>;
2927 if (_8)
2928 and similarly for addition. */
2929
2930static bool
2931match_uaddsub_overflow (gimple_stmt_iterator *gsi, gimple *stmt,
2932 enum tree_code code)
2933{
2934 tree lhs = gimple_assign_lhs (stmt);
2935 tree type = TREE_TYPE (lhs);
2936 use_operand_p use_p;
2937 imm_use_iterator iter;
2938 bool use_seen = false;
2939 bool ovf_use_seen = false;
2940 gimple *use_stmt;
2941
2942 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR);
2943 if (!INTEGRAL_TYPE_P (type)
2944 || !TYPE_UNSIGNED (type)
2945 || has_zero_uses (lhs)
2946 || has_single_use (lhs)
2947 || optab_handler (code == PLUS_EXPR ? uaddv4_optab : usubv4_optab,
2948 TYPE_MODE (type)) == CODE_FOR_nothing)
2949 return false;
2950
2951 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
2952 {
2953 use_stmt = USE_STMT (use_p);
2954 if (is_gimple_debug (use_stmt))
2955 continue;
2956
2957 if (uaddsub_overflow_check_p (stmt, use_stmt))
2958 ovf_use_seen = true;
2959 else
2960 use_seen = true;
2961 if (ovf_use_seen && use_seen)
2962 break;
2963 }
2964
2965 if (!ovf_use_seen || !use_seen)
2966 return false;
2967
2968 tree ctype = build_complex_type (type);
2969 tree rhs1 = gimple_assign_rhs1 (stmt);
2970 tree rhs2 = gimple_assign_rhs2 (stmt);
2971 gcall *g = gimple_build_call_internal (code == PLUS_EXPR
2972 ? IFN_ADD_OVERFLOW : IFN_SUB_OVERFLOW,
2973 2, rhs1, rhs2);
2974 tree ctmp = make_ssa_name (ctype);
2975 gimple_call_set_lhs (g, ctmp);
2976 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2977 gassign *g2 = gimple_build_assign (lhs, REALPART_EXPR,
2978 build1 (REALPART_EXPR, type, ctmp));
2979 gsi_replace (gsi, g2, true);
2980 tree ovf = make_ssa_name (type);
2981 g2 = gimple_build_assign (ovf, IMAGPART_EXPR,
2982 build1 (IMAGPART_EXPR, type, ctmp));
2983 gsi_insert_after (gsi, g2, GSI_NEW_STMT);
2984
2985 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
2986 {
2987 if (is_gimple_debug (use_stmt))
2988 continue;
2989
2990 int ovf_use = uaddsub_overflow_check_p (stmt, use_stmt);
2991 if (ovf_use == 0)
2992 continue;
2993 if (gimple_code (use_stmt) == GIMPLE_COND)
2994 {
2995 gcond *cond_stmt = as_a <gcond *> (use_stmt);
2996 gimple_cond_set_lhs (cond_stmt, ovf);
2997 gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0));
2998 gimple_cond_set_code (cond_stmt, ovf_use == 1 ? NE_EXPR : EQ_EXPR);
2999 }
3000 else
3001 {
3002 gcc_checking_assert (is_gimple_assign (use_stmt));
3003 if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
3004 {
3005 gimple_assign_set_rhs1 (use_stmt, ovf);
3006 gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0));
3007 gimple_assign_set_rhs_code (use_stmt,
3008 ovf_use == 1 ? NE_EXPR : EQ_EXPR);
3009 }
3010 else
3011 {
3012 gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
3013 == COND_EXPR);
3014 tree cond = build2 (ovf_use == 1 ? NE_EXPR : EQ_EXPR,
3015 boolean_type_node, ovf,
3016 build_int_cst (type, 0));
3017 gimple_assign_set_rhs1 (use_stmt, cond);
3018 }
3019 }
3020 update_stmt (use_stmt);
3021 }
3022 return true;
3023}
3024
67f7b566 3025/* Return true if target has support for divmod. */
3026
3027static bool
3028target_supports_divmod_p (optab divmod_optab, optab div_optab, machine_mode mode)
3029{
3030 /* If target supports hardware divmod insn, use it for divmod. */
3031 if (optab_handler (divmod_optab, mode) != CODE_FOR_nothing)
3032 return true;
3033
3034 /* Check if libfunc for divmod is available. */
3035 rtx libfunc = optab_libfunc (divmod_optab, mode);
3036 if (libfunc != NULL_RTX)
3037 {
3038 /* If optab_handler exists for div_optab, perhaps in a wider mode,
3039 we don't want to use the libfunc even if it exists for given mode. */
19a4dce4 3040 machine_mode div_mode;
3041 FOR_EACH_MODE_FROM (div_mode, mode)
67f7b566 3042 if (optab_handler (div_optab, div_mode) != CODE_FOR_nothing)
3043 return false;
3044
3045 return targetm.expand_divmod_libfunc != NULL;
3046 }
3047
3048 return false;
3049}
3050
3051/* Check if stmt is candidate for divmod transform. */
3052
3053static bool
3054divmod_candidate_p (gassign *stmt)
3055{
3056 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
582adad1 3057 machine_mode mode = TYPE_MODE (type);
67f7b566 3058 optab divmod_optab, div_optab;
3059
3060 if (TYPE_UNSIGNED (type))
3061 {
3062 divmod_optab = udivmod_optab;
3063 div_optab = udiv_optab;
3064 }
3065 else
3066 {
3067 divmod_optab = sdivmod_optab;
3068 div_optab = sdiv_optab;
3069 }
3070
3071 tree op1 = gimple_assign_rhs1 (stmt);
3072 tree op2 = gimple_assign_rhs2 (stmt);
3073
3074 /* Disable the transform if either is a constant, since division-by-constant
3075 may have specialized expansion. */
3076 if (CONSTANT_CLASS_P (op1) || CONSTANT_CLASS_P (op2))
3077 return false;
3078
3079 /* Exclude the case where TYPE_OVERFLOW_TRAPS (type) as that should
3080 expand using the [su]divv optabs. */
3081 if (TYPE_OVERFLOW_TRAPS (type))
3082 return false;
3083
3084 if (!target_supports_divmod_p (divmod_optab, div_optab, mode))
3085 return false;
3086
3087 return true;
3088}
3089
3090/* This function looks for:
3091 t1 = a TRUNC_DIV_EXPR b;
3092 t2 = a TRUNC_MOD_EXPR b;
3093 and transforms it to the following sequence:
3094 complex_tmp = DIVMOD (a, b);
3095 t1 = REALPART_EXPR(a);
3096 t2 = IMAGPART_EXPR(b);
3097 For conditions enabling the transform see divmod_candidate_p().
3098
3099 The pass has three parts:
3100 1) Find top_stmt which is trunc_div or trunc_mod stmt and dominates all
3101 other trunc_div_expr and trunc_mod_expr stmts.
3102 2) Add top_stmt and all trunc_div and trunc_mod stmts dominated by top_stmt
3103 to stmts vector.
3104 3) Insert DIVMOD call just before top_stmt and update entries in
3105 stmts vector to use return value of DIMOVD (REALEXPR_PART for div,
3106 IMAGPART_EXPR for mod). */
3107
3108static bool
3109convert_to_divmod (gassign *stmt)
3110{
3111 if (stmt_can_throw_internal (stmt)
3112 || !divmod_candidate_p (stmt))
3113 return false;
3114
3115 tree op1 = gimple_assign_rhs1 (stmt);
3116 tree op2 = gimple_assign_rhs2 (stmt);
3117
3118 imm_use_iterator use_iter;
3119 gimple *use_stmt;
3120 auto_vec<gimple *> stmts;
3121
3122 gimple *top_stmt = stmt;
3123 basic_block top_bb = gimple_bb (stmt);
3124
3125 /* Part 1: Try to set top_stmt to "topmost" stmt that dominates
3126 at-least stmt and possibly other trunc_div/trunc_mod stmts
3127 having same operands as stmt. */
3128
3129 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, op1)
3130 {
3131 if (is_gimple_assign (use_stmt)
3132 && (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR
3133 || gimple_assign_rhs_code (use_stmt) == TRUNC_MOD_EXPR)
3134 && operand_equal_p (op1, gimple_assign_rhs1 (use_stmt), 0)
3135 && operand_equal_p (op2, gimple_assign_rhs2 (use_stmt), 0))
3136 {
3137 if (stmt_can_throw_internal (use_stmt))
3138 continue;
3139
3140 basic_block bb = gimple_bb (use_stmt);
3141
3142 if (bb == top_bb)
3143 {
3144 if (gimple_uid (use_stmt) < gimple_uid (top_stmt))
3145 top_stmt = use_stmt;
3146 }
3147 else if (dominated_by_p (CDI_DOMINATORS, top_bb, bb))
3148 {
3149 top_bb = bb;
3150 top_stmt = use_stmt;
3151 }
3152 }
3153 }
3154
3155 tree top_op1 = gimple_assign_rhs1 (top_stmt);
3156 tree top_op2 = gimple_assign_rhs2 (top_stmt);
3157
3158 stmts.safe_push (top_stmt);
3159 bool div_seen = (gimple_assign_rhs_code (top_stmt) == TRUNC_DIV_EXPR);
3160
3161 /* Part 2: Add all trunc_div/trunc_mod statements domianted by top_bb
3162 to stmts vector. The 2nd loop will always add stmt to stmts vector, since
3163 gimple_bb (top_stmt) dominates gimple_bb (stmt), so the
3164 2nd loop ends up adding at-least single trunc_mod_expr stmt. */
3165
3166 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, top_op1)
3167 {
3168 if (is_gimple_assign (use_stmt)
3169 && (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR
3170 || gimple_assign_rhs_code (use_stmt) == TRUNC_MOD_EXPR)
3171 && operand_equal_p (top_op1, gimple_assign_rhs1 (use_stmt), 0)
3172 && operand_equal_p (top_op2, gimple_assign_rhs2 (use_stmt), 0))
3173 {
3174 if (use_stmt == top_stmt
3175 || stmt_can_throw_internal (use_stmt)
3176 || !dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt), top_bb))
3177 continue;
3178
3179 stmts.safe_push (use_stmt);
3180 if (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR)
3181 div_seen = true;
3182 }
3183 }
3184
3185 if (!div_seen)
3186 return false;
3187
3188 /* Part 3: Create libcall to internal fn DIVMOD:
3189 divmod_tmp = DIVMOD (op1, op2). */
3190
3191 gcall *call_stmt = gimple_build_call_internal (IFN_DIVMOD, 2, op1, op2);
3192 tree res = make_temp_ssa_name (build_complex_type (TREE_TYPE (op1)),
3193 call_stmt, "divmod_tmp");
3194 gimple_call_set_lhs (call_stmt, res);
989f02dc 3195 /* We rejected throwing statements above. */
3196 gimple_call_set_nothrow (call_stmt, true);
67f7b566 3197
3198 /* Insert the call before top_stmt. */
3199 gimple_stmt_iterator top_stmt_gsi = gsi_for_stmt (top_stmt);
3200 gsi_insert_before (&top_stmt_gsi, call_stmt, GSI_SAME_STMT);
3201
3202 widen_mul_stats.divmod_calls_inserted++;
3203
3204 /* Update all statements in stmts vector:
3205 lhs = op1 TRUNC_DIV_EXPR op2 -> lhs = REALPART_EXPR<divmod_tmp>
3206 lhs = op1 TRUNC_MOD_EXPR op2 -> lhs = IMAGPART_EXPR<divmod_tmp>. */
3207
3208 for (unsigned i = 0; stmts.iterate (i, &use_stmt); ++i)
3209 {
3210 tree new_rhs;
3211
3212 switch (gimple_assign_rhs_code (use_stmt))
3213 {
3214 case TRUNC_DIV_EXPR:
3215 new_rhs = fold_build1 (REALPART_EXPR, TREE_TYPE (op1), res);
3216 break;
3217
3218 case TRUNC_MOD_EXPR:
3219 new_rhs = fold_build1 (IMAGPART_EXPR, TREE_TYPE (op1), res);
3220 break;
3221
3222 default:
3223 gcc_unreachable ();
3224 }
3225
3226 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
3227 gimple_assign_set_rhs_from_tree (&gsi, new_rhs);
3228 update_stmt (use_stmt);
3229 }
3230
3231 return true;
3232}
e11a63e8 3233
62be004c 3234/* Find integer multiplications where the operands are extended from
3235 smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR
3236 where appropriate. */
3237
65b0537f 3238namespace {
3239
3240const pass_data pass_data_optimize_widening_mul =
3241{
3242 GIMPLE_PASS, /* type */
3243 "widening_mul", /* name */
3244 OPTGROUP_NONE, /* optinfo_flags */
8ed378fe 3245 TV_TREE_WIDEN_MUL, /* tv_id */
65b0537f 3246 PROP_ssa, /* properties_required */
3247 0, /* properties_provided */
3248 0, /* properties_destroyed */
3249 0, /* todo_flags_start */
8b88439e 3250 TODO_update_ssa, /* todo_flags_finish */
65b0537f 3251};
3252
3253class pass_optimize_widening_mul : public gimple_opt_pass
3254{
3255public:
3256 pass_optimize_widening_mul (gcc::context *ctxt)
3257 : gimple_opt_pass (pass_data_optimize_widening_mul, ctxt)
3258 {}
3259
3260 /* opt_pass methods: */
3261 virtual bool gate (function *)
3262 {
3263 return flag_expensive_optimizations && optimize;
3264 }
3265
3266 virtual unsigned int execute (function *);
3267
3268}; // class pass_optimize_widening_mul
3269
3270unsigned int
3271pass_optimize_widening_mul::execute (function *fun)
62be004c 3272{
62be004c 3273 basic_block bb;
15dbdc8f 3274 bool cfg_changed = false;
62be004c 3275
30c4e60d 3276 memset (&widen_mul_stats, 0, sizeof (widen_mul_stats));
67f7b566 3277 calculate_dominance_info (CDI_DOMINATORS);
3278 renumber_gimple_stmt_uids ();
30c4e60d 3279
65b0537f 3280 FOR_EACH_BB_FN (bb, fun)
62be004c 3281 {
3282 gimple_stmt_iterator gsi;
3283
b9be572e 3284 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
62be004c 3285 {
42acab1c 3286 gimple *stmt = gsi_stmt (gsi);
00f4f705 3287 enum tree_code code;
62be004c 3288
b9be572e 3289 if (is_gimple_assign (stmt))
3290 {
3291 code = gimple_assign_rhs_code (stmt);
3292 switch (code)
3293 {
3294 case MULT_EXPR:
aff5fb4d 3295 if (!convert_mult_to_widen (stmt, &gsi)
eb728046 3296 && !convert_expand_mult_copysign (stmt, &gsi)
15dbdc8f 3297 && convert_mult_to_fma (stmt,
3298 gimple_assign_rhs1 (stmt),
3299 gimple_assign_rhs2 (stmt)))
b9be572e 3300 {
3301 gsi_remove (&gsi, true);
3302 release_defs (stmt);
3303 continue;
3304 }
3305 break;
3306
3307 case PLUS_EXPR:
3308 case MINUS_EXPR:
e11a63e8 3309 if (!convert_plusminus_to_widen (&gsi, stmt, code))
3310 match_uaddsub_overflow (&gsi, stmt, code);
b9be572e 3311 break;
62be004c 3312
67f7b566 3313 case TRUNC_MOD_EXPR:
3314 convert_to_divmod (as_a<gassign *> (stmt));
3315 break;
3316
b9be572e 3317 default:;
3318 }
3319 }
d4af184a 3320 else if (is_gimple_call (stmt)
3321 && gimple_call_lhs (stmt))
15dbdc8f 3322 {
3323 tree fndecl = gimple_call_fndecl (stmt);
3324 if (fndecl
8ff377a6 3325 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
15dbdc8f 3326 {
3327 switch (DECL_FUNCTION_CODE (fndecl))
3328 {
3329 case BUILT_IN_POWF:
3330 case BUILT_IN_POW:
3331 case BUILT_IN_POWL:
3332 if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
20cb53c9 3333 && real_equal
3334 (&TREE_REAL_CST (gimple_call_arg (stmt, 1)),
3335 &dconst2)
15dbdc8f 3336 && convert_mult_to_fma (stmt,
3337 gimple_call_arg (stmt, 0),
3338 gimple_call_arg (stmt, 0)))
3339 {
6716f635 3340 unlink_stmt_vdef (stmt);
13ff78a4 3341 if (gsi_remove (&gsi, true)
3342 && gimple_purge_dead_eh_edges (bb))
15dbdc8f 3343 cfg_changed = true;
13ff78a4 3344 release_defs (stmt);
15dbdc8f 3345 continue;
3346 }
3347 break;
3348
3349 default:;
3350 }
3351 }
3352 }
b9be572e 3353 gsi_next (&gsi);
62be004c 3354 }
3355 }
00f4f705 3356
65b0537f 3357 statistics_counter_event (fun, "widening multiplications inserted",
30c4e60d 3358 widen_mul_stats.widen_mults_inserted);
65b0537f 3359 statistics_counter_event (fun, "widening maccs inserted",
30c4e60d 3360 widen_mul_stats.maccs_inserted);
65b0537f 3361 statistics_counter_event (fun, "fused multiply-adds inserted",
30c4e60d 3362 widen_mul_stats.fmas_inserted);
67f7b566 3363 statistics_counter_event (fun, "divmod calls inserted",
3364 widen_mul_stats.divmod_calls_inserted);
30c4e60d 3365
15dbdc8f 3366 return cfg_changed ? TODO_cleanup_cfg : 0;
62be004c 3367}
3368
cbe8bda8 3369} // anon namespace
3370
3371gimple_opt_pass *
3372make_pass_optimize_widening_mul (gcc::context *ctxt)
3373{
3374 return new pass_optimize_widening_mul (ctxt);
3375}