Line data Source code
1 : /* Global, SSA-based optimizations using mathematical identities.
2 : Copyright (C) 2005-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU General Public License as published by the
8 : Free Software Foundation; either version 3, or (at your option) any
9 : later version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT
12 : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /* 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
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
45 : by the same divisor. This is probably because modern processors
46 : can pipeline the divisions; on older, in-order processors it should
47 : still be effective to optimize two divisions by the same number.
48 : We make this a param, and it shall be called N in the remainder of
49 : this comment.
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.cc, an efficient implementation would have
81 : to work on all the variables in a single pass, because we could not
82 : work on just a subset of the dominator tree, as we do now, and the
83 : cost would also be something like O(n_statements * n_basic_blocks).
84 : The data structures would be more complex in order to work on all the
85 : variables in a single pass. */
86 :
87 : #include "config.h"
88 : #include "system.h"
89 : #include "coretypes.h"
90 : #include "backend.h"
91 : #include "target.h"
92 : #include "rtl.h"
93 : #include "tree.h"
94 : #include "gimple.h"
95 : #include "predict.h"
96 : #include "alloc-pool.h"
97 : #include "tree-pass.h"
98 : #include "ssa.h"
99 : #include "optabs-tree.h"
100 : #include "gimple-pretty-print.h"
101 : #include "alias.h"
102 : #include "fold-const.h"
103 : #include "gimple-iterator.h"
104 : #include "gimple-fold.h"
105 : #include "stor-layout.h"
106 : #include "tree-cfg.h"
107 : #include "tree-dfa.h"
108 : #include "tree-ssa.h"
109 : #include "builtins.h"
110 : #include "internal-fn.h"
111 : #include "case-cfn-macros.h"
112 : #include "optabs-libfuncs.h"
113 : #include "tree-eh.h"
114 : #include "targhooks.h"
115 : #include "domwalk.h"
116 : #include "tree-ssa-math-opts.h"
117 : #include "dbgcnt.h"
118 : #include "langhooks.h"
119 : #include "cfghooks.h"
120 :
121 : /* This structure represents one basic block that either computes a
122 : division, or is a common dominator for basic block that compute a
123 : division. */
124 : struct occurrence {
125 : /* The basic block represented by this structure. */
126 : basic_block bb = basic_block();
127 :
128 : /* If non-NULL, the SSA_NAME holding the definition for a reciprocal
129 : inserted in BB. */
130 : tree recip_def = tree();
131 :
132 : /* If non-NULL, the SSA_NAME holding the definition for a squared
133 : reciprocal inserted in BB. */
134 : tree square_recip_def = tree();
135 :
136 : /* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that
137 : was inserted in BB. */
138 : gimple *recip_def_stmt = nullptr;
139 :
140 : /* Pointer to a list of "struct occurrence"s for blocks dominated
141 : by BB. */
142 : struct occurrence *children = nullptr;
143 :
144 : /* Pointer to the next "struct occurrence"s in the list of blocks
145 : sharing a common dominator. */
146 : struct occurrence *next = nullptr;
147 :
148 : /* The number of divisions that are in BB before compute_merit. The
149 : number of divisions that are in BB or post-dominate it after
150 : compute_merit. */
151 : int num_divisions = 0;
152 :
153 : /* True if the basic block has a division, false if it is a common
154 : dominator for basic blocks that do. If it is false and trapping
155 : math is active, BB is not a candidate for inserting a reciprocal. */
156 : bool bb_has_division = false;
157 :
158 : /* Construct a struct occurrence for basic block BB, and whose
159 : children list is headed by CHILDREN. */
160 627 : occurrence (basic_block bb, struct occurrence *children)
161 627 : : bb (bb), children (children)
162 : {
163 627 : bb->aux = this;
164 : }
165 :
166 : /* Destroy a struct occurrence and remove it from its basic block. */
167 627 : ~occurrence ()
168 : {
169 627 : bb->aux = nullptr;
170 627 : }
171 :
172 : /* Allocate memory for a struct occurrence from OCC_POOL. */
173 : static void* operator new (size_t);
174 :
175 : /* Return memory for a struct occurrence to OCC_POOL. */
176 : static void operator delete (void*, size_t);
177 : };
178 :
179 : static struct
180 : {
181 : /* Number of 1.0/X ops inserted. */
182 : int rdivs_inserted;
183 :
184 : /* Number of 1.0/FUNC ops inserted. */
185 : int rfuncs_inserted;
186 : } reciprocal_stats;
187 :
188 : static struct
189 : {
190 : /* Number of cexpi calls inserted. */
191 : int inserted;
192 :
193 : /* Number of conversions removed. */
194 : int conv_removed;
195 :
196 : } sincos_stats;
197 :
198 : static struct
199 : {
200 : /* Number of widening multiplication ops inserted. */
201 : int widen_mults_inserted;
202 :
203 : /* Number of integer multiply-and-accumulate ops inserted. */
204 : int maccs_inserted;
205 :
206 : /* Number of fp fused multiply-add ops inserted. */
207 : int fmas_inserted;
208 :
209 : /* Number of divmod calls inserted. */
210 : int divmod_calls_inserted;
211 :
212 : /* Number of highpart multiplication ops inserted. */
213 : int highpart_mults_inserted;
214 : } widen_mul_stats;
215 :
216 : /* The instance of "struct occurrence" representing the highest
217 : interesting block in the dominator tree. */
218 : static struct occurrence *occ_head;
219 :
220 : /* Allocation pool for getting instances of "struct occurrence". */
221 : static object_allocator<occurrence> *occ_pool;
222 :
223 627 : void* occurrence::operator new (size_t n)
224 : {
225 627 : gcc_assert (n == sizeof(occurrence));
226 627 : return occ_pool->allocate_raw ();
227 : }
228 :
229 627 : void occurrence::operator delete (void *occ, size_t n)
230 : {
231 627 : gcc_assert (n == sizeof(occurrence));
232 627 : occ_pool->remove_raw (occ);
233 627 : }
234 :
235 : /* Insert NEW_OCC into our subset of the dominator tree. P_HEAD points to a
236 : list of "struct occurrence"s, one per basic block, having IDOM as
237 : their common dominator.
238 :
239 : We try to insert NEW_OCC as deep as possible in the tree, and we also
240 : insert any other block that is a common dominator for BB and one
241 : block already in the tree. */
242 :
243 : static void
244 616 : insert_bb (struct occurrence *new_occ, basic_block idom,
245 : struct occurrence **p_head)
246 : {
247 621 : struct occurrence *occ, **p_occ;
248 :
249 644 : for (p_occ = p_head; (occ = *p_occ) != NULL; )
250 : {
251 28 : basic_block bb = new_occ->bb, occ_bb = occ->bb;
252 28 : basic_block dom = nearest_common_dominator (CDI_DOMINATORS, occ_bb, bb);
253 28 : if (dom == bb)
254 : {
255 : /* BB dominates OCC_BB. OCC becomes NEW_OCC's child: remove OCC
256 : from its list. */
257 6 : *p_occ = occ->next;
258 6 : occ->next = new_occ->children;
259 6 : new_occ->children = occ;
260 :
261 : /* Try the next block (it may as well be dominated by BB). */
262 : }
263 :
264 22 : else if (dom == occ_bb)
265 : {
266 : /* OCC_BB dominates BB. Tail recurse to look deeper. */
267 5 : insert_bb (new_occ, dom, &occ->children);
268 5 : return;
269 : }
270 :
271 17 : else if (dom != idom)
272 : {
273 11 : gcc_assert (!dom->aux);
274 :
275 : /* There is a dominator between IDOM and BB, add it and make
276 : two children out of NEW_OCC and OCC. First, remove OCC from
277 : its list. */
278 11 : *p_occ = occ->next;
279 11 : new_occ->next = occ;
280 11 : occ->next = NULL;
281 :
282 : /* None of the previous blocks has DOM as a dominator: if we tail
283 : recursed, we would reexamine them uselessly. Just switch BB with
284 : DOM, and go on looking for blocks dominated by DOM. */
285 11 : new_occ = new occurrence (dom, new_occ);
286 : }
287 :
288 : else
289 : {
290 : /* Nothing special, go on with the next element. */
291 6 : p_occ = &occ->next;
292 : }
293 : }
294 :
295 : /* No place was found as a child of IDOM. Make BB a sibling of IDOM. */
296 616 : new_occ->next = *p_head;
297 616 : *p_head = new_occ;
298 : }
299 :
300 : /* Register that we found a division in BB.
301 : IMPORTANCE is a measure of how much weighting to give
302 : that division. Use IMPORTANCE = 2 to register a single
303 : division. If the division is going to be found multiple
304 : times use 1 (as it is with squares). */
305 :
306 : static inline void
307 719 : register_division_in (basic_block bb, int importance)
308 : {
309 719 : struct occurrence *occ;
310 :
311 719 : occ = (struct occurrence *) bb->aux;
312 719 : if (!occ)
313 : {
314 616 : occ = new occurrence (bb, NULL);
315 616 : insert_bb (occ, ENTRY_BLOCK_PTR_FOR_FN (cfun), &occ_head);
316 : }
317 :
318 719 : occ->bb_has_division = true;
319 719 : occ->num_divisions += importance;
320 719 : }
321 :
322 :
323 : /* Compute the number of divisions that postdominate each block in OCC and
324 : its children. */
325 :
326 : static void
327 26 : compute_merit (struct occurrence *occ)
328 : {
329 26 : struct occurrence *occ_child;
330 26 : basic_block dom = occ->bb;
331 :
332 45 : for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
333 : {
334 19 : basic_block bb;
335 19 : if (occ_child->children)
336 3 : compute_merit (occ_child);
337 :
338 19 : if (flag_exceptions)
339 6 : bb = single_noncomplex_succ (dom);
340 : else
341 : bb = dom;
342 :
343 19 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, occ_child->bb))
344 12 : occ->num_divisions += occ_child->num_divisions;
345 : }
346 26 : }
347 :
348 :
349 : /* Return whether USE_STMT is a floating-point division by DEF. */
350 : static inline bool
351 356819 : is_division_by (gimple *use_stmt, tree def)
352 : {
353 356819 : auto assign = dyn_cast<gassign *> (use_stmt);
354 246170 : return assign
355 246170 : && gimple_assign_rhs_code (assign) == RDIV_EXPR
356 1261 : && gimple_assign_rhs2 (assign) == def
357 : /* Do not recognize x / x as valid division, as we are getting
358 : confused later by replacing all immediate uses x in such
359 : a stmt. */
360 889 : && gimple_assign_rhs1 (assign) != def
361 889 : && !stmt_can_throw_internal (cfun, assign);
362 : }
363 :
364 : /* Return TRUE if USE_STMT is a multiplication of DEF by A. */
365 : static inline bool
366 352499 : is_mult_by (gimple *use_stmt, tree def, tree a)
367 : {
368 352499 : auto assign = dyn_cast<gassign *> (use_stmt);
369 275352 : if (assign && gimple_assign_rhs_code (assign) == MULT_EXPR)
370 : {
371 80644 : tree op0 = gimple_assign_rhs1 (assign);
372 80644 : tree op1 = gimple_assign_rhs2 (assign);
373 :
374 80644 : return (op0 == def && op1 == a)
375 80644 : || (op0 == a && op1 == def);
376 : }
377 : return 0;
378 : }
379 :
380 : /* Return whether USE_STMT is DEF * DEF. */
381 : static inline bool
382 352454 : is_square_of (gimple *use_stmt, tree def)
383 : {
384 352454 : return is_mult_by (use_stmt, def, def);
385 : }
386 :
387 : /* Return whether USE_STMT is a floating-point division by
388 : DEF * DEF. */
389 : static inline bool
390 192 : is_division_by_square (gimple *use_stmt, tree def)
391 : {
392 192 : auto assign = dyn_cast<gassign *> (use_stmt);
393 185 : if (assign
394 185 : && gimple_assign_rhs_code (assign) == RDIV_EXPR
395 14 : && gimple_assign_rhs1 (assign) != gimple_assign_rhs2 (assign)
396 7 : && !stmt_can_throw_internal (cfun, assign))
397 : {
398 7 : tree denominator = gimple_assign_rhs2 (assign);
399 7 : if (TREE_CODE (denominator) == SSA_NAME)
400 7 : return is_square_of (SSA_NAME_DEF_STMT (denominator), def);
401 : }
402 : return 0;
403 : }
404 :
405 : /* Walk the subset of the dominator tree rooted at OCC, setting the
406 : RECIP_DEF field to a definition of 1.0 / DEF that can be used in
407 : the given basic block. The field may be left NULL, of course,
408 : if it is not possible or profitable to do the optimization.
409 :
410 : DEF_BSI is an iterator pointing at the statement defining DEF.
411 : If RECIP_DEF is set, a dominator already has a computation that can
412 : be used.
413 :
414 : If should_insert_square_recip is set, then this also inserts
415 : the square of the reciprocal immediately after the definition
416 : of the reciprocal. */
417 :
418 : static void
419 42 : insert_reciprocals (gimple_stmt_iterator *def_gsi, struct occurrence *occ,
420 : tree def, tree recip_def, tree square_recip_def,
421 : int should_insert_square_recip, int threshold)
422 : {
423 42 : tree type;
424 42 : gassign *new_stmt, *new_square_stmt;
425 42 : gimple_stmt_iterator gsi;
426 42 : struct occurrence *occ_child;
427 :
428 42 : if (!recip_def
429 26 : && (occ->bb_has_division || !flag_trapping_math)
430 : /* Divide by two as all divisions are counted twice in
431 : the costing loop. */
432 25 : && occ->num_divisions / 2 >= threshold)
433 : {
434 : /* Make a variable with the replacement and substitute it. */
435 24 : type = TREE_TYPE (def);
436 24 : recip_def = create_tmp_reg (type, "reciptmp");
437 24 : new_stmt = gimple_build_assign (recip_def, RDIV_EXPR,
438 : build_one_cst (type), def);
439 :
440 24 : if (should_insert_square_recip)
441 : {
442 4 : square_recip_def = create_tmp_reg (type, "powmult_reciptmp");
443 4 : new_square_stmt = gimple_build_assign (square_recip_def, MULT_EXPR,
444 : recip_def, recip_def);
445 : }
446 :
447 24 : if (occ->bb_has_division)
448 : {
449 : /* Case 1: insert before an existing division. */
450 21 : gsi = gsi_after_labels (occ->bb);
451 212 : while (!gsi_end_p (gsi)
452 212 : && (!is_division_by (gsi_stmt (gsi), def))
453 404 : && (!is_division_by_square (gsi_stmt (gsi), def)))
454 191 : gsi_next (&gsi);
455 :
456 21 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
457 21 : if (should_insert_square_recip)
458 3 : gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT);
459 : }
460 3 : else if (def_gsi && occ->bb == gsi_bb (*def_gsi))
461 : {
462 : /* Case 2: insert right after the definition. Note that this will
463 : never happen if the definition statement can throw, because in
464 : that case the sole successor of the statement's basic block will
465 : dominate all the uses as well. */
466 2 : gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT);
467 2 : if (should_insert_square_recip)
468 1 : gsi_insert_after (def_gsi, new_square_stmt, GSI_NEW_STMT);
469 : }
470 : else
471 : {
472 : /* Case 3: insert in a basic block not containing defs/uses. */
473 1 : gsi = gsi_after_labels (occ->bb);
474 1 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
475 1 : if (should_insert_square_recip)
476 0 : gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT);
477 : }
478 :
479 24 : reciprocal_stats.rdivs_inserted++;
480 :
481 24 : occ->recip_def_stmt = new_stmt;
482 : }
483 :
484 42 : occ->recip_def = recip_def;
485 42 : occ->square_recip_def = square_recip_def;
486 61 : for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
487 19 : insert_reciprocals (def_gsi, occ_child, def, recip_def,
488 : square_recip_def, should_insert_square_recip,
489 : threshold);
490 42 : }
491 :
492 : /* Replace occurrences of expr / (x * x) with expr * ((1 / x) * (1 / x)).
493 : Take as argument the use for (x * x). */
494 : static inline void
495 4 : replace_reciprocal_squares (use_operand_p use_p)
496 : {
497 4 : gimple *use_stmt = USE_STMT (use_p);
498 4 : basic_block bb = gimple_bb (use_stmt);
499 4 : struct occurrence *occ = (struct occurrence *) bb->aux;
500 :
501 8 : if (optimize_bb_for_speed_p (bb) && occ->square_recip_def
502 8 : && occ->recip_def)
503 : {
504 4 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
505 4 : gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
506 4 : gimple_assign_set_rhs2 (use_stmt, occ->square_recip_def);
507 4 : SET_USE (use_p, occ->square_recip_def);
508 4 : fold_stmt_inplace (&gsi);
509 4 : update_stmt (use_stmt);
510 : }
511 4 : }
512 :
513 :
514 : /* Replace the division at USE_P with a multiplication by the reciprocal, if
515 : possible. */
516 :
517 : static inline void
518 105 : replace_reciprocal (use_operand_p use_p)
519 : {
520 105 : gimple *use_stmt = USE_STMT (use_p);
521 105 : basic_block bb = gimple_bb (use_stmt);
522 105 : struct occurrence *occ = (struct occurrence *) bb->aux;
523 :
524 105 : if (optimize_bb_for_speed_p (bb)
525 105 : && occ->recip_def && use_stmt != occ->recip_def_stmt)
526 : {
527 80 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
528 80 : gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
529 80 : SET_USE (use_p, occ->recip_def);
530 80 : fold_stmt_inplace (&gsi);
531 80 : update_stmt (use_stmt);
532 : }
533 105 : }
534 :
535 :
536 : /* Free OCC and return one more "struct occurrence" to be freed. */
537 :
538 : static struct occurrence *
539 627 : free_bb (struct occurrence *occ)
540 : {
541 627 : struct occurrence *child, *next;
542 :
543 : /* First get the two pointers hanging off OCC. */
544 627 : next = occ->next;
545 627 : child = occ->children;
546 627 : delete occ;
547 :
548 : /* Now ensure that we don't recurse unless it is necessary. */
549 627 : if (!child)
550 : return next;
551 : else
552 : {
553 19 : while (next)
554 0 : next = free_bb (next);
555 :
556 : return child;
557 : }
558 : }
559 :
560 : /* Transform sequences like
561 : t = sqrt (a)
562 : x = 1.0 / t;
563 : r1 = x * x;
564 : r2 = a * x;
565 : into:
566 : t = sqrt (a)
567 : r1 = 1.0 / a;
568 : r2 = t;
569 : x = r1 * r2;
570 : depending on the uses of x, r1, r2. This removes one multiplication and
571 : allows the sqrt and division operations to execute in parallel.
572 : DEF_GSI is the gsi of the initial division by sqrt that defines
573 : DEF (x in the example above). */
574 :
575 : static void
576 564 : optimize_recip_sqrt (gimple_stmt_iterator *def_gsi, tree def)
577 : {
578 564 : gimple *use_stmt;
579 564 : imm_use_iterator use_iter;
580 564 : gimple *stmt = gsi_stmt (*def_gsi);
581 564 : tree x = def;
582 564 : tree orig_sqrt_ssa_name = gimple_assign_rhs2 (stmt);
583 564 : tree div_rhs1 = gimple_assign_rhs1 (stmt);
584 :
585 564 : if (TREE_CODE (orig_sqrt_ssa_name) != SSA_NAME
586 559 : || TREE_CODE (div_rhs1) != REAL_CST
587 753 : || !real_equal (&TREE_REAL_CST (div_rhs1), &dconst1))
588 552 : return;
589 :
590 113 : gcall *sqrt_stmt
591 594 : = dyn_cast <gcall *> (SSA_NAME_DEF_STMT (orig_sqrt_ssa_name));
592 :
593 42 : if (!sqrt_stmt || !gimple_call_lhs (sqrt_stmt))
594 : return;
595 :
596 42 : switch (gimple_call_combined_fn (sqrt_stmt))
597 : {
598 31 : CASE_CFN_SQRT:
599 31 : CASE_CFN_SQRT_FN:
600 31 : break;
601 :
602 : default:
603 : return;
604 : }
605 31 : tree a = gimple_call_arg (sqrt_stmt, 0);
606 :
607 : /* We have 'a' and 'x'. Now analyze the uses of 'x'. */
608 :
609 : /* Statements that use x in x * x. */
610 43 : auto_vec<gimple *> sqr_stmts;
611 : /* Statements that use x in a * x. */
612 12 : auto_vec<gimple *> mult_stmts;
613 31 : bool has_other_use = false;
614 31 : bool mult_on_main_path = false;
615 :
616 89 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, x)
617 : {
618 58 : if (is_gimple_debug (use_stmt))
619 1 : continue;
620 57 : if (is_square_of (use_stmt, x))
621 : {
622 12 : sqr_stmts.safe_push (use_stmt);
623 12 : if (gimple_bb (use_stmt) == gimple_bb (stmt))
624 58 : mult_on_main_path = true;
625 : }
626 45 : else if (is_mult_by (use_stmt, x, a))
627 : {
628 14 : mult_stmts.safe_push (use_stmt);
629 14 : if (gimple_bb (use_stmt) == gimple_bb (stmt))
630 58 : mult_on_main_path = true;
631 : }
632 : else
633 : has_other_use = true;
634 31 : }
635 :
636 : /* In the x * x and a * x cases we just rewire stmt operands or
637 : remove multiplications. In the has_other_use case we introduce
638 : a multiplication so make sure we don't introduce a multiplication
639 : on a path where there was none. */
640 31 : if (has_other_use && !mult_on_main_path)
641 19 : return;
642 :
643 12 : if (sqr_stmts.is_empty () && mult_stmts.is_empty ())
644 : return;
645 :
646 : /* If x = 1.0 / sqrt (a) has uses other than those optimized here we want
647 : to be able to compose it from the sqr and mult cases. */
648 41 : if (has_other_use && (sqr_stmts.is_empty () || mult_stmts.is_empty ()))
649 : return;
650 :
651 12 : if (dump_file)
652 : {
653 10 : fprintf (dump_file, "Optimizing reciprocal sqrt multiplications of\n");
654 10 : print_gimple_stmt (dump_file, sqrt_stmt, 0, TDF_NONE);
655 10 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
656 10 : fprintf (dump_file, "\n");
657 : }
658 :
659 12 : bool delete_div = !has_other_use;
660 12 : tree sqr_ssa_name = NULL_TREE;
661 22 : if (!sqr_stmts.is_empty ())
662 : {
663 : /* r1 = x * x. Transform the original
664 : x = 1.0 / t
665 : into
666 : tmp1 = 1.0 / a
667 : r1 = tmp1. */
668 :
669 10 : sqr_ssa_name
670 10 : = make_temp_ssa_name (TREE_TYPE (a), NULL, "recip_sqrt_sqr");
671 :
672 10 : if (dump_file)
673 : {
674 10 : fprintf (dump_file, "Replacing original division\n");
675 10 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
676 10 : fprintf (dump_file, "with new division\n");
677 : }
678 10 : stmt
679 10 : = gimple_build_assign (sqr_ssa_name, gimple_assign_rhs_code (stmt),
680 : gimple_assign_rhs1 (stmt), a);
681 10 : gsi_insert_before (def_gsi, stmt, GSI_SAME_STMT);
682 10 : gsi_remove (def_gsi, true);
683 10 : *def_gsi = gsi_for_stmt (stmt);
684 10 : fold_stmt_inplace (def_gsi);
685 10 : update_stmt (stmt);
686 :
687 10 : if (dump_file)
688 10 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
689 :
690 10 : delete_div = false;
691 10 : gimple *sqr_stmt;
692 10 : unsigned int i;
693 20 : FOR_EACH_VEC_ELT (sqr_stmts, i, sqr_stmt)
694 : {
695 10 : gimple_stmt_iterator gsi2 = gsi_for_stmt (sqr_stmt);
696 10 : gimple_assign_set_rhs_from_tree (&gsi2, sqr_ssa_name);
697 10 : update_stmt (sqr_stmt);
698 : }
699 : }
700 24 : if (!mult_stmts.is_empty ())
701 : {
702 : /* r2 = a * x. Transform this into:
703 : r2 = t (The original sqrt (a)). */
704 : unsigned int i;
705 24 : gimple *mult_stmt = NULL;
706 24 : FOR_EACH_VEC_ELT (mult_stmts, i, mult_stmt)
707 : {
708 12 : gimple_stmt_iterator gsi2 = gsi_for_stmt (mult_stmt);
709 :
710 12 : if (dump_file)
711 : {
712 10 : fprintf (dump_file, "Replacing squaring multiplication\n");
713 10 : print_gimple_stmt (dump_file, mult_stmt, 0, TDF_NONE);
714 10 : fprintf (dump_file, "with assignment\n");
715 : }
716 12 : gimple_assign_set_rhs_from_tree (&gsi2, orig_sqrt_ssa_name);
717 12 : fold_stmt_inplace (&gsi2);
718 12 : update_stmt (mult_stmt);
719 12 : if (dump_file)
720 10 : print_gimple_stmt (dump_file, mult_stmt, 0, TDF_NONE);
721 : }
722 : }
723 :
724 12 : if (has_other_use)
725 : {
726 : /* Using the two temporaries tmp1, tmp2 from above
727 : the original x is now:
728 : x = tmp1 * tmp2. */
729 10 : gcc_assert (orig_sqrt_ssa_name);
730 10 : gcc_assert (sqr_ssa_name);
731 :
732 10 : gimple *new_stmt
733 10 : = gimple_build_assign (x, MULT_EXPR,
734 : orig_sqrt_ssa_name, sqr_ssa_name);
735 10 : gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT);
736 10 : update_stmt (stmt);
737 : }
738 2 : else if (delete_div)
739 : {
740 : /* Remove the original division. */
741 2 : gimple_stmt_iterator gsi2 = gsi_for_stmt (stmt);
742 2 : gsi_remove (&gsi2, true);
743 2 : release_defs (stmt);
744 : }
745 : else
746 0 : release_ssa_name (x);
747 : }
748 :
749 : /* Look for floating-point divisions among DEF's uses, and try to
750 : replace them by multiplications with the reciprocal. Add
751 : as many statements computing the reciprocal as needed.
752 :
753 : DEF must be a GIMPLE register of a floating-point type. */
754 :
755 : static void
756 215737 : execute_cse_reciprocals_1 (gimple_stmt_iterator *def_gsi, tree def)
757 : {
758 215737 : use_operand_p use_p, square_use_p;
759 215737 : imm_use_iterator use_iter, square_use_iter;
760 215737 : tree square_def;
761 215737 : struct occurrence *occ;
762 215737 : int count = 0;
763 215737 : int threshold;
764 215737 : int square_recip_count = 0;
765 215737 : int sqrt_recip_count = 0;
766 :
767 215737 : gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def)) && TREE_CODE (def) == SSA_NAME);
768 215737 : threshold = targetm.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def)));
769 :
770 : /* If DEF is a square (x * x), count the number of divisions by x.
771 : If there are more divisions by x than by (DEF * DEF), prefer to optimize
772 : the reciprocal of x instead of DEF. This improves cases like:
773 : def = x * x
774 : t0 = a / def
775 : t1 = b / def
776 : t2 = c / x
777 : Reciprocal optimization of x results in 1 division rather than 2 or 3. */
778 215737 : auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (def));
779 :
780 168746 : if (def_stmt
781 168746 : && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
782 40881 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
783 81608 : && gimple_assign_rhs1 (def_stmt) == gimple_assign_rhs2 (def_stmt))
784 : {
785 758 : tree op0 = gimple_assign_rhs1 (def_stmt);
786 :
787 3042 : FOR_EACH_IMM_USE_FAST (use_p, use_iter, op0)
788 : {
789 2284 : gimple *use_stmt = USE_STMT (use_p);
790 2284 : if (is_division_by (use_stmt, op0))
791 17 : sqrt_recip_count++;
792 758 : }
793 : }
794 :
795 568122 : FOR_EACH_IMM_USE_FAST (use_p, use_iter, def)
796 : {
797 352385 : gimple *use_stmt = USE_STMT (use_p);
798 352385 : if (is_division_by (use_stmt, def))
799 : {
800 645 : register_division_in (gimple_bb (use_stmt), 2);
801 645 : count++;
802 : }
803 :
804 352385 : if (is_square_of (use_stmt, def))
805 : {
806 1524 : square_def = gimple_assign_lhs (use_stmt);
807 3334 : FOR_EACH_IMM_USE_FAST (square_use_p, square_use_iter, square_def)
808 : {
809 1810 : gimple *square_use_stmt = USE_STMT (square_use_p);
810 1810 : if (is_division_by (square_use_stmt, square_def))
811 : {
812 : /* This is executed twice for each division by a square. */
813 74 : register_division_in (gimple_bb (square_use_stmt), 1);
814 74 : square_recip_count++;
815 : }
816 1524 : }
817 : }
818 215737 : }
819 :
820 : /* Square reciprocals were counted twice above. */
821 215737 : square_recip_count /= 2;
822 :
823 : /* If it is more profitable to optimize 1 / x, don't optimize 1 / (x * x). */
824 215737 : if (sqrt_recip_count > square_recip_count)
825 17 : goto out;
826 :
827 : /* Do the expensive part only if we can hope to optimize something. */
828 215720 : if (count + square_recip_count >= threshold && count >= 1)
829 : {
830 23 : gimple *use_stmt;
831 46 : for (occ = occ_head; occ; occ = occ->next)
832 : {
833 23 : compute_merit (occ);
834 23 : insert_reciprocals (def_gsi, occ, def, NULL, NULL,
835 : square_recip_count, threshold);
836 : }
837 :
838 143 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
839 : {
840 120 : if (is_division_by (use_stmt, def))
841 : {
842 210 : FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
843 105 : replace_reciprocal (use_p);
844 : }
845 15 : else if (square_recip_count > 0 && is_square_of (use_stmt, def))
846 : {
847 12 : FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
848 : {
849 : /* Find all uses of the square that are divisions and
850 : * replace them by multiplications with the inverse. */
851 8 : imm_use_iterator square_iterator;
852 8 : gimple *powmult_use_stmt = USE_STMT (use_p);
853 8 : tree powmult_def_name = gimple_assign_lhs (powmult_use_stmt);
854 :
855 16 : FOR_EACH_IMM_USE_STMT (powmult_use_stmt,
856 : square_iterator, powmult_def_name)
857 16 : FOR_EACH_IMM_USE_ON_STMT (square_use_p, square_iterator)
858 : {
859 8 : gimple *powmult_use_stmt = USE_STMT (square_use_p);
860 8 : if (is_division_by (powmult_use_stmt, powmult_def_name))
861 4 : replace_reciprocal_squares (square_use_p);
862 8 : }
863 : }
864 : }
865 23 : }
866 : }
867 :
868 215697 : out:
869 216364 : for (occ = occ_head; occ; )
870 627 : occ = free_bb (occ);
871 :
872 215737 : occ_head = NULL;
873 215737 : }
874 :
875 : /* Return an internal function that implements the reciprocal of CALL,
876 : or IFN_LAST if there is no such function that the target supports. */
877 :
878 : internal_fn
879 113 : internal_fn_reciprocal (gcall *call)
880 : {
881 113 : internal_fn ifn;
882 :
883 113 : switch (gimple_call_combined_fn (call))
884 : {
885 97 : CASE_CFN_SQRT:
886 97 : CASE_CFN_SQRT_FN:
887 97 : ifn = IFN_RSQRT;
888 97 : break;
889 :
890 : default:
891 : return IFN_LAST;
892 : }
893 :
894 97 : tree_pair types = direct_internal_fn_types (ifn, call);
895 97 : if (!direct_internal_fn_supported_p (ifn, types, OPTIMIZE_FOR_SPEED))
896 44 : return IFN_LAST;
897 :
898 : return ifn;
899 : }
900 :
901 : /* Go through all the floating-point SSA_NAMEs, and call
902 : execute_cse_reciprocals_1 on each of them. */
903 : namespace {
904 :
905 : const pass_data pass_data_cse_reciprocals =
906 : {
907 : GIMPLE_PASS, /* type */
908 : "recip", /* name */
909 : OPTGROUP_NONE, /* optinfo_flags */
910 : TV_TREE_RECIP, /* tv_id */
911 : PROP_ssa, /* properties_required */
912 : 0, /* properties_provided */
913 : 0, /* properties_destroyed */
914 : 0, /* todo_flags_start */
915 : TODO_update_ssa, /* todo_flags_finish */
916 : };
917 :
918 : class pass_cse_reciprocals : public gimple_opt_pass
919 : {
920 : public:
921 302985 : pass_cse_reciprocals (gcc::context *ctxt)
922 605970 : : gimple_opt_pass (pass_data_cse_reciprocals, ctxt)
923 : {}
924 :
925 : /* opt_pass methods: */
926 1068301 : bool gate (function *) final override
927 : {
928 1068301 : return optimize && flag_reciprocal_math;
929 : }
930 : unsigned int execute (function *) final override;
931 :
932 : }; // class pass_cse_reciprocals
933 :
934 : unsigned int
935 9082 : pass_cse_reciprocals::execute (function *fun)
936 : {
937 9082 : basic_block bb;
938 9082 : tree arg;
939 :
940 9082 : occ_pool = new object_allocator<occurrence> ("dominators for recip");
941 :
942 9082 : memset (&reciprocal_stats, 0, sizeof (reciprocal_stats));
943 9082 : calculate_dominance_info (CDI_DOMINATORS);
944 9082 : calculate_dominance_info (CDI_POST_DOMINATORS);
945 :
946 9082 : if (flag_checking)
947 95782 : FOR_EACH_BB_FN (bb, fun)
948 86700 : gcc_assert (!bb->aux);
949 :
950 22228 : for (arg = DECL_ARGUMENTS (fun->decl); arg; arg = DECL_CHAIN (arg))
951 20908 : if (FLOAT_TYPE_P (TREE_TYPE (arg))
952 14233 : && is_gimple_reg (arg))
953 : {
954 6470 : tree name = ssa_default_def (fun, arg);
955 6470 : if (name)
956 5475 : execute_cse_reciprocals_1 (NULL, name);
957 : }
958 :
959 95782 : FOR_EACH_BB_FN (bb, fun)
960 : {
961 86700 : tree def;
962 :
963 198293 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
964 111593 : gsi_next (&gsi))
965 : {
966 111593 : gphi *phi = gsi.phi ();
967 111593 : def = PHI_RESULT (phi);
968 111593 : if (! virtual_operand_p (def)
969 111593 : && FLOAT_TYPE_P (TREE_TYPE (def)))
970 30936 : execute_cse_reciprocals_1 (NULL, def);
971 : }
972 :
973 1384386 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
974 1297686 : gsi_next (&gsi))
975 : {
976 1297686 : gimple *stmt = gsi_stmt (gsi);
977 :
978 2595372 : if (gimple_has_lhs (stmt)
979 814687 : && (def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF)) != NULL
980 774038 : && FLOAT_TYPE_P (TREE_TYPE (def))
981 202698 : && TREE_CODE (def) == SSA_NAME)
982 : {
983 179326 : execute_cse_reciprocals_1 (&gsi, def);
984 179326 : auto assign = dyn_cast< gassign *> (gsi_stmt (gsi));
985 179326 : if (flag_unsafe_math_optimizations
986 179281 : && assign
987 168703 : && gimple_assign_lhs (assign) == def
988 168701 : && !stmt_can_throw_internal (cfun, assign)
989 347983 : && gimple_assign_rhs_code (assign) == RDIV_EXPR)
990 564 : optimize_recip_sqrt (&gsi, def);
991 : }
992 : }
993 :
994 86700 : if (optimize_bb_for_size_p (bb))
995 5412 : continue;
996 :
997 : /* Scan for a/func(b) and convert it to reciprocal a*rfunc(b). */
998 1356810 : for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi);
999 1275522 : gsi_next (&gsi))
1000 : {
1001 1275522 : gimple *stmt = gsi_stmt (gsi);
1002 :
1003 1275522 : if (is_gimple_assign (stmt)
1004 1275522 : && gimple_assign_rhs_code (stmt) == RDIV_EXPR)
1005 : {
1006 606 : tree arg1 = gimple_assign_rhs2 (stmt);
1007 606 : gimple *stmt1;
1008 :
1009 606 : if (TREE_CODE (arg1) != SSA_NAME)
1010 5 : continue;
1011 :
1012 601 : stmt1 = SSA_NAME_DEF_STMT (arg1);
1013 :
1014 601 : if (is_gimple_call (stmt1)
1015 601 : && gimple_call_lhs (stmt1))
1016 : {
1017 113 : bool fail;
1018 113 : imm_use_iterator ui;
1019 113 : use_operand_p use_p;
1020 113 : tree fndecl = NULL_TREE;
1021 :
1022 113 : gcall *call = as_a <gcall *> (stmt1);
1023 113 : internal_fn ifn = internal_fn_reciprocal (call);
1024 113 : if (ifn == IFN_LAST)
1025 : {
1026 60 : fndecl = gimple_call_fndecl (call);
1027 120 : if (!fndecl
1028 60 : || !fndecl_built_in_p (fndecl, BUILT_IN_MD))
1029 62 : continue;
1030 0 : fndecl = targetm.builtin_reciprocal (fndecl);
1031 0 : if (!fndecl)
1032 0 : continue;
1033 : }
1034 :
1035 : /* Check that all uses of the SSA name are divisions,
1036 : otherwise replacing the defining statement will do
1037 : the wrong thing. */
1038 53 : fail = false;
1039 106 : FOR_EACH_IMM_USE_FAST (use_p, ui, arg1)
1040 : {
1041 55 : if (is_gimple_debug (USE_STMT (use_p)))
1042 0 : continue;
1043 :
1044 108 : auto *stmt2 = dyn_cast<gassign *> (USE_STMT (use_p));
1045 55 : if (!stmt2
1046 55 : || gimple_assign_rhs_code (stmt2) != RDIV_EXPR
1047 53 : || gimple_assign_rhs1 (stmt2) == arg1
1048 53 : || gimple_assign_rhs2 (stmt2) != arg1)
1049 : {
1050 : fail = true;
1051 : break;
1052 : }
1053 53 : }
1054 53 : if (fail)
1055 2 : continue;
1056 :
1057 51 : gimple_replace_ssa_lhs (call, arg1);
1058 51 : reset_flow_sensitive_info (arg1);
1059 51 : if (gimple_call_internal_p (call) != (ifn != IFN_LAST))
1060 : {
1061 30 : auto_vec<tree, 4> args;
1062 30 : for (unsigned int i = 0;
1063 60 : i < gimple_call_num_args (call); i++)
1064 30 : args.safe_push (gimple_call_arg (call, i));
1065 30 : gcall *stmt2;
1066 30 : if (ifn == IFN_LAST)
1067 0 : stmt2 = gimple_build_call_vec (fndecl, args);
1068 : else
1069 30 : stmt2 = gimple_build_call_internal_vec (ifn, args);
1070 30 : gimple_call_set_lhs (stmt2, arg1);
1071 30 : gimple_move_vops (stmt2, call);
1072 30 : gimple_call_set_nothrow (stmt2,
1073 : gimple_call_nothrow_p (call));
1074 30 : gimple_stmt_iterator gsi2 = gsi_for_stmt (call);
1075 30 : gsi_replace (&gsi2, stmt2, true);
1076 30 : }
1077 : else
1078 : {
1079 21 : if (ifn == IFN_LAST)
1080 0 : gimple_call_set_fndecl (call, fndecl);
1081 : else
1082 21 : gimple_call_set_internal_fn (call, ifn);
1083 21 : update_stmt (call);
1084 : }
1085 51 : reciprocal_stats.rfuncs_inserted++;
1086 :
1087 102 : FOR_EACH_IMM_USE_STMT (stmt, ui, arg1)
1088 : {
1089 51 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1090 51 : gimple_assign_set_rhs_code (stmt, MULT_EXPR);
1091 51 : fold_stmt_inplace (&gsi);
1092 51 : update_stmt (stmt);
1093 51 : }
1094 : }
1095 : }
1096 : }
1097 : }
1098 :
1099 9082 : statistics_counter_event (fun, "reciprocal divs inserted",
1100 : reciprocal_stats.rdivs_inserted);
1101 9082 : statistics_counter_event (fun, "reciprocal functions inserted",
1102 : reciprocal_stats.rfuncs_inserted);
1103 :
1104 9082 : free_dominance_info (CDI_DOMINATORS);
1105 9082 : free_dominance_info (CDI_POST_DOMINATORS);
1106 18164 : delete occ_pool;
1107 9082 : return 0;
1108 : }
1109 :
1110 : } // anon namespace
1111 :
1112 : gimple_opt_pass *
1113 302985 : make_pass_cse_reciprocals (gcc::context *ctxt)
1114 : {
1115 302985 : return new pass_cse_reciprocals (ctxt);
1116 : }
1117 :
1118 : /* If NAME is the result of a type conversion, look for other
1119 : equivalent dominating or dominated conversions, and replace all
1120 : uses with the earliest dominating name, removing the redundant
1121 : conversions. Return the prevailing name. */
1122 :
1123 : static tree
1124 1069 : execute_cse_conv_1 (tree name, bool *cfg_changed)
1125 : {
1126 1069 : if (SSA_NAME_IS_DEFAULT_DEF (name)
1127 1069 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1128 : return name;
1129 :
1130 969 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1131 :
1132 969 : if (!gimple_assign_cast_p (def_stmt))
1133 : return name;
1134 :
1135 136 : tree src = gimple_assign_rhs1 (def_stmt);
1136 :
1137 136 : if (TREE_CODE (src) != SSA_NAME)
1138 : return name;
1139 :
1140 136 : imm_use_iterator use_iter;
1141 136 : gimple *use_stmt;
1142 :
1143 : /* Find the earliest dominating def. */
1144 525 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, src)
1145 : {
1146 771 : if (use_stmt == def_stmt
1147 389 : || !gimple_assign_cast_p (use_stmt))
1148 382 : continue;
1149 :
1150 7 : tree lhs = gimple_assign_lhs (use_stmt);
1151 :
1152 7 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
1153 14 : || (gimple_assign_rhs1 (use_stmt)
1154 7 : != gimple_assign_rhs1 (def_stmt))
1155 14 : || !types_compatible_p (TREE_TYPE (name), TREE_TYPE (lhs)))
1156 0 : continue;
1157 :
1158 7 : bool use_dominates;
1159 7 : if (gimple_bb (def_stmt) == gimple_bb (use_stmt))
1160 : {
1161 0 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1162 0 : while (!gsi_end_p (gsi) && gsi_stmt (gsi) != def_stmt)
1163 0 : gsi_next (&gsi);
1164 0 : use_dominates = !gsi_end_p (gsi);
1165 : }
1166 7 : else if (dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt),
1167 7 : gimple_bb (def_stmt)))
1168 : use_dominates = false;
1169 7 : else if (dominated_by_p (CDI_DOMINATORS, gimple_bb (def_stmt),
1170 7 : gimple_bb (use_stmt)))
1171 : use_dominates = true;
1172 : else
1173 4 : continue;
1174 :
1175 0 : if (use_dominates)
1176 : {
1177 : std::swap (name, lhs);
1178 : std::swap (def_stmt, use_stmt);
1179 : }
1180 136 : }
1181 :
1182 : /* Now go through all uses of SRC again, replacing the equivalent
1183 : dominated conversions. We may replace defs that were not
1184 : dominated by the then-prevailing defs when we first visited
1185 : them. */
1186 525 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, src)
1187 : {
1188 771 : if (use_stmt == def_stmt
1189 389 : || !gimple_assign_cast_p (use_stmt))
1190 382 : continue;
1191 :
1192 7 : tree lhs = gimple_assign_lhs (use_stmt);
1193 :
1194 7 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
1195 14 : || (gimple_assign_rhs1 (use_stmt)
1196 7 : != gimple_assign_rhs1 (def_stmt))
1197 14 : || !types_compatible_p (TREE_TYPE (name), TREE_TYPE (lhs)))
1198 0 : continue;
1199 :
1200 7 : basic_block use_bb = gimple_bb (use_stmt);
1201 7 : if (gimple_bb (def_stmt) == use_bb
1202 7 : || dominated_by_p (CDI_DOMINATORS, use_bb, gimple_bb (def_stmt)))
1203 : {
1204 3 : sincos_stats.conv_removed++;
1205 :
1206 3 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1207 3 : replace_uses_by (lhs, name);
1208 3 : if (gsi_remove (&gsi, true)
1209 3 : && gimple_purge_dead_eh_edges (use_bb))
1210 3 : *cfg_changed = true;
1211 3 : release_defs (use_stmt);
1212 : }
1213 136 : }
1214 :
1215 136 : return name;
1216 : }
1217 :
1218 : /* Records an occurrence at statement USE_STMT in the vector of trees
1219 : STMTS if it is dominated by *TOP_BB or dominates it or this basic block
1220 : is not yet initialized. Returns true if the occurrence was pushed on
1221 : the vector. Adjusts *TOP_BB to be the basic block dominating all
1222 : statements in the vector. */
1223 :
1224 : static bool
1225 1293 : maybe_record_sincos (vec<gimple *> *stmts,
1226 : basic_block *top_bb, gimple *use_stmt)
1227 : {
1228 1293 : basic_block use_bb = gimple_bb (use_stmt);
1229 1293 : if (*top_bb
1230 1293 : && (*top_bb == use_bb
1231 66 : || dominated_by_p (CDI_DOMINATORS, use_bb, *top_bb)))
1232 171 : stmts->safe_push (use_stmt);
1233 1122 : else if (!*top_bb
1234 1122 : || dominated_by_p (CDI_DOMINATORS, *top_bb, use_bb))
1235 : {
1236 1102 : stmts->safe_push (use_stmt);
1237 1102 : *top_bb = use_bb;
1238 : }
1239 : else
1240 : return false;
1241 :
1242 : return true;
1243 : }
1244 :
1245 : /* Look for sin, cos and cexpi calls with the same argument NAME and
1246 : create a single call to cexpi CSEing the result in this case.
1247 : We first walk over all immediate uses of the argument collecting
1248 : statements that we can CSE in a vector and in a second pass replace
1249 : the statement rhs with a REALPART or IMAGPART expression on the
1250 : result of the cexpi call we insert before the use statement that
1251 : dominates all other candidates. */
1252 :
1253 : static bool
1254 1069 : execute_cse_sincos_1 (tree name)
1255 : {
1256 1069 : gimple_stmt_iterator gsi;
1257 1069 : imm_use_iterator use_iter;
1258 1069 : tree fndecl, res, type = NULL_TREE;
1259 1069 : gimple *def_stmt, *use_stmt, *stmt;
1260 1069 : int seen_cos = 0, seen_sin = 0, seen_cexpi = 0;
1261 1069 : auto_vec<gimple *> stmts;
1262 1069 : basic_block top_bb = NULL;
1263 1069 : int i;
1264 1069 : bool cfg_changed = false;
1265 :
1266 1069 : name = execute_cse_conv_1 (name, &cfg_changed);
1267 :
1268 4219 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, name)
1269 : {
1270 3152 : if (gimple_code (use_stmt) != GIMPLE_CALL
1271 3152 : || !gimple_call_lhs (use_stmt))
1272 1827 : continue;
1273 :
1274 1325 : switch (gimple_call_combined_fn (use_stmt))
1275 : {
1276 464 : CASE_CFN_COS:
1277 464 : seen_cos |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
1278 464 : break;
1279 :
1280 823 : CASE_CFN_SIN:
1281 823 : seen_sin |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
1282 823 : break;
1283 :
1284 6 : CASE_CFN_CEXPI:
1285 6 : seen_cexpi |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
1286 6 : break;
1287 :
1288 32 : default:;
1289 32 : continue;
1290 : }
1291 :
1292 1293 : auto stmt_cfn = gimple_call_combined_fn (use_stmt);
1293 1293 : tree t = mathfn_built_in_type (stmt_cfn);
1294 1293 : if (!t)
1295 : {
1296 : /* It is possible to get IFN_{SIN,COS} calls, for which
1297 : mathfn_built_in_type will return NULL. Those are normally only
1298 : present for vector operations. We won't be able to CSE those
1299 : at the moment. */
1300 2 : gcc_checking_assert (internal_fn_p (stmt_cfn));
1301 : return false;
1302 : }
1303 :
1304 1291 : if (!type)
1305 : {
1306 1067 : type = t;
1307 1067 : t = TREE_TYPE (name);
1308 : }
1309 : /* This checks that NAME has the right type in the first round,
1310 : and, in subsequent rounds, that the built_in type is the same
1311 : type, or a compatible type. */
1312 1291 : if (type != t && !types_compatible_p (type, t))
1313 : return false;
1314 2 : }
1315 1067 : if (seen_cos + seen_sin + seen_cexpi <= 1)
1316 : return false;
1317 :
1318 : /* Simply insert cexpi at the beginning of top_bb but not earlier than
1319 : the name def statement. */
1320 204 : fndecl = mathfn_built_in (type, BUILT_IN_CEXPI);
1321 204 : if (!fndecl)
1322 : return false;
1323 160 : stmt = gimple_build_call (fndecl, 1, name);
1324 160 : res = make_temp_ssa_name (TREE_TYPE (TREE_TYPE (fndecl)), stmt, "sincostmp");
1325 160 : gimple_call_set_lhs (stmt, res);
1326 :
1327 160 : def_stmt = SSA_NAME_DEF_STMT (name);
1328 160 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
1329 130 : && gimple_code (def_stmt) != GIMPLE_PHI
1330 282 : && gimple_bb (def_stmt) == top_bb)
1331 : {
1332 122 : gsi = gsi_for_stmt (def_stmt);
1333 122 : gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
1334 : }
1335 : else
1336 : {
1337 38 : gsi = gsi_after_labels (top_bb);
1338 38 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1339 : }
1340 160 : sincos_stats.inserted++;
1341 :
1342 : /* And adjust the recorded old call sites. */
1343 480 : for (i = 0; stmts.iterate (i, &use_stmt); ++i)
1344 : {
1345 320 : tree rhs = NULL;
1346 :
1347 320 : switch (gimple_call_combined_fn (use_stmt))
1348 : {
1349 160 : CASE_CFN_COS:
1350 160 : rhs = fold_build1 (REALPART_EXPR, type, res);
1351 160 : break;
1352 :
1353 160 : CASE_CFN_SIN:
1354 160 : rhs = fold_build1 (IMAGPART_EXPR, type, res);
1355 160 : break;
1356 :
1357 : CASE_CFN_CEXPI:
1358 : rhs = res;
1359 : break;
1360 :
1361 0 : default:;
1362 0 : gcc_unreachable ();
1363 : }
1364 :
1365 : /* Replace call with a copy. */
1366 320 : stmt = gimple_build_assign (gimple_call_lhs (use_stmt), rhs);
1367 :
1368 320 : gsi = gsi_for_stmt (use_stmt);
1369 320 : gsi_replace (&gsi, stmt, true);
1370 320 : if (gimple_purge_dead_eh_edges (gimple_bb (stmt)))
1371 0 : cfg_changed = true;
1372 : }
1373 :
1374 160 : return cfg_changed;
1375 1069 : }
1376 :
1377 : /* To evaluate powi(x,n), the floating point value x raised to the
1378 : constant integer exponent n, we use a hybrid algorithm that
1379 : combines the "window method" with look-up tables. For an
1380 : introduction to exponentiation algorithms and "addition chains",
1381 : see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
1382 : "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
1383 : 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
1384 : Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
1385 :
1386 : /* Provide a default value for POWI_MAX_MULTS, the maximum number of
1387 : multiplications to inline before calling the system library's pow
1388 : function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
1389 : so this default never requires calling pow, powf or powl. */
1390 :
1391 : #ifndef POWI_MAX_MULTS
1392 : #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
1393 : #endif
1394 :
1395 : /* The size of the "optimal power tree" lookup table. All
1396 : exponents less than this value are simply looked up in the
1397 : powi_table below. This threshold is also used to size the
1398 : cache of pseudo registers that hold intermediate results. */
1399 : #define POWI_TABLE_SIZE 256
1400 :
1401 : /* The size, in bits of the window, used in the "window method"
1402 : exponentiation algorithm. This is equivalent to a radix of
1403 : (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
1404 : #define POWI_WINDOW_SIZE 3
1405 :
1406 : /* The following table is an efficient representation of an
1407 : "optimal power tree". For each value, i, the corresponding
1408 : value, j, in the table states than an optimal evaluation
1409 : sequence for calculating pow(x,i) can be found by evaluating
1410 : pow(x,j)*pow(x,i-j). An optimal power tree for the first
1411 : 100 integers is given in Knuth's "Seminumerical algorithms". */
1412 :
1413 : static const unsigned char powi_table[POWI_TABLE_SIZE] =
1414 : {
1415 : 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
1416 : 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
1417 : 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
1418 : 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
1419 : 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
1420 : 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
1421 : 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
1422 : 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
1423 : 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
1424 : 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
1425 : 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
1426 : 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
1427 : 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
1428 : 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
1429 : 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
1430 : 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
1431 : 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
1432 : 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
1433 : 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
1434 : 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
1435 : 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
1436 : 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
1437 : 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
1438 : 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
1439 : 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
1440 : 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
1441 : 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
1442 : 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
1443 : 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
1444 : 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
1445 : 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
1446 : 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
1447 : };
1448 :
1449 :
1450 : /* Return the number of multiplications required to calculate
1451 : powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
1452 : subroutine of powi_cost. CACHE is an array indicating
1453 : which exponents have already been calculated. */
1454 :
1455 : static int
1456 1202 : powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
1457 : {
1458 : /* If we've already calculated this exponent, then this evaluation
1459 : doesn't require any additional multiplications. */
1460 2007 : if (cache[n])
1461 1202 : return 0;
1462 :
1463 805 : cache[n] = true;
1464 805 : return powi_lookup_cost (n - powi_table[n], cache)
1465 805 : + powi_lookup_cost (powi_table[n], cache) + 1;
1466 : }
1467 :
1468 : /* Return the number of multiplications required to calculate
1469 : powi(x,n) for an arbitrary x, given the exponent N. This
1470 : function needs to be kept in sync with powi_as_mults below. */
1471 :
1472 : static int
1473 392 : powi_cost (HOST_WIDE_INT n)
1474 : {
1475 392 : bool cache[POWI_TABLE_SIZE];
1476 392 : unsigned HOST_WIDE_INT digit;
1477 392 : unsigned HOST_WIDE_INT val;
1478 392 : int result;
1479 :
1480 392 : if (n == 0)
1481 : return 0;
1482 :
1483 : /* Ignore the reciprocal when calculating the cost. */
1484 387 : val = absu_hwi (n);
1485 :
1486 : /* Initialize the exponent cache. */
1487 387 : memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
1488 387 : cache[1] = true;
1489 :
1490 387 : result = 0;
1491 :
1492 457 : while (val >= POWI_TABLE_SIZE)
1493 : {
1494 70 : if (val & 1)
1495 : {
1496 10 : digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
1497 10 : result += powi_lookup_cost (digit, cache)
1498 10 : + POWI_WINDOW_SIZE + 1;
1499 10 : val >>= POWI_WINDOW_SIZE;
1500 : }
1501 : else
1502 : {
1503 60 : val >>= 1;
1504 60 : result++;
1505 : }
1506 : }
1507 :
1508 387 : return result + powi_lookup_cost (val, cache);
1509 : }
1510 :
1511 : /* Recursive subroutine of powi_as_mults. This function takes the
1512 : array, CACHE, of already calculated exponents and an exponent N and
1513 : returns a tree that corresponds to CACHE[1]**N, with type TYPE. */
1514 :
1515 : static tree
1516 6503 : powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type,
1517 : unsigned HOST_WIDE_INT n, tree *cache)
1518 : {
1519 6503 : tree op0, op1, ssa_target;
1520 6503 : unsigned HOST_WIDE_INT digit;
1521 6503 : gassign *mult_stmt;
1522 :
1523 6503 : if (n < POWI_TABLE_SIZE && cache[n])
1524 : return cache[n];
1525 :
1526 2353 : ssa_target = make_temp_ssa_name (type, NULL, "powmult");
1527 :
1528 2353 : if (n < POWI_TABLE_SIZE)
1529 : {
1530 2282 : cache[n] = ssa_target;
1531 2282 : op0 = powi_as_mults_1 (gsi, loc, type, n - powi_table[n], cache);
1532 2282 : op1 = powi_as_mults_1 (gsi, loc, type, powi_table[n], cache);
1533 : }
1534 71 : else if (n & 1)
1535 : {
1536 11 : digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
1537 11 : op0 = powi_as_mults_1 (gsi, loc, type, n - digit, cache);
1538 11 : op1 = powi_as_mults_1 (gsi, loc, type, digit, cache);
1539 : }
1540 : else
1541 : {
1542 60 : op0 = powi_as_mults_1 (gsi, loc, type, n >> 1, cache);
1543 60 : op1 = op0;
1544 : }
1545 :
1546 2353 : mult_stmt = gimple_build_assign (ssa_target, MULT_EXPR, op0, op1);
1547 2353 : gimple_set_location (mult_stmt, loc);
1548 2353 : gsi_insert_before (gsi, mult_stmt, GSI_SAME_STMT);
1549 :
1550 2353 : return ssa_target;
1551 : }
1552 :
1553 : /* Convert ARG0**N to a tree of multiplications of ARG0 with itself.
1554 : This function needs to be kept in sync with powi_cost above. */
1555 :
1556 : tree
1557 1857 : powi_as_mults (gimple_stmt_iterator *gsi, location_t loc,
1558 : tree arg0, HOST_WIDE_INT n)
1559 : {
1560 1857 : tree cache[POWI_TABLE_SIZE], result, type = TREE_TYPE (arg0);
1561 1857 : gassign *div_stmt;
1562 1857 : tree target;
1563 :
1564 1857 : if (n == 0)
1565 0 : return build_one_cst (type);
1566 :
1567 1857 : memset (cache, 0, sizeof (cache));
1568 1857 : cache[1] = arg0;
1569 :
1570 1857 : result = powi_as_mults_1 (gsi, loc, type, absu_hwi (n), cache);
1571 1857 : if (n >= 0)
1572 : return result;
1573 :
1574 : /* If the original exponent was negative, reciprocate the result. */
1575 7 : target = make_temp_ssa_name (type, NULL, "powmult");
1576 7 : div_stmt = gimple_build_assign (target, RDIV_EXPR,
1577 : build_real (type, dconst1), result);
1578 7 : gimple_set_location (div_stmt, loc);
1579 7 : gsi_insert_before (gsi, div_stmt, GSI_SAME_STMT);
1580 :
1581 7 : return target;
1582 : }
1583 :
1584 : /* ARG0 and N are the two arguments to a powi builtin in GSI with
1585 : location info LOC. If the arguments are appropriate, create an
1586 : equivalent sequence of statements prior to GSI using an optimal
1587 : number of multiplications, and return an expression holding the
1588 : result. */
1589 :
1590 : static tree
1591 639 : gimple_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc,
1592 : tree arg0, HOST_WIDE_INT n)
1593 : {
1594 639 : if ((n >= -1 && n <= 2)
1595 639 : || (optimize_function_for_speed_p (cfun)
1596 356 : && powi_cost (n) <= POWI_MAX_MULTS))
1597 631 : return powi_as_mults (gsi, loc, arg0, n);
1598 :
1599 : return NULL_TREE;
1600 : }
1601 :
1602 : /* Build a gimple call statement that calls FN with argument ARG.
1603 : Set the lhs of the call statement to a fresh SSA name. Insert the
1604 : statement prior to GSI's current position, and return the fresh
1605 : SSA name. */
1606 :
1607 : static tree
1608 51 : build_and_insert_call (gimple_stmt_iterator *gsi, location_t loc,
1609 : tree fn, tree arg)
1610 : {
1611 51 : gcall *call_stmt;
1612 51 : tree ssa_target;
1613 :
1614 51 : call_stmt = gimple_build_call (fn, 1, arg);
1615 51 : ssa_target = make_temp_ssa_name (TREE_TYPE (arg), NULL, "powroot");
1616 51 : gimple_set_lhs (call_stmt, ssa_target);
1617 51 : gimple_set_location (call_stmt, loc);
1618 51 : gsi_insert_before (gsi, call_stmt, GSI_SAME_STMT);
1619 :
1620 51 : return ssa_target;
1621 : }
1622 :
1623 : /* Build a gimple binary operation with the given CODE and arguments
1624 : ARG0, ARG1, assigning the result to a new SSA name for variable
1625 : TARGET. Insert the statement prior to GSI's current position, and
1626 : return the fresh SSA name.*/
1627 :
1628 : static tree
1629 2658 : build_and_insert_binop (gimple_stmt_iterator *gsi, location_t loc,
1630 : const char *name, enum tree_code code,
1631 : tree arg0, tree arg1)
1632 : {
1633 2658 : tree result = make_temp_ssa_name (TREE_TYPE (arg0), NULL, name);
1634 2658 : gassign *stmt = gimple_build_assign (result, code, arg0, arg1);
1635 2658 : gimple_set_location (stmt, loc);
1636 2658 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1637 2658 : return result;
1638 : }
1639 :
1640 : /* Build a gimple assignment to cast VAL to TYPE. Insert the statement
1641 : prior to GSI's current position, and return the fresh SSA name. */
1642 :
1643 : static tree
1644 14817 : build_and_insert_cast (gimple_stmt_iterator *gsi, location_t loc,
1645 : tree type, tree val)
1646 : {
1647 0 : return gimple_convert (gsi, true, GSI_SAME_STMT, loc, type, val);
1648 : }
1649 :
1650 : struct pow_synth_sqrt_info
1651 : {
1652 : bool *factors;
1653 : unsigned int deepest;
1654 : unsigned int num_mults;
1655 : };
1656 :
1657 : /* Return true iff the real value C can be represented as a
1658 : sum of powers of 0.5 up to N. That is:
1659 : C == SUM<i from 1..N> (a[i]*(0.5**i)) where a[i] is either 0 or 1.
1660 : Record in INFO the various parameters of the synthesis algorithm such
1661 : as the factors a[i], the maximum 0.5 power and the number of
1662 : multiplications that will be required. */
1663 :
1664 : bool
1665 34 : representable_as_half_series_p (REAL_VALUE_TYPE c, unsigned n,
1666 : struct pow_synth_sqrt_info *info)
1667 : {
1668 34 : REAL_VALUE_TYPE factor = dconsthalf;
1669 34 : REAL_VALUE_TYPE remainder = c;
1670 :
1671 34 : info->deepest = 0;
1672 34 : info->num_mults = 0;
1673 34 : memset (info->factors, 0, n * sizeof (bool));
1674 :
1675 98 : for (unsigned i = 0; i < n; i++)
1676 : {
1677 91 : REAL_VALUE_TYPE res;
1678 :
1679 : /* If something inexact happened bail out now. */
1680 91 : if (real_arithmetic (&res, MINUS_EXPR, &remainder, &factor))
1681 27 : return false;
1682 :
1683 : /* We have hit zero. The number is representable as a sum
1684 : of powers of 0.5. */
1685 91 : if (real_equal (&res, &dconst0))
1686 : {
1687 27 : info->factors[i] = true;
1688 27 : info->deepest = i + 1;
1689 27 : return true;
1690 : }
1691 64 : else if (!REAL_VALUE_NEGATIVE (res))
1692 : {
1693 29 : remainder = res;
1694 29 : info->factors[i] = true;
1695 29 : info->num_mults++;
1696 : }
1697 : else
1698 35 : info->factors[i] = false;
1699 :
1700 64 : real_arithmetic (&factor, MULT_EXPR, &factor, &dconsthalf);
1701 : }
1702 : return false;
1703 : }
1704 :
1705 : /* Return the tree corresponding to FN being applied
1706 : to ARG N times at GSI and LOC.
1707 : Look up previous results from CACHE if need be.
1708 : cache[0] should contain just plain ARG i.e. FN applied to ARG 0 times. */
1709 :
1710 : static tree
1711 65 : get_fn_chain (tree arg, unsigned int n, gimple_stmt_iterator *gsi,
1712 : tree fn, location_t loc, tree *cache)
1713 : {
1714 65 : tree res = cache[n];
1715 65 : if (!res)
1716 : {
1717 41 : tree prev = get_fn_chain (arg, n - 1, gsi, fn, loc, cache);
1718 41 : res = build_and_insert_call (gsi, loc, fn, prev);
1719 41 : cache[n] = res;
1720 : }
1721 :
1722 65 : return res;
1723 : }
1724 :
1725 : /* Print to STREAM the repeated application of function FNAME to ARG
1726 : N times. So, for FNAME = "foo", ARG = "x", N = 2 it would print:
1727 : "foo (foo (x))". */
1728 :
1729 : static void
1730 36 : print_nested_fn (FILE* stream, const char *fname, const char* arg,
1731 : unsigned int n)
1732 : {
1733 36 : if (n == 0)
1734 10 : fprintf (stream, "%s", arg);
1735 : else
1736 : {
1737 26 : fprintf (stream, "%s (", fname);
1738 26 : print_nested_fn (stream, fname, arg, n - 1);
1739 26 : fprintf (stream, ")");
1740 : }
1741 36 : }
1742 :
1743 : /* Print to STREAM the fractional sequence of sqrt chains
1744 : applied to ARG, described by INFO. Used for the dump file. */
1745 :
1746 : static void
1747 7 : dump_fractional_sqrt_sequence (FILE *stream, const char *arg,
1748 : struct pow_synth_sqrt_info *info)
1749 : {
1750 29 : for (unsigned int i = 0; i < info->deepest; i++)
1751 : {
1752 22 : bool is_set = info->factors[i];
1753 22 : if (is_set)
1754 : {
1755 10 : print_nested_fn (stream, "sqrt", arg, i + 1);
1756 10 : if (i != info->deepest - 1)
1757 3 : fprintf (stream, " * ");
1758 : }
1759 : }
1760 7 : }
1761 :
1762 : /* Print to STREAM a representation of raising ARG to an integer
1763 : power N. Used for the dump file. */
1764 :
1765 : static void
1766 7 : dump_integer_part (FILE *stream, const char* arg, HOST_WIDE_INT n)
1767 : {
1768 7 : if (n > 1)
1769 3 : fprintf (stream, "powi (%s, " HOST_WIDE_INT_PRINT_DEC ")", arg, n);
1770 4 : else if (n == 1)
1771 3 : fprintf (stream, "%s", arg);
1772 7 : }
1773 :
1774 : /* Attempt to synthesize a POW[F] (ARG0, ARG1) call using chains of
1775 : square roots. Place at GSI and LOC. Limit the maximum depth
1776 : of the sqrt chains to MAX_DEPTH. Return the tree holding the
1777 : result of the expanded sequence or NULL_TREE if the expansion failed.
1778 :
1779 : This routine assumes that ARG1 is a real number with a fractional part
1780 : (the integer exponent case will have been handled earlier in
1781 : gimple_expand_builtin_pow).
1782 :
1783 : For ARG1 > 0.0:
1784 : * For ARG1 composed of a whole part WHOLE_PART and a fractional part
1785 : FRAC_PART i.e. WHOLE_PART == floor (ARG1) and
1786 : FRAC_PART == ARG1 - WHOLE_PART:
1787 : Produce POWI (ARG0, WHOLE_PART) * POW (ARG0, FRAC_PART) where
1788 : POW (ARG0, FRAC_PART) is expanded as a product of square root chains
1789 : if it can be expressed as such, that is if FRAC_PART satisfies:
1790 : FRAC_PART == <SUM from i = 1 until MAX_DEPTH> (a[i] * (0.5**i))
1791 : where integer a[i] is either 0 or 1.
1792 :
1793 : Example:
1794 : POW (x, 3.625) == POWI (x, 3) * POW (x, 0.625)
1795 : --> POWI (x, 3) * SQRT (x) * SQRT (SQRT (SQRT (x)))
1796 :
1797 : For ARG1 < 0.0 there are two approaches:
1798 : * (A) Expand to 1.0 / POW (ARG0, -ARG1) where POW (ARG0, -ARG1)
1799 : is calculated as above.
1800 :
1801 : Example:
1802 : POW (x, -5.625) == 1.0 / POW (x, 5.625)
1803 : --> 1.0 / (POWI (x, 5) * SQRT (x) * SQRT (SQRT (SQRT (x))))
1804 :
1805 : * (B) : WHOLE_PART := - ceil (abs (ARG1))
1806 : FRAC_PART := ARG1 - WHOLE_PART
1807 : and expand to POW (x, FRAC_PART) / POWI (x, WHOLE_PART).
1808 : Example:
1809 : POW (x, -5.875) == POW (x, 0.125) / POWI (X, 6)
1810 : --> SQRT (SQRT (SQRT (x))) / (POWI (x, 6))
1811 :
1812 : For ARG1 < 0.0 we choose between (A) and (B) depending on
1813 : how many multiplications we'd have to do.
1814 : So, for the example in (B): POW (x, -5.875), if we were to
1815 : follow algorithm (A) we would produce:
1816 : 1.0 / POWI (X, 5) * SQRT (X) * SQRT (SQRT (X)) * SQRT (SQRT (SQRT (X)))
1817 : which contains more multiplications than approach (B).
1818 :
1819 : Hopefully, this approach will eliminate potentially expensive POW library
1820 : calls when unsafe floating point math is enabled and allow the compiler to
1821 : further optimise the multiplies, square roots and divides produced by this
1822 : function. */
1823 :
1824 : static tree
1825 26 : expand_pow_as_sqrts (gimple_stmt_iterator *gsi, location_t loc,
1826 : tree arg0, tree arg1, HOST_WIDE_INT max_depth)
1827 : {
1828 26 : tree type = TREE_TYPE (arg0);
1829 26 : machine_mode mode = TYPE_MODE (type);
1830 26 : tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
1831 26 : bool one_over = true;
1832 :
1833 26 : if (!sqrtfn)
1834 : return NULL_TREE;
1835 :
1836 26 : if (TREE_CODE (arg1) != REAL_CST)
1837 : return NULL_TREE;
1838 :
1839 26 : REAL_VALUE_TYPE exp_init = TREE_REAL_CST (arg1);
1840 :
1841 26 : gcc_assert (max_depth > 0);
1842 26 : tree *cache = XALLOCAVEC (tree, max_depth + 1);
1843 :
1844 26 : struct pow_synth_sqrt_info synth_info;
1845 26 : synth_info.factors = XALLOCAVEC (bool, max_depth + 1);
1846 26 : synth_info.deepest = 0;
1847 26 : synth_info.num_mults = 0;
1848 :
1849 26 : bool neg_exp = REAL_VALUE_NEGATIVE (exp_init);
1850 26 : REAL_VALUE_TYPE exp = real_value_abs (&exp_init);
1851 :
1852 : /* The whole and fractional parts of exp. */
1853 26 : REAL_VALUE_TYPE whole_part;
1854 26 : REAL_VALUE_TYPE frac_part;
1855 :
1856 26 : real_floor (&whole_part, mode, &exp);
1857 26 : real_arithmetic (&frac_part, MINUS_EXPR, &exp, &whole_part);
1858 :
1859 :
1860 26 : REAL_VALUE_TYPE ceil_whole = dconst0;
1861 26 : REAL_VALUE_TYPE ceil_fract = dconst0;
1862 :
1863 26 : if (neg_exp)
1864 : {
1865 10 : real_ceil (&ceil_whole, mode, &exp);
1866 10 : real_arithmetic (&ceil_fract, MINUS_EXPR, &ceil_whole, &exp);
1867 : }
1868 :
1869 26 : if (!representable_as_half_series_p (frac_part, max_depth, &synth_info))
1870 : return NULL_TREE;
1871 :
1872 : /* Check whether it's more profitable to not use 1.0 / ... */
1873 19 : if (neg_exp)
1874 : {
1875 8 : struct pow_synth_sqrt_info alt_synth_info;
1876 8 : alt_synth_info.factors = XALLOCAVEC (bool, max_depth + 1);
1877 8 : alt_synth_info.deepest = 0;
1878 8 : alt_synth_info.num_mults = 0;
1879 :
1880 8 : if (representable_as_half_series_p (ceil_fract, max_depth,
1881 : &alt_synth_info)
1882 8 : && alt_synth_info.deepest <= synth_info.deepest
1883 16 : && alt_synth_info.num_mults < synth_info.num_mults)
1884 : {
1885 2 : whole_part = ceil_whole;
1886 2 : frac_part = ceil_fract;
1887 2 : synth_info.deepest = alt_synth_info.deepest;
1888 2 : synth_info.num_mults = alt_synth_info.num_mults;
1889 2 : memcpy (synth_info.factors, alt_synth_info.factors,
1890 : (max_depth + 1) * sizeof (bool));
1891 2 : one_over = false;
1892 : }
1893 : }
1894 :
1895 19 : HOST_WIDE_INT n = real_to_integer (&whole_part);
1896 19 : REAL_VALUE_TYPE cint;
1897 19 : real_from_integer (&cint, VOIDmode, n, SIGNED);
1898 :
1899 19 : if (!real_identical (&whole_part, &cint))
1900 : return NULL_TREE;
1901 :
1902 19 : if (powi_cost (n) + synth_info.num_mults > POWI_MAX_MULTS)
1903 : return NULL_TREE;
1904 :
1905 19 : memset (cache, 0, (max_depth + 1) * sizeof (tree));
1906 :
1907 19 : tree integer_res = n == 0 ? build_real (type, dconst1) : arg0;
1908 :
1909 : /* Calculate the integer part of the exponent. */
1910 19 : if (n > 1)
1911 : {
1912 7 : integer_res = gimple_expand_builtin_powi (gsi, loc, arg0, n);
1913 7 : if (!integer_res)
1914 : return NULL_TREE;
1915 : }
1916 :
1917 19 : if (dump_file)
1918 : {
1919 7 : char string[64];
1920 :
1921 7 : real_to_decimal (string, &exp_init, sizeof (string), 0, 1);
1922 7 : fprintf (dump_file, "synthesizing pow (x, %s) as:\n", string);
1923 :
1924 7 : if (neg_exp)
1925 : {
1926 2 : if (one_over)
1927 : {
1928 1 : fprintf (dump_file, "1.0 / (");
1929 1 : dump_integer_part (dump_file, "x", n);
1930 1 : if (n > 0)
1931 1 : fprintf (dump_file, " * ");
1932 1 : dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1933 1 : fprintf (dump_file, ")");
1934 : }
1935 : else
1936 : {
1937 1 : dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1938 1 : fprintf (dump_file, " / (");
1939 1 : dump_integer_part (dump_file, "x", n);
1940 1 : fprintf (dump_file, ")");
1941 : }
1942 : }
1943 : else
1944 : {
1945 5 : dump_fractional_sqrt_sequence (dump_file, "x", &synth_info);
1946 5 : if (n > 0)
1947 4 : fprintf (dump_file, " * ");
1948 5 : dump_integer_part (dump_file, "x", n);
1949 : }
1950 :
1951 7 : fprintf (dump_file, "\ndeepest sqrt chain: %d\n", synth_info.deepest);
1952 : }
1953 :
1954 :
1955 19 : tree fract_res = NULL_TREE;
1956 19 : cache[0] = arg0;
1957 :
1958 : /* Calculate the fractional part of the exponent. */
1959 60 : for (unsigned i = 0; i < synth_info.deepest; i++)
1960 : {
1961 41 : if (synth_info.factors[i])
1962 : {
1963 24 : tree sqrt_chain = get_fn_chain (arg0, i + 1, gsi, sqrtfn, loc, cache);
1964 :
1965 24 : if (!fract_res)
1966 : fract_res = sqrt_chain;
1967 :
1968 : else
1969 5 : fract_res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1970 : fract_res, sqrt_chain);
1971 : }
1972 : }
1973 :
1974 19 : tree res = NULL_TREE;
1975 :
1976 19 : if (neg_exp)
1977 : {
1978 8 : if (one_over)
1979 : {
1980 6 : if (n > 0)
1981 4 : res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1982 : fract_res, integer_res);
1983 : else
1984 : res = fract_res;
1985 :
1986 6 : res = build_and_insert_binop (gsi, loc, "powrootrecip", RDIV_EXPR,
1987 : build_real (type, dconst1), res);
1988 : }
1989 : else
1990 : {
1991 2 : res = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR,
1992 : fract_res, integer_res);
1993 : }
1994 : }
1995 : else
1996 11 : res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
1997 : fract_res, integer_res);
1998 : return res;
1999 : }
2000 :
2001 : /* ARG0 and ARG1 are the two arguments to a pow builtin call in GSI
2002 : with location info LOC. If possible, create an equivalent and
2003 : less expensive sequence of statements prior to GSI, and return an
2004 : expression holding the result. */
2005 :
2006 : static tree
2007 760 : gimple_expand_builtin_pow (gimple_stmt_iterator *gsi, location_t loc,
2008 : tree arg0, tree arg1)
2009 : {
2010 760 : REAL_VALUE_TYPE c, cint, dconst1_3, dconst1_4, dconst1_6;
2011 760 : REAL_VALUE_TYPE c2, dconst3;
2012 760 : HOST_WIDE_INT n;
2013 760 : tree type, sqrtfn, cbrtfn, sqrt_arg0, result, cbrt_x, powi_cbrt_x;
2014 760 : machine_mode mode;
2015 760 : bool speed_p = optimize_bb_for_speed_p (gsi_bb (*gsi));
2016 760 : bool hw_sqrt_exists, c_is_int, c2_is_int;
2017 :
2018 760 : dconst1_4 = dconst1;
2019 760 : SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2);
2020 :
2021 : /* If the exponent isn't a constant, there's nothing of interest
2022 : to be done. */
2023 760 : if (TREE_CODE (arg1) != REAL_CST)
2024 : return NULL_TREE;
2025 :
2026 383 : if (flag_errno_math)
2027 : return NULL_TREE;
2028 :
2029 : /* Don't perform the operation if flag_signaling_nans is on
2030 : and either operand is a signaling NaN. */
2031 85 : if (tree_expr_maybe_signaling_nan_p (arg0)
2032 85 : || tree_expr_maybe_signaling_nan_p (arg1))
2033 : return NULL_TREE;
2034 :
2035 : /* If the exponent is equivalent to an integer, expand to an optimal
2036 : multiplication sequence when profitable. */
2037 85 : c = TREE_REAL_CST (arg1);
2038 85 : n = real_to_integer (&c);
2039 85 : real_from_integer (&cint, VOIDmode, n, SIGNED);
2040 85 : c_is_int = real_identical (&c, &cint);
2041 :
2042 85 : if (c_is_int
2043 85 : && ((n >= -1 && n <= 2)
2044 23 : || (flag_unsafe_math_optimizations
2045 13 : && speed_p
2046 13 : && powi_cost (n) <= POWI_MAX_MULTS)))
2047 32 : return gimple_expand_builtin_powi (gsi, loc, arg0, n);
2048 :
2049 : /* Attempt various optimizations using sqrt and cbrt. */
2050 53 : type = TREE_TYPE (arg0);
2051 53 : mode = TYPE_MODE (type);
2052 53 : sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
2053 :
2054 : /* Optimize pow(x,0.5) = sqrt(x). This replacement is safe
2055 : except for signed zeros and negative infinity.
2056 : pow(-0,0.5) = +0, while sqrt(-0) = -0.
2057 : pow(-Inf,0.5) = +Inf, while sqrt(-Inf) is NaN. */
2058 53 : if (sqrtfn
2059 53 : && real_equal (&c, &dconsthalf)
2060 14 : && !tree_expr_maybe_real_minus_zero_p (arg0)
2061 60 : && (!tree_expr_maybe_infinite_p (arg0)
2062 1 : || tree_expr_nonnegative_p (arg0)))
2063 6 : return build_and_insert_call (gsi, loc, sqrtfn, arg0);
2064 :
2065 47 : hw_sqrt_exists = optab_handler (sqrt_optab, mode) != CODE_FOR_nothing;
2066 :
2067 : /* Optimize pow(x,1./3.) = cbrt(x). This requires unsafe math
2068 : optimizations since 1./3. is not exactly representable. If x
2069 : is negative and finite, the correct value of pow(x,1./3.) is
2070 : a NaN with the "invalid" exception raised, because the value
2071 : of 1./3. actually has an even denominator. The correct value
2072 : of cbrt(x) is a negative real value. */
2073 47 : cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
2074 47 : dconst1_3 = real_value_truncate (mode, dconst_third ());
2075 :
2076 47 : if (flag_unsafe_math_optimizations
2077 26 : && cbrtfn
2078 26 : && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
2079 73 : && real_equal (&c, &dconst1_3))
2080 0 : return build_and_insert_call (gsi, loc, cbrtfn, arg0);
2081 :
2082 : /* Optimize pow(x,1./6.) = cbrt(sqrt(x)). Don't do this optimization
2083 : if we don't have a hardware sqrt insn. */
2084 47 : dconst1_6 = dconst1_3;
2085 47 : SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
2086 :
2087 47 : if (flag_unsafe_math_optimizations
2088 26 : && sqrtfn
2089 26 : && cbrtfn
2090 26 : && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
2091 : && speed_p
2092 26 : && hw_sqrt_exists
2093 73 : && real_equal (&c, &dconst1_6))
2094 : {
2095 : /* sqrt(x) */
2096 0 : sqrt_arg0 = build_and_insert_call (gsi, loc, sqrtfn, arg0);
2097 :
2098 : /* cbrt(sqrt(x)) */
2099 0 : return build_and_insert_call (gsi, loc, cbrtfn, sqrt_arg0);
2100 : }
2101 :
2102 :
2103 : /* Attempt to expand the POW as a product of square root chains.
2104 : Expand the 0.25 case even when optimising for size. */
2105 47 : if (flag_unsafe_math_optimizations
2106 26 : && sqrtfn
2107 26 : && hw_sqrt_exists
2108 26 : && (speed_p || real_equal (&c, &dconst1_4))
2109 73 : && !HONOR_SIGNED_ZEROS (mode))
2110 : {
2111 78 : unsigned int max_depth = speed_p
2112 26 : ? param_max_pow_sqrt_depth
2113 : : 2;
2114 :
2115 26 : tree expand_with_sqrts
2116 26 : = expand_pow_as_sqrts (gsi, loc, arg0, arg1, max_depth);
2117 :
2118 26 : if (expand_with_sqrts)
2119 : return expand_with_sqrts;
2120 : }
2121 :
2122 28 : real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2123 28 : n = real_to_integer (&c2);
2124 28 : real_from_integer (&cint, VOIDmode, n, SIGNED);
2125 28 : c2_is_int = real_identical (&c2, &cint);
2126 :
2127 : /* Optimize pow(x,c), where 3c = n for some nonzero integer n, into
2128 :
2129 : powi(x, n/3) * powi(cbrt(x), n%3), n > 0;
2130 : 1.0 / (powi(x, abs(n)/3) * powi(cbrt(x), abs(n)%3)), n < 0.
2131 :
2132 : Do not calculate the first factor when n/3 = 0. As cbrt(x) is
2133 : different from pow(x, 1./3.) due to rounding and behavior with
2134 : negative x, we need to constrain this transformation to unsafe
2135 : math and positive x or finite math. */
2136 28 : real_from_integer (&dconst3, VOIDmode, 3, SIGNED);
2137 28 : real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2138 28 : real_round (&c2, mode, &c2);
2139 28 : n = real_to_integer (&c2);
2140 28 : real_from_integer (&cint, VOIDmode, n, SIGNED);
2141 28 : real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2142 28 : real_convert (&c2, mode, &c2);
2143 :
2144 28 : if (flag_unsafe_math_optimizations
2145 7 : && cbrtfn
2146 7 : && (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0))
2147 7 : && real_identical (&c2, &c)
2148 4 : && !c2_is_int
2149 4 : && optimize_function_for_speed_p (cfun)
2150 32 : && powi_cost (n / 3) <= POWI_MAX_MULTS)
2151 : {
2152 4 : tree powi_x_ndiv3 = NULL_TREE;
2153 :
2154 : /* Attempt to fold powi(arg0, abs(n/3)) into multiplies. If not
2155 : possible or profitable, give up. Skip the degenerate case when
2156 : abs(n) < 3, where the result is always 1. */
2157 4 : if (absu_hwi (n) >= 3)
2158 : {
2159 4 : powi_x_ndiv3 = gimple_expand_builtin_powi (gsi, loc, arg0,
2160 : abs_hwi (n / 3));
2161 4 : if (!powi_x_ndiv3)
2162 : return NULL_TREE;
2163 : }
2164 :
2165 : /* Calculate powi(cbrt(x), n%3). Don't use gimple_expand_builtin_powi
2166 : as that creates an unnecessary variable. Instead, just produce
2167 : either cbrt(x) or cbrt(x) * cbrt(x). */
2168 4 : cbrt_x = build_and_insert_call (gsi, loc, cbrtfn, arg0);
2169 :
2170 4 : if (absu_hwi (n) % 3 == 1)
2171 : powi_cbrt_x = cbrt_x;
2172 : else
2173 2 : powi_cbrt_x = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
2174 : cbrt_x, cbrt_x);
2175 :
2176 : /* Multiply the two subexpressions, unless powi(x,abs(n)/3) = 1. */
2177 4 : if (absu_hwi (n) < 3)
2178 : result = powi_cbrt_x;
2179 : else
2180 4 : result = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR,
2181 : powi_x_ndiv3, powi_cbrt_x);
2182 :
2183 : /* If n is negative, reciprocate the result. */
2184 4 : if (n < 0)
2185 1 : result = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR,
2186 : build_real (type, dconst1), result);
2187 :
2188 : return result;
2189 : }
2190 :
2191 : /* No optimizations succeeded. */
2192 : return NULL_TREE;
2193 : }
2194 :
2195 : /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1
2196 : on the SSA_NAME argument of each of them. */
2197 :
2198 : namespace {
2199 :
2200 : const pass_data pass_data_cse_sincos =
2201 : {
2202 : GIMPLE_PASS, /* type */
2203 : "sincos", /* name */
2204 : OPTGROUP_NONE, /* optinfo_flags */
2205 : TV_TREE_SINCOS, /* tv_id */
2206 : PROP_ssa, /* properties_required */
2207 : 0, /* properties_provided */
2208 : 0, /* properties_destroyed */
2209 : 0, /* todo_flags_start */
2210 : TODO_update_ssa, /* todo_flags_finish */
2211 : };
2212 :
2213 : class pass_cse_sincos : public gimple_opt_pass
2214 : {
2215 : public:
2216 302985 : pass_cse_sincos (gcc::context *ctxt)
2217 605970 : : gimple_opt_pass (pass_data_cse_sincos, ctxt)
2218 : {}
2219 :
2220 : /* opt_pass methods: */
2221 1068301 : bool gate (function *) final override
2222 : {
2223 1068301 : return optimize;
2224 : }
2225 :
2226 : unsigned int execute (function *) final override;
2227 :
2228 : }; // class pass_cse_sincos
2229 :
2230 : unsigned int
2231 1068253 : pass_cse_sincos::execute (function *fun)
2232 : {
2233 1068253 : basic_block bb;
2234 1068253 : bool cfg_changed = false;
2235 :
2236 1068253 : calculate_dominance_info (CDI_DOMINATORS);
2237 1068253 : memset (&sincos_stats, 0, sizeof (sincos_stats));
2238 :
2239 11291834 : FOR_EACH_BB_FN (bb, fun)
2240 : {
2241 10223581 : gimple_stmt_iterator gsi;
2242 :
2243 99602298 : for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2244 : {
2245 89378717 : gimple *stmt = gsi_stmt (gsi);
2246 :
2247 89378717 : if (is_gimple_call (stmt)
2248 89378717 : && gimple_call_lhs (stmt))
2249 : {
2250 2078544 : tree arg;
2251 2078544 : switch (gimple_call_combined_fn (stmt))
2252 : {
2253 1069 : CASE_CFN_COS:
2254 1069 : CASE_CFN_SIN:
2255 1069 : CASE_CFN_CEXPI:
2256 1069 : arg = gimple_call_arg (stmt, 0);
2257 : /* Make sure we have either sincos or cexp. */
2258 1069 : if (!targetm.libc_has_function (function_c99_math_complex,
2259 1069 : TREE_TYPE (arg))
2260 1069 : && !targetm.libc_has_function (function_sincos,
2261 0 : TREE_TYPE (arg)))
2262 : break;
2263 :
2264 1069 : if (TREE_CODE (arg) == SSA_NAME)
2265 1069 : cfg_changed |= execute_cse_sincos_1 (arg);
2266 : break;
2267 : default:
2268 : break;
2269 : }
2270 : }
2271 : }
2272 : }
2273 :
2274 1068253 : statistics_counter_event (fun, "sincos statements inserted",
2275 : sincos_stats.inserted);
2276 1068253 : statistics_counter_event (fun, "conv statements removed",
2277 : sincos_stats.conv_removed);
2278 :
2279 1068253 : return cfg_changed ? TODO_cleanup_cfg : 0;
2280 : }
2281 :
2282 : } // anon namespace
2283 :
2284 : gimple_opt_pass *
2285 302985 : make_pass_cse_sincos (gcc::context *ctxt)
2286 : {
2287 302985 : return new pass_cse_sincos (ctxt);
2288 : }
2289 :
2290 : /* Expand powi(x,n) into an optimal number of multiplies, when n is a
2291 : constant. */
2292 : namespace {
2293 :
2294 : const pass_data pass_data_expand_pow =
2295 : {
2296 : GIMPLE_PASS, /* type */
2297 : "pow", /* name */
2298 : OPTGROUP_NONE, /* optinfo_flags */
2299 : TV_TREE_POW, /* tv_id */
2300 : PROP_ssa, /* properties_required */
2301 : PROP_gimple_opt_math, /* properties_provided */
2302 : 0, /* properties_destroyed */
2303 : 0, /* todo_flags_start */
2304 : TODO_update_ssa, /* todo_flags_finish */
2305 : };
2306 :
2307 : class pass_expand_pow : public gimple_opt_pass
2308 : {
2309 : public:
2310 302985 : pass_expand_pow (gcc::context *ctxt)
2311 605970 : : gimple_opt_pass (pass_data_expand_pow, ctxt)
2312 : {}
2313 :
2314 : /* opt_pass methods: */
2315 1068301 : bool gate (function *) final override
2316 : {
2317 1068301 : return optimize;
2318 : }
2319 :
2320 : unsigned int execute (function *) final override;
2321 :
2322 : }; // class pass_expand_pow
2323 :
2324 : unsigned int
2325 1068296 : pass_expand_pow::execute (function *fun)
2326 : {
2327 1068296 : basic_block bb;
2328 1068296 : bool cfg_changed = false;
2329 :
2330 1068296 : calculate_dominance_info (CDI_DOMINATORS);
2331 :
2332 11631863 : FOR_EACH_BB_FN (bb, fun)
2333 : {
2334 10563567 : gimple_stmt_iterator gsi;
2335 10563567 : bool cleanup_eh = false;
2336 :
2337 97475305 : for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2338 : {
2339 86911738 : gimple *stmt = gsi_stmt (gsi);
2340 :
2341 : /* Only the last stmt in a bb could throw, no need to call
2342 : gimple_purge_dead_eh_edges if we change something in the middle
2343 : of a basic block. */
2344 86911738 : cleanup_eh = false;
2345 :
2346 86911738 : if (is_gimple_call (stmt)
2347 86911738 : && gimple_call_lhs (stmt))
2348 : {
2349 2056914 : tree arg0, arg1, result;
2350 2056914 : HOST_WIDE_INT n;
2351 2056914 : location_t loc;
2352 :
2353 2056914 : switch (gimple_call_combined_fn (stmt))
2354 : {
2355 760 : CASE_CFN_POW:
2356 760 : arg0 = gimple_call_arg (stmt, 0);
2357 760 : arg1 = gimple_call_arg (stmt, 1);
2358 :
2359 760 : loc = gimple_location (stmt);
2360 760 : result = gimple_expand_builtin_pow (&gsi, loc, arg0, arg1);
2361 :
2362 760 : if (result)
2363 : {
2364 61 : tree lhs = gimple_get_lhs (stmt);
2365 61 : gassign *new_stmt = gimple_build_assign (lhs, result);
2366 61 : gimple_set_location (new_stmt, loc);
2367 61 : unlink_stmt_vdef (stmt);
2368 61 : gsi_replace (&gsi, new_stmt, true);
2369 61 : cleanup_eh = true;
2370 122 : if (gimple_vdef (stmt))
2371 0 : release_ssa_name (gimple_vdef (stmt));
2372 : }
2373 : break;
2374 :
2375 808 : CASE_CFN_POWI:
2376 808 : arg0 = gimple_call_arg (stmt, 0);
2377 808 : arg1 = gimple_call_arg (stmt, 1);
2378 808 : loc = gimple_location (stmt);
2379 :
2380 808 : if (real_minus_onep (arg0))
2381 : {
2382 0 : tree t0, t1, cond, one, minus_one;
2383 0 : gassign *stmt;
2384 :
2385 0 : t0 = TREE_TYPE (arg0);
2386 0 : t1 = TREE_TYPE (arg1);
2387 0 : one = build_real (t0, dconst1);
2388 0 : minus_one = build_real (t0, dconstm1);
2389 :
2390 0 : cond = make_temp_ssa_name (t1, NULL, "powi_cond");
2391 0 : stmt = gimple_build_assign (cond, BIT_AND_EXPR,
2392 : arg1, build_int_cst (t1, 1));
2393 0 : gimple_set_location (stmt, loc);
2394 0 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
2395 :
2396 0 : result = make_temp_ssa_name (t0, NULL, "powi");
2397 0 : stmt = gimple_build_assign (result, COND_EXPR, cond,
2398 : minus_one, one);
2399 0 : gimple_set_location (stmt, loc);
2400 0 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
2401 : }
2402 : else
2403 : {
2404 808 : if (!tree_fits_shwi_p (arg1))
2405 : break;
2406 :
2407 596 : n = tree_to_shwi (arg1);
2408 596 : result = gimple_expand_builtin_powi (&gsi, loc, arg0, n);
2409 : }
2410 :
2411 596 : if (result)
2412 : {
2413 588 : tree lhs = gimple_get_lhs (stmt);
2414 588 : gassign *new_stmt = gimple_build_assign (lhs, result);
2415 588 : gimple_set_location (new_stmt, loc);
2416 588 : unlink_stmt_vdef (stmt);
2417 588 : gsi_replace (&gsi, new_stmt, true);
2418 588 : cleanup_eh = true;
2419 86912326 : if (gimple_vdef (stmt))
2420 0 : release_ssa_name (gimple_vdef (stmt));
2421 : }
2422 : break;
2423 :
2424 212 : default:;
2425 : }
2426 : }
2427 : }
2428 10563567 : if (cleanup_eh)
2429 3 : cfg_changed |= gimple_purge_dead_eh_edges (bb);
2430 : }
2431 :
2432 1068296 : return cfg_changed ? TODO_cleanup_cfg : 0;
2433 : }
2434 :
2435 : } // anon namespace
2436 :
2437 : gimple_opt_pass *
2438 302985 : make_pass_expand_pow (gcc::context *ctxt)
2439 : {
2440 302985 : return new pass_expand_pow (ctxt);
2441 : }
2442 :
2443 : /* Return true if stmt is a type conversion operation that can be stripped
2444 : when used in a widening multiply operation. */
2445 : static bool
2446 532855 : widening_mult_conversion_strippable_p (tree result_type, gassign *stmt)
2447 : {
2448 532855 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
2449 :
2450 532855 : if (TREE_CODE (result_type) == INTEGER_TYPE)
2451 : {
2452 532855 : tree op_type;
2453 532855 : tree inner_op_type;
2454 :
2455 532855 : if (!CONVERT_EXPR_CODE_P (rhs_code))
2456 : return false;
2457 :
2458 206236 : op_type = TREE_TYPE (gimple_assign_lhs (stmt));
2459 :
2460 : /* If the type of OP has the same precision as the result, then
2461 : we can strip this conversion. The multiply operation will be
2462 : selected to create the correct extension as a by-product. */
2463 206236 : if (TYPE_PRECISION (result_type) == TYPE_PRECISION (op_type))
2464 : return true;
2465 :
2466 : /* We can also strip a conversion if it preserves the signed-ness of
2467 : the operation and doesn't narrow the range. */
2468 1167 : inner_op_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
2469 :
2470 : /* If the inner-most type is unsigned, then we can strip any
2471 : intermediate widening operation. If it's signed, then the
2472 : intermediate widening operation must also be signed. */
2473 1167 : if ((TYPE_UNSIGNED (inner_op_type)
2474 1163 : || TYPE_UNSIGNED (op_type) == TYPE_UNSIGNED (inner_op_type))
2475 2330 : && TYPE_PRECISION (op_type) > TYPE_PRECISION (inner_op_type))
2476 : return true;
2477 :
2478 1164 : return false;
2479 : }
2480 :
2481 0 : return rhs_code == FIXED_CONVERT_EXPR;
2482 : }
2483 :
2484 : /* Return true if RHS is a suitable operand for a widening multiplication,
2485 : assuming a target type of TYPE.
2486 : There are two cases:
2487 :
2488 : - RHS makes some value at least twice as wide. Store that value
2489 : in *NEW_RHS_OUT if so, and store its type in *TYPE_OUT.
2490 :
2491 : - RHS is an integer constant. Store that value in *NEW_RHS_OUT if so,
2492 : but leave *TYPE_OUT untouched. */
2493 :
2494 : static bool
2495 990079 : is_widening_mult_rhs_p (tree type, tree rhs, tree *type_out,
2496 : tree *new_rhs_out)
2497 : {
2498 990079 : gimple *stmt;
2499 990079 : tree type1, rhs1;
2500 :
2501 990079 : if (TREE_CODE (rhs) == SSA_NAME)
2502 : {
2503 : /* Use tree_non_zero_bits to see if this operand is zero_extended
2504 : for unsigned widening multiplications or non-negative for
2505 : signed widening multiplications. */
2506 822155 : if (TREE_CODE (type) == INTEGER_TYPE
2507 822155 : && (TYPE_PRECISION (type) & 1) == 0
2508 1644310 : && int_mode_for_size (TYPE_PRECISION (type) / 2, 1).exists ())
2509 : {
2510 815717 : unsigned int prec = TYPE_PRECISION (type);
2511 815717 : unsigned int hprec = prec / 2;
2512 815717 : wide_int bits = wide_int::from (tree_nonzero_bits (rhs), prec,
2513 1631434 : TYPE_SIGN (TREE_TYPE (rhs)));
2514 815717 : if (TYPE_UNSIGNED (type)
2515 1391309 : && wi::bit_and (bits, wi::mask (hprec, true, prec)) == 0)
2516 : {
2517 142505 : *type_out = build_nonstandard_integer_type (hprec, true);
2518 : /* X & MODE_MASK can be simplified to (T)X. */
2519 142505 : stmt = SSA_NAME_DEF_STMT (rhs);
2520 285010 : if (is_gimple_assign (stmt)
2521 127336 : && gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
2522 12331 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
2523 166567 : && wide_int::from (wi::to_wide (gimple_assign_rhs2 (stmt)),
2524 12031 : prec, TYPE_SIGN (TREE_TYPE (rhs)))
2525 178598 : == wi::mask (hprec, false, prec))
2526 10114 : *new_rhs_out = gimple_assign_rhs1 (stmt);
2527 : else
2528 : *new_rhs_out = rhs;
2529 142505 : return true;
2530 : }
2531 673212 : else if (!TYPE_UNSIGNED (type)
2532 913337 : && wi::bit_and (bits, wi::mask (hprec - 1, true, prec)) == 0)
2533 : {
2534 26162 : *type_out = build_nonstandard_integer_type (hprec, false);
2535 26162 : *new_rhs_out = rhs;
2536 26162 : return true;
2537 : }
2538 815717 : }
2539 :
2540 653488 : stmt = SSA_NAME_DEF_STMT (rhs);
2541 653488 : if (auto a_stmt = dyn_cast<gassign *> (stmt))
2542 : {
2543 :
2544 532855 : if (widening_mult_conversion_strippable_p (type, a_stmt))
2545 : {
2546 205072 : rhs1 = gimple_assign_rhs1 (a_stmt);
2547 :
2548 205072 : if (TREE_CODE (rhs1) == INTEGER_CST)
2549 : {
2550 0 : *new_rhs_out = rhs1;
2551 0 : *type_out = NULL;
2552 0 : return true;
2553 : }
2554 : }
2555 : else
2556 : rhs1 = rhs;
2557 : }
2558 : else
2559 : rhs1 = rhs;
2560 :
2561 653488 : type1 = TREE_TYPE (rhs1);
2562 :
2563 653488 : if (TREE_CODE (type1) != TREE_CODE (type)
2564 653488 : || TYPE_PRECISION (type1) * 2 > TYPE_PRECISION (type))
2565 : return false;
2566 :
2567 64866 : *new_rhs_out = rhs1;
2568 64866 : *type_out = type1;
2569 64866 : return true;
2570 : }
2571 :
2572 167924 : if (TREE_CODE (rhs) == INTEGER_CST)
2573 : {
2574 167924 : *new_rhs_out = rhs;
2575 167924 : *type_out = NULL;
2576 167924 : return true;
2577 : }
2578 :
2579 : return false;
2580 : }
2581 :
2582 : /* Return true if STMT performs a widening multiplication, assuming the
2583 : output type is TYPE. If so, store the unwidened types of the operands
2584 : in *TYPE1_OUT and *TYPE2_OUT respectively. Also fill *RHS1_OUT and
2585 : *RHS2_OUT such that converting those operands to types *TYPE1_OUT
2586 : and *TYPE2_OUT would give the operands of the multiplication. */
2587 :
2588 : static bool
2589 786374 : is_widening_mult_p (gimple *stmt,
2590 : tree *type1_out, tree *rhs1_out,
2591 : tree *type2_out, tree *rhs2_out)
2592 : {
2593 786374 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
2594 :
2595 786374 : if (TREE_CODE (type) == INTEGER_TYPE)
2596 : {
2597 786374 : if (TYPE_OVERFLOW_TRAPS (type))
2598 : return false;
2599 : }
2600 0 : else if (TREE_CODE (type) != FIXED_POINT_TYPE)
2601 : return false;
2602 :
2603 786334 : if (!is_widening_mult_rhs_p (type, gimple_assign_rhs1 (stmt), type1_out,
2604 : rhs1_out))
2605 : return false;
2606 :
2607 203745 : if (!is_widening_mult_rhs_p (type, gimple_assign_rhs2 (stmt), type2_out,
2608 : rhs2_out))
2609 : return false;
2610 :
2611 197712 : if (*type1_out == NULL)
2612 : {
2613 0 : if (*type2_out == NULL || !int_fits_type_p (*rhs1_out, *type2_out))
2614 : return false;
2615 0 : *type1_out = *type2_out;
2616 : }
2617 :
2618 197712 : if (*type2_out == NULL)
2619 : {
2620 167924 : if (!int_fits_type_p (*rhs2_out, *type1_out))
2621 : return false;
2622 164392 : *type2_out = *type1_out;
2623 : }
2624 :
2625 : /* Ensure that the larger of the two operands comes first. */
2626 194180 : if (TYPE_PRECISION (*type1_out) < TYPE_PRECISION (*type2_out))
2627 : {
2628 74 : std::swap (*type1_out, *type2_out);
2629 74 : std::swap (*rhs1_out, *rhs2_out);
2630 : }
2631 :
2632 : return true;
2633 : }
2634 :
2635 : /* Check to see if the CALL statement is an invocation of copysign
2636 : with 1. being the first argument. */
2637 : static bool
2638 207797 : is_copysign_call_with_1 (gimple *call)
2639 : {
2640 207797 : gcall *c = dyn_cast <gcall *> (call);
2641 4573 : if (! c)
2642 : return false;
2643 :
2644 4573 : enum combined_fn code = gimple_call_combined_fn (c);
2645 :
2646 4573 : if (code == CFN_LAST)
2647 : return false;
2648 :
2649 3536 : if (builtin_fn_p (code))
2650 : {
2651 1049 : switch (as_builtin_fn (code))
2652 : {
2653 30 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
2654 30 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
2655 30 : return real_onep (gimple_call_arg (c, 0));
2656 : default:
2657 : return false;
2658 : }
2659 : }
2660 :
2661 2487 : if (internal_fn_p (code))
2662 : {
2663 2487 : switch (as_internal_fn (code))
2664 : {
2665 24 : case IFN_COPYSIGN:
2666 24 : return real_onep (gimple_call_arg (c, 0));
2667 : default:
2668 : return false;
2669 : }
2670 : }
2671 :
2672 : return false;
2673 : }
2674 :
2675 : /* Try to expand the pattern x * copysign (1, y) into xorsign (x, y).
2676 : This only happens when the xorsign optab is defined, if the
2677 : pattern is not a xorsign pattern or if expansion fails FALSE is
2678 : returned, otherwise TRUE is returned. */
2679 : static bool
2680 769102 : convert_expand_mult_copysign (gassign *stmt, gimple_stmt_iterator *gsi)
2681 : {
2682 769102 : tree treeop0, treeop1, lhs, type;
2683 769102 : location_t loc = gimple_location (stmt);
2684 769102 : lhs = gimple_assign_lhs (stmt);
2685 769102 : treeop0 = gimple_assign_rhs1 (stmt);
2686 769102 : treeop1 = gimple_assign_rhs2 (stmt);
2687 769102 : type = TREE_TYPE (lhs);
2688 769102 : machine_mode mode = TYPE_MODE (type);
2689 :
2690 769102 : if (HONOR_SNANS (type))
2691 : return false;
2692 :
2693 768701 : if (TREE_CODE (treeop0) == SSA_NAME && TREE_CODE (treeop1) == SSA_NAME)
2694 : {
2695 248080 : gimple *call0 = SSA_NAME_DEF_STMT (treeop0);
2696 248080 : if (!has_single_use (treeop0) || !is_copysign_call_with_1 (call0))
2697 : {
2698 248054 : call0 = SSA_NAME_DEF_STMT (treeop1);
2699 248054 : if (!has_single_use (treeop1) || !is_copysign_call_with_1 (call0))
2700 : return false;
2701 :
2702 : treeop1 = treeop0;
2703 : }
2704 43 : if (optab_handler (xorsign_optab, mode) == CODE_FOR_nothing)
2705 : return false;
2706 :
2707 43 : gcall *c = as_a<gcall*> (call0);
2708 43 : treeop0 = gimple_call_arg (c, 1);
2709 :
2710 43 : gcall *call_stmt
2711 43 : = gimple_build_call_internal (IFN_XORSIGN, 2, treeop1, treeop0);
2712 43 : gimple_set_lhs (call_stmt, lhs);
2713 43 : gimple_set_location (call_stmt, loc);
2714 43 : gsi_replace (gsi, call_stmt, true);
2715 43 : return true;
2716 : }
2717 :
2718 : return false;
2719 : }
2720 :
2721 : /* Process a single gimple statement STMT, which has a MULT_EXPR as
2722 : its rhs, and try to convert it into a WIDEN_MULT_EXPR. The return
2723 : value is true iff we converted the statement. */
2724 :
2725 : static bool
2726 778100 : convert_mult_to_widen (gassign *stmt, gimple_stmt_iterator *gsi)
2727 : {
2728 778100 : tree lhs, rhs1, rhs2, type, type1, type2;
2729 778100 : enum insn_code handler;
2730 778100 : scalar_int_mode to_mode, from_mode, actual_mode;
2731 778100 : optab op;
2732 778100 : int actual_precision;
2733 778100 : location_t loc = gimple_location (stmt);
2734 778100 : bool from_unsigned1, from_unsigned2;
2735 :
2736 778100 : lhs = gimple_assign_lhs (stmt);
2737 778100 : type = TREE_TYPE (lhs);
2738 778100 : if (TREE_CODE (type) != INTEGER_TYPE)
2739 : return false;
2740 :
2741 638576 : if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2))
2742 : return false;
2743 :
2744 : /* if any one of rhs1 and rhs2 is subject to abnormal coalescing,
2745 : avoid the transform. */
2746 160267 : if ((TREE_CODE (rhs1) == SSA_NAME
2747 160267 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
2748 320533 : || (TREE_CODE (rhs2) == SSA_NAME
2749 22808 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2)))
2750 : return false;
2751 :
2752 160266 : to_mode = SCALAR_INT_TYPE_MODE (type);
2753 160266 : from_mode = SCALAR_INT_TYPE_MODE (type1);
2754 160266 : if (to_mode == from_mode)
2755 : return false;
2756 :
2757 160262 : from_unsigned1 = TYPE_UNSIGNED (type1);
2758 160262 : from_unsigned2 = TYPE_UNSIGNED (type2);
2759 :
2760 160262 : if (from_unsigned1 && from_unsigned2)
2761 : op = umul_widen_optab;
2762 62287 : else if (!from_unsigned1 && !from_unsigned2)
2763 : op = smul_widen_optab;
2764 : else
2765 1916 : op = usmul_widen_optab;
2766 :
2767 160262 : handler = find_widening_optab_handler_and_mode (op, to_mode, from_mode,
2768 : &actual_mode);
2769 :
2770 160262 : if (handler == CODE_FOR_nothing)
2771 : {
2772 151264 : if (op != smul_widen_optab)
2773 : {
2774 : /* We can use a signed multiply with unsigned types as long as
2775 : there is a wider mode to use, or it is the smaller of the two
2776 : types that is unsigned. Note that type1 >= type2, always. */
2777 92393 : if ((TYPE_UNSIGNED (type1)
2778 90668 : && TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode))
2779 92393 : || (TYPE_UNSIGNED (type2)
2780 1725 : && TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode)))
2781 : {
2782 92393 : if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode)
2783 184786 : || GET_MODE_SIZE (to_mode) <= GET_MODE_SIZE (from_mode))
2784 : return false;
2785 : }
2786 :
2787 0 : op = smul_widen_optab;
2788 0 : handler = find_widening_optab_handler_and_mode (op, to_mode,
2789 : from_mode,
2790 : &actual_mode);
2791 :
2792 0 : if (handler == CODE_FOR_nothing)
2793 : return false;
2794 :
2795 : from_unsigned1 = from_unsigned2 = false;
2796 : }
2797 : else
2798 : {
2799 : /* Expand can synthesize smul_widen_optab if the target
2800 : supports umul_widen_optab. */
2801 58871 : op = umul_widen_optab;
2802 58871 : handler = find_widening_optab_handler_and_mode (op, to_mode,
2803 : from_mode,
2804 : &actual_mode);
2805 58871 : if (handler == CODE_FOR_nothing)
2806 : return false;
2807 : }
2808 : }
2809 :
2810 : /* Ensure that the inputs to the handler are in the correct precision
2811 : for the opcode. This will be the full mode size. */
2812 8998 : actual_precision = GET_MODE_PRECISION (actual_mode);
2813 8998 : if (2 * actual_precision > TYPE_PRECISION (type))
2814 : return false;
2815 8998 : if (actual_precision != TYPE_PRECISION (type1)
2816 8998 : || from_unsigned1 != TYPE_UNSIGNED (type1))
2817 : {
2818 3 : if (!useless_type_conversion_p (type1, TREE_TYPE (rhs1)))
2819 : {
2820 0 : if (TREE_CODE (rhs1) == INTEGER_CST)
2821 0 : rhs1 = fold_convert (type1, rhs1);
2822 : else
2823 0 : rhs1 = build_and_insert_cast (gsi, loc, type1, rhs1);
2824 : }
2825 3 : type1 = build_nonstandard_integer_type (actual_precision,
2826 : from_unsigned1);
2827 : }
2828 8998 : if (!useless_type_conversion_p (type1, TREE_TYPE (rhs1)))
2829 : {
2830 8263 : if (TREE_CODE (rhs1) == INTEGER_CST)
2831 0 : rhs1 = fold_convert (type1, rhs1);
2832 : else
2833 8263 : rhs1 = build_and_insert_cast (gsi, loc, type1, rhs1);
2834 : }
2835 8998 : if (actual_precision != TYPE_PRECISION (type2)
2836 8998 : || from_unsigned2 != TYPE_UNSIGNED (type2))
2837 : {
2838 3 : if (!useless_type_conversion_p (type2, TREE_TYPE (rhs2)))
2839 : {
2840 3 : if (TREE_CODE (rhs2) == INTEGER_CST)
2841 3 : rhs2 = fold_convert (type2, rhs2);
2842 : else
2843 0 : rhs2 = build_and_insert_cast (gsi, loc, type2, rhs2);
2844 : }
2845 3 : type2 = build_nonstandard_integer_type (actual_precision,
2846 : from_unsigned2);
2847 : }
2848 8998 : if (!useless_type_conversion_p (type2, TREE_TYPE (rhs2)))
2849 : {
2850 8456 : if (TREE_CODE (rhs2) == INTEGER_CST)
2851 1918 : rhs2 = fold_convert (type2, rhs2);
2852 : else
2853 6538 : rhs2 = build_and_insert_cast (gsi, loc, type2, rhs2);
2854 : }
2855 :
2856 8998 : gimple_assign_set_rhs1 (stmt, rhs1);
2857 8998 : gimple_assign_set_rhs2 (stmt, rhs2);
2858 8998 : gimple_assign_set_rhs_code (stmt, WIDEN_MULT_EXPR);
2859 8998 : update_stmt (stmt);
2860 8998 : widen_mul_stats.widen_mults_inserted++;
2861 8998 : return true;
2862 : }
2863 :
2864 : /* Process a single gimple statement STMT, which is found at the
2865 : iterator GSI and has a either a PLUS_EXPR or a MINUS_EXPR as its
2866 : rhs (given by CODE), and try to convert it into a
2867 : WIDEN_MULT_PLUS_EXPR or a WIDEN_MULT_MINUS_EXPR. The return value
2868 : is true iff we converted the statement. */
2869 :
2870 : static bool
2871 2685652 : convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple *stmt,
2872 : enum tree_code code)
2873 : {
2874 2685652 : gimple *rhs1_stmt = NULL, *rhs2_stmt = NULL;
2875 2685652 : gimple *conv1_stmt = NULL, *conv2_stmt = NULL, *conv_stmt;
2876 2685652 : tree type, type1, type2, optype;
2877 2685652 : tree lhs, rhs1, rhs2, mult_rhs1, mult_rhs2, add_rhs;
2878 2685652 : enum tree_code rhs1_code = ERROR_MARK, rhs2_code = ERROR_MARK;
2879 2685652 : optab this_optab;
2880 2685652 : enum tree_code wmult_code;
2881 2685652 : enum insn_code handler;
2882 2685652 : scalar_mode to_mode, from_mode, actual_mode;
2883 2685652 : location_t loc = gimple_location (stmt);
2884 2685652 : int actual_precision;
2885 2685652 : bool from_unsigned1, from_unsigned2;
2886 :
2887 2685652 : lhs = gimple_assign_lhs (stmt);
2888 2685652 : type = TREE_TYPE (lhs);
2889 2685652 : if ((TREE_CODE (type) != INTEGER_TYPE
2890 405347 : && TREE_CODE (type) != FIXED_POINT_TYPE)
2891 2685652 : || !type_has_mode_precision_p (type))
2892 : return false;
2893 :
2894 2277573 : if (code == MINUS_EXPR)
2895 : wmult_code = WIDEN_MULT_MINUS_EXPR;
2896 : else
2897 2020136 : wmult_code = WIDEN_MULT_PLUS_EXPR;
2898 :
2899 2277573 : rhs1 = gimple_assign_rhs1 (stmt);
2900 2277573 : rhs2 = gimple_assign_rhs2 (stmt);
2901 :
2902 2277573 : if (TREE_CODE (rhs1) == SSA_NAME)
2903 : {
2904 2245389 : rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
2905 2245389 : if (is_gimple_assign (rhs1_stmt))
2906 1337238 : rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
2907 : }
2908 :
2909 2277573 : if (TREE_CODE (rhs2) == SSA_NAME)
2910 : {
2911 845647 : rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
2912 845647 : if (is_gimple_assign (rhs2_stmt))
2913 649770 : rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
2914 : }
2915 :
2916 : /* Allow for one conversion statement between the multiply
2917 : and addition/subtraction statement. If there are more than
2918 : one conversions then we assume they would invalidate this
2919 : transformation. If that's not the case then they should have
2920 : been folded before now. */
2921 2277573 : if (CONVERT_EXPR_CODE_P (rhs1_code))
2922 : {
2923 425492 : conv1_stmt = rhs1_stmt;
2924 425492 : rhs1 = gimple_assign_rhs1 (rhs1_stmt);
2925 425492 : if (TREE_CODE (rhs1) == SSA_NAME)
2926 : {
2927 354604 : rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
2928 354604 : if (is_gimple_assign (rhs1_stmt))
2929 211940 : rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
2930 : }
2931 : else
2932 : return false;
2933 : }
2934 2206685 : if (CONVERT_EXPR_CODE_P (rhs2_code))
2935 : {
2936 196417 : conv2_stmt = rhs2_stmt;
2937 196417 : rhs2 = gimple_assign_rhs1 (rhs2_stmt);
2938 196417 : if (TREE_CODE (rhs2) == SSA_NAME)
2939 : {
2940 186949 : rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
2941 186949 : if (is_gimple_assign (rhs2_stmt))
2942 122910 : rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
2943 : }
2944 : else
2945 : return false;
2946 : }
2947 :
2948 : /* If code is WIDEN_MULT_EXPR then it would seem unnecessary to call
2949 : is_widening_mult_p, but we still need the rhs returns.
2950 :
2951 : It might also appear that it would be sufficient to use the existing
2952 : operands of the widening multiply, but that would limit the choice of
2953 : multiply-and-accumulate instructions.
2954 :
2955 : If the widened-multiplication result has more than one uses, it is
2956 : probably wiser not to do the conversion. Also restrict this operation
2957 : to single basic block to avoid moving the multiply to a different block
2958 : with a higher execution frequency. */
2959 2197217 : if (code == PLUS_EXPR
2960 1944608 : && (rhs1_code == MULT_EXPR || rhs1_code == WIDEN_MULT_EXPR))
2961 : {
2962 157062 : if (!has_single_use (rhs1)
2963 84247 : || (conv1_stmt
2964 5311 : && !has_single_use (gimple_assign_lhs (conv1_stmt)))
2965 81460 : || gimple_bb (rhs1_stmt) != gimple_bb (stmt)
2966 229278 : || !is_widening_mult_p (rhs1_stmt, &type1, &mult_rhs1,
2967 : &type2, &mult_rhs2))
2968 : return false;
2969 : add_rhs = rhs2;
2970 : conv_stmt = conv1_stmt;
2971 : }
2972 2040155 : else if (rhs2_code == MULT_EXPR || rhs2_code == WIDEN_MULT_EXPR)
2973 : {
2974 131584 : if (!has_single_use (rhs2)
2975 84811 : || (conv2_stmt
2976 9140 : && !has_single_use (gimple_assign_lhs (conv2_stmt)))
2977 83657 : || gimple_bb (rhs2_stmt) != gimple_bb (stmt)
2978 207166 : || !is_widening_mult_p (rhs2_stmt, &type1, &mult_rhs1,
2979 : &type2, &mult_rhs2))
2980 : return false;
2981 : add_rhs = rhs1;
2982 : conv_stmt = conv2_stmt;
2983 : }
2984 : else
2985 : return false;
2986 :
2987 33913 : to_mode = SCALAR_TYPE_MODE (type);
2988 33913 : from_mode = SCALAR_TYPE_MODE (type1);
2989 33913 : if (to_mode == from_mode)
2990 : return false;
2991 :
2992 : /* For fixed point types, the mode classes could be different
2993 : so reject that case. */
2994 33912 : if (GET_MODE_CLASS (from_mode) != GET_MODE_CLASS (to_mode))
2995 : return false;
2996 :
2997 33912 : from_unsigned1 = TYPE_UNSIGNED (type1);
2998 33912 : from_unsigned2 = TYPE_UNSIGNED (type2);
2999 33912 : optype = type1;
3000 :
3001 : /* There's no such thing as a mixed sign madd yet, so use a wider mode. */
3002 33912 : if (from_unsigned1 != from_unsigned2)
3003 : {
3004 928 : if (!INTEGRAL_TYPE_P (type))
3005 : return false;
3006 : /* We can use a signed multiply with unsigned types as long as
3007 : there is a wider mode to use, or it is the smaller of the two
3008 : types that is unsigned. Note that type1 >= type2, always. */
3009 928 : if ((from_unsigned1
3010 54 : && TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode))
3011 928 : || (from_unsigned2
3012 874 : && TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode)))
3013 : {
3014 928 : if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode)
3015 1856 : || GET_MODE_SIZE (from_mode) >= GET_MODE_SIZE (to_mode))
3016 : return false;
3017 : }
3018 :
3019 18 : from_unsigned1 = from_unsigned2 = false;
3020 18 : optype = build_nonstandard_integer_type (GET_MODE_PRECISION (from_mode),
3021 : false);
3022 : }
3023 :
3024 : /* If there was a conversion between the multiply and addition
3025 : then we need to make sure it fits a multiply-and-accumulate.
3026 : The should be a single mode change which does not change the
3027 : value. */
3028 33002 : if (conv_stmt)
3029 : {
3030 : /* We use the original, unmodified data types for this. */
3031 701 : tree from_type = TREE_TYPE (gimple_assign_rhs1 (conv_stmt));
3032 701 : tree to_type = TREE_TYPE (gimple_assign_lhs (conv_stmt));
3033 701 : int data_size = TYPE_PRECISION (type1) + TYPE_PRECISION (type2);
3034 701 : bool is_unsigned = TYPE_UNSIGNED (type1) && TYPE_UNSIGNED (type2);
3035 :
3036 701 : if (TYPE_PRECISION (from_type) > TYPE_PRECISION (to_type))
3037 : {
3038 : /* Conversion is a truncate. */
3039 0 : if (TYPE_PRECISION (to_type) < data_size)
3040 : return false;
3041 : }
3042 701 : else if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type))
3043 : {
3044 : /* Conversion is an extend. Check it's the right sort. */
3045 367 : if (TYPE_UNSIGNED (from_type) != is_unsigned
3046 367 : && !(is_unsigned && TYPE_PRECISION (from_type) > data_size))
3047 : return false;
3048 : }
3049 : /* else convert is a no-op for our purposes. */
3050 : }
3051 :
3052 : /* Verify that the machine can perform a widening multiply
3053 : accumulate in this mode/signedness combination, otherwise
3054 : this transformation is likely to pessimize code. */
3055 32710 : this_optab = optab_for_tree_code (wmult_code, optype, optab_default);
3056 32710 : handler = find_widening_optab_handler_and_mode (this_optab, to_mode,
3057 : from_mode, &actual_mode);
3058 :
3059 32710 : if (handler == CODE_FOR_nothing)
3060 : return false;
3061 :
3062 : /* Ensure that the inputs to the handler are in the correct precision
3063 : for the opcode. This will be the full mode size. */
3064 0 : actual_precision = GET_MODE_PRECISION (actual_mode);
3065 0 : if (actual_precision != TYPE_PRECISION (type1)
3066 0 : || from_unsigned1 != TYPE_UNSIGNED (type1))
3067 : {
3068 0 : if (!useless_type_conversion_p (type1, TREE_TYPE (mult_rhs1)))
3069 : {
3070 0 : if (TREE_CODE (mult_rhs1) == INTEGER_CST)
3071 0 : mult_rhs1 = fold_convert (type1, mult_rhs1);
3072 : else
3073 0 : mult_rhs1 = build_and_insert_cast (gsi, loc, type1, mult_rhs1);
3074 : }
3075 0 : type1 = build_nonstandard_integer_type (actual_precision,
3076 : from_unsigned1);
3077 : }
3078 0 : if (!useless_type_conversion_p (type1, TREE_TYPE (mult_rhs1)))
3079 : {
3080 0 : if (TREE_CODE (mult_rhs1) == INTEGER_CST)
3081 0 : mult_rhs1 = fold_convert (type1, mult_rhs1);
3082 : else
3083 0 : mult_rhs1 = build_and_insert_cast (gsi, loc, type1, mult_rhs1);
3084 : }
3085 0 : if (actual_precision != TYPE_PRECISION (type2)
3086 0 : || from_unsigned2 != TYPE_UNSIGNED (type2))
3087 : {
3088 0 : if (!useless_type_conversion_p (type2, TREE_TYPE (mult_rhs2)))
3089 : {
3090 0 : if (TREE_CODE (mult_rhs2) == INTEGER_CST)
3091 0 : mult_rhs2 = fold_convert (type2, mult_rhs2);
3092 : else
3093 0 : mult_rhs2 = build_and_insert_cast (gsi, loc, type2, mult_rhs2);
3094 : }
3095 0 : type2 = build_nonstandard_integer_type (actual_precision,
3096 : from_unsigned2);
3097 : }
3098 0 : if (!useless_type_conversion_p (type2, TREE_TYPE (mult_rhs2)))
3099 : {
3100 0 : if (TREE_CODE (mult_rhs2) == INTEGER_CST)
3101 0 : mult_rhs2 = fold_convert (type2, mult_rhs2);
3102 : else
3103 0 : mult_rhs2 = build_and_insert_cast (gsi, loc, type2, mult_rhs2);
3104 : }
3105 :
3106 0 : if (!useless_type_conversion_p (type, TREE_TYPE (add_rhs)))
3107 0 : add_rhs = build_and_insert_cast (gsi, loc, type, add_rhs);
3108 :
3109 0 : gimple_assign_set_rhs_with_ops (gsi, wmult_code, mult_rhs1, mult_rhs2,
3110 : add_rhs);
3111 0 : update_stmt (gsi_stmt (*gsi));
3112 0 : widen_mul_stats.maccs_inserted++;
3113 0 : return true;
3114 : }
3115 :
3116 : /* Given a result MUL_RESULT which is a result of a multiplication of OP1 and
3117 : OP2 and which we know is used in statements that can be, together with the
3118 : multiplication, converted to FMAs, perform the transformation. */
3119 :
3120 : static void
3121 17716 : convert_mult_to_fma_1 (tree mul_result, tree op1, tree op2)
3122 : {
3123 17716 : gimple *use_stmt;
3124 17716 : imm_use_iterator imm_iter;
3125 17716 : gcall *fma_stmt;
3126 :
3127 35495 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, mul_result)
3128 : {
3129 17779 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
3130 17779 : tree addop, mulop1 = op1, result = mul_result;
3131 17779 : bool negate_p = false;
3132 17779 : gimple_seq seq = NULL;
3133 :
3134 17779 : if (is_gimple_debug (use_stmt))
3135 0 : continue;
3136 :
3137 : /* If the use is a type convert, look further into it if the operations
3138 : are the same under two's complement. */
3139 17779 : tree lhs_type;
3140 17779 : if (gimple_assign_cast_p (use_stmt)
3141 0 : && (lhs_type = TREE_TYPE (gimple_get_lhs (use_stmt)))
3142 17779 : && tree_nop_conversion_p (lhs_type, TREE_TYPE (op1)))
3143 : {
3144 0 : tree cast_lhs = gimple_get_lhs (use_stmt);
3145 0 : gimple *tmp_use;
3146 0 : use_operand_p tmp_use_p;
3147 0 : if (single_imm_use (cast_lhs, &tmp_use_p, &tmp_use))
3148 : {
3149 0 : release_defs (use_stmt);
3150 0 : use_stmt = tmp_use;
3151 0 : result = cast_lhs;
3152 0 : gsi_remove (&gsi, true);
3153 0 : gsi = gsi_for_stmt (use_stmt);
3154 : }
3155 : }
3156 :
3157 17779 : auto use_assign = dyn_cast<gassign *> (use_stmt);
3158 17641 : if (use_assign && gimple_assign_rhs_code (use_assign) == NEGATE_EXPR)
3159 : {
3160 508 : result = gimple_assign_lhs (use_assign);
3161 508 : use_operand_p use_p;
3162 508 : gimple *neguse_stmt;
3163 508 : single_imm_use (gimple_assign_lhs (use_assign), &use_p, &neguse_stmt);
3164 508 : gsi_remove (&gsi, true);
3165 508 : release_defs (use_stmt);
3166 :
3167 508 : use_stmt = neguse_stmt;
3168 508 : gsi = gsi_for_stmt (use_stmt);
3169 508 : negate_p = true;
3170 : }
3171 :
3172 17779 : tree cond, else_value, ops[3], len, bias;
3173 17779 : tree_code code;
3174 17779 : if (!can_interpret_as_conditional_op_p (use_stmt, &cond, &code,
3175 : ops, &else_value,
3176 : &len, &bias))
3177 0 : gcc_unreachable ();
3178 17779 : addop = ops[0] == result ? ops[1] : ops[0];
3179 :
3180 17779 : if (code == MINUS_EXPR)
3181 : {
3182 5694 : if (ops[0] == result)
3183 : /* a * b - c -> a * b + (-c) */
3184 2757 : addop = gimple_build (&seq, NEGATE_EXPR, TREE_TYPE (addop), addop);
3185 : else
3186 : /* a - b * c -> (-b) * c + a */
3187 2937 : negate_p = !negate_p;
3188 : }
3189 :
3190 17779 : if (negate_p)
3191 3445 : mulop1 = gimple_build (&seq, NEGATE_EXPR, TREE_TYPE (mulop1), mulop1);
3192 :
3193 17779 : if (seq)
3194 5689 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
3195 :
3196 : /* Ensure all the operands are of the same type. Use the type of the
3197 : addend as that's the statement being replaced. */
3198 17779 : op2 = gimple_convert (&gsi, true, GSI_SAME_STMT,
3199 17779 : UNKNOWN_LOCATION, TREE_TYPE (addop), op2);
3200 17779 : mulop1 = gimple_convert (&gsi, true, GSI_SAME_STMT,
3201 17779 : UNKNOWN_LOCATION, TREE_TYPE (addop), mulop1);
3202 :
3203 17779 : if (len)
3204 0 : fma_stmt
3205 0 : = gimple_build_call_internal (IFN_COND_LEN_FMA, 7, cond, mulop1, op2,
3206 : addop, else_value, len, bias);
3207 17779 : else if (cond)
3208 150 : fma_stmt = gimple_build_call_internal (IFN_COND_FMA, 5, cond, mulop1,
3209 : op2, addop, else_value);
3210 : else
3211 17629 : fma_stmt = gimple_build_call_internal (IFN_FMA, 3, mulop1, op2, addop);
3212 17779 : gimple_set_lhs (fma_stmt, gimple_get_lhs (use_stmt));
3213 17779 : gimple_call_set_nothrow (fma_stmt, !stmt_can_throw_internal (cfun,
3214 : use_stmt));
3215 17779 : gsi_replace (&gsi, fma_stmt, true);
3216 : /* Follow all SSA edges so that we generate FMS, FNMA and FNMS
3217 : regardless of where the negation occurs. */
3218 17779 : gimple *orig_stmt = gsi_stmt (gsi);
3219 17779 : if (fold_stmt (&gsi, follow_all_ssa_edges))
3220 : {
3221 6026 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, gsi_stmt (gsi)))
3222 0 : gcc_unreachable ();
3223 6026 : update_stmt (gsi_stmt (gsi));
3224 : }
3225 :
3226 17779 : if (dump_file && (dump_flags & TDF_DETAILS))
3227 : {
3228 3 : fprintf (dump_file, "Generated FMA ");
3229 3 : print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, TDF_NONE);
3230 3 : fprintf (dump_file, "\n");
3231 : }
3232 :
3233 : /* If the FMA result is negated in a single use, fold the negation
3234 : too. */
3235 17779 : orig_stmt = gsi_stmt (gsi);
3236 17779 : use_operand_p use_p;
3237 17779 : gimple *neg_stmt;
3238 17779 : if (is_gimple_call (orig_stmt)
3239 17779 : && gimple_call_internal_p (orig_stmt)
3240 17779 : && gimple_call_lhs (orig_stmt)
3241 17779 : && TREE_CODE (gimple_call_lhs (orig_stmt)) == SSA_NAME
3242 17779 : && single_imm_use (gimple_call_lhs (orig_stmt), &use_p, &neg_stmt)
3243 12628 : && is_gimple_assign (neg_stmt)
3244 9958 : && gimple_assign_rhs_code (neg_stmt) == NEGATE_EXPR
3245 18556 : && !stmt_could_throw_p (cfun, neg_stmt))
3246 : {
3247 777 : gsi = gsi_for_stmt (neg_stmt);
3248 777 : if (fold_stmt (&gsi, follow_all_ssa_edges))
3249 : {
3250 201 : if (maybe_clean_or_replace_eh_stmt (neg_stmt, gsi_stmt (gsi)))
3251 0 : gcc_unreachable ();
3252 201 : update_stmt (gsi_stmt (gsi));
3253 201 : if (dump_file && (dump_flags & TDF_DETAILS))
3254 : {
3255 0 : fprintf (dump_file, "Folded FMA negation ");
3256 0 : print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, TDF_NONE);
3257 0 : fprintf (dump_file, "\n");
3258 : }
3259 : }
3260 : }
3261 :
3262 17779 : widen_mul_stats.fmas_inserted++;
3263 17716 : }
3264 17716 : }
3265 :
3266 : /* Data necessary to perform the actual transformation from a multiplication
3267 : and an addition to an FMA after decision is taken it should be done and to
3268 : then delete the multiplication statement from the function IL. */
3269 :
3270 : struct fma_transformation_info
3271 : {
3272 : gimple *mul_stmt;
3273 : tree mul_result;
3274 : tree op1;
3275 : tree op2;
3276 : };
3277 :
3278 : /* Structure containing the current state of FMA deferring, i.e. whether we are
3279 : deferring, whether to continue deferring, and all data necessary to come
3280 : back and perform all deferred transformations. */
3281 :
3282 10544529 : class fma_deferring_state
3283 : {
3284 : public:
3285 : /* Class constructor. Pass true as PERFORM_DEFERRING in order to actually
3286 : do any deferring. */
3287 :
3288 10544529 : fma_deferring_state (bool perform_deferring)
3289 10544529 : : m_candidates (), m_mul_result_set (), m_initial_phi (NULL),
3290 10544529 : m_last_result (NULL_TREE), m_deferring_p (perform_deferring) {}
3291 :
3292 : /* List of FMA candidates for which we the transformation has been determined
3293 : possible but we at this point in BB analysis we do not consider them
3294 : beneficial. */
3295 : auto_vec<fma_transformation_info, 8> m_candidates;
3296 :
3297 : /* Set of results of multiplication that are part of an already deferred FMA
3298 : candidates. */
3299 : hash_set<tree> m_mul_result_set;
3300 :
3301 : /* The PHI that supposedly feeds back result of a FMA to another over loop
3302 : boundary. */
3303 : gphi *m_initial_phi;
3304 :
3305 : /* Result of the last produced FMA candidate or NULL if there has not been
3306 : one. */
3307 : tree m_last_result;
3308 :
3309 : /* If true, deferring might still be profitable. If false, transform all
3310 : candidates and no longer defer. */
3311 : bool m_deferring_p;
3312 : };
3313 :
3314 : /* Transform all deferred FMA candidates and mark STATE as no longer
3315 : deferring. */
3316 :
3317 : static void
3318 3754018 : cancel_fma_deferring (fma_deferring_state *state)
3319 : {
3320 3754018 : if (!state->m_deferring_p)
3321 : return;
3322 :
3323 2707699 : for (unsigned i = 0; i < state->m_candidates.length (); i++)
3324 : {
3325 949 : if (dump_file && (dump_flags & TDF_DETAILS))
3326 0 : fprintf (dump_file, "Generating deferred FMA\n");
3327 :
3328 949 : const fma_transformation_info &fti = state->m_candidates[i];
3329 949 : convert_mult_to_fma_1 (fti.mul_result, fti.op1, fti.op2);
3330 :
3331 949 : gimple_stmt_iterator gsi = gsi_for_stmt (fti.mul_stmt);
3332 949 : gsi_remove (&gsi, true);
3333 949 : release_defs (fti.mul_stmt);
3334 : }
3335 2706750 : state->m_deferring_p = false;
3336 : }
3337 :
3338 : /* If OP is an SSA name defined by a PHI node, return the PHI statement.
3339 : Otherwise return NULL. */
3340 :
3341 : static gphi *
3342 5370 : result_of_phi (tree op)
3343 : {
3344 0 : if (TREE_CODE (op) != SSA_NAME)
3345 : return NULL;
3346 :
3347 5245 : return dyn_cast <gphi *> (SSA_NAME_DEF_STMT (op));
3348 : }
3349 :
3350 : /* After processing statements of a BB and recording STATE, return true if the
3351 : initial phi is fed by the last FMA candidate result ore one such result from
3352 : previously processed BBs marked in LAST_RESULT_SET. */
3353 :
3354 : static bool
3355 373 : last_fma_candidate_feeds_initial_phi (fma_deferring_state *state,
3356 : hash_set<tree> *last_result_set)
3357 : {
3358 373 : ssa_op_iter iter;
3359 373 : use_operand_p use;
3360 917 : FOR_EACH_PHI_ARG (use, state->m_initial_phi, iter, SSA_OP_USE)
3361 : {
3362 645 : tree t = USE_FROM_PTR (use);
3363 645 : if (t == state->m_last_result
3364 645 : || last_result_set->contains (t))
3365 101 : return true;
3366 : }
3367 :
3368 : return false;
3369 : }
3370 :
3371 : /* Combine the multiplication at MUL_STMT with operands MULOP1 and MULOP2
3372 : with uses in additions and subtractions to form fused multiply-add
3373 : operations. Returns true if successful and MUL_STMT should be removed.
3374 : If MUL_COND is nonnull, the multiplication in MUL_STMT is conditional
3375 : on MUL_COND, otherwise it is unconditional.
3376 :
3377 : If STATE indicates that we are deferring FMA transformation, that means
3378 : that we do not produce FMAs for basic blocks which look like:
3379 :
3380 : <bb 6>
3381 : # accumulator_111 = PHI <0.0(5), accumulator_66(6)>
3382 : _65 = _14 * _16;
3383 : accumulator_66 = _65 + accumulator_111;
3384 :
3385 : or its unrolled version, i.e. with several FMA candidates that feed result
3386 : of one into the addend of another. Instead, we add them to a list in STATE
3387 : and if we later discover an FMA candidate that is not part of such a chain,
3388 : we go back and perform all deferred past candidates. */
3389 :
3390 : static bool
3391 769244 : convert_mult_to_fma (gimple *mul_stmt, tree op1, tree op2,
3392 : fma_deferring_state *state, tree mul_cond = NULL_TREE,
3393 : tree mul_len = NULL_TREE, tree mul_bias = NULL_TREE)
3394 : {
3395 769244 : tree mul_result = gimple_get_lhs (mul_stmt);
3396 : /* If there isn't a LHS then this can't be an FMA. There can be no LHS
3397 : if the statement was left just for the side-effects. */
3398 769244 : if (!mul_result)
3399 : return false;
3400 769244 : tree type = TREE_TYPE (mul_result);
3401 769244 : gimple *use_stmt, *neguse_stmt;
3402 769244 : use_operand_p use_p;
3403 769244 : imm_use_iterator imm_iter;
3404 :
3405 670535 : if (FLOAT_TYPE_P (type)
3406 795200 : && flag_fp_contract_mode != FP_CONTRACT_FAST)
3407 : return false;
3408 :
3409 : /* We don't want to do bitfield reduction ops. */
3410 764167 : if (INTEGRAL_TYPE_P (type)
3411 764167 : && (!type_has_mode_precision_p (type) || TYPE_OVERFLOW_TRAPS (type)))
3412 : return false;
3413 :
3414 : /* If the target doesn't support it, don't generate it. We assume that
3415 : if fma isn't available then fms, fnma or fnms are not either. */
3416 763946 : optimization_type opt_type = bb_optimization_type (gimple_bb (mul_stmt));
3417 763946 : if (!direct_internal_fn_supported_p (IFN_FMA, type, opt_type))
3418 : return false;
3419 :
3420 : /* If the multiplication has zero uses, it is kept around probably because
3421 : of -fnon-call-exceptions. Don't optimize it away in that case,
3422 : it is DCE job. */
3423 23229 : if (has_zero_uses (mul_result))
3424 : return false;
3425 :
3426 23229 : bool check_defer
3427 23229 : = (state->m_deferring_p
3428 23229 : && maybe_le (tree_to_poly_int64 (TYPE_SIZE (type)),
3429 23229 : param_avoid_fma_max_bits));
3430 23229 : bool defer = check_defer;
3431 23229 : bool seen_negate_p = false;
3432 :
3433 : /* There is no numerical difference between fused and unfused integer FMAs,
3434 : and the assumption below that FMA is as cheap as addition is unlikely
3435 : to be true, especially if the multiplication occurs multiple times on
3436 : the same chain. E.g., for something like:
3437 :
3438 : (((a * b) + c) >> 1) + (a * b)
3439 :
3440 : we do not want to duplicate the a * b into two additions, not least
3441 : because the result is not a natural FMA chain. */
3442 23229 : if (ANY_INTEGRAL_TYPE_P (type)
3443 23229 : && !has_single_use (mul_result))
3444 : return false;
3445 :
3446 23229 : if (!dbg_cnt (form_fma))
3447 : return false;
3448 :
3449 : /* Make sure that the multiplication statement becomes dead after
3450 : the transformation, thus that all uses are transformed to FMAs.
3451 : This means we assume that an FMA operation has the same cost
3452 : as an addition. */
3453 41797 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, mul_result)
3454 : {
3455 23980 : tree result = mul_result;
3456 23980 : bool negate_p = false;
3457 :
3458 23980 : use_stmt = USE_STMT (use_p);
3459 :
3460 23980 : if (is_gimple_debug (use_stmt))
3461 214 : continue;
3462 :
3463 : /* If the use is a type convert, look further into it if the operations
3464 : are the same under two's complement. */
3465 23766 : tree lhs_type;
3466 23766 : if (gimple_assign_cast_p (use_stmt)
3467 248 : && (lhs_type = TREE_TYPE (gimple_get_lhs (use_stmt)))
3468 24014 : && tree_nop_conversion_p (lhs_type, TREE_TYPE (op1)))
3469 : {
3470 0 : tree cast_lhs = gimple_get_lhs (use_stmt);
3471 0 : gimple *tmp_use;
3472 0 : use_operand_p tmp_use_p;
3473 0 : if (single_imm_use (cast_lhs, &tmp_use_p, &tmp_use))
3474 0 : use_stmt = tmp_use;
3475 0 : result = cast_lhs;
3476 : }
3477 :
3478 : /* For now restrict this operations to single basic blocks. In theory
3479 : we would want to support sinking the multiplication in
3480 : m = a*b;
3481 : if ()
3482 : ma = m + c;
3483 : else
3484 : d = m;
3485 : to form a fma in the then block and sink the multiplication to the
3486 : else block. */
3487 23766 : if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
3488 5412 : return false;
3489 :
3490 : /* A negate on the multiplication leads to FNMA. */
3491 22896 : auto use_assign = dyn_cast<gassign *> (use_stmt);
3492 21196 : if (use_assign && gimple_assign_rhs_code (use_assign) == NEGATE_EXPR)
3493 : {
3494 514 : ssa_op_iter iter;
3495 514 : use_operand_p usep;
3496 :
3497 : /* If (due to earlier missed optimizations) we have two
3498 : negates of the same value, treat them as equivalent
3499 : to a single negate with multiple uses. */
3500 514 : if (seen_negate_p)
3501 0 : return false;
3502 :
3503 514 : result = gimple_assign_lhs (use_assign);
3504 :
3505 : /* Make sure the negate statement becomes dead with this
3506 : single transformation. */
3507 514 : if (!single_imm_use (gimple_assign_lhs (use_assign),
3508 : &use_p, &neguse_stmt))
3509 : return false;
3510 :
3511 : /* Make sure the multiplication isn't also used on that stmt. */
3512 2068 : FOR_EACH_PHI_OR_STMT_USE (usep, neguse_stmt, iter, SSA_OP_USE)
3513 1040 : if (USE_FROM_PTR (usep) == mul_result)
3514 : return false;
3515 :
3516 : /* Re-validate. */
3517 514 : use_stmt = neguse_stmt;
3518 514 : if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
3519 : return false;
3520 :
3521 514 : negate_p = seen_negate_p = true;
3522 : }
3523 :
3524 22896 : tree cond, else_value, ops[3], len, bias;
3525 22896 : tree_code code;
3526 22896 : if (!can_interpret_as_conditional_op_p (use_stmt, &cond, &code, ops,
3527 : &else_value, &len, &bias))
3528 : return false;
3529 :
3530 : /* The multiplication result must be one of the addition operands. */
3531 20536 : if (ops[0] != result && ops[1] != result)
3532 : return false;
3533 :
3534 19923 : switch (code)
3535 : {
3536 5700 : case MINUS_EXPR:
3537 5700 : if (ops[1] == result)
3538 2937 : negate_p = !negate_p;
3539 : break;
3540 : case PLUS_EXPR:
3541 : break;
3542 : default:
3543 : /* FMA can only be formed from PLUS and MINUS. */
3544 : return false;
3545 : }
3546 :
3547 18376 : if (len)
3548 : {
3549 : /* For COND_LEN_* operations, we may have dummpy mask which is
3550 : the all true mask. Such TREE type may be mul_cond != cond
3551 : but we still consider they are equal. */
3552 0 : if (mul_cond && cond != mul_cond
3553 0 : && !(integer_truep (mul_cond) && integer_truep (cond)))
3554 : return false;
3555 :
3556 0 : if (else_value == result)
3557 : return false;
3558 :
3559 0 : if (!direct_internal_fn_supported_p (IFN_COND_LEN_FMA, type,
3560 : opt_type))
3561 : return false;
3562 :
3563 0 : if (mul_len)
3564 : {
3565 0 : poly_int64 mul_value, value;
3566 0 : if (poly_int_tree_p (mul_len, &mul_value)
3567 0 : && poly_int_tree_p (len, &value)
3568 0 : && maybe_ne (mul_value, value))
3569 0 : return false;
3570 0 : else if (mul_len != len)
3571 : return false;
3572 :
3573 0 : if (wi::to_widest (mul_bias) != wi::to_widest (bias))
3574 : return false;
3575 : }
3576 : }
3577 : else
3578 : {
3579 18376 : if (mul_cond && cond != mul_cond)
3580 : return false;
3581 :
3582 18364 : if (cond)
3583 : {
3584 160 : if (cond == result || else_value == result)
3585 : return false;
3586 150 : if (!direct_internal_fn_supported_p (IFN_COND_FMA, type,
3587 : opt_type))
3588 : return false;
3589 : }
3590 : }
3591 :
3592 : /* If the subtrahend (OPS[1]) is computed by a MULT_EXPR that
3593 : we'll visit later, we might be able to get a more profitable
3594 : match with fnma.
3595 : OTOH, if we don't, a negate / fma pair has likely lower latency
3596 : that a mult / subtract pair. */
3597 18354 : if (code == MINUS_EXPR
3598 5694 : && !negate_p
3599 2249 : && ops[0] == result
3600 2249 : && !direct_internal_fn_supported_p (IFN_FMS, type, opt_type)
3601 0 : && direct_internal_fn_supported_p (IFN_FNMA, type, opt_type)
3602 0 : && TREE_CODE (ops[1]) == SSA_NAME
3603 18354 : && has_single_use (ops[1]))
3604 : {
3605 0 : gimple *stmt2 = SSA_NAME_DEF_STMT (ops[1]);
3606 0 : if (is_gimple_assign (stmt2)
3607 0 : && gimple_assign_rhs_code (stmt2) == MULT_EXPR)
3608 : return false;
3609 : }
3610 :
3611 : /* We can't handle a * b + a * b. */
3612 18354 : if (ops[0] == ops[1])
3613 : return false;
3614 : /* If deferring, make sure we are not looking at an instruction that
3615 : wouldn't have existed if we were not. */
3616 18354 : if (state->m_deferring_p
3617 18354 : && (state->m_mul_result_set.contains (ops[0])
3618 6597 : || state->m_mul_result_set.contains (ops[1])))
3619 : return false;
3620 :
3621 18354 : if (check_defer)
3622 : {
3623 6423 : tree use_lhs = gimple_get_lhs (use_stmt);
3624 6423 : if (state->m_last_result)
3625 : {
3626 1053 : if (ops[1] == state->m_last_result
3627 1052 : || ops[0] == state->m_last_result)
3628 : defer = true;
3629 : else
3630 6423 : defer = false;
3631 : }
3632 : else
3633 : {
3634 5370 : gcc_checking_assert (!state->m_initial_phi);
3635 5370 : gphi *phi;
3636 5370 : if (ops[0] == result)
3637 3161 : phi = result_of_phi (ops[1]);
3638 : else
3639 : {
3640 2209 : gcc_assert (ops[1] == result);
3641 2209 : phi = result_of_phi (ops[0]);
3642 : }
3643 :
3644 : if (phi)
3645 : {
3646 975 : state->m_initial_phi = phi;
3647 975 : defer = true;
3648 : }
3649 : else
3650 : defer = false;
3651 : }
3652 :
3653 6423 : state->m_last_result = use_lhs;
3654 6423 : check_defer = false;
3655 : }
3656 : else
3657 : defer = false;
3658 :
3659 : /* While it is possible to validate whether or not the exact form that
3660 : we've recognized is available in the backend, the assumption is that
3661 : if the deferring logic above did not trigger, the transformation is
3662 : never a loss. For instance, suppose the target only has the plain FMA
3663 : pattern available. Consider a*b-c -> fma(a,b,-c): we've exchanged
3664 : MUL+SUB for FMA+NEG, which is still two operations. Consider
3665 : -(a*b)-c -> fma(-a,b,-c): we still have 3 operations, but in the FMA
3666 : form the two NEGs are independent and could be run in parallel. */
3667 5412 : }
3668 :
3669 17817 : if (defer)
3670 : {
3671 1050 : fma_transformation_info fti;
3672 1050 : fti.mul_stmt = mul_stmt;
3673 1050 : fti.mul_result = mul_result;
3674 1050 : fti.op1 = op1;
3675 1050 : fti.op2 = op2;
3676 1050 : state->m_candidates.safe_push (fti);
3677 1050 : state->m_mul_result_set.add (mul_result);
3678 :
3679 1050 : if (dump_file && (dump_flags & TDF_DETAILS))
3680 : {
3681 0 : fprintf (dump_file, "Deferred generating FMA for multiplication ");
3682 0 : print_gimple_stmt (dump_file, mul_stmt, 0, TDF_NONE);
3683 0 : fprintf (dump_file, "\n");
3684 : }
3685 :
3686 1050 : return false;
3687 : }
3688 : else
3689 : {
3690 16767 : if (state->m_deferring_p)
3691 5043 : cancel_fma_deferring (state);
3692 16767 : convert_mult_to_fma_1 (mul_result, op1, op2);
3693 16767 : return true;
3694 : }
3695 : }
3696 :
3697 :
3698 : /* Helper function of match_arith_overflow. For MUL_OVERFLOW, if we have
3699 : a check for non-zero like:
3700 : _1 = x_4(D) * y_5(D);
3701 : *res_7(D) = _1;
3702 : if (x_4(D) != 0)
3703 : goto <bb 3>; [50.00%]
3704 : else
3705 : goto <bb 4>; [50.00%]
3706 :
3707 : <bb 3> [local count: 536870913]:
3708 : _2 = _1 / x_4(D);
3709 : _9 = _2 != y_5(D);
3710 : _10 = (int) _9;
3711 :
3712 : <bb 4> [local count: 1073741824]:
3713 : # iftmp.0_3 = PHI <_10(3), 0(2)>
3714 : then in addition to using .MUL_OVERFLOW (x_4(D), y_5(D)) we can also
3715 : optimize the x_4(D) != 0 condition to 1. */
3716 :
3717 : static void
3718 173 : maybe_optimize_guarding_check (vec<gimple *> &mul_stmts, gimple *cond_stmt,
3719 : gimple *div_stmt, bool *cfg_changed)
3720 : {
3721 173 : basic_block bb = gimple_bb (cond_stmt);
3722 346 : if (gimple_bb (div_stmt) != bb || !single_pred_p (bb))
3723 61 : return;
3724 173 : edge pred_edge = single_pred_edge (bb);
3725 173 : basic_block pred_bb = pred_edge->src;
3726 173 : if (EDGE_COUNT (pred_bb->succs) != 2)
3727 : return;
3728 130 : edge other_edge = EDGE_SUCC (pred_bb, EDGE_SUCC (pred_bb, 0) == pred_edge);
3729 130 : edge other_succ_edge = NULL;
3730 130 : if (gimple_code (cond_stmt) == GIMPLE_COND)
3731 : {
3732 48 : if (EDGE_COUNT (bb->succs) != 2)
3733 : return;
3734 48 : other_succ_edge = EDGE_SUCC (bb, 0);
3735 48 : if (gimple_cond_code (cond_stmt) == NE_EXPR)
3736 : {
3737 24 : if (other_succ_edge->flags & EDGE_TRUE_VALUE)
3738 24 : other_succ_edge = EDGE_SUCC (bb, 1);
3739 : }
3740 : else if (other_succ_edge->flags & EDGE_FALSE_VALUE)
3741 48 : other_succ_edge = EDGE_SUCC (bb, 0);
3742 48 : if (other_edge->dest != other_succ_edge->dest)
3743 : return;
3744 : }
3745 138 : else if (!single_succ_p (bb) || other_edge->dest != single_succ (bb))
3746 : return;
3747 311 : gcond *zero_cond = safe_dyn_cast <gcond *> (*gsi_last_bb (pred_bb));
3748 125 : if (zero_cond == NULL
3749 125 : || (gimple_cond_code (zero_cond)
3750 125 : != ((pred_edge->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR))
3751 125 : || !integer_zerop (gimple_cond_rhs (zero_cond)))
3752 : return;
3753 125 : tree zero_cond_lhs = gimple_cond_lhs (zero_cond);
3754 125 : if (TREE_CODE (zero_cond_lhs) != SSA_NAME)
3755 : return;
3756 125 : if (gimple_assign_rhs2 (div_stmt) != zero_cond_lhs)
3757 : {
3758 : /* Allow the divisor to be result of a same precision cast
3759 : from zero_cond_lhs. */
3760 61 : tree rhs2 = gimple_assign_rhs2 (div_stmt);
3761 61 : if (TREE_CODE (rhs2) != SSA_NAME)
3762 : return;
3763 61 : gimple *g = SSA_NAME_DEF_STMT (rhs2);
3764 61 : if (!gimple_assign_cast_p (g)
3765 54 : || gimple_assign_rhs1 (g) != gimple_cond_lhs (zero_cond)
3766 54 : || !INTEGRAL_TYPE_P (TREE_TYPE (zero_cond_lhs))
3767 115 : || (TYPE_PRECISION (TREE_TYPE (zero_cond_lhs))
3768 54 : != TYPE_PRECISION (TREE_TYPE (rhs2))))
3769 : return;
3770 : }
3771 118 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
3772 118 : mul_stmts.safe_push (div_stmt);
3773 118 : if (is_gimple_debug (gsi_stmt (gsi)))
3774 0 : gsi_next_nondebug (&gsi);
3775 118 : unsigned cast_count = 0;
3776 663 : while (gsi_stmt (gsi) != cond_stmt)
3777 : {
3778 : /* If original mul_stmt has a single use, allow it in the same bb,
3779 : we are looking then just at __builtin_mul_overflow_p.
3780 : Though, in that case the original mul_stmt will be replaced
3781 : by .MUL_OVERFLOW, REALPART_EXPR and IMAGPART_EXPR stmts. */
3782 : gimple *mul_stmt;
3783 : unsigned int i;
3784 2425 : bool ok = false;
3785 2425 : FOR_EACH_VEC_ELT (mul_stmts, i, mul_stmt)
3786 : {
3787 2280 : if (gsi_stmt (gsi) == mul_stmt)
3788 : {
3789 : ok = true;
3790 : break;
3791 : }
3792 : }
3793 545 : if (!ok && gimple_assign_cast_p (gsi_stmt (gsi)) && ++cast_count < 4)
3794 : ok = true;
3795 400 : if (!ok)
3796 61 : return;
3797 545 : gsi_next_nondebug (&gsi);
3798 : }
3799 118 : if (gimple_code (cond_stmt) == GIMPLE_COND)
3800 : {
3801 48 : basic_block succ_bb = other_edge->dest;
3802 78 : for (gphi_iterator gpi = gsi_start_phis (succ_bb); !gsi_end_p (gpi);
3803 30 : gsi_next (&gpi))
3804 : {
3805 36 : gphi *phi = gpi.phi ();
3806 36 : tree v1 = gimple_phi_arg_def (phi, other_edge->dest_idx);
3807 36 : tree v2 = gimple_phi_arg_def (phi, other_succ_edge->dest_idx);
3808 36 : if (!operand_equal_p (v1, v2, 0))
3809 6 : return;
3810 : }
3811 : }
3812 : else
3813 : {
3814 70 : tree lhs = gimple_assign_lhs (cond_stmt);
3815 70 : if (!lhs || !INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
3816 : return;
3817 70 : gsi_next_nondebug (&gsi);
3818 70 : if (!gsi_end_p (gsi))
3819 : {
3820 70 : if (gimple_assign_rhs_code (cond_stmt) == COND_EXPR)
3821 : return;
3822 70 : gimple *cast_stmt = gsi_stmt (gsi);
3823 70 : if (!gimple_assign_cast_p (cast_stmt))
3824 : return;
3825 70 : tree new_lhs = gimple_assign_lhs (cast_stmt);
3826 70 : gsi_next_nondebug (&gsi);
3827 70 : if (!gsi_end_p (gsi)
3828 70 : || !new_lhs
3829 70 : || !INTEGRAL_TYPE_P (TREE_TYPE (new_lhs))
3830 140 : || TYPE_PRECISION (TREE_TYPE (new_lhs)) <= 1)
3831 : return;
3832 : lhs = new_lhs;
3833 : }
3834 70 : edge succ_edge = single_succ_edge (bb);
3835 70 : basic_block succ_bb = succ_edge->dest;
3836 70 : gsi = gsi_start_phis (succ_bb);
3837 70 : if (gsi_end_p (gsi))
3838 : return;
3839 70 : gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
3840 70 : gsi_next (&gsi);
3841 70 : if (!gsi_end_p (gsi))
3842 : return;
3843 70 : if (gimple_phi_arg_def (phi, succ_edge->dest_idx) != lhs)
3844 : return;
3845 70 : tree other_val = gimple_phi_arg_def (phi, other_edge->dest_idx);
3846 70 : if (gimple_assign_rhs_code (cond_stmt) == COND_EXPR)
3847 : {
3848 0 : tree cond = gimple_assign_rhs1 (cond_stmt);
3849 0 : if (TREE_CODE (cond) == NE_EXPR)
3850 : {
3851 0 : if (!operand_equal_p (other_val,
3852 0 : gimple_assign_rhs3 (cond_stmt), 0))
3853 : return;
3854 : }
3855 0 : else if (!operand_equal_p (other_val,
3856 0 : gimple_assign_rhs2 (cond_stmt), 0))
3857 : return;
3858 : }
3859 70 : else if (gimple_assign_rhs_code (cond_stmt) == NE_EXPR)
3860 : {
3861 41 : if (!integer_zerop (other_val))
3862 : return;
3863 : }
3864 29 : else if (!integer_onep (other_val))
3865 : return;
3866 : }
3867 112 : if (pred_edge->flags & EDGE_TRUE_VALUE)
3868 59 : gimple_cond_make_true (zero_cond);
3869 : else
3870 53 : gimple_cond_make_false (zero_cond);
3871 112 : update_stmt (zero_cond);
3872 112 : reset_flow_sensitive_info_in_bb (bb);
3873 112 : *cfg_changed = true;
3874 : }
3875 :
3876 : /* Helper function for arith_overflow_check_p. Return true
3877 : if VAL1 is equal to VAL2 cast to corresponding integral type
3878 : with other signedness or vice versa. */
3879 :
3880 : static bool
3881 394 : arith_cast_equal_p (tree val1, tree val2)
3882 : {
3883 394 : if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
3884 71 : return wi::eq_p (wi::to_wide (val1), wi::to_wide (val2));
3885 323 : else if (TREE_CODE (val1) != SSA_NAME || TREE_CODE (val2) != SSA_NAME)
3886 : return false;
3887 279 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (val1))
3888 279 : && gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val1)) == val2)
3889 : return true;
3890 167 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (val2))
3891 167 : && gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val2)) == val1)
3892 120 : return true;
3893 : return false;
3894 : }
3895 :
3896 : /* Helper function of match_arith_overflow. Return 1
3897 : if USE_STMT is unsigned overflow check ovf != 0 for
3898 : STMT, -1 if USE_STMT is unsigned overflow check ovf == 0
3899 : and 0 otherwise. */
3900 :
3901 : static int
3902 3027880 : arith_overflow_check_p (gimple *stmt, gimple *cast_stmt, gimple *&use_stmt,
3903 : tree maxval, tree *other)
3904 : {
3905 3027880 : enum tree_code ccode = ERROR_MARK;
3906 3027880 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
3907 3027880 : enum tree_code code = gimple_assign_rhs_code (stmt);
3908 6032783 : tree lhs = gimple_assign_lhs (cast_stmt ? cast_stmt : stmt);
3909 3027880 : tree rhs1 = gimple_assign_rhs1 (stmt);
3910 3027880 : tree rhs2 = gimple_assign_rhs2 (stmt);
3911 3027880 : tree multop = NULL_TREE, divlhs = NULL_TREE;
3912 3027880 : gimple *cur_use_stmt = use_stmt;
3913 :
3914 3027880 : if (code == MULT_EXPR)
3915 : {
3916 695145 : auto assign = dyn_cast<gassign *> (use_stmt);
3917 552800 : if (!assign)
3918 694793 : return 0;
3919 602438 : if (gimple_assign_rhs_code (assign) != TRUNC_DIV_EXPR)
3920 : return 0;
3921 2371 : if (gimple_assign_rhs1 (assign) != lhs)
3922 : return 0;
3923 2308 : if (cast_stmt)
3924 : {
3925 322 : if (arith_cast_equal_p (gimple_assign_rhs2 (assign), rhs1))
3926 : multop = rhs2;
3927 174 : else if (arith_cast_equal_p (gimple_assign_rhs2 (assign), rhs2))
3928 : multop = rhs1;
3929 : else
3930 : return 0;
3931 : }
3932 4294 : else if (gimple_assign_rhs2 (assign) == rhs1)
3933 : multop = rhs2;
3934 2013 : else if (operand_equal_p (gimple_assign_rhs2 (assign), rhs2, 0))
3935 : multop = rhs1;
3936 : else
3937 : return 0;
3938 356 : if (stmt_ends_bb_p (assign))
3939 : return 0;
3940 356 : divlhs = gimple_assign_lhs (use_stmt);
3941 356 : if (!divlhs)
3942 : return 0;
3943 356 : use_operand_p use;
3944 356 : if (!single_imm_use (divlhs, &use, &cur_use_stmt))
3945 : return 0;
3946 352 : if (cast_stmt && gimple_assign_cast_p (cur_use_stmt))
3947 : {
3948 4 : tree cast_lhs = gimple_assign_lhs (cur_use_stmt);
3949 8 : if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs))
3950 4 : && TYPE_UNSIGNED (TREE_TYPE (cast_lhs))
3951 4 : && (TYPE_PRECISION (TREE_TYPE (cast_lhs))
3952 4 : == TYPE_PRECISION (TREE_TYPE (divlhs)))
3953 8 : && single_imm_use (cast_lhs, &use, &cur_use_stmt))
3954 : {
3955 : cast_stmt = NULL;
3956 : divlhs = cast_lhs;
3957 : }
3958 : else
3959 : return 0;
3960 : }
3961 : }
3962 2333087 : if (gimple_code (cur_use_stmt) == GIMPLE_COND)
3963 : {
3964 592423 : ccode = gimple_cond_code (cur_use_stmt);
3965 592423 : crhs1 = gimple_cond_lhs (cur_use_stmt);
3966 592423 : crhs2 = gimple_cond_rhs (cur_use_stmt);
3967 : }
3968 1740664 : else if (auto assign = dyn_cast<gassign *> (cur_use_stmt))
3969 : {
3970 830166 : if (gimple_assign_rhs_class (assign) == GIMPLE_BINARY_RHS)
3971 : {
3972 470696 : ccode = gimple_assign_rhs_code (assign);
3973 470696 : crhs1 = gimple_assign_rhs1 (assign);
3974 470696 : crhs2 = gimple_assign_rhs2 (assign);
3975 : }
3976 : else
3977 : return 0;
3978 : }
3979 : else
3980 : return 0;
3981 :
3982 1063119 : if (maxval
3983 1063119 : && ccode == RSHIFT_EXPR
3984 33 : && crhs1 == lhs
3985 17 : && TREE_CODE (crhs2) == INTEGER_CST
3986 1063136 : && wi::to_widest (crhs2) == TYPE_PRECISION (TREE_TYPE (maxval)))
3987 : {
3988 16 : tree shiftlhs = gimple_assign_lhs (use_stmt);
3989 16 : if (!shiftlhs)
3990 : return 0;
3991 16 : use_operand_p use;
3992 16 : if (!single_imm_use (shiftlhs, &use, &cur_use_stmt))
3993 : return 0;
3994 12 : if (gimple_code (cur_use_stmt) == GIMPLE_COND)
3995 : {
3996 0 : ccode = gimple_cond_code (cur_use_stmt);
3997 0 : crhs1 = gimple_cond_lhs (cur_use_stmt);
3998 0 : crhs2 = gimple_cond_rhs (cur_use_stmt);
3999 : }
4000 28 : else if (auto cur_use_assign = dyn_cast<gassign *> (cur_use_stmt))
4001 : {
4002 12 : if (gimple_assign_rhs_class (cur_use_assign) == GIMPLE_BINARY_RHS)
4003 : {
4004 0 : ccode = gimple_assign_rhs_code (cur_use_assign);
4005 0 : crhs1 = gimple_assign_rhs1 (cur_use_assign);
4006 0 : crhs2 = gimple_assign_rhs2 (cur_use_assign);
4007 : }
4008 12 : else if (gimple_assign_rhs_code (cur_use_assign) == COND_EXPR)
4009 : {
4010 0 : tree cond = gimple_assign_rhs1 (cur_use_assign);
4011 0 : if (COMPARISON_CLASS_P (cond))
4012 : {
4013 0 : ccode = TREE_CODE (cond);
4014 0 : crhs1 = TREE_OPERAND (cond, 0);
4015 0 : crhs2 = TREE_OPERAND (cond, 1);
4016 : }
4017 : else
4018 : return 0;
4019 : }
4020 : else
4021 : {
4022 12 : enum tree_code sc = gimple_assign_rhs_code (cur_use_assign);
4023 12 : tree castlhs = gimple_assign_lhs (cur_use_assign);
4024 12 : if (!CONVERT_EXPR_CODE_P (sc)
4025 12 : || !castlhs
4026 12 : || !INTEGRAL_TYPE_P (TREE_TYPE (castlhs))
4027 24 : || (TYPE_PRECISION (TREE_TYPE (castlhs))
4028 12 : > TYPE_PRECISION (TREE_TYPE (maxval))))
4029 0 : return 0;
4030 : return 1;
4031 : }
4032 : }
4033 : else
4034 : return 0;
4035 0 : if ((ccode != EQ_EXPR && ccode != NE_EXPR)
4036 0 : || crhs1 != shiftlhs
4037 0 : || !integer_zerop (crhs2))
4038 0 : return 0;
4039 : return 1;
4040 : }
4041 :
4042 1063103 : if (TREE_CODE_CLASS (ccode) != tcc_comparison)
4043 : return 0;
4044 :
4045 629504 : switch (ccode)
4046 : {
4047 115305 : case GT_EXPR:
4048 115305 : case LE_EXPR:
4049 115305 : if (maxval)
4050 : {
4051 : /* r = a + b; r > maxval or r <= maxval */
4052 40 : if (crhs1 == lhs
4053 39 : && TREE_CODE (crhs2) == INTEGER_CST
4054 57 : && tree_int_cst_equal (crhs2, maxval))
4055 13 : return ccode == GT_EXPR ? 1 : -1;
4056 : break;
4057 : }
4058 : /* r = a - b; r > a or r <= a
4059 : r = a + b; a > r or a <= r or b > r or b <= r. */
4060 115265 : if ((code == MINUS_EXPR && crhs1 == lhs && crhs2 == rhs1)
4061 115201 : || (code == PLUS_EXPR && (crhs1 == rhs1 || crhs1 == rhs2)
4062 8007 : && crhs2 == lhs))
4063 8071 : return ccode == GT_EXPR ? 1 : -1;
4064 : /* r = ~a; b > r or b <= r. */
4065 107194 : if (code == BIT_NOT_EXPR && crhs2 == lhs)
4066 : {
4067 254 : if (other)
4068 127 : *other = crhs1;
4069 254 : return ccode == GT_EXPR ? 1 : -1;
4070 : }
4071 : break;
4072 65451 : case LT_EXPR:
4073 65451 : case GE_EXPR:
4074 65451 : if (maxval)
4075 : break;
4076 : /* r = a - b; a < r or a >= r
4077 : r = a + b; r < a or r >= a or r < b or r >= b. */
4078 65445 : if ((code == MINUS_EXPR && crhs1 == rhs1 && crhs2 == lhs)
4079 65307 : || (code == PLUS_EXPR && crhs1 == lhs
4080 30991 : && (crhs2 == rhs1 || crhs2 == rhs2)))
4081 4133 : return ccode == LT_EXPR ? 1 : -1;
4082 : /* r = ~a; r < b or r >= b. */
4083 61312 : if (code == BIT_NOT_EXPR && crhs1 == lhs)
4084 : {
4085 347 : if (other)
4086 182 : *other = crhs2;
4087 347 : return ccode == LT_EXPR ? 1 : -1;
4088 : }
4089 : break;
4090 448748 : case EQ_EXPR:
4091 448748 : case NE_EXPR:
4092 : /* r = a * b; _1 = r / a; _1 == b
4093 : r = a * b; _1 = r / b; _1 == a
4094 : r = a * b; _1 = r / a; _1 != b
4095 : r = a * b; _1 = r / b; _1 != a. */
4096 448748 : if (code == MULT_EXPR)
4097 : {
4098 349 : if (cast_stmt)
4099 : {
4100 146 : if ((crhs1 == divlhs && arith_cast_equal_p (crhs2, multop))
4101 146 : || (crhs2 == divlhs && arith_cast_equal_p (crhs1, multop)))
4102 : {
4103 146 : use_stmt = cur_use_stmt;
4104 146 : return ccode == NE_EXPR ? 1 : -1;
4105 : }
4106 : }
4107 142 : else if ((crhs1 == divlhs && operand_equal_p (crhs2, multop, 0))
4108 203 : || (crhs2 == divlhs && crhs1 == multop))
4109 : {
4110 203 : use_stmt = cur_use_stmt;
4111 203 : return ccode == NE_EXPR ? 1 : -1;
4112 : }
4113 : }
4114 : break;
4115 : default:
4116 : break;
4117 : }
4118 : return 0;
4119 : }
4120 :
4121 : extern bool gimple_unsigned_integer_sat_add (tree, tree*, tree (*)(tree));
4122 : extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree));
4123 : extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree));
4124 : extern bool gimple_unsigned_integer_sat_mul (tree, tree*, tree (*)(tree));
4125 : extern bool gimple_spaceship (tree, tree*, tree (*)(tree));
4126 :
4127 : extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree));
4128 : extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree));
4129 : extern bool gimple_signed_integer_sat_trunc (tree, tree*, tree (*)(tree));
4130 :
4131 : static bool
4132 351 : build_saturation_binary_arith_call_and_replace (gimple_stmt_iterator *gsi,
4133 : internal_fn fn, tree lhs,
4134 : tree op_0, tree op_1)
4135 : {
4136 351 : if (direct_internal_fn_supported_p (fn, TREE_TYPE (op_0), OPTIMIZE_FOR_BOTH))
4137 : {
4138 335 : gcall *call = gimple_build_call_internal (fn, 2, op_0, op_1);
4139 335 : gimple_call_set_lhs (call, lhs);
4140 335 : gsi_replace (gsi, call, /* update_eh_info */ true);
4141 335 : return true;
4142 : }
4143 :
4144 : return false;
4145 : }
4146 :
4147 : static bool
4148 107 : build_saturation_binary_arith_call_and_insert (gimple_stmt_iterator *gsi,
4149 : internal_fn fn, tree lhs,
4150 : tree op_0, tree op_1)
4151 : {
4152 107 : if (!direct_internal_fn_supported_p (fn, TREE_TYPE (op_0), OPTIMIZE_FOR_BOTH))
4153 : return false;
4154 :
4155 67 : gcall *call = gimple_build_call_internal (fn, 2, op_0, op_1);
4156 67 : gimple_call_set_lhs (call, lhs);
4157 67 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
4158 :
4159 67 : return true;
4160 : }
4161 :
4162 : /*
4163 : * Try to match saturation unsigned add with assign.
4164 : * _7 = _4 + _6;
4165 : * _8 = _4 > _7;
4166 : * _9 = (long unsigned int) _8;
4167 : * _10 = -_9;
4168 : * _12 = _7 | _10;
4169 : * =>
4170 : * _12 = .SAT_ADD (_4, _6);
4171 : *
4172 : * Try to match IMM=-1 saturation signed add with assign.
4173 : * <bb 2> [local count: 1073741824]:
4174 : * x.0_1 = (unsigned char) x_5(D);
4175 : * _3 = -x.0_1;
4176 : * _10 = (signed char) _3;
4177 : * _8 = x_5(D) & _10;
4178 : * if (_8 < 0)
4179 : * goto <bb 4>; [1.40%]
4180 : * else
4181 : * goto <bb 3>; [98.60%]
4182 : * <bb 3> [local count: 434070867]:
4183 : * _2 = x.0_1 + 255;
4184 : * <bb 4> [local count: 1073741824]:
4185 : * # _9 = PHI <_2(3), 128(2)>
4186 : * _4 = (int8_t) _9;
4187 : * =>
4188 : * _4 = .SAT_ADD (x_5, -1);
4189 : * Return true if the statement was replaced. */
4190 :
4191 : static bool
4192 5053425 : match_saturation_add_with_assign (gimple_stmt_iterator *gsi, gassign *stmt)
4193 : {
4194 5053425 : tree ops[2];
4195 5053425 : tree lhs = gimple_assign_lhs (stmt);
4196 :
4197 5053425 : if (gimple_unsigned_integer_sat_add (lhs, ops, NULL)
4198 5053425 : || gimple_signed_integer_sat_add (lhs, ops, NULL))
4199 : {
4200 : /* .SAT_ADD saturates in the type of its first operand, which must be
4201 : the type of the result. */
4202 208 : gcc_checking_assert (types_compatible_p (TREE_TYPE (lhs),
4203 : TREE_TYPE (ops[0])));
4204 208 : return build_saturation_binary_arith_call_and_replace (gsi, IFN_SAT_ADD,
4205 : lhs, ops[0],
4206 208 : ops[1]);
4207 : }
4208 :
4209 : return false;
4210 : }
4211 :
4212 : /*
4213 : * Try to match saturation add with PHI.
4214 : * For unsigned integer:
4215 : * <bb 2> :
4216 : * _1 = x_3(D) + y_4(D);
4217 : * if (_1 >= x_3(D))
4218 : * goto <bb 3>; [INV]
4219 : * else
4220 : * goto <bb 4>; [INV]
4221 : *
4222 : * <bb 3> :
4223 : *
4224 : * <bb 4> :
4225 : * # _2 = PHI <255(2), _1(3)>
4226 : * =>
4227 : * <bb 4> [local count: 1073741824]:
4228 : * _2 = .SAT_ADD (x_4(D), y_5(D));
4229 : *
4230 : * For signed integer:
4231 : * x.0_1 = (long unsigned int) x_7(D);
4232 : * y.1_2 = (long unsigned int) y_8(D);
4233 : * _3 = x.0_1 + y.1_2;
4234 : * sum_9 = (int64_t) _3;
4235 : * _4 = x_7(D) ^ y_8(D);
4236 : * _5 = x_7(D) ^ sum_9;
4237 : * _15 = ~_4;
4238 : * _16 = _5 & _15;
4239 : * if (_16 < 0)
4240 : * goto <bb 3>; [41.00%]
4241 : * else
4242 : * goto <bb 4>; [59.00%]
4243 : * _11 = x_7(D) < 0;
4244 : * _12 = (long int) _11;
4245 : * _13 = -_12;
4246 : * _14 = _13 ^ 9223372036854775807;
4247 : * # _6 = PHI <_14(3), sum_9(2)>
4248 : * =>
4249 : * _6 = .SAT_ADD (x_5(D), y_6(D)); [tail call] */
4250 :
4251 : static bool
4252 4338159 : match_saturation_add (gimple_stmt_iterator *gsi, gphi *phi)
4253 : {
4254 4338159 : if (gimple_phi_num_args (phi) != 2)
4255 : return false;
4256 :
4257 3464291 : tree ops[2];
4258 3464291 : tree phi_result = gimple_phi_result (phi);
4259 :
4260 3464291 : if (!gimple_unsigned_integer_sat_add (phi_result, ops, NULL)
4261 3464291 : && !gimple_signed_integer_sat_add (phi_result, ops, NULL))
4262 : return false;
4263 :
4264 : /* .SAT_ADD saturates in the type of its first operand, which must be the
4265 : type of the result. */
4266 43 : gcc_checking_assert (types_compatible_p (TREE_TYPE (phi_result),
4267 : TREE_TYPE (ops[0])));
4268 :
4269 43 : if (!TYPE_UNSIGNED (TREE_TYPE (ops[0])) && TREE_CODE (ops[1]) == INTEGER_CST)
4270 20 : ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
4271 :
4272 43 : return build_saturation_binary_arith_call_and_insert (gsi, IFN_SAT_ADD,
4273 : phi_result, ops[0],
4274 43 : ops[1]);
4275 : }
4276 :
4277 : /*
4278 : * Try to match saturation sub with assign.
4279 : * _1 = _4 >= _5;
4280 : * _3 = _4 - _5;
4281 : * _6 = _1 ? _3 : 0;
4282 : * =>
4283 : * _6 = .SAT_SUB (_4, _5);
4284 : * Return true if the statement was replaced. */
4285 :
4286 : static bool
4287 6307975 : match_saturation_sub_with_assign (gimple_stmt_iterator *gsi, gassign *stmt)
4288 : {
4289 6307975 : tree ops[2];
4290 6307975 : tree lhs = gimple_assign_lhs (stmt);
4291 :
4292 6307975 : if (gimple_unsigned_integer_sat_sub (lhs, ops, NULL)
4293 6307975 : || gimple_signed_integer_sat_sub (lhs, ops, NULL))
4294 143 : return build_saturation_binary_arith_call_and_replace (gsi, IFN_SAT_SUB,
4295 143 : lhs, ops[0], ops[1]);
4296 :
4297 : return false;
4298 : }
4299 :
4300 : /*
4301 : * Try to match saturation unsigned mul.
4302 : * _1 = (unsigned int) a_6(D);
4303 : * _2 = (unsigned int) b_7(D);
4304 : * x_8 = _1 * _2;
4305 : * overflow_9 = x_8 > 255;
4306 : * _3 = (unsigned char) overflow_9;
4307 : * _4 = -_3;
4308 : * _5 = (unsigned char) x_8;
4309 : * _10 = _4 | _5;
4310 : * =>
4311 : * _10 = .SAT_SUB (a_6, b_7); */
4312 :
4313 : static void
4314 2684568 : match_unsigned_saturation_mul (gimple_stmt_iterator *gsi, gassign *stmt)
4315 : {
4316 2684568 : tree ops[2];
4317 2684568 : tree lhs = gimple_assign_lhs (stmt);
4318 :
4319 2684568 : if (gimple_unsigned_integer_sat_mul (lhs, ops, NULL))
4320 0 : build_saturation_binary_arith_call_and_replace (gsi, IFN_SAT_MUL, lhs,
4321 : ops[0], ops[1]);
4322 2684568 : }
4323 :
4324 : /* Try to match saturation unsigned mul, aka:
4325 : _6 = .MUL_OVERFLOW (a_4(D), b_5(D));
4326 : _2 = IMAGPART_EXPR <_6>;
4327 : if (_2 != 0)
4328 : goto <bb 4>; [35.00%]
4329 : else
4330 : goto <bb 3>; [65.00%]
4331 :
4332 : <bb 3> [local count: 697932184]:
4333 : _1 = REALPART_EXPR <_6>;
4334 :
4335 : <bb 4> [local count: 1073741824]:
4336 : # _3 = PHI <18446744073709551615(2), _1(3)>
4337 : =>
4338 : _3 = .SAT_MUL (a_4(D), b_5(D)); */
4339 :
4340 : static bool
4341 4338092 : match_saturation_mul (gimple_stmt_iterator *gsi, gphi *phi)
4342 : {
4343 4338092 : if (gimple_phi_num_args (phi) != 2)
4344 : return false;
4345 :
4346 3464224 : tree ops[2];
4347 3464224 : tree phi_result = gimple_phi_result (phi);
4348 :
4349 3464224 : if (!gimple_unsigned_integer_sat_mul (phi_result, ops, NULL))
4350 : return false;
4351 :
4352 12 : return build_saturation_binary_arith_call_and_insert (gsi, IFN_SAT_MUL,
4353 : phi_result, ops[0],
4354 12 : ops[1]);
4355 : }
4356 :
4357 : /* Try to match variants of spaceship operation:
4358 : <bb 2>
4359 : if (a_3(D) >= b_4(D)) -- CMP_1
4360 : goto <bb 3>;
4361 : else
4362 : goto <bb 4>;
4363 :
4364 : <bb 3>
4365 : _1 = a_3(D) > b_4(D); -- CMP_2
4366 : _5 = (int) _1;
4367 :
4368 : <bb 4>
4369 : # _2 = PHI <-1(2), _5(3)>
4370 : =>
4371 : _2 = .SPACESHIP (a_3(D), b_4(D), -1);
4372 :
4373 : All possible canonical variants of the comparison operator in CMP_1 and
4374 : CMP_2 has been included in gimple_spaceship function. */
4375 : static bool
4376 4338092 : match_spaceship (gimple_stmt_iterator *gsi, gphi *phi)
4377 : {
4378 4338092 : if (gimple_phi_num_args (phi) != 2)
4379 : return false;
4380 3464224 : tree ops[2];
4381 3464224 : tree phi_result = gimple_phi_result (phi);
4382 :
4383 3464224 : if (!gimple_spaceship (phi_result, ops, NULL))
4384 : return false;
4385 :
4386 : /* Allow different modes as long as both are integral types. */
4387 302 : if (!INTEGRAL_TYPE_P (TREE_TYPE (phi_result))
4388 302 : || !INTEGRAL_TYPE_P (TREE_TYPE (ops[0])))
4389 : return false;
4390 :
4391 151 : tree ops_type = TREE_TYPE (ops[0]);
4392 151 : machine_mode ops_mode = TYPE_MODE (ops_type);
4393 151 : machine_mode promoted_mode = ops_mode;
4394 151 : tree promoted_type = ops_type;
4395 151 : bool is_unsigned = TYPE_UNSIGNED (ops_type);
4396 :
4397 : /* Check if spaceship optab is available for the operand mode.
4398 : If not, try promoting to a wider mode that is supported. */
4399 151 : if (optab_handler (spaceship_optab, ops_mode) == CODE_FOR_nothing)
4400 : {
4401 : /* Try promoting to wider modes (e.g., QI/HI -> SI -> DI). */
4402 : machine_mode wider_mode;
4403 0 : FOR_EACH_WIDER_MODE_FROM (wider_mode, ops_mode)
4404 : {
4405 0 : if (optab_handler (spaceship_optab, wider_mode)
4406 : != CODE_FOR_nothing)
4407 : {
4408 : /* Check if we can get a type for this mode with matching
4409 : signedness. */
4410 0 : promoted_type = lang_hooks.types.type_for_mode (wider_mode,
4411 : is_unsigned);
4412 0 : if (promoted_type != NULL_TREE && INTEGRAL_TYPE_P (promoted_type))
4413 : {
4414 : promoted_mode = wider_mode;
4415 : break;
4416 : }
4417 : }
4418 : }
4419 :
4420 : // If no suitable promoted mode found, give up.
4421 0 : if (promoted_mode == ops_mode)
4422 4338092 : return false;
4423 : }
4424 :
4425 : /* If promotion is needed, insert conversion statements.
4426 : We must use GIMPLE assignments rather than fold_convert because
4427 : gimple_call arguments must be valid GIMPLE values (SSA names or
4428 : constants), not tree expressions. */
4429 151 : ops[0] = gimple_convert (gsi, true, GSI_SAME_STMT, UNKNOWN_LOCATION,
4430 : promoted_type, ops[0]);
4431 151 : ops[1] = gimple_convert (gsi, true, GSI_SAME_STMT, UNKNOWN_LOCATION,
4432 : promoted_type, ops[1]);
4433 :
4434 151 : tree spaceship_arg_3 = is_unsigned ? build_one_cst (integer_type_node)
4435 118 : : build_minus_one_cst (integer_type_node);
4436 :
4437 151 : gcall *call = gimple_build_call_internal (IFN_SPACESHIP, 3, ops[0], ops[1],
4438 : spaceship_arg_3);
4439 :
4440 : /* SPACESHIP optab always returns signed int (SI mode).
4441 : Cast to phi_result's type if needed. */
4442 151 : tree call_result_type = integer_type_node;
4443 151 : if (!types_compatible_p (TREE_TYPE (phi_result), call_result_type))
4444 : {
4445 48 : tree call_result = make_ssa_name (call_result_type);
4446 48 : gimple_call_set_lhs (call, call_result);
4447 48 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
4448 48 : gassign *cast_stmt = gimple_build_assign (phi_result, NOP_EXPR,
4449 : call_result);
4450 48 : gsi_insert_before (gsi, cast_stmt, GSI_SAME_STMT);
4451 : }
4452 : else
4453 : {
4454 103 : gimple_call_set_lhs (call, phi_result);
4455 103 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
4456 : }
4457 : return true;
4458 : }
4459 :
4460 :
4461 : /*
4462 : * Try to match saturation unsigned sub.
4463 : * <bb 2> [local count: 1073741824]:
4464 : * if (x_2(D) > y_3(D))
4465 : * goto <bb 3>; [50.00%]
4466 : * else
4467 : * goto <bb 4>; [50.00%]
4468 : *
4469 : * <bb 3> [local count: 536870912]:
4470 : * _4 = x_2(D) - y_3(D);
4471 : *
4472 : * <bb 4> [local count: 1073741824]:
4473 : * # _1 = PHI <0(2), _4(3)>
4474 : * =>
4475 : * <bb 4> [local count: 1073741824]:
4476 : * _1 = .SAT_SUB (x_2(D), y_3(D)); */
4477 : static bool
4478 4338140 : match_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
4479 : {
4480 4338140 : if (gimple_phi_num_args (phi) != 2)
4481 : return false;
4482 :
4483 3464272 : tree ops[2];
4484 3464272 : tree phi_result = gimple_phi_result (phi);
4485 :
4486 3464272 : if (!gimple_unsigned_integer_sat_sub (phi_result, ops, NULL)
4487 3464272 : && !gimple_signed_integer_sat_sub (phi_result, ops, NULL))
4488 : return false;
4489 :
4490 52 : return build_saturation_binary_arith_call_and_insert (gsi, IFN_SAT_SUB,
4491 : phi_result, ops[0],
4492 52 : ops[1]);
4493 : }
4494 :
4495 : /*
4496 : * Try to match saturation unsigned sub.
4497 : * uint16_t x_4(D);
4498 : * uint8_t _6;
4499 : * overflow_5 = x_4(D) > 255;
4500 : * _1 = (unsigned char) x_4(D);
4501 : * _2 = (unsigned char) overflow_5;
4502 : * _3 = -_2;
4503 : * _6 = _1 | _3;
4504 : * =>
4505 : * _6 = .SAT_TRUNC (x_4(D));
4506 : * */
4507 : static void
4508 2684568 : match_unsigned_saturation_trunc (gimple_stmt_iterator *gsi, gassign *stmt)
4509 : {
4510 2684568 : tree ops[1];
4511 2684568 : tree lhs = gimple_assign_lhs (stmt);
4512 2684568 : tree type = TREE_TYPE (lhs);
4513 :
4514 2684568 : if (gimple_unsigned_integer_sat_trunc (lhs, ops, NULL)
4515 2684703 : && direct_internal_fn_supported_p (IFN_SAT_TRUNC,
4516 135 : tree_pair (type, TREE_TYPE (ops[0])),
4517 : OPTIMIZE_FOR_BOTH))
4518 : {
4519 108 : gcall *call = gimple_build_call_internal (IFN_SAT_TRUNC, 1, ops[0]);
4520 108 : gimple_call_set_lhs (call, lhs);
4521 108 : gsi_replace (gsi, call, /* update_eh_info */ true);
4522 : }
4523 2684568 : }
4524 :
4525 : /*
4526 : * Try to match saturation truncate.
4527 : * Aka:
4528 : * x.0_1 = (unsigned long) x_4(D);
4529 : * _2 = x.0_1 + 2147483648;
4530 : * if (_2 > 4294967295)
4531 : * goto <bb 4>; [50.00%]
4532 : * else
4533 : * goto <bb 3>; [50.00%]
4534 : * ;; succ: 4
4535 : * ;; 3
4536 : *
4537 : * ;; basic block 3, loop depth 0
4538 : * ;; pred: 2
4539 : * trunc_5 = (int32_t) x_4(D);
4540 : * goto <bb 5>; [100.00%]
4541 : * ;; succ: 5
4542 : *
4543 : * ;; basic block 4, loop depth 0
4544 : * ;; pred: 2
4545 : * _7 = x_4(D) < 0;
4546 : * _8 = (int) _7;
4547 : * _9 = -_8;
4548 : * _10 = _9 ^ 2147483647;
4549 : * ;; succ: 5
4550 : *
4551 : * ;; basic block 5, loop depth 0
4552 : * ;; pred: 3
4553 : * ;; 4
4554 : * # _3 = PHI <trunc_5(3), _10(4)>
4555 : * =>
4556 : * _6 = .SAT_TRUNC (x_4(D));
4557 : */
4558 :
4559 : static bool
4560 4338092 : match_saturation_trunc (gimple_stmt_iterator *gsi, gphi *phi)
4561 : {
4562 4338092 : if (gimple_phi_num_args (phi) != 2)
4563 : return false;
4564 :
4565 3464224 : tree ops[1];
4566 3464224 : tree phi_result = gimple_phi_result (phi);
4567 3464224 : tree type = TREE_TYPE (phi_result);
4568 :
4569 3464224 : if (!gimple_unsigned_integer_sat_trunc (phi_result, ops, NULL)
4570 3464224 : && !gimple_signed_integer_sat_trunc (phi_result, ops, NULL))
4571 : return false;
4572 :
4573 99 : if (!direct_internal_fn_supported_p (IFN_SAT_TRUNC,
4574 99 : tree_pair (type, TREE_TYPE (ops[0])),
4575 : OPTIMIZE_FOR_BOTH))
4576 : return false;
4577 :
4578 0 : gcall *call = gimple_build_call_internal (IFN_SAT_TRUNC, 1, ops[0]);
4579 0 : gimple_call_set_lhs (call, phi_result);
4580 0 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
4581 :
4582 0 : return true;
4583 : }
4584 :
4585 : /* Recognize for unsigned x
4586 : x = y - z;
4587 : if (x > y)
4588 : where there are other uses of x and replace it with
4589 : _7 = .SUB_OVERFLOW (y, z);
4590 : x = REALPART_EXPR <_7>;
4591 : _8 = IMAGPART_EXPR <_7>;
4592 : if (_8)
4593 : and similarly for addition.
4594 :
4595 : Also recognize:
4596 : yc = (type) y;
4597 : zc = (type) z;
4598 : x = yc + zc;
4599 : if (x > max)
4600 : where y and z have unsigned types with maximum max
4601 : and there are other uses of x and all of those cast x
4602 : back to that unsigned type and again replace it with
4603 : _7 = .ADD_OVERFLOW (y, z);
4604 : _9 = REALPART_EXPR <_7>;
4605 : _8 = IMAGPART_EXPR <_7>;
4606 : if (_8)
4607 : and replace (utype) x with _9.
4608 : Or with x >> popcount (max) instead of x > max.
4609 :
4610 : Also recognize:
4611 : x = ~z;
4612 : if (y > x)
4613 : and replace it with
4614 : _7 = .ADD_OVERFLOW (y, z);
4615 : _8 = IMAGPART_EXPR <_7>;
4616 : if (_8)
4617 :
4618 : And also recognize:
4619 : z = x * y;
4620 : if (x != 0)
4621 : goto <bb 3>; [50.00%]
4622 : else
4623 : goto <bb 4>; [50.00%]
4624 :
4625 : <bb 3> [local count: 536870913]:
4626 : _2 = z / x;
4627 : _9 = _2 != y;
4628 : _10 = (int) _9;
4629 :
4630 : <bb 4> [local count: 1073741824]:
4631 : # iftmp.0_3 = PHI <_10(3), 0(2)>
4632 : and replace it with
4633 : _7 = .MUL_OVERFLOW (x, y);
4634 : z = REALPART_EXPR <_7>;
4635 : _8 = IMAGPART_EXPR <_7>;
4636 : _9 = _8 != 0;
4637 : iftmp.0_3 = (int) _9; */
4638 :
4639 : static bool
4640 3486733 : match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt,
4641 : enum tree_code code, bool *cfg_changed)
4642 : {
4643 3486733 : tree lhs = gimple_assign_lhs (stmt);
4644 3486733 : tree type = TREE_TYPE (lhs);
4645 3486733 : use_operand_p use_p;
4646 3486733 : imm_use_iterator iter;
4647 3486733 : bool use_seen = false;
4648 3486733 : bool ovf_use_seen = false;
4649 3486733 : gimple *use_stmt;
4650 3486733 : gimple *add_stmt = NULL;
4651 3486733 : bool add_first = false;
4652 3486733 : gimple *cond_stmt = NULL;
4653 3486733 : gimple *cast_stmt = NULL;
4654 3486733 : tree cast_lhs = NULL_TREE;
4655 :
4656 3486733 : gcc_checking_assert (code == PLUS_EXPR
4657 : || code == MINUS_EXPR
4658 : || code == MULT_EXPR
4659 : || code == BIT_NOT_EXPR);
4660 3486733 : if (!INTEGRAL_TYPE_P (type)
4661 2955612 : || !TYPE_UNSIGNED (type)
4662 2024612 : || has_zero_uses (lhs)
4663 3486733 : || (code != PLUS_EXPR
4664 2024286 : && code != MULT_EXPR
4665 178026 : && optab_handler (code == MINUS_EXPR ? usubv4_optab : uaddv4_optab,
4666 151774 : TYPE_MODE (type)) == CODE_FOR_nothing))
4667 : return false;
4668 :
4669 2022149 : tree rhs1 = gimple_assign_rhs1 (stmt);
4670 2022149 : tree rhs2 = gimple_assign_rhs2 (stmt);
4671 5553734 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4672 : {
4673 3537584 : use_stmt = USE_STMT (use_p);
4674 3537584 : if (is_gimple_debug (use_stmt))
4675 551324 : continue;
4676 :
4677 2986260 : tree other = NULL_TREE;
4678 2986260 : if (arith_overflow_check_p (stmt, NULL, use_stmt, NULL_TREE, &other))
4679 : {
4680 6637 : if (code == BIT_NOT_EXPR)
4681 : {
4682 309 : gcc_assert (other);
4683 309 : if (TREE_CODE (other) != SSA_NAME)
4684 0 : return false;
4685 309 : if (rhs2 == NULL)
4686 309 : rhs2 = other;
4687 : else
4688 : return false;
4689 309 : cond_stmt = use_stmt;
4690 : }
4691 : ovf_use_seen = true;
4692 : }
4693 : else
4694 : {
4695 2979623 : use_seen = true;
4696 2979623 : if (code == MULT_EXPR
4697 2979623 : && cast_stmt == NULL
4698 2979623 : && gimple_assign_cast_p (use_stmt))
4699 : {
4700 24550 : cast_lhs = gimple_assign_lhs (use_stmt);
4701 49100 : if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs))
4702 23998 : && !TYPE_UNSIGNED (TREE_TYPE (cast_lhs))
4703 43224 : && (TYPE_PRECISION (TREE_TYPE (cast_lhs))
4704 18674 : == TYPE_PRECISION (TREE_TYPE (lhs))))
4705 : cast_stmt = use_stmt;
4706 : else
4707 : cast_lhs = NULL_TREE;
4708 : }
4709 : }
4710 2986260 : if (ovf_use_seen && use_seen)
4711 : break;
4712 0 : }
4713 :
4714 2022149 : if (!ovf_use_seen
4715 2022149 : && code == MULT_EXPR
4716 470770 : && cast_stmt)
4717 : {
4718 18221 : if (TREE_CODE (rhs1) != SSA_NAME
4719 18221 : || (TREE_CODE (rhs2) != SSA_NAME && TREE_CODE (rhs2) != INTEGER_CST))
4720 : return false;
4721 42853 : FOR_EACH_IMM_USE_FAST (use_p, iter, cast_lhs)
4722 : {
4723 24632 : use_stmt = USE_STMT (use_p);
4724 24632 : if (is_gimple_debug (use_stmt))
4725 1752 : continue;
4726 :
4727 22880 : if (arith_overflow_check_p (stmt, cast_stmt, use_stmt,
4728 : NULL_TREE, NULL))
4729 24632 : ovf_use_seen = true;
4730 18221 : }
4731 18221 : }
4732 : else
4733 : {
4734 : cast_stmt = NULL;
4735 : cast_lhs = NULL_TREE;
4736 : }
4737 :
4738 2022149 : tree maxval = NULL_TREE;
4739 2022149 : if (!ovf_use_seen
4740 13227 : || (code != MULT_EXPR && (code == BIT_NOT_EXPR ? use_seen : !use_seen))
4741 6239 : || (code == PLUS_EXPR
4742 5847 : && optab_handler (uaddv4_optab,
4743 5847 : TYPE_MODE (type)) == CODE_FOR_nothing)
4744 2035079 : || (code == MULT_EXPR
4745 235 : && optab_handler (cast_stmt ? mulv4_optab : umulv4_optab,
4746 155 : TYPE_MODE (type)) == CODE_FOR_nothing
4747 3 : && (use_seen
4748 3 : || cast_stmt
4749 0 : || !can_mult_highpart_p (TYPE_MODE (type), true))))
4750 : {
4751 2015758 : if (code != PLUS_EXPR)
4752 : return false;
4753 1395815 : if (TREE_CODE (rhs1) != SSA_NAME
4754 1395815 : || !gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs1)))
4755 : return false;
4756 328622 : rhs1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs1));
4757 328622 : tree type1 = TREE_TYPE (rhs1);
4758 328622 : if (!INTEGRAL_TYPE_P (type1)
4759 173284 : || !TYPE_UNSIGNED (type1)
4760 33104 : || TYPE_PRECISION (type1) >= TYPE_PRECISION (type)
4761 342755 : || (TYPE_PRECISION (type1)
4762 342755 : != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type1))))
4763 : return false;
4764 9519 : if (TREE_CODE (rhs2) == INTEGER_CST)
4765 : {
4766 3922 : if (wi::ne_p (wi::rshift (wi::to_wide (rhs2),
4767 3922 : TYPE_PRECISION (type1),
4768 7844 : UNSIGNED), 0))
4769 : return false;
4770 1362 : rhs2 = fold_convert (type1, rhs2);
4771 : }
4772 : else
4773 : {
4774 5597 : if (TREE_CODE (rhs2) != SSA_NAME
4775 5597 : || !gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs2)))
4776 : return false;
4777 2640 : rhs2 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs2));
4778 2640 : tree type2 = TREE_TYPE (rhs2);
4779 2640 : if (!INTEGRAL_TYPE_P (type2)
4780 1350 : || !TYPE_UNSIGNED (type2)
4781 560 : || TYPE_PRECISION (type2) >= TYPE_PRECISION (type)
4782 3172 : || (TYPE_PRECISION (type2)
4783 3172 : != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type2))))
4784 : return false;
4785 : }
4786 1881 : if (TYPE_PRECISION (type1) >= TYPE_PRECISION (TREE_TYPE (rhs2)))
4787 : type = type1;
4788 : else
4789 3 : type = TREE_TYPE (rhs2);
4790 :
4791 1881 : if (TREE_CODE (type) != INTEGER_TYPE
4792 3762 : || optab_handler (uaddv4_optab,
4793 1881 : TYPE_MODE (type)) == CODE_FOR_nothing)
4794 : return false;
4795 :
4796 1881 : maxval = wide_int_to_tree (type, wi::max_value (TYPE_PRECISION (type),
4797 : UNSIGNED));
4798 1881 : ovf_use_seen = false;
4799 1881 : use_seen = false;
4800 1881 : basic_block use_bb = NULL;
4801 2100 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
4802 : {
4803 2026 : use_stmt = USE_STMT (use_p);
4804 2026 : if (is_gimple_debug (use_stmt))
4805 141 : continue;
4806 :
4807 1885 : if (arith_overflow_check_p (stmt, NULL, use_stmt, maxval, NULL))
4808 : {
4809 13 : ovf_use_seen = true;
4810 13 : use_bb = gimple_bb (use_stmt);
4811 : }
4812 : else
4813 : {
4814 1872 : if (!gimple_assign_cast_p (use_stmt)
4815 1872 : || gimple_assign_rhs_code (use_stmt) == VIEW_CONVERT_EXPR)
4816 : return false;
4817 126 : tree use_lhs = gimple_assign_lhs (use_stmt);
4818 252 : if (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
4819 252 : || (TYPE_PRECISION (TREE_TYPE (use_lhs))
4820 126 : > TYPE_PRECISION (type)))
4821 : return false;
4822 : use_seen = true;
4823 : }
4824 1807 : }
4825 74 : if (!ovf_use_seen)
4826 : return false;
4827 13 : if (!useless_type_conversion_p (type, TREE_TYPE (rhs1)))
4828 : {
4829 2 : if (!use_seen)
4830 : return false;
4831 2 : tree new_rhs1 = make_ssa_name (type);
4832 2 : gimple *g = gimple_build_assign (new_rhs1, NOP_EXPR, rhs1);
4833 2 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
4834 2 : rhs1 = new_rhs1;
4835 : }
4836 11 : else if (!useless_type_conversion_p (type, TREE_TYPE (rhs2)))
4837 : {
4838 2 : if (!use_seen)
4839 : return false;
4840 2 : tree new_rhs2 = make_ssa_name (type);
4841 2 : gimple *g = gimple_build_assign (new_rhs2, NOP_EXPR, rhs2);
4842 2 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
4843 2 : rhs2 = new_rhs2;
4844 : }
4845 9 : else if (!use_seen)
4846 : {
4847 : /* If there are no uses of the wider addition, check if
4848 : forwprop has not created a narrower addition.
4849 : Require it to be in the same bb as the overflow check. */
4850 12 : FOR_EACH_IMM_USE_FAST (use_p, iter, rhs1)
4851 : {
4852 11 : use_stmt = USE_STMT (use_p);
4853 11 : if (is_gimple_debug (use_stmt))
4854 0 : continue;
4855 :
4856 11 : if (use_stmt == stmt)
4857 0 : continue;
4858 :
4859 11 : auto assign = dyn_cast<gassign *> (use_stmt);
4860 11 : if (!assign
4861 11 : || gimple_bb (assign) != use_bb
4862 11 : || gimple_assign_rhs_code (assign) != PLUS_EXPR)
4863 3 : continue;
4864 :
4865 8 : if (gimple_assign_rhs1 (assign) == rhs1)
4866 : {
4867 16 : if (!operand_equal_p (gimple_assign_rhs2 (assign),
4868 : rhs2, 0))
4869 0 : continue;
4870 : }
4871 0 : else if (gimple_assign_rhs2 (assign) == rhs1)
4872 : {
4873 0 : if (gimple_assign_rhs1 (assign) != rhs2)
4874 0 : continue;
4875 : }
4876 : else
4877 0 : continue;
4878 :
4879 : add_stmt = assign;
4880 : break;
4881 9 : }
4882 9 : if (add_stmt == NULL)
4883 : return false;
4884 :
4885 : /* If stmt and add_stmt are in the same bb, we need to find out
4886 : which one is earlier. If they are in different bbs, we've
4887 : checked add_stmt is in the same bb as one of the uses of the
4888 : stmt lhs, so stmt needs to dominate add_stmt too. */
4889 8 : if (gimple_bb (stmt) == gimple_bb (add_stmt))
4890 : {
4891 8 : gimple_stmt_iterator gsif = *gsi;
4892 8 : gimple_stmt_iterator gsib = *gsi;
4893 8 : int i;
4894 : /* Search both forward and backward from stmt and have a small
4895 : upper bound. */
4896 20 : for (i = 0; i < 128; i++)
4897 : {
4898 20 : if (!gsi_end_p (gsib))
4899 : {
4900 18 : gsi_prev_nondebug (&gsib);
4901 18 : if (gsi_stmt (gsib) == add_stmt)
4902 : {
4903 : add_first = true;
4904 : break;
4905 : }
4906 : }
4907 2 : else if (gsi_end_p (gsif))
4908 : break;
4909 18 : if (!gsi_end_p (gsif))
4910 : {
4911 18 : gsi_next_nondebug (&gsif);
4912 18 : if (gsi_stmt (gsif) == add_stmt)
4913 : break;
4914 : }
4915 : }
4916 8 : if (i == 128)
4917 0 : return false;
4918 8 : if (add_first)
4919 2 : *gsi = gsi_for_stmt (add_stmt);
4920 : }
4921 : }
4922 : }
4923 :
4924 6403 : if (code == BIT_NOT_EXPR)
4925 292 : *gsi = gsi_for_stmt (cond_stmt);
4926 :
4927 6403 : auto_vec<gimple *, 8> mul_stmts;
4928 6403 : if (code == MULT_EXPR && cast_stmt)
4929 : {
4930 75 : type = TREE_TYPE (cast_lhs);
4931 75 : gimple *g = SSA_NAME_DEF_STMT (rhs1);
4932 75 : if (gimple_assign_cast_p (g)
4933 38 : && useless_type_conversion_p (type,
4934 38 : TREE_TYPE (gimple_assign_rhs1 (g)))
4935 113 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g)))
4936 : rhs1 = gimple_assign_rhs1 (g);
4937 : else
4938 : {
4939 37 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, rhs1);
4940 37 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
4941 37 : rhs1 = gimple_assign_lhs (g);
4942 37 : mul_stmts.quick_push (g);
4943 : }
4944 75 : if (TREE_CODE (rhs2) == INTEGER_CST)
4945 32 : rhs2 = fold_convert (type, rhs2);
4946 : else
4947 : {
4948 43 : g = SSA_NAME_DEF_STMT (rhs2);
4949 43 : if (gimple_assign_cast_p (g)
4950 22 : && useless_type_conversion_p (type,
4951 22 : TREE_TYPE (gimple_assign_rhs1 (g)))
4952 65 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g)))
4953 : rhs2 = gimple_assign_rhs1 (g);
4954 : else
4955 : {
4956 21 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, rhs2);
4957 21 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
4958 21 : rhs2 = gimple_assign_lhs (g);
4959 21 : mul_stmts.quick_push (g);
4960 : }
4961 : }
4962 : }
4963 6403 : internal_fn ifn = (code == MULT_EXPR
4964 6403 : ? IFN_MUL_OVERFLOW
4965 : : code != MINUS_EXPR
4966 6251 : ? IFN_ADD_OVERFLOW : IFN_SUB_OVERFLOW);
4967 : if (code != MINUS_EXPR
4968 6303 : && tree_swap_operands_p (rhs1, rhs2))
4969 : std::swap (rhs1, rhs2);
4970 :
4971 6403 : tree ctype = build_complex_type (type);
4972 6403 : gcall *g = gimple_build_call_internal (ifn, 2, rhs1, rhs2);
4973 6403 : tree ctmp = make_ssa_name (ctype);
4974 6403 : gimple_call_set_lhs (g, ctmp);
4975 6403 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
4976 6403 : tree new_lhs = (maxval || cast_stmt) ? make_ssa_name (type) : lhs;
4977 6403 : gassign *g2;
4978 6403 : if (code != BIT_NOT_EXPR)
4979 : {
4980 6111 : g2 = gimple_build_assign (new_lhs, REALPART_EXPR,
4981 : build1 (REALPART_EXPR, type, ctmp));
4982 6111 : if (maxval || cast_stmt)
4983 : {
4984 87 : gsi_insert_before (gsi, g2, GSI_SAME_STMT);
4985 87 : if (add_first)
4986 2 : *gsi = gsi_for_stmt (stmt);
4987 : }
4988 : else
4989 6024 : gsi_replace (gsi, g2, true);
4990 6111 : if (code == MULT_EXPR)
4991 : {
4992 152 : mul_stmts.quick_push (g);
4993 152 : mul_stmts.quick_push (g2);
4994 152 : if (cast_stmt)
4995 : {
4996 75 : g2 = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
4997 75 : gsi_replace (gsi, g2, true);
4998 75 : mul_stmts.quick_push (g2);
4999 : }
5000 : }
5001 : }
5002 6403 : tree ovf = make_ssa_name (type);
5003 6403 : g2 = gimple_build_assign (ovf, IMAGPART_EXPR,
5004 : build1 (IMAGPART_EXPR, type, ctmp));
5005 6403 : if (code != BIT_NOT_EXPR)
5006 6111 : gsi_insert_after (gsi, g2, GSI_NEW_STMT);
5007 : else
5008 292 : gsi_insert_before (gsi, g2, GSI_SAME_STMT);
5009 6403 : if (code == MULT_EXPR)
5010 152 : mul_stmts.quick_push (g2);
5011 :
5012 33891 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, cast_lhs ? cast_lhs : lhs)
5013 : {
5014 21160 : if (is_gimple_debug (use_stmt))
5015 4305 : continue;
5016 :
5017 16855 : gimple *orig_use_stmt = use_stmt;
5018 16855 : bool remove_use_stmt = false;
5019 16855 : int ovf_use = arith_overflow_check_p (stmt, cast_stmt, use_stmt,
5020 : maxval, NULL);
5021 16855 : if (ovf_use == 0)
5022 : {
5023 10401 : gcc_assert (code != BIT_NOT_EXPR);
5024 10401 : if (maxval)
5025 : {
5026 4 : tree use_lhs = gimple_assign_lhs (use_stmt);
5027 4 : gimple_assign_set_rhs1 (use_stmt, new_lhs);
5028 4 : if (useless_type_conversion_p (TREE_TYPE (use_lhs),
5029 4 : TREE_TYPE (new_lhs)))
5030 4 : gimple_assign_set_rhs_code (use_stmt, SSA_NAME);
5031 4 : update_stmt (use_stmt);
5032 : }
5033 10401 : continue;
5034 10401 : }
5035 6454 : if (gimple_code (use_stmt) == GIMPLE_COND)
5036 : {
5037 4223 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
5038 4223 : gimple_cond_set_lhs (cond_stmt, ovf);
5039 4223 : gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0));
5040 4453 : gimple_cond_set_code (cond_stmt, ovf_use == 1 ? NE_EXPR : EQ_EXPR);
5041 : }
5042 : else
5043 : {
5044 2231 : gcc_checking_assert (is_gimple_assign (use_stmt));
5045 2231 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
5046 : {
5047 2231 : if (gimple_assign_rhs_code (use_stmt) == RSHIFT_EXPR)
5048 : {
5049 6 : g2 = gimple_build_assign (make_ssa_name (boolean_type_node),
5050 : ovf_use == 1 ? NE_EXPR : EQ_EXPR,
5051 : ovf, build_int_cst (type, 0));
5052 6 : gimple_stmt_iterator gsiu = gsi_for_stmt (use_stmt);
5053 6 : gsi_insert_before (&gsiu, g2, GSI_SAME_STMT);
5054 6 : gimple_assign_set_rhs_with_ops (&gsiu, NOP_EXPR,
5055 : gimple_assign_lhs (g2));
5056 6 : update_stmt (use_stmt);
5057 6 : use_operand_p use;
5058 6 : single_imm_use (gimple_assign_lhs (use_stmt), &use,
5059 : &use_stmt);
5060 6 : if (gimple_code (use_stmt) == GIMPLE_COND)
5061 : {
5062 0 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
5063 0 : gimple_cond_set_lhs (cond_stmt, ovf);
5064 0 : gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0));
5065 : }
5066 : else
5067 : {
5068 6 : gcc_checking_assert (is_gimple_assign (use_stmt));
5069 6 : if (gimple_assign_rhs_class (use_stmt)
5070 : == GIMPLE_BINARY_RHS)
5071 : {
5072 0 : gimple_assign_set_rhs1 (use_stmt, ovf);
5073 0 : gimple_assign_set_rhs2 (use_stmt,
5074 : build_int_cst (type, 0));
5075 : }
5076 6 : else if (gimple_assign_cast_p (use_stmt))
5077 6 : gimple_assign_set_rhs1 (use_stmt, ovf);
5078 : else
5079 : {
5080 0 : tree_code sc = gimple_assign_rhs_code (use_stmt);
5081 0 : gcc_checking_assert (sc == COND_EXPR);
5082 0 : tree cond = gimple_assign_rhs1 (use_stmt);
5083 0 : cond = build2 (TREE_CODE (cond),
5084 : boolean_type_node, ovf,
5085 : build_int_cst (type, 0));
5086 0 : gimple_assign_set_rhs1 (use_stmt, cond);
5087 : }
5088 : }
5089 6 : update_stmt (use_stmt);
5090 6 : gsi_remove (&gsiu, true);
5091 6 : gsiu = gsi_for_stmt (g2);
5092 6 : gsi_remove (&gsiu, true);
5093 6 : continue;
5094 6 : }
5095 : else
5096 : {
5097 2225 : gimple_assign_set_rhs1 (use_stmt, ovf);
5098 2225 : gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0));
5099 2359 : gimple_assign_set_rhs_code (use_stmt,
5100 : ovf_use == 1
5101 : ? NE_EXPR : EQ_EXPR);
5102 2225 : if (ovf_use == 1
5103 2225 : && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1))
5104 : {
5105 : /* Try to optimize further.
5106 : _6 = ovf != 0;
5107 : _7 = (unsigned long int) _6;
5108 : where we know ovf is in [0, 1] range can be
5109 : optimized into
5110 : _7 = (unsigned long int) ovf;
5111 : Normally, vrp pass can do this, but there is
5112 : no vrp after widening_mul. */
5113 2091 : imm_use_iterator use2_iter;
5114 2091 : gimple *use2_stmt;
5115 2091 : tree cmp_lhs = gimple_assign_lhs (use_stmt);
5116 :
5117 4479 : FOR_EACH_IMM_USE_STMT (use2_stmt, use2_iter, cmp_lhs)
5118 : {
5119 2388 : if (!gimple_assign_cast_p (use2_stmt))
5120 1558 : continue;
5121 830 : tree cvt_lhs = gimple_assign_lhs (use2_stmt);
5122 830 : tree cvt_rhs = gimple_assign_rhs1 (use2_stmt);
5123 1660 : if (INTEGRAL_TYPE_P (TREE_TYPE (cvt_lhs))
5124 1660 : && cvt_rhs == cmp_lhs)
5125 : {
5126 830 : gimple_assign_set_rhs1 (use2_stmt, ovf);
5127 830 : update_stmt (use2_stmt);
5128 : }
5129 2091 : }
5130 2091 : if (has_zero_uses (cmp_lhs))
5131 : remove_use_stmt = true;
5132 : }
5133 : }
5134 : }
5135 : else
5136 : {
5137 0 : gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
5138 : == COND_EXPR);
5139 0 : tree cond = build2 (ovf_use == 1 ? NE_EXPR : EQ_EXPR,
5140 : boolean_type_node, ovf,
5141 : build_int_cst (type, 0));
5142 0 : gimple_assign_set_rhs1 (use_stmt, cond);
5143 : }
5144 : }
5145 6448 : update_stmt (use_stmt);
5146 6448 : if (code == MULT_EXPR && use_stmt != orig_use_stmt)
5147 : {
5148 173 : gimple_stmt_iterator gsi2 = gsi_for_stmt (orig_use_stmt);
5149 173 : maybe_optimize_guarding_check (mul_stmts, use_stmt, orig_use_stmt,
5150 : cfg_changed);
5151 173 : use_operand_p use;
5152 173 : gimple *cast_stmt;
5153 173 : if (single_imm_use (gimple_assign_lhs (orig_use_stmt), &use,
5154 : &cast_stmt)
5155 173 : && gimple_assign_cast_p (cast_stmt))
5156 : {
5157 2 : gimple_stmt_iterator gsi3 = gsi_for_stmt (cast_stmt);
5158 2 : gsi_remove (&gsi3, true);
5159 2 : release_ssa_name (gimple_assign_lhs (cast_stmt));
5160 : }
5161 173 : gsi_remove (&gsi2, true);
5162 173 : release_ssa_name (gimple_assign_lhs (orig_use_stmt));
5163 : }
5164 6448 : if (remove_use_stmt)
5165 : {
5166 821 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5167 821 : gsi_remove (&gsi2, true);
5168 821 : release_ssa_name (gimple_assign_lhs (use_stmt));
5169 : }
5170 6403 : }
5171 6403 : if (maxval)
5172 : {
5173 12 : gimple_stmt_iterator gsi2 = gsi_for_stmt (stmt);
5174 12 : gsi_remove (&gsi2, true);
5175 12 : if (add_stmt)
5176 : {
5177 8 : gimple *g = gimple_build_assign (gimple_assign_lhs (add_stmt),
5178 : new_lhs);
5179 8 : gsi2 = gsi_for_stmt (add_stmt);
5180 8 : gsi_replace (&gsi2, g, true);
5181 : }
5182 : }
5183 6391 : else if (code == BIT_NOT_EXPR)
5184 : {
5185 292 : *gsi = gsi_for_stmt (stmt);
5186 292 : gsi_remove (gsi, true);
5187 292 : release_ssa_name (lhs);
5188 292 : return true;
5189 : }
5190 : return false;
5191 6403 : }
5192 :
5193 : /* Helper of match_uaddc_usubc. Look through an integral cast
5194 : which should preserve [0, 1] range value (unless source has
5195 : 1-bit signed type) and the cast has single use. */
5196 :
5197 : static gimple *
5198 2115587 : uaddc_cast (gimple *g)
5199 : {
5200 2115587 : if (!gimple_assign_cast_p (g))
5201 : return g;
5202 492903 : tree op = gimple_assign_rhs1 (g);
5203 492903 : if (TREE_CODE (op) == SSA_NAME
5204 413245 : && INTEGRAL_TYPE_P (TREE_TYPE (op))
5205 278994 : && (TYPE_PRECISION (TREE_TYPE (op)) > 1
5206 5685 : || TYPE_UNSIGNED (TREE_TYPE (op)))
5207 771897 : && has_single_use (gimple_assign_lhs (g)))
5208 164356 : return SSA_NAME_DEF_STMT (op);
5209 : return g;
5210 : }
5211 :
5212 : /* Helper of match_uaddc_usubc. Look through a NE_EXPR
5213 : comparison with 0 which also preserves [0, 1] value range. */
5214 :
5215 : static gimple *
5216 2115747 : uaddc_ne0 (gimple *g)
5217 : {
5218 2115747 : auto a = dyn_cast<gassign *> (g);
5219 1305905 : if (a
5220 1305905 : && gimple_assign_rhs_code (a) == NE_EXPR
5221 61057 : && integer_zerop (gimple_assign_rhs2 (a))
5222 6478 : && TREE_CODE (gimple_assign_rhs1 (a)) == SSA_NAME
5223 6466 : && has_single_use (gimple_assign_lhs (a)))
5224 6190 : return SSA_NAME_DEF_STMT (gimple_assign_rhs1 (a));
5225 : return g;
5226 : }
5227 :
5228 : /* Return true if G is {REAL,IMAG}PART_EXPR PART with SSA_NAME
5229 : operand. */
5230 :
5231 : static bool
5232 2116594 : uaddc_is_cplxpart (gimple *g, tree_code part)
5233 : {
5234 2116594 : return (is_gimple_assign (g)
5235 1305256 : && gimple_assign_rhs_code (g) == part
5236 2120304 : && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (g), 0)) == SSA_NAME);
5237 : }
5238 :
5239 : /* Try to match e.g.
5240 : _29 = .ADD_OVERFLOW (_3, _4);
5241 : _30 = REALPART_EXPR <_29>;
5242 : _31 = IMAGPART_EXPR <_29>;
5243 : _32 = .ADD_OVERFLOW (_30, _38);
5244 : _33 = REALPART_EXPR <_32>;
5245 : _34 = IMAGPART_EXPR <_32>;
5246 : _35 = _31 + _34;
5247 : as
5248 : _36 = .UADDC (_3, _4, _38);
5249 : _33 = REALPART_EXPR <_36>;
5250 : _35 = IMAGPART_EXPR <_36>;
5251 : or
5252 : _22 = .SUB_OVERFLOW (_6, _5);
5253 : _23 = REALPART_EXPR <_22>;
5254 : _24 = IMAGPART_EXPR <_22>;
5255 : _25 = .SUB_OVERFLOW (_23, _37);
5256 : _26 = REALPART_EXPR <_25>;
5257 : _27 = IMAGPART_EXPR <_25>;
5258 : _28 = _24 | _27;
5259 : as
5260 : _29 = .USUBC (_6, _5, _37);
5261 : _26 = REALPART_EXPR <_29>;
5262 : _288 = IMAGPART_EXPR <_29>;
5263 : provided _38 or _37 above have [0, 1] range
5264 : and _3, _4 and _30 or _6, _5 and _23 are unsigned
5265 : integral types with the same precision. Whether + or | or ^ is
5266 : used on the IMAGPART_EXPR results doesn't matter, with one of
5267 : added or subtracted operands in [0, 1] range at most one
5268 : .ADD_OVERFLOW or .SUB_OVERFLOW will indicate overflow. */
5269 :
5270 : static bool
5271 2899558 : match_uaddc_usubc (gimple_stmt_iterator *gsi, gimple *stmt, tree_code code)
5272 : {
5273 2899558 : tree rhs[4];
5274 2899558 : rhs[0] = gimple_assign_rhs1 (stmt);
5275 2899558 : rhs[1] = gimple_assign_rhs2 (stmt);
5276 2899558 : rhs[2] = NULL_TREE;
5277 2899558 : rhs[3] = NULL_TREE;
5278 2899558 : tree type = TREE_TYPE (rhs[0]);
5279 2899558 : if (!INTEGRAL_TYPE_P (type) || !TYPE_UNSIGNED (type))
5280 : return false;
5281 :
5282 1688908 : auto_vec<gimple *, 2> temp_stmts;
5283 1688908 : if (code != BIT_IOR_EXPR && code != BIT_XOR_EXPR)
5284 : {
5285 : /* If overflow flag is ignored on the MSB limb, we can end up with
5286 : the most significant limb handled as r = op1 + op2 + ovf1 + ovf2;
5287 : or r = op1 - op2 - ovf1 - ovf2; or various equivalent expressions
5288 : thereof. Handle those like the ovf = ovf1 + ovf2; case to recognize
5289 : the limb below the MSB, but also create another .UADDC/.USUBC call
5290 : for the last limb.
5291 :
5292 : First look through assignments with the same rhs code as CODE,
5293 : with the exception that subtraction of a constant is canonicalized
5294 : into addition of its negation. rhs[0] will be minuend for
5295 : subtractions and one of addends for addition, all other assigned
5296 : rhs[i] operands will be subtrahends or other addends. */
5297 1559832 : while (TREE_CODE (rhs[0]) == SSA_NAME && !rhs[3])
5298 : {
5299 1537599 : gimple *g = SSA_NAME_DEF_STMT (rhs[0]);
5300 1537599 : if (has_single_use (rhs[0])
5301 494754 : && is_gimple_assign (g)
5302 1964900 : && (gimple_assign_rhs_code (g) == code
5303 401736 : || (code == MINUS_EXPR
5304 52944 : && gimple_assign_rhs_code (g) == PLUS_EXPR
5305 16109 : && TREE_CODE (gimple_assign_rhs2 (g)) == INTEGER_CST)))
5306 : {
5307 38396 : tree r2 = gimple_assign_rhs2 (g);
5308 38396 : if (gimple_assign_rhs_code (g) != code)
5309 : {
5310 12831 : r2 = const_unop (NEGATE_EXPR, TREE_TYPE (r2), r2);
5311 12831 : if (!r2)
5312 : break;
5313 : }
5314 38396 : rhs[0] = gimple_assign_rhs1 (g);
5315 38396 : tree &r = rhs[2] ? rhs[3] : rhs[2];
5316 38396 : r = r2;
5317 38396 : temp_stmts.quick_push (g);
5318 : }
5319 : else
5320 : break;
5321 : }
5322 4564308 : for (int i = 1; i <= 2; ++i)
5323 3077997 : while (rhs[i] && TREE_CODE (rhs[i]) == SSA_NAME && !rhs[3])
5324 : {
5325 539919 : gimple *g = SSA_NAME_DEF_STMT (rhs[i]);
5326 539919 : if (has_single_use (rhs[i])
5327 256987 : && is_gimple_assign (g)
5328 778486 : && gimple_assign_rhs_code (g) == PLUS_EXPR)
5329 : {
5330 35125 : rhs[i] = gimple_assign_rhs1 (g);
5331 35125 : if (rhs[2])
5332 5310 : rhs[3] = gimple_assign_rhs2 (g);
5333 : else
5334 29815 : rhs[2] = gimple_assign_rhs2 (g);
5335 35125 : temp_stmts.quick_push (g);
5336 : }
5337 : else
5338 : break;
5339 : }
5340 : /* If there are just 3 addends or one minuend and two subtrahends,
5341 : check for UADDC or USUBC being pattern recognized earlier.
5342 : Say r = op1 + op2 + ovf1 + ovf2; where the (ovf1 + ovf2) part
5343 : got pattern matched earlier as __imag__ .UADDC (arg1, arg2, arg3)
5344 : etc. */
5345 1521436 : if (rhs[2] && !rhs[3])
5346 : {
5347 220950 : for (int i = (code == MINUS_EXPR ? 1 : 0); i < 3; ++i)
5348 162226 : if (TREE_CODE (rhs[i]) == SSA_NAME)
5349 : {
5350 122901 : gimple *im = uaddc_cast (SSA_NAME_DEF_STMT (rhs[i]));
5351 122901 : im = uaddc_ne0 (im);
5352 122901 : if (uaddc_is_cplxpart (im, IMAGPART_EXPR))
5353 : {
5354 : /* We found one of the 3 addends or 2 subtrahends to be
5355 : __imag__ of something, verify it is .UADDC/.USUBC. */
5356 232 : tree rhs1 = gimple_assign_rhs1 (im);
5357 232 : gimple *ovf = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs1, 0));
5358 232 : tree ovf_lhs = NULL_TREE;
5359 232 : tree ovf_arg1 = NULL_TREE, ovf_arg2 = NULL_TREE;
5360 252 : if (gimple_call_internal_p (ovf, code == PLUS_EXPR
5361 : ? IFN_ADD_OVERFLOW
5362 : : IFN_SUB_OVERFLOW))
5363 : {
5364 : /* Or verify it is .ADD_OVERFLOW/.SUB_OVERFLOW.
5365 : This is for the case of 2 chained .UADDC/.USUBC,
5366 : where the first one uses 0 carry-in and the second
5367 : one ignores the carry-out.
5368 : So, something like:
5369 : _16 = .ADD_OVERFLOW (_1, _2);
5370 : _17 = REALPART_EXPR <_16>;
5371 : _18 = IMAGPART_EXPR <_16>;
5372 : _15 = _3 + _4;
5373 : _12 = _15 + _18;
5374 : where the first 3 statements come from the lower
5375 : limb addition and the last 2 from the higher limb
5376 : which ignores carry-out. */
5377 197 : ovf_lhs = gimple_call_lhs (ovf);
5378 197 : tree ovf_lhs_type = TREE_TYPE (TREE_TYPE (ovf_lhs));
5379 197 : ovf_arg1 = gimple_call_arg (ovf, 0);
5380 197 : ovf_arg2 = gimple_call_arg (ovf, 1);
5381 : /* In that case we need to punt if the types don't
5382 : mismatch. */
5383 197 : if (!types_compatible_p (type, ovf_lhs_type)
5384 197 : || !types_compatible_p (type, TREE_TYPE (ovf_arg1))
5385 393 : || !types_compatible_p (type,
5386 196 : TREE_TYPE (ovf_arg2)))
5387 : ovf_lhs = NULL_TREE;
5388 : else
5389 : {
5390 492 : for (int i = (code == PLUS_EXPR ? 1 : 0);
5391 492 : i >= 0; --i)
5392 : {
5393 350 : tree r = gimple_call_arg (ovf, i);
5394 350 : if (TREE_CODE (r) != SSA_NAME)
5395 0 : continue;
5396 350 : if (uaddc_is_cplxpart (SSA_NAME_DEF_STMT (r),
5397 : REALPART_EXPR))
5398 : {
5399 : /* Punt if one of the args which isn't
5400 : subtracted isn't __real__; that could
5401 : then prevent better match later.
5402 : Consider:
5403 : _3 = .ADD_OVERFLOW (_1, _2);
5404 : _4 = REALPART_EXPR <_3>;
5405 : _5 = IMAGPART_EXPR <_3>;
5406 : _7 = .ADD_OVERFLOW (_4, _6);
5407 : _8 = REALPART_EXPR <_7>;
5408 : _9 = IMAGPART_EXPR <_7>;
5409 : _12 = _10 + _11;
5410 : _13 = _12 + _9;
5411 : _14 = _13 + _5;
5412 : We want to match this when called on
5413 : the last stmt as a pair of .UADDC calls,
5414 : but without this check we could turn
5415 : that prematurely on _13 = _12 + _9;
5416 : stmt into .UADDC with 0 carry-in just
5417 : on the second .ADD_OVERFLOW call and
5418 : another replacing the _12 and _13
5419 : additions. */
5420 : ovf_lhs = NULL_TREE;
5421 : break;
5422 : }
5423 : }
5424 : }
5425 192 : if (ovf_lhs)
5426 : {
5427 142 : use_operand_p use_p;
5428 142 : imm_use_iterator iter;
5429 142 : tree re_lhs = NULL_TREE;
5430 426 : FOR_EACH_IMM_USE_FAST (use_p, iter, ovf_lhs)
5431 : {
5432 284 : gimple *use_stmt = USE_STMT (use_p);
5433 284 : if (is_gimple_debug (use_stmt))
5434 0 : continue;
5435 284 : if (use_stmt == im)
5436 142 : continue;
5437 142 : if (!uaddc_is_cplxpart (use_stmt,
5438 : REALPART_EXPR))
5439 : {
5440 : ovf_lhs = NULL_TREE;
5441 : break;
5442 : }
5443 142 : re_lhs = gimple_assign_lhs (use_stmt);
5444 142 : }
5445 142 : if (ovf_lhs && re_lhs)
5446 : {
5447 385 : FOR_EACH_IMM_USE_FAST (use_p, iter, re_lhs)
5448 : {
5449 297 : gimple *use_stmt = USE_STMT (use_p);
5450 297 : if (is_gimple_debug (use_stmt))
5451 108 : continue;
5452 189 : internal_fn ifn
5453 189 : = gimple_call_internal_fn (ovf);
5454 : /* Punt if the __real__ of lhs is used
5455 : in the same .*_OVERFLOW call.
5456 : Consider:
5457 : _3 = .ADD_OVERFLOW (_1, _2);
5458 : _4 = REALPART_EXPR <_3>;
5459 : _5 = IMAGPART_EXPR <_3>;
5460 : _7 = .ADD_OVERFLOW (_4, _6);
5461 : _8 = REALPART_EXPR <_7>;
5462 : _9 = IMAGPART_EXPR <_7>;
5463 : _12 = _10 + _11;
5464 : _13 = _12 + _5;
5465 : _14 = _13 + _9;
5466 : We want to match this when called on
5467 : the last stmt as a pair of .UADDC calls,
5468 : but without this check we could turn
5469 : that prematurely on _13 = _12 + _5;
5470 : stmt into .UADDC with 0 carry-in just
5471 : on the first .ADD_OVERFLOW call and
5472 : another replacing the _12 and _13
5473 : additions. */
5474 189 : if (gimple_call_internal_p (use_stmt, ifn))
5475 : {
5476 : ovf_lhs = NULL_TREE;
5477 : break;
5478 : }
5479 142 : }
5480 : }
5481 : }
5482 : }
5483 142 : if ((ovf_lhs
5484 153 : || gimple_call_internal_p (ovf,
5485 : code == PLUS_EXPR
5486 : ? IFN_UADDC : IFN_USUBC))
5487 250 : && (optab_handler (code == PLUS_EXPR
5488 : ? uaddc5_optab : usubc5_optab,
5489 94 : TYPE_MODE (type))
5490 : != CODE_FOR_nothing))
5491 : {
5492 : /* And in that case build another .UADDC/.USUBC
5493 : call for the most significand limb addition.
5494 : Overflow bit is ignored here. */
5495 63 : if (i != 2)
5496 63 : std::swap (rhs[i], rhs[2]);
5497 63 : gimple *g
5498 77 : = gimple_build_call_internal (code == PLUS_EXPR
5499 : ? IFN_UADDC
5500 : : IFN_USUBC,
5501 : 3, rhs[0], rhs[1],
5502 : rhs[2]);
5503 63 : tree nlhs = make_ssa_name (build_complex_type (type));
5504 63 : gimple_call_set_lhs (g, nlhs);
5505 63 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5506 63 : tree ilhs = gimple_assign_lhs (stmt);
5507 63 : g = gimple_build_assign (ilhs, REALPART_EXPR,
5508 : build1 (REALPART_EXPR,
5509 63 : TREE_TYPE (ilhs),
5510 : nlhs));
5511 63 : gsi_replace (gsi, g, true);
5512 : /* And if it is initialized from result of __imag__
5513 : of .{ADD,SUB}_OVERFLOW call, replace that
5514 : call with .U{ADD,SUB}C call with the same arguments,
5515 : just 0 added as third argument. This isn't strictly
5516 : necessary, .ADD_OVERFLOW (x, y) and .UADDC (x, y, 0)
5517 : produce the same result, but may result in better
5518 : generated code on some targets where the backend can
5519 : better prepare in how the result will be used. */
5520 63 : if (ovf_lhs)
5521 : {
5522 57 : tree zero = build_zero_cst (type);
5523 57 : g = gimple_build_call_internal (code == PLUS_EXPR
5524 : ? IFN_UADDC
5525 : : IFN_USUBC,
5526 : 3, ovf_arg1,
5527 : ovf_arg2, zero);
5528 57 : gimple_call_set_lhs (g, ovf_lhs);
5529 57 : gimple_stmt_iterator gsi2 = gsi_for_stmt (ovf);
5530 57 : gsi_replace (&gsi2, g, true);
5531 : }
5532 : return true;
5533 : }
5534 : }
5535 : }
5536 : return false;
5537 : }
5538 1462649 : if (code == MINUS_EXPR && !rhs[2])
5539 : return false;
5540 241 : if (code == MINUS_EXPR)
5541 : /* Code below expects rhs[0] and rhs[1] to have the IMAGPART_EXPRs.
5542 : So, for MINUS_EXPR swap the single added rhs operand (others are
5543 : subtracted) to rhs[3]. */
5544 241 : std::swap (rhs[0], rhs[3]);
5545 : }
5546 : /* Walk from both operands of STMT (for +/- even sometimes from
5547 : all the 4 addends or 3 subtrahends), see through casts and != 0
5548 : statements which would preserve [0, 1] range of values and
5549 : check which is initialized from __imag__. */
5550 1518993 : gimple *im1 = NULL, *im2 = NULL;
5551 15186330 : for (int i = 0; i < (code == MINUS_EXPR ? 3 : 4); i++)
5552 6075020 : if (rhs[i] && TREE_CODE (rhs[i]) == SSA_NAME)
5553 : {
5554 1992594 : gimple *im = uaddc_cast (SSA_NAME_DEF_STMT (rhs[i]));
5555 1992594 : im = uaddc_ne0 (im);
5556 1992594 : if (uaddc_is_cplxpart (im, IMAGPART_EXPR))
5557 : {
5558 2954 : if (im1 == NULL)
5559 : {
5560 2585 : im1 = im;
5561 2585 : if (i != 0)
5562 904 : std::swap (rhs[0], rhs[i]);
5563 : }
5564 : else
5565 : {
5566 369 : im2 = im;
5567 369 : if (i != 1)
5568 20 : std::swap (rhs[1], rhs[i]);
5569 : break;
5570 : }
5571 : }
5572 : }
5573 : /* If we don't find at least two, punt. */
5574 1518993 : if (!im2)
5575 : return false;
5576 : /* Check they are __imag__ of .ADD_OVERFLOW or .SUB_OVERFLOW call results,
5577 : either both .ADD_OVERFLOW or both .SUB_OVERFLOW and that we have
5578 : uaddc5/usubc5 named pattern for the corresponding mode. */
5579 369 : gimple *ovf1
5580 369 : = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im1), 0));
5581 369 : gimple *ovf2
5582 369 : = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im2), 0));
5583 369 : internal_fn ifn;
5584 369 : if (!is_gimple_call (ovf1)
5585 369 : || !gimple_call_internal_p (ovf1)
5586 369 : || ((ifn = gimple_call_internal_fn (ovf1)) != IFN_ADD_OVERFLOW
5587 43 : && ifn != IFN_SUB_OVERFLOW)
5588 366 : || ovf1 == ovf2
5589 364 : || !gimple_call_internal_p (ovf2, ifn)
5590 394 : || optab_handler (ifn == IFN_ADD_OVERFLOW ? uaddc5_optab : usubc5_optab,
5591 360 : TYPE_MODE (type)) == CODE_FOR_nothing
5592 95 : || (rhs[2]
5593 17 : && optab_handler (code == PLUS_EXPR ? uaddc5_optab : usubc5_optab,
5594 15 : TYPE_MODE (type)) == CODE_FOR_nothing)
5595 95 : || !types_compatible_p (type,
5596 95 : TREE_TYPE (TREE_TYPE (gimple_call_lhs (ovf1))))
5597 463 : || !types_compatible_p (type,
5598 94 : TREE_TYPE (TREE_TYPE (gimple_call_lhs (ovf2)))))
5599 : return false;
5600 94 : tree arg1, arg2, arg3 = NULL_TREE;
5601 94 : gimple *re1 = NULL, *re2 = NULL;
5602 : /* On one of the two calls, one of the .ADD_OVERFLOW/.SUB_OVERFLOW arguments
5603 : should be initialized from __real__ of the other of the two calls.
5604 : Though, for .SUB_OVERFLOW, it has to be the first argument, not the
5605 : second one. */
5606 249 : for (int i = (ifn == IFN_ADD_OVERFLOW ? 1 : 0); i >= 0; --i)
5607 351 : for (gimple *ovf = ovf1; ovf; ovf = (ovf == ovf1 ? ovf2 : NULL))
5608 : {
5609 290 : tree arg = gimple_call_arg (ovf, i);
5610 290 : if (TREE_CODE (arg) != SSA_NAME)
5611 2 : continue;
5612 288 : re1 = SSA_NAME_DEF_STMT (arg);
5613 288 : if (uaddc_is_cplxpart (re1, REALPART_EXPR)
5614 382 : && (SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (re1), 0))
5615 94 : == (ovf == ovf1 ? ovf2 : ovf1)))
5616 : {
5617 94 : if (ovf == ovf1)
5618 : {
5619 : /* Make sure ovf2 is the .*_OVERFLOW call with argument
5620 : initialized from __real__ of ovf1. */
5621 20 : std::swap (rhs[0], rhs[1]);
5622 20 : std::swap (im1, im2);
5623 20 : std::swap (ovf1, ovf2);
5624 : }
5625 94 : arg3 = gimple_call_arg (ovf, 1 - i);
5626 94 : i = -1;
5627 94 : break;
5628 : }
5629 : }
5630 94 : if (!arg3)
5631 : return false;
5632 94 : arg1 = gimple_call_arg (ovf1, 0);
5633 94 : arg2 = gimple_call_arg (ovf1, 1);
5634 94 : if (!types_compatible_p (type, TREE_TYPE (arg1)))
5635 : return false;
5636 94 : int kind[2] = { 0, 0 };
5637 94 : tree arg_im[2] = { NULL_TREE, NULL_TREE };
5638 : /* At least one of arg2 and arg3 should have type compatible
5639 : with arg1/rhs[0], and the other one should have value in [0, 1]
5640 : range. If both are in [0, 1] range and type compatible with
5641 : arg1/rhs[0], try harder to find after looking through casts,
5642 : != 0 comparisons which one is initialized to __imag__ of
5643 : .{ADD,SUB}_OVERFLOW or .U{ADD,SUB}C call results. */
5644 282 : for (int i = 0; i < 2; ++i)
5645 : {
5646 188 : tree arg = i == 0 ? arg2 : arg3;
5647 188 : if (types_compatible_p (type, TREE_TYPE (arg)))
5648 163 : kind[i] = 1;
5649 376 : if (!INTEGRAL_TYPE_P (TREE_TYPE (arg))
5650 376 : || (TYPE_PRECISION (TREE_TYPE (arg)) == 1
5651 25 : && !TYPE_UNSIGNED (TREE_TYPE (arg))))
5652 0 : continue;
5653 188 : if (tree_zero_one_valued_p (arg))
5654 52 : kind[i] |= 2;
5655 188 : if (TREE_CODE (arg) == SSA_NAME)
5656 : {
5657 185 : gimple *g = SSA_NAME_DEF_STMT (arg);
5658 185 : if (gimple_assign_cast_p (g))
5659 : {
5660 30 : tree op = gimple_assign_rhs1 (g);
5661 30 : if (TREE_CODE (op) == SSA_NAME
5662 30 : && INTEGRAL_TYPE_P (TREE_TYPE (op)))
5663 30 : g = SSA_NAME_DEF_STMT (op);
5664 : }
5665 185 : g = uaddc_ne0 (g);
5666 185 : if (!uaddc_is_cplxpart (g, IMAGPART_EXPR))
5667 125 : continue;
5668 60 : arg_im[i] = gimple_assign_lhs (g);
5669 60 : g = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (g), 0));
5670 60 : if (!is_gimple_call (g) || !gimple_call_internal_p (g))
5671 0 : continue;
5672 60 : switch (gimple_call_internal_fn (g))
5673 : {
5674 60 : case IFN_ADD_OVERFLOW:
5675 60 : case IFN_SUB_OVERFLOW:
5676 60 : case IFN_UADDC:
5677 60 : case IFN_USUBC:
5678 60 : break;
5679 0 : default:
5680 0 : continue;
5681 : }
5682 60 : kind[i] |= 4;
5683 : }
5684 : }
5685 : /* Make arg2 the one with compatible type and arg3 the one
5686 : with [0, 1] range. If both is true for both operands,
5687 : prefer as arg3 result of __imag__ of some ifn. */
5688 94 : if ((kind[0] & 1) == 0 || ((kind[1] & 1) != 0 && kind[0] > kind[1]))
5689 : {
5690 1 : std::swap (arg2, arg3);
5691 1 : std::swap (kind[0], kind[1]);
5692 1 : std::swap (arg_im[0], arg_im[1]);
5693 : }
5694 94 : if ((kind[0] & 1) == 0 || (kind[1] & 6) == 0)
5695 : return false;
5696 70 : if (!has_single_use (gimple_assign_lhs (im1))
5697 68 : || !has_single_use (gimple_assign_lhs (im2))
5698 68 : || !has_single_use (gimple_assign_lhs (re1))
5699 138 : || num_imm_uses (gimple_call_lhs (ovf1)) != 2)
5700 : return false;
5701 : /* Check that ovf2's result is used in __real__ and set re2
5702 : to that statement. */
5703 68 : use_operand_p use_p;
5704 68 : imm_use_iterator iter;
5705 68 : tree lhs = gimple_call_lhs (ovf2);
5706 203 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5707 : {
5708 135 : gimple *use_stmt = USE_STMT (use_p);
5709 135 : if (is_gimple_debug (use_stmt))
5710 0 : continue;
5711 135 : if (use_stmt == im2)
5712 68 : continue;
5713 67 : if (re2)
5714 : return false;
5715 67 : if (!uaddc_is_cplxpart (use_stmt, REALPART_EXPR))
5716 : return false;
5717 : re2 = use_stmt;
5718 0 : }
5719 : /* Build .UADDC/.USUBC call which will be placed before the stmt. */
5720 68 : gimple_stmt_iterator gsi2 = gsi_for_stmt (ovf2);
5721 68 : gimple *g;
5722 68 : if ((kind[1] & 4) != 0 && types_compatible_p (type, TREE_TYPE (arg_im[1])))
5723 : arg3 = arg_im[1];
5724 68 : if ((kind[1] & 1) == 0)
5725 : {
5726 25 : if (TREE_CODE (arg3) == INTEGER_CST)
5727 0 : arg3 = fold_convert (type, arg3);
5728 : else
5729 : {
5730 25 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, arg3);
5731 25 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
5732 25 : arg3 = gimple_assign_lhs (g);
5733 : }
5734 : }
5735 91 : g = gimple_build_call_internal (ifn == IFN_ADD_OVERFLOW
5736 : ? IFN_UADDC : IFN_USUBC,
5737 : 3, arg1, arg2, arg3);
5738 68 : tree nlhs = make_ssa_name (TREE_TYPE (lhs));
5739 68 : gimple_call_set_lhs (g, nlhs);
5740 68 : gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
5741 : /* In the case where stmt is | or ^ of two overflow flags
5742 : or addition of those, replace stmt with __imag__ of the above
5743 : added call. In case of arg1 + arg2 + (ovf1 + ovf2) or
5744 : arg1 - arg2 - (ovf1 + ovf2) just emit it before stmt. */
5745 68 : tree ilhs = rhs[2] ? make_ssa_name (type) : gimple_assign_lhs (stmt);
5746 68 : g = gimple_build_assign (ilhs, IMAGPART_EXPR,
5747 68 : build1 (IMAGPART_EXPR, TREE_TYPE (ilhs), nlhs));
5748 68 : if (rhs[2])
5749 : {
5750 15 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5751 : /* Remove some further statements which can't be kept in the IL because
5752 : they can use SSA_NAMEs whose setter is going to be removed too. */
5753 75 : for (gimple *g2 : temp_stmts)
5754 : {
5755 30 : gsi2 = gsi_for_stmt (g2);
5756 30 : gsi_remove (&gsi2, true);
5757 30 : release_defs (g2);
5758 : }
5759 : }
5760 : else
5761 53 : gsi_replace (gsi, g, true);
5762 : /* Remove some statements which can't be kept in the IL because they
5763 : use SSA_NAME whose setter is going to be removed too. */
5764 68 : tree rhs1 = rhs[1];
5765 86 : for (int i = 0; i < 2; i++)
5766 86 : if (rhs1 == gimple_assign_lhs (im2))
5767 : break;
5768 : else
5769 : {
5770 18 : g = SSA_NAME_DEF_STMT (rhs1);
5771 18 : rhs1 = gimple_assign_rhs1 (g);
5772 18 : gsi2 = gsi_for_stmt (g);
5773 18 : gsi_remove (&gsi2, true);
5774 18 : release_defs (g);
5775 : }
5776 68 : gcc_checking_assert (rhs1 == gimple_assign_lhs (im2));
5777 68 : gsi2 = gsi_for_stmt (im2);
5778 68 : gsi_remove (&gsi2, true);
5779 68 : release_defs (im2);
5780 : /* Replace the re2 statement with __real__ of the newly added
5781 : .UADDC/.USUBC call. */
5782 68 : if (re2)
5783 : {
5784 67 : gsi2 = gsi_for_stmt (re2);
5785 67 : tree rlhs = gimple_assign_lhs (re2);
5786 67 : g = gimple_build_assign (rlhs, REALPART_EXPR,
5787 67 : build1 (REALPART_EXPR, TREE_TYPE (rlhs), nlhs));
5788 67 : gsi_replace (&gsi2, g, true);
5789 : }
5790 68 : if (rhs[2])
5791 : {
5792 : /* If this is the arg1 + arg2 + (ovf1 + ovf2) or
5793 : arg1 - arg2 - (ovf1 + ovf2) case for the most significant limb,
5794 : replace stmt with __real__ of another .UADDC/.USUBC call which
5795 : handles the most significant limb. Overflow flag from this is
5796 : ignored. */
5797 17 : g = gimple_build_call_internal (code == PLUS_EXPR
5798 : ? IFN_UADDC : IFN_USUBC,
5799 : 3, rhs[3], rhs[2], ilhs);
5800 15 : nlhs = make_ssa_name (TREE_TYPE (lhs));
5801 15 : gimple_call_set_lhs (g, nlhs);
5802 15 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5803 15 : ilhs = gimple_assign_lhs (stmt);
5804 15 : g = gimple_build_assign (ilhs, REALPART_EXPR,
5805 15 : build1 (REALPART_EXPR, TREE_TYPE (ilhs), nlhs));
5806 15 : gsi_replace (gsi, g, true);
5807 : }
5808 68 : if (TREE_CODE (arg3) == SSA_NAME)
5809 : {
5810 : /* When pattern recognizing the second least significant limb
5811 : above (i.e. first pair of .{ADD,SUB}_OVERFLOW calls for one limb),
5812 : check if the [0, 1] range argument (i.e. carry in) isn't the
5813 : result of another .{ADD,SUB}_OVERFLOW call (one handling the
5814 : least significant limb). Again look through casts and != 0. */
5815 67 : gimple *im3 = SSA_NAME_DEF_STMT (arg3);
5816 92 : for (int i = 0; i < 2; ++i)
5817 : {
5818 92 : gimple *im4 = uaddc_cast (im3);
5819 92 : if (im4 == im3)
5820 : break;
5821 : else
5822 25 : im3 = im4;
5823 : }
5824 67 : im3 = uaddc_ne0 (im3);
5825 67 : if (uaddc_is_cplxpart (im3, IMAGPART_EXPR))
5826 : {
5827 60 : gimple *ovf3
5828 60 : = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im3), 0));
5829 60 : if (gimple_call_internal_p (ovf3, ifn))
5830 : {
5831 25 : lhs = gimple_call_lhs (ovf3);
5832 25 : arg1 = gimple_call_arg (ovf3, 0);
5833 25 : arg2 = gimple_call_arg (ovf3, 1);
5834 25 : if (types_compatible_p (type, TREE_TYPE (TREE_TYPE (lhs)))
5835 25 : && types_compatible_p (type, TREE_TYPE (arg1))
5836 50 : && types_compatible_p (type, TREE_TYPE (arg2)))
5837 : {
5838 : /* And if it is initialized from result of __imag__
5839 : of .{ADD,SUB}_OVERFLOW call, replace that
5840 : call with .U{ADD,SUB}C call with the same arguments,
5841 : just 0 added as third argument. This isn't strictly
5842 : necessary, .ADD_OVERFLOW (x, y) and .UADDC (x, y, 0)
5843 : produce the same result, but may result in better
5844 : generated code on some targets where the backend can
5845 : better prepare in how the result will be used. */
5846 25 : g = gimple_build_call_internal (ifn == IFN_ADD_OVERFLOW
5847 : ? IFN_UADDC : IFN_USUBC,
5848 : 3, arg1, arg2,
5849 : build_zero_cst (type));
5850 25 : gimple_call_set_lhs (g, lhs);
5851 25 : gsi2 = gsi_for_stmt (ovf3);
5852 25 : gsi_replace (&gsi2, g, true);
5853 : }
5854 : }
5855 : }
5856 : }
5857 : return true;
5858 1688908 : }
5859 :
5860 : /* Replace .POPCOUNT (x) == 1 or .POPCOUNT (x) != 1 with
5861 : (x & (x - 1)) > x - 1 or (x & (x - 1)) <= x - 1 if .POPCOUNT
5862 : isn't a direct optab. Also handle `<=`/`>` to be
5863 : `x & (x - 1) !=/== x`. */
5864 :
5865 : static void
5866 4580639 : match_single_bit_test (gimple_stmt_iterator *gsi, gimple *stmt)
5867 : {
5868 4580639 : tree clhs, crhs;
5869 4580639 : enum tree_code code;
5870 4580639 : bool was_le = false;
5871 4580639 : if (gimple_code (stmt) == GIMPLE_COND)
5872 : {
5873 4248801 : clhs = gimple_cond_lhs (stmt);
5874 4248801 : crhs = gimple_cond_rhs (stmt);
5875 4248801 : code = gimple_cond_code (stmt);
5876 : }
5877 : else
5878 : {
5879 331838 : clhs = gimple_assign_rhs1 (stmt);
5880 331838 : crhs = gimple_assign_rhs2 (stmt);
5881 331838 : code = gimple_assign_rhs_code (stmt);
5882 : }
5883 4580639 : if (code != LE_EXPR && code != GT_EXPR
5884 4580639 : && code != EQ_EXPR && code != NE_EXPR)
5885 4580597 : return;
5886 2134382 : if (code == LE_EXPR || code == GT_EXPR)
5887 4315964 : was_le = true;
5888 4315964 : if (TREE_CODE (clhs) != SSA_NAME || !integer_onep (crhs))
5889 : return;
5890 165195 : gimple *call = SSA_NAME_DEF_STMT (clhs);
5891 165195 : combined_fn cfn = gimple_call_combined_fn (call);
5892 165195 : switch (cfn)
5893 : {
5894 51 : CASE_CFN_POPCOUNT:
5895 51 : break;
5896 : default:
5897 : return;
5898 : }
5899 51 : if (!has_single_use (clhs))
5900 : return;
5901 50 : tree arg = gimple_call_arg (call, 0);
5902 50 : tree type = TREE_TYPE (arg);
5903 50 : if (!INTEGRAL_TYPE_P (type))
5904 : return;
5905 50 : bool nonzero_arg = tree_expr_nonzero_p (arg);
5906 50 : if (direct_internal_fn_supported_p (IFN_POPCOUNT, type, OPTIMIZE_FOR_BOTH))
5907 : {
5908 : /* Tell expand_POPCOUNT the popcount result is only used in equality
5909 : comparison with one, so that it can decide based on rtx costs. */
5910 16 : gimple *g = gimple_build_call_internal (IFN_POPCOUNT, 2, arg,
5911 : was_le ? integer_minus_one_node
5912 8 : : (nonzero_arg ? integer_zero_node
5913 : : integer_one_node));
5914 8 : gimple_call_set_lhs (g, gimple_call_lhs (call));
5915 8 : gimple_stmt_iterator gsi2 = gsi_for_stmt (call);
5916 8 : gsi_replace (&gsi2, g, true);
5917 8 : return;
5918 : }
5919 42 : tree argm1 = make_ssa_name (type);
5920 42 : gassign *g = gimple_build_assign (argm1, PLUS_EXPR, arg,
5921 : build_int_cst (type, -1));
5922 42 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5923 42 : g = gimple_build_assign (make_ssa_name (type),
5924 42 : (nonzero_arg || was_le) ? BIT_AND_EXPR : BIT_XOR_EXPR,
5925 : arg, argm1);
5926 42 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5927 42 : tree_code cmpcode;
5928 42 : if (was_le)
5929 : {
5930 0 : argm1 = build_zero_cst (type);
5931 0 : cmpcode = code == LE_EXPR ? EQ_EXPR : NE_EXPR;
5932 : }
5933 42 : else if (nonzero_arg)
5934 : {
5935 2 : argm1 = build_zero_cst (type);
5936 2 : cmpcode = code;
5937 : }
5938 : else
5939 40 : cmpcode = code == EQ_EXPR ? GT_EXPR : LE_EXPR;
5940 42 : if (gcond *cond = dyn_cast <gcond *> (stmt))
5941 : {
5942 2 : gimple_cond_set_lhs (cond, gimple_assign_lhs (g));
5943 2 : gimple_cond_set_rhs (cond, argm1);
5944 2 : gimple_cond_set_code (cond, cmpcode);
5945 : }
5946 : else
5947 : {
5948 40 : gimple_assign_set_rhs1 (stmt, gimple_assign_lhs (g));
5949 40 : gimple_assign_set_rhs2 (stmt, argm1);
5950 40 : gimple_assign_set_rhs_code (stmt, cmpcode);
5951 : }
5952 42 : update_stmt (stmt);
5953 42 : gimple_stmt_iterator gsi2 = gsi_for_stmt (call);
5954 42 : gsi_remove (&gsi2, true);
5955 42 : release_defs (call);
5956 : }
5957 :
5958 : /* Return true if target has support for divmod. */
5959 :
5960 : static bool
5961 41670 : target_supports_divmod_p (optab divmod_optab, optab div_optab, machine_mode mode)
5962 : {
5963 : /* If target supports hardware divmod insn, use it for divmod. */
5964 41670 : if (optab_handler (divmod_optab, mode) != CODE_FOR_nothing)
5965 : return true;
5966 :
5967 : /* Check if libfunc for divmod is available. */
5968 2550 : rtx libfunc = optab_libfunc (divmod_optab, mode);
5969 2550 : if (libfunc != NULL_RTX)
5970 : {
5971 : /* If optab_handler exists for div_optab, perhaps in a wider mode,
5972 : we don't want to use the libfunc even if it exists for given mode. */
5973 : machine_mode div_mode;
5974 10603 : FOR_EACH_MODE_FROM (div_mode, mode)
5975 8053 : if (optab_handler (div_optab, div_mode) != CODE_FOR_nothing)
5976 : return false;
5977 :
5978 2550 : return targetm.expand_divmod_libfunc != NULL;
5979 : }
5980 :
5981 : return false;
5982 : }
5983 :
5984 : /* Check if stmt is candidate for divmod transform. */
5985 :
5986 : static bool
5987 61716 : divmod_candidate_p (gassign *stmt)
5988 : {
5989 61716 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
5990 61716 : machine_mode mode = TYPE_MODE (type);
5991 61716 : optab divmod_optab, div_optab;
5992 :
5993 61716 : if (TYPE_UNSIGNED (type))
5994 : {
5995 : divmod_optab = udivmod_optab;
5996 : div_optab = udiv_optab;
5997 : }
5998 : else
5999 : {
6000 31117 : divmod_optab = sdivmod_optab;
6001 31117 : div_optab = sdiv_optab;
6002 : }
6003 :
6004 61716 : tree op1 = gimple_assign_rhs1 (stmt);
6005 61716 : tree op2 = gimple_assign_rhs2 (stmt);
6006 :
6007 : /* Disable the transform if either is a constant, since division-by-constant
6008 : may have specialized expansion. */
6009 61716 : if (CONSTANT_CLASS_P (op1))
6010 : return false;
6011 :
6012 57293 : if (CONSTANT_CLASS_P (op2))
6013 : {
6014 17832 : if (integer_pow2p (op2))
6015 : return false;
6016 :
6017 15663 : if (element_precision (type) <= HOST_BITS_PER_WIDE_INT
6018 16713 : && element_precision (type) <= BITS_PER_WORD)
6019 : return false;
6020 :
6021 : /* If the divisor is not power of 2 and the precision wider than
6022 : HWI, expand_divmod punts on that, so in that case it is better
6023 : to use divmod optab or libfunc. Similarly if choose_multiplier
6024 : might need pre/post shifts of BITS_PER_WORD or more. */
6025 : }
6026 :
6027 : /* Exclude the case where TYPE_OVERFLOW_TRAPS (type) as that should
6028 : expand using the [su]divv optabs. */
6029 41670 : if (TYPE_OVERFLOW_TRAPS (type))
6030 : return false;
6031 :
6032 41670 : if (!target_supports_divmod_p (divmod_optab, div_optab, mode))
6033 : return false;
6034 :
6035 : return true;
6036 : }
6037 :
6038 : /* This function looks for:
6039 : t1 = a TRUNC_DIV_EXPR b;
6040 : t2 = a TRUNC_MOD_EXPR b;
6041 : and transforms it to the following sequence:
6042 : complex_tmp = DIVMOD (a, b);
6043 : t1 = REALPART_EXPR(a);
6044 : t2 = IMAGPART_EXPR(b);
6045 : For conditions enabling the transform see divmod_candidate_p().
6046 :
6047 : The pass has three parts:
6048 : 1) Find top_stmt which is trunc_div or trunc_mod stmt and dominates all
6049 : other trunc_div_expr and trunc_mod_expr stmts.
6050 : 2) Add top_stmt and all trunc_div and trunc_mod stmts dominated by top_stmt
6051 : to stmts vector.
6052 : 3) Insert DIVMOD call just before top_stmt and update entries in
6053 : stmts vector to use return value of DIMOVD (REALEXPR_PART for div,
6054 : IMAGPART_EXPR for mod). */
6055 :
6056 : static bool
6057 61735 : convert_to_divmod (gassign *stmt)
6058 : {
6059 61735 : if (stmt_can_throw_internal (cfun, stmt)
6060 61735 : || !divmod_candidate_p (stmt))
6061 : return false;
6062 :
6063 41670 : tree op1 = gimple_assign_rhs1 (stmt);
6064 41670 : tree op2 = gimple_assign_rhs2 (stmt);
6065 :
6066 41670 : imm_use_iterator use_iter;
6067 41670 : gimple *use_stmt;
6068 41670 : auto_vec<gimple *> stmts;
6069 :
6070 41670 : gassign *top_stmt = stmt;
6071 41670 : basic_block top_bb = gimple_bb (stmt);
6072 :
6073 : /* Part 1: Try to set top_stmt to "topmost" stmt that dominates
6074 : at-least stmt and possibly other trunc_div/trunc_mod stmts
6075 : having same operands as stmt. */
6076 :
6077 373195 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, op1)
6078 : {
6079 331525 : auto assign = dyn_cast<gassign *> (use_stmt);
6080 279848 : if (assign
6081 279848 : && (gimple_assign_rhs_code (assign) == TRUNC_DIV_EXPR
6082 267058 : || gimple_assign_rhs_code (assign) == TRUNC_MOD_EXPR)
6083 253907 : && operand_equal_p (op1, gimple_assign_rhs1 (assign), 0)
6084 253790 : && operand_equal_p (op2, gimple_assign_rhs2 (assign), 0))
6085 : {
6086 54351 : if (stmt_can_throw_internal (cfun, assign))
6087 0 : continue;
6088 :
6089 54351 : basic_block bb = gimple_bb (assign);
6090 :
6091 54351 : if (bb == top_bb)
6092 : {
6093 53591 : if (gimple_uid (assign) < gimple_uid (top_stmt))
6094 331525 : top_stmt = assign;
6095 : }
6096 760 : else if (dominated_by_p (CDI_DOMINATORS, top_bb, bb))
6097 : {
6098 331525 : top_bb = bb;
6099 331525 : top_stmt = assign;
6100 : }
6101 : }
6102 41670 : }
6103 :
6104 41670 : tree top_op1 = gimple_assign_rhs1 (top_stmt);
6105 41670 : tree top_op2 = gimple_assign_rhs2 (top_stmt);
6106 :
6107 41670 : stmts.safe_push (top_stmt);
6108 41670 : bool div_seen = (gimple_assign_rhs_code (top_stmt) == TRUNC_DIV_EXPR);
6109 :
6110 : /* Part 2: Add all trunc_div/trunc_mod statements domianted by top_bb
6111 : to stmts vector. The 2nd loop will always add stmt to stmts vector, since
6112 : gimple_bb (top_stmt) dominates gimple_bb (stmt), so the
6113 : 2nd loop ends up adding at-least single trunc_mod_expr stmt. */
6114 :
6115 373195 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, top_op1)
6116 : {
6117 331525 : auto assign = dyn_cast<gassign *> (use_stmt);
6118 279848 : if (assign
6119 279848 : && (gimple_assign_rhs_code (assign) == TRUNC_DIV_EXPR
6120 267058 : || gimple_assign_rhs_code (assign) == TRUNC_MOD_EXPR)
6121 253907 : && operand_equal_p (top_op1, gimple_assign_rhs1 (assign), 0)
6122 253790 : && operand_equal_p (top_op2, gimple_assign_rhs2 (assign), 0))
6123 : {
6124 96113 : if (assign == top_stmt
6125 12681 : || stmt_can_throw_internal (cfun, assign)
6126 67032 : || !dominated_by_p (CDI_DOMINATORS, gimple_bb (assign), top_bb))
6127 41762 : continue;
6128 :
6129 12589 : stmts.safe_push (assign);
6130 12589 : if (gimple_assign_rhs_code (assign) == TRUNC_DIV_EXPR)
6131 331525 : div_seen = true;
6132 : }
6133 41670 : }
6134 :
6135 41670 : if (!div_seen)
6136 : return false;
6137 :
6138 : /* Part 3: Create libcall to internal fn DIVMOD:
6139 : divmod_tmp = DIVMOD (op1, op2). */
6140 :
6141 12562 : gcall *call_stmt = gimple_build_call_internal (IFN_DIVMOD, 2, op1, op2);
6142 12562 : tree res = make_temp_ssa_name (build_complex_type (TREE_TYPE (op1)),
6143 : call_stmt, "divmod_tmp");
6144 12562 : gimple_call_set_lhs (call_stmt, res);
6145 : /* We rejected throwing statements above. */
6146 12562 : gimple_call_set_nothrow (call_stmt, true);
6147 :
6148 : /* Insert the call before top_stmt. */
6149 12562 : gimple_stmt_iterator top_stmt_gsi = gsi_for_stmt (top_stmt);
6150 12562 : gsi_insert_before (&top_stmt_gsi, call_stmt, GSI_SAME_STMT);
6151 :
6152 12562 : widen_mul_stats.divmod_calls_inserted++;
6153 :
6154 : /* Update all statements in stmts vector:
6155 : lhs = op1 TRUNC_DIV_EXPR op2 -> lhs = REALPART_EXPR<divmod_tmp>
6156 : lhs = op1 TRUNC_MOD_EXPR op2 -> lhs = IMAGPART_EXPR<divmod_tmp>. */
6157 :
6158 79381 : for (unsigned i = 0; stmts.iterate (i, &use_stmt); ++i)
6159 : {
6160 25149 : tree new_rhs;
6161 :
6162 25149 : switch (gimple_assign_rhs_code (use_stmt))
6163 : {
6164 12572 : case TRUNC_DIV_EXPR:
6165 12572 : new_rhs = fold_build1 (REALPART_EXPR, TREE_TYPE (op1), res);
6166 12572 : break;
6167 :
6168 12577 : case TRUNC_MOD_EXPR:
6169 12577 : new_rhs = fold_build1 (IMAGPART_EXPR, TREE_TYPE (op1), res);
6170 12577 : break;
6171 :
6172 0 : default:
6173 0 : gcc_unreachable ();
6174 : }
6175 :
6176 25149 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
6177 25149 : gimple_assign_set_rhs_from_tree (&gsi, new_rhs);
6178 25149 : update_stmt (use_stmt);
6179 : }
6180 :
6181 : return true;
6182 41670 : }
6183 :
6184 : /* Process a single gimple assignment STMT, which has a RSHIFT_EXPR as
6185 : its rhs, and try to convert it into a MULT_HIGHPART_EXPR. The return
6186 : value is true iff we converted the statement. */
6187 :
6188 : static bool
6189 178721 : convert_mult_to_highpart (gassign *stmt, gimple_stmt_iterator *gsi)
6190 : {
6191 178721 : tree lhs = gimple_assign_lhs (stmt);
6192 178721 : tree stype = TREE_TYPE (lhs);
6193 178721 : tree sarg0 = gimple_assign_rhs1 (stmt);
6194 178721 : tree sarg1 = gimple_assign_rhs2 (stmt);
6195 :
6196 178721 : if (TREE_CODE (stype) != INTEGER_TYPE
6197 170680 : || TREE_CODE (sarg1) != INTEGER_CST
6198 153391 : || TREE_CODE (sarg0) != SSA_NAME
6199 153390 : || !tree_fits_uhwi_p (sarg1)
6200 332111 : || !has_single_use (sarg0))
6201 : return false;
6202 :
6203 49416 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (sarg0));
6204 46183 : if (!def)
6205 : return false;
6206 :
6207 46183 : enum tree_code mcode = gimple_assign_rhs_code (def);
6208 46183 : if (mcode == NOP_EXPR)
6209 : {
6210 11143 : tree tmp = gimple_assign_rhs1 (def);
6211 11143 : if (TREE_CODE (tmp) != SSA_NAME || !has_single_use (tmp))
6212 : return false;
6213 3715 : def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (tmp));
6214 3427 : if (!def)
6215 : return false;
6216 3427 : mcode = gimple_assign_rhs_code (def);
6217 : }
6218 :
6219 38467 : if (mcode != WIDEN_MULT_EXPR
6220 38467 : || gimple_bb (def) != gimple_bb (stmt))
6221 : return false;
6222 2601 : tree mtype = TREE_TYPE (gimple_assign_lhs (def));
6223 2601 : if (TREE_CODE (mtype) != INTEGER_TYPE
6224 2601 : || TYPE_PRECISION (mtype) != TYPE_PRECISION (stype))
6225 : return false;
6226 :
6227 2601 : tree mop1 = gimple_assign_rhs1 (def);
6228 2601 : tree mop2 = gimple_assign_rhs2 (def);
6229 2601 : tree optype = TREE_TYPE (mop1);
6230 2601 : bool unsignedp = TYPE_UNSIGNED (optype);
6231 2601 : unsigned int prec = TYPE_PRECISION (optype);
6232 :
6233 2601 : if (unsignedp != TYPE_UNSIGNED (mtype)
6234 2601 : || TYPE_PRECISION (mtype) != 2 * prec)
6235 : return false;
6236 :
6237 2601 : unsigned HOST_WIDE_INT bits = tree_to_uhwi (sarg1);
6238 2601 : if (bits < prec || bits >= 2 * prec)
6239 : return false;
6240 :
6241 : /* For the time being, require operands to have the same sign. */
6242 2594 : if (unsignedp != TYPE_UNSIGNED (TREE_TYPE (mop2)))
6243 : return false;
6244 :
6245 2594 : machine_mode mode = TYPE_MODE (optype);
6246 2594 : optab tab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
6247 2594 : if (optab_handler (tab, mode) == CODE_FOR_nothing)
6248 : return false;
6249 :
6250 2594 : location_t loc = gimple_location (stmt);
6251 2594 : tree highpart1 = build_and_insert_binop (gsi, loc, "highparttmp",
6252 : MULT_HIGHPART_EXPR, mop1, mop2);
6253 2594 : tree highpart2 = highpart1;
6254 2594 : tree ntype = optype;
6255 :
6256 2594 : if (TYPE_UNSIGNED (stype) != TYPE_UNSIGNED (optype))
6257 : {
6258 16 : ntype = TYPE_UNSIGNED (stype) ? unsigned_type_for (optype)
6259 7 : : signed_type_for (optype);
6260 16 : highpart2 = build_and_insert_cast (gsi, loc, ntype, highpart1);
6261 : }
6262 2594 : if (bits > prec)
6263 29 : highpart2 = build_and_insert_binop (gsi, loc, "highparttmp",
6264 : RSHIFT_EXPR, highpart2,
6265 29 : build_int_cst (ntype, bits - prec));
6266 :
6267 2594 : gassign *new_stmt = gimple_build_assign (lhs, NOP_EXPR, highpart2);
6268 2594 : gsi_replace (gsi, new_stmt, true);
6269 :
6270 2594 : widen_mul_stats.highpart_mults_inserted++;
6271 2594 : return true;
6272 : }
6273 :
6274 : /* If target has spaceship<MODE>3 expander, pattern recognize
6275 : <bb 2> [local count: 1073741824]:
6276 : if (a_2(D) == b_3(D))
6277 : goto <bb 6>; [34.00%]
6278 : else
6279 : goto <bb 3>; [66.00%]
6280 :
6281 : <bb 3> [local count: 708669601]:
6282 : if (a_2(D) < b_3(D))
6283 : goto <bb 6>; [1.04%]
6284 : else
6285 : goto <bb 4>; [98.96%]
6286 :
6287 : <bb 4> [local count: 701299439]:
6288 : if (a_2(D) > b_3(D))
6289 : goto <bb 5>; [48.89%]
6290 : else
6291 : goto <bb 6>; [51.11%]
6292 :
6293 : <bb 5> [local count: 342865295]:
6294 :
6295 : <bb 6> [local count: 1073741824]:
6296 : and turn it into:
6297 : <bb 2> [local count: 1073741824]:
6298 : _1 = .SPACESHIP (a_2(D), b_3(D), 0);
6299 : if (_1 == 0)
6300 : goto <bb 6>; [34.00%]
6301 : else
6302 : goto <bb 3>; [66.00%]
6303 :
6304 : <bb 3> [local count: 708669601]:
6305 : if (_1 == -1)
6306 : goto <bb 6>; [1.04%]
6307 : else
6308 : goto <bb 4>; [98.96%]
6309 :
6310 : <bb 4> [local count: 701299439]:
6311 : if (_1 == 1)
6312 : goto <bb 5>; [48.89%]
6313 : else
6314 : goto <bb 6>; [51.11%]
6315 :
6316 : <bb 5> [local count: 342865295]:
6317 :
6318 : <bb 6> [local count: 1073741824]:
6319 : so that the backend can emit optimal comparison and
6320 : conditional jump sequence. If the
6321 : <bb 6> [local count: 1073741824]:
6322 : above has a single PHI like:
6323 : # _27 = PHI<0(2), -1(3), -128(4), 1(5)>
6324 : then replace it with effectively
6325 : _1 = .SPACESHIP (a_2(D), b_3(D), -128);
6326 : _27 = _1; */
6327 :
6328 : static void
6329 4248801 : optimize_spaceship (gcond *stmt)
6330 : {
6331 4248801 : enum tree_code code = gimple_cond_code (stmt);
6332 4248801 : if (code != EQ_EXPR && code != NE_EXPR)
6333 4248679 : return;
6334 3450626 : tree arg1 = gimple_cond_lhs (stmt);
6335 3450626 : tree arg2 = gimple_cond_rhs (stmt);
6336 3450626 : if ((!SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1))
6337 3338913 : && !INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6338 2684141 : || optab_handler (spaceship_optab,
6339 2684141 : TYPE_MODE (TREE_TYPE (arg1))) == CODE_FOR_nothing
6340 6121385 : || operand_equal_p (arg1, arg2, 0))
6341 : return;
6342 :
6343 2669548 : basic_block bb0 = gimple_bb (stmt), bb1, bb2 = NULL;
6344 2669548 : edge em1 = NULL, e1 = NULL, e2 = NULL;
6345 2669548 : bb1 = EDGE_SUCC (bb0, 1)->dest;
6346 2669548 : if (((EDGE_SUCC (bb0, 0)->flags & EDGE_TRUE_VALUE) != 0) ^ (code == EQ_EXPR))
6347 1581111 : bb1 = EDGE_SUCC (bb0, 0)->dest;
6348 :
6349 9545839 : gcond *g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb1));
6350 1160836 : if (g == NULL
6351 4863238 : || !single_pred_p (bb1)
6352 733943 : || (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
6353 614559 : ? !operand_equal_p (gimple_cond_rhs (g), arg2, 0)
6354 495175 : : (!operand_equal_p (gimple_cond_lhs (g), arg2, 0)
6355 961 : || !operand_equal_p (gimple_cond_rhs (g), arg1, 0)))
6356 627150 : || !cond_only_block_p (bb1))
6357 : return;
6358 :
6359 11786 : enum tree_code ccode = (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
6360 11786 : ? LT_EXPR : GT_EXPR);
6361 11786 : switch (gimple_cond_code (g))
6362 : {
6363 : case LT_EXPR:
6364 : case LE_EXPR:
6365 : break;
6366 10076 : case GT_EXPR:
6367 10076 : case GE_EXPR:
6368 10076 : ccode = ccode == LT_EXPR ? GT_EXPR : LT_EXPR;
6369 : break;
6370 : default:
6371 : return;
6372 : }
6373 :
6374 35286 : for (int i = 0; i < 2; ++i)
6375 : {
6376 : /* With NaNs, </<=/>/>= are false, so we need to look for the
6377 : third comparison on the false edge from whatever non-equality
6378 : comparison the second comparison is. */
6379 23566 : if (HONOR_NANS (TREE_TYPE (arg1))
6380 23566 : && (EDGE_SUCC (bb1, i)->flags & EDGE_TRUE_VALUE) != 0)
6381 129 : continue;
6382 :
6383 23437 : bb2 = EDGE_SUCC (bb1, i)->dest;
6384 69900 : g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb2));
6385 15711 : if (g == NULL
6386 15711 : || !single_pred_p (bb2)
6387 19862 : || (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
6388 11724 : ? !operand_equal_p (gimple_cond_rhs (g), arg2, 0)
6389 3586 : : (!operand_equal_p (gimple_cond_lhs (g), arg2, 0)
6390 23 : || !operand_equal_p (gimple_cond_rhs (g), arg1, 0)))
6391 66 : || !cond_only_block_p (bb2)
6392 11790 : || EDGE_SUCC (bb2, 0)->dest == EDGE_SUCC (bb2, 1)->dest)
6393 23371 : continue;
6394 :
6395 66 : enum tree_code ccode2
6396 66 : = (operand_equal_p (gimple_cond_lhs (g), arg1, 0) ? LT_EXPR : GT_EXPR);
6397 66 : switch (gimple_cond_code (g))
6398 : {
6399 : case LT_EXPR:
6400 : case LE_EXPR:
6401 : break;
6402 41 : case GT_EXPR:
6403 41 : case GE_EXPR:
6404 41 : ccode2 = ccode2 == LT_EXPR ? GT_EXPR : LT_EXPR;
6405 : break;
6406 0 : default:
6407 0 : continue;
6408 : }
6409 66 : if (HONOR_NANS (TREE_TYPE (arg1)) && ccode == ccode2)
6410 0 : continue;
6411 :
6412 132 : if ((ccode == LT_EXPR)
6413 66 : ^ ((EDGE_SUCC (bb1, i)->flags & EDGE_TRUE_VALUE) != 0))
6414 : {
6415 41 : em1 = EDGE_SUCC (bb1, 1 - i);
6416 41 : e1 = EDGE_SUCC (bb2, 0);
6417 41 : e2 = EDGE_SUCC (bb2, 1);
6418 41 : if ((ccode2 == LT_EXPR) ^ ((e1->flags & EDGE_TRUE_VALUE) == 0))
6419 0 : std::swap (e1, e2);
6420 : }
6421 : else
6422 : {
6423 25 : e1 = EDGE_SUCC (bb1, 1 - i);
6424 25 : em1 = EDGE_SUCC (bb2, 0);
6425 25 : e2 = EDGE_SUCC (bb2, 1);
6426 25 : if ((ccode2 != LT_EXPR) ^ ((em1->flags & EDGE_TRUE_VALUE) == 0))
6427 : std::swap (em1, e2);
6428 : }
6429 : break;
6430 : }
6431 :
6432 11761 : if (em1 == NULL)
6433 : {
6434 23440 : if ((ccode == LT_EXPR)
6435 11720 : ^ ((EDGE_SUCC (bb1, 0)->flags & EDGE_TRUE_VALUE) != 0))
6436 : {
6437 4778 : em1 = EDGE_SUCC (bb1, 1);
6438 4778 : e1 = EDGE_SUCC (bb1, 0);
6439 4778 : e2 = (e1->flags & EDGE_TRUE_VALUE) ? em1 : e1;
6440 : }
6441 : else
6442 : {
6443 6942 : em1 = EDGE_SUCC (bb1, 0);
6444 6942 : e1 = EDGE_SUCC (bb1, 1);
6445 6942 : e2 = (e1->flags & EDGE_TRUE_VALUE) ? em1 : e1;
6446 : }
6447 : }
6448 :
6449 : /* Check if there is a single bb into which all failed conditions
6450 : jump to (perhaps through an empty block) and if it results in
6451 : a single integral PHI which just sets it to -1, 0, 1, X
6452 : (or -1, 0, 1 when NaNs can't happen). In that case use 1 rather
6453 : than 0 as last .SPACESHIP argument to tell backends it might
6454 : consider different code generation and just cast the result
6455 : of .SPACESHIP to the PHI result. X above is some value
6456 : other than -1, 0, 1, for libstdc++ -128, for libc++ -127. */
6457 11786 : tree arg3 = integer_zero_node;
6458 11786 : edge e = EDGE_SUCC (bb0, 0);
6459 11786 : if (e->dest == bb1)
6460 9088 : e = EDGE_SUCC (bb0, 1);
6461 11786 : basic_block bbp = e->dest;
6462 11786 : gphi *phi = NULL;
6463 11786 : for (gphi_iterator psi = gsi_start_phis (bbp);
6464 14014 : !gsi_end_p (psi); gsi_next (&psi))
6465 : {
6466 3752 : gphi *gp = psi.phi ();
6467 3752 : tree res = gimple_phi_result (gp);
6468 :
6469 3752 : if (phi != NULL
6470 3406 : || virtual_operand_p (res)
6471 2531 : || !INTEGRAL_TYPE_P (TREE_TYPE (res))
6472 6112 : || TYPE_PRECISION (TREE_TYPE (res)) < 2)
6473 : {
6474 : phi = NULL;
6475 : break;
6476 : }
6477 2228 : phi = gp;
6478 : }
6479 11786 : if (phi
6480 1882 : && integer_zerop (gimple_phi_arg_def_from_edge (phi, e))
6481 12407 : && EDGE_COUNT (bbp->preds) == (HONOR_NANS (TREE_TYPE (arg1)) ? 4 : 3))
6482 : {
6483 129 : HOST_WIDE_INT argval
6484 129 : = SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1)) ? -128 : -1;
6485 750 : for (unsigned i = 0; phi && i < EDGE_COUNT (bbp->preds) - 1; ++i)
6486 : {
6487 269 : edge e3 = i == 0 ? e1 : i == 1 ? em1 : e2;
6488 269 : if (e3->dest != bbp)
6489 : {
6490 124 : if (!empty_block_p (e3->dest)
6491 115 : || !single_succ_p (e3->dest)
6492 239 : || single_succ (e3->dest) != bbp)
6493 : {
6494 : phi = NULL;
6495 : break;
6496 : }
6497 : e3 = single_succ_edge (e3->dest);
6498 : }
6499 260 : tree a = gimple_phi_arg_def_from_edge (phi, e3);
6500 260 : if (TREE_CODE (a) != INTEGER_CST
6501 260 : || (i == 0 && !integer_onep (a))
6502 510 : || (i == 1 && !integer_all_onesp (a)))
6503 : {
6504 : phi = NULL;
6505 : break;
6506 : }
6507 250 : if (i == 2)
6508 : {
6509 30 : tree minv = TYPE_MIN_VALUE (signed_char_type_node);
6510 30 : tree maxv = TYPE_MAX_VALUE (signed_char_type_node);
6511 30 : widest_int w = widest_int::from (wi::to_wide (a), SIGNED);
6512 41 : if ((w >= -1 && w <= 1)
6513 26 : || w < wi::to_widest (minv)
6514 60 : || w > wi::to_widest (maxv))
6515 : {
6516 4 : phi = NULL;
6517 4 : break;
6518 : }
6519 26 : argval = w.to_shwi ();
6520 26 : }
6521 : }
6522 129 : if (phi)
6523 106 : arg3 = build_int_cst (integer_type_node,
6524 130 : TYPE_UNSIGNED (TREE_TYPE (arg1)) ? 1 : argval);
6525 : }
6526 :
6527 : /* For integral <=> comparisons only use .SPACESHIP if it is turned
6528 : into an integer (-1, 0, 1). */
6529 11786 : if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1)) && arg3 == integer_zero_node)
6530 : return;
6531 :
6532 228 : gcall *gc = gimple_build_call_internal (IFN_SPACESHIP, 3, arg1, arg2, arg3);
6533 228 : tree lhs = make_ssa_name (integer_type_node);
6534 228 : gimple_call_set_lhs (gc, lhs);
6535 228 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
6536 228 : gsi_insert_before (&gsi, gc, GSI_SAME_STMT);
6537 :
6538 350 : wide_int wmin = wi::minus_one (TYPE_PRECISION (integer_type_node));
6539 350 : wide_int wmax = wi::one (TYPE_PRECISION (integer_type_node));
6540 228 : if (HONOR_NANS (TREE_TYPE (arg1)))
6541 : {
6542 129 : if (arg3 == integer_zero_node)
6543 103 : wmin = wi::shwi (-128, TYPE_PRECISION (integer_type_node));
6544 26 : else if (tree_int_cst_sgn (arg3) < 0)
6545 19 : wmin = wi::to_wide (arg3);
6546 : else
6547 7 : wmax = wi::to_wide (arg3);
6548 : }
6549 350 : int_range<1> vr (TREE_TYPE (lhs), wmin, wmax);
6550 228 : set_range_info (lhs, vr);
6551 :
6552 228 : if (arg3 != integer_zero_node)
6553 : {
6554 106 : tree type = TREE_TYPE (gimple_phi_result (phi));
6555 106 : if (!useless_type_conversion_p (type, integer_type_node))
6556 : {
6557 64 : tree tem = make_ssa_name (type);
6558 64 : gimple *gcv = gimple_build_assign (tem, NOP_EXPR, lhs);
6559 64 : gsi_insert_before (&gsi, gcv, GSI_SAME_STMT);
6560 64 : lhs = tem;
6561 : }
6562 106 : SET_PHI_ARG_DEF_ON_EDGE (phi, e, lhs);
6563 106 : gimple_cond_set_lhs (stmt, boolean_false_node);
6564 106 : gimple_cond_set_rhs (stmt, boolean_false_node);
6565 195 : gimple_cond_set_code (stmt, (e->flags & EDGE_TRUE_VALUE)
6566 : ? EQ_EXPR : NE_EXPR);
6567 106 : update_stmt (stmt);
6568 106 : return;
6569 : }
6570 :
6571 122 : gimple_cond_set_lhs (stmt, lhs);
6572 122 : gimple_cond_set_rhs (stmt, integer_zero_node);
6573 122 : update_stmt (stmt);
6574 :
6575 244 : gcond *cond = as_a <gcond *> (*gsi_last_bb (bb1));
6576 122 : gimple_cond_set_lhs (cond, lhs);
6577 122 : if (em1->src == bb1 && e2 != em1)
6578 : {
6579 67 : gimple_cond_set_rhs (cond, integer_minus_one_node);
6580 73 : gimple_cond_set_code (cond, (em1->flags & EDGE_TRUE_VALUE)
6581 : ? EQ_EXPR : NE_EXPR);
6582 : }
6583 : else
6584 : {
6585 55 : gcc_assert (e1->src == bb1 && e2 != e1);
6586 55 : gimple_cond_set_rhs (cond, integer_one_node);
6587 55 : gimple_cond_set_code (cond, (e1->flags & EDGE_TRUE_VALUE)
6588 : ? EQ_EXPR : NE_EXPR);
6589 : }
6590 122 : update_stmt (cond);
6591 :
6592 122 : if (e2 != e1 && e2 != em1)
6593 : {
6594 80 : cond = as_a <gcond *> (*gsi_last_bb (bb2));
6595 40 : gimple_cond_set_lhs (cond, lhs);
6596 40 : if (em1->src == bb2)
6597 25 : gimple_cond_set_rhs (cond, integer_minus_one_node);
6598 : else
6599 : {
6600 15 : gcc_assert (e1->src == bb2);
6601 15 : gimple_cond_set_rhs (cond, integer_one_node);
6602 : }
6603 40 : gimple_cond_set_code (cond,
6604 40 : (e2->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR);
6605 40 : update_stmt (cond);
6606 : }
6607 : }
6608 :
6609 :
6610 : /* Long-multiply inverse-lowering helper.
6611 :
6612 : The forwprop long-multiply recognizer canonicalizes a hand-written
6613 : longhand high-part multiply into a cast+mult+shift+cast chain
6614 : `(N) ((2N) a * (2N) b) >> N'. When the target lacks an expansion
6615 : path for the wide form, `lower_long_mul_high_chain' resynthesizes
6616 : the longhand at narrow precision via `build_long_mul_partials'. */
6617 :
6618 : /* Test whether the target supports an (HALF)-by-(HALF)->NARROW unsigned
6619 : widening multiply. Returns true on success, with the half-width
6620 : scalar int mode placed in *HALF_MODE. */
6621 :
6622 : static bool
6623 2230 : can_widen_to_narrow_p (scalar_int_mode narrow_mode, unsigned int half_width,
6624 : scalar_int_mode *half_mode)
6625 : {
6626 2230 : if (!int_mode_for_size (half_width, 0).exists (half_mode))
6627 0 : return false;
6628 2230 : return convert_optab_handler (umul_widen_optab, narrow_mode, *half_mode)
6629 2230 : != CODE_FOR_nothing;
6630 : }
6631 :
6632 : static bool long_mul_op_fits_p (tree, unsigned, bitmap);
6633 :
6634 : /* Build into *SEQ the (N/2)-bit halves *LO and *HI of OP, in the form
6635 : USE_WIDEN selects. An operand provably within N/2 bits has a zero
6636 : high half: *HI is left null. */
6637 :
6638 : static void
6639 4460 : build_long_mul_partial_operand (gimple_seq *seq, location_t loc, tree op,
6640 : tree half_type, tree half_amt, bool use_widen,
6641 : tree *lo, tree *hi)
6642 : {
6643 4460 : tree acc_type = TREE_TYPE (op);
6644 4460 : unsigned int half_prec = TYPE_PRECISION (half_type);
6645 4460 : auto_bitmap phi_seen;
6646 4460 : bool fits = long_mul_op_fits_p (op, half_prec, phi_seen);
6647 :
6648 4460 : *hi = NULL_TREE;
6649 4460 : if (!fits)
6650 4262 : *hi = gimple_build (seq, loc, RSHIFT_EXPR, acc_type, op, half_amt);
6651 :
6652 4460 : if (use_widen)
6653 : {
6654 4460 : *lo = gimple_build (seq, loc, NOP_EXPR, half_type, op);
6655 4460 : if (*hi)
6656 4262 : *hi = gimple_build (seq, loc, NOP_EXPR, half_type, *hi);
6657 : }
6658 0 : else if (fits)
6659 0 : *lo = op;
6660 : else
6661 : {
6662 0 : tree mask = wide_int_to_tree (acc_type,
6663 0 : wi::mask (half_prec, false,
6664 0 : TYPE_PRECISION (acc_type)));
6665 0 : *lo = gimple_build (seq, loc, BIT_AND_EXPR, acc_type, op, mask);
6666 : }
6667 4460 : }
6668 :
6669 : /* Append to *SEQ the operand split and partial products for an unsigned
6670 : long multiply of OP1 by OP2 at the precision of TREE_TYPE (OP1).
6671 : HALF_TYPE is the (N/2)-bit unsigned type; HALF_AMT is the integer-typed
6672 : shift constant equal to N/2.
6673 :
6674 : Outputs the four partial products via *LOLO, *HILO, *LOHI, *HIHI.
6675 : An operand provably within N/2 bits contributes literal-zero high
6676 : partials.
6677 :
6678 : USE_WIDEN selects the partial-product form:
6679 : true - cast halves to HALF_TYPE and use WIDEN_MULT_EXPR (needs
6680 : an (N/2)-by-(N/2)->N widening multiply optab).
6681 : false - mask/shift halves within the N-bit accumulator and use
6682 : plain MULT_EXPR; the halves fit in N/2 bits so the N-bit
6683 : low product is exact. */
6684 :
6685 : static void
6686 2230 : build_long_mul_partials (gimple_seq *seq, location_t loc, tree op1, tree op2,
6687 : tree half_type, tree half_amt,
6688 : tree *lolo, tree *hilo, tree *lohi, tree *hihi,
6689 : bool use_widen)
6690 : {
6691 2230 : tree acc_type = TREE_TYPE (op1);
6692 2230 : tree zero = build_zero_cst (acc_type);
6693 2230 : tree_code mul_code = use_widen ? WIDEN_MULT_EXPR : MULT_EXPR;
6694 2230 : tree op1_lo, op1_hi, op2_lo, op2_hi;
6695 :
6696 2230 : build_long_mul_partial_operand (seq, loc, op1, half_type, half_amt, use_widen,
6697 : &op1_lo, &op1_hi);
6698 2230 : build_long_mul_partial_operand (seq, loc, op2, half_type, half_amt, use_widen,
6699 : &op2_lo, &op2_hi);
6700 :
6701 2230 : *lolo = gimple_build (seq, loc, mul_code, acc_type, op1_lo, op2_lo);
6702 4460 : *hilo = op1_hi
6703 2230 : ? gimple_build (seq, loc, mul_code, acc_type, op1_hi, op2_lo) : zero;
6704 4460 : *lohi = op2_hi
6705 2230 : ? gimple_build (seq, loc, mul_code, acc_type, op1_lo, op2_hi) : zero;
6706 2221 : *hihi = op1_hi && op2_hi
6707 4262 : ? gimple_build (seq, loc, mul_code, acc_type, op1_hi, op2_hi) : zero;
6708 2230 : }
6709 :
6710 : /* Emit into *SEQ the high N bits of the unsigned product A * B, where A and B
6711 : are NARROW_TYPE (N-bit) values, as a longhand over (N/2)-bit partials.
6712 : Returns the high-part SSA. */
6713 :
6714 : static tree
6715 2230 : emit_long_mul_highpart (gimple_seq *seq, location_t loc, tree a, tree b,
6716 : tree narrow_type)
6717 : {
6718 2230 : scalar_int_mode narrow_mode
6719 2230 : = as_a <scalar_int_mode> (TYPE_MODE (narrow_type));
6720 2230 : unsigned int half_width = GET_MODE_PRECISION (narrow_mode) / 2;
6721 : /* Prefer (N/2)-by-(N/2)->N widening partials; fall back to plain MULT_EXPR
6722 : when the target lacks the widen optab. See build_long_mul_partials. */
6723 2230 : scalar_int_mode half_mode;
6724 2230 : bool use_widen = can_widen_to_narrow_p (narrow_mode, half_width, &half_mode);
6725 2230 : tree half_type = build_nonstandard_integer_type (half_width, 1);
6726 2230 : tree half_amt = build_int_cst (integer_type_node, half_width);
6727 2230 : tree half_mask = wide_int_to_tree (narrow_type,
6728 2230 : wi::mask (half_width, false,
6729 2230 : TYPE_PRECISION (narrow_type)));
6730 :
6731 2230 : tree lolo, hilo, lohi, hihi;
6732 2230 : build_long_mul_partials (seq, loc, a, b, half_type, half_amt,
6733 : &lolo, &hilo, &lohi, &hihi, use_widen);
6734 2230 : tree cross_sum = gimple_build (seq, loc, PLUS_EXPR, narrow_type, hilo, lohi);
6735 2230 : tree cross_lt = gimple_build (seq, loc, LT_EXPR, boolean_type_node,
6736 : cross_sum, hilo);
6737 2230 : tree cross_lt_n = gimple_build (seq, loc, NOP_EXPR, narrow_type, cross_lt);
6738 2230 : tree cross_carry = gimple_build (seq, loc, LSHIFT_EXPR, narrow_type,
6739 : cross_lt_n, half_amt);
6740 2230 : tree lolo_hi = gimple_build (seq, loc, RSHIFT_EXPR, narrow_type,
6741 : lolo, half_amt);
6742 2230 : tree cross_lo = gimple_build (seq, loc, BIT_AND_EXPR, narrow_type,
6743 : cross_sum, half_mask);
6744 2230 : tree low_accum = gimple_build (seq, loc, PLUS_EXPR, narrow_type,
6745 : lolo_hi, cross_lo);
6746 2230 : tree low_accum_hi = gimple_build (seq, loc, RSHIFT_EXPR, narrow_type,
6747 : low_accum, half_amt);
6748 2230 : tree cross_hi = gimple_build (seq, loc, RSHIFT_EXPR, narrow_type,
6749 : cross_sum, half_amt);
6750 2230 : tree t1 = gimple_build (seq, loc, PLUS_EXPR, narrow_type, hihi, cross_hi);
6751 2230 : tree t2 = gimple_build (seq, loc, PLUS_EXPR, narrow_type, t1, low_accum_hi);
6752 2230 : return gimple_build (seq, loc, PLUS_EXPR, narrow_type, t2, cross_carry);
6753 : }
6754 :
6755 : /* Emit into *SEQ the high N bits (NARROW_TYPE) of the unsigned product of two
6756 : 2N-bit values given as N-bit halves, x = L1 + H1*2^N and y = L2 + H2*2^N:
6757 : the high half of x*y is the high N bits of L1*L2, plus H1*L2 and L1*H2, all
6758 : mod 2^N. */
6759 :
6760 : static tree
6761 2230 : combine_long_mul_halves (gimple_seq *seq, location_t loc, tree l1, tree h1,
6762 : tree l2, tree h2, tree narrow_type)
6763 : {
6764 2230 : tree hh = emit_long_mul_highpart (seq, loc, l1, l2, narrow_type);
6765 2230 : tree c1 = gimple_build (seq, loc, MULT_EXPR, narrow_type, h1, l2);
6766 2230 : tree c2 = gimple_build (seq, loc, MULT_EXPR, narrow_type, l1, h2);
6767 2230 : tree s = gimple_build (seq, loc, PLUS_EXPR, narrow_type, hh, c1);
6768 2230 : return gimple_build (seq, loc, PLUS_EXPR, narrow_type, s, c2);
6769 : }
6770 :
6771 : /* True when OP fits NARROW_PREC bits as an unsigned value. Looks
6772 : through widening casts and PHIs, falling back to `tree_nonzero_bits'
6773 : otherwise. PHI_SEEN guards against cycles. */
6774 :
6775 : static bool
6776 6046 : long_mul_op_fits_p (tree op, unsigned narrow_prec, bitmap phi_seen)
6777 : {
6778 6401 : if (!TYPE_UNSIGNED (TREE_TYPE (op)))
6779 : return false;
6780 6386 : if (TYPE_PRECISION (TREE_TYPE (op)) <= narrow_prec)
6781 : return true;
6782 6135 : if (TREE_CODE (op) == SSA_NAME)
6783 : {
6784 4951 : gassign *def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op));
6785 4109 : if (def
6786 4109 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
6787 355 : return long_mul_op_fits_p (gimple_assign_rhs1 (def), narrow_prec,
6788 355 : phi_seen);
6789 4596 : if (gphi *phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (op)))
6790 696 : if (bitmap_set_bit (phi_seen, SSA_NAME_VERSION (op)))
6791 : {
6792 1061 : for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
6793 1006 : if (!long_mul_op_fits_p (gimple_phi_arg_def (phi, i),
6794 : narrow_prec, phi_seen))
6795 : return false;
6796 : return true;
6797 : }
6798 : }
6799 5084 : return wi::min_precision (tree_nonzero_bits (op), UNSIGNED) <= narrow_prec;
6800 : }
6801 :
6802 : static bool long_mul_split_operand (gimple_seq *, location_t, tree, tree,
6803 : tree *, tree *);
6804 :
6805 : struct long_mul_halves
6806 : {
6807 : tree lo;
6808 : tree hi;
6809 : };
6810 :
6811 : /* Halves recorded for one run of the pass, keyed on the PHI they came from.
6812 : Several chains can reach one operand PHI, and each that splits it again
6813 : leaves another redundant pair of half PHIs behind. */
6814 :
6815 : static hash_map<tree, long_mul_halves> *long_mul_phi_halves;
6816 :
6817 : struct long_mul_arg_split
6818 : {
6819 : gimple_seq seq;
6820 : tree lo;
6821 : tree hi;
6822 : };
6823 :
6824 : /* Split the 2N-bit result of PHI into N-bit halves *LO and *HI, by splitting
6825 : each argument and merging the halves with two new PHIs. A split goes at
6826 : the end of its argument's incoming block, where the argument is available,
6827 : rather than on the edge, which could split a critical edge while the
6828 : dominator walk is still running. Returns false, having changed nothing,
6829 : when an argument cannot be split. A split that succeeds stands even if
6830 : the caller then gives up. */
6831 :
6832 : static bool
6833 0 : long_mul_split_phi (gphi *phi, tree narrow_type, tree *lo, tree *hi)
6834 : {
6835 0 : tree res = gimple_phi_result (phi);
6836 0 : if (long_mul_halves *prev = long_mul_phi_halves->get (res))
6837 : {
6838 : /* Every operand reaching a split has the 2N type, so the width is
6839 : fixed by the PHI's own type. */
6840 0 : gcc_checking_assert (types_compatible_p (TREE_TYPE (prev->lo),
6841 : narrow_type));
6842 0 : *lo = prev->lo;
6843 0 : *hi = prev->hi;
6844 0 : return true;
6845 : }
6846 :
6847 0 : unsigned int n = gimple_phi_num_args (phi);
6848 0 : location_t loc = gimple_location (phi);
6849 0 : basic_block bb = gimple_bb (phi);
6850 0 : auto_vec<long_mul_arg_split, 4> args;
6851 :
6852 0 : for (unsigned int i = 0; i < n; i++)
6853 : {
6854 0 : edge e = gimple_phi_arg_edge (phi, i);
6855 : /* A back edge could lead back to PHI and recurse forever. The entry
6856 : block cannot hold a split. */
6857 0 : if (dominated_by_p (CDI_DOMINATORS, e->src, bb)
6858 0 : || e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
6859 0 : return false;
6860 :
6861 0 : long_mul_arg_split arg = {};
6862 0 : if (!long_mul_split_operand (&arg.seq, loc, gimple_phi_arg_def (phi, i),
6863 : narrow_type, &arg.lo, &arg.hi))
6864 : return false;
6865 :
6866 0 : args.safe_push (arg);
6867 : }
6868 :
6869 : /* Every argument split, so the rewrite can be committed. */
6870 0 : gphi *lo_phi = create_phi_node (make_ssa_name (narrow_type), bb);
6871 0 : gphi *hi_phi = create_phi_node (make_ssa_name (narrow_type), bb);
6872 0 : for (unsigned int i = 0; i < n; i++)
6873 : {
6874 0 : edge e = gimple_phi_arg_edge (phi, i);
6875 0 : if (args[i].seq)
6876 : {
6877 0 : gimple_stmt_iterator gsi = gsi_last_bb (e->src);
6878 0 : if (!gsi_end_p (gsi) && stmt_ends_bb_p (gsi_stmt (gsi)))
6879 0 : gsi_insert_seq_before (&gsi, args[i].seq, GSI_SAME_STMT);
6880 : else
6881 0 : gsi_insert_seq_after (&gsi, args[i].seq, GSI_CONTINUE_LINKING);
6882 : }
6883 0 : add_phi_arg (lo_phi, args[i].lo, e, UNKNOWN_LOCATION);
6884 0 : add_phi_arg (hi_phi, args[i].hi, e, UNKNOWN_LOCATION);
6885 : }
6886 0 : *lo = gimple_phi_result (lo_phi);
6887 0 : *hi = gimple_phi_result (hi_phi);
6888 0 : long_mul_phi_halves->put (res, { *lo, *hi });
6889 :
6890 0 : if (dump_file && (dump_flags & TDF_DETAILS))
6891 0 : fprintf (dump_file, "Split long-multiply operand PHI.\n");
6892 : return true;
6893 0 : }
6894 :
6895 : /* Split the 2N-bit unsigned value OP into its low and high N bits (*LO and
6896 : *HI, both NARROW_TYPE) using only N-bit operations, as the target has no 2N
6897 : multiply or shift. A 2N product recurses on its operands, its high half
6898 : coming from combine_long_mul_halves. A value shifted down by N recurses on
6899 : the shifted value and takes its high half, rather than reading the 2N shift.
6900 : A widening cast's low half is the truncated source and its high half is what
6901 : the cast extended with, zero or the source's replicated sign bit. A value
6902 : that provably fits N bits has a zero high half. A PHI is split through its
6903 : arguments as a last resort. Returns false otherwise. */
6904 :
6905 : static bool
6906 4468 : long_mul_split_operand (gimple_seq *seq, location_t loc, tree op,
6907 : tree narrow_type, tree *lo, tree *hi)
6908 : {
6909 4468 : unsigned int narrow_prec = TYPE_PRECISION (narrow_type);
6910 4468 : if (TREE_CODE (op) == SSA_NAME)
6911 : {
6912 3941 : auto *def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op));
6913 3906 : if (def && gimple_assign_rhs_code (def) == MULT_EXPR)
6914 : {
6915 13 : tree a_lo, a_hi, b_lo, b_hi;
6916 13 : if (!long_mul_split_operand (seq, loc, gimple_assign_rhs1 (def),
6917 : narrow_type, &a_lo, &a_hi)
6918 26 : || !long_mul_split_operand (seq, loc, gimple_assign_rhs2 (def),
6919 : narrow_type, &b_lo, &b_hi))
6920 : return false;
6921 13 : *lo = gimple_build (seq, loc, MULT_EXPR, narrow_type, a_lo, b_lo);
6922 13 : *hi = combine_long_mul_halves (seq, loc, a_lo, a_hi, b_lo, b_hi,
6923 : narrow_type);
6924 13 : return true;
6925 : }
6926 : /* A 2N value shifted down by N is its own high half: split the source
6927 : and use that half. Reading the shift instead leaves the 2N source
6928 : live, and the target cannot expand it. This has to come before the
6929 : widening-cast case below, which would take such a value as it
6930 : stands. */
6931 3928 : if (def
6932 3893 : && gimple_assign_rhs_code (def) == RSHIFT_EXPR
6933 8 : && tree_fits_uhwi_p (gimple_assign_rhs2 (def))
6934 3936 : && tree_to_uhwi (gimple_assign_rhs2 (def)) == narrow_prec)
6935 : {
6936 8 : tree src_lo, src_hi;
6937 8 : if (!long_mul_split_operand (seq, loc, gimple_assign_rhs1 (def),
6938 : narrow_type, &src_lo, &src_hi))
6939 : return false;
6940 8 : *lo = src_hi;
6941 8 : *hi = build_zero_cst (narrow_type);
6942 8 : return true;
6943 : }
6944 7805 : if (def && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
6945 : {
6946 3867 : tree src = gimple_assign_rhs1 (def);
6947 3867 : tree src_type = TREE_TYPE (src);
6948 3867 : if (INTEGRAL_TYPE_P (src_type)
6949 3867 : && TYPE_PRECISION (src_type) <= narrow_prec)
6950 : {
6951 3867 : *lo = gimple_convert (seq, loc, narrow_type, src);
6952 3867 : if (TYPE_UNSIGNED (src_type))
6953 3852 : *hi = build_zero_cst (narrow_type);
6954 : else
6955 : {
6956 : /* Sign extension: the high N bits replicate the sign bit. */
6957 15 : tree snarrow = signed_type_for (narrow_type);
6958 15 : tree s = gimple_convert (seq, loc, snarrow, *lo);
6959 15 : tree amt = build_int_cst (integer_type_node, narrow_prec - 1);
6960 15 : tree sh = gimple_build (seq, loc, RSHIFT_EXPR, snarrow, s,
6961 : amt);
6962 15 : *hi = gimple_convert (seq, loc, narrow_type, sh);
6963 : }
6964 3867 : return true;
6965 : }
6966 : }
6967 : }
6968 :
6969 : /* A value provably within N bits: its low half is the truncation to N bits
6970 : (a subreg, not a 2N shift), its high half is zero. */
6971 580 : auto_bitmap phi_seen;
6972 580 : if (long_mul_op_fits_p (op, narrow_prec, phi_seen))
6973 : {
6974 580 : *lo = gimple_convert (seq, loc, narrow_type, op);
6975 580 : *hi = build_zero_cst (narrow_type);
6976 580 : return true;
6977 : }
6978 :
6979 : /* A PHI that does not fit N bits can still be split through its arguments,
6980 : which is how a sign-extended value reaches the cast case above. */
6981 0 : if (TREE_CODE (op) == SSA_NAME)
6982 580 : if (gphi *phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (op)))
6983 0 : return long_mul_split_phi (phi, narrow_type, lo, hi);
6984 : return false;
6985 580 : }
6986 :
6987 : /* Collect into HIGH_USES the uses of PROD forming its high half,
6988 : `PROD >> NARROW_PREC'. A use reading only the low NARROW_PREC bits is
6989 : accepted but not collected. Returns false on any other use. */
6990 :
6991 : static bool
6992 2243 : long_mul_high_half_uses (tree prod, unsigned int narrow_prec,
6993 : vec<gassign *> *high_uses)
6994 : {
6995 2243 : imm_use_iterator iui;
6996 2243 : gimple *use_stmt;
6997 4484 : FOR_EACH_IMM_USE_STMT (use_stmt, iui, prod)
6998 : {
6999 2246 : if (is_gimple_debug (use_stmt))
7000 0 : continue;
7001 2246 : auto a_use = dyn_cast<gassign *> (use_stmt);
7002 2246 : if (!a_use)
7003 5 : return false;
7004 2246 : tree_code code = gimple_assign_rhs_code (a_use);
7005 2246 : if (code == RSHIFT_EXPR
7006 2225 : && gimple_assign_rhs1 (a_use) == prod
7007 4450 : && tree_fits_uhwi_p (gimple_assign_rhs2 (a_use))
7008 4471 : && tree_to_uhwi (gimple_assign_rhs2 (a_use)) == narrow_prec)
7009 2225 : high_uses->safe_push (a_use);
7010 21 : else if (CONVERT_EXPR_CODE_P (code))
7011 : {
7012 0 : tree t = TREE_TYPE (gimple_assign_lhs (a_use));
7013 0 : if (!INTEGRAL_TYPE_P (t) || TYPE_PRECISION (t) > narrow_prec)
7014 : return false;
7015 : }
7016 21 : else if (code == BIT_AND_EXPR
7017 37 : && TREE_CODE (gimple_assign_rhs2 (a_use)) == INTEGER_CST)
7018 : {
7019 16 : if (wi::min_precision (wi::to_wide (gimple_assign_rhs2 (a_use)),
7020 : UNSIGNED) > narrow_prec)
7021 : return false;
7022 : }
7023 : else
7024 : return false;
7025 5 : }
7026 2238 : return true;
7027 : }
7028 :
7029 : /* True when every use of PROD reads only its low NARROW_PREC bits. */
7030 :
7031 : static bool
7032 2235 : long_mul_only_low_half_used_p (tree prod, unsigned int narrow_prec)
7033 : {
7034 2235 : auto_vec<gassign *, 4> high_uses;
7035 2235 : return (long_mul_high_half_uses (prod, narrow_prec, &high_uses)
7036 4470 : && high_uses.is_empty ());
7037 2235 : }
7038 :
7039 : /* True when STMT is res = a * b whose unsigned 2N-bit result is in a mode the
7040 : target cannot multiply, having neither insn nor libcall, so that expand_mult
7041 : would abort; set *NARROW_TYPE to the N-bit unsigned type its halves are
7042 : built at. A mode the target does support is left to convert_mult_to_widen
7043 : and convert_mult_to_highpart. */
7044 :
7045 : static bool
7046 88007912 : unexpandable_long_mul_p (gimple *stmt, tree *narrow_type)
7047 : {
7048 88007912 : auto a_stmt = dyn_cast<gassign *> (stmt);
7049 36252630 : if (!a_stmt || gimple_assign_rhs_code (a_stmt) != MULT_EXPR)
7050 : return false;
7051 :
7052 1527292 : tree wide_type = TREE_TYPE (gimple_assign_lhs (a_stmt));
7053 1527292 : scalar_int_mode wide_mode;
7054 1527292 : if (!INTEGRAL_TYPE_P (wide_type)
7055 1265959 : || !TYPE_UNSIGNED (wide_type)
7056 932106 : || !is_a <scalar_int_mode> (TYPE_MODE (wide_type), &wide_mode)
7057 2793251 : || targetm.scalar_mode_supported_p (wide_mode))
7058 : return false;
7059 :
7060 2243 : *narrow_type
7061 2243 : = build_nonstandard_integer_type (TYPE_PRECISION (wide_type) / 2, 1);
7062 2243 : return true;
7063 : }
7064 :
7065 : static bool narrow_long_mul_low_half (gimple_stmt_iterator *);
7066 :
7067 : /* OP1 and OP2 are the operands of a 2N multiply just narrowed or lowered;
7068 : that rewrite now reads each through an N-bit low-half cast. An operand
7069 : defined by another 2N multiply can thereby become low-half-only -- narrow
7070 : it too, recursing through chained wide products such as (a*b)*c. */
7071 :
7072 : static void
7073 2214 : narrow_long_mul_operands (tree op1, tree op2)
7074 : {
7075 6642 : for (tree op : { op1, op2 })
7076 4428 : if (TREE_CODE (op) == SSA_NAME)
7077 : {
7078 3901 : gimple *def = SSA_NAME_DEF_STMT (op);
7079 3901 : if (is_gimple_assign (def) && gimple_assign_rhs_code (def) == MULT_EXPR)
7080 : {
7081 5 : gimple_stmt_iterator dgsi = gsi_for_stmt (def);
7082 5 : narrow_long_mul_low_half (&dgsi);
7083 : }
7084 : }
7085 2214 : }
7086 :
7087 : /* If the statement at *GSI is res = a * b with a 2N-bit unsigned result the
7088 : target cannot multiply and every use reads only the low N bits, narrow it
7089 : to res = (2N) ((N) a * (N) b) and return true. The low N bits of a product
7090 : depend only on the low N bits of the operands, so this preserves every use;
7091 : the unused high half becomes zero. match.pd's shorten rule omits this for
7092 : MULT_EXPR. */
7093 :
7094 : static bool
7095 778121 : narrow_long_mul_low_half (gimple_stmt_iterator *gsi)
7096 : {
7097 778121 : gimple *stmt = gsi_stmt (*gsi);
7098 778121 : tree narrow_type;
7099 778121 : if (!unexpandable_long_mul_p (stmt, &narrow_type))
7100 : return false;
7101 :
7102 2235 : tree lhs = gimple_assign_lhs (stmt);
7103 2235 : if (!long_mul_only_low_half_used_p (lhs, TYPE_PRECISION (narrow_type)))
7104 : return false;
7105 :
7106 13 : tree op1 = gimple_assign_rhs1 (stmt);
7107 13 : tree op2 = gimple_assign_rhs2 (stmt);
7108 13 : location_t loc = gimple_location (stmt);
7109 13 : gimple_seq seq = NULL;
7110 13 : tree a = gimple_convert (&seq, loc, narrow_type, op1);
7111 13 : tree b = gimple_convert (&seq, loc, narrow_type, op2);
7112 13 : tree np = gimple_build (&seq, loc, MULT_EXPR, narrow_type, a, b);
7113 13 : gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
7114 13 : gimple *conv = gimple_build_assign (lhs, NOP_EXPR, np);
7115 13 : gimple_set_location (conv, loc);
7116 13 : gsi_replace (gsi, conv, true);
7117 :
7118 13 : if (dump_file && (dump_flags & TDF_DETAILS))
7119 0 : fprintf (dump_file, "Narrowed low-half-only long multiply.\n");
7120 :
7121 13 : narrow_long_mul_operands (op1, op2);
7122 13 : return true;
7123 : }
7124 :
7125 : /* The 2N multiply at *GSI has had its high half synthesized elsewhere, so any
7126 : use left reads only its low half: narrow it in place, or remove it when it
7127 : has no use at all. Removing it drops a use of each operand, so a 2N
7128 : multiply defining one may become low-half-only. Narrow those operands. */
7129 :
7130 : static void
7131 2217 : finish_long_mul_low_half (gimple_stmt_iterator *gsi)
7132 : {
7133 2217 : gimple *stmt = gsi_stmt (*gsi);
7134 :
7135 2217 : if (has_zero_uses (gimple_assign_lhs (stmt)))
7136 : {
7137 2201 : tree op1 = gimple_assign_rhs1 (stmt);
7138 2201 : tree op2 = gimple_assign_rhs2 (stmt);
7139 2201 : gsi_remove (gsi, true);
7140 2201 : release_defs (stmt);
7141 2201 : narrow_long_mul_operands (op1, op2);
7142 2201 : return;
7143 : }
7144 :
7145 16 : narrow_long_mul_low_half (gsi);
7146 : }
7147 :
7148 : /* Rewrite the multiply STMT into the N-bit halves its uses read, when the
7149 : target can neither multiply at 2N bits nor form an N-bit high part.
7150 : Returns true on a rewrite, which may remove STMT.
7151 :
7152 : Runs once lower_long_mul_high_chain has been applied to every statement:
7153 : both rewrite products with a high half, and this one, keyed on the
7154 : definition rather than on a consumer, would otherwise pre-empt it. What
7155 : reaches it is a product that lowering could not retire, its high half also
7156 : read as an operand of another product, or read alongside the low half. A
7157 : product read only for its low half is left to narrow_long_mul_low_half. */
7158 :
7159 : static bool
7160 87229791 : narrow_long_mul_halves (gimple *stmt)
7161 : {
7162 87229791 : tree narrow_type;
7163 87229791 : if (!unexpandable_long_mul_p (stmt, &narrow_type))
7164 : return false;
7165 :
7166 : /* Only worth lowering where the target cannot form the N-bit high part. */
7167 8 : scalar_int_mode narrow_mode;
7168 8 : if (!is_a <scalar_int_mode> (TYPE_MODE (narrow_type), &narrow_mode)
7169 8 : || can_mult_highpart_p (narrow_mode, true))
7170 : return false;
7171 :
7172 8 : tree lhs = gimple_assign_lhs (stmt);
7173 8 : auto_vec<gassign *, 4> high_uses;
7174 8 : if (!long_mul_high_half_uses (lhs, TYPE_PRECISION (narrow_type), &high_uses)
7175 16 : || high_uses.is_empty ())
7176 : return false;
7177 :
7178 8 : tree op1 = gimple_assign_rhs1 (stmt);
7179 8 : tree op2 = gimple_assign_rhs2 (stmt);
7180 8 : location_t loc = gimple_location (stmt);
7181 8 : gimple_seq seq = NULL;
7182 8 : tree l1, h1, l2, h2;
7183 8 : if (!long_mul_split_operand (&seq, loc, op1, narrow_type, &l1, &h1)
7184 8 : || !long_mul_split_operand (&seq, loc, op2, narrow_type, &l2, &h2))
7185 : return false;
7186 8 : tree hi = combine_long_mul_halves (&seq, loc, l1, h1, l2, h2, narrow_type);
7187 : /* The high part is < 2^N, so widening it back to 2N leaves every use of a
7188 : shift, full width or truncated, reading the same value. */
7189 8 : tree hi_wide = gimple_convert (&seq, loc, TREE_TYPE (lhs), hi);
7190 8 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7191 8 : gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
7192 :
7193 8 : unsigned int i;
7194 8 : gassign *shift_stmt;
7195 24 : FOR_EACH_VEC_ELT (high_uses, i, shift_stmt)
7196 : {
7197 8 : gimple_stmt_iterator sgsi = gsi_for_stmt (shift_stmt);
7198 8 : gimple *conv = gimple_build_assign (gimple_assign_lhs (shift_stmt),
7199 : hi_wide);
7200 8 : gimple_set_location (conv, loc);
7201 8 : gsi_replace (&sgsi, conv, true);
7202 : }
7203 :
7204 8 : if (dump_file && (dump_flags & TDF_DETAILS))
7205 0 : fprintf (dump_file, "Narrowed high half of long multiply.\n");
7206 :
7207 8 : finish_long_mul_low_half (&gsi);
7208 8 : return true;
7209 8 : }
7210 :
7211 : /* Match.pd recognizer for the long-multiply recognizer's high-part
7212 : emit chain. */
7213 :
7214 : extern bool gimple_long_mul_high_chain (tree, tree *, tree (*)(tree));
7215 :
7216 : /* Rewrite the `long_mul_high_chain' whose tail is the statement at GSI
7217 :
7218 : wide_a = (T_2N) op1
7219 : wide_b = (T_2N) op2
7220 : wide_prod = wide_a * wide_b
7221 : hi = wide_prod >> N
7222 : lhs = (convert) hi
7223 :
7224 : to a longhand high-part synthesis at T_N precision. Never materializes
7225 : T_2N in gimple, so it covers cases where the 2N mode has no expansion path
7226 : (e.g. the high 128 bits of a 128x128 product where 2N=OImode). An operand
7227 : wider than T_N -- a shared wide product or a sign-extended cast -- is split
7228 : into T_N halves rather than truncated, so no high input bits are dropped.
7229 : Returns true on a rewrite. */
7230 :
7231 : static bool
7232 2498313 : lower_long_mul_high_chain (gimple_stmt_iterator *gsi)
7233 : {
7234 2498313 : gassign *trunc_stmt = dyn_cast<gassign *> (gsi_stmt (*gsi));
7235 2498313 : if (!trunc_stmt)
7236 : return false;
7237 :
7238 2498313 : tree narrow_lhs = gimple_assign_lhs (trunc_stmt);
7239 2498313 : tree ops[2];
7240 2498313 : if (!gimple_long_mul_high_chain (narrow_lhs, ops, NULL))
7241 : return false;
7242 :
7243 : /* Walk the matched chain back to the 2N multiply and take narrow_type at
7244 : half its precision. */
7245 2477 : gimple *shift_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (trunc_stmt));
7246 2477 : gimple *mult_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (shift_stmt));
7247 2477 : unsigned int narrow_prec
7248 2477 : = TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (mult_stmt))) / 2;
7249 2477 : tree narrow_type = build_nonstandard_integer_type (narrow_prec, /*uns=*/1);
7250 2477 : scalar_int_mode narrow_mode;
7251 2477 : if (!is_a <scalar_int_mode> (TYPE_MODE (narrow_type), &narrow_mode))
7252 : return false;
7253 :
7254 : /* Lower only when the target cannot form the N-bit high part itself. */
7255 2477 : if (can_mult_highpart_p (narrow_mode, true))
7256 : return false;
7257 :
7258 2209 : location_t loc = gimple_location (trunc_stmt);
7259 2209 : gimple_seq seq = NULL;
7260 :
7261 : /* Split each operand into N-bit halves and combine. An operand that fits
7262 : N bits yields h == 0, so its cross term folds away; with both fitting
7263 : the combine is just a plain N-bit high part. */
7264 2209 : tree l1, h1, l2, h2;
7265 2209 : if (!long_mul_split_operand (&seq, loc, gimple_assign_rhs1 (mult_stmt),
7266 : narrow_type, &l1, &h1)
7267 2209 : || !long_mul_split_operand (&seq, loc, gimple_assign_rhs2 (mult_stmt),
7268 : narrow_type, &l2, &h2))
7269 : return false;
7270 2209 : tree hi = combine_long_mul_halves (&seq, loc, l1, h1, l2, h2, narrow_type);
7271 :
7272 : /* Merging the chain's truncation with a later user cast can retarget the
7273 : outer convert to any integral type, so convert the narrow result once
7274 : here (the high part is < 2^N, so the conversion preserves it). */
7275 2209 : gimple *result_stmt;
7276 2209 : tree lhs_type = TREE_TYPE (narrow_lhs);
7277 2209 : if (useless_type_conversion_p (lhs_type, narrow_type))
7278 2203 : result_stmt = gimple_build_assign (narrow_lhs, hi);
7279 : else
7280 6 : result_stmt = gimple_build_assign (narrow_lhs, NOP_EXPR, hi);
7281 2209 : gimple_set_location (result_stmt, loc);
7282 2209 : gimple_seq_add_stmt (&seq, result_stmt);
7283 :
7284 2209 : gsi_replace_with_seq (gsi, seq, true);
7285 :
7286 : /* Clean up the shift and the 2N mult now -- LTRANS runs no DCE between
7287 : widening_mul and expand, and a dead 2N mult would abort expand_mult.
7288 : Dead upstream (T_2N) casts, if any, are harmless NOP_EXPRs and land
7289 : with normal DCE. */
7290 2209 : if (has_zero_uses (gimple_assign_lhs (shift_stmt)))
7291 : {
7292 2201 : gimple_stmt_iterator dgsi = gsi_for_stmt (shift_stmt);
7293 2201 : gsi_remove (&dgsi, true);
7294 2201 : release_defs (shift_stmt);
7295 : }
7296 :
7297 : /* The mult is either dead (low half recomputed elsewhere) or now read only
7298 : for its low half. */
7299 2209 : gimple_stmt_iterator mgsi = gsi_for_stmt (mult_stmt);
7300 2209 : finish_long_mul_low_half (&mgsi);
7301 :
7302 2209 : if (dump_file && (dump_flags & TDF_DETAILS))
7303 4 : fprintf (dump_file, "Lowered long-mul high-part chain.\n");
7304 : return true;
7305 : }
7306 :
7307 : /* True when pass_optimize_widening_mul will run. Shared with the
7308 : forwprop long-multiply recognizer so its wide-chain emit stays
7309 : paired with the lowering that rescues an unsupported 2N shape.
7310 : The -Og pipeline (pass_all_optimizations_g) does not contain
7311 : pass_optimize_widening_mul at all, so -Og -fexpensive-optimizations
7312 : must not enable the emit: the unlowered 2N multiply would reach
7313 : expand as an unexpandable mode (e.g. OImode) and ICE.
7314 : -fdisable-tree-widening_mul is not observed. */
7315 :
7316 : bool
7317 1087111 : optimize_widening_mul_active_p (void)
7318 : {
7319 1087111 : return flag_expensive_optimizations && optimize && !optimize_debug;
7320 : }
7321 :
7322 : /* Find integer multiplications where the operands are extended from
7323 : smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR
7324 : or MULT_HIGHPART_EXPR where appropriate. */
7325 :
7326 : namespace {
7327 :
7328 : const pass_data pass_data_optimize_widening_mul =
7329 : {
7330 : GIMPLE_PASS, /* type */
7331 : "widening_mul", /* name */
7332 : OPTGROUP_NONE, /* optinfo_flags */
7333 : TV_TREE_WIDEN_MUL, /* tv_id */
7334 : PROP_ssa, /* properties_required */
7335 : 0, /* properties_provided */
7336 : 0, /* properties_destroyed */
7337 : 0, /* todo_flags_start */
7338 : TODO_update_ssa, /* todo_flags_finish */
7339 : };
7340 :
7341 : class pass_optimize_widening_mul : public gimple_opt_pass
7342 : {
7343 : public:
7344 302985 : pass_optimize_widening_mul (gcc::context *ctxt)
7345 605970 : : gimple_opt_pass (pass_data_optimize_widening_mul, ctxt)
7346 : {}
7347 :
7348 : /* opt_pass methods: */
7349 1068301 : bool gate (function *) final override
7350 : {
7351 1068301 : return optimize_widening_mul_active_p ();
7352 : }
7353 :
7354 : unsigned int execute (function *) final override;
7355 :
7356 : }; // class pass_optimize_widening_mul
7357 :
7358 : /* Walker class to perform the transformation in reverse dominance order. */
7359 :
7360 : class math_opts_dom_walker : public dom_walker
7361 : {
7362 : public:
7363 : /* Constructor, CFG_CHANGED is a pointer to a boolean flag that will be set
7364 : if walking modidifes the CFG. */
7365 :
7366 987745 : math_opts_dom_walker (bool *cfg_changed_p)
7367 1975490 : : dom_walker (CDI_DOMINATORS), m_last_result_set (),
7368 987745 : m_cfg_changed_p (cfg_changed_p) {}
7369 :
7370 : /* The actual actions performed in the walk. */
7371 :
7372 : void after_dom_children (basic_block) final override;
7373 :
7374 : /* Set of results of chains of multiply and add statement combinations that
7375 : were not transformed into FMAs because of active deferring. */
7376 : hash_set<tree> m_last_result_set;
7377 :
7378 : /* Pointer to a flag of the user that needs to be set if CFG has been
7379 : modified. */
7380 : bool *m_cfg_changed_p;
7381 : };
7382 :
7383 : void
7384 10544529 : math_opts_dom_walker::after_dom_children (basic_block bb)
7385 : {
7386 10544529 : gimple_stmt_iterator gsi;
7387 :
7388 10544529 : fma_deferring_state fma_state (param_avoid_fma_max_bits > 0
7389 10648471 : && param_widening_mul_defer_fma);
7390 :
7391 14882688 : for (gphi_iterator psi_next, psi = gsi_start_phis (bb); !gsi_end_p (psi);
7392 4338159 : psi = psi_next)
7393 : {
7394 4338159 : psi_next = psi;
7395 4338159 : gsi_next (&psi_next);
7396 :
7397 4338159 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
7398 4338159 : gphi *phi = psi.phi ();
7399 :
7400 4338159 : if (match_saturation_add (&gsi, phi)
7401 4338140 : || match_saturation_sub (&gsi, phi)
7402 4338092 : || match_saturation_trunc (&gsi, phi)
7403 4338092 : || match_saturation_mul (&gsi, phi)
7404 8676251 : || match_spaceship (&gsi, phi))
7405 218 : remove_phi_node (&psi, /* release_lhs_p */ false);
7406 : }
7407 :
7408 96649616 : for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
7409 : {
7410 86105087 : gimple *stmt = gsi_stmt (gsi);
7411 86105087 : enum tree_code code;
7412 :
7413 86105087 : if (auto assign = dyn_cast<gassign *> (stmt))
7414 : {
7415 22026302 : code = gimple_assign_rhs_code (assign);
7416 22026302 : switch (code)
7417 : {
7418 778100 : case MULT_EXPR:
7419 778100 : if (narrow_long_mul_low_half (&gsi))
7420 : break;
7421 778100 : if (!convert_mult_to_widen (assign, &gsi)
7422 769102 : && !convert_expand_mult_copysign (assign, &gsi)
7423 1547159 : && convert_mult_to_fma (assign,
7424 : gimple_assign_rhs1 (assign),
7425 : gimple_assign_rhs2 (assign),
7426 : &fma_state))
7427 : {
7428 16627 : gsi_remove (&gsi, true);
7429 16627 : release_defs (assign);
7430 16627 : continue;
7431 : }
7432 761473 : match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p);
7433 761473 : match_saturation_sub_with_assign (&gsi, as_a<gassign *> (stmt));
7434 761473 : break;
7435 :
7436 2368857 : case PLUS_EXPR:
7437 2368857 : if (match_saturation_add_with_assign (&gsi,
7438 : as_a<gassign *> (stmt)))
7439 : break;
7440 : /* fall-through */
7441 2685774 : case MINUS_EXPR:
7442 2685774 : if (!match_saturation_sub_with_assign (&gsi,
7443 : as_a<gassign *> (stmt))
7444 2685774 : && !convert_plusminus_to_widen (&gsi, stmt, code))
7445 : {
7446 2685652 : match_arith_overflow (&gsi, assign, code, m_cfg_changed_p);
7447 2685652 : if (gsi_stmt (gsi) == assign)
7448 2679693 : match_uaddc_usubc (&gsi, stmt, code);
7449 : }
7450 : break;
7451 :
7452 39608 : case BIT_NOT_EXPR:
7453 39608 : if (match_arith_overflow (&gsi, assign, code, m_cfg_changed_p))
7454 292 : continue;
7455 : break;
7456 :
7457 61735 : case TRUNC_MOD_EXPR:
7458 61735 : convert_to_divmod (as_a<gassign *> (stmt));
7459 61735 : break;
7460 :
7461 178721 : case RSHIFT_EXPR:
7462 178721 : convert_mult_to_highpart (as_a<gassign *> (stmt), &gsi);
7463 178721 : break;
7464 :
7465 186175 : case BIT_IOR_EXPR:
7466 186175 : match_unsigned_saturation_mul (&gsi, as_a<gassign *> (stmt));
7467 186175 : match_saturation_add_with_assign (&gsi, as_a<gassign *> (stmt));
7468 186175 : match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt));
7469 : /* fall-through */
7470 219865 : case BIT_XOR_EXPR:
7471 219865 : match_uaddc_usubc (&gsi, assign, code);
7472 219865 : break;
7473 :
7474 331838 : case EQ_EXPR:
7475 331838 : case NE_EXPR:
7476 331838 : case LE_EXPR:
7477 331838 : case GT_EXPR:
7478 331838 : match_single_bit_test (&gsi, assign);
7479 331838 : break;
7480 :
7481 362335 : case COND_EXPR:
7482 362335 : case BIT_AND_EXPR:
7483 362335 : match_saturation_sub_with_assign (&gsi, as_a<gassign *> (stmt));
7484 362335 : break;
7485 :
7486 2498393 : case NOP_EXPR:
7487 2498393 : match_unsigned_saturation_mul (&gsi, as_a<gassign *> (stmt));
7488 2498393 : match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt));
7489 2498393 : match_saturation_add_with_assign (&gsi, as_a<gassign *> (stmt));
7490 2498393 : match_saturation_sub_with_assign (&gsi, as_a<gassign *> (stmt));
7491 : /* fall-through */
7492 2498415 : case CONVERT_EXPR:
7493 : /* The long-multiply recognizer's high-part emit ends in an
7494 : outer convert. If the trailing cast+mult+shift+cast
7495 : chain has no expansion strategy at the 2N width, lower
7496 : the whole chain to a longhand high-part at narrow
7497 : precision. */
7498 2498415 : if (gsi_stmt (gsi) == assign
7499 2498415 : && lower_long_mul_high_chain (&gsi))
7500 2209 : continue;
7501 : break;
7502 :
7503 193 : default:;
7504 : }
7505 : }
7506 64078785 : else if (is_gimple_call (stmt))
7507 : {
7508 4940043 : switch (gimple_call_combined_fn (stmt))
7509 : {
7510 185 : case CFN_COND_MUL:
7511 185 : if (convert_mult_to_fma (stmt,
7512 : gimple_call_arg (stmt, 1),
7513 : gimple_call_arg (stmt, 2),
7514 : &fma_state,
7515 : gimple_call_arg (stmt, 0)))
7516 :
7517 : {
7518 140 : gsi_remove (&gsi, true);
7519 140 : release_defs (stmt);
7520 140 : continue;
7521 : }
7522 : break;
7523 :
7524 0 : case CFN_COND_LEN_MUL:
7525 0 : if (convert_mult_to_fma (stmt,
7526 : gimple_call_arg (stmt, 1),
7527 : gimple_call_arg (stmt, 2),
7528 : &fma_state,
7529 : gimple_call_arg (stmt, 0),
7530 : gimple_call_arg (stmt, 4),
7531 : gimple_call_arg (stmt, 5)))
7532 :
7533 : {
7534 0 : gsi_remove (&gsi, true);
7535 0 : release_defs (stmt);
7536 0 : continue;
7537 : }
7538 : break;
7539 :
7540 3748703 : case CFN_LAST:
7541 3748703 : cancel_fma_deferring (&fma_state);
7542 3748703 : break;
7543 :
7544 : default:
7545 : break;
7546 : }
7547 : }
7548 59138742 : else if (gimple_code (stmt) == GIMPLE_COND)
7549 : {
7550 4248801 : match_single_bit_test (&gsi, stmt);
7551 4248801 : optimize_spaceship (as_a <gcond *> (stmt));
7552 : }
7553 86085819 : gsi_next (&gsi);
7554 : }
7555 10544529 : if (fma_state.m_deferring_p
7556 7734109 : && fma_state.m_initial_phi)
7557 : {
7558 373 : gcc_checking_assert (fma_state.m_last_result);
7559 373 : if (!last_fma_candidate_feeds_initial_phi (&fma_state,
7560 : &m_last_result_set))
7561 272 : cancel_fma_deferring (&fma_state);
7562 : else
7563 101 : m_last_result_set.add (fma_state.m_last_result);
7564 : }
7565 10544529 : }
7566 :
7567 :
7568 : unsigned int
7569 987745 : pass_optimize_widening_mul::execute (function *fun)
7570 : {
7571 987745 : bool cfg_changed = false;
7572 :
7573 987745 : memset (&widen_mul_stats, 0, sizeof (widen_mul_stats));
7574 987745 : calculate_dominance_info (CDI_DOMINATORS);
7575 987745 : renumber_gimple_stmt_uids (cfun);
7576 :
7577 987745 : long_mul_phi_halves = new hash_map<tree, long_mul_halves>;
7578 :
7579 987745 : math_opts_dom_walker (&cfg_changed).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
7580 :
7581 : /* A 2N multiply the target cannot expand would abort expand_mult. Every
7582 : statement has been through the lowerings above, so one left here matched
7583 : none of them. */
7584 987745 : basic_block bb;
7585 10544529 : FOR_EACH_BB_FN (bb, fun)
7586 106343359 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
7587 : {
7588 87229791 : gimple *stmt = gsi_stmt (gsi);
7589 87229791 : gsi_next (&gsi);
7590 87229791 : narrow_long_mul_halves (stmt);
7591 : }
7592 :
7593 1975490 : delete long_mul_phi_halves;
7594 987745 : long_mul_phi_halves = NULL;
7595 :
7596 987745 : statistics_counter_event (fun, "widening multiplications inserted",
7597 : widen_mul_stats.widen_mults_inserted);
7598 987745 : statistics_counter_event (fun, "widening maccs inserted",
7599 : widen_mul_stats.maccs_inserted);
7600 987745 : statistics_counter_event (fun, "fused multiply-adds inserted",
7601 : widen_mul_stats.fmas_inserted);
7602 987745 : statistics_counter_event (fun, "divmod calls inserted",
7603 : widen_mul_stats.divmod_calls_inserted);
7604 987745 : statistics_counter_event (fun, "highpart multiplications inserted",
7605 : widen_mul_stats.highpart_mults_inserted);
7606 :
7607 987745 : return cfg_changed ? TODO_cleanup_cfg : 0;
7608 : }
7609 :
7610 : } // anon namespace
7611 :
7612 : gimple_opt_pass *
7613 302985 : make_pass_optimize_widening_mul (gcc::context *ctxt)
7614 : {
7615 302985 : return new pass_optimize_widening_mul (ctxt);
7616 : }
|